// Composition 2: orbits — the score (score worker). // // Several double pendulums at once, all steered along the same energy envelope from random // starting states. Each orbit's crossings play its own instrument (orbit n uses slot n, cycling) // in its own register. Each orbit plays until it comes to rest; its last // note rings out, and the piece ends when every orbit has rested and faded. // // Drawing data: track 0 = [mean energy, max energy, then per orbit θ1, θ2, p1, p2, active] // (30 per piece second, for the zoom and the portrait's traces); events "x" = crossings (o = orbit). import type { Params } from "../../core/params.ts"; import { mulberry32 } from "../../core/rng.ts"; import { DoublePendulum, wrapAngle } from "../../physics/doublePendulum.ts"; import type { NoteRef, ScoreFactory } from "../../stream/score.ts"; import { energyTarget } from "../shared/form.ts"; import { noteFor } from "../shared/noteMap.ts"; import { SLOTS, orbitCount, orbitStems } from "./layout.ts"; const DT = 0.003; // simulation step (sim seconds) const MAX_SETTLE = 10; // s after duration: count every orbit as at rest regardless const FRAME_DT = 1 / 30; const INFO_DT = 0.1; interface Orbit { pend: DoublePendulum; prev: number; prevTh2: number; prevP2: number; lastNote: NoteRef | null; restAt: number | null; stem: number; octave: number; } export const orbitsScore: ScoreFactory = (P: Params, seed: number, sink) => { const speed = P.speed as number, duration = P.duration as number, restE = P.restEnergy as number; const ring = (P.finalHold as number) + (P.finalFade as number) + 0.3; const { stemOf } = orbitStems(P); const e0 = Math.max(1e-4, energyTarget(P, 0)); const orbits: Orbit[] = Array.from({ length: orbitCount(P) }, (_, i) => { const pend = new DoublePendulum({ m1: 1, l1: 1, g: 1, m2: P.massRatio as number, l2: P.lengthRatio as number }); pend.randomize(e0, mulberry32(seed * 1009 + i * 7919 + 1)); pend.tau = P.tau as number; return { pend, prev: wrapAngle(pend.y[0]), prevTh2: pend.y[1], prevP2: pend.y[3], lastNote: null, restAt: null, stem: stemOf[i], octave: P[`slot${(i % SLOTS) + 1}Octave`] as number }; }); let time = 0; let restAll: number | null = null; let notes = 0; let nextFrame = 0, nextInfo = 0; const row: number[] = []; function step() { const eT = energyTarget(P, time); const t0 = time; time += DT / speed; orbits.forEach((o, i) => { const pend = o.pend, y = pend.y; pend.eTarget = eT * pend.vTop; pend.step(DT); const cur = wrapAngle(y[0]); if (o.restAt === null && o.prev < 0 && cur >= 0 && cur - o.prev < Math.PI) { const f = -o.prev / (cur - o.prev); const tc = t0 + f * (time - t0); const th2 = wrapAngle(o.prevTh2 + f * (y[1] - o.prevTh2)); const p2 = (o.prevP2 + f * (y[3] - o.prevP2)) / pend.p2Scale; sink.event({ t: tc, kind: "x", o: i, th2, p2 }); const { midi, velocity } = noteFor(P, th2, p2, o.octave); o.lastNote = sink.note(o.stem, tc, midi, velocity, P.noteLength as number) ?? o.lastNote; notes++; } if (o.restAt === null && time >= duration && (pend.energyNorm() < restE || time >= duration + MAX_SETTLE)) { o.restAt = time; // this orbit's last real note rings: sustain, then fade if (o.lastNote) sink.hold(o.lastNote, o.lastNote.t, P.finalHold as number, P.finalFade as number); } o.prev = cur; o.prevTh2 = y[1]; o.prevP2 = y[3]; }); if (restAll === null && orbits.every((o) => o.restAt !== null)) { restAll = time; sink.end(time + ring); } let sum = 0, max = 0; for (const o of orbits) { const e = o.pend.energyNorm(); sum += e; max = Math.max(max, e); } const mean = sum / orbits.length; if (time >= nextFrame) { nextFrame = time + FRAME_DT; row.length = 0; row.push(mean, max); for (const o of orbits) row.push(o.pend.y[0], o.pend.y[1], o.pend.y[2], o.pend.y[3], o.restAt === null ? 1 : 0); sink.frame(0, time, row); } if (time >= nextInfo) { nextInfo = time + INFO_DT; sink.info(time, { "energy target": eT.toFixed(4), "energy (mean)": mean.toFixed(4), notes: String(notes), "at rest": `${orbits.filter((o) => o.restAt !== null).length} / ${orbits.length}`, }); } } return { get time() { return time; }, advance(t: number) { while (time < t) step(); }, }; };