Sign in to follow this  

How to play one sound several times at once

This topic is 3730 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

Hello! At the moment I'm writing some classes to handle sounds with DirectX. I can load soundfiles, play them etc. but I cant play one sound several times at once. An Example: I have an IDirectMusicSegment8 which contains the "die-sound" (soldierdie.wav) for a soldier. If a soldier dies, this IDirectMusicSegment8 is being played in the destructor of the soldier-class. But I don't know how to play this sound several times if, e.g., 7 soldiers die at the same time (or in a short time intervall). Do I have to load and create for each soldier-object an own IDirectMusicSegment8 with this sound? Or is there another, more resources-saving, way? Thanks for your help :)

Share this post


Link to post
Share on other sites
I use DirectSound directly for this purpose: load a wav file into a static DirectSound buffer.

Have an instance count variable in your Sound resource class, increment it every time Play() is called. When instance count is 1, you call Play() directly on the original sound buffer you loaded wav file into. When instance count is greater than one, you duplicate the original sound buffer and then call Play() on that duplicate.

On each Play(), I also create a PlayingSoundInstance object with reference to the Sound and the DirectSound buffer pointer. The very first instance will have pointer to the original sound buffer (just AddRef it). All subsequent instances will have pointers to duplicated sound buffers. PlayingSoundInstance's destructor just calls Release() on the sound buffer.

The last piece is managing PlayingSoundInstance objects once they are created. I just add them to a linked list, and loop through that list every second (using time accumulator in my engine's Update function). I check if the sound instance should have finished playing by now and if so, I deallocate it and remove it from the list.

I know when a sound instance should finish playing because each PlayingSoundInstance stores time when it was created. Then all I have to do is get delta time between that time and current time. If delta is greater than sound duration, sound should be done playing.

I calculate the sound duration time when a sound resource is loaded from wav (just slightly overestimating it to be safe), by using a known formula you can find in Platform SDK docs.

Share this post


Link to post
Share on other sites

This topic is 3730 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