[SDL] Some sounds not played by SDL_mixer, but channels not exhausted

Pierre Sarrazin sarrazip at sympatico.ca
Wed Jan 8 17:14:00 PST 2003

A video game I am developing uses SDL_mixer (2.2.4) to produce sound
effects and sometimes, some sounds are not heard, even though there
are more than enough channels.

The program calls this statement:

    Mix_PlayChannel(-1, sample, 0)

and if it returns -1, the string returned by Mix_GetError() is
thrown as a C++ exception (of type string).  No such exception is
ever thrown by my program, so the failure does not seem to be there.
'sample' is loaded by Mix_LoadWAV().

The sound effects are heard correctly most of the time.  However,
it seems like when two or three calls to Mix_PlayChannel() happen
too close to one another, one of the sounds is not played.

The sound system is initialized this way:

    if (Mix_OpenAudio(11025, AUDIO_U8, 1, 256) == -1)
	throw ...;

The game produces about 20 frames per second.  When I coded two
consecutive calls to Mix_PlayChannel(), one of the sounds was not
played.  Then I created a queue of sounds to be played, and coded a
system that played only one sound out of this queue at each frame.
With this system, both sounds were heard.

However, this hack does not work all the time.  I still get
situations in the game where a sound effect should be played but
is not heard.  Unfortunately, this is difficult to reproduce.

Adding some trace code has shown that at most 3 channels are used
at the same time in this game, so the missed sounds cannot be due
to a lack of available channels (I allocate 16).  Mix_PlayChannel()
never returns -1 in these scenarios.  It returns 0, 1 or 2.

Yet it seems like whenever a sound needs to play on channel 2
(because 0 and 1 are already playing a sound), the problem occurs.

My program links with SDL 1.2.4, SDL_image 1.2.2 and SDL_mixer 1.2.4.
It is compiled with g++ 2.96-112 on a RedHat 7.2 system.

The sound card is an Ensoniq 5880 AudioPCI according to /proc/pci.

If anyone has had a similar problem in the past, where should I
look for trouble?


Pierre Sarrazin <sarrazip at sympatico dot ca>

More information about the SDL mailing list