📚 Encyclopedia

Audio & Music · updated 2026-10-13

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

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:

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

Linked from

Sources: circus project memory