Search code examples
c#audionaudio

Mix two audio files using NAudio


I need to make noise on one audio file. I have the first audio (data) and the second audio (noise). How to put noise on the first audio so that it is looped. Or, if the first audio is shorter than the audio noise, then the result was the duration of the first audio.

I gave an example of their tutorial. But there is simply an overlay of one file on another. Without the possibility of looping and length optimization

using(var reader1 = new AudioFileReader("file1.wav"))
using(var reader2 = new AudioFileReader("file2.wav"))
{
    var mixer = new MixingSampleProvider(new[] { reader1, reader2 });
    WaveFileWriter.CreateWaveFile16("mixed.wav", mixer);
}

Solution

  • You could use a looping technique like the one I describe here to loop the shorter file (would then need to follow that with a ToSampleProvider). And then use the Take extension method on your MixingSampleProvider to ensure you only read the duration of the longer file, so you don't create an infinitely large WAV file.