diff --git a/Pipfile b/Pipfile index 645640b..d3bf0b4 100644 --- a/Pipfile +++ b/Pipfile @@ -14,4 +14,5 @@ python_version = "3.12" [scripts] midi = "python scripts/pad/midi.py" +intercept = "python -m padeditor.intercept" padeditor = "python -m padeditor" diff --git a/README.md b/README.md index da1d338..b9fa07d 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ shows/ Workspaces to open in QLC+ small_eye.qxw Movers-only figure-of-8 show archive/ Older scratch workspaces scripts/ Launchers (pattern_studio.sh, flash_luxdmx_xiao.sh) - pad/ SMC-PAD tools: smoother, Play layer, lamps, MIDI patching + pad/ SMC-PAD tools: lamps, MIDI patching, capture decoding shows/ Generators / patchers for the shows and Chataigne anim/ showfile animations to preview over Art-Net stage/ Stage map (SVG, JSON, canvas) @@ -51,6 +51,20 @@ python3 scripts/shows/figure8_small_eye.py --write-qxw shows/small_eye.qxw Copy `.qxf` files from `fixtures/` into QLC+’s fixture library if the manufacturer/model is missing. +## SMC-PAD intercept + +`padeditor.intercept` reads the SMC-PAD, smooths its knobs, applies the Play +layer, and sends the result on a virtual MIDI port named `SMC-PAD Smooth` for +QLC+. Run it by hand with `.venv/bin/python -m padeditor.intercept`, or install +it as a systemd user service that starts at login and waits for the pad: + +```bash +padeditor/systemd/install.sh +``` + +Options after the command go to the intercept (for example `--lock-knobs`). +`--uninstall` removes the service. Logs: `journalctl --user -u smc-pad-intercept -f`. + ## Chataigne `scripts/shows/make_chataigne.py` turns a show spec into a Chataigne project, so the @@ -62,8 +76,8 @@ python3 scripts/shows/make_chataigne.py shows/test.show.json chataigne/test Open `chataigne/test/test.noisette`. The **Pad** (MIDI) module reads the SMC-PAD directly (`--pad` picks the port: USB `SINCO SMC-PAD-Master` by default, or -`SMC-PAD Bluetooth`). The script does the Play-layer shift that `pad_layer.py` does for -QLC+, with no knob smoothing, so `smooth_smc_pad.py` is not needed. Set the lamps with +`SMC-PAD Bluetooth`). The script does the Play-layer shift that `padeditor/layer.py` does for +QLC+, with no knob smoothing, so the intercept is not needed. Set the lamps with `scripts/pad/pad_lamps.py`. The **Lights** (DMX) module sends Art-Net to the node in the spec. The **Preview** module sends a copy to 127.0.0.1 for the simulator (`--preview ''` leaves it out). If it comes up on the wrong output, set DMX Type to Art-Net, the remote host, and an output universe of net 0, subnet 0, universe 0. diff --git a/chataigne/show.template.js b/chataigne/show.template.js index 00721be..712db0c 100644 --- a/chataigne/show.template.js +++ b/chataigne/show.template.js @@ -1,5 +1,5 @@ // Pads pick colours, sliders mix a custom colour, like the QLC+ virtual console. -// It reads the pad directly: Play shifts bank 2 here, as scripts/pad/pad_layer.py does +// It reads the pad directly: Play shifts bank 2 here, as padeditor/layer.py does // when QLC+ is used. // Generated by scripts/shows/make_chataigne.py: edit this template, not the output. // @@ -11,7 +11,7 @@ var LAMP = "Lamp"; var LAMP_OFF = 0.3; // brightness of a pad's tile when it is not playing var FADE_RATE = 100; // script updates per second -var PLAY_CC = 27; // hold Play to move bank 2 (notes 52-67) up 16, like pad_layer.py +var PLAY_CC = 27; // hold Play to move bank 2 (notes 52-67) up 16, like padeditor/layer.py var BANK_FIRST = 52; var BANK_LAST = 67; var BANK_SHIFT = 16; diff --git a/chataigne/test/test.js b/chataigne/test/test.js index 64fef6d..4d63392 100644 --- a/chataigne/test/test.js +++ b/chataigne/test/test.js @@ -1,5 +1,5 @@ // Pads pick colours, sliders mix a custom colour, like the QLC+ virtual console. -// It reads the pad directly: Play shifts bank 2 here, as scripts/pad/pad_layer.py does +// It reads the pad directly: Play shifts bank 2 here, as padeditor/layer.py does // when QLC+ is used. // Generated by scripts/shows/make_chataigne.py: edit this template, not the output. // @@ -70,7 +70,7 @@ var GROUPS = [ var LAMP = "Lamp"; var LAMP_OFF = 0.3; // brightness of a pad's tile when it is not playing var FADE_RATE = 100; // script updates per second -var PLAY_CC = 27; // hold Play to move bank 2 (notes 52-67) up 16, like pad_layer.py +var PLAY_CC = 27; // hold Play to move bank 2 (notes 52-67) up 16, like padeditor/layer.py var BANK_FIRST = 52; var BANK_LAST = 67; var BANK_SHIFT = 16; diff --git a/scripts/pad/smooth_smc_pad.py b/padeditor/intercept.py similarity index 91% rename from scripts/pad/smooth_smc_pad.py rename to padeditor/intercept.py index 3be7aa5..336e86b 100644 --- a/scripts/pad/smooth_smc_pad.py +++ b/padeditor/intercept.py @@ -1,20 +1,21 @@ -#!/usr/bin/env python3 -"""Smooth SMC-PAD knob CCs so RGBW fades feel less stepped. +"""Intercept the SMC-PAD's MIDI and smooth its knob CCs so RGBW fades feel less stepped. 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 +On start it also sets the pad lamps over USB (scripts/pad/pad_lamps.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). +While Play is held, bank 2 notes 52–67 go out as 68–83 (see padeditor/layer.py). Point QLC+ MIDI input at that virtual port (midi.qxw does this via -patch_smc_pad.py once the smoother is running). +patch_smc_pad.py once the intercept is running). Usage: - .venv/bin/python scripts/pad/smooth_smc_pad.py + .venv/bin/python -m padeditor.intercept # optional: --tau 0.05 (seconds; lower = snappier, higher = silkier) + +padeditor/systemd/install.sh runs it as a systemd user service. """ from __future__ import annotations @@ -27,16 +28,18 @@ import sys import time from pathlib import Path -ROOT = Path(__file__).resolve().parents[2] +ROOT = Path(__file__).resolve().parents[1] import glob 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 padeditor.layer import PlayLayer # noqa: E402 + +# The lamp colours come from the show scripts, which are not part of this package. +sys.path.insert(0, str(ROOT / "scripts" / "pad")) from pad_lamps import set_lamps # noqa: E402 -from pad_layer import PlayLayer # noqa: E402 VIRTUAL_NAME = "SMC-PAD Smooth" SOURCE_HINTS = ( diff --git a/scripts/pad/pad_layer.py b/padeditor/layer.py similarity index 100% rename from scripts/pad/pad_layer.py rename to padeditor/layer.py diff --git a/padeditor/systemd/install.sh b/padeditor/systemd/install.sh new file mode 100755 index 0000000..11d4a2d --- /dev/null +++ b/padeditor/systemd/install.sh @@ -0,0 +1,40 @@ +#!/usr/bin/env bash +# Install the SMC-PAD MIDI intercept as a systemd user service and start it. +# +# padeditor/systemd/install.sh # install, enable, start +# padeditor/systemd/install.sh --lock-knobs # pass options to the intercept +# padeditor/systemd/install.sh --uninstall +# +# Logs: journalctl --user -u smc-pad-intercept -f +set -euo pipefail + +UNIT=smc-pad-intercept.service +HERE="$(cd "$(dirname "$0")" && pwd)" +ROOT="$(cd "$HERE/../.." && pwd)" +DEST="${XDG_CONFIG_HOME:-$HOME/.config}/systemd/user" + +if [[ "${1:-}" == "--uninstall" ]]; then + systemctl --user disable --now "$UNIT" 2>/dev/null || true + rm -f "$DEST/$UNIT" + systemctl --user daemon-reload + echo "Removed $UNIT." + exit 0 +fi + +PYTHON="$ROOT/.venv/bin/python" +[[ -x "$PYTHON" ]] || PYTHON="$(command -v python3)" +"$PYTHON" -c "import rtmidi" 2>/dev/null || { + echo "$PYTHON cannot import rtmidi. Run pipenv install (or pip install python-rtmidi) first." >&2 + exit 1 +} + +ARGS="$*" +mkdir -p "$DEST" +sed -e "s|@ROOT@|$ROOT|g" -e "s|@PYTHON@|$PYTHON|g" -e "s|@ARGS@|$ARGS|g" \ + "$HERE/$UNIT" >"$DEST/$UNIT" + +systemctl --user daemon-reload +systemctl --user enable "$UNIT" +systemctl --user restart "$UNIT" +echo "Installed $DEST/$UNIT and started it." +systemctl --user --no-pager status "$UNIT" | head -5 || true diff --git a/padeditor/systemd/smc-pad-intercept.service b/padeditor/systemd/smc-pad-intercept.service new file mode 100644 index 0000000..71285e6 --- /dev/null +++ b/padeditor/systemd/smc-pad-intercept.service @@ -0,0 +1,22 @@ +# SMC-PAD MIDI intercept (padeditor.intercept) as a systemd user service. +# install.sh fills in @ROOT@, @PYTHON@ and @ARGS@ and copies it to +# ~/.config/systemd/user/. + +[Unit] +Description=SMC-PAD MIDI intercept (virtual port "SMC-PAD Smooth") +After=sound.target +# Keep retrying while the pad is switched off or not paired yet. +StartLimitIntervalSec=0 + +[Service] +Type=simple +WorkingDirectory=@ROOT@ +ExecStart=@PYTHON@ -m padeditor.intercept @ARGS@ +Environment=PYTHONUNBUFFERED=1 +# The intercept closes its ports on Ctrl+C. +KillSignal=SIGINT +Restart=always +RestartSec=3 + +[Install] +WantedBy=default.target diff --git a/scripts/pad/pad_lamps.py b/scripts/pad/pad_lamps.py index 6363fb5..14befae 100644 --- a/scripts/pad/pad_lamps.py +++ b/scripts/pad/pad_lamps.py @@ -3,7 +3,7 @@ .venv/bin/python scripts/pad/pad_lamps.py -Needs the USB Private port. smooth_smc_pad.py does this on start; run this +Needs the USB Private port. padeditor.intercept does this on start; run this when the pad is read by Chataigne instead. """ diff --git a/scripts/pad/patch_smc_pad.py b/scripts/pad/patch_smc_pad.py index ed40f8d..bc5307b 100644 --- a/scripts/pad/patch_smc_pad.py +++ b/scripts/pad/patch_smc_pad.py @@ -75,7 +75,7 @@ PAD_COLORS = [ def detect_midi_uid() -> str: - """Prefer smoothed virtual port when the smoother daemon is running.""" + """Prefer smoothed virtual port when the intercept (padeditor.intercept) is running.""" try: out = subprocess.check_output(["aconnect", "-l"], text=True) except (FileNotFoundError, subprocess.CalledProcessError): diff --git a/scripts/pad/start_midi.sh b/scripts/pad/start_midi.sh index 18d967d..0e65981 100755 --- a/scripts/pad/start_midi.sh +++ b/scripts/pad/start_midi.sh @@ -3,7 +3,7 @@ # # Order matters: # 1) pad present (Bluetooth or USB) -# 2) smoother (creates "SMC-PAD Smooth") +# 2) intercept (creates "SMC-PAD Smooth") # 3) patch workspace to that port # 4) open QLC+ last set -euo pipefail @@ -30,12 +30,21 @@ if ! pad_present; then exit 1 fi -# Restart smoother so it re-opens the pad port cleanly (avoid pkill -f; it matches this script) -while read -r pid; do - [[ -n "$pid" ]] && kill "$pid" 2>/dev/null || true -done < <(ps -eo pid,args | awk '/[.]venv\/bin\/python .*smooth_smc_pad[.]py/ {print $1}') -sleep 0.4 -nohup "$PYTHON" scripts/pad/smooth_smc_pad.py --tau 0.055 >>/tmp/smooth_smc_pad.log 2>&1 & +# Restart the intercept so it re-opens the pad port cleanly. Use the systemd +# user service when it is installed (padeditor/systemd/install.sh). +SERVICE=smc-pad-intercept.service +LOG=/tmp/smc_pad_intercept.log +if systemctl --user is-enabled --quiet "$SERVICE" 2>/dev/null; then + systemctl --user restart "$SERVICE" + LOG= +else + # avoid pkill -f; it matches this script + while read -r pid; do + [[ -n "$pid" ]] && kill "$pid" 2>/dev/null || true + done < <(ps -eo pid,args | awk '/python.* -m padeditor[.]intercept/ {print $1}') + sleep 0.4 + nohup "$PYTHON" -m padeditor.intercept --tau 0.055 >>"$LOG" 2>&1 & +fi for _ in $(seq 1 20); do if aconnect -l 2>/dev/null | grep -q 'SMC-PAD Smooth'; then break @@ -43,8 +52,12 @@ for _ in $(seq 1 20); do sleep 0.25 done if ! aconnect -l 2>/dev/null | grep -q 'SMC-PAD Smooth'; then - echo "Smoother failed to create SMC-PAD Smooth. See /tmp/smooth_smc_pad.log" >&2 - tail -20 /tmp/smooth_smc_pad.log >&2 || true + echo "The intercept failed to create SMC-PAD Smooth." >&2 + if [[ -n "$LOG" ]]; then + tail -20 "$LOG" >&2 || true + else + journalctl --user -u "$SERVICE" -n 20 --no-pager >&2 || true + fi exit 1 fi diff --git a/scripts/shows/colour_sets.py b/scripts/shows/colour_sets.py index 5659064..f4441d5 100644 --- a/scripts/shows/colour_sets.py +++ b/scripts/shows/colour_sets.py @@ -4,15 +4,15 @@ Colour 1 paints all 15 cans and colour 2 the four movers, so one colour for each can be on at once. Each set is a solo frame and every scene fades in and out over FADE_MS. Bank 2 (notes 52–67) picks colour 1. -Holding Play picks set B: the smoother turns those notes into 68–83 while +Holding Play picks set B: the intercept turns those notes into 68–83 while Play is down. Bank 1 is kept for animations. python3 scripts/shows/colour_sets.py && python3 -m showfile write shows/test.show.json -o shows/test.qxw The sliders are Playback sliders over five scenes per set (Dim, R, G, B, W), so they never override a channel. Knobs 1–5 move colour 1's sliders and knobs -6–10 colour 2's. Run the smoother with --lock-knobs to make them work only +6–10 colour 2's. Run the intercept with --lock-knobs to make them work only after the Custom pad is pressed; any other pad sends them back to zero (see -pad_layer.py). +padeditor/layer.py). """ from __future__ import annotations @@ -26,12 +26,12 @@ SPEC = ROOT / "shows" / "test.show.json" FADE_MS = 1000 DIMMER, RED, GREEN, BLUE, WHITE = 0, 2, 3, 4, 5 PAD_BANK_2_FIRST_NOTE = 52 -HELD_FIRST_NOTE = 68 # scripts/pad/pad_layer.py sends bank 2 here while Play is held +HELD_FIRST_NOTE = 68 # padeditor/layer.py sends bank 2 here while Play is held PROFILE = "M-VAVE SMC-PAD" OMNI = 16 # QLC+ MIDI plugin channel that accepts every MIDI channel PAD_MIDI_CH = 9 # the pads send notes on channel 10 KNOB_FIRST_CC = 30 # pad knob 1; colour 2's sliders follow colour 1's -PAD_PORT = "SMC-PAD Smooth" # scripts/pad/smooth_smc_pad.py; it applies the Play layer +PAD_PORT = "SMC-PAD Smooth" # padeditor/intercept.py; it applies the Play layer # name, r, g, b, w. Bottom row first, four to a row. The cans have a white emitter, so the whites use it. COLOURS = ( diff --git a/tests/test_pad_layer.py b/tests/test_pad_layer.py index 238f576..f89f592 100644 --- a/tests/test_pad_layer.py +++ b/tests/test_pad_layer.py @@ -2,9 +2,9 @@ import sys import unittest from pathlib import Path -sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "scripts" / "pad")) +sys.path.insert(0, str(Path(__file__).resolve().parents[1])) -from pad_layer import PlayLayer # noqa: E402 +from padeditor.layer import PlayLayer # noqa: E402 PLAY_DOWN = [0xB0, 27, 127] PLAY_UP = [0xB0, 27, 0]