// Mastering studio (DoD): client-side WebAudio chain EQ → compressor → limiter, loudness normalisation to target LUFS // (ITU-R BS.1770-4 K-weighting + gating, measured offline), live meter, A/B, WAV/MP3 export, save to workspace. import { STUDIOS, onStop, loop, cv } from './widgets.js?v=50'; import { result, MODULES, esc, setS, keep } from './real.js?v=136'; /* ---------- BS.1770 loudness ---------- */ function biquad(x, b0, b1, b2, a0, a1, a2) { const y = new Float32Array(x.length); b0 /= a0; b1 /= a0; b2 /= a0; a1 /= a0; a2 /= a0; let x1 = 0, x2 = 0, y1 = 0, y2 = 0; for (let i = 0; i < x.length; i++) { const v = b0 * x[i] + b1 * x1 + b2 * x2 - a1 * y1 - a2 * y2; x2 = x1; x1 = x[i]; y2 = y1; y1 = v; y[i] = v; } return y; } function kweight(x, fs) { let w0 = 2 * Math.PI * 1681.974450955533 / fs, A = Math.pow(10, 3.99984385397 / 40), Q = 0.7071752369554196, al = Math.sin(w0) / (2 * Q), c = Math.cos(w0), sA = Math.sqrt(A); const s1 = biquad(x, A * ((A + 1) + (A - 1) * c + 2 * sA * al), -2 * A * ((A - 1) + (A + 1) * c), A * ((A + 1) + (A - 1) * c - 2 * sA * al), (A + 1) - (A - 1) * c + 2 * sA * al, 2 * ((A - 1) - (A + 1) * c), (A + 1) - (A - 1) * c - 2 * sA * al); w0 = 2 * Math.PI * 38.13547087602444 / fs; Q = 0.5003270373238773; al = Math.sin(w0) / (2 * Q); c = Math.cos(w0); return biquad(s1, (1 + c) / 2, -(1 + c), (1 + c) / 2, 1 + al, -2 * c, 1 - al); } export function lufs(buf) { const fs = buf.sampleRate, ch = Math.min(buf.numberOfChannels, 2), K = [...Array(ch)].map((_, c) => kweight(buf.getChannelData(c), fs)); const blk = Math.round(0.4 * fs), hop = Math.round(0.1 * fs), z = []; for (let s = 0; s + blk <= buf.length; s += hop) { let sum = 0; for (let c = 0; c < ch; c++) { const k = K[c]; let a = 0; for (let i = s; i < s + blk; i++) a += k[i] * k[i]; sum += a / blk; } z.push(sum); } if (!z.length) return -70; const L = (v) => -0.691 + 10 * Math.log10(v); const g1 = z.filter(v => L(v) > -70); if (!g1.length) return -70; const rel = L(g1.reduce((a, b) => a + b, 0) / g1.length) - 10; const g2 = g1.filter(v => L(v) > rel); return L(g2.reduce((a, b) => a + b, 0) / g2.length); } const peakDb = (buf) => { let p = 0; for (let c = 0; c < buf.numberOfChannels; c++) { const d = buf.getChannelData(c); for (let i = 0; i < d.length; i++) { const v = Math.abs(d[i]); if (v > p) p = v; } } return 20 * Math.log10(p || 1e-9); }; /* ---------- processing chain (offline render) ---------- */ export const PRESETS = { streaming: { name: 'Streaming −14 LUFS', target: -14, ceiling: -1 }, club: { name: 'Club −9 LUFS', target: -9, ceiling: -0.5 }, podcast: { name: 'Podcast −16 LUFS', target: -16, ceiling: -1.5 }, broadcast: { name: 'EBU R128 −23 LUFS', target: -23, ceiling: -1 } }; async function render(buf, p, makeupDb) { const oc = new OfflineAudioContext(buf.numberOfChannels, buf.length, buf.sampleRate); const s = oc.createBufferSource(); s.buffer = buf; const lo = oc.createBiquadFilter(); lo.type = 'lowshelf'; lo.frequency.value = 100; lo.gain.value = p.low; const mid = oc.createBiquadFilter(); mid.type = 'peaking'; mid.frequency.value = 1000; mid.Q.value = 0.8; mid.gain.value = p.mid; const hi = oc.createBiquadFilter(); hi.type = 'highshelf'; hi.frequency.value = 8000; hi.gain.value = p.high; const comp = oc.createDynamicsCompressor(); comp.threshold.value = p.thr; comp.ratio.value = p.ratio; comp.knee.value = 6; comp.attack.value = 0.01; comp.release.value = 0.2; const g = oc.createGain(); g.gain.value = Math.pow(10, makeupDb / 20); const lim = oc.createDynamicsCompressor(); lim.threshold.value = p.ceiling - 0.5; lim.knee.value = 0; lim.ratio.value = 20; lim.attack.value = 0.001; lim.release.value = 0.05; s.connect(lo); lo.connect(mid); mid.connect(hi); hi.connect(comp); comp.connect(g); g.connect(lim); lim.connect(oc.destination); s.start(); const out = await oc.startRendering(); const pk = peakDb(out); if (pk > p.ceiling) { const k = Math.pow(10, (p.ceiling - pk) / 20); for (let c = 0; c < out.numberOfChannels; c++) { const d = out.getChannelData(c); for (let i = 0; i < d.length; i++) d[i] *= k; } } // safety ceiling return out; } export async function master(buf, p) { const before = lufs(buf); let mk = p.target - lufs(await render(buf, p, 0)), out; for (let i = 0; i < 3; i++) { out = await render(buf, p, mk); const l = lufs(out); if (Math.abs(l - p.target) < 0.3) break; mk += p.target - l; } // limiter eats gain → iterate return { out, before, after: lufs(out), peakBefore: peakDb(buf), peakAfter: peakDb(out), makeup: mk }; } /* ---------- export ---------- */ export function toWav(buf) { const ch = buf.numberOfChannels, sr = buf.sampleRate, n = buf.length, dv = new DataView(new ArrayBuffer(44 + n * ch * 2)); const w = (o, s) => [...s].forEach((c, i) => dv.setUint8(o + i, c.charCodeAt(0))); w(0, 'RIFF'); dv.setUint32(4, 36 + n * ch * 2, true); w(8, 'WAVEfmt '); dv.setUint32(16, 16, true); dv.setUint16(20, 1, true); dv.setUint16(22, ch, true); dv.setUint32(24, sr, true); dv.setUint32(28, sr * ch * 2, true); dv.setUint16(32, ch * 2, true); dv.setUint16(34, 16, true); w(36, 'data'); dv.setUint32(40, n * ch * 2, true); const D = [...Array(ch)].map((_, c) => buf.getChannelData(c)); for (let i = 0, o = 44; i < n; i++) for (let c = 0; c < ch; c++, o += 2) dv.setInt16(o, Math.max(-1, Math.min(1, D[c][i])) * 32767, true); return new Blob([dv], { type: 'audio/wav' }); } export async function toMp3(buf, kbps = 192, onP) { const { Mp3Encoder } = await import('./vendor/lame.js?v=1'); const ch = Math.min(2, buf.numberOfChannels), enc = new Mp3Encoder(ch, buf.sampleRate, kbps); const I = (d) => { const o = new Int16Array(d.length); for (let i = 0; i < d.length; i++) o[i] = Math.max(-1, Math.min(1, d[i])) * 32767; return o; }; const L = I(buf.getChannelData(0)), R = ch > 1 ? I(buf.getChannelData(1)) : null; const parts = []; for (let i = 0; i < L.length; i += 1152 * 50) { const e = R ? enc.encodeBuffer(L.subarray(i, i + 1152 * 50), R.subarray(i, i + 1152 * 50)) : enc.encodeBuffer(L.subarray(i, i + 1152 * 50)); if (e.length) parts.push(new Uint8Array(e)); onP?.(i / L.length); await new Promise(r => setTimeout(r)); } const f = enc.flush(); if (f.length) parts.push(new Uint8Array(f)); return new Blob(parts, { type: 'audio/mpeg' }); } const decode = async (blob) => { const ac = new OfflineAudioContext(2, 1, 44100); return ac.decodeAudioData(await blob.arrayBuffer()); }; const toBlob = async (a) => a.blob || a.file || (await fetch(a.url, { credentials: 'include' })).blob(); /* ---------- studio ---------- */ STUDIOS.push({ id: 'master', cat: 'Audio', icon: '🎛', name: 'Mastering', like: 'Mastering-Suite', real: 'LOKAL', accepts: ['audio'], build(el) { el.innerHTML = `