我对此很陌生,所以我会尽力解释。我想计算音频电平超过某个电平的次数。我已经找到/创建了一些代码,可以检测级别是否超过某个阈值,但无法弄清楚如何计算它可靠地超过该阈值的次数。声音/噪音是由连接到麦克风的开关产生的,每次开关都会产生噪音。我需要使用某种过滤吗?
C#.net Nadio 库史蒂夫。
public void StartListening()
{
WaveIn waveInStream = new WaveIn();
waveInStream.BufferMilliseconds = 500;
waveInStream.DataAvailable += new EventHandler<WaveInEventArgs>(waveInStream_DataAvailable);
waveInStream.StartRecording();
}
//Handler for the sound listener
private void waveInStream_DataAvailable(object sender, WaveInEventArgs e)
{
bool result = ProcessData(e);
if (result)
{
intCounter++;
label1.Text = intCounter.ToString();
}
else
{
//no peak in sound
}
}
//calculate the sound level based on the AudioThresh
private bool ProcessData(WaveInEventArgs e)
{
bool result = false;
bool Tr = false;
double Sum2 = 0;
int Count = e.BytesRecorded / 2;
for (int index = 0; index < e.BytesRecorded; index += 2)
{
double Tmp = (short)((e.Buffer[index + 1] << 8) | e.Buffer[index + 0]);
Tmp /= 32768.0;
Sum2 += Tmp * Tmp;
if (Tmp > AudioThresh)
Tr = true;
}
Sum2 /= Count;
// If the Mean-Square is greater than a threshold, set a flag to indicate that noise has happened
if (Sum2 > AudioThresh)
{
result = true;
}
else
{
result = false;
}
return result;
}