PSA: SoundInterval Cancels Looping

So, this is an issue that just caused me some confusion and frustration until I uncovered what was going on, and thus I want to post it here for any others who may in the future encounter similar issues:

In short, it seems that being played by a SoundInterval causes an AudioSound to become non-looping, even if it was set to loop via a call to “setLoop(True)”.

This state remains even after the SoundInterval is done, with a subsequent call to the sound’s “play” method resulting in only a single, non-looped playback.

An interesting little detail in this is that calls to “getLoop” will not immediately start returning “False” for the sound in question, even if those calls occur after the call to the interval’s “start” method.

It’s only after at least one subsequent game-loop update (I think) that calls to “getLoop” start to return “False”.

Here below is a quick test-program that demonstrates this little “gotcha”:

from direct.showbase.ShowBase import ShowBase
from direct.interval.IntervalGlobal import Sequence, SoundInterval

class Game(ShowBase):
	def __init__(self):
		ShowBase.__init__(self)

		self.accept("1", self.mew)
		self.accept("2", self.meow)
		
		self.seq = None
		self.sound = loader.loadSfx("chimeSingle.ogg")
		self.sound.setLoop(True)
		
	def mew(self):
		print (self.sound.getLoop())
		self.seq = SoundInterval(self.sound)
		self.seq.start()
		print (self.sound.getLoop())
		print ("~")
		
	def meow(self):
		self.sound.play()
		
	def update(self, task):
		dt = self.clock.getDt()
		
		print (self.sound.getLoop())
		
		return task.cont

app = Game()
app.run()

(I’m not currently classifying this as a bug in the engine, as I suspect that it’s the code working as intended, and can see the logic for it:

Being placed in an interval arguably implies that the sound should run for a finite duration. Intervals, after all, are all about controlling the order in which things happen–and by implication, conclude. Thus SoundInterval may be intentionally setting the sound to not loop.

It is, however, something that has the potential to trip up developers–as it just did to me–and thus I wanted to have a resource describing the behaviour be present on the forum!)

3 Likes

It makes sense once you know a key detail about intervals: they must have a duration that is defined up-front. A SoundInterval can therefore never loop indefinitely.

1 Like