Fixed-lag cross-correlation (even after the leading_anchor_windows fix) cannot find every real match: hpr0901 and hpr0902 share hpr0900's confirmed jingle (by ear, and by a matching ~34s intro-envelope duration across all three) but never correlate above ~0.3-0.4 against it at any single fixed lag or speed ratio, because the jingle has internal tempo variation no global alignment can follow. Adds audio.dtw_chroma_match: chroma-feature DTW against a reference clip, which isn't defeated by that since it doesn't assume one global alignment, and whose warping-path end position gives a real intro_end estimate. Optional dependency (librosa), same philosophy as ASR -- a missing import returns None, never an exception. Wired into detect._sibling_fallback as a last resort, tried only when fixed-lag correlation finds nothing, since it's far more expensive. Calibrated on real episodes against the newly vendored references/pre2015_jingle_variant_b.flac (hpr0900's jingle, confirmed to recur in ~5% of a random sample): confirmed/likely matches scored cost 0.09-0.17, a confirmed non-match (hpr0898, no intro at all) scored 0.94. Also tried and rejected for this purpose: generic chroma classification (can't tell whose music it is, not just that it's music) and chromaprint/pyacoustid fingerprinting (clean separation but no tolerance for tempo drift). See docs/DESIGN.md for the full writeup, including a screenshot showing the matching intro-envelope duration that motivated this.
93 lines
4.7 KiB
Python
93 lines
4.7 KiB
Python
"""
|
|
Regression test for a real gap: fixed-lag cross-correlation can completely
|
|
miss a genuine shared jingle when it has internal tempo variation. Found
|
|
directly: hpr0901 and hpr0902 share hpr0900's confirmed jingle (confirmed
|
|
by ear, and by a matching ~34s intro-envelope duration across all three),
|
|
but never correlated above ~0.3-0.4 against it at any single fixed lag or
|
|
speed ratio -- well under XCORR_MATCH_THRESHOLD (0.9). Chroma-feature DTW
|
|
(audio.dtw_chroma_match) isn't defeated by that, since it doesn't assume
|
|
one global alignment.
|
|
|
|
audio.dtw_chroma_match itself needs librosa (an optional dependency, same
|
|
philosophy as ASR -- see asr.py) and real audio, so it isn't exercised
|
|
directly here. What's tested is detect._sibling_fallback's wiring: it
|
|
must fall through to DTW only when fixed-lag correlation found nothing
|
|
(< 2s), and must use the DTW result when the cost clears
|
|
config.DTW_MATCH_THRESHOLD.
|
|
"""
|
|
import unittest
|
|
from unittest import mock
|
|
|
|
from hpr_stripper import audio, config, detect
|
|
|
|
|
|
class TestDtwChromaMatchResilience(unittest.TestCase):
|
|
def test_missing_librosa_returns_none_not_exception(self):
|
|
# Regression test: librosa is an optional dependency (see
|
|
# dtw_chroma_match's docstring) -- it not being installed must be
|
|
# treated exactly like ASR being unavailable (asr.py), never an
|
|
# exception that stops the pipeline.
|
|
real_import = __import__
|
|
|
|
def fake_import(name, *args, **kwargs):
|
|
if name == "librosa":
|
|
raise ImportError("no module named librosa")
|
|
return real_import(name, *args, **kwargs)
|
|
|
|
with mock.patch("builtins.__import__", side_effect=fake_import):
|
|
result = audio.dtw_chroma_match("/tmp/does-not-matter-a.flac", "/tmp/does-not-matter-b.mp3")
|
|
self.assertIsNone(result)
|
|
|
|
|
|
class TestSiblingFallbackDtwWiring(unittest.TestCase):
|
|
def test_falls_through_to_dtw_when_correlation_finds_nothing(self):
|
|
with mock.patch("hpr_stripper.audio.load_pcm_f32", return_value=[0.0]), \
|
|
mock.patch("hpr_stripper.audio.windowed_corr", return_value=([0.1, 0.1], 0.25)), \
|
|
mock.patch("hpr_stripper.audio.leading_match_seconds", return_value=0.0), \
|
|
mock.patch("hpr_stripper.audio.dtw_chroma_match", return_value=(0.12, 31.1)):
|
|
median, flag, _ = detect._sibling_fallback(
|
|
"/tmp/ep.mp3", 1200.0, "hpr0901", None, None,
|
|
["/tmp/neighbour.mp3"], suffix=False)
|
|
self.assertEqual(median, 31.1)
|
|
self.assertIsNone(flag)
|
|
|
|
def test_ignores_dtw_result_above_threshold(self):
|
|
with mock.patch("hpr_stripper.audio.load_pcm_f32", return_value=[0.0]), \
|
|
mock.patch("hpr_stripper.audio.windowed_corr", return_value=([0.1, 0.1], 0.25)), \
|
|
mock.patch("hpr_stripper.audio.leading_match_seconds", return_value=0.0), \
|
|
mock.patch("hpr_stripper.audio.dtw_chroma_match",
|
|
return_value=(config.DTW_MATCH_THRESHOLD + 0.1, 31.1)):
|
|
median, flag, _ = detect._sibling_fallback(
|
|
"/tmp/ep.mp3", 1200.0, "hpr0901", None, None,
|
|
["/tmp/neighbour.mp3"], suffix=False)
|
|
self.assertEqual(median, 0.0)
|
|
self.assertEqual(flag, "no_asr_anchor_and_xcorr_inconclusive")
|
|
|
|
def test_does_not_call_dtw_when_correlation_already_found_a_match(self):
|
|
with mock.patch("hpr_stripper.audio.load_pcm_f32", return_value=[0.0]), \
|
|
mock.patch("hpr_stripper.audio.windowed_corr", return_value=([0.99, 0.99], 0.25)), \
|
|
mock.patch("hpr_stripper.audio.leading_match_seconds", return_value=29.25), \
|
|
mock.patch("hpr_stripper.audio.dtw_chroma_match") as dtw_mock:
|
|
median, flag, _ = detect._sibling_fallback(
|
|
"/tmp/ep.mp3", 1200.0, "hpr0400", None, None,
|
|
["/tmp/neighbour.mp3"], suffix=False)
|
|
dtw_mock.assert_not_called()
|
|
self.assertEqual(median, 29.25)
|
|
|
|
def test_dtw_unavailable_falls_back_cleanly(self):
|
|
# librosa not installed -- dtw_chroma_match returns None, same
|
|
# contract as asr.transcribe_clip's "engine unavailable" case.
|
|
with mock.patch("hpr_stripper.audio.load_pcm_f32", return_value=[0.0]), \
|
|
mock.patch("hpr_stripper.audio.windowed_corr", return_value=([0.1, 0.1], 0.25)), \
|
|
mock.patch("hpr_stripper.audio.leading_match_seconds", return_value=0.0), \
|
|
mock.patch("hpr_stripper.audio.dtw_chroma_match", return_value=None):
|
|
median, flag, _ = detect._sibling_fallback(
|
|
"/tmp/ep.mp3", 1200.0, "hpr0901", None, None,
|
|
["/tmp/neighbour.mp3"], suffix=False)
|
|
self.assertEqual(median, 0.0)
|
|
self.assertEqual(flag, "no_asr_anchor_and_xcorr_inconclusive")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|