/* ===================================================================== LAUTHEIT WIE IM RUNDFUNK – der Rechenteil (05.11.2026, Neuplan C9/E7) Läuft als AudioWorklet in einem eigenen Audiofaden: Die Seite selbst rechnet nichts. Gemessen nach ITU-R BS.1770-4 / EBU R128: · K-Gewichtung (Hochregal + Hochpass, Koeffizienten je Abtastrate), · Momentan (400 ms), Kurzzeit (3 s), Integriert (Gating: absolut −70 LUFS, relativ −10 LU), · True Peak über vierfache Überabtastung. Eine Tonspur mit EINEM Kanal wird als Doppel-Mono gezählt – sie kommt beim Zuschauer auf beiden Lautsprechern an. Der Kern ist reines Rechnen ohne Audio-Schnittstellen, damit ihn die Prüfung in Node mit den Testsignalen aus EBU Tech 3341 füttern kann. ===================================================================== */ 'use strict'; function kStufen(fs) { /* Hochregal (Kopfeinfluss) */ let f0 = 1681.974450955533, G = 3.999843853973347, Q = 0.7071752369554196; let K = Math.tan(Math.PI * f0 / fs); const Vh = Math.pow(10, G / 20), Vb = Math.pow(Vh, 0.4996667741545416); let a0 = 1 + K / Q + K * K; const eins = { b: [(Vh + Vb * K / Q + K * K) / a0, 2 * (K * K - Vh) / a0, (Vh - Vb * K / Q + K * K) / a0], a: [2 * (K * K - 1) / a0, (1 - K / Q + K * K) / a0], }; /* Hochpass (RLB) */ f0 = 38.13547087602444; Q = 0.5003270373238773; K = Math.tan(Math.PI * f0 / fs); a0 = 1 + K / Q + K * K; const zwei = { b: [1, -2, 1], a: [2 * (K * K - 1) / a0, (1 - K / Q + K * K) / a0] }; return [eins, zwei]; } /* Vierfache Überabtastung: 48 Abgriffe, Kaiser-gefensterter Sinc, je Phase auf Verstärkung 1 normiert. */ function firBauen() { const N = 48, L = 4, beta = 7; const bessel = (x) => { let s = 1, t = 1; for (let k = 1; k < 30; k++) { t *= (x / (2 * k)) * (x / (2 * k)); s += t; } return s; }; const h = new Float64Array(N); for (let n = 0; n < N; n++) { const m = n - (N - 1) / 2; const x = m / L; const sinc = x === 0 ? 1 : Math.sin(Math.PI * x) / (Math.PI * x); const r = 2 * n / (N - 1) - 1; h[n] = sinc * bessel(beta * Math.sqrt(Math.max(0, 1 - r * r))) / bessel(beta); } const phasen = []; for (let p = 0; p < L; p++) { const c = []; for (let n = p; n < N; n += L) c.push(h[n]); const s = c.reduce((a, b) => a + b, 0); phasen.push(Float64Array.from(c.map((x) => x / s))); } return phasen; } class LautheitKern { constructor(fs) { this.fs = fs; this.stufen = kStufen(fs); this.fir = firBauen(); this.teilLaenge = Math.round(fs / 10); /* 100 ms */ this.kanal = []; this.summe = 0; this.zahl = 0; this.spitze = 0; this.teile = []; /* Energie je 100 ms, für Momentan/Kurzzeit */ this.bloecke = []; /* Energie je 400-ms-Block, alle 100 ms – fürs Integrierte */ this.spitzeGesamt = 0; } zuruecksetzen() { this.bloecke = []; this.spitzeGesamt = 0; } zustand(i) { if (!this.kanal[i]) this.kanal[i] = { z: [[0, 0], [0, 0]], verlauf: new Float64Array(12), pos: 0 }; return this.kanal[i]; } /* kanaele: Liste gleich langer Float32Array. Gibt fertige 100-ms-Teile zurück. */ schieben(kanaele) { const fertig = []; const gewicht = kanaele.length === 1 ? 2 : 1; const n = kanaele[0]?.length || 0; for (let j = 0; j < n; j++) { let quadrat = 0; for (let c = 0; c < kanaele.length; c++) { const k = this.zustand(c); const x = kanaele[c][j]; /* True Peak */ k.verlauf[k.pos] = x; k.pos = (k.pos + 1) % 12; for (const ph of this.fir) { let y = 0; for (let t = 0; t < 12; t++) y += ph[t] * k.verlauf[(k.pos - 1 - t + 24) % 12]; const b = Math.abs(y); if (b > this.spitze) this.spitze = b; } /* K-Gewichtung, zwei Biquads (Direktform II transponiert) */ let v = x; for (let s = 0; s < 2; s++) { const f = this.stufen[s], z = k.z[s]; const y = f.b[0] * v + z[0]; z[0] = f.b[1] * v - f.a[0] * y + z[1]; z[1] = f.b[2] * v - f.a[1] * y; v = y; } quadrat += v * v; } this.summe += quadrat * gewicht; this.zahl++; if (this.zahl >= this.teilLaenge) { const e = this.summe / this.zahl; this.teile.push(e); if (this.teile.length > 30) this.teile.shift(); if (this.teile.length >= 4) { const t = this.teile; this.bloecke.push((t[t.length - 1] + t[t.length - 2] + t[t.length - 3] + t[t.length - 4]) / 4); if (this.bloecke.length > 216000) this.bloecke.shift(); /* 6 h */ } if (this.spitze > this.spitzeGesamt) this.spitzeGesamt = this.spitze; fertig.push({ e, tp: this.spitze }); this.summe = 0; this.zahl = 0; this.spitze = 0; } } return fertig; } static lufs(e) { return e > 0 ? -0.691 + 10 * Math.log10(e) : -Infinity; } static db(a) { return a > 0 ? 20 * Math.log10(a) : -Infinity; } mittel(anzahl) { const t = this.teile; if (t.length < anzahl) return -Infinity; let s = 0; for (let i = t.length - anzahl; i < t.length; i++) s += t[i]; return LautheitKern.lufs(s / anzahl); } integriert() { const ABS = Math.pow(10, (-70 + 0.691) / 10); let s = 0, n = 0; for (const b of this.bloecke) if (b > ABS) { s += b; n++; } if (!n) return -Infinity; const rel = (s / n) * Math.pow(10, -10 / 10); let s2 = 0, n2 = 0; for (const b of this.bloecke) if (b > ABS && b > rel) { s2 += b; n2++; } return n2 ? LautheitKern.lufs(s2 / n2) : -Infinity; } bericht() { return { momentan: this.mittel(4), kurz: this.mittel(30), integriert: this.integriert(), spitze: LautheitKern.db(this.spitzeGesamt), bloecke: this.bloecke.length, }; } } if (typeof registerProcessor === 'function') { registerProcessor('lautheit', class extends AudioWorkletProcessor { constructor() { super(); this.kern = new LautheitKern(sampleRate); this.port.onmessage = (ev) => { if (ev.data === 'neu') this.kern.zuruecksetzen(); }; } process(eingaenge) { const ein = eingaenge[0]; if (ein && ein.length && ein[0].length) { const fertig = this.kern.schieben(ein); if (fertig.length) { const b = this.kern.bericht(); b.spitzeJetzt = LautheitKern.db(fertig[fertig.length - 1].tp); this.port.postMessage(b); } } return true; } }); } else if (typeof globalThis !== 'undefined') { globalThis.LautheitKern = LautheitKern; }