@onda-lang/processor-abi 0.5.3 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -7,12 +7,37 @@ used by `@onda-lang/wasm-compiler`, `@onda-lang/binaryen-web`, and `@onda-lang/w
7
7
 
8
8
  ```js
9
9
  import {
10
+ createParamControl,
10
11
  loadProcessorArtifactFiles,
11
12
  validateProcessorArtifact,
12
13
  } from "@onda-lang/processor-abi";
14
+
15
+ const cutoff = artifact.metadata.metadata.params.find(
16
+ (param) => param.name === "cutoff",
17
+ );
18
+ const cutoffControl = createParamControl(cutoff);
19
+ const plain = cutoffControl.normalizedToPlain(0.5);
20
+ const normalized440 = cutoffControl.plainToNormalized(440);
13
21
  ```
14
22
 
15
23
  This package does not compile Onda source or host Web Audio. It exists so loaders and hosts can
16
24
  validate artifacts without installing the compiler or duplicating the ABI schema.
17
25
  The detailed TypeScript records mirror `onda_processor_abi::ProcessorDescriptor`; both packages
18
26
  validate the same checked-in conformance fixture.
27
+
28
+ `createParamControl()` validates and decodes one parameter descriptor into a reusable control with
29
+ `constrainPlain()`, `normalizedToPlain()`, and `plainToNormalized()` methods. It preserves exact
30
+ endpoints, clamps out-of-range values, applies linear, logarithmic, or SuperCollider-style curved
31
+ mapping, and snaps stepped domains. The package also exports one-shot functions with the same
32
+ behavior. Numeric parameters without a control domain and parameter arrays are rejected. These
33
+ APIs are synchronous and do not require a compiler, WebAssembly instance, or `AudioWorklet`, so
34
+ editors can prepare controls once and use them responsively.
35
+ Boolean plain and normalized numeric inputs use the same `value >= 0.5` threshold as the native
36
+ runtime and generated processor-object helpers.
37
+
38
+ `createParamDomain()` prepares the same reusable control from an already-decoded domain. It is
39
+ intended for products such as the native run view whose transport already provides numeric
40
+ `minimum`, `maximum`, `step`, and `stepCount` values.
41
+
42
+ Controlled `i64` domains are limited by the descriptor contract to the exact binary64 integer range;
43
+ unranged `i64` parameters retain their full width through typed storage.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onda-lang/processor-abi",
3
- "version": "0.5.3",
3
+ "version": "0.6.0",
4
4
  "type": "module",
5
5
  "description": "Shared Onda processor artifact schema, validation, and integrity helpers",
6
6
  "license": "MIT",
package/src/index.d.ts CHANGED
@@ -1,7 +1,9 @@
1
1
  export const PROCESSOR_ARTIFACT_FORMAT: "onda-processor";
2
2
  // Synchronized from format-versions.json; do not edit these copies directly.
3
- export const PROCESSOR_ARTIFACT_FORMAT_VERSION: 1;
4
- export const PROCESSOR_ABI_VERSION: 1;
3
+ export const PROCESSOR_ARTIFACT_FORMAT_VERSION: 2;
4
+ export const PROCESSOR_ABI_VERSION: 2;
5
+ export const PROCESSOR_EXECUTION_OK: 0;
6
+ export const PROCESSOR_EXECUTION_RUNTIME_SAFETY_FAILURE: 1;
5
7
  export const PROCESSOR_SNAPSHOT_FORMAT_VERSION: 1;
6
8
 
7
9
  export type OndaScalarType = "f32" | "f64" | "i32" | "i64" | "bool";
@@ -50,8 +52,69 @@ export interface OndaIoMetadata {
50
52
  default_reprs: string[] | null;
51
53
  range_min_repr: string | null;
52
54
  range_max_repr: string | null;
55
+ param_control: OndaParamControlMetadata | null;
53
56
  }
54
57
 
58
+ export interface OndaParamControlMetadata {
59
+ scale: "linear" | "log";
60
+ /** SuperCollider-style lincurve value; null selects linear/log scale directly. */
61
+ curve: number | null;
62
+ unit: string | null;
63
+ step_repr: string | null;
64
+ step_count: number | null;
65
+ }
66
+
67
+ export interface OndaPreparedParamControl {
68
+ readonly name: string | null;
69
+ readonly scalar: OndaScalarType;
70
+ readonly minimum: number | null;
71
+ readonly maximum: number | null;
72
+ readonly scale: "linear" | "log" | null;
73
+ readonly curve: number | null;
74
+ readonly unit: string | null;
75
+ readonly step: number | null;
76
+ readonly stepCount: number | null;
77
+ constrainPlain(plain: number | boolean): number | boolean;
78
+ normalizedToPlain(normalized: number): number | boolean;
79
+ plainToNormalized(plain: number | boolean): number;
80
+ }
81
+
82
+ export interface OndaParamDomain {
83
+ name?: string | null;
84
+ scalar: OndaScalarType;
85
+ minimum: number | null;
86
+ maximum: number | null;
87
+ scale: "linear" | "log" | null;
88
+ curve?: number | null;
89
+ unit?: string | null;
90
+ step?: number | null;
91
+ stepCount?: number | null;
92
+ }
93
+
94
+ /** Validate an already-decoded domain once for repeated host-control use. */
95
+ export function createParamDomain(domain: OndaParamDomain): OndaPreparedParamControl;
96
+
97
+ /** Validate and decode descriptor metadata once for repeated host-control use. */
98
+ export function createParamControl(param: OndaIoMetadata): OndaPreparedParamControl;
99
+
100
+ /** Clamp and snap a plain scalar value according to descriptor metadata. */
101
+ export function constrainParamPlain(
102
+ param: OndaIoMetadata,
103
+ plain: number | boolean,
104
+ ): number | boolean;
105
+
106
+ /** Convert a normalized host value to its canonical plain scalar value. */
107
+ export function paramNormalizedToPlain(
108
+ param: OndaIoMetadata,
109
+ normalized: number,
110
+ ): number | boolean;
111
+
112
+ /** Convert a plain scalar value to its canonical normalized host value. */
113
+ export function paramPlainToNormalized(
114
+ param: OndaIoMetadata,
115
+ plain: number | boolean,
116
+ ): number;
117
+
55
118
  export interface OndaBufferMetadata {
56
119
  name: string;
57
120
  type_repr: string;
@@ -98,9 +161,9 @@ export interface OndaStateMetadata {
98
161
 
99
162
  export interface OndaProcessorMetadata {
100
163
  format: "onda-processor";
101
- format_version: 1;
164
+ format_version: 2;
102
165
  artifact_kind: OndaArtifactKind;
103
- abi_version: 1;
166
+ abi_version: 2;
104
167
  backend: string;
105
168
  mir_schema_version: number;
106
169
  target: OndaTargetInfo;
package/src/index.js CHANGED
@@ -1,9 +1,23 @@
1
+ import "./param-control.js";
2
+
3
+ const PARAM_CONTROL = globalThis.__ONDA_PARAM_CONTROL_V2__;
4
+
1
5
  export const PROCESSOR_ARTIFACT_FORMAT = "onda-processor";
2
6
  // Synchronized from format-versions.json; do not edit these copies directly.
3
- export const PROCESSOR_ARTIFACT_FORMAT_VERSION = 1;
4
- export const PROCESSOR_ABI_VERSION = 1;
7
+ export const PROCESSOR_ARTIFACT_FORMAT_VERSION = 2;
8
+ export const PROCESSOR_ABI_VERSION = 2;
9
+ export const PROCESSOR_EXECUTION_OK = 0;
10
+ export const PROCESSOR_EXECUTION_RUNTIME_SAFETY_FAILURE = 1;
5
11
  export const PROCESSOR_SNAPSHOT_FORMAT_VERSION = 1;
6
12
 
13
+ export const {
14
+ createParamDomain,
15
+ createParamControl,
16
+ constrainParamPlain,
17
+ paramNormalizedToPlain,
18
+ paramPlainToNormalized,
19
+ } = PARAM_CONTROL;
20
+
7
21
  export class OndaArtifactError extends Error {
8
22
  constructor(message) {
9
23
  super(message);
@@ -199,7 +213,11 @@ export function validateProcessorMetadata(metadata, expectedKind = null) {
199
213
  }
200
214
  for (const field of ["inputs", "outputs", "control_outputs", "params"]) {
201
215
  metadata.metadata[field].forEach((entry, index) =>
202
- validateIoMetadata(entry, `metadata.${field}[${index}]`)
216
+ validateIoMetadata(
217
+ entry,
218
+ `metadata.${field}[${index}]`,
219
+ field === "params",
220
+ )
203
221
  );
204
222
  }
205
223
  metadata.metadata.buffers.forEach((entry, index) =>
@@ -242,7 +260,7 @@ export function validateProcessorMetadata(metadata, expectedKind = null) {
242
260
  return metadata;
243
261
  }
244
262
 
245
- function validateIoMetadata(value, path) {
263
+ function validateIoMetadata(value, path, isParameter) {
246
264
  requireString(value?.name, `${path}.name`);
247
265
  requireString(value?.type_repr, `${path}.type_repr`);
248
266
  requireScalar(value?.scalar, `${path}.scalar`);
@@ -255,6 +273,7 @@ function validateIoMetadata(value, path) {
255
273
  requireNullableStringArray(value?.default_reprs, `${path}.default_reprs`);
256
274
  requireNullableString(value?.range_min_repr, `${path}.range_min_repr`);
257
275
  requireNullableString(value?.range_max_repr, `${path}.range_max_repr`);
276
+ validateParamControlMetadata(value?.param_control, `${path}.param_control`);
258
277
  requireScalarLayout(value, path);
259
278
  if (value.default_reprs !== null && value.default_reprs.length !== value.array_len) {
260
279
  throw new OndaArtifactError(`${path}.default_reprs must contain one value per element`);
@@ -262,6 +281,65 @@ function validateIoMetadata(value, path) {
262
281
  if ((value.range_min_repr === null) !== (value.range_max_repr === null)) {
263
282
  throw new OndaArtifactError(`${path} range bounds must either both be present or both be null`);
264
283
  }
284
+ if (value.param_control !== null && value.range_min_repr === null) {
285
+ throw new OndaArtifactError(`${path}.param_control requires range bounds`);
286
+ }
287
+ if (
288
+ value.param_control !== null
289
+ && (value.array_len !== 1 || value.type_repr !== value.scalar)
290
+ ) {
291
+ throw new OndaArtifactError(`${path}.param_control requires a scalar parameter`);
292
+ }
293
+ if (value.param_control !== null && !isParameter) {
294
+ throw new OndaArtifactError(`${path}.param_control is only valid for parameters`);
295
+ }
296
+ if (
297
+ isParameter
298
+ && value.range_min_repr !== null
299
+ && value.type_repr === value.scalar
300
+ && value.scalar !== "bool"
301
+ && value.param_control === null
302
+ ) {
303
+ throw new OndaArtifactError(`${path}.param_control is required for a scalar numeric range`);
304
+ }
305
+ if (value.param_control !== null) {
306
+ try {
307
+ PARAM_CONTROL.validateParamControlDomain(value, true);
308
+ } catch (error) {
309
+ throw new OndaArtifactError(`${path}.param_control is invalid: ${error.message}`);
310
+ }
311
+ }
312
+ }
313
+
314
+ function validateParamControlMetadata(value, path) {
315
+ if (value === undefined) {
316
+ throw new OndaArtifactError(`${path} must be present`);
317
+ }
318
+ if (value === null) return;
319
+ if (typeof value !== "object" || Array.isArray(value)) {
320
+ throw new OndaArtifactError(`${path} must be an object or null`);
321
+ }
322
+ if (!PARAM_CONTROL.scales.includes(value.scale)) {
323
+ throw new OndaArtifactError(`${path}.scale must be 'linear' or 'log'`);
324
+ }
325
+ requireNullableFinite(value.curve, `${path}.curve`);
326
+ requireNullableString(value.unit, `${path}.unit`);
327
+ requireNullableString(value.step_repr, `${path}.step_repr`);
328
+ requireNullableInteger(value.step_count, `${path}.step_count`, 1);
329
+ if (value.step_count !== null && value.step_count > 0xffff_ffff) {
330
+ throw new OndaArtifactError(`${path}.step_count must fit u32`);
331
+ }
332
+ if ((value.step_repr === null) !== (value.step_count === null)) {
333
+ throw new OndaArtifactError(
334
+ `${path}.step_repr and step_count must either both be present or both be null`,
335
+ );
336
+ }
337
+ if (value.scale === "log" && value.step_repr !== null) {
338
+ throw new OndaArtifactError(`${path} cannot combine logarithmic scale with step`);
339
+ }
340
+ if (value.scale === "log" && value.curve !== null) {
341
+ throw new OndaArtifactError(`${path} cannot combine logarithmic scale with curve`);
342
+ }
265
343
  }
266
344
 
267
345
  function validateBufferMetadata(value, path) {
@@ -686,6 +764,15 @@ function requireNullableInteger(value, path, minimum) {
686
764
  if (value !== null) requireInteger(value, path, minimum);
687
765
  }
688
766
 
767
+ function requireNullableFinite(value, path) {
768
+ if (value === undefined) {
769
+ throw new OndaArtifactError(`${path} must be present`);
770
+ }
771
+ if (value !== null && !Number.isFinite(value)) {
772
+ throw new OndaArtifactError(`${path} must be a finite number or null`);
773
+ }
774
+ }
775
+
689
776
  function requireNullableString(value, path) {
690
777
  if (value === undefined) {
691
778
  throw new OndaArtifactError(`${path} must be present`);
@@ -0,0 +1,458 @@
1
+ (() => {
2
+ const GLOBAL_NAME = "__ONDA_PARAM_CONTROL_V2__";
3
+ if (globalThis[GLOBAL_NAME]) return;
4
+ const SCALES = Object.freeze(["linear", "log"]);
5
+ const INTEGER_REPR = /^-?(0|[1-9][0-9]*)$/;
6
+ const I32_MIN = -2147483648n;
7
+ const I32_MAX = 2147483647n;
8
+ const I64_MIN = -(1n << 63n);
9
+ const I64_MAX = (1n << 63n) - 1n;
10
+ const MAX_EXACT_HOST_INTEGER = BigInt(Number.MAX_SAFE_INTEGER);
11
+ const CURVE_LINEAR_EPSILON = 0.001;
12
+ const F64_MIN_NORMAL = 2.2250738585072014e-308;
13
+ const GRID_ROUNDING_ULPS = 8;
14
+ const MAX_GRID_ERROR_IN_STEPS = 0.125;
15
+
16
+ function fail(param, message) {
17
+ const name = typeof param?.name === "string" ? ` '${param.name}'` : "";
18
+ throw new TypeError(`Onda parameter${name} ${message}`);
19
+ }
20
+
21
+ function scalarKind(param) {
22
+ if (!param || typeof param !== "object" || Array.isArray(param)) {
23
+ fail(param, "metadata must be an object");
24
+ }
25
+ if (
26
+ Number(param.array_len) !== 1
27
+ || typeof param.scalar !== "string"
28
+ || param.type_repr !== param.scalar
29
+ ) {
30
+ fail(param, "does not have a scalar host-control domain");
31
+ }
32
+ if (!["bool", "f32", "f64", "i32", "i64"].includes(param.scalar)) {
33
+ fail(param, `has unsupported scalar type '${String(param.scalar)}'`);
34
+ }
35
+ return param.scalar;
36
+ }
37
+
38
+ function integerValue(param, value, field, minimum, maximum) {
39
+ if (typeof value !== "string" || !INTEGER_REPR.test(value)) {
40
+ fail(param, `has invalid ${field} metadata`);
41
+ }
42
+ let decoded;
43
+ try {
44
+ decoded = BigInt(value);
45
+ } catch {
46
+ fail(param, `has invalid ${field} metadata`);
47
+ }
48
+ if (decoded < minimum || decoded > maximum) {
49
+ fail(param, `has ${field} metadata outside its scalar range`);
50
+ }
51
+ return decoded;
52
+ }
53
+
54
+ function scalarMetadataValue(param, value, field) {
55
+ if (param.scalar === "i32") {
56
+ return integerValue(param, value, field, I32_MIN, I32_MAX);
57
+ }
58
+ if (param.scalar === "i64") {
59
+ return integerValue(param, value, field, I64_MIN, I64_MAX);
60
+ }
61
+ if (typeof value !== "string") {
62
+ fail(param, `has invalid ${field} metadata`);
63
+ }
64
+ const decoded = Number(value);
65
+ if (!Number.isFinite(decoded)) {
66
+ fail(param, `has invalid ${field} metadata`);
67
+ }
68
+ return param.scalar === "f32" ? Math.fround(decoded) : decoded;
69
+ }
70
+
71
+ function numericDomain(param) {
72
+ const scalar = scalarKind(param);
73
+ if (scalar === "bool") return null;
74
+ const control = param.param_control;
75
+ if (!control || typeof control !== "object" || Array.isArray(control)) {
76
+ fail(param, "has no numeric host-control domain");
77
+ }
78
+ if (control.unit !== null && typeof control.unit !== "string") {
79
+ fail(param, "has invalid host-control unit metadata");
80
+ }
81
+ if (control.unit?.includes("\0")) {
82
+ fail(param, "has a host-control unit containing a NUL character");
83
+ }
84
+ const integer = scalar === "i32" || scalar === "i64";
85
+ const minimumValue = scalarMetadataValue(
86
+ param,
87
+ param.range_min_repr,
88
+ "range minimum",
89
+ );
90
+ const maximumValue = scalarMetadataValue(
91
+ param,
92
+ param.range_max_repr,
93
+ "range maximum",
94
+ );
95
+ const integerMinimum = integer ? minimumValue : null;
96
+ const integerMaximum = integer ? maximumValue : null;
97
+ if (integer && integerMinimum >= integerMaximum) {
98
+ fail(param, "has invalid host-control range metadata");
99
+ }
100
+ if (
101
+ scalar === "i64"
102
+ && (
103
+ integerMinimum < -MAX_EXACT_HOST_INTEGER
104
+ || integerMaximum > MAX_EXACT_HOST_INTEGER
105
+ || integerMaximum - integerMinimum > MAX_EXACT_HOST_INTEGER
106
+ )
107
+ ) {
108
+ fail(param, "has an i64 domain outside the exact host-control integer range");
109
+ }
110
+ const minimum = integer ? Number(integerMinimum) : minimumValue;
111
+ const maximum = integer ? Number(integerMaximum) : maximumValue;
112
+ if (!Number.isFinite(minimum) || !Number.isFinite(maximum) || minimum >= maximum) {
113
+ fail(param, "has invalid host-control range metadata");
114
+ }
115
+ if (!SCALES.includes(control.scale)) {
116
+ fail(param, `has unsupported control scale '${String(control.scale)}'`);
117
+ }
118
+ if (control.curve !== null && !Number.isFinite(control.curve)) {
119
+ fail(param, "has invalid host-control curve metadata");
120
+ }
121
+ if (control.scale === SCALES[1] && control.curve !== null) {
122
+ fail(param, "combines logarithmic scale with curve");
123
+ }
124
+ if (control.scale === SCALES[1] && scalar !== "f32" && scalar !== "f64") {
125
+ fail(param, "uses logarithmic scale with a non-floating scalar");
126
+ }
127
+ if (control.scale === SCALES[1] && minimum <= 0) {
128
+ fail(param, "has a non-positive logarithmic host-control range");
129
+ }
130
+ if (
131
+ control.scale === SCALES[1]
132
+ && (control.step_repr !== null || control.step_count !== null)
133
+ ) {
134
+ fail(param, "combines logarithmic scale with a step");
135
+ }
136
+ if ((control.step_repr === null) !== (control.step_count === null)) {
137
+ fail(param, "must provide step and step_count together");
138
+ }
139
+ let step = null;
140
+ let integerStep = null;
141
+ if (control.step_repr !== null) {
142
+ const stepValue = scalarMetadataValue(param, control.step_repr, "step");
143
+ integerStep = integer ? stepValue : null;
144
+ step = integer ? Number(integerStep) : stepValue;
145
+ if (!Number.isFinite(step) || step <= 0) {
146
+ fail(param, "has invalid host-control step metadata");
147
+ }
148
+ if (
149
+ !Number.isSafeInteger(control.step_count)
150
+ || control.step_count <= 0
151
+ || control.step_count > 0xffff_ffff
152
+ ) {
153
+ fail(param, "has invalid host-control step_count metadata");
154
+ }
155
+ if (integer) {
156
+ const width = integerMaximum - integerMinimum;
157
+ if (
158
+ width % integerStep !== 0n
159
+ || width / integerStep !== BigInt(control.step_count)
160
+ ) {
161
+ fail(param, "has step_count inconsistent with its range and step");
162
+ }
163
+ } else {
164
+ if (
165
+ validatedFloatStepCount(scalar, minimum, maximum, step)
166
+ !== control.step_count
167
+ ) {
168
+ fail(param, "has step_count inconsistent with its range and step");
169
+ }
170
+ }
171
+ } else if (integer) {
172
+ fail(param, "has an integer range without a step");
173
+ }
174
+ return {
175
+ minimum,
176
+ maximum,
177
+ scale: control.scale,
178
+ curve: control.curve,
179
+ step,
180
+ integerMinimum,
181
+ integerMaximum,
182
+ integerStep,
183
+ };
184
+ }
185
+
186
+ function validateParamControlDomain(param, requireDefault = false) {
187
+ if (scalarKind(param) === "bool") {
188
+ fail(param, "does not have a numeric host-control domain");
189
+ }
190
+ const domain = numericDomain(param);
191
+ if (!requireDefault) return;
192
+ if (!Array.isArray(param.default_reprs) || param.default_reprs.length !== 1) {
193
+ fail(param, "must provide one scalar host-control default");
194
+ }
195
+ const defaultValue = scalarMetadataValue(
196
+ param,
197
+ param.default_reprs[0],
198
+ "default",
199
+ );
200
+ if (domain.integerMinimum !== null) {
201
+ if (defaultValue < domain.integerMinimum || defaultValue > domain.integerMaximum) {
202
+ fail(param, "has a default outside its host-control range");
203
+ }
204
+ if (
205
+ domain.integerStep !== null
206
+ && (defaultValue - domain.integerMinimum) % domain.integerStep !== 0n
207
+ ) {
208
+ fail(param, "has a default outside its host-control step grid");
209
+ }
210
+ return;
211
+ }
212
+ if (defaultValue < domain.minimum || defaultValue > domain.maximum) {
213
+ fail(param, "has a default outside its host-control range");
214
+ }
215
+ if (domain.step !== null) {
216
+ if (!floatValueIsOnGrid(
217
+ param.scalar,
218
+ domain.minimum,
219
+ defaultValue,
220
+ domain.step,
221
+ param.param_control.step_count,
222
+ )) {
223
+ fail(param, "has a default outside its host-control step grid");
224
+ }
225
+ }
226
+ }
227
+
228
+ function validatedFloatStepCount(scalar, minimum, maximum, step) {
229
+ const intervals = (maximum - minimum) / step;
230
+ if (!Number.isFinite(intervals)) return null;
231
+ const count = Math.round(intervals);
232
+ if (count < 1 || count > 0xffff_ffff) return null;
233
+ return floatGridValueMatches(scalar, minimum, maximum, step, count)
234
+ ? count
235
+ : null;
236
+ }
237
+
238
+ function floatValueIsOnGrid(
239
+ scalar,
240
+ minimum,
241
+ value,
242
+ step,
243
+ stepCount,
244
+ ) {
245
+ const index = (value - minimum) / step;
246
+ if (!Number.isFinite(index)) return false;
247
+ const rounded = Math.round(index);
248
+ return rounded >= 0
249
+ && rounded <= stepCount
250
+ && floatGridValueMatches(scalar, minimum, value, step, rounded);
251
+ }
252
+
253
+ function floatGridValueMatches(scalar, minimum, expected, step, index) {
254
+ const scaledStep = step * index;
255
+ const reconstructed = minimum + scaledStep;
256
+ if (!Number.isFinite(reconstructed)) return false;
257
+ if (scalar === "f32") {
258
+ return Math.fround(reconstructed) === Math.fround(expected);
259
+ }
260
+ const scale = Math.max(
261
+ Math.abs(minimum),
262
+ Math.abs(expected),
263
+ Math.abs(scaledStep),
264
+ F64_MIN_NORMAL,
265
+ );
266
+ const roundingTolerance = GRID_ROUNDING_ULPS * Number.EPSILON * scale;
267
+ const gridTolerance = MAX_GRID_ERROR_IN_STEPS * step;
268
+ return Math.abs(reconstructed - expected)
269
+ <= Math.min(roundingTolerance, gridTolerance);
270
+ }
271
+
272
+ function constrainDomainPlain({ minimum, maximum, step }, plain) {
273
+ const numeric = Number(plain);
274
+ let constrained = Number.isNaN(numeric)
275
+ ? minimum
276
+ : Math.min(maximum, Math.max(minimum, numeric));
277
+ if (step !== null) {
278
+ constrained = minimum + Math.round((constrained - minimum) / step) * step;
279
+ constrained = Math.min(maximum, Math.max(minimum, constrained));
280
+ }
281
+ return constrained;
282
+ }
283
+
284
+ function curveNormalizedToUnit(curve, normalized) {
285
+ if (Math.abs(curve) < CURVE_LINEAR_EPSILON) return normalized;
286
+ if (curve > 0) return 1 - curveNormalizedToUnit(-curve, 1 - normalized);
287
+ return Math.expm1(curve * normalized) / Math.expm1(curve);
288
+ }
289
+
290
+ function curveUnitToNormalized(curve, unit) {
291
+ if (Math.abs(curve) < CURVE_LINEAR_EPSILON) return unit;
292
+ if (curve > 0) return 1 - curveUnitToNormalized(-curve, 1 - unit);
293
+ return Math.log1p(unit * Math.expm1(curve)) / curve;
294
+ }
295
+
296
+ function linearUnitToPlain(minimum, maximum, unit) {
297
+ const width = maximum - minimum;
298
+ return Number.isFinite(width)
299
+ ? minimum + unit * width
300
+ : (1 - unit) * minimum + unit * maximum;
301
+ }
302
+
303
+ function linearPlainToUnit(minimum, maximum, plain) {
304
+ const width = maximum - minimum;
305
+ if (Number.isFinite(width)) return (plain - minimum) / width;
306
+ const scale = Math.max(Math.abs(minimum), Math.abs(maximum));
307
+ return ((plain / scale) - (minimum / scale))
308
+ / ((maximum / scale) - (minimum / scale));
309
+ }
310
+
311
+ function domainNormalizedToPlain(domain, normalized) {
312
+ const {
313
+ minimum,
314
+ maximum,
315
+ scale,
316
+ curve,
317
+ } = domain;
318
+ const numeric = Number(normalized);
319
+ const unit = Number.isNaN(numeric)
320
+ ? 0
321
+ : Math.min(1, Math.max(0, numeric));
322
+ if (unit === 0) return minimum;
323
+ if (unit === 1) return maximum;
324
+ let plain;
325
+ if (curve !== null) {
326
+ plain = linearUnitToPlain(
327
+ minimum,
328
+ maximum,
329
+ curveNormalizedToUnit(curve, unit),
330
+ );
331
+ } else if (scale === SCALES[1]) {
332
+ const logMinimum = Math.log(minimum);
333
+ plain = Math.exp(logMinimum + unit * (Math.log(maximum) - logMinimum));
334
+ } else {
335
+ plain = linearUnitToPlain(minimum, maximum, unit);
336
+ }
337
+ return constrainDomainPlain(domain, plain);
338
+ }
339
+
340
+ function domainPlainToNormalized(domain, plain) {
341
+ const {
342
+ minimum,
343
+ maximum,
344
+ scale,
345
+ curve,
346
+ } = domain;
347
+ const constrained = constrainDomainPlain(domain, plain);
348
+ if (constrained === minimum) return 0;
349
+ if (constrained === maximum) return 1;
350
+ let normalized;
351
+ if (curve !== null) {
352
+ normalized = curveUnitToNormalized(
353
+ curve,
354
+ linearPlainToUnit(minimum, maximum, constrained),
355
+ );
356
+ } else if (scale === SCALES[1]) {
357
+ const logMinimum = Math.log(minimum);
358
+ normalized = (Math.log(constrained) - logMinimum)
359
+ / (Math.log(maximum) - logMinimum);
360
+ } else {
361
+ normalized = linearPlainToUnit(minimum, maximum, constrained);
362
+ }
363
+ return Math.min(1, Math.max(0, normalized));
364
+ }
365
+
366
+ function createParamControl(param) {
367
+ const scalar = scalarKind(param);
368
+ if (scalar === "bool") {
369
+ return Object.freeze({
370
+ name: typeof param.name === "string" ? param.name : null,
371
+ scalar,
372
+ minimum: null,
373
+ maximum: null,
374
+ scale: null,
375
+ curve: null,
376
+ unit: null,
377
+ step: null,
378
+ stepCount: null,
379
+ constrainPlain: (plain) => Number(plain) >= 0.5,
380
+ normalizedToPlain: (normalized) => Number(normalized) >= 0.5,
381
+ plainToNormalized: (plain) => (Number(plain) >= 0.5 ? 1 : 0),
382
+ });
383
+ }
384
+
385
+ const domain = numericDomain(param);
386
+ return Object.freeze({
387
+ name: typeof param.name === "string" ? param.name : null,
388
+ scalar,
389
+ minimum: domain.minimum,
390
+ maximum: domain.maximum,
391
+ scale: domain.scale,
392
+ curve: domain.curve,
393
+ unit: param.param_control.unit,
394
+ step: domain.step,
395
+ stepCount: param.param_control.step_count,
396
+ constrainPlain: (plain) => constrainDomainPlain(domain, plain),
397
+ normalizedToPlain: (normalized) => domainNormalizedToPlain(domain, normalized),
398
+ plainToNormalized: (plain) => domainPlainToNormalized(domain, plain),
399
+ });
400
+ }
401
+
402
+ function createParamDomain({
403
+ name = null,
404
+ scalar,
405
+ minimum,
406
+ maximum,
407
+ scale,
408
+ curve = null,
409
+ unit = null,
410
+ step = null,
411
+ stepCount = null,
412
+ }) {
413
+ return createParamControl({
414
+ name,
415
+ type_repr: scalar,
416
+ scalar,
417
+ array_len: 1,
418
+ range_min_repr: minimum === null ? null : String(minimum),
419
+ range_max_repr: maximum === null ? null : String(maximum),
420
+ param_control: scale === null
421
+ ? null
422
+ : {
423
+ scale,
424
+ curve,
425
+ unit,
426
+ step_repr: step === null ? null : String(step),
427
+ step_count: stepCount,
428
+ },
429
+ });
430
+ }
431
+
432
+ function constrainParamPlain(param, plain) {
433
+ return createParamControl(param).constrainPlain(plain);
434
+ }
435
+
436
+ function paramNormalizedToPlain(param, normalized) {
437
+ return createParamControl(param).normalizedToPlain(normalized);
438
+ }
439
+
440
+ function paramPlainToNormalized(param, plain) {
441
+ return createParamControl(param).plainToNormalized(plain);
442
+ }
443
+
444
+ Object.defineProperty(globalThis, GLOBAL_NAME, {
445
+ configurable: false,
446
+ enumerable: false,
447
+ writable: false,
448
+ value: Object.freeze({
449
+ scales: SCALES,
450
+ validateParamControlDomain,
451
+ createParamDomain,
452
+ createParamControl,
453
+ constrainParamPlain,
454
+ paramNormalizedToPlain,
455
+ paramPlainToNormalized,
456
+ }),
457
+ });
458
+ })();