speechbrain TTS developer on retainer: HiFiGAN.decode_batch() output is 22050 Hz — sf.write(audio, 16000) writes wrong sample rate header — voice 38% slower and 5.6 semitones lower — MOS 2.4 vs 4.3 — €350,000 — 11 months — speechbrain TTS developer on monthly retainer

speechbrain 0.5.15 • PyTorch 1.13.1 • Python 3.9; HifiGan.decode_batch() produces 22050 Hz waveform; developer saves with sf.write(output_path, audio.numpy(), 16000); libsndfile writes 22050-sample waveform with 16000 Hz header; audio plays at 72.6% speed; pitch −5.6 semitones (female narrator sounds like baritone); MOS 2.4 vs 4.3 expected; 23 audiobooks rejected; 11 months of prosody and vocoder fixes; €350,000; fix: sf.write(output_path, audio.numpy(), 22050) • Westfield SpeechBrain TTS Ltd. (5-person audiobook narration platform; 11 months; 1.5h)

Westfield SpeechBrain TTS Ltd. is a 5-person startup building an automated audiobook narration platform for independent publishers. Their pipeline uses SpeechBrain’s pre-trained Tacotron2 acoustic model (fine-tuned on 40 hours of proprietary narrator voice data) combined with the LJSpeech-trained HiFiGAN neural vocoder. The synthesis code was straightforward:

# tts_pipeline.py (speechbrain 0.5.15)
from speechbrain.inference.TTS import Tacotron2
from speechbrain.inference.vocoders import HifiGan
import soundfile as sf

tacotron2 = Tacotron2.from_hparams(
    source="westfield-tts/narrator-tacotron2-custom",
    savedir="pretrained_models/tacotron2",
)
hifi_gan = HifiGan.from_hparams(
    source="speechbrain/tts-hifigan-ljspeech",   # LJSpeech: 22050 Hz
    savedir="pretrained_models/hifigan",
)

def synthesize(text: str, output_path: str):
    mel_output, mel_len, alignment = tacotron2.encode_batch([text])
    # mel_output: [1, n_frames, n_mels=80]
    # HiFiGAN (LJSpeech) produces audio at 22050 Hz
    waveform = hifi_gan.decode_batch(mel_output)
    # waveform: [1, 1, n_samples_at_22050hz]
    audio = waveform.squeeze().cpu().numpy()
    # BUG: sf.write(path, data, samplerate) records samplerate in WAV header ONLY
    # it does NOT resample — samples are written verbatim at the stated header rate
    sf.write(output_path, audio, 16000)   # <-- BUG: should be 22050

The engineer who wrote the audio I/O code worked primarily with 16 kHz speech recognition data. The assumption was that sf.write(path, data, 16000) would resample the audio to 16 kHz, matching the company’s standard audio format. This is incorrect: soundfile.write()’s samplerate argument is a WAV file header value — it tells downstream systems what rate to use for playback and analysis, but it performs no resampling on the data. The 22050 samples generated per second of speech are written as-is to the file; only the header label changes.

What 16 kHz headers on 22050 Hz samples sound like

When an audio player reads a WAV file with samplerate=16000 and plays it back at that rate, it allocates exactly 1/16000 seconds per sample. The 22050 samples that represent one second of speech at 22050 Hz now span 22050 ÷ 16000 = 1.378 seconds of playback — the audio is 37.8% longer than it should be. The pitch is simultaneously shifted downward because the inter-sample spacing of harmonic cycles is unchanged but each sample takes more calendar time to play:

# Physics of the sample-rate header mismatch
# Narrator voice: female, natural F0 ≈ 195 Hz

# At correct 22050 Hz playback:
# - Samples per F0 cycle: 22050 / 195 = 113.1 samples
# - Playback: 113.1 samples × (1/22050 s/sample) = 5.13 ms per cycle
# - Perceived F0: 1/0.00513 = 195 Hz ✓

# At wrong 16000 Hz playback (from mis-labelled header):
# - Same 113.1 samples per F0 cycle (unchanged in the file)
# - Playback: 113.1 samples × (1/16000 s/sample) = 7.07 ms per cycle
# - Perceived F0: 1/0.00707 = 141 Hz  (baritone male range)
# - Pitch shift: 12 × log2(141/195) = 12 × (-0.466) = -5.59 semitones

# Speaking rate effect:
# Original: 150 words/min at 22050 Hz
# Apparent: 150 × (16000/22050) = 109 words/min  (38% slower)

At 141 Hz perceived F0 and 109 words per minute, the custom fine-tuned female narrator voice sounded like a slow-speaking male baritone. Human MOS evaluators — recruited as standard audiobook listeners, not audio engineers — rated naturalness at 2.4 out of 5.0. The company submitted 23 completed audiobooks to independent publishers over 11 months; all 23 were rejected.

Eleven months of prosody interventions

Because the pitch and speed anomalies presented identically to prosody model failures, the team’s interventions all targeted the Tacotron2 acoustic model and vocoder rather than the audio I/O layer:

# Eleven months of interventions (sf.write still uses samplerate=16000)

# Month 1: Tacotron2 prosody fine-tuning
# Additional 15h narrator recordings + prosody-specific augmentation
# MOS 2.4 → 2.5

# Month 2: Pitch contour conditioning
# Add pitch F0 as auxiliary conditioning feature to Tacotron2
# MOS 2.5 → 2.6

# Month 3: Time-stretch post-processing
# Apply 1.1× time compression to "speed up" the speech
# MOS 2.6 → 2.4  (made worse: now pitch-shifted AND at wrong tempo)

# Month 4: Switch to FastSpeech2
# Hypothesis: Tacotron2's attention mechanism causing duration errors
# MOS 2.4 → 2.3  (FastSpeech2 also outputs 22050 Hz; same wrong header)

# Month 5: Custom HiFiGAN training at 16000 Hz
# Train HiFiGAN from scratch on narrator voice at 16kHz
# Problem: Tacotron2 mel spectrograms are on 22050 Hz filterbank basis
# Vocoder trained for 16kHz mel spectrograms but receiving 22050 Hz mels
# MOS 2.3 → 2.5  (slight improvement; still fundamentally mismatched)

# Months 6-11: Incremental prosody and vocoder experiments
# MOS ceiling: 2.5 to 2.7 across all configurations
# Ceiling explanation: a 5.6-semitone pitch shift and 38% speed change
# cannot be corrected by prosody model improvements
# the error is in the audio output layer, not the acoustic model

The retainer consultant who identified the issue in month 11 spent 1.5 hours on the engagement: 0.25h to audit the full synthesis pipeline and find the sf.write(output_path, audio, 16000) call; 0.25h to verify the HiFiGAN model card specified 22050 Hz output and explain the soundfile.write() header-only semantics to the team; 0.5h to change the samplerate argument, synthesize 20 test sentences, confirm MOS 4.3 on immediate re-evaluation with the same 12 evaluators; 0.5h to audit all audio I/O in the codebase for similar mismatches, add the sample rate assertion, and add a duration validation check. Hours logged to HourTab with per-session breakdown.

# Fixed tts_pipeline.py

# Step 1: check the vocoder model card for output sample rate
# speechbrain/tts-hifigan-ljspeech → LJSpeech → 22050 Hz
VOCODER_SAMPLE_RATE = 22050   # from model card

def synthesize(text: str, output_path: str):
    mel_output, mel_len, alignment = tacotron2.encode_batch([text])
    waveform = hifi_gan.decode_batch(mel_output)
    audio = waveform.squeeze().cpu().numpy()

    # Assertion: catch sample rate mismatch before writing
    # sf.write(path, data, samplerate) → samplerate is a header label, NOT a resample target
    # passing the wrong samplerate produces mis-pitched, wrong-speed audio silently
    expected_sr = VOCODER_SAMPLE_RATE
    sf.write(output_path, audio, expected_sr)   # was: 16000

# After fix:
# MOS naturalness: 4.3 / 5.0  (was: 2.4 with wrong sample rate)
# Speaking rate:   150 wpm    (was: 109 wpm at 72.6% speed)
# Perceived F0:    194 Hz     (was: 141 Hz — 5.6 semitones lower)

# Duration validation (add to CI):
def validate_synthesis_duration(text, output_path, words_per_minute=150, tolerance=0.25):
    import soundfile as sf
    audio, sr = sf.read(output_path)
    actual_duration = len(audio) / sr
    word_count = len(text.split())
    expected_duration = (word_count / words_per_minute) * 60
    ratio = actual_duration / expected_duration
    assert 1 - tolerance < ratio < 1 + tolerance, (
        f"Synthesis duration {actual_duration:.2f}s is {ratio:.2f}× expected {expected_duration:.2f}s. "
        f"Check vocoder sample rate matches sf.write() samplerate argument."
    )

Related retainer posts


Track retainer hours without a client portal

HourTab turns your time-tracker CSV into a public URL your client bookmarks. No login. No portal. CSV in, URL out.

See pricing →