forked from hdavid/Launchpad95
/
DeviceControllerComponent.py
executable file
·496 lines (429 loc) · 18.3 KB
/
DeviceControllerComponent.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
# -*- coding: utf-8 -*-
from consts import * # noqa
from _Framework.DeviceComponent import DeviceComponent
from _Framework.ButtonElement import ButtonElement
from DeviceControllerStrip import DeviceControllerStrip
import time
import Live
class DeviceControllerComponent(DeviceComponent):
__module__ = __name__
__doc__ = ''
def __init__(self, matrix, side_buttons, top_buttons, control_surface):
self._control_surface = control_surface
self._skin = self._control_surface._skin
self._matrix = matrix
self._prev_track_button = None
self._next_track_button = None
self._prev_device_button = None
self._next_device_button = None
self._prev_bank_button = None
self._next_bank_button = None
self._precision_button = None
self._precision_mode = False
self._remaining_buttons = None
self._device = None
self._lock_button_slots = [None,None,None,None]
self._lock_buttons = [None,None,None,None]
self._locked_devices = [None,None,None,None]
self._locked_device_index = None
self._is_active = False
self._force = True
self._osd = None
DeviceComponent.__init__(self)
# Sliders
self._sliders = []
self.set_enabled(False)
for column in range(self._matrix.width()):
slider = DeviceControllerStrip(tuple([self._matrix.get_button(column, (self._matrix.height() - 1 - row)) for row in range(self._matrix.height())]),self._control_surface, self)
self._sliders.append(slider)
self._sliders = tuple(self._sliders)
self.set_parameter_controls(self._sliders)
self._selected_track = None
# device selection buttons
self.set_prev_device_button(top_buttons[0])
self.set_next_device_button(top_buttons[1])
# track selection buttons
self.set_prev_track_button(top_buttons[2])
self.set_next_track_button(top_buttons[3])
# on/off button
self.set_on_off_button(side_buttons[0])
# bank nav buttons
self.set_bank_nav_buttons(side_buttons[1], side_buttons[2])
self._prev_bank_button = side_buttons[1]
self._next_bank_button = side_buttons[2]
# precision
self.set_precision_button(side_buttons[3])
# lock buttons
self.set_lock_buttons([side_buttons[4],side_buttons[5],side_buttons[6],side_buttons[7]])
# selected device listener
self.song().add_appointed_device_listener(self._on_device_changed)
def disconnect(self):
self.song().remove_appointed_device_listener(self._on_device_changed)
# DeviceComponent.disconnect(self)
self._prev_track_button = None
self._next_track_button = None
self._prev_device_button = None
self._next_device_button = None
self._prev_bank_button = None
self._next_bank_button = None
self._precision_button = None
self._precision_mode = None
self._remaining_buttons = None
self._device = None
@property
def _locked_to_device2(self):
return self._locked_device_index != None
def set_enabled(self, active):
if active:
self.force = True
self.on_selected_track_changed()
# disable matrix.
for slider in self._sliders:
slider.set_enabled(active)
DeviceComponent.set_enabled(self, active)
def set_osd(self, osd):
self._osd = osd
def _update_OSD(self):
if self._osd != None:
self._osd.mode = "Device Controller"
i = 0
for slider in self._parameter_controls:
if slider._parameter_to_map_to != None:
self._osd.attribute_names[i] = str(slider._parameter_to_map_to.name)
try:
self._osd.attributes[i] = str(slider._parameter_to_map_to)
except:
self._osd.attributes[i] = str(slider._parameter_to_map_to.value)
else:
self._osd.attribute_names[i] = " "
self._osd.attributes[i] = " "
i += 1
if self._selected_track != None:
if self._locked_to_device2:
if self._device != None:
self._osd.info[0] = "track : " + self.get_device_track_name(self._device) + " (locked)"
else:
self._osd.info[0] = "track : " + self._selected_track.name
else:
self._osd.info[0] = "track : " + self._selected_track.name
else:
self._osd.info[0] = " "
if self._device != None:
name = self._device.name
if name == "":
name = "(unamed device)"
if self._locked_to_device2:
self._osd.info[1] = "device : " + name + " (locked)"
else:
self._osd.info[1] = "device : " + name
else:
self._osd.info[1] = "no device selected"
self._osd.update()
# DEVICE SELECTION
def _on_device_changed(self):
if not self._locked_to_device2:
self._selected_track = self.song().view.selected_track
self.set_device(self.song().appointed_device)
#self.set_device(self._selected_track.view.selected_device)
if self.is_enabled():
self.update()
def on_selected_track_changed(self):
if not self._locked_to_device2:
self._selected_track = self.song().view.selected_track
self.set_device(self._selected_track.view.selected_device)
if self.is_enabled():
self.update()
def set_device(self, device):
if(device != self._device):
if self._number_of_parameter_banks() <= self._bank_index:
self._bank_index = 0
self._device = device
DeviceComponent.set_device(self, device)
# UPDATE
def update(self):
if self.is_enabled():
if self._number_of_parameter_banks() <= self._bank_index:
self._bank_index = 0
if(not self._locked_to_device2):
if(self._device != None):
if ((not self.application().view.is_view_visible('Detail')) or (not self.application().view.is_view_visible('Detail/DeviceChain'))):
self.application().view.show_view('Detail')
self.application().view.show_view('Detail/DeviceChain')
# update bank buttons colors
if self._device != None:
if(self._prev_bank_button != None):
self._prev_bank_button.set_on_off_values(self._skin.AMBER_FULL, self._skin.AMBER_THIRD)
if(self._next_bank_button != None):
self._next_bank_button.set_on_off_values(self._skin.AMBER_FULL, self._skin.AMBER_THIRD)
else:
self._prev_bank_button.set_on_off_values(self._skin.off, self._skin.off)
self._next_bank_button.set_on_off_values(self._skin.off, self._skin.off)
for x in range(self._matrix.width()):
for y in range(self._matrix.height()):
#if self._force:
if(self._device == None):
self._matrix.get_button(x, y).set_on_off_values(self._skin.off, self._skin.off)
self._matrix.get_button(x, y).turn_off()
DeviceComponent.update(self)
for slider in self._sliders:
slider.reset_if_no_parameter()
# additional updates :
self.update_track_buttons()
self.update_device_buttons()
self.update_lock_buttons()
self.update_on_off_button()
self.update_precision_button()
self._update_OSD()
self._force = False
# LOCK button
def update_lock_buttons(self):
# lock button
if self.is_enabled():
for index in range(len(self._locked_devices)):
if self._locked_devices[index] != None:
self._lock_buttons[index].set_on_off_values(self._skin.RED_FULL, self._skin.RED_THIRD)
else:
self._lock_buttons[index].set_on_off_values(self._skin.AMBER_THIRD, self._skin.AMBER_THIRD) #LED_OFF
if self._locked_device_index==index:
self._lock_buttons[index].turn_on()
else:
self._lock_buttons[index].turn_off()
def set_lock_buttons(self, buttons):
self._lock_buttons = [None for index in range(len(buttons))]
self._locked_device_bank = [0 for index in range(len(buttons))]
self._lock_button_press = [0 for index in range(len(buttons))]
self._locked_devices = [None for index in range(len(buttons))]
for index in range(len(buttons)):
if (self._lock_buttons[index] != None):
self._lock_buttons[index].remove_value_listener(self._lock_value)
self._lock_buttons[index] = buttons[index]
if (self._lock_buttons[index] != None):
assert isinstance(self._lock_buttons[index], ButtonElement)
self._lock_buttons[index].add_value_listener(self._lock_value, identify_sender=True)
self.update_lock_buttons()
def _lock_value(self, value, sender):
if self.is_enabled():
index = 0
for i in range(len(self._lock_buttons)):
if self._lock_buttons[i] == sender:
index = i
if value != 0:
self._lock_button_press[index] = time.time()
else:
now = time.time()
if now - self._lock_button_press[index]>0.4:
if self._locked_devices[index] == None:
#save locked device
dev = -1
for i in range(len(self._locked_devices)):
if self._locked_devices[i] == self._device:
dev = i
if dev>=0:
if self._device != None:
self._control_surface.show_message(" '"+self.get_device_track_name(self._device)+" - "+str(self._device.name)+"' is already stored in lock button "+ str(dev+1)+" ! aborting." )
else:
if self._device!=None:
self._locked_devices[index] = self._device
if self._device != None:
self._control_surface.show_message(" '"+self.get_device_track_name(self._device)+" - "+str(self._device.name)+"' stored into lock button " + str(index+1))
self._locked_device_index = index
self.update()
else:
#remove saved device
self._locked_devices[index] = None
self._locked_device_index = None
if self._device != None:
self._control_surface.show_message("removing '"+self.get_device_track_name(self._locked_devices[index])+" - "+str(self._device.name)+"' from block button "+ str(index+1) )
else:
#use selected device
if self._locked_device_index == index:
if self._locked_devices[index] != None:
if self._locked_devices[index]!= None:
self._control_surface.show_message("unlocked from ' "+self.get_device_track_name(self._locked_devices[index])+" - "+str(self._locked_devices[index].name)+"' ("+str(index+1)+")")
self._locked_device_index = None
elif self._locked_devices[index] != None:
self._locked_device_index = index
self.set_device(self._locked_devices[index])
if self._locked_devices[index]!= None:
self._control_surface.show_message("locked to '"+self.get_device_track_name(self._locked_devices[index])+" - "+str(self._locked_devices[index].name)+" (" +str(index+1)+")" )
self.update()
self.update_track_buttons()
self.update_device_buttons()
self._update_OSD()
if(self._locked_to_device2):
self.on_selected_track_changed()
self.update_lock_buttons()
def get_device_track_name(self, device):
if str(type(device)) == "<class 'Track.Track'>":
return str(device.name)
else:
return self.get_device_track_name(device.canonical_parent)
# Precision button
def update_precision_button(self):
if (self._precision_button != None and self.is_enabled()):
if (self._precision_button != None):
if self._device != None:
self._precision_button.set_on_off_values(self._skin.GREEN_FULL, self._skin.GREEN_THIRD)
if self._precision_mode:
self._precision_button.turn_on()
else:
self._precision_button.turn_off()
else:
self._precision_button.set_on_off_values(self._skin.off, self._skin.off)
self._precision_button.turn_off()
def _precision_value(self, value, sender):
if ((not sender.is_momentary()) or (value is not 0)):
if (self._precision_button != None and self.is_enabled()):
self._precision_mode = not self._precision_mode
self.update_precision_button()
for slider in self._sliders:
slider.set_precision_mode(self._precision_mode)
def set_precision_button(self, button):
assert (isinstance(button, (ButtonElement, type(None))))
if (self._precision_button != button):
if (self._precision_button != None):
self._precision_button.remove_value_listener(self._precision_value)
self._precision_button = button
if (self._precision_button != None):
assert isinstance(button, ButtonElement)
self._precision_button.add_value_listener(self._precision_value, identify_sender=True)
self.update()
# ON OFF button
def update_on_off_button(self):
# on/off button
if (self._on_off_button != None and self.is_enabled()):
if (self._on_off_button != None):
parameter = self._on_off_parameter()
if parameter != None:
self._on_off_button.set_on_off_values(self._skin.GREEN_FULL, self._skin.GREEN_THIRD)
if parameter.is_enabled and parameter.value > 0:
self._on_off_button.turn_on()
else:
self._on_off_button.turn_off()
else:
self._on_off_button.set_on_off_values(self._skin.off, self._skin.off)
self._on_off_button.turn_off()
def _on_off_value(self, value):
if (self._on_off_button != None and self.is_enabled()):
DeviceComponent._on_off_value(self, value)
self.update_on_off_button()
def set_on_off_button(self, button):
assert (isinstance(button, (ButtonElement, type(None))))
if (self._on_off_button != button):
if (self._on_off_button != None):
self._on_off_button.remove_value_listener(self._on_off_value)
self._on_off_button = button
if (self._on_off_button != None):
assert isinstance(button, ButtonElement)
self._on_off_button.add_value_listener(self._on_off_value)
# TRACKS Buttons
def update_track_buttons(self):
# tracks
if self.is_enabled():
if(self._prev_track_button != None):
self._prev_track_button.set_on_off_values(self._skin.GREEN_FULL, self._skin.GREEN_THIRD)
if(self.selected_track_idx() > 0 and not self._locked_to_device2):
self._prev_track_button.turn_on()
else:
self._prev_track_button.turn_off()
if(self._next_track_button != None):
self._next_track_button.set_on_off_values(self._skin.GREEN_FULL, self._skin.GREEN_THIRD)
if(self.selected_track_idx() < len(self.song().tracks) - 1 and not self._locked_to_device2):
self._next_track_button.turn_on()
else:
self._next_track_button.turn_off()
def set_next_track_button(self, button):
assert (isinstance(button, (ButtonElement, type(None))))
if (self._next_track_button != button):
if (self._next_track_button != None):
self._next_track_button.remove_value_listener(self._next_track_value)
self._next_track_button = button
if (self._next_track_button != None):
assert isinstance(button, ButtonElement)
self._next_track_button.add_value_listener(self._next_track_value, identify_sender=True)
def _next_track_value(self, value, sender):
assert (self._next_track_button != None)
assert (value in range(128))
if self.is_enabled() and self._is_active:
if ((not sender.is_momentary()) or (value is not 0)):
if(self.selected_track_idx() < len(self.song().tracks) - 1 and not self._locked_to_device2):
self.song().view.selected_track = self.song().tracks[self.selected_track_idx() + 1]
def set_prev_track_button(self, button):
assert (isinstance(button, (ButtonElement, type(None))))
if (self._prev_track_button != button):
if (self._prev_track_button != None):
self._prev_track_button.remove_value_listener(self._prev_track_value)
self._prev_track_button = button
self._prev_track_button.set_on_off_values(self._skin.GREEN_FULL, self._skin.GREEN_THIRD)
if (self._prev_track_button != None):
assert isinstance(button, ButtonElement)
self._prev_track_button.add_value_listener(self._prev_track_value, identify_sender=True)
def _prev_track_value(self, value, sender):
assert (self._prev_track_button != None)
assert (value in range(128))
if ((not sender.is_momentary()) or (value is not 0)):
if self.is_enabled() and self._is_active:
if(self.selected_track_idx() > 0 and not self._locked_to_device2):
self.song().view.selected_track = self.song().tracks[self.selected_track_idx() - 1]
self.update()
def selected_track(self):
return self.song().view.selected_track
def selected_track_idx(self):
return self.tuple_idx(self.song().tracks, self.song().view.selected_track)
# DEVICES
def update_device_buttons(self):
if self.is_enabled():
if(self._prev_device_button != None):
self._prev_device_button.set_on_off_values(self._skin.GREEN_FULL, self._skin.GREEN_THIRD)
if(self.selected_device_idx() > 0 and not self._locked_to_device2 and len(self.selected_track().devices) > 0):
self._prev_device_button.turn_on()
else:
self._prev_device_button.turn_off()
if(self._next_device_button != None):
self._next_device_button.set_on_off_values(self._skin.GREEN_FULL, self._skin.GREEN_THIRD)
if(self.selected_device_idx() < len(self.selected_track().devices) - 1 and not self._locked_to_device2 and len(self.selected_track().devices) > 0):
self._next_device_button.turn_on()
else:
self._next_device_button.turn_off()
def set_next_device_button(self, button):
assert (isinstance(button, (ButtonElement, type(None))))
if (self._next_device_button != button):
if (self._next_device_button != None):
self._next_device_button.remove_value_listener(self._next_device_value)
self._next_device_button = button
if (self._next_device_button != None):
assert isinstance(button, ButtonElement)
self._next_device_button.add_value_listener(self._next_device_value, identify_sender=True)
def _next_device_value(self, value, sender):
assert (self._next_device_button != None)
assert (value in range(128))
if self.is_enabled() and self._is_active:
if ((not sender.is_momentary()) or (value is not 0)):
if self.selected_track() != None and len(self.selected_track().devices) > 0:
if(self.selected_device_idx() < len(self.selected_track().devices) - 1 and not self._locked_to_device2):
self.song().view.select_device(self.selected_track().devices[self.selected_device_idx() + 1])
def set_prev_device_button(self, button):
assert (isinstance(button, (ButtonElement, type(None))))
if (self._prev_device_button != button):
if (self._prev_device_button != None):
self._prev_device_button.remove_value_listener(self._prev_device_value)
self._prev_device_button = button
if (self._prev_device_button != None):
assert isinstance(button, ButtonElement)
self._prev_device_button.add_value_listener(self._prev_device_value, identify_sender=True)
def _prev_device_value(self, value, sender):
assert (self._prev_device_button != None)
assert (value in range(128))
if self.is_enabled() and self._is_active:
if ((not sender.is_momentary()) or (value is not 0)):
if self.selected_track() != None and len(self.selected_track().devices) > 0:
if(self.selected_device_idx() > 0 and not self._locked_to_device2):
self.song().view.select_device(self.selected_track().devices[self.selected_device_idx() - 1])
def selected_device_idx(self):
return self.tuple_idx(self.song().view.selected_track.devices, self._device)
# utils
def tuple_idx(self, tuple, obj):
for i in xrange(0, len(tuple)):
if (tuple[i] == obj):
return i
return(False)