1

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);

}
4
  • can you show us what you have tried so far? Commented Aug 13, 2019 at 11:36
  • added the code. Commented Aug 13, 2019 at 12:02
  • 1
    Is that all the code? You haven't shown where you created wavesource. We can only see where you've created wavesource1. Would also help if you describe what you're 'not getting'. Are you seeing errors, or ... ? Commented Aug 13, 2019 at 12:05
  • yes receiving same audio on both files Commented Aug 13, 2019 at 12:15

1 Answer 1

2

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);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.