Search

Search papers and lessons

High Fidelity Neural Audio Compression

Alexandre Défossez, Jade Copet, Gabriel Synnaeve et al. · 2022

EnCodec (Défossez et al., 2022) compresses 24 kHz and 48 kHz audio into a short stream of discrete codes with a convolutional encoder, residual vector quantization and a decoder, trained with adversarial losses.

On this page

  • Colour = topic mix
  • Calculus
  • Probability
  • Discrete math
  • Generative models
  • Linear algebra
  • Neural network basics
  • ReadReady to readNot yet

TL;DR

EnCodec is a neural audio codec. An encoder network turns a waveform into a short sequence of latent vectors, a quantizer replaces each vector with entries from small lookup tables, and a decoder rebuilds the sound from those table indices. The system is trained end to end with reconstruction losses and adversarial losses, so the decoder learns to produce audio that listeners rate highly. In the paper's listening tests it beats Opus at the same bitrate, and its 12 kbps model beats EVS at 9.6 kbps. The system runs faster than real time on one CPU core.

Builds on: Residual Vector Quantization, Generative Adversarial Network, Convolutional Neural Network, Straight-Through Estimator, Discrete Fourier Transform · Leads to: Autoregressive Model (the paper's optional language model over the codes)

Why it mattered

Streaming audio and video made up most internet traffic by 2021, and the paper cites a figure of 82 percent from Cisco for that year. Every call, podcast and song sent over a network needs an audio codec, so a small gain in quality at a fixed bitrate is worth a lot.

Neural codecs had been proposed before, with promising results, but the paper's main comparisons were against Opus and EVS, the strong traditional standards. EnCodec showed that a learned codec could run faster than real time on one laptop CPU core and, in human ratings across speech and music, beat Opus at the same bitrate and beat EVS with its 12 kbps model. Its design, a convolutional encoder and decoder, a quantized bottleneck of discrete codes and adversarial training, is a pattern that later audio systems have also used to turn sound into tokens.

The problem

Lossy compression asks for two things that pull against each other. The bitrate should be as low as possible, and the distortion that listeners hear should be as small as possible. A codec is judged by human perception, not by a formula, so any evaluation has to use listening tests.

Traditional codecs solve this with carefully engineered signal-processing steps. Opus and EVS are the strong baselines in the paper. Neural codecs can learn the transform instead, but they face two problems that the paper names. First, the model must cover a wide range of signals (speech, noisy speech, music and general sound) without overfitting or producing artifacts outside its training data. Second, it must compress efficiently in both computation and size. The paper addresses the second with residual vector quantization of the encoder output, and the first with a large, diverse training set and discriminator networks that act as learned perceptual losses.

How it works

The paper's pipeline runs in one direction at inference and has a second branch that is used only during training.

flowchart LR
  X["Audio x, 24,000 samples per second"] --> E["Encoder E: convolutions, LSTM"]
  E --> Z["Latent z, 75 vectors per second"]
  Z --> Q["Residual vector quantizer Q: up to 32 codebooks"]
  Q --> ZQ["Quantized z_q, from codebook indices"]
  ZQ --> G["Decoder G: mirrored convolutions"]
  G --> XH["Reconstruction x hat"]
  XH -.-> D["Discriminators: training only"]
  X -.-> D

Take one second of mono audio at 24 kHz as the running example. It has 24,000 samples.

  1. Input. The waveform is a sequence of 24,000 numbers, each between minus one and one. Check: one second at 24 kHz means 24,000 samples, by definition of the sample rate.
  2. Encode with convolutions. A first one-dimensional convolution with 32 channels and kernel size 7 is followed by four convolution blocks. Each block has a residual unit (two convolutions with a skip connection) and then a strided convolution that shortens the sequence. The strides are 2, 4, 5 and 8. Check: 2×4×5×8=3202 \times 4 \times 5 \times 8 = 320 samples shrink to one latent step, so the sequence becomes 24,000/320=7524{,}000 / 320 = 75 steps long.
  3. Model the sequence. Two LSTM layers mix information across the 75 steps, and a final convolution maps each step to a vector of size DD. Check: the latent is still 75 vectors, now carrying context from neighbouring steps.
  4. Quantize with residual codebooks. For each latent vector, the quantizer picks the closest entry in a codebook of 1024 vectors and records its index. It subtracts that entry, and the leftover is quantized by a second codebook, and so on. At 24 kHz the paper uses at most 32 codebooks. Check: each codebook index needs log⁡21024=10\log_2 1024 = 10 bits, so one codebook costs 10 bits per latent step.
  5. Count the bits. At 75 steps per second, each codebook costs 75×10=75075 \times 10 = 750 bits per second. Using 2 codebooks gives 1.5 kbps, and 32 codebooks give 24 kbps. Check: the raw input at 16 bits per sample is 24,000×16=38424{,}000 \times 16 = 384 kbps, so 24 kbps is a ratio of 16 to 1.
  6. Optionally compress the codes again. A small Transformer predicts the probability of each next index. An arithmetic coder uses those probabilities to store the indices in fewer bits. Check: the paper reports this stage cuts the bandwidth by roughly 25 to 40 percent, which is a lossless step on top of the lossy one.
  7. Decode. The decoder mirrors the encoder with transposed convolutions and strides in reverse order. Before decoding, the chosen codebook entries are summed to rebuild the quantized latent. Check: the decoder output has 24,000 samples per second again.
  8. Train with feedback. During training, the reconstruction is compared with the input on the waveform and on mel-spectrograms, and a set of discriminators judges whether it sounds real. The discriminators are not used at inference.

The bandwidth knob is step 5. Training on a random number of codebooks lets one model serve every target bitrate, which is why the paper lists 1.5, 3, 6, 12 and 24 kbps as separate operating points.

The math

Write x∈[−1,1]Ca×T\bm{x} \in [-1,1]^{C_a \times T} for the input audio, with CaC_a channels (1 for mono, 2 for stereo) and T=d⋅fsrT = d \cdot f_{\mathrm{sr}} samples for a clip of dd seconds at sample rate fsrf_{\mathrm{sr}}. The three networks are an encoder EE, a quantizer QQ and a decoder GG:

z=E(x),zq=Q(z),x^=G(zq).\bm{z} = E(\bm{x}), \qquad \bm{z}_q = Q(\bm{z}), \qquad \hat{\bm{x}} = G(\bm{z}_q).

Step 4 in symbols: residual quantization

Let r0=z\bm{r}_0 = \bm{z}. For each codebook c=1,…,Nqc = 1, \dots, N_q, pick the nearest entry qcq_c of codebook cc to the residual rc−1\bm{r}_{c-1}, and set rc=rc−1−qc\bm{r}_c = \bm{r}_{c-1} - q_c. The quantized latent is the sum of the chosen entries:

zq=∑c=1Nqqc.\bm{z}_q = \sum_{c=1}^{N_q} q_c.

Each extra codebook makes the residual smaller, so the reconstruction gets closer to z\bm{z}. The symbol NqN_q is the number of codebooks used, which is set by the bandwidth target.

Step 5 in symbols: the bitrate

If the latent runs at fzf_z steps per second, each with NqN_q codebooks of KK entries, then

R=fz⋅Nq⋅log⁡2Kbits per second.R = f_z \cdot N_q \cdot \log_2 K \quad \text{bits per second.}

Here fz=75f_z = 75 and K=1024K = 1024, so R=750 NqR = 750 \, N_q. The value Nq=2N_q = 2 gives 1.5 kbps, and Nq=32N_q = 32 gives 24 kbps.

Step 8 in symbols: the training loss

The generator, which is the encoder and decoder together, minimises a weighted sum of five terms:

LG=λt ℓt+λf ℓf+λg ℓg+λfeat ℓfeat+λw ℓw.L_G = \lambda_t \, \ell_t + \lambda_f \, \ell_f + \lambda_g \, \ell_g + \lambda_{\mathrm{feat}} \, \ell_{\mathrm{feat}} + \lambda_w \, \ell_w.

The paper's 24 kHz setting uses λt=0.1\lambda_t = 0.1, λf=1\lambda_f = 1, λg=3\lambda_g = 3 and λfeat=3\lambda_{\mathrm{feat}} = 3. Each term is as follows.

  • Time-domain reconstruction: ℓt=∥x−x^∥1\ell_t = \|\bm{x} - \hat{\bm{x}}\|_1, the sum of absolute differences between the waveforms.
  • Frequency-domain reconstruction: ℓf\ell_f compares mel-spectrograms Si\mathcal{S}_i at several window sizes 2i2^i, for ii from 5 to 11, with an L1 term plus an L2 term. Comparing spectrograms matches how loudness and pitch are heard.
  • Adversarial: the discriminators DkD_k are trained with a hinge loss, and the generator with ℓg=1K∑kmax⁡(0,1−Dk(x^))\ell_g = \frac{1}{K} \sum_k \max(0, 1 - D_k(\hat{\bm{x}})), where KK is the number of discriminators.
  • Feature matching: ℓfeat\ell_{\mathrm{feat}} compares the internal layers of each discriminator for the real and generated audio, so the generator is pushed toward the same internal statistics as real sound.
  • Commitment: ℓw=∑c=1C∥rc−1−qc∥22\ell_w = \sum_{c=1}^{C} \|\bm{r}_{c-1} - q_c\|_2^2. This keeps the encoder's outputs close to the codebook entries they are assigned to. It applies only to the encoder.

Because quantization picks an entry, which has no gradient, the paper uses the straight-through estimator: during the backward pass, the quantizer is treated as the identity, so the gradient reaches the encoder.

The balancer

The weights λi\lambda_i are hard to tune when the losses have very different scales. The balancer rescales each loss's gradient gi=∂ℓi/∂x^g_i = \partial \ell_i / \partial \hat{\bm{x}} by its running average norm ⟨∥gi∥2⟩β\langle \|g_i\|_2 \rangle_\beta, with β=0.999\beta = 0.999:

g~i=R λi∑jλj⋅gi⟨∥gi∥2⟩β,R=1.\tilde{g}_i = R \, \frac{\lambda_i}{\sum_j \lambda_j} \cdot \frac{g_i}{\langle \|g_i\|_2 \rangle_\beta}, \qquad R = 1.

If the λi\lambda_i sum to 1, each λi\lambda_i is the fraction of the gradient coming from its loss, whatever the natural scale of that loss.

Results

The main numbers come from MUSHRA listening tests, in which crowd-sourced raters score each sample from 1 to 100 against a hidden reference and a low anchor. Table 1 gives the mean score for each category at 24 kHz, in the streamable setting.

ModelBitrateClean speechNoisy speechMusic set 1Music set 2
Referencenone95.593.993.297.1
Opus6 kbps30.119.120.617.9
Opus12 kbps76.561.977.865.4
EVS9.6 kbps84.480.089.987.7
Lyra-v23.0 kbps53.152.069.342.3
Lyra-v26.0 kbps66.259.975.748.6
EnCodec1.5 kbps49.241.368.266.5
EnCodec3.0 kbps67.062.589.687.8
EnCodec6.0 kbps83.169.492.991.3
EnCodec12.0 kbps90.680.191.892.9

The paper's central claim is that at the same bitrate EnCodec beats the baselines. The table shows the claim has limits. At 3 kbps, EnCodec's average over the four columns is about 76.7, above Opus at 12 kbps (about 70.4) and Lyra-v2 at 6 kbps (about 62.6). But on clean speech alone, Opus at 12 kbps scores 76.5 and EnCodec at 3 kbps scores 67.0, so the advantage is not uniform. At 6 kbps, EnCodec's average is about 84.2, slightly below EVS at 9.6 kbps (about 85.5). At 12 kbps it averages about 88.9, above EVS. So the lesson is that the win depends on the content and on the bitrate chosen for the comparison.

The entropy-coding stage moves each point left. The 3.0 kbps model drops to 1.9 kbps, the 6.0 kbps model to 4.1 kbps and the 12.0 kbps model to 8.9 kbps.

Other results in the paper:

  • Discriminators (Table 2). A multi-scale STFT discriminator alone gives a MUSHRA score of 77.5, with ViSQOL 4.35 and SI-SNR 6.67. Adding the multi-period discriminator gives 79.0 on MUSHRA with ViSQOL 4.34. The paper concludes that the STFT discriminator alone is nearly as good and simpler to train.
  • Streaming (Table 3). At 6 kbps, the non-streamable model gets SI-SNR 7.46 and ViSQOL 4.39, against 6.67 and 4.35 for the streamable one. Streaming costs a little quality for the ability to run on live input.
  • Stereo music at 48 kHz (Table 4). The 12 kbps stereo model, which entropy-codes down to 8.9 kbps, scores MUSHRA 88.0. That is above MP3 at 64 kbps (82.7) and Opus at 24 kbps (82.9).
  • Speed (Table 5). On a single thread of a 2019 laptop CPU at 6 kbps, the 24 kHz streaming model has an initial latency of 13 ms and a real-time factor of 9.8 for encoding and 10.4 for decoding. A real-time factor above 1 means faster than real time. With entropy coding the factor falls to 1.6.

Limits. The 48 kHz model is non-streaming and has an initial latency of one second, because its normalization uses the whole chunk. With entropy coding at 48 kHz the real-time factor is 0.68 for encoding and 0.66 for decoding, so it is slower than real time on that machine. The paper also notes that batch and streaming evaluation of the language model can differ by more than 10−810^{-8}, which can break decoding if the two sides do not agree exactly. The language model is small, and the paper suggests it struggles to model all codebooks together at high bitrates.

Reading guide

Read the paper in this order. Each section lists the lessons it depends on.

Check yourself

QuestionRecall: what are the two numbers that set the bitrate of EnCodec's residual quantizer at 24 kHz?Show answer

The number of latent steps per second, which is 75, and the number of codebooks used, NqN_q. Each codebook index takes log⁡21024=10\log_2 1024 = 10 bits, so the bitrate is 75×Nq×1075 \times N_q \times 10 bits per second. Changing NqN_q is how one model covers the whole range of bandwidths.

QuestionCompute: a 10-second clip at 24 kHz is encoded with 4 codebooks. How many bits does the quantized code take, and what is the bitrate?Show answer

The encoder makes 75 latent steps per second, so 10 seconds give 750750 steps. Each step has 4 codebook indices of 10 bits, so the total is 750×4×10=30,000750 \times 4 \times 10 = 30{,}000 bits. The bitrate is 75×4×10=300075 \times 4 \times 10 = 3000 bits per second, which is 3 kbps.

QuestionWhy: why does the paper use a straight-through estimator, and what would go wrong without it?Show answer

Picking the nearest codebook entry is a lookup, and a lookup has no useful gradient: a small change in the encoder output usually changes nothing in the selected entry. Without a gradient, the encoder could not learn from the decoder's errors. The straight-through estimator treats the quantizer as the identity in the backward pass, so the gradient reaches the encoder as if no quantization had happened. It is an approximation, and it works well enough for training.

QuestionApplied: a podcast host wants to send a 60-second voice recording at 24 kHz for the lowest size that still sounds clean. The paper's entropy coding moves 3 kbps to 1.9 kbps. How many bits does each option take, and by what percentage does the entropy stage reduce the size?Show answer

Without entropy coding at 3 kbps, the recording takes 60×3000=180,00060 \times 3000 = 180{,}000 bits. With entropy coding it takes 60×1900=114,00060 \times 1900 = 114{,}000 bits. The saving is 66,00066{,}000 bits, which is 66,000/180,000≈3766{,}000 / 180{,}000 \approx 37 percent. That is inside the 25 to 40 percent range the paper reports. The trade-off is that the entropy stage needs the decoder to run the same language model, so it adds compute and a little latency. The paper's clean-speech score at 3 kbps is 67.0, so the host should check the result on their own voice before choosing the lower rate.

Sources

Read the full paper

Alexandre Défossez, Jade Copet, Gabriel Synnaeve et al., 2022. arXiv:2210.13438