Mix_AllocateChannels

Occasionally a sound doesn’t get played on my game, and I’m wondering if that will occur if I haven’t allocated enough channels in:

Mix_AllocateChannels

Currently I allocate 36, but the game is quite intense. Is there a maximum number of channels that can be allocated, and are there any serious drawbacks of say setting it to 256?

If you play sound without specifying a channel index, SDL looks for a first free channel and if it finds one, plays the sound on it. But if it does not find a free channel, the sound will not be played at all — the Mix_PlayChannel function will return -1 in this case. So if you want to know whether in certain situations there is actually no free channel and therefore no sound is played, check the results of the function.

Intense? Is the mixer using a lot of CPU time to mix sounds? 36 channels doesn’t sound like much at all. If you want to reduce CPU time spent mixing sounds, use one format for all your sounds files and open the audio device specifically for that format, if available. Chunk size also matters.

There is an upper limit, but I checked for a test and SDL has no problem allocating a million channels (not that it’s useful, it’s just a test). The more channels (actually: tracks), the more sounds that can be played simultaneously, but the more sounds to mix at a given moment, the more CPU time the mixer will consume. Allocate as many channels as your game needs to play sounds.


If you need to be able to play a large number of sounds, but at the same time have a small number of channels, you can always write the engine code in such a way that if there is no free channel, you overwrite the sound on one channel. In this case, the previous sound will end immediately and a new one will be played in its place.

I used this method in Fairtris, but not because there are too many channels needed, but to achieve an interesting effect. Sound types are assigned to specific channel indexes, so if I need to play a new sound on the same channel, I play it and the old one ends first. In my case, i.e. porting a game from NES, this effect was useful, but in your case it may not be so.

Thanks for a very informative reply! I found this really useful:

Mix_Playing(-1)

to show how many channels were in use, and it looks like I did hit my 36 channel limit, so I’ve upped that to 64 now.

I try and keep all my sound files as 44,100kHz and mono and as .ogg files. But, I let people mod my game so they may add .ogg files of different frequencies.

I use:

Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 1024)

as I wanted to keep latency as low as possible as it’s a real-time game, and some Android devices seem to delay sound playing by a few ms.