title: Detect wheter a sound buffer is filled with data or not
body: <p>I'm recording using WaveAPI, and I want to detect after I'm finish recording the data, if the buffer has sound in it, or it have recorded nothing (just the void of the room).</p>
  
  <p>I wrote a function that gets an average of the absolute value of the buffer, and it works "Ok", but it has many problems :</p>
  
  <p>1) I've detected, that when it is void, the average is ~860, and when I'm talking, it is ~875, which is barely a different at all. How can it be ? I'm recording for 1 sec.</p>
  
  <p>2) Some times, I see that the average is ~860, some times ~500, some days even ~400. Why is it changing every time? I mean, shouldent it be the same, as in all the times it captures the void and there isnt a change ?</p>
  
  <p>Here is the function I wrote :</p>
  
  <pre><code>bool isEmpty(short int *wave)
  {
  int avg = 0;
  
  for (int i = 0 ; i &lt; NUMPTS ; i++)
  {
      if (wave[i] &lt; 0)
          avg = avg + (wave[i]) * -1;
  
      else
          avg = avg + (wave[i]);
  }
  
  avg = avg / NUMPTS;
  
  if (avg &gt; avg_voice)
      return false;
  
  return true;
  }
  </code></pre>
  
  <p>This function isnt good enough, as it isnt allways right, and I have to constatly change the <code>avg_voice</code> into something else, and some times the buffer is like only 10 points in the average higher with sound, than with void, which is very hard to detect wheter it has voice in it or not....</p>
  
  <p>So what can I do ? How can I improve it ? Maybe theres an option for that when I record the voice, and fill in all the <code>WAVEFORMATEX</code>and <code>WAVEHDR</code> settings?</p>
  
  <p>Thanks!</p>
  
  <p>Edit: <code>wave</code> is a short int array that contains <code>8000</code> cells, and stors the voice inside, and looks like this (example) :
  <code>wave[0] = -123;</code>
  <code>wave[1] = -205;</code>
  <code>wave[2] = -212'</code></p>
  
  <p>and such...</p>
  
  <p>Second Edit :
  I'm recording the data like this :</p>
  
  <pre><code>void StartRecord()
  {
  short int *waveIn = new short int[NUMPTS];
  
  HWAVEIN hWaveIn;
  WAVEHDR WaveInHdr;
  MMRESULT result;
  HWAVEOUT hWaveOut;
  
  WAVEFORMATEX pFormat;
  pFormat.wFormatTag = WAVE_FORMAT_PCM;
  pFormat.nChannels = 1;
  pFormat.nSamplesPerSec = sampleRate;
  pFormat.nAvgBytesPerSec = 2 * sampleRate;
  pFormat.nBlockAlign = 2;
  pFormat.wBitsPerSample = 16;
  pFormat.cbSize = 0;
  
  result = waveInOpen(&amp;hWaveIn, WAVE_MAPPER, &amp;pFormat, 0, 0, WAVE_FORMAT_DIRECT);
  
  if(result)
  {
      char fault[256];
      waveInGetErrorTextA(result, fault, 256);
      MessageBoxA(NULL, fault, "Failed to open waveform input device.", MB_OK | MB_ICONEXCLAMATION);
      return;
  }
  
  WaveInHdr.lpData = (LPSTR)waveIn;
  WaveInHdr.dwBufferLength = 2 * NUMPTS;
  WaveInHdr.dwBytesRecorded = 0;
  WaveInHdr.dwUser = 0;
  WaveInHdr.dwFlags = 0;
  WaveInHdr.dwLoops = 0;
  
  while (true)
  {
      waveInPrepareHeader(hWaveIn, &amp;WaveInHdr, sizeof(WAVEHDR));
      result = waveInAddBuffer(hWaveIn, &amp;WaveInHdr, sizeof(WAVEHDR));
  
      result = waveInStart(hWaveIn);
      if(result)
      {
          MessageBoxA(NULL, "Failed to start recording", NULL, MB_OK | MB_ICONEXCLAMATION);
          return;
      }
  
      // Wait until finished recording 
      Sleep(seconds * 1000); //Sleep for as long as there was recorded
      waveInUnprepareHeader(hWaveIn, &amp;WaveInHdr, sizeof(WAVEHDR));
  
      if (isEmpty(waveIn)) // Checks here
                   .....
  }
   }
  </code></pre>