78 lines
2.7 KiB
Python
78 lines
2.7 KiB
Python
"""Integration test for Milestone 5 (Stage 7 Strumming Analysis)."""
|
|
|
|
import json
|
|
from pathlib import Path
|
|
|
|
import numpy as np
|
|
import soundfile as sf
|
|
|
|
from unmix.pipeline import PipelineContext
|
|
from unmix.pipeline.s7_strumming import StrummingStage
|
|
from unmix.schema.meta import MetaDocument, SourceFileInfo
|
|
from unmix.schema.strumming import StrummingDocument
|
|
from unmix.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"
|