Reputation: 303
I am implementing a Media Player and using NAudio to play my files. Is there any way to increase the playback speed like (2X or 4X) while the sound is playing. Code samples will be appreciated.
Thanks in Advance Cheers.
Upvotes: 4
Views: 9050
Reputation: 2109
I've recently added variable speed playback to ispy - that uses naudio for playback. PlaybackRate is a double - set it to slow down or speed up audio:
if (WaveOutProvider != null)
{
if (Math.Abs(PlaybackRate - 1) > double.Epsilon)
{
//resample audio if playback speed changed
var newRate = Convert.ToInt32(_waveProvider.WaveFormat.SampleRate/PlaybackRate);
var wf = new WaveFormat(newRate, 16, _waveProvider.WaveFormat.Channels);
var resampleInputMemoryStream = new MemoryStream(data) {Position = 0};
WaveStream ws = new RawSourceWaveStream(resampleInputMemoryStream, _waveProvider.WaveFormat);
var wfcs = new WaveFormatConversionStream(wf,ws) {Position = 0};
var b = new byte[ws.WaveFormat.AverageBytesPerSecond];
int bo = wfcs.Read(b, 0, ws.WaveFormat.AverageBytesPerSecond);
while (bo > 0)
{
WaveOutProvider.AddSamples(b, 0, bo);
bo = wfcs.Read(b, 0, ws.WaveFormat.AverageBytesPerSecond);
}
wfcs.Dispose();
ws.Dispose();
}
else
{
WaveOutProvider.AddSamples(data, 0, data.Length);
}
}
Upvotes: 3
Reputation: 11
I have gone throughthe PracticeSharp they are implementing the Speed on Fly(while Audio Playing). but my requirement didn't match. If any simple solution to work on Speed on Fly please provide, for eample for volume the VolumeSampleProvider like this is there any class for Speed change.
Upvotes: 0
Reputation: 49482
NAudio does not include a ready-made component to change the speed of audio playback. However, it is possible if you create your own derived WaveStream / IWaveProvider and implement a speedup algorithm yourself. The simplest way to get a 2x or 4x speed increase is just to throw away samples. However, the quality will not be good (artefacts will be introduced), so it depends on your needs as to whether you can go with that option or not.
I have implemented variable playback speed in NAudio once myself but unfortunately I can't share the code here as it is not open source. Yuval Naveh, however has implemented variable playback speed as part of his PracticeSharp application, which uses NAudio, so you might want to check out how he has done it (I think he achieves it by wrapping SoundTouch).
Upvotes: 5