forked from hdavid/Launchpad95
/
DeviceControllerComponent.py
executable file
·390 lines (329 loc) · 14 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
# emacs-mode: -*- python-*-
import Live
from consts import *
from _Framework.MixerComponent import MixerComponent
from _Framework.DeviceComponent import DeviceComponent
from _Framework.ButtonElement import ButtonElement
from _Framework.ControlSurfaceComponent import ControlSurfaceComponent
from PreciseButtonSliderElement import PreciseButtonSliderElement
# todo :
# - XXX fix precise mode increments
# - if no device selected => turn matrix led off.
class DeviceControllerComponent(DeviceComponent):
__module__ = __name__
__doc__ = ''
def __init__(self,matrix,side_buttons,top_buttons,parent):
self._parent=parent
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._is_active = False
self._force=True
DeviceComponent.__init__(self)
#Sliders
self._sliders = []
self.set_enabled(False)
for column in range(self._matrix.width()):
slider = PreciseButtonSliderElement(tuple([self._matrix.get_button(column, (self._matrix.height() - 1 - row)) for row in range(self._matrix.height()) ]))
slider.set_parent(self)
slider.set_mode(3)
self._sliders.append(slider)
self._sliders=tuple(self._sliders)
self.set_parameter_controls(self._sliders)
#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])
#lock button
self.set_lock_button(side_buttons[1])
self.set_lock_callback(self._lock_callback_function)
#bank nav buttons
self.set_bank_nav_buttons(side_buttons[2],side_buttons[3])
self._prev_bank_button=side_buttons[2]
self._next_bank_button=side_buttons[3]
#precision
self.set_precision_button(side_buttons[4])
#remaining buttons that need to be turned off !
self.set_remaining_buttons([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
def set_enabled(self,active):
if active:
self.force=True
#disable matrix.
for slider in self._sliders:
slider.set_disabled(not active)
#ping parent
DeviceComponent.set_enabled(self,active)
#DEVICE SELECTION
def _on_device_changed(self):
if self._locked_to_device != True:
self.set_device(self.song().appointed_device)
if self.is_enabled():
self.update()
def on_selected_track_changed(self):
if self._locked_to_device != True:
track = self.song().view.selected_track
self.set_device(track.view.selected_device)
if self.is_enabled():
self.update()
def set_device(self,device):
if(device != self._device):
self._device=device
DeviceComponent.set_device(self,device)
#UPDATE
def update(self):
if self.is_enabled():
if(not self._locked_to_device):
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(AMBER_FULL,AMBER_THIRD)
if(self._next_bank_button != None):
self._next_bank_button.set_on_off_values(AMBER_FULL,AMBER_THIRD)
else :
self._prev_bank_button.set_on_off_values(LED_OFF,LED_OFF)
self._next_bank_button.set_on_off_values(LED_OFF,LED_OFF)
for x in range(self._matrix.width()):
for y in range(self._matrix.height()):
self._matrix.get_button(x,y).set_on_off_values(AMBER_FULL,LED_OFF)
if self._force:
self._matrix.get_button(x,y).turn_off()
#update parent
DeviceComponent.update(self)
#reset sliders if no device
if(self._device==None):
for slider in self._sliders:
slider.reset()
#additional updates :
self.update_track_buttons()
self.update_device_buttons()
self.update_lock_button()
self.update_on_off_button()
self.update_precision_button()
self.update_remaining_buttons()
self._force=False
#REMAINING buttons
def set_remaining_buttons(self,remaining_buttons):
self._remaining_buttons=remaining_buttons
for index in range(len(self._remaining_buttons)):
self._remaining_buttons[index].set_on_off_values(LED_OFF,LED_OFF)
def update_remaining_buttons(self):
if(self._remaining_buttons!=None):
for index in range(len(self._remaining_buttons)) :
if(self._remaining_buttons[index]!=None):
self._remaining_buttons[index].set_on_off_values(LED_OFF,LED_OFF)
self._remaining_buttons[index].turn_off()
# LOCK button
def update_lock_button(self):
#lock button
if (self._lock_button!=None and self.is_enabled()):
if (self._device != None):
self._lock_button.set_on_off_values(RED_FULL,RED_THIRD)
if (self._locked_to_device):
self._lock_button.turn_on()
else:
self._lock_button.turn_off()
else:
self._lock_button.set_on_off_values(LED_OFF,LED_OFF)
self._lock_button.turn_off()
def _lock_callback_function(self):
#lock to a one device
self.set_lock_to_device(not self._locked_to_device, self._device)
#display a message about the lock status
if(self._locked_to_device and self._device != None):
self._parent._parent.show_message("Launchpad locked to " + str(self._device.name)+" !")
self.update_track_buttons()
self.update_device_buttons()
if(self._locked_to_device):
self.on_selected_track_changed()
# 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(GREEN_FULL,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(LED_OFF,LED_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._lock_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(GREEN_FULL,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(LED_OFF,LED_OFF)
self._on_off_button.turn_off()
def _on_off_value(self, value):
if (self._lock_button!=None and self.is_enabled()):
DeviceComponent._on_off_value(self,value)
self.update_on_off_button()
# 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(GREEN_FULL,GREEN_THIRD)
if(self.selected_track_idx() >0 and not self._locked_to_device):
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(GREEN_FULL,GREEN_THIRD)
if(self.selected_track_idx() < len(self.song().tracks)-1 and not self._locked_to_device):
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_device):
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(GREEN_FULL,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_device):
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(GREEN_FULL,GREEN_THIRD)
if(self.selected_device_idx() >0 and not self._locked_to_device 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(GREEN_FULL,GREEN_THIRD)
if(self.selected_device_idx() < len(self.selected_track().devices)-1 and not self._locked_to_device 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_device):
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)):
#self._parent._parent.log_message(str(self.selected_track()))
if self.selected_track()!=None and len(self.selected_track().devices)>0:
if(self.selected_device_idx()>0 and not self._locked_to_device):
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)