/
reader.py
142 lines (101 loc) · 4.16 KB
/
reader.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
# -*- coding: utf-8 -*-
from lxml import etree
from mingus.core import notes, chords, mt_exceptions
import logging
class MusicXMLReader(object):
def __init__(self, filename, part=0):
self.__notes_depot = []
logging.debug("Parsing file %s" % filename)
try:
if isinstance(filename, basestring):
with open(filename) as fp:
t = etree.parse(fp)
else:
t = etree.parse(filename)
except (IOError, etree.XMLSyntaxError):
logging.error("Error reading file %s" % filename)
return None
parts = etree.ETXPath("//part")(t)
logging.debug("Found %s parts in the file, reading part #%s" % (len(parts), part))
if part > len(parts) - 1:
logging.error("Part %s not available" % part)
return None
for m in etree.ETXPath(".//measure")(parts[part]):
logging.debug("Reading measure %s" % m.attrib.get("number"))
for chunk in etree.ETXPath(".//note|harmony|direction")(m):
# for chunk in etree.ETXPath(".//direction")(m):
if chunk.tag == "note":
note = self.__parse_note(chunk)
if note:
self.__notes_depot.append(note)
elif chunk.tag == "direction":
chord = self.__try_to_parse_label(chunk)
if chord:
self.__dump_chord(chord)
elif chunk.tag == "harmony":
self.__dump_chord(self.__parse_harmony(chunk))
def __dump_chord(self, chord):
if self.__notes_depot:
logging.info("Notes in melody are: %s" % (", ".join(self.__notes_depot)))
self.__notes_depot = []
if chord:
try:
chord_tones = chords.from_shorthand(chord)
logging.info("\nChord is %s" % chord)
logging.info("Chord tones are: %s" % (", ".join(chord_tones)))
except mt_exceptions.FormatError:
logging.error("Error parsing chord symbol %s" % chord)
def __calc_alteration(self, alter):
if alter:
alter = int(alter[0].text)
return ("#" if alter > 0 else "b") * abs(alter)
return ""
def __parse_note(self, note):
step = etree.ETXPath("./pitch/step")(note)
alter = etree.ETXPath("./pitch/alter")(note)
n = ""
if step:
n = step[0].text
n += self.__calc_alteration(alter)
if n:
return notes.remove_redundant_accidentals(n)
def __unify_chord(self, label):
if u"(" not in label:
label = label.rstrip(u")")
if u")" not in label:
label = label.lstrip("u(")
label = label.replace(u"7sus4", u"sus47")
label = label.replace(u"m(7)", u"mM7")
return label
def __parse_harmony(self, chord):
step = etree.ETXPath("./root/root-step")(chord)
kind = etree.ETXPath("./kind")(chord)
alter = etree.ETXPath("./root/root-alter")(chord)
bass = etree.ETXPath("./bass/bass-step")(chord)
bass_alter = etree.ETXPath("./bass/bass-alter")(chord)
c = ""
if step:
c = step[0].text
c += self.__calc_alteration(alter)
if kind:
kind = kind[0].get("text")
if kind.startswith("o"):
kind = "dim" + kind[1:]
c += kind
if bass:
c += "/" + bass[0].text
c += self.__calc_alteration(bass_alter)
return self.__unify_chord(c)
def __try_to_parse_label(self, label):
words = etree.ETXPath("./direction-type/words")(label)
if words and words[0].get("default-y") == "100":
label = self.__unify_chord(words[0].text)
return label
if __name__ == "__main__":
import sys
import codecs
logging.basicConfig(level=logging.INFO)
# Printing to stdout in utf-8
sys.stdout = codecs.getwriter('utf-8')(sys.stdout)
MusicXMLReader("samples/musescore/L03.xml")
# print(parse_music_xml("samples/musescore/L04.xml"))