Initial commit: 基于旋律的音乐哼唱检索系统 (Query by Humming)
This commit is contained in:
@@ -0,0 +1,117 @@
|
||||
"""音频输入与预处理测试。"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import numpy as np
|
||||
import pytest
|
||||
|
||||
from qbh.audio import (
|
||||
bandpass,
|
||||
denoise,
|
||||
encode_wav_bytes,
|
||||
frame_signal,
|
||||
normalize,
|
||||
read_audio,
|
||||
read_wav_bytes,
|
||||
resample,
|
||||
rms_db,
|
||||
trim_silence,
|
||||
write_wav,
|
||||
)
|
||||
|
||||
|
||||
def _tone(freq: float, seconds: float, sr: int, 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_wav_roundtrip(tmp_path):
|
||||
sr = 16000
|
||||
signal = _tone(440.0, 0.5, sr)
|
||||
path = tmp_path / "tone.wav"
|
||||
write_wav(path, signal, sr)
|
||||
|
||||
loaded, loaded_sr = read_audio(path)
|
||||
assert loaded_sr == sr
|
||||
assert loaded.shape == signal.shape
|
||||
# 16-bit 量化误差应很小
|
||||
assert np.max(np.abs(loaded - signal)) < 1e-3
|
||||
|
||||
|
||||
def test_wav_bytes_roundtrip():
|
||||
sr = 8000
|
||||
signal = _tone(300.0, 0.2, sr)
|
||||
blob = encode_wav_bytes(signal, sr)
|
||||
loaded, loaded_sr = read_wav_bytes(blob)
|
||||
assert loaded_sr == sr
|
||||
assert np.allclose(loaded, signal, atol=1e-3)
|
||||
|
||||
|
||||
def test_resample_preserves_duration():
|
||||
sr = 44100
|
||||
signal = _tone(220.0, 0.4, sr)
|
||||
out = resample(signal, sr, 16000)
|
||||
assert abs(out.size / 16000 - 0.4) < 0.01
|
||||
|
||||
|
||||
def test_resample_identity():
|
||||
signal = _tone(440.0, 0.1, 16000)
|
||||
out = resample(signal, 16000, 16000)
|
||||
assert np.array_equal(out, signal)
|
||||
|
||||
|
||||
def test_bandpass_rejects_out_of_band():
|
||||
sr = 16000
|
||||
low = _tone(20.0, 0.3, sr) # 低于通带
|
||||
mid = _tone(440.0, 0.3, sr) # 通带内
|
||||
high = _tone(6000.0, 0.3, sr) # 高于通带
|
||||
|
||||
low_out = bandpass(low, sr, 60.0, 2000.0)
|
||||
mid_out = bandpass(mid, sr, 60.0, 2000.0)
|
||||
high_out = bandpass(high, sr, 60.0, 2000.0)
|
||||
|
||||
def rms(x: np.ndarray) -> float:
|
||||
return float(np.sqrt(np.mean(x**2)))
|
||||
|
||||
assert rms(mid_out) > 10 * rms(low_out)
|
||||
assert rms(mid_out) > 10 * rms(high_out)
|
||||
|
||||
|
||||
def test_denoise_reduces_high_frequency_noise():
|
||||
rng = np.random.default_rng(0)
|
||||
signal = _tone(440.0, 0.3, 16000, amp=0.5)
|
||||
noisy = signal + rng.normal(0, 0.05, signal.size).astype(np.float32)
|
||||
cleaned = denoise(noisy, 5)
|
||||
assert np.std(cleaned - signal) < np.std(noisy - signal)
|
||||
|
||||
|
||||
def test_trim_silence_removes_padding():
|
||||
sr = 16000
|
||||
core = _tone(440.0, 0.3, sr)
|
||||
padded = np.concatenate([np.zeros(sr // 2, np.float32), core, np.zeros(sr // 2, np.float32)])
|
||||
trimmed = trim_silence(padded, sr)
|
||||
assert trimmed.size < padded.size
|
||||
assert abs(trimmed.size / sr - 0.3) < 0.15
|
||||
|
||||
|
||||
def test_normalize_sets_rms():
|
||||
signal = _tone(440.0, 0.2, 16000, amp=0.01)
|
||||
out = normalize(signal, target_rms=0.1)
|
||||
assert abs(rms_db(out) - 20 * np.log10(0.1)) < 1.0
|
||||
|
||||
|
||||
def test_rms_db_of_silence_is_negative_infinity():
|
||||
assert rms_db(np.zeros(100, dtype=np.float32)) == float("-inf")
|
||||
|
||||
|
||||
def test_frame_signal_shape():
|
||||
x = np.arange(1000, dtype=np.float32)
|
||||
frames = frame_signal(x, 100, 50)
|
||||
assert frames.shape == (19, 100)
|
||||
assert np.allclose(frames[0][:5], x[:5])
|
||||
assert np.allclose(frames[1][:5], x[50:55])
|
||||
|
||||
|
||||
def test_read_audio_rejects_empty():
|
||||
with pytest.raises(Exception):
|
||||
read_wav_bytes(b"")
|
||||
Reference in New Issue
Block a user