Initial commit: 基于旋律的音乐哼唱检索系统 (Query by Humming)
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
"""YIN 基频提取测试。"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import numpy as np
|
||||
import pytest
|
||||
|
||||
from qbh.pitch import extract_pitch, hz_to_midi, midi_to_hz
|
||||
|
||||
|
||||
def _tone(freq: float, seconds: float, sr: int = 16000, amp: float = 0.5) -> np.ndarray:
|
||||
t = np.arange(int(sr * seconds)) / sr
|
||||
return (amp * np.sin(2 * np.pi * freq * t)).astype(np.float32)
|
||||
|
||||
|
||||
def test_midi_hz_roundtrip():
|
||||
for midi in (40.0, 60.0, 69.0, 72.5):
|
||||
assert abs(hz_to_midi(midi_to_hz(np.array([midi])))[0] - midi) < 1e-9
|
||||
assert abs(midi_to_hz(np.array([69.0]))[0] - 440.0) < 1e-9
|
||||
|
||||
|
||||
@pytest.mark.parametrize("freq", [80.0, 110.0, 220.0, 440.0, 660.0, 880.0])
|
||||
def test_pure_tone_pitch_accuracy(freq):
|
||||
track = extract_pitch(_tone(freq, 0.8), 16000)
|
||||
voiced = track.midi[track.voiced]
|
||||
assert voiced.size > 0
|
||||
expected = 69.0 + 12.0 * np.log2(freq / 440.0)
|
||||
# 允许 0.1 半音误差(含抛物线插值残差)
|
||||
assert abs(float(np.median(voiced)) - expected) < 0.1
|
||||
|
||||
|
||||
def test_silence_is_unvoiced():
|
||||
track = extract_pitch(np.zeros(16000, dtype=np.float32), 16000)
|
||||
assert track.voiced.mean() == 0.0
|
||||
|
||||
|
||||
def test_low_level_noise_is_unvoiced():
|
||||
rng = np.random.default_rng(1)
|
||||
track = extract_pitch(rng.normal(0, 1e-6, 16000).astype(np.float32), 16000)
|
||||
assert track.voiced.mean() == 0.0
|
||||
|
||||
|
||||
def test_harmonic_rich_signal_tracks_fundamental():
|
||||
sr = 16000
|
||||
t = np.arange(sr) / sr
|
||||
signal = np.zeros_like(t)
|
||||
for harmonic, amp in ((1, 1.0), (2, 0.4), (3, 0.2), (4, 0.1)):
|
||||
signal += amp * np.sin(2 * np.pi * 110.0 * harmonic * t)
|
||||
signal = (0.4 * signal / np.abs(signal).max()).astype(np.float32)
|
||||
|
||||
track = extract_pitch(signal, sr)
|
||||
voiced = track.midi[track.voiced]
|
||||
assert abs(float(np.median(voiced)) - 45.0) < 0.15 # A2 = MIDI 45
|
||||
|
||||
|
||||
def test_vibrato_tracked_within_reasonable_band():
|
||||
sr = 16000
|
||||
t = np.arange(sr) / sr
|
||||
f0 = 440.0 * 2 ** ((0.3 * np.sin(2 * np.pi * 5 * t)) / 12.0)
|
||||
phase = 2 * np.pi * np.cumsum(f0) / sr
|
||||
track = extract_pitch((0.5 * np.sin(phase)).astype(np.float32), sr)
|
||||
voiced = track.midi[track.voiced]
|
||||
assert abs(float(np.median(voiced)) - 69.0) < 0.1
|
||||
assert 0.05 < float(np.std(voiced)) < 0.5 # 确实跟踪到了颤音
|
||||
|
||||
|
||||
def test_confidence_high_for_clean_tone():
|
||||
track = extract_pitch(_tone(440.0, 0.5), 16000)
|
||||
assert float(np.median(track.conf[track.voiced])) > 0.9
|
||||
|
||||
|
||||
def test_empty_and_short_input():
|
||||
track = extract_pitch(np.zeros(0, dtype=np.float32), 16000)
|
||||
assert track.n_frames == 0
|
||||
short = extract_pitch(np.zeros(100, dtype=np.float32), 16000)
|
||||
assert short.voiced.size == 0
|
||||
Reference in New Issue
Block a user