-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSpnOSCReceiver.py
executable file
·291 lines (239 loc) · 9.9 KB
/
SpnOSCReceiver.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
#!/usr/bin/env python3
import traceback
import better_exceptions
import threading
import SpoonModes
import numpy as np
import random
MODES = SpoonModes.SpoonSliceModes()
LM = SpoonModes.SpoonLoopModes()
from spoon_logging import D, L, W, E
from pythonosc import dispatcher
from pythonosc import osc_server
def set_slice_mode(loop_number, slice_number, new_mode, the_status_obj):
loop_id_s = "/loop/{}".format(loop_number)
the_status_obj.loops[loop_id_s].set_slice_mode(slice_number,new_mode)
D("set the slice mode {} : {} : {} ".format(loop_id_s, slice_number, new_mode))
def set_loop_mode(loop_number, new_mode, the_status_obj):
loop_id_s = "/loop/{}".format(loop_number)
the_status_obj.loops[loop_id_s].set_loop_mode(new_mode)
D("set the loop mode {} : {} ".format(loop_id_s, new_mode))
def set_loop_jump(loop_number, new_position, the_status_obj):
loop_id_s = "/loop/{}".format(loop_number)
the_status_obj.loops[loop_id_s].jump_slice(new_position)
D("set {} new focus to {}".format(loop_id_s,new_position))
return
def set_loop_volume(loop_number, new_vol, the_status_obj):
loop_id_s = "/loop/{}".format(loop_number)
the_status_obj.loops[loop_id_s].set_volume(new_vol)
D("set {} volume to {}".format(loop_id_s, new_vol ))
return
def try_load_new_file_by_int(loop_number, new_file_number,the_status_obj):
# pass an file index number to the system and get the loop to load it.
loop_id_s = "/loop/{}".format(loop_number)
the_status_obj.load_new_audio_file_by_index(loop_id_s, new_file_number)
D("attempted setting loop {} to file index {}".format(loop_number, new_file_number))
return
def loop_mode_callback(unused_addr, args, the_OSC_message_argument):
D("LoopCallback")
#print("{} {} {} ".format(unused_addr, args, the_obj))
message_ok = True
if(len(unused_addr) != 12):
message_ok = False
loop_i = -1
new_mode = 'NOT-SET'
the_status_obj = args[0]
try:
loop_s = unused_addr[6]
loop_i = int(loop_s)
except Exception as jeje:
L("exception in handling OSC message {}".format(unused_addr))
L("Exception: {}".format(jeje))
L("{}".format(repr(jeje)))
message_ok = False
if(loop_i >4 or loop_i < 1):
D("Loop value out of range {} ".format(loop_i))
#do nothing
message_ok = False
new_slice_mode = the_OSC_message_argument
if not LM.is_valid_mode(new_slice_mode):
message_ok = False
if(message_ok):
set_loop_mode(loop_i, new_slice_mode, the_status_obj)
the_status_obj.set_osc_message( "{} : {} DONE".format(unused_addr, the_OSC_message_argument))
else:
# log error message
#TODO fix this error message
L("unable to parse message {} {} ".format(unused_addr, the_OSC_message_argument))
the_status_obj.set_osc_message( "{} : {} FAIL".format(unused_addr, the_OSC_message_argument))
return
#print("Loop:{} slice:{}".format(loop_i,slice_i))
return
def loop_jump_callback(unused_addr, args, the_OSC_message_argument):
D("loop jump callback")
message_ok = True
if(len(unused_addr) != 12):
message_ok = False
the_status_obj = args[0]
try:
loop_s = unused_addr[6]
loop_i = int(loop_s)
jump_i = int(the_OSC_message_argument)
except Exception as jj:
L("Exception parsing {} {}".format(unused_addr), the_OSC_message_argument)
L("Exception {} ".format(jj))
message_ok = False
loop_i = -1
jump_i = -1
if(loop_i >4 or loop_i<1):
D("Loop value out of range {} ".format(loop_i))
message_ok = False
if(jump_i >7 or jump_i <0):
D("jump value out of range {}".format(jump_i))
message_ok = False
if(message_ok):
set_loop_jump(loop_i, jump_i, the_status_obj)
the_status_obj.set_osc_message( "{} : {} DONE".format(unused_addr, the_OSC_message_argument))
else:
# log error message
#TODO fix this error message
L("unable to parse message {} {} ".format(unused_addr, the_OSC_message_argument))
the_status_obj.set_osc_message( "{} : {} FAIL".format(unused_addr, the_OSC_message_argument))
return
def slice_callback(unused_addr, args, the_OSC_message_argument):
D("SliceCallback")
#print("{} {} {} ".format(unused_addr, args, the_obj))
message_ok = True
if(len(unused_addr) != 15):
message_ok = False
loop_i = -1
slice_i = -1
new_mode = 'NOT-SET'
the_status_obj = args[0]
#probably should do a type() check here on the_status_obj
try:
loop_s = unused_addr[6]
slice_s = unused_addr[14]
loop_i = int(loop_s)
slice_i = int(slice_s)
except Exception as jeje:
L("exception in handling OSC message {}".format(unused_addr))
L("Exception: {}".format(jeje))
L("{}".format(repr(jeje)))
message_ok = False
if(loop_i >4 or loop_i < 1):
D("Loop value out of range {} ".format(loop_i))
#do nothing
message_ok = False
if(slice_i<0 or slice_i>7):
D("slice value out of range {} ".format(slice_i))
#do nothing
message_ok = False
#check is the_OSC_message_argument is a correct slice mode
new_slice_mode = the_OSC_message_argument
if not MODES.is_valid_mode(new_slice_mode):
message_ok = False
if(message_ok):
set_slice_mode(loop_i,slice_i, new_slice_mode, the_status_obj)
the_status_obj.set_osc_message( "{} : {} DONE".format(unused_addr, the_OSC_message_argument))
else:
# log error message
#TODO fix this error message
L("unable to parse message {} {} ".format(unused_addr, the_OSC_message_argument))
the_status_obj.set_osc_message( "{} : {} FAIL".format(unused_addr, the_OSC_message_argument))
return
#print("Loop:{} slice:{}".format(loop_i,slice_i))
return
def loop_volume_callback(unused_addr, args, the_OSC_message_argument ):
D("loop_volume_callback")
#set the volume level of a loop.
#.set_volume(float)
#range 0.0 - 1.0
# if(len(unused_addr) != 12):
# message_ok = False
loop_i = -1
new_volume_level = -1.0
message_ok=True
the_status_obj = args[0]
#get the loop id. /loop/*
try:
loop_s = unused_addr[6]
loop_i = int(loop_s)
except Exception as jeje:
L("exception in handling OSC message {}".format(unused_addr))
L("Exception: {}".format(jeje))
L("{}".format(repr(jeje)))
message_ok = False
if(loop_i >4 or loop_i < 1):
D("Loop value out of range {} ".format(loop_i))
#do nothing
message_ok = False
#convert the argument to a float.
try:
new_volume_level = float(the_OSC_message_argument)
if(new_volume_level < 0.0):
message_ok = False
#float parsing error handling.
except Exception as flt_error:
L("exception handing OSC message {}".format(unused_addr))
L("exception parsing {}".format( the_OSC_message_argument))
L("Exception: {}".format(flt_error))
L("{}".format(rep(flt_error)))
message_ok = False
if(message_ok):
set_loop_volume(loop_i, new_volume_level, the_status_obj)
the_status_obj.set_osc_message( "{} : {} DONE".format(unused_addr, the_OSC_message_argument))
else:
# log error message
#TODO fix this error message
L("unable to parse message {} {} ".format(unused_addr, the_OSC_message_argument))
the_status_obj.set_osc_message( "{} : {} FAIL".format(unused_addr, the_OSC_message_argument))
return
def loop_file_callback(unused_addr, args, the_OSC_message_argument):
D("loop file callback")
# has an argument of an int.
message_ok=True
the_status_obj = args[0]
#get the loop id. /loop/*
try:
loop_s = unused_addr[6]
loop_i = int(loop_s)
# get the track index
new_track_index=int(the_OSC_message_argument)
except Exception as errerr:
L("exception with loop_file_callback ")
L("Error: {}".format(errerr))
message_ok = False
return
if(loop_i not in range(1,5)):
message_ok = False
if ( message_ok):
try_load_new_file_by_int(loop_i,new_track_index, the_status_obj)
the_status_obj.set_osc_message( "{} : {} DONE".format(unused_addr, the_OSC_message_argument))
D("loop_file_callback {} {}".format(loop_i,new_track_index))
else:
L("message not processed. loop_file_callback")
L("Message received {}".format(the_OSC_message_argument))
L("Message Received {} ".format(unused_addr))
L("Message Received {}".format(args))
the_status_obj.set_osc_message( "{} : {} FAIL".format(unused_addr, the_OSC_message_argument))
return
def default_callback(unused_addr, args):
L("unknown message {} {}".format(unused_addr, args))
return
def start_OSC_server(spoon_status_object):
disp = dispatcher.Dispatcher()
disp.map("/loop/*/slice/*", slice_callback, spoon_status_object)
disp.map("/loop/*/mode", loop_mode_callback, spoon_status_object)
disp.map("/loop/*/jump", loop_jump_callback, spoon_status_object)
disp.map("/loop/*/volume", loop_volume_callback, spoon_status_object)
disp.map("/loop/*/file", loop_file_callback, spoon_status_object)
disp.set_default_handler(default_callback)
new_osc_port = spoon_status_object.osc_port
server = osc_server.ThreadingOSCUDPServer( ("127.0.0.1", new_osc_port), disp)
server_thread = threading.Thread(target=server.serve_forever)
server_thread.start()
L("Serving on {}".format(server.server_address))
#return the server and thread so we can shut it down when we quit
# server.shutdown()
return server , server_thread