Files
dogfather-universe/workspace/assets/js/pads.js
T
DogFatherGitandClaude Opus 5.5 af47c1d23c Reaction: Nachbericht mit Dogen und Technik, Deck in der Transportleiste, RØDECaster-Pads
Nachbericht (C14/E8): Spenden der Sendung in Dogen (bestätigt/offen getrennt,
nie Geld, keine Namen) als Kennzahl und Marken im Verlauf; Technik je Minute
aus der Cockpit-Ampel (neuer Weg POST /workspace/api/reaktion/technik, nur
Regie, nur live, höchstens einmal je Minute je Person, feste Werte), gespeichert
additiv in reaktion_protokoll.technik. Nicht gemeldet/nicht messbar zählt nie
als gut. Werte als umschaltbare Tabelle (Vorleseprogramme), Druck-/PDF-Ansicht
hell auf Papier. Der Saal bekommt nachweislich nur freigegebene Schlüssel.

Deck (C6.8-10): Bereiche Video/Szenen/Ich & schnell; Szenen klicken die
vorhandenen Knöpfe in #layout-reihe, K/M/B drücken die vorhandenen Tasten.
Keine neue Taste, am Handy und unter 900 px ausgeblendet.

Pads (E9): Lernmodus mit Web MIDI, feste Aktionsliste, ein Pad = eine
Funktion, Zuordnung in localStorage wird beim Lesen geprüft; drei Ausgänge
(nicht da / nicht erlaubt / kein Gerät). Fragt nur nach Klick oder bei schon
erteilter Erlaubnis.

pruef-reaktion 674 -> 696, Browserprobe wt-bericht/bericht-test.mjs 48/48.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-04 03:51:31 +01:00

302 lines
13 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/* =====================================================================
DIE RØDECASTER-PADS (04.11.2026, Neuplan E9 / C6.11)
Die SMART-Pads des RØDECaster Duo können MIDI an den Rechner senden
(laut RØDE: Einstellungen › System › MIDI). Chrome liest MIDI nach einer
einmaligen Erlaubnis (Web MIDI). Diese Datei lernt, welches Pad was tun
soll, und löst danach genau das aus.
NUR AKTIONEN AUS EINER FESTEN LISTE (AKTIONEN): Moment H, Band B,
Kamera K, Mikro M, Szene 1–5. Ausgelöst wird jede über die TASTE, die
reaktion.js ohnehin belegt – am <body>, wie befehle.js. Keine zweite
Logik, keine neue Tastaturtaste, keine dynamische Auswertung: Eine
MIDI-Nachricht wird zu einem Schlüssel („note:1:36“), der Schlüssel in
der Zuordnung nachgeschlagen, und nur eine Kennung aus AKTIONEN führt
zu einem Tastendruck.
EIN PAD = EINE FUNKTION, EINE FUNKTION = EIN PAD. Wer ein schon belegtes
Pad neu zuordnet, nimmt es der alten Aktion weg – und das steht auch da.
DIE ZUORDNUNG WIRD BEIM LESEN GEPRÜFT: Was in localStorage steht, kann
jeder Browser-Zusatz, jede alte Fassung und jede Hand verändert haben.
Unbekannte Aktionen, kaputte Schlüssel, Zahlen außerhalb von MIDI und
doppelte Aktionen werden verworfen, nicht „irgendwie“ ausgeführt.
DREI AUSGÄNGE BEIM VERBINDEN: verbunden (mit Gerätenamen) · „kein
MIDI-Gerät gefunden“ · „nicht erlaubt“ – dazu „gibt es in diesem Browser
nicht“. Gefragt wird NUR nach einem Klick auf „Mit MIDI verbinden“; von
selbst verbindet sich die Seite nur, wenn die Erlaubnis schon erteilt
ist (Permissions-Abfrage, die selbst nichts fragt) und es Zuordnungen
gibt.
Kein innerHTML: Gerätenamen sind fremder Text und gehen über textContent.
===================================================================== */
(() => {
'use strict';
const $ = (id) => document.getElementById(id);
const el = (tag, cls, text) => { const e = document.createElement(tag); if (cls) e.className = cls; if (text != null) e.textContent = text; return e; };
const taste = (key) => document.body.dispatchEvent(new KeyboardEvent('keydown', { key, bubbles: true, cancelable: true }));
/* Die feste Liste. pruef-reaktion leitet daraus ab, dass jede Taste im
Handler von reaktion.js steht. */
const AKTIONEN = [
{ id: 'moment', titel: 'Moment merken', taste: 'h', zeige: 'H' },
{ id: 'band', titel: 'Werbeband aus / an', taste: 'b', zeige: 'B' },
{ id: 'kamera', titel: 'Eigene Kamera aus / an', taste: 'k', zeige: 'K' },
{ id: 'mikro', titel: 'Eigenes Mikro stumm / an', taste: 'm', zeige: 'M' },
{ id: 'szene1', titel: 'Szene 1 (Video groß)', taste: '1', zeige: '1' },
{ id: 'szene2', titel: 'Szene 2 (Gleich groß)', taste: '2', zeige: '2' },
{ id: 'szene3', titel: 'Szene 3 (Kameras groß)', taste: '3', zeige: '3' },
{ id: 'szene4', titel: 'Szene 4 (Nur Video)', taste: '4', zeige: '4' },
{ id: 'szene5', titel: 'Szene 5 (Nur Kamera)', taste: '5', zeige: '5' },
];
const AKTION = new Map(AKTIONEN.map((a) => [a.id, a]));
const SPEICHER = 'saal.pads';
const PAD_FORM = /^(note|cc|pc):([1-9]|1[0-6]):(\d{1,3})$/;
const LERNEN_MS = 15_000;
/* ------------------------------------------------- Zuordnung lesen/prüfen */
function gueltigerPad(p) {
const m = PAD_FORM.exec(String(p));
return !!m && Number(m[3]) <= 127;
}
/** Aus einem beliebigen Wert eine saubere Zuordnung { pad: aktion }. */
function pruefen(roh) {
const aus = {};
const vergeben = new Set();
const z = roh && typeof roh === 'object' && !Array.isArray(roh) && roh.zuordnung;
if (!z || typeof z !== 'object' || Array.isArray(z)) return aus;
for (const [pad, aktion] of Object.entries(z)) {
if (!gueltigerPad(pad) || typeof aktion !== 'string' || !AKTION.has(aktion) || vergeben.has(aktion)) continue;
aus[pad] = aktion;
vergeben.add(aktion);
}
return aus;
}
function lesen() {
try { return pruefen(JSON.parse(localStorage.getItem(SPEICHER) || 'null')); } catch { return {}; }
}
let zuordnung = lesen();
function speichern() {
try { localStorage.setItem(SPEICHER, JSON.stringify({ version: 1, zuordnung })); } catch { /* privat */ }
}
/* ------------------------------------------------ MIDI-Nachricht deuten */
/** Rohbytes -> { pad, ausloesen, text } oder null (Takt, Sysex, Unbekanntes). */
function deuten(daten) {
const b = Array.from(daten || []);
if (!b.length) return null;
const status = b[0];
if (status >= 0xF0) return null; /* Systemnachrichten: Takt, Aktivität, Sysex */
const art = status & 0xF0;
const kanal = (status & 0x0F) + 1;
const n = b[1] ?? 0;
const w = b[2] ?? 0;
if (art === 0x90) return { pad: `note:${kanal}:${n}`, ausloesen: w > 0, text: `Note ${w > 0 ? 'an' : 'aus'} · Kanal ${kanal} · Nummer ${n} · Stärke ${w}` };
if (art === 0x80) return { pad: `note:${kanal}:${n}`, ausloesen: false, text: `Note aus · Kanal ${kanal} · Nummer ${n}` };
if (art === 0xB0) return { pad: `cc:${kanal}:${n}`, ausloesen: w > 0, text: `Regler (CC) · Kanal ${kanal} · Nummer ${n} · Wert ${w}` };
if (art === 0xC0) return { pad: `pc:${kanal}:${n}`, ausloesen: true, text: `Programmwechsel · Kanal ${kanal} · Nummer ${n}` };
return { pad: null, ausloesen: false, text: `andere Nachricht (${b.map((x) => x.toString(16).padStart(2, '0')).join(' ')})` };
}
const padWort = (p) => {
const m = PAD_FORM.exec(p);
if (!m) return '–';
return `${{ note: 'Note', cc: 'CC', pc: 'Programm' }[m[1]]} ${m[3]} · Kanal ${m[2]}`;
};
/* --------------------------------------------------------------- Zustand */
let lage = null;
let zugang = null;
let stand = { art: 'aus', wort: 'Noch nicht verbunden.' };
let lernt = null; /* { aktion, uhr } */
const protokoll = []; /* die letzten Nachrichten, nur zum Ansehen */
function standSetzen(art, wort) { stand = { art, wort }; zeichnen(); }
function eingaengeAnhaengen() {
if (!zugang) return;
const namen = [];
for (const e of zugang.inputs.values()) {
e.onmidimessage = nachricht;
namen.push(e.name || 'MIDI-Gerät');
}
if (!namen.length) standSetzen('kein_geraet', 'Kein MIDI-Gerät gefunden. Ist das Pult per USB verbunden und MIDI im Pult eingeschaltet (Einstellungen › System › MIDI)?');
else standSetzen('verbunden', `Verbunden: ${namen.join(', ')}`);
}
async function verbinden() {
if (typeof navigator.requestMIDIAccess !== 'function') {
standSetzen('nicht_da', 'Web MIDI gibt es in diesem Browser nicht – bitte Chrome oder Edge am Rechner nehmen.');
return stand.art;
}
standSetzen('fragt', 'Frage nach der Erlaubnis …');
try {
zugang = await navigator.requestMIDIAccess({ sysex: false });
} catch (f) {
zugang = null;
standSetzen('nicht_erlaubt', f?.name === 'SecurityError' || f?.name === 'NotAllowedError'
? 'MIDI ist nicht erlaubt. Im Schloss links neben der Adresse lässt es sich wieder erlauben.'
: 'MIDI ging gerade nicht – bitte noch einmal versuchen.');
return stand.art;
}
zugang.onstatechange = () => eingaengeAnhaengen();
eingaengeAnhaengen();
return stand.art;
}
/* Von selbst nur, wenn die Erlaubnis schon da ist UND es etwas zu tun
gibt. `permissions.query` fragt den Menschen nicht – sie liest nur. */
async function stillVerbinden() {
if (zugang || !Object.keys(zuordnung).length || typeof navigator.requestMIDIAccess !== 'function') return;
try {
const p = await navigator.permissions?.query?.({ name: 'midi' });
if (p?.state === 'granted') await verbinden();
} catch { /* unbekannte Erlaubnis: nicht fragen */ }
}
function nachricht(e) {
const d = deuten(e?.data);
if (!d) return;
protokoll.unshift({ zeit: new Date(), text: d.text, pad: d.pad });
protokoll.length = Math.min(protokoll.length, 8);
if (lernt) {
if (d.pad && d.ausloesen) zuordnen(lernt.aktion, d.pad);
else zeichnen();
return;
}
zeichnen();
if (!d.pad || !d.ausloesen || !lage?.ich?.host) return;
const a = AKTION.get(zuordnung[d.pad]);
if (a) taste(a.taste);
}
function lernen(aktion) {
if (!AKTION.has(aktion)) return;
if (lernt) clearTimeout(lernt.uhr);
lernt = { aktion, uhr: setTimeout(() => { lernt = null; sagen('Kein Pad gedrückt – abgebrochen.'); zeichnen(); }, LERNEN_MS) };
sagen(`Drück jetzt das Pad für „${AKTION.get(aktion).titel}“.`);
zeichnen();
}
function zuordnen(aktion, pad) {
if (!AKTION.has(aktion) || !gueltigerPad(pad)) return false;
if (lernt) { clearTimeout(lernt.uhr); lernt = null; }
const vorher = zuordnung[pad];
for (const [p, a] of Object.entries(zuordnung)) if (a === aktion) delete zuordnung[p];
zuordnung[pad] = aktion;
speichern();
sagen(vorher && vorher !== aktion
? `${padWort(pad)} war „${AKTION.get(vorher).titel}“ – jetzt „${AKTION.get(aktion).titel}“.`
: `${padWort(pad)} → „${AKTION.get(aktion).titel}“.`);
zeichnen();
return true;
}
function loesen(aktion) {
for (const [p, a] of Object.entries(zuordnung)) if (a === aktion) delete zuordnung[p];
speichern();
zeichnen();
}
function sagen(t) { const s = $('pads-sagt'); if (s) s.textContent = t; }
/* ------------------------------------------------------------- Oberfläche */
let dlg = null;
function bauen() {
if (dlg) return;
dlg = el('dialog', 'pads sg-teil');
dlg.id = 'pads';
dlg.setAttribute('aria-labelledby', 'pads-titel');
const kopf = el('div', 'pads__kopf');
const h = el('h2', 'pads__titel', 'RØDECaster-Pads');
h.id = 'pads-titel';
const zu = el('button', 'btn btn-klein btn-sekundaer', 'Schließen');
zu.type = 'button';
zu.addEventListener('click', () => dlg.close());
kopf.append(h, zu);
const satz = el('p', 'pads__satz', 'Aktion wählen, „Pad zuordnen“ drücken, dann das Pad am Pult. Ein Pad tut genau eine Sache.');
const standZeile = el('p', 'pads__stand');
standZeile.id = 'pads-stand';
standZeile.setAttribute('role', 'status');
const verb = el('button', 'btn btn-klein btn-primaer', 'Mit MIDI verbinden');
verb.type = 'button';
verb.id = 'pads-verbinden';
verb.addEventListener('click', verbinden);
const liste = el('ul', 'pads__liste');
liste.id = 'pads-liste';
const sagt = el('p', 'pads__sagt');
sagt.id = 'pads-sagt';
sagt.setAttribute('aria-live', 'polite');
const pt = el('h3', 'pads__unter', 'Zuletzt empfangen');
const prot = el('ol', 'pads__protokoll');
prot.id = 'pads-protokoll';
dlg.append(kopf, satz, standZeile, verb, sagt, liste, pt, prot);
dlg.addEventListener('close', () => { if (lernt) { clearTimeout(lernt.uhr); lernt = null; } });
document.body.append(dlg);
}
function zeichnen() {
if (!dlg) return;
const s = $('pads-stand');
s.textContent = stand.wort;
s.dataset.art = stand.art;
$('pads-verbinden').hidden = stand.art === 'verbunden';
const liste = $('pads-liste');
liste.textContent = '';
for (const a of AKTIONEN) {
const pad = Object.keys(zuordnung).find((p) => zuordnung[p] === a.id);
const li = el('li', 'pads__zeile');
li.dataset.aktion = a.id;
if (lernt?.aktion === a.id) li.dataset.lernt = 'ja';
const name = el('span', 'pads__aktion', a.titel);
name.append(el('kbd', '', a.zeige));
const p = el('span', 'pads__pad', lernt?.aktion === a.id ? 'drück jetzt das Pad …' : (pad ? padWort(pad) : '–'));
const lern = el('button', 'btn btn-klein btn-sekundaer', 'Pad zuordnen');
lern.type = 'button';
lern.dataset.lernen = a.id;
lern.addEventListener('click', () => lernen(a.id));
li.append(name, p, lern);
if (pad) {
const weg = el('button', 'btn btn-klein btn-sekundaer', 'Lösen');
weg.type = 'button';
weg.dataset.loesen = a.id;
weg.setAttribute('aria-label', `${a.titel}: Pad lösen`);
weg.addEventListener('click', () => loesen(a.id));
li.append(weg);
}
liste.append(li);
}
const prot = $('pads-protokoll');
prot.textContent = '';
if (!protokoll.length) prot.append(el('li', 'pads__leer', 'Noch nichts empfangen.'));
for (const n of protokoll) {
const li = el('li', '', n.text);
if (n.pad && zuordnung[n.pad]) li.append(el('span', 'pads__ziel', ` → ${AKTION.get(zuordnung[n.pad]).titel}`));
prot.append(li);
}
}
function oeffnen() {
if (!lage?.ich?.host) return;
bauen();
zeichnen();
if (!dlg.open) dlg.showModal();
}
window.addEventListener('saal:lage', (e) => {
const erstes = !lage;
lage = e.detail;
if (erstes && lage?.ich?.host) stillVerbinden();
});
window.Pads = {
oeffnen, verbinden, lernen, zuordnen, loesen,
anzahl: () => Object.keys(zuordnung).length,
zuordnung: () => ({ ...zuordnung }),
stand: () => ({ ...stand }),
/* Für die Prüfung: dieselbe Prüfung, die beim Laden läuft. */
pruefen, deuten,
aktionen: () => AKTIONEN.map(({ id, taste: t }) => ({ id, taste: t })),
};
})();