alemangui / pizzicato

Library to simplify the way you create and manipulate sounds with the Web Audio API.
https://alemangui.github.io/pizzicato/
MIT License
1.67k stars 132 forks source link

sound.currentTime Needed #140

Open Mr-Ahmadi opened 4 years ago

Mr-Ahmadi commented 4 years ago

Thank you for your cool library.

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

Something like audio.currentTime = 5;?

nascent commented 4 years ago

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.

Mr-Ahmadi commented 4 years ago

@nascent ow can we do that?

kyoukhana commented 4 years ago

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

Mr-Ahmadi commented 4 years ago

might be possible with a little mathematics...

1hko commented 1 year ago

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)
})
LeonBaudouin commented 1 year ago

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;