/ music / double-pendulum
Sign in
music / double-pendulum
double-pendulum / src / compositions / shared / noteMap.ts
28 lines · 1.2 KBCodeBlameHistory
75ca48aOverhaul playback: background-computed, buffered, one clockJeremy Magland 1// Poincaré crossing → note: θ2 → pitch (scale-quantized), |p2| → loudness. Deterministic (dmath).
5c043e1Add composition 2: orbitsJeremy Magland 2import type { Params } from "../../core/params.ts";
3import { NOTE_NAMES, quantize } from "../../music/scales.ts";
75ca48aOverhaul playback: background-computed, buffered, one clockJeremy Magland 4import { pow } from "../../physics/dmath.ts";
5c043e1Add composition 2: orbitsJeremy Magland 5
6export interface MappedNote {
7 midi: number;
8 velocity: number;
9}
11/** `octave` shifts the pitch range (e.g. per orbit in composition 2) before quantizing. */
12export function noteFor(P: Params, th2: number, p2: number, octave = 0): MappedNote {
13 const range = P.thetaRange as number;
14 let u =
15 P.pitchSource === "absolute"
16 ? Math.min(1, Math.abs(th2) / range)
17 : Math.max(0, Math.min(1, 0.5 + (0.5 * th2) / range));
18 if (P.invertPitch) u = 1 - u;
75ca48aOverhaul playback: background-computed, buffered, one clockJeremy Magland 19 u = pow(u, P.pitchCurve as number);
5c043e1Add composition 2: orbitsJeremy Magland 20 const low = P.pitchLow as number, high = P.pitchHigh as number;
21 const root = NOTE_NAMES.indexOf(P.root as string);
22 const midi = quantize(low + (high - low) * u + 12 * octave, P.scale as string, root);
24 const x = Math.min(1, Math.abs(p2) / (P.p2Ref as number));
25 const floor = P.loudFloor as number;
75ca48aOverhaul playback: background-computed, buffered, one clockJeremy Magland 26 const velocity = floor + (1 - floor) * pow(x, P.loudCurve as number);
27 return { midi, velocity };
5c043e1Add composition 2: orbitsJeremy Magland 28}
↑↓ move↵openescclose