tools/forced_align.py gibt dem Alignment-Modell den Text vor und setzt die Wortzeiten; Ausgabe im Format von align_words.py. Logik ohne Modell getestet; Handbuch ergänzt. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
83 lines
3.7 KiB
Python
83 lines
3.7 KiB
Python
"""Tests für tools/forced_align.py ohne Modell: die Logik (Zeilen, Lücken, Prüfungen) und main() mit Attrappe."""
|
|
import importlib.util
|
|
import json
|
|
import tempfile
|
|
import unittest
|
|
from pathlib import Path
|
|
|
|
spec = importlib.util.spec_from_file_location("forced_align", Path(__file__).with_name("forced_align.py"))
|
|
fa = importlib.util.module_from_spec(spec)
|
|
spec.loader.exec_module(fa)
|
|
|
|
|
|
class TextLinesTest(unittest.TestCase):
|
|
def test_skips_headings_directions_and_empty_lines(self):
|
|
text = "[Verse 1]\n\nHello there world\n# Kommentar\n(…)\n Second line \n[Chorus]\n123\n"
|
|
self.assertEqual(fa.text_lines(text), [["Hello", "there", "world"], ["Second", "line"]])
|
|
|
|
|
|
class FillGapsTest(unittest.TestCase):
|
|
def test_gaps_are_spread_between_neighbours(self):
|
|
out = fa.fill_gaps([(1.0, 1.5), None, None, (3.5, 4.0)], 10.0)
|
|
self.assertEqual([o[2] for o in out], [True, False, False, True])
|
|
# Lücke von 1,5 bis 3,5 s auf zwei Wörter: je eine Sekunde.
|
|
self.assertEqual(out[1][:2], (1.5, 2.5))
|
|
self.assertEqual(out[2][:2], (2.5, 3.5))
|
|
|
|
def test_edges_use_zero_and_duration(self):
|
|
out = fa.fill_gaps([None, (2.0, 3.0), None], 6.0)
|
|
self.assertEqual(out[0][:2], (0.0, 2.0))
|
|
self.assertEqual(out[2][:2], (3.0, 6.0))
|
|
|
|
def test_all_missing_spreads_over_the_whole_duration(self):
|
|
out = fa.fill_gaps([None, None], 4.0)
|
|
self.assertEqual([o[:2] for o in out], [(0.0, 2.0), (2.0, 4.0)])
|
|
|
|
def test_overlapping_neighbours_do_not_go_backwards(self):
|
|
out = fa.fill_gaps([(1.0, 3.0), None, (2.0, 2.5)], 10.0)
|
|
self.assertGreaterEqual(out[1][1], out[1][0])
|
|
|
|
|
|
class RowsTest(unittest.TestCase):
|
|
def test_rows_follow_the_line_structure_and_format_of_align_words(self):
|
|
lines = [["a", "b"], ["c"]]
|
|
aligned = [{"start": 0.5, "end": 0.9}, {"word": "b"}, {"start": 2.0, "end": 2.4}]
|
|
rows = fa.rows_from_words(lines, aligned, 5.0)
|
|
self.assertEqual([len(r) for r in rows], [2, 1])
|
|
self.assertEqual(rows[0][0], {"w": "a", "start": 0.5, "end": 0.9, "matched": True})
|
|
self.assertEqual(rows[0][1], {"w": "b", "start": 0.9, "end": 2.0, "matched": False})
|
|
|
|
def test_word_count_mismatch_is_an_error(self):
|
|
with self.assertRaises(ValueError):
|
|
fa.rows_from_words([["a", "b"]], [{"start": 0, "end": 1}], 2.0)
|
|
|
|
|
|
class MainTest(unittest.TestCase):
|
|
def test_main_writes_anchors_with_a_fake_aligner(self):
|
|
with tempfile.TemporaryDirectory() as d:
|
|
lyrics, out = Path(d) / "l.txt", Path(d) / "a.json"
|
|
lyrics.write_text("[Verse]\nHello world\nAgain\n", encoding="utf-8")
|
|
seen = {}
|
|
|
|
def fake(tokens, audio, language):
|
|
seen.update(tokens=tokens, audio=audio, language=language)
|
|
return [{"start": 1.0, "end": 1.4, "score": 0.9}, {"start": 1.5, "end": 2.0, "score": 0.1},
|
|
{"start": 3.0, "end": 3.3, "score": 0.8}], 4.0
|
|
|
|
fa.align_audio = fake
|
|
self.assertEqual(fa.main([str(lyrics), "x.mp3", "--out", str(out), "--language", "de"]), 0)
|
|
self.assertEqual(seen, {"tokens": ["Hello", "world", "Again"], "audio": "x.mp3", "language": "de"})
|
|
data = json.loads(out.read_text(encoding="utf-8"))
|
|
self.assertEqual([[w["w"] for w in r] for r in data["lines"]], [["Hello", "world"], ["Again"]])
|
|
self.assertEqual(data["lines"][1][0]["start"], 3.0)
|
|
|
|
def test_text_without_sung_lines_is_refused(self):
|
|
with tempfile.TemporaryDirectory() as d:
|
|
lyrics = Path(d) / "l.txt"
|
|
lyrics.write_text("[Intro]\n\n", encoding="utf-8")
|
|
self.assertEqual(fa.main([str(lyrics), "x.mp3", "--out", str(Path(d) / "a.json")]), 1)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|