From e4135306581861aadcef1b74d515f6e3f07c74cb Mon Sep 17 00:00:00 2001 From: jimmy Date: Thu, 22 Oct 2026 11:44:19 +1300 Subject: [PATCH] Hold Play to move bank 2 up a layer in the SMC-PAD smoother. Co-Authored-By: Claude Sonnet 5.5 --- scripts/pad_layer.py | 64 ++++++++++++++++++++++++++++++ scripts/smooth_smc_pad.py | 46 ++++++++++++++-------- tests/test_pad_layer.py | 83 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 177 insertions(+), 16 deletions(-) create mode 100644 scripts/pad_layer.py create mode 100644 tests/test_pad_layer.py diff --git a/scripts/pad_layer.py b/scripts/pad_layer.py new file mode 100644 index 0000000..1c73a4d --- /dev/null +++ b/scripts/pad_layer.py @@ -0,0 +1,64 @@ +"""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] diff --git a/scripts/smooth_smc_pad.py b/scripts/smooth_smc_pad.py index 991c43e..3401abd 100644 --- a/scripts/smooth_smc_pad.py +++ b/scripts/smooth_smc_pad.py @@ -5,6 +5,10 @@ Reads the physical pad (Bluetooth or USB), passes pad notes through immediately, and eases Control Change values toward their targets at ~120 Hz before emitting on a virtual port named "SMC-PAD Smooth". +On start it also sets the pad lamps over USB (pad_colours.py): bank 1 off and +bank 2 in the colours from shows/test.qxw. On Bluetooth it says so and carries on. +While Play is held, bank 2 notes 52–67 go out as 68–83 (see pad_layer.py). + Point QLC+ MIDI input at that virtual port (midi.qxw does this via patch_smc_pad.py once the smoother is running). @@ -30,6 +34,10 @@ for p in glob.glob(str(ROOT / ".venv" / "lib" / "python*" / "site-packages")): sys.path.insert(0, p) import rtmidi +sys.path.insert(0, str(Path(__file__).resolve().parent)) +from pad_lamps import set_lamps # noqa: E402 +from pad_layer import PlayLayer # noqa: E402 + VIRTUAL_NAME = "SMC-PAD Smooth" SOURCE_HINTS = ( "SMC-PAD Bluetooth", @@ -100,6 +108,12 @@ def main() -> None: help="smoothing time constant in seconds (default 0.055)", ) ap.add_argument("--rate", type=float, default=120.0, help="update Hz") + ap.add_argument( + "--lock-knobs", + action="store_true", + help="knobs 1-10 move test.qxw's custom sliders only after the Custom pad is pressed", + ) + ap.add_argument("--no-lamps", action="store_true", help="leave the pad lamp colours alone") args = ap.parse_args() midiin = rtmidi.MidiIn(rtapi=rtmidi.API_LINUX_ALSA, name="smc-smooth-in") @@ -115,11 +129,14 @@ def main() -> None: current: dict[tuple[int, int], float] = {} target: dict[tuple[int, int], float] = {} last_sent: dict[tuple[int, int], int] = {} + layer = PlayLayer(lock_knobs=args.lock_knobs) print(f"Source: {src_name}", flush=True) print(f"Virtual: {VIRTUAL_NAME} (tau={args.tau}s @ {args.rate} Hz)", flush=True) - print("Pass notes through; smooth CC. Ctrl+C to stop.", flush=True) + print("Pass notes through (Play held shifts bank 2 up 16); smooth CC. Ctrl+C to stop.", flush=True) ensure_alsa_link() + if not args.no_lamps: + set_lamps() dt = 1.0 / args.rate alpha = 1.0 - math.exp(-dt / max(args.tau, 1e-4)) @@ -140,21 +157,18 @@ def main() -> None: data, _ = msg if not data: continue - status = data[0] - cmd = status & 0xF0 - ch = status & 0x0F - - if cmd == 0xB0 and len(data) >= 3: - # Control Change → smooth - cc, val = data[1], data[2] - key = (ch, cc) - target[key] = float(val) - if key not in current: - current[key] = float(val) - last_sent[key] = -1 - else: - # Notes, aftertouch, PC, etc. → immediate - midiout.send_message(data) + for out in layer.handle(list(data)): + cmd = out[0] & 0xF0 + if cmd == 0xB0 and len(out) >= 3: + # Control Change → smooth + key = (out[0] & 0x0F, out[1]) + target[key] = float(out[2]) + if key not in current: + current[key] = float(out[2]) + last_sent[key] = -1 + else: + # Notes, aftertouch, PC, etc. → immediate + midiout.send_message(out) # Ease CC toward targets and emit when quantized value moves for key, tgt in list(target.items()): diff --git a/tests/test_pad_layer.py b/tests/test_pad_layer.py new file mode 100644 index 0000000..7acf69f --- /dev/null +++ b/tests/test_pad_layer.py @@ -0,0 +1,83 @@ +import sys +import unittest +from pathlib import Path + +sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "scripts")) + +from pad_layer import PlayLayer # noqa: E402 + +PLAY_DOWN = [0xB0, 27, 127] +PLAY_UP = [0xB0, 27, 0] + + +class PlayLayerTest(unittest.TestCase): + def test_notes_pass_through_when_play_is_up(self): + layer = PlayLayer() + self.assertEqual(layer.handle([0x99, 52, 100]), [[0x99, 52, 100]]) + + def test_play_is_not_passed_on(self): + layer = PlayLayer() + self.assertEqual(layer.handle(PLAY_DOWN), []) + self.assertEqual(layer.handle(PLAY_UP), []) + + def test_bank_2_moves_up_while_play_is_held(self): + layer = PlayLayer() + layer.handle(PLAY_DOWN) + self.assertEqual(layer.handle([0x99, 52, 100]), [[0x99, 68, 100]]) + self.assertEqual(layer.handle([0x99, 67, 100]), [[0x99, 83, 100]]) + layer.handle(PLAY_UP) + self.assertEqual(layer.handle([0x99, 52, 100]), [[0x99, 52, 100]]) + + def test_other_notes_and_ccs_are_left_alone(self): + layer = PlayLayer() + layer.handle(PLAY_DOWN) + self.assertEqual(layer.handle([0x99, 40, 100]), [[0x99, 40, 100]]) + self.assertEqual(layer.handle([0xB0, 30, 64]), [[0xB0, 30, 64]]) + + def test_release_matches_its_press(self): + layer = PlayLayer() + layer.handle(PLAY_DOWN) + layer.handle([0x99, 55, 100]) + layer.handle(PLAY_UP) + self.assertEqual(layer.handle([0x89, 55, 0]), [[0x89, 71, 0]]) + layer.handle([0x99, 55, 100]) + layer.handle(PLAY_DOWN) + self.assertEqual(layer.handle([0x99, 55, 0]), [[0x99, 55, 0]]) + + +if __name__ == "__main__": + unittest.main() + + +class LockedKnobsTest(unittest.TestCase): + def test_knobs_are_dropped_until_custom_is_pressed(self): + layer = PlayLayer(lock_knobs=True) + self.assertEqual(layer.handle([0xB0, 31, 64]), []) + layer.handle([0x99, 67, 100]) + self.assertEqual(layer.handle([0xB0, 31, 64]), [[0xB0, 31, 64]]) + self.assertEqual(layer.handle([0xB0, 36, 64]), []) + + def test_another_pad_locks_them_again(self): + layer = PlayLayer(lock_knobs=True) + layer.handle([0x99, 67, 100]) + layer.handle([0x99, 60, 100]) + self.assertEqual(layer.handle([0xB0, 31, 64]), []) + + def test_locking_sends_the_sliders_back_to_zero(self): + layer = PlayLayer(lock_knobs=True) + layer.handle([0x99, 67, 100]) + sent = layer.handle([0x99, 60, 100]) + self.assertEqual(sent[0], [0x99, 60, 100]) + self.assertEqual(sent[1:], [[0xB0, 30 + n, 0] for n in range(5)]) + self.assertEqual(layer.handle([0x99, 61, 100]), [[0x99, 61, 100]]) + + def test_play_layer_unlocks_the_second_set(self): + layer = PlayLayer(lock_knobs=True) + layer.handle(PLAY_DOWN) + layer.handle([0x99, 67, 100]) + self.assertEqual(layer.handle([0xB0, 36, 64]), [[0xB0, 36, 64]]) + self.assertEqual(layer.handle([0xB0, 31, 64]), []) + + def test_other_knobs_and_unlocked_mode_are_untouched(self): + self.assertEqual(PlayLayer(lock_knobs=True).handle([0xB0, 40, 64]), [[0xB0, 40, 64]]) + self.assertEqual(PlayLayer().handle([0xB0, 31, 64]), [[0xB0, 31, 64]])