fix(review): pad preview audio when submitted lyrics already carry the countdown

CodeRabbit: corrections that already include the countdown segment (timings
shifted) were rendered against unpadded audio and reported a 0s offset.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012RYLVVUdF2J1bNeVa2uLXA
This commit is contained in:
Andrew BeveridgeandClaude Opus 5.5 committed 2026-11-11 13:18:58 -05:00
1 parent a47435c6d3
commit 0c029a98ba
3 files changed
+45 -12

No files matched your search

@@ -98,13 +98,13 @@ class CountdownProcessor:
Same rule as ``process``/``add_countdown_to_lyrics_if_needed`` but on the
plain ``corrected_segments`` dicts a review client sends, so an API can
tell the client how far the preview's timeline is shifted without
rebuilding a CorrectionResult. 0.0 if no countdown applies (or one is
already present).
rebuilding a CorrectionResult. A countdown that is already present still
means a shifted timeline (and padded audio). 0.0 if no countdown applies.
"""
if not segments:
return 0.0
if (segments[0] or {}).get("text") == cls.COUNTDOWN_TEXT:
return 0.0
return cls.COUNTDOWN_PADDING_SECONDS
for segment in segments:
words = (segment or {}).get("words") or []
if words:
@@ -124,13 +124,17 @@ class CountdownProcessor:
Used by the review preview, which must match the final render but pads
its audio separately (or has the encoder use pre-padded audio).
Already-countdown-processed results are returned unchanged.
Already-countdown-processed results are returned unchanged, but still
report the padding: their timings are already shifted, so the audio
must be padded to match.
Returns:
(possibly modified CorrectionResult, padding seconds the caller must
prepend to the audio — 0.0 when no countdown was added)
prepend to the audio — 0.0 when the song has no countdown intro)
"""
if self.has_countdown(correction_result) or not self._needs_countdown(correction_result):
if self.has_countdown(correction_result):
return correction_result, self.COUNTDOWN_PADDING_SECONDS
if not self._needs_countdown(correction_result):
return correction_result, 0.0
return self._add_countdown_to_result(correction_result), self.COUNTDOWN_PADDING_SECONDS
@@ -104,3 +104,23 @@ def test_countdown_previews_get_a_fresh_hash(run_preview):
result_late, *_ = run_preview(late, ass_only=True)
legacy_late = hashlib.md5(json.dumps(late, sort_keys=True).encode("utf-8")).hexdigest()[:12]
assert result_late["preview_hash"] == legacy_late
def test_submitted_countdown_is_not_doubled_but_audio_is_padded(run_preview):
"""Corrections that already carry the countdown (timings shifted) must not be
shifted again, but the audio must still be padded or lyrics run 3s early."""
data = {
"corrections": [],
"corrected_segments": [
{"id": "c", "text": CountdownProcessor.COUNTDOWN_TEXT, "start_time": 0.1, "end_time": 2.9,
"words": [{"id": "cw", "text": CountdownProcessor.COUNTDOWN_TEXT, "start_time": 0.1, "end_time": 2.9}]},
{"id": "s1", "text": "hello", "start_time": 3.4, "end_time": 3.9,
"words": [{"id": "w1", "text": "hello", "start_time": 3.4, "end_time": 3.9}]},
],
}
result, captured, pad_calls, padded = run_preview(data, ass_only=False)
assert result["countdown_padding_seconds"] == 3.0
segs = captured["correction"].corrected_segments
assert [s.text for s in segs] == [CountdownProcessor.COUNTDOWN_TEXT, "hello"]
assert segs[1].words[0].start_time == pytest.approx(3.4)
assert captured["audio"] == padded
@@ -37,10 +37,12 @@ class TestPaddingForSegmentsData:
segs = [{"text": "", "words": []}, {"text": "hi", "words": [{"text": "hi", "start_time": 1.0}]}]
assert CountdownProcessor.padding_for_segments_data(segs) == 3.0
def test_already_counted_down_needs_none(self):
def test_already_counted_down_still_reports_offset(self):
"""Timings are already shifted, so the timeline offset is still 3s."""
segs = [{"text": CountdownProcessor.COUNTDOWN_TEXT,
"words": [{"text": CountdownProcessor.COUNTDOWN_TEXT, "start_time": 0.1}]}]
assert CountdownProcessor.padding_for_segments_data(segs) == 0.0
"words": [{"text": CountdownProcessor.COUNTDOWN_TEXT, "start_time": 0.1}]},
{"text": "hi", "words": [{"text": "hi", "start_time": 3.4}]}]
assert CountdownProcessor.padding_for_segments_data(segs) == 3.0
def test_empty_or_malformed(self):
assert CountdownProcessor.padding_for_segments_data(None) == 0.0
@@ -67,17 +69,24 @@ class TestAddCountdownToLyricsIfNeeded:
assert padding == 0.0
assert result is original
def test_idempotent(self, tmp_path):
def test_idempotent_but_still_needs_padded_audio(self, tmp_path):
"""A result that already has the countdown isn't shifted again, but its
audio must still be padded to match the shifted timings."""
cp = CountdownProcessor(cache_dir=str(tmp_path))
once, _ = cp.add_countdown_to_lyrics_if_needed(_result(0.4))
twice, padding = cp.add_countdown_to_lyrics_if_needed(once)
assert padding == 0.0
assert padding == 3.0
assert twice is once
assert twice.corrected_segments[1].words[0].start_time == 3.4
def test_agrees_with_dict_helper(self, tmp_path):
cp = CountdownProcessor(cache_dir=str(tmp_path))
for start in (0.0, 2.99, 3.0, 7.5):
r = _result(start)
_, padding = cp.add_countdown_to_lyrics_if_needed(r)
out, padding = cp.add_countdown_to_lyrics_if_needed(r)
segs = [s.to_dict() for s in r.corrected_segments]
assert CountdownProcessor.padding_for_segments_data(segs) == padding
# ...and on the already-processed result too.
_, again = cp.add_countdown_to_lyrics_if_needed(out)
assert CountdownProcessor.padding_for_segments_data(
[s.to_dict() for s in out.corrected_segments]) == again