From b37af2234a6e4228e5057efe77e3dea581475804 Mon Sep 17 00:00:00 2001 From: Jimmy Date: Fri, 9 Oct 2026 14:00:09 +1300 Subject: [PATCH] Add an SMC-PAD MIDI listener that maps pads and knobs. Learn pad and knob banks into shows/midi_map.json, then print pad and knob numbers with relative encoder steps. Co-authored-by: Cursor --- .gitignore | 1 + Pipfile | 18 ++++ Pipfile.lock | 71 +++++++++++++ scripts/midi.py | 194 ++++++++++++++++++++++++++++++++++ shows/midi_map.json | 246 ++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 530 insertions(+) create mode 100644 Pipfile create mode 100644 Pipfile.lock create mode 100644 scripts/midi.py create mode 100644 shows/midi_map.json diff --git a/.gitignore b/.gitignore index 044b806..36fbca7 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ __pycache__/ *.py[cod] *.qxw~ .DS_Store +.venv/ diff --git a/Pipfile b/Pipfile new file mode 100644 index 0000000..3c6bb31 --- /dev/null +++ b/Pipfile @@ -0,0 +1,18 @@ +[[source]] +url = "https://pypi.org/simple" +verify_ssl = true +name = "pypi" + +[packages] +mido = "*" +python-rtmidi = "*" + +[dev-packages] + +[requires] +python_version = "3.12" + +[scripts] +positions = "python scripts/position_movers.py" +pattern = "python -m pattern_pad" +midi = "python scripts/midi.py" diff --git a/Pipfile.lock b/Pipfile.lock new file mode 100644 index 0000000..d454fa0 --- /dev/null +++ b/Pipfile.lock @@ -0,0 +1,71 @@ +{ + "_meta": { + "hash": { + "sha256": "2d3b4d17f2edcaf6aca89b25728de94c131f89c846aebf67a8c6ec4981497791" + }, + "pipfile-spec": 6, + "requires": { + "python_version": "3.12" + }, + "sources": [ + { + "name": "pypi", + "url": "https://pypi.org/simple", + "verify_ssl": true + } + ] + }, + "default": { + "mido": { + "hashes": [ + "sha256:01033c9b10b049e4436fca2762194ca839b09a4334091dd3c34e7f4ae674fd8a", + "sha256:1aecb30b7f282404f17e43768cbf74a6a31bf22b3b783bdd117a1ce9d22cb74c" + ], + "index": "pypi", + "markers": "python_version ~= '3.7'", + "version": "==1.3.3" + }, + "packaging": { + "hashes": [ + "sha256:94edc256424af38762eb31306eed28beb9f0efc50a8837492c9d6fd6004aed79", + "sha256:d7193f7c8e4e93f444fde0262bf90af30e16fa0ad0ad44cb553c87339b23cd1c" + ], + "markers": "python_version >= '3.9'", + "version": "==26.3" + }, + "python-rtmidi": { + "hashes": [ + "sha256:052c89933cae4fca354012d8ca7248f4f9e1e3f062471409d48415a7f7d7e59e", + "sha256:1d5da765184150fb946043d59be4039b36a8060ede025f109ef20492dbf99075", + "sha256:25f5a5db7be98911c41ca5bebb262fcf9a7c89600b88fd3c207ceafd3101e721", + "sha256:26149186367341bf5b0a3ac17b495f6a25950bd3da6b4f13d25ac0a9ce8208dd", + "sha256:271d625c489fffb39b3edc5aba67f7c8e29a04a0a0f056ce19e5a888a08b4c59", + "sha256:29661939f9b7bd1a4e29835f50f4790e741dacd21a5cb143297aefb51deefdec", + "sha256:29d9c9d9f82ce679fecad7bb4cb79f3a24574ea84600e377194b4cc1baacec0e", + "sha256:30d117193dcad8af67c600c405f53eb096e4ff84849760be14c97270af334922", + "sha256:46bbf32c8a4bf6c8f0df1c02a68689d0757f13cb7a69f27ccbbed3d7b2365918", + "sha256:4e234dca7f9d783dd3f1e9c9c5c2f295f02b7af3085301d6eed3b428cf49d327", + "sha256:5443634597eb340cdec0734f76267a827c2d366f00a6f9195141c78828016ac2", + "sha256:5966172ed28add6ff2b76d389702931bfc7ff3cc741c0e4b0d1aaae269ab7a8e", + "sha256:7bce7f17c71a71d8ef0bfeae3cb8a7652dd02f0d5067de882e1ee44eb38518db", + "sha256:7f9ade68b068ae09000ecb562ae9521da3a234361ad5449e83fc734544d004fa", + "sha256:82e61bc1b51aa91d9e615827056e80f78dbe364248eecd61698b233f7af903f6", + "sha256:844bd12840c9d4e03dfc89b2cd57c55dcbf5ed7246504d69c6c661732249b19c", + "sha256:878ce085dfb65c0974810a7e919f73708cbb4c0430c7924b78f25aea1dd4ebee", + "sha256:8bbaf7c7164471712a93ac60c8f9ed146b336a294a5103223bbaf8f10709a0bf", + "sha256:a5582983ad57ea7f0a7797ddc3e258efb00f8326113b6ddfa85b5165a4151806", + "sha256:a706e9850e22acc57fa840c60fdc4541baafe462a05ff7631a6d9eb91c65e171", + "sha256:c60dd180e5130fb87571e71aea30e2ef0512131aab45865a7d67063ed8e52ca4", + "sha256:cec30924e305f55284594ccf35a71dee7216fd308dfa2dec1b3ed03e6f243803", + "sha256:cfea32c91752fa7aecfe3d6827535c190ba0e646a9accd6604f4fc70cf4b780f", + "sha256:dd2bcbea822488fca6b8d9fc7e78a91da12914f3b88dc086f051cb65a643449f", + "sha256:efc07413b30b0039c0d35abe25a81d740c7405124eb58eed141a8f24388e6fe0", + "sha256:f2138005c6bd3d8b9af05df383679f6d0827d16056e68a941110732310dcb7dd" + ], + "index": "pypi", + "markers": "python_version >= '3.8'", + "version": "==1.5.8" + } + }, + "develop": {} +} diff --git a/scripts/midi.py b/scripts/midi.py new file mode 100644 index 0000000..7e6325e --- /dev/null +++ b/scripts/midi.py @@ -0,0 +1,194 @@ +#!/usr/bin/env python3 +"""Print SMC-PAD pad and knob numbers. + + pipenv run python scripts/midi.py + +Each pad press prints pad N. Each knob turn prints knob N and its step +(+ for up, - for down). +Type 1 on pad 1 to store pads 1–16, or 17 on pad 17 to store pads 17–32. +Type 1 on knob 1 to store knobs 1–8, or 9 on knob 9 to store knobs 9–16. +Mappings are saved to shows/midi_map.json. +""" + +from __future__ import annotations + +import json +import sys +from pathlib import Path + +import mido + +ROOT = Path(__file__).resolve().parents[1] +MAP_PATH = ROOT / "shows" / "midi_map.json" + +# (channel, note) -> pad number. Loaded from shows/midi_map.json. +PADS: dict[tuple[int, int], int] = {} +# (channel, controller) -> knob number. +KNOBS: dict[tuple[int, int], int] = {} + +PAD_BANK = 16 +KNOB_BANK = 8 + + +def load_maps(path: Path = MAP_PATH) -> None: + PADS.clear() + KNOBS.clear() + if not path.is_file(): + return + data = json.loads(path.read_text()) + for item in data.get("pads", []): + PADS[(int(item["channel"]), int(item["note"]))] = int(item["pad"]) + for item in data.get("knobs", []): + KNOBS[(int(item["channel"]), int(item["cc"]))] = int(item["knob"]) + + +def save_maps(path: Path = MAP_PATH) -> None: + data = { + "pads": [ + {"channel": channel, "note": note, "pad": pad} + for (channel, note), pad in sorted(PADS.items()) + ], + "knobs": [ + {"channel": channel, "cc": cc, "knob": knob} + for (channel, cc), knob in sorted(KNOBS.items()) + ], + } + path.parent.mkdir(parents=True, exist_ok=True) + path.write_text(json.dumps(data, indent=2) + "\n") + + +def ask(kind: str) -> int | None: + word = "pad" if kind == "note" else "knob" + raw = input(f"{word}: ").strip() + if raw == "": + return None + if not raw.isdigit(): + print("enter a number", flush=True) + return None + return int(raw) + + +def assign_bank(kind: str, channel: int, number: int, name: int) -> tuple[int, int]: + """Store one control, or a whole bank when name is the first in that bank.""" + size = PAD_BANK if kind == "note" else KNOB_BANK + target = PADS if kind == "note" else KNOBS + if name % size != 1: + target[(channel, number)] = name + return name, name + for key, value in list(target.items()): + if name <= value < name + size: + del target[key] + for offset in range(size): + target[(channel, number + offset)] = name + offset + return name, name + size - 1 + + +def remember(kind: str, channel: int, number: int, name: int) -> None: + first, last = assign_bank(kind, channel, number, name) + word = "pad" if kind == "note" else "knob" + if first == last: + print(f"{word} {first}", flush=True) + else: + print(f"{word}s {first}–{last}", flush=True) + save_maps() + print(f"saved {MAP_PATH}", flush=True) + + +def encoder_delta(value: int) -> int: + """Relative encoder step. 1..63 clockwise, 65..127 counter-clockwise.""" + if value <= 0 or value == 64 or value > 127: + return 0 + if value < 64: + return value + return value - 128 + + +def label(kind: str, channel: int, number: int, value: int | None = None) -> str | None: + if kind == "note": + pad = PADS.get((channel, number)) + if pad is None: + return None + return f"pad {pad}" + knob = KNOBS.get((channel, number)) + if knob is None: + return None + if value is None: + return f"knob {knob}" + delta = encoder_delta(value) + if delta == 0: + return None + return f"knob {knob} {delta:+d}" + + +def find_port(wanted: str | None) -> str: + names = mido.get_input_names() + if wanted: + for name in names: + if wanted in name: + return name + raise SystemExit(f"No MIDI input matching {wanted}. Ports: {', '.join(names) or 'none'}") + for name in names: + low = name.lower() + if "private" in low: + continue + if "smc-pad" in low or "sinco" in low: + return name + raise SystemExit("SMC-PAD not found. Plug it in, or pass part of the port name.") + + +def midi_hint(kind: str, channel: int, number: int, value: int) -> str: + if kind == "note": + return f"channel {channel} note {number} velocity {value}" + return f"channel {channel} cc {number} value {value}" + + +def listen(port: str) -> None: + skipped: set[tuple[str, int, int]] = set() + with mido.open_input(port) as incoming: + for msg in incoming: + if msg.type == "note_on" and msg.velocity > 0: + kind, number, value = "note", msg.note, msg.velocity + elif msg.type == "control_change": + kind, number, value = "cc", msg.control, msg.value + else: + continue + name = label(kind, msg.channel, number, value) + if name: + print(name, flush=True) + continue + if kind == "cc" and (msg.channel, number) in KNOBS: + continue + print(midi_hint(kind, msg.channel, number, value), flush=True) + key = (kind, msg.channel, number) + if key in skipped: + continue + try: + entered = ask(kind) + except EOFError: + return + if entered is None: + skipped.add(key) + continue + remember(kind, msg.channel, number, entered) + name = label(kind, msg.channel, number, value) + if name: + print(name, flush=True) + + +def main() -> int: + load_maps() + wanted = sys.argv[1] if len(sys.argv) > 1 else None + port = find_port(wanted) + print(f"Listening on {port}. Mappings in {MAP_PATH}.", flush=True) + print("Pad 1 stores pads 1–16. Pad 17 stores pads 17–32.", flush=True) + print("Knob 1 stores knobs 1–8. Knob 9 stores knobs 9–16.", flush=True) + print("Ctrl+C to stop.", flush=True) + try: + listen(port) + except KeyboardInterrupt: + return 0 + return 0 + + +if __name__ == "__main__": + raise SystemExit(main()) diff --git a/shows/midi_map.json b/shows/midi_map.json new file mode 100644 index 0000000..a51fc26 --- /dev/null +++ b/shows/midi_map.json @@ -0,0 +1,246 @@ +{ + "pads": [ + { + "channel": 9, + "note": 36, + "pad": 1 + }, + { + "channel": 9, + "note": 37, + "pad": 2 + }, + { + "channel": 9, + "note": 38, + "pad": 3 + }, + { + "channel": 9, + "note": 39, + "pad": 4 + }, + { + "channel": 9, + "note": 40, + "pad": 5 + }, + { + "channel": 9, + "note": 41, + "pad": 6 + }, + { + "channel": 9, + "note": 42, + "pad": 7 + }, + { + "channel": 9, + "note": 43, + "pad": 8 + }, + { + "channel": 9, + "note": 44, + "pad": 9 + }, + { + "channel": 9, + "note": 45, + "pad": 10 + }, + { + "channel": 9, + "note": 46, + "pad": 11 + }, + { + "channel": 9, + "note": 47, + "pad": 12 + }, + { + "channel": 9, + "note": 48, + "pad": 13 + }, + { + "channel": 9, + "note": 49, + "pad": 14 + }, + { + "channel": 9, + "note": 50, + "pad": 15 + }, + { + "channel": 9, + "note": 51, + "pad": 16 + }, + { + "channel": 9, + "note": 52, + "pad": 17 + }, + { + "channel": 9, + "note": 53, + "pad": 18 + }, + { + "channel": 9, + "note": 54, + "pad": 19 + }, + { + "channel": 9, + "note": 55, + "pad": 20 + }, + { + "channel": 9, + "note": 56, + "pad": 21 + }, + { + "channel": 9, + "note": 57, + "pad": 22 + }, + { + "channel": 9, + "note": 58, + "pad": 23 + }, + { + "channel": 9, + "note": 59, + "pad": 24 + }, + { + "channel": 9, + "note": 60, + "pad": 25 + }, + { + "channel": 9, + "note": 61, + "pad": 26 + }, + { + "channel": 9, + "note": 62, + "pad": 27 + }, + { + "channel": 9, + "note": 63, + "pad": 28 + }, + { + "channel": 9, + "note": 64, + "pad": 29 + }, + { + "channel": 9, + "note": 65, + "pad": 30 + }, + { + "channel": 9, + "note": 66, + "pad": 31 + }, + { + "channel": 9, + "note": 67, + "pad": 32 + } + ], + "knobs": [ + { + "channel": 0, + "cc": 30, + "knob": 1 + }, + { + "channel": 0, + "cc": 31, + "knob": 2 + }, + { + "channel": 0, + "cc": 32, + "knob": 3 + }, + { + "channel": 0, + "cc": 33, + "knob": 4 + }, + { + "channel": 0, + "cc": 34, + "knob": 5 + }, + { + "channel": 0, + "cc": 35, + "knob": 6 + }, + { + "channel": 0, + "cc": 36, + "knob": 7 + }, + { + "channel": 0, + "cc": 37, + "knob": 8 + }, + { + "channel": 0, + "cc": 38, + "knob": 9 + }, + { + "channel": 0, + "cc": 39, + "knob": 10 + }, + { + "channel": 0, + "cc": 40, + "knob": 11 + }, + { + "channel": 0, + "cc": 41, + "knob": 12 + }, + { + "channel": 0, + "cc": 42, + "knob": 13 + }, + { + "channel": 0, + "cc": 43, + "knob": 14 + }, + { + "channel": 0, + "cc": 44, + "knob": 15 + }, + { + "channel": 0, + "cc": 45, + "knob": 16 + } + ] +}