1// Composition 2: orbits — the score (score worker).
2//
3// Several double pendulums at once, all steered along the same energy envelope from random
4// starting states. Each orbit's crossings play its own instrument (orbit n uses slot n, cycling)
5// in its own register. Each orbit plays until it comes to rest; its last
6// note rings out, and the piece ends when every orbit has rested and faded.
7//
8// Drawing data: track 0 = [mean energy, max energy, then per orbit θ1, θ2, p1, p2, active]
9// (30 per piece second, for the zoom and the portrait's traces); events "x" = crossings (o = orbit).
11import type { Params } from "../../core/params.ts";
12import { mulberry32 } from "../../core/rng.ts";
13import { DoublePendulum, wrapAngle } from "../../physics/doublePendulum.ts";
14import type { NoteRef, ScoreFactory } from "../../stream/score.ts";
15import { energyTarget } from "../shared/form.ts";
16import { noteFor } from "../shared/noteMap.ts";
17import { SLOTS, orbitCount, orbitStems } from "./layout.ts";
19const DT = 0.003; // simulation step (sim seconds)
20const MAX_SETTLE = 10; // s after duration: count every orbit as at rest regardless
21const FRAME_DT = 1 / 30;
22const INFO_DT = 0.1;
24interface Orbit {
25 pend: DoublePendulum;
26 prev: number;
27 prevTh2: number;
28 prevP2: number;
29 lastNote: NoteRef | null;
30 restAt: number | null;
31 stem: number;
32 octave: number;
33}
35export const orbitsScore: ScoreFactory = (P: Params, seed: number, sink) => {
36 const speed = P.speed as number, duration = P.duration as number, restE = P.restEnergy as number;
37 const ring = (P.finalHold as number) + (P.finalFade as number) + 0.3;
38 const { stemOf } = orbitStems(P);
39 const e0 = Math.max(1e-4, energyTarget(P, 0));
40 const orbits: Orbit[] = Array.from({ length: orbitCount(P) }, (_, i) => {
41 const pend = new DoublePendulum({ m1: 1, l1: 1, g: 1, m2: P.massRatio as number, l2: P.lengthRatio as number });
42 pend.randomize(e0, mulberry32(seed * 1009 + i * 7919 + 1));
43 pend.tau = P.tau as number;
44 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 };
45 });
47 let time = 0;
48 let restAll: number | null = null;
49 let notes = 0;
50 let nextFrame = 0, nextInfo = 0;
51 const row: number[] = [];
53 function step() {
54 const eT = energyTarget(P, time);
55 const t0 = time;
56 time += DT / speed;
57 orbits.forEach((o, i) => {
58 const pend = o.pend, y = pend.y;
59 pend.eTarget = eT * pend.vTop;
60 pend.step(DT);
61 const cur = wrapAngle(y[0]);
62 if (o.restAt === null && o.prev < 0 && cur >= 0 && cur - o.prev < Math.PI) {
63 const f = -o.prev / (cur - o.prev);
64 const tc = t0 + f * (time - t0);
65 const th2 = wrapAngle(o.prevTh2 + f * (y[1] - o.prevTh2));
66 const p2 = (o.prevP2 + f * (y[3] - o.prevP2)) / pend.p2Scale;
67 sink.event({ t: tc, kind: "x", o: i, th2, p2 });
68 const { midi, velocity } = noteFor(P, th2, p2, o.octave);
69 o.lastNote = sink.note(o.stem, tc, midi, velocity, P.noteLength as number) ?? o.lastNote;
70 notes++;
71 }
72 if (o.restAt === null && time >= duration && (pend.energyNorm() < restE || time >= duration + MAX_SETTLE)) {
73 o.restAt = time;
74 // this orbit's last real note rings: sustain, then fade
75 if (o.lastNote) sink.hold(o.lastNote, o.lastNote.t, P.finalHold as number, P.finalFade as number);
76 }
77 o.prev = cur;
78 o.prevTh2 = y[1];
79 o.prevP2 = y[3];
80 });
81 if (restAll === null && orbits.every((o) => o.restAt !== null)) {
82 restAll = time;
83 sink.end(time + ring);
84 }
85 let sum = 0, max = 0;
86 for (const o of orbits) {
87 const e = o.pend.energyNorm();
88 sum += e;
89 max = Math.max(max, e);
90 }
91 const mean = sum / orbits.length;
92 if (time >= nextFrame) {
93 nextFrame = time + FRAME_DT;
94 row.length = 0;
95 row.push(mean, max);
96 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);
97 sink.frame(0, time, row);
98 }
99 if (time >= nextInfo) {
100 nextInfo = time + INFO_DT;
101 sink.info(time, {
102 "energy target": eT.toFixed(4),
103 "energy (mean)": mean.toFixed(4),
104 notes: String(notes),
105 "at rest": `${orbits.filter((o) => o.restAt !== null).length} / ${orbits.length}`,
106 });
107 }
108 }
110 return {
111 get time() {
112 return time;
113 },
114 advance(t: number) {
115 while (time < t) step();
116 },
117 };
118};