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

How do I get the playhead position of a Wad object? #140

Closed
W-KE opened this issue Nov 26, 2021 · 3 comments
Closed

How do I get the playhead position of a Wad object? #140

W-KE opened this issue Nov 26, 2021 · 3 comments

Comments

@W-KE
Copy link

W-KE commented Nov 26, 2021

I'm trying to make a simple music player with Wad, and here is how I used Wad:

const audio = new Wad({ source: 'noise' });
console.log(this.audio.duration); // I can get the length of the audio
audio.play();
// is there a way I can get the current playhead position so I can make a progress bar?
@rserota
Copy link
Owner

rserota commented Nov 26, 2021

There isn't a single property that provides this information currently, but you can compute the current position like this:

const audioClip = new Wad({source: '/audio/song.wav'})
audioClip.play()

let playheadPosition = Wad.audioContext.currentTime - audioClip.lastPlayedTime

I hope that helps. Cheers!

@W-KE
Copy link
Author

W-KE commented Nov 27, 2021

Kind of, this only works when you have a fixed playback rate, the position in samples will mismatch with the position in seconds calculated this way when playback with a variable rate.
For example, the audio is played with the rate of 1 for 1 second, then sample position is at 44100, and then played with the rate of 2 for 1 second, now the sample position is 3 * 44100, but the position in second calculated with Wad.audioContext.currentTime - audioClip.lastPlayedTime is still 2 seconds, which does not make sense anymore, and can not tell very much information about how many percentage of the audio clip was played.

@rserota
Copy link
Owner

rserota commented Nov 27, 2021

Yeah, my solution won't work if you need to adjust the playback rate dynamically, but it's the best solution I can offer you. This is a known issue in the Web Audio API. There's a proposal to add a position attribute to the AudioBufferSourceNode, but it hasn't been implemented yet.
WebAudio/web-audio-api#2397

@rserota rserota closed this as completed Dec 10, 2021
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

2 participants