test: add unit and integration tests for M4 chord parsing, template matching, and pipeline
This commit is contained in:
75
tests/test_chords.py
Normal file
75
tests/test_chords.py
Normal file
@ -0,0 +1,75 @@
|
|||||||
|
"""Unit tests for Harte chord parsing, key detection, and template chord recognition."""
|
||||||
|
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
|
from songsplit.pipeline.s6_chords import estimate_key, extract_chords_template, parse_harte_chord
|
||||||
|
|
||||||
|
|
||||||
|
def test_parse_harte_chord_labels():
|
||||||
|
# Major triad
|
||||||
|
root, quality, bass = parse_harte_chord("C")
|
||||||
|
assert root == "C"
|
||||||
|
assert quality == "maj"
|
||||||
|
assert bass is None
|
||||||
|
|
||||||
|
# Minor triad
|
||||||
|
root, quality, bass = parse_harte_chord("Am")
|
||||||
|
assert root == "A"
|
||||||
|
assert quality == "min"
|
||||||
|
assert bass is None
|
||||||
|
|
||||||
|
# Dominant 7th
|
||||||
|
root, quality, bass = parse_harte_chord("G7")
|
||||||
|
assert root == "G"
|
||||||
|
assert quality == "7"
|
||||||
|
assert bass is None
|
||||||
|
|
||||||
|
# Sus4
|
||||||
|
root, quality, bass = parse_harte_chord("Dsus4")
|
||||||
|
assert root == "D"
|
||||||
|
assert quality == "sus4"
|
||||||
|
assert bass is None
|
||||||
|
|
||||||
|
# Slash chord
|
||||||
|
root, quality, bass = parse_harte_chord("F/A")
|
||||||
|
assert root == "F"
|
||||||
|
assert quality == "maj"
|
||||||
|
assert bass == "A"
|
||||||
|
|
||||||
|
# No chord
|
||||||
|
root, quality, bass = parse_harte_chord("N")
|
||||||
|
assert root is None
|
||||||
|
assert quality is None
|
||||||
|
assert bass is None
|
||||||
|
|
||||||
|
|
||||||
|
def test_estimate_key():
|
||||||
|
# Synthetic C major profile (peaks at C, E, G -> indices 0, 4, 7)
|
||||||
|
c_maj_chroma = np.zeros(12)
|
||||||
|
c_maj_chroma[0] = 5.0
|
||||||
|
c_maj_chroma[4] = 4.0
|
||||||
|
c_maj_chroma[7] = 4.5
|
||||||
|
assert estimate_key(c_maj_chroma) == "C"
|
||||||
|
|
||||||
|
# Synthetic A minor profile (peaks at A, C, E -> indices 9, 0, 4)
|
||||||
|
a_min_chroma = np.zeros(12)
|
||||||
|
a_min_chroma[9] = 5.0
|
||||||
|
a_min_chroma[0] = 4.0
|
||||||
|
a_min_chroma[4] = 4.5
|
||||||
|
assert estimate_key(a_min_chroma) == "Am"
|
||||||
|
|
||||||
|
|
||||||
|
def test_extract_chords_template_synthetic():
|
||||||
|
sr = 22050
|
||||||
|
duration = 1.0
|
||||||
|
t = np.linspace(0, duration, int(sr * duration), endpoint=False)
|
||||||
|
|
||||||
|
# Synthesize C major: C4 (261.63), E4 (329.63), G4 (392.00)
|
||||||
|
c_chord = (np.sin(2 * np.pi * 261.63 * t) + np.sin(2 * np.pi * 329.63 * t) + np.sin(2 * np.pi * 392.00 * t)).astype(
|
||||||
|
np.float32
|
||||||
|
)
|
||||||
|
|
||||||
|
raw_chords = extract_chords_template(c_chord, sr)
|
||||||
|
labels = [ch for _, ch in raw_chords]
|
||||||
|
# Dominant detected chord should be "C"
|
||||||
|
assert labels.count("C") > len(labels) * 0.5
|
||||||
76
tests/test_pipeline_m4.py
Normal file
76
tests/test_pipeline_m4.py
Normal file
@ -0,0 +1,76 @@
|
|||||||
|
"""Integration test for Milestone 4 (Stage 6 Beat-Quantized Chord Recognition)."""
|
||||||
|
|
||||||
|
import json
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
import numpy as np
|
||||||
|
import soundfile as sf
|
||||||
|
|
||||||
|
from songsplit.pipeline import PipelineContext
|
||||||
|
from songsplit.pipeline.s6_chords import ChordsStage
|
||||||
|
from songsplit.schema.chords import ChordsDocument
|
||||||
|
from songsplit.schema.meta import MetaDocument, SourceFileInfo
|
||||||
|
from songsplit.schema.tempo import TempoAnchor, TempoDocument
|
||||||
|
|
||||||
|
|
||||||
|
def test_m4_chords_stage_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 instrumental audio:
|
||||||
|
# 2.0s C major (261.63, 329.63, 392.00) + 2.0s A minor (220.0, 261.63, 329.63)
|
||||||
|
sr = 22050
|
||||||
|
t_half = np.linspace(0, 2.0, int(sr * 2.0), endpoint=False)
|
||||||
|
|
||||||
|
c_major = (
|
||||||
|
np.sin(2 * np.pi * 261.63 * t_half) + np.sin(2 * np.pi * 329.63 * t_half) + np.sin(2 * np.pi * 392.00 * t_half)
|
||||||
|
).astype(np.float32)
|
||||||
|
|
||||||
|
a_minor = (
|
||||||
|
np.sin(2 * np.pi * 220.00 * t_half) + np.sin(2 * np.pi * 261.63 * t_half) + np.sin(2 * np.pi * 329.63 * t_half)
|
||||||
|
).astype(np.float32)
|
||||||
|
|
||||||
|
inst_audio = np.concatenate([c_major, a_minor])
|
||||||
|
inst_wav = stems_dir / "instrumental.wav"
|
||||||
|
sf.write(str(inst_wav), inst_audio, sr)
|
||||||
|
|
||||||
|
# 2. Prepare tempo.json: 120 BPM starting at 0.0s (0.5s per beat)
|
||||||
|
# 4.0s = 8 beats total (0.0 to 4.0 beat is C, 4.0 to 8.0 beat is Am)
|
||||||
|
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="Chord Test Song",
|
||||||
|
artist="Test Pianist",
|
||||||
|
duration_sec=4.0,
|
||||||
|
source_file=SourceFileInfo(path=str(inst_wav), sha256="fakehash", size_bytes=len(inst_audio)),
|
||||||
|
created_at="2026-11-02T10:00:00Z",
|
||||||
|
)
|
||||||
|
(song_dir / "meta.json").write_text(meta.model_dump_json(indent=2))
|
||||||
|
|
||||||
|
# 4. Run ChordsStage with template backend
|
||||||
|
ctx = PipelineContext(source_file=inst_wav, output_dir=song_dir)
|
||||||
|
stage = ChordsStage(backend="template")
|
||||||
|
stage.run(ctx)
|
||||||
|
|
||||||
|
chords_json = song_dir / "chords.json"
|
||||||
|
assert chords_json.exists()
|
||||||
|
|
||||||
|
with open(chords_json, encoding="utf-8") as f:
|
||||||
|
doc = ChordsDocument.model_validate(json.load(f))
|
||||||
|
|
||||||
|
assert len(doc.chords) >= 1
|
||||||
|
labels = [c.label for c in doc.chords]
|
||||||
|
assert "C" in labels or "Am" in labels
|
||||||
|
|
||||||
|
# Check beat-based timing
|
||||||
|
for ch in doc.chords:
|
||||||
|
assert ch.start_beat >= 0.0
|
||||||
|
assert ch.end_beat > ch.start_beat
|
||||||
|
assert ch.confidence > 0.0
|
||||||
|
|
||||||
|
# 5. Verify meta.json updated with key
|
||||||
|
updated_meta = ctx.load_or_init_meta()
|
||||||
|
assert updated_meta.key in ("C", "Am")
|
||||||
Reference in New Issue
Block a user