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.66k stars 130 forks source link

sound.duration() needed #109

Open kdemarest opened 5 years ago

kdemarest commented 5 years ago

I am chaining together sounds and visual animations in a game. In order to run an animation "when a sound ends" I need to know the sound's duration.

I'd rather not hard-code that information. Is it possible to say

let someDuration = sound.duration()

PS I'm converting from howlerjs and that code does provide the sound duration, though it is not clear to me how the achieve it.

michaelslevy commented 5 years ago

Hi Ken,

Check the sound object's source node. sound.sourceNode.buffer.duration

Michael

1hko commented 1 year ago

When I console.log(sound) I can see the sound.sourceNode property but I console.log(sound.sourceNode) I get undefined. Strange...

console.log(sound.__proto__) reveals some insight:

addEffect: function(e2)
applyAttack: function()
clone: function()
connect: function(e2)
connectEffects: function()
disconnect: function(e2)
frequency:
getInputNode: function()
getSourceNode: function()      // <-
onEnded: function(e2)
pause: function()
play: function(e2, t2)
removeEffect: function(e2)
stop: function()
stopWithRelease: function(e2)
sustain:
volume:

@types/pizzicato is unaware of this sound.getSourceNode() function, but indeed it exists and it does return the underlying source node. I had to do this:

sound.getSourceNode().buffer.duration
// => ...