I'm writing a game and I'd like to use NAudio to add sound to it. I followed a few examples and was rather pleased with NAudio's API and performance.
I ran into the following issue: NAudio will not play short audio files. I've used WaveChannel32.Length to get the following numbers.
Files with length 755712 or less will not play.
Files with length 5935104 do play.
I'm not sure where the exact limit is. There are no exceptions thrown. I was hoping to use NAudio to play files of about 1 second long. Is anyone else having this issue? Is there a fix? Do I need to find a different library?
Comments: Here's the NAudio code I'm calling.
Initializing:
WaveMixerStream32 mixer = new WaveMixerStream32();
mixer.AutoStop = false;
IWavePlayer waveOutDevice = new WaveOut();
waveOutDevice.Init(mixer);
waveOutDevice.Play();
WaveChannel32 sample = CreateInputStream(filename);
Loading functions: (From tutorial, really :b)
private static WaveChannel32 CreateInputStream(string fileName)
{
if (!File.Exists(fileName))
{
throw new InvalidOperationException("Cannot find audio file: " + fileName);
}
WaveChannel32 inputStream;
if (fileName.EndsWith(".mp3"))
{
WaveStream mp3Reader = new Mp3FileReader(fileName);
inputStream = new WaveChannel32(mp3Reader);
}
else if (fileName.EndsWith(".wav"))
{
WaveStream mp3Reader = new WaveFileReader(fileName);
inputStream = new WaveChannel32(mp3Reader);
}
else
{
throw new InvalidOperationException("Unsupported extension");
}
return inputStream;
}
Play sound:
mixer.AddInputStream(sample);
Stop sound:
mixer.RemoveInputStream(sample);
I don't have an idea how to make an IWaveProvider that adds trailing silence. But instead I've added trailing silence in my audio files and tried out stuff that way. I found some rather curious things.
If I add 30 seconds of trailing silence to a 1 second effect sound, it won't play it.
If I add 10 seconds of silence before the 1 second effect and 30 seconds after, it won't play it.
If I put three 1 second effects in sequence, separated by 5 seconds and add 30 seconds after the last one, I will hear only the last 2 effects in the sequence.
It seems like silence and short sounds aren't enough to be taken serious by the library. It feels like the first effect in the sequence is just lost trying to convince the system that there's actual sound in the file.
Does anyone have a good idea?
I can work around this issue by making a single sound file of all my effects and adding the first effect twice. It's ugly, but it should work.