naudio / NAudio

Audio and MIDI library for .NET
MIT License
5.37k stars 1.09k forks source link

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

Open KANDURISHOURI opened 5 months ago

KANDURISHOURI commented 5 months ago

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

}

markheath commented 4 months ago

have briefly answered your question on stack overflow: https://stackoverflow.com/questions/77859554/simultaneous-audio-recording-of-speaker-and-microphone-in-c-sharp/77987622#77987622