NAudio playing multiple sounds together gives exception "All mixer inputs must have the same WaveFormat"

StackOverflow https://stackoverflow.com/questions/23352132

Pergunta

I am trying to play multiple sound files at the same time using NAudio MixingSampleProvider's AddMixerInput method. I am using the AudioPlaybackEngine found at this page: https://gist.github.com/markheath/8783999

However, when I tried to play different files, I get an exception while playing some sounds: "All mixer inputs must have the same WaveFormat" It is probably because the sound files I have do not have the same sample rate (I see some files have 7046 as sample rate, instead of 44100).

Is there a way to modify the CachedSound class so that the sample rates are all converted to 44100 when reading the files?

Thanks!

Foi útil?

Solução

You cant mix audio streams together without converting them to the same sample rate. In NAudio there are three ways of accessing a resampler:

  • WaveFormatConversionStream - this uses the ACM resampler component under the hood. Note that you may have issues resampling IEEE float with this one.
  • MediaFoundationResampler - this uses MediaFoundation. It allows you to adjust the resampling quality.
  • WdlResamplingSampleProvider - this is brand new in the latest NAudio, and offers fully managed resampling.

As well as matching sample rates, you also need matching channel counts. NAudio has various ways to turn mono into stereo and vice versa.

Outras dicas

For file playback, modify PlaySound:

public void PlaySound(string fileName)
{
    var input = new AudioFileReader(fileName);
    AddMixerInput(new WdlResamplingSampleProvider(input, mixer.WaveFormat.SampleRate));
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top