root/trunk/libffado/support/mixer-qt4/ffado/widgets/matrixmixer.py

Revision 2021, 12.4 kB (checked in by jwoithe, 9 years ago)

matrixmixer: complete initial implementation of optional mute functionality. A graphical indication of mute state (beyond the toggled menu item) is still needed.
matrixmixer: implement optional phase inversion interface. Again, a graphical indication of this state is still required.
rme: make use of the mute/invert functionality of the matrix mixer. Currently this is only connected for the input faders.

  • Property svn:mergeinfo set to
Line 
1 # coding=utf8
2 #
3 # Copyright (C) 2009 by Arnold Krille
4 #
5 # This file is part of FFADO
6 # FFADO = Free Firewire (pro-)audio drivers for linux
7 #
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 2 of the License, or
11 # (at your option) version 3 of the License.
12 #
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 #
21
22 from PyQt4 import QtGui, QtCore, Qt
23 import dbus, math, decimal
24
25 import logging
26 log = logging.getLogger("matrixmixer")
27
28 class ColorForNumber:
29     def __init__(self):
30         self.colors = dict()
31
32     def addColor(self, n, color):
33         self.colors[n] = color
34
35     def getColor(self, n):
36         #print "ColorForNumber.getColor( %g )" % (n)
37         keys = self.colors.keys()
38         keys.sort()
39         low = keys[-1]
40         high = keys[-1]
41         for i in range(len(keys)-1):
42             if keys[i] <= n and keys[i+1] > n:
43                 low = keys[i]
44                 high = keys[i+1]
45         #print "%g is between %g and %g" % (n, low, high)
46         f = 0
47         if high != low:
48             f = (n-low) / (high-low)
49         lc = self.colors[low]
50         hc = self.colors[high]
51         return QtGui.QColor(
52                 (1-f)*lc.red()   + f*hc.red(),
53                 (1-f)*lc.green() + f*hc.green(),
54                 (1-f)*lc.blue()  + f*hc.blue() )
55
56 class MixerNode(QtGui.QAbstractSlider):
57     def __init__(self, input, output, value, max, muted, inverted, parent):
58         QtGui.QAbstractSlider.__init__(self, parent)
59         #log.debug("MixerNode.__init__( %i, %i, %i, %i, %s )" % (input, output, value, max, str(parent)) )
60
61         self.pos = QtCore.QPointF(0, 0)
62         self.input = input
63         self.output = output
64         self.setOrientation(Qt.Qt.Vertical)
65         if max == -1:
66             max = pow(2, 16)-1
67         self.setRange(0, max)
68         self.setValue(value)
69         self.connect(self, QtCore.SIGNAL("valueChanged(int)"), self.internalValueChanged)
70
71         self.setSmall(False)
72
73         self.bgcolors = ColorForNumber()
74         self.bgcolors.addColor(             0.0, QtGui.QColor(  0,   0,   0))
75         self.bgcolors.addColor(             1.0, QtGui.QColor(  0,   0, 128))
76         self.bgcolors.addColor(   math.pow(2,6), QtGui.QColor(  0, 255,   0))
77         self.bgcolors.addColor(  math.pow(2,14), QtGui.QColor(255, 255,   0))
78         self.bgcolors.addColor(math.pow(2,16)-1, QtGui.QColor(255,   0,   0))
79
80         self.setContextMenuPolicy(Qt.Qt.ActionsContextMenu)
81         self.mapper = QtCore.QSignalMapper(self)
82         self.connect(self.mapper, QtCore.SIGNAL("mapped(const QString&)"), self.directValues)
83
84         self.spinbox = QtGui.QDoubleSpinBox(self)
85         self.spinbox.setRange(-40, 12)
86         self.spinbox.setSuffix(" dB")
87         self.connect(self.spinbox, QtCore.SIGNAL("valueChanged(const QString&)"), self.directValues)
88         action = QtGui.QWidgetAction(self)
89         action.setDefaultWidget(self.spinbox)
90         self.addAction(action)
91
92         for text in ["3 dB", "0 dB", "-3 dB", "-20 dB", "-inf dB"]:
93             action = QtGui.QAction(text, self)
94             self.connect(action, QtCore.SIGNAL("triggered()"), self.mapper, QtCore.SLOT("map()"))
95             self.mapper.setMapping(action, text)
96             self.addAction(action)
97
98         # Only show the mute menu item if a value has been supplied
99         self.mute_action = None
100         if (muted != None):
101             action = QtGui.QAction(text, self)
102             action.setSeparator(True)
103             self.addAction(action)
104             self.mute_action = QtGui.QAction("Mute", self)
105             self.mute_action.setCheckable(True)
106             self.mute_action.setChecked(muted)
107             self.connect(self.mute_action, QtCore.SIGNAL("triggered()"), self.mapper, QtCore.SLOT("map()"))
108             self.mapper.setMapping(self.mute_action, "Mute")
109             self.addAction(self.mute_action)
110
111         # Similarly, only show a phase inversion menu item if in use
112         self.inv_action = None
113         if (inverted != None):
114             if (muted == None):
115                 action = QtGui.QAction(text, self)
116                 action.setSeparator(True)
117                 self.addAction(action)
118             self.inv_action = QtGui.QAction("Invert", self)
119             self.inv_action.setCheckable(True)
120             self.inv_action.setChecked(inverted)
121             self.connect(self.inv_action, QtCore.SIGNAL("triggered()"), self.mapper, QtCore.SLOT("map()"))
122             self.mapper.setMapping(self.inv_action, "Invert")
123             self.addAction(self.inv_action)
124
125     def directValues(self,text):
126         #log.debug("MixerNode.directValues( '%s' )" % text)
127         if text == "Mute":
128             #log.debug("Mute %d" % self.mute_action.isChecked())
129             self.parent().mutes_interface.setValue(self.output, self.input, self.mute_action.isChecked())
130         elif text == "Invert":
131             log.debug("Invert %d" % self.inv_action.isChecked())
132             self.parent().inverts_interface.setValue(self.output, self.input, self.inv_action.isChecked())
133         else:
134             text = text.split(" ")[0].replace(",",".")
135             n = pow(10, (float(text)/20)) * pow(2,14)
136             #log.debug("%g" % n)
137             self.setValue(n)
138
139     def mousePressEvent(self, ev):
140         if ev.buttons() & Qt.Qt.LeftButton:
141             self.pos = ev.posF()
142             self.tmpvalue = self.value()
143             ev.accept()
144             #log.debug("MixerNode.mousePressEvent() %s" % str(self.pos))
145
146     def mouseMoveEvent(self, ev):
147         if hasattr(self, "tmpvalue") and self.pos is not QtCore.QPointF(0, 0):
148             newpos = ev.posF()
149             change = newpos.y() - self.pos.y()
150             #log.debug("MixerNode.mouseReleaseEvent() change %s" % (str(change)))
151             self.setValue( self.tmpvalue - math.copysign(pow(abs(change), 2), change) )
152             ev.accept()
153
154     def mouseReleaseEvent(self, ev):
155         if hasattr(self, "tmpvalue") and self.pos is not QtCore.QPointF(0, 0):
156             newpos = ev.posF()
157             change = newpos.y() - self.pos.y()
158             #log.debug("MixerNode.mouseReleaseEvent() change %s" % (str(change)))
159             self.setValue( self.tmpvalue - math.copysign(pow(abs(change), 2), change) )
160             self.pos = QtCore.QPointF(0, 0)
161             del self.tmpvalue
162             ev.accept()
163
164     def paintEvent(self, ev):
165         p = QtGui.QPainter(self)
166         rect = self.rect()
167         v = self.value()
168         color = self.bgcolors.getColor(v)
169         p.fillRect(rect, color)
170
171         if self.small:
172             return
173
174         if color.valueF() < 0.6:
175             p.setPen(QtGui.QColor(255, 255, 255))
176         else:
177             p.setPen(QtGui.QColor(0, 0, 0))
178         lv=decimal.Decimal('-Infinity')
179         if v != 0:
180             lv = 20 * math.log10(v * 1.0 / math.pow(2,14))
181             #log.debug("new value is %g dB" % lv)
182         text = "%.2g dB" % lv
183         if v == 0:
184             text = "-ꝏ dB"
185         p.drawText(rect, Qt.Qt.AlignCenter, QtCore.QString.fromUtf8(text))
186
187     def internalValueChanged(self, value):
188         #log.debug("MixerNode.internalValueChanged( %i )" % value)
189         if value is not 0:
190             dB = 20 * math.log10(value / math.pow(2,14))
191             if self.spinbox.value() is not dB:
192                 self.spinbox.setValue(dB)
193         self.emit(QtCore.SIGNAL("valueChanged"), (self.input, self.output, value) )
194         self.update()
195
196     def setSmall(self, small):
197         self.small = small
198         if small:
199             self.setMinimumSize(10, 10)
200         else:
201             fontmetrics = self.fontMetrics()
202             self.setMinimumSize(fontmetrics.boundingRect("-0.0 dB").size()*1.1)
203         self.update()
204
205
206 class MixerChannel(QtGui.QWidget):
207     def __init__(self, number, parent=None, name=""):
208         QtGui.QWidget.__init__(self, parent)
209         layout = QtGui.QGridLayout(self)
210         self.number = number
211         if name != "":
212             name = "\n(%s)" % name
213         self.name = name
214         self.lbl = QtGui.QLabel(self)
215         self.lbl.setAlignment(Qt.Qt.AlignCenter)
216         layout.addWidget(self.lbl, 0, 0, 1, 2)
217         self.hideChannel(False)
218
219         self.setContextMenuPolicy(Qt.Qt.ActionsContextMenu)
220
221         action = QtGui.QAction("Make this channel small", self)
222         action.setCheckable(True)
223         self.connect(action, QtCore.SIGNAL("triggered(bool)"), self.hideChannel)
224         self.addAction(action)
225
226     def hideChannel(self, hide):
227         if hide:
228             self.lbl.setText("%i" % self.number);
229         else:
230             self.lbl.setText("Ch. %i%s" % (self.number+1, self.name))
231         self.emit(QtCore.SIGNAL("hide"), self.number, hide)
232         self.update()
233
234
235
236 class MatrixMixer(QtGui.QWidget):
237     def __init__(self, servername, basepath, parent=None, sliderMaxValue=-1, mutespath=None, invertspath=None):
238         QtGui.QWidget.__init__(self, parent)
239         self.bus = dbus.SessionBus()
240         self.dev = self.bus.get_object(servername, basepath)
241         self.interface = dbus.Interface(self.dev, dbus_interface="org.ffado.Control.Element.MatrixMixer")
242
243         self.mutes_dev = None
244         self.mutes_interface = None
245         if (mutespath != None):
246             self.mutes_dev = self.bus.get_object(servername, mutespath)
247             self.mutes_interface = dbus.Interface(self.mutes_dev, dbus_interface="org.ffado.Control.Element.MatrixMixer")
248
249         self.inverts_dev = None
250         self.inverts_interface = None
251         if (invertspath != None):
252             self.inverts_dev = self.bus.get_object(servername, invertspath)
253             self.inverts_interface = dbus.Interface(self.inverts_dev, dbus_interface="org.ffado.Control.Element.MatrixMixer")
254
255         #palette = self.palette()
256         #palette.setColor(QtGui.QPalette.Window, palette.color(QtGui.QPalette.Window).darker());
257         #self.setPalette(palette)
258
259         cols = self.interface.getColCount()
260         rows = self.interface.getRowCount()
261         log.debug("Mixer has %i rows and %i columns" % (rows, cols))
262
263         layout = QtGui.QGridLayout(self)
264         self.setLayout(layout)
265
266         self.rowHeaders = []
267         self.columnHeaders = []
268         self.items = []
269
270         # Add row/column headers
271         for i in range(cols):
272             ch = MixerChannel(i, self, self.interface.getColName(i))
273             self.connect(ch, QtCore.SIGNAL("hide"), self.hideColumn)
274             layout.addWidget(ch, 0, i+1)
275             self.columnHeaders.append( ch )
276         for i in range(rows):
277             ch = MixerChannel(i, self, self.interface.getRowName(i))
278             self.connect(ch, QtCore.SIGNAL("hide"), self.hideRow)
279             layout.addWidget(ch, i+1, 0)
280             self.rowHeaders.append( ch )
281
282         # Add node-widgets
283         for i in range(rows):
284             self.items.append([])
285             for j in range(cols):
286                 mute_value = None
287                 if (self.mutes_interface != None):
288                     mute_value = self.mutes_interface.getValue(i,j)
289                 inv_value = None
290                 if (self.inverts_interface != None):
291                     inv_value = self.inverts_interface.getValue(i,j)
292                 node = MixerNode(j, i, self.interface.getValue(i,j), sliderMaxValue, mute_value, inv_value, self)
293                 self.connect(node, QtCore.SIGNAL("valueChanged"), self.valueChanged)
294                 layout.addWidget(node, i+1, j+1)
295                 self.items[i].append(node)
296
297         self.hiddenRows = []
298         self.hiddenCols = []
299
300
301     def checkVisibilities(self):
302         for x in range(len(self.items)):
303             for y in range(len(self.items[x])):
304                 self.items[x][y].setSmall(
305                         (x in self.hiddenRows)
306                         | (y in self.hiddenCols)
307                         )
308
309
310     def hideColumn(self, column, hide):
311         if hide:
312             self.hiddenCols.append(column)
313         else:
314             self.hiddenCols.remove(column)
315         self.checkVisibilities()
316     def hideRow(self, row, hide):
317         if hide:
318             self.hiddenRows.append(row)
319         else:
320             self.hiddenRows.remove(row)
321         self.checkVisibilities()
322
323     def valueChanged(self, n):
324         #log.debug("MatrixNode.valueChanged( %s )" % str(n))
325         self.interface.setValue(n[1], n[0], n[2])
326
327 #
328 # vim: et ts=4 sw=4 fileencoding=utf8
Note: See TracBrowser for help on using the browser.