juicewrld-desktopsrc-taurisrcpluginsworklet.js
1.4.2Released Nov 21, 2026
worklet.js
202 lines7.2 KBJavaScript// The page's half of the plugin audio path: an AudioWorklet processor that writes each render quantum into a ring in a// WebView2 shared buffer and plays the plugin's output back from it (src/plugins/ring.rs has the layout; the offsets// below are the same words). The plugin host, another process, reads the input and writes the output; nothing goes// through the page's main thread, so a busy page can't interrupt the sound.//// Real time: input frame p plays at p + latency, exactly, or the quantum plays silence and counts an underrun; the// delay never drifts, so the studio can delay its other tracks by the same amount. Offline (an OfflineAudioContext, an// export): each quantum waits for the host to finish it, with no latency of its own.//// The app serves this file at /__juicewrld-desktop/plugins-worklet-1.js on the page's own origin; site.js loads it// (juicewrldDesktop.plugins, Instance.createNode). Messages on the node's port: in, {type: "ring", buffer} (the shared// ArrayBuffer, transferred), {type: "close"}, {type: "transport", tempo, playing, position}; out, {type: "stats", ...}// four times a second, {type: "lost"} when the host went, {type: "error", message}."use strict";const MAGIC = 0x3152574a;const HEADER = 256;const AT = { MAGIC: 0, VERSION: 1, CHANNELS: 2, CAPACITY: 3, LATENCY: 4, MODE: 5, RATE: 6, MAX_BLOCK: 7, IN_WRITE: 16, OUT_READ: 17, UNDERRUNS: 18, QUANTA: 19, CLOSED: 20, TEMPO: 21, PLAYING: 22, POSITION_LO: 23, POSITION_HI: 24, IN_READ: 32, OUT_WRITE: 33, HOST_BEAT: 34, STATE: 35, MAX_US: 36, LAST_US: 37, PLUGIN_LATENCY: 38, LATE: 39, SKIPS: 40, HOLE_START: 41, HOLE_END: 42,};const STATE_LOST = 3;/// An offline quantum gives up on the host after this long (it's gone, or stuck: the app will say which).const OFFLINE_WAIT_MS = 15000;class JuicewrldPluginProcessor extends AudioWorkletProcessor { constructor() { super(); this.h = null; this.quanta = 0; this.underruns = 0; this.done = false; this.lost = false; this.tempo = 0; this.playing = 0; this.position = 0; this.reportEvery = Math.max(1, Math.round(sampleRate / 128 / 4)); this.tempBits = new Float32Array(1); this.tempInt = new Int32Array(this.tempBits.buffer); this.silence = new Float32Array(128); this.port.onmessage = (e) => this.message(e.data || {}); } message(m) { if (m.type === "ring") this.attach(m.buffer); else if (m.type === "close") { if (this.h) Atomics.store(this.h, AT.CLOSED, 1); this.h = null; this.done = true; } else if (m.type === "transport") { if (Number.isFinite(m.tempo)) this.tempo = m.tempo; if (m.playing !== undefined) this.playing = m.playing ? 1 : 0; if (Number.isFinite(m.position)) this.position = Math.max(0, Math.floor(m.position)); } } attach(buffer) { try { const h = new Int32Array(buffer, 0, HEADER / 4); if (Atomics.load(h, AT.MAGIC) !== MAGIC || h[AT.CHANNELS] !== 2) throw new Error("not an audio ring"); const cap = h[AT.CAPACITY]; const data = new Float32Array(buffer, HEADER, cap * 4); this.cap = cap; this.mask = cap - 1; this.latency = h[AT.LATENCY]; this.offline = h[AT.MODE] === 1; this.planes = [data.subarray(0, cap), data.subarray(cap, 2 * cap), data.subarray(2 * cap, 3 * cap), data.subarray(3 * cap, 4 * cap)]; this.h = h; } catch (err) { this.port.postMessage({ type: "error", message: String((err && err.message) || err) }); } } /** Copies n frames of `src` into a ring plane at position pos. */ put(plane, pos, src, n) { const at = pos & this.mask; const first = Math.min(n, this.cap - at); plane.set(first === n ? src : src.subarray(0, first), at); if (first < n) plane.set(src.subarray(first, n), 0); } /** Copies n frames from a ring plane at position pos into `dst`. */ take(plane, pos, dst, n) { const at = pos & this.mask; const first = Math.min(n, this.cap - at); dst.set(plane.subarray(at, at + first), 0); if (first < n) dst.set(plane.subarray(0, n - first), first); } process(inputs, outputs) { const out = outputs[0]; const n = out[0].length; const h = this.h; if (!h) { for (const ch of out) ch.fill(0); return !this.done; } if (Atomics.load(h, AT.STATE) === STATE_LOST) { for (const ch of out) ch.fill(0); if (!this.lost) { this.lost = true; this.port.postMessage({ type: "lost" }); } return true; } const input = inputs[0] || []; const left = input[0] || this.silence; const right = input[1] || left; // The page's transport, for the plugin's ProcessContext (tempo-synced delays and the like). this.tempBits[0] = this.tempo; Atomics.store(h, AT.TEMPO, this.tempInt[0]); Atomics.store(h, AT.PLAYING, this.playing); Atomics.store(h, AT.POSITION_LO, this.position % 4294967296 | 0); Atomics.store(h, AT.POSITION_HI, Math.floor(this.position / 4294967296) | 0); if (this.playing) this.position += n; // In. const w = Atomics.load(h, AT.IN_WRITE); this.put(this.planes[0], w, left, n); this.put(this.planes[1], w, right, n); Atomics.store(h, AT.IN_WRITE, (w + n) | 0); const q = this.quanta++; Atomics.add(h, AT.QUANTA, 1); // Out: what went in `latency` frames ago (offline: what just went in, once the host has it). const due = this.offline ? w : (w - this.latency) | 0; if (!this.offline && q * n < this.latency) { for (const ch of out) ch.fill(0); return true; } let ready = ((Atomics.load(h, AT.OUT_WRITE) - due) | 0) >= n; // A hole the host left when it fell a whole ring behind (ring.rs skip_to_newest): never processed, so an underrun. if (ready && !this.offline) { const start = Atomics.load(h, AT.HOLE_START); const len = (Atomics.load(h, AT.HOLE_END) - start) >>> 0; if (((due - start) >>> 0) < len || ((due + n - 1 - start) >>> 0) < len) ready = false; } if (!ready && this.offline) { const t0 = Date.now(); let spins = 0; while (!ready) { if ((++spins & 1023) === 0 && (Date.now() - t0 > OFFLINE_WAIT_MS || Atomics.load(h, AT.STATE) === STATE_LOST)) break; ready = ((Atomics.load(h, AT.OUT_WRITE) - due) | 0) >= n; } } if (ready) { this.take(this.planes[2], due, out[0], n); if (out[1]) this.take(this.planes[3], due, out[1], n); } else { for (const ch of out) ch.fill(0); this.underruns++; Atomics.add(h, AT.UNDERRUNS, 1); } Atomics.store(h, AT.OUT_READ, (due + n) | 0); if (q % this.reportEvery === 0) { this.port.postMessage({ type: "stats", quanta: this.quanta, underruns: this.underruns, late: Atomics.load(h, AT.LATE), skips: Atomics.load(h, AT.SKIPS), slowestBlockUs: Atomics.load(h, AT.MAX_US), lastBlockUs: Atomics.load(h, AT.LAST_US), pluginLatency: Atomics.load(h, AT.PLUGIN_LATENCY), hostBeat: Atomics.load(h, AT.HOST_BEAT), state: Atomics.load(h, AT.STATE), backlog: (Atomics.load(h, AT.IN_WRITE) - Atomics.load(h, AT.IN_READ)) | 0, }); } return true; }}registerProcessor("juicewrld-plugin", JuicewrldPluginProcessor);