-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTrackVolHandler.py
358 lines (265 loc) · 13.8 KB
/
TrackVolHandler.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
# ******************************************************************************
# This file is part of the AaMakro5oul project
# (An OSC/MIDI controller for Ableton Live with DJ features)
#
# Full project source: https://github.com/hiramegl/AaMakro5oul
#
# License : Apache License 2.0
# Full license: https://github.com/hiramegl/AaMakro5oul/blob/master/LICENSE
#
# Copyright 2018, 2019 by Hiram Galicia ([email protected])
# http://www.unasystems.com
#
# All rights reserved.
# ******************************************************************************
from CoreHandler import CoreHandler
# ******************************************************************************
# Track Volume commands handler
# ******************************************************************************
class TrackVolHandler(CoreHandler):
def __init__(self, _oCtrlInstance, _oOscServer, _hConfig):
CoreHandler.__init__(self, _oCtrlInstance, _oOscServer, _hConfig)
bIgnoreRelease = False # the index of sends can be 0 (may appear as a release)
bLogInfo = False # do not log (fader messages overflow the log!)
self.config('/track/vol', bIgnoreRelease, bLogInfo)
# these commands apply to all gui visible tracks, the master track,
# the cue volume and to the selected track
self.m_aCmds = [
'fader', # volume fader
'reset', # volume reset: 85 %
'min', # volume min : 0 %
'max', # volume max : 100 %
]
aTracks = self.track_indeces_list()
for sCmd in self.m_aCmds:
self.add_callbacks_pref(sCmd, aTracks + ['cue'])
# NOTE: use 'aTracks' variable when we actually have separated
# controls for every track sends, currently we have not :-(
# sends volumes apply only to the selected track,
# master track and audio tracks are not handled
self.add_callbacks_pref('sends', ['selected']) # use aTracks when GUI implemented
# returns are managed with a multifader
self.add_callback_cmd('returns')
self.m_hTrackListeners = {}
self.reset_tracks()
self.update_tracks()
self.update_selected_track()
self.add_listeners()
def disconnect(self):
self.remove_listeners()
self.reset_tracks()
def update(self, _sEvent, _oArgs = None):
if (_sEvent == 'new_tracks_sel' or # SessionCmdHandler
_sEvent == 'session_reset'): # SessionCmdHandler
self.update_tracks()
elif (_sEvent == 'tracks_changed'):# TrackCmdHandler
self.add_listeners()
self.update_tracks()
self.update_selected_track()
elif (_sEvent == 'new_track_sel'): # TrackCmdHandler
self.update_selected_track()
def reset_tracks(self):
aTrackMsgs = []
aTracks = self.track_indeces_list() + ['cue']
for nTrackIdxRel in aTracks:
self.append_idx_msg('fader', nTrackIdxRel, 0.0, aTrackMsgs)
# reset the values of the selected-track sends
for nSendIdxAbs in self.gui_vis_sends_range():
self.append_mul_msg('sends/selected', nSendIdxAbs, 0.0, aTrackMsgs)
for nReturnIdxAbs in self.gui_vis_returns_range():
self.append_mul_msg('returns', nReturnIdxAbs, 0.0, aTrackMsgs)
sMsg = 'TrackVolHandler, reset_tracks, track/vol/fader, reset'
self.send_bundle(sMsg, aTrackMsgs)
def update_tracks(self):
aTrackMsgs = []
# iterate through the visible tracks in the GUI by using the
# absolute indeces of the tracks and send the volumes and
# sends values for the selected track
for nTrackIdxAbs in self.gui_visible_tracks_abs_range():
nTrackIdxRel = self.track_idx_rel(nTrackIdxAbs)
nVolume = 0.0
# check if the track is actually available
if (self.is_track_available(nTrackIdxAbs)):
oTrack = self.get_track(nTrackIdxAbs)
oMixDev = oTrack.mixer_device
nVolume = oMixDev.volume.value
aSends = oMixDev.sends
# NOTE: Do this when we actually have a GUI for the sends
# of every track
# for nSendIdxAbs in range(len(aSends)):
# nSendValue = aSends[nSendIdxAbs]
# self.append_mul_msg('sends/%d' % (nTrackIdxRel), nSendIdxAbs, nSendValue, aTrackMsgs)
if (self.sel_track_idx_abs() == nTrackIdxAbs):
for nSendIdxAbs in range(len(aSends)):
nSendValue = aSends[nSendIdxAbs].value
self.append_mul_msg('sends/selected', nSendIdxAbs, nSendValue, aTrackMsgs)
else: # track is unavailable, reset sends values in case is selected track
if (self.sel_track_idx_abs() == nTrackIdxAbs):
for nSendIdxAbs in range(len(aSends)):
nSendValue = aSends[nSendIdxAbs].value
self.append_mul_msg('sends/selected', nSendIdxAbs, 0.0, aTrackMsgs)
self.append_idx_msg('fader', nTrackIdxRel, nVolume, aTrackMsgs)
# master track and cue volumes
oMixDev = self.master().mixer_device
self.append_idx_msg('fader', 'master', oMixDev.volume.value , aTrackMsgs)
self.append_idx_msg('fader', 'cue' , oMixDev.cue_volume.value, aTrackMsgs)
# returns volumes
for nReturnIdxAbs in self.gui_vis_returns_range():
nVolume = 0.0
if (self.is_return_available(nReturnIdxAbs)):
oReturn = self.get_return(nReturnIdxAbs)
nVolume = oReturn.mixer_device.volume.value
self.append_mul_msg('returns', nReturnIdxAbs, nVolume, aTrackMsgs)
sMsg = 'TrackVolHandler, update_tracks, track/vol/fader, update'
self.send_bundle(sMsg, aTrackMsgs)
def update_selected_track(self):
aTrackMsgs = []
oTrack = self.sel_track()
oMixDev = oTrack.mixer_device
self.append_idx_msg('fader', 'selected', oMixDev.volume.value, aTrackMsgs)
# update sends volumes if is not a return-track
if (self.is_return_track(oTrack) == False):
aSends = oMixDev.sends
for nSendIdxAbs in range(len(aSends)):
nSendValue = aSends[nSendIdxAbs].value
self.append_mul_msg('sends/selected', nSendIdxAbs, nSendValue, aTrackMsgs)
sMsg = 'TrackVolHandler, update_selected_track, track/vol/fader/selected, update'
self.send_bundle(sMsg, aTrackMsgs)
def handle(self, _aMessage):
# first of all handle returns volumes
if (self.m_sCmd == 'returns'):
nReturnIdxAbs = int(_aMessage[2])
nReturnValue = _aMessage[3]
# check if the return-track is actually available
if (self.is_return_available(nReturnIdxAbs) == False):
self.send_msg('returns', [nReturnIdxAbs, 0.0])
return # return-track not available, nothing else to do here!
oReturn = self.get_return(nReturnIdxAbs)
oReturn.mixer_device.volume.value = nReturnValue
return # done changing the return-track volume, nothing else to do here!
sCmd = self.m_aParts[0] # fader, reset, min, max, sends, returns
sTrackIdxRel = self.m_aParts[1]
# find out which track is operated (master, selected, 0-7)
if (sTrackIdxRel == 'master'):
oTrack = self.master()
elif (sTrackIdxRel == 'selected'):
oTrack = self.sel_track()
elif (sTrackIdxRel == 'cue'):
oTrack = self.master()
else:
nTrackIdxAbs = self.track_idx_abs(int(sTrackIdxRel))
if (self.is_track_available(nTrackIdxAbs) == False):
return # unavailable track, nothing else to do here!
oTrack = self.get_track(nTrackIdxAbs)
oMixDev = oTrack.mixer_device
nVolume = -1.0
if (sCmd == 'fader'):
nVolume = _aMessage[2]
elif (sCmd == 'reset'):
nVolume = 0.85
elif (sCmd == 'max'):
nVolume = 1.0
elif (sCmd == 'min'):
nVolume = 0.0
if (sCmd == 'sends'):
nSendIdxAbs = int(_aMessage[2])
nSendValue = _aMessage[3]
# return-tracks have no sends!
if (self.is_return_track(oTrack)):
self.send_msg('sends/selected', [nSendIdxAbs, 0.0])
return # is a return-track, nothing else to do here!
# check that the send is actually available for this track!
aSends = oTrack.mixer_device.sends
if (nSendIdxAbs < len(aSends)):
aSends[nSendIdxAbs].value = nSendValue
else:
self.send_msg('sends/selected', [nSendIdxAbs, 0.0])
return # nothing else to do here
if (sTrackIdxRel == 'cue'):
oMixDev.cue_volume.value = nVolume
else:
oMixDev.volume.value = nVolume
# Ableton Live events management *******************************************
def add_listeners(self):
self.remove_listeners()
oMasterMixDev = self.master().mixer_device
if (not oMasterMixDev.volume.value_has_listener(self.on_master_changed)):
oMasterMixDev.volume.add_value_listener(self.on_master_changed)
if (not oMasterMixDev.cue_volume.value_has_listener(self.on_cue_changed)):
oMasterMixDev.cue_volume.add_value_listener(self.on_cue_changed)
for nTrackIdxAbs in self.tracks_and_returns_range():
oTrack = self.get_track_or_return(nTrackIdxAbs)
self.add_track_listeners(nTrackIdxAbs, oTrack)
def on_master_changed(self):
nVolume = self.master().mixer_device.volume.value
self.send_msg('fader/master', nVolume)
def on_cue_changed(self):
nVolume = self.master().mixer_device.cue_volume.value
self.send_msg('fader/cue', nVolume)
def add_track_listeners(self, _nTrackIdxAbs, _oTrack):
fVolCallback = lambda :self.on_track_vol_changed(_nTrackIdxAbs, _oTrack)
fSendCallback = lambda :self.on_track_send_changed(_nTrackIdxAbs, _oTrack)
if (not _oTrack in self.m_hTrackListeners):
oMixDev = _oTrack.mixer_device
oMixDev.volume.add_value_listener(fVolCallback)
self.m_hTrackListeners[_oTrack] = [fVolCallback]
if (self.is_return_track(_oTrack)):
return # return-track, nothing else to do
# normal track: add listeners to sends
aSends = oMixDev.sends
for nSendIdxAbs in range(len(aSends)):
if (not aSends[nSendIdxAbs].value_has_listener(fSendCallback)):
aSends[nSendIdxAbs].add_value_listener(fSendCallback)
self.m_hTrackListeners[_oTrack].append(fSendCallback)
def on_track_vol_changed(self, _nTrackIdxAbs, _oTrack):
if (self.is_return_track(_oTrack)):
nReturnIdxAbs = _nTrackIdxAbs - len(self.tracks())
nVolume = _oTrack.mixer_device.volume.value
self.send_msg('returns', [nReturnIdxAbs, nVolume])
elif (self.is_track_visible(_nTrackIdxAbs)):
nTrackIdxRel = self.track_idx_rel(_nTrackIdxAbs)
nVolume = _oTrack.mixer_device.volume.value
self.send_msg('fader/%d' % (nTrackIdxRel), nVolume)
if (self.sel_track_idx_abs() == _nTrackIdxAbs):
self.send_msg('fader/selected', nVolume)
def on_track_send_changed(self, _nTrackIdxAbs, _oTrack):
oMixDev = _oTrack.mixer_device
aSends = oMixDev.sends
aTrackMsgs = []
# NOTE: Do this when we actually have a GUI for the sends
# of every track
# if (self.is_track_visible(_nTrackIdxAbs)):
# nTrackIdxRel = self.track_idx_rel(_nTrackIdxAbs)
# for nSendIdxAbs in range(len(aSends)):
# nSendValue = aSends[nSendIdxAbs].value
# self.append_mul_msg('sends/%d' % (nTrackIdxRel), nSendIdxAbs, nSendValue, aTrackMsgs)
if (self.sel_track_idx_abs() == _nTrackIdxAbs):
for nSendIdxAbs in range(len(aSends)):
nSendValue = aSends[nSendIdxAbs].value
self.append_mul_msg('sends/selected', nSendIdxAbs, nSendValue, aTrackMsgs)
sMsg = 'TrackVolHandler, on_track_send_changed, track/vol/send/*, updating_all_sends_values'
self.send_bundle(sMsg, aTrackMsgs)
def remove_listeners(self):
oMasterMixDev = self.master().mixer_device
if (oMasterMixDev.volume.value_has_listener(self.on_master_changed)):
oMasterMixDev.volume.remove_value_listener(self.on_master_changed)
if (oMasterMixDev.cue_volume.value_has_listener(self.on_cue_changed)):
oMasterMixDev.cue_volume.remove_value_listener(self.on_cue_changed)
try:
for oTrack in self.m_hTrackListeners:
if (not oTrack in self.m_hTrackListeners):
continue # the key exists but the hash cannot recover the value!!!
if (oTrack != None):
aListeners = self.m_hTrackListeners[oTrack]
fVolCallback = aListeners[0]
fSendCallback = aListeners[1]
oMixDev = oTrack.mixer_device
aSends = oMixDev.sends
if (oMixDev.volume.value_has_listener(fVolCallback)):
oMixDev.volume.remove_value_listener(fVolCallback)
for nSendIdxAbs in range(len(aSends)):
if (aSends[nSendIdxAbs].value_has_listener(fSendCallback)):
aSends[nSendIdxAbs].remove_value_listener(fSendCallback)
except IndexError as oError:
self.log('! Error: %s' % (str(oError)))
self.m_hTrackListeners = {}