feat(export): align UltraStar export with songpack spec (export/song.txt, word boundaries, headers)

This commit is contained in:
2026-11-02 12:22:03 +01:00
parent fd17b63c7a
commit e73c4ec328

View File

@ -82,6 +82,12 @@ def export_ultrastar(song_dir: Path) -> Path:
f"#LANGUAGE:{language}", f"#LANGUAGE:{language}",
] ]
# Pass through additional header fields from meta.ultrastar
if meta and hasattr(meta, "ultrastar") and meta.ultrastar:
for k, v in meta.ultrastar.items():
if k.upper() not in ["TITLE", "ARTIST", "MP3", "BPM", "GAP", "LANGUAGE"]:
out_lines.append(f"#{k.upper()}:{v}")
def sec_to_ultrastar_beat(sec: float) -> int: def sec_to_ultrastar_beat(sec: float) -> int:
rel_sec = max(0.0, sec - (gap_ms / 1000.0)) rel_sec = max(0.0, sec - (gap_ms / 1000.0))
if tempo and tempo.anchors: if tempo and tempo.anchors:
@ -91,12 +97,14 @@ def export_ultrastar(song_dir: Path) -> Path:
beats_per_sec = base_bpm / 60.0 beats_per_sec = base_bpm / 60.0
return max(0, int(round(rel_sec * beats_per_sec * 4.0))) return max(0, int(round(rel_sec * beats_per_sec * 4.0)))
tag_map = {"normal": ":", "golden": "*", "freestyle": "F", "rap": "R"}
current_beat = 0 current_beat = 0
for line_idx, line in enumerate(vocals.lines): for line_idx, line in enumerate(vocals.lines):
if not line.syllables: if not line.syllables:
continue continue
for syl in line.syllables: for syl_idx, syl in enumerate(line.syllables):
syl_start_beat = sec_to_ultrastar_beat(syl.start) syl_start_beat = sec_to_ultrastar_beat(syl.start)
syl_end_beat = sec_to_ultrastar_beat(syl.end) syl_end_beat = sec_to_ultrastar_beat(syl.end)
duration_beats = max(1, syl_end_beat - syl_start_beat) duration_beats = max(1, syl_end_beat - syl_start_beat)
@ -108,11 +116,15 @@ def export_ultrastar(song_dir: Path) -> Path:
current_beat = syl_start_beat + duration_beats current_beat = syl_start_beat + duration_beats
# UltraStar pitch relative to C4 (MIDI 60) # UltraStar pitch relative to C4 (MIDI 60)
pitch = int(round(syl.midi - 60.0)) midi_val = syl.midi if syl.midi is not None else 60.0
tag = "*" if syl.golden else ":" pitch = int(round(midi_val - 60.0))
tag = tag_map.get(getattr(syl, "type", "normal"), "*" if getattr(syl, "golden", False) else ":")
# Preserve trailing space or text # Preserve trailing space or text (don't add trailing space to the last syllable of a line)
txt = syl.text txt = syl.text
is_last_in_line = syl_idx == len(line.syllables) - 1
if getattr(syl, "word_end", False) and not txt.endswith(" ") and not is_last_in_line:
txt += " "
out_lines.append(f"{tag} {syl_start_beat} {duration_beats} {pitch} {txt}") out_lines.append(f"{tag} {syl_start_beat} {duration_beats} {pitch} {txt}")
# Add line break if not the last line # Add line break if not the last line
@ -124,10 +136,20 @@ def export_ultrastar(song_dir: Path) -> Path:
out_lines.append("E") out_lines.append("E")
out_lines.append("") out_lines.append("")
with open(output_txt, "w", encoding="utf-8") as f: content_str = "\n".join(out_lines)
f.write("\n".join(out_lines))
return output_txt # Save to export/song.txt per Songpack specification v1.0
export_dir = song_dir / "export"
export_dir.mkdir(parents=True, exist_ok=True)
export_txt = export_dir / "song.txt"
with open(export_txt, "w", encoding="utf-8") as f:
f.write(content_str)
# Also save to root song.txt for backward compatibility
with open(output_txt, "w", encoding="utf-8") as f:
f.write(content_str)
return export_txt
class UltraStarNote: class UltraStarNote:
@ -218,7 +240,7 @@ def parse_ultrastar(file_path: Path) -> UltraStarSong:
continue continue
# Note line: : 10 4 2 text # Note line: : 10 4 2 text
match = re.match(r"^([:*F])\s+(-?\d+)\s+(\d+)\s+(-?\d+)\s*(.*)$", line) match = re.match(r"^([:*FR])\s+(-?\d+)\s+(\d+)\s+(-?\d+)\s*(.*)$", line)
if match: if match:
ntype, s_beat, dur, pitch, text = match.groups() ntype, s_beat, dur, pitch, text = match.groups()
note = UltraStarNote( note = UltraStarNote(