Monday, March 23, 2009

NAudio Tutorials - A minor note

In the 2 & 3 NAudio tutorials, you may have noticed that sample playback seems fast, almost twice the speed of the sample playback as you would probably otherwise expect. I've traced this issue down and it seems to stem from how and when additional samples are added to the mixer and when the wave device is initialised.

The code in tutorial 2 & 3 followed this similar line:

//Setup the Mixer
mixer = new WaveMixerStream32();
mixer.AutoStop = false;
if (waveOutDevice == null)
     //waveOutDevice = new AsioOut();
     waveOutDevice = new WaveOut(0, 300, false);

And after the sample was loaded we add it to the mixer.


However adding the sample to the mixer, which in turn has been used when the waveOutDevice has been initiated seems to be the cause of this issue.

If the last line, when the Mixer.AddInputStream(Sample) is replaced with the following code, then playback is completed at the normal, expected speed:

// Need to dispose the waveOutDevice - if it is created before the sample has been added
// to the mixer then sample playback is undertaken at twice the speed for some reason
// Add the stream to the mixer
//Re-initalise the waveOutDevice and play back sound

Try that out and give me a yell if you have any problems..

Next NAudio tutorial instalment coming soon; Reversed Sample Playback.