forked from lvm/LiveOSC2
-
Notifications
You must be signed in to change notification settings - Fork 2
/
LO2ClipSlotComponent.py
executable file
·305 lines (226 loc) · 9.32 KB
/
LO2ClipSlotComponent.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
import Live
from _Framework.ClipSlotComponent import ClipSlotComponent
from _Framework.SubjectSlot import subject_slot
from LO2Mixin import LO2Mixin, wrap_init
class LO2ClipSlotComponent(ClipSlotComponent, LO2Mixin):
def with_clip(fn):
def wrap(self, *a, **kw):
if self._clip_slot is not None:
if self._clip_slot.has_clip:
fn(self, *a, **kw)
return wrap
@wrap_init
def __init__(self, tid, sid, *a, **k):
self._track_id = tid
self._scene_id = sid
self._has_clip = 0
super(LO2ClipSlotComponent, self).__init__(*a, **k)
self.set_default('_track_id', '_scene_id')
callbacks = {
'color': 'color',
'name': 'name',
'warping': 'warping',
'looping': 'looping',
'loopstart': 'loop_start',
'loopend': 'loop_end',
'start': 'start_marker',
'end': 'end_marker',
'loopjump': 'loop_jump',
'muted': 'muted'
}
for n,p in callbacks.iteritems():
self.add_simple_callback('/live/clip/'+n, '_clip_slot.clip', p, self._is_clip, getattr(self, '_on_clip_'+n+'_changed'))
self.add_callback('/live/clip/play', self._fire)
self.add_callback('/live/clip/stop', self._stop)
self.add_callback('/live/clip/pitch', self._pitch)
self.add_callback('/live/clip/select', self._view)
self.add_callback('/live/clip/notes', self._notes)
self.add_callback('/live/clip/notes/add', self._notes_add)
self.add_callback('/live/clip/notes/remove', self._notes_remove)
self.add_callback('/live/clip/notes/set', self._notes_set)
def _lo2__update_clip_property_slots(self):
clip = self._clip_slot.clip if self._clip_slot else None
self._on_clip_name_changed.subject = clip
self._on_clip_warping_changed.subject = clip
self._on_clip_looping_changed.subject = clip
self._on_clip_loopjump_changed.subject = clip
self._on_clip_loopstart_changed.subject = clip
self._on_clip_loopend_changed.subject = clip
self._on_clip_start_changed.subject = clip
self._on_clip_end_changed.subject = clip
self._on_clip_gain_changed.subject = clip
self._on_clip_muted_changed.subject = clip
def _is_clip(self, msg):
if len(msg) >= 4:
return msg[2] == self._track_id and msg[3] == self._scene_id
# Properties
@property
def id(self):
return (self._track_id, self._scene_id)
def _get_name(self):
if self._clip_slot is not None:
if self._clip_slot.has_clip:
return self._clip_slot.clip.name
else:
return ''
else:
return ''
#@with_clip
def _set_name(self, name):
if self._clip_slot is not None:
if self._clip_slot.has_clip:
self._clip_slot.clip.name = name
clip_name = property(_get_name, _set_name)
def _get_color(self):
if self._clip_slot is not None:
if self._clip_slot.has_clip:
return self._clip_slot.clip.color
else:
return 0
else:
return 0
#@with_clip
def _set_color(self, color):
self._clip_slot.clip.name = color
color = property(_get_color, _set_color)
# Listeners
def _lo2__on_clip_state_changed(self):
self._send_state()
def _lo2__on_clip_playing_state_changed(self):
self._send_state()
def _send_state(self):
if self._scene_id == -1:
return
state = LO2ClipSlotComponent.compute_state(self._clip_slot)
name = ''
if self._clip_slot.has_clip:
name = self._clip_slot.clip.name
self.send('/live/clip/state', self._track_id, self._scene_id, state)
if self._clip_slot.has_clip != self._has_clip:
self._has_clip = self._clip_slot.has_clip
self.send_default('/live/clip/name', name)
@staticmethod
def compute_state(clip_slot):
state = int(clip_slot.has_clip) if clip_slot is not None else 0
if clip_slot.has_clip:
c = clip_slot.clip
if c.is_playing:
state = 2
if c.is_triggered:
state = 3
return state
def _lo2__on_clip_color_changed(self):
self.send_default('/live/clip/color', self._clip_slot.clip.color)
@subject_slot('name')
def _on_clip_name_changed(self):
self.send_default('/live/clip/name', self._clip_slot.clip.name)
@subject_slot('warping')
def _on_clip_warping_changed(self):
self.send_default('/live/clip/warping', self._clip_slot.clip.warping)
@subject_slot('loop_jump')
def _on_clip_loopjump_changed(self):
self.send_default('/live/clip/loopjump')
@subject_slot('looping')
def _on_clip_looping_changed(self):
self.send_default('/live/clip/loopstate', self._clip_slot.clip.looping)
@subject_slot('loop_start')
def _on_clip_loopstart_changed(self):
self.send_default('/live/clip/loopstart', self._clip_slot.clip.loop_start)
@subject_slot('loop_end')
def _on_clip_loopend_changed(self):
self.send_default('/live/clip/loopend', self._clip_slot.clip.loop_end)
@subject_slot('start_marker')
def _on_clip_start_changed(self):
self.send_default('/live/clip/start', self._clip_slot.clip.start_marker)
@subject_slot('end_marker')
def _on_clip_end_changed(self):
self.send_default('/live/clip/end', self._clip_slot.clip.end_marker)
@subject_slot('gain')
def _on_clip_gain_changed(self):
self.send_default('/live/clip/gain', self._clip_slot.clip.gain)
@subject_slot('muted')
def _on_clip_muted_changed(self):
self.send_default('/live/clip/muted', self._clip_slot.clip.muted)
# Callbacks
def _fire(self, msg, src):
if self._clip_slot is not None and self._is_clip(msg):
if self._clip_slot.has_clip:
self._clip_slot.clip.fire()
else:
self._clip_slot.fire()
def _stop(self, msg, src):
if self._clip_slot is not None and self._is_clip(msg):
if self._clip_slot.has_clip:
self._clip_slot.clip.stop()
else:
self._clip_slot.stop()
@with_clip
def _pitch(self, msg, src):
if self._is_clip(msg):
c = self._clip_slot.clip
if len(msg) == 6:
c.pitch_coarse = msg[4]
c.pitch_fine = msg[5]
else:
self.send_default('/live/clip/pitch', c.pitch_coarse, c.pitch_fine)
@with_clip
def _view(self, msg, src):
if self._is_clip(msg):
tr = self._clip_slot.canonical_parent
self.song().view.detail_clip = self._clip_slot.clip
self.application().view.show_view('Detail/Clip')
if 0:
self.song().view.selected_track = self._clip_slot.canonical_parent
self.song().view.selected_scene = self.song().scenes[self._scene_id]
def _notes(self, msg, src):
if self._is_clip(msg):
c = self._clip_slot.clip
if c.is_midi_clip and len(msg) == 8:
notes = c.get_notes(msg[4], msg[5], msg[6], msg[7])
data = []
data.append(self._track_id)
data.append(self._scene_id)
for n in notes:
for p in n:
data.append(p)
self.send('/live/clip/notes', data)
def _notes_set(self, msg, src):
if self._is_clip(msg):
c = self._clip_slot.clip
# todo refactor these method to avoid code duplication
if c.is_midi_clip and len(msg) >= 9:
param_count = len(msg) - 4
if param_count % 5 != 0:
return # bad param count/format
note_count = param_count / 5
notes_to_add = []
for x in range(note_count):
notes_to_add.append(
(msg[x * 5 + 4], msg[x * 5 + 5], msg[x * 5 + 6], msg[x * 5 + 7], msg[x * 5 + 8])
)
c.select_all_notes()
c.replace_selected_notes(tuple(notes_to_add))
def _notes_add(self, msg, src):
if self._is_clip(msg):
c = self._clip_slot.clip
if c.is_midi_clip and len(msg) >= 9:
param_count = len(msg) - 4
if param_count % 5 != 0:
return # bad param count/format
note_count = param_count / 5
notes_to_add = []
for x in range(note_count):
notes_to_add.append(
(msg[x * 5 + 4], msg[x * 5 + 5], msg[x * 5 + 6], msg[x * 5 + 7], msg[x * 5 + 8])
)
c.set_notes(tuple(notes_to_add))
def _notes_remove(self, msg, src):
if self._is_clip(msg):
c = self._clip_slot.clip
if c.is_midi_clip and len(msg) == 8:
c.remove_notes(msg[4], msg[5], msg[6], msg[7])
def _notes_remove_all(self, msg, src):
if self._is_clip(msg):
c = self._clip_slot.clip
if c.is_midi_clip:
c.remove_notes(msg[4], msg[5], msg[6], msg[7])