// Simulates composition 1 with its defaults (no audio) and prints note activity per window: // how many crossings, their loudness (velocity before level) and pitch spread. // node scripts/note-activity.ts [seed] import { readFileSync } from "node:fs"; import { DoublePendulum, wrapAngle } from "../src/physics/doublePendulum.ts"; import { energyTarget } from "../src/compositions/c01-poincare/form.ts"; import { NOTE_NAMES, quantize } from "../src/music/scales.ts"; import { mulberry32 } from "../src/core/rng.ts"; const P = JSON.parse(readFileSync(new URL("../src/compositions/c01-poincare/defaults.json", import.meta.url), "utf8")); const seed = Number(process.argv[2] ?? 1); const DT = 0.002, WIN = 4; const pend = new DoublePendulum({ m1: 1, l1: 1, g: 1, m2: P.massRatio, l2: P.lengthRatio }); pend.randomize(energyTarget(P, 0), mulberry32(seed)); pend.tau = P.tau; let t = 0, prev = wrapAngle(pend.y[0]); let restAt: number | null = null, lastNote = -1, lastVel = 0; const wins: { n: number; vel: number[]; pitch: Set; e: number }[] = []; while (restAt === null) { const y0 = [...pend.y]; pend.eTarget = energyTarget(P, t) * pend.vTop; pend.step(DT); t += DT / P.speed; const cur = wrapAngle(pend.y[0]); const w = (wins[Math.floor(t / WIN)] ??= { n: 0, vel: [], pitch: new Set(), e: 0 }); w.e = pend.energyNorm(); if (prev < 0 && cur >= 0 && cur - prev < Math.PI) { const f = -prev / (cur - prev); const th2 = wrapAngle(y0[1] + f * (pend.y[1] - y0[1])); const p2 = (y0[3] + f * (pend.y[3] - y0[3])) / pend.p2Scale; let u = Math.max(0, Math.min(1, 0.5 + (0.5 * th2) / P.thetaRange)); const midi = quantize(P.pitchLow + (P.pitchHigh - P.pitchLow) * Math.pow(u, P.pitchCurve), P.scale, NOTE_NAMES.indexOf(P.root)); const x = Math.min(1, Math.abs(p2) / P.p2Ref); w.n++; w.vel.push(P.loudFloor + (1 - P.loudFloor) * Math.pow(x, P.loudCurve)); w.pitch.add(midi); lastNote = t; lastVel = w.vel[w.vel.length - 1]; } if (t >= P.duration && (pend.energyNorm() < P.restEnergy || t >= P.duration + 10)) restAt = t; prev = cur; } console.log(`at rest ${restAt!.toFixed(2)} s; last note ${lastNote.toFixed(2)} s (velocity ${lastVel.toFixed(3)}); fades out by ${(restAt! + P.finalHold + P.finalFade + 0.3).toFixed(1)} s`); console.log(`seed ${seed} speed ${P.speed} p2Ref ${P.p2Ref} loudFloor ${P.loudFloor}`); wins.forEach((w, i) => { const v = w.vel.length ? w.vel.reduce((a, b) => a + b) / w.vel.length : 0; const db = v > 0 ? (20 * Math.log10(v)).toFixed(0) : "-inf"; console.log(`${String(i * WIN).padStart(3)}–${String((i + 1) * WIN).padEnd(3)}s energy ${w.e.toFixed(4)} notes ${String(w.n).padStart(3)} mean vel ${v.toFixed(3)} (${db} dB) distinct pitches ${w.pitch.size}`); });