Files
STSheets/tests/pipeline/drums.test.ts
T
2026-10-30 04:50:28 +01:00

58 lines
2.7 KiB
TypeScript

import { test, expect, beforeAll } from "bun:test";
import { join } from "node:path";
import { createSeparator } from "../../src/pipeline/separate.ts";
import { buildSong, loadSongDir } from "../../src/bench/dataset.ts";
import { transcribeDrums } from "../../src/pipeline/drums.ts";
import { matchNotes } from "../../src/eval/notes.ts";
import { DRUM_CLASSES } from "../../src/eval/score.ts";
import type { Audio, TimedNote } from "../../src/core/types.ts";
import { ticksToSeconds } from "../../src/core/time.ts";
const SLOW = 300_000;
const SEEDS = [1, 2, 3];
const CLASSES = ["kick", "snare", "hihat", "cymbal"] as const;
const classOf = (p: number) => CLASSES.find((c) => DRUM_CLASSES[c].includes(p));
const only = (notes: TimedNote[], c: string) => notes.filter((n) => classOf(n.pitch) === c).map((n) => ({ ...n, pitch: 0 }));
const songs: { ref: TimedNote[]; stem: Audio }[] = [];
beforeAll(async () => {
for (const seed of SEEDS) {
const d = await loadSongDir(await buildSong("t1b", seed));
const t = d.song.tracks.find((x) => x.role === "drums")!;
songs.push({ ref: t.notes.map((n) => ({ onset: ticksToSeconds(n.tick, d.song.bpm), offset: ticksToSeconds(n.tick + n.dur, d.song.bpm), pitch: n.pitch })), stem: d.stems.get(t.name)! });
}
}, SLOW);
function f1(c: string): number {
let tp = 0, fp = 0, fn = 0;
for (const s of songs) {
const m = matchNotes(only(s.ref, c), only(transcribeDrums(s.stem), c));
tp += m.tp; fp += m.fp; fn += m.fn;
}
const f = tp + fp + fn === 0 ? 0 : (2 * tp) / (2 * tp + fp + fn);
console.log(`heuristic drums oracle t1b seeds 1-3 ${c}: F1=${f.toFixed(3)} tp=${tp} fp=${fp} fn=${fn}`);
return f;
}
test("oracle drum stems: kick F1 > 0.7", () => { expect(f1("kick")).toBeGreaterThan(0.7); }, SLOW);
test("oracle drum stems: snare F1 > 0.6", () => { expect(f1("snare")).toBeGreaterThan(0.6); }, SLOW);
test("reports hihat and cymbal F1", () => { f1("hihat"); f1("cymbal"); }, SLOW);
test("silence yields no notes", () => {
expect(transcribeDrums({ sampleRate: 44100, channels: [new Float32Array(44100 * 3)] })).toEqual([]);
});
test("demucs drum stem of t1b/1 (report only)", async () => {
const model = Bun.file(join(import.meta.dir, "../../models/demucs/htdemucs_6s.onnx"));
if (!(await model.exists())) return;
const separate = await createSeparator(new Uint8Array(await model.arrayBuffer()), { intraOpNumThreads: 8 });
const d = await loadSongDir(await buildSong("t1b", 1));
const est = transcribeDrums((await separate(d.mix)).drums);
for (const c of CLASSES) {
const m = matchNotes(songs[0]!.ref.filter((n) => classOf(n.pitch) === c).map((n) => ({ ...n, pitch: 0 })), only(est, c));
console.log(`heuristic drums demucs t1b/1 ${c}: F1=${(m.f1 ?? 0).toFixed(3)} tp=${m.tp} fp=${m.fp} fn=${m.fn}`);
}
}, SLOW);