-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbmx2bmson.py
357 lines (264 loc) · 8.01 KB
/
bmx2bmson.py
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
#!/usr/bin/env python
# -*- coding: utf8 -*-
import os
import re
import sys
import json
import operator
import traceback
__author__ = "xert*"
__version__ = "0.3"
__bmsonversion__ = "1.0.0"
class bms2bmson:
@staticmethod
def ToBaseX(str, shift):
a = ord(str[0])
b = ord(str[1])
c = 0
c += a - ord('A') + 10 if (a >= ord('A') and a <= ord('Z')) else a - ord('0')
c *= shift
c += b - ord('A') + 10 if (b >= ord('A') and b <= ord('Z')) else b - ord('0')
return c
@staticmethod
def SaveBmson(jsondata):
try:
with open(sys.argv[2], "wb") as jf:
jf.write(jsondata)
return True
except Exception:
traceback.print_exc()
return False
@staticmethod
def LoadBMS(bmsfile):
bmsfilename = bmsfile
ext_formats = [".bms", ".bme", ".bml", ".pms"]
ext = os.path.splitext(os.path.basename(bmsfile))[1]
for ptr, format in enumerate(ext_formats):
if ext == format:
with open(bmsfile, "rb") as bmsdata:
return bmsdata.read()
return None
def ExportToJson(self):
bmson = {}
bmson["version"] = __bmsonversion__
bmson["info"] = self.BMSInfo
bmson["lines"] = self.lines
bmson["bpm_events"] = self.bpmnotes
bmson["stop_events"] = self.stopnotes
bmson["sound_channels"] = []
cnotes = {}
wavslen = len(self.wavHeader)
for i in xrange(wavslen):
cnotes[self.wavHeader[i]["ID"]] = []
for wn in self.notes:
if wn["id"] not in cnotes:
continue
n = {}
n["c"] = wn["channel"] > 30
if wn["channel"] is 1:
n["x"] = 0
else:
n["x"] = (wn["channel"]-10) % 20
n["y"] = wn["locate"]
n["l"] = wn["length"]
cnotes[wn["id"]].append(n)
for i in xrange(wavslen):
tempdict = {}
tempdict["name"] = self.wavHeader[i]["name"]
tempdict["notes"] = cnotes[self.wavHeader[i]["ID"]]
bmson["sound_channels"].append(tempdict)
bga = {}
bga["bga_header"] = self.bgaHeader
bga["bga_events"] = self.bbnotes
bga["layer_events"] = self.blnotes
bga["poor_events"] = self.bpnotes
bmson["bga"] = bga
bmson = json.dumps(bmson, ensure_ascii=False, sort_keys=True, indent=4)
self.SaveBmson(bmson)
def GetMetadata(self, bmsdata):
self.BMSInfo = { "title" : None,
"subtitle" : None,
"artist" : None,
"subartists" : None,
"genre" : None,
"mode_hint" : "beat-7k",
"chart_name" : None,
"level" : 0,
"init_bpm" : 0.0,
"total" : 100.0,
"back_image" : None,
"eyecatch_image" : None,
"banner_image" : None,
"preview_music" : None,
"resolation" : 240 }
self.wavHeader = []
self.bgaHeader = []
self.stopnum = []
self.bpmnum = []
tags = [ "ARTIST", "GENRE", "TITLE", "BPM", "TOTAL", "PLAYLEVEL" ]
extags = [ "WAV", "BMP", "BPM", "STOP" ]
for tag in tags:
value = re.search(r"#" + tag + "\s(.+)\r", bmsdata)
if value is not None:
value = value.group(1)
if tag is "PLAYLEVEL" and value is not None:
self.BMSInfo["level"] = int(value)
elif tag is "BPM" and value is not None:
self.BMSInfo["init_bpm"] = float(value)
elif tag is "TOTAL" and value is not None:
self.BMSInfo["total"] = float(value)
elif (tag is "TITLE") or (tag is "GENRE") or (tag is "ARTIST"):
self.BMSInfo[tag.lower()] = str(value)
else:
pass
for tag in extags:
value = re.findall(r"#" + tag + "([0-9A-Z]{2})\s(.+)\r", bmsdata)
if value is not None:
for v, parameter in value:
if tag is "WAV":
self.wavHeader.append({ "ID" : self.ToBaseX(v, 36), "name" : parameter })
elif tag is "BMP":
self.bgaHeader.append({ "ID" : self.ToBaseX(v, 36), "name" : parameter })
elif tag is "BPM":
self.bpmnum.append({ self.ToBaseX(v, 36) : float(parameter) })
elif tag is "STOP":
self.stopnum.append({ self.ToBaseX(v, 36) : int(parameter) })
return self.BMSInfo
def ReadBMSLines(self, bmsdata):
self.lineh = { i : 960 for i in xrange(1000) }
self.isln = { i : False for i in xrange(4096) }
self.lines = []
self.NotePre = []
self.linemax = 0
GlobalCounter = 0
bmslines = re.findall(r"#([0-9]{3})([0-9]{2}):(.+)\r", bmsdata)
for measure, channel, parameter in bmslines:
ch = int(channel)
ms = int(measure)
if ch >= 10 and ch < 70:
c = ch % 10
m = ch / 10
if c == 6: c = 8
elif c == 7: c = 9
elif c == 8: c = 6
elif c == 9: c = 7
ch = m * 10 + c
if ch == 2:
self.lineh[ms] = int(960 * float(parameter))
else:
paramlen = len(parameter) / 2
for j in xrange(paramlen):
paramsub = parameter[j*2:j*2+2]
nn = self.ToBaseX(paramsub, 16) if ch == 3 else self.ToBaseX(paramsub, 36)
if nn is not 0:
self.linemax = max([self.linemax, ms + 1])
self.NotePre.append({"x" : ch, "y" : 0, "n" : nn, "ms" : ms, "mm" : paramlen, "mc" : j})
y = 0
for i in xrange(self.linemax + 1):
self.lines.append({"y" : y})
y += self.lineh[i]
for i in xrange(len(self.NotePre)):
ms = self.NotePre[i]["ms"]
seq_y = (self.lines[ms+1]["y"] - self.lines[ms]["y"]) * self.NotePre[i]["mc"] / self.NotePre[i]["mm"]
self.NotePre[i]["y"] = self.lines[ms]["y"] + seq_y
self.NotePre = sorted(self.NotePre, key=lambda k: k['y'])
for i in xrange(len(self.NotePre)):
"""
Longnote Processor
"""
ch = self.NotePre[i]['x']
if (ch > 10 and ch < 50) and self.isln[self.NotePre[i]['n']]:
pln = i
while True:
pln = pln - 1
ch2 = self.NotePre[pln]['x']
if ch == ch2:
self.NotePre.append({ "x" : self.NotePre[pln]['x'],
"y" : self.NotePre[pln]['y'],
"n" : self.NotePre[pln]['n'],
"ms" : 0,
"mm" : 0,
"mc" : 0 })
self.NotePre[pln]['x'] = 0
self.NotePre[i]['x'] = 0
break
if (ch > 50 and ch < 70):
pln = i
while True:
pln = pln + 1
ch2 = self.NotePre[pln]['x']
if ch == ch2:
self.NotePre.append({ "x" : self.NotePre[i]['x'] - 40,
"y" : self.NotePre[i]['y'],
"n" : self.NotePre[i]['n'],
"ms" : 0,
"mm" : 0,
"mc" : 0 })
self.NotePre[pln]['x'] = 0
self.NotePre[i]['x'] = 0
break
TempNotePre = [r for r in self.NotePre if r['x'] != 0]
self.NotePre = sorted(TempNotePre, key=lambda k: k['y'])
self.SetNotes()
def SetNotes(self):
self.notes = []
self.bbnotes = []
self.blnotes = []
self.bpnotes = []
self.bpmnotes = []
self.stopnotes = []
for i, np in enumerate(self.NotePre):
if np['x'] in [4, 6, 7]:
bn = { 'y' : np['y'],
'id' : np['n'] }
if np['x'] == 4:
self.bbnotes.append(bn)
elif np['x'] == 6:
self.bpnotes.append(bn)
elif np['x'] == 7:
self.blnotes.append(bn)
if (np['x'] == 1) or (np['x'] / 10 >= 1) or (np['x'] / 10 <= 4):
n = { "channel" : np['x'],
"id" : np['n'],
"locate" : np['y'],
"length" : 0 }
self.notes.append(n)
else:
en = { "y" : np['y'] }
if np['x'] == 3:
en['v'] = float(np['n'])
self.bpmnotes.append(en)
elif np['x'] == 8:
en['v'] = self.bpmnum[np['n']]
self.bpmnotes.append(en)
elif np['x'] == 9:
en['v'] = self.stopnum[np['n']]
self.stopnotes.append(en)
def Convert(self, file):
bmsdata = self.LoadBMS(file)
self.GetMetadata(bmsdata)
self.ReadBMSLines(bmsdata)
print "[-] defined sound files : {}".format(len(self.wavHeader))
print "[-] defined bga/bgi files : {}".format(len(self.bgaHeader))
print "[-] total note count : {}".format(len(self.notes))
self.ExportToJson()
print "[+] done."
if __name__ == "__main__":
uinput = sys.argv[1]
bms = bms2bmson()
formats = (".bms", ".bme", ".bml", ".pms")
"""
if os.path.isdir(uinput):
for root, dirs, files in os.walk(uinput):
for file in filter(lambda x: x.endswith(formats), files):
targetbms = os.path.join(root, file)
try:
#print targetbms
meta = bms.Convert(targetbms)
#print meta
except:
pass
else:
meta = bms.Convert(uinput)
"""
bms.Convert(uinput)