// Composition 1: a double pendulum and its Poincaré section — the score (score worker). // // Section: θ1 = 0 crossed upward (dθ1/dt > 0); plotted at (θ2, p2). Each crossing plays a note: // θ2 → pitch, |p2| → loudness. A hum runs throughout; its timbre follows the energy. Energy is // steered along an envelope: small → peak → hold → zero. Once the pendulum is at rest, no more // crossings register; the last note is held and faded, the hum fades with it, and the piece ends. // // Drawing data: track 0 = [θ1, θ2, energy] (960 per piece second); events "x" = crossings. 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 { HumScore } from "../shared/humScore.ts"; import { noteFor } from "../shared/noteMap.ts"; import { HUM, NOTES } from "./layout.ts"; const DT = 0.002; // simulation step (sim seconds) const MAX_SETTLE = 10; // s after duration: count the pendulum as at rest regardless const FRAME_DT = 1 / 960; const INFO_DT = 0.1; export const poincareScore: ScoreFactory = (P: Params, seed: number, sink) => { const pend = new DoublePendulum({ m1: 1, l1: 1, g: 1, m2: P.massRatio as number, l2: P.lengthRatio as number }); pend.randomize(energyTarget(P, 0), mulberry32(seed)); pend.tau = P.tau as number; 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 hum = new HumScore(P, HUM, sink); const y = pend.y; let time = 0; let prev = wrapAngle(y[0]), prevTh2 = y[1], prevP2 = y[3]; let restAt: number | null = null; let lastNote: NoteRef | null = null; let notes = 0; let nextFrame = 0, nextInfo = 0; function step() { pend.eTarget = energyTarget(P, time) * pend.vTop; const t0 = time; pend.step(DT); time += DT / speed; const cur = wrapAngle(y[0]); if (restAt === null && prev < 0 && cur >= 0 && cur - prev < Math.PI) { const f = -prev / (cur - prev); const tc = t0 + f * (time - t0); const th2 = wrapAngle(prevTh2 + f * (y[1] - prevTh2)); const p2 = (prevP2 + f * (y[3] - prevP2)) / pend.p2Scale; sink.event({ t: tc, kind: "x", th2, p2 }); const { midi, velocity } = noteFor(P, th2, p2); lastNote = sink.note(NOTES, tc, midi, velocity, P.noteLength as number) ?? lastNote; notes++; } if (restAt === null && time >= duration && (pend.energyNorm() < restE || time >= duration + MAX_SETTLE)) { restAt = time; // the last real note rings: sustain, then fade; the piece ends with the fade if (lastNote) sink.hold(lastNote, lastNote.t, P.finalHold as number, P.finalFade as number); sink.end(time + ring); } prev = cur; prevTh2 = y[1]; prevP2 = y[3]; const e = pend.energyNorm(); hum.update(time, e, restAt !== null ? restAt + ring : null); if (time >= nextFrame) { sink.frame(0, time, [y[0], y[1], e]); nextFrame = time + FRAME_DT; } if (time >= nextInfo) { nextInfo = time + INFO_DT; sink.info(time, { "energy target": energyTarget(P, time).toFixed(4), energy: e.toFixed(4), notes: String(notes) }); } } return { get time() { return time; }, advance(t: number) { while (time < t) step(); }, }; };