// Ensemble lab: six audio-rate double pendulums with their own physics (pitch, mass and length // ratios, ring time), mixed across the stereo field. A steady tempo, kept here on the audio clock, // pushes energy into pendulums 1…k on every beat; k is set by the page (keys 0–6 or a slider) or // grows by one every `build` beats. 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, lowMode, 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 MAX_STEP = 0.006; // sim seconds per RK4 step (audio rate is plenty fine) interface V { p: DoublePendulum; dc: DcBlock; trail: Float32Array; } class Ensemble extends AudioWorkletProcessor { private s: EnsembleSettings | null = null; private v: V[] = []; private rand = mulberry32(777); private k = 0; // active pendulums private lastActiveSetting = -1; private beat = 0; private sinceBeat = 1e9; // samples 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) }); } this.port.onmessage = (e: MessageEvent) => { const m = e.data; if (m.type === "settings") { this.s = m.s; for (let i = 0; i < ENSEMBLE; i++) { this.v[i].p.cfg.m2 = m.s.voices[i].massRatio; this.v[i].p.cfg.l2 = m.s.voices[i].lengthRatio; } // the slider sets k when it moves; keys (below) set it directly if (m.s.active !== this.lastActiveSetting) { this.lastActiveSetting = m.s.active; this.k = m.s.active; } } else if (m.type === "active") this.k = Math.max(0, Math.min(ENSEMBLE, m.k)); else if (m.type === "reset") { for (const v of this.v) v.p.y.fill(0); this.beat = 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 beatSamples = (60 / Math.max(1, s.tempo)) * 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 = (2 * Math.PI * vs.pitch) / lowMode(v.p); // sim seconds per audio second const dtS = k / sampleRate; const sub = Math.max(1, Math.ceil(dtS / MAX_STEP)); 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 beat: push energy into pendulums 1…k if (++this.sinceBeat >= beatSamples) { this.sinceBeat = 0; this.beat++; if (s.build > 0 && this.beat % Math.round(s.build) === 0 && this.k < ENSEMBLE) this.k++; for (let i = 0; i < this.k; i++) kick(this.v[i].p, s.pulseAdds * this.v[i].p.vTop, this.rand); } 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; 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 snap: EnsembleSnap = { type: "snap", active: this.k, beat: this.beat, sinceBeat: this.sinceBeat / sampleRate, energy: this.v.map((v) => v.p.energyNorm()), trails, }; this.port.postMessage(snap, [trails.buffer]); } return true; } } registerProcessor("pendulum-ensemble", Ensemble);