pizzicato
pizzicato copied to clipboard
sound.currentTime Needed
Thank you for your cool library.
Is there any way for getting the currentTime
of sound and change it?
Something like audio.currentTime = 5;
?
I believe you can start the sound from a certain point, but I don't believe you can have something like an interactive song position that is changeable at any time, typical with modern video and audio players on the web.
Of course anything is better than nothing.
@nascent ow can we do that?
Yea we need this. But even better would be awesome to get the current play back time for each individual sound object within a group object
might be possible with a little mathematics...
I also ran into this issue using Pizzicato. Here's the basic moving pieces I used to solve the issue:
const state = { // transport state
type: "stop", // sound is stopped
playhead: 0, // current playhead position
timeRef: null, // time reference when play was clicked
}
const sound = new Pizzicato.Sound(...)
Now implement your transport controls:
function play() {
switch (state.type) {
case "stop":
state.type = "play" // change play state
transport.timeRef = Date.now() // record time reference
sound.play(0, state.playhead) // play sound from current playhead
break
case "play":
break // sound is already playing
}
}
function stop() {
switch (state.type) {
case "stop":
break // sound is already stopped
case "play":
const delta = (Date.now() - state.timeRef) / 1000 // compute time delta
state.type = "stop" // change play state
state.playhead = state.playhead + delta // update playhead
state.timeRef = null // unset time reference
break
}
}
Now you can get the playhead position at any time:
requestAnimationFrame(function onFrame() {
switch (state.type) {
case "stop":
console.log("playhead", state.playhead) // render
break
case "play":
const delta = (Date.now() - state.timeRef) / 1000
console.log("playhead", state.playhead + delta) // render
break
}
requestAnimationFrame(onFrame)
})
Hacky way to get current time, works with paused tracks and doesn't need manual tracking. I didn't test it with stopped or detached tracks tho
const isPaused = !audio.playing;
const lastTimePlayed = audio.lastTimePlayed;
const offsetTime = audio.offsetTime || 0;
const contextTime = audio.sourceNode?.context?.currentTime || 0;
const currentTime = isPaused ? offsetTime : contextTime - lastTimePlayed;