Sub topic 2
Preparing Audio: 16 kHz Mono and Log-Mel Spectrograms
Prerequisites: You should know basic Python, how to install packages with pip, and the difference between an audio sample rate and an audio channel. You should also know that a neural network consumes numeric arrays rather than MP3 or WAV files directly.
Why Whisper preprocesses audio
Whisper expects audio represented as one mono signal sampled at 16,000 Hz. The sample rate is the number of measurements taken from the sound each second. A 16 kHz signal has 16,000 samples per second.
Mono audio has one channel. Stereo audio has two channels, usually one for the left speaker and one for the right speaker. Converting stereo to mono prevents the model from receiving two separate signals when it was trained to process one.
Whisper can accept many common formats because its loader uses FFmpeg to decode them. The loader resamples the audio to 16 kHz and downmixes multiple channels to mono.
import whisper
audio = whisper.load_audio("speech.mp3")
print(audio.dtype)
print(audio.shape)
print(audio.shape[0] / whisper.audio.SAMPLE_RATE)The returned array contains floating-point samples. Its length divided by 16,000 gives the duration in seconds. FFmpeg must be installed and available on your system path.
From samples to a log-Mel spectrogram
A waveform contains amplitude values over time. Whisper does not pass the complete waveform directly into its transformer. It first converts the waveform into a spectrogram.
A spectrogram divides sound into short overlapping windows. For each window, it estimates how much energy appears at different frequencies. Whisper then maps these frequencies into 80 Mel bands. A Mel band is a frequency range designed to roughly reflect how people hear pitch differences.
The result is a Mel spectrogram with time on one axis and 80 frequency bands on the other. Whisper applies a logarithm to the energy values. This produces a log-Mel spectrogram, where large and small sound energies fit into a more useful numeric range.
Whisper uses a 25 millisecond analysis window and a 10 millisecond hop between adjacent windows:
n_fft = 400samples, which equals 25 ms at 16 kHz.hop_length = 160samples, which equals 10 ms.n_mels = 80frequency bands.- A 30-second input produces about 3,000 time frames.
The model normalizes the log values after limiting the lowest values relative to the loudest part of the input. This reduces the effect of overall recording volume.
import whisper
audio = whisper.load_audio("speech.mp3")
audio = whisper.pad_or_trim(audio)
mel = whisper.log_mel_spectrogram(audio, n_mels=80)
print(f"Audio samples: {audio.shape[0]}")
print(f"Mel shape: {tuple(mel.shape)}")For a normal 30-second input, mel has the shape (80, 3000). The first dimension contains Mel bands. The second dimension contains time frames. Longer recordings are processed in separate windows rather than being given to the model as one unlimited array.
Common preprocessing errors
Do not resample by changing only a metadata field. That changes the claimed sample rate without changing the samples. Use a real resampling operation, such as Whisper's loader or FFmpeg.
Do not average channels without considering their phase. Simple downmixing is usually adequate for ordinary speech, but badly aligned stereo channels can partially cancel each other. Listen to the mono result when audio quality matters.
Do not normalize every short chunk independently when comparing recordings. Independent normalization can hide genuine volume differences and make evaluation less consistent. Let Whisper apply its normal feature scaling, and keep your evaluation pipeline consistent.
>callout note > Whisper's load_audio and log_mel_spectrogram functions are safer than manually recreating the preprocessing path. Manual preprocessing is useful when integrating another audio system, but small differences in padding, windowing, or normalization can change model behavior.
Exercise
Why does Whisper use a log-Mel spectrogram instead of passing raw stereo MP3 data to the transformer?
Show answer
The loader converts the file into a consistent 16 kHz mono waveform. The spectrogram summarizes short-time frequency energy, the Mel filter bank compresses frequencies into 80 hearing-oriented bands, and logarithmic scaling makes the energy range easier for the model to use.