← All posts

Speech generation from the inside

Post 1 of 8

What a computer hears: samples, frames and mel spectrograms

Posts in this series · 8

When you say “hello” to your phone, the phone never hears a word. It receives a long list of numbers. Everything a speech model does, from writing down what you said to answering in a new voice, starts from that list.

This post builds that list from scratch. We start with air, end with a picture called a mel spectrogram, and check every number along the way with simple arithmetic. Later posts in this series (Pocket TTS, Mimi) assume everything here.

What sound is

Stretch a slinky across a table and give one end a quick push. You will see a bunch of tight coils travel along it, then a stretched-out gap, then more tight coils. No single coil travels far. Each one just wiggles forward and back, and passes the push to its neighbour.

Air does the same thing. When you speak, your vocal folds push the air in front of them, and that push travels outward as squeezed and stretched patches of air. When the patches reach your ear, they wiggle your eardrum forward and back. That wiggle is what you hear.

If we draw how hard the air pushes at one spot over time, we get a wavy line. Up means the air is a little squeezed, down means it is a little stretched. This line is called a waveform.

Two things about the line matter most:

We count repeats per second in hertz, written Hz. One hertz is one repeat each second. A thousand hertz is a kilohertz, written kHz.

Worked example. The A above middle C on a piano repeats 440 times a second, so it is 440 Hz. One full wave lasts 1/4401/440 of a second, about 2.27 milliseconds (thousandths of a second). The A one octave higher is 880 Hz: twice the repeats, each wave half as long.

People hear from about 20 Hz (a deep rumble) to about 20,000 Hz, or 20 kHz (a thin whistle). The top end drops as we get older. A speaking voice sits much lower: a typical adult male voice buzzes at about 85 to 155 Hz, a typical adult female voice at about 165 to 255 Hz. That buzz rate is called the pitch; we look at it closely in section 9.

Air pushed by a speaker, and the pressure at one spot drawn as a waveformair, drawn as a slinky: each line is a patch of airsqueezedpressure at the red patch↑ squeezed↓ stretched10 ms of time →
Figure 1. Air as a slinky. The motion is slowed down a great deal; the plot below is in real milliseconds. Pitch changes how close the squeezed patches are; loudness changes how tightly they bunch.

Recording: taking many snapshots

A flipbook is a stack of drawings. Each page is a still picture, but flip them fast and you see motion. Your eye fills in the gaps between pages.

A microphone and a computer record sound the same way. The microphone turns the air’s push into a tiny electrical voltage that goes up and down with the waveform. The computer then measures that voltage again and again, very fast, and writes each measurement down as a number. Each measurement is one page of the flipbook. It is called a sample.

How many samples we take each second is called the sample rate. It is measured in hertz too, because it counts something per second.

Worked example. Mimi, the audio codec under Pocket TTS, records at 24,000 samples per second, or 24 kHz. So:

A recording, then, is just a very long list of heights. A ten-second voice message at 24 kHz is a list of 240,000 numbers. That list is what the computer “hears”.

A 440 hertz wave and its samples at the chosen sample rate0 ms5 mswhat the computer stores, first 12 samples (16-bit, see section 4):
Sample rate
View
Figure 2. A recording is a list of dots. Pick a sample rate and see how many dots land on 5 ms of a 440 Hz note. “Hide the true wave” leaves only what the computer stores.

How often is enough

In films, a car’s wheels sometimes seem to spin slowly backwards while the car speeds forward. The camera takes only about 24 pictures a second. If a spoke moves almost a full turn between two pictures, it lands just behind where the last spoke was, and your brain sees it creep backwards. The camera took snapshots too slowly to follow the real motion.

Sound has the same trap. If a wave wiggles too fast for our sample rate, the samples land in a pattern that looks like a slower wave. The fast wave pretends to be a slow one. This is called aliasing (the wave takes on a false name, an alias).

How fast must we sample? We need at least two samples in every wave: one near the top and one near the bottom. So the highest pitch we can record honestly is half the sample rate. That limit is called the Nyquist frequency, after Harry Nyquist, an engineer who studied this in the 1920s:

fNyquist=sample rate2f_{\text{Nyquist}} = \frac{\text{sample rate}}{2}

Worked examples:

To stop this, a recorder filters out everything above the Nyquist frequency before it takes samples. You lose the very high sounds, but you never get fake ones.

A wheel filmed too slowly spins backwards; a tone sampled too slowly looks like a lower toneA wheel, filmed at 24 pictures a secondA tone, sampled 24,000 times a second0 ms1 ms12 kHz: the Nyquist limit024 kHz
Figure 3. Too slow to follow. Drag the tone above 12 kHz: the dots no longer follow the grey wave and draw a slower fake instead (dashed red).

How precise each sample is

Measure a pencil with a ruler that has only centimetre marks and you might say “about 14 cm”. With millimetre marks you can say “14.3 cm”. The pencil did not change; your ruler got finer.

Each sample is a measurement of height, so it needs a ruler too. A computer stores each height using a fixed number of bits (yes/no switches). The number of bits per sample is called the bit depth. Each extra bit doubles the number of marks on the ruler.

Worked example:

When a true height falls between two marks, it is rounded to the nearest one. That small rounding error sounds like a faint hiss. Each extra bit makes the hiss about half as loud, which is roughly 6 decibels quieter (decibels measure loudness ratios; 6 dB is about a factor of two in wave height). So 16 bits give about 16×6≈16 \times 6 \approx 96 dB between the loudest sound and the hiss, more than a quiet room to a loud concert.

Now we can count how much data a recording takes:

Models usually turn these whole numbers into decimals between −1 and +1 by dividing by 32,768, but the ruler marks stay the same.

Samples rounded to the nearest mark of a ruler with 2 to the power bits marks0 ms2 ms (48 samples at 24 kHz)
Hear it
Figure 4. Bit depth is a ruler. Each dot snaps to the nearest mark; red bars are the rounding error. Listen plays a 440 Hz tone at the chosen depth so you can hear the hiss.

Slicing into frames

Imagine reading a long book one letter at a time, never seeing a whole word. It would be slow and confusing. Grouping letters into words makes reading easier.

A single sample tells us almost nothing: one height, for one 24,000th of a second. A sound like “s” or “a” only shows its character over many samples. So computers cut the long list into short, overlapping slices and look at one slice at a time. Each slice is called a frame. Its length is the window, and how far we slide forward to start the next slice is the hop.

Why overlap? Speech changes smoothly. If slices sat end to end, a sound that started right at a cut would be split in two. Sliding by less than a full window means every moment lands near the middle of some frame.

Here is the count, explained in words first: the number of samples in a frame is the sample rate times how long the frame lasts.

samples per frame=sample rate×frame length in seconds\text{samples per frame} = \text{sample rate} \times \text{frame length in seconds}

Worked examples:

One more detail. Chopping a wave abruptly at the slice edges creates false clicks. So each frame is multiplied by a gentle hump shape that fades in and out at the edges, usually a Hann window (named after Julius von Hann). That is the main reason for the overlap: the faded edges of one frame are covered by the middle of the next.

The word see cut into overlapping framesthe word "see", 0.62 secondsseethis frame0620 msone frame, faded at the edges
Presets
Figure 5. Window and hop. The humps are Hann windows. Step moves the highlighted frame forward by one hop; the box on the right shows that slice after fading its edges.

Inside one frame: which piano keys are pressed

Press one piano key and you hear one clean note. Press three keys together and you hear a chord. The air does not carry three separate waves; it carries one wiggly wave that is the sum of the three. Yet a trained ear can still pick out the three notes.

This is true of every sound. Any wiggle, however messy, can be built by adding up pure, smooth tones (sine waves), each with its own pitch and loudness. A voice saying “ah” is a pile of pure tones. Jean-Baptiste Fourier, a French mathematician, showed this in the early 1800s.

So for each frame we can ask: which pure tones, at what loudness, add up to this slice? It is like looking at the wiggle and answering “which piano keys are pressed, and how hard”. The tool that answers this is the Fourier transform. Computers use a fast recipe for it called the FFT, the fast Fourier transform. The answer for one frame is a list of loudnesses, one per pitch. That list is called a spectrum.

The FFT does not give every pitch. It gives evenly spaced “keys”, and the spacing is set by how long the frame is: one key per 1/window length1/\text{window length} hertz. A longer slice can tell closer notes apart.

Worked example. A frame of 400 samples at 16 kHz lasts 25 ms. Its keys are 1/0.025=1 / 0.025 = 40 Hz apart: 0, 40, 80, … up to 8,000 Hz, which is 201 keys. Two tones at 200 Hz and 220 Hz fall into neighbouring keys and blur together. A 100 ms frame has keys 10 Hz apart and can tell them apart.

Now a chord. Mix 262 Hz (middle C), 330 Hz (E) and 392 Hz (G). The wiggle looks complicated. The spectrum shows three tall spikes, near 262, 330 and 392, and almost nothing else.

Piano keys, the wave they make together, and the spectrum that finds them againC3C4C5what the air carries: one wiggle (30 ms)the spectrum of one frame: which keys, how hard0100200300400500600700800Hz
Sound
Figure 6. Which keys are pressed? Click keys to build a chord. A short frame has keys far apart and blurs close notes together; drag the frame length to 100 ms and the three notes separate.

The spectrogram: a picture of sound

Take every frame’s spectrum and stand it up as a thin vertical strip, with low pitches at the bottom and high pitches at the top. Colour each spot by loudness: dark for quiet, bright for loud. Now put the strips side by side, in time order, like the pages of the flipbook laid out on a table.

The result is a picture. Time runs left to right, pitch runs bottom to top, and brightness shows loudness. It is called a spectrogram. Making one this way, with short overlapping windows and an FFT for each, is called the short-time Fourier transform, or STFT.

You can read speech in it. Vowels show as stacks of bright horizontal stripes. “S” shows as a bright fuzzy cloud up high, above 4 kHz. Silence is dark. A stop like “t” is a thin vertical line, a quick burst at all pitches.

Loudness in a spectrogram is usually shown in decibels, a scale that compresses big ratios. Our ears work that way too: a sound with ten times the power sounds only about twice as loud.

There is a catch, and it comes from section 6. A long window gives finely spaced keys, so pitch is sharp, but a quick click gets smeared across the whole long window, so timing is blurry. A short window catches the click exactly but its keys are far apart, so pitch is blurry. You cannot have both at once.

Worked example at 16 kHz:

WindowSamplesKey spacingA click is smeared over
5 ms80200 Hz5 ms
25 ms40040 Hz25 ms
100 ms1,60010 Hz100 ms

Speech systems settle near 25 ms: short enough to follow sounds that change every few tens of milliseconds, long enough to see the voice’s stripes. With a 10 ms hop, one second becomes 100 strips of 201 numbers each, 20,100 numbers in all.

The waveform of see two and its spectrogram0 s1.2 stime →loud (0 dB) ← → quiet (−80 dB)
Pitch axis
Figure 7. A picture of sound. The sound is synthetic, built from the formants in section 9. Time runs left to right, pitch bottom to top, brightness is loudness. Try 5 ms and 100 ms to see the trade-off, and Mel to preview section 8.

Mel: squeezing the top to match the ear

Look at a piano. Each octave, from one C to the next, has the same 12 keys and the same width on the keyboard. But in hertz, octaves are wildly different sizes. The bottom octave of a piano spans about 27 to 55 Hz, a gap of 28 Hz. The top octave spans about 2,093 to 4,186 Hz, a gap of over 2,000 Hz. The keyboard squeezes high frequencies together because that is how we hear: a doubling of pitch always sounds like the same step.

The spectrogram in section 7 does not do this. Its keys are evenly spaced in hertz, so it spends most of its rows on high pitches where our ears are least picky. Listen to the difference:

So we bend the pitch axis to match the ear: keep low pitches spread out, squeeze high ones together. The bent scale is called the mel scale (from “melody”), from listening experiments by Stevens, Volkmann and Newman in 1937. The usual formula, in words: take the frequency, divide by 700, add 1, take the base-10 logarithm, and multiply by 2,595.

m=2595⋅log⁡10 ⁣(1+f700)m = 2595 \cdot \log_{10}\!\left(1 + \frac{f}{700}\right)

Below about 1,000 Hz it is nearly a straight line; above it, it grows more and more slowly. The numbers are chosen so that 1,000 Hz is 1,000 mel.

Worked example (check with a calculator):

The same 100 Hz step counts ten times more at the bottom than at the top, just as our ears say.

To make a mel spectrogram, we place a set of overlapping triangle filters, evenly spaced on the mel scale, so they are narrow at low pitches and wide at high ones. Each triangle adds up the loudness of the FFT keys under it into one number. The usual count is 80 triangles, called 80 mel bands. Then we take the logarithm, so the numbers follow loudness the way the ear does.

Worked example. With a 25 ms window at 16 kHz, each frame’s 201 FFT numbers become 80 mel numbers. At 100 frames per second, one second of speech becomes 80 × 100 = 8,000 numbers, compared with 16,000 raw samples. The picture is smaller and closer to what a listener cares about. (Some newer models use 128 bands; the idea is the same.)

A hertz ruler bending into the mel scale, and mel triangle filtersevenly spaced hertzmel filters on a hertz axis (0 to 8 kHz): narrow at the bottom, wide at the top01k2k4k6k8k
Compare a 100 Hz step
Figure 8. Bending the ruler to fit the ear. Pick a pair of tones 100 Hz apart and compare their gap in mel. Show one “ee” frame to watch 201 FFT values collapse into band values.

Pitch and the shape of the voice

Sing one note as “ee”, then the same note as “oo”. The pitch did not change, yet anyone can tell the two apart. Two separate things are happening, and a spectrogram shows both.

The first is the buzz. Your vocal folds open and close many times a second, like lips buzzing in a trumpet. That buzz rate is the pitch. Because the buzz is not a pure tone, it also makes copies at 2, 3, 4 and more times the base rate, called harmonics. The base rate is called the fundamental frequency, written F0 (“F zero”).

The second is the shape of your mouth and throat. They act like a tube that makes some pitches ring louder and muffles others. Change the tongue and lips and you change which pitches ring. Those loud regions are called formants, numbered F1, F2 and up from the bottom. They decide the vowel.

Think of a guitar string and the guitar’s wooden body. The string sets the note (F0). The body decides which harmonics come out loud (formants). Swap the body and the same note sounds different.

Worked example, for a voice buzzing at F0 = 120 Hz:

This split matters for models. In a mel spectrogram, the fine stripes (pitch) and the broad patches (vowel and voice character) are mixed together. The evaluation post measures F0 separately to check whether synthetic speech has the right melody (speech evaluation metrics).

Harmonics of the voice under a vowel's formant curveloudness of each pitch in one frame05001000150020002500300035004000Hz
Vowel
Figure 9. Same note, different vowel. Moving F0 slides the lines but leaves the curve alone; changing the vowel reshapes the curve but leaves the lines alone. Formants are Peterson and Barney’s adult male averages. Listen plays a buzz through those formants.

What models do with all this

We now have three ways to hand sound to a model: raw samples, a mel spectrogram, or something learned. Each family of speech models picks one.

Speech recognisers read mel. Automatic speech recognition (ASR), turning speech into text, usually starts from a log-mel spectrogram. Whisper, for example, resamples audio to 16 kHz, uses 25 ms windows with a 10 ms hop and 80 mel bands (128 in its largest recent version). The mel picture already throws away what does not help with words, such as tiny phase details.

Older text-to-speech predicts mel, then a vocoder fills in the rest. Text-to-speech (TTS) models like Tacotron 2 learned to draw a mel spectrogram from text. But a mel spectrogram is not sound: it has lost the exact timing of each wave (the phase) and squeezed the high pitches. A second network, called a vocoder (voice encoder), learns to turn the mel picture back into 24,000-ish samples a second. WaveNet and HiFi-GAN are well-known vocoders.

Mimi skips mel and learns its own frames. Mimi is a neural audio codec (coder-decoder). Instead of a hand-made recipe of windows, FFTs and triangles, it learns its own way to squeeze sound. It takes 24 kHz samples, and each 80 ms chunk of 1,920 samples comes out as one frame of a few numbers, 12.5 frames a second. Its decoder learns to turn those frames straight back into samples, so no separate vocoder is needed.

Worked example, one second of speech:

FormNumbers per second
Raw samples at 24 kHz24,000
Linear spectrogram, 16 kHz, 25/10 ms20,100
80-band mel, 100 frames/s8,000
Pocket TTS latents: 12.5 frames × 32 numbers400

Each row keeps less, and each was chosen so that what it drops is something a listener would not miss. The trick in the next posts is how a model can generate those 400 numbers a second and still sound like a person.

Three model pipelines and the numbers per second of each form of soundSpeech recognition (ASR)waveform, 16 kHzlog-mel80 × 100 per smodeltext"hello"Older TTS (Tacotron 2 style)textacoustic modelmel80 bandsvocoder → waveformCodec TTS (Mimi, Pocket TTS)textmodelMimi frames12.5 per sMimi decoder → 24 kHzNumbers per second for one second of speech (log scale)raw samples at 24 kHz24,000linear spectrogram (16 kHz, 25/10 ms)20,10080-band mel, 100 frames/s8,000Pocket TTS latents, 12.5 × 32400
Figure 10. Three roads from sound to model. Bars use a log scale so that 400 stays visible next to 24,000.

Next reads

You now know what the numbers are: samples at a sample rate, grouped into frames, turned into spectra, bent onto the mel scale, and squeezed. Two posts pick up from here:

Sources