-
Notifications
You must be signed in to change notification settings - Fork 9
/
pyrespeeder_gui.py
216 lines (188 loc) · 8.05 KB
/
pyrespeeder_gui.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
import numpy as np
import logging
from PyQt5 import QtWidgets
# custom modules
from util import spectrum, wow_detection, qt_threads, widgets, io_ops, markers
from util.fourier import timed_log
from util.undo import AddAction, MoveAction, MergeAction
from util.config import logging_setup
logging_setup()
class MainWindow(widgets.MainWindow):
EXT = ".spd"
STORE = {"lines": markers.TraceLine, "regs": markers.RegLine}
def __init__(self):
widgets.MainWindow.__init__(self, "pyrespeeder", widgets.ParamWidget, Canvas, 1)
main_menu = self.menuBar()
file_menu = main_menu.addMenu('File')
edit_menu = main_menu.addMenu('Edit')
# view_menu = main_menu.addMenu('View')
# help_menu = main_menu.addMenu('Help')
button_data = (
(file_menu, "Open", self.props.load, "CTRL+O", "dir"),
(file_menu, "Save", self.props.save, "CTRL+S", "save"),
(file_menu, "Resample", self.canvas.run_resample, "CTRL+R", "curve"),
(file_menu, "Batch Resample", self.canvas.run_resample_batch, "CTRL+B", "curve2"),
(file_menu, "Exit", self.close, "", "exit"),
(edit_menu, "Undo", self.props.undo_stack.undo, "CTRL+Z", "undo"),
(edit_menu, "Redo", self.props.undo_stack.redo, "CTRL+Y", "redo"),
(edit_menu, "Select All", self.canvas.select_all, "CTRL+A", "select_extend"),
(edit_menu, "Invert Selection", self.canvas.invert_selection, "CTRL+I", "select_intersect"),
(edit_menu, "Merge Selected", self.canvas.merge_selected_traces, "CTRL+M"),
(edit_menu, "Merge Overlapping", self.canvas.group_traces, "CTRL+G"),
(edit_menu, "Delete Selected", self.canvas.delete_traces, "DEL", "x"),
# (edit_menu, "Play/Pause", self.canvas.audio_widget.play_pause, "SPACE"),
)
self.add_to_menu(button_data)
class Canvas(spectrum.SpectrumCanvas):
def __init__(self, parent):
spectrum.SpectrumCanvas.__init__(self, spectra_colors=(None,), y_axis='Octaves')
self.create_native()
self.native.setParent(parent)
self.unfreeze()
self.parent = parent
self.show_regs = True
self.show_lines = True
self.grouped_traces = []
self.master_speed = markers.MasterSpeedLine(self)
self.master_reg_speed = markers.MasterRegLine(self, (0, 0, 1, .5))
# threading & links
self.resampling_thread = qt_threads.ResamplingThread()
self.resampling_thread.notifyProgress.connect(self.parent.props.progress_bar.setValue)
self.fourier_thread.notifyProgress.connect(self.parent.props.progress_bar.setValue)
self.parent.props.display_widget.canvas = self
self.parent.props.tracing_widget.canvas = self
self.parent.props.filters_widget.bands_changed.connect(self.master_speed.update_bands)
self.parent.props.alignment_widget.setVisible(False)
self.freeze()
@property
def lines(self):
return [m for m in self.markers if isinstance(m, markers.TraceLine)]
@property
def regs(self):
return [m for m in self.markers if isinstance(m, markers.RegLine)]
def load_visuals(self, ):
# read any saved traces or regressions
for offset, times, freqs in io_ops.read_trace(self.filenames[0]):
yield markers.TraceLine(self, times, freqs, offset=offset)
for t0, t1, amplitude, omega, phase, offset in io_ops.read_regs(self.filenames[0]):
yield markers.RegLine(self, t0, t1, amplitude, omega, phase, offset)
def merge_selected_traces(self):
self.merge_traces(list(reversed(self.selected_markers)))
def merge_traces(self, traces_to_merge):
if traces_to_merge and len(traces_to_merge) > 1:
logging.info(f"Merging {len(traces_to_merge)} lines")
t0 = 999999
t1 = 0
means = []
offsets = []
for trace in traces_to_merge:
t0 = min(t0, trace.speed_data[0, 0])
t1 = max(t1, trace.speed_data[-1, 0])
means.append(trace.spec_center[1])
offsets.append(trace.offset)
sr = self.sr
hop = self.hop
i0 = int(t0 * sr / hop)
i1 = int(t1 * sr / hop)
data = self.master_speed.data[i0:i1]
freqs = np.power(2, data[:, 1] + np.log2(np.mean(means)))
line = markers.TraceLine(self, data[:, 0], freqs, offset=None, auto_align=True)
self.props.undo_stack.push(MergeAction((line,), traces_to_merge))
def group_traces(self):
"""Convert overlapping traces into grouped representations"""
groups = self.master_speed.get_overlapping_lines()
logging.info(f"Merging {len(groups)} groups")
for group in groups:
self.merge_traces(group)
def run_resample(self):
spec = self.spectra[0]
if spec.audio_path and self.markers:
channels = self.props.files_widget.files[0].channel_widget.channels
if channels:
self.resampling_thread.settings = {
"filenames": (spec.audio_path,),
"signal_data": ((spec.signal, spec.sr),),
"speed_curve": self.get_speed_curve(),
"use_channels": channels}
self.props.resampling_widget.bump_index()
self.props.resampling_widget.to_cfg(self.resampling_thread.settings)
self.resampling_thread.start()
def get_speed_curve(self):
if self.regs:
speed_curve = self.master_reg_speed.get_linspace()
logging.info("Using regressed speed")
else:
speed_curve = self.master_speed.get_linspace()
logging.info("Using measured speed")
return speed_curve
def run_resample_batch(self):
filenames = QtWidgets.QFileDialog.getOpenFileNames(
self.parent, 'Open Files for Batch Resampling',
self.parent.cfg["dir_in"], "Audio files (*.flac *.wav)")[0]
if filenames:
self.resample_files(filenames)
def resample_files(self, files):
channels = self.props.files_widget.files[0].channel_widget.channels
if self.markers and channels:
self.resampling_thread.settings = {
"filenames" : files,
"speed_curve" : self.get_speed_curve(),
"use_channels" : channels}
self.props.resampling_widget.bump_index()
self.props.resampling_widget.to_cfg(self.resampling_thread.settings)
self.resampling_thread.start()
def update_lines(self):
self.master_speed.update()
self.master_reg_speed.update()
def on_mouse_press(self, event):
# #audio cursor
# b = self.px_to_spectrum(event.pos)
# #are they in spec_view?
# if b is not None:
# self.props.audio_widget.cursor(b[0])
# selection, single or multi
if event.button == 2:
closest_marker = self.get_closest(self.markers, event.pos)
if closest_marker:
closest_marker.select_handle("Shift" in event.modifiers)
event.handled = True
def on_mouse_release(self, event):
if self.filenames[0] and (event.trail() is not None) and event.button == 1 and "Control" in event.modifiers:
# event.trail() is integer pixel coordinates on vispy canvas
trail = [self.px_to_spectrum(click) for click in event.trail()]
# filter out any clicks outside of spectrum, for which px_to_spectrum returns None
trail = [x for x in trail if x is not None]
if trail:
t0 = trail[0][0]
t1 = trail[-1][0]
settings = self.props.tracing_widget
# maybe query it here from the button instead of the other way
if settings.mode == "Sine Regression":
amplitude, omega, phase, offset = wow_detection.trace_sine_reg(
self.master_speed.get_linspace(), t0, t1, settings.rpm)
if amplitude == 0:
logging.warning("Regressed to no amplitude, trying to sample regression curve")
amplitude, omega, phase, offset = wow_detection.trace_sine_reg(
self.master_reg_speed.get_linspace(), t0, t1, settings.rpm)
marker = markers.RegLine(self, t0, t1, amplitude, omega, phase, offset)
self.props.undo_stack.push(AddAction((marker,)))
else:
self.track_wow(settings, trail)
return
# or in speed view?
trail = [self.px_to_speed(click) for click in event.trail()]
trail = [x for x in trail if x is not None]
if trail:
# only interested in the Y difference, so we can move the selected speed trace up or down
a = trail[0]
b = trail[-1]
self.props.undo_stack.push(MoveAction(self.selected_markers, a[1], b[1]))
def track_wow(self, settings, trail):
spec = self.spectra[0]
track = wow_detection.Track(
settings.mode, spec.fft_storage[spec.key], trail, self.fft_size,
self.hop, spec.sr, settings.tolerance, settings.adapt)
marker = markers.TraceLine(self, track.times, track.freqs, auto_align=settings.auto_align)
self.props.undo_stack.push(AddAction((marker,)))
if __name__ == '__main__':
widgets.startup(MainWindow)