Problems with repetitive playback

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);
}

This is probably unrelated to your problem, but is there a reason
you’re passing by const reference (int const&) instead of by value
(int)?
-:sigma.SBOn 4/4/08, Daniel Grace wrote:

int MixPlaySnd(int const &snd_index, int const &volume, int const &angle,
int const &distance, int const &loop, bool &free_channel,
int &channel)
int PlayingChannelSnd(int const &channel)

Passing by value would mean I could not pass a variable.On Sat, Apr 5, 2008 at 3:46 AM, Solra Bizna wrote:

This is probably unrelated to your problem, but is there a reason
you’re passing by const reference (int const&) instead of by value
(int)?
-:sigma.SB


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Note:

  • the app is compiled in KDevelop
  • I am running in a terminal window under KDE desktop

Either you’re misunderstanding your needs or I am.
What’s important in this case wouldn’t be passing the variable but
passing the value of the variable, would it not? Maybe it’s iffy
reference semantics that’s screwing things up?
-:sigma.SBOn 4/5/08, Daniel Grace wrote:

Passing by value would mean I could not pass a variable.

I’m not sure that would cause a problem; if he was passing a value where the function requires a reference, or vice versa, it wouldn’t compile in the first place, right?> ----- Original Message -----

From: sbizna@tejat.net (Solra Bizna)
To: A list for developers using the SDL library. (includes SDL-announce)
Sent: Saturday, April 5, 2008 10:58:42 AM
Subject: Re: [SDL] problems with repetitive playback

On 4/5/08, Daniel Grace wrote:

Passing by value would mean I could not pass a variable.
Either you’re misunderstanding your needs or I am.
What’s important in this case wouldn’t be passing the variable but
passing the value of the variable, would it not? Maybe it’s iffy
reference semantics that’s screwing things up?
-:sigma.SB


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Daniel Grace wrote:

Passing by value would mean I could not pass a variable.

Passing by reference is a good habit to get into, but it doesn’t buy you
anything when the object being referenced is the same size as the
resulting reference. (IE: passing anything larger than a pointer as a
reference instead, saves the object from being copied to the stack).

Since your objects are (likely) the same size as a pointer, you’re not
getting anything out of passing by reference.

The only real question would be whether a const reference or a copy of
the variable performs faster, but to discover that, you’d have to get a
disassembly to see the overhead each way.

Syntactically, they are functionally equivalent. But if Solra is correct
and your compiler is messing things up, you could just copy the function
header, comment out one copy and change the other to pass all const
references by value instead. A quick compile and test will tell you if
that’s where the problem lies. I suspect it’s not the problem, but it
would be quick to find out for sure.

Good Luck,
CWC

I would have thought the problem is more to do with timing / compiler
opimisation such as loop optimisation.