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.
concepts
-
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, thenclose()the channel and watch replies stop. -
Port inspector
An inspection table for both ends: the getter-only descriptor on
AudioWorklet.prototype, identity across reads, one distinct port perBaseAudioContext, strict-mode readonly behaviour — and, from inside the worklet,typeof port, its prototype and what it sits next to. -
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.
-
Bootstrap-processor migration
The workaround this feature retires, run for real beside the spec path: a dummy
AudioWorkletNodecreated only to borrow a port. Plus edge probes — posting beforeaddModule(),close(), per-context isolation, and why you send WebAssembly bytes rather than aWebAssembly.Module. -
Worker direct line
Advanced composition: hand one end of a
MessageChannelto the audio global scope through the shared port and the other to aWorker. 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
- ChromeStatus entry
- Web Audio API — AudioWorklet.port
- Web Audio API — AudioWorkletGlobalScope.port
- WebAudio issue #2456 and PR #2507
- Emscripten PR #25354 — the bootstrap-processor workaround in the wild
- WPT: audioworklet-messageport.https.html
- issues.chromium.org/446920095 — implementation tracking
- MDN: AudioWorkletGlobalScope.port