Files
unmix/tests/test_pipeline_m4.py

77 lines
2.7 KiB
Python

"""Integration test for Milestone 4 (Stage 6 Beat-Quantized Chord Recognition)."""
import json
from pathlib import Path
import numpy as np
import soundfile as sf
from songsplit.pipeline import PipelineContext
from songsplit.pipeline.s6_chords import ChordsStage
from songsplit.schema.chords import ChordsDocument
from songsplit.schema.meta import MetaDocument, SourceFileInfo
from songsplit.schema.tempo import TempoAnchor, TempoDocument
def test_m4_chords_stage_pipeline(tmp_path: Path):
song_dir = tmp_path / "song_pkg"
stems_dir = song_dir / "stems"
stems_dir.mkdir(parents=True, exist_ok=True)
# 1. Synthesize 4.0s instrumental audio:
# 2.0s C major (261.63, 329.63, 392.00) + 2.0s A minor (220.0, 261.63, 329.63)
sr = 22050
t_half = np.linspace(0, 2.0, int(sr * 2.0), endpoint=False)
c_major = (
np.sin(2 * np.pi * 261.63 * t_half) + np.sin(2 * np.pi * 329.63 * t_half) + np.sin(2 * np.pi * 392.00 * t_half)
).astype(np.float32)
a_minor = (
np.sin(2 * np.pi * 220.00 * t_half) + np.sin(2 * np.pi * 261.63 * t_half) + np.sin(2 * np.pi * 329.63 * t_half)
).astype(np.float32)
inst_audio = np.concatenate([c_major, a_minor])
inst_wav = stems_dir / "instrumental.wav"
sf.write(str(inst_wav), inst_audio, sr)
# 2. Prepare tempo.json: 120 BPM starting at 0.0s (0.5s per beat)
# 4.0s = 8 beats total (0.0 to 4.0 beat is C, 4.0 to 8.0 beat is Am)
tempo = TempoDocument(anchors=[TempoAnchor(sec=0.0, beat=0.0, bpm=120.0)])
(song_dir / "tempo.json").write_text(tempo.model_dump_json(indent=2))
# 3. Prepare meta.json
meta = MetaDocument(
title="Chord Test Song",
artist="Test Pianist",
duration_sec=4.0,
source_file=SourceFileInfo(path=str(inst_wav), sha256="fakehash", size_bytes=len(inst_audio)),
created_at="2026-11-02T10:00:00Z",
)
(song_dir / "meta.json").write_text(meta.model_dump_json(indent=2))
# 4. Run ChordsStage with template backend
ctx = PipelineContext(source_file=inst_wav, output_dir=song_dir)
stage = ChordsStage(backend="template")
stage.run(ctx)
chords_json = song_dir / "chords.json"
assert chords_json.exists()
with open(chords_json, encoding="utf-8") as f:
doc = ChordsDocument.model_validate(json.load(f))
assert len(doc.chords) >= 1
labels = [c.label for c in doc.chords]
assert "C" in labels or "Am" in labels
# Check beat-based timing
for ch in doc.chords:
assert ch.start_beat >= 0.0
assert ch.end_beat > ch.start_beat
assert ch.confidence > 0.0
# 5. Verify meta.json updated with key
updated_meta = ctx.load_or_init_meta()
assert updated_meta.key in ("C", "Am")