/ music / double-pendulum
Sign in
music / double-pendulum
double-pendulum / src / compositions / c02-orbits / score.ts
121 lines · 4.6 KBCodeBlameHistory
75ca48aOverhaul playback: background-computed, buffered, one clockJeremy Magland 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. A hum follows the energy. 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 { HumScore } from "../shared/humScore.ts";
17import { noteFor } from "../shared/noteMap.ts";
18import { SLOTS, humStem, orbitCount, orbitStems } from "./layout.ts";
20const DT = 0.003; // simulation step (sim seconds)
21const MAX_SETTLE = 10; // s after duration: count every orbit as at rest regardless
22const FRAME_DT = 1 / 30;
23const INFO_DT = 0.1;
25interface Orbit {
26 pend: DoublePendulum;
27 prev: number;
28 prevTh2: number;
29 prevP2: number;
30 lastNote: NoteRef | null;
31 restAt: number | null;
32 stem: number;
33 octave: number;
36export const orbitsScore: ScoreFactory = (P: Params, seed: number, sink) => {
37 const speed = P.speed as number, duration = P.duration as number, restE = P.restEnergy as number;
38 const ring = (P.finalHold as number) + (P.finalFade as number) + 0.3;
39 const { stemOf } = orbitStems(P);
40 const hum = new HumScore(P, humStem(P), sink);
41 const e0 = Math.max(1e-4, energyTarget(P, 0));
42 const orbits: Orbit[] = Array.from({ length: orbitCount(P) }, (_, i) => {
43 const pend = new DoublePendulum({ m1: 1, l1: 1, g: 1, m2: P.massRatio as number, l2: P.lengthRatio as number });
44 pend.randomize(e0, mulberry32(seed * 1009 + i * 7919 + 1));
45 pend.tau = P.tau as number;
46 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 };
47 });
49 let time = 0;
50 let restAll: number | null = null;
51 let notes = 0;
52 let nextFrame = 0, nextInfo = 0;
53 const row: number[] = [];
55 function step() {
56 const eT = energyTarget(P, time);
57 const t0 = time;
58 time += DT / speed;
59 orbits.forEach((o, i) => {
60 const pend = o.pend, y = pend.y;
61 pend.eTarget = eT * pend.vTop;
62 pend.step(DT);
63 const cur = wrapAngle(y[0]);
64 if (o.restAt === null && o.prev < 0 && cur >= 0 && cur - o.prev < Math.PI) {
65 const f = -o.prev / (cur - o.prev);
66 const tc = t0 + f * (time - t0);
67 const th2 = wrapAngle(o.prevTh2 + f * (y[1] - o.prevTh2));
68 const p2 = (o.prevP2 + f * (y[3] - o.prevP2)) / pend.p2Scale;
69 sink.event({ t: tc, kind: "x", o: i, th2, p2 });
70 const { midi, velocity } = noteFor(P, th2, p2, o.octave);
71 o.lastNote = sink.note(o.stem, tc, midi, velocity, P.noteLength as number) ?? o.lastNote;
72 notes++;
73 }
74 if (o.restAt === null && time >= duration && (pend.energyNorm() < restE || time >= duration + MAX_SETTLE)) {
75 o.restAt = time;
76 // this orbit's last real note rings: sustain, then fade
77 if (o.lastNote) sink.hold(o.lastNote, o.lastNote.t, P.finalHold as number, P.finalFade as number);
78 }
79 o.prev = cur;
80 o.prevTh2 = y[1];
81 o.prevP2 = y[3];
82 });
83 if (restAll === null && orbits.every((o) => o.restAt !== null)) {
84 restAll = time;
85 sink.end(time + ring);
86 }
87 let sum = 0, max = 0;
88 for (const o of orbits) {
89 const e = o.pend.energyNorm();
90 sum += e;
91 max = Math.max(max, e);
92 }
93 const mean = sum / orbits.length;
94 hum.update(time, mean, restAll !== null ? restAll + ring : null);
95 if (time >= nextFrame) {
96 nextFrame = time + FRAME_DT;
97 row.length = 0;
98 row.push(mean, max);
99 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);
100 sink.frame(0, time, row);
101 }
102 if (time >= nextInfo) {
103 nextInfo = time + INFO_DT;
104 sink.info(time, {
105 "energy target": eT.toFixed(4),
106 "energy (mean)": mean.toFixed(4),
107 notes: String(notes),
108 "at rest": `${orbits.filter((o) => o.restAt !== null).length} / ${orbits.length}`,
109 });
110 }
111 }
113 return {
114 get time() {
115 return time;
116 },
117 advance(t: number) {
118 while (time < t) step();
119 },
120 };
121};
↑↓ move↵openescclose