This project has moved and is read-only. For the latest updates, please go here.

failed to play the following sound file in naudiodemo

Jan 27, 2014 at 4:20 PM
Edited Jan 27, 2014 at 4:53 PM
Hi ..
in the naudio demo I tried to play back the following file (same file on both links ):
http://www.filedropper.com/mix4stereochannels16bitpcm10sec

http://www.4shared.com/music/zSpIZ2JRba/Mix_4StereoChannels_16BitPcm_1.html


I need to select the direct sound as output driver ...
and play to primary sound driver..
(check the image below)

it says:
value does not fall within the expected range.

iam using the naudio 1.7

any help is really appreciated ..

Image

in my c# application iam trying to play the audio file (but when I click to play the audio file, it just calls the WaveOutPlaybackStopped event, I think it is the same error as in naudio):
private DirectSoundOut waveOut;
private MeteringSampleProvider postVolumeMeter;
...

this.AudioFile = new AudioFileReader(this.audioFilePath);
 AudioFileTotalTime = this.AudioFile.TotalTime.TotalMilliseconds;
this.waveOut = new DirectSoundOut(this.deviceGuid);
this.postVolumeMeter = new MeteringSampleProvider(this.AudioFile);
this.postVolumeMeter.StreamVolume += OnPostVolumeMeter;
waveOut.PlaybackStopped += WaveOutPlaybackStopped;
this.waveOut.Init(new SampleToWaveProvider(this.postVolumeMeter));
return true;
...
        private void WaveOutPlaybackStopped(object sender, StoppedEventArgs e)
        {
            PlayBackStopped = true;
        }

public bool Deinitialize()
        {
            if (this.waveOut != null)
            {
                Stop();

                DispatcherControlledCall(() => this.waveOut.Dispose());
                this.waveOut.PlaybackStopped -= WaveOutPlaybackStopped;
                this.waveOut = null;
            }
            
            if (this.AudioFile != null)
            {
                this.AudioFile.Dispose();
                this.AudioFile = null;
            }

            if (this.postVolumeMeter != null)
            {
                this.postVolumeMeter.StreamVolume -= OnPostVolumeMeter;
                this.postVolumeMeter = null;
            }
            return true;
        }

        private void OnPostVolumeMeter(object sender, StreamVolumeEventArgs e)
        {
            if (e.MaxSampleValues != null)
            {
                LeftChannelStreamVolume = e.MaxSampleValues[0];
                if (e.MaxSampleValues.Length > 1)
                {
                    RightChannelStreamVolume = e.MaxSampleValues[1];
                }
            }
        }

Jan 28, 2014 at 12:20 AM
The file seems corrupted, not even Winamp can play it. DirectSound buffer errors.
Jan 28, 2014 at 9:16 AM
Edited Jan 28, 2014 at 9:17 AM
Thanks for your answer ...
the file is playable on vlc !?

also,
is there anything I need to set in my c# code ? (buffer size?, etc ...)

and,

does the directsound driver works with different bit rates and support multichannel files (like 8 channels or 6channels)??

thanks in advance for any help ..
Jan 28, 2014 at 10:59 AM
Edited Jan 28, 2014 at 11:02 AM
Wow, didn't realise it had 8 channels. But you are right, it works in VLC which obviously supports 8 channel audio.

The WaveFileReader appears to support many channels but I dont see a static Property for the buffer size in DirectSoundOut.

Instead of using AudioFileReader(), try MediaFoundationReader() and WaveFileReader()



Additionally, if you download the NAudio source and run that demo app through the debugger, you'll be able to spot exactly where it is failing.
Jan 28, 2014 at 1:19 PM
it's the number of channels that's the problem here rather than the reader. Not all audio output device types will support >2 output channels.
Jan 28, 2014 at 2:44 PM
Edited Jan 28, 2014 at 3:18 PM
Hi ..
thanks for replying ...

(Does the windows media player 11 on windows xp use the direct sound driver)? the file can be played on windows 7 & xp with wmp 11&12.... so it is not audio output device related, right? or how does the wmp play the file?

I did run the demo through the debugger, here is where the exception appears :
                    // Create SecondaryBuffer
                    directSound.CreateSoundBuffer(bufferDesc2, out soundBufferObj, IntPtr.Zero);
                    secondaryBuffer = (IDirectSoundBuffer)soundBufferObj;
                    handleOnWaveFormat.Free();
// at NAudio.Wave.DirectSoundOut.IDirectSound.CreateSoundBuffer(BufferDescription desc, Object& dsDSoundBuffer, IntPtr pUnkOuter)
// {Void CreateSoundBuffer(BufferDescription, System.Object ByRef, IntPtr)}
mark: is there any work around ?
I need to support windows xp & windows 7 so I need to use the directsound driver ...or do I have any other options ?
Jan 29, 2014 at 12:56 AM
Edited Jan 29, 2014 at 7:37 AM
Yes WMP uses DirectShow codecs in XP as well as previous interfaces.

DirectSoundOut is most likely the best option for XP, MediaFoundationReader may be a better option in Win7 but DirectSoundOut should work fine.
Jan 29, 2014 at 7:17 AM
Edited Jan 29, 2014 at 7:37 AM
I had a quick look and tried increasing the CreateSoundBuffer buffer size 8 fold but it didn't work. I also tried using WaveOut and MediaFoundationReader, nothing seems to accept or convert 4 stereo channels to the speaker setup of the PC. 4 stereo channels is an odd format after all.

You could downsize the output to 2 channels or perhaps 4 channels. I believe NAudio has the functionality to mix audio data in this fashion.
Jan 29, 2014 at 8:38 AM
Thank you K24A3 for your help ..
iam still not able to understand why this won`t work..
the directsoundout is initializing with the right values (channel number, file size, bit rate and so on)...


anyway thanks again for the great support ..
Jan 29, 2014 at 8:58 AM
I'm pretty sure VLC and WMP are ignoring some channels or converting them down to stereo or 5.1. NAudio doesn't do this automatically. You need to put a processor/converter in between the input and output.

Winamp gives the same error so it seems the sound card driver simply wont accept 4 stereo channels. I've never seen an 8 channel sound card. 7.1 yes (7 normal channels + 1 low frequency subwoofer channel) but you wont be able to play four full stereo channels through a 7.1 system, it's too much data.
Jan 29, 2014 at 10:59 AM
Hi ..
k24A3 I tried changing the buffer size, still no success ...
also I tried to create the soundbuffer based on the information of a 2 channel file ..didn't work ..
:(
as said the wav file can be played in vlc, but looking into the codec information tab I found out that vlc has problems reading the number of channels in the audio file ..

Image
Jan 29, 2014 at 11:28 AM
Ok so it seems that VLC is running into the same error but handling it.

I can hear different audio from all 5 of my speakers so it seems VLC is playing 5 of the 8 channels, dropping the other 3.

If you need NAudio to handle it you need to convert the 8 channels into what ever the speaker setup is.

For example 7.1 speakers:

First 2 channels > Combine into 1 channel > Front Center speaker
Next 2 channels > Front L and R
Next 2 channels > Middle L and R
Next 2 channels > Rear L and R


Stereo Speakers

Combine all 4 left channels to 1 Left speaker channel
Combine all 4 right channels to 1 Right speaker channel


NAudio is an audio library for programmers, so we can't really assume NAudio will handle this automatically. The conversion will need to be done by the application using NAudio.

Have a look at the NAudio WPF Demo App, the MediaFoundation Resampler can convert the 8 channels to Stereo. I tried it and it works. The 2 channel wav output file plays fine in Winamp. You should be able to set it up so it outputs to surround sound format (5.1 or 7.1).

WaveStream file input -> resampler -> DirectSoundOut