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

recording synchronously from multiple microphones

Dec 28, 2011 at 11:46 AM

Hi, I have 2 identical USB microphones and I would like to start recording sound(with WaveIn) at the same time from both microphones. I tried to use 2 System.Threading.Thread’s with the WaveCallbackInfo.FunctionCallback() option when initiating the WaveIn class. Also I used 2 different device numbers. But the first recording(wav file) in the first thread I started, is a smaller and unrecognized file. But the second recording(wav file) in the second thread I started, records and plays the file perfectly. Does Naudio support multithreading or/and recording from multiple sources. If so, does anybody have any suggestions/examples for me to achieve my goal?

		Thread tWaveRecord = new Thread(StartRecording);
		tWaveRecord.IsBackground = true;

		Thread tWaveRecord2 = new Thread(StartRecording2);
		tWaveRecord2.IsBackground = true;

        private void StartRecording()
            waveIn = new WaveIn(WaveCallbackInfo.FunctionCallback());
            waveIn.DeviceNumber = 0;
            waveIn.WaveFormat = new WaveFormat(96000, 1);

            writer = new WaveFileWriter(outputFilename, waveIn.WaveFormat);

            waveIn.DataAvailable += new EventHandler<WaveInEventArgs>(waveIn_DataAvailable);
            waveIn.RecordingStopped += new EventHandler(waveIn_RecordingStopped);
            Debug.WriteLine("Start Recording");

        private void StartRecording2()
            waveIn2 = new WaveIn(WaveCallbackInfo.FunctionCallback());
            waveIn2.DeviceNumber = 3;
            waveIn2.WaveFormat = new WaveFormat(96000, 1);

            writer2 = new WaveFileWriter(outputFilename2, waveIn2.WaveFormat);

            waveIn2.DataAvailable += new EventHandler<WaveInEventArgs>(waveIn_DataAvailable2);
            waveIn2.RecordingStopped += new EventHandler(waveIn_RecordingStopped2);
            Debug.WriteLine("Start Recording 2");

Jan 3, 2012 at 2:07 PM

it ought to work. Have you tried it using window callbacks instead of function callbacks?

Jan 5, 2012 at 10:03 AM

Window callbacks generated some other errors. What I have learned from MSDN for System.IO.Stream is "public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe". WaveFileWriter is derived from System.IO.Stream so I am not using WaveFileWriter.

Jan 5, 2012 at 10:12 AM

WaveFileWriter is perfectly thread safe so long as you write to it from the same thread. This will definitely be the case with windowed callbacks, and also I expect function callbacks will be the same. There is also no need to start new threads for your StartRecording functions.