Skip to content

Porting my synth's DSP off wasm-bindgen, and a filter that was an octave off

My browser synth runs its DSP, the signal processing, in Rust compiled to WebAssembly, inside an AudioWorklet, the browser's way to run audio code on the audio thread. Until November 2026 that Rust needed a lot around it. It built with Rust nightly, the waw-rs framework and wasm-bindgen with their crates (Rust packages) and tools, and a patched copy of web-thread. The worklet also needed a polyfill for TextEncoder.

On 28 November I decided to keep the DSP in Rust, with no crates at all, instead of porting it to TypeScript, so that the build is one cargo command on stable Rust. I did the port the next day.

The new DSP is a no_std library for wasm32-unknown-unknown with an empty list of dependencies. It has no allocator, and it exports six plain functions with a C ABI, so JavaScript can call them with numbers and needs no generated bindings: init, note_on, note_off, set_param, render and output_ptr. render writes up to 128 samples to a static buffer, and output_ptr says where that buffer is in the memory of the module:

apps/synth/dsp/src/lib.rs
/// Renders up to 128 frames and returns how many it wrote.
#[no_mangle]
pub extern "C" fn render(frames: u32) -> u32 {
    let (synth, tables) = state();
    synth.render(frames, tables)
}

/// The byte offset in `memory` of the 128 f32 samples that `render` writes.
#[no_mangle]
pub extern "C" fn output_ptr() -> *const f32 {
    state().0.output().as_ptr()
}

On the TypeScript side, a Float32Array looks straight at that buffer. Nothing is copied into the module, and the view is made once:

apps/synth/src/dsp.ts
// The DSP has no allocator, so the memory never grows and this view stays valid.
const output = new Float32Array(memory.buffer, outputPtr(), MAX_FRAMES);

The view stays valid because the memory never grows. When WebAssembly memory grows, its old buffer is detached, and every view on it goes empty. With no allocator, the DSP never grows it. The built module, dsp.wasm, is 13,983 bytes, and it has no imports, so the worklet needs no glue code to load it.

The page fetches the module and passes its bytes to the AudioWorkletNode in processorOptions. The processor compiles them with the async WebAssembly.compile, so process() never waits, and it writes silence until the module is ready.

Each voice of the synth has a low-pass filter, a Chamberlin state-variable filter, that runs twice per sample. The frequency coefficient decides the filter cutoff together with the sample rate. Running twice per sample makes the filter behave as if the sample rate were doubled, so the coefficient needs twice the rate too. I compared the port with the old Rust and found that the old version used the plain sample rate. Its -3 dB point sat at twice the cutoff, an octave too high:

A chart of the level of a sine note through the filter at a cutoff of 1 kHz, from 130 Hz to 8 kHz. Now the level is -3 dB at 1 kHz. Before the fix it was -3 dB near 2 kHz, an octave higher.
The level of a sine through the filter at a cutoff of 1 kHz and a resonance (Q) of 0.71. "Now" is the module, played note by note. "Before the fix" runs the same filter step in TypeScript with the old coefficient, and a test checks that TypeScript step against the module within 0.1 dB.

A second bug was in the same line. It capped the coefficient at 0.99, but with a low resonance and a high cutoff the filter still blew up to NaN. Now the limit comes from the resonance, and the coefficient stays under it.

The built dsp.wasm is in the repo, so CI needs no Rust. The build script writes the SHA-256 of the Rust sources next to it, and a test fails when the sources no longer match, so a stale module can't ship.

The tests play notes through the built module and measure what comes out. MIDI note 69 must come out at 440 Hz, counted from the zero crossings, and a saw through a low cutoff must lose energy above it.

You can play the result on the synth page. For C and C++, the same path goes through Emscripten, and I have a small example of that too:

I also do this kind of port for clients: services.