Is NAudio suitable for a game?

Aug 17, 2009 at 3:35 PM

So I'm making a small game and looking for a sound library, NAudio looks pretty good, but I haven't been able to get it to work very well with multiple sounds playing over and over or different sounds playing at the same time. 
I don't really understand how it's supposed to be used in a situation like mine, should I use a WaveMixerStream and add each sound as an InputStream, or should I just do Init and Play on my out IWavePlayer multiple times, or am I missing something?

Editor
Aug 17, 2009 at 10:24 PM

Hi Myris,

NAudio certainly would be a good choice.

Have you reviewed the NAudio tutorials? Playing multiple sounds together in real time is covered in tutorial 2 http://opensebj.blogspot.com/2009/02/naudio-tutorial-2-mixing-multiple-wave.html

Cheers,

Sebastian

Aug 18, 2009 at 11:18 AM
Edited Aug 18, 2009 at 11:19 AM

Thanks for you reply,

Yes, I did read that tutorial, but I did not get it to work, it's just silent.

I initialize with:

IWavePlayer outDevice = new WaveOut(0, 300, null);
WaveMixerStream32 mixer = new WaveMixerStream32();
mixer.AutoStop = false;

outDevice.Init(mixer);
outDevice.Play();

and then when I do:

mixer.AddInputStream(SoundStream);

nothing happends.

 

If I instead do:

outDevice.Init(SoundStream);
outDevice.Play();

the sounds play, so the sound stream seems to be correct.

 

 

Editor
Aug 19, 2009 at 9:22 AM

Hi Myris,

Assuming that SoundStream is a WaveChannel32 instance, which has been setup correctly and a .wav file that you have specified earlier in your code, has been loaded then I would suggest you try adding this:

SoundStream.Position = 0;

When you want the sample to start playing.

If that doesn't work, please post up a full copy of the example code that your having difficulty with. It sounds like one of the setup items has been missed.

Cheers,

Sebastian

 

 

Jun 10, 2013 at 6:16 PM
Hi all,

I am attempting to use NAudio for the same purpose and I seem to be experiencing the same problem.

Here is my current implementation (VB):
Private waveOutDevice As IWavePlayer
Private mixer As WaveMixerStream32

mixer = New WaveMixerStream32()
mixer.AutoStop = False
waveOutDevice = New DirectSoundOut
waveOutDevice.Init(mixer)
waveOutDevice.Play()

Dim mainOutputStream As WaveStream = CreateStream(theFile)
mainOutputStream.Position = 0
mixer.AddInputStream(mainOutputStream)
From my understanding of the mixer tutorial, this code should work and result on the audio file being played. Unfortunately it does not. I know the sound stream is "correct" because if I feed it directly to the waveOutDevice directly it plays OK.

Can anyone suggest a correction?

Thanks!
Coordinator
Jun 11, 2013 at 11:00 AM
I'd try with MixingSampleProvider instead of WaveMixerStream32 which was not really designed for adding inputs on the fly.
Jun 12, 2013 at 12:47 PM
Thank you. I will try to implement it this way.