Search code examples
audioweb-audio-api

How do I know the duration of a sound in Web Audio API


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.


Solution

  • 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.