Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sound.currentTime Needed #140

Open
Mr-Ahmadi opened this issue Jun 25, 2020 · 6 comments
Open

sound.currentTime Needed #140

Mr-Ahmadi opened this issue Jun 25, 2020 · 6 comments

Comments

@Mr-Ahmadi
Copy link

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
Copy link

nascent commented Aug 16, 2020

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
Copy link
Author

@nascent ow can we do that?

@kyoukhana
Copy link

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
Copy link
Author

might be possible with a little mathematics...

@1hko
Copy link

1hko commented Jan 19, 2023

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
Copy link

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;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants