no recording devices on Windows 7 / Vista?

Sep 28, 2011 at 9:22 PM

I use NAudio for microphone data processing in one of my programs.  On Windows XP it  has always worked fine with the default audio device.  Trying to run the same program on Windows Vista and 7, I get a BadDeviceID exception.  Investigating further, it seems that the WaveIn.DeviceCount number is actually 0 on these newer versions of windows, despite having the sound drivers installed properly (I've tried it on two Win 7 desktops and 1 Win Vista laptop), where it is normally at least 1 on my XP machines.  I'm using NAudio 1.4.0.0 with Visual C# Express 2008, and my setup code looks like this:

In constructor:

waveInStream = new NAudio.Wave.WaveIn();
waveInStream.DataAvailable += waveIn_DataAvailable;
waveInStream.WaveFormat = new WaveFormat((int)samplingRate, 1);

Then I have a method that starts the recording:

waveInStream.StartRecording();

Any idea why I would be seeing 0 devices on newer versions of windows?  

Sep 29, 2011 at 4:49 PM
Edited Sep 29, 2011 at 4:49 PM

The problem seems to stem from Windows Vista / 7 disabling the microphone device by default, only enabling it when something is physically plugged in.  If something is plugged into the microphone port when starting the program, it actually opens and works fine.  But this program could be started before a mic is plugged in.  In this case I guess I could also check the number of devices and only start recording if it's > 0.  But, when there are 0 devices initially, is there some callback for when the device number increases, or would I need to start a timer to check it periodically?  Unplugging the line while recording also makes the recording system throw an exception... I'll have to find some way around this.  Any suggestions to help me through this?  

Oct 4, 2011 at 6:14 PM

I fixed the problem in a fork of the NAudio source:

http://naudio.codeplex.com/SourceControl/network/Forks/bobasaurus/recordingPullCableFix

This just changes part of the wavein callback code to cleanly stop recording if the recording device has a problem (like being disabled from pulling the cable on some Win 7 boxes).  It will call the RecordingStopped event when this happens.  I implemented this event handler in my main program's GUI controller, which, if it detects that WaveIn.DeviceCount is 0, will start a timer that will poll WaveIn.DeviceCount until it's back above 0 and restart the recording.  I hope this helps anyone else having the same issue.  

Coordinator
Oct 17, 2011 at 9:43 AM

thanks for this bobasaurus, I'll look at merging your fork in next time I get round to doing some work on NAudio