forked from stufisher/LiveOSC2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LO2Mixin.py
217 lines (149 loc) · 6.4 KB
/
LO2Mixin.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
# -----------------------------------------------------------------
# LiveOSC2
# Live 9 Compatible version of LiveOSC using _Framework
# Stu Fisher <[email protected]>
# -----------------------------------------------------------------
from _Framework.SubjectSlot import SubjectSlot, CallableSlotMixin
from _Framework.Util import mixin
from functools import wraps, partial
import types
import OSC
# -----------------------------------------------------------------
# This decorator allows us to inject code into framework listeners
# without having to subclass and super each one. This is important
# for subject_slots where these methods are already decorated
#
# It will iterate through the class methods searching for ones
# beginning _lo2_<FrameworkListener>
def _decorate(fn, child, name):
def wrap(*a, **kw):
ret = fn(*a, **kw)
child(*a, **kw)
return ret
return wrap
def wrap_init(fn):
def decorate(self, *a, **kw):
fn(self, *a, **kw)
if not hasattr(self, 'liveosc'):
for m in dir(self):
if m.startswith('_lo2_'):
method = getattr(self, m.replace('_lo2_', ''))
child = getattr(self, m)
# If its a subject slot replace the listener
if hasattr(method, 'function'):
#method.function = _decorate(method.function, child, m)
method.listener = _decorate(method.function, child, m)
# If its a normal method just overwrite it
else:
setattr(self, m.replace('_lo2_', ''), _decorate(method, child, m))
self.liveosc = True
return decorate
# -----------------------------------------------------------------
# Various Helpers
class LO2Mixin:
_track_types = ['track/', 'return/', 'master/']
_default_args = []
_subject_slots = []
_is_enabled_ovr = True
_registered_callbacks = []
@staticmethod
def set_log(func):
LO2Mixin.log_message = func
@staticmethod
def set_osc_handler(handler):
LO2Mixin._osc_handler = handler
@staticmethod
def release_attributes():
LO2Mixin._osc_handler = None
LO2Mixin.log_message = None
def set_is_enabled(self, val):
self._is_enabled_ovr = val
def bundle(self):
return OSC.OSCBundle()
def send(self, addr, *msg):
if self._is_enabled_ovr:
self._osc_handler.send(addr, tuple(msg))
def sendb(self, bundle):
self._osc_handler.send_message(bundle)
def disconnect(self):
self.log_message('Disconnecting instance' + str(self))
for cb in self._registered_callbacks:
self._osc_handler._callback_manager.rem(cb)
self.log_message(str(self._osc_handler._callback_manager.callbacks))
def add_callback(self, msg, func):
""" Add a callback for an osc message """
if self._osc_handler:
self._osc_handler._callback_manager.add(msg, func)
self._registered_callbacks.append(func)
def add_default_callback(self, addr, subject, property, type):
""" Add a simple one argument osc setter / getter
eg.
self.add_default_callback('/live/tempo', self.song(), 'tempo', float)
"""
def cb(msg, src):
if self.has_arg(msg):
setattr(subject, property, msg[2])
else:
self.send(addr, type(getattr(subject, property)))
self.add_callback(addr, cb)
def add_function_callback(self, addr, fn, *a):
""" Add a simple callback that just calls a function """
def cb(msg, src):
if callable(fn):
fn(*a)
self.add_callback(addr, cb)
def add_simple_callback(self, addr, obj, property, check, fn):
""" Add a simple setter / getter callback based on default args """
def cb(msg, src):
if check(msg):
if len(msg) == len(self._default_args) + 2 or (len(msg) == len(self._default_args) + 3 and msg[-1] == 'query'):
fn()
else:
setattr(self._get_object(obj), property, msg[len(self._default_args) + 2])
self.add_callback(addr, cb)
def _get_object(self, obj):
objs = obj.split('.')
current = getattr(self, objs[0])
if len(objs) > 1:
for o in objs[1:]:
current = getattr(current, o)
return current
def set_default(self, *a):
self._default_args = a
def send_default(self, addr, *ar):
args = []
for a in self._default_args:
args.append(getattr(self, a))
args.extend(ar)
self.send(addr, *args)
def add_listener(self, addr, property, obj, value, *a):
""" Dynamically add a subject slot and callback """
def fn(self):
self.log_message('listener called: ' + addr + ' ' + property)
args = []
for arg in a:
args.append(getattr(self, arg))
args.append(getattr(self._get_object(obj), value))
self.send(addr, *args)
fn = wraps(fn)(mixin(SubjectSlot, CallableSlotMixin)(event=property, listener=partial(fn,self), function=partial(fn,self)))
self.register_slot(fn)
self._subject_slots.append('_on_'+property+'_changed')
if not hasattr(self, '_on_'+property+'_changed'):
setattr(self, '_on_'+property+'_changed', fn)
def track_id_type(self, t):
""" Returns the track type and id for a given track """
if t in self.song().visible_tracks:
id = list(self.song().visible_tracks).index(t)
type = 0
elif t in self.song().return_tracks:
id = list(self.song().return_tracks).index(t)
type = 1
elif t == self.song().master_track:
id = 0
type = 2
else:
id = None
type = None
return id, type
def has_arg(self, msg):
return not (len(msg) == 2 or (len(msg) == 3 and msg[2] == 'query'))