-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathconvert.py
147 lines (123 loc) · 5.48 KB
/
convert.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
'''
Copy from convert
'''
# usage: python convert.py <source_path> <target_path> <components/shorthand>
import sys, os
import re
import numpy as np
import mir_eval
from mir_eval.chord import QUALITIES
from mir_eval.chord import EXTENDED_QUALITY_REDUX as EXTENDED
SCALE_DEGREES_INV = dict([(s, d) for d, s in mir_eval.chord.SCALE_DEGREES.items()])
def find_components(chords):
output_chords = []
for chord in chords:
root, quality, extra, bass = mir_eval.chord.split(chord)
quality, extend = mir_eval.chord.reduce_extended_quality(quality)
quality_bmap = mir_eval.chord.quality_to_bitmap(quality)
tmp = [i for i, x in enumerate(quality_bmap) if x == 1]
components = set()
for x in tmp:
if x in SCALE_DEGREES_INV.keys():
components.add(SCALE_DEGREES_INV[x])
else:
components.add('b' + SCALE_DEGREES_INV[x + 1])
if quality == "dim7":
components.remove("6")
components.add("bb7")
components = components | extend
for component in extra - components:
if component[0] in "*b#" and component[1:] in components:
components.remove(component[1:])
if component[0] in "b#":
components.add(component)
else:
components.add(component)
components = sorted(list(components), key=lambda x: int(re.search(r"\d+", x).group()))
output_chords.append(mir_eval.chord.join(root, "", components, bass))
return(np.array(output_chords))
def find_quality(bitmap, extra):
min_mismatch = 24
best_quality = None
isExtend = False
for quality in QUALITIES.keys():
num_mismatch = np.count_nonzero(bitmap != QUALITIES[quality]) + len(extra)
if num_mismatch < min_mismatch:
min_mismatch = num_mismatch
best_quality = quality
for extended_quality in EXTENDED.keys():
quality, extension = EXTENDED[extended_quality]
num_mismatch = np.count_nonzero(bitmap != QUALITIES[quality]) + len(extension ^ extra)
if num_mismatch < min_mismatch and extended_quality[0] != 'b' and extended_quality[0] != '#':
min_mismatch = num_mismatch
best_quality = extended_quality
isExtend = True
if not isExtend:
bitmap -= np.array(QUALITIES[best_quality])
new_extra = extra
else:
bitmap -= np.array(QUALITIES[EXTENDED[best_quality][0]])
new_extra = extra - EXTENDED[best_quality][1]
for degree in EXTENDED[best_quality][1] - extra:
new_extra.add('*' + str(degree))
for idx in range(bitmap.shape[0]):
if bitmap[idx] == 0:
continue
removed = '*' if bitmap[idx] == -1 else ''
if idx in SCALE_DEGREES_INV.keys():
new_extra.add(removed + SCALE_DEGREES_INV[idx])
elif idx + 1 in SCALE_DEGREES_INV.keys():
new_extra.add(removed + 'b' + SCALE_DEGREES_INV[idx + 1])
# clear dummpy degrees
for degree in range(1, 14): # two octaves
degree = str(degree)
if degree in new_extra or '*' + degree in new_extra or 'b' + degree in new_extra or '#' + degree in new_extra:
if degree in new_extra and '*' + degree in new_extra:
new_extra.remove(degree)
new_extra.remove('*' + degree)
if degree in new_extra and ('b' + degree in new_extra or '#' + degree in new_extra):
new_extra.remove(degree)
if '*' + degree in new_extra and ('b' + degree in new_extra or '#' + degree in new_extra):
new_extra.remove('*' + degree)
return best_quality, new_extra
def find_shorthand(chords):
output_chords = []
for chord in chords:
root, quality, extra, bass = mir_eval.chord.split(chord)
if quality or chord == mir_eval.chord.NO_CHORD:
output_chords.append(chord)
continue
bitmap = np.zeros(12)
extra_set = set()
for note in extra:
if int(re.search(r"\d+", note).group()) > 7:
extra_set.add(note)
else:
bitmap += mir_eval.chord.scale_degree_to_bitmap(note)
quality, extra = find_quality(bitmap, extra_set)
output_chord = mir_eval.chord.join(root, quality, extra, bass)
if '(' in output_chord:
tmp = sorted(output_chord.split('(')[1].split(')')[0].split(','), key=lambda x: int(re.search(r"\d+", x).group()))
output_chord = output_chord.split('(')[0]
output_chord += '('
for degree in tmp:
output_chord += degree + ','
output_chord = output_chord[:-1] + ')' if bass == "" or bass == '1' else output_chord[:-1] + ')' + '/' + bass
output_chords.append(output_chord)
return np.array(output_chords)
if __name__ == "__main__":
if len(sys.argv) != 4 or sys.argv[3] not in ("components", "shorthand"):
sys.exit("usage: python convert.py <source_path> <target_path> <components/shorthand>")
src_pth = sys.argv[1]
tar_pth = sys.argv[2]
mode = sys.argv[3]
with open(src_pth) as src_f:
labels = np.array([line.split() for line in src_f], dtype="<U64")
chords = labels[:, 2]
if mode == "components":
labels[:, 2] = find_components(chords)
else:
labels[:, 2] = find_shorthand(chords)
with open(tar_pth, "w") as tar_f:
for label in labels:
tar_f.write(f"{label[0]}\t{label[1]}\t{label[2]}\n")