javascriptweb-audio-apiaudiobuffer

Arranging AudioBuffers in JavaScript


I would like to write a program that "arranges" multiple audio buffers. Similar to a DAW, I want to "layer" audio tracks on top of each other at custom timestamps. Is this even possible, and if so, how would I implement it?

I apologize in advance for this extremely general question, but I couldn't find any good resources on this topic. Thanks for the help!


Solution

  • Yes, this is possible.

    First, load your audio data into AudioBuffer instances.

    Next, you want to create an AudioContext, which is basically the root of a graph of connected nodes where your audio flows about.

    Now, create an AudioBufferSourceNode for each AudioBuffer and connect it to the audio context's destinationNode. This basically plugs a buffer player directly to the output.

    From there, you can call .start() on your AudioBufferSourceNode instances to play them back immediately, or schedule them to play at some point in the future.