views:

2090

answers:

2

Hi,

I've got a c# application that plays simple wav files through directsound. With the test data I had, the code worked fine. However when I used real-world data, it produced a very unhelpful error on creation of the secondary buffer: "ArgumentException: Value does not fall within the expected range."

The test wavs had a 512kbps bit rate, 16bit audio sample size, and 32kHz audio sample rate. The new wavs is 1152kbps, 24bit and 48kHz respectively. How can I get directsound to cope with these larger values, or if not how can I programatically detect these values before attempting to play the file?

it's managed DirectX v9.00.1126 I'm using, and I've included some sample code below:

using DS = Microsoft.DirectX.DirectSound;  
...  
DS.Device device = new DS.Device();
device.SetCooperativeLevel(this, CooperativeLevel.Normal);  
...
BufferDescription bufferDesc = new BufferDescription();
bufferDesc.ControlEffects = false;  
...
try
{
    SecondaryBuffer sound = new SecondaryBuffer(path, bufferDesc, device);
    sound.Play(0, BufferPlayFlags.Default);
}
...

Additional info: the real-world wav files won't play in windows media player either, telling me a codec is needed to play the file, while they play fine in winamp.

Additional info 2: Comparing the bytes of the working test data and the bad real-world data, I can see that past the RIFF chunk, the bad data has a "bext" chunk, that the internet informs me is metadata associated with the broadcast audio extension, while the test data goes straight into a fmt chunk. There /is/ a fmt chunk in the bad data, so I don't know if it's badly-formed or if the loaders should be looking further for fmt data. I can see if I can get some information on this rouge bext chunk from the people supplying me the data - if they can remove it my code may still work.

+3  A: 

Not all soundcards support 24 bit sample playback, and even when they do, they often have to be exclusively opened in that mode. There is a similar issue with sample rates. Your soundcard may be operating at 44.1kHz, in which case 48kHz needs to be resampled to be played.

I have written an open source .NET audio library called NAudio which will allow you to find out what sample rate and bit depth a given WAV file is. It also offers alternative ways of playing back audio (e.g. through the Wav... APIs), and the ability to resample files using the DMO resampler object.

Mark Heath
Cheers Mark. I will have a look at your app.
tenpn
Having tried your sample apps, it still doesn't load - but now I get better error messages: "Not a wave file - no fmt header"See my question for some more info...
tenpn
+3  A: 

In addition to the sampling issue, WAV is just a container format and the audio could be compressed in any of a myriad of audio formats (just like AVI is a container of video). So you could use a tool like GSpot to find out if your WAV is encoded in a non-standard format in and install the codec. Winamp has more codecs installed by default than WMP, which would explain the Winamp plays it and WMP doesn't.

Vinko Vrsalovic
Thanks - gspot says both types of file are PCM, it's just the stats in my original post that are different. Great app though.
tenpn