How to do multiple short sounds with SDL3_mixer

The CategorySDLMixer page on the wiki says:

You need one track for each sound that will be played simultaneously; think of tracks as individual sliders on a mixer board.

Can one track overlap itself? As in, if it has a .5 second sound and you trigger it two times with .25 seconds between them, what happens? Do they overlap? Does the second play not happen because the track is busy? Does the second play wait until the first is done? Or does it just interrupt it?

I’ve been working on a vertical scrolling shooter engine since last fall and I finally started looking at the audio part - mainly because I’ve been developing this on a Mac (though I have it running on Linux as well) and SDL3_mixer just got added to homebrew. I am curious how to implement the ‘pew pew’ sounds. Can I use a track for each different sound and they can overlap (as described above)? Would I need a track for each blaster on the screen? That could get crazy.

Adding a basic soundboard with buttons to the SDL3_mixer Examples page would be a great demo for this problem. :slight_smile:

As a side note, I have been very impressed with SDL3 and how easy it is to work with. Great job!

It interrupts it.

Calling MIX_PlayTrack() on a track that is currently playing will restart it with the new specifications.

An option might be to have X number of tracks set aside for various things like shooty sounds, and just play sounds on them in a round-robin way, so if you have more than, say, 64 shooty sounds playing, the oldest one will get replaced, but in practice older tracks are finishing before you get back around to using them again.

You’d have to fine-tune this, between, “am I replacing sounds a lot, and is it noticible?” and “do I really need to be mixing 1024 shooty sounds at the same time?” Maybe 8 is a good number, maybe 32 is, but there’s probably a small number that makes a good upper limit.

Another option is to just use MIX_PlayAudio(), which will handle this for you as a fire-and-forget (literally!) sort of thing. It’ll create tracks as necessary, and when the track is done playing, it’ll put it in a pool to use for later fire-and-forget sounds…after a few seconds, it’ll have built up a reasonable rotation of tracks to handle what you are throwing at it, but it’ll mix as many things as you ask it until it runs out of memory, so maybe an upper bound makes sense.

(But note that MIX_PlayAudio() doesn’t let you adjust or pause those tracks. They play until they are done and that’s that.)

Thanks for the responses!

I suspected I would have to make a track pool but that MIX_PlayAudio() option looks pretty good!

1 Like