@onda-lang/webaudio 0.8.39 → 0.9.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/LICENSE +202 -19
- package/README.md +19 -0
- package/api.md +46 -7
- package/package.json +3 -3
- package/src/execution-status-ring.js +74 -0
- package/src/index.d.ts +31 -2
- package/src/index.js +173 -28
- package/src/runtime-status.js +48 -0
- package/src/worklet.js +204 -122
package/src/worklet.js
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { ONDA_RUNTIME_ERROR as ERROR, STATUS_OPERATIONS, failureStatus } from "./runtime-status.js";
|
|
2
|
+
import { openExecutionStatusRing, writeExecutionStatus } from "./execution-status-ring.js";
|
|
3
|
+
import { paramSmoothingSamples, ParamSmoothing } from "./param-metadata.js";
|
|
2
4
|
import {
|
|
3
5
|
EXECUTION_OPERATION_EVENT,
|
|
4
6
|
EXECUTION_OPERATION_INIT,
|
|
@@ -20,30 +22,39 @@ const DEFAULT_EVENT_PAYLOAD_CAPACITY_BYTES = 64 * 1024;
|
|
|
20
22
|
const DEFAULT_DELEGATE_CAPACITY_BYTES = 64 * 1024;
|
|
21
23
|
const DEFAULT_PRINT_CAPACITY_BYTES = 64 * 1024;
|
|
22
24
|
|
|
23
|
-
class ProcessorExecutionError extends Error {
|
|
24
|
-
constructor(message, status) {
|
|
25
|
-
super(message);
|
|
26
|
-
this.status = status;
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
25
|
const DELEGATE_BATCH_SIZE_BYTES = 20;
|
|
30
26
|
const PRINT_BATCH_SIZE_BYTES = 20;
|
|
31
27
|
const EXECUTION_OUTPUT_SIZE_BYTES = 12;
|
|
32
28
|
const HOST_LITTLE_ENDIAN = new Uint8Array(new Uint16Array([1]).buffer)[0] === 1;
|
|
33
29
|
|
|
30
|
+
const DECIMAL_I64 = /^-?[0-9]+$/;
|
|
31
|
+
|
|
32
|
+
// Match Onda's float-to-integer casts at the host audio boundary.
|
|
33
|
+
function audioI32(value) {
|
|
34
|
+
if (value >= 2147483647) return 2147483647;
|
|
35
|
+
if (value <= -2147483648) return -2147483648;
|
|
36
|
+
return Math.trunc(value) || 0;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function audioI64(value) {
|
|
40
|
+
if (value >= 2 ** 63) return 9223372036854775807n;
|
|
41
|
+
if (value <= -(2 ** 63)) return -9223372036854775808n;
|
|
42
|
+
return Number.isNaN(value) ? 0n : BigInt(Math.trunc(value));
|
|
43
|
+
}
|
|
44
|
+
|
|
34
45
|
function exactI64(value) {
|
|
35
46
|
let integer;
|
|
36
47
|
if (typeof value === "bigint") {
|
|
37
48
|
integer = value;
|
|
38
49
|
} else if (typeof value === "number" && Number.isSafeInteger(value)) {
|
|
39
50
|
integer = BigInt(value);
|
|
40
|
-
} else if (typeof value === "string" &&
|
|
51
|
+
} else if (typeof value === "string" && DECIMAL_I64.test(value)) {
|
|
41
52
|
integer = BigInt(value);
|
|
42
53
|
} else {
|
|
43
|
-
throw
|
|
54
|
+
throw ERROR.INVALID_PARAMETER;
|
|
44
55
|
}
|
|
45
56
|
if (BigInt.asIntN(64, integer) !== integer) {
|
|
46
|
-
throw
|
|
57
|
+
throw ERROR.INVALID_PARAMETER;
|
|
47
58
|
}
|
|
48
59
|
return integer;
|
|
49
60
|
}
|
|
@@ -290,7 +301,25 @@ class OndaWasmProcessor extends AudioWorkletProcessor {
|
|
|
290
301
|
printOverflowCount: 0,
|
|
291
302
|
printTransportDropCount: 0,
|
|
292
303
|
};
|
|
304
|
+
this.executionStatusRing = processorOptions.executionStatusRing == null
|
|
305
|
+
? null : openExecutionStatusRing(processorOptions.executionStatusRing);
|
|
306
|
+
this.statusMessage = { type: "onda-status", requestId: 0, operation: 0, status: 0 };
|
|
307
|
+
this.statusReadyMessage = { type: "onda-status-ready" };
|
|
308
|
+
this.transportFailed = false;
|
|
309
|
+
this.eventIds = new Map(this.eventInfo.map((event, index) => [event.name, index]));
|
|
310
|
+
this.bufferIds = new Map(this.bufferInfo.map((buffer, index) => [buffer.name, index]));
|
|
311
|
+
this.paramAddresses = new Map();
|
|
312
|
+
this.arrayParamAddresses = [];
|
|
313
|
+
this.paramInfo.forEach((info, index) => {
|
|
314
|
+
const address = { info };
|
|
315
|
+
this.paramAddresses.set(index, address);
|
|
316
|
+
this.paramAddresses.set(info.name, address);
|
|
317
|
+
if (this.isFixedArray(info)) {
|
|
318
|
+
this.arrayParamAddresses.push({ info, prefix: `${info.name}[` });
|
|
319
|
+
}
|
|
320
|
+
});
|
|
293
321
|
this.writeParamDefaults();
|
|
322
|
+
this.paramDefaults = new Uint8Array(this.memory.buffer, this.paramsPtr, this.paramSizeBytes).slice();
|
|
294
323
|
this.writeInitialParams(processorOptions.params ?? {});
|
|
295
324
|
this.paramSmoothing = new ParamSmoothing(
|
|
296
325
|
this.paramInfo,
|
|
@@ -342,6 +371,7 @@ class OndaWasmProcessor extends AudioWorkletProcessor {
|
|
|
342
371
|
this.dataViewCache = new DataView(buffer);
|
|
343
372
|
if (!this.viewsReady) return;
|
|
344
373
|
|
|
374
|
+
this.paramBytes = new Uint8Array(buffer, this.paramsPtr, this.paramSizeBytes);
|
|
345
375
|
this.stateBytes = new Uint8Array(
|
|
346
376
|
buffer,
|
|
347
377
|
this.statePtr,
|
|
@@ -385,7 +415,7 @@ class OndaWasmProcessor extends AudioWorkletProcessor {
|
|
|
385
415
|
if (scalar === "i64") return new BigInt64Array(this.memoryBuffer, address, length);
|
|
386
416
|
if (scalar === "f32") return new Float32Array(this.memoryBuffer, address, length);
|
|
387
417
|
if (scalar === "f64") return new Float64Array(this.memoryBuffer, address, length);
|
|
388
|
-
throw
|
|
418
|
+
throw ERROR.INVALID_STORAGE;
|
|
389
419
|
}
|
|
390
420
|
|
|
391
421
|
configuredEventPayloadCapacity(configuredCapacity) {
|
|
@@ -488,6 +518,11 @@ class OndaWasmProcessor extends AudioWorkletProcessor {
|
|
|
488
518
|
}
|
|
489
519
|
|
|
490
520
|
writeParamDefaults() {
|
|
521
|
+
if (this.paramDefaults) {
|
|
522
|
+
this.refreshMemoryCache();
|
|
523
|
+
this.paramBytes.set(this.paramDefaults);
|
|
524
|
+
return;
|
|
525
|
+
}
|
|
491
526
|
for (const param of this.paramInfo) {
|
|
492
527
|
const value = this.metadataDefaultValue(param);
|
|
493
528
|
if (value !== undefined) {
|
|
@@ -501,11 +536,37 @@ class OndaWasmProcessor extends AudioWorkletProcessor {
|
|
|
501
536
|
}
|
|
502
537
|
|
|
503
538
|
setParam(selector, value, explicitElement = null) {
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
539
|
+
let param = this.paramAddresses.get(selector)?.info;
|
|
540
|
+
let element = explicitElement;
|
|
541
|
+
if (!param && typeof selector === "string" && selector.endsWith("]") && explicitElement === null) {
|
|
542
|
+
for (const address of this.arrayParamAddresses) {
|
|
543
|
+
if (!selector.startsWith(address.prefix)) continue;
|
|
544
|
+
const start = address.prefix.length;
|
|
545
|
+
const end = selector.length - 1;
|
|
546
|
+
if (start === end || (selector[start] === "0" && end - start > 1)) break;
|
|
547
|
+
element = 0;
|
|
548
|
+
for (let index = start; index < end; index += 1) {
|
|
549
|
+
const digit = selector.charCodeAt(index) - 48;
|
|
550
|
+
if (digit < 0 || digit > 9) throw ERROR.INVALID_PARAMETER;
|
|
551
|
+
element = element * 10 + digit;
|
|
552
|
+
}
|
|
553
|
+
param = address.info;
|
|
554
|
+
break;
|
|
555
|
+
}
|
|
556
|
+
}
|
|
557
|
+
if (!param || value === undefined) throw ERROR.INVALID_PARAMETER;
|
|
558
|
+
if (element !== null && (!Number.isSafeInteger(element) || element < 0 || element >= param.array_len)) {
|
|
559
|
+
throw ERROR.INVALID_PARAMETER;
|
|
560
|
+
}
|
|
561
|
+
// Validate every element before storing any: rejection must not partially
|
|
562
|
+
// update parameters or smoothing targets (notably an invalid i64 array tail).
|
|
563
|
+
if (element === null) this.validateStorageValue(param, value);
|
|
564
|
+
if (element !== null || !this.isFixedArray(param)) {
|
|
565
|
+
this.validateScalarValue(param.scalar, value);
|
|
566
|
+
} else {
|
|
567
|
+
for (let index = 0; index < param.array_len; index += 1) {
|
|
568
|
+
this.validateScalarValue(param.scalar, value[index]);
|
|
569
|
+
}
|
|
509
570
|
}
|
|
510
571
|
const offset = Number(param.byte_offset);
|
|
511
572
|
const byteSize = Number(param.byte_size);
|
|
@@ -520,7 +581,7 @@ class OndaWasmProcessor extends AudioWorkletProcessor {
|
|
|
520
581
|
byteSize !== expectedByteSize ||
|
|
521
582
|
offset + byteSize > this.paramSizeBytes
|
|
522
583
|
) {
|
|
523
|
-
throw
|
|
584
|
+
throw ERROR.INVALID_PARAMETER;
|
|
524
585
|
}
|
|
525
586
|
if (element !== null) {
|
|
526
587
|
this.setParamScalar(param, element, value);
|
|
@@ -547,10 +608,10 @@ class OndaWasmProcessor extends AudioWorkletProcessor {
|
|
|
547
608
|
|
|
548
609
|
init(mode) {
|
|
549
610
|
if (mode !== PROCESSOR_INIT_PRESERVE_PINNED && mode !== PROCESSOR_INIT_FULL) {
|
|
550
|
-
throw
|
|
611
|
+
throw ERROR.INVALID_INIT_MODE;
|
|
551
612
|
}
|
|
552
613
|
if (mode === PROCESSOR_INIT_PRESERVE_PINNED && !this.initialized) {
|
|
553
|
-
throw
|
|
614
|
+
throw ERROR.UNINITIALIZED;
|
|
554
615
|
}
|
|
555
616
|
this.runInitialization(mode);
|
|
556
617
|
}
|
|
@@ -568,6 +629,7 @@ class OndaWasmProcessor extends AudioWorkletProcessor {
|
|
|
568
629
|
}
|
|
569
630
|
|
|
570
631
|
runInitialization(mode, afterInitialize, collectOutput = true) {
|
|
632
|
+
if (this.transportFailed) throw ERROR.TRANSPORT_OVERFLOW;
|
|
571
633
|
// Reinitializing state does not create a compile-block boundary. Retain
|
|
572
634
|
// the host-side position so the next process segment cannot synthesize an
|
|
573
635
|
// extra BEGIN_BLOCK or postpone the matching END_BLOCK.
|
|
@@ -596,28 +658,25 @@ class OndaWasmProcessor extends AudioWorkletProcessor {
|
|
|
596
658
|
0,
|
|
597
659
|
);
|
|
598
660
|
}
|
|
599
|
-
this.checkExecutionStatus(status
|
|
661
|
+
this.checkExecutionStatus(status);
|
|
600
662
|
afterInitialize?.();
|
|
601
663
|
this.commitInitializedState(blockCursor);
|
|
602
664
|
}
|
|
603
665
|
|
|
604
|
-
requireInitialized(
|
|
666
|
+
requireInitialized() {
|
|
605
667
|
if (!this.initialized) {
|
|
606
|
-
throw
|
|
668
|
+
throw ERROR.UNINITIALIZED;
|
|
607
669
|
}
|
|
608
670
|
}
|
|
609
671
|
|
|
610
|
-
checkExecutionStatus(status,
|
|
672
|
+
checkExecutionStatus(status, inputRejectionAllowed = false) {
|
|
611
673
|
if (status === PROCESSOR_EXECUTION_OK) return;
|
|
612
|
-
this.invalidateState();
|
|
613
|
-
throw
|
|
614
|
-
`${operation} failed with Onda execution status ${String(status)}`,
|
|
615
|
-
status,
|
|
616
|
-
);
|
|
674
|
+
if (!inputRejectionAllowed || status !== PROCESSOR_EXECUTION_INPUT_REJECTED) this.invalidateState();
|
|
675
|
+
throw status;
|
|
617
676
|
}
|
|
618
677
|
|
|
619
678
|
createSnapshot() {
|
|
620
|
-
this.requireInitialized(
|
|
679
|
+
this.requireInitialized();
|
|
621
680
|
const snapshot = new Uint8Array(this.snapshotSizeBytes);
|
|
622
681
|
this.refreshMemoryCache();
|
|
623
682
|
const state = this.stateBytes;
|
|
@@ -636,11 +695,6 @@ class OndaWasmProcessor extends AudioWorkletProcessor {
|
|
|
636
695
|
|
|
637
696
|
restoreSnapshot(value) {
|
|
638
697
|
const snapshot = this.snapshotBytes(value);
|
|
639
|
-
if (snapshot.byteLength !== this.snapshotSizeBytes) {
|
|
640
|
-
throw new Error(
|
|
641
|
-
`snapshot has ${snapshot.byteLength} bytes; expected ${this.snapshotSizeBytes}`,
|
|
642
|
-
);
|
|
643
|
-
}
|
|
644
698
|
// The ABI restore base is a fresh post-init image, so scratch and
|
|
645
699
|
// control-mirror state never leak across a restore. Initialization and
|
|
646
700
|
// overlay form one lifecycle transition: neither partial result is ready.
|
|
@@ -663,25 +717,27 @@ class OndaWasmProcessor extends AudioWorkletProcessor {
|
|
|
663
717
|
normalizeSnapshotIntegerRange(entry, state, offset, byteSize) {
|
|
664
718
|
const range = entry.integer_range;
|
|
665
719
|
if (range === null || range === undefined) return;
|
|
666
|
-
if (entry.
|
|
667
|
-
throw
|
|
720
|
+
if (entry.scalar !== "i32" && entry.scalar !== "i64") {
|
|
721
|
+
throw ERROR.INVALID_STORAGE;
|
|
668
722
|
}
|
|
669
723
|
const mode = range.mode;
|
|
670
724
|
if (mode !== "clamp" && mode !== "wrap") {
|
|
671
|
-
throw
|
|
725
|
+
throw ERROR.INVALID_STORAGE;
|
|
672
726
|
}
|
|
673
727
|
const view = new DataView(state.buffer, state.byteOffset + offset, byteSize);
|
|
674
728
|
if (entry.scalar === "i32") {
|
|
675
729
|
const min = Number(range.min?.value);
|
|
676
730
|
const max = Number(range.max?.value);
|
|
677
|
-
if (!Number.isInteger(min) || !Number.isInteger(max) || min > max || byteSize !== 4) {
|
|
678
|
-
throw
|
|
731
|
+
if (!Number.isInteger(min) || !Number.isInteger(max) || min > max || byteSize !== entry.array_len * 4) {
|
|
732
|
+
throw ERROR.INVALID_STORAGE;
|
|
733
|
+
}
|
|
734
|
+
for (let element = 0; element < byteSize; element += 4) {
|
|
735
|
+
const value = view.getInt32(element, true);
|
|
736
|
+
const normalized = mode === "clamp"
|
|
737
|
+
? Math.min(Math.max(value, min), max)
|
|
738
|
+
: min + (((value - min) % (max - min + 1)) + (max - min + 1)) % (max - min + 1);
|
|
739
|
+
view.setInt32(element, normalized, true);
|
|
679
740
|
}
|
|
680
|
-
const value = view.getInt32(0, true);
|
|
681
|
-
const normalized = mode === "clamp"
|
|
682
|
-
? Math.min(Math.max(value, min), max)
|
|
683
|
-
: min + (((value - min) % (max - min + 1)) + (max - min + 1)) % (max - min + 1);
|
|
684
|
-
view.setInt32(0, normalized, true);
|
|
685
741
|
return;
|
|
686
742
|
}
|
|
687
743
|
let min;
|
|
@@ -690,26 +746,27 @@ class OndaWasmProcessor extends AudioWorkletProcessor {
|
|
|
690
746
|
min = BigInt(range.min?.value);
|
|
691
747
|
max = BigInt(range.max?.value);
|
|
692
748
|
} catch {
|
|
693
|
-
throw
|
|
749
|
+
throw ERROR.INVALID_STORAGE;
|
|
694
750
|
}
|
|
695
|
-
if (min > max || byteSize !== 8) {
|
|
696
|
-
throw
|
|
751
|
+
if (min > max || byteSize !== entry.array_len * 8) {
|
|
752
|
+
throw ERROR.INVALID_STORAGE;
|
|
697
753
|
}
|
|
698
|
-
const value = view.getBigInt64(0, true);
|
|
699
754
|
const width = max - min + 1n;
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
755
|
+
for (let element = 0; element < byteSize; element += 8) {
|
|
756
|
+
const value = view.getBigInt64(element, true);
|
|
757
|
+
const normalized = mode === "clamp"
|
|
758
|
+
? (value < min ? min : (value > max ? max : value))
|
|
759
|
+
: min + ((value - min) % width + width) % width;
|
|
760
|
+
view.setBigInt64(element, normalized, true);
|
|
761
|
+
}
|
|
704
762
|
}
|
|
705
763
|
|
|
706
764
|
snapshotBytes(value) {
|
|
765
|
+
if (!(value instanceof ArrayBuffer) && !ArrayBuffer.isView(value)) throw ERROR.INVALID_SNAPSHOT;
|
|
766
|
+
if (value.byteLength !== this.snapshotSizeBytes) throw ERROR.INVALID_SNAPSHOT;
|
|
707
767
|
if (value instanceof Uint8Array) return value;
|
|
708
768
|
if (value instanceof ArrayBuffer) return new Uint8Array(value);
|
|
709
|
-
|
|
710
|
-
return new Uint8Array(value.buffer, value.byteOffset, value.byteLength);
|
|
711
|
-
}
|
|
712
|
-
throw new Error("snapshot restore requires byte storage");
|
|
769
|
+
return new Uint8Array(value.buffer, value.byteOffset, value.byteLength);
|
|
713
770
|
}
|
|
714
771
|
|
|
715
772
|
validateSnapshotEntry(entry, packedOffset, physicalOffset, byteSize) {
|
|
@@ -723,9 +780,7 @@ class OndaWasmProcessor extends AudioWorkletProcessor {
|
|
|
723
780
|
|| packedOffset + byteSize > this.snapshotSizeBytes
|
|
724
781
|
|| physicalOffset + byteSize > this.stateSizeBytes
|
|
725
782
|
) {
|
|
726
|
-
throw
|
|
727
|
-
`state '${String(entry.name)}' has invalid snapshot metadata`,
|
|
728
|
-
);
|
|
783
|
+
throw ERROR.INVALID_STORAGE;
|
|
729
784
|
}
|
|
730
785
|
}
|
|
731
786
|
|
|
@@ -740,6 +795,7 @@ class OndaWasmProcessor extends AudioWorkletProcessor {
|
|
|
740
795
|
}
|
|
741
796
|
|
|
742
797
|
handleMessage(message) {
|
|
798
|
+
if (this.transportFailed) return;
|
|
743
799
|
try {
|
|
744
800
|
if (message.type === "set-param") {
|
|
745
801
|
this.setParam(
|
|
@@ -747,24 +803,29 @@ class OndaWasmProcessor extends AudioWorkletProcessor {
|
|
|
747
803
|
message.value,
|
|
748
804
|
message.element ?? null,
|
|
749
805
|
);
|
|
750
|
-
this.
|
|
806
|
+
this.publishStatus(message.requestId, STATUS_OPERATIONS.indexOf(message.type), 0);
|
|
751
807
|
} else if (message.type === "set-param-smoothing") {
|
|
752
|
-
const
|
|
808
|
+
const seconds = message.seconds;
|
|
809
|
+
const count = Math.ceil(seconds * this.compileSampleRate);
|
|
810
|
+
if (!Number.isFinite(seconds) || seconds < 0 || !Number.isSafeInteger(count)) {
|
|
811
|
+
throw ERROR.INVALID_SMOOTHING;
|
|
812
|
+
}
|
|
813
|
+
const samples = count > this.blockSize ? count : 0;
|
|
753
814
|
this.paramSmoothing.setDuration(samples, this.memoryView(), this.paramsPtr);
|
|
754
|
-
this.
|
|
815
|
+
this.publishStatus(message.requestId, STATUS_OPERATIONS.indexOf(message.type), 0);
|
|
755
816
|
} else if (message.type === "reset-params") {
|
|
756
817
|
this.writeParamDefaults();
|
|
757
818
|
this.paramSmoothing.reset(this.memoryView(), this.paramsPtr);
|
|
758
|
-
this.
|
|
819
|
+
this.publishStatus(message.requestId, STATUS_OPERATIONS.indexOf(message.type), 0);
|
|
759
820
|
} else if (message.type === "init") {
|
|
760
821
|
this.init(message.mode);
|
|
761
|
-
this.
|
|
822
|
+
this.publishStatus(message.requestId, STATUS_OPERATIONS.indexOf(message.type), 0);
|
|
762
823
|
} else if (message.type === "event") {
|
|
763
824
|
this.dispatchEvent(
|
|
764
825
|
message.event ?? message.name ?? message.index,
|
|
765
826
|
message.payload,
|
|
766
827
|
);
|
|
767
|
-
this.
|
|
828
|
+
this.publishStatus(message.requestId, STATUS_OPERATIONS.indexOf(message.type), 0);
|
|
768
829
|
} else if (message.type === "read-control-outputs") {
|
|
769
830
|
this.postResponse(message, {
|
|
770
831
|
type: "control-outputs",
|
|
@@ -786,46 +847,69 @@ class OndaWasmProcessor extends AudioWorkletProcessor {
|
|
|
786
847
|
);
|
|
787
848
|
} else if (message.type === "restore-snapshot") {
|
|
788
849
|
this.restoreSnapshot(message.snapshot ?? message.bytes);
|
|
789
|
-
this.
|
|
850
|
+
this.publishStatus(message.requestId, STATUS_OPERATIONS.indexOf(message.type), 0);
|
|
790
851
|
} else if (message.type === "delegate-subscription") {
|
|
791
852
|
this.setDelegateSubscription(message.enabled, message.subscriptionId);
|
|
853
|
+
this.publishStatus(message.requestId, STATUS_OPERATIONS.indexOf(message.type), 0);
|
|
792
854
|
} else if (message.type === "print-subscription") {
|
|
793
855
|
this.setPrintSubscription(message.enabled, message.subscriptionId);
|
|
856
|
+
this.publishStatus(message.requestId, STATUS_OPERATIONS.indexOf(message.type), 0);
|
|
794
857
|
} else {
|
|
795
|
-
throw
|
|
858
|
+
throw ERROR.UNKNOWN_OPERATION;
|
|
796
859
|
}
|
|
797
860
|
} catch (error) {
|
|
798
|
-
const
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
861
|
+
const status = failureStatus(error);
|
|
862
|
+
if (status === ERROR.UNEXPECTED_FAILURE || status === ERROR.INVALID_STORAGE || status === ERROR.INVALID_OUTPUT) {
|
|
863
|
+
this.invalidateState();
|
|
864
|
+
}
|
|
865
|
+
this.publishStatus(message.requestId, Math.max(0, STATUS_OPERATIONS.indexOf(message.type)), status);
|
|
866
|
+
} finally {
|
|
867
|
+
// Control-path wakeup for hosts without Atomics.waitAsync. Render failures
|
|
868
|
+
// still only write shared storage and are picked up by the polling reader.
|
|
869
|
+
if (this.executionStatusRing && message.notifyStatus) {
|
|
870
|
+
this.port.postMessage(this.statusReadyMessage);
|
|
871
|
+
}
|
|
872
|
+
}
|
|
873
|
+
}
|
|
874
|
+
|
|
875
|
+
publishStatus(requestId, operation, status) {
|
|
876
|
+
if (requestId === undefined && status === 0) return;
|
|
877
|
+
if (this.executionStatusRing) {
|
|
878
|
+
if (!writeExecutionStatus(this.executionStatusRing, requestId ?? 0, operation, status)) {
|
|
879
|
+
this.transportFailed = true;
|
|
880
|
+
this.invalidateState();
|
|
881
|
+
}
|
|
882
|
+
return;
|
|
806
883
|
}
|
|
884
|
+
// Reuse a prepared packet. Structured cloning by MessagePort can still
|
|
885
|
+
// allocate; browsers without shared memory have no allocation-free transport.
|
|
886
|
+
const message = this.statusMessage;
|
|
887
|
+
message.requestId = requestId ?? 0;
|
|
888
|
+
message.operation = operation;
|
|
889
|
+
message.status = status;
|
|
890
|
+
this.port.postMessage(message);
|
|
807
891
|
}
|
|
808
892
|
|
|
809
893
|
dispatchEvent(selector, payload) {
|
|
810
|
-
this.requireInitialized(
|
|
894
|
+
this.requireInitialized();
|
|
811
895
|
if (Number.isInteger(selector) && selector >= this.eventInfo.length) return;
|
|
812
896
|
const eventId = Number.isInteger(selector)
|
|
813
897
|
? selector
|
|
814
|
-
: this.
|
|
898
|
+
: this.eventIds.get(selector);
|
|
815
899
|
const event = this.eventInfo[eventId];
|
|
816
900
|
if (!event) {
|
|
817
|
-
throw
|
|
901
|
+
throw ERROR.UNKNOWN_EVENT;
|
|
818
902
|
}
|
|
819
|
-
if (!(payload instanceof Uint8Array)) throw
|
|
903
|
+
if (!(payload instanceof Uint8Array)) throw ERROR.INVALID_PAYLOAD;
|
|
820
904
|
const payloadSize = payload.byteLength;
|
|
821
905
|
if (payloadSize > this.eventPayloadCapacity) {
|
|
822
|
-
throw
|
|
906
|
+
throw ERROR.PAYLOAD_CAPACITY;
|
|
823
907
|
}
|
|
824
908
|
const view = this.memoryView();
|
|
825
909
|
this.eventPayloadView.set(payload);
|
|
826
910
|
const handler = this.exports[event.export];
|
|
827
911
|
if (typeof handler !== "function") {
|
|
828
|
-
throw
|
|
912
|
+
throw ERROR.INVALID_STORAGE;
|
|
829
913
|
}
|
|
830
914
|
view.setUint32(this.eventInputPtr, this.eventPayloadPtr, true);
|
|
831
915
|
view.setUint32(this.eventInputPtr + 4, payloadSize, true);
|
|
@@ -841,21 +925,15 @@ class OndaWasmProcessor extends AudioWorkletProcessor {
|
|
|
841
925
|
this.bufferSampleRatesPtr,
|
|
842
926
|
this.executionOutputPtr,
|
|
843
927
|
);
|
|
844
|
-
if (status === PROCESSOR_EXECUTION_INPUT_REJECTED) {
|
|
845
|
-
throw new ProcessorExecutionError(
|
|
846
|
-
`event '${event.name}' input rejected: invalid payload or insufficient workspace`,
|
|
847
|
-
status,
|
|
848
|
-
);
|
|
849
|
-
}
|
|
850
928
|
this.publishExecutionOutput(
|
|
851
929
|
EXECUTION_OPERATION_EVENT,
|
|
852
930
|
eventId,
|
|
853
931
|
);
|
|
854
|
-
this.checkExecutionStatus(status,
|
|
932
|
+
this.checkExecutionStatus(status, true);
|
|
855
933
|
}
|
|
856
934
|
|
|
857
935
|
readControlOutputs() {
|
|
858
|
-
this.requireInitialized(
|
|
936
|
+
this.requireInitialized();
|
|
859
937
|
return Object.fromEntries(
|
|
860
938
|
this.controlOutputInfo.map((output) => [
|
|
861
939
|
output.name,
|
|
@@ -901,10 +979,10 @@ class OndaWasmProcessor extends AudioWorkletProcessor {
|
|
|
901
979
|
? view.getUint32(this.printBatchPtr + 16, true)
|
|
902
980
|
: 0;
|
|
903
981
|
if (delegateUsed > this.delegateCapacity) {
|
|
904
|
-
throw
|
|
982
|
+
throw ERROR.INVALID_OUTPUT;
|
|
905
983
|
}
|
|
906
984
|
if (printUsed > this.printCapacity) {
|
|
907
|
-
throw
|
|
985
|
+
throw ERROR.INVALID_OUTPUT;
|
|
908
986
|
}
|
|
909
987
|
const hasCurrent = delegateUsed
|
|
910
988
|
|| delegateRecordCount
|
|
@@ -1113,10 +1191,10 @@ class OndaWasmProcessor extends AudioWorkletProcessor {
|
|
|
1113
1191
|
readBuffer(selector) {
|
|
1114
1192
|
const bufferId = Number.isInteger(selector)
|
|
1115
1193
|
? selector
|
|
1116
|
-
: this.
|
|
1194
|
+
: this.bufferIds.get(selector);
|
|
1117
1195
|
const binding = this.bufferBindings[bufferId];
|
|
1118
1196
|
if (!binding) {
|
|
1119
|
-
throw
|
|
1197
|
+
throw ERROR.UNKNOWN_BUFFER;
|
|
1120
1198
|
}
|
|
1121
1199
|
const length = binding.frames * binding.channels;
|
|
1122
1200
|
const elementSize = this.scalarByteSize(binding.scalar);
|
|
@@ -1208,20 +1286,20 @@ class OndaWasmProcessor extends AudioWorkletProcessor {
|
|
|
1208
1286
|
const isArrayValue = Array.isArray(value)
|
|
1209
1287
|
|| (ArrayBuffer.isView(value) && typeof value.length === "number");
|
|
1210
1288
|
if (isFixedArray !== isArrayValue) {
|
|
1211
|
-
throw
|
|
1212
|
-
isFixedArray
|
|
1213
|
-
? `'${info.name}' requires exactly ${length} ${info.scalar} value(s)`
|
|
1214
|
-
: `'${info.name}' requires one ${info.scalar} value`,
|
|
1215
|
-
);
|
|
1216
|
-
}
|
|
1217
|
-
if (isFixedArray && value.length !== length) {
|
|
1218
|
-
throw new Error(
|
|
1219
|
-
`'${info.name}' requires exactly ${length} ${info.scalar} value(s)`,
|
|
1220
|
-
);
|
|
1289
|
+
throw ERROR.INVALID_PARAMETER;
|
|
1221
1290
|
}
|
|
1291
|
+
if (isFixedArray && value.length !== length) throw ERROR.INVALID_PARAMETER;
|
|
1222
1292
|
return length;
|
|
1223
1293
|
}
|
|
1224
1294
|
|
|
1295
|
+
validateScalarValue(scalar, value) {
|
|
1296
|
+
if (scalar === "i64") {
|
|
1297
|
+
exactI64(value);
|
|
1298
|
+
} else if (typeof value !== "number" && typeof value !== "boolean" && typeof value !== "string") {
|
|
1299
|
+
throw ERROR.INVALID_PARAMETER;
|
|
1300
|
+
}
|
|
1301
|
+
}
|
|
1302
|
+
|
|
1225
1303
|
writeStorage(address, info, value, view = this.memoryView()) {
|
|
1226
1304
|
const length = this.validateStorageValue(info, value);
|
|
1227
1305
|
if (!this.isFixedArray(info)) {
|
|
@@ -1267,7 +1345,7 @@ class OndaWasmProcessor extends AudioWorkletProcessor {
|
|
|
1267
1345
|
if (scalar === "bool") return 1;
|
|
1268
1346
|
if (scalar === "i32" || scalar === "f32") return 4;
|
|
1269
1347
|
if (scalar === "i64" || scalar === "f64") return 8;
|
|
1270
|
-
throw
|
|
1348
|
+
throw ERROR.INVALID_STORAGE;
|
|
1271
1349
|
}
|
|
1272
1350
|
|
|
1273
1351
|
writeScalar(address, scalar, value, view = this.memoryView()) {
|
|
@@ -1276,7 +1354,7 @@ class OndaWasmProcessor extends AudioWorkletProcessor {
|
|
|
1276
1354
|
else if (scalar === "i64") view.setBigInt64(address, exactI64(value), true);
|
|
1277
1355
|
else if (scalar === "f32") view.setFloat32(address, Number(value), true);
|
|
1278
1356
|
else if (scalar === "f64") view.setFloat64(address, Number(value), true);
|
|
1279
|
-
else throw
|
|
1357
|
+
else throw ERROR.INVALID_STORAGE;
|
|
1280
1358
|
}
|
|
1281
1359
|
|
|
1282
1360
|
readScalar(address, scalar, view = this.memoryView()) {
|
|
@@ -1285,7 +1363,7 @@ class OndaWasmProcessor extends AudioWorkletProcessor {
|
|
|
1285
1363
|
if (scalar === "i64") return view.getBigInt64(address, true);
|
|
1286
1364
|
if (scalar === "f32") return view.getFloat32(address, true);
|
|
1287
1365
|
if (scalar === "f64") return view.getFloat64(address, true);
|
|
1288
|
-
throw
|
|
1366
|
+
throw ERROR.INVALID_STORAGE;
|
|
1289
1367
|
}
|
|
1290
1368
|
|
|
1291
1369
|
ensureInputCapacity(frames) {
|
|
@@ -1334,7 +1412,7 @@ class OndaWasmProcessor extends AudioWorkletProcessor {
|
|
|
1334
1412
|
const bus = inputs[busId];
|
|
1335
1413
|
if (bus.length > 0) return bus[0].length;
|
|
1336
1414
|
}
|
|
1337
|
-
throw
|
|
1415
|
+
throw ERROR.INVALID_AUDIO_SHAPE;
|
|
1338
1416
|
}
|
|
1339
1417
|
|
|
1340
1418
|
copyInputSamples(
|
|
@@ -1365,15 +1443,13 @@ class OndaWasmProcessor extends AudioWorkletProcessor {
|
|
|
1365
1443
|
}
|
|
1366
1444
|
if (info.scalar === "i32") {
|
|
1367
1445
|
for (let frame = 0; frame < segmentFrames; frame += 1) {
|
|
1368
|
-
target[startFrame + frame] =
|
|
1446
|
+
target[startFrame + frame] = audioI32(source[callbackOffset + frame]);
|
|
1369
1447
|
}
|
|
1370
1448
|
return;
|
|
1371
1449
|
}
|
|
1372
1450
|
if (info.scalar === "i64") {
|
|
1373
1451
|
for (let frame = 0; frame < segmentFrames; frame += 1) {
|
|
1374
|
-
target[startFrame + frame] =
|
|
1375
|
-
Math.trunc(source[callbackOffset + frame]),
|
|
1376
|
-
);
|
|
1452
|
+
target[startFrame + frame] = audioI64(source[callbackOffset + frame]);
|
|
1377
1453
|
}
|
|
1378
1454
|
return;
|
|
1379
1455
|
}
|
|
@@ -1383,7 +1459,7 @@ class OndaWasmProcessor extends AudioWorkletProcessor {
|
|
|
1383
1459
|
}
|
|
1384
1460
|
return;
|
|
1385
1461
|
}
|
|
1386
|
-
throw
|
|
1462
|
+
throw ERROR.INVALID_STORAGE;
|
|
1387
1463
|
}
|
|
1388
1464
|
|
|
1389
1465
|
for (let frame = 0; frame < segmentFrames; frame += 1) {
|
|
@@ -1394,9 +1470,9 @@ class OndaWasmProcessor extends AudioWorkletProcessor {
|
|
|
1394
1470
|
info.scalar === "bool"
|
|
1395
1471
|
? value !== 0
|
|
1396
1472
|
: info.scalar === "i64"
|
|
1397
|
-
?
|
|
1473
|
+
? audioI64(value)
|
|
1398
1474
|
: info.scalar === "i32"
|
|
1399
|
-
?
|
|
1475
|
+
? audioI32(value)
|
|
1400
1476
|
: value,
|
|
1401
1477
|
view,
|
|
1402
1478
|
);
|
|
@@ -1436,7 +1512,7 @@ class OndaWasmProcessor extends AudioWorkletProcessor {
|
|
|
1436
1512
|
for (let busChannel = 0; busChannel < bus.length; busChannel += 1) {
|
|
1437
1513
|
const source = bus[busChannel];
|
|
1438
1514
|
if (source.length !== callbackFrames) {
|
|
1439
|
-
throw
|
|
1515
|
+
throw ERROR.INVALID_AUDIO_SHAPE;
|
|
1440
1516
|
}
|
|
1441
1517
|
if (inputChannel < this.inputCount) {
|
|
1442
1518
|
const info = this.inputChannels[inputChannel];
|
|
@@ -1524,7 +1600,7 @@ class OndaWasmProcessor extends AudioWorkletProcessor {
|
|
|
1524
1600
|
for (let busChannel = 0; busChannel < bus.length; busChannel += 1) {
|
|
1525
1601
|
const destination = bus[busChannel];
|
|
1526
1602
|
if (destination.length !== callbackFrames) {
|
|
1527
|
-
throw
|
|
1603
|
+
throw ERROR.INVALID_AUDIO_SHAPE;
|
|
1528
1604
|
}
|
|
1529
1605
|
if (outputChannel < this.outputCount) {
|
|
1530
1606
|
const info = this.outputChannels[outputChannel];
|
|
@@ -1600,6 +1676,17 @@ class OndaWasmProcessor extends AudioWorkletProcessor {
|
|
|
1600
1676
|
}
|
|
1601
1677
|
|
|
1602
1678
|
processInitialized(inputs, outputs) {
|
|
1679
|
+
try {
|
|
1680
|
+
return this.render(inputs, outputs);
|
|
1681
|
+
} catch (error) {
|
|
1682
|
+
this.invalidateState();
|
|
1683
|
+
this.clearOutputs(outputs);
|
|
1684
|
+
this.publishStatus(0, 1, failureStatus(error));
|
|
1685
|
+
return true;
|
|
1686
|
+
}
|
|
1687
|
+
}
|
|
1688
|
+
|
|
1689
|
+
render(inputs, outputs) {
|
|
1603
1690
|
this.refreshMemoryCache();
|
|
1604
1691
|
const frames = this.audioFrameCount(inputs, outputs);
|
|
1605
1692
|
|
|
@@ -1637,12 +1724,7 @@ class OndaWasmProcessor extends AudioWorkletProcessor {
|
|
|
1637
1724
|
if (status !== PROCESSOR_EXECUTION_OK) {
|
|
1638
1725
|
this.invalidateState();
|
|
1639
1726
|
this.clearOutputs(outputs);
|
|
1640
|
-
this.
|
|
1641
|
-
type: "onda-error",
|
|
1642
|
-
operation: "process",
|
|
1643
|
-
status,
|
|
1644
|
-
error: `processor process failed with Onda execution status ${String(status)}`,
|
|
1645
|
-
});
|
|
1727
|
+
this.publishStatus(0, 1, status);
|
|
1646
1728
|
return true;
|
|
1647
1729
|
}
|
|
1648
1730
|
this.marshalOutputSegment(
|