Microphone input

Sep 20, 2009 at 5:04 PM

I'm attempting to use NAudio to record microphone input (for voice comms in a game). Following your tutorial I have this (just in a quick test class to find out how this all works):

 

static IWaveIn waveInStream;
static WaveFileWriter writer;
private static void Record()
{
     waveInStream = new WaveIn();
     waveInStream.WaveFormat = new WaveFormat();

     writer = new WaveFileWriter("Test.wav", waveInStream.WaveFormat);

     waveInStream.DataAvailable += waveInStream_DataAvailable;
     waveInStream.StartRecording();

     Console.WriteLine("recording");

     Console.ReadLine();

     waveInStream.StopRecording();
     waveInStream.Dispose();
     waveInStream = null;
     writer.Flush();
     writer.Close();
     writer = null;
}

private static void waveInStream_DataAvailable(object sender, WaveInEventArgs e)
{
     writer.WriteData(e.Buffer, 0, e.BytesRecorded);
}

 

Very simple, except this wave the "waveInStream_DataAvailable" method never gets called. If I change it to use a WasapiCapture instance, the DataAvailable method gets called, and it all seems to work, but nothing can play the file back!

 

Am I doing something wrong? Or is this a bug in NAudio?

Coordinator
Sep 20, 2009 at 9:17 PM

Hi there,

By default WaveIn uses a Window handle for callback. Since it looks like you are in a console application, nothing will process the message queue for the window it creates.

If you have the very latest NAudio code, you can call new WaveIn(WaveCallbackInfo.FunctionCallback()) and it should work for you. (older versions had a boolean property UseWindowCallbacks or something similar but I am bringing it into line with the way WaveOut works). Let me know if you encounter any problems.

Mark

Sep 20, 2009 at 9:32 PM

It works, that's excellent, thankyou very much!