feat(export): implement UltraStar writer and reader for song.txt

This commit is contained in:
2026-11-02 10:42:10 +01:00
parent ac9831d195
commit cd6f2a8fa3
3 changed files with 327 additions and 5 deletions

81
tests/test_ultrastar.py Normal file
View File

@ -0,0 +1,81 @@
"""Unit tests for UltraStar writer and reader round-trip."""
from pathlib import Path
from songsplit.export.ultrastar import export_ultrastar, parse_ultrastar
from songsplit.schema.meta import MetaDocument, SourceFileInfo
from songsplit.schema.tempo import TempoAnchor, TempoDocument
from songsplit.schema.vocals import VocalLine, VocalsDocument, VocalSyllable
def test_ultrastar_writer_and_reader(tmp_path: Path):
song_dir = tmp_path / "song_pkg"
song_dir.mkdir(parents=True, exist_ok=True)
# 1. Prepare meta.json
meta = MetaDocument(
title="Test Song",
artist="Test Artist",
language="de",
duration_sec=30.0,
source_file=SourceFileInfo(path="/tmp/test.mp3", sha256="abc", size_bytes=100),
created_at="2026-11-02T10:00:00Z",
)
(song_dir / "meta.json").write_text(meta.model_dump_json(indent=2))
# 2. Prepare tempo.json (120 BPM)
tempo = TempoDocument(anchors=[TempoAnchor(sec=0.5, beat=0.0, bpm=120.0)])
(song_dir / "tempo.json").write_text(tempo.model_dump_json(indent=2))
# 3. Prepare vocals.json
vocals = VocalsDocument(
lines=[
VocalLine(
start=0.5,
end=2.0,
syllables=[
VocalSyllable(text="Hal-", start=0.5, end=1.0, midi=60.0),
VocalSyllable(text="lo ", start=1.0, end=2.0, midi=62.0),
],
),
VocalLine(
start=3.0,
end=4.5,
syllables=[
VocalSyllable(text="Welt", start=3.0, end=4.5, midi=64.0),
],
),
]
)
(song_dir / "vocals.json").write_text(vocals.model_dump_json(indent=2))
# 4. Export to UltraStar song.txt
txt_path = export_ultrastar(song_dir)
assert txt_path.exists()
content = txt_path.read_text(encoding="utf-8")
assert "#TITLE:Test Song" in content
assert "#ARTIST:Test Artist" in content
assert "#BPM:480.00" in content # 120 * 4 = 480
assert "#GAP:500" in content # 0.5s = 500 ms
assert "E" in content
# 5. Parse back using parse_ultrastar
parsed = parse_ultrastar(txt_path)
assert parsed.title == "Test Song"
assert parsed.artist == "Test Artist"
assert parsed.bpm == 120.0
assert parsed.gap_ms == 500.0
assert len(parsed.lines) == 2
line1 = parsed.lines[0]
assert len(line1) == 2
assert line1[0].text == "Hal-"
assert line1[0].midi == 60.0
assert line1[1].text == "lo "
assert line1[1].midi == 62.0
line2 = parsed.lines[1]
assert len(line2) == 1
assert line2[0].text == "Welt"
assert line2[0].midi == 64.0