-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChordsToChromaVectors.py
257 lines (225 loc) · 7.97 KB
/
ChordsToChromaVectors.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
#############################################################################
# ChordsToChromaVectors.py
# Jérôme Nika, IRCAM STMS LAB
# copyleft 2018
#############################################################################
#############################################################################
# TODO: Doc
#############################################################################
"""
Chords to chroma vectors
===================
Tools to handle chord labels (using Mirex notation) and convert them into "binary chroma vectors".
"""
from math import *
###############################################
# Global variables for modes
# 'bin_chroma / weigth' and 'bin_chroma / root'
max_weight = 1.5
min_weight = 1
max_num_notes = 5
###############################################
# Chris Harte phd thesis, p. 103
chordtype_to_rel_tone_vec = {}
#Triad chords
chordtype_to_rel_tone_vec[''] = [1, 3, 5]
chordtype_to_rel_tone_vec['maj'] = [1, 3, 5]
chordtype_to_rel_tone_vec['min'] = [1, 2.5, 5]
chordtype_to_rel_tone_vec['dim'] = [1, 2.5, 4.5]
chordtype_to_rel_tone_vec['aug'] = [1, 3, 5.5]
#Seventh chords
chordtype_to_rel_tone_vec['maj7'] = [1, 3, 5, 7]
chordtype_to_rel_tone_vec['min7'] = [1, 2.5, 5, 6.5]
chordtype_to_rel_tone_vec['7'] = [1, 3, 5, 6.5]
chordtype_to_rel_tone_vec['dim7'] = [1, 2.5, 4.5, 6]
chordtype_to_rel_tone_vec['hdim7'] = [1, 2.5, 4.5, 6.5]
chordtype_to_rel_tone_vec['hdim'] = [1, 2.5, 4.5, 6.5]
chordtype_to_rel_tone_vec['minmaj7'] = [1, 2.5, 5, 7]
#Six chords
chordtype_to_rel_tone_vec['maj6'] = [1, 3, 5, 6]
chordtype_to_rel_tone_vec['6'] = [1, 3, 5, 6]
chordtype_to_rel_tone_vec['min6'] = [1, 2.5, 5, 6]
#Extended chords
chordtype_to_rel_tone_vec['9'] = [1, 3, 5, 6.5, 9]
chordtype_to_rel_tone_vec['maj9'] = [1, 3, 5, 7, 9]
chordtype_to_rel_tone_vec['min9'] = [1, 2.5, 5, 6.5, 9]
#Triad chords
chordtype_to_rel_tone_vec['sus2'] = [1, 2, 5]
chordtype_to_rel_tone_vec['sus4'] = [1, 4, 5]
cum_int_tones = [0, 1, 2, 2.5, 3.5, 4.5, 5.5, 6]
chordtype_to_rel_semitone_vec = {}
vec = []
for chord_type, int_vec in chordtype_to_rel_tone_vec.items():
chordtype_to_rel_semitone_vec[chord_type] = [
int((cum_int_tones[(int(ceil(i)-1)) % 7]-i % 1) * 2)
for i in int_vec]
def rel_semitone_vec_to_rel_chroma_vec(input_dict,
mode='bin'):
chordtype_to_rel_bin_chroma_vec = {}
for chord_type, int_vec in input_dict.items():
bin_chroma_vec = [0] * 12
c = 0
w = len(int_vec)
for i in int_vec:
if mode == 'bin':
bin_chroma_vec[i % 12] = 1
elif mode == 'weight':
bin_chroma_vec[i % 12] = (max_weight
- c * (max_weight-min_weight)
/ max_num_notes)
elif mode == 'root':
if c == 0:
bin_chroma_vec[i % 12] = max_weight
else:
bin_chroma_vec[i % 12] = 1
c += 1
chordtype_to_rel_bin_chroma_vec[chord_type] = bin_chroma_vec
return chordtype_to_rel_bin_chroma_vec
def normalized_note(note):
'''
Return the normalized version of the input note to avoid dealing with
several representation of the same note
Parameters
----------
note: str
The input note. Must be lower case only, or it will not be normalized.
Returns
-------
normalized_note: str
The normalized note.
'''
normalized_note = note
if note == 'Db':
normalized_note = 'C#'
elif note == 'D#':
normalized_note = 'Eb'
elif note == 'Gb':
normalized_note = 'F#'
elif note == 'Ab':
normalized_note = 'G#'
elif note == 'A#':
normalized_note = 'Bb'
elif note == 'E#':
normalized_note = 'F'
elif note == 'Fb':
normalized_note = 'E'
elif note == 'B#':
normalized_note = 'C'
elif note == 'Cb':
normalized_note = 'B'
elif note == 'N':
normalized_note = 'N'
elif note == 'X':
normalized_note = 'X'
return normalized_note
def delta_root(n1,n2):
l = ['C', 'C#', 'D', 'Eb', 'E', 'F', 'F#', 'G', 'G#', 'A', 'Bb', 'B']
p1 = l.index(normalized_note(n1))
p2 = l.index(normalized_note(n2))
return ((p2-p1+5) % 12)-5
def parse_mir_label(label):
'''
Split a input chord between its root and type and normalize it.
Parameters
----------
label: str
The input chord.
The chord must be written as follow: 'root:type'.
Returns
-------
label: list of string
The root and type of the normalized input chord.
'''
#label = label.lower()
label = label.split(':')
if len(label) == 1:
if label[0] == 'N':
return ['N', 'N']
label += ['maj']
return label
def parse_mir_label_root(label):
'''
Split a input chord between its root and type and normalize it.
Parameters
----------
label: str
The input chord.
The chord must be written as follow: 'root:type'.
Returns
-------
label: list of string
The root and type of the normalized input chord.
'''
#label = label.lower()
label = label.split('/')
if len(label) == 1:
return [label[0], 'N']
return label
def mir_label_to_semitones_vec(label):
'''
Change the input chord into a vector containing a number for each note of
the chord corresponding to its position in the semitone scale
(c = 0, c# = 1 etc...)
Parameters
----------
label: str
The input chord.
The chord must be written as follow: 'root:type'.
Returns
-------
semi_vec: list of int
The chord described as numbers representing the notes.
Notes
-----
The function can only transform chords whose types are defined in the
chord_type_to_rel_tone_vec dictionnary.
'''
root, chord_type = parse_mir_label(label)
if root == 'N':
return [0]*4
vec = chordtype_to_rel_semitone_vec[chord_type]
delta = delta_root(root, 'C')
semi_vec = [(i-delta) % 12 for i in vec]
return semi_vec
def rotate(L, x):
return L[-x%len(L):] + L[:-x%len(L)]
def mir_label_to_bin_chroma_vec(label,
mode = 'bin'):
root, chord_type = parse_mir_label(label)
if root == 'N':
return [0]*12
chordtype_to_rel_bin_chroma_vec = rel_semitone_vec_to_rel_chroma_vec(
chordtype_to_rel_semitone_vec,
mode)
vec = chordtype_to_rel_bin_chroma_vec[chord_type]
delta = delta_root(root, 'C')
return rotate(vec,-delta)
def list_mir_label_to_list_vec(list_label,
mode='bin_chroma',
chroma_mode='bin'):
list_vec = []
for l in list_label:
if mode == 'bin_chroma' or mode is None:
list_vec.append(mir_label_to_bin_chroma_vec(l, chroma_mode))
elif mode == 'semitones':
list_vec.append(mir_label_to_semitones_vec(l))
return list_vec
def mir_labels_file_to_list_vec(in_file_path,
mode='bin_chroma',
chroma_mode='bin'):
with open(in_file_path,'r') as f:
i = 0
lines = f.read().splitlines()
for line in lines:
line.replace('\n', '')
return list_mir_label_to_list_vec(lines, mode, chroma_mode)
def write_list_vec_from_mir_labels_file(in_file_path,
out_file_path = None,
mode = 'bin_chroma',
chroma_mode = 'bin'):
list_vec = mir_labels_file_to_list_vec(in_file_path, mode, chroma_mode)
if out_file_path is None:
out_file_path = in_file_path + '_out'
with open(out_file_path, 'w') as f:
for vec in list_vec:
f.write(format(vec) + '\n')