Hello, I have problems using playdate.sound.sampleplayer:setPlayRange(). I have an audio file that is 1 second long and contains 10 samples of 0.1 seconds. At some point in the game, I would like to play one of these samples as an audio effect, selecting which sample to play randomly. Code example:
local pd <const> = playdate local gfx <const> = pd.graphics math.randomseed(pd.getSecondsSinceEpoch()) local i = nil local playTime = 2*1000 -- (ms) Play sample each 2 seconds local sampleTimer = nil local mySamples = pd.sound.sampleplayer.new("sounds/test") local sampleRate = 44100 -- (1/s) local sampleLength = 0.1 -- (s) Length of one "sample", not the total length of the file local sampleFrame = sampleRate*sampleLength -- (= 4410) is this the "frame offset"??? local function resetTimer(time) sampleTimer = pd.timer.new(time, time, 0, pd.easingFunctions.lin) end resetTimer(playTime) function pd.update() if sampleTimer.value == 0 then i = math.random(0, 9) mySamples:setPlayRange(sampleFrame*i, sampleFrame*(i+1)) mySamples:play() resetTimer(playTime) end pd.timer.updateTimers() gfx.sprite.update() gfx.clear() gfx.drawText("Timer: " .. sampleTimer.value/1000, 50, 50) if i then gfx.drawText("Last sample played: " .. i, 50, 75) end end
With that code, the problem that I am having is that the audio is played from the beginning of the file (instead from the point when I want it to start) until the point when I want it to stop. I have also tried fixed values instead of random ones and the same thing happens, it always plays from the beginning of the file. I don't know if this is some kind of bug or if I am doing something wrong (at first I didn't understand the meaning of "frame offset" in the documentation, so maybe I am using the function wrong).
I would appreciate if someone could help me with this. I know that I could just load the 10 samples as 10 individual audio files, but i would like to understand what is happening here.
(I am using the Playdate SDK 1.10.0 with the Lua API, running the Simulator on Windows 10, and I am using this VSCode template for compiling)