audiogen developer on retainer: AudioGen medium outputs 16000 Hz — torchaudio.save(path, wav, 44100) hardcoded from AudioLDM pipeline — audio 2.756× too fast — pitch +17.5 semitones — $125,000 — 4 months — audiogen developer on monthly retainer
audiocraft 1.3.0 (Meta AudioGen, facebook/audiogen-medium) • PyTorch 2.1.0 • Python 3.11; model.sample_rate = 16000; torchaudio.save(output_path, wav, 44100) hardcoded from prior AudioLDM 2 pipeline; 16000 Hz PCM written with 44100 Hz WAV header; Unity AudioSource reads 44100 Hz from header; playback 2.756× too fast; pitch +17.5 semitones (12 × log⊂2;(44100/16000)); 500 sound effects unusable; fix: torchaudio.save(output_path, wav, model.sample_rate) • Northfield AudioGen Games Ltd. (4-person indie game studio; 4 months; 1.5h)
Northfield AudioGen Games Ltd. is a 4-person indie game studio building an open-world RPG that uses generative AI to produce environmental sound effects at runtime. Their original sound generation pipeline used AudioLDM 2 (cvssp/audioldm2-full), which produces audio at 44100 Hz. When Meta released AudioGen with better environmental sound fidelity (rain, thunder, crowds, wildlife), the team migrated to facebook/audiogen-medium for sound generation. The audio saving code — which had been written and tested during the AudioLDM 2 era — used a hardcoded torchaudio.save(output_path, wav, 44100). AudioGen produces audio at model.sample_rate = 16000 (all AudioGen model sizes use a 16kHz EnCodec codec). The 16000 Hz PCM data was saved with a 44100 Hz WAV header. Unity’s AudioSource loaded the file, read the 44100 Hz sample rate from the WAV header, and played back the 16000 Hz content at 2.756× the correct speed. The first play-test session revealed the problem immediately: a thunderstorm sounded like a metallic rattle, and a crowd cheering sounded like squealing.
# sound_generator.py — Northfield AudioGen Games Ltd.
# audiocraft 1.3.0; PyTorch 2.1.0; Python 3.11
import torch
import torchaudio
from audiocraft.models import AudioGen
# AudioGen medium — EnCodec 16kHz codec
# model.sample_rate = 16000
model = AudioGen.get_pretrained('facebook/audiogen-medium')
model.set_generation_params(duration=3.0)
# model.sample_rate == 16000 ← AudioGen medium native output rate
SAVE_SAMPLE_RATE = 44100 # Bug: hardcoded from AudioLDM 2 era
# AudioLDM 2 (cvssp/audioldm2-full): model.sampling_rate=44100
# AudioGen medium: model.sample_rate=16000
# These are different models with different sample rates
DESCRIPTIONS = [
'thunderstorm with heavy rain and distant thunder',
'crowd of people cheering at a stadium',
'dense forest at night with crickets and frogs',
'city traffic with car horns and sirens',
'ocean waves crashing on a rocky shore',
]
for i, desc in enumerate(DESCRIPTIONS):
wav = model.generate([desc]) # shape: [1, 1, N] at 16000 Hz
wav = wav.squeeze(0).cpu() # shape: [1, N]
torchaudio.save(
f'sfx_{i:04d}.wav',
wav,
SAVE_SAMPLE_RATE # Bug: writes 44100 Hz header on 16000 Hz data
)
# WAV header: samplerate = 44100
# PCM data: 16000 samples per second of sound
#
# Unity AudioSource reads header samplerate = 44100
# Consumes 44100 samples per second of wall time
# 16000 samples (1 second of sound) consumed in 16000/44100 = 0.363 seconds
# Speed: 44100/16000 = 2.756× too fast
# Pitch: 12 × log2(44100/16000) = 12 × 1.463 = +17.5 semitones
#
# 'thunderstorm': low rumble at 80 Hz → perceived at 80 × 2.756 = 220 Hz (A3)
# sounds like metallic rattling, not thunder
# 'crowd cheering': typical F0 ~200 Hz → perceived at 200 × 2.756 = 551 Hz
# sounds like high-pitched squealing
The AudioLDM 2 to AudioGen migration created a silent incompatibility because both models use model.sample_rate (or model.sampling_rate in AudioLDM’s diffusers-based API) as their attribute, but report different values. The team’s prior test confirmed that torchaudio.save(path, wav, 44100) was correct for AudioLDM 2 — the WAV files played back correctly in Unity, the game shipped its first alpha. When AudioGen replaced AudioLDM 2, no one audited the save call because the code structure looked identical: wav = model.generate(...) followed by torchaudio.save(..., 44100). The only difference was the model’s internal sample rate, which was invisible to anyone reading the generation code.
How 16000 Hz output saved with a 44100 Hz header produces 2.756× playback and +17.5 semitone pitch shift
# Sample rate mismatch arithmetic — Northfield AudioGen Games Ltd.
#
# AudioGen medium model:
# model.sample_rate = 16000 ← EnCodec 16kHz codec; all AudioGen sizes use 16kHz
#
# AudioLDM 2 model (prior pipeline):
# model.sampling_rate = 44100 ← latent diffusion on 64-mel spectrogram; HiFi-GAN 44.1kHz
#
# Bug: torchaudio.save(path, wav, 44100) — correct for AudioLDM 2; wrong for AudioGen
#
# Example: 'thunderstorm with heavy rain' — target duration 3.0 seconds
# AudioGen generates: N = 3.0 × 16000 = 48,000 samples
# WAV file header written: samplerate = 44100
#
# Unity AudioSource reads WAV header: samplerate = 44100
# Consumes 44100 samples per second of wall time
# 48,000 samples ÷ 44100 samples/sec = 1.088 seconds of playback
# Sound content is 3.0 seconds of audio compressed into 1.088 seconds
# Speed ratio: 3.0 / 1.088 = 2.756× too fast
#
# Pitch shift formula:
# Δf = 12 × log2(claimed_rate / actual_rate)
# = 12 × log2(44100 / 16000)
# = 12 × log2(2.756)
# = 12 × 1.463
# = +17.54 semitones ≈ +17.5 semitones
#
# F0 examples for environmental sounds:
# Thunder rumble: 80 Hz (E2) → perceived 80 × 2.756 = 220 Hz (A3) — string tone, not thunder
# Rain white noise: broadband → perceived at 2.756× higher freq center — hiss, not rain
# Crowd cheer: 200 Hz (G3) → perceived 200 × 2.756 = 551 Hz (C#5) — squealing
# Ocean wave: 100 Hz (G2) → perceived 100 × 2.756 = 276 Hz (C#4) — tapping, not waves
# Cricket chirp: 4000 Hz → perceived 4000× 2.756 = 11,024 Hz — ultrasonic-range, inaudible
#
# CLAP similarity (audio–text cosine similarity via LAION CLAP model):
# Correct output (model.sample_rate=16000): avg CLAP score = 0.31
# Mismatch output (save at 44100): avg CLAP score = 0.07
# (Perceived audio no longer matches the text description after pitch/speed corruption)
#
# Duration:
# Target: 3.0 seconds (set via model.set_generation_params(duration=3.0))
# Generated: N = 48,000 samples at 16000 Hz = 3.0 seconds (correct generation)
# Playback: 48,000 / 44100 = 1.088 seconds (wrong — 63.7% shorter than target)
#
# 500 sound effects generated:
# 100% unusable — all suffer same speed/pitch mismatch
# Game alpha shipped with no dynamic sound effects (silent environment)
Why four months of AudioGen parameter tuning could not fix a WAV header
The team immediately recognized the audio was “wrong” in playtest, but diagnosed it as a generation quality issue rather than a file format issue. Because AudioLDM 2 had worked correctly with torchaudio.save(..., 44100), the team’s working hypothesis was that AudioGen’s generation quality was simply worse — producing high-pitched, fast-sounding audio instead of natural environmental sound. Over four months they tried every generation parameter and even attempted fine-tuning before engaging a retainer consultant.
# Month 1: Reverted to AudioLDM 2 to compare
# Hypothesis: AudioGen quality is worse than AudioLDM 2 for game sound effects
# Switched back to AudioLDM 2 (cvssp/audioldm2-full)
# model.sampling_rate = 44100; torchaudio.save(..., 44100) → correct (accidentally)
# AudioLDM 2 output: plays correctly at correct speed and pitch in Unity
# Conclusion: "AudioLDM 2 works; AudioGen sounds terrible; AudioGen unusable"
# Wrong conclusion: AudioLDM 2 worked because its sample rate (44100) matched the
# hardcoded save value (44100); AudioGen failed because its sample rate (16000) did not
# The team attributed AudioLDM 2's correctness to quality, not sample rate match
#
# Month 2: Tried AudioGen model variants
# Hypothesis: 'audiogen-medium' may have quality issues; try other sizes
# Tested facebook/audiogen-small (model.sample_rate=16000) — same issue
# Tested facebook/audiogen-large (model.sample_rate=16000) — same issue
# torchaudio.save(..., 44100) — Bug unchanged for all variants
# CLAP score: 0.07 for all AudioGen sizes
# Conclusion: "AudioGen architecture itself produces wrong output for game audio"
#
# Month 3: Fine-tuned AudioGen on 44100 Hz game sound library
# Hypothesis: AudioGen needs domain adaptation on game-specific sounds
# Downloaded commercial game sound library: 2,000 clips at 44100 Hz
# Fine-tuned facebook/audiogen-medium on 44100 Hz clips for 48 GPU hours
# Model internals: EnCodec codec is frozen during fine-tuning; sample rate = 16000
# Fine-tuned model still generates at 16000 Hz (codec cannot be changed by fine-tuning)
# torchaudio.save(..., 44100) — Bug unchanged
# CLAP score after fine-tuning: 0.08 (marginal improvement in generation semantics;
# sample rate mismatch unchanged; output still 2.756× too fast)
# "Fine-tuning improved semantic match slightly but sound is still wrong"
# GPU cost: $2,400 (48h × $50/h A100 on-demand)
#
# Month 4: Retainer engagement
#
# Retainer triage (1.5h):
# Step 1 (0.3h): Compare AudioGen and AudioLDM 2 model attributes
# AudioGen: model.sample_rate = 16000
# AudioLDM 2: model.sampling_rate = 44100
# torchaudio.save(..., 44100) hardcoded
# Mismatch: AudioGen 16000 ≠ save 44100 — found immediately
# Step 2 (0.3h): Verify WAV header on existing files
# torchaudio.info('sfx_0000.wav').sample_rate = 44100 ← wrong
# torchaudio.info('sfx_0000.wav').num_frames = 48000 ← 3.0s at 16000 Hz
# true_duration = 48000 / 16000 = 3.0s; header claims 48000/44100 = 1.09s
# Step 3 (0.5h): Fix save call; regenerate 20 test effects; verify in Unity
# torchaudio.save(path, wav, model.sample_rate) ← 16000 Hz header
# Unity AudioSource: correct speed; correct pitch
# CLAP score: 0.07 → 0.31
# Step 4 (0.4h): Add assertion; audit all save paths in codebase
# assert save_sr == model.sample_rate, (
# f"save sample_rate={save_sr} ≠ model.sample_rate={model.sample_rate}"
# )
# 4 engineers × 4 months × $7,813/month = $125,000
# Additional wasted GPU cost: $2,400 (fine-tuning that changed nothing)
Month 1’s revert to AudioLDM 2 contained the decisive clue that was not recognized at the time. AudioLDM 2 worked correctly with torchaudio.save(..., 44100) not because its generation quality was better, but because its output sample rate happened to match the hardcoded save value. The two models had identical API shapes — model.generate() returning a tensor, torchaudio.save() writing to disk — but different model.sample_rate values. The team’s Month 1 conclusion that “AudioLDM 2 produces correct sound” was correct; the conclusion that “AudioGen produces wrong sound” was wrong. AudioGen produced correct sound; the WAV header was wrong. The fix was replacing 44100 with model.sample_rate in torchaudio.save(). Hours logged to HourTab with per-session breakdown.
# Fixed sound_generator.py
import torch
import torchaudio
from audiocraft.models import AudioGen
model = AudioGen.get_pretrained('facebook/audiogen-medium')
model.set_generation_params(duration=3.0)
# Assert sample rate so future model swaps fail loudly if rate changes
assert model.sample_rate == 16000, (
f"model.sample_rate={model.sample_rate} — update assertion if model changes"
)
DESCRIPTIONS = [
'thunderstorm with heavy rain and distant thunder',
'crowd of people cheering at a stadium',
'dense forest at night with crickets and frogs',
'city traffic with car horns and sirens',
'ocean waves crashing on a rocky shore',
]
for i, desc in enumerate(DESCRIPTIONS):
wav = model.generate([desc]).squeeze(0).cpu()
torchaudio.save(
f'sfx_{i:04d}.wav',
wav,
model.sample_rate # ← FIX: use model.sample_rate (16000), not hardcoded 44100
)
# After fix — Unity AudioSource results (500 regenerated sound effects):
# Playback speed: 1.0× (was 2.756× too fast)
# Pitch shift: 0 semitones (was +17.5 semitones)
# CLAP similarity: 0.31 (was 0.07)
# Usable effects: 487/500 (97.4%; remainder re-prompted for better semantics)
# Root cause: AudioGen model.sample_rate=16000; prior pipeline hardcoded 44100
Related retainer posts
- silero tts developer on retainer — silero-models v3 (v3_1_ru); v2→v3 upgrade; v3 output 24000 Hz;
soundfile.write(samplerate=16000)hardcoded from v2 era; audio 1.5× too slow; pitch −7.0 semitones; fix:model.sample_rate; 1.5h - magphase developer on retainer — magphase 2.3; MagPhase vocoder output 16000 Hz;
torchaudio.save(sample_rate=22050)hardcoded from WaveGlow era; audio 1.38× too fast; pitch +5.6 semitones; fix:sample_rate=magphase_model.fs; 1.5h - audiocraft developer on retainer — audiocraft MusicGen and EnCodec; related sample rate and generation pipeline issues; see post for details
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 →