Search

Search papers and lessons

SoundStream: An End-to-End Neural Audio Codec

Neil Zeghidour, Alejandro Luebs, Ahmed Omran et al. · 2021

SoundStream (Zeghidour et al., 2021) trains a convolutional encoder, a residual vector quantizer and a decoder together; at 3 kbps it beats Opus at 12 kbps in listening tests.

On this page

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

TL;DR

SoundStream is a neural audio codec. A convolutional encoder turns a waveform into a short sequence of embeddings, a residual vector quantizer turns each embedding into a few small integer codes, and a decoder rebuilds the waveform from those codes. All three parts are trained together with adversarial and reconstruction losses. In listening tests at 3 kbps it beat Opus at 12 kbps and approached EVS at 9.6 kbps, and one model covers bitrates from 3 to 18 kbps.

Builds on: Vector Quantization, Residual Vector Quantization, Generative Adversarial Network, Dilated Convolution, Causal Convolution · Leads to: EnCodec, a later neural codec built on the same ideas

Why it mattered

Speech and music are sent over the internet at low bitrates by traditional codecs such as Opus and EVS. These are carefully engineered signal-processing systems. The paper notes that Opus runs inside applications such as WebRTC services, and that it is one of the main codecs used for streaming on YouTube. Any gain in rate-quality trade-off therefore lands on very large volumes of traffic.

SoundStream's central result is the size of that gain. At 3 kbps, listeners preferred it to Opus at 12 kbps, and to match its quality EVS needed at least 9.6 kbps and Opus at least 12 kbps. That is 3.2 to 4 times the bits. The authors also claim it is the first neural codec shown to beat both Opus and EVS over this broad bitrate range. The model runs in real time on one CPU thread of a smartphone.

The design also changed how neural codecs are built. Earlier learned codecs were trained for one bitrate, were limited to speech, or fixed the features and learned only a decoder. SoundStream trains every part end-to-end and uses one set of weights for every bitrate.

The problem

Waveform codecs keep audio faithful at medium and high bitrates, but at low bitrates they produce coding artefacts. Parametric codecs handle low bitrates by assuming a source model, usually speech, so they struggle with music and general audio. A codec that is good at both low rate and general content has to learn the representation from data.

A learned codec needs a bottleneck that can be trained by gradient descent and that stays small. Vector quantization offers that, but it has a cost problem. At 6 kbps with 75 frames per second, each frame gets 80 bits. A plain quantizer for 80 bits would need a codebook of 2802^{80} vectors, which cannot be stored or searched. The paper therefore needs a quantizer whose size grows with the number of bits in a practical way.

A codec also has to serve many bitrates without retraining, and it has to run on a phone with little delay. The paper's answer to both is in the structure of the model, not in extra training data.

How it works

The codec is a pipeline with a learned encoder, a quantizer and a decoder, and a set of discriminators that are used only during training.

flowchart LR
  A["24 kHz waveform"] --> B["Causal conv encoder, strides 2 4 5 8"]
  B --> C["Embedding every 320 samples"]
  C --> D["Residual VQ: N_q stages of 1024 codes"]
  D --> E["Sum of selected codes"]
  E --> F["Transposed-conv decoder"]
  F --> G["Reconstructed waveform"]
  1. Frame the waveform. At 24 kHz, the product of the encoder strides is 2×4×5×8=3202 \times 4 \times 5 \times 8 = 320, so one embedding covers 320 samples. Check: 24000/320=7524000 / 320 = 75 embeddings per second.
  2. Encode with causal convolutions. The encoder is a stack of blocks. Each block has three residual units with dilated convolutions, dilation rates 1, 3 and 9, followed by a strided convolution that down-samples. The channel count doubles at each down-sampling step. Every convolution is causal, so padding is applied only to the past. Check: a sample's embedding never waits for future audio, so the latency comes only from the frame length.
  3. Quantize residually. The first codebook replaces the embedding with its nearest code. The leftover, the residual, is quantized by a second codebook, and so on for NqN_q stages. The reconstruction is the sum of the chosen codes. With 1024 codes per stage, each stage costs 10 bits. Check: 8 stages give 80 bits per frame, and 75×80=600075 \times 80 = 6000 bits per second, which is 6 kbps.
  4. Decode. The summed embedding goes through a convolution and then mirrored blocks. Each block up-samples with a transposed convolution and then runs the same three residual units, with the strides in reverse order. A final one-filter convolution produces the waveform. Check: the strides multiply back to 320, so the output has the same length as the input.
  5. Score the output during training. Three waveform discriminators work at three resolutions, and one spectrogram discriminator works on the complex short-time Fourier transform with a window of 1024 and a hop of 256. The generator is trained with an adversarial loss, a feature-matching loss from inside the discriminators, and a multi-scale spectral loss. Check: the feature weight is 100, the other two weights are 1.
  6. Choose a bitrate at run time. During training, the number of stages nqn_q is drawn at random from 1 to NqN_q for each example, so the decoder learns to work from any prefix of the stages. At run time, nqn_q sets the bitrate. Check: at 3 kbps, nq=4n_q = 4, since 4×10×75=30004 \times 10 \times 75 = 3000 bits per second.

The math

Let xx be the waveform, enc\text{enc} the encoder, QQ the residual quantizer and dec\text{dec} the decoder. The generator is G(x)=dec(Q(enc(x)))\mathcal{G}(x) = \text{dec}(Q(\text{enc}(x))), and the reconstruction is x^=G(x)\hat{x} = \mathcal{G}(x).

Step 3 in symbols: residual quantization

Start with r0=er_0 = e, where ee is an embedding. For stage ii with codebook Ci\mathcal{C}_i, choose the nearest code cic_i to ri−1r_{i-1} and update the residual:

ci=arg⁡min⁡c∈Ci∥ri−1−c∥,ri=ri−1−cic_i = \arg\min_{c \in \mathcal{C}_i} \lVert r_{i-1} - c \rVert, \qquad r_i = r_{i-1} - c_i

The quantized embedding is e^=∑i=1nqci\hat{e} = \sum_{i=1}^{n_q} c_i. If each codebook has NN entries, the stage costs log⁡2N\log_2 N bits, so nqn_q stages cost nqlog⁡2Nn_q \log_2 N bits per frame. At S=75S = 75 frames per second, the bitrate is S nqlog⁡2NS \, n_q \log_2 N. With N=1024N = 1024 this is 750 nq750 \, n_q bits per second.

Step 1 in symbols: the frame rate

With TT input samples and stride product R=320R = 320, the encoder outputs S=T/RS = T / R embeddings, a rate of fs/Rf_s / R per second. At fs=24000f_s = 24000 Hz this is 75.

Step 5 in symbols: the losses

The discriminator uses a hinge loss, averaged over time tt. The paper also averages over its discriminators, and the formulas below show one discriminator D\mathcal{D} for brevity:

LD=Ex[max⁡(0,1−D(x))]+Ex[max⁡(0,1+D(G(x)))]\mathcal{L}_{\mathcal{D}} = \mathbb{E}_x\left[\max\bigl(0, 1 - \mathcal{D}(x)\bigr)\right] + \mathbb{E}_x\left[\max\bigl(0, 1 + \mathcal{D}(\mathcal{G}(x))\bigr)\right]

The adversarial loss for the generator is LGadv=Ex[max⁡(0,1−D(G(x)))]\mathcal{L}^{\text{adv}}_{\mathcal{G}} = \mathbb{E}_x\left[\max\bigl(0, 1 - \mathcal{D}(\mathcal{G}(x))\bigr)\right]. The feature loss compares the intermediate activations D(l)\mathcal{D}^{(l)} of the discriminator on real and decoded audio:

LGfeat=Ex[1L∑l1Tl∑t∣Dt(l)(x)−Dt(l)(G(x))∣]\mathcal{L}^{\text{feat}}_{\mathcal{G}} = \mathbb{E}_x\left[\frac{1}{L} \sum_{l} \frac{1}{T_l} \sum_{t} \bigl|\mathcal{D}^{(l)}_t(x) - \mathcal{D}^{(l)}_t(\mathcal{G}(x))\bigr|\right]

The spectral loss sums over window lengths s∈{26,…,211}s \in \{2^6, \ldots, 2^{11}\} an L1L_1 term on mel spectra and a log term weighted by αs=s/2\alpha_s = \sqrt{s/2}. The generator loss is

LG=1⋅LGadv+100⋅LGfeat+1⋅LGrec\mathcal{L}_{\mathcal{G}} = 1 \cdot \mathcal{L}^{\text{adv}}_{\mathcal{G}} + 100 \cdot \mathcal{L}^{\text{feat}}_{\mathcal{G}} + 1 \cdot \mathcal{L}^{\text{rec}}_{\mathcal{G}}

Step 6 in symbols: quantizer dropout

For each training example, sample nqn_q uniformly from {1,…,Nq}\{1, \ldots, N_q\} and use only the first nqn_q quantizers. The embedding dimension does not change with nqn_q, so the same encoder and decoder serve every bitrate.

Joint enhancement in symbols

The denoising switch enters through FiLM layers, which scale and shift an activation an,ca_{n,c} by values γn,c\gamma_{n,c} and βn,c\beta_{n,c} computed from a one-hot mode signal: a~n,c=γn,can,c+βn,c\tilde{a}_{n,c} = \gamma_{n,c} a_{n,c} + \beta_{n,c}.

Worked examples

1. Tiny: bits and bitrate

  1. Frames per second: 75. Check: from the stride product 320 at 24 kHz.
  2. Bits per frame with 8 stages of 1024 codes: 8×10=808 \times 10 = 80. Check: log⁡21024=10\log_2 1024 = 10.
  3. Bitrate: 75×80=600075 \times 80 = 6000 bits per second, which is 6 kbps.
  4. Interpretation: halving the stages to 4 gives 3 kbps, and doubling them to 16 gives 12 kbps. The codebook stays the same size at every bitrate, and only the number of stages changes.

2. Standard: residual quantization on a number line

Take the embedding e=2.7e = 2.7. Stage 1 has codes {0,2,4}\{0, 2, 4\}, stage 2 has codes {0,0.5,1}\{0, 0.5, 1\}, and stage 3 has codes {0,0.25,0.5}\{0, 0.25, 0.5\}.

  1. Stage 1: nearest code to 2.7 is 2, since ∣2.7−2∣=0.7|2.7 - 2| = 0.7 and ∣2.7−4∣=1.3|2.7 - 4| = 1.3. Residual: 0.70.7.
  2. Stage 2: nearest code to 0.7 is 0.5, since ∣0.7−0.5∣=0.2|0.7 - 0.5| = 0.2 and ∣0.7−1∣=0.3|0.7 - 1| = 0.3. Residual: 0.20.2.
  3. Stage 3: nearest code to 0.2 is 0.25, since ∣0.2−0.25∣=0.05|0.2 - 0.25| = 0.05. Residual: −0.05-0.05.
  4. Reconstruction: 2+0.5+0.25=2.752 + 0.5 + 0.25 = 2.75. Check: the error is 2.7−2.75=−0.052.7 - 2.75 = -0.05, which equals the last residual exactly.
  5. Interpretation: each stage removes most of what the stages before it left, so a few small codes reach a precise value. This is why residual stages make a large effective codebook from small ones.

3. Realistic: the learned encoder at half the bitrate

The paper's ablation at 6 kbps compares a learned encoder with a fixed mel-filterbank encoder. The ViSQOL quality score is 3.96 for the learned encoder and 3.33 for the mel encoder. The learned encoder at 3 kbps scores 3.76.

  1. Quality gap at 6 kbps: 3.96−3.33=0.633.96 - 3.33 = 0.63 points.
  2. Relative size of that gap: 0.63/3.96≈15.90.63 / 3.96 \approx 15.9 percent of the learned score.
  3. Learned encoder at 3 kbps against mel encoder at 6 kbps: 3.76−3.33=0.433.76 - 3.33 = 0.43 points. Check: the learned model at half the bitrate still scores higher than the mel model at full bitrate.
  4. Interpretation: the learned encoder buys more than a bitrate doubling does. The paper's wording is that the extra complexity of a learnable encoder gives a very significant improvement in the rate-quality trade-off.
  5. Try it: how much above the mel model's 6 kbps score is the learned model's 3 kbps score, as a percentage of 3.33?

Answer: 0.43/3.33≈0.1290.43 / 3.33 \approx 0.129, so about 12.9 percent higher.

Common mistakes and tradeoffs

  • It is tempting to think a neural codec wins by being a better speech model. In fact the paper's gains hold across speech, noisy speech and music, and it calls the 3 kbps music result the first time a codec operates on diverse content at such a low bitrate.
  • It is tempting to think more quantizer stages always help. In fact at a fixed bitrate, fewer stages with larger codebooks are the more efficient choice, at a higher computational cost. The paper's table shows 8 stages of 1024 codes at 4.01 and 80 stages of 2 codes at 3.92.
  • It is tempting to think a longer frame gives better quality for the same bits. In fact the paper's latency study finds the three stride settings equally good in quality, while longer frames, which mean more latency, raise the real-time factor (the audio duration divided by the processing time, so a value above 1 means faster than real time).
  • It is tempting to think the same stride and stage settings carry over to a new sampling rate. In fact the stride product fixes the frame in samples, so the frame duration and the bit budget per frame both change, and the model should be retrained at the new rate; the paper trains at 24 kHz.
Design choiceBenefitCost
Learned encoderMuch better rate-quality trade-offMore compute than mel features
Residual VQLarge effective codebook from small codesMore stages to run
Quantizer dropoutOne model for many bitratesSlight loss at the very lowest rate versus a model trained for it
Causal convolutionsStreaming with low latencyNo look-ahead, so less context per frame

Results

ResultNumber in the paper
3 kbps against OpusOpus needs 12 kbps or more to match, so 4 times the bits
3 kbps against EVSEVS needs at least 9.6 kbps to match, so about 3.2 times the bits
Medium bitrate, 6 kbpsEVS and Opus need 2.2 to 2.6 times as many bits
High bitrate, 12 kbpsEVS and Opus need 1.3 to 1.6 times as many bits
Music at 3 kbpsBetter than Opus at 12 kbps and EVS at 5.9 kbps
ViSQOL across 3 to 18 kbpsStays above 3.7 even at 3 kbps
Entropy coding on topPotential saving of 7 to 20 percent
Learned vs mel encoder at 6 kbpsViSQOL 3.96 vs 3.33
Quantizer depth at 6 kbps8 stages of 1024 give 4.01; 80 stages of 2 give 3.92
Real-time factor, default modelAbove 2.3 on one CPU thread of a Pixel 4
Architectural latency, default320 samples, about 13 ms per frame at 24 kHz

Bitrate scalability. The single model trained with quantizer dropout matches models trained for one bitrate at 6 and 12 kbps, and is slightly worse at 3 kbps. Without dropout, a model trained at 18 kbps degrades when run at lower bitrates, and the gap grows as the bitrate falls.

Joint compression and enhancement. A conditioning signal turns background-noise suppression on or off at run time, with no extra latency. The paper reports that adding the conditioning at the encoder or at the decoder both work.

Limits. The main listening test used 200 clips of 2 to 4 seconds, each rated 20 times by crowd raters, so it measures perceived quality in short clips. ViSQOL was chosen for model selection because it correlates with listening results and is freely available, and PESQ and POLQA were set aside because of licensing restrictions. The encoder is a real cost: the paper's table shows that lighter encoders run much faster with little quality loss, and that a lighter decoder hurts more. The constant-bitrate model does not exploit the 7 to 20 percent entropy-coding saving in its main results.

Reading guide

Check yourself

QuestionWhat is quantizer dropout, and what does it let one model do?Show answer
For each training example, the number of quantizer stages is sampled uniformly from one to NqN_q, and only those stages are used. The decoder learns to work from any prefix of the stages. Because the embedding dimension does not change with the number of stages, one model serves every bitrate without changing the architecture.
QuestionA codec uses 16 quantizer stages with 1024 codes each, at 75 frames per second. What is its bitrate?Show answer
Each stage costs log⁡21024=10\log_2 1024 = 10 bits, so a frame costs 16×10=16016 \times 10 = 160 bits. Then 75×160=1200075 \times 160 = 12000 bits per second, which is 12 kbps.
QuestionWhy does a plain vector quantizer fail at 80 bits per frame, and how does the residual version avoid the problem?Show answer
A plain quantizer with 80 bits per frame needs 2802^{80} codebook vectors, which cannot be stored or searched. The residual version uses 8 stages of 1024 codes, which is 80 bits in total but only 8×1024=81928 \times 1024 = 8192 stored vectors. Each stage quantizes what the previous stages left.
QuestionA team wants to run a SoundStream-style codec at 16 kHz with a 40 ms latency budget and a 6 kbps target. What should they check?Show answer
The default stride product of 320 at 16 kHz gives 20 ms per frame, which fits the 40 ms budget. The frame rate is then 16000/320=5016000 / 320 = 50 frames per second, so 6 kbps needs 6000/50=1206000 / 50 = 120 bits per frame, which is 12 stages of 1024 codes. The model should be retrained at 16 kHz, since the paper trains at 24 kHz, and the stride and stage choices should be checked against the listening test at that rate.

Sources

Read the full paper

Neil Zeghidour, Alejandro Luebs, Ahmed Omran et al., 2021. arXiv:2107.03312