This project has moved. For the latest updates, please go here.

WaveIn DataAvailable Event

Jun 30, 2010 at 4:09 AM

Mark,

Great work with NAudio.  I've been looking around for a solution like this for a while and finally found this.

I'm trying to capture sound from the sound card, however I can't get this to work correctly.  Here is my code:

 

        WaveFileWriter writer;
        WaveIn waveInStream;

        private void formLoad(object sender, EventArgs e)
        {
            waveInStream = new WaveIn();

            WaveFormat format = new WaveFormat(44100, 2);
            waveInStream.WaveFormat = format;
            writer = new WaveFileWriter("myfile.wav");

            waveInStream.DataAvailable += new EventHandler(waveInStream_DataAvailable);
        }

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

        private void button1_Click(object sender, EventArgs e)
        {
            waveInStream.StartRecording();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            waveInStream.StopRecording();
            waveInStream.Dispose();
            waveInStream = null;
            writer.Close();
            writer = null;
        }

After tracing through this, I see the DataAvailable event is never being fired and the wav file is never being written and I can't figure out why.  Also, I'm getting a NullReferenceException on writer.close() even though it should be instantiated.

Also, I see that the WaveIn class has a constructor for a WindowHandle - is this to specify a window so that only sound from that window is captured?

 

Thanks in advance,

Jon

Coordinator
Jun 30, 2010 at 7:19 AM

are you sure that formLoad is being called?

you shouldn't need to pass in a Window handle. If you look at the source code for NAudio demo, you will see an example of how to use recording which might help

Mark

Jun 30, 2010 at 9:27 PM

Yes, formLoad is being called.

Anyway, I got the demo to NAudio demo to work, however it is capturing my microphone input.  I was expecting that it would capture my sound output, and furthermore be able to capture sound output from a specific Window handle.

I know I've read that only some drivers have stereo mix as a device.  Does NAudio work around that problem, or does the driver have to have a stereo mix in order to capture sound output.

Ultimately, I am trying to capture sound output from a game window that is running.  Is this possible using NAudio?

Thanks much,

Jon

Coordinator
Jul 12, 2010 at 10:27 AM
you are at the mercy of the sound card driver writers when using WaveIn. You certainly can't ask it to record just the output from a particular application though. I believe that it is possible to use WASAPI to capture the output from specific applications, but I haven't worked out how to do this Mark