65 lines
2.5 KiB
Python
65 lines
2.5 KiB
Python
"""Hold Play to move the bank 2 pads up a layer.
|
||
|
||
The SMC-PAD sends CC 27 as 127 when Play goes down and 0 when it comes up.
|
||
While it is down, pad notes 52–67 go out as 68–83, so QLC+ can tell a
|
||
second set of buttons from the first. Play itself is not passed on.
|
||
|
||
With lock_knobs, knobs 1–5 (CC 30–34) drive colour 1's sliders and knobs 6–10
|
||
(CC 35–39) drive colour 2's, but only after the Custom pad (note 67, or 83
|
||
with Play held) has been pressed. Pressing any other bank 2 pad locks them
|
||
again, and its sliders are sent back to zero so their scenes stop.
|
||
Locked knob turns are dropped.
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
PLAY_CC = 27
|
||
FIRST_NOTE = 52
|
||
LAST_NOTE = 67
|
||
SHIFT = 16
|
||
CUSTOM_NOTE = 67
|
||
KNOB_FIRST_CC = 30
|
||
KNOBS_PER_LAYER = 5
|
||
|
||
|
||
class PlayLayer:
|
||
def __init__(self, lock_knobs: bool = False) -> None:
|
||
self.lock_knobs = lock_knobs
|
||
self.unlocked: set[int] = set() # layers 0 and 1
|
||
self.held = False
|
||
# (channel, note as pressed) -> note sent, so a release matches its press
|
||
self.sent: dict[tuple[int, int], int] = {}
|
||
|
||
def handle(self, data: list[int]) -> list[list[int]]:
|
||
"""The messages to send on. Empty when the message is swallowed."""
|
||
status = data[0]
|
||
kind = status & 0xF0
|
||
channel = status & 0x0F
|
||
if kind == 0xB0 and len(data) >= 3 and data[1] == PLAY_CC:
|
||
self.held = data[2] > 0
|
||
return []
|
||
if kind == 0xB0 and len(data) >= 3 and self.lock_knobs:
|
||
index = data[1] - KNOB_FIRST_CC
|
||
if 0 <= index < 2 * KNOBS_PER_LAYER and index // KNOBS_PER_LAYER not in self.unlocked:
|
||
return []
|
||
if kind not in (0x80, 0x90) or len(data) < 3:
|
||
return [list(data)]
|
||
note = data[1]
|
||
key = (channel, note)
|
||
is_on = kind == 0x90 and data[2] > 0
|
||
extra: list[list[int]] = []
|
||
if is_on and FIRST_NOTE <= note <= LAST_NOTE:
|
||
layer = 1 if self.held else 0
|
||
if note == CUSTOM_NOTE:
|
||
self.unlocked.add(layer)
|
||
elif layer in self.unlocked:
|
||
self.unlocked.discard(layer)
|
||
first = KNOB_FIRST_CC + layer * KNOBS_PER_LAYER
|
||
extra = [[0xB0, first + n, 0] for n in range(KNOBS_PER_LAYER)]
|
||
if is_on:
|
||
out = note + SHIFT if self.held and FIRST_NOTE <= note <= LAST_NOTE else note
|
||
self.sent[key] = out
|
||
else:
|
||
out = self.sent.pop(key, note)
|
||
return [[status, out, *data[2:]], *extra]
|