@onda-lang/processor-abi 0.5.4 → 0.7.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 +31 -0
- package/package.json +1 -1
- package/src/index.d.ts +76 -4
- package/src/index.js +128 -6
- package/src/param-control.js +458 -0
package/README.md
CHANGED
|
@@ -7,12 +7,43 @@ 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
|
+
The current descriptor represents every bindable buffer-array slot as a physical
|
|
29
|
+
`metadata.buffers` entry and records logical contiguous groups in `metadata.buffer_arrays`. At
|
|
30
|
+
runtime all four descriptor tables are present when any buffer exists, but an individual sample
|
|
31
|
+
pointer may be null. Null entries select the processor's neutral one-frame zero/discard storage;
|
|
32
|
+
they are valid bindings, not an incomplete artifact.
|
|
33
|
+
|
|
34
|
+
`createParamControl()` validates and decodes one parameter descriptor into a reusable control with
|
|
35
|
+
`constrainPlain()`, `normalizedToPlain()`, and `plainToNormalized()` methods. It preserves exact
|
|
36
|
+
endpoints, clamps out-of-range values, applies linear, logarithmic, or SuperCollider-style curved
|
|
37
|
+
mapping, and snaps stepped domains. The package also exports one-shot functions with the same
|
|
38
|
+
behavior. Numeric parameters without a control domain and parameter arrays are rejected. These
|
|
39
|
+
APIs are synchronous and do not require a compiler, WebAssembly instance, or `AudioWorklet`, so
|
|
40
|
+
editors can prepare controls once and use them responsively.
|
|
41
|
+
Boolean plain and normalized numeric inputs use the same `value >= 0.5` threshold as the native
|
|
42
|
+
runtime and generated processor-object helpers.
|
|
43
|
+
|
|
44
|
+
`createParamDomain()` prepares the same reusable control from an already-decoded domain. It is
|
|
45
|
+
intended for products such as the native run view whose transport already provides numeric
|
|
46
|
+
`minimum`, `maximum`, `step`, and `stepCount` values.
|
|
47
|
+
|
|
48
|
+
Controlled `i64` domains are limited by the descriptor contract to the exact binary64 integer range;
|
|
49
|
+
unranged `i64` parameters retain their full width through typed storage.
|
package/package.json
CHANGED
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:
|
|
4
|
-
export const PROCESSOR_ABI_VERSION:
|
|
3
|
+
export const PROCESSOR_ARTIFACT_FORMAT_VERSION: 3;
|
|
4
|
+
export const PROCESSOR_ABI_VERSION: 3;
|
|
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;
|
|
@@ -59,10 +122,18 @@ export interface OndaBufferMetadata {
|
|
|
59
122
|
element_size_bytes: number;
|
|
60
123
|
channels: "mono" | "static" | "dynamic";
|
|
61
124
|
static_channels: number | null;
|
|
125
|
+
/** Declared buffer access capability. */
|
|
62
126
|
access: "read_only" | "read_write";
|
|
127
|
+
/** Whether reachable code may write this physical slot; unresolved selectors are conservative. */
|
|
63
128
|
may_write: boolean;
|
|
64
129
|
}
|
|
65
130
|
|
|
131
|
+
export interface OndaBufferArrayMetadata {
|
|
132
|
+
name: string;
|
|
133
|
+
first_buffer: number;
|
|
134
|
+
len: number;
|
|
135
|
+
}
|
|
136
|
+
|
|
66
137
|
export interface OndaEventParamMetadata {
|
|
67
138
|
name: string;
|
|
68
139
|
type_repr: string;
|
|
@@ -98,9 +169,9 @@ export interface OndaStateMetadata {
|
|
|
98
169
|
|
|
99
170
|
export interface OndaProcessorMetadata {
|
|
100
171
|
format: "onda-processor";
|
|
101
|
-
format_version:
|
|
172
|
+
format_version: 3;
|
|
102
173
|
artifact_kind: OndaArtifactKind;
|
|
103
|
-
abi_version:
|
|
174
|
+
abi_version: 3;
|
|
104
175
|
backend: string;
|
|
105
176
|
mir_schema_version: number;
|
|
106
177
|
target: OndaTargetInfo;
|
|
@@ -135,6 +206,7 @@ export interface OndaProcessorMetadata {
|
|
|
135
206
|
control_outputs: OndaIoMetadata[];
|
|
136
207
|
params: OndaIoMetadata[];
|
|
137
208
|
buffers: OndaBufferMetadata[];
|
|
209
|
+
buffer_arrays: OndaBufferArrayMetadata[];
|
|
138
210
|
events: OndaEventMetadata[];
|
|
139
211
|
states: OndaStateMetadata[];
|
|
140
212
|
};
|
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 =
|
|
4
|
-
export const PROCESSOR_ABI_VERSION =
|
|
7
|
+
export const PROCESSOR_ARTIFACT_FORMAT_VERSION = 3;
|
|
8
|
+
export const PROCESSOR_ABI_VERSION = 3;
|
|
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,12 +213,51 @@ 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(
|
|
216
|
+
validateIoMetadata(
|
|
217
|
+
entry,
|
|
218
|
+
`metadata.${field}[${index}]`,
|
|
219
|
+
field === "params",
|
|
220
|
+
)
|
|
203
221
|
);
|
|
204
222
|
}
|
|
205
223
|
metadata.metadata.buffers.forEach((entry, index) =>
|
|
206
224
|
validateBufferMetadata(entry, `metadata.buffers[${index}]`)
|
|
207
225
|
);
|
|
226
|
+
const bufferArrays = metadata.metadata.buffer_arrays ?? [];
|
|
227
|
+
if (!Array.isArray(bufferArrays)) {
|
|
228
|
+
throw new OndaArtifactError("metadata.buffer_arrays must be an array");
|
|
229
|
+
}
|
|
230
|
+
const bufferArrayNames = new Set();
|
|
231
|
+
const groupedBuffers = new Set();
|
|
232
|
+
bufferArrays.forEach((entry, index) => {
|
|
233
|
+
const path = `metadata.buffer_arrays[${index}]`;
|
|
234
|
+
requireString(entry?.name, `${path}.name`);
|
|
235
|
+
requireInteger(entry?.first_buffer, `${path}.first_buffer`, 0);
|
|
236
|
+
requireInteger(entry?.len, `${path}.len`, 1);
|
|
237
|
+
if (bufferArrayNames.has(entry.name)) {
|
|
238
|
+
throw new OndaArtifactError(`${path}.name duplicates buffer array '${entry.name}'`);
|
|
239
|
+
}
|
|
240
|
+
bufferArrayNames.add(entry.name);
|
|
241
|
+
if (entry.first_buffer + entry.len > metadata.metadata.buffers.length) {
|
|
242
|
+
throw new OndaArtifactError(`${path} exceeds metadata.buffers`);
|
|
243
|
+
}
|
|
244
|
+
const first = metadata.metadata.buffers[entry.first_buffer];
|
|
245
|
+
for (let slot = entry.first_buffer; slot < entry.first_buffer + entry.len; slot += 1) {
|
|
246
|
+
if (groupedBuffers.has(slot)) {
|
|
247
|
+
throw new OndaArtifactError(`${path} overlaps another buffer array at slot ${slot}`);
|
|
248
|
+
}
|
|
249
|
+
groupedBuffers.add(slot);
|
|
250
|
+
const buffer = metadata.metadata.buffers[slot];
|
|
251
|
+
if (
|
|
252
|
+
buffer.scalar !== first.scalar ||
|
|
253
|
+
buffer.channels !== first.channels ||
|
|
254
|
+
buffer.static_channels !== first.static_channels ||
|
|
255
|
+
buffer.access !== first.access
|
|
256
|
+
) {
|
|
257
|
+
throw new OndaArtifactError(`${path} contains incompatible buffer descriptors`);
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
});
|
|
208
261
|
metadata.metadata.events.forEach((entry, index) =>
|
|
209
262
|
validateEventMetadata(entry, `metadata.events[${index}]`)
|
|
210
263
|
);
|
|
@@ -242,7 +295,7 @@ export function validateProcessorMetadata(metadata, expectedKind = null) {
|
|
|
242
295
|
return metadata;
|
|
243
296
|
}
|
|
244
297
|
|
|
245
|
-
function validateIoMetadata(value, path) {
|
|
298
|
+
function validateIoMetadata(value, path, isParameter) {
|
|
246
299
|
requireString(value?.name, `${path}.name`);
|
|
247
300
|
requireString(value?.type_repr, `${path}.type_repr`);
|
|
248
301
|
requireScalar(value?.scalar, `${path}.scalar`);
|
|
@@ -255,6 +308,7 @@ function validateIoMetadata(value, path) {
|
|
|
255
308
|
requireNullableStringArray(value?.default_reprs, `${path}.default_reprs`);
|
|
256
309
|
requireNullableString(value?.range_min_repr, `${path}.range_min_repr`);
|
|
257
310
|
requireNullableString(value?.range_max_repr, `${path}.range_max_repr`);
|
|
311
|
+
validateParamControlMetadata(value?.param_control, `${path}.param_control`);
|
|
258
312
|
requireScalarLayout(value, path);
|
|
259
313
|
if (value.default_reprs !== null && value.default_reprs.length !== value.array_len) {
|
|
260
314
|
throw new OndaArtifactError(`${path}.default_reprs must contain one value per element`);
|
|
@@ -262,6 +316,65 @@ function validateIoMetadata(value, path) {
|
|
|
262
316
|
if ((value.range_min_repr === null) !== (value.range_max_repr === null)) {
|
|
263
317
|
throw new OndaArtifactError(`${path} range bounds must either both be present or both be null`);
|
|
264
318
|
}
|
|
319
|
+
if (value.param_control !== null && value.range_min_repr === null) {
|
|
320
|
+
throw new OndaArtifactError(`${path}.param_control requires range bounds`);
|
|
321
|
+
}
|
|
322
|
+
if (
|
|
323
|
+
value.param_control !== null
|
|
324
|
+
&& (value.array_len !== 1 || value.type_repr !== value.scalar)
|
|
325
|
+
) {
|
|
326
|
+
throw new OndaArtifactError(`${path}.param_control requires a scalar parameter`);
|
|
327
|
+
}
|
|
328
|
+
if (value.param_control !== null && !isParameter) {
|
|
329
|
+
throw new OndaArtifactError(`${path}.param_control is only valid for parameters`);
|
|
330
|
+
}
|
|
331
|
+
if (
|
|
332
|
+
isParameter
|
|
333
|
+
&& value.range_min_repr !== null
|
|
334
|
+
&& value.type_repr === value.scalar
|
|
335
|
+
&& value.scalar !== "bool"
|
|
336
|
+
&& value.param_control === null
|
|
337
|
+
) {
|
|
338
|
+
throw new OndaArtifactError(`${path}.param_control is required for a scalar numeric range`);
|
|
339
|
+
}
|
|
340
|
+
if (value.param_control !== null) {
|
|
341
|
+
try {
|
|
342
|
+
PARAM_CONTROL.validateParamControlDomain(value, true);
|
|
343
|
+
} catch (error) {
|
|
344
|
+
throw new OndaArtifactError(`${path}.param_control is invalid: ${error.message}`);
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
function validateParamControlMetadata(value, path) {
|
|
350
|
+
if (value === undefined) {
|
|
351
|
+
throw new OndaArtifactError(`${path} must be present`);
|
|
352
|
+
}
|
|
353
|
+
if (value === null) return;
|
|
354
|
+
if (typeof value !== "object" || Array.isArray(value)) {
|
|
355
|
+
throw new OndaArtifactError(`${path} must be an object or null`);
|
|
356
|
+
}
|
|
357
|
+
if (!PARAM_CONTROL.scales.includes(value.scale)) {
|
|
358
|
+
throw new OndaArtifactError(`${path}.scale must be 'linear' or 'log'`);
|
|
359
|
+
}
|
|
360
|
+
requireNullableFinite(value.curve, `${path}.curve`);
|
|
361
|
+
requireNullableString(value.unit, `${path}.unit`);
|
|
362
|
+
requireNullableString(value.step_repr, `${path}.step_repr`);
|
|
363
|
+
requireNullableInteger(value.step_count, `${path}.step_count`, 1);
|
|
364
|
+
if (value.step_count !== null && value.step_count > 0xffff_ffff) {
|
|
365
|
+
throw new OndaArtifactError(`${path}.step_count must fit u32`);
|
|
366
|
+
}
|
|
367
|
+
if ((value.step_repr === null) !== (value.step_count === null)) {
|
|
368
|
+
throw new OndaArtifactError(
|
|
369
|
+
`${path}.step_repr and step_count must either both be present or both be null`,
|
|
370
|
+
);
|
|
371
|
+
}
|
|
372
|
+
if (value.scale === "log" && value.step_repr !== null) {
|
|
373
|
+
throw new OndaArtifactError(`${path} cannot combine logarithmic scale with step`);
|
|
374
|
+
}
|
|
375
|
+
if (value.scale === "log" && value.curve !== null) {
|
|
376
|
+
throw new OndaArtifactError(`${path} cannot combine logarithmic scale with curve`);
|
|
377
|
+
}
|
|
265
378
|
}
|
|
266
379
|
|
|
267
380
|
function validateBufferMetadata(value, path) {
|
|
@@ -285,8 +398,8 @@ function validateBufferMetadata(value, path) {
|
|
|
285
398
|
throw new OndaArtifactError(`${path}.access has an unsupported value`);
|
|
286
399
|
}
|
|
287
400
|
requireBoolean(value?.may_write, `${path}.may_write`);
|
|
288
|
-
if (value.may_write
|
|
289
|
-
throw new OndaArtifactError(`${path}.may_write
|
|
401
|
+
if (value.may_write && value.access !== "read_write") {
|
|
402
|
+
throw new OndaArtifactError(`${path}.may_write requires read_write access`);
|
|
290
403
|
}
|
|
291
404
|
}
|
|
292
405
|
|
|
@@ -686,6 +799,15 @@ function requireNullableInteger(value, path, minimum) {
|
|
|
686
799
|
if (value !== null) requireInteger(value, path, minimum);
|
|
687
800
|
}
|
|
688
801
|
|
|
802
|
+
function requireNullableFinite(value, path) {
|
|
803
|
+
if (value === undefined) {
|
|
804
|
+
throw new OndaArtifactError(`${path} must be present`);
|
|
805
|
+
}
|
|
806
|
+
if (value !== null && !Number.isFinite(value)) {
|
|
807
|
+
throw new OndaArtifactError(`${path} must be a finite number or null`);
|
|
808
|
+
}
|
|
809
|
+
}
|
|
810
|
+
|
|
689
811
|
function requireNullableString(value, path) {
|
|
690
812
|
if (value === undefined) {
|
|
691
813
|
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
|
+
})();
|