// Ensemble lab: six audio-rate double pendulums with their own physics (pitch, mass and length // ratios, ring time), mixed across the stereo field. A score, kept here on the audio clock so it is // sample-accurate, pushes energy into chosen pendulums at each step; keys on the page push too. import { DoublePendulum } from "../physics/doublePendulum.ts"; import { mulberry32 } from "../core/rng.ts"; import { ENSEMBLE, type EnsembleMsg, type EnsembleSettings, type EnsembleSnap } from "./protocol.ts"; import { DcBlock, kick, signal } from "./voice.ts"; declare const sampleRate: number; declare class AudioWorkletProcessor { readonly port: MessagePort; constructor(); } declare function registerProcessor(name: string, ctor: new () => AudioWorkletProcessor): void; const TRAIL = 192; // bob positions per pendulum in each snapshot const DECIM = 3; // one trail sample every DECIM audio samples const STEPS_PER_CYCLE = 40; // RK4 steps per cycle of a pendulum's faster mode, at least interface V { p: DoublePendulum; dc: DcBlock; trail: Float32Array; struck: number; // samples since the last push } class Ensemble extends AudioWorkletProcessor { private s: EnsembleSettings | null = null; private v: V[] = []; private rand = mulberry32(777); private seed = 0; private clock = 0; // samples since the score began private step = 0; // score steps played private trailW = 0; private count = 0; private sinceSnap = 0; constructor() { super(); for (let i = 0; i < ENSEMBLE; i++) { const p = new DoublePendulum({ m1: 1, m2: 1, l1: 1, l2: 1, g: 1 }); p.controlled = false; this.v.push({ p, dc: new DcBlock(), trail: new Float32Array(2 * TRAIL), struck: 1e9 }); } this.port.onmessage = (e: MessageEvent) => { const m = e.data; if (m.type === "settings") { this.s = m.s; if (m.s.seed !== this.seed) { this.seed = m.s.seed; this.rand = mulberry32(m.s.seed); } for (let i = 0; i < ENSEMBLE; i++) { const cfg = this.v[i].p.cfg, vs = m.s.voices[i]; cfg.m1 = 1; cfg.g = 1; cfg.m2 = vs.m2; cfg.l1 = vs.l1; cfg.l2 = vs.l2; } } else if (m.type === "strike") { const v = this.v[m.i]; if (v && this.s) { kick(v.p, this.s.pulseAdds * v.p.vTop, this.rand); v.struck = 0; } } else if (m.type === "reset") { for (const v of this.v) v.p.y.fill(0); } }; } 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 stepSamples = Math.max(1, s.stepSec * sampleRate), delay = s.startDelay * sampleRate; const dcR = 1 - (2 * Math.PI * s.dcCut) / sampleRate; // per-voice constants for this block const per = this.v.map((v, i) => { const vs = s.voices[i]; const k = s.k; // one time scale: each pendulum's pitch comes from its size const dtS = k / sampleRate; // RK4 is very accurate at ≥ 40 steps per cycle: at audio rate that is usually one per sample const sub = Math.max(1, Math.ceil((STEPS_PER_CYCLE * vs.upperHz) / sampleRate)); const pan = ENSEMBLE > 1 ? s.spread * ((2 * i) / (ENSEMBLE - 1) - 1) : 0; const a = ((pan + 1) * Math.PI) / 4; // equal-power v.p.damping = vs.ringTime > 0 ? (2 * 6.9078) / (vs.ringTime * k) : 0; return { h: dtS / sub, sub, gl: Math.cos(a) * vs.level, gr: Math.sin(a) * vs.level }; }); for (let t = 0; t < n; t++) { // the score: step k falls at delay + k·step if (this.step < s.score.length && this.clock >= delay + this.step * stepSamples) { for (const { i, add } of s.score[this.step]) { const v = this.v[i]; if (!v) continue; kick(v.p, add * v.p.vTop, this.rand); v.struck = 0; } this.step++; } this.clock++; let l = 0, r = 0; for (let i = 0; i < ENSEMBLE; i++) { const v = this.v[i], c = per[i], p = v.p; for (let j = 0; j < c.sub; j++) p.step(c.h); if (!Number.isFinite(p.y[0])) p.y.fill(0); const d = p.derived(); const x = Math.tanh(s.gain * v.dc.run(signal(p, s.signal, d.w1, d.w2), dcR)); l += c.gl * x; r += c.gr * x; } Lo[t] = l; Ro[t] = r; for (const v of this.v) v.struck++; if (++this.count % DECIM === 0) { for (const v of this.v) { const y = v.p.y, cfg = v.p.cfg, o = 2 * this.trailW; v.trail[o] = cfg.l1 * Math.sin(y[0]) + cfg.l2 * Math.sin(y[1]); v.trail[o + 1] = cfg.l1 * Math.cos(y[0]) + cfg.l2 * Math.cos(y[1]); } this.trailW = (this.trailW + 1) % TRAIL; } } this.sinceSnap += n; if (this.sinceSnap >= sampleRate / 30) { this.sinceSnap = 0; const trails = new Float32Array(ENSEMBLE * 2 * TRAIL); this.v.forEach((v, i) => { const o = i * 2 * TRAIL; trails.set(v.trail.subarray(2 * this.trailW), o); trails.set(v.trail.subarray(0, 2 * this.trailW), o + 2 * (TRAIL - this.trailW)); }); const joints = new Float32Array(4 * ENSEMBLE); this.v.forEach((v, i) => { const y = v.p.y, c = v.p.cfg; const x1 = c.l1 * Math.sin(y[0]), y1 = c.l1 * Math.cos(y[0]); joints.set([x1, y1, x1 + c.l2 * Math.sin(y[1]), y1 + c.l2 * Math.cos(y[1])], 4 * i); }); const snap: EnsembleSnap = { type: "snap", joints, struck: this.v.map((v) => v.struck / sampleRate), time: this.clock / sampleRate, step: this.step, energy: this.v.map((v) => v.p.energyNorm()), trails, }; this.port.postMessage(snap, [trails.buffer, joints.buffer]); } return true; } } registerProcessor("pendulum-ensemble", Ensemble);