feat(lyrics): split Whisper segments into singable lines
Segments are cut at word pauses > 0.6 s and long lines at their largest gap. Mistimed single leading/trailing words are merged into the adjacent line. Raw Whisper segments are cached in whisper_segments.json so line building can be A/B tested without re-running Whisper. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@ -37,3 +37,57 @@ def test_split_word_into_syllables_timed():
|
||||
# Syllables should be "Hel" and "lo "
|
||||
assert timed[0]["text"] == "Hel"
|
||||
assert timed[1]["text"] == "lo "
|
||||
|
||||
|
||||
def test_split_into_vocal_lines_by_gap_and_length():
|
||||
from unmix.pipeline.s3_lyrics import split_into_vocal_lines
|
||||
|
||||
def syl(text, start, end, word_end=True):
|
||||
return {"text": text, "start": start, "end": end, "word_end": word_end}
|
||||
|
||||
# Pause of 1.0 s between words -> two lines
|
||||
sylls = [syl("a ", 0.0, 0.5), syl("b ", 0.5, 1.0), syl("c ", 2.0, 2.5)]
|
||||
assert [len(c) for c in split_into_vocal_lines(sylls)] == [2, 1]
|
||||
|
||||
# No pause but > max_line_sec -> split at word boundary, never inside a word
|
||||
long_line = [syl("x", 0.0, 2.0, word_end=False), syl("y ", 2.0, 4.0), syl("z ", 4.0, 6.0), syl("w ", 6.0, 8.0)]
|
||||
chunks = split_into_vocal_lines(long_line)
|
||||
assert len(chunks) >= 2
|
||||
assert chunks[0][-1]["word_end"] is True
|
||||
assert sum(len(c) for c in chunks) == 4
|
||||
|
||||
assert split_into_vocal_lines([]) == []
|
||||
|
||||
|
||||
def test_orphan_trailing_word_moves_to_next_segment():
|
||||
from unmix.pipeline.s3_lyrics import pop_trailing_orphan, prepend_orphan
|
||||
|
||||
def syl(text, start, end, word_end=True):
|
||||
return {"text": text, "start": start, "end": end, "word_end": word_end}
|
||||
|
||||
chunks = [[syl("any ", 34.7, 36.1)], [syl("If ", 37.0, 37.8)]]
|
||||
rest, orphan = pop_trailing_orphan(chunks)
|
||||
assert len(rest) == 1 and orphan is not None
|
||||
|
||||
nxt = prepend_orphan(orphan, [[syl("I ", 40.9, 41.1), syl("lay ", 41.1, 41.4)]])
|
||||
assert [s["text"] for s in nxt[0]] == ["If ", "I ", "lay "]
|
||||
assert nxt[0][0]["start"] == 40.9
|
||||
assert nxt[0][1]["start"] >= nxt[0][0]["end"]
|
||||
|
||||
# Long single word (> 1 s) stays a line of its own
|
||||
chunks2 = [[syl("a ", 1.0, 2.0)], [syl("b ", 5.0, 7.5)]]
|
||||
assert pop_trailing_orphan(chunks2) == (chunks2, None)
|
||||
|
||||
|
||||
def test_merge_leading_orphan():
|
||||
from unmix.pipeline.s3_lyrics import merge_leading_orphan
|
||||
|
||||
def syl(text, start, end, word_end=True):
|
||||
return {"text": text, "start": start, "end": end, "word_end": word_end}
|
||||
|
||||
chunks = [[syl("If ", 37.0, 37.8)], [syl("I ", 40.9, 41.1), syl("lay ", 41.1, 41.4)]]
|
||||
merged = merge_leading_orphan(chunks)
|
||||
assert len(merged) == 1
|
||||
assert [s["text"] for s in merged[0]] == ["If ", "I ", "lay "]
|
||||
assert merged[0][0]["start"] == 40.9
|
||||
assert len(merge_leading_orphan([[syl("a ", 0.0, 0.5)]])) == 1
|
||||
|
||||
Reference in New Issue
Block a user