Sampleplayer stops working when playing different PlayRange

The simulator silently crashes when reusing the same samplePlayer and playing different range.

Resetting the sampleplayer in between calls of SetRange fixes the issues (
uncomment self.sampleplayer = pd.sound.sampleplayer.new(self.pathSample in the code below)

To briefly explain the call below:
I am loading a sample and want to play the first section, then the second, etc. until I reached the last section. Not resetting/reloading the sample player, it freezes after sample 2.

-- main.lua
local arrTimeRange = {1, 20552, 38934, 60933, 82268}
local songPlayer = SongPlayer("sound/french", arrTimeRange)
-- SongPlayer.lua
local pd <const> = playdate

class("SongPlayer").extends()

function SongPlayer:init(pathSample, arrTimeRange)
    SongPlayer.super.init(self)

    print("Start init")

    self.arrTimeRange = arrTimeRange
    self.idxSound = 1

    self.pathSample = pathSample
    self.sampleplayer = pd.sound.sampleplayer.new(self.pathSample)
    self:playSound()
end

function SongPlayer:playSound()
	print("Play sample ", self.idxSound)
    -- self.sampleplayer = pd.sound.sampleplayer.new(self.pathSample)
	if self.idxSound == #self.arrTimeRange then print("END") return end -- Nothing to play anymore

	self.sampleplayer:setPlayRange(self.arrTimeRange[self.idxSound], self.arrTimeRange[self.idxSound+1])
	self.sampleplayer:setFinishCallback(function ()
		self.idxSound += 1
        self:playSound()
	end)
	-- self.sampleplayer:play(1, math.random(3, 17) / 10)
    self.sampleplayer:play(1)
end

I'm not getting a crash, but I do see it stop playing after the first range. That's happening because songPlayer is declared local and there aren't any other references to it, so it scopes out as soon as main.lua finishes executing and gets cleaned up. Removing the local modifier to make songPlayer global fixed it for me.

Do you see the same thing, or is there an actual crash or hang on your end?

Thanks Dave. I thought the local in main would not necesarily scope out as the pd.update() function is still running. I assume this is because I had forgotten that pd.update is a coroutine and main.lua just calls it.
This is also further confirmed by using a class for instance and keeping a reference to the songplayer (in this example a Room from roomy).
Thank you Dave for looking into this. For others passing by, here is a git repo showing how it is supposed to work (apologies for not sharing this earlier Dave)

1 Like