// Poincaré crossing → note: θ2 → pitch (scale-quantized), |p2| → loudness, θ2 → pan. import type { Params } from "../../core/params.ts"; import { NOTE_NAMES, quantize } from "../../music/scales.ts"; export interface MappedNote { midi: number; velocity: number; pan: number; } /** `octave` shifts the pitch range (e.g. per orbit in composition 2) before quantizing. */ export function noteFor(P: Params, th2: number, p2: number, octave = 0): MappedNote { const range = P.thetaRange as number; let u = P.pitchSource === "absolute" ? Math.min(1, Math.abs(th2) / range) : Math.max(0, Math.min(1, 0.5 + (0.5 * th2) / range)); if (P.invertPitch) u = 1 - u; u = Math.pow(u, P.pitchCurve as number); const low = P.pitchLow as number, high = P.pitchHigh as number; const root = NOTE_NAMES.indexOf(P.root as string); const midi = quantize(low + (high - low) * u + 12 * octave, P.scale as string, root); const x = Math.min(1, Math.abs(p2) / (P.p2Ref as number)); const floor = P.loudFloor as number; const velocity = floor + (1 - floor) * Math.pow(x, P.loudCurve as number); const pan = (P.panAmount as number) * Math.max(-1, Math.min(1, th2 / Math.PI)); return { midi, velocity, pan }; }