pytsmod developer on retainer: wsola(audio, s=2.0) produces 2× longer output instead of 2× faster — s is time-stretch factor not speed multiplier — downstream ASR receives half-speed speech — WER 0.08 → 0.31 — 3 weeks — pytsmod developer on monthly retainer

pytsmod 0.3.1 • Python 3.9; pytsmod.wsola(x, s=2.0) intended for 2× speed-up; s is time-stretch factor (output_length / input_length); s=2.0 → 2× longer output at half speed (not 2× faster); 16000-sample input → 32000-sample output; downstream ASR receives 2-second half-speed clips instead of 0.5-second double-speed clips; WER 0.08 → 0.31; 3 weeks of WSOLA parameter tuning; fix: s=0.5 for 2× speed-up; assert len(output) < len(input) catches direction error • Southfield Pytsmod Ltd. (audio speed normalization for ASR; 100,000 clips/month; 3 weeks; 2.0h retainer engagement)

Southfield Pytsmod Ltd. built an audio speed normalization preprocessing pipeline for an automatic speech recognition (ASR) system. The pipeline used pytsmod (0.3.1) to normalize fast speakers to a target speaking rate before transcription: speakers above 180 words per minute were sped up slightly to consume less compute in the ASR model, and slow speakers (below 100 wpm) were sped up more aggressively. The developer’s goal for slow speakers: apply 2× speed-up to halve the clip duration, reducing ASR inference time and improving model performance on over-articulated slow speech. The developer wrote output = pytsmod.wsola(x, s=2.0), reasoning that s=2.0 meant “2× speed.” In pytsmod’s WSOLA implementation, the parameter s is the time-stretch factor: the ratio of output samples to input samples (output_length / input_length). When s = 2.0, the output is 2× longer than the input — the speech plays back at half speed (slower), not double speed. A 1-second 16 kHz input (16,000 samples) produced a 32,000-sample output (2 seconds of half-speed speech). The downstream Whisper ASR model received 2-second half-speed clips instead of 0.5-second double-speed clips. Whisper’s mel spectrogram extraction and temporal attention patterns are calibrated for normal speaking rates; half-speed speech has stretched temporal envelopes that fall outside the model’s training distribution. Word error rate on the slow-speaker subset rose from 0.08 to 0.31. The 3-week investigation explored WSOLA algorithm parameters, Whisper model decoding temperature, and beam search width, none of which addressed the direction error.

The source of the confusion: in audio player UI controls, “2× speed” means the audio plays back twice as fast and takes half as long. This is the speed multiplier convention: speed = playback_rate / original_rate. In time-scale modification (TSM) algorithms, the controlling parameter is the stretch factor: s = output_length / input_length. These two conventions are reciprocals of each other: speed = 1 / s. Libraries using the speed convention include librosa.effects.time_stretch (also uses a time-stretch factor — same convention as pytsmod, documented as “rate: Positive rate of speed-up — rate>1 speeds up; rate<1 slows down”, which inverts the intuition again relative to “s”) and rubberband CLI (--tempo 2.0 means 2× faster). The inconsistency across libraries makes this a frequent source of direction errors. pytsmod’s WSOLA produces the correct output for the given s — at s=2.0, len(output) ≈ len(input) * 2 — but the developer did not check output length against expected length. The 3-week investigation found no defect in the WSOLA algorithm because there was none: the audio quality was perfect, just at the wrong speed. The diagnostic was simply print(len(output)): 32,000 instead of the expected 8,000 immediately revealed the wrong direction.

The annotated buggy and fixed speed normalization pipeline, with direction assertion and speed-to-stretch conversion:

# Southfield Pytsmod Ltd. — pytsmod 0.3.1 stretch factor direction fix
# pytsmod 0.3.1; Python 3.9; NumPy 1.24
# Audio speed normalization for ASR preprocessing; 100,000 clips/month
#
# Problem: pytsmod.wsola(x, s=2.0) for 2× speed-up
#          s is time-stretch factor = output_length / input_length
#          s=2.0 → output is 2× LONGER (half speed / slower), NOT 2× shorter (faster)
#          Speed multiplier = 1 / s — to speed up 2× use s=0.5
#          Developer expected output_len=8000 for 16000-sample input; got 32000
#          Downstream Whisper receives 2-second half-speed clips (not 0.5s double-speed)
#          WER 0.08 → 0.31 on slow-speaker test set
# Fix:     s = 1 / speed_multiplier → s=0.5 for 2× speed-up

import numpy as np
import pytsmod
import soundfile as sf


SAMPLE_RATE = 16000


# ── BUGGY speed normalization ─────────────────────────────────────────────────

def normalize_speed_buggy(audio: np.ndarray, target_speed_multiplier: float) -> np.ndarray:
    """
    BUGGY: uses speed_multiplier directly as WSOLA stretch factor s.
    For target_speed_multiplier=2.0 (want 2× faster / shorter output):
      wsola(audio, s=2.0) produces 2× LONGER output (half speed, not double).
    output_len ≈ 32000 for 16000-sample input when target was ≈ 8000.
    """
    # BUG: s is stretch factor (output/input), NOT speed multiplier
    # speed_multiplier=2.0 → s should be 0.5 (not 2.0)
    output = pytsmod.wsola(audio, target_speed_multiplier)  # BUG: wrong convention
    return output


# ── FIXED speed normalization ─────────────────────────────────────────────────

def normalize_speed(
    audio: np.ndarray,
    target_speed_multiplier: float,
    sample_rate: int = SAMPLE_RATE,
) -> np.ndarray:
    """
    FIXED: convert speed multiplier to WSOLA stretch factor before calling.

    pytsmod stretch factor convention:
      s = output_length / input_length
      s > 1.0  →  output is LONGER  → audio plays SLOWER (speed < 1×)
      s < 1.0  →  output is SHORTER → audio plays FASTER (speed > 1×)
      speed_multiplier = 1 / s

    Speed multiplier convention (audio player / user-facing):
      speed = 2.0  →  audio plays 2× faster (half duration)
      speed = 0.5  →  audio plays 0.5× speed (double duration)
      stretch_factor = 1 / speed_multiplier

    Examples:
      2× speed-up:  speed_multiplier=2.0  → s=0.5  (output 0.5× input length)
      0.75× speed:  speed_multiplier=0.75 → s=1.333 (output 1.333× input length)
      1.5× speed:   speed_multiplier=1.5  → s=0.667 (output 0.667× input length)
    """
    assert target_speed_multiplier > 0, f"Speed must be positive, got {target_speed_multiplier}"

    # Convert speed multiplier to time-stretch factor
    stretch_factor = 1.0 / target_speed_multiplier

    output = pytsmod.wsola(audio, stretch_factor)

    # Direction assertion: verify output length is in expected direction
    expected_len = int(len(audio) * stretch_factor)
    if target_speed_multiplier > 1.0:
        # Speed-up: output must be shorter than input
        assert len(output) < len(audio), (
            f"Speed-up s={stretch_factor:.4f} (speed={target_speed_multiplier}×) "
            f"produced output longer than input: "
            f"len(output)={len(output)} >= len(input)={len(audio)}. "
            f"Check that s = 1/speed_multiplier (not s = speed_multiplier). "
            f"For 2× speed use s=0.5, not s=2.0."
        )
    else:
        # Slow-down: output must be longer than input
        assert len(output) > len(audio), (
            f"Slow-down s={stretch_factor:.4f} (speed={target_speed_multiplier}×) "
            f"produced output shorter than input: "
            f"len(output)={len(output)} <= len(input)={len(audio)}."
        )

    # Length tolerance: WSOLA rounds to frame boundaries; allow ±10 samples
    if abs(len(output) - expected_len) > 10:
        import warnings
        warnings.warn(
            f"WSOLA output length {len(output)} differs from expected {expected_len} "
            f"by {abs(len(output) - expected_len)} samples. "
            f"This is unusual and may indicate incorrect s parameter."
        )

    return output


# ── Batch speed normalization with length logging ─────────────────────────────

def batch_normalize_speed(
    audio_paths: list,
    output_paths: list,
    target_speed_multiplier: float,
    sample_rate: int = SAMPLE_RATE,
) -> list:
    """
    Apply speed normalization to a batch of audio files.
    Returns list of (input_len, output_len, actual_speed) tuples for validation.
    """
    stretch_factor = 1.0 / target_speed_multiplier
    results = []

    for in_path, out_path in zip(audio_paths, output_paths):
        audio, sr = sf.read(in_path)
        assert sr == sample_rate, f"Expected {sample_rate} Hz, got {sr} Hz in {in_path}"

        output = normalize_speed(audio, target_speed_multiplier, sample_rate)
        actual_speed = len(audio) / len(output)

        sf.write(out_path, output, sample_rate)
        results.append((len(audio), len(output), actual_speed))

    # Sanity check: mean actual speed should be close to target
    mean_speed = np.mean([r[2] for r in results])
    if abs(mean_speed - target_speed_multiplier) > 0.05:
        import warnings
        warnings.warn(
            f"Mean actual speed {mean_speed:.3f}× differs from target "
            f"{target_speed_multiplier:.3f}× by more than 5%. "
            f"Verify stretch_factor = 1 / speed_multiplier = {stretch_factor:.4f}."
        )

    return results

Why s=2.0 in pytsmod produces half-speed audio and why the wrong direction is easy to miss for 3 weeks

Three factors combine to make this a multi-week debugging session. First, the convention conflict between “time-stretch factor” and “speed multiplier” is widespread across audio libraries and there is no universal standard. librosa.effects.time_stretch(y, rate) uses a parameter named rate where the documentation states “rate > 1 speeds up; rate < 1 slows down” — same convention as pytsmod’s s but with the opposite naming intuition (rate sounds like a speed rate, not a stretch factor). ffmpeg -filter:a "atempo=2.0" uses speed multiplier convention where atempo=2.0 means 2× faster. rubberband --tempo 2.0 also uses speed multiplier. pytsmod’s choice to use the mathematical stretch factor convention is correct for DSP purposes but conflicts with the user-facing convention developers encounter in production tools. Second, pytsmod raises no warning or error when s=2.0 produces a 32,000-sample output from a 16,000-sample input: both lengths are valid and the WSOLA algorithm runs correctly. The developer expected an 8,000-sample output but did not check output length before passing to the downstream ASR model. The downstream model (Whisper) accepts variable-length audio without error, processing 2-second clips just as it would process any input. The model’s output is a valid transcript — just degraded because Whisper’s temporal attention was not trained on heavily slow-spoken speech with exaggerated phoneme durations. Third, the word error rate increase from 0.08 to 0.31 is consistent with many plausible root causes: incorrect audio preprocessing (feature normalization, sample rate mismatch, pre-emphasis), Whisper model configuration (temperature, beam size, language detection), or speaker demographic mismatch. The 3-week investigation explored all these before the developer ran print(len(output), 'expected:', int(len(audio)/2)) and found the output 4× larger than expected. Southfield Pytsmod Ltd.’s retainer engagement was 2.0h: 0.25h triage (print output length vs expected length; 32000 vs 8000 confirms wrong direction; listen to output at half speed, not double); 0.5h root cause (s is stretch factor not speed; speed = 1/s; s=2.0 → output 2× longer → half speed; fix: s=0.5 for 2× speed-up); 0.75h fix (speed-to-stretch conversion utility; docstring with convention table; direction assertion; mean actual speed validation); 0.5h validation (WER restored to 0.08 on slow-speaker test set; output lengths match expected half-duration; pitch preserved by WSOLA; 2.0h total). Hours logged to HourTab with per-session breakdown.

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 →