This project has moved and is read-only. For the latest updates, please go here.

Detect which input device has audio

Aug 25, 2012 at 12:19 AM

Hello,

I'm writing a program that reads from line-in device. However, some computers only have a mic port. Moreover, if there are two ports, a person may by mistake plug into the mic port instead of line-in port. I want my program to be resilient and read wave data from the device where there is music data coming in. How can I do that ?  I guess I need to do silence detection. How would I do that ? This is my sample code:

        
private void on_data(object sender, NAudio.Wave.WaveInEventArgs e) {
     if (waveWriter == null) return;
     waveWriter.WriteData(e.Buffer, 0, e.BytesRecorded);
     waveWriter.Flush();
}

Somewhere in this code I have to do silence detection.

Is there any other way to solve this, without doing silence detection? Does NAudio have some filters that I could use out of the box?

Aug 27, 2012 at 9:50 PM

this is a similar question to this one. The basic answer is that you would analyse the audio in short blocks, calculating a decibel level. You probably shouldn't detect for perfect silence (which would be all samples zero), but you could check that a block of audio for say 20ms didn't go over a set threshold.