Problems with looped sounds (I think)

BlitzMax Forums/BlitzMax Programming/Problems with looped sounds (I think)

GfK(Posted 2006) [#1]
I've had this problem for a few months now and its starting to do my head in.

I have an array of 32 sound channels which I cycle through to play sounds -this ensures I can cap the number of sounds that are playing at once. I used to have 16 but increased it to 32 in a failed effort to resolve this problem.

The problem seems to be with looped sounds. I have one looped sound which is allocated its own channel each time it is played, and I change the channel rate slightly depending on how many points the player scored. This also determines the length of time the sound is played for.

Occasionally, my other sounds get looped too. There is no pattern to this whatsoever - it appears to be completely random. When it happens, I get a sound effect playing over and over that should NOT be looped, and no further sounds will play.

This is my function that plays non-looped sounds
Function playSnd(hnd:TSound,rate:Float = 1,pan:Float = 0)
	chanPtr:+1
	If chanPtr>maxChans-1
		chanPtr = 0
	EndIf
	chan[chanPtr].Stop
	chan[chanPtr] = Null
	chan[chanPtr] = New TChannel

	chan[chanPtr] = CueSound(hnd)
	SetChannelRate chan[chanPtr],rate
	SetChannelPan chan[chanPtr],pan
	ResumeChannel chan[chanPtr]
End Function


That's pretty straightforward so I won't bother to explain what its doing, except to say that maxChans is set to 32 elsewhere in my code.

The method below is where I start/stop the looping sound. I believe the problem is here somewhere. Its slightly messy just now as I'm constantly rearranging stuff in an effort to get it to work correctly.

	Method UpdateLevelScore()
		Local chanRate:Float
		If levelScore < levelTargetScore
			levelScore :+ rate

			If chanAddScore = Null
				chanAddScore = New TChannel
				chanAddScore = PlaySound(sndAddScore) 'AM I USING THIS RIGHT????????
				chanRate = 1
				SetChannelRate chanAddScore,1
			EndIf
			If levelScore >= levelTargetScore
				levelScore = levelTargetScore
				If chanAddScore <> Null
					chanAddScore.Stop
					chanAddScore = Null
				EndIf
			EndIf
		EndIf
		If chanAddScore <> Null
			chanRate = 1+((Float(levelScore)/Float(winPoints))*0.3)
			SetChannelRate chanAddScore,chanRate
		EndIf
	End Method


I'm pretty much out of ideas. I'm either doing something wrong somewhere, or the BRL sound module has a problem.

[edit] Hope this post is clear. Its currently after 3am and I've been messing with this for ages tonight - brain is all over the place now.


Grey Alien(Posted 2006) [#2]
no you are not using playsound correctly. I got confused by this too initially. From the docs:

"If no channel is specified, PlaySound automatically allocates a channel for you."

And you are not specifying a channel so Playsound is creating one for you and returning it. This means that channAddScore = New TChannel is making a channel which is then thrown away because it's being overwritten with a new channel by PlaySound, I don't know what effect that may have.

You should use CueSound(sndAddScore, chanAddScore), then set the channel volume, rat, pan etc and then call resumechannel. That's what my framework does successfully, however I'm not using looping sounds so you *could* be experiencing some other issue.

Also are you up-to-date with syncmodes, I presume so, as there were some well dodgy sound channel problems about 6 months ago...


GfK(Posted 2006) [#3]
Hmm... the docs need a serious update here, I think. Reading it again (about 40 times) it doesn't actually state what the correct usage is.

So which of the following is correct?

chanAddScore = PlaySound(sndAddScore,chanAddScore)

or

PlaySound(sndAddScore,ChanAddScore)

??

I'm using the first at the moment (minus the channel parameter) because that's the usual syntax when you're returning a value from a function.

I'll try incorporating looping sounds into my playSnd function - shouldn't be too hard if I return the channel pointer so I can stop the sound later.

Interestingly though I don't get any sound problems whatsoever if I don't use looped sounds*, but I guess there are circumstances when only a looped sound will do, so just not using them isn't a viable option.

I'm 90% convinced there is some sort of issue with looped sounds.

* I can't prove that with 100% certainty as its one of those crappy bugs that only happens once in a while with no apparent way to accurately reproduce it.


Yan(Posted 2006) [#4]
You either allocate a channel manually and tell PlaySound() to use that channel...
chanAddScore = AllocChannel()
PlaySound(sndAddScore, chanAddScore)


..or you can let PlaySound() automatically allocate a channel for you...
chanAddScore = PlaySound(sndAddScore)



GfK(Posted 2006) [#5]
Does AllocChannel() do the same thing as chanAddScore = New TChannel?


Grey Alien(Posted 2006) [#6]
Yan is correct and your second method in your 2nd post is correct.

Does AllocChannel() do the same thing as chanAddScore = New TChannel?

No it is NOT the same, you have to use AllocChannel(), this TOO confused me at first. New doesn't actually make a valid channel ready to use. You never noticed this because you were overwriting the variable anyway and hopefully it was garbage collected. Doesn't explain the looping prob. Try using play correctly OR using CueSound like I suggested and see what happens...