Fading to to 50%

Dec 12, 2012 at 4:51 PM


I am working on a project where i use Naudio to merge 2 mp3 files, the second mp3 file is inserted 20 seconds into the first using the offsetsampleprovider to add 20 seconds silence before using wavemixerstream32 to merge the 2 streams.

I would like the first file to fade to 50% of the volume at ~18 secs, keeping the volume at 50% until 25 secs and the fading back to 100%. I am saving the result to a file. the file is totally 5 mins.

How can I accomplish the fading part using Naudio?



Dec 12, 2012 at 8:08 PM

The FadeInOutSampleProvider is close to what you want but not exactly right. You could copy the source code and modify it to fade to just 50%. I guess a configurable fade-out value would be a good additional feature for this class.

Dec 12, 2012 at 8:14 PM

Thanks, I will try it out!


Dec 13, 2012 at 8:52 PM


Thanks for pointing me in the right direction, I ended up rewriting the FadeInOutSampleProvider, fading down to 50% could be done changing the mupliplier and fading up in the middle of the track by adding a few more states and checking the total time converted to samples.

Fading down: 1.0F - 0.5F * (fadeSamplePosition / CSng(fadeSampleCount))

Fading up:  0.5F + 0.5F * (fadeSamplePosition / CSng(fadeSampleCount))

Though I have a problem knowing how to get the result into the mixer.

Dim wave_song As WaveStream = New AudioFileReader("c:\samples\song.mp3")
Dim song_isample As ISampleProvider = New WaveToSampleProvider(wave_song)
Dim fader As myFadeInOutSampleProvider
fader = New myFadeInOutSampleProvider(song_isample)
fader.setfade(5000, 20000) 

Dim waveprovider As SampleToWaveProvider = New SampleToWaveProvider(fader)

Dim mixer As WaveMixerStream32
mixer = New WaveMixerStream32()
' Doesnt work
mixer.AddInputStream(wave2) ' From above snippet

' Works
WaveFileWriter.CreateWaveFile("c:\samples\test2.wav", waveprovider)


How can I convert back to a wavestream from my fader Isampleprovider so i can put it in the mixer.



Dec 13, 2012 at 9:29 PM

use MixingSampleProvider instead of WaveMixerStream32. Then use sampleToWaveProvider at the very end (probably you want SampleToWaveProvider16).

Jun 27, 2013 at 3:45 PM
Hello Karl,
I have a situation nearly identical to yours , though it uses wav files to start with.
I would be really grateful if you could share your code in which you have made changes to the fadeinOut by adding states for partial fading in or Out and changing the fade start or fade stop points.