/ music / double-pendulum
Sign in
music / double-pendulum
double-pendulum / src / instruments / sounds.ts
60 lines · 2.8 KBBlameHistoryRaw
1// The instrument sounds compositions can pick: modal-synth's presets (imported live from the
2// sibling project through the @modal-synth alias) and pasted custom presets. Worker-safe: the
3// sounds are just parameter sets; the synth workers render them.
5import { DEFAULTS, PARAM_BY_ID, type Params } from "@modal-synth/engine/params.ts";
6import { PRESETS } from "@modal-synth/presets.ts";
8/** Parse a pasted preset (the JSON from modal-synth's Copy button). */
9export function parseModalPreset(text: string | undefined): Params | null {
10 if (!text || !text.trim()) return null;
11 try {
12 const v = JSON.parse(text);
13 return v && typeof v === "object" && !Array.isArray(v) ? (v as Params) : null;
14 } catch {
15 return null;
16 }
19const SUSTAINED_EXCITERS = ["bow", "reed", "selfosc", "noise"];
21/** Menu names for note instruments: every preset, plus "modal: custom". */
22export const NOTE_SOUNDS: string[] = [...PRESETS.map((p) => `modal: ${p.name}`), "modal: custom"];
24/** Menu names for drones: sustained presets (bowed, blown, self-oscillating, fed back), plus custom. */
25export const DRONE_SOUNDS: string[] = [
26 ...PRESETS.filter((p) => {
27 const q = { ...DEFAULTS, ...p.p };
28 return SUSTAINED_EXCITERS.includes(q.exciter as string) || (q.fbGain as number) > 0;
29 }).map((p) => `modal: ${p.name}`),
30 "modal: custom",
31];
33/** The full synth parameters for a menu name (custom: the pasted JSON). */
34export function soundOf(name: string, custom: string, overrides: Params = {}): Params {
35 const preset = name === "modal: custom" ? parseModalPreset(custom) : PRESETS.find((p) => `modal: ${p.name}` === name)?.p;
36 return { ...DEFAULTS, ...(preset ?? {}), ...overrides };
39/** Settings the synth reads every block, so they can follow the energy while a drone is held. */
40export const LIVE_TARGETS = [
41 "bowForce", "bowSpeed", "bowNoise", "exPos", "reedPressure", "reedGain", "breath",
42 "selfGain", "selfSat", "noiseLevel", "noiseColor", "tension", "cascade", "curvature",
43 "fbGain", "fbFreq", "fbQ", "fbSat", "hfDamp", "t60", "pickup", "tilt", "vibDepth",
44];
46/** 0..1 → a modal-synth setting's value, following that setting's own slider scale. */
47export function targetValue(id: string, u: number, pow: (x: number, y: number) => number): number {
48 const d = PARAM_BY_ID[id];
49 if (!d || d.kind !== "num") return NaN;
50 const x = Math.max(0, Math.min(1, u));
51 if (d.scale === "log") return d.min * pow(d.max / d.min, x);
52 if (d.scale === "pow2") return d.min + (d.max - d.min) * x * x;
53 return d.min + (d.max - d.min) * x;
56/** A setting's range, for deciding whether a change is worth sending. */
57export function targetRange(id: string): number {
58 const d = PARAM_BY_ID[id] as { min?: number; max?: number } | undefined;
59 return d && d.min !== undefined && d.max !== undefined ? d.max - d.min : 1;
↑↓ move↵openescclose