Neural audio codecs: SoundStream, EnCodec, DAC and Mimi
A neural codec squeezes audio into a few tokens per frame with residual vector quantization and rebuilds it. Work out bitrates, quantize a vector by hand, and see why these tokens became the alphabet of audio generators.
What runs in CodeSplash
- AUD037SoundStream Explained here
- AUD038EnCodec Explained here
- AUD039Descript Audio Codec (DAC) Explained here
- AUD067Mimi streaming semantic–acoustic codec Explained here
You will be able to
- Describe a neural codec's encoder, residual quantizer and decoder, and how it is trained
- Run residual vector quantization by hand in Python and R
- Compute a codec's nominal bitrate from frame rate, codebooks and codebook size
- Explain why codec tokens feed generators, and why they aren't interchangeable between codecs
Encode, quantize, decode
ReadA neural audio codecNeural audio codecAn encoder, quantizer and decoder trained to squeeze audio into a few tokens per frame and rebuild it. Its tokens also feed generators. has three parts. A convolutional encoder turns the waveform into a short sequence of vectors, tens per second. A quantizer replaces each vector with a few integers, the tokens. A decoder rebuilds the waveform from them.
Training mixes losses you've met: spectrogram errors at several resolutions, adversarial and feature-matching terms from GAN vocoders, and commitment terms from the VQ-VAEVQ-VAEAn autoencoder whose code snaps to the nearest of a fixed set of learned vectors, giving discrete codes instead of Gaussian ones.. The tokens are the point: they can be stored, sent, or modeled by a language model.
Residual quantization by hand
ReadOne codebook can't describe a vector finely. Residual vector quantizationResidual vector quantizationCodes a vector with a stack of codebooks, each coding what the earlier ones missed. More codebooks mean more bits and better sound. (RVQ) uses a stack: the first codebook picks the nearest code, the second codes what the first missed, and so on.
import numpy as np
z = np.array([0.9, 0.35]) # one encoder frame
books = [np.array([[1.0, 0.0], [0.0, 1.0], [-1.0, 0.0]]), # codebook 1: coarse
np.array([[0.0, 0.25], [-0.1, 0.0], [0.1, -0.1]])] # codebook 2: small corrections
residual, codes = z.copy(), []
for book in books:
k = int(np.argmin(((book - residual) ** 2).sum(axis=1))) # nearest code to what's left
codes.append(k)
residual = residual - book[k]
print(codes, round(float(np.linalg.norm(residual)), 3)) # leftover error per stagez <- c(0.9, 0.35) # one encoder frame
books <- list(rbind(c(1, 0), c(0, 1), c(-1, 0)), # codebook 1: coarse
rbind(c(0, 0.25), c(-0.1, 0), c(0.1, -0.1))) # codebook 2: small corrections
residual <- z; codes <- integer(0)
for (book in books) {
k <- which.min(rowSums(sweep(book, 2, residual)^2)) # nearest code to what's left
codes <- c(codes, k)
residual <- residual - book[k, ]
print(c(codes, round(sqrt(sum(residual^2)), 3))) # leftover error per stage
}The leftover error shrinks with each codebook. R counts codes from 1, Python from 0: the same choices.
Bits per second
ReadWith frames a second, codebooks and codes per book, each frame costs bits:
That is the nominal bitrateCodec bitrateFrames per second × codebooks × log₂(codebook size): a codec's nominal token payload before any entropy coding., before any entropy coding. SoundStream at 50 frames a second with books of 1,024 codes gives 500, 1,000, 1,500 and 2,000 bits a second for one to four books. Trained with quantizer dropout, randomly using only the first few books, one SoundStream model serves all those bitrates.
Reference · AUD037 · Explained hereSoundStreamA neural audio codec using an encoder, residual vector quantization, and decoder, trained with reconstruction and adversarial signals.EnCodec: tokens for generators
ReadEnCodec follows the same design, with a convolutional encoder and decoder, RVQ, and adversarial training against discriminators on spectrograms. Its tokens became the alphabet of Meta's audio generators, MusicGen and AudioGen among them.
The reference's example: 75 frames a second and 8 books of 1,024 codes give 6,000 bits a second, so ten seconds take 7,500 bytes of tokens. A generator trained on one EnCodec checkpoint's tokens can't read another's: the integers mean different things.
Reference · AUD038 · Explained hereEnCodecA neural audio codec using convolutional encoding/decoding, residual quantization, and adversarial perceptual training. It supports learned audio tokens used by multiple later generation systems;DAC: using the whole codebook
ReadCodebooks often go partly unused: a few codes do all the work and the rest are dead, wasting bits. The Descript Audio Codec (DAC) projects each vector into a small space and normalizes it before looking up the nearest code, which keeps codes in use and training steady. Better codebook use is much of why it sounds good at low bitrates.
In the reference's example a projected vector is closest to code 0 (counting from 0).
Reference · AUD039 · Explained hereDescript Audio Codec (DAC)A high-fidelity neural codec based on improved residual-vector-quantized adversarial modeling. Its design changes codebook representation and training to improve reconstruction and token utilization, making it useful…Mimi: a codec that streams and knows words
ReadMimi is built for live conversation. It runs causally, with cached state, so it can encode and decode as audio arrives, and its frame rate is very low: an 80 ms frame. Its first codebook is trained by semantic distillationSemantic distillation (codec)Training a codec's first codebook to match a speech model's features, so early tokens carry content and later ones sound detail. to match a self-supervised speech model's features, so the first tokens carry what is said and the later ones how it sounds.
With 8 active books of 2,048 codes at that frame rate, the nominal rate is 1,100 bits a second, far below EnCodec's. Mimi is the tokenizer under the Moshi dialogue model, not the dialogue model itself.
Reference · AUD067 · Explained hereMimi streaming semantic–acoustic codecA streaming neural audio codec combining perceptual reconstruction with semantic representation distillation. It supplies low-frame-rate acoustic tokens used by Moshi while retaining more content-related information in…Why generators want tokens
ReadA second of 24 kHz audio is 24,000 numbers; a codec makes it a few hundred integers. A language model can then generate audio the way it generates text: next-token prediction over codec tokens, a codec language modelCodec language modelA language model over neural-codec tokens. It writes audio as token sequences, and the codec's decoder turns them into sound.. VALL-E, AudioLM, MusicGen and Moshi all work this way, and the codec's decoder plays the result. The codec sets the ceiling: generated audio can't sound better than the codec's own reconstruction at that bitrate.
Where it stands in CodeSplash
Read| Entry | In CodeSplash |
|---|---|
| SoundStream (AUD037) | Explained here |
| EnCodec (AUD038) | Explained here |
| Descript Audio Codec (AUD039) | Explained here |
| Mimi (AUD067) | Explained here |
CodeSplash doesn't run audio codecs. The reference programs are component checks, verified in Python and R: bitrate arithmetic and a nearest-code lookup, where R's answer is one higher because R counts from 1.
Check: codecs
CheckpointCheck yourself
What does SoundStream's quantizer dropout give you?
Show the answer
Answer: b
Dropping later codebooks at random during training makes every prefix of the stack a usable code.
Check yourself
A codec runs at 50 frames a second with 6 codebooks of 1,024 codes. What is its nominal bitrate in bits per second?
Show the answer
Answer: 3000
.
Check yourself
A music generator was trained on tokens from one EnCodec checkpoint. Can you decode its output with another codec checkpoint?
Show the answer
Answer: b
Tokens are indexes into learned codebooks. Generator and codec must match exactly.
Check yourself
At 75 frames a second, 4 codebooks of 1,024 codes, how many bytes do 12 seconds of tokens take?
Show the answer
Answer: 4500
bits a second; bytes.
Check yourself
What codebook problem does DAC's projected, normalized lookup address?
Show the answer
Answer: a
Looking up codes in a small normalized space keeps more of them in use.
Check yourself
The reference's Python example picks code 0 and its R twin picks code 1. What does that mean?
Show the answer
Answer: b
Index bases differ between the languages; the choice is the same.
Check yourself
What does Mimi's semantic distillation change about its tokens?
Show the answer
Answer: b
Separating content from acoustic detail helps a language model over the tokens plan what to say.
Check yourself
What is Mimi's role in the Moshi system?
Show the answer
Answer: b
Mimi tokenizes and renders audio; Moshi's language model predicts the tokens.
Recap
Recap- A neural codec encodes, quantizes with a stack of codebooks, and decodes.
- Residual quantization codes what earlier books missed; nominal bitrate is .
- SoundStream serves many bitrates; EnCodec's tokens feed generators; DAC keeps its codes in use; Mimi streams and separates content from detail.
- Tokens only mean something to their own codec. All four are explained here.
Concepts in this lesson