Files
konduktor/backend/test_grid_detect.py
T
2026-10-04 16:18:43 +02:00

215 lines
9.4 KiB
Python

"""Tests for beatgrid detection (`core/grid_detect.py`) on synthetic audio.
Needs no audio files and no library: every track here is generated, so its
tempo and first beat are KNOWN rather than taken from another analyser. That
is what these can pin that `bench_grid_detect.py` (real music, scored against
Rekordbox) cannot: exactness, and each trap that fooled a band in isolation —
the loud off-beat hat, the syncopated bassline, and the frame-quantised tempo
the previous detector could not escape.
"""
import warnings
import numpy as np
warnings.filterwarnings("ignore")
from konduktor.core.grid_detect import detect_grid # noqa: E402
SR = 22050
failed = False
def check(label, cond, detail=""):
global failed
print(f" [{'PASS' if cond else 'FAIL'}] {label}" + (f" — {detail}" if detail and not cond else ""))
if not cond:
failed = True
rng = np.random.default_rng(0)
def _faded(x: np.ndarray) -> np.ndarray:
"""End a sound smoothly: a hard cut is itself a click, half a beat after the
kick, which no real sample has and which would test the generator instead."""
n = min(len(x), int(0.02 * SR))
x = x.copy()
x[-n:] *= np.linspace(1.0, 0.0, n)
return x
def _kick() -> np.ndarray:
t = np.arange(int(0.25 * SR)) / SR
body = np.sin(2 * np.pi * (50 * t + 60 * (1 - np.exp(-t / 0.02)) * 0.02)) * np.exp(-t / 0.12)
click = np.zeros_like(t)
click[: int(0.002 * SR)] = rng.standard_normal(int(0.002 * SR)) * 0.5
return _faded(body + click)
def _hat(amp: float) -> np.ndarray:
n = int(0.04 * SR)
noise = np.diff(rng.standard_normal(n + 1)) # tilt up: hat-like
return _faded(amp * noise * np.exp(-np.arange(n) / SR / 0.012))
def _bass(amp: float) -> np.ndarray:
t = np.arange(int(0.2 * SR)) / SR
ramp = np.minimum(1.0, t / 0.015) # a bass note swells; it has no click
return _faded(amp * np.sin(2 * np.pi * 70 * t) * ramp * np.exp(-t / 0.15))
def track(bpm, first=0.3, seconds=180.0, hat=0.0, bass=0.0, tempo_b=None):
"""Kick on every beat from ``first``; optional off-beat hat and a bass note
60% of the way to the next beat. ``tempo_b`` is the tempo at the END: the
tempo glides linearly from ``bpm`` to it, as a live drummer's does."""
y = np.zeros(int(seconds * SR))
def put(x, at):
i = int(round(at * SR))
if 0 <= i < len(y):
j = min(len(y), i + len(x))
y[i:j] += x[: j - i]
t, beat = first, 60.0 / bpm
k, h, b = _kick(), _hat(hat), _bass(bass)
while t < seconds:
put(k, t)
if hat:
put(h, t + beat / 2)
if bass:
put(b, t + 0.6 * beat)
if tempo_b:
beat = 60.0 / (bpm + (tempo_b - bpm) * t / seconds)
t += beat
return (0.3 * y).astype(np.float32)
def detect(y, **kw):
return detect_grid("", y=y, sr=SR, **kw)
def beat_error_ms(res, bpm, first, at):
"""Distance from the true beat nearest ``at`` to the detected grid's."""
p = 60.0 / bpm
true = first + round((at - first) / p) * p
dp = 60.0 / res.bpm
got = res.anchor + round((true - res.anchor) / dp) * dp
return (got - true) * 1000.0
print("== round tempo: exact BPM, anchor on the first kick ==")
r = detect(track(124, first=0.3))
check("124 BPM detected as exactly 124", r.bpm == 124.0, f"got {r.bpm}")
check("anchor on the first kick (±3 ms)", abs(r.anchor - 0.3) <= 0.003, f"got {r.anchor}")
check("still on the beat at the end (±3 ms)", abs(beat_error_ms(r, 124, 0.3, 175)) <= 3)
check("a constant track reports little drift", r.drift_ms is not None and r.drift_ms <= 5, f"{r.drift_ms}")
print("== the old detector's blind spot: 125 BPM ==")
# librosa's frame-quantised tempo could only return 123.05 or 129.20 here.
r = detect(track(125, first=0.05))
check("125 BPM is 125, not 123.05", r.bpm == 125.0, f"got {r.bpm}")
print("== non-round tempo is fitted, not snapped ==")
r = detect(track(123.37, first=0.2))
check("123.37 within 0.005", abs(r.bpm - 123.37) <= 0.005, f"got {r.bpm}")
check("…and on the beat at the end (±5 ms)", abs(beat_error_ms(r, 123.37, 0.2, 175)) <= 5)
print("== octave: a four-on-the-floor kick is not half or double tempo ==")
r = detect(track(128))
check("128 BPM, not 64 or 256", r.bpm == 128.0, f"got {r.bpm}")
r = detect(track(90), bpm_range=(100.0, 200.0))
check("bpm_range folds 90 up to 180", r.bpm == 180.0, f"got {r.bpm}")
print("== phase traps: each fooled one band on real music ==")
r = detect(track(126, first=0.4, hat=1.5))
check("an off-beat hat louder than the kick does not take the beat",
abs(beat_error_ms(r, 126, 0.4, 60)) <= 3, f"{beat_error_ms(r, 126, 0.4, 60):+.1f} ms")
r = detect(track(124, first=0.25, bass=1.5))
check("a syncopated bassline does not take the beat",
abs(beat_error_ms(r, 124, 0.25, 60)) <= 3, f"{beat_error_ms(r, 124, 0.25, 60):+.1f} ms")
r = detect(track(124, first=0.25, hat=1.2, bass=1.2))
check("…nor both together", abs(beat_error_ms(r, 124, 0.25, 60)) <= 3,
f"{beat_error_ms(r, 124, 0.25, 60):+.1f} ms")
print("== a wandering tempo is reported, not hidden ==")
# 124 → 124.15 over three minutes: any one constant grid is ~25 ms off somewhere.
r = detect(track(124, tempo_b=124.15))
check("drift is reported when one grid cannot fit", r.drift_ms is not None and r.drift_ms >= 12, f"{r.drift_ms}")
print("== Grid alone: the BPM is held, only the phase is found ==")
r = detect(track(124, first=0.3), bpm=124.0)
check("the given BPM comes back untouched", r.bpm == 124.0, f"got {r.bpm}")
check("the anchor is found on the first kick (±3 ms)", abs(r.anchor - 0.3) <= 0.003, f"got {r.anchor}")
r = detect(track(123.37, first=0.2), bpm=123.37)
check("a non-round held BPM is not snapped to 123", r.bpm == 123.37, f"got {r.bpm}")
check("…and its grid is on the beat at the end (±5 ms)", abs(beat_error_ms(r, 123.37, 0.2, 175)) <= 5)
r = detect(track(124, first=0.4, hat=1.5, bass=1.2), bpm=124.0)
check("the phase traps still hold with a held BPM",
abs(beat_error_ms(r, 124, 0.4, 60)) <= 3, f"{beat_error_ms(r, 124, 0.4, 60):+.1f} ms")
r = detect(track(87, first=0.3), bpm=174.0)
check("a held double-time BPM is kept (the octave is the user's call)",
r.bpm == 174.0 and abs(beat_error_ms(r, 87, 0.3, 60)) <= 3, f"{r.bpm} {beat_error_ms(r, 87, 0.3, 60):+.1f}")
r = detect(track(124, first=0.3), bpm=124.2)
check("a WRONG held BPM is kept, and its drift reported", r.bpm == 124.2 and r.drift_ms >= 20, f"{r.drift_ms}")
print("== BPM alone: the tempo is found, the anchor is held ==")
r = detect(track(125, first=0.3), anchor=0.3 + 2 * 60 / 125) # a downbeat two beats in
check("the tempo is detected", r.bpm == 125.0, f"got {r.bpm}")
check("the given anchor comes back untouched", r.anchor == 0.3 + 2 * 60 / 125, f"got {r.anchor}")
check("an anchor on the beat reports little drift", r.drift_ms <= 5, f"{r.drift_ms}")
r = detect(track(125, first=0.3), anchor=0.33)
check("an anchor 30 ms off the beat is kept, and the drift says so",
r.anchor == 0.33 and 25 <= r.drift_ms <= 35, f"{r.anchor} {r.drift_ms}")
print("== holding both is refused ==")
try:
detect(track(124), bpm=124.0, anchor=0.3)
check("bpm + anchor raises ValueError", False)
except ValueError:
check("bpm + anchor raises ValueError", True)
print("== nothing to fit ==")
for label, y in (("silence", np.zeros(SR * 20, dtype=np.float32)), ("too short", np.zeros(SR, dtype=np.float32))):
try:
detect(y)
check(f"{label} raises ValueError", False, "returned a grid")
except ValueError:
check(f"{label} raises ValueError", True)
print("== the BPM / Grid plan (core/grid_plan) ==")
from types import SimpleNamespace as M # noqa: E402
from konduktor.core.grid_plan import plan_grid # noqa: E402
one = [M(start=0.5, bpm=124.0)]
two = [M(start=60.0, bpm=126.0), M(start=0.5, bpm=124.0)] # unsorted on purpose
def plan(bpm, grid, markers=(), track_bpm=None, locked=False, replace=False):
return plan_grid(bpm=bpm, grid=grid, markers=list(markers), track_bpm=track_bpm,
locked=locked, replace_existing=replace)
check("nothing ticked does nothing", plan(False, False, one).action == "none")
check("locked is skipped whatever is ticked",
all(plan(b, g, one, locked=True).reason == "locked" for b, g in ((1, 1), (1, 0), (0, 1))))
check("BPM+Grid on an ungridded track: full", plan(True, True).action == "full")
check("BPM+Grid on a gridded track, no Replace: skipped as existing",
plan(True, True, one).reason == "existing")
check("BPM+Grid with Replace replaces even a flexible grid", plan(True, True, two, replace=True).action == "full")
s_ = plan(True, False, one)
check("BPM alone keeps the anchor", s_.action == "bpm" and s_.hold_anchor == 0.5)
check("BPM alone ignores Replace (adjusting IS the point)", plan(True, False, one, replace=False).action == "bpm")
check("BPM alone, no grid: analysed in full", plan(True, False, track_bpm=124).action == "full")
s_ = plan(False, True, one, track_bpm=999)
check("Grid alone keeps the MARKER's BPM", s_.action == "phase" and s_.hold_bpm == 124.0)
s_ = plan(False, True, track_bpm=128.0)
check("Grid alone, no grid but a BPM: keeps that BPM", s_.action == "phase" and s_.hold_bpm == 128.0)
check("Grid alone, no BPM at all: analysed in full", plan(False, True).action == "full")
check("one tick on a flexible grid: skipped, never flattened",
plan(True, False, two).reason == "flexible" and plan(False, True, two).reason == "flexible")
print()
print("FAILED" if failed else "OK")
raise SystemExit(1 if failed else 0)