AnyLearn
All lessons
AIadvanced

Turning Sound Into Tokens

Before a model can process speech it has to be discretised, and audio resists that harder than text does. This lesson covers why raw waveforms are the wrong representation, how neural audio codecs learn a discrete one, what residual vector quantization actually does, and the arithmetic that governs every speech model: a minute of talking is around 195 text tokens or 36,000 audio tokens.

Updated · AI-authored, review-gated · how lessons are made

Not signed in: your progress and quiz score won't be saved.
Progress1 / 9

Why audio is the awkward modality

Text arrives already discrete. Characters are symbols, a tokenizer groups them, and the sequence is short: a minute of speech transcribed is on the order of two hundred tokens.

Audio arrives as a waveform, a continuous pressure signal sampled many thousands of times a second. At 24 kHz with 16 bits per sample, one second is 24,000 numbers, or 384 kilobits.

Two problems follow, and they pull in opposite directions.

The sequence is far too long. A transformer over raw samples would need to attend across 24,000 positions per second, which is untenable for anything but very short clips.

And the samples are the wrong unit. An individual sample carries almost no meaning. What matters is structure across thousands of them: the pitch of a voice, the shape of a vowel, the timing of a consonant. Predicting the next sample is a task about waveform continuity, not about speech.

So the first job in any speech model is finding a representation that is short enough to model and meaningful enough to be worth modelling. Everything else in this path depends on how that is solved.

Full lesson text

All 9 steps on one page, for reading, reference, and search.

Show

1. Why audio is the awkward modality

Text arrives already discrete. Characters are symbols, a tokenizer groups them, and the sequence is short: a minute of speech transcribed is on the order of two hundred tokens.

Audio arrives as a waveform, a continuous pressure signal sampled many thousands of times a second. At 24 kHz with 16 bits per sample, one second is 24,000 numbers, or 384 kilobits.

Two problems follow, and they pull in opposite directions.

The sequence is far too long. A transformer over raw samples would need to attend across 24,000 positions per second, which is untenable for anything but very short clips.

And the samples are the wrong unit. An individual sample carries almost no meaning. What matters is structure across thousands of them: the pitch of a voice, the shape of a vowel, the timing of a consonant. Predicting the next sample is a task about waveform continuity, not about speech.

So the first job in any speech model is finding a representation that is short enough to model and meaningful enough to be worth modelling. Everything else in this path depends on how that is solved.

2. The classical answer, and its ceiling

For decades the answer was the spectrogram, and it is still the right starting point for understanding what came after.

Take short overlapping windows of the waveform, apply a Fourier transform to each, and keep the magnitudes. The result is a two-dimensional array: time along one axis, frequency along the other, energy as the value. Warp the frequency axis to match human hearing, which resolves low frequencies finely and high ones coarsely, and you have a mel spectrogram.

This is a very good representation. It shortens the sequence by an enormous factor, since one frame covers ten or twenty milliseconds rather than one sample. It aligns with perception. And speech structure is legible in it, to the point that a trained person can read words off a spectrogram.

Its limitation is that it is continuous. Each frame is a vector of real numbers, so a model producing speech has to regress those values, and generative modelling of continuous vectors is harder than predicting from a fixed set.

It also throws away phase, so reconstructing a waveform from a spectrogram is lossy and needs a separate vocoder.

Discretisation is what removes both problems at once.

3. The neural audio codec

A neural audio codec learns a discrete representation of audio rather than specifying one by hand. The architecture has three parts and is trained end to end.

An encoder, a convolutional network that consumes the waveform and produces a sequence of vectors at a much lower rate. This is the downsampling step, and the output rate is a design choice with large consequences.

A quantizer, which maps each continuous vector to the nearest entry in a learned codebook, replacing it with that entry's index. This is the step that makes the representation discrete, and the indices are what a downstream model treats as tokens.

A decoder, which reconstructs the waveform from the quantized sequence.

Training minimises reconstruction error, and adds an adversarial loss from a discriminator trained to tell real audio from reconstructed. That second term matters more than it sounds: reconstruction error alone produces muffled output, because averaging is the safe way to minimise it. The discriminator penalises exactly that, since averaged audio is easy to identify as fake.

SoundStream and EnCodec are the systems that established this design, and essentially every speech model since builds on one of them.

4. Why one codebook is not enough

The obvious quantizer uses a single codebook, and it fails for a reason worth understanding, because the fix is the central idea of the field.

Quality depends on how finely the codebook covers the space of possible vectors. Finer coverage needs more entries. But the number of bits per token is the logarithm of the codebook size, so getting one more bit of precision means doubling the codebook.

Reaching acceptable speech quality this way would need a codebook with an astronomically large number of entries, which is impossible to store and impossible to train, since every entry needs to be visited enough times to learn a useful value.

Residual vector quantization solves it by using several modest codebooks in sequence.

Quantize the vector with the first codebook, which gets you close. Compute the residual, the difference between the original and the chosen entry. Quantize that residual with a second codebook, which corrects part of the error. Take the new residual, quantize with a third, and so on.

Each stage refines what the previous ones left. Eight codebooks of a thousand entries give the precision of one codebook with a thousand to the eighth power entries, and are entirely trainable.

5. The arithmetic that governs everything

Put numbers on a standard configuration. EnCodec at 24 kHz produces frames at 75 Hz, with codebooks of 1024 entries, which is 10 bits each.

One codebook is 75 tokens per second at 0.8 kilobits per second, a 512-fold compression against the raw 384 kbps.

Four codebooks give 300 tokens per second at 3 kbps, a 128-fold compression.

Eight codebooks give 600 tokens per second at 6 kbps, a 64-fold compression, and this is roughly where quality becomes good.

Now compare against text. Speech at 150 words per minute is about 3.2 text tokens per second.

So one minute of talking is around 195 tokens as text, and around 36,000 tokens as audio at eight codebooks. The audio representation is roughly 185 times longer for the same content.

That ratio is the single most important number in speech modelling. It decides context lengths, training costs, and latency, and it is why every design in this path is in some way an attempt to reduce it.

6. The stack, and where the tokens come out

Laying the pipeline out shows where each design decision sits and what it controls.

The waveform enters the encoder, which downsamples it to a frame rate. That rate is the first lever: lower means fewer tokens per second and less temporal detail.

Each frame vector then goes through the residual quantizer stack. The first codebook produces a coarse index and a residual; each subsequent codebook refines. The number of codebooks is the second lever: more means better reconstruction and proportionally more tokens.

The output is not one token sequence but several parallel ones, one per codebook, all at the same frame rate. That structure is awkward for a language model, which expects a single stream, and how to handle it is a real design problem covered in the synthesis lesson.

The decoder inverts the whole thing, summing the codebook entries back into a vector sequence and reconstructing a waveform.

Note what the two levers trade against each other. Frame rate controls time resolution; codebook count controls per-frame fidelity. Both multiply into the token rate, and the token rate is what everything downstream pays for.

flowchart LR
A["Waveform at 24 kHz"] --> B["Encoder: downsample to 75 Hz frames"]
B --> C["Codebook 1: coarse index plus residual"]
C --> D["Codebook 2: refine the residual"]
D --> E["Codebooks 3 to 8: refine further"]
E --> F["Parallel token streams, one per codebook"]
F --> G["Decoder: sum entries, reconstruct waveform"]
C --> F
D --> F

7. Semantic and acoustic information

A codec trained purely to reconstruct audio learns tokens that are good at reconstruction, which is not the same as being good to model.

The issue is what the tokens encode. Reconstruction requires everything: the words, the speaker's voice, the room acoustics, the background noise. All of it is mixed across the codebooks with no particular organisation, so a language model predicting these tokens spends capacity predicting microphone characteristics alongside content.

The useful distinction is between semantic information, meaning what was said, and acoustic information, meaning how it sounded. A model generating speech needs both, and they behave very differently: semantics carry long-range structure, acoustics are local and largely stationary within an utterance.

SpeechTokenizer addressed this by distilling semantic information into the first codebook specifically, so that the coarsest stream carries content and the later ones carry acoustic detail. That makes the first stream something close to a phonetic transcription, which is far easier to model.

The general design goal, pursued by several tokenizers since, is to make the token structure match the structure of the problem rather than only the structure of the signal.

8. What the frame rate costs

Since the token rate governs everything downstream, reducing the frame rate is the most direct lever available, and there is active work pushing it well below 75 Hz.

The constraint is that speech has real structure at short timescales. Phonemes last on the order of 50 to 100 milliseconds, and some distinctions are shorter: the difference between certain consonants is a matter of a few tens of milliseconds in the onset. Drop the frame rate too far and those distinctions cannot be represented, so the content itself degrades rather than only the fidelity.

The trade is therefore not fidelity against cost in a simple sense. Below a certain rate you stop losing audio quality and start losing intelligibility, which is a different kind of failure.

Lower-rate tokenizers get there by making each token carry more, which usually means larger or better-organised codebooks and more capacity in the encoder.

A related design axis is streaming. A codec that needs to see a whole utterance before encoding cannot be used in a live conversation, so tokenizers built for real-time use, such as Mimi, constrain themselves to causal operations that can emit a token as soon as its frame arrives.

9. Why this lesson comes first

The representation decides what the rest of the system can be, and three consequences run through the remaining lessons.

Recognition can mostly avoid this problem. A speech recogniser consumes audio and produces text, so it needs a good encoder and never has to generate audio tokens. That is why recognition was solved earlier and more thoroughly than synthesis, and why its architectures look different.

Synthesis is where discretisation pays off. Once audio is a sequence of tokens from a fixed vocabulary, generating speech becomes the same shape of problem as generating text, and the entire language modelling toolkit applies directly. That reframing is what made zero-shot voice cloning possible.

And end-to-end speech-to-speech is where the token rate becomes the binding constraint. A model consuming and producing audio tokens over a real conversation is working with sequences roughly two orders of magnitude longer than the text equivalent, and every architectural compromise in that area traces back to this arithmetic.

With the representation established, the next lesson takes the easier direction: turning speech into text.

Check your understanding

The lesson ends with a 5-question quiz. Take it in the player above to see your score.

  1. Why are raw waveform samples a poor unit for a speech model?
    • They are stored as integers rather than floats
    • The sequence is enormously long and an individual sample carries almost no meaning, since speech structure lives across thousands of them
    • They cannot be batched efficiently on GPUs
    • They contain phase information that models cannot use
  2. Why does the adversarial loss matter when training a neural audio codec?
    • It speeds up convergence of the encoder
    • It enforces the discreteness of the codebook indices
    • Reconstruction error alone rewards averaging, which produces muffled audio that a discriminator easily identifies as fake
    • It allows the codec to run in streaming mode
  3. What problem does residual vector quantization solve?
    • It removes the need for an encoder network
    • It makes the codec causal so it can stream
    • It separates semantic from acoustic information automatically
    • Bits per token grow only logarithmically with codebook size, so several modest codebooks refining each other's residuals replace one impossibly large codebook
  4. Roughly how does one minute of speech compare as text tokens versus audio tokens at eight codebooks?
    • About 195 text tokens against about 36,000 audio tokens, a ratio near 185 to 1
    • About the same, since both are discrete sequences
    • About 2,000 text tokens against about 6,000 audio tokens
    • Audio is shorter, since codecs compress by 64 times
  5. Why do tokenizers like SpeechTokenizer distil semantic information into the first codebook?
    • To reduce the total bitrate of the codec
    • So the coarsest stream carries content rather than a mix of content and acoustics, making it far easier for a language model to predict
    • To allow the codec to operate without a decoder
    • Because the first codebook has more entries than the others

Related lessons

Math
intermediate

Sampling and Aliasing: The Rule You Cannot Break

Turning a continuous signal into numbers is safe only above a specific rate, and below it the damage is silent and permanent. This lesson derives the Nyquist limit, shows exactly where a too-high frequency reappears, and explains why the fix has to be analogue and has to happen before the converter.

10 steps·~15 min
Math
intermediate

Gradients, Jacobians, and Hessians: Calculus in Many Dimensions

One derivative becomes three objects once a function has many inputs and many outputs. This lesson builds the gradient, the Jacobian and the Hessian, shows what each one actually tells you, and explains why curvature decides how many steps an optimiser needs and why nobody ever writes the Hessian down.

10 steps·~15 min
Math
intermediate

The Derivative Is a Local Linear Model

Machine learning uses the derivative as a search strategy, not a symbolic exercise. This lesson builds it as the best local linear approximation, derives the gradient descent update from it, and shows why estimating derivatives numerically loses half your digits and costs one function evaluation per parameter.

10 steps·~15 min
AI
intermediate

Bedrock: One Door to Many Models

Amazon Bedrock's pitch is that model choice becomes a configuration value instead of a rewrite. This lesson takes that claim apart: the four API dialects Bedrock exposes over the same models, what the unified Converse API actually normalises, what it cannot normalise, and the inference and governance machinery that decides cost and blast radius.

7 steps·~11 min