64 lines
2.4 KiB
Python
64 lines
2.4 KiB
Python
"""Unit tests for tempo map builder and click track generator."""
|
|
|
|
import numpy as np
|
|
import pytest
|
|
|
|
from unmix.schema.tempo import TempoAnchor, TempoDocument, TimeSignature
|
|
from unmix.tempo import build_tempo_map_from_beats, generate_click_track
|
|
|
|
|
|
def test_build_tempo_map_constant_tempo():
|
|
# 120 BPM = 0.5s per beat, 16 beats = 8.0s
|
|
beat_times = np.arange(0.5, 8.5, 0.5)
|
|
# Downbeats every 4 beats (indices 0, 4, 8, 12 -> 0.5, 2.5, 4.5, 6.5)
|
|
downbeat_times = beat_times[::4]
|
|
|
|
tempo_doc = build_tempo_map_from_beats(beat_times, downbeat_times)
|
|
|
|
# Should only create 1 anchor for steady tempo
|
|
assert len(tempo_doc.anchors) == 1
|
|
anchor = tempo_doc.anchors[0]
|
|
assert anchor.bpm == pytest.approx(120.0, abs=0.5)
|
|
assert anchor.sec == pytest.approx(0.5, abs=0.01)
|
|
assert tempo_doc.time_signature.numerator == 4
|
|
assert tempo_doc.downbeat_offset == 0
|
|
|
|
|
|
def test_build_tempo_map_3_4_time_signature():
|
|
# 3/4 time signature: downbeats every 3 beats
|
|
beat_times = np.arange(0.0, 9.0, 0.5) # 120 BPM
|
|
downbeat_times = beat_times[::3]
|
|
|
|
tempo_doc = build_tempo_map_from_beats(beat_times, downbeat_times)
|
|
assert tempo_doc.time_signature.numerator == 3
|
|
|
|
|
|
def test_build_tempo_map_sustained_tempo_change():
|
|
# Section 1: 120 BPM (0.5s per beat) for 16 beats -> 8.0s
|
|
sec1 = np.arange(0.0, 8.0, 0.5)
|
|
# Section 2: 150 BPM (0.4s per beat) for 16 beats -> 6.4s
|
|
sec2 = 8.0 + np.arange(0.0, 6.4, 0.4)
|
|
all_beats = np.concatenate([sec1, sec2])
|
|
|
|
tempo_doc = build_tempo_map_from_beats(all_beats, min_tempo_change_bpm=3.0, min_beats_for_change=6)
|
|
# Should detect 2 anchors: one around 120 BPM, one around 150 BPM
|
|
assert len(tempo_doc.anchors) >= 2
|
|
assert tempo_doc.anchors[0].bpm == pytest.approx(120.0, abs=1.0)
|
|
assert tempo_doc.anchors[1].bpm == pytest.approx(150.0, abs=2.0)
|
|
|
|
|
|
def test_generate_click_track():
|
|
tempo_doc = TempoDocument(
|
|
anchors=[TempoAnchor(sec=0.5, beat=0.0, bpm=120.0)],
|
|
time_signature=TimeSignature(numerator=4, denominator=4),
|
|
downbeat_offset=0,
|
|
)
|
|
sr = 44100
|
|
duration_sec = 4.0
|
|
clicks = generate_click_track(tempo_doc, duration_sec=duration_sec, sr=sr)
|
|
|
|
assert len(clicks) == int(duration_sec * sr)
|
|
# At sec=0.5, there should be non-zero audio (first beat click)
|
|
click_idx = int(0.5 * sr)
|
|
assert np.max(np.abs(clicks[click_idx : click_idx + 100])) > 0.1
|