-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdotsm_converter.py
More file actions
221 lines (185 loc) · 6.6 KB
/
dotsm_converter.py
File metadata and controls
221 lines (185 loc) · 6.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# stepmania: audio is on the set, charts inherit it
# osu!mania: audio is on the chart
# IR: each chart gets audio
# Decisions:
# - We will turn roll notes into holds
# - We will ignore step-mania specifics things like mines for now
# - Perhaps they could be included as extra metadata e.g. set.charts[0].stepmania_data.mines
from collections import defaultdict
from enum import IntEnum, StrEnum
import config
class StepSet:
def __init__(
self,
title: str,
subtitle: str,
artist: str,
titletranslit: str,
literal_subtitle: str,
literal_artist: str,
genre: str,
music: str,
banner: str,
background: str,
samplestart: float,
offset: float,
timing_points,
breaks,
charts: list["StepChart"],
):
self.title = title
self.subtitle = subtitle
self.artist = artist
self.literal_title = titletranslit
self.literal_subtitle = literal_subtitle
self.literal_artist = literal_artist
self.genre = genre
self.music = music
self.banner = banner
self.background = background
self.samplestart = samplestart
self.offset = offset
self.breaks = breaks
self.charts = charts
class KeyColumn(IntEnum):
# TODO: are these names even right?
LEFT = 0
DOWN = 1
UP = 2
RIGHT = 3
class NoteType(StrEnum):
TAP = "tap"
HOLD = "hold"
class StepNote:
def __init__(
self,
note_type: NoteType,
key_column: KeyColumn,
start_time_ms: float,
# end time is only used for holds
end_time_ms: float | None = None,
) -> None:
self.note_type = note_type
self.key_column = key_column
self.start_time_ms = start_time_ms
self.end_time_ms = end_time_ms
class StepChart:
def __init__(
self,
key_type,
credit,
difficulty,
timing_points,
notes: list[StepNote],
):
self.key_type = key_type
self.credit = credit
self.difficulty = difficulty
self.timing_points = timing_points
self.notes = notes
class StepmaniaBPMChange:
# (stepmania-specific parsing helper)
def __init__(self, beat_num: float, bpm: float) -> None:
self.beat_num = beat_num
self.bpm = bpm
class StepmaniaNoteType(IntEnum):
EMPTY = 0
TAP_NOTE = 1
HOLD_NOTE_HEAD = 2
ROLL_NOTE_HEAD = 4
HOLD_ROLL_NOTE_TAIL = 3
# MINES = "M"
# AUTOKEYSOUNDS = "K"
# LIFTS = "L"
# FAKES = "F"
def parse_from_osu_chart_file(filepath: str) -> StepSet: ...
def parse_from_stepmania_chart_file(filepath: str) -> StepSet:
with open(filepath) as f:
metadata = f.read()
set_header_data, *set_charts_data = metadata.split("#NOTES:")
set_headers: dict = {}
for header in set_header_data.split("#"):
if not header:
continue
key, value = header.split(";")[0].split(":", maxsplit=1)
set_headers[key] = value
bpm_changes: list[StepmaniaBPMChange] = [
StepmaniaBPMChange(beat_num=float(beat_num), bpm=float(bpm))
for beat_num, bpm in [x.split("=") for x in set_headers["BPMS"].split(",")]
]
charts: list[StepChart] = []
for chart_data in "#NOTES:".join(set_charts_data).split("#"):
notedata_specs = [x.lstrip() for x in chart_data.split(":")]
key_type, credit, difficulty, chart_meter, radar_values, all_notes_data = (
notedata_specs
)
notes: list[StepNote] = []
ms_offset = float(set_headers["OFFSET"]) * 1000
open_hold_notes: dict[KeyColumn, list[StepNote]] = defaultdict(
list
) # support nested holds
for beat_num, measure_data in enumerate(all_notes_data.split(",\n"), start=1):
for row_data in (
measure_data.removesuffix("\n").removesuffix(";").split("\n")
):
column_notes = [StepmaniaNoteType(int(value)) for value in row_data]
for column_num, note_type in enumerate(column_notes):
key_column = KeyColumn(column_num)
if note_type is StepmaniaNoteType.EMPTY:
continue
elif note_type is StepmaniaNoteType.TAP_NOTE:
note = StepNote(
note_type=NoteType.TAP,
key_column=key_column,
start_time_ms=ms_offset,
)
elif (
note_type is StepmaniaNoteType.HOLD_NOTE_HEAD
# ignore rolls for now, only store them as holds
or note_type is StepmaniaNoteType.ROLL_NOTE_HEAD
):
note = StepNote(
note_type=NoteType.HOLD,
key_column=key_column,
start_time_ms=ms_offset,
)
open_hold_notes[key_column].append(note)
elif note_type is StepmaniaNoteType.HOLD_ROLL_NOTE_TAIL:
note = open_hold_notes[key_column].pop()
note.end_time_ms = ms_offset
else:
raise ValueError(f"Unknown note type: {note_type}")
notes.append(note)
current_bpm = bpm_changes[0].bpm
for change in bpm_changes[1:]:
if beat_num < change.beat_num:
break
current_bpm = change.bpm
ms_offset += 60000 / current_bpm
chart = StepChart(
key_type=key_type,
credit=credit,
difficulty=difficulty,
timing_points=set_headers["BPMS"],
notes=notes,
)
charts.append(chart)
return StepSet(
title=set_headers["TITLE"],
subtitle=set_headers["SUBTITLE"],
artist=set_headers["ARTIST"],
titletranslit=set_headers["TITLETRANSLIT"],
literal_subtitle=set_headers["SUBTITLETRANSLIT"],
literal_artist=set_headers["ARTISTTRANSLIT"],
genre=set_headers["GENRE"],
music=set_headers["MUSIC"],
banner=set_headers["BANNER"],
background=set_headers["BACKGROUND"],
samplestart=set_headers["SAMPLESTART"],
offset=set_headers["OFFSET"],
timing_points=set_headers["BPMS"],
breaks=set_headers["STOPS"],
charts=charts,
)
if __name__ == "__main__":
parse_from_stepmania_chart_file(config.TEST_SM_CHART_FILE_PATH)