同时从两个输入设备录制音频并保存为 wave 文件?

Record audio from two input device at the same time and save into wave file?

我正在尝试同时在两个不同的设备上录制音频,文件的输出应该保存在一个 wave 文件中

我尝试使用 NAudio 解决问题,如下所示,但我仍然没有得到它

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

这是第一个波源

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

}

我不熟悉 naudio,但是...

看起来您的两个波源都在使用相同的 dataAvailable 事件处理程序。这意味着无论 source 还是 source1 接收音频,它最终都会将数据写入同一个文件。

解决这个问题的一种方法是将它们分开,这样每个都有自己的事件处理程序,然后每个都写入一个唯一的文件

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

然后你的事件处理程序:

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