// Composition 5: ensemble — the score (score worker). The sound is the pendulums themselves: // six double pendulums simulated at audio rate on one shared time scale (so each one's pitch comes // from its size; proportions solved from musical ratios in harmony.ts), each contributing a state // quantity (by default its outer bob's horizontal position) as its waveform. The score pushes them // in chords ("36 25 … 1!4"); after the last step they ring out and fade. // // Drawing data: track 0 = per pendulum [x1, y1, x2, y2] (120 per piece second); track 1 = per // pendulum [x2, y2] (8000 per piece second, for the bobs' short paths); events "push" (i). import type { Params } from "../../core/params.ts"; import { mulberry32 } from "../../core/rng.ts"; import { DcBlock, kick, signal } from "../../lab/voice.ts"; import type { Signal } from "../../lab/protocol.ts"; import { cos, exp, sin, tanh } from "../../physics/dmath.ts"; import { DoublePendulum } from "../../physics/doublePendulum.ts"; import { SAMPLE_RATE } from "../../stream/protocol.ts"; import type { ScoreFactory } from "../../stream/score.ts"; import { build, parseRatio, timeScale, type Built } from "./harmony.ts"; import { START_DELAY, parseScore, scoreEnd, stepSec } from "./layout.ts"; export const N = 6; const STEPS_PER_CYCLE = 40; // RK4 steps per cycle of a pendulum's faster mode, at least const JOINTS_EVERY = SAMPLE_RATE / 120; const BOBS_EVERY = 6; // samples between rows of the bobs' paths (8 kHz) /** Physics for every pendulum from the musical ratios: one time scale, sizes and masses solved. */ export function physics(P: Params): { k: number; built: Built[] } { const base = P.basePitch as number; const v = Array.from({ length: N }, (_, i) => ({ ratio: parseRatio(P[`ratio${i + 1}`] as string), partial: parseRatio(P[`partial${i + 1}`] as string), shape: P[`shape${i + 1}`] as number, })); // the lowest-pitched pendulum is the biggest: give it l1 = 1 const low = v.reduce((a, b) => (b.ratio < a.ratio ? b : a)); const k = timeScale(base, low.ratio, low.partial, low.shape); return { k, built: v.map((x) => build(base, x.ratio, x.partial, x.shape, k)) }; } export const ensembleScore: ScoreFactory = (P: Params, seed: number, sink) => { const { k, built } = physics(P); const rand = mulberry32(seed); const sig = P.signal as Signal; const gain = exp((P.gain as number) * 0.11512925464970229); // dB → linear const dcR = 1 - (2 * Math.PI * (P.dcCut as number)) / SAMPLE_RATE; const steps = parseScore(P.score as string, P.pulseAdds as number, P.accent as number); const stepS = stepSec(P); const end = scoreEnd(P) + (P.tail as number); const fadeLen = Math.min(P.fade as number, P.tail as number); const dtS = k / SAMPLE_RATE; const voices = built.map((b, i) => { const p = new DoublePendulum({ m1: 1, m2: b.m2, l1: b.l1, l2: b.l2, g: 1 }); p.controlled = false; const ring = P[`ring${i + 1}`] as number; p.damping = ring > 0 ? (2 * 6.9078) / (ring * k) : 0; const sub = Math.max(1, Math.ceil((STEPS_PER_CYCLE * b.upper) / SAMPLE_RATE)); const pan = P.spread as number * ((2 * i) / (N - 1) - 1); const a = ((pan + 1) * Math.PI) / 4; // equal-power const level = exp((P[`level${i + 1}`] as number) * 0.11512925464970229); return { p, dc: new DcBlock(), sub, h: dtS / sub, gl: cos(a) * level, gr: sin(a) * level }; }); let sample = 0; // samples simulated let step = 0; // score steps played let ended = false; const joints = new Float64Array(4 * N), bobs = new Float64Array(2 * N); function advance(t: number) { if (!ended) { ended = true; sink.end(end); } const target = Math.round(t * SAMPLE_RATE); const n = target - sample; if (n <= 0) return; const L = new Float32Array(n), R = new Float32Array(n); for (let j = 0; j < n; j++, sample++) { const time = sample / SAMPLE_RATE; // the score: step k falls at START_DELAY + k·step if (step < steps.length && time >= START_DELAY + step * stepS) { for (const push of steps[step]) { const v = voices[push.i]; if (!v) continue; kick(v.p, push.add * v.p.vTop, rand); sink.event({ t: time, kind: "push", i: push.i }); } step++; } let l = 0, r = 0; for (const v of voices) { for (let s = 0; s < v.sub; s++) v.p.step(v.h); if (!Number.isFinite(v.p.y[0])) v.p.y.fill(0); const d = v.p.derived(); const x = tanh(gain * v.dc.run(signal(v.p, sig, d.w1, d.w2), dcR)); l += v.gl * x; r += v.gr * x; } // the end: fade over the last `fadeLen` s const u = Math.max(0, Math.min(1, (end - time) / fadeLen)); L[j] = l * u * u; R[j] = r * u * u; if (sample % BOBS_EVERY === 0) { voices.forEach((v, i) => { const y = v.p.y, c = v.p.cfg; bobs[2 * i] = c.l1 * sin(y[0]) + c.l2 * sin(y[1]); bobs[2 * i + 1] = c.l1 * cos(y[0]) + c.l2 * cos(y[1]); }); sink.frame(1, time, bobs); } if (sample % JOINTS_EVERY === 0) { voices.forEach((v, i) => { const y = v.p.y, c = v.p.cfg; const x1 = c.l1 * sin(y[0]), y1 = c.l1 * cos(y[0]); joints.set([x1, y1, x1 + c.l2 * sin(y[1]), y1 + c.l2 * cos(y[1])], 4 * i); }); sink.frame(0, time, joints); sink.info(time, { score: `step ${step} of ${steps.length}`, energies: voices.map((v) => v.p.energyNorm().toFixed(2)).join(" ") }); } } sink.audio(0, L, R); } return { get time() { return sample / SAMPLE_RATE; }, advance, }; };