Some sounds not played by SDL_mixer, but channels not exhausted

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 ...;
Mix_AllocateChannels(16);

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?

Thanks.–
Pierre Sarrazin

Dixit Pierre Sarrazin (2003-01-08 20:15):

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)
[...]

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.

I inserted trace code in the sources of SDL_mixer 1.2.4
(mix_channels() in mixer.c) and saw that channel 2 always has a
volume of 1 (instead of MIX_MAX_VOLUME (128)), while channels 0
and 1 have a volume of MIX_MAX_VOLUME as expected.

At first glance, I seem to have solved my problem with this patch
to make sure that sounds played on channel 2 are heard:

int channelNo = Mix_PlayChannel(-1, sample, 0);
if (channelNo == -1)
throw ...;
Mix_Volume(channelNo, MIX_MAX_VOLUME);

I tried calling Mix_Volume() only once (for each allocated channel)
after initializing the audio system, but that did not work. It seems
like something is constantly resetting the volume of channel 2 to 1.

Does anyone understand where this situation could come from?–
Pierre Sarrazin

i know almost nothing about sound programming but a stab in the dark would
be that your hardware or sound driver only supports channels 0 and 1. This
is a random guess and i appologize in advance for this question but does
channel 0 sound come out of the left speaker and channel 1 comes out of the
right? No one else seems to be responding so i figure id throw something
out there. Hope it helps.> ----- Original Message -----

From: sarrazip@sympatico.ca (Pierre Sarrazin)
To:
Sent: Thursday, January 09, 2003 11:03 PM
Subject: Re: [SDL] Some sounds not played by SDL_mixer, but channels not
exhausted

Dixit Pierre Sarrazin (2003-01-08 20:15):

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)

[…]

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.

I inserted trace code in the sources of SDL_mixer 1.2.4
(mix_channels() in mixer.c) and saw that channel 2 always has a
volume of 1 (instead of MIX_MAX_VOLUME (128)), while channels 0
and 1 have a volume of MIX_MAX_VOLUME as expected.

At first glance, I seem to have solved my problem with this patch
to make sure that sounds played on channel 2 are heard:

int channelNo = Mix_PlayChannel(-1, sample, 0);
if (channelNo == -1)

throw …;
Mix_Volume(channelNo, MIX_MAX_VOLUME);

I tried calling Mix_Volume() only once (for each allocated channel)
after initializing the audio system, but that did not work. It seems
like something is constantly resetting the volume of channel 2 to 1.

Does anyone understand where this situation could come from?


Pierre Sarrazin


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

I inserted trace code in the sources of SDL_mixer 1.2.4
(mix_channels() in mixer.c) and saw that channel 2 always has a
volume of 1 (instead of MIX_MAX_VOLUME (128)), while channels 0
and 1 have a volume of MIX_MAX_VOLUME as expected.

At first glance, I seem to have solved my problem with this patch
to make sure that sounds played on channel 2 are heard:
[…]

Dixit Atrix Wolfe (2003-01-09 23:32):

i know almost nothing about sound programming but a stab in the dark would
be that your hardware or sound driver only supports channels 0 and 1. This
is a random guess and i appologize in advance for this question but does
channel 0 sound come out of the left speaker and channel 1 comes out of the
right?
[…]

The mono/stereo channels have nothing to do with the mixing channels.
The terminology used in the SDL programming interface is a bit
misleading on that point.

The patch I described actually solves the problem: I can now hear
all three sounds on both speakers. The problem seems to be purely
in software. When I wrote a small program to try to reproduce the
problem, I could not: this small program played three simultaneous
sounds on the first try. Apparently, something in the dynamics of
my video game could be disrupting something in SDL_mixer.>From: sarrazip@sympatico dot ca (Pierre Sarrazin)
Sent: Thursday, January 09, 2003 11:03 PM


Pierre Sarrazin

Dixit Pierre Sarrazin (2003-01-08 20:15):

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)

[…]

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.

I inserted trace code in the sources of SDL_mixer 1.2.4
(mix_channels() in mixer.c) and saw that channel 2 always has a
volume of 1 (instead of MIX_MAX_VOLUME (128)), while channels 0
and 1 have a volume of MIX_MAX_VOLUME as expected.

At first glance, I seem to have solved my problem with this patch
to make sure that sounds played on channel 2 are heard:

int channelNo = Mix_PlayChannel(-1, sample, 0);
if (channelNo == -1)

throw …;
Mix_Volume(channelNo, MIX_MAX_VOLUME);

I tried calling Mix_Volume() only once (for each allocated channel)
after initializing the audio system, but that did not work. It seems
like something is constantly resetting the volume of channel 2 to 1.

Does anyone understand where this situation could come from?

Not offhand. Are you using a single thread or trying to call the play
functions from multiple threads?

See ya,
-Sam Lantinga, Software Engineer, Blizzard Entertainment