199 lines
6.6 KiB
Python
199 lines
6.6 KiB
Python
"""Unit tests for UltraStar writer and reader round-trip."""
|
|
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
from unmix.export.ultrastar import export_ultrastar, import_ultrastar, parse_ultrastar, ultrastar_to_vocals
|
|
from unmix.schema.meta import MetaDocument, SourceFileInfo
|
|
from unmix.schema.tempo import TempoAnchor, TempoDocument
|
|
from unmix.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:120.00" in content
|
|
assert "#GAP:500" in content # 0.5s = 500 ms
|
|
assert "#LANGUAGE:German" in content
|
|
assert "stems/vocals.wav" not in content
|
|
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[0].start_sec == pytest.approx(0.5, abs=0.02)
|
|
assert line1[0].end_sec == pytest.approx(1.0, abs=0.02)
|
|
|
|
assert line1[1].text == "lo "
|
|
assert line1[1].midi == 62.0
|
|
assert line1[1].start_sec == pytest.approx(1.0, abs=0.02)
|
|
assert line1[1].end_sec == pytest.approx(2.0, abs=0.02)
|
|
|
|
line2 = parsed.lines[1]
|
|
assert len(line2) == 1
|
|
assert line2[0].text == "Welt"
|
|
assert line2[0].midi == 64.0
|
|
assert line2[0].start_sec == pytest.approx(3.0, abs=0.02)
|
|
assert line2[0].end_sec == pytest.approx(4.5, abs=0.02)
|
|
|
|
|
|
def test_ultrastar_metadata_and_language_english(tmp_path: Path):
|
|
song_dir = tmp_path / "song_pkg"
|
|
song_dir.mkdir(parents=True, exist_ok=True)
|
|
|
|
meta = MetaDocument(
|
|
title="Chasing Cars",
|
|
artist="Snow Patrol",
|
|
language="en",
|
|
duration_sec=269.0,
|
|
source_file=SourceFileInfo(
|
|
filename="Chasing_Cars.mp3", path="/tmp/Chasing_Cars.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))
|
|
|
|
# Tempo anchor starting at 65.34s (reproducing Chasing Cars test scenario)
|
|
tempo = TempoDocument(anchors=[TempoAnchor(sec=65.34, beat=0.0, bpm=104.0)])
|
|
(song_dir / "tempo.json").write_text(tempo.model_dump_json(indent=2))
|
|
|
|
# Vocals starting at 7.08s, ending at 258.7s
|
|
vocals = VocalsDocument(
|
|
lines=[
|
|
VocalLine(
|
|
start=7.08,
|
|
end=10.0,
|
|
syllables=[
|
|
VocalSyllable(text="I'll ", start=7.08, end=8.0, midi=52.0),
|
|
VocalSyllable(text="do ", start=8.0, end=9.0, midi=52.0),
|
|
VocalSyllable(text="it ", start=9.0, end=10.0, midi=52.0),
|
|
],
|
|
),
|
|
VocalLine(
|
|
start=250.0,
|
|
end=258.7,
|
|
syllables=[
|
|
VocalSyllable(text="cars", start=250.0, end=258.7, midi=50.0),
|
|
],
|
|
),
|
|
]
|
|
)
|
|
(song_dir / "vocals.json").write_text(vocals.model_dump_json(indent=2))
|
|
|
|
txt_path = export_ultrastar(song_dir)
|
|
content = txt_path.read_text(encoding="utf-8")
|
|
|
|
assert "#TITLE:Chasing Cars" in content
|
|
assert "#ARTIST:Snow Patrol" in content
|
|
assert "#LANGUAGE:English" in content
|
|
assert "#MP3:Chasing_Cars.mp3" in content
|
|
assert "#GAP:7080" in content
|
|
assert "#BPM:104.00" in content
|
|
|
|
parsed = parse_ultrastar(txt_path)
|
|
assert parsed.bpm == 104.0
|
|
assert parsed.gap_ms == 7080.0
|
|
# First note must be at 7.08s
|
|
assert parsed.lines[0][0].start_sec == pytest.approx(7.08, abs=0.05)
|
|
# Last note must be around 250.0s to 258.7s (NOT compressed to 52s!)
|
|
# Quantization resolution at 104 BPM is 15 / 104 = 0.144s per tick
|
|
last_note = parsed.lines[1][0]
|
|
assert last_note.start_sec == pytest.approx(250.0, abs=0.15)
|
|
assert last_note.end_sec == pytest.approx(258.7, abs=0.15)
|
|
|
|
|
|
def test_ultrastar_to_vocals_and_import(tmp_path: Path):
|
|
txt_content = """#TITLE:Imported Song
|
|
#ARTIST:Imported Artist
|
|
#BPM:120
|
|
#GAP:1000
|
|
: 0 4 0 Hel-
|
|
: 4 4 2 lo
|
|
* 8 8 4 World
|
|
- 16
|
|
: 20 4 0 Again
|
|
E
|
|
"""
|
|
txt_file = tmp_path / "song.txt"
|
|
txt_file.write_text(txt_content, encoding="utf-8")
|
|
|
|
song = parse_ultrastar(txt_file)
|
|
doc = ultrastar_to_vocals(song)
|
|
|
|
assert len(doc.lines) == 2
|
|
l0 = doc.lines[0]
|
|
assert len(l0.syllables) == 3
|
|
assert l0.syllables[0].text == "Hel-"
|
|
assert l0.syllables[0].word_end is False
|
|
assert l0.syllables[1].text == "lo"
|
|
assert l0.syllables[1].word_end is True
|
|
assert l0.syllables[2].text == "World"
|
|
assert l0.syllables[2].golden is True
|
|
assert l0.syllables[2].type == "golden"
|
|
|
|
# Test import_ultrastar creates vocals.json
|
|
out_dir = tmp_path / "imported_pkg"
|
|
vocals_json = import_ultrastar(txt_file, out_dir)
|
|
assert vocals_json.exists()
|
|
|
|
with open(vocals_json, encoding="utf-8") as vf:
|
|
loaded = VocalsDocument.model_validate_json(vf.read())
|
|
assert len(loaded.lines) == 2
|
|
assert loaded.lines[0].syllables[2].type == "golden"
|
|
|