Group scripts into pad/, shows/ and anim/ folders.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
5f593b973c
commit
647cfa3a75
26 files changed
+68
-66
No files matched your search
@@ -0,0 +1,194 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Print SMC-PAD pad and knob numbers.
|
||||
|
||||
pipenv run python scripts/pad/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[2]
|
||||
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())
|
||||
Reference in new issue
Block a user