"""Tests for key detection (`core/key_detect.py`) on synthetic audio. Needs no audio files: every track is generated, so its key is KNOWN. These pin what `bench_key_detect.py` (real music) cannot: that all 24 keys come out — a wrong label order or wheel mapping would get exactly some of them wrong — that loudness and sample rate do not change the answer, that silence gives no key rather than a made-up one, and that the numpy network computes what was trained (the convolution against a direct reference implementation). The progressions are cadences that state their key unambiguously (I-IV-V-I; i-iv-V-i with the raised leading tone). A pop I-V-vi-IV is deliberately NOT used: it spends a quarter of its time on the relative minor, and a model trained on dance music — 85% minor — fairly calls some of those minor. """ import warnings import numpy as np warnings.filterwarnings("ignore") from konduktor.core import key_detect # 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 def tone(midi: int, dur: float, amp: float, harmonics: int = 6, sr: int = SR) -> np.ndarray: t = np.arange(int(dur * sr)) / sr f = 440.0 * 2 ** ((midi - 69) / 12) x = sum(np.sin(2 * np.pi * f * h * t) / h for h in range(1, harmonics + 1) if f * h < sr / 2) env = np.minimum(1, t / 0.01) * np.minimum(1, (dur - t) / 0.05) return amp * x * env MAJOR = [(0, "M"), (5, "M"), (7, "M"), (0, "M")] # I IV V I MINOR = [(0, "m"), (5, "m"), (7, "M"), (0, "m")] # i iv V i def song(tonic: int, mode: str, bars: int = 16, bpm: float = 124.0, sr: int = SR) -> np.ndarray: beat = 60.0 / bpm prog = MAJOR if mode == "major" else MINOR out = [] for b in range(bars): root, q = prog[b % len(prog)] r = 48 + (tonic + root) % 12 chord = sum(tone(r + 12 + i, 4 * beat, 0.12, sr=sr) for i in (0, 4 if q == "M" else 3, 7)) bass = np.concatenate([tone(r - 12, beat, 0.3, 3, sr=sr) for _ in range(4)]) n = min(len(chord), len(bass)) out.append(chord[:n] + bass[:n]) y = np.concatenate(out) return (y / np.abs(y).max() * 0.8).astype(np.float32) def name(pc: int, mode: str) -> str: return key_detect.PITCH_NAMES[pc] + ("m" if mode == "minor" else "") print("all 24 keys") for mode in ("major", "minor"): for pc in range(12): r = key_detect.detect_key_samples(song(pc, mode), SR) check(f"{name(pc, mode):4s} -> {r.name if r else None}", r is not None and (r.pitch_class, r.mode) == (pc, mode)) if r: check(f"{name(pc, mode):4s} wheel {r.wheel}", r.wheel == key_detect.camelot_wheel(pc, mode)) print("the Camelot mapping") check("C major = 8B", key_detect.camelot_wheel(0, "major") == 8) check("A minor = 8A", key_detect.camelot_wheel(9, "minor") == 8) check("G major = 9B (a fifth up = one step)", key_detect.camelot_wheel(7, "major") == 9) check("F minor = 4A", key_detect.camelot_wheel(5, "minor") == 4) print("invariance") y = song(2, "minor") base = key_detect.detect_key_samples(y, SR) quiet = key_detect.detect_key_samples(y * 0.05, SR) check("a quiet master gives the same key", quiet and quiet.name == base.name) check("...and the same confidence", quiet and abs(quiet.confidence - base.confidence) < 0.02, f"{base.confidence:.3f} vs {quiet and quiet.confidence:.3f}") hi = key_detect.detect_key_samples(song(2, "minor", sr=44100), 44100) check("44.1 kHz input gives the same key", hi and hi.name == base.name) check("confidence is a probability", 0.0 < base.confidence <= 1.0) print("nothing to say") check("silence -> None", key_detect.detect_key_samples(np.zeros(SR * 30, np.float32), SR) is None) check("under 5 s -> None", key_detect.detect_key_samples(song(0, "major")[: SR * 3], SR) is None) print("the numpy network") rng = np.random.default_rng(1) x = rng.standard_normal((3, 20, 300)).astype(np.float32) # > one time chunk, odd sizes w = rng.standard_normal((4, 3, 5, 5)).astype(np.float32) b = rng.standard_normal(4).astype(np.float32) got = key_detect._conv5(x, w, b) xp = np.pad(x, ((0, 0), (2, 2), (2, 2))) ref = np.zeros_like(got) for o in range(4): for i in range(3): for di in range(5): for dj in range(5): ref[o] += w[o, i, di, dj] * xp[i, di:di + 20, dj:dj + 300] ref[o] += b[o] check("5x5 'same' conv == direct cross-correlation", np.allclose(got, ref, atol=1e-3), f"max diff {np.abs(got - ref).max():.2e}") members = key_detect._members() check("the shipped model is an ensemble of >= 1 network", len(members) >= 1) spec = key_detect.features(y, SR) check("24 logits", key_detect.logits(spec).shape == (24,)) print("end to end: the Analyze route with the REAL decode and detectors") # Every other route test fakes the detectors. A route can still be broken # around working helpers (Auto Hotcues' once 500'd for a week), so one real # run: a generated F minor track at 124 BPM, written to a WAV, analysed for # BPM + Grid + Key through `/api/tracks/analyze` on a temp copy of the collection. import os # noqa: E402 import shutil # noqa: E402 import tempfile # noqa: E402 import time # noqa: E402 from pathlib import Path # noqa: E402 import soundfile # noqa: E402 REAL = Path(__file__).resolve().parents[1] / "collection.nml" with tempfile.TemporaryDirectory() as d: os.environ["KONDUKTOR_DATA_DIR"] = str(Path(d) / "appdata") work = Path(d) / "collection.nml" shutil.copy2(REAL, work) os.environ["KONDUKTOR_NML"] = str(work) music = song(5, "minor", bars=24, bpm=124.0) kick = np.zeros_like(music) beat = int(round(SR * 60 / 124.0)) t = np.arange(int(0.12 * SR)) / SR hit = (np.sin(2 * np.pi * 55 * t) * np.exp(-t / 0.05)).astype(np.float32) for i in range(0, len(kick) - len(hit), beat): kick[i:i + len(hit)] += hit wav = Path(d) / "f-minor.wav" soundfile.write(wav, 0.6 * music + 0.4 * kick, SR) from fastapi.testclient import TestClient import konduktor.main as main with TestClient(main.app) as c: a = main.require_adapter() tid = next(t.id for t in a.tracks if t.grid_marker_count == 0 and t.key_wheel is None) a.audio_path = lambda track_id: wav r = c.post("/api/tracks/analyze", json={"track_ids": [tid], "bpm": True, "grid": True, "key": True}) job = r.json() while job["state"] == "running": time.sleep(0.05) job = c.get(f"/api/jobs/{job['id']}").json() res = job.get("result") or {} check("the run finishes without failures", job["state"] == "done" and not res.get("failed"), str(job)[:300]) track = a.track(tid) check("the key is F minor (4A), written in the collection's Open Key (9m)", (track.key_wheel, track.key_mode, track.key) == (4, "minor", "9m"), f"{track.key} {track.key_wheel} {track.key_mode}") g = a.track_cues(tid).grid_markers check("the grid is 124 BPM", len(g) == 1 and abs(g[0].bpm - 124.0) < 0.05, str(g)) print("\nFAILED" if failed else "\nall key-detection tests passed") raise SystemExit(1 if failed else 0)