"""End-to-end integration test for Milestone 2 (Lyrics, Pitch, UltraStar export, and Eval runner).""" import json from pathlib import Path import numpy as np import soundfile as sf from songsplit.eval.metrics import evaluate_vocals from songsplit.export.ultrastar import export_ultrastar, parse_ultrastar from songsplit.pipeline import PipelineContext from songsplit.pipeline.s4_pitch import PitchStage from songsplit.schema.meta import MetaDocument, SourceFileInfo from songsplit.schema.vocals import VocalsDocument def test_m2_pitch_ultrastar_and_eval(tmp_path: Path): song_dir = tmp_path / "song_pkg" stems_dir = song_dir / "stems" cache_dir = song_dir / ".cache" stems_dir.mkdir(parents=True, exist_ok=True) cache_dir.mkdir(parents=True, exist_ok=True) # 1. Create a 2.0s 440 Hz (A4 = MIDI 69) test vocal stem sr = 16000 t = np.linspace(0, 2.0, int(sr * 2.0), endpoint=False) sine_wave = 0.5 * np.sin(2 * np.pi * 440.0 * t).astype(np.float32) vocals_wav = stems_dir / "vocals.wav" sf.write(str(vocals_wav), sine_wave, sr) # 2. Write mock lyrics in .cache/lyrics_raw.json (two syllables) lyrics_data = { "language": "de", "lines": [ { "start": 0.2, "end": 1.8, "text": "Hal-lo ", "syllables": [ {"text": "Hal-", "start": 0.2, "end": 0.9}, {"text": "lo ", "start": 0.9, "end": 1.8}, ], } ], } (cache_dir / "lyrics_raw.json").write_text(json.dumps(lyrics_data, indent=2)) # 3. Write meta.json meta = MetaDocument( title="Integration Song", artist="Test Singer", language="de", duration_sec=2.0, source_file=SourceFileInfo(path=str(vocals_wav), sha256="fakehash", size_bytes=len(sine_wave)), created_at="2026-11-02T10:00:00Z", ) (song_dir / "meta.json").write_text(meta.model_dump_json(indent=2)) # 4. Run PitchStage (CREPE) with crepe_model="tiny" for fast test execution ctx = PipelineContext( source_file=vocals_wav, output_dir=song_dir, quality="fast", # uses tiny crepe model ) pitch_stage = PitchStage() pitch_stage.run(ctx) vocals_json = song_dir / "vocals.json" assert vocals_json.exists() with open(vocals_json, encoding="utf-8") as f: doc = VocalsDocument.model_validate(json.load(f)) assert len(doc.lines) == 1 sylls = doc.lines[0].syllables assert len(sylls) == 2 # Sine wave is at 440 Hz -> MIDI 69.0 assert 68.0 <= sylls[0].midi <= 70.0 assert 68.0 <= sylls[1].midi <= 70.0 assert sylls[0].confidence > 0.5 # 5. Export UltraStar song.txt txt_path = export_ultrastar(song_dir) assert txt_path.exists() song = parse_ultrastar(txt_path) assert song.title == "Integration Song" assert len(song.lines) == 1 assert len(song.lines[0]) == 2 # Pitch relative to C4 (60) -> 69 - 60 = 9 assert song.lines[0][0].pitch == 9 # 6. Evaluate predicted vocals against ground truth UltraStar song metrics = evaluate_vocals(doc, song) assert metrics["mae_timing_ms"] <= 50.0 assert metrics["strict_pitch_accuracy"] >= 0.99 assert metrics["octave_equiv_pitch_accuracy"] >= 0.99 assert metrics["wer"] == 0.0