-
Notifications
You must be signed in to change notification settings - Fork 3
/
Deepbeat.py
451 lines (334 loc) · 13.7 KB
/
Deepbeat.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
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
import os
import sys
import time
import threading
import shutil
from Mainwindow import Ui_MainWindow
from PyQt5 import QtCore, QtGui, QtWidgets, Qt
from PyQt5.QtWidgets import QPushButton, QMainWindow, QApplication
from PyQt5.QtMultimedia import QSound, QAudioRecorder, QAudioFormat, QAudioEncoderSettings
from PyQt5.QtCore import QObject, QThread, pyqtSignal, QUrl
import qdarkstyle
# My Classes
from utils.myterrain import Terrain
from utils.midi_player import MidiPlayer
from utils.audio2midi import audio2midi
from utils.metronome import Metronome
import numpy as np
import pyqtgraph as pg
import librosa
from pydub import AudioSegment
#------------------Embedding files in Pyinstaller Build------------------------------
def resource_path(relative_path):
""" Get absolute path to resource, works for dev and for PyInstaller """
try:
# PyInstaller creates a temp folder and stores path in _MEIPASS
base_path = sys._MEIPASS
except Exception:
base_path = os.path.abspath(".")
return os.path.join(base_path, relative_path)
#-------------------------------------------------------------------------------
class MainWindow_EXEC():
def __init__(self):
#-------------------Init QT Setup---------------------------
app = QtWidgets.QApplication(sys.argv)
self.MainWindow = QtWidgets.QMainWindow()
self.ui = Ui_MainWindow()
self.ui.setupUi(self.MainWindow)
app.setStyleSheet(qdarkstyle.load_stylesheet_pyqt5())
#------------------Exporting Setup------------------------------
self.ui.export_midi.clicked.connect(self.openDirectory_midi)
self.ui.export_midi.setFocusPolicy(QtCore.Qt.NoFocus)
self.ui.export_audio.clicked.connect(self.openDirectory_audio)
self.ui.export_audio.setFocusPolicy(QtCore.Qt.NoFocus)
#------------------Metronome Setup------------------------------
self.ui.metronome_button.clicked.connect(self.metro_thread)
#------------------Recording Setup------------------------------
self.ui.start_stop_rec.clicked.connect(self.start_stop_recording)
self.ui.play_gui.clicked.connect(self.play)
# QAudio setup
self.settings = QAudioEncoderSettings()
self.settings.setBitRate(16)
self.settings.setChannelCount(1)
self.audioRecorder = QAudioRecorder()
self.audioRecorder.setEncodingSettings(self.settings)
self.file_path = os.path.abspath(os.path.join(os.path.dirname(__file__), resource_path("resources/output.wav")))
self.url = QUrl.fromLocalFile(self.file_path)
self.audioRecorder.setOutputLocation(self.url)
#------------------Audio Terrain Gui Setup------------------------------
self.terrain = Terrain()
self.terrain.update()
self.terrain_widget = self.terrain.getwidget()
self.layout = QtGui.QGridLayout()
self.layout.setContentsMargins(0, 0, 0, 0)
self.ui.t_widget.setLayout(self.layout)
self.layout.addWidget(self.terrain_widget,0,0,1,1)
#------------------Audio Trimmer Setup------------------------------
self.ui.audio_trimmer.clicked.connect(self.trim_audio)
if os.path.isfile("resources/output.wav"):
self.y, self.sr = librosa.load(resource_path("resources/output.wav"), sr=44100)
else:
new_wav = AudioSegment.empty()
new_wav.export("resources/output.wav", format="wav")
self.y, self.sr = librosa.load(resource_path("resources/output.wav"), sr=44100)
self.duration = round(librosa.core.get_duration(y=self.y, sr=self.sr) * self.sr)
self.maxv = np.iinfo(np.int16).max
self.win = pg.GraphicsLayoutWidget()
self.p = self.win.addPlot()
#removes X & Y Axis and disables mouse movement
self.p.showAxis('bottom', show=False)
self.p.showAxis('left', show=False)
self.p.setMouseEnabled(x=False, y=False)
self.region = pg.LinearRegionItem(brush=(100, 100, 100, 60), bounds=(0, self.duration))
self.region.setRegion([0, self.duration])
self.p.addItem(self.region, ignoreBounds=True)
self.p.plot(self.y, pen="w")
self.layout.addWidget(self.win)
self.win.hide()
#------------------Midi Setup------------------------------
self.ui.convert_midi.clicked.connect(self.convertMidi)
self.ui.midi_play.clicked.connect(self.midiplayer_thread)
self.ui.tempo_slider.valueChanged[int].connect(self.tempo_value)
self.ui.midi_loop.toggle()
# default bpm is 120
self.current_tempo = 120
self.detected_tempo = 120
#------------------Drum Kit Selector Setup----------------------
self.ui.drum_kits.clicked.connect(self.select_drumkit)
self.drum_number = 0
self.drum_folders = ['Drum_Kit_1','Drum_Kit_2','Drum_Kit_3','Drum_Kit_4']
self.drum_current = self.drum_folders[self.drum_number]
#------------------EXEC Window---------------------------------
self.MainWindow.show()
sys.exit(app.exec_())
#---------------------------------------------------------------
#------------------Functions----------------------------------
#------------------Drum Kit Selector------------------------------
def select_drumkit(self):
if self.drum_number < 3:
self.drum_number += 1
self.drum_current = self.drum_folders[self.drum_number]
self.ui.drum_kits.setText(self.drum_current.replace("_"," "))
else:
self.drum_number = 0
self.drum_current = self.drum_folders[self.drum_number]
self.ui.drum_kits.setText(self.drum_current.replace("_"," "))
#------------------Audio Trimmer------------------------------
def trim_audio(self):
# Switch to Trimmer widget
self.layout.removeWidget(self.terrain_widget)
self.terrain_widget.hide()
self.win.show()
self.trim_values = self.region.getRegion()
self.updateaudio()
# Trims signal array with region values
self.y = self.y[round(self.trim_values[0]):round(self.trim_values[1])]
# save the new signal values to wav
librosa.output.write_wav(resource_path("resources/output.wav"), (self.y * self.maxv).astype(np.int16), self.sr)
self.updateplot()
def updateplot(self):
# Replot the trimmed wav and update region bounds
self.duration = round(librosa.core.get_duration(y=self.y, sr=self.sr) * self.sr)
self.p.plot(clear=True)
self.p.plot(self.y, pen="w")
self.region = pg.LinearRegionItem(brush=(100, 100, 100, 50), bounds=(0, self.duration))
self.p.addItem(self.region, ignoreBounds=True)
self.region.setRegion([0, self.duration])
def updateaudio(self):
self.y, self.sr = librosa.load(resource_path("resources/output.wav"), sr=44100)
#------------------Metronome Threading------------------------------
def metro_thread(self):
if self.ui.metronome_button.isChecked():
print('metronome is On')
self.thread = QThread() # a new thread to run our background tasks in
self.worker = Worker(self.current_tempo) # a new worker to perform those tasks
self.worker.moveToThread(self.thread) # move the worker into the thread, do this first before connecting the signals
self.thread.started.connect(self.worker.work) # begin our worker object's loop when the thread starts running
self.thread.start()
else:
print('metronome is Off')
self.stop_loop()
self.worker.finished.connect(self.loop_finished) # do something in the gui when the worker loop ends
self.worker.finished.connect(self.thread.quit) # tell the thread it's time to stop running
self.worker.finished.connect(self.thread.wait)
self.worker.finished.connect(self.worker.deleteLater) # have worker mark itself for deletion
self.thread.finished.connect(self.thread.deleteLater)
def stop_loop(self):
self.worker.working = False
def loop_finished(self):
# print('Worker Finished')
pass
#---------------------------------------------------------
#------------------ MIDI ------------------------------
def tempo_value(self, value):
self.current_tempo = value
def convertMidi(self):
self.ui.convert_midi.setEnabled(False)
self.thread2 = QThread()
self.worker2 = ConvertMidi_Worker()
self.worker2.moveToThread(self.thread2)
self.thread2.started.connect(self.worker2.work)
self.thread2.start()
self.worker2.finished.connect(self.convert_finished)
self.worker2.finished.connect(self.thread2.quit)
self.worker2.finished.connect(self.thread2.wait)
self.worker2.finished.connect(self.worker2.deleteLater)
self.thread2.finished.connect(self.thread2.deleteLater)
def convert_finished(self,tempo):
self.detected_tempo = tempo
self.ui.tempo_slider.setValue(self.detected_tempo)
self.ui.convert_midi.clearFocus()
self.ui.convert_midi.setEnabled(True)
print('Midi Conversion finished')
def midiplayer_thread(self):
if self.ui.midi_play.isChecked() and self.ui.midi_loop.isChecked()==False:
self.ui.midi_play.setEnabled(False)
self.win.hide()
self.terrain_widget.show()
self.terrain.animate()
self.thread3 = QThread()
self.worker3 = MidiPlayer_Worker(self.current_tempo, self.drum_current)
self.worker3.moveToThread(self.thread3)
self.thread3.started.connect(self.worker3.workonce)
self.thread3.start()
self.worker3.finished2.connect(self.midi_loop_finished2)
self.worker3.finished2.connect(self.thread3.quit)
self.worker3.finished2.connect(self.thread3.wait)
self.worker3.finished2.connect(self.worker3.deleteLater)
self.thread3.finished.connect(self.thread3.deleteLater)
elif self.ui.midi_play.isChecked() and self.ui.midi_loop.isChecked()==True:
self.win.hide()
self.terrain_widget.show()
self.start_Midi_Thread()
self.terrain.animate()
elif self.ui.midi_play.isChecked()==False:
self.terrain.stop_animate()
self.stop_Midi_Thread()
def start_Midi_Thread(self):
self.thread3 = QThread()
self.worker3 = MidiPlayer_Worker(self.current_tempo, self.drum_current)
self.worker3.moveToThread(self.thread3)
self.thread3.started.connect(self.worker3.work)
self.thread3.start()
def stop_Midi_Thread(self):
self.worker3.working = False
self.worker3.stop()
self.worker3.finished.connect(self.midi_loop_finished)
self.worker3.finished.connect(self.thread3.quit)
self.worker3.finished.connect(self.thread3.wait)
self.worker3.finished.connect(self.worker3.deleteLater)
self.thread3.finished.connect(self.thread3.deleteLater)
print('done')
def midi_loop_finished(self):
print('Midi loop Finished')
def midi_loop_finished2(self):
print('Midi Player Finished')
self.ui.midi_play.toggle()
self.ui.midi_play.setEnabled(True)
self.terrain.stop_animate()
#---------------------------------------------------------
#------------------ Recorder & Player ------------------------------
def start_stop_recording(self):
if self.ui.start_stop_rec.isChecked():
self.ui.play_gui.setEnabled(False)
self.ui.audio_trimmer.setEnabled(False)
self.win.hide()
self.terrain_widget.show()
self.layout.addWidget(self.terrain_widget)
self.audioRecorder.record()
self.terrain.update()
self.terrain.animate()
print('Recording...')
else:
self.ui.play_gui.setEnabled(True)
self.ui.audio_trimmer.setEnabled(True)
self.terrain.stop_animate()
self.audioRecorder.stop()
self.layout.removeWidget(self.terrain_widget)
self.terrain_widget.hide()
self.updateaudio()
self.win.show()
self.updateplot()
print('Stop Recording')
def play(self):
if self.ui.play_gui.isChecked():
self.win.hide()
self.terrain_widget.show()
self.player = QSound(resource_path("resources/output.wav"))
self.terrain.animate()
self.player.play()
# if self.player.isFinished():
# self.ui.play_gui.toggle()
# print('done')
else:
self.terrain.stop_animate()
self.player.stop()
self.player.deleteLater()
#------------------ Exporting ------------------------------
def openDirectory_midi(self):
self.openDirectoryDialog= QtGui.QFileDialog.getExistingDirectory(self.MainWindow, "Save Midi File")
if self.openDirectoryDialog:
self.saveMidi(self.openDirectoryDialog)
else:
pass
def openDirectory_audio(self):
self.openDirectoryDialog= QtGui.QFileDialog.getExistingDirectory(self.MainWindow, "Save Audio File")
if self.openDirectoryDialog:
self.saveAudio(self.openDirectoryDialog)
else:
pass
def saveMidi(self, directory):
shutil.copy("resources/beatbox.mid", directory)
def saveAudio(self, directory):
shutil.copy("resources/output.wav", directory)
#---------------------------------------------------------------
#----------------- Thread Classes ----------------------------
class Worker(QObject):
finished = pyqtSignal()
def __init__(self, bpm):
super(Worker, self).__init__()
self.working = True
self.m = Metronome(bpm)
def work(self):
while self.working:
self.m.is_playing()
time.sleep(1)
self.m.stop()
self.m.end()
self.finished.emit()
class ConvertMidi_Worker(QObject):
finished = pyqtSignal(int)
def __init__(self):
super(ConvertMidi_Worker, self).__init__()
self.working = True
def work(self):
while self.working:
self.model = resource_path('resources/beatbox_model2.hdf5')
self.detected_tempo = audio2midi(resource_path('resources/output.wav'), self.model)
self.working = False
self.finished.emit(self.detected_tempo)
class MidiPlayer_Worker(QObject):
finished = pyqtSignal()
finished2 = pyqtSignal()
def __init__(self, bpm, current_drum_kit):
super(MidiPlayer_Worker, self).__init__()
self.current_drum_kit = current_drum_kit
self.current_tempo = bpm
self.working = True
self.kick = resource_path(f"resources/drumkits/{self.current_drum_kit}/kick.wav")
self.snare = resource_path(f"resources/drumkits/{self.current_drum_kit}/snare.wav")
self.hihat = resource_path(f"resources/drumkits/{self.current_drum_kit}/hihat.wav")
self.mplayer = MidiPlayer(resource_path('resources/beatbox.mid'), self.current_tempo, self.kick, self.snare, self.hihat)
def work(self):
self.mplayer.playmidi()
self.finished.emit()
def workonce(self):
while self.working:
self.mplayer.playonce()
self.working = False
self.finished2.emit()
def stop(self):
self.mplayer.stopMidi()
#---------------------------------------------------------------
if __name__ == "__main__":
MainWindow_EXEC()