Procedural music and sound effects with the Web Audio API
web-audiosynthesisjavascriptschedulingsound-design
A browser app can generate all of its music and sound effects from oscillators and noise,
with no audio files at all. That keeps a page self-contained (no CDN, no assets, safe under
a strict Content Security Policy) and tiny. The techniques below come from "The Big Top"
(~/dev/circus/index.html, July 2026), a single-file circus experience whose whole band,
crowd and effects are synthesized.
Unlocking audio
Browsers keep an AudioContext suspended until a user gesture. Create or resume() it
inside the first meaningful click (in the circus, tearing the "ADMIT ONE" ticket), and
make that click part of the experience instead of a bare "enable sound" button.
Voices from primitives
- Calliope / steam organ: two slightly detuned square oscillators plus a triangle an octave up, with about 6.2 Hz vibrato from an LFO into the oscillator frequency.
- Tuba "oom" on beats 1 and 3 and short chord stabs ("pah") on 2 and 4 give the oom-pah march feel.
- Snare: a short burst of white noise through a bandpass or highpass with a fast decay envelope.
- Crowd bed: looped lowpassed noise with a slow LFO on gain.
- Applause: a buffer of random crackle impulses, a noise swell for cheering, and a few sine whistles gliding upward.
- Boom: a sine dropping quickly in pitch plus lowpassed noise; pair it with a screen shake.
- Ducking: a helper that ramps the music bus gain down for N seconds under an announcement or drumroll, then back up.
Look-ahead scheduling
JavaScript timers jitter, so never start notes from setTimeout at the moment they should
sound. Use the standard look-ahead scheduler: a setInterval every ~60 ms schedules every
note whose start time falls within the next ~0.28 s, using audioCtx.currentTime-based
start times. Audio timing then stays sample-accurate even when the main thread stalls.
Encoding a melody
Store the tune as [midiNote, durationInEighths] pairs. MIDI to frequency:
440 * 2 ** ((midi - 69) / 12). Accompaniment (bass and chords) is indexed per bar, so:
- Every bar must sum to exactly the bar length (8 eighths in 4/4), and the bar count must equal the chord table length. One bar in the circus melody summed to 9 eighths and the band slowly drifted off the beat against the chord track. Write a check that sums each bar.
The circus band ran an "Entrance of the Gladiators"-style strain at 168 BPM, 8 bars of 8 eighths.
Accessibility
Ship a visible mute toggle, and honor prefers-reduced-motion for the visual side of
effects (no shake).
See also
- Browser synthesizer stack (Tone.js) and LLM-generated music as JSON
- Canvas 2D and SVG animation techniques for web scenes
- Zero-build web apps (vanilla JS, localStorage, single file)
Linked from
- Adaptive game audio (mood crossfades and procedural sound)
- Browser synthesizer stack (Tone.js) and LLM-generated music as JSON
- Canvas 2D and SVG animation techniques for web scenes
- Generating game music loops and a countdown you can hear
- Simulating Chladni figures (sand on a vibrating plate)
- Sonifying the planets' orbits ("Music of the Spheres")