I am trying to record speaker audio as well as microphone audio in Windows, using the C# NAudio library. The output recording has some blanks introduced that increase in the recording time. The actual content is there but split apart.
I tried using WasapiLoopbackCapture for speaker audio and WasapiCapture for microphone. Individual applications are right on the spot. But, when integrated together to record simultaneously, the audio output is not as intended.
Below is the code that I tried.
public class Program
{
static void Main(string[] args)
{
// Set your desired output file name
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();
}
}
}
Objective: Combine audio from two input sources.
Current Approach: Currently, you are saving each recorded buffer to the same file.
Challenges with Realtime Mixing:
Achieving realtime audio mixing is complex. You must wait until you’ve received audio from both devices before committing to the final mixed file. Synchronization is critical to avoid audio going out of sync. Note that loopback capture won’t provide buffers if the system audio is inactive.
Simpler Alternative:
Record both inputs separately to separate files. After recording, mix the audio from these separate files.