1// Poincaré crossing → note: θ2 → pitch (scale-quantized), |p2| → loudness, θ2 → pan.
2import type { Params } from "../../core/params.ts";
3import { NOTE_NAMES, quantize } from "../../music/scales.ts";
5export interface MappedNote {
6 midi: number;
7 velocity: number;
8 pan: 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;
19 u = Math.pow(u, P.pitchCurve as number);
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;
26 const velocity = floor + (1 - floor) * Math.pow(x, P.loudCurve as number);
27 const pan = (P.panAmount as number) * Math.max(-1, Math.min(1, th2 / Math.PI));
28 return { midi, velocity, pan };
29}