forked from napulen/AugmentedNet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dcml2rntxt.py
183 lines (149 loc) · 5.53 KB
/
dcml2rntxt.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
import music21
from harmalysis.parsers.roman import parse as parse_rn
import re
from AugmentedNet.data.mps import (
annotation_score_duples as ANNOTATIONSCOREDUPLES,
)
def _isInitialKey(token):
# return re.match(r"\.[a-gA-G][b#]*\.[b#]*[ivIV]+", token)
return re.match(r"[a-gA-G][b#]*\..*", token)
def _isKeyChange(token):
# return re.match(r"[.]?[b#]*[ivIV]+\.[b#]*[ivIV]+", token)
# return re.match(r"[.]?[b#]*[ivIV]+\..*", token)
return re.match(r"[b#]*[ivIV]+\..*", token)
def _isHalfDiminishedSeventh(token):
return "%" in token
def _isPhraseBoundaryV2(token):
return "{" in token or "}" in token
def _isPhraseEnding(token):
return "\\" in token
def _isCadenceEnding(token):
return "\\\\" in token
def _isCad64(token):
return token == "V(64)"
def _hasAlternateReading(token):
return "-" in token
def _hasSquareBracket(token):
return "[" in token or "]" in token
def _hasAddedIntervals(token):
return re.match(".*\(.*\).*", token)
def _isAugmentedSixth(token):
return [aug6 for aug6 in ["Ger", "It", "Fr"] if aug6 in token]
def _isDiminishedSeventh(token):
return "#viio" in token
def _processABCToken(token, globalKey):
key = None
figure = token[1:] if token.startswith(".") else token
if _isInitialKey(figure):
keyStr, figure = figure.split(".")
key = music21.key.Key(keyStr).tonicPitchNameWithCase
if _isKeyChange(figure):
keyStr, figure = figure.split(".")
# key = music21.roman.RomanNumeral(keyStr, globalKey).root().name
natmin = "_nat" if globalKey.islower() else ""
rnobj = parse_rn(f"{globalKey}{natmin}:{keyStr}")
key = str(rnobj.chord.root)
if keyStr.islower():
key = key.lower()
if _hasAlternateReading(figure):
# The preferred reading should be the first one
figure = figure.split("-")[0]
if _isHalfDiminishedSeventh(figure):
figure = figure.replace("%", "ø")
if _isCadenceEnding(figure):
figure = figure.replace("\\\\", "")
if _isPhraseEnding(figure):
figure = figure.replace("\\", "")
if _isPhraseBoundaryV2(figure):
figure = figure.replace("{", "").replace("}", "")
if _isCad64(figure):
figure = "Cad64"
if _hasSquareBracket(figure):
figure = figure.replace("]", "").split("[")[0]
if _hasAddedIntervals(figure):
figure = re.sub(r"\(.*\)", "", figure)
if _isAugmentedSixth(figure):
# figure = figure[1:] if figure.startswith(".") else figure
figure = figure.replace("Ger6", "Ger65")
figure = figure.replace("Fr6", "Fr65")
if _isDiminishedSeventh(figure):
figure = figure.replace("#", "")
if key:
key = key.replace("-", "b")
return figure, key
def _measureDict(m21Score):
tss = {}
ms = {}
globalKey = None
for harm in m21Score.flat.getElementsByClass("Harmony"):
m = harm.measureNumber
b = round(float(harm.beat), 2)
b = int(b) if b.is_integer() else b
annotation = harm.chordKindStr
if annotation in ["@none", "{", "}"]:
continue
if m not in ms:
ms[m] = {}
# print(annotation)
figure, key = _processABCToken(annotation, globalKey)
if key and not globalKey:
globalKey = key
if b not in ms[m]:
ms[m][b] = (key, figure)
else:
print(f"Collission! m{m} b{b}")
# if key:
# print(f"{annotation} -> {key}:{figure}")
# else:
# print(f"{annotation} -> {figure}")
for ts in m21Score.flat.getElementsByClass("TimeSignature"):
m = ts.measureNumber
tss[m] = ts.ratioString
return tss, ms
def makeRntxtHeader(metadata, abc_or_mps="mps"):
if abc_or_mps == "abc":
analyst = (
"Neuwirth et al. ABC dataset. See https://github.com/DCMLab/ABC"
)
else:
analyst = "Hentschel et al. MPS dataset. See https://github.com/DCMLab/mozart_piano_sonatas"
composer = metadata.composer
title = metadata.title
movementNumber = metadata.movementNumber
movementName = metadata.movementName
header = f"Composer: {composer}\n"
header += f"Title: {title} - {movementNumber}: {movementName}\n"
header += f"Analyst: {analyst}\n"
header += f"Proofreader: Automated translation by Néstor Nápoles López\n"
return header
def makeRntxtBody(tss, ms):
body = ""
allMeasures = list(sorted(set(list(tss.keys()) + list(ms.keys()))))
for m in allMeasures:
if m in tss:
body += f"\nTime Signature: {tss[m]}\n\n"
if m in ms:
line = f"m{m} "
for b, (key, rn) in ms[m].items():
beat = f"b{b} " if b != 1 else ""
if key:
key = key.replace("-", "b")
line += f"{beat}{key}: {rn} "
else:
line += f"{beat}{rn} "
if re.match(r"m(\d)+ $", line):
continue
body += f"{line[:-1]}\n"
return body
if __name__ == "__main__":
for filename, (annotation, score) in ANNOTATIONSCOREDUPLES.items():
score = score.replace(".mscx", ".mxl")
print(score)
harm = music21.converter.parse(score)
tss, ms = _measureDict(harm)
abc_or_mps = "abc" if filename.startswith("abc") else "mps"
rntxtHeader = makeRntxtHeader(harm.metadata, abc_or_mps)
rntxtBody = makeRntxtBody(tss, ms)
with open(annotation, "w") as fd:
fd.write(rntxtHeader)
fd.write(rntxtBody)