Visualizations
380 / 500

380 · Sound

Perceptual Codec

Watch a psychoacoustic model decide which parts of the music you will never miss.

A loop of music synthesized in code is cut into overlapping sine-windowed blocks and transformed with an FFT-based MDCT, the lapped transform AAC and Vorbis use. A psychoacoustic model on a Hann-windowed FFT gathers energy into Bark-scale critical bands, smears it with the Schroeder spreading function, lowers it by a tonality-dependent offset and floors it with the absolute threshold of hearing, and a rate loop bisects one global offset until the estimated bits fit the bitrate. Each band gets a quantizer step, every line under half a step rounds to zero and turns red, and the decoder's inverse MDCT and overlap-add rebuild what is left. Scrolling spectrograms show the holes and swirling 'birdies' at low rates, and a close-up of the latest attack shows pre-echo, the noise long blocks smear in front of a click.

Try it. Drag the bitrate slider, or drag up and down on the spectrum, anywhere from 16 to 320 kbps (keys 1 to 5 jump to presets). Click for sound and switch between the Original, the Codec output and what it Removed (O, C and R, or Space to A/B), and try Short blocks (B) to cure pre-echo at the cost of bits. A toggles the bitrate sweep.

  • MDCT
  • Psychoacoustic masking
  • Bark scale
  • Rate control
  • Web Audio

View the source · one module, plus a small shared runtime for sizing, the animation loop and input

Build your own

Paste this into Claude Code, Codex or any coding agent to get a simple version running, then take it wherever you like.

Build a tiny perceptual audio codec you can watch, with JavaScript, the HTML canvas element and Web Audio. Put everything in a single index.html file with no libraries or build step, so I can open it directly in a browser.

Start simple:
- Synthesize a few seconds of music in code into a Float32Array at 44.1 kHz: a few decaying chords made of sine partials, a bass line, and noise-burst hi-hats.
- Cut it into 2048-sample blocks with 50% overlap, multiply by a sine window and take the MDCT of each block (1024 coefficients). You can compute it directly at first; later through an FFT. Check that the inverse MDCT with the same window and overlap-add gives the original back.
- For each block, compute a Hann-windowed FFT power spectrum, group it into critical bands on the Bark scale, z = 13 atan(0.00076 f) + 3.5 atan((f / 7500)^2), and set a masking threshold a fixed number of dB below each band's energy, but never below the absolute threshold of hearing.
- Quantize every MDCT coefficient with a step size per band chosen so the quantization noise (step^2 / 12) sits under that threshold. Anything smaller than half a step becomes zero.
- Draw the current block: spectral lines over a Bark axis, kept lines bright and zeroed lines red, with the threshold as a glowing curve.

Once that works, make it beautiful:
- Spread energy between bands with the Schroeder spreading function, add a rate loop that shifts every threshold up or down until the estimated bits fit a bitrate slider, and show the bits saved against 16-bit PCM.
- Add scrolling spectrograms of the original and the decoded audio so holes left by the codec are visible.
- After a click, play the original, the decoded audio, or the difference between them through Web Audio.

Explain the key ideas in short code comments. When you're done, tell me how to open it and suggest three directions I could take it next, such as short blocks to cure pre-echo, a bit reservoir, or stereo mid/side coding.
PreviousNeural EcosystemHerbivores and predators with tiny neural brains evolve in a pastel petri dish. NextNeutrophil ChaseA crawling white blood cell hunts a bacterium among red cells, in grainy phase contrast.

Related visualizations

  • Spectrogram PainterSound Paint sound in time and frequency, then watch the real spectrogram of what you hear.
  • Bytebeat MachineSound Whole songs from one line of bit arithmetic, drawn as a tapestry of bytes.
  • Generative MusicSound A self-composing synthesizer you can see, played with the Web Audio API.
  • Granular CloudSound Granular synthesis made visible: every grain is a glowing droplet shaped by its window.
  • FM SynthesisSound A four-operator FM synth whose sidebands land exactly where Bessel functions say.
  • Modular SynthSound A eurorack case with a working signal graph and patch cables that sag and swing.
  • DatamoshRetro A real motion-compensated codec, fed a broken stream until the video melts.
  • Vinyl GrooveSound Zoom 1,500x from a turntable into the groove, cut from a synthesized track.
  • Phase MusicSound Two players loop one figure, one slightly faster, printing a moire record of the drift.

Use ← and → to move between demos. While the canvas has focus, keys go to the demo instead.

← More from Emergent Mind Labs