Run audio.js atoms as Web Audio Modules / AudioWorklet nodes.
A single JS factory function with attached metadata becomes a WAM 2.0-shaped plugin — live numbers ride AudioParams; enums, bools, restart values, notes, the transport and later buffers come over the MessagePort; works in browsers and Node (web-audio-api).
Use
This adapter is part of the packed preview.
The package includes examples/gain.js, examples/compressor.js and demo/index.html.
After installing it, serve the consumer directory over HTTP and open
/node_modules/@audio/plugin-wam/demo/index.html to audition both processors.
The release check runs this installed demo in Chromium, Firefox and WebKit.
WAM-shaped metadata does not by itself establish compatibility with an independent WAM host.
import { toWam } from '@audio/plugin-wam'
import { gain } from '@audio/plugin-wam/examples/gain.js'
const wam = toWam(gain) // this example is self-contained
await wam.register(audioContext) // installs the factory as an AudioWorklet processor
const node = wam.create(audioContext, { channels: 2, parameterData: { value: -6 } })
source.connect(node).connect(audioContext.destination)
node.setParam('value', -12) // a live number → its AudioParam; an enum, a bool, a restart value → MessagePort
node.onemit = (name, args) => {} // events.out emissions (meters, detected pitch)
node.dispose() // releases the MessagePort (needed after offline render in Node)
For processors that import helpers or capture module-scope values, pass
toWam(factory, { url: new URL('./processor.js', import.meta.url).href }) so
the worklet imports the original module. The included compressor demo exercises this path.
Any package exposing an audio.js manifest (the "audio" field in its package.json) works — see the contract.
streaming: false (whole-render) atoms are refused at toWam() — they hold the entire timeline, so wrapping one in a 128-frame worklet would silently produce wrong output; render those offline via audio/batch instead.
What the node does for a processor
The node keeps the contract's "What a host owes": items 2 to 10 in full, item 1 as far as a worklet can (below); item 11, a plugin's bypass and value text, it does not add. Its tests run the contract's fixture suite (conformance.js at the repository root) against it in Node, and test what one render cannot show: a replaced instance, state, allocation.
Channels. channels gives every bus the processor declares as 'any' its count (default 2), or one count per input bus: [2, 1] is a stereo input and a mono key on input 1. The node takes exactly that many on each input (channelCountMode: 'explicit', so a source of other channels is mixed to them), and ctx.maxChannels is the widest bus. Another count is another node.
const node = wam.create(audioContext, { channels: [2, 1] })
key.connect(node, 0, 1)
Values. A live number is an AudioParam the processor reads at audio rate: a change inside a render quantum reaches it at its frame. The node splits the quantum where a value jumps and, while one moves, calls the processor every 32 frames at most, with each value at the call's first frame; smoothing ramps a jump, not a ramp you schedule. parameterData and setParam take text in the param's unit, clamp a number to its range, put it on its step, and refuse an enum value the processor does not declare.
node.setParam('attack', '10ms') // 0.01 to an attack declared in s
Restart values (restart: true, or flags: ['restart'] as manifests before 1.0 write it) are no AudioParam: setParam sends them, and automation cannot reach them. Once every restart value has held for 0.1 s, the node builds a new instance and hands over: from that quantum the new instance takes the input and new notes, while the old one is called with silent input and the offs of its notes, its output added, until its latency and tail have passed. A slider drag rebuilds once, after it stops. The new instance's latency replaces node.latency.
node.setParam('lookahead', 0.005) // a new instance 0.1 s after the last change
Notes, for a processor with events = { in: ['note'] }: time is the context's, in seconds (none: the next quantum). Each call's ctx.events holds the notes in it, sorted; an id is assigned where none is given, and an off pairs with the earliest sounding note of its midi.
node.note({ kind: 'on', midi: 60, velocity: 0.8, time: audioContext.currentTime + 0.5 })
node.note({ kind: 'off', midi: 60, time: audioContext.currentTime + 1 })
Transport. ctx.transport is { playing: false, bpm: 120, beat: 0, barStart: 0, timeSig: [4, 4] } until set; from the next quantum the beat goes on from where it is, moving with the frames while playing.
node.transport({ playing: true, bpm: 96, timeSig: [3, 4] })
Buffers are planar arrays at the context's rate (Float32Array[]; one Float32Array is one channel), refused when the processor does not take their channel count. ctx.buffers.ir is undefined when none is given. A new buffer is a new instance, handed over.
const node = wam.create(audioContext, { buffers: { ir: [left, right] } })
node.buffer('ir', [mono])
State. node.state() returns { [stableId]: value } with numbers in their unit and enums by value, and each buffer that is set under its name. node.state(saved) sets the live values and builds one new instance for the restart values and buffers; an id it does not know is skipped, a missing one takes its default.
const saved = node.state()
node.state(saved)
Latency and tail. node.latency is in frames, taken as the instance is built; node.tail is in seconds, read with the values and the transport in force (node.transport's, its beat moving with the context's time while playing). Both are asked on the page's thread, and in the worklet only as an instance is built or handed over (its latency, and the old one's tail with the transport where its input ends), never per quantum.
Input that holds a NaN or an Infinity reaches the processor as 0, in a copy; input bus 0 with nothing connected reaches it as silent channels.
What a worklet cannot do that the contract asks
- The processor is built on the render thread. An AudioWorklet has no other thread to build it on, so a new instance (a restart value, a buffer, a loaded state) is built inside the render quantum it starts in, its latency and the old instance's tail taken there. A processor that takes long to build, one that allocates a long delay line or analyses an impulse response, still glitches when it is replaced: the hand-over keeps the old instance's tail, not the quantum's deadline.
- An emission allocates.
ctx.emitposts a message, an object and its arguments' array: the one allocation per quantum the contract leaves a worklet. Nothing else allocates per quantum:test/alloc.test.jsdrives the generated processor through every path (a moving value, jumps, notes, the transport, an instance handed over, NaN input, an unconnected input) and V8's young generation takes in 104 to 464 bytes over 2^17 quanta, under the validator's line of 3/64 of a byte a quantum (Node 20.8, 22.22 and 25.9). - Values are read from samples. The node finds jumps and the points where a value starts or stops moving in the AudioParam's samples, not in the automation you scheduled: a jump while the value already moves is neither split at nor smoothed (it falls inside a call of 32 frames at most).
- Calls carry at most 128 frames, the render quantum; a context with a longer quantum stops the processor with an error.
- An endless tail (
Infinity) cannot be waited out: an instance replaced stops after its latency.
For batch/stream hosting without an AudioContext see audio/batch; for building experimental macOS VST3 plugins from the same source see @audio/plugin.