I have made a test programs that is supposed to play a sound five
times each time with the sound getting closer to the listener. What
happens is that the sound is played once at full volume and then it is
played 5 times each time getting closer. Below is the playback code,
note MixPlaySnd and PlayingChannel is written below. What is very
confusing is if I use PlayingSnd instead to check for playback
finishing, then I get the sound playing 10 times, getting louder each
time. Something seems to be interferring with the continuity of the
program. Where do these extra playbacks come from ?
for (i = 5; i >= 0; i--)
{
// Play sound A (at position i*40, i*40)
CalcAngleDistance(0, 0, i*40, i*40, angle, dist);
if ((!MixPlaySnd(testsndA, MIX_MAX_VOLUME, angle, dist, 0,
free_channel, channelA)) && (free_channel))
exit(1);
// wait for sound to finish
while (PlayingChannelSnd(channelA)) {};
//while (PlayingSnd()) {};
}
int MixPlaySnd(int const &snd_index, int const &volume, int const &angle,
int const &distance, int const &loop, bool &free_channel,
int &channel)
// This looks for a free channel and if found plays the specified sound on
// that channel. distance is in 0-255 range, and angle is in degrees,
// 0-360 range. A loop value of 0 indicates no looping
// free channel returns true if a free channel was found, and
// channel the number of the channel assigned.
{
channel = 0;
while((Mix_Playing(channel) != 0) && (channel < n_channels))
channel++;
if (channel < n_channels)
{
// set the channel volume and the position
Mix_Volume(channel, volume);
Mix_SetPosition(channel, angle, distance);
channel = Mix_PlayChannel(channel, sound[snd_index], loop);
free_channel = true;
if (channel == -1)
OutputSndError("Unable to play WAV file: %s\n");
return (channel != -1);
}
else
{
free_channel = false;
return (false);
}
}
int PlayingChannelSnd(int const &channel)
// returns true if specified channel is playing a sound
{
return (Mix_Playing(channel) != 0);
}
int PlayingSnd()
// returns true if any channel is playing a sound (excluding music)
{
int channel = 0;
while((channel < n_channels) && (Mix_Playing(channel) == 0))
channel++;
if (channel < n_channels)
{
return (true);
}
else return (false);
}