-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmusescore2nbs.py
298 lines (259 loc) · 12.6 KB
/
musescore2nbs.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
# This file is a part of:
# ‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
# ███▄▄▄▄ ▀█████████▄ ▄████████ ███ ▄██████▄ ▄██████▄ ▄█
# ███▀▀▀██▄ ███ ███ ███ ███ ▀█████████▄ ███ ███ ███ ███ ███
# ███ ███ ███ ███ ███ █▀ ▀███▀▀██ ███ ███ ███ ███ ███
# ███ ███ ▄███▄▄▄██▀ ███ ███ ▀ ███ ███ ███ ███ ███
# ███ ███ ▀▀███▀▀▀██▄ ▀███████████ ███ ███ ███ ███ ███ ███
# ███ ███ ███ ██▄ ███ ███ ███ ███ ███ ███ ███
# ███ ███ ███ ███ ▄█ ███ ███ ███ ███ ███ ███ ███▌ ▄
# ▀█ █▀ ▄█████████▀ ▄████████▀ ▄████▀ ▀██████▀ ▀██████▀ █████▄▄██
# __________________________________________________________________________________
# NBSTool is a tool to work with .nbs (Note Block Studio) files.
# Author: IoeCmcomc (https://github.com/IoeCmcomc)
# Programming language: Python
# License: MIT license
# Source codes are hosted on: GitHub (https://github.com/IoeCmcomc/NBSTool)
# ‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
import zipfile
from asyncio import sleep
from functools import lru_cache
from os.path import basename
from lxml import etree
from common import MIDI_DRUMS, MIDI_INSTRUMENTS
from nbsio import Layer, NbsSong, Note
MIDI_DRUMS_BY_MIDI_PITCH = {obj.pitch: obj for obj in MIDI_DRUMS}
expandMulDict = {
"64th": 4,
"32nd": 2,
}
tupletMulDict = {
"64th": 6,
"32nd": 5,
"16th": 4,
"eighth": 3,
"quarter": 2,
}
durationMap = {
"128th": 0.125,
"64th": 0.25,
"32nd": 0.5,
"16th": 1,
"eighth": 2,
"quarter": 4,
"half": 8,
"whole": 16,
}
MAX_TEMPO = 60
class FileError(Exception):
pass
@lru_cache
def fraction2length(fraction: str) -> int:
'''It takes a string in the form of a fraction, and returns the length of
the note in 16th notes
Parameters
----------
fraction : str
str
Returns
-------
The length of the note in 16th notes.
'''
if isinstance(fraction, str):
parts = fraction.split('/')
return int(parts[0]) * int(16 / int(parts[1]))
return 0
async def musescore2nbs(filepath: str, expandMultiplier=1, autoExpand=True, dialog=None) -> NbsSong:
"""Convert a MuseScore file and return a NbsSong instance.
if the conversation fails, this function returns None.
Args:
- filepath: The path of the input file. .mscz and .mscx files are supported.
- expandMultiplier: Multiplys all note positions by this variable.
The default is 1, meaning not multiplying
- autoExpand: Optional; If it's True, the expand multiplier will be detected automatically.
- dialog: Optional; The ProgressDialog to be used for reporting progress.
Return:
A NbsSong contains meta-information and notes' data (position, pitch, velocity
and tuning).
"""
if autoExpand:
expandMultiplier = 1
# Reads the input file
xml = None
if filepath.endswith(".mscz"):
with zipfile.ZipFile(filepath, 'r') as zip:
filename: str = ""
for name in zip.namelist():
if name.endswith(".mscx"):
filename = name
break
if filename:
with zip.open(filename) as file:
xml = etree.parse(file, parser=None)
else:
raise FileError("This file isn't a MuseScore file or it doesn't contain a MuseScore file.")
elif filepath.endswith(".mscx"):
xml = etree.parse(filepath, parser=None)
else:
raise FileError("This file isn't a MuseScore file")
if version := xml.findtext('programVersion'):
if not (version.startswith('3') or version.startswith('4')):
raise NotImplementedError("This file is created by a older version of MuseScore. Please use MuseScore 3 or 4 to re-save the files before importing.")
nbs: NbsSong = NbsSong()
# Get meta-information
header = nbs.header
score = xml.find("Score")
header.import_name = basename(filepath)
header.name = (score.xpath("metaTag[@name='workTitle']/text()") or ('',))[0]
header.author = (score.xpath("metaTag[@name='arranger']/text()") or ('',))[0]
header.orig_author = (score.xpath("metaTag[@name='composer']/text()") or ('',))[0]
if timeSign := score.findtext("Staff/Measure/voice/TimeSig/sigN"):
header.time_sign = int(timeSign)
if tempoTxt := score.findtext("Staff/Measure/voice/Tempo/tempo"):
bpm: float = 60 * float(tempoTxt)
tps: float = bpm * 4 / 60
header.tempo = tps
if dialog:
dialog.currentProgress.set(20)
await sleep(0.001)
# Remove empty layers
emptyStaffs: list = []
staffCount = 0
for staff in score.iterfind("Staff"):
staffCount += 1
for elem in staff.xpath("Measure/voice/*"):
if elem.tag == "Chord":
break
else:
staffId = int(staff.get("id"))
emptyStaffs.append(staffId)
# Get layer instruments from staff program IDs
staffInsts = {}
for part in score.iterfind("Part"):
isPerc = bool(part.xpath("Instrument[@id='drumset']")) \
or bool(part.xpath("Staff/StaffType[@group='percussion']"))
program = int(part.find("Instrument/Channel/program").get("value"))
for staff in part.iterfind("Staff"):
staffId = int(staff.get("id"))
if staffId not in emptyStaffs:
# INST_INFO[-1] is the percussion (drumset) instrument
staffInsts[staffId] = \
MIDI_INSTRUMENTS[program] if not isPerc else MIDI_INSTRUMENTS[-1]
tempo: float = header.tempo
if dialog:
dialog.currentProgress.set(30)
await sleep(0.001)
# Perform note auto-expanding (if specified) and tuplet detection
hasComplexTuplets = False
for elem in score.xpath("Staff/Measure/voice/*"):
if elem.tag == "Tuplet":
normalNotes = int(elem.findtext("normalNotes"))
actualNotes = int(elem.findtext("actualNotes"))
if not hasComplexTuplets:
hasComplexTuplets = actualNotes != normalNotes
if hasComplexTuplets and autoExpand and (baseNote := elem.findtext("baseNote")):
if baseNote in tupletMulDict:
multiplier = max(tupletMulDict[baseNote], expandMultiplier)
if (tempo * multiplier) <= MAX_TEMPO:
expandMultiplier = multiplier
if autoExpand and (duration := elem.findtext("durationType")):
if duration in expandMulDict:
multiplier = max(expandMulDict[duration], expandMultiplier)
if (tempo * multiplier) <= MAX_TEMPO:
expandMultiplier = multiplier
header.tempo = int(tempo * expandMultiplier)
header.time_sign *= expandMultiplier
if dialog:
dialog.currentProgress.set(40)
await sleep(0.001)
# Import note data
baseLayer = -1
ceilingLayer = baseLayer
for staff in score.iterfind("Staff"):
staffId = int(staff.get("id"))
if staffId in emptyStaffs:
continue
baseLayer = ceilingLayer + 1
chords = 0
rests = 0
tick = 0
lastTick = -1
layer = -1
staffInst = staffInsts[staffId][1]
for measure in staff.iterfind("Measure"):
beginTick = tick
endTick = -1
innerBaseLayer = baseLayer
innerCeilingLayer = innerBaseLayer
for voi, voice in enumerate(measure.iterfind("voice")):
tick = beginTick
tick += fraction2length(voice.findtext("location/fractions")) * expandMultiplier
tupletNotesRemaining = 0
normalNotes = 0
actualNotes = 0
for elem in voice:
#print(f'{elem.tag=}, {tick=}, {tickCheckpoint=}')
dots = int(elem.findtext("dots") or 0)
if elem.tag == "Chord":
chords += 1
enoughSpace = int(tick) != int(lastTick)
# print(f'{int(lastTick)=} {int(tick)=} {enoughSpace=}')
for i, note in enumerate(elem.iterfind("Note")):
if voi > 0:
innerBaseLayer = innerCeilingLayer
if note.xpath("Spanner[@type='Tie']/prev"):
break
if not enoughSpace:
layer += 1
else:
layer = innerBaseLayer+i
if layer >= len(nbs.layers):
nbs.layers.append(Layer("{} (v. {})".format(staffInsts[staffId][0], voi+1), False, 100, 100))
inst = staffInst
isPerc = False
key = -1
if inst > -1:
key = int(note.find("pitch").text) - 21
else:
drumIndex = int(note.find("pitch").text)
_, _, inst, key = MIDI_DRUMS_BY_MIDI_PITCH.get(drumIndex, MIDI_DRUMS[27])
key += 36
isPerc = True
if inst == -1: inst = 0
tuning = note.find("tuning")
pitch = int(float(tuning.text)) if tuning is not None else 0
# TODO: Support relative velocity
vel = max(min(int(note.findtext("velocity") or 100), 127), 0)
nbs.notes.append(Note(int(tick), layer, inst, key, vel, 100, pitch))
ceilingLayer = max(ceilingLayer, layer)
innerCeilingLayer = max(innerCeilingLayer, i)
lastTick = tick
length = durationMap[elem.findtext("durationType")] * (2-(1/(2**dots))) * expandMultiplier
if hasComplexTuplets and (tupletNotesRemaining > 0):
length = length * normalNotes / actualNotes
tupletNotesRemaining -= 1
tick += length
elif elem.tag == "Rest":
rests += 1
durationType = elem.findtext("durationType")
length = 0
if durationType == "measure":
length = fraction2length(elem.findtext("duration")) * expandMultiplier
else:
length = int(durationMap[durationType] * (2-(1/(2**dots))) * expandMultiplier)
if hasComplexTuplets and (tupletNotesRemaining > 0):
length = length * normalNotes / actualNotes
tupletNotesRemaining -= 1
tick += length
elif (elem.tag == "Tuplet") and hasComplexTuplets:
normalNotes = int(elem.findtext("normalNotes"))
actualNotes = int(elem.findtext("actualNotes"))
tupletNotesRemaining = actualNotes
endTick = max(endTick, tick)
tick = round(endTick)
# print(f'{tick=}, {chords=}, {rests=}, {ceilingLayer=}')
if dialog:
dialog.currentProgress.set(40 + staffId * 40 / staffCount)
await sleep(0.001)
nbs.correctData()
return nbs