I'm pretty new to this so I'll try to explain as best I can. I want to count the number of times the audio level goes above a certain level. I have found/created some code which can detect if the level has gone above a certain threshold, but can't figure out how to count the number of times it has risen above that threshold reliably. the sound/noise is being created by a switch which is connected to the microphone, every time it switches a noise is created by the switching. Do I need to use some sort of filtering?
C#.net Naudio Library Steve.
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;
}