-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathrouter.py
306 lines (259 loc) · 9.65 KB
/
router.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
"""
pilink - OSC to midi for the Raspberry Pi
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
import os
import sys
import time
import socket
import struct
import traceback
import unittest
import Queue
import config
import stats
outq = Queue.Queue()
def parseHexValue(v) :
if v.startswith("0x") or v.startswith("0X") :
return int(v,16)
else :
return int(v,10)
def oscParser(msg, parsedMsg):
""" parse the osc buffer; from http://www.cs.cmu.edu/~15104/week11/server.py
"""
end_of_addr = msg.find('\000')
if end_of_addr == -1:
raise EOFError("Unable to find end of message")
address = msg[ : end_of_addr]
# Since the RaspberryPi doesn't have any time synchro, bundle
# time frames are not yet processed.
if address == "#bundle" :
msgSize = struct.unpack('!i', msg[16 : 20])[0]
oscParser(msg[20:20+msgSize], parsedMsg)
return
ret = [address]
# skip past the address
tsx = end_of_addr # find type string index tsx
while tsx < len(msg) and msg[tsx] == '\000':
tsx += 1
if tsx >= len(msg):
actual_types = ""
else: # i will be index into msg for parameters
i = msg.find('\000', tsx)
if i > tsx:
# skip the comma
actual_types = msg[tsx + 1 : i]
else:
i = len(msg)
# skip past the type string to the next multiple of 4 bytes:
while i < len(msg) and i % 4 > 0:
i += 1
for typechar in actual_types:
if typechar == 'i':
if i + 4 <= len(msg):
intval = struct.unpack('!i', msg[i : i + 4])
i += 4
ret.append(intval[0])
elif typechar == 'f':
if i + 4 <= len(msg):
floval = struct.unpack('!f', msg[i : i + 4])
i += 4
ret.append(floval[0])
elif typechar in "sSc":
i2 = msg.find('\000', i)
if i2 != -1:
strval = msg[i : i2]
i = i2 + 1
ret.append(strval[0])
elif typechar == 'd':
if i + 4 <= len(msg):
floval = struct.unpack('!d', msg[i : i + 8])
i += 8
ret.append(floval[0])
parsedMsg.append(ret)
def parseMidiMsg(msg, replaceValues) :
midiMsg = []
msg = msg.replace(',', ' ') # touchosc sends ' ' ; lemur sends ','
for i in msg.split() :
first = i[0].lower()
if first in "xyz" :
replacePos = ord(first) - ord('x')
if len(i) > 1 and i[1] == "(" :
rng = map(parseHexValue, i[2:-1].split(".."))
ratio = rng[1] - rng[0]
midiMsg.append(int(replaceValues[replacePos] * ratio) + rng[0])
else :
midiMsg.append(int(replaceValues[replacePos] * 127))
else :
midiMsg.append(parseHexValue(i))
return midiMsg
def osc2Midi(msg) :
oscToken = msg[0].split("/")[1:]
token = oscToken.pop(0)
midiMsg = []
# missing link protocol
if token == 'midi' :
token = oscToken.pop(0)
if token == 'z' :
while len(oscToken) != 0 :
token = oscToken.pop(0)
midiMsg.extend(parseMidiMsg(token, msg[1:]))
else :
midiMsg = parseMidiMsg(token, msg[1:])
# TouchOSC and Reaktor protocol
elif token.isdigit() :
chl = int(token)
if chl >= 0 and chl < 16 :
if len(oscToken) == 0 :
return midiMsg
else :
token = oscToken.pop()
# Reaktor vs TouchOSC protocol
if token.find('_') >= 0 :
token.replace('_', ' ')
msgToken = token.split()
action = msgToken.pop()
### TODO add more OSC actions here
if action == 'note' :
midiMsg = [ ( '0x90' + chl ), int(msgToken[0]), int(msgToken[1]) ]
else :
stats.error("Unknown OSC action %s" % action)
return midiMsg
# #########################################################################
def oscInput() :
networkConn = -1
lastError = 0
while 1 :
try :
networkConn = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
networkConn.bind(("0.0.0.0", config.receiveport))
print("LISTENING UDP PORT %d" % config.receiveport)
while 1 :
midiMsg = [] # send empty midi message
msg = networkConn.recvfrom(4096)[0]
try :
parsedMsg = []
oscParser(msg, parsedMsg)
stats.oscin(str(parsedMsg))
for i in parsedMsg :
midiMsg.extend(osc2Midi(i))
except Exception as e :
stats.error(e)
exc_type, exc_value, exc_traceback = sys.exc_info()
print(str(traceback.format_exception(exc_type, exc_value, exc_traceback)))
if len(midiMsg) != 0 :
outq.put_nowait(midiMsg)
except Exception as e :
stats.error(e)
print("**> Exception cought: %s" % e)
if lastError + 5 > time.time() :
print("Waiting before trying again....")
time.sleep(5)
lastError = time.time()
except BaseException :
print("Keyboard / base interrupt received, stopping deamon...")
return 1
finally :
try :
if networkConn != - 1 :
networkConn.close()
except :
pass
def midiInput() :
lastError = 0
while 1 :
try :
midiIn = os.open(config.mididev, os.O_RDONLY)
sysexBuffer = []
while 1 :
midiMsg = os.read(midiIn, 1024);
toSend = []
for i in midiMsg :
i = ord(i)
if len(sysexBuffer) > 0 :
sysexBuffer.append(i)
if i == 0xF7 :
toSend.extend(sysexBuffer)
sysexBuffer = []
elif i == 0xF0 :
sysexBuffer.append(i)
else :
toSend.append(i)
if len(toSend) == 0 :
continue
stats.midiin(toSend);
outq.put_nowait(toSend)
if config.sendport != 0 :
oscMsg = "/midi/"
for i in toSend :
oscMsg = oscMsg + "%x " % i
# OSC string must be filled with nulls, multiples of 4
footer = len(oscMsg) % 4
if footer == 0 :
footer = 4
oscMsg = oscMsg + ("\0" * footer)
networkConn = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
networkConn.sendto(oscMsg, (config.sendhost, config.sendport))
except Exception as e :
stats.error(e)
print("**> Midi Input Exception cought: %s" % e)
if lastError + 4 > time.time() :
print("Waiting before trying again....")
time.sleep(4)
lastError = time.time()
except BaseException :
return 1
finally :
try :
os.close(midiIn)
except :
pass
def midiOutput() :
mididev = config.mididev
midiConn = -1
lastError = 0
while True :
try :
midiConn = os.open(config.mididev, os.O_WRONLY)
while 1 :
midiMsg = outq.get(True)
display = ""
for i in midiMsg :
display = display + "0x%x " % i
stats.midiout(display)
os.write(midiConn, bytearray(midiMsg))
except Exception as e :
stats.error(e)
print("**> Midi Output Exception cought: %s" % e)
if lastError + 5 > time.time() :
print("Waiting before trying again....")
time.sleep(5)
lastError = time.time()
except BaseException :
print("Keyboard / base interrupt received, stopping deamon...")
return 1
finally :
# close stuff we need to
try :
if midiConn != -1 :
os.close(midiConn)
except :
pass
# #########################################################################
# ## UNIT TESTS
# #########################################################################
class PiLink(unittest.TestCase) :
def testOne(self) :
self.assertEquals(osc2Midi(["/midi/0x90 60 127", 0]), [0x90, 60, 127])
self.assertEquals(osc2Midi(["/midi/z/0x90 60 127/0x91 0x21 00", 0]), [0x90, 60, 127, 0x91, 0x21, 0])
self.assertEquals(osc2Midi(["/midi/0xf0 0xb0 0 0 0xb0 0x20 0x00 0xc0 52 0xf7", 0]), [0xf0, 0xb0, 0, 0, 0xb0, 0x20, 0x00, 0xc0, 52, 0xF7])
if __name__ == "__main__" :
unittest.main()