"use strict"; // Dashboard: an overview page built from modules, stored by the server as `dashboard` in config.json. // Every module only looks: nothing here changes a DSP. Browsing (channel, EQ band) stays in this session. const DASH_ROW = 40; // grid row + gap in px, see .dash const DASH_DEFAULT = () => [ { type: "status", w: 4 }, { type: "meters", w: 8 }, { type: "curve", w: 8 }, { type: "mixer", w: 4 }, ].map((m, i) => ({ id: `m${i}`, cfg: {}, ...m })); let dashEdit = false, dashTimer = 0, dashSaveTimer = 0; const dashBrowse = new Map(); // module id -> { kind, idx, band } const dashMods = () => S.dash || (S.dash = DASH_DEFAULT()); const dashDevs = () => S.order.map(id => S.devices[id]).filter(d => d && (!d.demo || S.demo.enabled)); // the chosen DSP, or the first one if it is gone (e.g. while demo mode is on) const dashDev = m => S.devices[m.cfg.device] && dashDevs().includes(S.devices[m.cfg.device]) ? S.devices[m.cfg.device] : dashDevs()[0]; const offline = d => h("p", { class: "info dempty" }, d ? t("{name} is not connected.", { name: d.name }) : t("No DSP added yet.")); function dashSave() { clearTimeout(dashSaveTimer); dashSaveTimer = setTimeout(() => api("PUT", "/api/dashboard", { modules: dashMods() }).catch(e => toast(t("Error: ") + trMsg(e.message))), 300); } // parameter changes redraw the page, but not for every single step of a fader function dashSoon() { clearTimeout(dashTimer); dashTimer = setTimeout(() => { if (S.view === "dash" && !S.settings) render(); }, 250); } function dashRemote(data) { S.dash = data && data.modules ? data.modules : null; if (S.view === "dash" && !S.settings && !S.busy) render(); } // ------------------------------------------------------------------ modules const dashKv = (k, v, cls = "") => h("div", { class: "dkv " + cls }, h("span", {}, k), h("b", {}, v)); const dashChannels = d => [...chans(d, "in").map((_, i) => ["in", i]), ...chans(d, "out").map((_, i) => ["out", i])]; // a channel of a module, browsed with the arrows function dashSel(m, d) { const all = dashChannels(d); let s = dashBrowse.get(m.id); if (!s) { s = { kind: m.cfg.kind || "in", idx: m.cfg.idx || 0, band: 0 }; dashBrowse.set(m.id, s); } if (!all.some(([k, i]) => k === s.kind && i === s.idx)) { s.kind = all[0][0]; s.idx = all[0][1]; } return s; } function dashChannelNav(m, d, s) { const all = dashChannels(d), at = all.findIndex(([k, i]) => k === s.kind && i === s.idx); const go = dir => { const [k, i] = all[(at + dir + all.length) % all.length]; s.kind = k; s.idx = i; s.band = 0; render(); }; const ch = chans(d, s.kind)[s.idx]; return h("div", { class: "dnav" }, h("button", { class: "small", "data-view-ok": "", "aria-label": t("Previous channel"), onclick: () => go(-1) }, "‹"), h("span", { class: "dnavname" }, h("b", {}, chLabel(s.kind, s.idx)), " ", ch.name), h("button", { class: "small", "data-view-ok": "", "aria-label": t("Next channel"), onclick: () => go(1) }, "›")); } const DASH_MODULES = { status: { title: () => t("DSP overview"), w: 4, min: [3, 3], max: [12, 14], h: () => 2 + Math.ceil(dashDevs().length * 1.2), global: true, icon: '', body() { const list = dashDevs(); if (!list.length) return offline(); return h("div", { class: "dlist" }, list.map(d => h("button", { class: "drow dlink", "data-view-ok": "", title: t("Open in the mixer"), onclick: () => { S.cur = d.id; S.view = "mixer"; store.set("dsp.view", "mixer"); render(); window.scrollTo(0, 0); } }, h("span", { class: "dot " + d.status }), h("span", { class: "grow dname" }, h("b", {}, d.name), h("small", {}, [MODEL_LABEL[d.detected_model || d.model] || "", d.state ? d.state.preset_name : (STATUS_TEXT[d.status] || d.status)].filter(Boolean).join(" · "))), d.state && dashChannels(d).some(([k, i]) => chans(d, k)[i].mute) ? h("span", { class: "dtag mute" }, "MUTE") : null))); }, }, meters: { title: () => t("Levels"), w: 8, min: [3, 5], max: [12, 14], h: 7, icon: '', body(m, d) { const col = (k, i) => h("div", { class: "dm" + (chans(d, k)[i].mute ? " muted" : ""), title: chTitle(d, k, i), style: `${ccStyle(d, k, i)};grid-column:${i + 1}` }, ledMeter(d.id, k, i), k === "out" ? limLed(d.id, i) : h("span", { class: "limled ph", "aria-hidden": "true" }), h("small", {}, chLabel(k, i).replace("Out ", "").replace("In ", ""))); const group = (k, label) => h("div", { class: "dmgroup", style: `flex-grow:${chans(d, k).length}` }, h("div", { class: "sub" }, label), h("div", { class: "dms", style: `--n:${chans(d, k).length}` }, linkBands(d, k), chans(d, k).map((_, i) => col(k, i)))); const bands = linkedGroups(d, "in").length + linkedGroups(d, "out").length > 0; return h("div", { class: "dmeters" + (bands ? " hasbands" : "") }, group("in", t("Inputs")), group("out", t("Outputs"))); }, }, mixer: { title: () => t("Faders"), w: 4, min: [3, 4], max: [8, 14], h: d => (d && d.state ? 2 + Math.ceil(dashChannels(d).length * 0.8) : 5), icon: '', body(m, d) { const row = (k, i) => { const ch = chans(d, k)[i]; return h("div", { class: "drow dgain" + (ch.mute ? " muted" : ""), style: ccStyle(d, k, i) }, h("span", { class: "dlab" }, chLabel(k, i)), h("span", { class: "dbar" }, h("i", { style: `width:${ch.gain / 4}%` }), h("u")), h("span", { class: "dval" }, ch.mute ? "MUTE" : `${sign(gainDb(ch.gain))} dB`)); }; return h("div", { class: "dlist" }, chans(d, "in").map((_, i) => row("in", i)), h("hr"), chans(d, "out").map((_, i) => row("out", i))); }, }, curve: { title: () => t("Frequency response"), w: 8, min: [4, 7], max: [12, 16], h: 9, icon: '', body(m, d) { const s = dashSel(m, d), ch = chans(d, s.kind)[s.idx]; if (s.band >= ch.peq.length) s.band = 0; const canvas = h("canvas", { class: "dcurve" }); const shown = (m.cfg.overlays || []).map(key => { const k = key.startsWith("in") ? "in" : "out", i = +key.replace(/\D/g, ""); return i < chans(d, k).length && !(k === s.kind && i === s.idx) ? { k, i, ch: chans(d, k)[i], color: chColor(d, k, i) } : null; }).filter(Boolean); requestAnimationFrame(() => drawCurve(canvas, ch, s.band, { mode: "mag", overlays: shown })); const b = ch.peq[s.band]; return [dashChannelNav(m, d, s), canvas, shown.length ? h("div", { class: "dlegend" }, shown.map(o => h("span", {}, h("i", { style: `background:${o.color}` }), chLabel(o.k, o.i)))) : null, h("div", { class: "dbands" }, ch.peq.map((bb, i) => h("button", { class: "dband" + (i === s.band ? " on" : "") + (bb.bypass ? " off" : "") + (bb.gain !== 120 ? " act" : ""), "data-view-ok": "", onclick: () => { s.band = i; render(); } }, h("b", {}, i + 1), h("small", {}, fmtHzShort(freqHz(bb.freq)))))), h("p", { class: "info dbandinfo" }, `${PEQ_TYPES[b.type]} · ${SPEC.freq.fmt(b.freq)} · ${sign((b.gain - 120) / 10)} dB · Q ${Q_TABLE[b.q]}${b.bypass ? " · Bypass" : ""}`)]; }, }, channel: { title: () => t("Channel"), w: 4, min: [3, 6], max: [8, 12], h: 9, icon: '', body(m, d) { const s = dashSel(m, d), ch = chans(d, s.kind)[s.idx]; const filt = f => (f && f.slope ? `${SLOPES[f.slope]} @ ${SPEC.freq.fmt(f.freq)}` : t("off")); const on = ch.peq.filter(b => !b.bypass && b.gain !== 120).length; return [dashChannelNav(m, d, s), h("div", { class: "dkvs" }, dashKv("Gain", ch.mute ? "MUTE" : SPEC.gain.fmt(ch.gain), ch.mute ? "bad" : ""), dashKv("Delay", SPEC.delay.fmt(ch.delay)), dashKv("Phase", ch.polarity ? "180°" : "0°"), ch.hp ? dashKv(t("High-pass"), filt(ch.hp)) : null, ch.lp ? dashKv(t("Low-pass"), filt(ch.lp)) : null, dashKv("EQ", on ? t("{n} bands active", { n: on }) : t("flat")), s.kind === "out" ? dashKv(t("Sources"), chans(d, "in").map((_, i) => (ch.matrix >> i & 1 ? chLabel("in", i) : "")).filter(Boolean).join(", ") || "–") : null, s.kind === "out" ? dashKv("Limiter", SPEC.thresh.fmt(ch.limit.threshold)) : null, s.kind === "in" ? dashKv("Gate", SPEC.gthresh.fmt(ch.gate.threshold)) : null)]; }, }, limiter: { title: () => "Limiter", w: 4, min: [3, 3], max: [8, 12], h: d => (d && d.state ? 2 + Math.ceil(chans(d, "out").length * 0.8) : 5), icon: '', body(m, d) { return h("div", { class: "dlist" }, chans(d, "out").map((ch, o) => { const c = limCalc(d.id, o, ch.limit.threshold); return h("div", { class: "drow" }, limLed(d.id, o), h("span", { class: "dlab" }, chLabel("out", o)), h("span", { class: "grow dsub" }, c ? c.amp.name : ""), h("b", { class: "dval" }, c ? fmtW(c.p) : SPEC.thresh.fmt(ch.limit.threshold))); })); }, }, routing: { title: () => "Routing", w: 4, min: [3, 3], max: [8, 12], h: d => (d && d.state ? 2 + Math.ceil((chans(d, "out").length + 1) * 0.5) : 5), icon: '', body(m, d) { const ins = chans(d, "in"); return h("div", { class: "dmatrix", style: `--n:${ins.length}` }, h("span"), ins.map((_, i) => h("b", { style: `color:${inColor(d, i)}` }, chLabel("in", i).replace("In ", ""))), chans(d, "out").flatMap((ch, o) => [h("span", {}, chLabel("out", o)), ...ins.map((_, i) => h("i", { class: ch.matrix >> i & 1 ? "on" : "", style: `--c:${inColor(d, i)}`, title: `${chLabel("in", i)} → ${chLabel("out", o)}` }))])); }, }, delays: { title: () => "Delay", w: 4, min: [3, 3], max: [8, 14], h: d => (d && d.state ? 2 + Math.ceil(dashChannels(d).length * 0.8) : 5), icon: '', body(m, d) { const row = (k, i) => { const ch = chans(d, k)[i]; return h("div", { class: "drow" + (ch.delay ? "" : " zero") }, h("span", { class: "dlab" }, chLabel(k, i)), h("span", { class: "grow dsub" }, ch.name), h("b", { class: "dval" }, SPEC.delay.fmt(ch.delay))); }; return h("div", { class: "dlist" }, dashChannels(d).map(([k, i]) => row(k, i))); }, }, fir: { title: () => t("FIR filters"), w: 4, min: [3, 3], max: [8, 12], h: 5, only: () => dashDevs().some(d => d.fir), icon: '', body(m, d) { if (!d.fir) return h("p", { class: "info dempty" }, t("{name} has no FIR filters.", { name: d.name })); const used = firUsed(d); const list = dashChannels(d).filter(([k, i]) => firTapsOf(chans(d, k)[i])); return [h("div", { class: "firmeter" }, h("span", { style: `width:${Math.min(100, used / 40.96)}%` })), h("p", { class: "info" }, t("{used} of 4096 taps in use", { used })), list.length ? h("div", { class: "dlist" }, list.map(([k, i]) => { const f = chans(d, k)[i].fir; return h("div", { class: "drow" }, h("span", { class: "dlab" }, chLabel(k, i)), h("span", { class: "grow dsub" }, "type" in f ? FIR_TYPES()[f.type] : f.name), h("b", { class: "dval" }, firTapsOf(chans(d, k)[i]))); })) : null]; }, }, notes: { title: () => t("Notes"), w: 4, min: [2, 2], max: [12, 14], h: 4, global: true, icon: '', body(m) { return m.cfg.text ? h("p", { class: "dnote" }, m.cfg.text) : h("p", { class: "info dempty" }, t("Empty – add text in the module settings.")); }, }, }; // ------------------------------------------------------------------ editing function dashAdd(type, at = dashMods().length) { const mods = dashMods(), def = DASH_MODULES[type]; const id = "m" + Date.now().toString(36) + Math.floor(Math.random() * 1296).toString(36); const d = dashDevs()[0]; const m = { id, type, w: def.w, cfg: def.global || !d ? {} : { device: d.id } }; mods.splice(at, 0, m); dashSave(); return m; } async function dashSettings(m) { const def = DASH_MODULES[m.type], d = dashDev(m); const title = h("input", { type: "text", maxlength: 40, value: m.cfg.title || "", placeholder: def.title() }); const dev_ = def.global ? null : h("select", {}, dashDevs().map(x => h("option", { value: x.id, selected: x === d }, x.name))); const text = m.type === "notes" ? h("textarea", { rows: 6, maxlength: 2000 }, m.cfg.text || "") : null; // channels drawn over the curve: fixed per module, whichever channel is being looked at const overlays = new Set(m.cfg.overlays || []); const overlayBox = m.type === "curve" ? h("div", { class: "doverlays" }) : null; const fillOverlays = () => { const x = S.devices[dev_.value]; if (!x || !x.state) return overlayBox.replaceChildren(h("p", { class: "info" }, t("{name} is not connected.", { name: x ? x.name : "DSP" }))); overlayBox.replaceChildren(...dashChannels(x).map(([k, i]) => { const key = k + i, box = h("input", { type: "checkbox", checked: overlays.has(key), onchange: () => (box.checked ? overlays.add(key) : overlays.delete(key)) }); return h("label", { class: "chk", style: `--c:${chColor(x, k, i)}` }, box, h("i"), chLabel(k, i)); })); }; if (overlayBox) { fillOverlays(); dev_.addEventListener("change", fillOverlays); } const res = await modal({ title: def.title(), body: h("div", { class: "form dform" }, h("label", { class: "full" }, t("Title"), title), dev_ ? h("label", { class: "full" }, "DSP", dev_) : null, text ? h("label", { class: "full" }, t("Text"), text) : null, overlayBox ? h("div", { class: "full" }, h("div", { class: "sub" }, t("Show on top of the curve")), overlayBox) : null), actions: [{ label: t("Cancel") }, { label: t("Save"), primary: true, value: () => true }] }); if (!res) return; m.cfg = { ...m.cfg, title: title.value.trim() }; if (dev_) m.cfg.device = dev_.value; if (text) m.cfg.text = text.value; if (overlayBox) m.cfg.overlays = [...overlays]; dashBrowse.delete(m.id); dashSave(); render(); } // Drag with pointer events (mouse and touch): modules are moved, new ones come from the palette. function dashDrag(ev, source, el) { if (ev.button) return; const sx = ev.clientX, sy = ev.clientY; let ghost = null, ph = null, grid = null; const start = () => { S.busy = true; grid = $("#main .dash"); const size = source.mod || { type: source.type, w: DASH_MODULES[source.type].w, cfg: {} }; ph = h("div", { class: "dmod dph", style: dashSizeStyle(size.w, dashHeight(size, source.mod ? dashDev(size) : dashDevs()[0])) }); const r = el.getBoundingClientRect(); ghost = (source.mod ? el.closest(".dmod") : el).cloneNode(true); ghost.classList.add("dghost"); ghost.style.width = (source.mod ? el.closest(".dmod").getBoundingClientRect().width : r.width) + "px"; document.body.append(ghost); if (source.mod) { const home = el.closest(".dmod"); home.after(ph); home.hidden = true; } else grid.append(ph); document.body.classList.add("ddragging"); }; const place = (x, y) => { ghost.style.transform = `translate(${x + 8}px, ${y + 8}px)`; if (y < 70) window.scrollBy(0, -12); else if (y > window.innerHeight - 70) window.scrollBy(0, 12); const items = [...grid.children].filter(c => c !== ph && !c.hidden); const next = items.find(c => { const r = c.getBoundingClientRect(); return y < r.top || (y < r.bottom && x < r.left + r.width / 2); }); if (next) { if (ph.nextSibling !== next) grid.insertBefore(ph, next); } else if (grid.lastChild !== ph) grid.append(ph); }; const move = e => { if (!ghost) { if (Math.hypot(e.clientX - sx, e.clientY - sy) < 6) return; start(); } e.preventDefault(); place(e.clientX, e.clientY); }; const end = e => { window.removeEventListener("pointermove", move); window.removeEventListener("pointerup", end); window.removeEventListener("pointercancel", end); if (!ghost) { if (e.type === "pointerup" && source.type) { dashAdd(source.type); render(); } return; } const items = [...grid.children].filter(c => !c.hidden); const at = items.indexOf(ph); const onGrid = e.type === "pointerup"; ghost.remove(); document.body.classList.remove("ddragging"); S.busy = false; const mods = dashMods(); if (onGrid && at >= 0) { if (source.mod) { mods.splice(mods.indexOf(source.mod), 1); mods.splice(at, 0, source.mod); dashSave(); } else dashAdd(source.type, at); } render(); }; window.addEventListener("pointermove", move, { passive: false }); window.addEventListener("pointerup", end); window.addEventListener("pointercancel", end); } function dashPalette() { const list = Object.entries(DASH_MODULES).filter(([, def]) => !def.only || def.only()); return h("div", { class: "card dpalette" }, h("h3", {}, h("span", { class: "grow" }, t("Add modules")), h("span", { class: "info dhelp" }, t("Drag into the page or tap"))), h("div", { class: "dchips" }, list.map(([type, def]) => { const c = h("button", { class: "dchip", type: "button" }, svgIcon(def.icon), def.title()); c.addEventListener("pointerdown", e => dashDrag(e, { type }, c)); return c; }))); } // ------------------------------------------------------------------ page const dashSizeStyle = (w, rows) => `--w:${w};--wt:${w < 6 ? 6 : 12};--h:${rows}`; const dashLimit = (m, axis, v) => clamp(v, DASH_MODULES[m.type].min[axis], DASH_MODULES[m.type].max[axis]); // rows: chosen by the user, or what the module needs on this DSP function dashHeight(m, d) { const def = DASH_MODULES[m.type]; return dashLimit(m, 1, m.h || (typeof def.h === "function" ? def.h(d) : def.h)); } function dashResizeStart(ev, m, el, axes) { ev.preventDefault(); ev.stopPropagation(); const grid = $("#main .dash"), col = (grid.clientWidth + 10) / 12, sx = ev.clientX, sy = ev.clientY; const w0 = m.w, h0 = dashHeight(m, dashDev(m)); let w = w0, rows = h0; S.busy = true; document.body.classList.add("dresizing"); const move = e => { if (axes.includes("w")) w = dashLimit(m, 0, w0 + Math.round((e.clientX - sx) / col)); if (axes.includes("h")) rows = dashLimit(m, 1, h0 + Math.round((e.clientY - sy) / DASH_ROW)); el.setAttribute("style", dashSizeStyle(w, rows)); }; const end = () => { window.removeEventListener("pointermove", move); window.removeEventListener("pointerup", end); window.removeEventListener("pointercancel", end); document.body.classList.remove("dresizing"); S.busy = false; if (w !== w0 || rows !== h0) { m.w = w; m.h = rows; dashSave(); } render(); }; window.addEventListener("pointermove", move); window.addEventListener("pointerup", end); window.addEventListener("pointercancel", end); } function dashModule(m) { const def = DASH_MODULES[m.type]; if (!def) return null; const d = def.global ? null : dashDev(m); let body; try { body = def.global || (d && d.status === "online" && d.state) ? def.body(m, d) : offline(d); } catch (e) { console.error(e); body = h("p", { class: "info dempty" }, t("This module can't show this DSP.")); } const controls = dashEdit ? (() => { return h("div", { class: "dctl" }, h("button", { class: "small", "aria-label": t("Module settings"), title: t("Module settings"), onclick: () => dashSettings(m) }, "⚙"), h("button", { class: "small danger", "aria-label": t("Remove"), title: t("Remove"), onclick: () => { dashMods().splice(dashMods().indexOf(m), 1); dashSave(); render(); } }, "×")); })() : null; const head = h("h3", { title: dashEdit ? t("Drag to move") : null }, svgIcon(def.icon), h("span", { class: "grow dtitle" }, m.cfg.title || def.title()), d && dashDevs().length > 1 ? h("small", { class: "ddev" }, d.name) : null, controls); const el = h("section", { class: `card dmod dt-${m.type}`, style: dashSizeStyle(m.w, dashHeight(m, d)) }, head, h("div", { class: "dbody" }, body)); if (dashEdit) { head.addEventListener("pointerdown", e => { if (!e.target.closest("button")) dashDrag(e, { mod: m }, head); }); const handle = (axes, cls, arrow, label) => { const g = h("span", { class: "dgrab " + cls, title: label }, arrow); g.addEventListener("pointerdown", e => dashResizeStart(e, m, el, axes)); return g; }; el.append(handle("w", "de", "↔", t("Drag to resize")), handle("h", "ds", "↕", t("Drag to resize")), handle("wh", "dse", "↘", t("Drag to resize"))); } return el; } function viewDash() { if (viewOnly()) dashEdit = false; const mods = dashMods(); return [ h("div", { class: "dhead" }, h("h2", {}, "Dashboard"), viewOnly() ? null : h("button", { class: dashEdit ? "on" : "", onclick: () => { dashEdit = !dashEdit; render(); } }, dashEdit ? t("Done") : t("Customise"))), dashEdit ? dashPalette() : null, mods.length ? h("div", { class: "dash" + (dashEdit ? " editing" : "") }, mods.map(dashModule)) : h("div", { class: "dash" + (dashEdit ? " editing" : "") }, h("div", { class: "dmod dblank", style: dashSizeStyle(12, 4) }, t("Nothing here yet – press “Customise” and add modules."))), ]; } let dashResize = 0; window.addEventListener("resize", () => { clearTimeout(dashResize); dashResize = setTimeout(() => { if (S.view === "dash" && !S.settings && !S.busy) render(); }, 200); });