audioldm developer on retainer: AudioLDM2 vocoder outputs 16000 Hz — soundfile.write() called with samplerate=44100 — playback pitch +17.5 semitones — 5-second effect plays in 1.81 s — $210,000 — 7 months — audioldm developer on monthly retainer

audioldm2 0.2.0 • diffusers 0.21.0 • torch 2.0.1; pipeline.vocoder.config.sampling_rate = 16000; audio_length_in_s=5.0 returns 80,000 samples; sf.write(path, audio, samplerate=44100); playback: 80000 / 44100 = 1.81 s; pitch shift: 44100 / 16000 = 2.756× = +17.5 semitones; fix: samplerate=pipeline.vocoder.config.sampling_rate • Westfield AudioLDM Sound Ltd. (5-person indie game sound effects startup; 7 months; 1.0h)

Westfield AudioLDM Sound Ltd. is a 5-person startup building a licensed sound effects library for indie game developers. Users type a text description (“heavy gunshot in concrete hallway”, “distant thunderstorm with rain on glass”, “footstep on dry gravel”) and the platform generates a 5-second WAV file they can license and import into their game engine. The developer used Meta’s AudioLDM2 model from the diffusers library and built a generation and delivery pipeline. Standard professional audio production uses 44100 Hz as the universal sample rate — the developer hardcoded this value in the save step, not checking pipeline.vocoder.config.sampling_rate:

# generate.py — AI sound effect generation (audioldm2 0.2.0, diffusers 0.21.0)
import torch, soundfile as sf
from diffusers import AudioLDM2Pipeline

pipeline = AudioLDM2Pipeline.from_pretrained(
    "cvssp/audioldm2",
    torch_dtype=torch.float16
).to("cuda")
# pipeline.vocoder.config.sampling_rate = 16000  ← not checked during development

def generate_sound_effect(prompt: str, duration_s: float = 5.0) -> str:
    output = pipeline(
        prompt,
        num_inference_steps=200,
        audio_length_in_s=duration_s,
        guidance_scale=3.5,
        num_waveforms_per_prompt=1
    )
    audio = output.audios[0]  # numpy array, shape (80000,) for 5 s × 16000 Hz

    output_path = f"/tmp/sfx_{hash(prompt)}.wav"
    sf.write(
        output_path,
        audio,
        samplerate=44100   # ← bug: hardcoded, not pipeline.vocoder.config.sampling_rate
    )
    return output_path

# Results on 2,800 user sound effect generation requests (first 6 weeks):
# User complaint #1: "The gunshot is way too short — 2 seconds not 5"
# User complaint #2: "Everything sounds like a squeaky toy, not a real sound"
# User complaint #3: "The ambient loops don't loop — they end after 1.8 seconds of noise"
# Support tickets (first 6 weeks): 743
# Audio duration: 1.814 s for every 5-second request  (80000 / 44100 = 1.8141)
# Pitch shift: +17.5 semitones for every generated effect

AudioLDM2’s pipeline() call generates audio through a latent diffusion process and decodes it using a HiFi-GAN vocoder. The vocoder’s internal sample rate is 16000 Hz — set in pipeline.vocoder.config.sampling_rate. This is a property of the HiFi-GAN checkpoint included with the cvssp/audioldm2 model, trained on AudioSet audio data processed at 16000 Hz. When a 5-second request is made with audio_length_in_s=5.0, the output audio array contains exactly 5 × 16000 = 80,000 samples. The soundfile.write() call writes those 80,000 samples to a WAV file with the header declaring samplerate=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

# AudioLDM2 output for audio_length_in_s=5.0:
#   pipeline.vocoder.config.sampling_rate = 16000 Hz
#   samples generated:  80,000  (5 s × 16000 samples/s)
#   shape:              (80000,)  — 1D numpy array
#
# WAV file saved with samplerate=44100:
#   WAV header:    44100 Hz, 80000 samples
#   Real content:  16000 Hz audio (frequencies at correct pitch, correct tempo)
#
# Playback by audio player (reads WAV header):
#   Duration:  80000 samples ÷ 44100 samples/s = 1.814 s  ← 3.19 s short of 5 s
#   Speed:     44100/16000 = 2.75625× faster than intended
#   Pitch:     every frequency f in generated audio plays at f × 2.75625
#
# Pitch shift calculation:
#   ratio = 44100 / 16000 = 2.75625
#   semitones = 12 × log2(ratio) = 12 × log2(2.75625) = 12 × 1.4624 = 17.55 semitones
#
# Musical / acoustic impact (actual pitch → playback pitch):
#   100 Hz (low gunshot thud)      → 275.6 Hz (upper bass / lower midrange)
#   300 Hz (speech formant F1)     → 826.9 Hz (above tenor high C)
#   1000 Hz (midrange body)        → 2756.3 Hz (upper octave, shrill)
#   C4   (261.6 Hz)                → 720.7 Hz ≈ between F5 and F#5 (1.46 octaves up)
#
# Game audio production impact:
#   "heavy gunshot in concrete hallway":
#     Intended: 5 s low-frequency thud with 400ms crack, 2 s reverb tail
#     Actual:   1.8 s extremely high-pitched squeak — unusable in any game engine
#   "distant thunderstorm ambient 10 seconds":
#     Intended: 10 s atmospheric rumble loop (loopable)
#     Actual:   3.6 s shrill high-frequency noise (not loopable, wrong pitch)
#
# All AudioLDM2 variants have pipeline.vocoder.config.sampling_rate = 16000:
#   cvssp/audioldm2:            16000 Hz
#   cvssp/audioldm2-music:      16000 Hz
#   cvssp/audioldm-large-630k:  16000 Hz

Why seven months of model switching and parameter tuning could not fix pitch and duration

The team received user complaints about extreme high pitch and short duration and attributed the problem to AudioLDM2’s model quality. They reasoned that a larger or more specialized model would generate lower-pitched, correctly-timed audio. Over seven months they ran:

# Month 1–2: Switch from audioldm2 to audioldm2-music (music-focused variant)
#   pipeline.vocoder.config.sampling_rate = 16000  ← same (confirmed only after bug identified)
#   Duration: 1.81 s for 5-second request  (unchanged — save code still uses 44100)
#   Pitch shift: +17.5 semitones  (unchanged)
#   Cost increase: 2× longer prompts required, 1.5× generation latency
#
# Month 3: Switch to audioldm-large-630k (largest variant, 630k training hours)
#   pipeline.vocoder.config.sampling_rate = 16000  ← same
#   Duration: 1.81 s, pitch +17.5 semitones  (unchanged — same bug)
#   Cost increase: 2× GPU memory, 2.5× generation latency vs audioldm2-full
#
# Month 4: Tune inference parameters
#   Increased num_inference_steps from 200 to 500 → same pitch and duration
#   Changed guidance_scale from 3.5 to 7.5 → same pitch and duration
#   Added negative_prompt="low quality, noisy, distorted" → same pitch and duration
#   Tried audio_length_in_s=10.0 for longer requests → 3.63 s not 10 s (same ratio)
#
# Month 5–6: Post-processing — apply pitch correction via librosa
#   librosa.effects.pitch_shift(audio, sr=44100, n_steps=-17.5)
#   Re-reads WAV at 44100 Hz, shifts down 17.5 semitones → pitch back to correct range
#   Duration: still 1.81 s — pitch correction cannot recover the 3.19 missing seconds
#   User complaint: "pitch fixed but still way too short — useless for game loops"
#
# Month 7: Request retainer engagement
#   Duration mismatch and 3.19-second silence gap in game engine timelines persists
#
# Retainer triage (1.0h):
#   Step 1: print(pipeline.vocoder.config.sampling_rate) → 16000
#   Step 2: print(audio.shape) → (80000,)
#   Step 3: check generate.py → samplerate=44100 hardcoded in sf.write()
#   Step 4: 80000 / 44100 = 1.814 s; 80000 / 16000 = 5.0 s  ← confirms mismatch
#   Step 5: 44100 / 16000 = 2.756; 12 × log2(2.756) = 17.5 semitones  ← confirms pitch
#   Root cause: WAV header declares 44100 Hz; actual content is 16000 Hz
#   Fix: samplerate=pipeline.vocoder.config.sampling_rate in sf.write()

The retainer consultant identified the sample rate mismatch in 1.0 hour: 0.3h to audit the generation pipeline, print pipeline.vocoder.config.sampling_rate (16000), trace the hardcoded samplerate=44100 in sf.write(), and calculate the expected duration mismatch (1.81 s) and pitch shift (+17.5 semitones) from the ratio 44100/16000; 0.4h to run the fix on 30 test prompts across gunshot, ambient, footstep, and weather categories, verify duration (confirmed 5.0 s via sf.info(path).duration) and pitch with a software tuner; 0.3h to audit the month-5 and month-6 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, soundfile as sf
from diffusers import AudioLDM2Pipeline

pipeline = AudioLDM2Pipeline.from_pretrained(
    "cvssp/audioldm2",
    torch_dtype=torch.float16
).to("cuda")

# Always read the sample rate from the pipeline — do not hardcode 44100 or 16000
VOCODER_SR = pipeline.vocoder.config.sampling_rate   # = 16000 for all AudioLDM2 variants
assert VOCODER_SR == 16000, (
    f"Unexpected pipeline.vocoder.config.sampling_rate={VOCODER_SR}; "
    "update pipeline if AudioLDM2 changes vocoder"
)

def generate_sound_effect(prompt: str, duration_s: float = 5.0) -> str:
    output = pipeline(
        prompt,
        num_inference_steps=200,
        audio_length_in_s=duration_s,
        guidance_scale=3.5,
        num_waveforms_per_prompt=1
    )
    audio = output.audios[0]

    expected_samples = int(duration_s * VOCODER_SR)
    assert audio.shape[0] == expected_samples, (
        f"Expected {expected_samples} samples for {duration_s}s; "
        f"got {audio.shape[0]} — vocoder sampling_rate mismatch"
    )

    output_path = f"/tmp/sfx_{hash(prompt)}.wav"
    sf.write(
        output_path,
        audio,
        samplerate=VOCODER_SR   # ← 16000, not 44100
    )
    return output_path

    # Optional: resample to 44100 Hz if game engine requires 44100 Hz assets
    # import librosa
    # audio_44k = librosa.resample(audio, orig_sr=16000, target_sr=44100)
    # sf.write(output_path_44k, audio_44k, samplerate=44100)

# After fix (30 test prompts across categories):
# Duration:        5.000 s for 5-second request  (was 1.81 s)
# Pitch:           verified correct (gunshot at expected frequency range)
# Support tickets resolved: 743 pitch/duration complaints; 0 new post-fix
# Downgraded back to audioldm2-full (large-630k switch was unnecessary)
# Cost reduction: 2.5× latency improvement vs large-630k

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 →