Sign in to follow this  

DirectSound Buffer management

This topic is 4757 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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...

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites

This topic is 4757 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this