87 lines
2.8 KiB
Python
87 lines
2.8 KiB
Python
"""检索引擎端到端测试(合成曲库 + 合成哼唱)。"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import numpy as np
|
|
import pytest
|
|
|
|
from qbh.config import DEFAULT
|
|
from qbh.search import SearchEngine
|
|
from qbh.synth import render_melody
|
|
|
|
|
|
@pytest.fixture
|
|
def engine(tiny_library):
|
|
return SearchEngine(tiny_library, DEFAULT)
|
|
|
|
|
|
def test_index_built_from_library(engine, tiny_library):
|
|
assert engine.index.n_songs == tiny_library.count() == 3
|
|
|
|
|
|
def test_exact_melody_retrieves_itself(engine, tiny_library):
|
|
melody = tiny_library.get_melody("m_a")
|
|
result = engine.search_melody(melody, top_k=3, with_path=False)
|
|
assert result.hits[0].song_id == "m_a"
|
|
assert result.hits[0].score > 0.9
|
|
|
|
|
|
def test_transposed_melody_still_retrieved(engine, tiny_library):
|
|
melody = tiny_library.get_melody("m_b").transpose(7)
|
|
result = engine.search_melody(melody, top_k=3, with_path=False)
|
|
assert result.hits[0].song_id == "m_b"
|
|
|
|
|
|
def test_fragment_retrieval(engine, tiny_library):
|
|
melody = tiny_library.get_melody("m_a").slice(1, 7).transpose(-3)
|
|
result = engine.search_melody(melody, top_k=3, with_path=False)
|
|
assert result.hits[0].song_id == "m_a"
|
|
|
|
|
|
def test_audio_pipeline_retrieves_rendered_hum(engine, tiny_library):
|
|
melody = tiny_library.get_melody("m_a")
|
|
audio = render_melody(melody.transpose(4), 16000, timbre="hum", seed=21)
|
|
result, track = engine.search_audio(audio, 16000, top_k=3)
|
|
assert track.voiced_ratio() > 0.5
|
|
assert result.hits[0].song_id == "m_a"
|
|
|
|
|
|
def test_result_serialization(engine, tiny_library):
|
|
melody = tiny_library.get_melody("m_a")
|
|
result = engine.search_melody(melody, top_k=2, with_path=True)
|
|
payload = result.to_dict()
|
|
assert payload["n_songs"] == 3
|
|
assert payload["hits"]
|
|
assert "query" in payload
|
|
hit = payload["hits"][0]
|
|
for key in ("song_id", "title", "score", "dtw_sim", "index_sim", "shift", "path"):
|
|
assert key in hit
|
|
assert payload["query"]["contour"]
|
|
|
|
|
|
def test_hit_exposes_matched_segment(engine, tiny_library):
|
|
melody = tiny_library.get_melody("m_a")
|
|
result = engine.search_melody(melody, top_k=1, with_path=True)
|
|
hit = result.hits[0]
|
|
assert hit.target_end_s >= hit.target_start_s
|
|
assert hit.target_note_end >= hit.target_note_start
|
|
|
|
|
|
def test_coarse_candidates_include_answer(engine, tiny_library):
|
|
melody = tiny_library.get_melody("m_c").transpose(5)
|
|
candidates = [sid for sid, _ in engine.coarse_candidates(melody)]
|
|
assert "m_c" in candidates
|
|
|
|
|
|
def test_empty_melody_returns_no_hits(engine):
|
|
from qbh.melody import Melody
|
|
|
|
result = engine.search_melody(Melody.empty(), top_k=3)
|
|
assert result.hits == []
|
|
|
|
|
|
def test_elapsed_ms_is_reported(engine, tiny_library):
|
|
melody = tiny_library.get_melody("m_a")
|
|
result = engine.search_melody(melody, top_k=3, with_path=False)
|
|
assert result.elapsed_ms >= 0.0
|