-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontroller.py
More file actions
454 lines (371 loc) · 12.4 KB
/
Copy pathcontroller.py
File metadata and controls
454 lines (371 loc) · 12.4 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
from gui import *
import sys
import wave
import time
import numpy
import scales
from scipy import interpolate
from operator import itemgetter
from PyQt5.QtWidgets import QApplication
import pyaudio
#TODO: stop on window close
NUM_COL = 8
NUM_ROW = 13
class App():
def __init__(self,args):
self.app = QApplication(args)
self.gui = Gui()
self.bpm = self.gui.controlpanel.tempoSlider.value()
self.sleepTime = 60/float(self.bpm)
self.max_frames = int((60/float(self.bpm))*15000)
print(self.max_frames)
# global event handlers
self.gui.controlpanel.tempoSlider.valueChanged.connect(self.updateTempo)
self.gui.controlpanel.volumeSlider.valueChanged.connect(self.updateGlobalVolume)
self.gui.controlpanel.selectBox.currentIndexChanged.connect(self.updateMode)
et = scales.EvenTempered(528)
pythag = scales.Pythagorean(528)
dodec = scales.Dodecaphonic(528)
ptolemy = scales.Ptolemy(528)
mt = scales.MeanTone(528)
allScales = [pythag,dodec,et,mt,ptolemy]
self.grid = []
for i in range(13):
rowObj = Row(i)
for scale in allScales:
if (i < len(scale.scale)):
#rowObj.scaleData.append(self.pluck(scale.get_frequency(i+1)))
pluckData = self.pluck(scale.get_frequency(i+1))
rowObj.scaleDataUnchanged.append(pluckData)
rowObj.scaleData.append(pluckData)
#rowObj.scaleData.append(numpy.multiply(pluckData,0.75))
else:
rowObj.scaleData.append(numpy.zeros(1))
rowObj.scaleDataUnchanged.append(numpy.zeros(1))
self.grid.append(rowObj)
self.mode = self.gui.controlpanel.selectBox.currentIndex()
self.curColumn = 0
self.playButton = self.gui.controlpanel.playButton
self.playButton.clicked.connect(self.play)
self.volume = 100
self.globalVolumeFactor = 1.0
self.playing = False
print("MAX FRAMES: " + str(self.max_frames))
cwd = os.getcwd()
sampleDir = cwd+'/samples/'
for child in self.gui.grid.children():
if (isinstance(child,GridFileButton)):
if (child.fileName!=""):
waveObj = waveFile(child.fileName)
#print(len(waveObj.intData))
if (len(waveObj.intData)>self.max_frames):
print("OVER: " + str(len(waveObj.intData)))
waveObj.intData = self.truncate(waveObj)
else:
waveObj.intData = self.addZeros(waveObj)
self.grid[child.row].fileObj = waveObj
if (isinstance(child,GridButton)):
child.clicked.connect(self.updateGrid)
if (isinstance(child,GridVolumeDial)):
child.valueChanged.connect(self.updateRowVolume)
if (isinstance(child,GridCheckbox)):
child.clicked.connect(self.updateStresses)
def makehit(self, hit_data):
'''
Returns numpy array of compiled hits
'''
if (len(hit_data)==0):
return ""
#decoded_data = [numpy.fromstring(data, numpy.int32) for data in hit_data]
_max = max([len(d) for d in hit_data])
mixed = numpy.zeros(_max, dtype=numpy.int32)
denom = len(hit_data)
for _data in hit_data:
mixed[:len(_data)] += ((_data/denom).astype(numpy.int32))
return mixed.tostring()
def shape(self, data, points, kind='slinear'):
items = points.items()
items.sort(key=itemgetter(0))
keys = map(itemgetter(0), items)
vals = map(itemgetter(1), items)
interp = interpolate.interp1d(keys, vals, kind=kind)
factor = 1.0 / len(data)
shape = interp(np.arange(len(data)) * factor)
return data * shape
def truncate(self,waveObj):
return waveObj.intData[:int(self.max_frames)]
#TODO: shape the tail
#chunk = numpy.fromstring(waveObj.data, numpy.int32)
#return self.shape(_data, {0.0: 0.0, 0.005: 1.0, 0.25: 0.5, 0.9: 0.1, 1.0:0.0})
#waveObj.data = shaped.tostring()
def shape(self, data, points, kind='slinear'):
items = list(points.items())
items.sort(key=itemgetter(0))
keys = list(map(itemgetter(0), items))
vals = list(map(itemgetter(1), items))
interp = interpolate.interp1d(keys, vals, kind=kind)
factor = 1.0 / len(data)
shape = interp(numpy.arange(len(data)) * factor)
return data * shape
def harmonics(self,freq,length):
#make sure it takes in self.max_frames
a = scales.sine(freq*1.00, (self.max_frames/44100), 44100)
b = scales.sine(freq*2.00, (self.max_frames/44100), 44100)
return (a+b)*.2
def pluck(self,freq):
chunk = self.harmonics(freq, 1)
return self.shape(chunk, {0.0:0.0,0.005:1.0,0.25:0.5,0.9:0.0,1.1:0.0})
def chord(self,freqs):
thechord = sum([pluck(freq) for freq in freqs])
return thechord
def addZeros(self,waveObj):
'''
add (max_frames - len(waveObj.intData)) zeros to end
'''
diff = self.max_frames - (len(waveObj.intData))
arr = numpy.zeros(diff)
return numpy.append(waveObj.intData,arr)
def play(self):
#TODO: change stream based on scale or wav files
p = pyaudio.PyAudio()
if (self.mode==0):
stream = p.open(format=pyaudio.paInt32,
channels=1,
rate=44100,
output=True)
else:
stream = p.open(format=pyaudio.paFloat32,
channels=1,
rate=44100,
output=True)
if (self.playButton.playing):
print("Play->Pause")
self.gui.controlpanel.playButton.setText("Play")
else:
print("Pause->Play")
self.gui.controlpanel.playButton.setText("Pause")
self.playButton.playing = not self.playButton.playing
if (self.mode=="rhythm"):
self.updateFiles()
while (self.playButton.playing):
self.updateGrid() # gets state of button in gui and updates controller array
if (self.mode==0):
hitData = []
for row in self.grid:
self.app.processEvents() #THIS IS SUPER IMPORTANT! else use threading:/
pressed = row.array[self.curColumn]
if (pressed and (row.fileObj)):
_data = numpy.multiply(row.fileObj.intData,row.stressFactors[self.curColumn])
hitData.append(_data)
compiledHit = self.makehit(hitData)
stream.write(compiledHit)
else:
freqs = []
for row in self.grid:
self.app.processEvents()
pressed = row.array[self.curColumn]
if (pressed and (len(row.scaleData[self.mode-1])>1)):
freqs.append(row.scaleData[self.mode-1])
if (len(freqs)>0):
playData = sum([freqData for freqData in freqs])
_data = numpy.multiply(playData,row.stressFactors[self.curColumn])
stream.write(_data.astype(numpy.float32).tostring())
#snchanged to update colors
for child in self.gui.grid.children():
if(isinstance(child, GridButton)):
if child.col == self.curColumn:
print(child.row, child.col, "green")
child.setStyleSheet("background-color: green")
else:
child.setStyleSheet("background-color: gray")
print("process")
self.curColumn+=1
self.curColumn%=8
for i in range(1000):
self.app.processEvents() #THIS IS SUPER IMPORTANT! else use threading:/
time.sleep(self.sleepTime/float(1000))
stream.stop_stream()
stream.close()
p.terminate()
### EVENT HANDLERS ###
def updateGrid(self):
button = self.app.sender()
if (not isinstance(button,PlayButton)):
button.pressed = not button.pressed
if (button.pressed):
self.grid[button.row].array[button.col] = True
else:
self.grid[button.row].array[button.col] = False
print(str(button.row)+', '+str(button.col))
def updateFiles(self):
'''
Update the underlying wave data when new file is selected
'''
for child in self.gui.grid.children():
if (isinstance(child,GridFileButton)):
if (child.fileName!=""):
if (child.fileName != self.grid[child.row].fileObj.fileName):
print("FILE CHANGED: "+child.fileName)
# Update the underlying file
waveObj = waveFile(child.fileName)
#print(len(waveObj.intData))
if (len(waveObj.intData)>self.max_frames):
waveObj.intData = self.truncate(waveObj)
else:
waveObj.intData = self.addZeros(waveObj)
#print(len(waveObj.intData))
self.grid[child.row].fileObj = waveObj
def updateMode(self):
'''
Changes row data based on change in mode
'''
sender = self.app.sender()
print("now "+str(sender.currentIndex())+ " "+sender.currentText())
self.mode= sender.currentIndex()
for child in self.gui.grid.children():
if (isinstance(child,GridFileButton)):
if (self.mode == 0):
if (child.fileName!=""):
child.setText(child.fileName.split("/")[-1].split(".")[0])
else:
child.setText("Select File")
#snchanged to put interval names in boxes instead of numers
elif self.mode == 1:
if child.row > 7:
child.setText("")
else:
child.setText(str(child.row + 1))
elif (self.mode == 2) | (self.mode == 4):
child.setText(self.interval_name_dodecaphonic(child.row))
else:
child.setText(self.interval_name(child.row))
def updateTempo(self):
slider = self.app.sender()
self.bpm = slider.value() * 3 #snchanged to make it play faster
self.sleepTime = 60/float(self.bpm)
return
def updateStresses(self):
checkbox = self.app.sender()
for rowObj in self.grid:
rowObj.stressFactors[checkbox.col] = 1.0
print("Checkbox col: "+str(checkbox.col))
def updateGlobalVolume(self):
slider = self.app.sender()
factor = (slider.value()/100.0)
print(factor)
self.globalVolumeFactor = factor
for rowObj in self.grid:
rowVolumeFactor = factor*rowObj.volumeFactor
if (rowObj.fileObj):
rowObj.fileObj.intData = numpy.multiply(rowObj.fileObj.intDataUnchanged,rowVolumeFactor)
lenDiff = self.max_frames-len(rowObj.fileObj.intData)
if (lenDiff>0):
rowObj.fileObj.intData = numpy.append(rowObj.fileObj.intData,numpy.zeros(lenDiff))
for i in range(len(rowObj.scaleData)):
rowObj.scaleData[i] = numpy.multiply(rowObj.scaleDataUnchanged[i],rowVolumeFactor)
lenDiff = self.max_frames - len(rowObj.scaleData[i])
if (lenDiff > 0):
rowObj.scaleData[i] = numpy.append(rowObj.scaleData[i],numpy.zeros(lenDiff))
def updateRowVolume(self):
slider = self.app.sender()
rowObj = self.grid[slider.row]
factor = slider.value()/100.0
print("Local factor: " + str(factor))
print("Global factor: " + str(self.globalVolumeFactor))
rowObj.volumeFactor = factor
effectiveFactor = rowObj.volumeFactor*self.globalVolumeFactor
print("effectiveFactor: " + str(effectiveFactor))
rowObj.fileObj.intData = numpy.multiply(rowObj.fileObj.intDataUnchanged,effectiveFactor)
lenDiff = self.max_frames-len(rowObj.fileObj.intData)
print(lenDiff)
if (lenDiff>0):
rowObj.fileObj.intData = numpy.append(rowObj.fileObj.intData,numpy.zeros(lenDiff))
for i in range(len(rowObj.scaleData)):
rowObj.scaleData[i] = numpy.multiply(rowObj.scaleDataUnchanged[i],effectiveFactor)
lenDiff = self.max_frames - len(rowObj.scaleData[i])
if (lenDiff > 0):
rowObj.scaleData[i] = numpy.append(rowObj.scaleData[i],numpy.zeros(lenDiff))
#snchanged to put interval names in boxes instead of numbers
def interval_name(self, row_number):
return {
0: "1",
1: "m2",
2: "M2",
3: "m3",
4: "M3",
5: "4",
6: "b5",
7: "5",
8: "m6",
9: "M6",
10: "m7",
11: "M7",
12: "Octave",
}[row_number]
def interval_name_dodecaphonic(self, row_number):
return {
0: "1",
1: "m2",
2: "M2",
3: "m3",
4: "M3",
5: "4",
6: "b5-",
7: "b5+",
8: "5",
9: "m6",
10: "M6",
11: "m7",
12: "M7"
}[row_number]
class waveFile(object):
'''
class for wave file...
'''
CHUNK = 1024
def __init__(self,file_name):
try:
fileObj = wave.open(file_name, 'rb')
except wave.Error as e:
print("WAVE read error ({0}): {1}".format(e.errno, e.strerror))
exit(0) #HANDLE THIS BETTER
if (fileObj.getsampwidth()!=2):
print(file_name + " not compatible :/")
exit(0)
'''
allData = b''
data = fileObj.readframes(waveFile.CHUNK)
while (data != b''):
allData+=str(data)
data = fileObj.readframes(waveFile.CHUNK)
'''
self.fileName = file_name
self.data = fileObj.readframes(fileObj.getnframes())
self.intDataUnchanged = numpy.fromstring(self.data, dtype=numpy.int32)
self.intData = numpy.fromstring(self.data, dtype=numpy.int32)
self.file_name = file_name
self.obj = fileObj
self.duration = fileObj.getnframes()/float(fileObj.getframerate())
class Row():
def __init__(self,i):
self.fileObj = None
self.interval = i
self.scaleData = [] # Pythag, Dodec, ET, MT, Pt
self.scaleDataUnchanged = []
self.array = [False]*8 #Just a boolean array
self.volume = 100
self.volumeFactor = 1.0
self.stressFactors = [.5]*8
class RhythmRow(Row):
def __init__(self, i):
#self.array = [GridButton() for i in range(8)]
super().__init__()
self.fileObj = None
self.id = i
class ScaleRow(Row):
def __init__(self, freq, i):
self.frequency = freq
self.id = i
if __name__ == '__main__':
myApp = App(sys.argv)
sys.exit(myApp.app.exec_())