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

Revision 2017, 10.8 kB (checked in by jwoithe, 9 years ago)

matrixmixer: more infrastructure to permit control of muting if supported by the hardware

  • 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, 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     def directValues(self,text):
112         #log.debug("MixerNode.directValues( '%s' )" % text)
113         if text == "Mute":
114             log.debug("Mute %d" % self.mute_action.isChecked())
115         else:
116             text = text.split(" ")[0].replace(",",".")
117             n = pow(10, (float(text)/20)) * pow(2,14)
118             #log.debug("%g" % n)
119             self.setValue(n)
120
121     def mousePressEvent(self, ev):
122         if ev.buttons() & Qt.Qt.LeftButton:
123             self.pos = ev.posF()
124             self.tmpvalue = self.value()
125             ev.accept()
126             #log.debug("MixerNode.mousePressEvent() %s" % str(self.pos))
127
128     def mouseMoveEvent(self, ev):
129         if hasattr(self, "tmpvalue") and self.pos is not QtCore.QPointF(0, 0):
130             newpos = ev.posF()
131             change = newpos.y() - self.pos.y()
132             #log.debug("MixerNode.mouseReleaseEvent() change %s" % (str(change)))
133             self.setValue( self.tmpvalue - math.copysign(pow(abs(change), 2), change) )
134             ev.accept()
135
136     def mouseReleaseEvent(self, ev):
137         if hasattr(self, "tmpvalue") and self.pos is not QtCore.QPointF(0, 0):
138             newpos = ev.posF()
139             change = newpos.y() - self.pos.y()
140             #log.debug("MixerNode.mouseReleaseEvent() change %s" % (str(change)))
141             self.setValue( self.tmpvalue - math.copysign(pow(abs(change), 2), change) )
142             self.pos = QtCore.QPointF(0, 0)
143             del self.tmpvalue
144             ev.accept()
145
146     def paintEvent(self, ev):
147         p = QtGui.QPainter(self)
148         rect = self.rect()
149         v = self.value()
150         color = self.bgcolors.getColor(v)
151         p.fillRect(rect, color)
152
153         if self.small:
154             return
155
156         if color.valueF() < 0.6:
157             p.setPen(QtGui.QColor(255, 255, 255))
158         else:
159             p.setPen(QtGui.QColor(0, 0, 0))
160         lv=decimal.Decimal('-Infinity')
161         if v != 0:
162             lv = 20 * math.log10(v * 1.0 / math.pow(2,14))
163             #log.debug("new value is %g dB" % lv)
164         text = "%.2g dB" % lv
165         if v == 0:
166             text = "-ꝏ dB"
167         p.drawText(rect, Qt.Qt.AlignCenter, QtCore.QString.fromUtf8(text))
168
169     def internalValueChanged(self, value):
170         #log.debug("MixerNode.internalValueChanged( %i )" % value)
171         if value is not 0:
172             dB = 20 * math.log10(value / math.pow(2,14))
173             if self.spinbox.value() is not dB:
174                 self.spinbox.setValue(dB)
175         self.emit(QtCore.SIGNAL("valueChanged"), (self.input, self.output, value) )
176         self.update()
177
178     def setSmall(self, small):
179         self.small = small
180         if small:
181             self.setMinimumSize(10, 10)
182         else:
183             fontmetrics = self.fontMetrics()
184             self.setMinimumSize(fontmetrics.boundingRect("-0.0 dB").size()*1.1)
185         self.update()
186
187
188 class MixerChannel(QtGui.QWidget):
189     def __init__(self, number, parent=None, name=""):
190         QtGui.QWidget.__init__(self, parent)
191         layout = QtGui.QGridLayout(self)
192         self.number = number
193         if name != "":
194             name = "\n(%s)" % name
195         self.name = name
196         self.lbl = QtGui.QLabel(self)
197         self.lbl.setAlignment(Qt.Qt.AlignCenter)
198         layout.addWidget(self.lbl, 0, 0, 1, 2)
199         self.hideChannel(False)
200
201         self.setContextMenuPolicy(Qt.Qt.ActionsContextMenu)
202
203         action = QtGui.QAction("Make this channel small", self)
204         action.setCheckable(True)
205         self.connect(action, QtCore.SIGNAL("triggered(bool)"), self.hideChannel)
206         self.addAction(action)
207
208     def hideChannel(self, hide):
209         if hide:
210             self.lbl.setText("%i" % self.number);
211         else:
212             self.lbl.setText("Ch. %i%s" % (self.number+1, self.name))
213         self.emit(QtCore.SIGNAL("hide"), self.number, hide)
214         self.update()
215
216
217
218 class MatrixMixer(QtGui.QWidget):
219     def __init__(self, servername, basepath, parent=None, sliderMaxValue=-1, mutespath=None):
220         QtGui.QWidget.__init__(self, parent)
221         self.bus = dbus.SessionBus()
222         self.dev = self.bus.get_object(servername, basepath)
223         self.interface = dbus.Interface(self.dev, dbus_interface="org.ffado.Control.Element.MatrixMixer")
224
225         self.mutes_dev = None
226         self.mutes_interface = None
227         if (mutespath != None):
228             self.mutes_dev = self.bus.get_object(servername, mutespath)
229             self.mutes_interface = dbus.Interface(self.mutes_dev, dbus_interface="org.ffado.Control.Element.MatrixMixer")
230
231         #palette = self.palette()
232         #palette.setColor(QtGui.QPalette.Window, palette.color(QtGui.QPalette.Window).darker());
233         #self.setPalette(palette)
234
235         cols = self.interface.getColCount()
236         rows = self.interface.getRowCount()
237         log.debug("Mixer has %i rows and %i columns" % (rows, cols))
238
239         layout = QtGui.QGridLayout(self)
240         self.setLayout(layout)
241
242         self.rowHeaders = []
243         self.columnHeaders = []
244         self.items = []
245
246         # Add row/column headers
247         for i in range(cols):
248             ch = MixerChannel(i, self, self.interface.getColName(i))
249             self.connect(ch, QtCore.SIGNAL("hide"), self.hideColumn)
250             layout.addWidget(ch, 0, i+1)
251             self.columnHeaders.append( ch )
252         for i in range(rows):
253             ch = MixerChannel(i, self, self.interface.getRowName(i))
254             self.connect(ch, QtCore.SIGNAL("hide"), self.hideRow)
255             layout.addWidget(ch, i+1, 0)
256             self.rowHeaders.append( ch )
257
258         # Add node-widgets
259         for i in range(rows):
260             self.items.append([])
261             for j in range(cols):
262                 node = MixerNode(j, i, self.interface.getValue(i,j), sliderMaxValue, None, self)
263                 self.connect(node, QtCore.SIGNAL("valueChanged"), self.valueChanged)
264                 layout.addWidget(node, i+1, j+1)
265                 self.items[i].append(node)
266
267         self.hiddenRows = []
268         self.hiddenCols = []
269
270
271     def checkVisibilities(self):
272         for x in range(len(self.items)):
273             for y in range(len(self.items[x])):
274                 self.items[x][y].setSmall(
275                         (x in self.hiddenRows)
276                         | (y in self.hiddenCols)
277                         )
278
279
280     def hideColumn(self, column, hide):
281         if hide:
282             self.hiddenCols.append(column)
283         else:
284             self.hiddenCols.remove(column)
285         self.checkVisibilities()
286     def hideRow(self, row, hide):
287         if hide:
288             self.hiddenRows.append(row)
289         else:
290             self.hiddenRows.remove(row)
291         self.checkVisibilities()
292
293     def valueChanged(self, n):
294         #log.debug("MatrixNode.valueChanged( %s )" % str(n))
295         self.interface.setValue(n[1], n[0], n[2])
296
297 #
298 # vim: et ts=4 sw=4 fileencoding=utf8
Note: See TracBrowser for help on using the browser.