-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodes.py
More file actions
259 lines (200 loc) · 7.6 KB
/
Copy pathmodes.py
File metadata and controls
259 lines (200 loc) · 7.6 KB
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
"""Instrument modes for channels.
"""
from abc import abstractmethod, ABCMeta
from collections import defaultdict
import copy
from midi import ControlChange, ProgramChange
class SubState(object):
__metaclass__ = ABCMeta
@abstractmethod
def __eq__(self, other):
raise NotImplementedError()
def __ne__(self, other):
return not self == other
@abstractmethod
def activate(self, client):
"""Interface to activate a substate.
parms:
client: [awb_client.Client]
"""
raise NotImplementedError()
class MidiState(SubState):
def __init__(self, portName, bank, program, channel=0):
assert isinstance(portName, str)
self.portName = portName
self.bank = bank
self.program = program
self.channel = channel
def __eq__(self, other):
return self is other or \
isinstance(other, MidiState) and \
self.portName == other.portName and \
self.bank == other.bank and \
self.program == other.program and \
self.channel == other.channel
def clone(self):
return MidiState(self.portName, self.bank, self.program, self.channel)
def activate(self, client):
port = client.seq.getPort(self.portName)
client.seq.sendEvent(ControlChange(0, self.channel, 0, self.bank >> 7),
port)
client.seq.sendEvent(ControlChange(0, self.channel, 32, self.bank & 127),
port)
client.seq.sendEvent(ProgramChange(0, self.channel, self.program), port)
class Route(object):
__metaclass__ = ABCMeta
@abstractmethod
def getCurrentOutbounds(self, client):
"""Returns the list of all outbound connections from the source port
as a list of keys.
Keys should be compatible with the values returned from getDestKey().
parms:
client: [awb_client.Client]
"""
@abstractmethod
def getSourceKey(self):
"""Returns the source key for a given connection."""
@abstractmethod
def getDestKey(self):
"""Returns the destination key for a given connection."""
@abstractmethod
def disconnect(self, client, destKey):
"""Remove the system connection from the source port to the
destination key.
Args:
client: [awb_client.Client] See getCurrentOutbounds.
destKey: [object] A key compatible with that returned from
getDestKey().
"""
@abstractmethod
def connect(self, client):
"""Connect the route.
Args:
client: [awb_client.Client] See getCurrentOutbounds.
"""
@abstractmethod
def __eq__(self, other):
pass
class RouteImpl(Route):
"""Partial route implementation for routes whose keys are simple
strings.
"""
def __init__(self, src, dst):
"""
Args:
src: [str] name of source port ("client/port")
dst: [str] name of destination port ("client/port")
"""
self.src = src
self.dst = dst
def getSourceKey(self):
return self.src
def getDestKey(self):
return self.dst
def __eq__(self, other):
return isinstance(other, RouteImpl) and \
self.src == other.src and \
self.dst == other.dst
class MidiRoute(RouteImpl):
def getCurrentOutbounds(self, client):
port = client.seq.getPort(self.src)
return [str(sub) for sub in client.seq.iterSubs(port)]
def disconnect(self, client, destKey):
client.seq.disconnect(client.seq.getPort(self.src),
client.seq.getPort(destKey)
)
def connect(self, client):
client.seq.connect(client.seq.getPort(self.src),
client.seq.getPort(self.dst)
)
class JackRoute(RouteImpl):
def getCurrentOutbounds(self, client):
return [
port.name for port in client.jack.get_all_connections(self.src)
]
def disconnect(self, client, destKey):
client.jack.disconnect(self.src, destKey)
def connect(self, client):
client.jack.connect(self.src, self.dst)
class Routing(SubState):
"""A set of jack audio or midi routes.
When establishing a set of routes, we clear all connections from all
source ports in the routing that are not already connected to their
destination ports.
"""
def __init__(self, *routes: Route):
"""Constructor.
args:
*routes: A list of routes.
"""
self.routes = list(routes)
def __eq__(self, other):
return self is other or \
self.routes == other.routes
def activate(self, client):
# Mapping of all routes for a given port. The keys can be whatever
# works for a route type, as returned by getSourceKey()
routesBySource = defaultdict(list)
for route in self.routes:
routesBySource[route.getSourceKey()].append(route)
# Go through the source ports, remove all existing connections that
# aren't in the new connections and add all new connections that aren't
# in the existing connections.
for routes in routesBySource.values():
# Convert the routes to a map indexed by destination ports.
routeMap = dict((route.getDestKey(), route) for route in routes)
# Go through the existing outbound connections, remove the ones
# that aren't in the set of desired routes and remove the ones
# that are from the set of routes that we need to connect.
for dest in routes[0].getCurrentOutbounds(client):
try:
del routeMap[dest]
except KeyError:
# We don't want to preserve this connection. Remove it.
routes[0].disconnect(client, dest)
# connect everything remaining in the routeMap (only the
# connections that we want but don't currently exist should
# remain).
for route in routeMap.values():
try:
route.connect(client)
except Exception as ex:
print('error connecting %s: %s' % (route, ex))
class StateVec(object):
"""A state vector.
xxx TransitionVect?
"""
def __init__(self, **kwargs):
self.__dict = kwargs
def __setattr__(self, name, val):
if name == '_StateVec__dict':
object.__setattr__(self, name, val)
else:
self.__dict[name] = val
def __delattr__(self, name):
del self.__dict[name]
def __getattr__(self, name):
if name == '_StateVec_dict':
raise AttributeError('_StateVec_dict')
try:
return self.__dict[name]
except KeyError as ex:
raise AttributeError(str(ex))
def __dir__(self):
return self.__dict.keys()
def __hasattr__(self, name):
return self.__dict.has_key(name)
def __setstate__(self, dict):
self.__dict = dict['_StateVec__dict']
def clone(self):
return copy.deepcopy(self)
def activate(self, client, curState):
"""Activate the reciver.
Args:
client: (awb_client.Client) The awb client, which gets passed to
all substates to be activated.
curState: (SubState or None) the existing state.
"""
for name, val in self.__dict.items():
if not hasattr(curState, name) or getattr(curState, name) != val:
val.activate(client)