-
Notifications
You must be signed in to change notification settings - Fork 6
/
controller_definition.py
663 lines (521 loc) · 27.9 KB
/
controller_definition.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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
# MIT License
# Copyright (c) 2024 Pablo Peral
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import mixer_definition
import nihia
from nihia import *
import config
import mixer
import transport
import general
import ui
import device
import midi
import plugins
import channels
import math
class Core:
""" Common controller definition across all Komplete Kontrol keyboards. """
def __init__(self):
# Initialize mixer cache
self.mixer = mixer_definition.Mixer()
# Variable for the window change produced by the Quantize button
self.window = -1
# Variable for the window change produced by the Quantize button (second implementation)
self.window2 = 63
# Button light states
self.PLAY = None
self.STOP = None
self.REC = None
self.COUNT_IN = None
self.CLEAR = None
self.LOOP = None
self.METRO = None
self.TEMPO = None
self.UNDO = None
self.REDO = None
self.QUANTIZE = None
self.AUTO = None
self.MUTE_SELECTED = None
self.SOLO_SELECTED = None
def OnInit(self):
# Activates the deep integration mode
nihia.handShake()
# Additional controller-dependent code
try:
self.OnInitAdd()
except:
pass
# Update LEDs
self.OnRefresh(midi.HW_Dirty_LEDs)
self.OnRefresh(260)
nihia.buttons.setLight("QUANTIZE", 1)
nihia.buttons.setLight("AUTO", 0)
nihia.buttons.setLight("TEMPO", 1)
# Update mixer
self.mixer.update()
def OnInitAdd(self): # Intended to be declared by child
raise NotImplementedError()
def OnDeInit(self):
# Deactivates the deep integration mode
nihia.goodBye()
def OnMidiMsg(self, event):
# Play button
if event.data1 == nihia.buttons.button_list.get("PLAY"):
event.handled = True
transport.start()
# Restart button
elif event.data1 == nihia.buttons.button_list.get("RESTART"):
event.handled = True
transport.setLoopMode()
# Record button
elif event.data1 == nihia.buttons.button_list.get("REC"):
event.handled = True
transport.record()
# Count-In button
elif event.data1 == nihia.buttons.button_list.get("COUNT_IN"):
event.handled = True
# Defines the standard behavior (just to toggle "Countdown before recording" on/off)
if config.COUNT_IN_BEHAVIOR == 0:
transport.globalTransport(midi.FPT_CountDown, 1)
# Defines behavior of the button if the user chooses the Maschine-alike behavior
if config.COUNT_IN_BEHAVIOR == 1:
# Toggles recording on if it isn't enabled already
if transport.isRecording() == 0:
transport.record()
# Toggles countdown before recording on if it isn't enabled already
if ui.isPrecountEnabled() == 0:
transport.globalTransport(midi.FPT_CountDown, 1)
# Stops playback if FL Studio is playing
if transport.isPlaying() == True:
transport.stop()
# Then turns playback on again. This time record and countdown before recording will be activated
transport.start()
# Stop button
elif event.data1 == nihia.buttons.button_list.get("STOP"):
event.handled = True
transport.stop()
# Clear button
# This one in other DAWs (in Maschine, specifically) this button is meant to clear the MIDI clip you're
# on so you can record again on it without having to use a mouse to delete all of the notes on the clip before
# recording again
#
# However, since the MIDI API on FL Studio doesn't allow control over the piano roll specifically, for now it will only just
# emulate the delete button (which does the same)
elif event.data1 == nihia.buttons.button_list.get("CLEAR"):
event.handled = True
ui.delete()
# Loop button (toggles loop recording on/off)
elif event.data1 == nihia.buttons.button_list.get("LOOP"):
event.handled = True
transport.globalTransport(midi.FPT_LoopRecord, 1)
# Metronome button
elif event.data1 == nihia.buttons.button_list.get("METRO"):
event.handled = True
transport.globalTransport(midi.FPT_Metronome, 1)
# Tempo button
elif event.data1 == nihia.buttons.button_list.get("TEMPO"):
event.handled = True
transport.globalTransport(midi.FPT_TapTempo, 1)
# Undo button
elif event.data1 == nihia.buttons.button_list.get("UNDO"):
event.handled = True
general.undoUp()
# Redo button
elif event.data1 == nihia.buttons.button_list.get("REDO"):
event.handled = True
general.undoDown()
# Quantize button
elif (event.data1 == nihia.buttons.button_list.get("QUANTIZE")) and (config.QUANTIZE_BEHAVIOR == 0):
event.handled = True
channels.quickQuantize(channels.selectedChannel(), config.QUANTIZE_MODE)
# Instead, it changes between FL windows
# TODO: The code is correctly written, but the ui.showWindow() method has a bug that causes the Piano roll and Browser windows not to
# appear when invoked. It has been said it should be fixed in a future update.
# -----------------------------------------------------------------------------------------------------------------------------------
# if event.data1 == nihia.buttons.button_list.get("QUANTIZE"):
# global window
# window += 1
# if window <= 4:
# ui.showWindow(window)
# print("if reached")
# elif window > 4:
# window = 0
# ui.showWindow(window)
# -----------------------------------------------------------------------------------------------------------------------------------
#
# Alternative implementation: Emulate the Fn buttons
elif (event.data1 == nihia.buttons.button_list.get("QUANTIZE")) and (config.QUANTIZE_BEHAVIOR == 1):
event.handled = True
self.window2 += 1
# Normal behavior if the action ID is between the desired range
if self.window2 <= 68 and self.window2 != 67:
transport.globalTransport(self.window2, 1)
# Skips the 67 value which calls the full screen plugin picker and calls the mixer instead
elif self.window2 == 67:
self.window2 += 1
transport.globalTransport(self.window2, 1)
# Once window value is out of range, it sets it again to the first value in range
elif self.window2 > 68:
self.window2 = 64
transport.globalTransport(self.window2, 1)
# Automation button
# Enables and disables the recording automation events
# TODO: Not implemented yet in FL Studio MIDI API
#
# Instead, it shows the full-screen plugin browser
elif event.data1 == nihia.buttons.button_list.get("AUTO"):
event.handled = True
transport.globalTransport(midi.FPT_F8, 1)
# 4D Encoder +
elif event.data1 == nihia.buttons.button_list.get("ENCODER_GENERAL") and event.data2 == nihia.buttons.button_list.get("PLUS"):
event.handled = True
# Mixer navigation (right)
if ui.getFocused(midi.widMixer) == True:
ui.right()
# Playback jogging
elif (ui.getFocused(midi.widPianoRoll) == True) or (ui.getFocused(midi.widPlaylist) == True):
transport.setSongPos(transport.getSongPos(midi.SONGLENGTH_S) + 1, midi.SONGLENGTH_S)
# General navigation
else:
ui.down()
# 4D Encoder -
elif event.data1 == nihia.buttons.button_list.get("ENCODER_GENERAL") and event.data2 == nihia.buttons.button_list.get("MINUS"):
event.handled = True
# Mixer navigation
if ui.getFocused(midi.widMixer) == True:
ui.left()
elif (ui.getFocused(midi.widPianoRoll) == True) or (ui.getFocused(midi.widPlaylist) == True):
transport.setSongPos(transport.getSongPos(midi.SONGLENGTH_S) - 1, midi.SONGLENGTH_S)
# General navigation
else:
ui.up()
# 4D Encoder + (selected track volume)
elif event.data1 == nihia.buttons.button_list.get("ENCODER_VOLUME_SELECTED") and event.data2 == nihia.buttons.button_list.get("PLUS"):
event.handled = True
mixer.setTrackVolume(mixer.trackNumber(), mixer.getTrackVolume(mixer.trackNumber()) + 0.01)
# 4D Encoder - (selected track volume)
elif event.data1 == nihia.buttons.button_list.get("ENCODER_VOLUME_SELECTED") and event.data2 == nihia.buttons.button_list.get("MINUS"):
event.handled = True
mixer.setTrackVolume(mixer.trackNumber(), mixer.getTrackVolume(mixer.trackNumber()) - 0.01)
# 4D Encoder + (selected track pan)
elif event.data1 == nihia.buttons.button_list.get("ENCODER_PAN_SELECTED") and event.data2 == nihia.buttons.button_list.get("PLUS"):
event.handled = True
mixer.setTrackPan(mixer.trackNumber(), mixer.getTrackPan(mixer.trackNumber()) + 0.01)
# 4D Encoder + (selected track pan)
elif event.data1 == nihia.buttons.button_list.get("ENCODER_PAN_SELECTED") and event.data2 == nihia.buttons.button_list.get("MINUS"):
event.handled = True
mixer.setTrackPan(mixer.trackNumber(), mixer.getTrackPan(mixer.trackNumber()) - 0.01)
# 4D Encoder button
elif event.data1 == nihia.buttons.button_list.get("ENCODER_BUTTON"):
event.handled = True
# Open and close plugin window for the currently selected plugin on the channel rack
if ui.getFocused(midi.widChannelRack) == True:
channels.showEditor(channels.selectedChannel(), 1)
elif ui.getFocused(5) == True:
channels.showEditor(channels.selectedChannel(), 0)
else:
ui.enter()
# 4D Encoder button (shifted)
elif event.data1 == nihia.buttons.button_list.get("ENCODER_BUTTON_SHIFTED"):
event.handled = True
transport.globalTransport(midi.FPT_Menu, 1)
# Knobs
# Normal knobs (volume adjustment)
elif nihia.mixer.knobs[0][0] <= event.data1 <= nihia.mixer.knobs[0][7]:
event.handled = True
# Increase
if nihia.mixer.KNOB_INCREASE_MIN_SPEED <= event.data2 <= nihia.mixer.KNOB_INCREASE_MAX_SPEED:
self.adjustMixer(event.data1 - nihia.mixer.knobs[0][0], "VOLUME", "INCREASE", mixer.trackNumber(), self.normalizeKnobSpeed(event.data2))
# Decrease
elif nihia.mixer.KNOB_DECREASE_MIN_SPEED >= event.data2 >= nihia.mixer.KNOB_DECREASE_MAX_SPEED:
self.adjustMixer(event.data1 - nihia.mixer.knobs[0][0], "VOLUME", "DECREASE", mixer.trackNumber(), self.normalizeKnobSpeed(event.data2))
# Shifted knobs (pan adjustment)
elif nihia.mixer.knobs[1][0] <= event.data1 <= nihia.mixer.knobs[1][7]:
event.handled = True
# Increase
if nihia.mixer.KNOB_INCREASE_MIN_SPEED <= event.data2 <= nihia.mixer.KNOB_INCREASE_MAX_SPEED:
self.adjustMixer(event.data1 - nihia.mixer.knobs[1][0], "PAN", "INCREASE", mixer.trackNumber(), self.normalizeKnobSpeed(event.data2))
# Decrease
elif nihia.mixer.KNOB_DECREASE_MIN_SPEED >= event.data2 >= nihia.mixer.KNOB_DECREASE_MAX_SPEED:
self.adjustMixer(event.data1 - nihia.mixer.knobs[1][0], "PAN", "DECREASE", mixer.trackNumber(), self.normalizeKnobSpeed(event.data2))
# Additional controller-dependent code
try:
self.OnMidiMsgAdd(event)
except:
pass
def OnMidiMsgAdd(self, event): # Intended to be declared by child
raise NotImplementedError()
def OnIdle(self):
# Updates the LED of the CLEAR button (moved to OnIdle, since OnRefresh isn't called when focused window changes)
if ui.getFocused(midi.widPianoRoll) != self.CLEAR:
self.CLEAR = ui.getFocused(midi.widPianoRoll)
nihia.buttons.setLight("CLEAR", ui.getFocused(midi.widPianoRoll))
def OnRefresh(self, flag):
# LEDs update
if flag == midi.HW_Dirty_LEDs:
# PLAY button
if transport.isPlaying() != self.PLAY:
self.PLAY = transport.isPlaying()
# Handle transport.isPlaying() exception returning 2 when recording countdown is happening
if transport.isPlaying() == 2:
nihia.buttons.setLight("PLAY", 0)
else:
nihia.buttons.setLight("PLAY", transport.isPlaying())
# STOP button
if (not transport.isPlaying()) != self.STOP:
self.STOP = (not transport.isPlaying())
# Handle transport.isPlaying() exception returning 2 when recording countdown is happening
if transport.isPlaying() == 2:
nihia.buttons.setLight("STOP", 0)
else:
nihia.buttons.setLight("STOP", (not transport.isPlaying()))
# COUNT-IN button
if ui.isPrecountEnabled() != self.COUNT_IN:
self.COUNT_IN = ui.isPrecountEnabled()
nihia.buttons.setLight("COUNT_IN", ui.isPrecountEnabled())
# CLEAR button (moved to OnIdle, since OnRefresh isn't called when focused window changes)
# LOOP button
if ui.isLoopRecEnabled() != self.LOOP:
self.LOOP = ui.isLoopRecEnabled()
nihia.buttons.setLight("LOOP", ui.isLoopRecEnabled())
# METRO button
if ui.isMetronomeEnabled() != self.METRO:
self.METRO = ui.isMetronomeEnabled()
nihia.buttons.setLight("METRO", ui.isMetronomeEnabled())
# UNDO button
if self.getUndoStatus() != self.UNDO:
self.UNDO = self.getUndoStatus()
nihia.buttons.setLight("UNDO", self.getUndoStatus())
# REDO button
if self.getRedoStatus() != self.REDO:
self.REDO = self.getRedoStatus()
nihia.buttons.setLight("REDO", self.getRedoStatus())
# QUANTIZE button is set on init and permanently on
# AUTO button is set on init and permanently on
# MUTE button
if mixer.isTrackMuted(mixer.trackNumber()) != self.MUTE_SELECTED:
self.MUTE_SELECTED = mixer.isTrackMuted(mixer.trackNumber())
nihia.buttons.setLight("MUTE_SELECTED", mixer.isTrackMuted(mixer.trackNumber()))
# SOLO button
if mixer.isTrackSolo(mixer.trackNumber()) != self.SOLO_SELECTED:
self.SOLO_SELECTED = mixer.isTrackSolo(mixer.trackNumber())
nihia.buttons.setLight("SOLO_SELECTED", mixer.isTrackSolo(mixer.trackNumber()))
# Undocumented flag for recording state changes
elif flag == 260:
# REC button
if transport.isRecording() != self.REC:
self.REC = transport.isRecording()
nihia.buttons.setLight("REC", transport.isRecording())
else:
self.mixer.update()
def OnDirtyMixerTrack(self, index):
if index == -1:
self.mixer.need_full_refresh = True
# Queue an update for a specific track
elif self.mixer.trackFirst <= index < self.mixer.trackFirst + self.mixer.trackLimit:
self.mixer.need_refresh += [index - self.mixer.trackFirst]
def OnUpdateMeters(self): # Intended to be declared by child
raise NotImplementedError()
def normalizeKnobSpeed(self, speed: int) -> float:
"""
Normalize the speed value specified by the keyboard when turning a knob.
The speed values sent by the keyboard specify the speed at which the knob is being turned to in a
range of either 64 or 63 steps depending on the direction. This function normalizes the integer value sent by
the keyboard into a 0 (min speed) to 1 (max speed) float value that can be used to properly determine the
offsets to use when modifying parameters depending on the speed the knob is being turned at.
### Parameters
- speed: From 0 to 127. The speed value reported by the keyboard.
### Returns
Float value representing the proper sensitivity multiplier corresponding to the speed the knob is being turned at.
"""
# Convert the speed value received by the knob to be a normalized sensitivity multiplier
return abs(-1 / (speed - 64))
def adjustMixer(self, knob: int, dataType: str, action: str, selectedTrack: int, sensitivity: float):
""" Dynamically maps the physical knob to the right mixer track depending on the track group the selected track belongs to, and adjusts the parameter.
### Parameters
- knob: From 0 to 7. Number of the physical knob you are mapping.
- dataType: The parameter you are going to adjust. Can be PAN or VOLUME.
- action: Can be INCREASE or DECREASE.
- selectedTrack: The actual selected track that will be used to calculate the track group.
- sensitivity: From 0 (minimum) to 1 (maximum). The greater the sensitivity, the greater will be the modification of the desired parameter.
"""
# Calculates which track group the current track belongs to and truncates the value to get the exact number
trackGroup = math.trunc(1/8 * selectedTrack)
# Multiplies the trackGroup to 8 to get the index of the first track of that group
trackFirst = trackGroup * 8
if (trackGroup == 15) and (knob == 6 or knob == 7): # Control 15th group exception
return
else:
if dataType == "VOLUME":
if action == "INCREASE":
mixer.setTrackVolume(trackFirst + knob, mixer.getTrackVolume(trackFirst + knob) + (config.KNOB_INCREMENTS_VOL * sensitivity))
elif action == "DECREASE":
mixer.setTrackVolume(trackFirst + knob, mixer.getTrackVolume(trackFirst + knob) - (config.KNOB_INCREMENTS_VOL * sensitivity))
elif dataType == "PAN":
if action == "INCREASE":
mixer.setTrackPan(trackFirst + knob, mixer.getTrackPan(trackFirst + knob) + (config.KNOB_INCREMENTS_PAN * sensitivity))
elif action == "DECREASE":
mixer.setTrackPan(trackFirst + knob, mixer.getTrackPan(trackFirst + knob) - (config.KNOB_INCREMENTS_PAN * sensitivity))
def getUndoStatus(self):
""" Helper function to set the light on the UNDO button. """
# general.getUndoHistoryPos() is broken, returning always the same result
# as general.getUndoHistoryCount()
# Function is a stub
"""
undoLength = general.getUndoHistoryCount()
undoPosition = general.getUndoHistoryPos()
# In FL Studio, the first (most ancient) undo point in the history seems to have
# the greatest number and the entire history as an index of 1
if undoPosition == undoLength:
status = 0
else:
status = 1
return status
"""
return 1
def getRedoStatus(self):
""" Helper function to set the light on the REDO button. """
# general.getUndoHistoryPos() is broken, returning always the same result
# as general.getUndoHistoryCount()
# Function is a stub
"""
undoLength = general.getUndoHistoryCount()
undoPosition = general.getUndoHistoryPos()
# In FL Studio, the first (most ancient) undo point in the history seems to have
# the greatest number and the entire history as an index of 1
if general.getUndoHistoryPos() == 1:
status = 0
else:
status = 1
return status
"""
return 1
class A_Series(Core):
""" Controller code specific to A/M-Series keyboards. """
def OnMidiMsgAdd(self, event):
# Mute button - A-Series
if event.data1 == nihia.buttons.button_list.get("MUTE_SELECTED"):
event.handled = True
mixer.muteTrack(mixer.trackNumber())
# Solo button - A-Series
elif event.data1 == nihia.buttons.button_list.get("SOLO_SELECTED"):
event.handled = True
mixer.soloTrack(mixer.trackNumber())
# 4D Encoder up
elif event.data1 == nihia.buttons.button_list.get("ENCODER_Y_A") and event.data2 == nihia.buttons.button_list.get("UP"):
event.handled = True
ui.up()
# 4D Encoder down
elif event.data1 == nihia.buttons.button_list.get("ENCODER_Y_A") and event.data2 == nihia.buttons.button_list.get("DOWN"):
event.handled = True
ui.down()
# 4D Encoder (using FPT because ui.left doesn't work on the playlist)
elif event.data1 == nihia.buttons.button_list.get("ENCODER_X_A") and event.data2 == nihia.buttons.button_list.get("LEFT"):
event.handled = True
if ui.getFocused(midi.widMixer) == True:
# This one doesn't move the mixer view as you get to the border
# ----------------------------------------------------
# mixer.setTrackNumber(mixer.trackNumber() - 1)
# ----------------------------------------------------
ui.left()
else:
ui.left()
# 4D Encoder (using FPT because ui.right doesn't work on the playlist)
elif event.data1 == nihia.buttons.button_list.get("ENCODER_X_A") and event.data2 == nihia.buttons.button_list.get("RIGHT"):
event.handled = True
if ui.getFocused(midi.widMixer) == True:
# This one doesn't move the mixer view as you get to the border
# ----------------------------------------------------
# mixer.setTrackNumber(mixer.trackNumber() + 1)
# ----------------------------------------------------
ui.right()
else:
ui.right()
class S_SeriesMK2(Core):
""" Controller code specific to S-Series MK2 keyboards. """
def OnInitAdd(self):
# Tells to FL Studio the device has peak meters
device.setHasMeters()
# Sets the lights of the 4D Encoder on S-Series keyboards on
nihia.buttons.setLight("ENCODER_X_S", 1)
nihia.buttons.setLight("ENCODER_X_S", 127)
nihia.buttons.setLight("ENCODER_Y_S", 1)
nihia.buttons.setLight("ENCODER_Y_S", 127)
def OnMidiMsgAdd(self, event):
# Mute button - S-Series
if event.data1 == nihia.buttons.button_list.get("MUTE"):
event.handled = True
self.mixerCommandHandler("MUTE", event.data2, mixer.trackNumber())
# Solo button - S-Series
elif event.data1 == nihia.buttons.button_list.get("SOLO"):
event.handled = True
self.mixerCommandHandler("SOLO", event.data2, mixer.trackNumber())
# Track selection (top white buttons) - S-Series
elif event.data1 == nihia.buttons.button_list.get("TRACK_SELECT"):
event.handled = True
self.mixerCommandHandler("TRACK_SELECTION", event.data2, mixer.trackNumber())
# 4D Encoder up
elif event.data1 == nihia.buttons.button_list.get("ENCODER_Y_S") and event.data2 == nihia.buttons.button_list.get("UP"):
event.handled = True
ui.up()
# 4D Encoder down
elif event.data1 == nihia.buttons.button_list.get("ENCODER_Y_S") and event.data2 == nihia.buttons.button_list.get("DOWN"):
event.handled = True
ui.down()
# 4D Encoder (using FPT because ui.left doesn't work on the playlist)
elif event.data1 == nihia.buttons.button_list.get("ENCODER_X_S") and event.data2 == nihia.buttons.button_list.get("LEFT"):
event.handled = True
if ui.getFocused(midi.widMixer) == True:
# This one doesn't move the mixer view as you get to the border
# ----------------------------------------------------
# mixer.setTrackNumber(mixer.trackNumber() - 1)
# ----------------------------------------------------
ui.left()
else:
ui.left()
# 4D Encoder (using FPT because ui.right doesn't work on the playlist)
elif event.data1 == nihia.buttons.button_list.get("ENCODER_X_S") and event.data2 == nihia.buttons.button_list.get("RIGHT"):
event.handled = True
if ui.getFocused(midi.widMixer) == True:
# This one doesn't move the mixer view as you get to the border
# ----------------------------------------------------
# mixer.setTrackNumber(mixer.trackNumber() + 1)
# ----------------------------------------------------
ui.right()
else:
ui.right()
def OnUpdateMeters(self):
self.mixer.sendPeakInfo()
def mixerCommandHandler(self, action: str, targetTrack: int, selectedTrack: int):
""" Handles the way mute, solo and track selection commands are sent from S-Series keyboards.
### Parameters
- action: MUTE, SOLO or TRACK_SELECTION.
- targetTrack: From 0 to 7, the track that the user is trying to mute or solo from the ones showing on the device's mixer.
- selectedTrack: The currently selected track that is used to calculate the track group.
"""
# Uses the function to know which track group the current track belongs to and truncates the value to get the exact number
trackGroup = math.trunc(1/8 * selectedTrack)
# Multiplies the trackGroup to 8 to get the index of the first track that has to be shown
trackFirst = trackGroup * 8
# Adjusts the correct property of the right track
if action == "MUTE":
mixer.muteTrack(trackFirst + targetTrack)
elif action == "SOLO":
mixer.soloTrack(trackFirst + targetTrack)
elif action == "TRACK_SELECTION":
mixer.selectTrack(trackFirst + targetTrack)