抛砖引玉,贴一个计算声音大小的算法。
void CWaveOut::UpdateLevel(PBYTE pBuf, DWORD dwLen)
{
if (m_wfx.nChannels == 1 && m_wfx.wBitsPerSample == 16)
{
short* pWave = (short*)pBuf;
int nSmpl = dwLen / sizeof(short);
short max_sample = -32768;
short min_sample = 32767;
for (int i = 0; i < nSmpl; i++)
{
if (pWave[i] < min_sample)
{
min_sample = pWave[i];
}
if (pWave[i] > max_sample)
{
max_sample = pWave[i];
}
}
short level = max_sample - min_sample;
if (level < 0)
m_dwLevel = level * -1;
else
m_dwLevel = level;
TRACE1("WaveOut level %d/n", m_dwLevel);
}
}