Tonejs / Tone.js

A Web Audio framework for making interactive music in the browser.
https://tonejs.github.io
MIT License
13.37k stars 976 forks source link

Multiple seeks fail after some time #1158

Open dirkk0 opened 1 year ago

dirkk0 commented 1 year ago

Describe the bug

Multiple seeks fail after some time in the Player.

To Reproduce codepen. After the 4th time the seek fails, and the file plays to the end.

Expected behavior seek should always work

Additional context I slice the audio buffer to make the file shorter, the audio file was the only one that I could find that was long enough without CORS issues. The slicing is not the problem, though, the same behaviour happens with the slice, it just takes longer to who up.

jamieforth commented 1 year ago

This seems to relate to the implicit stop event not being updated within seek(), i.e. the player's state becomes stopped at startTime + duration regardless of the current playback position.

A workaround is to cancel the stopEvent before calling seek().

if (player._state.getValueAtTime(Tone.now()) === "started") {
    // Cancel the stop event since it's at a different time now.
    const stopEvent = player._state.getNextState("stopped", Tone.now());
    if (stopEvent && stopEvent.implicitEnd) {
            player._state.cancel(stopEvent.time);
            player.seek(offset, Tone.now());
    }
}

Or just to use player.restart(Tone.now(), offset) instead.