Audio Generation
Audio is a 1-D signal at 16-48 kHz, so a five-second clip is 80-240k samples. No transformer attends to that sequence directly. The solution for every production audio model in 2026 is the same: a neural codec (Encodec, SoundStream, DAC) compresses audio to discrete tokens at 50-75 Hz, and a transformer or diffusion model generates the tokens.
Learning Objectives
- Explain why raw audio is tokenized by a neural codec (Encodec, SoundStream, DAC) before a transformer or diffusion model touches it.
- Describe how residual vector quantization turns each timestep into K codebook indices, and compute the resulting token rate (8 codebooks at 75 Hz is 600 tokens/sec).
- Build a count-based bigram token predictor in stdlib Python and use it for style-conditioned sampling and VALL-E-style prompt continuation.
- Choose between token-autoregressive and flow-matching generators for a TTS, music, or sound-effects brief based on streaming and latency needs.
- Name the main pitfalls of audio generation: codec quality caps, RVQ error accumulation, long-sequence structure, training-data licensing, and voice cloning abuse.
The Problem
Three audio generation tasks:
- Text-to-speech. Given text, produce speech. Clean speech is narrow-band and has strong phonetic structure, so transformer-over-tokens works well. VALL-E (Microsoft), NaturalSpeech 3, ElevenLabs, OpenAI TTS.
- Music generation. Given a prompt (text, melody, chord progression, genre), produce music. The distribution is much broader. MusicGen (Meta), Stable Audio 2.5, Suno v4, Udio, Riffusion.
- Audio effects / sound design. Given a prompt, produce ambient sound or Foley. AudioGen, AudioLDM 2, Stable Audio Open.
These three share a substrate: a neural audio codec plus a token-AR or diffusion generator.
The Concept
Neural audio codecs
Encodec (Meta, 2022), SoundStream (Google, 2021), Descript Audio Codec (DAC, 2023). A convolutional encoder compresses the waveform to a per-timestep vector, and residual vector quantization (RVQ) converts each vector to a cascade of K codebook indices. The decoder reverses it. 24 kHz audio at 6 kbps using 8 RVQ codebooks (1024 entries each) at 75 Hz = 600 tokens/sec.
waveform (24000 samples/sec)
└─ encoder conv ─┐
├─ RVQ layer 1 → indices at 75 Hz
├─ RVQ layer 2 → indices at 75 Hz
├─ ...
└─ RVQ layer 8
Two generative paradigms on top
Token-autoregressive. Flatten RVQ tokens into a sequence, run a decoder-only transformer. MusicGen uses "delayed parallel" to emit K codebook streams in parallel with per-stream offsets. VALL-E generates speech tokens from a text prompt + 3-second voice sample.
Latent diffusion. Pack codec tokens as continuous latents or model them with categorical diffusion. Stable Audio 2.5 uses flow matching on continuous audio latents. AudioLDM 2 uses text-to-mel-to-audio diffusion.
The 2024-2026 trend: flow matching is winning for music (faster inference, cleaner samples) while token-AR still dominates speech, because it is naturally causal and streams well.
Production landscape
| System | Task | Backbone | Latency |
|---|---|---|---|
| ElevenLabs V3 | TTS | Token-AR + neural vocoder | ~300ms first token |
| OpenAI GPT-4o audio | Full-duplex speech | End-to-end multimodal AR | ~200ms |
| NaturalSpeech 3 | TTS | Latent flow matching | Non-streaming |
| Stable Audio 2.5 | Music / SFX | DiT + flow matching on audio latents | ~10s for 1-minute clip |
| Suno v4 | Full songs | Undisclosed; token-AR suspected | ~30s per song |
| Udio v1.5 | Full songs | Undisclosed | ~30s per song |
| MusicGen 3.3B | Music | Token-AR on Encodec 32kHz | Real-time |
| AudioCraft 2 | Music + SFX | Flow matching | ~5s for 5s clip |
| Riffusion v2 | Music | Spectrogram diffusion | ~10s |
Build It
code/main.py simulates the core idea with a count-based bigram model standing in for the transformer. It trains on synthetic "audio token" sequences from two styles: style 0 counts up by one token at a time, style 1 counts up by three, both modulo a 16-token vocabulary and both with a random 0 or 1 jitter on each position. Condition on style and sample.
Step 1: synthetic audio tokens
def make_tokens(style, length, vocab_size, rng):
if style == 0: # "speech-like": alternating
return [i % vocab_size for i in range(length)]
# "music-like": ramp
return [(i * 3) % vocab_size for i in range(length)]The snippet is the simplified form without the jitter; main.py adds rng.randint(0, 1) to each position.
Step 2: train a tiny token predictor
The predictor is a table of next-token counts per style, starting at 1 for every entry and updated from 500 sequences of 20 tokens per style. A transformer would replace the table in a real system. The pattern is the same: codec tokens, a likelihood-style training signal, then autoregressive sampling.
Step 3: sample conditionally
Given the style and a starting token, sample the next token from the predicted distribution and repeat. main.py generates 20 tokens per style at temperature 0.7 and 1.0, then continues a 5-token prompt for 15 more tokens, which is the VALL-E-style prompt continuation pattern in miniature.
Pitfalls
- Codec quality caps output quality. If the codec can't represent a sound faithfully, no amount of generator quality helps. DAC is the current open best.
- RVQ error accumulation. Each RVQ layer models the residual of the previous. Errors on layer 1 propagate to later layers. Sampling with temperature 0 on higher layers helps.
- Musical structure. 30 seconds at 75 Hz is 2,250 frames per codebook, or 18,000 tokens across 8 codebooks. That is hard for transformers. MusicGen uses sliding window + prompt continuation; Stable Audio uses shorter clips + crossfading.
- Artifacts at boundaries. Crossfading between generated clips needs careful overlap-add.
- Clean-data appetite. Music generators need tens of thousands of hours of licensed music. The Suno / Udio RIAA lawsuit (2024) brought this to the surface.
- Voice cloning ethics. A 3-second sample plus a text prompt is enough for VALL-E / XTTS / ElevenLabs to clone a voice, so production models need abuse detection + opt-out lists.
Use It
| Task | 2026 stack |
|---|---|
| Commercial TTS | ElevenLabs, OpenAI TTS, or Azure Neural |
| Voice cloning (consent-verified) | XTTS v2 (open) or ElevenLabs Pro |
| Background music, fast | Stable Audio 2.5 API, Suno, or Udio |
| Music with lyrics | Suno v4 or Udio v1.5 |
| Sound effects / Foley | AudioCraft 2, ElevenLabs SFX, or Stable Audio Open |
| Real-time voice agent | GPT-4o realtime or Gemini Live |
| Open-weights music research | MusicGen 3.3B, Stable Audio Open 1.0, AudioLDM 2 |
| Dubbing / translation | HeyGen, ElevenLabs Dubbing |
Ship It
Save outputs/skill-audio-brief.md. Skill takes an audio brief (task, duration, style, voice, license) and outputs: model + hosting, prompt format (genre tags, style descriptors, structural markers), codec + generator + vocoder chain, seed protocol, and eval plan (MOS / CLAP score / CER for TTS / user A/B).
I'd run main.py before reading the codec material twice. It prints 20 generated tokens per style at temperature 0.7 and again at 1.0, so you can see by eye that style 0 climbs by roughly one token per step and style 1 by roughly three, wrapping around the 16-token vocabulary. Lower temperature visibly tightens the pattern.
The predictor is a count table, not a transformer. I did that on purpose: with no network in the way, the whole loop is visible in a few lines. Count transitions, turn a row into probabilities, sample, append. A transformer swaps out the table and leaves the rest alone.
The part I'd look at longest is the prompt continuation at the end. It takes five tokens from style 0, treats them as a voice prompt, and samples 15 more. That is the VALL-E shape in miniature, and it works here only because the toy tokens are so predictable.
The toy also skips everything hard about real audio. There is no RVQ, so each step has one token instead of a stack of K, and no codec, so nothing here can lose fidelity. When I need to judge a real system, the Pitfalls list is where I'd start: codec quality and long-sequence structure.
Exercises
- Easy. Run
code/main.pyand set style explicitly. Verify the generated sequences match the style's pattern. - Medium. Add delayed parallel decoding: simulate 2 streams of tokens that must stay offset by 1 step. Train a joint predictor.
- Hard. Use HuggingFace transformers to run MusicGen-small locally. Generate a 10-second clip with three different prompts; A/B for style adherence.
Key Terms
| Term | What people say | What it actually means |
|---|---|---|
| Codec | "Neural compression" | Encoder / decoder for audio; typical output is 50-75 Hz tokens. |
| RVQ | "Residual VQ" | Cascade of K quantizers; each models the residual of the previous. |
| Token | "One codec symbol" | Discrete index into a codebook; 1024 or 2048 typical. |
| Delayed parallel | "Offset codebooks" | Emit K token streams with staggered offsets to reduce sequence length. |
| Flow matching | "The 2024 win for audio" | Straighter-path alternative to diffusion; faster sampling. |
| Voice prompt | "3-second sample" | Speaker embedding or token prefix that steers the cloned voice. |
| Mel spectrogram | "The visual" | Log-magnitude perceptual spectrogram; used by many TTS systems. |
| Vocoder | "Mel to wave" | Neural component that converts mel spectrograms back to audio. |
Production note: audio is a streaming problem
Audio is the one output modality users expect to arrive as it is generated, not all-at-once. In production terms this means TPOT matters (Time Per Output Token) because the target throughput is the user's listening speed, not their reading speed. For 24 kHz audio tokenized by Encodec at 75 frames/second with 8 codebooks, the server has to generate at least 600 tokens/sec per user (75 steps/sec with delayed parallel decoding) to keep playback smooth.
One architectural consequence:
- Flow-matching audio models don't stream easily. Stable Audio 2.5 and AudioCraft 2 render a fixed clip length in one pass. To stream, you chunk the clip and overlap the boundaries (think sliding-window diffusion), which adds 100-300ms of latency overhead vs a codec AR model.
If the product is "live voice chat" or "real-time music continuation", pick the codec AR path. If it is "render a 30-second clip on submit", flow-matching wins on quality and total latency.
Further Reading
- Défossez et al. (2022). Encodec: High Fidelity Neural Audio Compression: the codec standard.
- Zeghidour et al. (2021). SoundStream: the first widely used neural audio codec.
- Kumar et al. (2023). High-Fidelity Audio Compression with Improved RVQGAN (DAC): DAC.
- Wang et al. (2023). Neural Codec Language Models are Zero-Shot Text to Speech Synthesizers (VALL-E): VALL-E.
- Copet et al. (2023). Simple and Controllable Music Generation (MusicGen): MusicGen.
- Liu et al. (2023). AudioLDM 2: Learning Holistic Audio Generation with Self-supervised Pretraining: AudioLDM 2.
- Stability AI (2024). Stable Audio 2.5: 2025 text-to-music with flow matching.
Code
- main.py (python, 100 lines)