// The double pendulum as an instrument: simulated at audio rate inside the AudioWorklet, its // motion is the waveform. Time is scaled so the pendulum's lower small-swing mode lands at // `pitch` Hz. Modes: strike (start at an energy, ring down through damping, re-strike), // sustain (an energy controller holds the energy, like bowing), swell (energy rises, then rings // down). Any state quantity can be the left/right output. import { DoublePendulum } from "../physics/doublePendulum.ts"; import { mulberry32 } from "../core/rng.ts"; import type { LabMsg, LabSettings, LabSnap, Signal } from "./protocol.ts"; import { kick, lowMode, signal } from "./voice.ts"; declare const sampleRate: number; declare const currentTime: number; declare class AudioWorkletProcessor { readonly port: MessagePort; constructor(); } declare function registerProcessor(name: string, ctor: new () => AudioWorkletProcessor): void; const TRAIL = 384; // samples of bob path sent per snapshot (decimated) const DECIM = 2; class PendulumVoice extends AudioWorkletProcessor { private s: LabSettings | null = null; private p = new DoublePendulum({ m1: 1, m2: 1, l1: 1, l2: 1, g: 1 }); private rand = mulberry32(12345); private t = 0; // audio seconds since the last strike/swell private swellFrom = 0; // energy (normalized) when the current swell began private phase = "silent"; private dcL = { x: 0, y: 0 }; private dcR = { x: 0, y: 0 }; private trail = new Float32Array(4 * TRAIL); private trailW = 0; private sinceSnap = 0; private sampleCount = 0; constructor() { super(); this.p.controlled = false; this.port.onmessage = (e: MessageEvent) => { if (e.data.type === "settings") { const first = !this.s; this.s = e.data.s; this.p.cfg.m2 = this.s.massRatio; this.p.cfg.l2 = this.s.lengthRatio; if (first) { // start hanging at rest; the first strike sets it going this.p.y.fill(0); this.strike(); } } else if (e.data.type === "strike") this.strike(); }; } /** * A strike adds energy to the current motion (it never restarts): a momentum kick along the * current momentum (like pushing a swing in time, so it always adds), or in a random direction * if the pendulum is still, sized so the energy rises by exactly `strikeEnergy`. */ private strike() { const s = this.s; if (!s) return; this.t = 0; if (s.mode === "swell") { this.swellFrom = this.p.energyNorm(); this.phase = "swelling"; if (this.swellFrom < 1e-6) this.kick(1e-4 * this.p.vTop); // something to swell from return; } this.kick(s.strikeEnergy * this.p.vTop); } private kick(add: number) { kick(this.p, add, this.rand); } private signal(sig: Signal, w1: number, w2: number): number { return signal(this.p, sig, w1, w2); } process(_in: Float32Array[][], outputs: Float32Array[][]): boolean { const out = outputs[0]; const Lo = out[0], Ro = out[1] ?? out[0]; const s = this.s; if (!s) return true; const n = Lo.length; const p = this.p; const w0 = lowMode(p); const k = (2 * Math.PI * s.pitch) / w0; // sim seconds per audio second const dtSample = k / sampleRate; const sub = Math.max(1, Math.ceil(dtSample / 0.004)); const h = dtSample / sub; // ring time (amplitude −60 dB in s audio) → viscous damping rate (energy ∝ e^{−γ t_sim}) const gamma = s.ringTime > 0 ? (2 * 6.9078) / (s.ringTime * k) : 0; const dcR = 1 - (2 * Math.PI * s.dcCut) / sampleRate; const period = (2 * Math.PI) / w0; // sim seconds p.tau = Math.max(0.05, s.follow * period); for (let i = 0; i < n; i++) { // mode logic (audio time since the last strike) if (s.mode === "sustain") { this.phase = "sustaining"; p.controlled = true; p.eTarget = s.energy * p.vTop; p.damping = 0; } else if (s.mode === "swell") { if (this.t < s.swellTime) { p.controlled = true; const u = this.t / Math.max(0.01, s.swellTime); p.eTarget = (this.swellFrom + (s.energy - this.swellFrom) * u * u) * p.vTop; p.damping = 0; this.phase = "swelling"; } else { p.controlled = false; p.damping = gamma; this.phase = "ringing"; } } else { p.controlled = false; p.damping = gamma; this.phase = "ringing"; } for (let j = 0; j < sub; j++) p.step(h); this.t += 1 / sampleRate; if (s.restrike > 0 && this.t >= s.restrike) this.strike(); const d = p.derived(); let l = this.signal(s.left, d.w1, d.w2), r = s.right === s.left ? l : this.signal(s.right, d.w1, d.w2); // remove constant offsets (angles and positions sit off zero), then gain and a soft limit const yl = l - this.dcL.x + dcR * this.dcL.y; this.dcL.x = l; this.dcL.y = yl; const yr = r - this.dcR.x + dcR * this.dcR.y; this.dcR.x = r; this.dcR.y = yr; l = Math.tanh(s.gain * yl); r = Math.tanh(s.gain * yr); Lo[i] = Number.isFinite(l) ? l : 0; Ro[i] = Number.isFinite(r) ? r : 0; if (!Number.isFinite(p.y[0])) this.strike(); // never let a blow-up stick // bob path for the display if (++this.sampleCount % DECIM === 0) { const y = p.y, cfg = p.cfg, o = 4 * this.trailW; const x1 = cfg.l1 * Math.sin(y[0]), y1 = cfg.l1 * Math.cos(y[0]); this.trail[o] = x1; this.trail[o + 1] = y1; this.trail[o + 2] = x1 + cfg.l2 * Math.sin(y[1]); this.trail[o + 3] = y1 + cfg.l2 * Math.cos(y[1]); this.trailW = (this.trailW + 1) % TRAIL; } } this.sinceSnap += n; if (this.sinceSnap >= sampleRate / 60) { this.sinceSnap = 0; // oldest → newest const tr = new Float32Array(4 * TRAIL); tr.set(this.trail.subarray(4 * this.trailW)); tr.set(this.trail.subarray(0, 4 * this.trailW), 4 * (TRAIL - this.trailW)); const snap: LabSnap = { type: "snap", energy: p.energyNorm(), phase: this.phase, trail: tr, k }; this.port.postMessage(snap, [tr.buffer]); } void currentTime; return true; } } registerProcessor("pendulum-voice", PendulumVoice);