-
Notifications
You must be signed in to change notification settings - Fork 0
/
queries.py
365 lines (216 loc) · 7.28 KB
/
queries.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
import music21
from vis.analyzers.indexers import noterest
import numpy as np
import pprint as pp
import os
def getNotes(piece):
score = music21.converter.parse(piece)
notes = noterest.NoteRestIndexer(score).run()
for note in notes.columns:
notes = notes[note].tolist()
return notes
def getContour(size, notes):
notes = [x for x in notes if x != 'Rest']
contour = []
contour.append(notes[0])
if size == 'all':
size = len(notes)
for i in range(1, len(notes)):
if len(contour) < size and notes[i] != contour[len(contour)-1]:
contour.append(notes[i])
contour = list(map(music21.note.Note, contour))
return contour
def cont_num(contour):
cseg = [0] * len(contour)
for i in range(len(contour)):
notes = []
for x in range(len(contour)):
if (music21.interval.getAbsoluteHigherNote(contour[i], contour[x]) == contour[i]) and (contour[i].nameWithOctave != contour[x].nameWithOctave) and (contour[x].nameWithOctave not in notes):
notes.append(contour[x].nameWithOctave)
cseg[i] += 1
return cseg
def COM_matrix(contour):
com = []
for x in contour:
com.append([])
for i in range(len(contour)):
for x in range(len(contour)):
if contour[i] == contour[x]:
com[i].append("0")
elif contour[i] > contour[x]:
com[i].append("-")
else:
com[i].append("+")
return com
def compare(contour1, contour2):
count = 0
l = len(contour1)
for row in range(l):
for c1, c2 in zip(contour1[row], contour2[row]):
if c1 == c2:
count += 1
count = (count-l)/2
total = (l*(l-1))/2
return count/total
def compare_list(contours):
total = 0
for x in len(contours):
for y in len(contours):
if x is not y:
total += compare(contour[x], contour[y])
return total/len(contours)
def reduce_c(contour, depth):
def maxc(contour):
max_list = [0]
contour = list(map(music21.note.Note, contour))
for x in range(len(contour)-2):
if contour[x].nameWithOctave != contour[x+1].nameWithOctave:
if (music21.interval.getAbsoluteHigherNote(contour[x], contour[x+1]) == contour[x+1] and music21.interval.getAbsoluteHigherNote(contour[x+1], contour[x+2]) == contour[x+1]):
max_list.append(x+1)
max_list.append(len(contour)-1)
return max_list
def minc(contour):
min_list = [0]
contour = list(map(music21.note.Note, contour))
for x in range(len(contour)-2):
if contour[x].nameWithOctave != contour[x+1].nameWithOctave:
if (music21.interval.getAbsoluteLowerNote(contour[x], contour[x+1]) == contour[x+1] and music21.interval.getAbsoluteLowerNote(contour[x+1], contour[x+2]) == contour[x+1]):
min_list.append(x+1)
min_list.append(len(contour)-1)
return min_list
max2 = maxc(contour)
min2 = minc(contour)
while depth > 1:
maxcon = [contour[x] for x in max2]
mincon = [contour[x] for x in min2]
max1 = maxc(maxcon)
min1 = minc(mincon)
max2 = [max2[x] for x in max1]
min2 = [min2[x] for x in min1]
depth -= 1
max2.extend(min2)
max2 = list(set(max2))
max2.sort()
return [contour[x] for x in max2]
def composers():
beet = os.listdir('themes/Beethoven')
if '.DS_Store' in beet:
beet.remove('.DS_Store')
moz = os.listdir('themes/Mozart')
if '.DS_Store' in moz:
moz.remove('.DS_Store')
beethoven = []
mozart = []
for piece in beet:
beethoven.extend(os.listdir('themes/Beethoven/' + piece))
for piece in moz:
mozart.extend(os.listdir('themes/Mozart/' + piece))
while '.DS_Store' in beethoven:
beethoven.remove('.DS_Store')
while '.DS_Store' in mozart:
mozart.remove('.DS_Store')
b = {'name': 'Beethoven', 'files': beethoven}
m = {'name': 'Mozart', 'files': mozart}
return (b, m)
def pieces():
beet = os.listdir('themes/Beethoven')
while '.DS_Store' in beet:
beet.remove('.DS_Store')
moz = os.listdir('themes/Mozart')
if '.DS_Store' in moz:
moz.remove('.DS_Store')
toReturn = []
for piece in beet:
files = os.listdir('themes/Beethoven/' + piece)
if '.DS_Store' in beet:
files.remove('.DS_Store')
toReturn.append({'name': piece, 'files': files})
for piece in moz:
files = os.listdir('themes/Mozart/' + piece)
if '.DS_Store' in moz:
files.remove('.DS_Store')
toReturn.append({'name': piece, 'files': files})
return toReturn
def smoothness(contour):
plus = []
for x in range(len(contour)-1):
if contour[x] == contour[x+1]:
plus.append('=')
elif contour[x] < contour[x+1]:
plus.append('+')
else:
plus.append('-')
count = 0
for x in range(len(plus)-1):
if plus[x] == plus[x+1]:
count += 1
if len(plus) == 0:
return 0
else:
return count/len(plus)
composers = ['songs/gershwin', 'songs/foster', 'songs/schubert']
contours = []
piece_red = []
piece_conts = []
pieces = []
# all contours of all the themes'
for composer in composers:
print(composer)
for piece in os.listdir(composer):
print(piece)
notes = getNotes(composer + '/' + piece)
thing = []
for note in notes:
if note is not 'Rest':
thing.append(note)
else:
piece_conts.append(thing)
thing = []
smooths = 0
for cont in piece_conts:
# reduced = reduce_c(cont, 10)
# red = getContour(len(reduced), reduced)
cont = cont_num(getContour(len(cont), cont))
smooth = smoothness(cont)
smooths += smooth
print(smooths/len(piece_conts))
# all_conts = {}
# for cont in piece_red:
# if str(cont) in all_conts:
# all_conts[str(cont)] += 1
# else:
# all_conts[str(cont)] = 1
# print(all_conts)
# cont = getContour(5, notes)
# contour = cont_num(cont)
# contours.append(contour)
# contour2 = []
# for piece in os.listdir(composer2):
# notes = getNotes(composer2 + '/' + piece)
# cont = getContour(5, notes)
# contour = cont_num(cont)
# contour2.append(contour)
# coms = list(map(COM_matrix, contours))
# coms2 = list(map(COM_matrix, contour2))
# totes = []
# total = 0
# for cont in coms:
# for cont2 in coms2:
# this = compare(cont, cont2)
# totes.append(this)
# total += this
# print(total/len(totes))
# print(np.std(totes))
# all_conts = {}
# for cont in totes:
# if cont in all_conts:
# all_conts[cont] += 1
# else:
# all_conts[cont] = 1
# print(all_conts)
# average contour similarity within piece
# average contour similarity within composer
# sample contours inside and outside pieces
# figure out how to compute an average contour
# divide pieces up by instrument/style?
# divide themes by which one in variation they are