From e73c4ec3287a0ed5eaa44bb9d8aefff8fb0a3323 Mon Sep 17 00:00:00 2001 From: wlkns-dev Date: Mon, 2 Nov 2026 12:22:03 +0100 Subject: [PATCH] feat(export): align UltraStar export with songpack spec (export/song.txt, word boundaries, headers) --- src/songsplit/export/ultrastar.py | 38 ++++++++++++++++++++++++------- 1 file changed, 30 insertions(+), 8 deletions(-) diff --git a/src/songsplit/export/ultrastar.py b/src/songsplit/export/ultrastar.py index 0ca668f..59d55fb 100644 --- a/src/songsplit/export/ultrastar.py +++ b/src/songsplit/export/ultrastar.py @@ -82,6 +82,12 @@ def export_ultrastar(song_dir: Path) -> Path: 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: rel_sec = max(0.0, sec - (gap_ms / 1000.0)) if tempo and tempo.anchors: @@ -91,12 +97,14 @@ def export_ultrastar(song_dir: Path) -> Path: beats_per_sec = base_bpm / 60.0 return max(0, int(round(rel_sec * beats_per_sec * 4.0))) + tag_map = {"normal": ":", "golden": "*", "freestyle": "F", "rap": "R"} + current_beat = 0 for line_idx, line in enumerate(vocals.lines): if not line.syllables: continue - for syl in line.syllables: + for syl_idx, syl in enumerate(line.syllables): syl_start_beat = sec_to_ultrastar_beat(syl.start) syl_end_beat = sec_to_ultrastar_beat(syl.end) 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 # UltraStar pitch relative to C4 (MIDI 60) - pitch = int(round(syl.midi - 60.0)) - tag = "*" if syl.golden else ":" + midi_val = syl.midi if syl.midi is not None else 60.0 + 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 + 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}") # 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("") - with open(output_txt, "w", encoding="utf-8") as f: - f.write("\n".join(out_lines)) + content_str = "\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: @@ -218,7 +240,7 @@ def parse_ultrastar(file_path: Path) -> UltraStarSong: continue # 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: ntype, s_beat, dur, pitch, text = match.groups() note = UltraStarNote(