Repeated Start/StopRecording does not work

Jul 19, 2012 at 8:01 PM

When I start/stop recordings, while a playback is running at the same time, NAudio hangs after some time. Down below you can find a small demo that shows the effect. The test-loop is expected to loop-back recorded audio to the output device forever. But after some random record sequences no recording will take place any more and the StopRecording method call hangs.

Any idea what's wrong here? I tested with a few days old NAudio codebase, but had the same effect with the official NAudio release 1.5 too.

Norbert

 

using System;
using System.Threading;
using NAudio.Wave;

namespace NAudioRecTest
{
    class Program
    {
        static BufferedWaveProvider bufferedWaveProvider = new BufferedWaveProvider(new WaveFormat(8000, 16, 1));

        static void Main(string[] args)
        {
            IWaveIn waveIn = new WaveInEvent();
            waveIn.DataAvailable += new EventHandler<WaveInEventArgs>(waveIn_DataAvailable);

            IWavePlayer waveOut = new WaveOutEvent();
            waveOut.Init(bufferedWaveProvider);
            waveOut.Play();

            while (true)
            {
                Console.Out.WriteLine("Start recording.");
                waveIn.StartRecording();

                Thread.Sleep(1000);
                
                Console.Out.WriteLine("Stop recording.");
                waveIn.StopRecording();

                Thread.Sleep(500);
            }
        }

        static void waveIn_DataAvailable(object sender, WaveInEventArgs e)
        {
            Console.Out.WriteLine("Data available");
            bufferedWaveProvider.AddSamples(e.Buffer, 0, e.BytesRecorded);
        }
    }
}

Coordinator
Jul 30, 2012 at 1:40 PM

are you able to identify the line in the NAudio code that is hanging?

Jul 30, 2012 at 6:05 PM

File: WaveInEvent.cs

Method: StopRecording()

Line: 188

MmException.Try(WaveInterop.waveInStop(waveInHandle), "waveInStop");

Build: naudio_b3a412ad55a8

Coordinator
Aug 6, 2012 at 1:59 PM

what soundcard are you using? That method should not hang with good soundcard drivers

Aug 8, 2012 at 5:28 PM

I have tested on a (Parallels) Windows 7 virtual machine on a MacBook Pro. I have not observed any other audio issues with this setup until now.

In the meantime I have worked around this issue by separating recording and playback into different processes.

Coordinator
Aug 9, 2012 at 1:21 PM

OK, might be something strange going on with the emulated soundcard then. You ought to be able to do both at once.