1// Composition 1: a double pendulum and its Poincaré section — the score (score worker).
2//
3// Section: θ1 = 0 crossed upward (dθ1/dt > 0); plotted at (θ2, p2). Each crossing plays a note:
4// θ2 → pitch, |p2| → loudness. Energy is steered along an envelope: small → peak → hold → zero.
5// Once the pendulum is at rest, no more crossings register; the last note is held and faded, and
6// the piece ends.
7//
8// Drawing data: track 0 = [θ1, θ2, energy] (960 per piece second); events "x" = crossings.
10import type { Params } from "../../core/params.ts";
11import { mulberry32 } from "../../core/rng.ts";
12import { DoublePendulum, wrapAngle } from "../../physics/doublePendulum.ts";
13import type { NoteRef, ScoreFactory } from "../../stream/score.ts";
14import { energyTarget } from "../shared/form.ts";
15import { noteFor } from "../shared/noteMap.ts";
16import { NOTES } from "./layout.ts";
18const DT = 0.002; // simulation step (sim seconds)
19const MAX_SETTLE = 10; // s after duration: count the pendulum as at rest regardless
20const FRAME_DT = 1 / 960;
21const INFO_DT = 0.1;
23export const poincareScore: ScoreFactory = (P: Params, seed: number, sink) => {
24 const pend = new DoublePendulum({ m1: 1, l1: 1, g: 1, m2: P.massRatio as number, l2: P.lengthRatio as number });
25 pend.randomize(energyTarget(P, 0), mulberry32(seed));
26 pend.tau = P.tau as number;
27 const speed = P.speed as number, duration = P.duration as number, restE = P.restEnergy as number;
28 const ring = (P.finalHold as number) + (P.finalFade as number) + 0.3;
29 const y = pend.y;
31 let time = 0;
32 let prev = wrapAngle(y[0]), prevTh2 = y[1], prevP2 = y[3];
33 let restAt: number | null = null;
34 let lastNote: NoteRef | null = null;
35 let notes = 0;
36 let nextFrame = 0, nextInfo = 0;
38 function step() {
39 pend.eTarget = energyTarget(P, time) * pend.vTop;
40 const t0 = time;
41 pend.step(DT);
42 time += DT / speed;
43 const cur = wrapAngle(y[0]);
44 if (restAt === null && prev < 0 && cur >= 0 && cur - prev < Math.PI) {
45 const f = -prev / (cur - prev);
46 const tc = t0 + f * (time - t0);
47 const th2 = wrapAngle(prevTh2 + f * (y[1] - prevTh2));
48 const p2 = (prevP2 + f * (y[3] - prevP2)) / pend.p2Scale;
49 sink.event({ t: tc, kind: "x", th2, p2 });
50 const { midi, velocity } = noteFor(P, th2, p2);
51 lastNote = sink.note(NOTES, tc, midi, velocity, P.noteLength as number) ?? lastNote;
52 notes++;
53 }
54 if (restAt === null && time >= duration && (pend.energyNorm() < restE || time >= duration + MAX_SETTLE)) {
55 restAt = time;
56 // the last real note rings: sustain, then fade; the piece ends with the fade
57 if (lastNote) sink.hold(lastNote, lastNote.t, P.finalHold as number, P.finalFade as number);
58 sink.end(time + ring);
59 }
60 prev = cur;
61 prevTh2 = y[1];
62 prevP2 = y[3];
63 const e = pend.energyNorm();
64 if (time >= nextFrame) {
65 sink.frame(0, time, [y[0], y[1], e]);
66 nextFrame = time + FRAME_DT;
67 }
68 if (time >= nextInfo) {
69 nextInfo = time + INFO_DT;
70 sink.info(time, { "energy target": energyTarget(P, time).toFixed(4), energy: e.toFixed(4), notes: String(notes) });
71 }
72 }
74 return {
75 get time() {
76 return time;
77 },
78 advance(t: number) {
79 while (time < t) step();
80 },
81 };
82};