Files
Lee Hanken 815ac0b1df Wire the pre-2015 jingle reference into the main detection path
Previously the vendored references/pre2015_jingle_variant_b.flac only
helped via _sibling_fallback's own DTW fallback, which is scoped to a
narrow +/-2 neighbour radius -- confirmed matches like hpr0908, hpr1117,
and hpr1467 are nowhere near hpr0900 or each other, so they'd never have
been found that way.

Adds detect._check_pre2015_variants: checks every vendored pre-2015
jingle variant directly against the episode being detected (fast
correlation first, DTW chroma matching as the per-variant fallback),
tried before falling back to neighbour-matching. This is the pre-2015
era's main-line reference check now, not just a neighbour-radius
fallback -- the old episodes are this tool's core use case, not an edge
case, and the ones most likely to have no official SRT and no other
usable signal.

Verified directly against real episodes outside any neighbour
relationship: hpr0908/hpr1117/hpr1467 resolve via fast correlation,
hpr0901/hpr0902 (tempo-drifted) via DTW, hpr0898 (confirmed no intro)
correctly finds nothing. hpr0899, previously assumed a non-match from
the old buggy correlation-only approach, now also resolves as a real
match -- consistent with that earlier label having been wrong.
2026-11-22 15:24:54 +00:00

136 lines
7.2 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 TestCheckPre2015Variants(unittest.TestCase):
"""detect._check_pre2015_variants is the pre-2015 era's main-line
reference check (not just the narrow ±2 neighbour-radius fallback in
_sibling_fallback) -- the old episodes are this tool's core use case,
and the most likely to have no official SRT and no other usable
signal, so this needs to work for any episode, not just ones lucky
enough to have a same-variant neighbour nearby."""
def test_uses_fast_correlation_when_it_finds_a_match(self):
with mock.patch("hpr_stripper.detect._pre2015_variant_paths",
return_value=["/refs/pre2015_jingle_variant_b.flac"]), \
mock.patch("hpr_stripper.audio.reference_prefix_match_seconds", return_value=32.0), \
mock.patch("hpr_stripper.audio.dtw_chroma_match") as dtw_mock:
end, method = detect._check_pre2015_variants("/tmp/ep.mp3")
dtw_mock.assert_not_called()
self.assertEqual(end, 32.0)
self.assertEqual(method, "pre2015_reference_xcorr:pre2015_jingle_variant_b")
def test_falls_back_to_dtw_when_correlation_finds_nothing(self):
with mock.patch("hpr_stripper.detect._pre2015_variant_paths",
return_value=["/refs/pre2015_jingle_variant_b.flac"]), \
mock.patch("hpr_stripper.audio.reference_prefix_match_seconds", return_value=0.0), \
mock.patch("hpr_stripper.audio.dtw_chroma_match", return_value=(0.15, 31.1)):
end, method = detect._check_pre2015_variants("/tmp/ep.mp3")
self.assertEqual(end, 31.1)
self.assertEqual(method, "pre2015_reference_dtw:pre2015_jingle_variant_b")
def test_no_variants_match_returns_none_none(self):
with mock.patch("hpr_stripper.detect._pre2015_variant_paths",
return_value=["/refs/pre2015_jingle_variant_b.flac"]), \
mock.patch("hpr_stripper.audio.reference_prefix_match_seconds", return_value=0.0), \
mock.patch("hpr_stripper.audio.dtw_chroma_match", return_value=(0.94, 2.0)):
end, method = detect._check_pre2015_variants("/tmp/ep.mp3")
self.assertIsNone(end)
self.assertIsNone(method)
def test_no_vendored_variants_at_all_returns_none_none(self):
with mock.patch("hpr_stripper.detect._pre2015_variant_paths", return_value=[]):
end, method = detect._check_pre2015_variants("/tmp/ep.mp3")
self.assertIsNone(end)
self.assertIsNone(method)
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()