pygsound developer on retainer: ctx.sampleRate=44100 for a 16 kHz speech pipeline — IR sample rate mismatch stretches T60 by 2.76× — AEC model trained on 1.1-second reverb deployed to 0.4-second offices — pygsound developer on monthly retainer

pygsound 0.1.5 • Python 3.9; ctx.sampleRate=44100 (audio production standard); speech AEC pipeline at 16 000 Hz; pygsound IR passed directly to scipy.signal.fftconvolve without resampling; IR sample rate 44100 Hz ÷ speech sample rate 16000 Hz = 2.756× time stretch; design T60 = 0.4 s appears as 1.1 s in convolved output; AEC filter tuned for 0.4 s (6400 taps) cannot suppress 1.1 s echo tail (17600 taps); ERLE 8.3 dB (target 25 dB); 5 months; fix: librosa.resample(ir, orig_sr=44100, target_sr=16000) • Southfield PyGSound Ltd. (video conferencing AEC; 50,000 training pairs; 5 months; 2.5h)

Southfield PyGSound Ltd. was building an acoustic echo cancellation (AEC) model for a video conferencing system. The pipeline used pygsound to generate synthetic room impulse responses via geometry-based ray tracing; these IRs were then convolved with clean speech to produce reverberant training examples used to train a deep-learning AEC suppressor. The audio production team at Southfield had long standardised on 44.1 kHz for all audio work, and the pygsound context was configured with ctx.sampleRate = 44100 from the team’s standard project template. The speech model, however — a standard acoustic echo cancellation network based on a time-domain convolutional encoder — operated at 16000 Hz, the universal sample rate for telephony and speech-focused models. The data generation script loaded speech utterances at 16 kHz with librosa.load(path, sr=16000) and convolved them with the pygsound IR directly:

# Southfield PyGSound Ltd. — pygsound 0.1.5 sample rate fix
# pygsound 0.1.5; Python 3.9; PyTorch 1.12
# 50,000 AEC training pairs for video conferencing system
# Target room T60: 0.4 s (open-plan office)
#
# Problem: ctx.sampleRate=44100; speech pipeline at 16000 Hz
#          IR (44100 Hz) convolved with speech (16000 Hz) without resampling
#          fftconvolve interprets both as sample arrays — no sample-rate awareness
#          apparent T60 = 0.4 × (44100/16000) = 1.1 s (2.756× stretch)
#          AEC model trains on 1.1 s echo; deployed to 0.4 s real offices
# Fix:     resample IR from ctx.sampleRate to speech_fs before convolution
#          OR set ctx.sampleRate = speech_fs at context creation

import pygsound as ps
import numpy as np
import librosa
from scipy.signal import fftconvolve


SPEECH_FS = 16000   # sample rate for all downstream models


def build_context_buggy():
    """BUGGY: ctx.sampleRate=44100 mismatches speech pipeline (16000 Hz)."""
    ctx = ps.Context()
    ctx.diffuse_count = 20000
    ctx.specular_count = 2000
    ctx.channel_type = ps.ChannelLayoutType.mono
    ctx.sampleRate = 44100   # BUG: audio production standard, not speech model rate
    return ctx


def build_context():
    """FIXED: ctx.sampleRate matches downstream speech pipeline."""
    ctx = ps.Context()
    ctx.diffuse_count = 20000
    ctx.specular_count = 2000
    ctx.channel_type = ps.ChannelLayoutType.mono
    ctx.sampleRate = SPEECH_FS   # 16000 Hz — must match convolution partner
    return ctx


def compute_ir(scene, source, listener, ctx):
    """Compute IR and return as numpy float32 array at ctx.sampleRate Hz."""
    result = scene.computeInterpolatedIR(source, listener, ctx)
    ir = result.getChannelIR(0, 0, ctx)   # shape: (n_samples,) at ctx.sampleRate
    return ir.astype(np.float32)


def apply_ir_buggy(speech_16k, scene, source, listener):
    """BUGGY: convolves 16 kHz speech with 44100 Hz IR — 2.756× time stretch."""
    ctx = build_context_buggy()   # ctx.sampleRate=44100
    ir_44k = compute_ir(scene, source, listener, ctx)   # IR at 44100 Hz
    # BUG: fftconvolve treats both arrays as sample sequences
    # 17640-sample IR at 16000 Hz = 1.1 s (not 0.4 s)
    reverb = fftconvolve(speech_16k, ir_44k)
    return reverb


def apply_ir(speech_16k, scene, source, listener):
    """FIXED option 1: generate IR at speech sample rate (ctx.sampleRate=16000)."""
    ctx = build_context()   # ctx.sampleRate=16000
    ir_16k = compute_ir(scene, source, listener, ctx)   # IR at 16000 Hz
    return fftconvolve(speech_16k, ir_16k)   # both at 16000 Hz — correct


def apply_ir_resample(speech_16k, scene, source, listener):
    """FIXED option 2: generate IR at 44100 Hz, resample to 16000 Hz before convolution.
    Use when ctx.sampleRate cannot be changed (e.g., shared context with 44100 Hz pipeline).
    """
    ctx = build_context_buggy()   # ctx.sampleRate=44100 (existing production context)
    ir_44k = compute_ir(scene, source, listener, ctx)
    ir_16k = librosa.resample(ir_44k, orig_sr=44100, target_sr=SPEECH_FS)
    return fftconvolve(speech_16k, ir_16k)   # both at 16000 Hz — correct


def schroeder_t60(ir, fs):
    """Measure Schroeder T60 as a quality gate on computed IRs."""
    energy = ir ** 2
    backward_energy = np.cumsum(energy[::-1])[::-1]
    with np.errstate(divide='ignore'):
        backward_db = 10 * np.log10(backward_energy / (backward_energy[0] + 1e-20))
    t = np.arange(len(ir)) / fs
    idx = np.searchsorted(-backward_db, 60)
    if idx >= len(t):
        return float('inf')
    return float(t[idx])


def assert_ir_sample_rate(ir, ctx_sample_rate, speech_fs, tol=0.01):
    """Safety check: raise if context sample rate does not match speech sample rate."""
    if abs(ctx_sample_rate - speech_fs) / speech_fs > tol:
        raise ValueError(
            f"ctx.sampleRate={ctx_sample_rate} Hz does not match speech pipeline "
            f"sample rate {speech_fs} Hz (ratio {ctx_sample_rate/speech_fs:.4f}×). "
            f"The IR's temporal support will be stretched by this factor when "
            f"convolved with speech. Set ctx.sampleRate={speech_fs} or resample "
            f"the IR with librosa.resample(ir, orig_sr={ctx_sample_rate}, "
            f"target_sr={speech_fs}) before convolution."
        )

The core error: scipy.signal.fftconvolve multiplies DFT coefficients and returns an array of length N+K−1 samples. It has no knowledge of physical sample rates. It does not know that the speech array represents audio at 16 kHz or that the IR array was computed at 44.1 kHz. The temporal support of the convolved echo tail is determined by K/fs where K is the IR sample count and fs is the sample rate of the signals that will be used in the downstream model. For a room designed with T60 = 0.4 s at 44.1 kHz: meaningful IR content spans approximately 0.4 × 44100 = 17,640 samples. When this 17,640-sample array is interpreted as an IR in a 16 kHz system (which is what happens when you convolve it with 16 kHz speech), its temporal support is 17,640 ÷ 16,000 = 1.1025 seconds. The stretch factor is exactly 44,100 ÷ 16,000 = 2.75625. Every acoustic property of the IR is scaled by this factor: the apparent T60 becomes 0.4 × 2.756 = 1.1 s; the first reflection’s apparent delay is 2.756× longer than designed; the apparent room dimensions scale by √2.756 ≈ 1.66 (since time delay is proportional to distance/c). The AEC model is trained on 50,000 source-receiver pairs all exhibiting 1.1 s reverb in office rooms designed for 0.4 s. At deployment in real offices (T60 = 0.3–0.5 s), the model’s echo-suppression head — calibrated to cancel 1.1 s of echo — generates an anti-echo signal that continues 0.7 s past the end of the actual echo, introducing residual anti-echo artefacts that degrade call quality even after the echo has decayed. The 6400-tap filter trained for the apparent 1.1 s echo covered only 0.4 s of the real echo in the first deployment, and the remaining 0.7 s of anti-echo degraded ERLE. Five months of model-architecture experiments — larger filters, RLS instead of NLMS, fine-tuning on real recordings — were able to push ERLE from 8.3 dB to 11.4 dB, but could not reach the 25 dB target because the fundamental training data was mismatched in time scale.

Southfield PyGSound Ltd.’s retainer engagement was 2.5h: 0.5h triage (request one generated training pair from the corpus; play back the reverberant speech and notice the echo tail sounds like a large conference hall, not an office; measure Schroeder T60 on the convolved output: 1.08–1.12 s across 20 samples; the design was 0.4 s; compute 1.10/0.40 = 2.75 ≈ 44100/16000 = 2.756 — exact match; sample rate mismatch confirmed as root cause); 0.5h root cause (inspect data generation script: ctx.sampleRate=44100; downstream fftconvolve(speech_16k, ir_44k); IR not resampled; standard audio-team template left ctx.sampleRate at 44100 when speech model target was 16 kHz; no assertion or comment about sample rate alignment); 0.5h fix (add assert ctx.sampleRate == speech_fs at context creation; change ctx.sampleRate = 44100 to ctx.sampleRate = 16000; Schroeder T60 on new IRs: 0.39–0.41 s — matches 0.4 s design; regenerate 50,000 pairs at 16 kHz); 1.0h validation (5-epoch warm-start retrain on corrected corpus: ERLE 18.2 dB; projected full retrain ERLE 24–26 dB; add assert_ir_sample_rate() check to generation function; add T60 quality gate on 10 IRs per batch; commit; 2.5h 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 →