Search code examples
c#naudio

How to chain together multiple NAudio ISampleProvider Effects


I have some DSP effects coded in the ISampleProvider model. To apply one effect I do this and it works fine.

string filename = "C:\myaudio.mp3";
MediaFoundationReader mediaFileReader = new MediaFoundationReader(filename);
ISampleProvider sampProvider = mediaFileReader.ToSampleProvider();
ReverbSampleProvider reverbSamplr = new ReverbSampleProvider(sampProvider);
IWavePlayer waveOutDevice.Init(reverbSamplr);
waveOutDevice.Play();

How can I apply multiple effects to the same input file simultaneously? For example, if i have a Reverb effect and Distortion effect providers, how can I chain them together to apply them at the same time to one input file?


Solution

  • Effects can be chained together by passing one as the "source" for the next. So if you wanted your audio to go first through a reverb, and then distortion, you might do something like this, passing the original audio into the Reverb effect, the output of the reverb into the distortion effect and then sending the distortion to the waveOut device.

    var reverb = new ReverbSampleProvider(sampProvider);
    var distortion = new DistortionSampleProvider(reverb);
    waveOutDevice.Init(distortion);
    

    (n.b. NAudio does not come with built in reverb/distortion effects - you must make these yourself or source them from elsewhere)