codec · lab

🎚️ how does a song shrink to 3% of its size and still sound fine

digital audio is a long list of numbers. a codec's whole job is deciding which of those numbers you would actually miss. this page builds one — badly, but honestly — in your browser: sampling a wave, the psychoacoustic trick that does most of the real work, and the neural codecs that turn sound into tokens.

nothing loaded yet

1.a wave becomes numbers

a microphone sees a continuous pressure wave. a computer needs a finite list of numbers. sampling asks "what's the amplitude right now?" some number of times per second; quantization rounds each answer to the nearest of a finite set of levels. do either one badly and you can hear it exactly — that's what these two sliders are for.

turn off anti-aliasing and drop the rate: you'll hear tones that were never in the recording — frequencies above half the new rate fold back down and impersonate real ones. turn off dither at low bit depth and quiet passages get a harsh, correlated crunch instead of soft hiss.

2.two ways to shrink

there are exactly two honest ways to make a file smaller. exploit redundancy — patterns the data repeats — and you can always get the original back exactly (that's FLAC, roughly 2:1, lossless). exploit irrelevance — information a human wouldn't notice missing — and you can never get it back (that's every lossy codec you've used). same three minutes of music, six very different diets.

wav (1411.2 kbps, CD-quality stereo PCM) · flac (~800 kbps, lossless) · mp3 (128 kbps) · opus (64 kbps) · opus voice mode (24 kbps) · neural, EnCodec-class (~3 kbps) — figures are round, illustrative averages, not guarantees for any one file.

3.store the recipe, not the wave

the short-time fourier transform rewrites a signal as "how much of each frequency, in each short slice of time" — a spectrogram. a pure tone is one loud bin and a wall of near-silence: one number worth keeping. noise lights up every bin at once — there's no redundant structure left to exploit. click anywhere on the spectrum below to add a partial to a tiny synth and hear the recipe you just wrote.

y-axis: dB relative to this window's loudest bin (not absolute level).

4.what your ear throws away

a loud tone doesn't just compete with quiet ones for attention — it makes nearby frequencies briefly, literally inaudible. that's masking, and it's where most of an mp3's compression actually comes from: a codec that knows exactly where your ear's blind spots are can spend zero bits on anything hiding in one.

prove it to yourself: one clip is the masker alone, the other has the probe hidden inside it — assigned to A/B at random. can you tell which is which?

5.spending the bits

real encoding is a budgeting problem: given a target bitrate, decide which STFT bins survive. bins under the masking threshold quantize to zero for free — the ear was never going to notice. everything else gets a coarser or finer step until the running bit-cost estimate hits the target. drag the slider down and watch which frequencies get sacrificed first.

the residual is original − encoded: everything the codec decided you wouldn't miss. it's boosted to an audible level for playback here — at high bitrate it's close to silence, which is itself the point.

6.when it breaks

every lossy codec has a party trick for embarrassing itself. these four are the classics — click one to hear it (and see why) at once.

7.beyond mp3

opus (RFC 6716, 2012) is the current state of the art for general-purpose audio: a hybrid of SILK (built for speech) and CELT (built for music), switchable frame by frame, with as little as 5 ms of algorithmic latency — good enough that it quietly replaced almost everything else in real-time voice and video. neural codecs go one step further: instead of a hand-designed transform and masking curve, a model learns to map short chunks of audio onto a small set of discrete codes. SoundStream (Zeghidour et al., 2021) and EnCodec (Défossez et al., 2022) both do it with residual vector quantization: one small codebook can't capture everything, so a second codebook cleans up its leftovers, and a third cleans up what's left after that. sound becomes tokens — the same trick that lets a language model "speak".