Reputation: 33
I'm trying to record audio on two different devices at the same time and the output of the file should be saved in a wave file
Using NAudio I tried to solve the problem as shown below, but still I'm not getting it
WaveInEvent waveSource1 = new WaveInEvent();
waveSource1.DeviceNumber = DeviceID1;
waveSource1.WaveFormat = new WaveFormat(44100, 2);
waveSource1.DataAvailable += new EventHandler<WaveInEventArgs>(waveSource_DataAvailable);
string tempFile1 = (@"C:\Users\Nirmalkumar\Desktop\speech1.wav");
waveFile1 = new WaveFileWriter(tempFile1, waveSource1.WaveFormat);
waveSource.StartRecording();
waveSource1.StartRecording();
Console.Beep();
int milliseconds = 5000;
Thread.Sleep(milliseconds);
waveSource.StopRecording();
waveSource1.StopRecording();
this is first wavesource
WaveInEvent waveSource = new WaveInEvent();
waveSource.DeviceNumber = DeviceID;
waveSource.WaveFormat = new WaveFormat(44100, 16, 2);
waveSource.DataAvailable += new EventHandler<WaveInEventArgs>(waveSource_DataAvailable);
string tempFile = (@"C:\Users\Nirmalkumar\Desktop\speech.wav");
waveFile = new WaveFileWriter(tempFile, waveSource.WaveFormat);
static void waveSource_DataAvailable(object sender, WaveInEventArgs e)
{
waveFile.Write(e.Buffer, 0, e.BytesRecorded);
}
Upvotes: 1
Views: 432
Reputation: 14021
I'm not familiar with naudio, but...
It looks like both your wavesources are using the same dataAvailable
event handler. That means no matter whether source
or source1
receives audio it will end up writing the data to the same file.
One way to fix this is to separate them out, so each has its own event handler, and each then writes to a unique file
WaveInEvent waveSource = new WaveInEvent();
...
waveSource.DataAvailable += new EventHandler<WaveInEventArgs>(waveSource_DataAvailable);
string tempFile = (@"C:\Users\Nirmalkumar\Desktop\speech.wav");
waveFile = new WaveFileWriter(tempFile, waveSource.WaveFormat);
WaveInEvent waveSource1 = new WaveInEvent();
...
waveSource1.DataAvailable += new EventHandler<WaveInEventArgs>(waveSource1_DataAvailable);
string tempFile1 = (@"C:\Users\Nirmalkumar\Desktop\speech1.wav");
waveFile1 = new WaveFileWriter(tempFile1, waveSource1.WaveFormat);
Then your event handlers:
static void waveSource_DataAvailable(object sender, WaveInEventArgs e)
{
waveFile.Write(e.Buffer, 0, e.BytesRecorded);
}
static void waveSource1_DataAvailable(object sender, WaveInEventArgs e)
{
waveFile1.Write(e.Buffer, 0, e.BytesRecorded);
}
Upvotes: 2