The rig sim receives Art-Net (UDP 6454, answers ArtPoll) and DDP (each port the room's DDP strips use) in sim_net.c and passes changed universes and pixels to the page over /sim/ws, now with a kind byte in front of each binary frame. Rooms gain fixture types: ddp strips, artnet pixel strips, and artnet cans and movers that read named channels from the room's dmx_definitions (the qlcplus stage map's format). Overlapping DMX channels are refused. Cans and movers are decoded and drawn as in the qlcplus stage simulator (dmx.js, dmx-draw.js). In Edit mode: pick a type to add, edit port/offset/universe/address/colour order/definition, see a can's or mover's live channels, make a controller output aimed at the sim and send its test. The room dialog lists DMX definitions, with presets, a channel editor, and .qxf and stage map import. GET /sim/api/dmx shows what the receiver has had. Tests: test/api/test_sim_net.py and test/browser/dmx_decode.test.mjs. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
493 lines
17 KiB
JavaScript
493 lines
17 KiB
JavaScript
// DMX fixtures for the simulated rig: a frame's channels to colour, beam and strobe.
|
||
// Ported from the qlcplus stage simulator (technicalkiwi/qlcplus simulator/decode.py), same rules and constants.
|
||
// Channel order comes from the room's DMX definitions: names such as Dimmer, Red, Pan and Zoom select the slots.
|
||
|
||
// Loaded as a classic script before app.js: everything is on RigDmx.
|
||
(function (g) {
|
||
'use strict';
|
||
|
||
const PIXEL = /^(red|green|blue|white) (\d+)$/i;
|
||
|
||
const PAN_DEGREES = 540.0;
|
||
// Tilt 127 is straight down. 0 leans along the pan heading, 255 away from it.
|
||
const TILT_DEGREES = 270.0;
|
||
// Plan reach of a beam at or above horizontal.
|
||
const BEAM_MAX_M = 4.7;
|
||
const ZOOM_MIN = 5.0;
|
||
const ZOOM_MAX = 45.0;
|
||
|
||
// BCC145 8ch: strobe 0–7 is open. Small Eye centre strobe 0–3 is open.
|
||
const CAN_STROBE_OPEN = 7;
|
||
const MOVER_STROBE_OPEN = 3;
|
||
|
||
// beamZ BCC145 8ch macro: 0–50 RGBW mix, 51–100 fixed colour, 101–150 jump, 151–200 pulse,
|
||
// 201–250 gradient, 251–255 sound. Speed is slow to fast.
|
||
const PAR_PALETTE = [
|
||
[255, 0, 0], [255, 96, 0], [255, 200, 0], [255, 255, 0], [0, 255, 0],
|
||
[0, 255, 160], [0, 64, 255], [140, 0, 255], [255, 0, 160], [255, 255, 255],
|
||
];
|
||
|
||
// Small Eye 43ch colour macros, static masks, and dynamic chases. 0–9 is no function.
|
||
const MOVER_MACROS = [
|
||
[255, 0, 0], [255, 80, 0], [255, 255, 0], [0, 255, 0], [0, 255, 255],
|
||
[0, 40, 255], [160, 0, 255], [255, 0, 180], [255, 255, 255], [255, 180, 80],
|
||
];
|
||
const STATIC_MASKS = [
|
||
['all', [1, 1, 1, 1, 1, 1, 1]],
|
||
['odds', [1, 0, 1, 0, 1, 0, 1]],
|
||
['evens', [0, 1, 0, 1, 0, 1, 0]],
|
||
['center', [0, 0, 0, 1, 0, 0, 0]],
|
||
['ends', [1, 0, 0, 0, 0, 0, 1]],
|
||
['left', [1, 1, 1, 1, 0, 0, 0]],
|
||
['right', [0, 0, 0, 1, 1, 1, 1]],
|
||
['one', null],
|
||
];
|
||
const DYNAMIC = ['chase', 'reverse', 'rainbow', 'bounce', 'pulse', 'pairs'];
|
||
const DYNAMIC_LABELS = {
|
||
chase: 'chase', reverse: 'reverse chase', rainbow: 'rainbow', bounce: 'bounce', pulse: 'pixel pulse', pairs: 'pairs',
|
||
};
|
||
|
||
const MOVER_43 = [
|
||
'Pan', 'Pan Fine', 'Tilt', 'Tilt Fine', 'Pan/Tilt Speed', 'Lens Rotate', 'Zoom', 'Focus',
|
||
'Center Brightness', 'Center Strobe',
|
||
'Red 1', 'Green 1', 'Blue 1', 'White 1',
|
||
'Red 2', 'Green 2', 'Blue 2', 'White 2',
|
||
'Red 3', 'Green 3', 'Blue 3', 'White 3',
|
||
'Red 4', 'Green 4', 'Blue 4', 'White 4',
|
||
'Red 5', 'Green 5', 'Blue 5', 'White 5',
|
||
'Red 6', 'Green 6', 'Blue 6', 'White 6',
|
||
'Red 7', 'Green 7', 'Blue 7', 'White 7',
|
||
'Colour Temperature', 'Colour Macro', 'Static Effect', 'Dynamic Effect', 'Reset',
|
||
];
|
||
|
||
/** The two fixtures of the qlcplus stage map, as DMX definitions. */
|
||
const PRESET_DEFINITIONS = {
|
||
'beamZ BCC145': {
|
||
manufacturer: 'beamZ', model: 'BCC145', mode: '8 Channel',
|
||
channels: ['Dimmer', 'Strobe', 'Red', 'Green', 'Blue', 'White', 'Macro', 'Speed'],
|
||
},
|
||
'Small Eye 7 Mover': { manufacturer: 'Small Eye 7', model: 'Mover', mode: '43 Channel', channels: MOVER_43.slice() },
|
||
};
|
||
|
||
const clamp = (v, lo, hi) => Math.max(lo, Math.min(hi, v));
|
||
const r2 = (v, d) => Math.round(v * 10 ** d) / 10 ** d + 0; // + 0: no -0
|
||
|
||
function find(names, wanted) {
|
||
const w = wanted.toLowerCase();
|
||
const i = names.findIndex((n) => n.toLowerCase() === w);
|
||
return i < 0 ? null : i;
|
||
}
|
||
|
||
function value(channels, names, wanted, dflt = 0) {
|
||
const i = find(names, wanted);
|
||
return i === null || i >= channels.length ? dflt : channels[i];
|
||
}
|
||
|
||
function mixRgbw(red, green, blue, white, dimmer) {
|
||
const level = clamp(dimmer, 0, 255) / 255;
|
||
return [
|
||
Math.round(Math.min(255, red + white) * level),
|
||
Math.round(Math.min(255, green + white) * level),
|
||
Math.round(Math.min(255, blue + white) * level),
|
||
];
|
||
}
|
||
|
||
function pixelGroups(channels, names, dimmer) {
|
||
const groups = new Map();
|
||
names.forEach((name, i) => {
|
||
const m = PIXEL.exec(name.trim());
|
||
if (!m) {
|
||
return;
|
||
}
|
||
const n = Number(m[2]);
|
||
if (!groups.has(n)) {
|
||
groups.set(n, {});
|
||
}
|
||
groups.get(n)[m[1].toLowerCase()] = i;
|
||
});
|
||
const at = (g, c) => (g[c] === undefined || g[c] >= channels.length ? 0 : channels[g[c]]);
|
||
return [...groups.keys()].sort((a, b) => a - b).map((n) => {
|
||
const g = groups.get(n);
|
||
return mixRgbw(at(g, 'red'), at(g, 'green'), at(g, 'blue'), at(g, 'white'), dimmer);
|
||
});
|
||
}
|
||
|
||
/** count slots from a 0-based address in a 512-byte frame */
|
||
function slotsAt(frame, address, count) {
|
||
const out = [];
|
||
for (let o = 0; o < count; o++) {
|
||
const i = address + o;
|
||
out.push(frame && i >= 0 && i < frame.length ? frame[i] : 0);
|
||
}
|
||
return out;
|
||
}
|
||
|
||
function strobeHz(v, openMax) {
|
||
if (v <= openMax) {
|
||
return 0;
|
||
}
|
||
const span = Math.max(1, 255 - openMax);
|
||
return r2(1 + ((v - openMax) / span) * 18, 2);
|
||
}
|
||
|
||
/** Indexed lens position, 0–60°. Spin ranges are not an index. */
|
||
function lensAngle(v) {
|
||
return v <= 127 ? r2((v / 127) * 60, 1) : 0;
|
||
}
|
||
|
||
/** Degrees per second, negative counter-clockwise: 0–127 index, 128–188 CCW fast to slow, 189–193 stop, 194–255 CW. */
|
||
function lensSpin(v) {
|
||
if (v >= 128 && v <= 188) {
|
||
return r2(-(40 + ((188 - v) / 60) * 320), 1);
|
||
}
|
||
if (v >= 194 && v <= 255) {
|
||
return r2(40 + ((v - 194) / 61) * 320, 1);
|
||
}
|
||
return 0;
|
||
}
|
||
|
||
/** Brightness-weighted mix, so one lit pixel stays its own colour. */
|
||
function beamColour(pixels) {
|
||
let red = 0, green = 0, blue = 0, weight = 0;
|
||
for (const [pr, pg, pb] of pixels) {
|
||
const w = pr + pg + pb;
|
||
red += pr * w;
|
||
green += pg * w;
|
||
blue += pb * w;
|
||
weight += w;
|
||
}
|
||
return weight <= 0 ? [0, 0, 0] : [Math.round(red / weight), Math.round(green / weight), Math.round(blue / weight)];
|
||
}
|
||
|
||
function scale(colour, dimmer) {
|
||
const level = clamp(dimmer, 0, 255) / 255;
|
||
return colour.map((c) => Math.round(c * level));
|
||
}
|
||
|
||
function speedHz(speed, slow = 0.35, fast = 8.0) {
|
||
return r2(slow + (clamp(speed, 0, 255) / 255) * (fast - slow), 2);
|
||
}
|
||
|
||
function canMode(macro) {
|
||
if (macro <= 50) return 'mix';
|
||
if (macro <= 100) return 'fixed';
|
||
if (macro <= 150) return 'jump';
|
||
if (macro <= 200) return 'pulse';
|
||
if (macro <= 250) return 'gradient';
|
||
return 'sound';
|
||
}
|
||
|
||
/** Map value onto count bands from start through 255: [index, 0–1 position], or null. */
|
||
function rangePick(v, start, count) {
|
||
if (v < start || count <= 0) {
|
||
return null;
|
||
}
|
||
const span = 256 - start;
|
||
const index = Math.min(count - 1, Math.floor(((v - start) * count) / span));
|
||
const band = span / count;
|
||
const frac = (v - start - index * band) / band;
|
||
return [index, clamp(frac, 0, 0.999)];
|
||
}
|
||
|
||
/** 0 leaves the colour alone. Higher values warm it, the way a CTO does. */
|
||
function applyCto(red, green, blue, amount) {
|
||
if (amount <= 0) {
|
||
return [red, green, blue];
|
||
}
|
||
const warm = clamp(amount, 0, 255) / 255;
|
||
return [red, Math.round(green * (1 - 0.35 * warm)), Math.round(blue * (1 - 0.75 * warm))];
|
||
}
|
||
|
||
const CAN_LABELS = {
|
||
mix: 'RGBW', fixed: 'fixed colour', jump: 'colour jump', pulse: 'colour pulse', gradient: 'colour gradient',
|
||
};
|
||
|
||
function canView(dimmer, strobe, red, green, blue, white, macro, speed, channels) {
|
||
const mode = canMode(macro);
|
||
const mixed = mixRgbw(red, green, blue, white, dimmer);
|
||
let peak = mixRgbw(red, green, blue, white, 255);
|
||
if (peak.every((c) => c === 0)) {
|
||
peak = [255, 255, 255];
|
||
}
|
||
let shown = mixed;
|
||
let palette = null;
|
||
let sound = 0;
|
||
let hz = 0;
|
||
if (mode === 'fixed') {
|
||
const index = Math.min(PAR_PALETTE.length - 1, Math.floor((Math.max(0, macro - 51) * PAR_PALETTE.length) / 50));
|
||
shown = scale(PAR_PALETTE[index], dimmer);
|
||
} else if (mode === 'pulse') {
|
||
shown = scale(peak, dimmer);
|
||
hz = speedHz(speed, 0.4, 6.0);
|
||
} else if (mode === 'jump' || mode === 'gradient' || mode === 'sound') {
|
||
palette = PAR_PALETTE.map((c) => c.slice());
|
||
hz = speedHz(speed);
|
||
shown = scale(PAR_PALETTE[0], dimmer);
|
||
if (mode === 'sound') {
|
||
sound = macro >= 254 ? 2 : 1;
|
||
}
|
||
}
|
||
const view = {
|
||
r: shown[0], g: shown[1], b: shown[2],
|
||
dimmer,
|
||
strobe_hz: strobeHz(strobe, CAN_STROBE_OPEN),
|
||
strobe_mode: strobe > CAN_STROBE_OPEN ? 'strobe' : 'open',
|
||
effect: mode !== 'mix',
|
||
function: mode,
|
||
function_label: mode === 'sound' ? (sound === 2 ? 'sound 2' : 'sound 1') : CAN_LABELS[mode],
|
||
speed_hz: hz,
|
||
sound,
|
||
channels,
|
||
};
|
||
if (palette) {
|
||
view.palette = palette;
|
||
}
|
||
return view;
|
||
}
|
||
|
||
function decodeCan(channels) {
|
||
const at = (i) => (i < channels.length ? channels[i] : 0);
|
||
return canView(at(0), at(1), at(2), at(3), at(4), at(5), at(6), at(7), channels);
|
||
}
|
||
|
||
function decodeCanNamed(channels, names) {
|
||
const dimmer = find(names, 'Dimmer') === null ? 255 : value(channels, names, 'Dimmer');
|
||
return canView(
|
||
dimmer,
|
||
value(channels, names, 'Strobe'),
|
||
value(channels, names, 'Red'),
|
||
value(channels, names, 'Green'),
|
||
value(channels, names, 'Blue'),
|
||
value(channels, names, 'White'),
|
||
value(channels, names, 'Macro'),
|
||
value(channels, names, 'Speed'),
|
||
channels,
|
||
);
|
||
}
|
||
|
||
/** Centre strobe: open, linear strobe, then three random-strobe bands. */
|
||
function moverStrobe(v) {
|
||
if (v <= MOVER_STROBE_OPEN) return [0, 'open'];
|
||
if (v <= 200) return [strobeHz(v, MOVER_STROBE_OPEN), 'strobe'];
|
||
if (v <= 215) return [6, 'random'];
|
||
if (v <= 234) return [12, 'random'];
|
||
return [18, 'random'];
|
||
}
|
||
|
||
function decodeMoverNamed(channels, names) {
|
||
const v = (n) => value(channels, names, n);
|
||
const pan = (v('Pan') << 8) | v('Pan Fine');
|
||
const tilt = (v('Tilt') << 8) | v('Tilt Fine');
|
||
const zoom = v('Zoom');
|
||
const rotate = v('Lens Rotate');
|
||
const dimmer = find(names, 'Center Brightness') === null ? 255 : v('Center Brightness');
|
||
const strobe = v('Center Strobe');
|
||
const pixels = pixelGroups(channels, names, dimmer);
|
||
const cto = v('Colour Temperature');
|
||
const macro = rangePick(v('Colour Macro'), 10, MOVER_MACROS.length);
|
||
const stat = rangePick(v('Static Effect'), 10, STATIC_MASKS.length);
|
||
const dynamic = rangePick(v('Dynamic Effect'), 10, DYNAMIC.length);
|
||
|
||
let source = pixels.map((p) => p.slice());
|
||
let fn = 'pixels';
|
||
let label = 'pixels';
|
||
let dynamicName = null;
|
||
let speed = 0;
|
||
if (macro) {
|
||
const colour = scale(MOVER_MACROS[macro[0]], dimmer);
|
||
source = Array.from({ length: 7 }, () => colour.slice());
|
||
fn = 'macro';
|
||
label = 'colour macro';
|
||
}
|
||
if ((stat || dynamic) && !source.some((p) => p[0] + p[1] + p[2] > 0)) {
|
||
const white = scale([255, 255, 255], dimmer);
|
||
source = Array.from({ length: 7 }, () => white.slice());
|
||
}
|
||
if (stat && !dynamic) {
|
||
let [name, mask] = STATIC_MASKS[stat[0]];
|
||
if (name === 'one') {
|
||
const pixel = Math.min(6, Math.floor(stat[1] * 7));
|
||
mask = Array.from({ length: 7 }, (_, i) => (i === pixel ? 1 : 0));
|
||
}
|
||
source = source.map((p, i) => (mask[i] ? p : [0, 0, 0]));
|
||
fn = 'static';
|
||
label = name === 'one' ? 'one pixel' : `static ${name}`;
|
||
}
|
||
if (dynamic) {
|
||
dynamicName = DYNAMIC[dynamic[0]];
|
||
speed = r2(0.4 + dynamic[1] * 6, 2);
|
||
fn = 'dynamic';
|
||
label = DYNAMIC_LABELS[dynamicName];
|
||
}
|
||
const tinted = source.map((p) => applyCto(p[0], p[1], p[2], cto));
|
||
const [red, green, blue] = beamColour(tinted);
|
||
const [rate, strobeMode] = moverStrobe(strobe);
|
||
const reset = v('Reset');
|
||
const panSpeed = v('Pan/Tilt Speed');
|
||
const panDeg = ((pan - 32768) / 65536) * PAN_DEGREES;
|
||
const zoomDeg = ZOOM_MIN + (zoom / 255) * (ZOOM_MAX - ZOOM_MIN);
|
||
const tiltDeg = ((tilt - 32768) / 65536) * TILT_DEGREES;
|
||
// Reach along the pan heading on the plan. Negative points behind it.
|
||
const lean = clamp(-tiltDeg, -90, 90);
|
||
const beamM = BEAM_MAX_M * Math.sin((lean * Math.PI) / 180);
|
||
return {
|
||
r: red, g: green, b: blue,
|
||
dimmer,
|
||
strobe_hz: rate,
|
||
strobe_mode: strobeMode,
|
||
effect: fn !== 'pixels',
|
||
function: fn,
|
||
function_label: label,
|
||
speed_hz: speed,
|
||
dynamic: dynamicName,
|
||
pan_deg: r2(panDeg, 2),
|
||
tilt_deg: r2(tiltDeg, 2),
|
||
zoom_deg: r2(zoomDeg, 2),
|
||
beam_m: r2(beamM, 3),
|
||
pixels: tinted,
|
||
spin_dps: lensSpin(rotate),
|
||
lens_deg: lensAngle(rotate),
|
||
focus: r2(v('Focus') / 255, 3),
|
||
move: r2(1 - panSpeed / 255, 3),
|
||
cto,
|
||
reset: reset >= 200 && reset <= 205,
|
||
channels,
|
||
};
|
||
}
|
||
|
||
function decodeMover(channels) {
|
||
return decodeMoverNamed(channels, MOVER_43);
|
||
}
|
||
|
||
/** Read a fixture using the channel names of its DMX definition. */
|
||
function decodeNamed(channels, names, { kind } = {}) {
|
||
if (kind === 'mover' || (find(names, 'Pan') !== null && find(names, 'Tilt') !== null)) {
|
||
return decodeMoverNamed(channels, names);
|
||
}
|
||
return decodeCanNamed(channels, names);
|
||
}
|
||
|
||
/**
|
||
* The live view of a can or mover.
|
||
* fixture: {kind, universe, address (DMX 1–512), channel_names, pan_reverse}; universes: Map<number, Uint8Array(512)>
|
||
*/
|
||
function viewFixture(fixture, universes) {
|
||
if (!fixture.address || fixture.universe === undefined || fixture.universe === null) {
|
||
return { patched: false, r: 0, g: 0, b: 0, dimmer: 0, strobe_hz: 0, channels: [] };
|
||
}
|
||
const names = fixture.channel_names || [];
|
||
const count = names.length || (fixture.kind === 'mover' ? 43 : 8);
|
||
const frame = universes.get(fixture.universe);
|
||
const channels = slotsAt(frame, fixture.address - 1, count);
|
||
let view;
|
||
if (names.length) {
|
||
view = decodeNamed(channels, names, { kind: fixture.kind });
|
||
} else if (fixture.kind === 'mover') {
|
||
view = decodeMover(channels);
|
||
} else {
|
||
view = decodeCan(channels);
|
||
}
|
||
if (fixture.pan_reverse && 'pan_deg' in view) {
|
||
view.pan_deg = -view.pan_deg;
|
||
}
|
||
view.patched = true;
|
||
return view;
|
||
}
|
||
|
||
/**
|
||
* The RGB bytes of a pixel strip fed by Art-Net: the first universe from the address, then 170 pixels a
|
||
* universe from channel 1 (the way the controller's Art-Net output sends them). Same rule as dmx_runs() in sim_rig.c.
|
||
*/
|
||
function artnetPixels(universes, universe, address, leds) {
|
||
const out = new Uint8Array(leds * 3);
|
||
let px = 0;
|
||
let u = universe;
|
||
let a0 = address - 1;
|
||
while (px < leds) {
|
||
const frame = universes.get(u);
|
||
const fit = Math.floor((512 - a0) / 3);
|
||
const take = Math.min(fit, leds - px);
|
||
if (frame) {
|
||
out.set(frame.subarray(a0, a0 + take * 3), px * 3);
|
||
}
|
||
px += take;
|
||
u += 1;
|
||
a0 = 0;
|
||
if (u - universe > 32) {
|
||
break;
|
||
}
|
||
}
|
||
return out;
|
||
}
|
||
|
||
/** The DMX channels [universe, start0, end0) an Art-Net fixture uses (for the editor's next free address). */
|
||
function dmxRuns(fixture, room, leds) {
|
||
const u = fixture.universe ?? 0;
|
||
const a0 = (fixture.address ?? 1) - 1;
|
||
if ((fixture.kind ?? 'pixels') !== 'pixels') {
|
||
const d = (room.dmx_definitions || {})[fixture.dmx_def];
|
||
return [[u, a0, a0 + (d ? d.channels.length : 0)]];
|
||
}
|
||
const runs = [];
|
||
let left = leds;
|
||
let n = 0;
|
||
while (left > 0 && n < 32) {
|
||
const take = Math.min(n === 0 ? Math.floor((512 - a0) / 3) : 170, left);
|
||
const start = n === 0 ? a0 : 0;
|
||
runs.push([u + n, start, start + take * 3]);
|
||
left -= take;
|
||
n++;
|
||
}
|
||
return runs;
|
||
}
|
||
|
||
/** Channel names of a QLC+ fixture definition (.qxf) for each mode: [{manufacturer, model, mode, channels}] */
|
||
function parseQxf(text) {
|
||
const doc = new DOMParser().parseFromString(text, 'application/xml');
|
||
if (doc.querySelector('parsererror')) {
|
||
throw new Error('not an XML file');
|
||
}
|
||
const tag = (el, name) => [...el.children].filter((c) => c.localName === name);
|
||
const root = doc.documentElement;
|
||
const text1 = (name) => (tag(root, name)[0]?.textContent || '').trim();
|
||
const manufacturer = text1('Manufacturer');
|
||
const model = text1('Model');
|
||
const modes = tag(root, 'Mode');
|
||
if (!modes.length) {
|
||
throw new Error('no <Mode> in this fixture definition');
|
||
}
|
||
return modes.map((mode) => {
|
||
const chans = tag(mode, 'Channel')
|
||
.sort((a, b) => Number(a.getAttribute('Number')) - Number(b.getAttribute('Number')))
|
||
.map((c) => c.textContent.trim());
|
||
return { manufacturer, model, mode: mode.getAttribute('Name') || '', channels: chans };
|
||
});
|
||
}
|
||
|
||
g.RigDmx = {
|
||
PAN_DEGREES,
|
||
TILT_DEGREES,
|
||
BEAM_MAX_M,
|
||
ZOOM_MIN,
|
||
ZOOM_MAX,
|
||
CAN_STROBE_OPEN,
|
||
MOVER_STROBE_OPEN,
|
||
PAR_PALETTE,
|
||
MOVER_43,
|
||
PRESET_DEFINITIONS,
|
||
mixRgbw,
|
||
slotsAt,
|
||
strobeHz,
|
||
lensAngle,
|
||
lensSpin,
|
||
applyCto,
|
||
decodeCan,
|
||
decodeMover,
|
||
decodeNamed,
|
||
viewFixture,
|
||
artnetPixels,
|
||
dmxRuns,
|
||
parseQxf,
|
||
};
|
||
})(typeof self !== 'undefined' ? self : globalThis);
|