AudioWorklet Usage

The current repository uses AudioWorklet in docs/demos/vgm.html when available. The player logic still runs on the main thread, but audio output timing is handled by the worklet path.

Related source: docs/js/vgm-output-worklet.js, docs/demos/vgm.html

Current Model

  1. The main thread owns VgmPlayer.
  2. The main thread renders float chunks with player.process(...).
  3. Those chunks are posted to an AudioWorkletNode.
  4. The worklet queues chunks and writes them to the audio output.

Why This Exists

Important Limitation

This is not yet a full "all audio logic is inside the worklet" design. The current implementation still computes VGM playback chunks on the main thread and streams them into the worklet queue.

Minimal Idea

await audioContext.audioWorklet.addModule("../js/vgm-output-worklet.js");

const node = new AudioWorkletNode(audioContext, "vgm-output-processor", {
  numberOfInputs: 0,
  numberOfOutputs: 1,
  outputChannelCount: [2],
});

node.port.postMessage({
  type: "enqueue",
  left: left.buffer,
  right: right.buffer,
}, [left.buffer, right.buffer]);

Good Next Reads

Back