Two seconds of speech once took Tacotron 2 and WaveNet 48,160 steps, one after another. VITS makes the same two seconds in one parallel pass.
Pick a system and press Speak. Watch how the text becomes frames and the frames become sound. Then we build each part from zero: the frontend, the mel, attention, the vocoder, durations, sampling, and codec tokens.
You need a little Python and the idea that sound is a list of numbers. We build the rest from zero.
Numbers come from the papers: Tacotron 2 (Shen et al., 2018), FastSpeech 2 (Ren et al., 2021), HiFi-GAN (Kong et al., 2020), VITS (Kim et al., 2021) and VALL-E (Wang et al., 2023). Links are in the field guide.
Step counts use each paper's settings: Tacotron 2 makes 80 mel frames per second and its WaveNet makes 24,000 samples per second, one at a time; VALL-E makes 75 first-codebook tokens per second, then 7 parallel passes. The animation speed is illustrative.
Chapter 0
Count how much sound one short sentence needs, and find the three problems every TTS system must solve
Type "the cat sat on the mat." That is 22 characters. A person says it in about two seconds. At a sample rate of 22,050 numbers per second, those two seconds are 44,100 numbers. A text-to-speech (TTS) system is a program that turns the 22 characters into the 44,100 numbers.
Three problems sit inside that job.
Each bar counts one kind of unit on a log scale, so each grid line is ten times the last. Drag the length of speech.
Mel frames and samples use the LJSpeech setup (22,050 Hz, hop 256). The speaking rates of 11 characters and 10 phonemes per second are illustrative.
The gap is about three powers of ten. No single layer bridges it well. So the field splits the job into stages and adds a middle picture of sound, the mel spectrogram. Every system in this lesson is a different answer to the three problems.
Chapter 1
Split text-to-audio at the mel, and compute how many frames and samples each stage makes
The classic design has two stages. The acoustic model turns phonemes into a mel spectrogram. The vocoder turns the mel spectrogram into audio samples. The mel is the meeting point.
A mel spectrogram is a grid: one column per short slice of time, one row per frequency band. Each column is a mel frame. Tacotron 2, FastSpeech 2 and HiFi-GAN all use 80 bands. The frames overlap. The step from one frame to the next is the hop size, counted in samples.
How many frames does one second of speech make? Call the sample rate s, in samples per second. Call the hop size h, in samples. Call the frame rate f, in frames per second.
f equals s divided by h.
Take one second of samples, s of them. Step through them h samples at a time. Each step starts a new frame, so you get f frames.
LJSpeech setup: s = 22,050 and h = 256, so f = 86.1 frames per second, one every 11.6 ms. Tacotron 2: s = 24,000 and a 12.5 ms hop, so h = 300 and f = 80.
Each box shows the tensor that leaves a stage. Pick a setup and a length, and watch one mel frame open into its samples.
Sample rates and hops come from the FastSpeech 2 and Tacotron 2 papers. The 10 phonemes per second is illustrative.
Why split here? The acoustic model works at 86 steps per second and decides what to say and how. The vocoder works at 22,050 steps per second and decides how it sounds. A good vocoder works for many acoustic models.
Symptom: the voice sounds sped up, slowed down or garbled. Measure: compare the hop and sample rate in the acoustic model's config with the vocoder's. Fix: train both on mels made with the same settings. A vocoder trained on hop 256 reads hop 300 frames at the wrong speed.
Chapter 2
Turn messy text into words a person would say, then into the sounds those words make
Your TTS reads a receipt: "Dr. Smith paid $2.50." If it reads the symbols as written, it says "D R dot Smith paid dollar two point five oh." The voice can be perfect and the speech still wrong. The text frontend is the code that runs before any neural network and fixes this. It has two jobs.
Pick an input. The rows show the raw text, the spoken words, then the phonemes. Words that changed light up.
Phonemes are ARPAbet symbols, the set the CMU Pronouncing Dictionary uses, with stress marks left out. The rules are a toy, not a real frontend.
Why phonemes and not letters? English spelling is irregular. "Through", "though" and "tough" share four letters and end in three different sounds. A model fed letters must learn those rules from data, and it fails on rare names.
Fed phonemes, it can spend its capacity on rhythm and voice. FastSpeech 2 and VITS take phonemes from a G2P tool. Tacotron 2 takes raw characters and learns spelling from 24.6 hours of one speaker.
Some words need context. "Read" is R IY D in "I read it now" and R EH D in "I read it yesterday". "St." is "Saint" before a name and "Street" after one. A homograph is a word with one spelling and two pronunciations, and the frontend must guess from the sentence around it.
Symptom: clean audio that says the wrong words, such as "three slash four" for a date. Measure: run a test list of numbers, dates and short forms and compare the words with a human reading. Fix: add a rule or a context model for that class, then rerun the list.
Chapter 3
Let attention decide which input to voice at each frame, and see how it skips and repeats
Tacotron 2 is a sequence-to-sequence model, built like a translation model. An encoder reads the characters and gives one vector per character. A decoder writes the mel one frame at a time, and each frame depends on the one before it. That makes it autoregressive.
At every frame the decoder must pick which characters it is voicing now. It does this with attention. Call the encoder vector for input j hj. At output frame i, attention gives each input a weight αij. The weights are positive and add to 1. The decoder reads one blend of the inputs, the context vector ci.
c-i equals the sum over j of alpha-i-j times h-j.
For frame i, scale each encoder vector h-j by its weight alpha-i-j. Add the scaled vectors. The sum c-i says what to voice now.
With made-up numbers, one feature of three inputs: h = 1.0, 4.0, −2.0 and weights 0.1, 0.7, 0.2. Then c = 0.1 + 2.8 − 0.4 = 2.5. The frame mostly voices input 2.
Speech moves left to right. So if you plot the weights, inputs on one axis and frames on the other, a healthy model draws a clean diagonal. Tacotron 2 uses location-sensitive attention: it also sees where it looked before, which pushes it to keep moving forward.
Bright cells are high attention. Drag stability down and press New run to see the decoder stall and jump.
An illustrative toy: each frame the focus moves forward, stalls or jumps at random. Real failures come from the learned weights.
The decoder also decides when to stop. A small output, the stop token, gives the chance that speech is over. Generation ends at the first frame where it passes 0.5. On real data Tacotron 2 scored a mean opinion score of 4.53 against 4.58 for the recordings (Table 1 of the paper).
Symptom: a word said twice, a word missing, or noise after the sentence ends. Measure: plot the attention map and look for flat runs, gaps and a smeared tail. Fix: location-aware attention helps; the full fix is to remove learned attention at inference, which Chapter 5 does.
Chapter 4
Stretch each mel frame into hundreds of samples, and see why a parallel GAN beat a sample-by-sample model
The acoustic model hands over 86 mel frames per second. The speaker needs 22,050 samples per second. The vocoder fills the gap: every frame must become exactly one hop of samples.
Call the number of mel frames T. Call the hop size h. Call the number of output samples N.
N equals T times h.
Give every mel frame its own block of h samples. Count the blocks, T of them. The vocoder must fill N samples.
T = 100 frames and h = 256 give N = 25,600 samples, or 1.16 s at 22,050 Hz. HiFi-GAN stretches time in four layers by 8, 8, 2 and 2, and 8 × 8 × 2 × 2 = 256 = h.
The first neural vocoder, WaveNet, writes one sample at a time, each from the samples before it. Quality is high, but N samples need N steps in a row. HiFi-GAN writes all N at once. A transposed convolution is a layer that stretches a sequence in time, and four of them grow the mel to full length. A GAN trains the generator against critics that try to tell its audio from real audio, so it learns to sound crisp.
Top: one frame grows through four upsampling layers. Bottom: the time each vocoder needs, on a log scale. Drag the clip length.
Speeds from HiFi-GAN Table 1, on one V100 GPU at 22.05 kHz: WaveNet (MoL) makes 0.07 kHz, HiFi-GAN V1 makes 3,701 kHz. The layer factors 8, 8, 2, 2 are from the released V1 config.
The table in the HiFi-GAN paper gives HiFi-GAN V1 a mean opinion score of 4.36, WaveNet 4.02, and the recordings 4.45. It was faster and better at once. That is why parallel vocoders took over.
Symptom: a buzzy, metallic voice from a vocoder that sounded clean in tests. Measure: feed it the acoustic model's predicted mels and compare with real mels. Fix: fine-tune the vocoder on predicted mels, as Tacotron 2 did for its WaveNet.
Chapter 5
Predict how many frames each phoneme lasts, copy it that many times, and make every frame at once
Tacotron 2 found the alignment one frame at a time. FastSpeech 2 states it up front. A duration predictor reads each phoneme and says how many mel frames it lasts. A length regulator copies each phoneme's vector that many times. A non-autoregressive decoder then writes every frame in one parallel pass.
Call the predicted duration of phoneme i di, in frames. Call the total number of frames T.
T equals the sum over i of d-i.
Copy phoneme i's vector d-i times. Lay the copies end to end. The row has T frames, so the decoder knows the length before it starts.
With made-up durations for "cat": K 5, AE 12, T 4. Then T = 21 frames, or 244 ms at 11.6 ms per frame.
Speed is now one knob. Multiply every duration by a scale α and round. At α = 0.8, "cat" becomes 4, 10 and 3 frames: 17 frames, 197 ms. At α = 1.5 it becomes 8, 18 and 6: 32 frames, 372 ms.
Each phoneme's colour block is its run of copies. Drag the duration scale to slow down or speed up the word.
The durations 5, 12 and 4 are illustrative. Frame length is 11.6 ms, from hop 256 at 22,050 Hz.
Where do training durations come from? FastSpeech took them from a teacher Tacotron model. FastSpeech 2 uses the Montreal Forced Aligner, a tool that finds where each phoneme starts and ends in a recording. Its variance adaptor also predicts pitch and energy per frame. Training took 17 hours against 53 for FastSpeech, and both generate about 48 times faster than an autoregressive Transformer TTS (Table 2).
Symptom: every word clear, but the rhythm feels mechanical. Measure: compare predicted durations with the aligner's durations on held-out speech. Fix: better aligner labels, and pitch and energy predictors, as FastSpeech 2 added. No skips or repeats can happen, because every phoneme gets at least one frame.
Chapter 6
See why a model trained to minimise error predicts the average reading, and why the average sounds flat
Say "you're coming?" twice. Once the pitch rises at the end, as a question. Once it falls, as surprise. Both are correct. A dataset holds many such readings of similar text, each with its own prosody: the pitch, rhythm and stress of speech.
Now train with mean squared error (MSE), the average of the squared gaps between prediction and target. Call the K correct readings of one point in the sentence y1 to yK. The single prediction that gives the lowest MSE is ŷ.
y-hat equals one over K, times the sum over k of y-k.
Add up the pitch of every correct reading y-k. Divide by K. The squared-error winner y-hat is the plain average.
With made-up pitches: a rising reading goes 200, 220, 240, 260 Hz and a falling one goes 260, 240, 220, 200 Hz. The average is 230 Hz at every point: a flat line. Its MSE is 500, half that of guessing either real reading.
Faint lines are correct readings. Pick what the model outputs. Press Resample to draw a new reading.
Illustrative contours: half rise, half fall, each with its own random wiggle.
The cure is to make the model generative: let it draw one reading from the spread of correct readings. Each method adds randomness in its own way. A VAE samples a hidden code. A normalizing flow turns simple noise into a detailed sample. Diffusion removes noise step by step. Each output commits to one lively reading.
Symptom: clear words with a monotone melody. Measure: compare the spread of pitch across many outputs with the spread in real speech. Fix: add a source of randomness, or feed the true pitch in training as FastSpeech 2 does.
Chapter 7
Find the best monotonic alignment with a small table, then generate a waveform straight from text
VITS goes from phonemes straight to a waveform, with no separate vocoder. Four parts do the work:
Where do durations come from without an aligner? VITS finds them itself with monotonic alignment search (MAS), taken from Glow-TTS. Build a table with phonemes down the side and frames along the top. Call the score that frame j belongs to phoneme i L[i, j]. Call the best total score of any path that ends at that cell Q[i, j]. A path may only stay on its phoneme or move down one.
Q of i, j equals L of i, j, plus the max of Q of i minus 1, j minus 1, and Q of i, j minus 1.
Fill the table one frame column at a time. For each cell, take the better of "stay" and "move on", and add the cell's own score L. Then walk back from the last cell to read the path.
With the made-up table in the device: Q[0, 1] = −1 + −1 = −2, and Q[1, 2] = −1 + max(−2, −4) = −3. The best path ends at −6 and gives each of the 3 phonemes 2 frames.
Each cell shows L on top and Q below once filled. Press Step to fill one cell, or Run to finish and trace the path.
The scores are a toy. The fill and backtrack follow the MAS pseudocode in the VITS paper, Appendix A.
The path gives each phoneme its frame count, and those counts train the duration predictor. On LJSpeech, VITS scored a mean opinion score of 4.43, against 4.46 for the recordings and 4.25 for Tacotron 2 with a fine-tuned HiFi-GAN (Table 1). It generated audio 67 times faster than real time on one V100 (Table 4).
Symptom: a word blurs into its neighbour. Measure: plot the MAS path for that clip and look for a phoneme with one frame. Fix: check the frontend's phonemes first; a wrong phoneme cannot align well.
Chapter 8
Turn audio into tokens, let a language model continue a 3-second prompt, and count the tokens it writes
A neural audio codec compresses audio into a grid of small integers. VALL-E uses EnCodec at 24 kHz. It makes 75 frames per second. Each frame has 8 tokens, one from each of 8 codebooks of 1,024 entries. The first codebook carries the coarse sound; each later one fixes what the earlier ones missed.
Call the codec frame rate r, in frames per second. Call the number of codebooks q. Call the length of the audio t, in seconds. Call the number of tokens n.
n equals r times q times t.
Count the frames, r for each of the t seconds. Give every frame q tokens. Multiply to get n.
A 3-second prompt: n = 75 × 8 × 3 = 1,800 tokens. Each token picks 1 of 1,024 entries, which is 10 bits, so the rate is 75 × 8 × 10 = 6,000 bits per second.
VALL-E splits generation in two. An autoregressive (AR) Transformer writes the first-codebook tokens one at a time, from the phonemes and the prompt's tokens. A non-autoregressive (NAR) Transformer then fills codebooks 2 to 8, one whole row per pass. The codec's decoder turns the grid back into a waveform.
Pink columns are the 3-second prompt from the target speaker. Press Generate and watch row 1 grow token by token, then rows 2 to 8 fill a row at a time.
The codec settings and the AR then NAR split are from the VALL-E paper; the grid draws one column for every 5 codec frames.
Why does the voice match? The language model continues the prompt's tokens, and those tokens carry the speaker's timbre and the room. Trained on 60,000 hours of LibriLight, VALL-E cut word error rate from 7.7 to 5.9 and raised speaker similarity from 0.337 to 0.580 against YourTTS (Table 2). Diffusion and flow-matching models such as Voicebox take another route to the same goal: they denoise a mel or a latent instead of writing tokens.
Symptom: a cloned voice that repeats a word or trails off, as Tacotron 2 did. Measure: transcribe the output with an ASR model and compute word error rate. Fix: sample several outputs and keep the one with the lowest error.
Chapter 9
Keep the numbers, the checklist and the core code in one place
A TTS system cleans text into phonemes, decides how long each phoneme lasts, samples one lively reading, and writes samples. The systems differ in how they align and how many steps they take in order.
| System | Alignment | Steps in order for 2 s | Variety | Reported MOS |
|---|---|---|---|---|
| Tacotron 2 + WaveNet | learned attention | 160 frames, then 48,000 samples | little | 4.53 (recordings 4.58) |
| FastSpeech 2 + vocoder | durations from MFA | 1 pass, then 1 pass | pitch and energy inputs | 3.83 (Tacotron 2: 3.70) |
| HiFi-GAN V1 (vocoder) | none needed | 1 pass | none | 4.36 (recordings 4.45) |
| VITS | MAS in training | 1 pass | flow and stochastic durations | 4.43 (recordings 4.46) |
| VALL-E | implicit in the LM | 150 tokens, then 7 passes | sampling; 3 s cloning | SMOS 4.38 (YourTTS 3.45) |
Each MOS comes from its own paper's listening test, so compare within a row, not across rows. FastSpeech 2 rated all systems through the same Parallel WaveGAN vocoder, which is why its numbers sit lower.
pythonimport numpy as np def length_regulate(h, d, alpha=1.0): # h: [P, D] phoneme vectors, d: [P] frames d = np.maximum(1, np.round(alpha * np.asarray(d))).astype(int) return np.repeat(h, d, axis=0) # [T, D], T = d.sum() def mas(L): # L: [P, T] log-likelihoods P, T = L.shape; Q = np.full((P, T), -np.inf) for j in range(T): for i in range(max(0, P + j - T), min(P, j + 1)): if j == 0: Q[i, j] = L[i, j]; continue move = Q[i - 1, j - 1] if i else -np.inf Q[i, j] = L[i, j] + max(move, Q[i, j - 1]) path, i = [0] * T, P - 1 for j in range(T - 1, -1, -1): path[j] = i if i and (i == j or Q[i, j - 1] < Q[i - 1, j - 1]): i -= 1 return np.bincount(path, minlength=P) # durations per phoneme frames_per_s = 22050 / 256 # f = s / h = 86.1 codec_tokens = 75 * 8 * 3 # n = r * q * t = 1800
Now press Present or Teach and explain the pipeline from memory: frontend, mel, alignment, vocoder, sampling. Then go back to the hero and speak the sentence four ways. Say which chapter explains each step count.