DirectSound Buffer management

Started by
3 comments, last by d000hg 19 years, 5 months ago
I've decided I will use DSound for my project. The basics seems quite, well, basic... but the way buffers are used is making me think hard... As I see it you would probably like to pre-load all .wav files at load time if memory allows this, especially in a game context where every sound is likely to be used often. Whether you store this in a DIRECTSOUNDBUFFER(8 for me) object or a custom tructure doesn't make too much difference, although the latter means every sound has an 'extra' copy taking up memory. When you want to play a sound, you create a DIRECTSOUNDBUFFER of the right size for the sound, and copy the original data across from the stored version (or use DuplicateBuffer) before playing this buffer. You keep this buffer to play the sound again. But what happens when you want to play the same sound more than once simultaneously - for me this will be an issue with the engine noise for each car in the game within range. You can't play the same buffer twice at once I'm sure, so you'll have to create more copies of the same sound in multiple buffers. Would you then store every buffer that's been created for sound X separately from every buffer created for sound Y - then when sound X is requested find and play the first 'sound X buffer' which isn't currently playing? Or is this highly wasteful and as soon as a sound stops the buffer should be deleted? I can see each .wav file having an object storing its data and a std::vector of every buffer so far created to play it. Fairly trivial to write but it seems wrong. I just don't see how to properly reuse buffers when they'll always be the wrong length for any other sample other than what they were created for. Thanks...
Advertisement
Use the DuplicateBuffer method to use the same buffer multiple times.
If your app has only a few sounds, then using static buffers would be fine. It's probably easier to just have a few static buffers and stream your sounds in as required, then you only need memory for sounds currently/likely to be playing.
Streaming seems like hard work, having to set timers or extra threads. I'd rather avoid it if possible.
Nah, streaming's easy. You'll need timers when you do music anyway (either mod type music or streamed wav/mp3). Plus you get a lot more flexibility.
You might even try sound chip emulation - that can be quite fun and educational.
Why would I need to use streaming for MP3 - I planned to use DirectShow to do that, although you'd still have to check like once a second if it was time to queue the next track. All by .wav's should be under 2s long.

By the way, the DX8.1 sdk docs say DuplicateBuffer is deprecated implying you shouldn't use it because normally it won't share memory. But why not use it anyway?

This topic is closed to new replies.

Advertisement