Lunar Boom Learning

1.4 · Audio Tokens and Neural Codecs

Section 1.4 of 1.4

Audio Tokens and Neural Codecs

Turning long audio signals into shorter streams of learned symbols

Learn how neural codecs compress audio, how quantization creates discrete token streams, and why music models often generate tokens instead of individual waveform samples.

About 12 minutes

Guiding question

Why do music models turn audio into tokens when digital audio already contains numbers?

By the end, you’ll be able to
  • Describe the encoder, quantizer, codebook, and decoder stages of a neural audio codec.
  • Explain why generative models often use audio tokens instead of predicting every waveform sample.
  • Recognise the trade-off between bitrate, sequence length, reconstruction quality, and generation cost.

Digital audio is already made of numbers. At a sample rate of 32 kHz, one second of mono audio contains 32,000 sample values. A three-minute track contains millions of them.

A model could try to generate those values one at a time, but the sequence would be extremely long. Neural audio codecs offer a more manageable route. They learn how to compress short sections of audio into a smaller set of codes, then reconstruct the sound from those codes.

When the codes are chosen from fixed codebooks, they can be stored as discrete IDs. These IDs are often called audio tokens. A music model can learn to predict the token sequence, then a decoder turns the completed sequence back into audio.

Waveform samples and codec tokens

Swipe sideways to view the full comparison

PropertyWaveform samplesCodec tokens
What they representSignal amplitude at each sample momentLearned compressed patterns selected from codebooks
Sequence lengthVery longMuch shorter
ValuesNumerical sample valuesDiscrete token IDs
Directly playableYes, with the correct audio formatNo, a compatible decoder is needed
Main challengeLarge number of prediction stepsSome detail can be lost during compression

The neural codec pipeline

  1. The encoder reads the waveform

    A neural network analyses the audio and produces a smaller continuous representation called a latent representation.

  2. The quantizer selects codebook entries

    Each latent vector is replaced by one or more nearby entries from learned codebooks. The selected entries can be stored as integer IDs.

  3. The IDs form token streams

    The codec produces a sequence of discrete IDs across time. Some codecs produce several parallel streams from several codebooks.

  4. The decoder reconstructs audio

    The selected codebook vectors are passed to a neural decoder, which produces a waveform that should sound close to the original.

Quantization: turning smooth values into choices

The encoder first creates continuous values. Continuous means they can take many possible numerical values. A language model is easier to train when the possible outputs come from a fixed set, much like a vocabulary.

The quantizer solves this by replacing each latent vector with an entry from a learned codebook. Each codebook entry has an ID. The signal is now represented by a sequence of choices such as 18, 402, 77, and 911 rather than by a continuous stream of latent values.

This step is lossy. The selected entry is only an approximation of the original latent vector. A well-trained codec tries to keep the parts that matter most to perceived audio quality.

Why use several codebooks?

One codebook may capture a rough version of the signal but miss smaller details. SoundStream and EnCodec use residual vector quantization, often shortened to RVQ.

The first codebook approximates the latent vector. The system then measures what is still missing, called the residual. A second codebook approximates that leftover information. More codebooks can continue refining the result.

This creates a useful quality control. Using fewer codebooks gives a lower bitrate and a rougher reconstruction. Using more codebooks carries more information and can improve quality, but it also creates more tokens for a generative model to predict.

Examples in practice

Codec example: SoundStream

SoundStream introduced an end-to-end neural audio codec built from a convolutional encoder, residual vector quantizer, and decoder. The researchers trained the parts together for speech, music, and general audio. They also used structured dropout across quantizer layers so one model could operate at several bitrates between 3 and 18 kbps.

Codec example: EnCodec

EnCodec follows the same broad encoder, quantizer, and decoder structure and was designed for real-time neural audio compression. The official models support several bitrate settings. For the 24 kHz mono model, these include 1.5, 3, 6, 12, and 24 kbps. The EnCodec paper evaluates speech, noisy speech, and music, as well as mono and stereo settings.

Bitrate: how much information is kept?

Bitrate measures how many bits are used each second. A lower bitrate stores or transmits less information. That gives stronger compression, but the decoder has less detail to work with.

At lower settings, a reconstruction may lose fine texture, sharp attacks, stereo detail, or clarity. It may also introduce artificial sounds known as compression artifacts. The exact result depends on the codec, the training data, and the type of audio.

Higher bitrate is not free. More codebooks or more frequent tokens increase storage and give a generative model more symbols to predict. Codec design is therefore a balance between compact sequences and convincing sound.

From codec tokens to a music model

Once audio has been turned into discrete IDs, generation begins to look more like sequence modelling. The model receives earlier tokens and predicts which tokens should come next.

The tokens are not musical notes. A single token does not usually mean piano, kick drum, or C major. Its meaning depends on the codebook, the surrounding tokens, and the decoder. The token streams collectively carry information about rhythm, pitch, timbre, voices, effects, and production.

After the music model finishes predicting the sequence, the codec decoder converts the tokens into a waveform.

Examples in practice

Generation example: AudioLM

AudioLM maps audio into discrete tokens and treats audio generation as a language modelling task. It combines token types with different strengths. Semantic tokens help with long-term structure, while codec tokens help preserve acoustic detail. The researchers demonstrated speech continuation and piano music continuation without using text transcripts or symbolic music.

Generation example: MusicGen

MusicGen uses several streams of compressed EnCodec tokens. Meta's official AudioCraft documentation describes the published model as using a 32 kHz EnCodec tokenizer with four codebooks sampled at 50 steps per second. MusicGen predicts the codebook streams with one Transformer model, then EnCodec decodes the result into audio.

What changes when compression changes?

Swipe sideways to view the full comparison

SettingInformation carriedToken workloadLikely reconstruction
Lower bitrateLessLowerMore detail may be lost
Higher bitrateMoreHigherMore detail can be preserved
Fewer codebooksCoarser representationFewer token streamsRougher reconstruction
More codebooksCoarse and fine informationMore token streamsPotentially higher fidelity

Interactive lesson

Compress a Track Into Audio Tokens

Try it: Switch between bitrate settings and inspect changes in attacks, high frequencies, background texture, and instrument timbre. Compare the number of active codebooks and token IDs produced over time.

A neural codec uses an encoder, quantizer, learned codebooks, and decoder. Lower bitrates produce shorter or smaller token representations but may lose more detail. Higher bitrates can preserve more information but create more data for a generative model to process.

Check your understanding

Ready for a quick check?

Test whether you can follow audio through a neural codec and explain why generative models use tokens.

Ready to continue?

Save this section to your account and continue from any device.

Checking your account…
Sources for this lesson (7)
  1. SoundStream: An End-to-End Neural Audio Codec — Neil Zeghidour et al. (2021)
  2. High Fidelity Neural Audio Compression — Alexandre Defossez, Jade Copet, Gabriel Synnaeve, and Yossi Adi (2022)
  3. EnCodec: High Fidelity Neural Audio Compression — Meta Research
  4. High-Fidelity Audio Compression with Improved RVQGAN — Rithesh Kumar, Prem Seetharaman, Alejandro Luebs, Ishaan Kumar, and Kundan Kumar (2023)
  5. AudioLM: A Language Modeling Approach to Audio Generation — Zalan Borsos et al. (2022)
  6. Simple and Controllable Music Generation — Jade Copet et al. (2023)
  7. MusicGen: Simple and Controllable Music Generation — Meta AI
Browse all contextual sources →