musicgen developer on retainer: MusicGen Small generates 32000 Hz audio — torchaudio.save() called with sample_rate=44100 — playback pitch shift +5.5 semitones — 10-second track plays in 7.26 s — $160,000 — 5 months — musicgen developer on monthly retainer
audiocraft 1.3.0 • torch 2.1.0 • Python 3.10; model.sample_rate = 32000; model.generate(duration=10) returns 320,000 samples; torchaudio.save(path, wav, sample_rate=44100); playback: 320000 / 44100 = 7.26 s; pitch shift: 44100 / 32000 = 1.378× = +5.55 semitones; C major plays as F♯ major; fix: sample_rate=model.sample_rate • Northfield MusicGen Studios Ltd. (4-person AI music generation service; 5 months; 1.5h)
Northfield MusicGen Studios Ltd. is a 4-person startup building an AI music generation service for YouTube and TikTok video creators. Users enter a video duration and a mood description (“upbeat acoustic guitar for a 10-second product promo”); the platform generates a background music loop and delivers a WAV download. The developer used Meta’s MusicGen Small model from the audiocraft library and built a generation and delivery pipeline. Standard music production uses 44100 Hz as the universal audio sample rate — the developer hardcoded this value in the save step, not checking model.sample_rate:
# generate.py — AI background music generation (audiocraft 1.3.0, torch 2.1.0)
import torch, torchaudio
from audiocraft.models import MusicGen
model = MusicGen.from_pretrained("facebook/musicgen-small")
# model.sample_rate = 32000 ← not checked during development
def generate_background_music(description: str, duration_s: int) -> str:
model.set_generation_params(duration=duration_s)
wav = model.generate(descriptions=[description])
# wav shape: [1, 1, duration_s × 32000]
# For duration_s=10: wav.shape = [1, 1, 320000]
output_path = f"/tmp/music_{hash(description)}.wav"
torchaudio.save(
output_path,
wav[0].cpu(),
sample_rate=44100 # ← bug: hardcoded, not model.sample_rate
)
return output_path
# Results on 1,200 user music generation requests:
# User complaint #1: "The music is way too short — I asked for 10 seconds and got 7"
# User complaint #2: "Everything sounds pitched up, like a chipmunk voice effect"
# User complaint #3: "The music is off-key — it clashes with my video's key"
# Support tickets (first 8 weeks): 634
# Audio duration: 7.26 s for every 10-second request (320000 / 44100 = 7.258)
# Pitch shift: +5.55 semitones for every generated track
MusicGen’s generate() method produces an audio tensor at exactly model.sample_rate samples per second. For MusicGen Small (and Medium, Large, and all Stereo variants), model.sample_rate = 32000. This is a property of the EnCodec audio codec that MusicGen uses internally — the codec was trained at 32000 Hz, and all generated tokens decode to 32000 Hz audio. When a 10-second request is made, the output tensor contains exactly 10 × 32000 = 320,000 samples. The torchaudio.save() call writes those 320,000 samples to a WAV file with the header declaring sample_rate=44100. The WAV format stores raw PCM data with no intrinsic timing — the sample rate header is the sole source of timing information for playback.
How the WAV header mismatch causes simultaneous pitch shift and duration error
# MusicGen Small output for duration=10:
# model.sample_rate = 32000 Hz
# samples generated: 320,000 (10 s × 32000 samples/s)
# shape: [1, 1, 320000]
#
# WAV file saved with sample_rate=44100:
# WAV header: 44100 Hz, 320000 samples
# Real content: 32000 Hz audio (notes at concert pitch, correct tempo)
#
# Playback by audio player (reads WAV header):
# Duration: 320000 samples ÷ 44100 samples/s = 7.258 s ← 2.74 s short
# Speed: 44100/32000 = 1.378× faster than intended
# Pitch: every frequency f in generated audio plays at f × 1.378
#
# Pitch shift calculation:
# ratio = 44100 / 32000 = 1.37813
# semitones = 12 × log2(ratio) = 12 × log2(1.37813) = 12 × 0.4625 = 5.55 semitones
#
# Musical impact (concert pitch → playback pitch):
# C4 (261.6 Hz) → 360.3 Hz ≈ between F#4 (370.0 Hz) and F4 (349.2 Hz)
# G4 (392.0 Hz) → 540.3 Hz ≈ between C5 (523.3 Hz) and C#5 (554.4 Hz)
# A4 (440.0 Hz) → 606.6 Hz ≈ between D#5 (622.3 Hz) and D5 (587.3 Hz)
# Generated music in C major sounds like it is in F# major at playback
#
# Video production impact:
# User requests: 10-second intro music for product demo video
# Timeline import (DaVinci Resolve, Premiere Pro):
# Reports: 44100 Hz, 7.26 s — does not match 10-second clip duration
# Stretch-to-fit workaround: further pitch-shifts the already-shifted audio
# Result: music at concert pitch is unrecoverable without knowing the real sample rate
#
# All MusicGen variants have model.sample_rate = 32000:
# musicgen-small: 32000 Hz
# musicgen-medium: 32000 Hz
# musicgen-large: 32000 Hz
# musicgen-stereo-small: 32000 Hz
# musicgen-stereo-medium:32000 Hz
Why five months of model switching and prompt tuning could not fix pitch and duration
The team received user complaints about short duration and chipmunk pitch and attributed the problem to MusicGen Small’s limited model capacity. They reasoned that a larger model would generate higher-quality, correctly-pitched audio. Over five months they ran:
# Month 1: Switch from musicgen-small to musicgen-medium (1.5B params)
# model.sample_rate = 32000 ← same (checked only after bug identified)
# Duration: 7.26 s for 10-second request (unchanged — save code still uses 44100)
# Pitch shift: +5.55 semitones (unchanged)
# Cost increase: 4× GPU memory, 3× generation latency
#
# Month 2: Switch to musicgen-large (3.3B params)
# model.sample_rate = 32000 ← same
# Duration: 7.26 s, pitch +5.55 semitones (unchanged — same bug)
# Cost increase: 8× GPU memory vs Small
#
# Month 3: Prompt engineering — add "concert pitch A=440 Hz" to all descriptions
# "upbeat acoustic guitar at concert pitch A=440 Hz for a 10-second promo"
# Duration: 7.26 s, pitch +5.55 semitones (prompt does not affect WAV header)
#
# Month 4: Post-processing — apply pitch correction via librosa
# librosa.effects.pitch_shift(y, sr=44100, n_steps=-5.55)
# This corrects pitch but not duration; audio is now 7.26 s at correct pitch
# User complaint: "pitch fixed but still too short"
#
# Month 5: Request retainer engagement
# Duration mismatch and 2.74-second silence gap in video timelines persists
#
# Retainer triage (1.5h):
# Step 1: print(model.sample_rate) → 32000
# Step 2: print(wav.shape) → torch.Size([1, 1, 320000])
# Step 3: check generate.py → sample_rate=44100 hardcoded in torchaudio.save()
# Step 4: 320000 / 44100 = 7.258 s; 320000 / 32000 = 10.0 s ← confirms mismatch
# Step 5: 44100 / 32000 = 1.378; 12 × log2(1.378) = 5.55 semitones ← confirms pitch
# Root cause: WAV header declares 44100 Hz; actual content is 32000 Hz
# Fix: sample_rate=model.sample_rate in torchaudio.save()
The retainer consultant identified the sample rate mismatch in 1.5 hours: 0.5h to audit the generation pipeline, print model.sample_rate (32000), trace the hardcoded sample_rate=44100 in torchaudio.save(), and calculate the expected duration mismatch (7.26 s) and pitch shift (+5.55 semitones) from the ratio 44100/32000; 0.5h to run the fix on 20 test prompts with verified duration (confirmed 10.0 s via torchaudio.info().num_frames / torchaudio.info().sample_rate) and pitch (verified at concert pitch using a software tuner); 0.5h to audit the month-4 pitch correction workaround, confirm that librosa pitch-shifted the already-wrong-duration audio, and remove the workaround since it was no longer needed. Hours logged to HourTab with per-session breakdown.
# Fixed generate.py
import torch, torchaudio
from audiocraft.models import MusicGen
model = MusicGen.from_pretrained("facebook/musicgen-small")
# Always read the sample rate from the model — do not hardcode 44100 or 16000
MODEL_SAMPLE_RATE = model.sample_rate # = 32000 for all MusicGen variants
assert MODEL_SAMPLE_RATE == 32000, (
f"Unexpected model.sample_rate={MODEL_SAMPLE_RATE}; "
"update pipeline if audiocraft changes codec sample rate"
)
def generate_background_music(description: str, duration_s: int) -> str:
model.set_generation_params(duration=duration_s)
wav = model.generate(descriptions=[description])
# wav shape: [1, channels, duration_s × MODEL_SAMPLE_RATE]
expected_samples = duration_s * MODEL_SAMPLE_RATE
assert wav.shape[-1] == expected_samples, (
f"Expected {expected_samples} samples for {duration_s}s; "
f"got {wav.shape[-1]} — model.sample_rate mismatch"
)
output_path = f"/tmp/music_{hash(description)}.wav"
torchaudio.save(
output_path,
wav[0].cpu(),
sample_rate=MODEL_SAMPLE_RATE # ← 32000, not 44100
)
return output_path
# Optional: resample to 44100 Hz if downstream workflow requires it
# wav_44k = torchaudio.functional.resample(wav[0].cpu(), 32000, 44100)
# torchaudio.save(output_path_44k, wav_44k, sample_rate=44100)
# After fix (20 test prompts):
# Duration: 10.000 s for 10-second request (was 7.26 s)
# Pitch: concert pitch verified (C major confirmed as C major)
# Support tickets resolved: 634 pitch/duration complaints; 0 new post-fix
# GPU model downgraded back to Small (Medium/Large switch was unnecessary)
# Cost reduction: 8× GPU memory freed vs Large, 3× latency improvement
Related retainer posts
- bark developer on retainer — bark 0.1.5;
generate_audio()returns 24000 Hz array; saved withsample_rate=22050; playback 8.2% faster; pitch −1.5 semitones; fix:SAMPLE_RATE = bark.generation.SAMPLE_RATE; 1.5h - encodec developer on retainer — encodec 0.1.1;
EncodecModel.encodec_model_24khz(); decoded output at 24000 Hz written as 48000 Hz WAV; playback 2× faster; pitch +12 semitones (1 octave up); fix:model.sample_rate; 1.5h - vocos developer on retainer — vocos 0.1.0;
Vocos.from_pretrained('charactr/vocos-mel-24khz'); output 24000 Hz; saved as 44100 Hz; pitch +10.5 semitones; fix:vocos.sample_rate; 2.0h
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 →