feat(pitch): implement stage 4 torchcrepe pitch tracking with octave error correction

This commit is contained in:
2026-11-02 10:42:10 +01:00
parent acb1411aa1
commit ac9831d195
2 changed files with 237 additions and 4 deletions

41
tests/test_pitch.py Normal file
View File

@ -0,0 +1,41 @@
"""Unit tests for Stage 4 pitch processing (Hz to MIDI and octave error correction)."""
import numpy as np
import pytest
from songsplit.pipeline.s4_pitch import correct_octave_errors, hz_to_midi
def test_hz_to_midi_standards():
# A4 = 440 Hz -> MIDI 69
# A3 = 220 Hz -> MIDI 57
# C4 = 261.6256 Hz -> MIDI 60
hz = np.array([440.0, 220.0, 261.6256])
midi = hz_to_midi(hz)
assert midi[0] == pytest.approx(69.0, abs=0.01)
assert midi[1] == pytest.approx(57.0, abs=0.01)
assert midi[2] == pytest.approx(60.0, abs=0.01)
def test_correct_octave_errors():
# Line with an octave spike in the middle: 60, 72 (+12 jump), 61
syllables = [
{"text": "A", "midi": 60.0},
{"text": "B", "midi": 72.0}, # Isolated octave jump
{"text": "C", "midi": 61.0},
]
corrected = correct_octave_errors(syllables)
assert corrected[0]["midi"] == 60.0
assert corrected[1]["midi"] == pytest.approx(60.0) # Corrected from 72 down to 60
assert corrected[2]["midi"] == 61.0
def test_no_correction_for_legitimate_contour():
# Gradual rise: 60 -> 64 -> 67 (C major triad arpeggio)
syllables = [
{"text": "A", "midi": 60.0},
{"text": "B", "midi": 64.0},
{"text": "C", "midi": 67.0},
]
corrected = correct_octave_errors(syllables)
assert corrected[1]["midi"] == 64.0