Playdate.sound.fileplayer getLength() / getOffset() returns incorrect values

playdate.sound.fileplayer getLength() / getOffset() return incorrect values since SDK 1.10.0

limport "CoreLibs/graphics"

local gfx <const> = playdate.graphics
local fp = playdate.sound.fileplayer.new("resource/test.mp3")
fp:play()


function playdate.update()
	gfx.clear()
	gfx.drawText("Length:" .. tostring(fp:getLength()), 10, 10)
	gfx.drawText("Offset: " .. tostring(fp:getOffset()), 10, 50)
end

SDK 1.9.3:
193

SDK 1.10.0:
110

getLength() returns a fixed decimal value.
getOffset() returns almost the same value, even during playback.

Reproduced on Windows/Mac Simulator and Playdate Device.
This has been occurring since updating to SDK 1.10.0

1 Like

I'm also seeing this, and i wondered if it was due to the new behaviour of fileplayers delaying opening the file.

I think getLength should open the file.

1 Like

For posterity's sake, I believe this is a dupe of I'm been having a lot of trouble with the fileplayer - #5 by NeoTechni

4 Likes

I think I’m also seeing incorrect lengths with sampleplayer – playdate.sound.sampleplayer:getLength() reports slightly more than double the actual length after using playdate.sound.sampleplayer.new(path) to load from a few different ADPCM wav files (encoded using the ffmpeg command in the docs), although I haven’t investigated it very thoroughly. Could that be the same underlying issue as well, or something different?

I haven’t tried getOffset, but playdate.sound.sampleplayer:setOffset() also requires doubling the value to get it to seek close to the right place.

just fixed that, thanks! I had the logic swapped for mono vs stereo, and was counting samples instead of pairs. :man_facepalming:

3 Likes

With SDK 1.12.0, getLength() / getOffset() now return the correct values. Thanks for the fix!
However, there is a new problem that when setOffset is called, the value of getLength() is incorrect.

local gfx = playdate.graphics
gfx.setColor(gfx.kColorBlack)

local _sndPlayer = nil
local _status = "Playing ..."

_sndPlayer = playdate.sound.fileplayer.new("sounds/Test.mp3")
_sndPlayer:play()
_sndPlayer:setFinishCallback(function () _status = "Finished!" end)

function playdate.AButtonDown()
    local offset = _sndPlayer:getOffset()
    offset += 1
    if offset < _sndPlayer:getLength() then
        _sndPlayer:setOffset(offset)
    end
end

function playdate.update()
    gfx.clear()
    gfx.drawText(_status , 50, 40)
    gfx.drawText("Offset: " .. _sndPlayer:getOffset() , 50, 80)
    gfx.drawText("Length: " .. _sndPlayer:getLength() , 50, 120)
end

TestSetOffset.zip (7.1 MB)

In my test program, when I press the AButton, I advance the Offset by 1, but each time I press the AButton, the value of getLength changes.

1 Like

sampleplayer:setOffset() seems to be completely broken now (as of 1.12.0).

I’m using sampleplayer:setOffset(seconds) followed by sampleplayer:play(1) and it starts at the wrong place in the sample (too early) and doesn’t finish playing before it stops.

In 1.11, I was able to work around the issue by simply halving all the values I got from sampleplayer:getLength and doubling all the values I passed to sampleplayer:setOffset. In 1.12.0, I can’t find any workaround: sampleplayer:getLength now returns the correct value, but sampleplayer:setOffset seeks to the wrong place. I attempted to work around this by adding a multiplier to the value I pass to sampleplayer:setOffset to correct for the wrong start time, which sort of works but crashes the simulator when the sample finishes playing (crash output here). Trying this on device results in a freeze followed by the “Sorry, your Playdate needs to restart” screen without the option to press B for the crash log. The file in question is a stereo ADPCM .wav.

I can confirm that all the issues I had with sampleplayer are fixed in 1.12.2

The fileplayer(MP3) getLength(), get/setOffset() issue also occurs in SDK1.12.2.

1 Like