Playing mulitple sounds in C + +? [gunshot sound] [mcisendstring]

admin / March 24th, 2011/ Posted in Software / No Comments »

Q: My brother wants a gunshot wound to play every time you shoot in a game he is writing sound. He wants them to overlap If you shoot multiple times fast. This code works but is very slow. I think spawning a new thread for every sound is a problem. Anyone know how to do it well?

void sound Thread (void * filename) (977 503 977 503 char * junk = new char (50); 977.503 (977.503 open dates sounds shot.wav type wave audio aka my file “, 977,503 junk, lstrlen (junk), NULL); 977 503 (“play myfile wait, junk, 977 503 lstrlen (junk), NULL); 977 503 (” close my file “junk, 977,503 lstrlen (junk), NULL); 977,503 junk removal, 977 503)

void PlaySound (char * filename ) (

_beginthread (Sound Thread, 0, (void *) filename)) 977 503


Sound Effects – Instant Sound FX For Multimedia
Re:Are you opening the file everytime you want to play the sound?

The Sound Membership Monthly For Multimedia Productions.
Re:Perhaps use a thread pool so you're not constantly spawning thread after thread after thread.

Colicky Baby Soothing Sounds Collection
Re:Ahh… debugging can always kill performance. It might be more fine to try my idea out though :) . Playing with
semaphores and junk is fun (and a challenge ;) ).

Albeit, I like the simplicity of your brother's approach.


Relaxing Nature Sound MP3’s – Very Useful Product
Re:That's what I was thinking. He doesn't know how to communicate between threads though ;) .

Anyway, it turns out that if you don't run within the IDE, even a debug-compiled binary is very very fast. I guess when it's being debugged, the thread overhead is much more.


101 Words that will make You Sound Smarter Right Away!
Re:No expert at this, but here is how I would do it…

The number of concurrent sounds would be determined by the programs configuration. For each concurrent sound, allocate
a thread for it. Spawning new threads for every single bullet seems a bit extreme. Instead have 8 threads running, and have
the threads run in a round robin fashion.

Have a sound queue data structure, that every thread can access (synchronization issues arrise here though). When a thread
is done it's sound, it will receive the next sound from the sound queue. The queue would have to be fancy and automatically
remove sounds that are too old. Don't want the bullet sound to come 1s after the trigger was pulled. That way if you get too many
sounds that the sound playing threads can handle, you don't go into sound lag hell.

I have no idea how real games do it, but that's just what came up to me right now :)


Related posts


Tags: ,

Leave a Reply

Name required

Mail (will not be published) required

Website