/ music / double-pendulum
Sign in
music / double-pendulum
double-pendulum / src / instruments / sounds.ts
26 lines · 1.2 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, 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 }
19/** Menu names for note instruments: every preset, plus "modal: custom". */
20export const NOTE_SOUNDS: string[] = [...PRESETS.map((p) => `modal: ${p.name}`), "modal: custom"];
22/** The full synth parameters for a menu name (custom: the pasted JSON). */
23export function soundOf(name: string, custom: string, overrides: Params = {}): Params {
24 const preset = name === "modal: custom" ? parseModalPreset(custom) : PRESETS.find((p) => `modal: ${p.name}` === name)?.p;
25 return { ...DEFAULTS, ...(preset ?? {}), ...overrides };
↑↓ move↵openescclose