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 /**
13 * Takes effect immediately while playing (drawing, mix levels). Every other setting changes the
14 * performance itself, so editing it while playing recomputes from the current moment.
15 */
16 live?: boolean;
17}
19export interface NumberSpec extends Base {
20 kind: "number";
21 default: number;
22 min: number;
23 max: number;
24 step?: number;
25 /** slider moves logarithmically (min must be > 0) */
26 log?: boolean;
27 unit?: string;
28}
30export interface SelectSpec extends Base {
31 kind: "select";
32 default: string;
33 options: string[];
34}
36export interface BoolSpec extends Base {
37 kind: "bool";
38 default: boolean;
39}
41export interface TextSpec extends Base {
42 kind: "text";
43 default: string;
44}
46export type ParamSpec = NumberSpec | SelectSpec | BoolSpec | TextSpec;
48export function schemaDefaults(schema: ParamSpec[]): Params {
49 const out: Params = {};
50 for (const s of schema) out[s.key] = s.default;
51 return out;
52}
54/** Merge layers onto the schema, keeping only known keys with the right type. */
55export function resolveParams(schema: ParamSpec[], ...layers: (Partial<Params> | null | undefined)[]): Params {
56 const out = schemaDefaults(schema);
57 for (const layer of layers) {
58 if (!layer) continue;
59 for (const s of schema) {
60 const v = layer[s.key];
61 if (v === undefined) continue;
62 if (s.kind === "number" && typeof v === "number" && Number.isFinite(v)) out[s.key] = v;
63 else if (s.kind === "select" && typeof v === "string" && s.options.includes(v)) out[s.key] = v;
64 else if (s.kind === "bool" && typeof v === "boolean") out[s.key] = v;
65 else if (s.kind === "text" && typeof v === "string") out[s.key] = v;
66 }
67 }
68 return out;
69}
71/** JSON in schema order, ready to paste over a composition's defaults.json. */
72export function serializeParams(schema: ParamSpec[], params: Params): string {
73 const ordered: Params = {};
74 for (const s of schema) ordered[s.key] = params[s.key];
75 return JSON.stringify(ordered, null, 2) + "\n";
76}