Add a virtual pad and an end-to-end test against the simulator, and run tests with pytest.
padcontroller.virtualpad presses pads and sets knobs through padcontroller-in, refusing unless QLC+ last opened shows/test.qxw. tests/test_e2e_pad.py uses it to check can and mover colours in the simulator's /api/state (PAD_E2E=1). pytest is a dev package now, so the plain test functions unittest skipped run too. test_small_eye.py is a rig tester, not a test, so pytest leaves it alone, and the #14 direction assertion now matches the stage map (-179). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
d522b1a2cf
commit
18f0ac2dc5
7 files changed
+363
-3
No files matched your search
@@ -0,0 +1,154 @@
|
||||
"""Virtual pad: press SMC-PAD pads and turn knobs from the command line or a test.
|
||||
|
||||
Sends into the pad controller's input port (padcontroller-in), so presses go
|
||||
through the same Play layer and CC smoothing as the real pad before QLC+ sees
|
||||
them. The real pad stays connected; both feed the same port.
|
||||
|
||||
Before sending it checks that the workspace QLC+ opened last is shows/test.qxw,
|
||||
which only sends Art-Net to 127.0.0.1 (the simulator). Pass --force to skip that.
|
||||
|
||||
Usage:
|
||||
.venv/bin/python -m padcontroller.virtualpad press 52 # note 52 on channel 10
|
||||
.venv/bin/python -m padcontroller.virtualpad press --pad 17 # pad number from shows/midi_map.json
|
||||
.venv/bin/python -m padcontroller.virtualpad press 52 --play # with Play held (52 -> 68)
|
||||
.venv/bin/python -m padcontroller.virtualpad cc 34 64 # knob CC 34 to 64 on channel 1
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import configparser
|
||||
import json
|
||||
import time
|
||||
from contextlib import contextmanager
|
||||
from pathlib import Path
|
||||
from typing import Iterator
|
||||
|
||||
import rtmidi
|
||||
|
||||
from padcontroller.layer import PLAY_CC
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
MIDI_MAP = ROOT / "shows" / "midi_map.json"
|
||||
TEST_SHOW = ROOT / "shows" / "test.qxw"
|
||||
QLC_CONF = Path.home() / ".config" / "qlcplus" / "Q Light Controller Plus.conf"
|
||||
TARGET_CLIENT = "padcontroller-in"
|
||||
# The SMC-PAD sends pads on MIDI channel 10 and knobs on channel 1 (0-based here).
|
||||
PAD_CHANNEL = 9
|
||||
KNOB_CHANNEL = 0
|
||||
|
||||
|
||||
def last_workspace(conf: Path = QLC_CONF) -> Path | None:
|
||||
"""The workspace QLC+ opened most recently, from its recent-files list."""
|
||||
parser = configparser.ConfigParser(interpolation=None, strict=False)
|
||||
try:
|
||||
parser.read(conf)
|
||||
recent = parser.get("workspace", "recent0")
|
||||
except (configparser.Error, OSError):
|
||||
return None
|
||||
path = Path(recent)
|
||||
return path if path.is_absolute() else Path.home() / path
|
||||
|
||||
|
||||
def check_test_show(conf: Path = QLC_CONF) -> None:
|
||||
"""Refuse to drive QLC+ unless its last workspace is the 127.0.0.1-only test show."""
|
||||
ws = last_workspace(conf)
|
||||
if ws is None or ws.resolve() != TEST_SHOW.resolve():
|
||||
raise SystemExit(
|
||||
f"QLC+'s last workspace is {ws or '(unknown)'}, not {TEST_SHOW.relative_to(ROOT)}. "
|
||||
"Open the test show in QLC+ (or pass --force) so presses can't reach the real rig."
|
||||
)
|
||||
|
||||
|
||||
def pad_note(pad: int, midi_map: Path = MIDI_MAP) -> tuple[int, int]:
|
||||
"""(channel, note) for a pad number in shows/midi_map.json."""
|
||||
for entry in json.loads(midi_map.read_text())["pads"]:
|
||||
if entry["pad"] == pad:
|
||||
return entry["channel"], entry["note"]
|
||||
raise SystemExit(f"Pad {pad} is not in {midi_map.relative_to(ROOT)}")
|
||||
|
||||
|
||||
class VirtualPad:
|
||||
"""An ALSA MIDI output linked to padcontroller-in."""
|
||||
|
||||
def __init__(self, target: str = TARGET_CLIENT) -> None:
|
||||
self.out = rtmidi.MidiOut(rtapi=rtmidi.API_LINUX_ALSA, name="virtualpad")
|
||||
ports = self.out.get_ports()
|
||||
i = next((i for i, name in enumerate(ports) if name.startswith(target + ":")), None)
|
||||
if i is None:
|
||||
raise RuntimeError(
|
||||
f"No {target} MIDI port; is the padcontroller service running? Ports:\n "
|
||||
+ "\n ".join(ports or ["(none)"])
|
||||
)
|
||||
self.out.open_port(i, "virtualpad")
|
||||
|
||||
def close(self) -> None:
|
||||
self.out.close_port()
|
||||
|
||||
def __enter__(self) -> VirtualPad:
|
||||
return self
|
||||
|
||||
def __exit__(self, *exc: object) -> None:
|
||||
self.close()
|
||||
|
||||
def send(self, data: list[int]) -> None:
|
||||
self.out.send_message(data)
|
||||
|
||||
def press(self, note: int, channel: int = PAD_CHANNEL, velocity: int = 127, hold: float = 0.05) -> None:
|
||||
"""Note on, wait hold seconds, note off: one tap of a pad."""
|
||||
self.send([0x90 | channel, note, velocity])
|
||||
time.sleep(hold)
|
||||
self.send([0x80 | channel, note, 0])
|
||||
|
||||
def cc(self, cc: int, value: int, channel: int = KNOB_CHANNEL) -> None:
|
||||
self.send([0xB0 | channel, cc, max(0, min(127, value))])
|
||||
|
||||
@contextmanager
|
||||
def play(self) -> Iterator[None]:
|
||||
"""Hold Play for the block, so bank 2 pads go out a layer up."""
|
||||
self.cc(PLAY_CC, 127)
|
||||
try:
|
||||
yield
|
||||
finally:
|
||||
self.cc(PLAY_CC, 0)
|
||||
|
||||
|
||||
def main() -> None:
|
||||
ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
|
||||
ap.add_argument("--force", action="store_true", help="skip the check that QLC+ has the test show open")
|
||||
sub = ap.add_subparsers(dest="cmd", required=True)
|
||||
p = sub.add_parser("press", help="tap a pad")
|
||||
p.add_argument("note", type=int, nargs="?", help="MIDI note (channel 10 unless --channel)")
|
||||
p.add_argument("--pad", type=int, help="pad number from shows/midi_map.json instead of a note")
|
||||
p.add_argument("--channel", type=int, default=PAD_CHANNEL + 1, help="MIDI channel 1-16 (default 10)")
|
||||
p.add_argument("--hold", type=float, default=0.05, help="seconds between note on and off")
|
||||
p.add_argument("--play", action="store_true", help="hold Play while pressing")
|
||||
c = sub.add_parser("cc", help="set a knob")
|
||||
c.add_argument("cc", type=int)
|
||||
c.add_argument("value", type=int)
|
||||
c.add_argument("--channel", type=int, default=KNOB_CHANNEL + 1, help="MIDI channel 1-16 (default 1)")
|
||||
args = ap.parse_args()
|
||||
|
||||
if not args.force:
|
||||
check_test_show()
|
||||
with VirtualPad() as pad:
|
||||
if args.cmd == "cc":
|
||||
pad.cc(args.cc, args.value, args.channel - 1)
|
||||
print(f"CC {args.cc} = {args.value} (channel {args.channel})")
|
||||
return
|
||||
if args.pad is not None:
|
||||
channel, note = pad_note(args.pad)
|
||||
elif args.note is not None:
|
||||
channel, note = args.channel - 1, args.note
|
||||
else:
|
||||
raise SystemExit("press needs a note or --pad")
|
||||
if args.play:
|
||||
with pad.play():
|
||||
pad.press(note, channel, hold=args.hold)
|
||||
else:
|
||||
pad.press(note, channel, hold=args.hold)
|
||||
print(f"Pressed note {note} (channel {channel + 1}){' with Play held' if args.play else ''}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in new issue
Block a user