-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
55 lines (45 loc) · 1.4 KB
/
main.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
from music21 import *
import math
def getDurationIndex(el):
return max(0, int(math.log2(4 / el.duration.quarterLength)))
filePath = input("Enter MusicXML file path: ")
song = converter.parse(filePath, forceSource=True)
# process the ties
song = song.stripTies()
# unfold repetitions
i = 0
for a in song:
if a.isStream:
song[i] = repeat.Expander(a).process()
i += 1
result = ""
for el in song.recurse().notesAndRests:
if el.duration.isGrace:
# ignore grace
continue
elif el.isNote:
octave = el.pitch.octave
if el.pitch.accidental is not None and el.pitch.accidental.modifier == "#":
# sharp note
if el.pitch.step == "B":
# convert B#x to C(x+1)
step = "c"
octave += 1
else:
step = el.pitch.step
elif el.pitch.accidental is not None and el.pitch.accidental.modifier == "-":
# flat note
# normalize to equivalent sharp note
step = el.pitch.transpose(-1).step
else:
# natural note or without accidental
step = el.pitch.step.lower()
duration = getDurationIndex(el)
result += f"{step}{octave}{duration} "
elif el.isRest:
duration = getDurationIndex(el)
result += f"-={duration} "
else:
print("UNKNOWN ELEMENT", el)
exit(1)
print(result)