pizzicato icon indicating copy to clipboard operation
pizzicato copied to clipboard

sound.currentTime Needed

Open Mr-Ahmadi opened this issue 4 years ago • 6 comments

Thank you for your cool library.

Is there any way for getting the currentTime of sound and change it?

Something like audio.currentTime = 5;?

Mr-Ahmadi avatar Jun 25 '20 13:06 Mr-Ahmadi

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 avatar Aug 16 '20 08:08 Nascent

@nascent ow can we do that?

Mr-Ahmadi avatar Aug 19 '20 08:08 Mr-Ahmadi

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

kyoukhana avatar Aug 27 '20 15:08 kyoukhana

might be possible with a little mathematics...

Mr-Ahmadi avatar Aug 27 '20 15:08 Mr-Ahmadi

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)
})

1hko avatar Jan 19 '23 18:01 1hko

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;

LeonBaudouin avatar May 05 '23 16:05 LeonBaudouin