keyshift-api / tests /test_key_detector.py
balakrishna567's picture
feat: key detector — Krumhansl-Schmuckler 24-key Pearson correlation
0845ad7
Raw
History Blame Contribute Delete
949 Bytes
import numpy as np
from app.services.key_detector import detect_key, MAJOR_PROFILE, MINOR_PROFILE
def test_c_major_profile_detects_c_major():
root, mode, conf = detect_key(np.array(MAJOR_PROFILE))
assert root == "C" and mode == "major" and conf > 0.8
def test_a_minor_profile_detects_a_minor():
# Roll minor profile so A (index 9) aligns as root
root, mode, conf = detect_key(np.roll(MINOR_PROFILE, 9))
assert root == "A" and mode == "minor" and conf > 0.8
def test_g_major_detects_correctly():
root, mode, _ = detect_key(np.roll(MAJOR_PROFILE, 7))
assert root == "G" and mode == "major"
def test_confidence_range():
_, _, conf = detect_key(np.random.rand(12))
assert 0.0 <= conf <= 1.0
def test_returns_valid_note_and_mode():
valid = {"C","C#","D","D#","E","F","F#","G","G#","A","A#","B"}
root, mode, _ = detect_key(np.random.rand(12))
assert root in valid
assert mode in {"major", "minor"}