test: add unit and integration tests for M5 strumming analysis and beat quantization

This commit is contained in:
2026-11-02 11:20:33 +01:00
parent 7ec6b3274b
commit 760bad7435
2 changed files with 116 additions and 0 deletions

77
tests/test_pipeline_m5.py Normal file
View File

@ -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"