73fe08adouble pendulum: album app with composition 1 (poincaré)Jeremy Magland 1// Parameter schema shared by all compositions. The author panel is generated from it,
2// and a composition's config is just a flat { key: value } object.
4export type ParamValue = number | string | boolean;
5export type Params = Record<string, ParamValue>;
7interface Base {
8 key: string;
9 label: string;
10 group: string;
11 hint?: string;
12}
14export interface NumberSpec extends Base {
15 kind: "number";
16 default: number;
17 min: number;
18 max: number;
19 step?: number;
20 /** slider moves logarithmically (min must be > 0) */
21 log?: boolean;
22 unit?: string;
23}
25export interface SelectSpec extends Base {
26 kind: "select";
27 default: string;
28 options: string[];
29}
31export interface BoolSpec extends Base {
32 kind: "bool";
33 default: boolean;
34}
36export interface TextSpec extends Base {
37 kind: "text";
38 default: string;
39}
41export type ParamSpec = NumberSpec | SelectSpec | BoolSpec | TextSpec;
43export function schemaDefaults(schema: ParamSpec[]): Params {
44 const out: Params = {};
45 for (const s of schema) out[s.key] = s.default;
46 return out;
47}
49/** Merge layers onto the schema, keeping only known keys with the right type. */
50export function resolveParams(schema: ParamSpec[], ...layers: (Partial<Params> | null | undefined)[]): Params {
51 const out = schemaDefaults(schema);
52 for (const layer of layers) {
53 if (!layer) continue;
54 for (const s of schema) {
55 const v = layer[s.key];
56 if (v === undefined) continue;
57 if (s.kind === "number" && typeof v === "number" && Number.isFinite(v)) out[s.key] = v;
58 else if (s.kind === "select" && typeof v === "string" && s.options.includes(v)) out[s.key] = v;
59 else if (s.kind === "bool" && typeof v === "boolean") out[s.key] = v;
60 else if (s.kind === "text" && typeof v === "string") out[s.key] = v;
61 }
62 }
63 return out;
64}
66/** JSON in schema order, ready to paste over a composition's defaults.json. */
67export function serializeParams(schema: ParamSpec[], params: Params): string {
68 const ordered: Params = {};
69 for (const s of schema) ordered[s.key] = params[s.key];
70 return JSON.stringify(ordered, null, 2) + "\n";
71}