pyroomacoustics developer on retainer: room.rir indexed as rir[source_idx][mic_idx] — developer iterates room.rir[mic_idx][0] — IndexError on 7 of 8 microphones — broad except returns same RIR for all channels — 10 000 utterances per month with no spatial diversity — 3 weeks — pyroomacoustics developer on monthly retainer

pyroomacoustics 0.7.3 • Python 3.9 • scipy 1.7.3; room.rir[source_idx][mic_idx] is the documented convention; developer writes room.rir[mic_idx][0]; room.rir[1][0] raises IndexError (1 source, index 0 only); • Southfield Pyroomacoustics Ltd. (8-channel ASR augmentation; 10,000 utterances/month; all 8 channels use room.rir[0][0]; no inter-channel spatial variation; beamforming WER 31% not 18%; 3 weeks; fix: room.rir[0][mic_idx]; 2.0h retainer engagement)

Southfield Pyroomacoustics Ltd. built a speech recognition training data augmentation pipeline that simulates reverberant 8-channel microphone array recordings of clean utterances. The pipeline used pyroomacoustics 0.7.3 to construct randomised ShoeBox rooms for each training batch: room dimensions, absorption materials, source position, and microphone array orientation were drawn from configurable distributions. After calling room.simulate(), the pipeline extracted the per-channel Room Impulse Responses (RIRs) and convolved them with clean utterance audio to produce 8-channel reverberant simulations for a beamforming-aware ASR model. The developer wrote the RIR extraction loop as for mic_idx in range(n_mics): rir = room.rir[mic_idx][0], interpreting the outer list index as the microphone selector and the inner index as the source selector — the inverse of the library’s actual convention. The beamforming model trained on 10,000 augmented utterances per month and was evaluated after 3 weeks on a held-out reverberant 8-channel test set recorded in a real meeting room. Its word error rate (WER) was 31%, compared to the expected 18% established by a reference implementation using correct RIR indexing.

pyroomacoustics room.rir is a Python list of lists indexed as room.rir[source_idx][mic_idx]: the outer list contains one element per source added with room.add_source(), and each inner list contains one RIR array per microphone added with room.add_microphone_array(). With n_sources=1 and n_mics=8, the valid accesses are room.rir[0][mic_idx] for mic_idx in range(8). The developer’s loop for mic_idx in range(8): rir = room.rir[mic_idx][0] accesses room.rir[0][0] correctly on the first iteration (mic_idx=0), but raises IndexError: list index out of range on room.rir[1] for mic_idx=1 through mic_idx=7, because the outer list has only one element (source index 0). The developer had added a try / except IndexError around the loop body after encountering an earlier test failure with a different room configuration, writing except IndexError: rir = room.rir[0][0] as a fallback. With this handler, the loop assigns the correct RIR for channel 0 and assigns room.rir[0][0] — the response from source 0 to microphone 0 — for all seven remaining channels. The augmentation pipeline wrote eight identical waveforms into the per-channel slots of the output tensor and returned it to the training DataLoader as an 8-channel utterance.

The annotated buggy and fixed pyroomacoustics RIR extraction pipeline, with per-channel shape validation:

# Southfield Pyroomacoustics Ltd. — pyroomacoustics 0.7.3 room.rir indexing fix
# pyroomacoustics 0.7.3; Python 3.9; scipy 1.7.3
# 8-channel ASR training augmentation; 10,000 utterances/month
#
# Problem: room.rir is indexed as rir[source_idx][mic_idx]
#          developer writes room.rir[mic_idx][0] — outer/inner indices reversed
#          room.rir[1][0] raises IndexError (n_sources=1, only room.rir[0] exists)
#          broad except IndexError: rir = room.rir[0][0] for channels 1-7
#          all 8 channels receive identical RIR → no spatial diversity
#          beamforming WER 31% not 18%
# Fix:     room.rir[source_idx][mic_idx] — rir[0][mic_idx] for n_sources=1

import numpy as np
import pyroomacoustics as pra
import scipy.signal

FS = 16000
ROOM_DIM = [5.0, 4.0, 2.7]  # metres
SOURCE_POS = [2.0, 3.0, 1.5]
MIC_CENTER = np.array([2.5, 2.0, 1.2])
MIC_RADIUS = 0.05  # 5 cm circular array
N_MICS = 8

# Build 8-element circular microphone array
angles = np.linspace(0, 2 * np.pi, N_MICS, endpoint=False)
mic_positions = np.column_stack([
    MIC_CENTER[0] + MIC_RADIUS * np.cos(angles),
    MIC_CENTER[1] + MIC_RADIUS * np.sin(angles),
    np.full(N_MICS, MIC_CENTER[2]),
])  # shape (N_MICS, 3)


# ── BUGGY RIR extraction ───────────────────────────────────────────────────────

def extract_rirs_buggy(audio: np.ndarray) -> np.ndarray:
    """Return 8-channel reverberant audio — BUGGY: room.rir[mic_idx][0]."""
    materials = pra.Material(0.1)
    room = pra.ShoeBox(ROOM_DIM, fs=FS, materials=materials, max_order=12)
    room.add_source(SOURCE_POS, signal=audio)
    room.add_microphone_array(mic_positions.T)  # pra expects (3, N_MICS)
    room.simulate()

    output = np.zeros((N_MICS, len(audio)), dtype=np.float32)
    for mic_idx in range(N_MICS):
        try:
            rir = room.rir[mic_idx][0]  # BUG: outer index = source, not mic
            # mic_idx=0: room.rir[0][0] — accidentally correct
            # mic_idx=1: room.rir[1] → IndexError (n_sources=1)
            # mic_idx=2-7: same IndexError
        except IndexError:
            rir = room.rir[0][0]  # BUG: fallback to mic-0 RIR for all 7 channels
        reverberant = scipy.signal.fftconvolve(audio, rir)
        output[mic_idx] = reverberant[:len(audio)]

    # NOTE: output[0] is correct; output[1]-output[7] = output[0]
    # Inter-channel cross-correlation: 0.999 for all pairs
    return output


# ── FIXED RIR extraction ────────────────────────────────────────────────────────

def extract_rirs_fixed(audio: np.ndarray) -> np.ndarray:
    """Return 8-channel reverberant audio — FIXED: room.rir[source_idx][mic_idx]."""
    materials = pra.Material(0.1)
    room = pra.ShoeBox(ROOM_DIM, fs=FS, materials=materials, max_order=12)
    room.add_source(SOURCE_POS, signal=audio)
    room.add_microphone_array(mic_positions.T)
    room.simulate()

    # FIX: validate indexing dimensions before extraction
    n_sources = len(room.rir)
    n_mics_actual = len(room.rir[0])
    assert n_sources == 1, f"Expected 1 source, got {n_sources}"
    assert n_mics_actual == N_MICS, (
        f"Expected {N_MICS} mics in room.rir[0], got {n_mics_actual}. "
        "room.rir is indexed as rir[source_idx][mic_idx], not rir[mic_idx][source_idx]."
    )

    output = np.zeros((N_MICS, len(audio)), dtype=np.float32)
    source_idx = 0
    for mic_idx in range(N_MICS):
        rir = room.rir[source_idx][mic_idx]  # FIX: source first, mic second
        reverberant = scipy.signal.fftconvolve(audio, rir)
        output[mic_idx] = reverberant[:len(audio)]

    # Verify spatial diversity: adjacent mics should have cross-correlation < 0.98
    cc_01 = np.max(scipy.signal.correlate(output[0], output[1], mode='full'))
    cc_norm = cc_01 / (np.linalg.norm(output[0]) * np.linalg.norm(output[1]) + 1e-9)
    assert cc_norm < 0.98, (
        f"Inter-channel cross-correlation {cc_norm:.3f} >= 0.98. "
        "Spatial diversity has collapsed — check RIR indexing or room geometry."
    )

    return output

How reversed room.rir indexing, an IndexError fallback, and identical per-channel RIRs combine to silently destroy 3 weeks of beamforming training data — and the correct pyroomacoustics pattern for multi-microphone array augmentation

The root cause is a mismatch between the developer’s mental model of room.rir (outer = mic, inner = source) and the library’s actual convention (outer = source, inner = mic). The convention is documented in the pyroomacoustics API reference: “room.rir[i][j] is the RIR between the i-th source and the j-th microphone.” The confusion is natural for developers coming from other array-processing libraries where the microphone axis is the leading dimension (e.g., scipy microphone array processing, or beamforming libraries where arrays are (n_mics, n_samples)). The except IndexError handler was added after a different bug — a room configured with zero sources — raised a similar error, so the developer generalised the handler as a defensive measure. For the single-source, 8-microphone case, the handler fires silently on seven of eight iterations, returning identical audio for all eight channels without logging a warning. The beamforming model receives a training batch where all eight channels are copies of the channel-0 signal: inter-channel time differences (ITDs) and level differences (ILDs) are exactly zero across all pairs. The model trains a spatial filter with zero inter-channel information, learning to weight all channels equally regardless of source direction. During evaluation on real 8-channel recordings with genuine inter-channel cues, the model applies a uniform beam pattern and achieves no directional gain, producing WER 31% versus the expected 18% that a correctly-trained beamformer achieves on the same test set. The fix is a one-line change to the loop variable: room.rir[source_idx][mic_idx] instead of room.rir[mic_idx][source_idx], plus a shape assertion on len(room.rir) and len(room.rir[0]) that would have caught the error immediately at any test. The inter-channel cross-correlation assertion is an inexpensive diagnostic that detects spatial collapse before it reaches the training loop. The 3-week augmented corpus was regenerated overnight; after 5 additional training epochs on the corrected data, the beamforming WER on the held-out reverberant test set reached 19%. The retainer engagement was 2.0h: 0.5h triage (per-channel spectrogram logging; channels 1–7 visually identical to channel 0; isolate to RIR extraction loop); 0.5h root cause (room.rir[source][mic] convention; room.rir[mic_idx][0] raises IndexError for mic_idx ≥ n_sources=1; except returns rir[0][0] for all); 0.5h fix (room.rir[0][mic_idx]; add indexing assertion; remove except; re-generate 3-week corpus); 0.5h validation (WER 31% → 19%; inter-channel cross-correlation 0.999 → 0.42; 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 →