forked from TheBigW/DRC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DRCTargetCurveUI.py
executable file
·212 lines (184 loc) · 7.48 KB
/
DRCTargetCurveUI.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
# DRCTargetCurveUI.py
# Copyright (C) 2013 - Tobias Wenig
# tobiaswenig@yahoo.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>
import inspect
import re
import gi
from gi.repository import RB
from gi.repository import Gtk
from DependsWrapper import DependsWrapperImpl
def loadTargetCurveFile(targetCurveFile):
pattern = re.compile("\n?(\d*\.?\d*)\s+(.*)\n?")
curveValueArray = []
with open(targetCurveFile) as fp:
for line in fp:
print("searching line:" + line)
values = pattern.findall(line)
print("found data:" + str(values))
curveValueArray.append(values[0])
return curveValueArray
def writeTargetCurveFile(targetCurveFile, data):
'''
enforce DRC curve expectations : The first line must have a frequency equal
to 0, the last line must have a frequency equal to BCSampleRate / 2 . A
post
filter definition file must have the following format:'''
data.sort()
if data[0][0] != 0:
data.insert(0, (0, -100.0))
if data[-1][0] != 22050:
data.append((22050, -100.0))
with open(targetCurveFile, "w") as fp:
for lineData in data:
print("{:.2f} {:.2f}".format(lineData[0], lineData[1]), file=fp)
class LabeledEdit:
def __init__(self, box, text, value, strDescription=None):
label = Gtk.Label(text)
box.add(label)
self.entry = Gtk.Entry()
if strDescription is not None:
self.entry.set_tooltip_text(strDescription)
label.set_tooltip_text(strDescription)
self.entry.set_text(value)
box.add(self.entry)
newToolTip = Gtk.Tooltip()
print(inspect.getdoc(newToolTip))
# newToolTip.set_tip(self.entry, "some tooltip")
class AddDialog(Gtk.Dialog):
params = []
def __init__(self, parent, params=None):
if None != params:
self.params = params
else:
self.params = [(100, 0)]
super(Gtk.Dialog, self).__init__()
okBtn = self.add_button(Gtk.STOCK_OK, Gtk.ResponseType.OK)
okBtn.connect("clicked", self.on_ok)
self.set_default_size(150, 100)
box = self.get_content_area()
self.freqLE = LabeledEdit(box, "frequency", str(self.params[0][0]),
"Frequency of the EQBand")
self.gainLE = LabeledEdit(box, "Gain", str(self.params[0][1]),
"Gain of the EQBand")
self.show_all()
def on_ok(self, param):
self.params = [(float(self.freqLE.entry.get_text()),
float(self.gainLE.entry.get_text()))]
class EQGroupControl(Gtk.VBox):
def __init__(self, frequency, amplitude, parent):
super(Gtk.VBox, self).__init__(False)
self.frequency = frequency
self.parent = parent
self.slider = Gtk.VScale()
self.slider.set_range(-100, 0)
self.slider.set_inverted(True)
self.slider.set_value_pos(Gtk.PositionType.TOP)
self.slider.set_size_request(100, 300)
self.slider.set_value(amplitude)
self.labelFreq = Gtk.Label("f=" + str(frequency) + "Hz")
remBtn = Gtk.Button("Remove")
remBtn.connect("clicked", self.on_remove_band)
self.add(self.slider)
self.add(self.labelFreq)
self.add(remBtn)
self.show_all()
def on_remove_band(self, param):
self.parent.on_remove_band(self)
def getAmplitude(self):
return self.slider.get_value()
def getFrequency(self):
return float(self.frequency)
class EQControl():
def __init__(self, targetCurveFile, parent):
# self.set_title( "N Bands parametric EQ" )
self.targetCurveFilename = targetCurveFile
addBtn = self.uibuilder = Gtk.Builder()
self.uibuilder.add_from_file(
DependsWrapperImpl.find_plugin_file(parent, "DRCUI.glade"))
self.dlg = self.uibuilder.get_object("EQDlg")
addBtn = self.uibuilder.get_object("buttonAddEQBand")
addBtn.connect("clicked", self.add_new_eq_band)
closeBtn = self.uibuilder.get_object("button_CloseEQ")
closeBtn.connect("clicked", self.on_Ok)
saveTargetCurveButton = self.uibuilder.get_object(
"saveTargetCurveButton")
saveTargetCurveButton.connect("file-set", self.on_file_selected)
data = loadTargetCurveFile(targetCurveFile)
self.eqBox = self.uibuilder.get_object("EQBox")
self.rebuild_eq_controls(data)
def getTargetCurveFile(self):
return self.targetCurveFilename
def on_file_selected(self, widget):
self.targetCurveFilename = widget.get_filename()
params = self.getEqParamListFromUI()
# open file safe dialog and safe
writeTargetCurveFile(self.targetCurveFilename, params)
def rebuild_eq_controls(self, params):
numEqBands = len(params)
# remove all controls
eqBandctrls = self.eqBox.get_children()
print("children : ", len(eqBandctrls))
numBands = len(eqBandctrls)
for i in range(0, numBands):
self.eqBox.remove(eqBandctrls[i])
for i in range(0, numEqBands):
self.eqBox.add(
EQGroupControl(params[i][0], float(params[i][1]), self))
self.eqBox.show_all()
def getEqParamListFromUI(self):
params = []
eqBandctrls = self.eqBox.get_children()
print("children : ", len(eqBandctrls))
numBands = len(eqBandctrls)
for i in range(0, numBands):
control = eqBandctrls[i]
params.append((control.getFrequency(), control.getAmplitude()))
# update UI in case of loudnes adaptation
print("num bands :", len(params))
return params
def add_new_eq_band(self, param):
dlg = AddDialog(self)
if dlg.run() == Gtk.ResponseType.OK:
params = self.getEqParamListFromUI()
frequency = dlg.params[0][0]
if frequency <= 22050:
print("params : " + str(params))
params.append(dlg.params[0])
print("params : " + str(params))
params.sort(key=lambda tup: tup[0])
self.rebuild_eq_controls(params)
else:
print("invalid frequency > 22050")
dlg.destroy()
def on_remove_band(self, eqbandCtrl):
params = self.getEqParamListFromUI()
numParams = len(params)
param = None
for i in range(0, numParams):
print("checking : " + str(eqbandCtrl.frequency) + " for : " + str(
params[i]))
if eqbandCtrl.getFrequency() == params[i][0]:
param = params[i]
break
print("attempt to remove : " + str(param))
params.remove(param)
self.rebuild_eq_controls(params)
def on_Ok(self, param):
self.dlg.response(Gtk.ResponseType.OK)
self.dlg.set_visible(False)
def run(self):
print("running dlg...")
return self.dlg.run()