diff --git a/tests/test_pipeline_m5.py b/tests/test_pipeline_m5.py new file mode 100644 index 0000000..81aa433 --- /dev/null +++ b/tests/test_pipeline_m5.py @@ -0,0 +1,77 @@ +"""Integration test for Milestone 5 (Stage 7 Strumming Analysis).""" + +import json +from pathlib import Path + +import numpy as np +import soundfile as sf + +from songsplit.pipeline import PipelineContext +from songsplit.pipeline.s7_strumming import StrummingStage +from songsplit.schema.meta import MetaDocument, SourceFileInfo +from songsplit.schema.strumming import StrummingDocument +from songsplit.schema.tempo import TempoAnchor, TempoDocument + + +def test_m5_strumming_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 audio with strumming acoustic pulses + sr = 22050 + duration_sec = 4.0 + audio = np.zeros(int(sr * duration_sec), dtype=np.float32) + + # Pulses every 0.25s (8th notes at 120 BPM: 0.5s per quarter beat -> 0.25s per 8th note) + pulse_len = int(sr * 0.05) + t_pulse = np.linspace(0, 0.05, pulse_len, endpoint=False) + pulse = (np.sin(2 * np.pi * 350.0 * t_pulse) * np.exp(-t_pulse / 0.01)).astype(np.float32) + + for onset_t in np.arange(0.25, 3.75, 0.25): + idx = int(onset_t * sr) + audio[idx : idx + pulse_len] += pulse + + guitar_wav = stems_dir / "guitar.wav" + sf.write(str(guitar_wav), audio, sr) + + # 2. Prepare tempo.json at 120 BPM starting at 0.0s + 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="Strumming Test Song", + artist="Test Guitarist", + duration_sec=duration_sec, + source_file=SourceFileInfo(path=str(guitar_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)) + + # 4. Run StrummingStage + ctx = PipelineContext(source_file=guitar_wav, output_dir=song_dir) + stage = StrummingStage(grid="8th") + stage.run(ctx) + + strum_json = song_dir / "strumming.json" + assert strum_json.exists() + + with open(strum_json, encoding="utf-8") as f: + doc = StrummingDocument.model_validate(json.load(f)) + + assert len(doc.events) > 0 + for e in doc.events: + # Quantized to 8th note grid (multiples of 0.5 beats) + assert (e.beat * 2).is_integer() + assert e.direction in ("down", "up") + assert 0.0 <= e.strength <= 1.0 + assert 0.0 <= e.confidence <= 1.0 + + # Verify marked as experimental in generated_by metadata + assert doc.generated_by is not None + assert doc.generated_by.params.get("status") == "experimental" + + # Patterns generated + assert len(doc.patterns) > 0 + assert doc.patterns[0].grid == "8th" diff --git a/tests/test_strumming.py b/tests/test_strumming.py new file mode 100644 index 0000000..fee833c --- /dev/null +++ b/tests/test_strumming.py @@ -0,0 +1,39 @@ +"""Unit tests for strumming direction estimation and pattern aggregation.""" + +import numpy as np + +from songsplit.pipeline.s7_strumming import aggregate_strum_patterns, estimate_strum_direction +from songsplit.schema.strumming import StrumEvent + + +def test_estimate_strum_direction_phase_prior(): + sr = 22050 + audio = np.random.randn(sr).astype(np.float32) + + # On-beat (beat 0.0) -> down + dir_down, conf_down = estimate_strum_direction(audio, sr, onset_sec=0.1, beat=0.0) + assert dir_down == "down" + assert conf_down >= 0.5 + + # Off-beat (beat 0.5) -> up + dir_up, conf_up = estimate_strum_direction(audio, sr, onset_sec=0.1, beat=0.5) + assert dir_up == "up" + assert conf_up >= 0.5 + + +def test_aggregate_strum_patterns_island_strum(): + # Famous "island strum": D . D U . U D U (slots: 0, 1, 2, 3, 4, 5, 6, 7 on 8th grid) + # slots: 0.0 (D), 1.0 (D), 1.5 (U), 2.5 (U), 3.0 (D), 3.5 (U) + events = [ + StrumEvent(beat=0.0, direction="down", strength=0.9, confidence=0.8), + StrumEvent(beat=1.0, direction="down", strength=0.8, confidence=0.8), + StrumEvent(beat=1.5, direction="up", strength=0.7, confidence=0.7), + StrumEvent(beat=2.5, direction="up", strength=0.7, confidence=0.7), + StrumEvent(beat=3.0, direction="down", strength=0.8, confidence=0.8), + StrumEvent(beat=3.5, direction="up", strength=0.7, confidence=0.7), + ] + + patterns = aggregate_strum_patterns(events, total_beats=4.0, grid="8th", pattern_beats=4.0, section_beats=4.0) + assert len(patterns) == 1 + assert patterns[0].pattern == "D . D U . U D U" + assert patterns[0].grid == "8th"