"""從 osu 譜面匯入成三軌(L/M/R)譜面,作為製譜起點 - taiko:don → L、ka → R、大音符 → M;同色快速連打改為左右交替,避免單手連打; 轉盤 → M 的 hold、連打條 → slide - mania 4K:1-2 軌 → L、3-4 軌 → R、長條 → hold;同手和弦時其中一顆移到 M """ from chart import Chart, Note from osu_parse import parse_osu # 同一隻手連續兩下的最短間隔,比這更快就換手(ms) ALTERNATE_BELOW = 150 def read_sections(path): sections, cur = {}, None for raw in open(path, encoding="utf-8"): line = raw.strip() if not line or line.startswith("//"): continue if line.startswith("[") and line.endswith("]"): cur = line[1:-1] sections[cur] = [] elif cur: sections[cur].append(line) return sections def timing_info(sections): """第一個非繼承 timing point 的 BPM 與時間""" for l in sections.get("TimingPoints", []): p = l.split(",") uninherited = p[6] == "1" if len(p) > 6 else True if uninherited and float(p[1]) > 0: return 60000.0 / float(p[1]), int(float(p[0])) return 120.0, 0 def general(sections, key, default=""): for l in sections.get("General", []): if l.startswith(key + ":"): return l.split(":", 1)[1].strip() return default def import_taiko(path): sec = read_sections(path) bpm, offset = timing_info(sec) chart = Chart(general(sec, "AudioFilename"), round(bpm, 3), offset, 4) last = {"L": -10**9, "R": -10**9} prev_lane = None for o in parse_osu(path): t = o["time"] if o["kind"] == "spinner": chart.notes.append(Note(t, "M", "hold", o["end"])) continue if o["kind"] == "drumroll": lane = "R" if prev_lane == "L" else "L" chart.notes.append(Note(t, lane, "slide", o["end"])) last[lane] = o["end"] prev_lane = lane continue if o["big"]: chart.notes.append(Note(t, "M", "tap")) last["L"] = last["R"] = t prev_lane = None continue lane = "R" if o["kat"] else "L" other = "L" if lane == "R" else "R" # 同一隻手太快 → 換另一隻手 if t - last[lane] < ALTERNATE_BELOW and t - last[other] >= ALTERNATE_BELOW: lane = other chart.notes.append(Note(t, lane, "tap")) last[lane] = t prev_lane = lane chart.sort() return chart def import_mania4k(path): sec = read_sections(path) bpm, offset = timing_info(sec) chart = Chart(general(sec, "AudioFilename"), round(bpm, 3), offset, 4) busy_until = {"L": -10**9, "R": -10**9, "M": -10**9} for l in sec["HitObjects"]: p = l.split(",") col = min(3, int(int(p[0]) * 4 / 512)) t, typ = int(p[2]), int(p[3]) end = int(p[5].split(":")[0]) if typ & 128 else None lane = "L" if col <= 1 else "R" # 同手已被佔用(和弦或長條中)→ 移到中間欄,再不行就略過 if busy_until[lane] >= t: lane = "M" if busy_until["M"] < t else None if lane is None: continue chart.notes.append(Note(t, lane, "hold" if end else "tap", end)) busy_until[lane] = end if end else t chart.sort() return chart def import_osu(path): sec = read_sections(path) mode = general(sec, "Mode", "0") if mode == "1": return import_taiko(path) if mode == "3": return import_mania4k(path) raise ValueError("目前只支援 osu!taiko 與 osu!mania 4K 譜面")