v160 · multimedia · web audio

Shared MessagePort for AudioWorklet

Every AudioWorkletNode has a port to its own processor — but until now Chrome had no channel to the AudioWorkletGlobalScope itself. Code that wanted to hand the audio thread a config object, a wavetable or WebAssembly bytes had to spin up a do-nothing “bootstrap” processor just to borrow its port. Chrome 160 exposes the Web Audio spec’s shared channel: audioContext.audioWorklet.port on the main thread, connected to a global port inside the worklet scope. Firefox has shipped it since 138.

Probing this browser…

concepts

  1. Global-scope handshake

    The canonical round-trip: the port exists before addModule(), the worklet module says hello during evaluation, you echo text into the audio thread and back, then close() the channel and watch replies stop.

  2. Port inspector

    An inspection table for both ends: the getter-only descriptor on AudioWorklet.prototype, identity across reads, one distinct port per BaseAudioContext, strict-mode readonly behaviour — and, from inside the worklet, typeof port, its prototype and what it sits next to.

  3. Shared tuning bus

    The product use case: one message on the shared port retunes every voice of a synth at once — no per-node fan-out. Change the reference pitch or temperament, render offline, and read the measured frequency of each voice; or play the chord live.

  4. Bootstrap-processor migration

    The workaround this feature retires, run for real beside the spec path: a dummy AudioWorkletNode created only to borrow a port. Plus edge probes — posting before addModule(), close(), per-context isolation, and why you send WebAssembly bytes rather than a WebAssembly.Module.

  5. Worker direct line

    Advanced composition: hand one end of a MessageChannel to the audio global scope through the shared port and the other to a Worker. The worker synthesises a wavetable and transfers it straight to the audio thread — the main thread never touches a sample.

the API

// main thread
const ctx = new AudioContext();
ctx.audioWorklet.port.onmessage = (e) => console.log("from global scope:", e.data);
await ctx.audioWorklet.addModule("synth.js");
ctx.audioWorklet.port.postMessage({ type: "tuning", a4: 442 });

// synth.js — runs in AudioWorkletGlobalScope
const shared = { a4: 440 };             // module-level state every processor reads
port.onmessage = (e) => { if (e.data.type === "tuning") shared.a4 = e.data.a4; };
port.postMessage({ type: "ready", sampleRate });   // allowed during module evaluation
registerProcessor("voice", class extends AudioWorkletProcessor { /* reads shared.a4 */ });

Feature-detect with "port" in AudioWorklet.prototype and keep the bootstrap-processor path only as a fallback for engines without it.

how to run it

Ships enabled by default in Chrome 160 (Blink runtime feature AudioWorkletSharedPort, status “stable”). On an earlier Canary/Dev build that carries the implementation but not the default, enable chrome://flags/#enable-experimental-web-platform-features, or launch with --enable-blink-features=AudioWorkletSharedPort (equivalently --enable-features=AudioWorkletSharedPort). Chrome 153 and older do not contain the implementation at all, flag or not — every concept page says so and, where it can, runs the labelled bootstrap fallback instead.

references