-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaseline_11.py
More file actions
482 lines (425 loc) · 19 KB
/
Baseline_11.py
File metadata and controls
482 lines (425 loc) · 19 KB
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
from pyAudioAnalysis import audioBasicIO
from pyAudioAnalysis import audioFeatureExtraction
import os
import pydub
import numpy as np
import csv
import pyaudio
from threading import Timer
import time
from phue import Bridge
import wave
from scipy.ndimage import gaussian_filter1d
import random
import shutil
bridge = Bridge('10.0.0.10')
lights = [7,8,10,11,12,14,15]
spatial_lights = [[14, 10, 8, 15], [8, 14, 7, 15], [7, 8, 12, 15], [12, 7, 11, 15], [11, 12, 10, 15], [10, 14, 11, 15], [15, 14, 8, 7, 12, 11, 10, 15]]
spatial_lights_index = [14, 8, 7, 12, 11, 10, 15]
def sample(i): #This function is what executes light commands
command = lights_track[i][2] #dictionary of command peices
bridge.set_light(lights_track[i][1], command)
print('Converting...')
sound = pydub.AudioSegment.from_mp3(os.path.join('C:\\', 'Users', 'akauf', 'Desktop', 'song.mp3'))
sound.export(os.path.join('E:\\', 'Python_Projects', 'Audio_engine', 'temp.wav'), format="wav")
print('Converted File to wav!')
print('Horaay!')
print('I am coolguy')
#Loads audio into bits file
print('Extracting Data...')
[Fs, x] = audioBasicIO.readAudioFile(os.path.join('E:\\', 'Python_Projects', 'Audio_engine', 'temp.wav'))
x = audioBasicIO.stereo2mono(x) # Collapses to mono signal
F = audioFeatureExtraction.stFeatureExtraction(x, Fs, 0.050*Fs, 0.025*Fs) #Creates an array of features per frame
print('Extracted!')
N = [] #Array of features that we are going to use and modify
harmonic_p = [] #Item for array N, power of higher frequencies
percussive_p = [] #Item for array N, power of lower frequencies
arc_p = [] #Item for array N, total track power
spec_center_p = []
chaos_p = []
#PHASE THIS OUT IN FUTURE VERSIONS
perc_colors = [0, 10879, 5461] #List of hue values for percparse to use
def trim(lst):
newlst = []
for i in range(len(lst)):
if lst[i] == lst[-1]:
continue
else:
newlst.append(float(lst[i]))
return newlst
for i in range(len(F[1])): #Combine top chroma frequencies and entropy for harmonic processing
tmp = 0
tmp += F[2][i] * .1
tmp += F[25][i] * .93
tmp += F[26][i] * .94
tmp += F[27][i] * .95
tmp += F[28][i] * .96
tmp += F[29][i] * .97
tmp += F[30][i] * .98
tmp += F[31][i] * .99
tmp += F[32][i]
harmonic_p.append(tmp * -1) #Creates harmonic list for N
harmonic = trim(harmonic_p)
for i in range(len(F[1])): #Combines chroma frequencies and mel frequencies for percussive processing
tmp = 0
tmp += F[8][i] * 1.25 * 0
tmp += F[9][i]
tmp += F[21][i] * 2
tmp += F[22][i]
percussive_p.append(tmp * -1) #Creates percussive list for N
percussive = trim(percussive_p)
for i in range(len(F[1])): #Combines all MFCCs for use in arc processing
tmp = 0
tmp += F[9][i]
tmp += F[10][i]
tmp += F[11][i]
tmp += F[12][i]
tmp += F[13][i]
tmp += F[14][i]
tmp += F[15][i]
tmp += F[16][i]
tmp += F[17][i]
tmp += F[18][i]
tmp += F[19][i]
tmp += F[20][i]
tmp += F[21][i]
arc_p.append(tmp * -1) #Creates arc list for N
arc = trim(arc_p)
for i in range(len(F[1])):
tmp = 0
tmp += F[0][i]
tmp += F[2][i] * .063
tmp += F[6][i]
tmp += F[33][i]
chaos_p.append(tmp)
chaos = trim(chaos_p)
for i in range(len(F[1])):
spec_center_p.append(F[3][i])
spec_center = trim(spec_center_p)
N.append(harmonic) #Wrap all created items into array N
N.append(percussive)
N.append(arc)
N.append(spec_center)
N.append(chaos)
def normalize(sequence, sigma=1):
values = []
for i in sequence:
values.append(float(i))
values = gaussian_filter1d(values, sigma)
low = min(values)
high = max(values)
if low > 0:
factor = 255 / (high - low)
elif low < 0:
factor = 255 / (high + abs(low))
for i in range(len(values)):
if low < 0:
values[i] += abs(low)
elif low > 0:
values[i] -= low
values[i] *= factor
newval = []
for i in range(len(values)):
newval.append([i, values[i]])
return newval
def sumgraph(extract, effect=0, sigma=15): #Akin to position/time graph, takes a list of frames, creates a graph
#Effect refers to what item in N is used, sigma is the sigma value for gaussian smoothing
avg_tmp = 0
for i in range(len(extract[effect])): #Grab the average power for given N
avg_tmp += extract[effect][i]
avg = avg_tmp / len(extract[effect])
run = avg #run is a value that gets modified each frame
graph = [] #[x, y] positions for the sumgraph
#CAN BE DONE WITH LAMBDA, PHASE THIS OUT
graph_values = [] #Sumgraph values without x value
for i in range(len(extract[effect])):
if extract[effect][i] < 0.1 and i != 0:
extract[effect][i] = abs(avg - 2)
run += extract[effect][i] - avg #Modifies run up or down depending on how far off average it is
graph.append([i, 0])
#REFERENCE TO FEATURE PLANNED FOR DELETION
graph_values.append(run)
graph_min = abs(min(graph_values)) #Determines lowest point of graph
graph_max = max(graph_values) #Determines highest point of graph
factor = 255 / (graph_max + graph_min) #Determines mulitplying factor for next function
for i in range(len(graph)):
graph_values[i] += graph_min #Raises graph so lowest point is now zero
graph_values[i] *= factor #Readjusts graph so that highest point is 255
graph_values = gaussian_filter1d(graph_values, sigma) #Applies gaussian smoothing to graph, using supplied sigma
for i in range(len(graph_values)):
graph[i][1] = graph_values[i]
return graph
def derive(graph, shift=True): #Makes a derivative of the supplied sumgraph
#Shift determines whether or not the graph is scaled to 0-255
deriv_values = [] #Y values
deriv_graph = [] #X and Y values
for i in range(len(graph) - 1): #Calculates slope at each frame, skips last frame
slope = (graph[i + 1][1] - graph[i][1]) / (graph[i + 1][0] - graph[i][0])
deriv_graph.append([i, slope])
deriv_values.append(slope)
deriv_max = max(deriv_values) #Max and min are used for scaling the graph
deriv_min = abs(min(deriv_values))
if shift == True:
factor = 255 / (deriv_max + deriv_min)
else:
factor = 255 / (deriv_max)
for i in range(len(deriv_graph)):
if shift == True:
deriv_graph[i][1] += deriv_min
deriv_graph[i][1] *= factor
return deriv_graph
def hue_process_i(value):
value += random.randrange(-40, 40)
if value > 255:
value = 255
if value < 1:
value = 1
if value > 128:
value = value * 1.1
if value > 255:
value = 255
if value < 128:
value = value * .8
value = 255 - value
hue = abs(value * 200)
return hue
def hue_process(value):
value += random.randrange(-10, 10)
if value > 255:
value = 255
if value < 1:
value = 1
if value < 1:
value = 1
if value > 128:
value = value * 1.32
if value > 255:
value = 255
if value < 128:
value = value * .68
hue = value * 213
return hue
def write_data(filename, graph):
with open(filename, 'w') as csvfile:
writer = csv.writer(csvfile)
for i in range(len(graph)):
if type(graph[i]) != list:
graph[i] = [graph[i]]
graph[i].append(i)
graph[i][0], graph[i][1] = graph[i][1], graph[i][0]
writer.writerow(graph[i])
absarc = normalize(arc, 25)
absharm = normalize(harmonic, 22)
absperc = normalize(percussive, 4)
abschaos = normalize(chaos)
spectral = normalize(spec_center)
harmgraph = sumgraph(N, 0, 15)
percgraph = sumgraph(N, 1, 1)
arcgraph = sumgraph(N, 2, 20)
arclarge = normalize(arc, 150)
perclarge = normalize(percussive, 150)
spectral_graph = sumgraph(N, 3, 30)
def arcparse(graph):
print('Parsing Arc...')
zero_list = []
deriv = derive(graph, False)
deriv2 = derive(deriv, False)
for i in range(len(deriv) - 1):
if (deriv[i][1] < 0 and deriv[i + 1][1] > 0) or (deriv[i][1] > 0 and deriv[i + 1][1] < 0) or deriv[i][1] == 0:
zero_list.append(graph[i])
for i in range(len(zero_list) - 1):
gap = zero_list[i + 1][0] - zero_list[i][0]
if gap < 15:
gap = 'DEL'
zero_list[i].append(gap)
arclist = []
for i in range(len(zero_list) - 1):
if len(zero_list[i]) == 3:
if zero_list[i][2] != 'DEL':
nex = zero_list[i + 1][0]
hue = hue_process_i(absarc[nex][1])
sat = int(spectral[nex][1] * 3 + 210)
bri = int((absarc[nex][1] + arcgraph[nex][1]) / 2)
if sat > 254:
sat = 254
arclist.append([zero_list[i][0], lights, {'bri': bri, 'sat': sat, 'transitiontime': int(zero_list[i][2]), 'hue': int(hue)}, 'arc'])
print('Success!')
return arclist
beatlocation = 0
def percparse(graph):
print('Parsing Beats...')
zero_list = []
deriv = derive(graph, False)
deriv2 = derive(deriv, False)
for i in range(len(deriv) - 1):
if ((deriv[i][1] < 0 and deriv[i + 1][1] > 0) or deriv[i][1] == 0) and graph[i][1] > 120:
if (arclarge[i][1] > 120 and perclarge[i][1] > 120 and absarc[i][1] > 110) or (graph[i] > 220):
zero_list.append(graph[i])
else:
continue
for i in range(len(zero_list) - 1):
gap = zero_list[i + 1][0] - zero_list[i][0]
zero_list[i].append(gap)
perclist = []
for i in zero_list:
if len(i) == 3:
if i[2] != 'DEL':
sat = int(deriv2[i[0]][1] * 2.25 + 255)
if sat > 254:
sat = 254
bri = int((absarc[i[0]][1] + graph[i[0]][1]) / 2) + 75
if bri > 254:
bri = 254
if bri < 1:
bri = 1
hue = int(hue_process_i(absarc[i[0]][1] * .60 + .40 * graph[i[0]][1]))
global beatlocation
fixture = spatial_lights[beatlocation][random.randrange(0, 4)]
beatlocation = spatial_lights_index.index(fixture)
perclist.append([i[0] - 1, fixture, {'bri': bri, 'sat': sat, 'transitiontime': 1, 'hue': hue}, 'beat'])
print('Success!')
return perclist
harmlocation = 0
def harmparse(graph):
print('Parsing Harmonies...')
deriv = derive(graph)
low = []
high = []
combined = []
deriv2 = derive(deriv, False)
for i in range(len(deriv2) - 1):
if (deriv2[i][1] < 0 and deriv2[i + 1][1] > 0 and arclarge[i][1] > 85):
low.append(deriv[i] + ['low'])
elif (deriv2[i][1] > 0 and deriv2[i + 1][1] < 0 and arclarge[i][1] > 85) or deriv2[i][1] == 0:
high.append(deriv[i] + ['high'])
for i in range(min([len(low), len(high)])):
combined.append(low.pop(0))
try:
if high[0][0] > combined[-1][0]:
combined.append(high.pop(0))
elif high[1][0] > combined[-1][0]:
combined.append(high.pop(1))
del high[0]
except:
continue
for i in range(len(combined) - 1):
combined[i][1] = combined[i + 1][1]
gap = combined[i + 1][0] - combined[i][0]
if gap < 5:
gap = 'DEL'
combined[i].append(gap)
combined[i].append(graph[combined[i][0]][1])
for i in range(len(combined) - 1):
if combined[i][2] == 'low':
bri = 140 + absarc[i][1] * 1.25
if bri > 254:
bri = 254
combined[i][1] = bri
if len(combined[i]) > 4:
sat = spectral[i][1] + 128
if sat > 254:
sat = 254
combined[i][4] = sat
hue = int(hue_process_i(absarc[i][1] * .65 + .35 * absharm[i][1]))
combined[i].append(hue)
harmlist = []
for i in range(0, len(combined) - 1, 2):
global beatlocation
light = spatial_lights[beatlocation][random.randrange(0, 3)]
beatlocation = spatial_lights_index.index(light)
if len(combined[i]) == 6:
if combined[i][3] != 'DEL':
harmlist.append([combined[i][0], light, {'bri': int(combined[i][1]), 'sat': int(combined[i][4]), 'transitiontime': int(combined[i][3]), 'hue': int(combined[i][5] * 1.2)}, combined[i][2]])
if len(combined[i + 1]) == 6:
if combined[i + 1][3] != 'DEL':
harmlist.append([combined[i + 1][0], light, {'bri': int(combined[i + 1][1] * .3), 'sat': int(combined[i + 1][4]), 'transitiontime': int(combined[i + 1][3] * .65), 'hue': int(combined[i + 1][5])}, combined[i + 1][2]])
print('Success!')
return harmlist
def chaosparse(chaos):
print('Processing Chaos...')
chaoslist = []
for i in range(len(chaos)):
if chaos[i] > .6:
light = lights[random.randrange(0,6)]
chaoslist.append([i, light, {'bri': 254, 'sat': 1, 'transitiontime': 0}, 'Chaos'])
chaoslist.append([i + 5, light, {'bri': 1, 'sat': 1, 'transitiontime': 0}, 'Chaos'])
print('Chaos Processed!')
return chaoslist
lights_track = arcparse(absarc) + harmparse(harmgraph) + percparse(absperc)
lights_track.sort(key=lambda x: x[0])
for i in lights_track:
i.append('Unmodified')
lock = 'blaps'
for i in range(1, len(lights_track) - 1):
if lights_track[i][3] == 'Chaos':
if lights_track[i - 1][0] > lights_track[i][0] - 5 and lights_track[i - 1][4] != ('Safe' or 'Chaos'):
lights_track[i - 1][4] = 'DEL'
lights_track[i].append('Safe')
if lights_track[i + 1][0] < lights_track[i][0] + 5 and lights_track[i + 1][4] != ('Safe' or 'Chaos'):
lights_track[i + 1][4] = 'DEL'
lights_track[i][4] = 'Safe'
if lights_track[i][3] == 'arc':
if lights_track[i - 1][0] > lights_track[i][0] - 24 and lights_track[i - 1][3] != 'Chaos' and lights_track[i - 1][4] != 'Safe':
lights_track[i - 1][4] = 'DEL'
lights_track[i][4] = 'Safe'
if lights_track[i + 1][0] < lights_track[i][0] + 24 and lights_track[i + 1][3] != 'Chaos' and lights_track[i + 1][4] != 'Safe':
lights_track[i + 1][4] = 'DEL'
lights_track[i][4] = 'Safe'
if lights_track[i][3] == 'low':
lock = lights_track[i][1]
if lights_track[i - 1][0] > lights_track[i][0] - 5 and lights_track[i - 1][3] == 'beat' and lights_track[i - 1][4] != 'Safe':
lights_track[i - 1][4] = 'DEL'
lights_track[i][4] = 'Safe'
if lights_track[i + 1][0] < lights_track[i][0] + 5 and lights_track[i + 1][3] == 'beat' and lights_track[i + 1][4] != 'Safe':
lights_track[i + 1][4] = 'DEL'
lights_track[i][4] = 'Safe'
if lights_track[i][3] == 'high':
lock = 'blaps'
if lights_track[i - 1][0] > lights_track[i][0] - 5 and lights_track[i - 1][3] == 'beat' and lights_track[i - 1][4] != 'Safe':
lights_track[i - 1][4] = 'DEL'
lights_track[i][4] = 'Safe'
if lights_track[i + 1][0] < lights_track[i][0] + 5 and lights_track[i + 1][3] == 'beat' and lights_track[i + 1][4] != 'Safe':
lights_track[i + 1][4] = 'DEL'
lights_track[i][4] = 'Safe'
if type(lights_track[i][1]) == int and lights_track[i][1] == lock and lights_track[i][3] != ('low' or 'high'):
lights_track[i][4] = 'DEL'
elif type(lights_track[i][1]) == list:
if lock in lights_track[i][1] and lights_track[i][3] != ('low' or 'high' or 'Chaos' or 'beat'):
lights_track[i][1] = [x for x in lights_track[i][1] if x != lock]
lights_track = [x for x in lights_track if x[4] != 'DEL']
def write_graphs():
print('Writing a whole bunch of stuff')
write_data(os.path.join('E:\\', 'Python_Projects', 'Audio_Engine', 'Logs', 'percraw.csv'), percussive)
write_data(os.path.join('E:\\', 'Python_Projects', 'Audio_Engine', 'Logs', 'perclarge.csv'), perclarge)
write_data(os.path.join('E:\\', 'Python_Projects', 'Audio_Engine', 'Logs', 'arclarge.csv'), arclarge)
write_data(os.path.join('E:\\', 'Python_Projects', 'Audio_Engine', 'Logs', 'arcgraph.csv'), arcgraph)
write_data(os.path.join('E:\\', 'Python_Projects', 'Audio_Engine', 'Logs', 'harmgraph.csv'), harmgraph)
write_data(os.path.join('E:\\', 'Python_Projects', 'Audio_Engine', 'Logs', 'percgraph.csv'), percgraph)
write_data(os.path.join('E:\\', 'Python_Projects', 'Audio_Engine', 'Logs', 'absarc.csv'), absarc)
write_data(os.path.join('E:\\', 'Python_Projects', 'Audio_Engine', 'Logs', 'absharm.csv'), absharm)
write_data(os.path.join('E:\\', 'Python_Projects', 'Audio_Engine', 'Logs', 'absperc.csv'), absperc)
write_data(os.path.join('E:\\', 'Python_Projects', 'Audio_Engine', 'Logs', 'lights_track.csv'), lights_track)
write_graphs()
def execute(wav):
chunk = 1024
wf = wave.open(wav, 'rb')
p = pyaudio.PyAudio()
stream = p.open(
format = p.get_format_from_width(wf.getsampwidth()),
channels = wf.getnchannels(),
rate = wf.getframerate(),
output = True)
data = wf.readframes(chunk)
print('running lights')
for i in range(0, len(lights_track)):
Timer(lights_track[i][0] / 40, sample, [i]).start()
time.sleep(0.1)
while data != '':
stream.write(data)
data = wf.readframes(chunk)
stream.close()
p.terminate()
def blah():
execute(os.path.join('E:\\', 'Python_Projects', 'Audio_engine', 'temp.wav'))
bridge.set_light(lights, 'on', True)
blah()