Reputation: 43
It's basically all in the title, how do I know or how can I access the duration of a soundNode
in Web Audio API. I was expecting something like source.buffer.size
or source.buffer.duration
to be available.
The only alternative I can think of in case this is not possible to accomplish is to read the file metadata.
Upvotes: 3
Views: 2425
Reputation: 1239
Assuming that you are loading audio, when you decode it with context.decodeAudioData the resulting arrayBuffer has a .duration property. This would be the same arraybuffer you use to create the source node.
You can look at the SoundJS implementation, although there are easier to follow tutorials out there too.
Hope that helps.
Upvotes: 1
Reputation: 56
Good and bad news; nodes do not have length - but I bet you can achieve what you want another way.
Audio nodes are sound processors or generators. The duration of a sound processed or made by a node can change - to the computer it is all the same, lack of sound is a buffer full of zeroes instead of other values.
So, if you needed to dynamically determine the duration of a sound, you could write a node which timed 'silences' in the input audio it received.
However I suspect from your mentioning of meta data that you are loading existing sounds - in which case you should indeed use their meta data, or determine the length by loading into an audio element and requesting the length from that.
Upvotes: 0