msaf developer on retainer: msaf.process() boundaries are float64 time values in seconds — developer calls boundaries.astype(int) expecting frame indices — 44.3 s boundary maps to index 44 not frame 1 908 — 5 000 tracks per month mis-segmented 43× too short — 3 weeks — msaf developer on monthly retainer
msaf 0.1.72 • Python 3.9 • librosa 0.9.2; msaf.process(audio_path, boundaries_id='scluster') returns boundaries as float64 seconds [0., 44.3, 92.7, 148.1, 192.0]; developer calls boundaries.astype(int) • Northfield Msaf Ltd. (DJ track auto-segmentation; 5,000 music tracks/month from 150 DJ software companies; fps=22050/512=43.07; .astype(int) gives [0, 44, 92, 148, 192] instead of frames [0, 1908, 3992, 6378, 8268]; DJ cue points appear at 1.02 s, 2.14 s, 3.44 s instead of 44.3 s, 92.7 s, 148.1 s; all segments ~1 second instead of ~44 seconds; 3 weeks; fix: np.round(boundaries * fps).astype(int); 2.0h retainer engagement)
Northfield Msaf Ltd. built a DJ track auto-segmentation microservice that used msaf 0.1.72 to automatically detect structural boundaries in music (intro, verse, chorus, bridge, outro) and return cue point timestamps to 150 DJ software integrations serving their platforms. Each track was processed via msaf.process(audio_path, boundaries_id='scluster', labels_id=None), which applied the Structural Clustering (scluster) algorithm to HPCP (Harmonic Pitch Class Profile) features extracted by librosa. The service was originally prototyped against a small test corpus of 10 tracks where the developer manually verified the returned boundaries looked like time values in seconds (e.g., 44.3, 92.7) and noted they “looked like frame numbers” — in a 192-second track analyzed at 43 fps, the total frame count is about 8,268, so a boundary at 44.3 seconds (frame 1,908) is not obviously “too small” to a developer who hasn’t read the msaf documentation carefully. The developer wrote segment_frames = boundaries.astype(int) and passed segment_frames to the downstream DJ database.
msaf.process() internally loads the audio file using librosa with default resampling to 22 050 Hz (sr=22050) and computes features with default hop_length=512 samples. The resulting frames-per-second rate is fps = sr / hop_length = 22050 / 512 = 43.0664 frames/second. The returned boundaries array is float64 time values in SECONDS: np.array([0., 44.3, 92.7, 148.1, 192.0], dtype=float64) for a 192-second track. The correct frame indices are np.round(boundaries * 43.07).astype(int) = [0, 1908, 3992, 6378, 8268]. The developer’s code ran boundaries.astype(int), which performs integer truncation on the float64 seconds values, producing [0, 44, 92, 148, 192]. These are all valid indices into the 8,268-frame feature matrix (they fall within bounds), so no IndexError or ValueError was raised. At 43.07 fps, frame 44 corresponds to t = 44 / 43.07 = 1.02 seconds; frame 92 to 2.14 seconds; frame 148 to 3.44 seconds; frame 192 to 4.46 seconds. The DJ software placed auto-cue points at 1.02 s, 2.14 s, 3.44 s, 4.46 s instead of 44.3 s, 92.7 s, 148.1 s, 192.0 s — all cue points fell within the first 5 seconds of every track. The auto-loop feature created loop regions spanning [0:44], [44:92], [92:148], [148:192] frames — each about 44–56 frames at 43.07 fps — approximately 1–1.3 seconds each (2–3 beats at 120 BPM). The correct segments were 1,908–2,386 frames each, approximately 44–55 seconds each (a full musical verse or chorus section). The mis-segmentation affected all 5,000 tracks per month. The service returned HTTP 200 responses with plausible-structured JSON (valid timestamps, valid segment count), so no alert fired. The issue ran for 3 weeks until a DJ customer submitted a support ticket: “the auto-loop feature always creates 2-beat loops on every track regardless of genre.”
The annotated buggy and fixed msaf segmentation pipeline, with seconds-to-frames conversion and boundary validation:
# Northfield Msaf Ltd. — msaf 0.1.72 seconds-to-frames conversion fix
# msaf 0.1.72; Python 3.9; librosa 0.9.2
# DJ track auto-segmentation; 5,000 music tracks/month from 150 DJ software companies
# Problem: msaf.process() returns boundaries as float64 SECONDS
# developer calls .astype(int) expecting frame indices
# boundary at 44.3s → index 44 (wrong) instead of frame 1908 (correct)
# at fps=43.07, frame 44 ≈ t=1.02s — entirely in the intro
# Fix: np.round(boundaries * fps).astype(int)
import logging
import numpy as np
import msaf
logger = logging.getLogger(__name__)
# msaf default internal feature extraction parameters
# (overridable via config= parameter — update fps if you change these)
MSAF_SR = 22050 # Hz — librosa resamples audio to this rate
MSAF_HOP_LENGTH = 512 # samples per frame
FPS = MSAF_SR / MSAF_HOP_LENGTH # = 43.0664 frames per second
# ── BUGGY pipeline ────────────────────────────────────────────────────────────
def segment_track_buggy(audio_path: str) -> list:
"""
BUG: treats float64 seconds as integer frame indices.
msaf.process() returns boundaries in SECONDS as float64.
.astype(int) truncates seconds to integers — NOT frame conversion.
At 43.07 fps, boundary at 44.3s should be frame 1908.
.astype(int) gives 44 — pointing to t=1.02s.
"""
boundaries, labels = msaf.process(
audio_path,
boundaries_id='scluster',
labels_id=None
)
# BUG: boundaries = [0., 44.3, 92.7, 148.1, 192.0] (seconds, float64)
# .astype(int) truncates to [0, 44, 92, 148, 192] — NOT frame indices
# these are valid array indices (< 8268 frames) — no IndexError
segment_frames = boundaries.astype(int) # WRONG
return segment_frames.tolist()
# ── FIXED pipeline ────────────────────────────────────────────────────────────
def segment_track_fixed(audio_path: str) -> list:
"""
FIX: convert seconds to frame indices using fps = sr / hop_length.
msaf default: sr=22050, hop_length=512 → fps=43.0664.
boundary at 44.3s → round(44.3 * 43.07) = round(1908.0) = 1908.
Use np.round() not .astype(int) — round-then-cast, not truncate-then-cast.
"""
boundaries, labels = msaf.process(
audio_path,
boundaries_id='scluster',
labels_id=None
)
# boundaries = [0., 44.3, 92.7, 148.1, 192.0] (seconds, float64)
# Correct frame conversion: multiply by fps, then round to nearest int
segment_frames = np.round(boundaries * FPS).astype(int)
# = [0, 1908, 3992, 6378, 8268] — correct frame indices
# Boundary validation
n_frames_estimate = int(boundaries[-1] * FPS) + 1
if not np.all(np.diff(segment_frames) > 0):
raise ValueError(
f"Boundary frames not strictly increasing: {segment_frames.tolist()}. "
"Check fps calculation and boundaries_id algorithm."
)
min_segment_frames = int(np.diff(segment_frames).min())
if min_segment_frames < 100:
logger.warning(
f"Shortest segment is {min_segment_frames} frames "
f"({min_segment_frames / FPS:.2f}s). "
"Expected at least 100 frames (2.3s). "
"Verify boundaries are in seconds, not already in frames."
)
return segment_frames.tolist()
def validate_segmentation(audio_path: str, expected_min_segment_sec: float = 10.0) -> None:
"""
Validates that segment boundaries look like structural boundaries
(verse/chorus length) not like per-frame or per-beat markers.
Raises RuntimeError if shortest segment is under expected_min_segment_sec.
"""
boundaries, _ = msaf.process(audio_path, boundaries_id='scluster', labels_id=None)
segment_frames = np.round(boundaries * FPS).astype(int)
segment_durations_sec = np.diff(segment_frames) / FPS
min_dur = float(segment_durations_sec.min())
if min_dur < expected_min_segment_sec:
raise RuntimeError(
f"Shortest segment {min_dur:.2f}s is below threshold {expected_min_segment_sec}s. "
f"boundaries (seconds) = {boundaries.tolist()}. "
f"segment_frames = {segment_frames.tolist()}. "
"If segment_frames match boundaries numerically (e.g., frame 44 ≈ 44.3), "
"likely cause: .astype(int) used instead of np.round(boundaries * fps).astype(int)."
)
logger.info(
f"Segmentation validated: {len(segment_frames) - 1} segments, "
f"min {min_dur:.1f}s, max {float(segment_durations_sec.max()):.1f}s. "
"Seconds-to-frames conversion verified."
)
How msaf’s seconds-domain output, the difference between .astype(int) truncation and seconds-to-frames conversion, and the absence of an out-of-bounds error combine to silently mis-segment 5,000 DJ tracks — and the correct frame conversion for production msaf deployments
The core issue is a distinction between two conceptually different operations that look identical in code: integer truncation and seconds-to-frames conversion. boundaries.astype(int) truncates each float64 seconds value to the nearest integer toward zero: np.float64(44.3) becomes np.int64(44). A seconds-to-frames conversion multiplies by the frame rate first: np.round(44.3 * 43.07).astype(int) = 1908. The two operations produce identical results only when the frame rate is exactly 1.0 (i.e., when one second equals exactly one frame) — which is never the case for audio feature extraction. The confusion arises because some libraries return frame indices from segmentation functions (e.g., librosa onset detection functions return sample indices), while msaf always returns absolute time in seconds. The error is invisible at runtime because the truncated integer values (44, 92, 148, 192) are all valid indices into the feature matrix (which has 8,268 frames) — they are just pointing to the wrong frames. The absence of an index-out-of-bounds exception is the key reason this ran for 3 weeks: the code produced valid integers, the downstream system stored them without schema validation, and the DJ software rendered them as cue points at plausible-looking timestamps (1s, 2s, 3s are all valid music timestamps in the first bar). The detection path is to compare the returned boundaries values to the frame index values: if boundaries[1] = 44.3 and segment_frames[1] = 44, they match to within rounding — this is the fingerprint of treating seconds as frame numbers. A correct conversion would give segment_frames[1] = 1908, which is 43× larger. The np.round() before .astype(int) matters: np.floor(44.3 * 43.07) gives 1907, while np.round(44.3 * 43.07) gives 1908; the difference is small but accumulates across multiple boundaries. If the pipeline uses a custom sr or hop_length via the msaf config parameter, fps must be recalculated: a pipeline at sr=44100 with hop_length=1024 runs at 44100/1024 = 43.07 fps (same rate), while sr=44100 with hop_length=512 runs at 86.13 fps — each change to these parameters requires a matching update to the fps constant. The retainer engagement was 2.0h: 0.5h triage (DJ complaint: 2-beat auto-loops; check segment_frames output: [0, 44, 92, 148, 192]; check boundaries: [0., 44.3, 92.7, 148.1, 192.0]; seconds-vs-frames confusion confirmed); 0.5h root cause (msaf returns float64 seconds; fps = 22050/512 = 43.07; .astype(int) truncates seconds to wrong integers); 0.5h fix (np.round(boundaries * fps).astype(int); boundary validation; minimum segment duration check); 0.5h validation (50-track test: cue points correct; loop regions verse-length; re-process 3-week backlog; 2.0h total). Hours logged to HourTab with per-session breakdown.
Related retainer posts
- librosa developer on retainer — librosa 0.9.2; Python 3.9; music analysis service; 40,000 tracks/month;
librosa.load(path)resamples to 22050 Hz by default; tempo detection off by 2×; fix:sr=None— 1.5h - kapre developer on retainer — kapre 0.3.7; Python 3.9; TF 2.8; music genre classification; 100,000 tracks/month;
load_model()raisesValueErrorforMelSpectrogram; handler uses untrained model; 97% “Electronic”; fix:custom_objects=kapre.utils.get_custom_objects()— 1.5h - openl3 developer on retainer — openl3 0.4.2; Python 3.9; audio similarity engine; 50,000 clips/month;
soundfile.read()returns int16;get_audio_embedding()receives [−32768, 32767]; mel spectrogram saturates; all embeddings cluster; cosine similarity 0.99; fix:dtype='float32'— 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 →