Skip to content

Synth

A polyphonic synth that runs in the browser tab. Click or touch the keys, or play the computer keyboard: A to ' are the white keys, W to P the black keys, and Z and X change the octave. The sound starts at the first click or key.

Synth

Full page version: joellof.com/synth/app/

  • Eight voices. A ninth note takes the oldest voice, with a 5 ms fade so the steal does not click.
  • Nine waveforms. Sine, PolyBLEP saw and square, triangle, and five band-limited wavetables: mellow saw, electric piano, organ, violin and a 25% pulse.
  • An ADSR envelope on each voice, from 1 ms to 10 s.
  • A low-pass filter on each voice: a two-pass state-variable filter with the cutoff from 20 Hz to 20 kHz and the resonance as Q from 0.1 to 30.
  • A waveform scope of the output, and the master volume.

The DSP is Rust with no crates, no allocator and no wasm-bindgen. It compiles to a 14 KB WebAssembly module with a C ABI: init, note_on, note_off, set_param and render.

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 page fetches the module and passes its bytes to an AudioWorkletProcessor in processorOptions. The processor compiles them, writes silence until the module is ready, and then calls render once per 128-frame block on the audio thread. The notes and the settings go to the processor as messages on its port, so a slider never stops a held note.

DSPRust, compiled to WebAssembly with no crates
AudioWeb Audio API, AudioWorklet
UIReact, TypeScript
BuildBun, Cargo