/ music / double-pendulum
Sign in
music / double-pendulum
double-pendulum / src / compositions / c01-poincare / score.ts
85 lines · 3.5 KBCodeBlameHistory
75ca48aOverhaul playback: background-computed, buffered, one clockJeremy Magland 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. A hum runs throughout; its timbre follows the energy. Energy is
5// steered along an envelope: small → peak → hold → zero. Once the pendulum is at rest, no more
6// crossings register; the last note is held and faded, the hum fades with it, and 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 { HumScore } from "../shared/humScore.ts";
16import { noteFor } from "../shared/noteMap.ts";
17import { HUM, NOTES } from "./layout.ts";
19const DT = 0.002; // simulation step (sim seconds)
20const MAX_SETTLE = 10; // s after duration: count the pendulum as at rest regardless
21const FRAME_DT = 1 / 960;
22const INFO_DT = 0.1;
24export const poincareScore: ScoreFactory = (P: Params, seed: number, sink) => {
25 const pend = new DoublePendulum({ m1: 1, l1: 1, g: 1, m2: P.massRatio as number, l2: P.lengthRatio as number });
26 pend.randomize(energyTarget(P, 0), mulberry32(seed));
27 pend.tau = P.tau as number;
28 const speed = P.speed as number, duration = P.duration as number, restE = P.restEnergy as number;
29 const ring = (P.finalHold as number) + (P.finalFade as number) + 0.3;
30 const hum = new HumScore(P, HUM, sink);
31 const y = pend.y;
33 let time = 0;
34 let prev = wrapAngle(y[0]), prevTh2 = y[1], prevP2 = y[3];
35 let restAt: number | null = null;
36 let lastNote: NoteRef | null = null;
37 let notes = 0;
38 let nextFrame = 0, nextInfo = 0;
40 function step() {
41 pend.eTarget = energyTarget(P, time) * pend.vTop;
42 const t0 = time;
43 pend.step(DT);
44 time += DT / speed;
45 const cur = wrapAngle(y[0]);
46 if (restAt === null && prev < 0 && cur >= 0 && cur - prev < Math.PI) {
47 const f = -prev / (cur - prev);
48 const tc = t0 + f * (time - t0);
49 const th2 = wrapAngle(prevTh2 + f * (y[1] - prevTh2));
50 const p2 = (prevP2 + f * (y[3] - prevP2)) / pend.p2Scale;
51 sink.event({ t: tc, kind: "x", th2, p2 });
52 const { midi, velocity } = noteFor(P, th2, p2);
53 lastNote = sink.note(NOTES, tc, midi, velocity, P.noteLength as number) ?? lastNote;
54 notes++;
55 }
56 if (restAt === null && time >= duration && (pend.energyNorm() < restE || time >= duration + MAX_SETTLE)) {
57 restAt = time;
58 // the last real note rings: sustain, then fade; the piece ends with the fade
59 if (lastNote) sink.hold(lastNote, lastNote.t, P.finalHold as number, P.finalFade as number);
60 sink.end(time + ring);
61 }
62 prev = cur;
63 prevTh2 = y[1];
64 prevP2 = y[3];
65 const e = pend.energyNorm();
66 hum.update(time, e, restAt !== null ? restAt + ring : null);
67 if (time >= nextFrame) {
68 sink.frame(0, time, [y[0], y[1], e]);
69 nextFrame = time + FRAME_DT;
70 }
71 if (time >= nextInfo) {
72 nextInfo = time + INFO_DT;
73 sink.info(time, { "energy target": energyTarget(P, time).toFixed(4), energy: e.toFixed(4), notes: String(notes) });
74 }
75 }
77 return {
78 get time() {
79 return time;
80 },
81 advance(t: number) {
82 while (time < t) step();
83 },
84 };
85};
↑↓ move↵openescclose