forked from libmapper/webmapper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmapperstorage.py
executable file
·311 lines (281 loc) · 13.2 KB
/
mapperstorage.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
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#!/usr/bin/env python
import json, re
import mapper
#for debugging
import pdb
def serialise(monitor, device):
sources = {}
destinations = {}
connections = {}
new_connections = []
next_src = 0
next_dest = 0
next_connection = 0
modeStr = {mapper.MO_BYPASS: 'bypass',
mapper.MO_LINEAR: 'linear',
mapper.MO_CALIBRATE: 'calibrate',
mapper.MO_EXPRESSION: 'expression'}
boundStr = {mapper.BA_NONE: 'none',
mapper.BA_MUTE: 'mute',
mapper.BA_CLAMP: 'clamp',
mapper.BA_FOLD: 'fold',
mapper.BA_WRAP: 'wrap'}
for c in monitor.db.connections_by_device_name(device):
this_connection = {
'src': [ c['src_name'] ],
'dest': [ c['dest_name'] ],
'mute': c['muted'],
'mode': modeStr[c['mode']],
'srcMin': c['src_min'],
'srcMax': c['src_max'],
'destMin': c['dest_min'],
'destMax': c['dest_max'],
'expression': c['expression'],
'boundMin': boundStr[c['bound_min']],
'boundMax': boundStr[c['bound_max']]
}
# To get proper expression nomenclature
# dest[0] = src[0] NOT y = x
this_connection['expression'] = this_connection['expression'].replace('y', 'dest[0]').replace('x', 'src[0]')
new_connections.append(this_connection);
"""
# does the following source already have something in the string?
if not sources.has_key(c['src_name']):
sources[c['src_name']] = {
'id': 's%d'%next_src,
'device': c['src_name'].split('/')[1],
'parameter': '/'+'/'.join(c['src_name'].split('/')[2:])
}
next_src += 1
# does the following destination already have something in the string?
if not destinations.has_key(c['dest_name']):
destinations[c['dest_name']] = {
'id': 'd%s'%next_dest,
'device': c['dest_name'].split('/')[1],
'parameter': '/'+'/'.join(c['dest_name'].split('/')[2:])
}
next_dest += 1
connections[(c['src_name'],c['dest_name'])] = {
'scaling': modeStr[c['mode']],
'range': ' '.join(map(lambda x: '-' if x==None else str(x),
c['range'])),
'expression': (c['expression'].
replace('x', sources[c['src_name']]['id']).
replace('y', destinations[c['dest_name']]['id'])),
'boundMin': boundStr[c['bound_min']],
'boundMax': boundStr[c['bound_max']],
'muted': c['muted'],
}"""
contents = {"fileversion": "2.1", "mapping": {
"connections": new_connections
}
}
"""
contents = {"mapping": {"fileversion": "dot-1",
"sources": sources.values(),
"destinations": destinations.values(),
"connections": connections.values()}}"""
return json.dumps(contents, indent=4)
def deserialise(monitor, mapping_json, devices):
js = json.loads(mapping_json)
#The version we're currently working with
version = '';
if 'fileversion' in js:
version = js['fileversion']
elif 'mapping' in js and 'fileversion' in js['mapping']:
version = js['mapping']['fileversion']
modeIdx = {'bypass': mapper.MO_BYPASS,
'linear': mapper.MO_LINEAR,
'calibrate': mapper.MO_CALIBRATE,
'expression': mapper.MO_EXPRESSION}
boundIdx = {'none': mapper.BA_NONE,
'mute': mapper.BA_MUTE,
'clamp': mapper.BA_CLAMP,
'fold': mapper.BA_FOLD,
'wrap': mapper.BA_WRAP}
m = js['mapping']
# This is a version 2.1 save file
if version == '2.1':
for c in m['connections']:
# First, make certain to create necessary links
# Since we're accomodating many-to-many connections, etc.
# sources and destinations are lists, devices are split from the second '/' character
srcdevs = devices['sources']
destdevs = devices['destinations']
links = [( str(x), str(y) ) for x in srcdevs for y in destdevs]
# Don't want to explicitly create links now
"""
for l in links:
# Only make a link if it does not already exist
if not monitor.db.get_link_by_src_dest_names(l[0], l[1]):
monitor.link(l[0], l[1])"""
#The name of the source signal (without device, assuming 1 to 1 for now)
srcsig = str(c['src'][0]).split('/')[2]
#And the destination
destsig = str(c['dest'][0]).split('/')[2]
for l in links:
if monitor.db.get_link_by_src_dest_names(l[0], l[1]):
args = (str(l[0]+'/'+srcsig),
str(l[1]+'/'+destsig),
{})
if 'mode' in c:
args[2]['mode'] = modeIdx[c['mode']]
if 'expression' in c:
args[2]['expression'] = str(c['expression']
.replace('src[0]', 'x')
.replace('dest[0]', 'y'))
if 'srcMin' in c:
args[2]['src_min'] = c['srcMin']
if 'srcMax' in c:
args[2]['src_max'] = c['srcMax']
if 'destMin' in c:
args[2]['dest_min'] = c['destMin']
if 'destMax' in c:
args[2]['dest_max'] = c['destMax']
if 'boundMin' in c:
args[2]['bound_min'] = boundIdx[c['boundMin']]
if 'boundMax' in c:
args[2]['bound_max'] = boundIdx[c['boundMax']]
if 'mute' in c:
args[2]['muted'] = c['mute']
# If connection already exists, use 'modify', otherwise 'connect'.
# Assumes 1 to 1, again
cs = list(monitor.db.connections_by_device_and_signal_names(
(l[0]).split('/')[1], srcsig,
(l[1]).split('/')[1], destsig) )
if len(cs) > 0:
args[2]['src_name'] = args[0]
args[2]['dest_name'] = args[1]
monitor.modify(args[2])
else:
monitor.connect(*args)
# This is a version 2.0 save file
elif version == '2.0':
for c in m['connections']:
# First, make certain to create necessary links
# Since we're accomodating many-to-many connections, etc.
# sources and destinations are lists, devices are split from the second '/' character
srcdevs = devices['sources']
destdevs = devices['destinations']
links = [( str(x), str(y) ) for x in srcdevs for y in destdevs]
# Don't want to explicitly create links now
"""
for l in links:
# Only make a link if it does not already exist
if not monitor.db.get_link_by_src_dest_names(l[0], l[1]):
monitor.link(l[0], l[1])"""
#The name of the source signal (without device, assuming 1 to 1 for now)
srcsig = str(c['src'][0]).split('/')[2]
#And the destination
destsig = str(c['dest'][0]).split('/')[2]
# The expression, agian we're simply replacing based on an assumption of 1 to 1 connections
e = str(c['expression'].replace('src[0]', 'x')
.replace('dest[0]', 'y'))
for l in links:
if monitor.db.get_link_by_src_dest_names(l[0], l[1]):
args = (str(l[0]+'/'+srcsig),
str(l[1]+'/'+destsig),
{})
if 'mode' in c:
args[2]['mode'] = modeIdx[c['mode']]
if 'expression' in c:
args[2]['expression'] = str(c['expression']
.replace('src[0]', 'x')
.replace('dest[0]', 'y'))
if 'boundMin' in c:
args[2]['bound_min'] = boundIdx[c['boundMin']]
if 'boundMax' in c:
args[2]['bound_max'] = boundIdx[c['boundMax']]
if 'mute' in c:
args[2]['muted'] = c['mute']
if 'range' in c and len(c['range']) == 4:
if c['range'][0] != '-':
args[2]['src_min'] = c['range'][0]
if c['range'][1] != '-':
args[2]['src_max'] = c['range'][1]
if c['range'][2] != '-':
args[2]['dest_min'] = c['range'][2]
if c['range'][3] != '-':
args[2]['dest_max'] = c['range'][3]
# If connection already exists, use 'modify', otherwise 'connect'.
# Assumes 1 to 1, again
cs = list(monitor.db.connections_by_device_and_signal_names(
(l[0]).split('/')[1], srcsig,
(l[1]).split('/')[1], destsig) )
if len(cs) > 0:
args[2]['src_name'] = args[0]
args[2]['dest_name'] = args[1]
monitor.modify(args[2])
else:
monitor.connect(*args)
# This is a version 1 save file
elif version == 'dot-1':
srcs = {}
dests = {}
for s in m['sources']:
srcs[s['id']] = s
for d in m['destinations']:
dests[d['id']] = d
for c in m['connections']:
s = [srcs[s] for s in srcs.keys()
if (s in re.findall('(s\\d+)', c['expression']))]
d = [dests[d] for d in dests.keys()
if (d in re.findall('(d\\d+)', c['expression']))]
links = [(x,y) for x in s for y in d]
if len(links)>1:
print 'Error, multiple links specified for connection', c
continue
if len(links)<1:
# If not enough sources or destinations are specified in the
# expression string, ignore this connection.
# This can happen e.g. if expression is a constant: "d1=1"
continue
link = links[0]
srcdev = str('/'+link[0]['device'])
destdev = str('/'+link[1]['device'])
# Note: do not create link
# Range may have integers, floats, or '-' strings. When
# converting to a list of floats, pass through anything that
# doesn't parse as a float or int.
srcsig = srcdev + str(link[0]['parameter'])
destsig = destdev + str(link[1]['parameter'])
args = (srcdev + str(link[0]['parameter']),
destdev + str(link[1]['parameter']),
{})
if 'scaling' in c:
args[2]['mode'] = modeIdx[c['scaling']]
if 'expression' in c:
args[2]['expression'] = str(c['expression']
.replace(link[0]['id'], 'x')
.replace(link[1]['id'], 'y'))
if 'clipMin' in c:
args[2]['bound_min'] = boundIdx[c['clipMin']]
if 'clipMax' in c:
args[2]['bound_max'] = boundIdx[c['clipMax']]
if 'muted' in c:
args[2]['muted'] = c['muted']
if 'range' in c and len(c['range']) == 4:
if c['range'][0] != '-':
args[2]['src_min'] = c['range'][0]
if c['range'][1] != '-':
args[2]['src_max'] = c['range'][1]
if c['range'][2] != '-':
args[2]['dest_min'] = c['range'][2]
if c['range'][3] != '-':
args[2]['dest_max'] = c['range'][3]
# If connection already exists, use 'modify', otherwise 'connect'.
cs = list(monitor.db.get_connections_by_device_and_signal_names(
str(link[0]['device']), str(link[0]['parameter']),
str(link[1]['device']), str(link[1]['parameter'])))
if len(cs)>0:
args[2]['src_name'] = args[0]
args[2]['dest_name'] = args[1]
monitor.modify(args[2])
else:
monitor.connect(*args)
else:
print 'Unknown file version'
# TODO: Strictly speaking we should wait until links are
# acknowledged before continuing with a connection. An
# asynchronous approach would be necessary for this, by
# passing a continuation to the monitor's link handler.