Lesson 2 / Unit 1: Audio Foundations
Mel Spectrograms
Our model predicts 80 numbers every 11.6 ms and never touches a waveform. Those 80 numbers only work if they follow the vocoder recipe to the last parameter.
Our acoustic model predicts 80 values per frame. The vocoder turns those frames into audio. They must use the same mel recipe: matching the shape alone is not enough.
Spectral Windows
A spectrum is the strength of each frequency in one short window of audio, computed with the discrete Fourier transform. A spectrogram is many of those windows side by side: time along one axis, frequency along the other, energy as brightness.
The waveform from the sampling lesson tells you when the pressure moved. It does not tell you which frequencies were present, and that is what separates an “s” from an “f”, or one vowel from another. Speech also changes every few tens of milliseconds, so a single spectrum of a whole clip is useless. We slide a window along the audio instead.
In our recipe the window is 1,024 samples (46.4 ms at 22,050 Hz) and it moves 256 samples at a time. That step is the hop: 256 / 22,050 = 11.6 ms. The median LJSpeech clip of 149,149 samples becomes 583 columns. Each FFT of 1,024 samples gives 513 frequency bins, about 21.5 Hz apart.
Mel Filters
A mel spectrogram replaces the evenly spaced FFT bins with a smaller set of overlapping triangular filters spaced on the mel scale, which is roughly linear at low frequencies and logarithmic above. Low frequencies get narrow filters and fine detail; high frequencies get wide filters and coarse detail, which matches how hearing resolves pitch.
Our recipe uses 80 filters between 0 and 8,000 Hz. So 513 bins collapse to 80 numbers per column. Taking the logarithm of each number gives the log-mel spectrogram, because loudness is perceived on a roughly logarithmic scale and because logs keep quiet and loud frames in a range a network can regress.
Most TTS systems predict this, not audio: a log-mel spectrogram, warped to how we hear. The moving bands are formants, which is why phonemes are visible here and a second model turns it into sound.
Predicting this compact picture instead of raw samples is the design most cascaded TTS systems share. Tacotron 2 made the case for it: its authors write that “using a compact acoustic intermediate representation” (mel spectrograms) allowed a significant reduction in the size of the WaveNet architecture. Our model sits in the same family.
Vocoder Recipe
The vocoder expects the exact recipe it was trained on, and for the SpeechBrain LJSpeech HiFi-GAN the model card lists it: 22,050 Hz, n_fft 1024, hop 256, 80 mels, fmin 0, fmax 8,000, power 1 (magnitude), Slaney mel scale and Slaney normalisation, with log compression. The card adds: “Use these specific parameters to match the Vocoder’s training settings.”
This is the build artifact for unit 1. It lives in common.py, and the training and vocoder scripts import it from there:
MEL_ARGS = dict( sample_rate=SR, # 22050 hop_length=HOP, # 256 n_fft=1024, n_mels=80, f_min=0.0, f_max=8000.0, power=1, norm="slaney", mel_scale="slaney", compression=True,)
def mel(audio): # (samples,) float32 at 22050 Hz -> (80, frames) log-mel return mel_spectrogram(audio=audio, **MEL_ARGS)compression=True is a natural log of the magnitude, clamped at 1e-5 so silence does not become minus infinity. Three details in that block matter more than they look:
power=1means magnitude. Many tutorials compute power (magnitude squared), which doubles every log value.mel_scale="slaney"picks one of two common mel formulas. The other is HTK.f_max=8000.0leaves the band from 8,000 to 11,025 Hz out of the picture entirely. That band is absent from the conditioning mel; the vocoder can still generate high-frequency output from learned structure.
The same function also produces the training targets. train.py prep runs mel() on every kept clip and checks that the frame count matches the aligner’s durations before saving, so the model learns to predict exactly what the vocoder reads.
Library Defaults
Two libraries rarely agree on a mel spectrogram because each picks different defaults, and none of them is wrong. librosa’s filter bank defaults to the Slaney formula, Slaney normalisation and fmax at half the sample rate. torchaudio’s MelSpectrogram defaults to the HTK formula, no normalisation, power 2, 128 bands and 16 kHz audio. Neither default is our vocoder’s recipe.
We measured what a mismatch costs on one clip, LJ001-0009, by comparing each vocoder output to the original with log-spectral distance (lower is closer; not a perceptual score). The correct recipe scored 12.3 dB. The HTK variant, which also removed Slaney normalisation, scored 33.2 dB. Raising fmax to 11,025 Hz scored 12.9 dB, barely different.
On this clip, changing the mel formula and filter normalisation nearly tripled the log-spectral distance. Changing the frequency ceiling moved it by 0.6 dB.
LJ001-0009, mel computed with the HTK scale, decoded by the Slaney-trained HiFi-GAN. Log-spectral distance 33.2 dB against the original. Press play.
The full mismatch table, with all four versions side by side, is in mel recipe mismatch. The short version: pin every parameter in one place, and import it.
Next: why a TTS model needs a vocoder at all, which comes down to the one thing a mel spectrogram throws away.
Common questions
What is a mel spectrogram?
A mel spectrogram is a spectrogram whose frequency axis has been squeezed into a small number of bands (80 in our build) spaced on the mel scale, which gives low frequencies finer resolution than high ones. Taking the log of the band energies gives the log-mel spectrogram most TTS acoustic models predict.
What mel settings does the SpeechBrain LJSpeech HiFi-GAN expect?
Its model card lists 22,050 Hz audio, n_fft 1024, hop length 256, 80 mel bands from 0 to 8,000 Hz, magnitude (power 1), the Slaney mel scale and Slaney normalisation, and log compression. Any acoustic model feeding that vocoder has to predict exactly this representation.
Are librosa and torchaudio mel spectrograms the same?
Not by default. librosa uses the Slaney mel formula and Slaney area normalisation; torchaudio MelSpectrogram defaults to the HTK formula, no normalisation, power 2, 128 bands and 16 kHz. You can make them match, but only by setting every parameter explicitly.
We do this work for teams shipping voice agents: Self-Hosted Voice Agent .
Edit this lesson on GitHub ↗Discussion
Questions, corrections and war stories welcome. Sign in with GitHub to comment.