Firstly, I predict that the buffer hasn't been filled by the time you analyze it. Rather than a simple sleep, you should poll for WaveInHdr.dwFlags for the WHDR_DONE bit to be set.
result = waveInStart(hWaveIn);
if(result)
{
MessageBoxA(NULL, "Failed to start recording", NULL, MB_OK | MB_ICONEXCLAMATION);
return;
}
// Wait until finished recording
while ((WaveInHdr.dwFlags & WHDR_DONE) == 0)
Sleep(100);
Secondly, I'd suggest a better method of measuring loudness. RMS Perhaps:
double Rms(short int *wave, int length)
{
double sumSquared = 0;
double scaleShortToDouble = 1.0/0x8000;
for (int i = 0 ; i < length; i++)
{
double s = wave[i] * scaleShortToDouble;
sumSquared += s * s;
}
return sqrt(2) * sqrt(sumSquared/length);
}
I've converted the shorts to doubles in the range of -1.0 to 1.0 because its easier to compute with. The extra sqrt(2) is going to scale the result so that if you were to put a sine wave into the A/D converter so that a full scale digital sine comes out (-32768,32767), the Rms result will be 1.0.
With that done, you can now convert the Rms value to dB and you'll have a number that is referred to as dBFS and is commonly used when talking about digital levels.
The conversion is: dBFS = 20*log10(rms)
and roughly:
- 0 dBFS = 1.0`
- -6 dBFS = 0.5
- -12 dBFS = 0.25
each halving of input level is another -6 dBFS down.
It also happens that each halving of the input signal is going to require one less bit of the A/D converter. Since you have a 16 bit signal, you're theoretical noise floor is going to be at around -96 dBFS. In practice though, since you have a mic hooked up, it's going to be somewhat higher than that - depending in large part upon the quality of your setup. And that's where you're going to need to experiment.