1 |
# |
---|
2 |
# Copyright (C) 2009 by Arnold Krille |
---|
3 |
# |
---|
4 |
# This file is part of FFADO |
---|
5 |
# FFADO = Free Firewire (pro-)audio drivers for linux |
---|
6 |
# |
---|
7 |
# This program is free software: you can redistribute it and/or modify |
---|
8 |
# it under the terms of the GNU General Public License as published by |
---|
9 |
# the Free Software Foundation, either version 2 of the License, or |
---|
10 |
# (at your option) version 3 of the License. |
---|
11 |
# |
---|
12 |
# This program is distributed in the hope that it will be useful, |
---|
13 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
14 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
15 |
# GNU General Public License for more details. |
---|
16 |
# |
---|
17 |
# You should have received a copy of the GNU General Public License |
---|
18 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
19 |
# |
---|
20 |
|
---|
21 |
from PyQt4 import QtGui, QtCore |
---|
22 |
import dbus |
---|
23 |
|
---|
24 |
import logging |
---|
25 |
log = logging.getLogger("crossbarrouter") |
---|
26 |
|
---|
27 |
class VuMeter(QtGui.QFrame): |
---|
28 |
def __init__(self, interface, output, input=None, parent=None): |
---|
29 |
QtGui.QFrame.__init__(self, parent) |
---|
30 |
self.setLineWidth(1) |
---|
31 |
self.setFrameStyle(QtGui.QFrame.Panel|QtGui.QFrame.Sunken) |
---|
32 |
self.setMinimumSize(20, 20) |
---|
33 |
|
---|
34 |
self.level = 0 |
---|
35 |
|
---|
36 |
self.interface = interface |
---|
37 |
|
---|
38 |
self.output = output |
---|
39 |
if input is None: |
---|
40 |
input = int(self.interface.getSourceForDestination(output)) |
---|
41 |
self.setInput(input) |
---|
42 |
|
---|
43 |
def setInput(self, input): |
---|
44 |
#log.debug("VuMeter.setInput() %i->%i" % (self.output, input)) |
---|
45 |
self.input = input |
---|
46 |
|
---|
47 |
def updateLevel(self, value): |
---|
48 |
self.level = value |
---|
49 |
self.update() |
---|
50 |
|
---|
51 |
def paintEvent(self, event): |
---|
52 |
p = QtGui.QPainter(self) |
---|
53 |
value = self.level/4096 |
---|
54 |
r = self.rect() |
---|
55 |
r.setHeight(r.height() * value) |
---|
56 |
r.moveBottom(self.rect().height()) |
---|
57 |
p.fillRect(r, self.palette().highlight()) |
---|
58 |
|
---|
59 |
class OutputSwitcher(QtGui.QFrame): |
---|
60 |
""" |
---|
61 |
The name is a bit misleading. This widget selectes sources for a specified |
---|
62 |
destination. |
---|
63 |
|
---|
64 |
In mixer-usage this widget is at the top of the input-channel. Because the input |
---|
65 |
of the mixer is an available output from the routers point. |
---|
66 |
""" |
---|
67 |
def __init__(self, interface, outname, parent): |
---|
68 |
QtGui.QFrame.__init__(self, parent) |
---|
69 |
self.interface = interface |
---|
70 |
self.outname = outname |
---|
71 |
|
---|
72 |
self.setLineWidth(1) |
---|
73 |
self.setFrameStyle(QtGui.QFrame.Sunken|QtGui.QFrame.Panel) |
---|
74 |
|
---|
75 |
self.layout = QtGui.QGridLayout(self) |
---|
76 |
self.setLayout(self.layout) |
---|
77 |
|
---|
78 |
self.lbl = QtGui.QLabel(self.outname, self) |
---|
79 |
self.layout.addWidget(self.lbl, 0, 0) |
---|
80 |
|
---|
81 |
self.vu = VuMeter(self.interface, self.interface.getDestinationIndex(outname), parent=self) |
---|
82 |
self.layout.addWidget(self.vu, 0, 1, 2, 1) |
---|
83 |
|
---|
84 |
self.btn = QtGui.QPushButton("Sel.", self) |
---|
85 |
self.layout.addWidget(self.btn, 1, 0) |
---|
86 |
|
---|
87 |
self.exclusiveGroup = QtGui.QActionGroup(self) |
---|
88 |
|
---|
89 |
sources = self.interface.getSourceNames() |
---|
90 |
self.ingroups = {} |
---|
91 |
for ch in sources: |
---|
92 |
tmp = str(ch).split(":")[0] |
---|
93 |
if tmp not in self.ingroups: |
---|
94 |
self.ingroups[tmp] = 0 |
---|
95 |
self.ingroups[tmp] += 1 |
---|
96 |
#log.debug("Detected ingroups: %s" % str(self.ingroups)) |
---|
97 |
|
---|
98 |
self.menu = QtGui.QMenu(self) |
---|
99 |
self.btn.setMenu(self.menu) |
---|
100 |
|
---|
101 |
action = QtGui.QAction("Disconnect", self) |
---|
102 |
action.setCheckable(True) |
---|
103 |
self.connect(action, QtCore.SIGNAL("toggled(bool)"), self.disconnectRoute) |
---|
104 |
self.exclusiveGroup.addAction(action) |
---|
105 |
self.menu.addAction(action) |
---|
106 |
|
---|
107 |
for group in self.ingroups: |
---|
108 |
submenu = InGroupMenu(self.interface, self.outname, group, self.ingroups[group], self, self.exclusiveGroup) |
---|
109 |
self.menu.addMenu(submenu) |
---|
110 |
|
---|
111 |
def peakValue(self, value): |
---|
112 |
self.vu.updateLevel(value) |
---|
113 |
|
---|
114 |
def disconnectRoute(self, checked): |
---|
115 |
log.debug("disconnectRoute( %s ) sender: %s" % (str(checked),str(self.sender()))) |
---|
116 |
dest = self.interface.getDestinationIndex(self.outname) |
---|
117 |
src = self.interface.getSourceName( self.interface.getSourceForDestination( dest ) ) |
---|
118 |
#log.debug(" source=%s destination=%s possible? %s" % (src, self.outname, self.interface.canConnectNamed(src, self.outname))) |
---|
119 |
if not self.interface.setConnectionStateNamed(src, self.outname, False): |
---|
120 |
log.debug(" Changing the connection table was successfull.") |
---|
121 |
else: |
---|
122 |
log.warning(" Failed to change the connection table!") |
---|
123 |
self.peakValue(0) |
---|
124 |
|
---|
125 |
class InGroupMenu(QtGui.QMenu): |
---|
126 |
def __init__(self, interface, outname, inname, insize, parent, exclusiveGroup = None): |
---|
127 |
QtGui.QMenu.__init__(self, inname, parent) |
---|
128 |
self.interface = interface |
---|
129 |
self.outname = str(outname) |
---|
130 |
self.inname = str(inname) |
---|
131 |
self.insize = insize |
---|
132 |
self.connect(self, QtCore.SIGNAL("aboutToShow()"), self.showMenu) |
---|
133 |
|
---|
134 |
self.lock = False |
---|
135 |
if exclusiveGroup: |
---|
136 |
self.exclusiveGroup = exclusiveGroup |
---|
137 |
else: |
---|
138 |
self.exclusiveGroup = QtGui.QActionGroup(self) |
---|
139 |
|
---|
140 |
def showMenu(self): |
---|
141 |
#print "About to show the menu" |
---|
142 |
if len(self.actions()) < self.insize+1: |
---|
143 |
# Do a lazy init of the sub-items. |
---|
144 |
for i in range(self.insize): |
---|
145 |
action = QtGui.QAction("%s:%02i" % (self.inname,i), self) |
---|
146 |
action.setCheckable(True) |
---|
147 |
self.connect(action, QtCore.SIGNAL("toggled(bool)"), self.connectionSwitched) |
---|
148 |
self.exclusiveGroup.addAction(action) |
---|
149 |
self.addAction(action) |
---|
150 |
idx = self.interface.getDestinationIndex(self.outname) |
---|
151 |
sourceidx = self.interface.getSourceForDestination(idx) |
---|
152 |
#print self.interface.getConnectionState(sourceidx, idx) |
---|
153 |
source = self.interface.getSourceName(sourceidx) |
---|
154 |
self.lock = True |
---|
155 |
for action in self.actions(): |
---|
156 |
action.setChecked(action.text() == source) |
---|
157 |
self.lock = False |
---|
158 |
|
---|
159 |
def connectionSwitched(self, checked): |
---|
160 |
if self.lock: return |
---|
161 |
#log.debug("connectionSwitched( %s ) sender: %s" % (str(checked),str(self.sender()))) |
---|
162 |
inname = str(self.sender().text()) |
---|
163 |
#log.debug(" source=%s destination=%s possible? %s" % (inname, self.outname, self.interface.canConnectNamed(inname, self.outname))) |
---|
164 |
if not self.interface.setConnectionStateNamed(inname, self.outname, checked): |
---|
165 |
log.warning(" Failed to change the routing.") |
---|
166 |
|
---|
167 |
|
---|
168 |
class CrossbarRouter(QtGui.QWidget): |
---|
169 |
def __init__(self, servername, basepath, parent=None): |
---|
170 |
QtGui.QWidget.__init__(self, parent); |
---|
171 |
self.bus = dbus.SessionBus() |
---|
172 |
self.dev = self.bus.get_object(servername, basepath) |
---|
173 |
self.interface = dbus.Interface(self.dev, dbus_interface="org.ffado.Control.Element.CrossbarRouter") |
---|
174 |
|
---|
175 |
log.info(self.interface.getDestinations()) |
---|
176 |
|
---|
177 |
destinations = self.interface.getDestinationNames() |
---|
178 |
self.outgroups = [] |
---|
179 |
for ch in destinations: |
---|
180 |
tmp = str(ch).split(":")[0] |
---|
181 |
if not tmp in self.outgroups: |
---|
182 |
self.outgroups.append(tmp) |
---|
183 |
|
---|
184 |
self.biglayout = QtGui.QVBoxLayout(self) |
---|
185 |
self.setLayout(self.biglayout) |
---|
186 |
|
---|
187 |
self.toplayout = QtGui.QHBoxLayout() |
---|
188 |
self.biglayout.addLayout(self.toplayout) |
---|
189 |
|
---|
190 |
btn = QtGui.QPushButton("Switch VU", self) |
---|
191 |
btn.setCheckable(True) |
---|
192 |
btn.setChecked(True) |
---|
193 |
self.connect(btn, QtCore.SIGNAL("toggled(bool)"), self.runVu) |
---|
194 |
self.toplayout.addWidget(btn) |
---|
195 |
|
---|
196 |
self.layout = QtGui.QGridLayout() |
---|
197 |
self.biglayout.addLayout(self.layout) |
---|
198 |
|
---|
199 |
self.switchers = {} |
---|
200 |
for out in destinations: |
---|
201 |
btn = OutputSwitcher(self.interface, out, self) |
---|
202 |
self.layout.addWidget(btn, int(out.split(":")[-1]) + 1, self.outgroups.index(out.split(":")[0])) |
---|
203 |
self.switchers[self.interface.getDestinationIndex(out)] = btn |
---|
204 |
|
---|
205 |
self.timer = QtCore.QTimer(self) |
---|
206 |
self.timer.setInterval(200) |
---|
207 |
self.connect(self.timer, QtCore.SIGNAL("timeout()"), self.updateLevels) |
---|
208 |
self.runVu(True) |
---|
209 |
|
---|
210 |
def runVu(self, run=True): |
---|
211 |
if run: |
---|
212 |
self.timer.start() |
---|
213 |
else: |
---|
214 |
self.timer.stop() |
---|
215 |
for sw in self.switchers: |
---|
216 |
self.switchers[sw].peakValue(0) |
---|
217 |
|
---|
218 |
def updateLevels(self): |
---|
219 |
#print "CrossbarRouter.updateLevels()" |
---|
220 |
peakvalues = self.interface.getPeakValues() |
---|
221 |
#print "Got %i peaks" % len(peakvalues) |
---|
222 |
for peak in peakvalues: |
---|
223 |
self.switchers[peak[0]].peakValue(peak[1]) |
---|
224 |
|
---|
225 |
# |
---|
226 |
# vim: sw=4 ts=4 et |
---|