naudio / NAudio

Audio and MIDI library for .NET

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Recording both mic and speaker audio using WASAPI when combined returns audio that includes blanks in between

KANDURISHOURI opened this issue · comments

Hi,

I am trying to record both microphone audio and speaker audio simultaneously. WasapiLoopbackCapture for the speaker and WasapiCapture for the microphone are being used. Both are working perfectly fine in separate applications. But, when I am trying to integrate both in a single application, the recorded output introduces blanks in between which increases the length of the output.
Any solutions for this would be really helpful.

Below is the example code that I am trying:

public class Program
{
static void Main(string[] args)
{
string outputFileName = "output.wav";

    // Record both microphone and speaker audio
    RecordAudio(outputFileName);
}

static void RecordAudio(string outputFileName)
{
    using (var waveIn = new WasapiCapture())
    using (var waveOut = new WasapiLoopbackCapture())
    using (var writer = new WaveFileWriter(outputFileName, waveIn.WaveFormat))
    {
        waveIn.DataAvailable += (s, e) => writer.Write(e.Buffer, 0, e.BytesRecorded);
        waveOut.DataAvailable += (s, e) => writer.Write(e.Buffer, 0, e.BytesRecorded);

        waveIn.StartRecording();
        waveOut.StartRecording();

        Console.WriteLine("Recording... Press any key to stop.");
        Console.ReadLine();

        // Record for a certain duration (you can modify this logic)
        //Thread.Sleep(5000);

        waveIn.StopRecording();
        waveOut.StopRecording();
        writer.Dispose();
    }
}

}