test: add integration test for M3 tempo stage and UltraStar BPM synchronization

This commit is contained in:
2026-11-02 10:53:19 +01:00
parent 887bd4a3e9
commit fac85bcd56

94
tests/test_pipeline_m3.py Normal file
View File

@ -0,0 +1,94 @@
"""Integration test for Milestone 3 (Stage 5 Tempo Map and Click Track)."""
import json
from pathlib import Path
import numpy as np
import pytest
import soundfile as sf
from songsplit.export.ultrastar import export_ultrastar, parse_ultrastar
from songsplit.pipeline import PipelineContext
from songsplit.pipeline.s5_tempo import TempoStage
from songsplit.schema.meta import MetaDocument, SourceFileInfo
from songsplit.schema.tempo import TempoDocument
from songsplit.schema.vocals import VocalLine, VocalsDocument, VocalSyllable
def test_m3_tempo_stage_and_click_track(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. Generate 6.0s synthetic drum beats at 120 BPM (beats at 0.0, 0.5, 1.0, 1.5, ...)
sr = 44100
duration_sec = 6.0
audio = np.zeros(int(sr * duration_sec), dtype=np.float32)
# Place a 10ms burst pulse every 0.5s
burst_len = int(sr * 0.01)
burst = np.sin(2 * np.pi * 300.0 * np.linspace(0, 0.01, burst_len, endpoint=False)).astype(np.float32)
for t in np.arange(0.5, 5.5, 0.5):
idx = int(t * sr)
audio[idx : idx + burst_len] += burst
drums_wav = stems_dir / "drums.wav"
sf.write(str(drums_wav), audio, sr)
# 2. Write meta.json
meta = MetaDocument(
title="Tempo Test Song",
artist="Test Drummer",
duration_sec=duration_sec,
source_file=SourceFileInfo(path=str(drums_wav), sha256="fakehash", size_bytes=len(audio)),
created_at="2026-11-02T10:00:00Z",
)
(song_dir / "meta.json").write_text(meta.model_dump_json(indent=2))
# 3. Write minimal vocals.json
vocals = VocalsDocument(
lines=[
VocalLine(
start=1.0,
end=2.0,
syllables=[VocalSyllable(text="Beat", start=1.0, end=2.0, midi=60.0)],
)
]
)
(song_dir / "vocals.json").write_text(vocals.model_dump_json(indent=2))
# 4. Run TempoStage
ctx = PipelineContext(source_file=drums_wav, output_dir=song_dir)
stage = TempoStage()
stage.run(ctx)
# 5. Verify tempo.json
tempo_json = song_dir / "tempo.json"
assert tempo_json.exists()
with open(tempo_json, encoding="utf-8") as f:
doc = TempoDocument.model_validate(json.load(f))
assert len(doc.anchors) >= 1
# Detected tempo should be around 120 BPM (or 60/240 subharmonic)
bpm = doc.anchors[0].bpm
assert (
bpm == pytest.approx(120.0, abs=5.0)
or bpm == pytest.approx(60.0, abs=5.0)
or bpm == pytest.approx(240.0, abs=10.0)
)
# 6. Verify click track audio generated
click_track = cache_dir / "click_track.wav"
assert click_track.exists()
click_data, click_sr = sf.read(str(click_track))
assert len(click_data) == len(audio)
# 7. Test UltraStar export derives BPM/GAP from tempo.json
txt_path = export_ultrastar(song_dir)
assert txt_path.exists()
song = parse_ultrastar(txt_path)
assert song.bpm == pytest.approx(doc.anchors[0].bpm, abs=1.0)