-
Notifications
You must be signed in to change notification settings - Fork 0
/
drum_gate_gui_from_python_file.py
356 lines (270 loc) · 13.6 KB
/
drum_gate_gui_from_python_file.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
# Load the noise gate UI from the Python file created by pyuic5
from PyQt5 import QtGui
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QWidget
# from QtDesigner_files.MainWindow2 import Ui_Form
from PyQt5_files.MainWindow3 import Ui_MainWindow
import numpy as np
import audiofile
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT
from matplotlib.figure import Figure
from matplotlib.animation import FuncAnimation
from noise_gate import Context, AudioConfig
from player import Player, PlaybackState
import queue
def zoom_factory(ax, base_scale=2):
def zoom_fun(event):
print(type(event))
# get the current x and y limits
cur_xlim = ax.get_xlim()
xdata = event.xdata # get event x location
if event.button == 'down':
# zoom in
scale_factor = 1/base_scale
elif event.button == 'up':
# zoom out
scale_factor = base_scale
ax.set_xlim([xdata - (xdata-cur_xlim[0]) / scale_factor, xdata + (cur_xlim[1]-xdata) / scale_factor])
ax.figure.canvas.draw() # Re-draw
fig = ax.get_figure() # get the figure of interest
# attach the call back
fig.canvas.mpl_connect('scroll_event', zoom_fun)
#return the function
return zoom_fun
# class MplWidget(QWidget):
# def __init__(self, parent=None):
# super().__init__(parent)
# self.fig = Figure(figsize=(5, 5))
# self.canvas = FigureCanvasQTAgg(self.fig)
# layout = QVBoxLayout(self)
# layout.addWidget(self.canvas)
# self.ax = self.canvas.figure.add_subplot(111)
class WaveformNavWidget(QWidget):
def __init__(self, player):#, parent=None):
super().__init__()#parent)
self.player = player
self.player.display = self
self.fig = Figure()
self.canvas = FigureCanvasQTAgg(self.fig)
layout = QVBoxLayout(self)
layout.addWidget(self.canvas)
self.plot_downsample = 100
self.ax = self.canvas.figure.add_subplot(111)
self.ax.plot(self.player.time_array[::self.plot_downsample],
self.player.audio_array[::self.plot_downsample],
lw=1, color="#00ADB5")
# Add a playhead
self.line_playhead = self.ax.axvline(0, linewidth=1, color='red')
# Remove the border around the figure
# for spine in self.ax.spines.values():
# spine.set_visible(False)
self.fig.subplots_adjust(left=0, right=1, bottom=0, top=1)
self.fig.set_facecolor("#f0f0f0")
self.ax.set_facecolor("#222831")
self.ax.set_yticks([])
# Implement the zoom and pan functions on self.ax
self.f = zoom_factory(self.ax, base_scale=1.33)
self.connect_events()
def connect_events(self):
self.fig.canvas.mpl_connect('button_press_event', self.move_playhead_onclick)
def move_playhead_onclick(self, event):
# Check the click happened inside self.ax
# Don't update the position if zoom or pan tools are enabled
if event.inaxes in [self.ax] and not self.ax.get_navigate_mode():
xpos = int(event.xdata)
# Position validation
if xpos <= 0:
xpos = 0
elif xpos >= len(self.player.audio_array):
xpos = len(self.player.audio_array)
# Set the new position of the playhead line
self.set_playhead_position(xpos)
# Update the player with the new starting position
self.player.set_start_idx(xpos)
def set_playhead_position(self, xpos):
# Set the new position of the playhead line
self.player.playhead_idx = xpos
self.line_playhead.set_xdata([xpos, xpos])
#self.canvas.draw()
self.canvas.blit()
def update_plot(self, frame):
''' Passed to FuncAnimation, used to update the playhead position while playing '''
# We only need to update the position if the player is playing
if self.player.playing:
self.line_playhead.set_xdata([self.player.playhead_idx, self.player.playhead_idx])
return self.ax.get_lines()
class ScrollingGatedAudioWidget(QWidget):
def __init__(self, player):#, parent=None):
super().__init__()#parent)
self.player = player
self.fig = Figure(figsize=(5, 5))
self.canvas = FigureCanvasQTAgg(self.fig)
self.ax = self.fig.add_subplot(111)
layout = QVBoxLayout(self)
layout.addWidget(self.canvas)
# Adjust the edges of the subplot using fractions of the figure dimensions
self.fig.subplots_adjust(left=0.07,right=0.98,bottom=0.05,top=0.95)
self.fig.set_facecolor("#f0f0f0")
self.ax.set_facecolor("#222831")
# Downsample factor to plot fewer points
self.downsample = 50
# How many milliseconds of audio will fit in the scrolling display
self.window = 4000
# Compute the number of points used to plot the lines
self.length = int(self.window*self.player.audio_config.fs/(1000*self.downsample))
# Create Line2D objects to display the (un-)gated audio
self.ungated_line, = self.ax.plot(np.ones(self.length) * -60, color="#00ADB5", alpha=0.3)
self.gated_line, = self.ax.plot(np.ones(self.length) * -60, color="#00ADB5")
# Initialise arrays to hold the gated and ungated audio data
self.gated_data = np.zeros(self.length)
self.ungated_data = np.zeros(self.length)
# Plot the threshold line on top of the gated/ungated lines
self.thresh_line = self.ax.axhline(self.player.noise_gate.thresh, color='red')
# Set up the plot limits and appearance
self.ax.set_ylim([-60, 0])
self.ax.set_yticks([val for val in range(-60, 10, 10)])
self.ax.set_xticks([])
def update_plot(self, frame):
''' Passed to FuncAnimation, used to update the scrolling plot '''
while True:
try:
# Get data from the player's queue
ungated_data, gated_data = self.player.q.get_nowait()
except queue.Empty:
break
# Get the magnitude of the ungated signal
ungated_data = np.abs(ungated_data)[:, 0]
num_rows = len(ungated_data) // self.downsample
# Get the maximum magnitude value in each window of size self.downsample
ungated_data = np.array([np.max(ungated_data[i*self.downsample : (i+1)*self.downsample]) for i in range(num_rows)])
# Convert magnitude to dBFS
ungated_data = 20*np.log10(ungated_data)
# Distance to scroll data between plot updates
shift = len(ungated_data)
gated_data = np.abs(gated_data)[:, 0]
num_rows = len(gated_data) // self.downsample
gated_data = np.array([np.max(gated_data[i*self.downsample : (i+1)*self.downsample]) for i in range(num_rows)])
gated_data = 20*np.log10(gated_data)
# Simulate scrolling - shift data to left and add new data on right end
self.ungated_data = np.roll(self.ungated_data, -shift, axis=0)
self.ungated_data[-shift:] = ungated_data
self.gated_data = np.roll(self.gated_data, -shift, axis=0)
self.gated_data[-shift:] = gated_data
# Update the data of the gated and ungated data lines
self.ungated_line.set_ydata(self.ungated_data)
self.gated_line.set_ydata(self.gated_data)
return self.ax.get_lines()
class MainWindow(QMainWindow, Ui_MainWindow):
def __init__(self, *args, obj=None, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
self.setupUi(self)
# Set up some audio things
self.audio_config = AudioConfig(fs=44100, blocksize=1024)
audio_data, _ = audiofile.read("data/snare_test.wav")
#time_arr = np.arange(len(audio_data))
# Slice audio if using a longer file
#audio_data = audio_data[160*self.audio_config.fs : 165*self.audio_config.fs]
self.player = Player(self.audio_config, audio_array=audio_data)
self.player.pause_behaviour = False
# Create and add the scrolling plot widget
self.gate_thresh_canvas = ScrollingGatedAudioWidget(self.player)#self)
self.scrolling_plot_layout.addWidget(self.gate_thresh_canvas)
# Create and add the waveform navigator widget
self.waveform_canvas = WaveformNavWidget(self.player)
self.waveform_nav_layout.addWidget(self.waveform_canvas)
# Animation for scrolling gated signal
self.gate_scroll_ani = FuncAnimation(self.gate_thresh_canvas.fig,
self.gate_thresh_canvas.update_plot,
interval=20,
blit=True,
cache_frame_data=False)
# Animation for playhead motion on waveform
self.playhead_ani = FuncAnimation(self.waveform_canvas.fig,
self.waveform_canvas.update_plot,
interval=20,
blit=True,
cache_frame_data=False)
# Group the gate controls
self.gate_controls = [self.threshold_slider,
self.attack_dial,
self.hold_dial,
self.release_dial,
self.lookahead_dial]
# Stop the on/off button getting toggled by space bar
self.on_off_button.setFocusPolicy(Qt.NoFocus)
self.make_connections()
self.set_initial_dial_values()
def set_initial_dial_values(self):
'''
Set the dial positions based on the initial noise gate parameters.
The values are multiplied by 1000 because the times are specified in
milliseconds but the QDial widget requires integer values.
'''
self.attack_dial.setValue(int(self.player.noise_gate.attack_time * 1000))
self.hold_dial.setValue(int(self.player.noise_gate.hold_time * 1000))
self.release_dial.setValue(int(self.player.noise_gate.release_time * 1000))
self.lookahead_dial.setValue(int(self.player.noise_gate.lookahead_time * 1000))
def make_connections(self):
''' Connect signals to slots '''
self.threshold_slider.valueChanged.connect(self.update_threshold_line)
self.on_off_button.pressed.connect(self.toggle_gate_enabled_state)
self.attack_dial.valueChanged.connect(self.update_attack)
self.hold_dial.valueChanged.connect(self.update_hold)
self.release_dial.valueChanged.connect(self.update_release)
self.lookahead_dial.valueChanged.connect(self.update_lookahead)
self.threshold_slider.valueChanged.connect(self.update_threshold)
self.threshold_slider.valueChanged.connect(self.update_threshold_line)
def toggle_gate_enabled_state(self):
''' Turn the noise gate on/off and update GUI elements '''
# Toggle the gate's enabled state
self.player.noise_gate.enabled = not self.player.noise_gate.enabled
# Disable gate controls - is there a better way than this?
if self.attack_dial.isEnabled():
self.on_off_button.setText("OFF")
# Change the colour of the horizontal threshold line
self.gate_thresh_canvas.thresh_line.set_color('lightgray')
self.gate_thresh_canvas.canvas.draw()
# Update the enabled state of the GUI elements
for el in self.gate_controls:
el.setEnabled(False)
else:
self.on_off_button.setText("ON")
# Change the colour of the horizontal threshold line
self.gate_thresh_canvas.thresh_line.set_color('red')
self.gate_thresh_canvas.canvas.draw()
# Update the enabled state of the GUI elements
for el in self.gate_controls:
el.setEnabled(True)
def update_threshold_line(self, val):
''' Update the position of the horizontal threshold line '''
#self.update_threshold(val)
self.gate_thresh_canvas.thresh_line.set_ydata([val, val])
def update_threshold(self, threshold_val):
''' Update the threshold value of the noise gate '''
#print(threshold_val)
self.player.noise_gate.thresh = threshold_val
def update_attack(self, attack_val):
''' Update the attack value of the noise gate '''
#print(attack_val)
self.player.noise_gate.set_attack_time(attack_val / 1000)
def update_hold(self, hold_val):
''' Update the hold value of the noise gate '''
#print(hold_val)
self.player.noise_gate.set_hold_time(hold_val / 1000)
def update_release(self, release_val):
''' Update the release value of the noise gate '''
#print(release_val)
self.player.noise_gate.set_release_time(release_val / 1000)
def update_lookahead(self, lookahead_val):
''' Update the lookahead value of the noise gate '''
# TODO
pass
def keyPressEvent(self, keyEvent):
''' Listener for key press events '''
if keyEvent.key() == Qt.Key_Space:
self.player.play_stop_command()
app = QApplication([])
window = MainWindow()
window.show()
app.exec_()