Calling playdate->sound->fileplayer->setBufferLength before playdate->sound->fileplayer->loadIntoPlayer has no effect (on device)

I am creating a little checkers game with a computer ai and when my computer AI was doing calculations my music from a FilePlayer would stop briefly and then resume (depending on amount of time my calculations take). So i figured it was not able to fill the buffer again in time and added a setBufferLength call with 5 second buffer BEFORE calling loadIntoPlayer and i still had the issue only when actually moving the setBufferLength call after the loadIntoPlayer call the buffer length increase seems to be applied on the fileplayer. I'm not sure if this is a bug or that the documentation for setBufferLength just needs a note / warning to call it after loadIntoPlayer.

Below is an example program to reproduce the problem, use for checkers.wav an adpcm (longer) wav file something like 1 - 5 minutes or so.

you need to test and run below code on the device itself as the simulator does not seem affected by it but running it on the playdate does. If i set bufferlength to 6 seconds before calling loadintoplayer and then in the update loop just "delay" 5 seconds the music has big long pauses and stutters, probably because it is still using the default buffer length. However if i move the setBufferLength call after loadIntoPlayer the problem no longer occurs and music plays fine.

So either this is a bug or we always need to call setBufferLength AFTER assigning a file using loadIntoPlayer (which is what i do now). and the docs need to get a warning about this

@timboe seems to have had the same issue in his game and moving the function calls also fixed the music stutters from fileplayer in his game (when doing something that takes a while to calculate during update loop which is exgagerated in below example)

static volatile char dummy;

void pdDelay(unsigned int milliseconds)
{
    unsigned int start = pd->system->getCurrentTimeMilliseconds();
    while (start + milliseconds > pd->system->getCurrentTimeMilliseconds())
    {
        (void)dummy;
    }
}

FilePlayer* player = NULL;
// main update function
int update(void* ud)
{
    if (player == NULL)
    {
        player = pd->sound->fileplayer->newPlayer();
        if (player)
        {
            pd->sound->fileplayer->setBufferLength(player, 6.0f);
            pd->sound->fileplayer->loadIntoPlayer(player, "checkers");
            //pd->sound->fileplayer->setBufferLength(player, 6.0f); //uncomment and comment previous one to have no skips
            pd->sound->fileplayer->setVolume(player, 1.0f, 1.0f);
            pd->sound->fileplayer->play(player, 0);
        }
    }
    pdDelay(5000);
    return 1;
}
2 Likes