using System; using System.Linq; using NAudio.Dsp; namespace NAudio.Wave.SampleProviders { /// /// Fully managed resampling sample provider, based on the WDL Resampler /// public class WdlResamplingSampleProvider : ISampleProvider { private readonly WdlResampler resampler; private readonly WaveFormat outFormat; private readonly ISampleProvider source; private readonly int channels; /// /// Constructs a new resampler /// /// Source to resample /// Desired output sample rate public WdlResamplingSampleProvider(ISampleProvider source, int newSampleRate) { channels = source.WaveFormat.Channels; outFormat = WaveFormat.CreateIeeeFloatWaveFormat(newSampleRate, channels); this.source = source; resampler = new WdlResampler(); resampler.SetMode(true, 2, false); resampler.SetFilterParms(); resampler.SetFeedMode(false); // output driven resampler.SetRates(source.WaveFormat.SampleRate, newSampleRate); } /// /// Reads from this sample provider /// public int Read(float[] buffer, int offset, int count) { float[] inBuffer; int inBufferOffset; int framesRequested = count / channels; int inNeeded = resampler.ResamplePrepare(framesRequested, outFormat.Channels, out inBuffer, out inBufferOffset); int inAvailable = source.Read(inBuffer, inBufferOffset, inNeeded * channels) / channels; int outAvailable = resampler.ResampleOut(buffer, offset, inAvailable, framesRequested, channels); return outAvailable * channels; } /// /// Output WaveFormat /// public WaveFormat WaveFormat { get { return outFormat; } } } }