// The instrument sounds compositions can pick: modal-synth's presets (imported live from the // sibling project through the @modal-synth alias) and pasted custom presets. Worker-safe: the // sounds are just parameter sets; the synth workers render them. import { DEFAULTS, PARAM_BY_ID, type Params } from "@modal-synth/engine/params.ts"; import { PRESETS } from "@modal-synth/presets.ts"; /** Parse a pasted preset (the JSON from modal-synth's Copy button). */ export function parseModalPreset(text: string | undefined): Params | null { if (!text || !text.trim()) return null; try { const v = JSON.parse(text); return v && typeof v === "object" && !Array.isArray(v) ? (v as Params) : null; } catch { return null; } } const SUSTAINED_EXCITERS = ["bow", "reed", "selfosc", "noise"]; /** Menu names for note instruments: every preset, plus "modal: custom". */ export const NOTE_SOUNDS: string[] = [...PRESETS.map((p) => `modal: ${p.name}`), "modal: custom"]; /** Menu names for drones: sustained presets (bowed, blown, self-oscillating, fed back), plus custom. */ export const DRONE_SOUNDS: string[] = [ ...PRESETS.filter((p) => { const q = { ...DEFAULTS, ...p.p }; return SUSTAINED_EXCITERS.includes(q.exciter as string) || (q.fbGain as number) > 0; }).map((p) => `modal: ${p.name}`), "modal: custom", ]; /** The full synth parameters for a menu name (custom: the pasted JSON). */ export function soundOf(name: string, custom: string, overrides: Params = {}): Params { const preset = name === "modal: custom" ? parseModalPreset(custom) : PRESETS.find((p) => `modal: ${p.name}` === name)?.p; return { ...DEFAULTS, ...(preset ?? {}), ...overrides }; } /** Settings the synth reads every block, so they can follow the energy while a drone is held. */ export const LIVE_TARGETS = [ "bowForce", "bowSpeed", "bowNoise", "exPos", "reedPressure", "reedGain", "breath", "selfGain", "selfSat", "noiseLevel", "noiseColor", "tension", "cascade", "curvature", "fbGain", "fbFreq", "fbQ", "fbSat", "hfDamp", "t60", "pickup", "tilt", "vibDepth", ]; /** 0..1 → a modal-synth setting's value, following that setting's own slider scale. */ export function targetValue(id: string, u: number, pow: (x: number, y: number) => number): number { const d = PARAM_BY_ID[id]; if (!d || d.kind !== "num") return NaN; const x = Math.max(0, Math.min(1, u)); if (d.scale === "log") return d.min * pow(d.max / d.min, x); if (d.scale === "pow2") return d.min + (d.max - d.min) * x * x; return d.min + (d.max - d.min) * x; } /** A setting's range, for deciding whether a change is worth sending. */ export function targetRange(id: string): number { const d = PARAM_BY_ID[id] as { min?: number; max?: number } | undefined; return d && d.min !== undefined && d.max !== undefined ? d.max - d.min : 1; }