// Composition 4: descent — the score (runs in the score worker). // // One double pendulum, balanced straight up and nudged by a calibrated amount so it tips at about // `fallAfter` seconds. Short piano notes mark the outer bob's height as it passes a ladder of // levels (higher level, higher note), so you hear it come down. Whenever the pendulum is fully // extended (the arms pass through a straight line, at any angle) there is an extension note: a // short violin note at a fixed pitch, its strength from the bob's speed, the first one accented. // The outer bob sweeping past the pivot (a folded pendulum) is a click. Slight friction // throughout, growing toward the end until it comes to rest; the last extension note rings out // and the piece ends with the fade. // // Drawing data: track 0 = [θ1, θ2] (480 per piece second); events "ring" where notes happen. import type { Params } from "../../core/params.ts"; import { mulberry32 } from "../../core/rng.ts"; import { NOTE_NAMES, quantize } from "../../music/scales.ts"; import { cos, hypot, pow, sin, wrap } from "../../physics/dmath.ts"; import type { NoteRef, ScoreFactory } from "../../stream/score.ts"; import { DT, bob, inverted, nudgeForTip } from "./fall.ts"; import { CLICK, EXT, HEIGHT } from "./layout.ts"; const MAX_SETTLE = 15; // s after the damping: count the pendulum as at rest regardless const FRAME_DT = 1 / 480; // piece-time spacing of drawing rows const INFO_DT = 0.1; const EXT_ARM = 0.05; // rad: the arms must bend this far from straight before the next extension note const HEIGHT_HYST = 0.3; // a level sounds once the position is this fraction of a level gap past it export const descentScore: ScoreFactory = (P: Params, seed: number, sink) => { const cfg = { m1: 1, l1: 1, g: 1, m2: P.massRatio as number, l2: P.lengthRatio as number }; const speed = P.speed as number; const rand = mulberry32(seed); const side = rand() < 0.5 ? -1 : 1; const ratio = 0.6 + 0.8 * rand(); const cal = nudgeForTip(cfg, ratio, (P.fallAfter as number) * speed, (P.tipThreshold as number) * (cfg.l1 + cfg.l2)); const p = inverted(cfg, cal.nudge, ratio, side); const L = cfg.l1 + cfg.l2; const E0 = p.energy(); const vmax = Math.sqrt((2 * E0) / cfg.m2); const end = (P.freeTime as number) + (P.dampTime as number); const restE = P.restEnergy as number; const minY = (P.minDepth as number) * L; const nudgeInfo = `${cal.nudge.toExponential(2)} rad ${side > 0 ? "→" : "←"} (shape ${ratio.toFixed(2)}), tips at ${(cal.tipsAt / speed).toFixed(1)} s`; // the height ladder: scale notes between the low and high pitch, every `step`-th const levels: number[] = (() => { if (!P.heightNotes) return []; const lo = P.heightLow as number, hi = P.heightHigh as number; const root = NOTE_NAMES.indexOf(P.root as string); const all: number[] = []; for (let m = Math.ceil(lo); m <= hi; m++) if (P.scale === "continuous" || Math.abs(quantize(m, P.scale as string, root) - m) < 1e-6) all.push(m); const step = Math.max(1, Math.round(P.heightStep as number)); return all.filter((_, i) => i % step === 0); })(); const pitchPos = (y: number) => (P.heightLow as number) + ((P.heightHigh as number) - (P.heightLow as number)) * ((L - y) / (2 * L)); const countBelow = (q: number) => { let i = 0; while (i < levels.length && levels[i] < q) i++; return i; }; const hyst = HEIGHT_HYST * (levels.length > 1 ? (levels[levels.length - 1] - levels[0]) / (levels.length - 1) : 1); let time = 0; let prevX = bob(cfg, p.y)[0]; let prevPhi = wrap(p.y[1] - p.y[0]); let extArmed = false; let levelIdx = countBelow(pitchPos(bob(cfg, p.y)[1])); let restAt: number | null = null; let lastNote: NoteRef | null = null; let extCount = 0, clickCount = 0, heightCount = 0; let nextFrame = 0, nextInfo = 0; /** Friction now (per sim second): slight throughout, growing smoothly over the damping section. */ const damping = (t: number) => { const u = Math.max(0, Math.min(1, (t - (P.freeTime as number)) / (P.dampTime as number))); const s = u * u * (3 - 2 * u); return (P.dampBase as number) + ((P.dampEnd as number) - (P.dampBase as number)) * s; }; /** 0..1 from the outer bob's speed now (loudness floor and curve applied). */ const speedStrength = () => { const w = p.derived(), y = p.y; const vx = cfg.l1 * cos(y[0]) * w.w1 + cfg.l2 * cos(y[1]) * w.w2; const vy = cfg.l1 * sin(y[0]) * w.w1 + cfg.l2 * sin(y[1]) * w.w2; const x = Math.min(1, hypot(vx, vy) / ((P.loudRef as number) * vmax)); const floor = P.loudFloor as number; return floor + (1 - floor) * pow(x, P.loudCurve as number); }; const heightNote = (t: number, midi: number) => { // strength: ramps in over the opening, fades with the energy at the end const ru = (P.heightRamp as number) > 0 ? Math.max(0, Math.min(1, t / (P.heightRamp as number))) : 1; const from = P.heightRampFrom as number; const ramp = from + (1 - from) * ru * ru * (3 - 2 * ru); const e = Math.min(1, Math.max(0, p.energy()) / E0); sink.note(HEIGHT, t, midi, (P.heightVel as number) * ramp * pow(e, P.heightFollow as number), P.heightLength as number); heightCount++; }; function step() { const t0 = time; time += DT / speed; p.damping = damping(t0); p.step(DT); const [x, y] = bob(cfg, p.y); // height levels passed since the last step (with hysteresis) if (restAt === null && levels.length) { const pos = pitchPos(y); while (levelIdx < levels.length && pos > levels[levelIdx] + hyst) heightNote(time, levels[levelIdx++]); while (levelIdx > 0 && pos < levels[levelIdx - 1] - hyst) heightNote(time, levels[--levelIdx]); } // the outer bob sweeping past the pivot (crossing the vertical close to it): a click if (restAt === null && Math.abs(y) <= minY && ((prevX < 0 && x >= 0) || (prevX > 0 && x <= 0))) { const tc = t0 + (prevX / (prevX - x)) * (time - t0); clickCount++; if (P.click) { const vel = (P.clickMaxVel as number) * speedStrength(); sink.note(CLICK, tc, P.clickPitch as number, vel, P.clickLength as number); sink.event({ t: tc, kind: "ring", x: 0, y, strength: vel, click: 1 }); } } prevX = x; // full extension: the arms pass through a straight line (relative angle crosses 0, not ±π), // at any orientation. It re-arms once the arms bend at least EXT_ARM away from straight. const phi = wrap(p.y[1] - p.y[0]); if (Math.abs(phi) > EXT_ARM) extArmed = true; if (restAt === null && extArmed && ((prevPhi < 0 && phi >= 0) || (prevPhi > 0 && phi <= 0)) && Math.abs(phi - prevPhi) < Math.PI) { const tc = t0 + (prevPhi / (prevPhi - phi)) * (time - t0); extArmed = false; extCount++; // the first extension note is the accent; later ones follow the bob's speed const vel = extCount === 1 ? (P.firstVel as number) : (P.maxVel as number) * speedStrength(); lastNote = sink.note(EXT, tc, P.extPitch as number, vel, P.extLength as number) ?? lastNote; sink.event({ t: tc, kind: "ring", x, y, strength: vel, click: 0 }); } prevPhi = phi; if (restAt === null && time >= end && (p.energyNorm() < restE || time >= end + MAX_SETTLE)) { restAt = time; // the last extension note rings: sustain, then fade; the piece ends with it if (lastNote) sink.hold(lastNote, lastNote.t, P.finalHold as number, P.finalFade as number); sink.end(time + (P.finalHold as number) + (P.finalFade as number) + 0.3); } if (time >= nextFrame) { sink.frame(0, time, [p.y[0], p.y[1]]); nextFrame = time + FRAME_DT; } if (time >= nextInfo) { nextInfo = time + INFO_DT; const free = P.freeTime as number; const phase = restAt !== null ? "at rest" : extCount === 0 ? "falling" : time < free ? "swinging" : time < end ? "damping" : "settling"; sink.info(time, { phase, energy: `${(p.energy() / E0).toFixed(3)} of start`, "extension notes": String(extCount), clicks: String(clickCount), "height notes": String(heightCount), nudge: nudgeInfo, friction: `${damping(time).toFixed(4)} /sim s`, }); } } if (sink.emitting) sink.frame(0, 0, [p.y[0], p.y[1]]); return { get time() { return time; }, advance(t: number) { while (time < t) step(); }, }; };