Files
qlcplus/simulator/__main__.py
T

173 lines
6.3 KiB
Python

"""Stage simulator.
Loads a stage map, patches DMX addresses from a QLC+ show, and listens for
Art-Net. Open the printed URL and point QLC+ Art-Net output at this machine.
python -m simulator
python -m simulator --stage stage/map.json --show shows/templeoftechno.qxw
python -m simulator --demo
"""
from __future__ import annotations
import argparse
import math
import threading
import time
from pathlib import Path
from simulator.artnet import ArtNetNode
from simulator.server import Simulator, make_server
from simulator.stage import Fixture, apply_show, load_stage
ROOT = Path(__file__).resolve().parents[1]
def _hsv(hue: float) -> tuple[int, int, int]:
hue = hue % 1.0
i = int(hue * 6)
f = hue * 6 - i
q = int(255 * (1 - f))
t = int(255 * f)
segment = i % 6
if segment == 0:
return 255, t, 0
if segment == 1:
return q, 255, 0
if segment == 2:
return 0, 255, t
if segment == 3:
return 0, q, 255
if segment == 4:
return t, 0, 255
return 255, 0, q
def _slot(fixture: Fixture, name: str) -> int | None:
if fixture.address is None:
return None
for index, channel in enumerate(fixture.channel_names):
if channel.lower() == name.lower():
return fixture.address + index
return None
def _put(frame: bytearray, fixture: Fixture, name: str, value: int) -> None:
slot = _slot(fixture, name)
if slot is not None and 0 <= slot < len(frame):
frame[slot] = max(0, min(255, value))
def _demo_frame(fixtures: list[Fixture], elapsed: float) -> bytearray:
frame = bytearray(512)
cans = [fixture for fixture in fixtures if fixture.kind == "can" and fixture.address is not None]
cans.sort(key=lambda fixture: fixture.label)
# Walk the BCC145 macro: RGBW mix, colour jump, pulse, then gradient.
look = int(elapsed / 5) % 4
macro = (0, 120, 170, 220)[look]
speed = int(40 + (math.sin(elapsed * 0.7) * 0.5 + 0.5) * 180)
for index, fixture in enumerate(cans):
_put(frame, fixture, "Dimmer", 255)
_put(frame, fixture, "Macro", macro)
_put(frame, fixture, "Speed", speed)
if macro == 0:
red, green, blue = _hsv(elapsed * 0.05 + index / max(1, len(cans)))
_put(frame, fixture, "Red", red)
_put(frame, fixture, "Green", green)
_put(frame, fixture, "Blue", blue)
movers = [fixture for fixture in fixtures if fixture.kind == "mover" and fixture.address is not None]
for index, fixture in enumerate(movers):
pan = int(32768 + math.sin(elapsed * 0.6 + index * math.pi) * 9000)
pan = max(0, min(65535, pan))
tilt = 42000
_put(frame, fixture, "Pan", (pan >> 8) & 0xFF)
_put(frame, fixture, "Pan Fine", pan & 0xFF)
_put(frame, fixture, "Tilt", (tilt >> 8) & 0xFF)
_put(frame, fixture, "Tilt Fine", tilt & 0xFF)
_put(frame, fixture, "Zoom", 40)
_put(frame, fixture, "Center Brightness", 255)
color = (255, 40, 0) if index % 2 == 0 else (40, 80, 255)
for pixel in range(1, 8):
_put(frame, fixture, f"Red {pixel}", color[0])
_put(frame, fixture, f"Green {pixel}", color[1])
_put(frame, fixture, f"Blue {pixel}", color[2])
_put(frame, fixture, "Dynamic Effect", 30 if index % 2 == 0 else 160)
_put(frame, fixture, "Lens Rotate", 210 if index % 2 == 0 else 40)
return frame
def _demo_loop(node: ArtNetNode, fixtures: list[Fixture], stop: threading.Event) -> None:
started = time.monotonic()
while not stop.is_set():
frame = _demo_frame(fixtures, time.monotonic() - started)
node.write(0, frame)
stop.wait(0.05)
def _parse_host_port(value: str, default_port: int) -> tuple[str, int]:
if ":" not in value:
return value, default_port
host, port = value.rsplit(":", 1)
return host or "0.0.0.0", int(port)
def main() -> None:
parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument("--stage", type=Path, default=ROOT / "stage" / "map.json")
parser.add_argument("--show", type=Path, default=ROOT / "shows" / "templeoftechno.qxw")
parser.add_argument("--no-show", action="store_true", help="Use addresses stored in the stage file only")
parser.add_argument("--http", default="0.0.0.0:8765", help="Web view host:port")
parser.add_argument("--artnet", default="0.0.0.0:6454", help="Art-Net listen host:port")
parser.add_argument("--demo", action="store_true", help="Play a local look when you want to see the rig without QLC+")
args = parser.parse_args()
stage = load_stage(args.stage)
if not args.no_show:
if not args.show.is_file():
raise SystemExit(f"Show not found: {args.show}")
apply_show(stage, args.show)
http_host, http_port = _parse_host_port(args.http, 8765)
artnet_host, artnet_port = _parse_host_port(args.artnet, 6454)
node = ArtNetNode(artnet_host, artnet_port)
node.start()
server = make_server(Simulator(stage, node), http_host, http_port)
print("Stage simulator", flush=True)
print(f" stage: {args.stage}", flush=True)
if stage.show:
print(f" show: {args.show}", flush=True)
print(f" Art-Net {artnet_host}:{artnet_port}", flush=True)
print(f" view: http://127.0.0.1:{server.server_address[1]}", flush=True)
if stage.output_ip:
print(f" QLC+ Art-Net output in this show is {stage.output_ip}.", flush=True)
print(" Point that output at this computer (universe 0), or broadcast.", flush=True)
for fixture in stage.fixtures:
if fixture.dmx is None:
print(f" {fixture.label:<4} no address", flush=True)
else:
print(
f" {fixture.label:<4} {fixture.name:<16} universe {fixture.universe} DMX {fixture.dmx}",
flush=True,
)
for warning in stage.warnings:
print(f" warning: {warning}", flush=True)
stop = threading.Event()
if args.demo:
threading.Thread(target=_demo_loop, args=(node, stage.fixtures, stop), daemon=True).start()
print(" demo look is running", flush=True)
try:
server.serve_forever()
except KeyboardInterrupt:
print("\nStopping.", flush=True)
finally:
stop.set()
server.shutdown()
node.close()
if __name__ == "__main__":
main()