↑↓ move Enter open ⌘K find from any page 256 of 256 tabs
Loading the recording…

What a neural audio codec is

A language model reads integers from a fixed vocabulary; a waveform is 24,000 real numbers a second. A neural audio codec turns one into the other. An encoder network compresses the waveform into a short sequence of vectors, a quantizer replaces each vector by the ids of codebook entries, and a decoder network turns the ids back into sound. The ids are the codec's tokens; their bits per second are its bandwidth.

EnCodec is such a codec. The checkpoint on this page, facebook/encodec_24khz, has 14.9 M parameters, works at 24 kHz and was trained for 1.5, 3, 6, 12 and 24 kbps. Its encoder makes 75 frames a second of 128 numbers, and its quantizer turns each frame into up to 32 codes of 10 bits. The page records the whole 2.04 s LibriSpeech clip “where is my brother now”, every quantizer step at every frame, and the model's decode at each bandwidth.

From waveform to frames

The clip is stored at 16 kHz, so it is first resampled to the model's 24 kHz (up 3, low-pass, down 2): 48,960 samples. The encoder, drawn in the Audio → 75 frames a second column with this run's shapes, is a stack of one-dimensional convolutions. A first one with kernel 7 makes 32 channels. Four blocks follow, each a residual unit, an ELU and a strided convolution, that double the channels and shrink time by 2, 4, 5 and 8. A two-layer LSTM runs over the frames and is added to its input, and a last convolution maps 512 channels to 128. The strides multiply to the hop:

So the clip becomes 153 frames, the columns of the Encoder output heatmap. This is EnCodec's streaming variant: all padding goes before the first sample, so frame is computed only from the audio up to its own end, sample .

Residual vector quantization

A plain vector quantizer would replace each frame by the nearest of codebook vectors and send its id, bits. At 6 kbps a frame gets 80 bits, and the SoundStream paper points out that one codebook of vectors cannot be stored. A residual vector quantizer (RVQ) uses small codebooks in turn instead, each quantizing what the ones before it left:

The frame's tokens are , and decoding from the first of them reads the sum of their vectors, . EnCodec has codebooks of 1,024 vectors of 128, so 32 codes of 10 bits name one of sums while the model stores only 32,768 vectors.

The Residual quantization column draws this ladder at the selected frame: per codebook, the id it chose, what is left (dark), what its vector removed (light) and the clip's mean (tick). At frame 75, where the page opens, codebook 1 takes the residual's norm from 58.2 to 12.5, 78 % of it. Averaged over the clip, a norm of 11.0 is left after codebook 1 and 1.48 after all 32. The panel below the code ids, Codebook 1 at frame 75 when the page opens, spells out one step, with the distances to the five nearest vectors. The search expands , so all 1,024 distances come from one matrix product.

Bandwidth is a prefix

Because each codebook only refines what the earlier ones did, the first codes of a frame are the same whatever is; the recording checks this at every frame. Dropping the last codebooks lowers the bandwidth without encoding again:

The toolbar's bandwidth buttons keep = 2, 4, 8, 16 or 32 codebooks: 1.5 to 24 kbps, 3,060 to 48,960 bits for this clip. The Tokens: 32 codes per frame column shows every id, a column per frame and a row per codebook, with the rows the chosen bandwidth drops faded. Not every vector is used: across the 153 frames codebook 1 picks 94 distinct ids of its 1,024.

For the decoder to work from any prefix, training has to show it every prefix. SoundStream introduced quantizer dropout, a random number of codebooks for each example; EnCodec picks one of its bandwidths at random for each batch. The codebooks themselves are not learned by gradient descent. As in VQ-VAE, the gradient passes the non-differentiable by a straight-through estimator (the backward pass treats quantization as the identity), a commitment loss pulls the encoder's output toward its quantized value,

and each codebook vector follows the residuals assigned to it as an exponential moving average with decay 0.99. A vector that goes unused is replaced by a candidate sampled from the current batch.

Decoding and the bandwidth ladder

The decoder mirrors the encoder, with transposed convolutions in place of the strided ones, and turns the summed vectors back into 24 kHz audio. The Bandwidth ladder strip plays the input and the model's decode from the first codebooks of every frame, each with its log-mel spectrogram and two errors against the resampled input: the waveform's signal-to-noise ratio,

and the mean absolute difference of the two log-mel spectrograms in dB. On this clip the SNR rises from 3.5 dB at 1.5 kbps through 9.2 dB at 6 kbps to 15.7 dB at 24 kbps, while the mel error falls from 4.63 dB through 3.07 dB to 2.20 dB.

Waveform SNR is a strict measure for this codec, which is trained to sound right rather than to match samples. Its losses are an L1 distance on the waveform, L1 and L2 distances between mel spectrograms at several time scales, and adversarial and feature-matching losses from a multi-scale STFT discriminator. A loss balancer combines them, each weight setting the fraction of the gradient that its loss contributes.

Where EnCodec came from

Quantizing a residual with a second codebook is old speech coding: Juang and Gray's multiple-stage vector quantizer (1982) already did it. VQ-VAE (2017) learned codebooks inside a neural autoencoder. SoundStream (2021) put a residual vector quantizer between an adversarially trained encoder and decoder, with quantizer dropout for many bitrates. EnCodec (2022) added the multi-scale STFT discriminator, the loss balancer and a small optional transformer that entropy-codes the ids, which the paper reports saves about 25 to 40 % of the bandwidth.

EnCodec's codes then became tokens for language models. VALL-E (2023) generates speech from text as EnCodec's 6 kbps codes, 8 codebooks at 75 Hz: the first codebook frame by frame, then each of the other seven for all frames at once. DAC (2023) refined the codec. The Mimi tab continues with a codec designed for a language model: 12.5 frames a second, and a first code distilled toward speech features.

Papers

  1. High Fidelity Neural Audio Compression Alexandre Défossez, Jade Copet, Gabriel Synnaeve, Yossi Adi · TMLR 2022 EnCodec, the codec this tab records: streaming convolutional encoder and decoder with an LSTM, residual vector quantization, the multi-scale STFT discriminator and the loss balancer.
  2. SoundStream: An End-to-End Neural Audio Codec Neil Zeghidour, Alejandro Luebs, Ahmed Omran, Jan Skoglund, Marco Tagliasacchi · IEEE/ACM Transactions on Audio, Speech, and Language Processing 2021 The residual vector quantizer between a learned encoder and decoder, and quantizer dropout for one model at many bitrates.
  3. Neural Discrete Representation Learning Aaron van den Oord, Oriol Vinyals, Koray Kavukcuoglu · NeurIPS 2017 VQ-VAE: a learned codebook inside an autoencoder, the straight-through gradient and the commitment loss.
  4. Multiple stage vector quantization for speech coding Biing-Hwang Juang, A. Gray · ICASSP 1982 Multistage vector quantization: a second codebook quantizes the error the first one leaves, the idea behind residual quantizers.
  5. Neural Codec Language Models are Zero-Shot Text to Speech Synthesizers Chengyi Wang et al. · arXiv 2023 VALL-E: text to speech as language modelling over EnCodec's 6 kbps codes, 8 codebooks at 75 Hz.
  6. High-Fidelity Audio Compression with Improved RVQGAN Rithesh Kumar, Prem Seetharaman, Alejandro Luebs, Ishaan Kumar, Kundan Kumar · NeurIPS 2023 DAC, a later residual-quantized codec that improves the codebooks, the discriminators and the losses.

See also