// Composition 3: divergence — the score (score worker). // // N double pendulums released together from rest at a wide angle, each outer arm offset by a tiny, // calibrated amount, so they swing as one and then visibly separate at about `separateAt` seconds, // and chaos takes over. A note sounds whenever a bottom bob crosses the vertical through the pivot // (either direction): in "chord" mode each pendulum has its own note of a chord (two interleaved // chords by direction); in "motion" mode the pitch follows the bob's speed. Notes go to the side // the bob is heading. After `freeTime` the pendulums are damped to rest over `dampTime`; each one's // last note rings out and the piece ends with the fade. // // Drawing data: track 0 = [θ1, θ2] per pendulum (120 per piece second). import type { Params } from "../../core/params.ts"; import { mulberry32 } from "../../core/rng.ts"; import { NOTE_NAMES, quantize } from "../../music/scales.ts"; import { cos, hypot, pow, sin } from "../../physics/dmath.ts"; import type { DoublePendulum } from "../../physics/doublePendulum.ts"; import type { NoteRef, ScoreFactory } from "../../stream/score.ts"; import { LEFT, RIGHT } from "./layout.ts"; import { DT, bob, offsetForSeparation, released } from "./release.ts"; const MAX_SETTLE = 10; // s after the damping: count every pendulum as at rest regardless const FRAME_DT = 1 / 120; const INFO_DT = 0.1; interface Pend { p: DoublePendulum; prevX: number; E0: number; lastNote: NoteRef | null; restAt: number | null; } /** Release angles: the nominal angle, varied per play by up to `jitter` (null rand: nominal). */ export function releaseAngles(P: Params, rand: (() => number) | null): [number, number] { const deg = Math.PI / 180; const base = (P.releaseAngle as number) * deg, j = (P.jitter as number) * deg; return rand ? [base + (2 * rand() - 1) * j, base + (2 * rand() - 1) * j] : [base, base]; } /** * The chord pitches per pendulum, low to high, from the scale's notes in range (an even spread * if continuous). With two chords, 2n notes are spread over the range and alternate between * them: left-to-right crossings play one, right-to-left the other (its neighbour above). */ function chords(P: Params, n: number): { right: number[]; left: number[] } { const low = P.pitchLow as number, high = P.pitchHigh as number; const root = NOTE_NAMES.indexOf(P.root as string); const two = !!P.directionChords; const k = two ? 2 * n : n; let notes: number[] = []; if (P.scale === "continuous") notes = Array.from({ length: Math.max(k, 2) }, (_, i) => low + ((high - low) * i) / Math.max(1, Math.max(k, 2) - 1)); else for (let m = Math.ceil(low); m <= high; m++) if (Math.abs(quantize(m, P.scale as string, root) - m) < 1e-6) notes.push(m); if (!notes.length) notes = [quantize((low + high) / 2, P.scale as string, root)]; const spread = Array.from({ length: k }, (_, i) => notes[k > 1 ? Math.round((i * (notes.length - 1)) / (k - 1)) : Math.floor(notes.length / 2)]); if (!two) return { right: spread, left: spread }; return { right: spread.filter((_, i) => i % 2 === 0), left: spread.filter((_, i) => i % 2 === 1) }; } export const divergenceScore: ScoreFactory = (P: Params, seed: number, sink) => { const cfg = { m1: 1, l1: 1, g: 1, m2: P.massRatio as number, l2: P.lengthRatio as number }; const speed = P.speed as number; const [th1, th2] = releaseAngles(P, mulberry32(seed)); const cal = offsetForSeparation(cfg, th1, th2, (P.separateAt as number) * speed, (P.sepThreshold as number) * (cfg.l1 + cfg.l2)); const n = Math.max(1, Math.round(P.count as number)); const pends: Pend[] = Array.from({ length: n }, (_, i) => { const p = released(cfg, th1, th2, n > 1 ? (i / (n - 1) - 0.5) * cal.offset : 0); return { p, prevX: bob(cfg, p.y)[0], E0: 0, lastNote: null, restAt: null }; }); const vmax0 = Math.sqrt((2 * pends[0].p.energy()) / cfg.m2); const free = P.freeTime as number, damp = P.dampTime as number; const curve = P.dampCurve as number, tau = P.tau as number, restE = P.restEnergy as number; const ring = (P.finalHold as number) + (P.finalFade as number) + 0.3; const chord = chords(P, n); const deg = 180 / Math.PI; const releaseInfo = `${(th1 * deg).toFixed(2)}°, ${(th2 * deg).toFixed(2)}°`; const offsetInfo = `${cal.offset.toExponential(2)} rad → separate at ${(cal.separatesAt / speed).toFixed(1)} s`; let time = 0; let restAll: number | null = null; let notes = 0; let nextFrame = 0, nextInfo = 0; const row = new Float64Array(2 * n); function note(i: number, tc: number, dir: number) { const d = pends[i], y = d.p.y, w = d.p.derived(); // bottom bob's speed at the crossing const vx = cfg.l1 * cos(y[0]) * w.w1 + cfg.l2 * cos(y[1]) * w.w2; const vy = cfg.l1 * sin(y[0]) * w.w1 + cfg.l2 * sin(y[1]) * w.w2; const x = Math.min(1, hypot(vx, vy) / ((P.loudRef as number) * vmax0)); const floor = P.loudFloor as number; const velocity = floor + (1 - floor) * pow(x, P.loudCurve as number); const midi = P.pitchMode === "motion" ? quantize((P.pitchLow as number) + ((P.pitchHigh as number) - (P.pitchLow as number)) * x, P.scale as string, NOTE_NAMES.indexOf(P.root as string)) : chord[dir > 0 ? "right" : "left"][i]; d.lastNote = sink.note(dir > 0 ? RIGHT : LEFT, tc, midi, velocity, P.noteLength as number) ?? d.lastNote; notes++; } function step() { const t0 = time; time += DT / speed; const u = (t0 - free) / damp; // damping progress (< 0: free swinging) pends.forEach((d, i) => { const p = d.p; if (u >= 0) { if (!p.controlled) { p.controlled = true; d.E0 = p.energy(); } p.tau = tau; p.eTarget = u < 1 ? d.E0 * pow(1 - u, curve) : 0; // the energy controller fades out near zero energy; real friction settles the last of it p.damping = u >= 1 ? 1 / tau : 0; } p.step(DT); const x = bob(cfg, p.y)[0]; if (d.restAt === null && ((d.prevX < 0 && x >= 0) || (d.prevX > 0 && x <= 0))) note(i, t0 + (d.prevX / (d.prevX - x)) * (time - t0), x > d.prevX ? 1 : -1); d.prevX = x; if (d.restAt === null && u >= 1 && (p.energyNorm() < restE || time >= free + damp + MAX_SETTLE)) { d.restAt = time; if (d.lastNote) sink.hold(d.lastNote, d.lastNote.t, P.finalHold as number, P.finalFade as number); } }); if (restAll === null && pends.every((d) => d.restAt !== null)) { restAll = time; sink.end(time + ring); } if (time >= nextFrame) { nextFrame = time + FRAME_DT; pends.forEach((d, i) => { row[2 * i] = d.p.y[0]; row[2 * i + 1] = d.p.y[1]; }); sink.frame(0, time, row); } if (time >= nextInfo) { nextInfo = time + INFO_DT; const phase = restAll !== null ? "at rest" : time < free ? "swinging" : time < free + damp ? "damping" : "settling"; sink.info(time, { phase, "energy (mean)": (pends.reduce((s, d) => s + d.p.energyNorm(), 0) / n).toFixed(4), notes: String(notes), release: releaseInfo, "offset (outermost)": offsetInfo, "at rest": `${pends.filter((d) => d.restAt !== null).length} / ${n}`, }); } } return { get time() { return time; }, advance(t: number) { while (time < t) step(); }, }; };