@webmusic/kernel 0.1.0 → 0.4.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 -21
- package/NOTICE +2 -0
- package/README.md +138 -101
- package/dist/audio-context.cjs.map +1 -1
- package/dist/build-receipt.json +3 -3
- package/dist/chunk-27LH6QLZ.cjs +277 -0
- package/dist/chunk-27LH6QLZ.cjs.map +1 -0
- package/dist/chunk-3INDOVUC.cjs.map +1 -1
- package/dist/chunk-3RR2VIKA.js +393 -0
- package/dist/chunk-3RR2VIKA.js.map +1 -0
- package/dist/chunk-3YTKIKKX.cjs +378 -0
- package/dist/chunk-3YTKIKKX.cjs.map +1 -0
- package/dist/chunk-4RG4WQKC.js +360 -0
- package/dist/chunk-4RG4WQKC.js.map +1 -0
- package/dist/chunk-5AHVQXTF.cjs +620 -0
- package/dist/chunk-5AHVQXTF.cjs.map +1 -0
- package/dist/chunk-624KWJWZ.js +170 -0
- package/dist/chunk-624KWJWZ.js.map +1 -0
- package/dist/chunk-6HGFEKD3.cjs +851 -0
- package/dist/chunk-6HGFEKD3.cjs.map +1 -0
- package/dist/chunk-7ZTZZ5PN.cjs.map +1 -1
- package/dist/chunk-ALWV4P7D.js +726 -0
- package/dist/chunk-ALWV4P7D.js.map +1 -0
- package/dist/chunk-BADJJ6DV.cjs +143 -0
- package/dist/chunk-BADJJ6DV.cjs.map +1 -0
- package/dist/chunk-C3LMS36L.cjs +393 -0
- package/dist/chunk-C3LMS36L.cjs.map +1 -0
- package/dist/chunk-C3YWUZW3.cjs.map +1 -1
- package/dist/chunk-CR6OUWBJ.cjs +726 -0
- package/dist/chunk-CR6OUWBJ.cjs.map +1 -0
- package/dist/chunk-CYWDZLWC.js +2141 -0
- package/dist/chunk-CYWDZLWC.js.map +1 -0
- package/dist/chunk-DDOGX2D5.js +378 -0
- package/dist/chunk-DDOGX2D5.js.map +1 -0
- package/dist/chunk-DVN6E5U3.js +620 -0
- package/dist/chunk-DVN6E5U3.js.map +1 -0
- package/dist/chunk-EURMRU2S.js +208 -0
- package/dist/chunk-EURMRU2S.js.map +1 -0
- package/dist/chunk-EXDCK2VL.cjs +20 -0
- package/dist/chunk-EXDCK2VL.cjs.map +1 -0
- package/dist/chunk-G5EIUP52.js +114 -0
- package/dist/chunk-G5EIUP52.js.map +1 -0
- package/dist/chunk-GB4DGKLZ.js +143 -0
- package/dist/chunk-GB4DGKLZ.js.map +1 -0
- package/dist/chunk-GPOEAYOW.js +2147 -0
- package/dist/chunk-GPOEAYOW.js.map +1 -0
- package/dist/chunk-I4YMA5VM.cjs +120 -0
- package/dist/chunk-I4YMA5VM.cjs.map +1 -0
- package/dist/chunk-IVWHMDCB.cjs +4383 -0
- package/dist/chunk-IVWHMDCB.cjs.map +1 -0
- package/dist/chunk-JFCBJMA7.cjs +2147 -0
- package/dist/chunk-JFCBJMA7.cjs.map +1 -0
- package/dist/chunk-JLVSAZ3K.cjs +813 -0
- package/dist/chunk-JLVSAZ3K.cjs.map +1 -0
- package/dist/chunk-K7FYH6UG.cjs +208 -0
- package/dist/chunk-K7FYH6UG.cjs.map +1 -0
- package/dist/chunk-L4MYCETM.cjs +970 -0
- package/dist/chunk-L4MYCETM.cjs.map +1 -0
- package/dist/chunk-MNCMYNYL.js +970 -0
- package/dist/chunk-MNCMYNYL.js.map +1 -0
- package/dist/chunk-NT2BZ3QM.js +120 -0
- package/dist/chunk-NT2BZ3QM.js.map +1 -0
- package/dist/chunk-O7SA5GLC.js +277 -0
- package/dist/chunk-O7SA5GLC.js.map +1 -0
- package/dist/chunk-OCKAHITL.cjs +548 -0
- package/dist/chunk-OCKAHITL.cjs.map +1 -0
- package/dist/{chunk-HOKRTYPV.js → chunk-OPBHDUZK.js} +39 -1
- package/dist/chunk-OPBHDUZK.js.map +1 -0
- package/dist/chunk-OTXYWCLA.js +206 -0
- package/dist/chunk-OTXYWCLA.js.map +1 -0
- package/dist/chunk-P6ZA63J7.js +548 -0
- package/dist/chunk-P6ZA63J7.js.map +1 -0
- package/dist/chunk-QLF3BOST.js +20 -0
- package/dist/chunk-QLF3BOST.js.map +1 -0
- package/dist/chunk-RCWFRZDR.js +4383 -0
- package/dist/chunk-RCWFRZDR.js.map +1 -0
- package/dist/chunk-RZBLPM3L.cjs +206 -0
- package/dist/chunk-RZBLPM3L.cjs.map +1 -0
- package/dist/chunk-TA3TNJZU.cjs +360 -0
- package/dist/chunk-TA3TNJZU.cjs.map +1 -0
- package/dist/chunk-TZSRPTQL.cjs +2141 -0
- package/dist/chunk-TZSRPTQL.cjs.map +1 -0
- package/dist/chunk-UMTPJ76S.cjs.map +1 -1
- package/dist/chunk-UY77FXDW.js +813 -0
- package/dist/chunk-UY77FXDW.js.map +1 -0
- package/dist/chunk-VFU3S7OC.js +851 -0
- package/dist/chunk-VFU3S7OC.js.map +1 -0
- package/dist/chunk-VY6WWZSW.cjs.map +1 -1
- package/dist/chunk-WN5DZOVT.cjs +170 -0
- package/dist/chunk-WN5DZOVT.cjs.map +1 -0
- package/dist/{chunk-HZQX4J2J.cjs → chunk-WOPW6WLR.cjs} +40 -2
- package/dist/chunk-WOPW6WLR.cjs.map +1 -0
- package/dist/chunk-XBSIYUHM.cjs +114 -0
- package/dist/chunk-XBSIYUHM.cjs.map +1 -0
- package/dist/chunk-ZMU72HJS.cjs.map +1 -1
- package/dist/context-DSa0ymyN.d.cts +664 -0
- package/dist/context-DaxfAg3b.d.ts +664 -0
- package/dist/effect-chain-Cu03O5Y0.d.ts +91 -0
- package/dist/effect-chain-Do75T7uK.d.cts +91 -0
- package/dist/effect.cjs +16 -1
- package/dist/effect.cjs.map +1 -1
- package/dist/effect.d.cts +56 -14
- package/dist/effect.d.ts +56 -14
- package/dist/effect.js +16 -1
- package/dist/elements.cjs +37 -2
- package/dist/elements.cjs.map +1 -1
- package/dist/elements.d.cts +197 -8
- package/dist/elements.d.ts +197 -8
- package/dist/elements.js +36 -1
- package/dist/events.cjs.map +1 -1
- package/dist/index.cjs +556 -9
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +17 -5
- package/dist/index.d.ts +17 -5
- package/dist/index.js +567 -20
- package/dist/message.cjs +104 -0
- package/dist/message.cjs.map +1 -0
- package/dist/message.d.cts +738 -0
- package/dist/message.d.ts +738 -0
- package/dist/message.js +104 -0
- package/dist/meter.cjs +2 -2
- package/dist/meter.cjs.map +1 -1
- package/dist/meter.d.cts +9 -1
- package/dist/meter.d.ts +9 -1
- package/dist/meter.js +1 -1
- package/dist/module-control-C25BMtH7.d.ts +61 -0
- package/dist/module-control-yH6nTJM9.d.cts +61 -0
- package/dist/module-plugin-BnPmq1VP.d.ts +198 -0
- package/dist/module-plugin-CHb-5W0Y.d.cts +198 -0
- package/dist/module.cjs +263 -0
- package/dist/module.cjs.map +1 -0
- package/dist/module.d.cts +1112 -0
- package/dist/module.d.ts +1112 -0
- package/dist/module.js +263 -0
- package/dist/parameter-DwQzFmBR.d.cts +172 -0
- package/dist/parameter-DwQzFmBR.d.ts +172 -0
- package/dist/player.cjs.map +1 -1
- package/dist/processor-worklet.cjs +18 -0
- package/dist/processor-worklet.cjs.map +1 -0
- package/dist/processor-worklet.d.cts +166 -0
- package/dist/processor-worklet.d.ts +166 -0
- package/dist/processor-worklet.js +18 -0
- package/dist/processor-worklet.js.map +1 -0
- package/dist/processor.cjs +51 -0
- package/dist/processor.cjs.map +1 -0
- package/dist/processor.d.cts +397 -0
- package/dist/processor.d.ts +397 -0
- package/dist/processor.js +51 -0
- package/dist/processor.js.map +1 -0
- package/dist/project-types-BU4awMnL.d.ts +323 -0
- package/dist/project-types-DufCV8Ma.d.cts +323 -0
- package/dist/ring-C-i9N90a.d.cts +139 -0
- package/dist/ring-DkcMt2Eg.d.ts +139 -0
- package/dist/sequence.cjs +68 -0
- package/dist/sequence.cjs.map +1 -0
- package/dist/sequence.d.cts +303 -0
- package/dist/sequence.d.ts +303 -0
- package/dist/sequence.js +68 -0
- package/dist/sequence.js.map +1 -0
- package/dist/session.cjs +16 -0
- package/dist/session.cjs.map +1 -0
- package/dist/session.d.cts +407 -0
- package/dist/session.d.ts +407 -0
- package/dist/session.js +16 -0
- package/dist/session.js.map +1 -0
- package/dist/tick.cjs.map +1 -1
- package/dist/transport.cjs +50 -2
- package/dist/transport.cjs.map +1 -1
- package/dist/transport.d.cts +145 -1
- package/dist/transport.d.ts +145 -1
- package/dist/transport.js +51 -3
- package/dist/worker.cjs.map +1 -1
- package/package.json +62 -11
- package/dist/chunk-53YD73OY.js +0 -162
- package/dist/chunk-53YD73OY.js.map +0 -1
- package/dist/chunk-BR63OD53.cjs +0 -1
- package/dist/chunk-BR63OD53.cjs.map +0 -1
- package/dist/chunk-HOKRTYPV.js.map +0 -1
- package/dist/chunk-HZQX4J2J.cjs.map +0 -1
- package/dist/chunk-JNXB74G3.cjs +0 -987
- package/dist/chunk-JNXB74G3.cjs.map +0 -1
- package/dist/chunk-SM5YPDDI.cjs +0 -162
- package/dist/chunk-SM5YPDDI.cjs.map +0 -1
- package/dist/chunk-TEM2I2NN.js +0 -1
- package/dist/chunk-VVZX7DQ6.cjs +0 -254
- package/dist/chunk-VVZX7DQ6.cjs.map +0 -1
- package/dist/chunk-WNFGDB7B.js +0 -254
- package/dist/chunk-WNFGDB7B.js.map +0 -1
- package/dist/chunk-XBJBY4J5.js +0 -987
- package/dist/chunk-XBJBY4J5.js.map +0 -1
- package/dist/sync.cjs +0 -12
- package/dist/sync.cjs.map +0 -1
- package/dist/sync.d.cts +0 -304
- package/dist/sync.d.ts +0 -304
- package/dist/sync.js +0 -12
- /package/dist/{chunk-TEM2I2NN.js.map → message.js.map} +0 -0
- /package/dist/{sync.js.map → module.js.map} +0 -0
|
@@ -0,0 +1,4383 @@
|
|
|
1
|
+
import {
|
|
2
|
+
AudioInputPort,
|
|
3
|
+
AudioOutputPort,
|
|
4
|
+
ModuleAudioEnvironment,
|
|
5
|
+
ModuleContext,
|
|
6
|
+
createControlPort,
|
|
7
|
+
moduleScopeChange,
|
|
8
|
+
routeModuleOutput
|
|
9
|
+
} from "./chunk-P6ZA63J7.js";
|
|
10
|
+
import {
|
|
11
|
+
NoteInputPort,
|
|
12
|
+
NoteOutputPort
|
|
13
|
+
} from "./chunk-EURMRU2S.js";
|
|
14
|
+
import {
|
|
15
|
+
SessionTransport,
|
|
16
|
+
attachSessionAutomation
|
|
17
|
+
} from "./chunk-GPOEAYOW.js";
|
|
18
|
+
import {
|
|
19
|
+
InsertChain
|
|
20
|
+
} from "./chunk-3RR2VIKA.js";
|
|
21
|
+
import {
|
|
22
|
+
ParamHandle,
|
|
23
|
+
ParamSurface
|
|
24
|
+
} from "./chunk-DVN6E5U3.js";
|
|
25
|
+
import {
|
|
26
|
+
SourceRelay
|
|
27
|
+
} from "./chunk-G5EIUP52.js";
|
|
28
|
+
import {
|
|
29
|
+
ControlModule,
|
|
30
|
+
MODULE_CONTROL
|
|
31
|
+
} from "./chunk-624KWJWZ.js";
|
|
32
|
+
import {
|
|
33
|
+
GraphModule,
|
|
34
|
+
MUSICAL_TIME,
|
|
35
|
+
MessageInputPort,
|
|
36
|
+
MessageOutputPort,
|
|
37
|
+
atomsOf,
|
|
38
|
+
connectMessageToParameter,
|
|
39
|
+
createMusicalTimeSource,
|
|
40
|
+
currentMessageDepth,
|
|
41
|
+
floatAtom,
|
|
42
|
+
intAtom,
|
|
43
|
+
isNumberAtom,
|
|
44
|
+
message,
|
|
45
|
+
modulePlugin,
|
|
46
|
+
numberOf,
|
|
47
|
+
numberedPort
|
|
48
|
+
} from "./chunk-ALWV4P7D.js";
|
|
49
|
+
import {
|
|
50
|
+
ModuleCapabilities,
|
|
51
|
+
PortListeners,
|
|
52
|
+
ProjectModuleUnavailableError,
|
|
53
|
+
assertPortInfo,
|
|
54
|
+
createCapabilityKey,
|
|
55
|
+
isProjectModuleUnavailable
|
|
56
|
+
} from "./chunk-NT2BZ3QM.js";
|
|
57
|
+
import {
|
|
58
|
+
createMusicalTempo,
|
|
59
|
+
timeValueSeconds
|
|
60
|
+
} from "./chunk-4RG4WQKC.js";
|
|
61
|
+
import {
|
|
62
|
+
ModuleRegistry,
|
|
63
|
+
assertModuleId,
|
|
64
|
+
createModuleHandle,
|
|
65
|
+
getModulePort
|
|
66
|
+
} from "./chunk-GB4DGKLZ.js";
|
|
67
|
+
import {
|
|
68
|
+
createConnectionLease
|
|
69
|
+
} from "./chunk-QLF3BOST.js";
|
|
70
|
+
import {
|
|
71
|
+
__privateAdd,
|
|
72
|
+
__privateGet,
|
|
73
|
+
__privateMethod,
|
|
74
|
+
__privateSet,
|
|
75
|
+
__privateWrapper
|
|
76
|
+
} from "./chunk-PXG64RU4.js";
|
|
77
|
+
|
|
78
|
+
// src/module/preset.ts
|
|
79
|
+
var _slots;
|
|
80
|
+
var PresetBank = class {
|
|
81
|
+
constructor(initial) {
|
|
82
|
+
__privateAdd(this, _slots, /* @__PURE__ */ new Map());
|
|
83
|
+
for (const [slot, values] of Object.entries(initial ?? {})) this.store(Number(slot), values);
|
|
84
|
+
}
|
|
85
|
+
/** Stored slot numbers, ascending. */
|
|
86
|
+
get slots() {
|
|
87
|
+
return [...__privateGet(this, _slots).keys()].sort((a, b) => a - b);
|
|
88
|
+
}
|
|
89
|
+
has(slot) {
|
|
90
|
+
return __privateGet(this, _slots).has(slot);
|
|
91
|
+
}
|
|
92
|
+
store(slot, values) {
|
|
93
|
+
if (!Number.isInteger(slot) || slot < 1) throw new RangeError("A preset slot is a positive integer.");
|
|
94
|
+
const copy = {};
|
|
95
|
+
for (const [key, value] of Object.entries(values)) {
|
|
96
|
+
if (Array.isArray(value)) {
|
|
97
|
+
if (value.every(Number.isFinite)) copy[key] = Object.freeze([...value]);
|
|
98
|
+
} else if (Number.isFinite(value)) copy[key] = value;
|
|
99
|
+
}
|
|
100
|
+
__privateGet(this, _slots).set(slot, Object.freeze(copy));
|
|
101
|
+
}
|
|
102
|
+
recall(slot) {
|
|
103
|
+
return __privateGet(this, _slots).get(slot);
|
|
104
|
+
}
|
|
105
|
+
clear(slot) {
|
|
106
|
+
__privateGet(this, _slots).delete(slot);
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Blend stored slots by weight. Weights are normalized; a key present in
|
|
110
|
+
* only some slots takes those slots' share. Numbers and arrays of equal
|
|
111
|
+
* length blend linearly; anything else takes the heaviest slot's value.
|
|
112
|
+
*/
|
|
113
|
+
interpolate(weights) {
|
|
114
|
+
const entries = Object.entries(weights).map(([slot, weight]) => [__privateGet(this, _slots).get(Number(slot)), Math.max(0, weight)]).filter((entry) => !!entry[0] && entry[1] > 0);
|
|
115
|
+
const result = {};
|
|
116
|
+
if (!entries.length) return result;
|
|
117
|
+
const keys = new Set(entries.flatMap(([values]) => Object.keys(values)));
|
|
118
|
+
for (const key of keys) {
|
|
119
|
+
const present = entries.filter(([values]) => values[key] !== void 0);
|
|
120
|
+
const total = present.reduce((sum, [, weight]) => sum + weight, 0);
|
|
121
|
+
const first = present[0][0][key];
|
|
122
|
+
if (typeof first === "number" && present.every(([values]) => typeof values[key] === "number")) {
|
|
123
|
+
result[key] = present.reduce((sum, [values, weight]) => sum + values[key] * weight, 0) / total;
|
|
124
|
+
} else if (Array.isArray(first) && present.every(([values]) => Array.isArray(values[key]) && values[key].length === first.length)) {
|
|
125
|
+
result[key] = first.map((_, index) => present.reduce((sum, [values, weight]) => sum + values[key][index] * weight, 0) / total);
|
|
126
|
+
} else {
|
|
127
|
+
result[key] = [...present].sort((a, b) => b[1] - a[1])[0][0][key];
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
return result;
|
|
131
|
+
}
|
|
132
|
+
/** Blend two slots: `fraction` 0 is `from`, 1 is `to`. */
|
|
133
|
+
between(from, to, fraction) {
|
|
134
|
+
const amount = Math.min(1, Math.max(0, fraction));
|
|
135
|
+
return this.interpolate(from === to ? { [from]: 1 } : { [from]: 1 - amount, [to]: amount });
|
|
136
|
+
}
|
|
137
|
+
toJSON() {
|
|
138
|
+
return Object.fromEntries(this.slots.map((slot) => [String(slot), Object.fromEntries(Object.entries(__privateGet(this, _slots).get(slot)).map(([key, value]) => [key, Array.isArray(value) ? [...value] : value]))]));
|
|
139
|
+
}
|
|
140
|
+
};
|
|
141
|
+
_slots = new WeakMap();
|
|
142
|
+
function captureParameters(modules) {
|
|
143
|
+
const values = {};
|
|
144
|
+
for (const module of modules) {
|
|
145
|
+
for (const handle of module.params ?? []) {
|
|
146
|
+
if (!handle.snapshot.disposed) values[`${module.id}.${handle.descriptor.id}`] = handle.snapshot.baseValue;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
return values;
|
|
150
|
+
}
|
|
151
|
+
function applyParameters(modules, values) {
|
|
152
|
+
let applied = 0;
|
|
153
|
+
for (const module of modules) {
|
|
154
|
+
for (const handle of module.params ?? []) {
|
|
155
|
+
const value = values[`${module.id}.${handle.descriptor.id}`];
|
|
156
|
+
if (typeof value !== "number" || handle.snapshot.disposed) continue;
|
|
157
|
+
const { min, max } = handle.descriptor.range;
|
|
158
|
+
try {
|
|
159
|
+
handle.setBaseValue(Math.min(max, Math.max(min, value)));
|
|
160
|
+
applied++;
|
|
161
|
+
} catch {
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
return applied;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
// src/module/mapping.ts
|
|
169
|
+
function controlFraction(event) {
|
|
170
|
+
switch (event.kind) {
|
|
171
|
+
case "cc":
|
|
172
|
+
case "program":
|
|
173
|
+
case "pressure":
|
|
174
|
+
return Math.min(1, Math.max(0, event.value / 127));
|
|
175
|
+
case "pitchbend":
|
|
176
|
+
return Math.min(1, Math.max(0, (event.value + 8192) / 16383));
|
|
177
|
+
case "key":
|
|
178
|
+
return event.down ? 1 : void 0;
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
function controlSource(event) {
|
|
182
|
+
switch (event.kind) {
|
|
183
|
+
case "cc":
|
|
184
|
+
return { kind: "cc", controller: event.controller, channel: event.channel };
|
|
185
|
+
case "pitchbend":
|
|
186
|
+
return { kind: "pitchbend", channel: event.channel };
|
|
187
|
+
case "program":
|
|
188
|
+
return { kind: "program", channel: event.channel };
|
|
189
|
+
case "pressure":
|
|
190
|
+
return { kind: "pressure", channel: event.channel };
|
|
191
|
+
case "key":
|
|
192
|
+
return { kind: "key", key: event.key };
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
function sourceMatches(source, event) {
|
|
196
|
+
if (source.kind !== event.kind) return false;
|
|
197
|
+
if (source.kind === "key") return event.kind === "key" && source.key === event.key;
|
|
198
|
+
if (source.kind === "cc" && (event.kind !== "cc" || source.controller !== event.controller)) return false;
|
|
199
|
+
return source.channel === void 0 || event.kind !== "key" && source.channel === event.channel;
|
|
200
|
+
}
|
|
201
|
+
function describeSource(source) {
|
|
202
|
+
const channel = "channel" in source && source.channel !== void 0 ? ` ch ${source.channel + 1}` : "";
|
|
203
|
+
switch (source.kind) {
|
|
204
|
+
case "cc":
|
|
205
|
+
return `CC ${source.controller}${channel}`;
|
|
206
|
+
case "pitchbend":
|
|
207
|
+
return `Bend${channel}`;
|
|
208
|
+
case "program":
|
|
209
|
+
return `Program${channel}`;
|
|
210
|
+
case "pressure":
|
|
211
|
+
return `Pressure${channel}`;
|
|
212
|
+
case "key":
|
|
213
|
+
return `Key ${source.key.length === 1 ? source.key.toUpperCase() : source.key}`;
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
var _mappings, _learning, _keyState, _options, _listeners, _ControlMappings_instances, notify_fn;
|
|
217
|
+
var ControlMappings = class {
|
|
218
|
+
constructor(options, mappings2 = []) {
|
|
219
|
+
__privateAdd(this, _ControlMappings_instances);
|
|
220
|
+
__privateAdd(this, _mappings, []);
|
|
221
|
+
__privateAdd(this, _learning);
|
|
222
|
+
__privateAdd(this, _keyState, /* @__PURE__ */ new Map());
|
|
223
|
+
__privateAdd(this, _options);
|
|
224
|
+
__privateAdd(this, _listeners, /* @__PURE__ */ new Set());
|
|
225
|
+
__privateSet(this, _options, options);
|
|
226
|
+
__privateSet(this, _mappings, mappings2);
|
|
227
|
+
}
|
|
228
|
+
get mappings() {
|
|
229
|
+
return __privateGet(this, _mappings);
|
|
230
|
+
}
|
|
231
|
+
/** The target waiting for its next control event, if any. */
|
|
232
|
+
get learning() {
|
|
233
|
+
return __privateGet(this, _learning);
|
|
234
|
+
}
|
|
235
|
+
/** Replace the list, as when a project opens. */
|
|
236
|
+
set(mappings2) {
|
|
237
|
+
__privateSet(this, _mappings, mappings2);
|
|
238
|
+
__privateGet(this, _keyState).clear();
|
|
239
|
+
__privateMethod(this, _ControlMappings_instances, notify_fn).call(this);
|
|
240
|
+
}
|
|
241
|
+
/** The next control event maps to `target`, replacing that target's previous mapping. */
|
|
242
|
+
learn(target) {
|
|
243
|
+
__privateSet(this, _learning, target);
|
|
244
|
+
__privateMethod(this, _ControlMappings_instances, notify_fn).call(this);
|
|
245
|
+
}
|
|
246
|
+
cancelLearn() {
|
|
247
|
+
if (__privateGet(this, _learning)) {
|
|
248
|
+
__privateSet(this, _learning, void 0);
|
|
249
|
+
__privateMethod(this, _ControlMappings_instances, notify_fn).call(this);
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
remove(id) {
|
|
253
|
+
const next = __privateGet(this, _mappings).filter((mapping) => mapping.id !== id);
|
|
254
|
+
if (next.length === __privateGet(this, _mappings).length) return;
|
|
255
|
+
__privateSet(this, _mappings, next);
|
|
256
|
+
__privateGet(this, _options).onChange?.(next);
|
|
257
|
+
__privateMethod(this, _ControlMappings_instances, notify_fn).call(this);
|
|
258
|
+
}
|
|
259
|
+
/** Apply one event. Returns true when it learned a mapping or set at least one parameter. */
|
|
260
|
+
handle(event) {
|
|
261
|
+
if (__privateGet(this, _learning) && (event.kind !== "key" || event.down)) {
|
|
262
|
+
const target = __privateGet(this, _learning);
|
|
263
|
+
__privateSet(this, _learning, void 0);
|
|
264
|
+
const source = controlSource(event);
|
|
265
|
+
const kept = __privateGet(this, _mappings).filter((mapping) => !(mapping.target.moduleId === target.moduleId && mapping.target.param === target.param));
|
|
266
|
+
let index = 1;
|
|
267
|
+
const used = new Set(kept.map((mapping) => mapping.id));
|
|
268
|
+
while (used.has(`map${index}`)) index++;
|
|
269
|
+
__privateSet(this, _mappings, [...kept, { id: `map${index}`, source, target }]);
|
|
270
|
+
__privateGet(this, _options).onChange?.(__privateGet(this, _mappings));
|
|
271
|
+
__privateMethod(this, _ControlMappings_instances, notify_fn).call(this);
|
|
272
|
+
return true;
|
|
273
|
+
}
|
|
274
|
+
let applied = false;
|
|
275
|
+
for (const mapping of __privateGet(this, _mappings)) {
|
|
276
|
+
if (!sourceMatches(mapping.source, event)) continue;
|
|
277
|
+
const handle = __privateGet(this, _options).resolve(mapping.target);
|
|
278
|
+
if (!handle || handle.snapshot.disposed) continue;
|
|
279
|
+
const range = handle.descriptor.displayRange ?? handle.descriptor.range;
|
|
280
|
+
const min = mapping.min ?? range.min, max = mapping.max ?? range.max;
|
|
281
|
+
let fraction = controlFraction(event);
|
|
282
|
+
if (event.kind === "key") {
|
|
283
|
+
if (!event.down) continue;
|
|
284
|
+
const on = !(__privateGet(this, _keyState).get(mapping.id) ?? false);
|
|
285
|
+
__privateGet(this, _keyState).set(mapping.id, on);
|
|
286
|
+
fraction = on ? 1 : 0;
|
|
287
|
+
}
|
|
288
|
+
if (fraction === void 0) continue;
|
|
289
|
+
const value = min + fraction * (max - min);
|
|
290
|
+
const { min: legalMin, max: legalMax } = handle.descriptor.range;
|
|
291
|
+
try {
|
|
292
|
+
handle.setBaseValue(Math.min(legalMax, Math.max(legalMin, value)));
|
|
293
|
+
applied = true;
|
|
294
|
+
} catch {
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
return applied;
|
|
298
|
+
}
|
|
299
|
+
subscribe(listener) {
|
|
300
|
+
__privateGet(this, _listeners).add(listener);
|
|
301
|
+
return () => {
|
|
302
|
+
__privateGet(this, _listeners).delete(listener);
|
|
303
|
+
};
|
|
304
|
+
}
|
|
305
|
+
};
|
|
306
|
+
_mappings = new WeakMap();
|
|
307
|
+
_learning = new WeakMap();
|
|
308
|
+
_keyState = new WeakMap();
|
|
309
|
+
_options = new WeakMap();
|
|
310
|
+
_listeners = new WeakMap();
|
|
311
|
+
_ControlMappings_instances = new WeakSet();
|
|
312
|
+
notify_fn = function() {
|
|
313
|
+
for (const listener of [...__privateGet(this, _listeners)]) {
|
|
314
|
+
try {
|
|
315
|
+
listener();
|
|
316
|
+
} catch {
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
};
|
|
320
|
+
|
|
321
|
+
// src/module/voices.ts
|
|
322
|
+
var _held, _order, _next;
|
|
323
|
+
var VoiceAllocator = class {
|
|
324
|
+
constructor(voices, steal = "oldest") {
|
|
325
|
+
__privateAdd(this, _held, /* @__PURE__ */ new Map());
|
|
326
|
+
__privateAdd(this, _order, []);
|
|
327
|
+
__privateAdd(this, _next, 0);
|
|
328
|
+
if (!Number.isInteger(voices) || voices < 1 || voices > 256) throw new RangeError("A voice allocator needs 1 to 256 voices.");
|
|
329
|
+
this.voices = voices;
|
|
330
|
+
this.steal = steal;
|
|
331
|
+
}
|
|
332
|
+
/** Keys currently holding a voice, oldest first. */
|
|
333
|
+
get held() {
|
|
334
|
+
return [...__privateGet(this, _order)];
|
|
335
|
+
}
|
|
336
|
+
/** The voice playing `key`, if any. */
|
|
337
|
+
voiceOf(key) {
|
|
338
|
+
return __privateGet(this, _held).get(key);
|
|
339
|
+
}
|
|
340
|
+
/** Give `key` a voice: a free one in turn, or the oldest held voice when all are busy. */
|
|
341
|
+
noteOn(key) {
|
|
342
|
+
const existing = __privateGet(this, _held).get(key);
|
|
343
|
+
if (existing !== void 0) return { voice: existing };
|
|
344
|
+
const busy = new Set(__privateGet(this, _held).values());
|
|
345
|
+
for (let offset = 0; offset < this.voices; offset++) {
|
|
346
|
+
const voice2 = (__privateGet(this, _next) + offset) % this.voices;
|
|
347
|
+
if (busy.has(voice2)) continue;
|
|
348
|
+
__privateSet(this, _next, (voice2 + 1) % this.voices);
|
|
349
|
+
__privateGet(this, _held).set(key, voice2);
|
|
350
|
+
__privateGet(this, _order).push(key);
|
|
351
|
+
return { voice: voice2 };
|
|
352
|
+
}
|
|
353
|
+
if (this.steal === "none") return { voice: void 0 };
|
|
354
|
+
const stolen = __privateGet(this, _order).shift();
|
|
355
|
+
const voice = __privateGet(this, _held).get(stolen);
|
|
356
|
+
__privateGet(this, _held).delete(stolen);
|
|
357
|
+
__privateGet(this, _held).set(key, voice);
|
|
358
|
+
__privateGet(this, _order).push(key);
|
|
359
|
+
return { voice, stolen };
|
|
360
|
+
}
|
|
361
|
+
/** Free the voice of `key`; returns it, or undefined when the key held none. */
|
|
362
|
+
noteOff(key) {
|
|
363
|
+
const voice = __privateGet(this, _held).get(key);
|
|
364
|
+
if (voice === void 0) return void 0;
|
|
365
|
+
__privateGet(this, _held).delete(key);
|
|
366
|
+
__privateGet(this, _order).splice(__privateGet(this, _order).indexOf(key), 1);
|
|
367
|
+
return voice;
|
|
368
|
+
}
|
|
369
|
+
/** Free every voice. */
|
|
370
|
+
clear() {
|
|
371
|
+
__privateGet(this, _held).clear();
|
|
372
|
+
__privateGet(this, _order).length = 0;
|
|
373
|
+
__privateSet(this, _next, 0);
|
|
374
|
+
}
|
|
375
|
+
};
|
|
376
|
+
_held = new WeakMap();
|
|
377
|
+
_order = new WeakMap();
|
|
378
|
+
_next = new WeakMap();
|
|
379
|
+
|
|
380
|
+
// src/module/observe.ts
|
|
381
|
+
var _clock, _watchpoints, _listeners2, _stop, _holding, _last, _disposed, _MessageDebugger_instances, check_fn;
|
|
382
|
+
var MessageDebugger = class {
|
|
383
|
+
constructor(runtime, clock) {
|
|
384
|
+
__privateAdd(this, _MessageDebugger_instances);
|
|
385
|
+
__privateAdd(this, _clock);
|
|
386
|
+
__privateAdd(this, _watchpoints, /* @__PURE__ */ new Map());
|
|
387
|
+
__privateAdd(this, _listeners2, /* @__PURE__ */ new Set());
|
|
388
|
+
__privateAdd(this, _stop);
|
|
389
|
+
__privateAdd(this, _holding, false);
|
|
390
|
+
__privateAdd(this, _last);
|
|
391
|
+
__privateAdd(this, _disposed, false);
|
|
392
|
+
__privateSet(this, _clock, clock);
|
|
393
|
+
__privateSet(this, _stop, runtime.observe((event) => __privateMethod(this, _MessageDebugger_instances, check_fn).call(this, event)));
|
|
394
|
+
}
|
|
395
|
+
get watchpoints() {
|
|
396
|
+
return [...__privateGet(this, _watchpoints).values()];
|
|
397
|
+
}
|
|
398
|
+
/** True while a break holds the clock. */
|
|
399
|
+
get paused() {
|
|
400
|
+
return __privateGet(this, _holding) && __privateGet(this, _clock).held;
|
|
401
|
+
}
|
|
402
|
+
/** The latest hit, for a debugger view. */
|
|
403
|
+
get lastHit() {
|
|
404
|
+
return __privateGet(this, _last);
|
|
405
|
+
}
|
|
406
|
+
set(watchpoint) {
|
|
407
|
+
if (__privateGet(this, _disposed)) throw new Error("The debugger is disposed.");
|
|
408
|
+
if (watchpoint.mode !== "trace" && watchpoint.mode !== "break") throw new TypeError("A watchpoint traces or breaks.");
|
|
409
|
+
__privateGet(this, _watchpoints).set(watchpoint.id, Object.freeze({ ...watchpoint }));
|
|
410
|
+
}
|
|
411
|
+
remove(id) {
|
|
412
|
+
__privateGet(this, _watchpoints).delete(id);
|
|
413
|
+
}
|
|
414
|
+
onHit(listener) {
|
|
415
|
+
__privateGet(this, _listeners2).add(listener);
|
|
416
|
+
return () => {
|
|
417
|
+
__privateGet(this, _listeners2).delete(listener);
|
|
418
|
+
};
|
|
419
|
+
}
|
|
420
|
+
continue() {
|
|
421
|
+
if (!__privateGet(this, _holding)) return;
|
|
422
|
+
__privateSet(this, _holding, false);
|
|
423
|
+
__privateGet(this, _clock).resume();
|
|
424
|
+
}
|
|
425
|
+
/** Deliver one held event; false when none is waiting. */
|
|
426
|
+
step() {
|
|
427
|
+
return this.paused ? __privateGet(this, _clock).step() : false;
|
|
428
|
+
}
|
|
429
|
+
dispose() {
|
|
430
|
+
if (__privateGet(this, _disposed)) return;
|
|
431
|
+
__privateSet(this, _disposed, true);
|
|
432
|
+
__privateGet(this, _stop).call(this);
|
|
433
|
+
this.continue();
|
|
434
|
+
__privateGet(this, _listeners2).clear();
|
|
435
|
+
__privateGet(this, _watchpoints).clear();
|
|
436
|
+
}
|
|
437
|
+
};
|
|
438
|
+
_clock = new WeakMap();
|
|
439
|
+
_watchpoints = new WeakMap();
|
|
440
|
+
_listeners2 = new WeakMap();
|
|
441
|
+
_stop = new WeakMap();
|
|
442
|
+
_holding = new WeakMap();
|
|
443
|
+
_last = new WeakMap();
|
|
444
|
+
_disposed = new WeakMap();
|
|
445
|
+
_MessageDebugger_instances = new WeakSet();
|
|
446
|
+
check_fn = function(event) {
|
|
447
|
+
for (const watchpoint of __privateGet(this, _watchpoints).values()) {
|
|
448
|
+
if (watchpoint.connectionId !== event.connectionId) continue;
|
|
449
|
+
let hit;
|
|
450
|
+
try {
|
|
451
|
+
hit = watchpoint.condition ? watchpoint.condition(event) === true : true;
|
|
452
|
+
} catch {
|
|
453
|
+
hit = false;
|
|
454
|
+
}
|
|
455
|
+
if (!hit) continue;
|
|
456
|
+
const found = Object.freeze({ watchpoint, event });
|
|
457
|
+
__privateSet(this, _last, found);
|
|
458
|
+
if (watchpoint.mode === "break" && !__privateGet(this, _clock).held) {
|
|
459
|
+
__privateSet(this, _holding, true);
|
|
460
|
+
__privateGet(this, _clock).hold();
|
|
461
|
+
}
|
|
462
|
+
for (const listener of [...__privateGet(this, _listeners2)]) {
|
|
463
|
+
try {
|
|
464
|
+
listener(found);
|
|
465
|
+
} catch {
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
};
|
|
470
|
+
|
|
471
|
+
// src/module/wam.ts
|
|
472
|
+
var WAM_MODULE = createCapabilityKey("webmusic.wam-module");
|
|
473
|
+
async function defaultLoad(url) {
|
|
474
|
+
const module = await import(
|
|
475
|
+
/* @vite-ignore */
|
|
476
|
+
url
|
|
477
|
+
);
|
|
478
|
+
if (!module.default || typeof module.default.createInstance !== "function") throw new ProjectModuleUnavailableError(`${url} is not a Web Audio Module.`);
|
|
479
|
+
return module.default;
|
|
480
|
+
}
|
|
481
|
+
var MIDI_AXIS = "audio-context-seconds";
|
|
482
|
+
function wamPlugin(options) {
|
|
483
|
+
const groups = /* @__PURE__ */ new WeakMap();
|
|
484
|
+
const allow = options.allow ?? ((url) => /^https?:\/\//i.test(url) || url.startsWith("/") || url.startsWith("./"));
|
|
485
|
+
return Object.freeze({
|
|
486
|
+
id: options.id ?? "webmusic.wam",
|
|
487
|
+
version: "1.0.0",
|
|
488
|
+
async create(request) {
|
|
489
|
+
const { record, context, signal } = request;
|
|
490
|
+
const url = typeof record.config.url === "string" ? record.config.url : "";
|
|
491
|
+
if (!url) throw new ProjectModuleUnavailableError("Give the Web Audio Module a URL.");
|
|
492
|
+
if (!allow(url)) throw new ProjectModuleUnavailableError(`This page does not load Web Audio Modules from ${url}.`);
|
|
493
|
+
const audio = context.audio.prepare();
|
|
494
|
+
let group = groups.get(audio);
|
|
495
|
+
if (!group) {
|
|
496
|
+
group = Promise.resolve(options.hostGroup(audio));
|
|
497
|
+
groups.set(audio, group);
|
|
498
|
+
}
|
|
499
|
+
let instance;
|
|
500
|
+
try {
|
|
501
|
+
const constructor = await (options.load ?? defaultLoad)(url);
|
|
502
|
+
signal.throwIfAborted();
|
|
503
|
+
instance = await constructor.createInstance(await group, audio, record.config.state);
|
|
504
|
+
} catch (error) {
|
|
505
|
+
if (error?.name === "AbortError") throw error;
|
|
506
|
+
throw new ProjectModuleUnavailableError(`The Web Audio Module did not load: ${error instanceof Error ? error.message : String(error)}`, { cause: error });
|
|
507
|
+
}
|
|
508
|
+
const node = instance.audioNode;
|
|
509
|
+
const destroy = () => {
|
|
510
|
+
try {
|
|
511
|
+
node.disconnect();
|
|
512
|
+
} catch {
|
|
513
|
+
}
|
|
514
|
+
try {
|
|
515
|
+
node.destroy?.();
|
|
516
|
+
} catch {
|
|
517
|
+
}
|
|
518
|
+
};
|
|
519
|
+
try {
|
|
520
|
+
signal.throwIfAborted();
|
|
521
|
+
const info = await node.getParameterInfo();
|
|
522
|
+
const values = await node.getParameterValues(false).catch(() => ({}));
|
|
523
|
+
signal.throwIfAborted();
|
|
524
|
+
let state = record.config.state;
|
|
525
|
+
const refreshState = () => {
|
|
526
|
+
void node.getState?.().then((next) => {
|
|
527
|
+
try {
|
|
528
|
+
state = JSON.parse(JSON.stringify(next ?? null));
|
|
529
|
+
} catch {
|
|
530
|
+
}
|
|
531
|
+
}, () => {
|
|
532
|
+
});
|
|
533
|
+
};
|
|
534
|
+
const handles = Object.values(info).map((param) => {
|
|
535
|
+
const min = Number(param.minValue), max = Number(param.maxValue);
|
|
536
|
+
const integer = param.type === "int" || param.type === "boolean" || param.type === "choice";
|
|
537
|
+
return new ParamHandle({
|
|
538
|
+
id: param.id,
|
|
539
|
+
label: param.label || param.id,
|
|
540
|
+
unit: param.units || (integer ? "step" : "value"),
|
|
541
|
+
range: { min, max },
|
|
542
|
+
defaultValue: Number(param.defaultValue),
|
|
543
|
+
...integer ? { step: 1 } : param.discreteStep ? { step: param.discreteStep } : {},
|
|
544
|
+
...param.exponent && param.exponent !== 0 ? { scale: "logarithmic" } : {}
|
|
545
|
+
}, {
|
|
546
|
+
baseValue: values[param.id]?.value ?? Number(param.defaultValue),
|
|
547
|
+
apply: (value) => {
|
|
548
|
+
void node.setParameterValues({ [param.id]: { id: param.id, value, normalized: false } }).then(refreshState, () => {
|
|
549
|
+
});
|
|
550
|
+
}
|
|
551
|
+
});
|
|
552
|
+
});
|
|
553
|
+
const params = new ParamSurface(handles);
|
|
554
|
+
const descriptor = instance.descriptor ?? {};
|
|
555
|
+
const output = descriptor.hasAudioOutput === false ? void 0 : new AudioOutputPort(node, { id: "output" });
|
|
556
|
+
const notes = descriptor.hasMidiInput ? new NoteInputPort({ accept: (event) => {
|
|
557
|
+
const time = event.axis === MIDI_AXIS ? Math.max(audio.currentTime, event.time) : audio.currentTime;
|
|
558
|
+
const pitch = Math.max(0, Math.min(127, Math.round(event.pitch.midi)));
|
|
559
|
+
const on = event.gate === "on";
|
|
560
|
+
node.scheduleEvents({ type: "wam-midi", time, data: { bytes: on ? [144, pitch, Math.max(1, Math.round(event.velocity * 127))] : [128, pitch, 0] } });
|
|
561
|
+
} }, { id: "notes" }) : void 0;
|
|
562
|
+
const lend = Object.freeze({
|
|
563
|
+
instance,
|
|
564
|
+
name: descriptor.name || url,
|
|
565
|
+
createGui: () => instance.createGui?.(),
|
|
566
|
+
destroyGui: (gui) => {
|
|
567
|
+
try {
|
|
568
|
+
instance.destroyGui?.(gui);
|
|
569
|
+
} catch {
|
|
570
|
+
}
|
|
571
|
+
}
|
|
572
|
+
});
|
|
573
|
+
let route;
|
|
574
|
+
let disposed = false;
|
|
575
|
+
const module = {
|
|
576
|
+
id: record.id,
|
|
577
|
+
params,
|
|
578
|
+
capabilities: new ModuleCapabilities().with(WAM_MODULE, lend),
|
|
579
|
+
ports: () => notes ? [notes] : [],
|
|
580
|
+
getPort: (id) => id === "output" ? output : void 0,
|
|
581
|
+
prepare: () => {
|
|
582
|
+
if (!disposed && output && !route) route = routeModuleOutput(context, module, output);
|
|
583
|
+
}
|
|
584
|
+
};
|
|
585
|
+
const registration = context.registry.register(module);
|
|
586
|
+
const ports = [
|
|
587
|
+
...descriptor.hasAudioInput === false ? [] : [{ id: "input", label: "audio in", kind: "audio", direction: "input" }],
|
|
588
|
+
...notes ? [{ id: "notes", label: "notes", kind: "note", direction: "input" }] : [],
|
|
589
|
+
...output ? [{ id: "output", label: "audio out", kind: "audio", direction: "output" }] : []
|
|
590
|
+
];
|
|
591
|
+
return {
|
|
592
|
+
module,
|
|
593
|
+
ports,
|
|
594
|
+
getInput: (id) => id === "input" && descriptor.hasAudioInput !== false ? node : void 0,
|
|
595
|
+
exportConfig: () => ({ ...record.config, ...state === void 0 ? {} : { state } }),
|
|
596
|
+
dispose: () => {
|
|
597
|
+
if (disposed) return;
|
|
598
|
+
disposed = true;
|
|
599
|
+
registration.release();
|
|
600
|
+
route?.release();
|
|
601
|
+
notes?.dispose();
|
|
602
|
+
output?.dispose();
|
|
603
|
+
for (const handle of handles) handle.dispose();
|
|
604
|
+
destroy();
|
|
605
|
+
}
|
|
606
|
+
};
|
|
607
|
+
} catch (error) {
|
|
608
|
+
destroy();
|
|
609
|
+
throw error;
|
|
610
|
+
}
|
|
611
|
+
}
|
|
612
|
+
});
|
|
613
|
+
}
|
|
614
|
+
|
|
615
|
+
// src/module/multichannel.ts
|
|
616
|
+
var MAX_MC_CHANNELS = 32;
|
|
617
|
+
function channelCount(value) {
|
|
618
|
+
const channels = Math.trunc(Number(value ?? 2));
|
|
619
|
+
if (!(channels >= 1 && channels <= MAX_MC_CHANNELS)) throw new ProjectModuleUnavailableError(`A multichannel module takes 1 to ${MAX_MC_CHANNELS} channels.`);
|
|
620
|
+
return channels;
|
|
621
|
+
}
|
|
622
|
+
function multichannelPlugin(inner, id = `${inner.id.replace(/^(.*\.)?/, "$1mc.")}`) {
|
|
623
|
+
return Object.freeze({
|
|
624
|
+
id,
|
|
625
|
+
version: inner.version,
|
|
626
|
+
async create(request) {
|
|
627
|
+
const { record, context } = request;
|
|
628
|
+
const channels = channelCount(record.config.channels);
|
|
629
|
+
const audio = context.audio.prepare();
|
|
630
|
+
const splitter = audio.createChannelSplitter(channels);
|
|
631
|
+
const merger = audio.createChannelMerger(channels);
|
|
632
|
+
const taps = Array.from({ length: channels }, () => audio.createGain());
|
|
633
|
+
taps.forEach((tap, channel) => tap.connect(merger, 0, channel));
|
|
634
|
+
const instances = [];
|
|
635
|
+
const registries = [];
|
|
636
|
+
const leases = [];
|
|
637
|
+
const cleanup = () => {
|
|
638
|
+
for (const lease of leases.splice(0)) {
|
|
639
|
+
try {
|
|
640
|
+
lease.release();
|
|
641
|
+
} catch {
|
|
642
|
+
}
|
|
643
|
+
}
|
|
644
|
+
for (const instance of instances.splice(0)) {
|
|
645
|
+
try {
|
|
646
|
+
instance.dispose();
|
|
647
|
+
} catch {
|
|
648
|
+
}
|
|
649
|
+
}
|
|
650
|
+
for (const registry of registries.splice(0)) {
|
|
651
|
+
try {
|
|
652
|
+
registry.dispose();
|
|
653
|
+
} catch {
|
|
654
|
+
}
|
|
655
|
+
}
|
|
656
|
+
for (const node of [splitter, merger, ...taps]) node.disconnect();
|
|
657
|
+
};
|
|
658
|
+
try {
|
|
659
|
+
for (let channel = 0; channel < channels; channel++) {
|
|
660
|
+
const registry = new ModuleRegistry();
|
|
661
|
+
registries.push(registry);
|
|
662
|
+
const scoped = context.derive({ id: `${context.id}/${record.id}.${channel}`, registry, admit: null, destination: () => taps[channel] });
|
|
663
|
+
const config = record.config.config ?? {};
|
|
664
|
+
instances.push(await inner.create({ ...request, context: scoped, record: { ...record, id: `${record.id}.${channel}`, config } }));
|
|
665
|
+
}
|
|
666
|
+
} catch (error) {
|
|
667
|
+
cleanup();
|
|
668
|
+
throw error;
|
|
669
|
+
}
|
|
670
|
+
const port = new AudioOutputPort(merger, { id: "output" });
|
|
671
|
+
const first = instances[0].module;
|
|
672
|
+
let disposed = false;
|
|
673
|
+
let mirroring = false;
|
|
674
|
+
const module = {
|
|
675
|
+
id: record.id,
|
|
676
|
+
get params() {
|
|
677
|
+
return first.params;
|
|
678
|
+
},
|
|
679
|
+
getPort: (name) => name === "output" ? port : void 0,
|
|
680
|
+
prepare: async () => {
|
|
681
|
+
if (disposed) return;
|
|
682
|
+
for (const instance of instances) await instance.module.prepare?.();
|
|
683
|
+
instances.forEach((instance, channel) => {
|
|
684
|
+
const input = instance.getInput?.("input");
|
|
685
|
+
if (input) {
|
|
686
|
+
splitter.connect(input, channel, 0);
|
|
687
|
+
leases.push(createConnectionLease(() => {
|
|
688
|
+
try {
|
|
689
|
+
splitter.disconnect(input);
|
|
690
|
+
} catch {
|
|
691
|
+
}
|
|
692
|
+
}));
|
|
693
|
+
}
|
|
694
|
+
});
|
|
695
|
+
for (const handle of first.params ?? []) {
|
|
696
|
+
leases.push(createConnectionLease(handle.subscribe((snapshot) => {
|
|
697
|
+
if (mirroring || snapshot.disposed) return;
|
|
698
|
+
mirroring = true;
|
|
699
|
+
try {
|
|
700
|
+
for (const instance of instances.slice(1)) instance.module.params?.get(handle.descriptor.id)?.setBaseValue(snapshot.baseValue);
|
|
701
|
+
} finally {
|
|
702
|
+
mirroring = false;
|
|
703
|
+
}
|
|
704
|
+
})));
|
|
705
|
+
}
|
|
706
|
+
leases.push(routeModuleOutput(context, module, port));
|
|
707
|
+
}
|
|
708
|
+
};
|
|
709
|
+
const registration = context.registry.register(module);
|
|
710
|
+
return {
|
|
711
|
+
module,
|
|
712
|
+
ports: [{ id: "input", label: `${channels} channels in`, kind: "audio", direction: "input" }, { id: "output", label: `${channels} channels out`, kind: "audio", direction: "output" }],
|
|
713
|
+
getInput: (name) => name === "input" ? splitter : void 0,
|
|
714
|
+
exportConfig: () => record.config,
|
|
715
|
+
dispose: () => {
|
|
716
|
+
if (disposed) return;
|
|
717
|
+
disposed = true;
|
|
718
|
+
registration.release();
|
|
719
|
+
port.dispose();
|
|
720
|
+
cleanup();
|
|
721
|
+
}
|
|
722
|
+
};
|
|
723
|
+
}
|
|
724
|
+
});
|
|
725
|
+
}
|
|
726
|
+
var _inputs, _merger, _port;
|
|
727
|
+
var _ChannelPack = class _ChannelPack extends GraphModule {
|
|
728
|
+
constructor(options = {}) {
|
|
729
|
+
super(_ChannelPack.type, options);
|
|
730
|
+
__privateAdd(this, _inputs);
|
|
731
|
+
__privateAdd(this, _merger);
|
|
732
|
+
__privateAdd(this, _port);
|
|
733
|
+
const channels = this.channels = channelCount(options.channels);
|
|
734
|
+
const audio = this.audio();
|
|
735
|
+
const merger = __privateSet(this, _merger, audio.createChannelMerger(channels));
|
|
736
|
+
__privateSet(this, _inputs, Array.from({ length: channels }, (_, channel) => {
|
|
737
|
+
const gain = audio.createGain();
|
|
738
|
+
gain.channelCount = 1;
|
|
739
|
+
gain.channelCountMode = "explicit";
|
|
740
|
+
gain.connect(merger, 0, channel);
|
|
741
|
+
this.addPort(void 0, { id: numberedPort("in", channel), label: `channel ${channel + 1}`, kind: "audio", direction: "input" });
|
|
742
|
+
return gain;
|
|
743
|
+
}));
|
|
744
|
+
__privateSet(this, _port, new AudioOutputPort(merger, { id: "output" }));
|
|
745
|
+
this.addPort(void 0, { id: "output", label: `${channels} channels`, kind: "audio", direction: "output" });
|
|
746
|
+
}
|
|
747
|
+
getPort(name) {
|
|
748
|
+
return name === "output" ? __privateGet(this, _port) : void 0;
|
|
749
|
+
}
|
|
750
|
+
getInput(name) {
|
|
751
|
+
return /^in\d+$/.test(name) ? __privateGet(this, _inputs)[Number(name.slice(2))] : void 0;
|
|
752
|
+
}
|
|
753
|
+
exportOptions() {
|
|
754
|
+
return { channels: this.channels };
|
|
755
|
+
}
|
|
756
|
+
onDispose() {
|
|
757
|
+
__privateGet(this, _port).dispose();
|
|
758
|
+
for (const node of [__privateGet(this, _merger), ...__privateGet(this, _inputs)]) node.disconnect();
|
|
759
|
+
}
|
|
760
|
+
};
|
|
761
|
+
_inputs = new WeakMap();
|
|
762
|
+
_merger = new WeakMap();
|
|
763
|
+
_port = new WeakMap();
|
|
764
|
+
_ChannelPack.type = "webmusic.mc.pack";
|
|
765
|
+
var ChannelPack = _ChannelPack;
|
|
766
|
+
var _splitter, _outputs, _ports;
|
|
767
|
+
var _ChannelUnpack = class _ChannelUnpack extends GraphModule {
|
|
768
|
+
constructor(options = {}) {
|
|
769
|
+
super(_ChannelUnpack.type, options);
|
|
770
|
+
__privateAdd(this, _splitter);
|
|
771
|
+
__privateAdd(this, _outputs);
|
|
772
|
+
__privateAdd(this, _ports);
|
|
773
|
+
const channels = this.channels = channelCount(options.channels);
|
|
774
|
+
const audio = this.audio();
|
|
775
|
+
const splitter = __privateSet(this, _splitter, audio.createChannelSplitter(channels));
|
|
776
|
+
this.addPort(void 0, { id: "input", label: `${channels} channels`, kind: "audio", direction: "input" });
|
|
777
|
+
__privateSet(this, _outputs, Array.from({ length: channels }, (_, channel) => {
|
|
778
|
+
const gain = audio.createGain();
|
|
779
|
+
splitter.connect(gain, channel, 0);
|
|
780
|
+
return gain;
|
|
781
|
+
}));
|
|
782
|
+
__privateSet(this, _ports, __privateGet(this, _outputs).map((gain, channel) => {
|
|
783
|
+
this.addPort(void 0, { id: numberedPort("out", channel), label: `channel ${channel + 1}`, kind: "audio", direction: "output" });
|
|
784
|
+
return new AudioOutputPort(gain, { id: numberedPort("out", channel) });
|
|
785
|
+
}));
|
|
786
|
+
}
|
|
787
|
+
getPort(name) {
|
|
788
|
+
return __privateGet(this, _ports).find((port) => port.id === name);
|
|
789
|
+
}
|
|
790
|
+
getInput(name) {
|
|
791
|
+
return name === "input" ? __privateGet(this, _splitter) : void 0;
|
|
792
|
+
}
|
|
793
|
+
exportOptions() {
|
|
794
|
+
return { channels: this.channels };
|
|
795
|
+
}
|
|
796
|
+
onDispose() {
|
|
797
|
+
for (const port of __privateGet(this, _ports)) port.dispose();
|
|
798
|
+
for (const node of [__privateGet(this, _splitter), ...__privateGet(this, _outputs)]) node.disconnect();
|
|
799
|
+
}
|
|
800
|
+
};
|
|
801
|
+
_splitter = new WeakMap();
|
|
802
|
+
_outputs = new WeakMap();
|
|
803
|
+
_ports = new WeakMap();
|
|
804
|
+
_ChannelUnpack.type = "webmusic.mc.unpack";
|
|
805
|
+
var ChannelUnpack = _ChannelUnpack;
|
|
806
|
+
|
|
807
|
+
// src/module/note-maker.ts
|
|
808
|
+
var text = (atom) => atom.type === "symbol" ? atom.value : String(atom.value);
|
|
809
|
+
var _velocity, _duration, _authored, _count, _timed, _NoteMaker_instances, play_fn;
|
|
810
|
+
var _NoteMaker = class _NoteMaker extends GraphModule {
|
|
811
|
+
constructor(options = {}) {
|
|
812
|
+
super(_NoteMaker.type, options);
|
|
813
|
+
__privateAdd(this, _NoteMaker_instances);
|
|
814
|
+
this.inputs = {
|
|
815
|
+
pitch: this.input("pitch", "pitch, or pitch velocity duration", (value) => __privateMethod(this, _NoteMaker_instances, play_fn).call(this, value)),
|
|
816
|
+
velocity: this.input("velocity", "velocity", (value) => {
|
|
817
|
+
const n = numberOf(value);
|
|
818
|
+
if (n !== void 0) __privateSet(this, _velocity, n);
|
|
819
|
+
}),
|
|
820
|
+
duration: this.input("duration", "duration (ms or a time value)", (value) => {
|
|
821
|
+
const atom = atomsOf(value)[0];
|
|
822
|
+
if (atom) __privateSet(this, _duration, text(atom));
|
|
823
|
+
})
|
|
824
|
+
};
|
|
825
|
+
/** The notes it plays, on the audio clock. */
|
|
826
|
+
this.notes = new NoteOutputPort({ id: "notes" });
|
|
827
|
+
__privateAdd(this, _velocity);
|
|
828
|
+
__privateAdd(this, _duration);
|
|
829
|
+
__privateAdd(this, _authored);
|
|
830
|
+
__privateAdd(this, _count, 0);
|
|
831
|
+
__privateAdd(this, _timed, false);
|
|
832
|
+
__privateSet(this, _velocity, typeof options.velocity === "number" && Number.isFinite(options.velocity) ? options.velocity : 100);
|
|
833
|
+
__privateSet(this, _duration, options.duration === void 0 ? "250" : String(options.duration));
|
|
834
|
+
const duration = Number(__privateGet(this, _duration));
|
|
835
|
+
__privateSet(this, _authored, { velocity: __privateGet(this, _velocity), duration: __privateGet(this, _duration).trim() !== "" && Number.isFinite(duration) ? duration : __privateGet(this, _duration) });
|
|
836
|
+
this.addPort(this.notes, { id: "notes", label: "notes", kind: "note", direction: "output" });
|
|
837
|
+
}
|
|
838
|
+
/** Velocity of the next note, 0 to 127. */
|
|
839
|
+
get velocity() {
|
|
840
|
+
return __privateGet(this, _velocity);
|
|
841
|
+
}
|
|
842
|
+
set velocity(value) {
|
|
843
|
+
__privateSet(this, _velocity, value);
|
|
844
|
+
}
|
|
845
|
+
/** Duration of the next note: milliseconds as text, or a time value. */
|
|
846
|
+
get duration() {
|
|
847
|
+
return __privateGet(this, _duration);
|
|
848
|
+
}
|
|
849
|
+
set duration(value) {
|
|
850
|
+
__privateSet(this, _duration, String(value));
|
|
851
|
+
}
|
|
852
|
+
/** Play `pitch` (MIDI 0 to 127) at `time` (now by default) with the current velocity and duration. */
|
|
853
|
+
play(pitch, time) {
|
|
854
|
+
if (this.disposed || __privateGet(this, _velocity) <= 0 || pitch < 0 || pitch > 127) return;
|
|
855
|
+
__privateSet(this, _timed, true);
|
|
856
|
+
const start = time ?? this.clock.now();
|
|
857
|
+
const seconds = timeValueSeconds(__privateGet(this, _duration), this.musicalTime?.tempo());
|
|
858
|
+
const occurrenceId = `${this.id}-${++__privateWrapper(this, _count)._}`;
|
|
859
|
+
const base = { occurrenceId, sourceId: this.id, pitch: { midi: Math.round(pitch) }, axis: "audio-context-seconds", epoch: this.context?.session?.scheduling.epoch ?? 0 };
|
|
860
|
+
this.notes.emit({ ...base, velocity: Math.min(1, __privateGet(this, _velocity) / 127), gate: "on", time: start });
|
|
861
|
+
this.clock.schedule(start + Math.max(1e-3, seconds), (when) => {
|
|
862
|
+
if (this.notes.held.some((event) => event.occurrenceId === occurrenceId)) this.notes.emit({ ...base, velocity: 0, gate: "off", time: when });
|
|
863
|
+
});
|
|
864
|
+
}
|
|
865
|
+
/** The options it was made with; a velocity or duration set later is not saved. */
|
|
866
|
+
exportOptions() {
|
|
867
|
+
return __privateGet(this, _authored);
|
|
868
|
+
}
|
|
869
|
+
// Held notes are cancelled at the clock's time when it was ever read.
|
|
870
|
+
onDispose() {
|
|
871
|
+
this.notes.dispose(__privateGet(this, _timed) ? this.clock.now() : void 0);
|
|
872
|
+
}
|
|
873
|
+
};
|
|
874
|
+
_velocity = new WeakMap();
|
|
875
|
+
_duration = new WeakMap();
|
|
876
|
+
_authored = new WeakMap();
|
|
877
|
+
_count = new WeakMap();
|
|
878
|
+
_timed = new WeakMap();
|
|
879
|
+
_NoteMaker_instances = new WeakSet();
|
|
880
|
+
play_fn = function(value) {
|
|
881
|
+
const atoms = atomsOf(value).filter(isNumberAtom);
|
|
882
|
+
const pitch = atoms[0]?.value;
|
|
883
|
+
if (pitch === void 0) return;
|
|
884
|
+
if (atoms[1]) __privateSet(this, _velocity, atoms[1].value);
|
|
885
|
+
if (atoms[2]) __privateSet(this, _duration, String(atoms[2].value));
|
|
886
|
+
this.play(pitch, value.time);
|
|
887
|
+
};
|
|
888
|
+
_NoteMaker.type = "webmusic.note-maker";
|
|
889
|
+
var NoteMaker = _NoteMaker;
|
|
890
|
+
|
|
891
|
+
// src/module/nested.ts
|
|
892
|
+
var BOUNDARY_KINDS = ["message", "audio", "note", "source"];
|
|
893
|
+
var COMPONENT_BOUNDARY = createCapabilityKey("webmusic.component-boundary");
|
|
894
|
+
var POLY_VOICE = createCapabilityKey("webmusic.poly-voice");
|
|
895
|
+
function kinds(value, name) {
|
|
896
|
+
if (value === void 0) return [];
|
|
897
|
+
if (!Array.isArray(value) || value.some((item) => !BOUNDARY_KINDS.includes(item))) {
|
|
898
|
+
throw new TypeError(`component ${name} must list message, audio, note or source channels.`);
|
|
899
|
+
}
|
|
900
|
+
return value;
|
|
901
|
+
}
|
|
902
|
+
function requireContext(module, context) {
|
|
903
|
+
if (!context) throw new Error(`${module} needs a module context.`);
|
|
904
|
+
return context;
|
|
905
|
+
}
|
|
906
|
+
var repeat = (relay) => (event) => {
|
|
907
|
+
if (relay.disposed) return;
|
|
908
|
+
if (event.gate !== "on" && !relay.held.some((held) => held.occurrenceId === event.occurrenceId)) return;
|
|
909
|
+
relay.emit(event);
|
|
910
|
+
};
|
|
911
|
+
var _inlets, _outlets, _disposers, _leases, _voicePorts, _voicesLent, _audio;
|
|
912
|
+
var _NestedComponent = class _NestedComponent extends GraphModule {
|
|
913
|
+
/** `type` and `voices` are for {@link PolyComponent}: voices make the shell own one child context per voice. */
|
|
914
|
+
constructor(options = {}, type = _NestedComponent.type, voices = 0) {
|
|
915
|
+
super(type, options);
|
|
916
|
+
__privateAdd(this, _inlets);
|
|
917
|
+
__privateAdd(this, _outlets);
|
|
918
|
+
__privateAdd(this, _disposers, []);
|
|
919
|
+
__privateAdd(this, _leases, []);
|
|
920
|
+
__privateAdd(this, _voicePorts);
|
|
921
|
+
__privateAdd(this, _voicesLent, 0);
|
|
922
|
+
__privateAdd(this, _audio);
|
|
923
|
+
requireContext(type, this.context);
|
|
924
|
+
this.inlets = Object.freeze(kinds(options.inlets, "inlets"));
|
|
925
|
+
this.outlets = Object.freeze(kinds(options.outlets, "outlets"));
|
|
926
|
+
__privateSet(this, _voicePorts, Array.from({ length: voices }, (_, index) => new NoteOutputPort({ id: `voice-${index}` })));
|
|
927
|
+
if (voices) {
|
|
928
|
+
const allocator = new VoiceAllocator(voices);
|
|
929
|
+
const held = /* @__PURE__ */ new Map();
|
|
930
|
+
const ports = __privateGet(this, _voicePorts);
|
|
931
|
+
this.voiceInput = new NoteInputPort({ accept: (event) => {
|
|
932
|
+
if (event.gate === "on") {
|
|
933
|
+
const { voice: voice2, stolen } = allocator.noteOn(event.occurrenceId);
|
|
934
|
+
if (voice2 === void 0) return;
|
|
935
|
+
if (stolen) {
|
|
936
|
+
const previous = held.get(stolen);
|
|
937
|
+
held.delete(stolen);
|
|
938
|
+
const port2 = ports[voice2];
|
|
939
|
+
if (previous && port2.held.some((item) => item.occurrenceId === stolen)) port2.emit({ ...previous, gate: "cancel", time: event.time, axis: event.axis });
|
|
940
|
+
}
|
|
941
|
+
held.set(event.occurrenceId, event);
|
|
942
|
+
ports[voice2].emit(event);
|
|
943
|
+
return;
|
|
944
|
+
}
|
|
945
|
+
const voice = allocator.noteOff(event.occurrenceId);
|
|
946
|
+
held.delete(event.occurrenceId);
|
|
947
|
+
const port = voice === void 0 ? void 0 : ports[voice];
|
|
948
|
+
if (port?.held.some((item) => item.occurrenceId === event.occurrenceId)) port.emit(event);
|
|
949
|
+
} }, { id: "notes" });
|
|
950
|
+
this.addPort(this.voiceInput, { id: "notes", label: "notes, one voice each", kind: "note", direction: "input" });
|
|
951
|
+
}
|
|
952
|
+
const audio = () => __privateGet(this, _audio) ?? __privateSet(this, _audio, this.audio());
|
|
953
|
+
__privateSet(this, _inlets, this.inlets.map((kind, index) => {
|
|
954
|
+
const id = numberedPort("in", index);
|
|
955
|
+
const label = `${kind} in ${index + 1}`;
|
|
956
|
+
if (kind === "note") {
|
|
957
|
+
const relay2 = new NoteOutputPort({ id: `inner-${id}` });
|
|
958
|
+
const port = new NoteInputPort({ accept: repeat(relay2) }, { id, multiplicity: "sum" });
|
|
959
|
+
__privateGet(this, _disposers).push(() => {
|
|
960
|
+
port.dispose();
|
|
961
|
+
relay2.dispose();
|
|
962
|
+
});
|
|
963
|
+
this.addPort(port, { id, label, kind, direction: "input" });
|
|
964
|
+
return { kind, channel: { kind, notes: relay2 } };
|
|
965
|
+
}
|
|
966
|
+
if (kind === "source") {
|
|
967
|
+
const relay2 = new SourceRelay(`${this.id}:${id}`);
|
|
968
|
+
__privateGet(this, _disposers).push(() => relay2.dispose());
|
|
969
|
+
this.addPort(void 0, { id, label, kind, direction: "input" });
|
|
970
|
+
return { kind, relay: relay2, channel: { kind, source: relay2 } };
|
|
971
|
+
}
|
|
972
|
+
if (kind === "message") {
|
|
973
|
+
const relay2 = new MessageOutputPort({ id: `inner-${id}` });
|
|
974
|
+
const port = new MessageInputPort((value) => relay2.send(value), { id });
|
|
975
|
+
__privateGet(this, _disposers).push(() => {
|
|
976
|
+
port.dispose();
|
|
977
|
+
relay2.dispose();
|
|
978
|
+
});
|
|
979
|
+
this.addPort(port, { id, label, kind, direction: "input" });
|
|
980
|
+
return { kind, channel: { kind, messages: relay2 } };
|
|
981
|
+
}
|
|
982
|
+
const node = audio().createGain();
|
|
983
|
+
const relay = new AudioOutputPort(node, { id: `inner-${id}` });
|
|
984
|
+
__privateGet(this, _disposers).push(() => {
|
|
985
|
+
relay.dispose();
|
|
986
|
+
node.disconnect();
|
|
987
|
+
});
|
|
988
|
+
this.addPort(void 0, { id, label, kind, direction: "input" });
|
|
989
|
+
return { kind, node, channel: { kind, audio: relay } };
|
|
990
|
+
}));
|
|
991
|
+
__privateSet(this, _outlets, this.outlets.map((kind, index) => {
|
|
992
|
+
const id = numberedPort("out", index);
|
|
993
|
+
const label = `${kind} out ${index + 1}`;
|
|
994
|
+
if (kind === "note") {
|
|
995
|
+
const port2 = new NoteOutputPort({ id });
|
|
996
|
+
const inner = new NoteInputPort({ accept: repeat(port2) }, { id: `inner-${id}`, multiplicity: "sum" });
|
|
997
|
+
__privateGet(this, _disposers).push(() => {
|
|
998
|
+
inner.dispose();
|
|
999
|
+
port2.dispose();
|
|
1000
|
+
});
|
|
1001
|
+
this.addPort(port2, { id, label, kind, direction: "output" });
|
|
1002
|
+
return { kind, channel: { kind, notes: inner } };
|
|
1003
|
+
}
|
|
1004
|
+
if (kind === "source") {
|
|
1005
|
+
const relay = new SourceRelay(`${this.id}:${id}`);
|
|
1006
|
+
__privateGet(this, _disposers).push(() => relay.dispose());
|
|
1007
|
+
this.addPort(void 0, { id, label, kind, direction: "output" });
|
|
1008
|
+
return { kind, relay, channel: { kind, source: relay } };
|
|
1009
|
+
}
|
|
1010
|
+
if (kind === "message") {
|
|
1011
|
+
const port2 = new MessageOutputPort({ id });
|
|
1012
|
+
__privateGet(this, _disposers).push(() => port2.dispose());
|
|
1013
|
+
this.addPort(port2, { id, label, kind, direction: "output" });
|
|
1014
|
+
return { kind, channel: { kind, send: (value) => port2.send(value) } };
|
|
1015
|
+
}
|
|
1016
|
+
const node = audio().createGain();
|
|
1017
|
+
const port = new AudioOutputPort(node, { id });
|
|
1018
|
+
__privateGet(this, _disposers).push(() => {
|
|
1019
|
+
port.dispose();
|
|
1020
|
+
node.disconnect();
|
|
1021
|
+
});
|
|
1022
|
+
this.addPort(void 0, { id, label, kind, direction: "output" });
|
|
1023
|
+
return { kind, node, port, channel: { kind, input: node } };
|
|
1024
|
+
}));
|
|
1025
|
+
this.boundary = Object.freeze({
|
|
1026
|
+
inlet: (index) => __privateGet(this, _inlets)[index]?.channel,
|
|
1027
|
+
outlet: (index) => __privateGet(this, _outlets)[index]?.channel
|
|
1028
|
+
});
|
|
1029
|
+
}
|
|
1030
|
+
getPort(name) {
|
|
1031
|
+
return __privateGet(this, _outlets).find((item, index) => item.kind === "audio" && numberedPort("out", index) === name)?.port;
|
|
1032
|
+
}
|
|
1033
|
+
/** The first audio outlet follows the context's default route, like any other audio module. */
|
|
1034
|
+
prepare() {
|
|
1035
|
+
if (this.disposed || __privateGet(this, _leases).length) return;
|
|
1036
|
+
const first = __privateGet(this, _outlets).find((item) => item.kind === "audio");
|
|
1037
|
+
if (first?.port) __privateGet(this, _leases).push(routeModuleOutput(requireContext(this.type, this.context), this, first.port));
|
|
1038
|
+
}
|
|
1039
|
+
getInput(name) {
|
|
1040
|
+
return __privateGet(this, _inlets).find((item, index) => item.kind === "audio" && numberedPort("in", index) === name)?.node;
|
|
1041
|
+
}
|
|
1042
|
+
/** A source cord into an inlet is followed by that inlet's relay. */
|
|
1043
|
+
connectSource(name, upstream) {
|
|
1044
|
+
return __privateGet(this, _inlets).find((item, index) => item.kind === "source" && numberedPort("in", index) === name)?.relay?.follow(upstream);
|
|
1045
|
+
}
|
|
1046
|
+
/** A source cord from an outlet carries the outlet's relay. */
|
|
1047
|
+
sourceOutput(name) {
|
|
1048
|
+
return __privateGet(this, _outlets).find((item, index) => item.kind === "source" && numberedPort("out", index) === name)?.relay;
|
|
1049
|
+
}
|
|
1050
|
+
/**
|
|
1051
|
+
* Inner modules keep the parent's destination and admission, so a module
|
|
1052
|
+
* not wired to an audio outlet sounds through the parent's default route.
|
|
1053
|
+
*/
|
|
1054
|
+
childContext(child) {
|
|
1055
|
+
let capabilities = child.capabilities.with(COMPONENT_BOUNDARY, this.boundary);
|
|
1056
|
+
if (__privateGet(this, _voicePorts).length) {
|
|
1057
|
+
const index = __privateWrapper(this, _voicesLent)._++;
|
|
1058
|
+
const port = __privateGet(this, _voicePorts)[index];
|
|
1059
|
+
if (port) capabilities = capabilities.with(POLY_VOICE, Object.freeze({ index, voices: __privateGet(this, _voicePorts).length, notes: port }));
|
|
1060
|
+
}
|
|
1061
|
+
return child.derive({ id: child.id, registry: child.registry, capabilities });
|
|
1062
|
+
}
|
|
1063
|
+
exportOptions() {
|
|
1064
|
+
return { inlets: [...this.inlets], outlets: [...this.outlets] };
|
|
1065
|
+
}
|
|
1066
|
+
onDispose() {
|
|
1067
|
+
for (const lease of __privateGet(this, _leases).splice(0)) lease.release();
|
|
1068
|
+
this.voiceInput?.dispose();
|
|
1069
|
+
for (const port of __privateGet(this, _voicePorts)) port.dispose();
|
|
1070
|
+
for (const dispose of __privateGet(this, _disposers).splice(0)) dispose();
|
|
1071
|
+
}
|
|
1072
|
+
};
|
|
1073
|
+
_inlets = new WeakMap();
|
|
1074
|
+
_outlets = new WeakMap();
|
|
1075
|
+
_disposers = new WeakMap();
|
|
1076
|
+
_leases = new WeakMap();
|
|
1077
|
+
_voicePorts = new WeakMap();
|
|
1078
|
+
_voicesLent = new WeakMap();
|
|
1079
|
+
_audio = new WeakMap();
|
|
1080
|
+
_NestedComponent.type = "webmusic.nested";
|
|
1081
|
+
var NestedComponent = _NestedComponent;
|
|
1082
|
+
var _PolyComponent = class _PolyComponent extends NestedComponent {
|
|
1083
|
+
constructor(options = {}) {
|
|
1084
|
+
const voices = Math.trunc(Number(options.voices ?? 4));
|
|
1085
|
+
if (!(voices >= 1 && voices <= 64)) throw new RangeError("poly needs 1 to 64 voices.");
|
|
1086
|
+
super(options, _PolyComponent.type, voices);
|
|
1087
|
+
this.voices = voices;
|
|
1088
|
+
}
|
|
1089
|
+
/** Notes in, one voice each. */
|
|
1090
|
+
get notes() {
|
|
1091
|
+
return this.voiceInput;
|
|
1092
|
+
}
|
|
1093
|
+
exportOptions() {
|
|
1094
|
+
return { ...super.exportOptions(), voices: this.voices };
|
|
1095
|
+
}
|
|
1096
|
+
};
|
|
1097
|
+
_PolyComponent.type = "webmusic.poly";
|
|
1098
|
+
var PolyComponent = _PolyComponent;
|
|
1099
|
+
function boundaryOptions(options) {
|
|
1100
|
+
const kind = options.kind ?? "message";
|
|
1101
|
+
if (!BOUNDARY_KINDS.includes(kind)) throw new TypeError("A boundary carries message, audio, note or source.");
|
|
1102
|
+
const index = options.index;
|
|
1103
|
+
if (index !== void 0 && !(typeof index === "number" && Number.isFinite(index))) throw new TypeError("A boundary index is a number.");
|
|
1104
|
+
return { kind, index: Math.max(0, Math.trunc(index ?? 0)) };
|
|
1105
|
+
}
|
|
1106
|
+
var what = (kind) => kind === "message" ? "messages" : kind === "audio" ? "audio" : kind === "note" ? "notes" : "a source";
|
|
1107
|
+
var _channel, _port2, _release;
|
|
1108
|
+
var _InletModule = class _InletModule extends GraphModule {
|
|
1109
|
+
constructor(options = {}) {
|
|
1110
|
+
super(_InletModule.type, options);
|
|
1111
|
+
__privateAdd(this, _channel);
|
|
1112
|
+
__privateAdd(this, _port2);
|
|
1113
|
+
__privateAdd(this, _release, []);
|
|
1114
|
+
const { kind, index } = boundaryOptions(options);
|
|
1115
|
+
this.kind = kind;
|
|
1116
|
+
this.index = index;
|
|
1117
|
+
const found = requireContext(_InletModule.type, this.context).capabilities.get(COMPONENT_BOUNDARY)?.inlet(index);
|
|
1118
|
+
const channel = __privateSet(this, _channel, found?.kind === kind ? found : void 0);
|
|
1119
|
+
const descriptor = { id: "output", label: `${what(kind)} from outside`, kind, direction: "output" };
|
|
1120
|
+
if (kind === "message") {
|
|
1121
|
+
const port = new MessageOutputPort({ id: "output", onListenerError: (error) => this.fail(error) });
|
|
1122
|
+
const stop = channel?.messages?.subscribe((value) => port.send(value));
|
|
1123
|
+
__privateGet(this, _release).push(() => stop?.());
|
|
1124
|
+
this.addPort(port, descriptor);
|
|
1125
|
+
} else if (kind === "note") {
|
|
1126
|
+
const notes = new NoteOutputPort({ id: "output" });
|
|
1127
|
+
const stop = channel?.notes?.subscribe(repeat(notes));
|
|
1128
|
+
__privateGet(this, _release).push(() => {
|
|
1129
|
+
stop?.();
|
|
1130
|
+
notes.dispose();
|
|
1131
|
+
});
|
|
1132
|
+
this.addPort(notes, descriptor);
|
|
1133
|
+
} else if (kind === "audio") {
|
|
1134
|
+
const node = this.audio().createGain();
|
|
1135
|
+
const lease = channel?.audio?.send(node) ?? createConnectionLease(() => {
|
|
1136
|
+
});
|
|
1137
|
+
__privateSet(this, _port2, new AudioOutputPort(node, { id: "output" }));
|
|
1138
|
+
const port = __privateGet(this, _port2);
|
|
1139
|
+
__privateGet(this, _release).push(() => {
|
|
1140
|
+
lease.release();
|
|
1141
|
+
port.dispose();
|
|
1142
|
+
node.disconnect();
|
|
1143
|
+
});
|
|
1144
|
+
this.addPort(void 0, descriptor);
|
|
1145
|
+
} else this.addPort(void 0, descriptor);
|
|
1146
|
+
}
|
|
1147
|
+
getPort(name) {
|
|
1148
|
+
return name === "output" ? __privateGet(this, _port2) : void 0;
|
|
1149
|
+
}
|
|
1150
|
+
/** A source inlet carries the component's inlet relay. */
|
|
1151
|
+
sourceOutput(name) {
|
|
1152
|
+
return name === "output" && this.kind === "source" ? __privateGet(this, _channel)?.source : void 0;
|
|
1153
|
+
}
|
|
1154
|
+
exportOptions() {
|
|
1155
|
+
return { kind: this.kind, index: this.index };
|
|
1156
|
+
}
|
|
1157
|
+
onDispose() {
|
|
1158
|
+
for (const release of __privateGet(this, _release).splice(0)) release();
|
|
1159
|
+
}
|
|
1160
|
+
};
|
|
1161
|
+
_channel = new WeakMap();
|
|
1162
|
+
_port2 = new WeakMap();
|
|
1163
|
+
_release = new WeakMap();
|
|
1164
|
+
_InletModule.type = "webmusic.inlet";
|
|
1165
|
+
var InletModule = _InletModule;
|
|
1166
|
+
var _channel2, _node, _release2;
|
|
1167
|
+
var _OutletModule = class _OutletModule extends GraphModule {
|
|
1168
|
+
constructor(options = {}) {
|
|
1169
|
+
super(_OutletModule.type, options);
|
|
1170
|
+
__privateAdd(this, _channel2);
|
|
1171
|
+
__privateAdd(this, _node);
|
|
1172
|
+
__privateAdd(this, _release2, []);
|
|
1173
|
+
const { kind, index } = boundaryOptions(options);
|
|
1174
|
+
this.kind = kind;
|
|
1175
|
+
this.index = index;
|
|
1176
|
+
const found = requireContext(_OutletModule.type, this.context).capabilities.get(COMPONENT_BOUNDARY)?.outlet(index);
|
|
1177
|
+
const channel = __privateSet(this, _channel2, found?.kind === kind ? found : void 0);
|
|
1178
|
+
const label = `${what(kind)} to send outside`;
|
|
1179
|
+
if (kind === "message") this.input("input", label, (value) => channel?.send?.(value));
|
|
1180
|
+
else if (kind === "note") {
|
|
1181
|
+
const notes = new NoteInputPort({ accept: (event) => channel?.notes?.accept(event) }, { id: "input", multiplicity: "sum" });
|
|
1182
|
+
__privateGet(this, _release2).push(() => notes.dispose());
|
|
1183
|
+
this.addPort(notes, { id: "input", label, kind, direction: "input" });
|
|
1184
|
+
} else if (kind === "audio") {
|
|
1185
|
+
const node = __privateSet(this, _node, this.audio().createGain());
|
|
1186
|
+
const target = channel?.input;
|
|
1187
|
+
if (target) node.connect(target);
|
|
1188
|
+
__privateGet(this, _release2).push(() => {
|
|
1189
|
+
if (target) {
|
|
1190
|
+
try {
|
|
1191
|
+
node.disconnect(target);
|
|
1192
|
+
} catch {
|
|
1193
|
+
}
|
|
1194
|
+
}
|
|
1195
|
+
node.disconnect();
|
|
1196
|
+
});
|
|
1197
|
+
this.addPort(void 0, { id: "input", label, kind, direction: "input" });
|
|
1198
|
+
} else this.addPort(void 0, { id: "input", label, kind, direction: "input" });
|
|
1199
|
+
}
|
|
1200
|
+
getInput(name) {
|
|
1201
|
+
return name === "input" ? __privateGet(this, _node) : void 0;
|
|
1202
|
+
}
|
|
1203
|
+
/** A source outlet hands what reaches it to the component's outlet relay. */
|
|
1204
|
+
connectSource(name, upstream) {
|
|
1205
|
+
return name === "input" && this.kind === "source" ? __privateGet(this, _channel2)?.source?.follow(upstream) : void 0;
|
|
1206
|
+
}
|
|
1207
|
+
exportOptions() {
|
|
1208
|
+
return { kind: this.kind, index: this.index };
|
|
1209
|
+
}
|
|
1210
|
+
onDispose() {
|
|
1211
|
+
for (const release of __privateGet(this, _release2).splice(0)) release();
|
|
1212
|
+
}
|
|
1213
|
+
};
|
|
1214
|
+
_channel2 = new WeakMap();
|
|
1215
|
+
_node = new WeakMap();
|
|
1216
|
+
_release2 = new WeakMap();
|
|
1217
|
+
_OutletModule.type = "webmusic.outlet";
|
|
1218
|
+
var OutletModule = _OutletModule;
|
|
1219
|
+
var _release3;
|
|
1220
|
+
var _VoiceNotes = class _VoiceNotes extends GraphModule {
|
|
1221
|
+
constructor(options = {}) {
|
|
1222
|
+
super(_VoiceNotes.type, options);
|
|
1223
|
+
this.notes = new NoteOutputPort({ id: "notes" });
|
|
1224
|
+
__privateAdd(this, _release3);
|
|
1225
|
+
this.voice = requireContext(_VoiceNotes.type, this.context).capabilities.get(POLY_VOICE);
|
|
1226
|
+
__privateSet(this, _release3, this.voice?.notes.subscribe(repeat(this.notes)));
|
|
1227
|
+
this.addPort(this.notes, { id: "notes", label: `notes for voice ${this.voice ? this.voice.index + 1 : "\u2014"}`, kind: "note", direction: "output" });
|
|
1228
|
+
}
|
|
1229
|
+
exportOptions() {
|
|
1230
|
+
return {};
|
|
1231
|
+
}
|
|
1232
|
+
onDispose() {
|
|
1233
|
+
var _a;
|
|
1234
|
+
(_a = __privateGet(this, _release3)) == null ? void 0 : _a.call(this);
|
|
1235
|
+
}
|
|
1236
|
+
};
|
|
1237
|
+
_release3 = new WeakMap();
|
|
1238
|
+
_VoiceNotes.type = "webmusic.voice";
|
|
1239
|
+
var VoiceNotes = _VoiceNotes;
|
|
1240
|
+
|
|
1241
|
+
// src/module/preset-modules.ts
|
|
1242
|
+
function controlValue(module) {
|
|
1243
|
+
const control = module.capabilities?.get(MODULE_CONTROL);
|
|
1244
|
+
const snapshot = control?.snapshot();
|
|
1245
|
+
if (!control || !snapshot || typeof snapshot !== "object") return void 0;
|
|
1246
|
+
const float = snapshot.float === true;
|
|
1247
|
+
const number2 = (value) => float ? floatAtom(value) : intAtom(Math.round(value));
|
|
1248
|
+
switch (control.kind) {
|
|
1249
|
+
case "toggle":
|
|
1250
|
+
return { value: snapshot.on ? 1 : 0, restore: (value) => message("int", [intAtom(Number(value) >= 0.5 ? 1 : 0)]) };
|
|
1251
|
+
case "number":
|
|
1252
|
+
case "slider":
|
|
1253
|
+
case "dial":
|
|
1254
|
+
return typeof snapshot.value === "number" ? { value: snapshot.value, restore: (value) => message(float ? "float" : "int", [number2(Number(value))]) } : void 0;
|
|
1255
|
+
case "menu":
|
|
1256
|
+
case "tab":
|
|
1257
|
+
return typeof snapshot.index === "number" ? { value: snapshot.index, restore: (value) => message("int", [intAtom(Math.round(Number(value)))]) } : void 0;
|
|
1258
|
+
case "multislider":
|
|
1259
|
+
return Array.isArray(snapshot.values) ? { value: snapshot.values.map(Number), restore: (value) => message("list", value.map(number2)) } : void 0;
|
|
1260
|
+
default:
|
|
1261
|
+
return void 0;
|
|
1262
|
+
}
|
|
1263
|
+
}
|
|
1264
|
+
var _current, _PresetModule_instances, others_fn, capture_fn, apply_fn;
|
|
1265
|
+
var _PresetModule = class _PresetModule extends ControlModule {
|
|
1266
|
+
constructor(options = {}) {
|
|
1267
|
+
super(_PresetModule.type, options);
|
|
1268
|
+
__privateAdd(this, _PresetModule_instances);
|
|
1269
|
+
this.kind = "preset";
|
|
1270
|
+
this.inputs = { control: this.input("control", "slot to recall, store n, clear n, interp a b f, weights \u2026", (value) => this.receive(value)) };
|
|
1271
|
+
this.outputs = { slot: this.output("slot", "the slot recalled") };
|
|
1272
|
+
__privateAdd(this, _current, 0);
|
|
1273
|
+
const count = options.count;
|
|
1274
|
+
if (count !== void 0 && !(typeof count === "number" && Number.isFinite(count))) throw new TypeError("A preset count is a number.");
|
|
1275
|
+
this.count = Math.max(1, Math.min(64, Math.trunc(count ?? 16)));
|
|
1276
|
+
this.bank = new PresetBank(options.slots && typeof options.slots === "object" ? options.slots : void 0);
|
|
1277
|
+
}
|
|
1278
|
+
/** The slot last stored or recalled; 0 after a blend. */
|
|
1279
|
+
get current() {
|
|
1280
|
+
return __privateGet(this, _current);
|
|
1281
|
+
}
|
|
1282
|
+
/** Capture the other modules into `slot`. */
|
|
1283
|
+
store(slot) {
|
|
1284
|
+
if (slot < 1 || slot > this.count) return;
|
|
1285
|
+
this.bank.store(slot, __privateMethod(this, _PresetModule_instances, capture_fn).call(this));
|
|
1286
|
+
__privateSet(this, _current, slot);
|
|
1287
|
+
this.changed();
|
|
1288
|
+
}
|
|
1289
|
+
/** Apply a stored slot and report it. */
|
|
1290
|
+
recall(slot, time) {
|
|
1291
|
+
const values = this.bank.recall(slot);
|
|
1292
|
+
if (!values) return;
|
|
1293
|
+
__privateSet(this, _current, slot);
|
|
1294
|
+
__privateMethod(this, _PresetModule_instances, apply_fn).call(this, values);
|
|
1295
|
+
this.changed();
|
|
1296
|
+
this.outputs.slot.send(message("int", [intAtom(slot)], time));
|
|
1297
|
+
}
|
|
1298
|
+
/** Apply a blend of stored slots. */
|
|
1299
|
+
blend(values) {
|
|
1300
|
+
__privateMethod(this, _PresetModule_instances, apply_fn).call(this, values);
|
|
1301
|
+
__privateSet(this, _current, 0);
|
|
1302
|
+
this.changed();
|
|
1303
|
+
}
|
|
1304
|
+
snapshot() {
|
|
1305
|
+
return { count: this.count, stored: [...this.bank.slots], current: __privateGet(this, _current) };
|
|
1306
|
+
}
|
|
1307
|
+
exportOptions() {
|
|
1308
|
+
return { count: this.count, slots: this.bank.toJSON() };
|
|
1309
|
+
}
|
|
1310
|
+
receive(value) {
|
|
1311
|
+
const numbers = atomsOf(value).filter(isNumberAtom).map((atom) => atom.value);
|
|
1312
|
+
switch (value.selector) {
|
|
1313
|
+
case "int":
|
|
1314
|
+
case "float":
|
|
1315
|
+
this.recall(Math.trunc(numbers[0]), value.time);
|
|
1316
|
+
return;
|
|
1317
|
+
case "bang":
|
|
1318
|
+
if (__privateGet(this, _current)) this.recall(__privateGet(this, _current), value.time);
|
|
1319
|
+
return;
|
|
1320
|
+
case "store":
|
|
1321
|
+
this.store(Math.trunc(numbers[0] ?? 0));
|
|
1322
|
+
return;
|
|
1323
|
+
case "clear":
|
|
1324
|
+
this.bank.clear(Math.trunc(numbers[0] ?? 0));
|
|
1325
|
+
this.changed();
|
|
1326
|
+
return;
|
|
1327
|
+
case "interp": {
|
|
1328
|
+
const [from, to, fraction] = numbers;
|
|
1329
|
+
if (from === void 0 || to === void 0 || fraction === void 0) return;
|
|
1330
|
+
this.blend(this.bank.between(Math.trunc(from), Math.trunc(to), fraction));
|
|
1331
|
+
return;
|
|
1332
|
+
}
|
|
1333
|
+
case "weights":
|
|
1334
|
+
this.blend(this.bank.interpolate(Object.fromEntries(numbers.map((weight, index) => [index + 1, weight]))));
|
|
1335
|
+
return;
|
|
1336
|
+
}
|
|
1337
|
+
}
|
|
1338
|
+
};
|
|
1339
|
+
_current = new WeakMap();
|
|
1340
|
+
_PresetModule_instances = new WeakSet();
|
|
1341
|
+
others_fn = function() {
|
|
1342
|
+
return (this.context?.registry.snapshot() ?? []).filter((module) => module.id !== this.id);
|
|
1343
|
+
};
|
|
1344
|
+
capture_fn = function() {
|
|
1345
|
+
const modules = __privateMethod(this, _PresetModule_instances, others_fn).call(this);
|
|
1346
|
+
const values = captureParameters(modules);
|
|
1347
|
+
for (const module of modules) {
|
|
1348
|
+
const held = controlValue(module);
|
|
1349
|
+
if (held) values[`${module.id}#value`] = held.value;
|
|
1350
|
+
}
|
|
1351
|
+
return values;
|
|
1352
|
+
};
|
|
1353
|
+
apply_fn = function(values) {
|
|
1354
|
+
const modules = __privateMethod(this, _PresetModule_instances, others_fn).call(this);
|
|
1355
|
+
applyParameters(modules, values);
|
|
1356
|
+
for (const module of modules) {
|
|
1357
|
+
const value = values[`${module.id}#value`];
|
|
1358
|
+
const held = value === void 0 ? void 0 : controlValue(module);
|
|
1359
|
+
if (held) module.capabilities?.get(MODULE_CONTROL)?.interact(held.restore(value));
|
|
1360
|
+
}
|
|
1361
|
+
};
|
|
1362
|
+
_PresetModule.type = "webmusic.preset";
|
|
1363
|
+
var PresetModule = _PresetModule;
|
|
1364
|
+
function nodeWeights(nodes, cursor) {
|
|
1365
|
+
const raw = nodes.map((node) => Math.max(0, 1 - Math.hypot(cursor.x - node.x, cursor.y - node.y) / node.radius));
|
|
1366
|
+
const total = raw.reduce((sum, weight) => sum + weight, 0);
|
|
1367
|
+
return total > 0 ? raw.map((weight) => weight / total) : raw;
|
|
1368
|
+
}
|
|
1369
|
+
var _nodes, _cursor, _NodesModule_instances, send_fn;
|
|
1370
|
+
var _NodesModule = class _NodesModule extends ControlModule {
|
|
1371
|
+
constructor(options = {}) {
|
|
1372
|
+
super(_NodesModule.type, options);
|
|
1373
|
+
__privateAdd(this, _NodesModule_instances);
|
|
1374
|
+
this.kind = "nodes";
|
|
1375
|
+
this.inputs = { control: this.input("control", "cursor x y, node i x y", (value) => this.receive(value)) };
|
|
1376
|
+
this.outputs = { weights: this.output("weights", "weights, one per node") };
|
|
1377
|
+
__privateAdd(this, _nodes);
|
|
1378
|
+
__privateAdd(this, _cursor);
|
|
1379
|
+
if (options.count !== void 0 && !(typeof options.count === "number" && Number.isFinite(options.count))) throw new TypeError("A node count is a number.");
|
|
1380
|
+
const count = Math.max(2, Math.min(12, Math.trunc(options.count ?? 4)));
|
|
1381
|
+
__privateSet(this, _nodes, Array.isArray(options.nodes) && options.nodes.length === count ? options.nodes.map((node) => ({ ...node })) : Array.from({ length: count }, (_, index) => {
|
|
1382
|
+
const angle = index / count * Math.PI * 2 - Math.PI / 2;
|
|
1383
|
+
return { x: 0.5 + 0.3 * Math.cos(angle), y: 0.5 + 0.3 * Math.sin(angle), radius: 0.45 };
|
|
1384
|
+
}));
|
|
1385
|
+
__privateSet(this, _cursor, options.cursor && Number.isFinite(options.cursor.x) ? { ...options.cursor } : { x: 0.5, y: 0.5 });
|
|
1386
|
+
}
|
|
1387
|
+
get nodes() {
|
|
1388
|
+
return __privateGet(this, _nodes).map((node) => ({ ...node }));
|
|
1389
|
+
}
|
|
1390
|
+
get cursor() {
|
|
1391
|
+
return { ...__privateGet(this, _cursor) };
|
|
1392
|
+
}
|
|
1393
|
+
/** The weight of each node for the cursor where it is. */
|
|
1394
|
+
get weights() {
|
|
1395
|
+
return nodeWeights(__privateGet(this, _nodes), __privateGet(this, _cursor));
|
|
1396
|
+
}
|
|
1397
|
+
/** Move the cursor and send the weights. */
|
|
1398
|
+
moveCursor(x, y, time) {
|
|
1399
|
+
__privateSet(this, _cursor, { x: clamp(x), y: clamp(y) });
|
|
1400
|
+
this.changed();
|
|
1401
|
+
__privateMethod(this, _NodesModule_instances, send_fn).call(this, time);
|
|
1402
|
+
}
|
|
1403
|
+
/** Move a node, and resize it when `radius` is given, and send the weights. */
|
|
1404
|
+
moveNode(index, x, y, radius, time) {
|
|
1405
|
+
const node = __privateGet(this, _nodes)[Math.trunc(index)];
|
|
1406
|
+
if (!node) return;
|
|
1407
|
+
node.x = clamp(x);
|
|
1408
|
+
node.y = clamp(y);
|
|
1409
|
+
if (radius !== void 0 && radius > 0) node.radius = Math.min(1, radius);
|
|
1410
|
+
this.changed();
|
|
1411
|
+
__privateMethod(this, _NodesModule_instances, send_fn).call(this, time);
|
|
1412
|
+
}
|
|
1413
|
+
snapshot() {
|
|
1414
|
+
return { nodes: this.nodes, cursor: { ...__privateGet(this, _cursor) }, weights: this.weights };
|
|
1415
|
+
}
|
|
1416
|
+
exportOptions() {
|
|
1417
|
+
return { count: __privateGet(this, _nodes).length, nodes: this.nodes, cursor: { ...__privateGet(this, _cursor) } };
|
|
1418
|
+
}
|
|
1419
|
+
receive(value) {
|
|
1420
|
+
const numbers = atomsOf(value).filter(isNumberAtom).map((atom) => atom.value);
|
|
1421
|
+
if (value.selector === "cursor" && numbers.length >= 2) {
|
|
1422
|
+
this.moveCursor(numbers[0], numbers[1], value.time);
|
|
1423
|
+
return;
|
|
1424
|
+
}
|
|
1425
|
+
if (value.selector === "node" && numbers.length >= 3) {
|
|
1426
|
+
this.moveNode(numbers[0], numbers[1], numbers[2], numbers[3], value.time);
|
|
1427
|
+
return;
|
|
1428
|
+
}
|
|
1429
|
+
if (value.selector === "bang") __privateMethod(this, _NodesModule_instances, send_fn).call(this, value.time);
|
|
1430
|
+
}
|
|
1431
|
+
};
|
|
1432
|
+
_nodes = new WeakMap();
|
|
1433
|
+
_cursor = new WeakMap();
|
|
1434
|
+
_NodesModule_instances = new WeakSet();
|
|
1435
|
+
send_fn = function(time) {
|
|
1436
|
+
this.outputs.weights.send(message("list", this.weights.map((weight) => floatAtom(weight)), time));
|
|
1437
|
+
};
|
|
1438
|
+
_NodesModule.type = "webmusic.nodes";
|
|
1439
|
+
var NodesModule = _NodesModule;
|
|
1440
|
+
var clamp = (value) => Math.min(1, Math.max(0, value));
|
|
1441
|
+
|
|
1442
|
+
// src/module/structure-plugins.ts
|
|
1443
|
+
function createStructurePlugins(environment = {}) {
|
|
1444
|
+
const classes = [NoteMaker, NestedComponent, PolyComponent, InletModule, OutletModule, VoiceNotes, PresetModule, NodesModule, ChannelPack, ChannelUnpack];
|
|
1445
|
+
return Object.freeze(classes.map((Type) => modulePlugin(Type.type, (options) => new Type(options), environment)));
|
|
1446
|
+
}
|
|
1447
|
+
|
|
1448
|
+
// src/module/source-port.ts
|
|
1449
|
+
var SOURCE_SCHEMAS = Object.freeze({
|
|
1450
|
+
score: "webmusic.score",
|
|
1451
|
+
scoreDocument: "webmusic.score-document",
|
|
1452
|
+
audioClip: "webmusic.audio-clip",
|
|
1453
|
+
audioDocument: "webmusic.audio-document"
|
|
1454
|
+
});
|
|
1455
|
+
var _listeners3, _snapshot, _disposed2, _SourcePublisher_instances, commit_fn;
|
|
1456
|
+
var SourcePublisher = class {
|
|
1457
|
+
constructor(schemaId, options = {}) {
|
|
1458
|
+
__privateAdd(this, _SourcePublisher_instances);
|
|
1459
|
+
this.family = "source";
|
|
1460
|
+
this.direction = "output";
|
|
1461
|
+
this.multiplicity = "single";
|
|
1462
|
+
__privateAdd(this, _listeners3);
|
|
1463
|
+
__privateAdd(this, _snapshot, Object.freeze({ revision: 0, readiness: "empty" }));
|
|
1464
|
+
__privateAdd(this, _disposed2, false);
|
|
1465
|
+
if (typeof schemaId !== "string" || !schemaId.trim()) throw new TypeError("Source schemaId must be a nonempty string");
|
|
1466
|
+
const version = options.schemaVersion ?? 1;
|
|
1467
|
+
if (!Number.isInteger(version) || version < 1) throw new TypeError("Port schemaVersion must be a positive integer");
|
|
1468
|
+
this.schemaId = schemaId;
|
|
1469
|
+
this.schemaVersion = version;
|
|
1470
|
+
this.id = options.id ?? "source";
|
|
1471
|
+
__privateSet(this, _listeners3, new PortListeners(options.onListenerError));
|
|
1472
|
+
Object.freeze(this);
|
|
1473
|
+
}
|
|
1474
|
+
get disposed() {
|
|
1475
|
+
return __privateGet(this, _disposed2);
|
|
1476
|
+
}
|
|
1477
|
+
get readiness() {
|
|
1478
|
+
return __privateGet(this, _disposed2) ? "detached" : "ready";
|
|
1479
|
+
}
|
|
1480
|
+
get revision() {
|
|
1481
|
+
return __privateGet(this, _snapshot).revision;
|
|
1482
|
+
}
|
|
1483
|
+
snapshot() {
|
|
1484
|
+
return __privateGet(this, _snapshot);
|
|
1485
|
+
}
|
|
1486
|
+
subscribe(listener) {
|
|
1487
|
+
if (__privateGet(this, _disposed2)) throw new Error("SourcePublisher is disposed");
|
|
1488
|
+
return __privateGet(this, _listeners3).add(listener);
|
|
1489
|
+
}
|
|
1490
|
+
/** Mark a replacement as pending; returns the revision a later result must still match. */
|
|
1491
|
+
loading() {
|
|
1492
|
+
return __privateMethod(this, _SourcePublisher_instances, commit_fn).call(this, { readiness: "loading" });
|
|
1493
|
+
}
|
|
1494
|
+
ready(value) {
|
|
1495
|
+
return __privateMethod(this, _SourcePublisher_instances, commit_fn).call(this, { readiness: "ready", value });
|
|
1496
|
+
}
|
|
1497
|
+
fail(error) {
|
|
1498
|
+
return __privateMethod(this, _SourcePublisher_instances, commit_fn).call(this, { readiness: "error", error });
|
|
1499
|
+
}
|
|
1500
|
+
clear() {
|
|
1501
|
+
return __privateMethod(this, _SourcePublisher_instances, commit_fn).call(this, { readiness: "empty" });
|
|
1502
|
+
}
|
|
1503
|
+
/** Detach every consumer. The last snapshot stays readable; no further revision is published. */
|
|
1504
|
+
dispose() {
|
|
1505
|
+
if (__privateGet(this, _disposed2)) return;
|
|
1506
|
+
__privateSet(this, _disposed2, true);
|
|
1507
|
+
__privateGet(this, _listeners3).clear();
|
|
1508
|
+
}
|
|
1509
|
+
};
|
|
1510
|
+
_listeners3 = new WeakMap();
|
|
1511
|
+
_snapshot = new WeakMap();
|
|
1512
|
+
_disposed2 = new WeakMap();
|
|
1513
|
+
_SourcePublisher_instances = new WeakSet();
|
|
1514
|
+
commit_fn = function(next) {
|
|
1515
|
+
if (__privateGet(this, _disposed2)) throw new Error("SourcePublisher is disposed");
|
|
1516
|
+
const snapshot = Object.freeze({ ...next, revision: __privateGet(this, _snapshot).revision + 1 });
|
|
1517
|
+
__privateSet(this, _snapshot, snapshot);
|
|
1518
|
+
__privateGet(this, _listeners3).emit(snapshot);
|
|
1519
|
+
return snapshot.revision;
|
|
1520
|
+
};
|
|
1521
|
+
|
|
1522
|
+
// src/module/conformance.ts
|
|
1523
|
+
function message2(error) {
|
|
1524
|
+
return error instanceof Error ? error.message : String(error);
|
|
1525
|
+
}
|
|
1526
|
+
async function checkModuleConformance(create, options = {}) {
|
|
1527
|
+
const failures = [];
|
|
1528
|
+
const fail = (check, error) => {
|
|
1529
|
+
failures.push(Object.freeze({ check, message: message2(error) }));
|
|
1530
|
+
};
|
|
1531
|
+
const repetitions = options.repetitions ?? 2;
|
|
1532
|
+
if (!Number.isInteger(repetitions) || repetitions < 1) throw new RangeError("repetitions must be a positive integer");
|
|
1533
|
+
let instances = 0;
|
|
1534
|
+
for (let index = 0; index < repetitions; index++) {
|
|
1535
|
+
let subject;
|
|
1536
|
+
try {
|
|
1537
|
+
subject = await create();
|
|
1538
|
+
} catch (error) {
|
|
1539
|
+
fail("create", error);
|
|
1540
|
+
continue;
|
|
1541
|
+
}
|
|
1542
|
+
instances++;
|
|
1543
|
+
const { module } = subject;
|
|
1544
|
+
try {
|
|
1545
|
+
assertModuleId(module.id);
|
|
1546
|
+
} catch (error) {
|
|
1547
|
+
fail("id", error);
|
|
1548
|
+
}
|
|
1549
|
+
if (options.expectedId !== void 0 && module.id !== options.expectedId) {
|
|
1550
|
+
fail("id", `Expected module ID ${options.expectedId}, received ${module.id}`);
|
|
1551
|
+
}
|
|
1552
|
+
try {
|
|
1553
|
+
const handle = createModuleHandle(module);
|
|
1554
|
+
if (!Object.isFrozen(handle)) fail("handle", "createModuleHandle must return a frozen view");
|
|
1555
|
+
if (handle.params !== module.params || handle.inserts !== module.inserts || handle.capabilities !== module.capabilities) {
|
|
1556
|
+
fail("handle", "Handle getters must delegate to the module");
|
|
1557
|
+
}
|
|
1558
|
+
} catch (error) {
|
|
1559
|
+
fail("handle", error);
|
|
1560
|
+
}
|
|
1561
|
+
let ports = [];
|
|
1562
|
+
try {
|
|
1563
|
+
ports = module.ports?.() ?? [];
|
|
1564
|
+
if (!Array.isArray(ports)) throw new TypeError("ports() must return an array");
|
|
1565
|
+
const keys = /* @__PURE__ */ new Set();
|
|
1566
|
+
for (const port of ports) {
|
|
1567
|
+
try {
|
|
1568
|
+
assertPortInfo(port);
|
|
1569
|
+
} catch (error) {
|
|
1570
|
+
fail(`port:${String(port?.id)}`, error);
|
|
1571
|
+
continue;
|
|
1572
|
+
}
|
|
1573
|
+
const key = `${port.family}/${port.direction}/${port.id}`;
|
|
1574
|
+
if (keys.has(key)) fail("ports", `Duplicate port ${key}`);
|
|
1575
|
+
keys.add(key);
|
|
1576
|
+
}
|
|
1577
|
+
const again = module.ports?.() ?? [];
|
|
1578
|
+
if (again.length !== ports.length || again.some((port, position) => port !== ports[position])) {
|
|
1579
|
+
fail("ports", "ports() must return the same port objects while nothing changed");
|
|
1580
|
+
}
|
|
1581
|
+
for (const port of ports) {
|
|
1582
|
+
if (port.family !== "audio" || port.direction !== "output" || !module.getPort) continue;
|
|
1583
|
+
const shortcut = module.getPort(port.id);
|
|
1584
|
+
if (shortcut !== void 0 && shortcut !== port) fail("ports", `getPort(${port.id}) differs from ports()`);
|
|
1585
|
+
}
|
|
1586
|
+
} catch (error) {
|
|
1587
|
+
fail("ports", error);
|
|
1588
|
+
}
|
|
1589
|
+
if (module.subscribe) {
|
|
1590
|
+
try {
|
|
1591
|
+
const stop = module.subscribe(() => {
|
|
1592
|
+
});
|
|
1593
|
+
if (typeof stop !== "function") throw new TypeError("subscribe() must return an unsubscribe function");
|
|
1594
|
+
stop();
|
|
1595
|
+
stop();
|
|
1596
|
+
} catch (error) {
|
|
1597
|
+
fail("subscribe", error);
|
|
1598
|
+
}
|
|
1599
|
+
}
|
|
1600
|
+
if (options.prepare && module.prepare) {
|
|
1601
|
+
try {
|
|
1602
|
+
await module.prepare();
|
|
1603
|
+
await module.prepare();
|
|
1604
|
+
} catch (error) {
|
|
1605
|
+
fail("prepare", error);
|
|
1606
|
+
}
|
|
1607
|
+
}
|
|
1608
|
+
if (subject.dispose) {
|
|
1609
|
+
try {
|
|
1610
|
+
await subject.dispose();
|
|
1611
|
+
await subject.dispose();
|
|
1612
|
+
} catch (error) {
|
|
1613
|
+
fail("dispose", error);
|
|
1614
|
+
}
|
|
1615
|
+
const borrowed = new Set(options.borrowedPorts ?? []);
|
|
1616
|
+
for (const port of ports) {
|
|
1617
|
+
if (borrowed.has(`${port.family}/${port.id}`)) continue;
|
|
1618
|
+
try {
|
|
1619
|
+
if (port.readiness !== "detached") fail("dispose", `Port ${port.family}/${port.id} is still ${port.readiness} after disposal`);
|
|
1620
|
+
} catch (error) {
|
|
1621
|
+
fail("dispose", error);
|
|
1622
|
+
}
|
|
1623
|
+
}
|
|
1624
|
+
}
|
|
1625
|
+
}
|
|
1626
|
+
return Object.freeze({ passed: failures.length === 0, instances, failures: Object.freeze(failures) });
|
|
1627
|
+
}
|
|
1628
|
+
|
|
1629
|
+
// src/module/control.ts
|
|
1630
|
+
var ControlLFO = class {
|
|
1631
|
+
constructor(context, options = {}) {
|
|
1632
|
+
this.disposed = false;
|
|
1633
|
+
const frequency = options.frequency ?? 1;
|
|
1634
|
+
if (!Number.isFinite(frequency) || frequency < 0 || frequency > context.sampleRate / 2) throw new RangeError("LFO frequency must be between zero and Nyquist.");
|
|
1635
|
+
const waveform = options.waveform ?? "sine";
|
|
1636
|
+
validateWaveform(waveform);
|
|
1637
|
+
this.oscillator = context.createOscillator();
|
|
1638
|
+
this.oscillator.type = waveform;
|
|
1639
|
+
this.oscillator.frequency.value = frequency;
|
|
1640
|
+
this.frequency = new ParamHandle({
|
|
1641
|
+
id: "frequency",
|
|
1642
|
+
label: "Rate",
|
|
1643
|
+
unit: "Hz",
|
|
1644
|
+
range: { min: 0, max: context.sampleRate / 2 },
|
|
1645
|
+
displayRange: { min: 0, max: Math.min(20, context.sampleRate / 2) },
|
|
1646
|
+
defaultValue: 1,
|
|
1647
|
+
updateTiming: "scheduled",
|
|
1648
|
+
modulation: "audio-rate"
|
|
1649
|
+
}, { baseValue: frequency });
|
|
1650
|
+
this.frequency.bindAudioParam(context, this.oscillator.frequency, { lazy: true });
|
|
1651
|
+
this.signal = Object.freeze({ output: this.oscillator, rate: "a-rate", range: Object.freeze({ min: -1, max: 1 }) });
|
|
1652
|
+
try {
|
|
1653
|
+
this.oscillator.start();
|
|
1654
|
+
} catch (error) {
|
|
1655
|
+
this.frequency.dispose();
|
|
1656
|
+
this.oscillator.disconnect();
|
|
1657
|
+
throw error;
|
|
1658
|
+
}
|
|
1659
|
+
}
|
|
1660
|
+
get waveform() {
|
|
1661
|
+
return this.oscillator.type;
|
|
1662
|
+
}
|
|
1663
|
+
setWaveform(waveform) {
|
|
1664
|
+
if (this.disposed) throw new Error("LFO is disposed.");
|
|
1665
|
+
validateWaveform(waveform);
|
|
1666
|
+
this.oscillator.type = waveform;
|
|
1667
|
+
}
|
|
1668
|
+
dispose() {
|
|
1669
|
+
if (this.disposed) return;
|
|
1670
|
+
this.disposed = true;
|
|
1671
|
+
let failure;
|
|
1672
|
+
try {
|
|
1673
|
+
this.frequency.dispose();
|
|
1674
|
+
} catch (error) {
|
|
1675
|
+
failure = error;
|
|
1676
|
+
}
|
|
1677
|
+
try {
|
|
1678
|
+
this.oscillator.stop();
|
|
1679
|
+
} catch (error) {
|
|
1680
|
+
failure ?? (failure = error);
|
|
1681
|
+
}
|
|
1682
|
+
try {
|
|
1683
|
+
this.oscillator.disconnect();
|
|
1684
|
+
} catch (error) {
|
|
1685
|
+
failure ?? (failure = error);
|
|
1686
|
+
}
|
|
1687
|
+
if (failure) throw failure;
|
|
1688
|
+
}
|
|
1689
|
+
};
|
|
1690
|
+
function validateWaveform(waveform) {
|
|
1691
|
+
if (!["sine", "square", "sawtooth", "triangle"].includes(waveform)) throw new TypeError("Unsupported LFO waveform.");
|
|
1692
|
+
}
|
|
1693
|
+
var ControlEnvelope = class {
|
|
1694
|
+
constructor(context, options = {}) {
|
|
1695
|
+
this.context = context;
|
|
1696
|
+
this.points = [{ time: 0, value: 0, linear: false }];
|
|
1697
|
+
this.disposed = false;
|
|
1698
|
+
this.options = { attack: options.attack ?? 0.01, decay: options.decay ?? 0.1, sustain: options.sustain ?? 0.7, release: options.release ?? 0.2 };
|
|
1699
|
+
for (const [name, value] of Object.entries(this.options)) {
|
|
1700
|
+
if (!Number.isFinite(value) || value < 0 || name === "sustain" && value > 1) throw new RangeError("Envelope times must be nonnegative and sustain must be in [0, 1].");
|
|
1701
|
+
}
|
|
1702
|
+
this.source = context.createConstantSource();
|
|
1703
|
+
this.source.offset.value = 0;
|
|
1704
|
+
this.signal = Object.freeze({ output: this.source, rate: "a-rate", range: Object.freeze({ min: 0, max: 1 }) });
|
|
1705
|
+
try {
|
|
1706
|
+
this.source.start();
|
|
1707
|
+
} catch (error) {
|
|
1708
|
+
this.source.disconnect();
|
|
1709
|
+
throw error;
|
|
1710
|
+
}
|
|
1711
|
+
}
|
|
1712
|
+
trigger(time, velocity = 1) {
|
|
1713
|
+
const now = this.context.currentTime;
|
|
1714
|
+
time = time === void 0 ? now : time;
|
|
1715
|
+
this.validateTime(time, now);
|
|
1716
|
+
if (!Number.isFinite(velocity) || velocity < 0 || velocity > 1) throw new RangeError("Envelope velocity must be in [0, 1].");
|
|
1717
|
+
const held = this.valueAt(time);
|
|
1718
|
+
const peak = time + this.options.attack;
|
|
1719
|
+
const sustain = peak + this.options.decay;
|
|
1720
|
+
this.replace(time, [
|
|
1721
|
+
{ time, value: held, linear: false },
|
|
1722
|
+
{ time: peak, value: velocity, linear: this.options.attack > 0 },
|
|
1723
|
+
{ time: sustain, value: velocity * this.options.sustain, linear: this.options.decay > 0 }
|
|
1724
|
+
], now);
|
|
1725
|
+
}
|
|
1726
|
+
release(time) {
|
|
1727
|
+
const now = this.context.currentTime;
|
|
1728
|
+
time = time === void 0 ? now : time;
|
|
1729
|
+
this.validateTime(time, now);
|
|
1730
|
+
this.replace(time, [
|
|
1731
|
+
{ time, value: this.valueAt(time), linear: false },
|
|
1732
|
+
{ time: time + this.options.release, value: 0, linear: this.options.release > 0 }
|
|
1733
|
+
], now);
|
|
1734
|
+
}
|
|
1735
|
+
dispose() {
|
|
1736
|
+
if (this.disposed) return;
|
|
1737
|
+
this.disposed = true;
|
|
1738
|
+
try {
|
|
1739
|
+
this.source.stop();
|
|
1740
|
+
} finally {
|
|
1741
|
+
this.source.disconnect();
|
|
1742
|
+
}
|
|
1743
|
+
}
|
|
1744
|
+
valueAt(time) {
|
|
1745
|
+
let previous = this.points[0];
|
|
1746
|
+
for (let index = 1; index < this.points.length; index++) {
|
|
1747
|
+
const point = this.points[index];
|
|
1748
|
+
if (time < point.time) {
|
|
1749
|
+
return point.linear ? previous.value + (point.value - previous.value) * (time - previous.time) / (point.time - previous.time) : previous.value;
|
|
1750
|
+
}
|
|
1751
|
+
previous = point;
|
|
1752
|
+
}
|
|
1753
|
+
return previous.value;
|
|
1754
|
+
}
|
|
1755
|
+
replace(time, points, now) {
|
|
1756
|
+
const prefix = this.points.filter((point) => point.time < time);
|
|
1757
|
+
const anchors = [{ ...points[0], linear: false }];
|
|
1758
|
+
if (time > now) {
|
|
1759
|
+
const previous = prefix[prefix.length - 1];
|
|
1760
|
+
const next = this.points.find((point) => point.time >= time);
|
|
1761
|
+
if (previous && next?.linear) {
|
|
1762
|
+
const incoming = previous.value + (next.value - previous.value) * (time - previous.time) / (next.time - previous.time);
|
|
1763
|
+
anchors[0] = { time, value: incoming, linear: true };
|
|
1764
|
+
if (incoming !== points[0].value) anchors.push({ ...points[0], linear: false });
|
|
1765
|
+
}
|
|
1766
|
+
}
|
|
1767
|
+
this.source.offset.cancelScheduledValues(time);
|
|
1768
|
+
const replacement = [...anchors, ...points.slice(1)];
|
|
1769
|
+
for (const point of replacement) {
|
|
1770
|
+
if (point.linear) this.source.offset.linearRampToValueAtTime(point.value, point.time);
|
|
1771
|
+
else this.source.offset.setValueAtTime(point.value, point.time);
|
|
1772
|
+
}
|
|
1773
|
+
const timeline = [...prefix, ...replacement];
|
|
1774
|
+
let first = 0;
|
|
1775
|
+
while (first + 1 < timeline.length && timeline[first + 1].time <= now) first++;
|
|
1776
|
+
this.points = first > 0 ? timeline.slice(first) : timeline;
|
|
1777
|
+
}
|
|
1778
|
+
validateTime(time, now) {
|
|
1779
|
+
if (this.disposed) throw new Error("Envelope is disposed.");
|
|
1780
|
+
if (!Number.isFinite(time) || time < now) throw new RangeError("Envelope time must be a finite current or future context time.");
|
|
1781
|
+
}
|
|
1782
|
+
};
|
|
1783
|
+
var EnvelopeFollower = class {
|
|
1784
|
+
constructor(context, options = {}) {
|
|
1785
|
+
this.disposed = false;
|
|
1786
|
+
const cutoff = options.smoothingHz ?? 20;
|
|
1787
|
+
if (!Number.isFinite(cutoff) || cutoff <= 0 || cutoff >= context.sampleRate / 2) throw new RangeError("Follower smoothing frequency must be positive and below Nyquist.");
|
|
1788
|
+
this.input = context.createGain();
|
|
1789
|
+
this.rectifier = context.createWaveShaper();
|
|
1790
|
+
const curve = new Float32Array(2049);
|
|
1791
|
+
for (let i = 0; i < curve.length; i++) curve[i] = Math.abs(i * 2 / (curve.length - 1) - 1);
|
|
1792
|
+
this.rectifier.curve = curve;
|
|
1793
|
+
const pole = Math.exp(-2 * Math.PI * cutoff / context.sampleRate);
|
|
1794
|
+
if (pole >= 1) throw new RangeError("Follower smoothing frequency is below native precision.");
|
|
1795
|
+
this.smoother = context.createIIRFilter([1 - pole], [1, -pole]);
|
|
1796
|
+
this.limiter = context.createWaveShaper();
|
|
1797
|
+
this.limiter.curve = new Float32Array([0, 0, 1]);
|
|
1798
|
+
try {
|
|
1799
|
+
this.input.connect(this.rectifier);
|
|
1800
|
+
this.rectifier.connect(this.smoother);
|
|
1801
|
+
this.smoother.connect(this.limiter);
|
|
1802
|
+
} catch (error) {
|
|
1803
|
+
for (const node of [this.input, this.rectifier, this.smoother, this.limiter]) {
|
|
1804
|
+
try {
|
|
1805
|
+
node.disconnect();
|
|
1806
|
+
} catch {
|
|
1807
|
+
}
|
|
1808
|
+
}
|
|
1809
|
+
throw error;
|
|
1810
|
+
}
|
|
1811
|
+
this.signal = Object.freeze({ output: this.limiter, rate: "a-rate", range: Object.freeze({ min: 0, max: 1 }) });
|
|
1812
|
+
}
|
|
1813
|
+
dispose() {
|
|
1814
|
+
if (this.disposed) return;
|
|
1815
|
+
this.disposed = true;
|
|
1816
|
+
let failure;
|
|
1817
|
+
for (const node of [this.input, this.rectifier, this.smoother, this.limiter]) {
|
|
1818
|
+
try {
|
|
1819
|
+
node.disconnect();
|
|
1820
|
+
} catch (error) {
|
|
1821
|
+
failure ?? (failure = error);
|
|
1822
|
+
}
|
|
1823
|
+
}
|
|
1824
|
+
if (failure) throw failure;
|
|
1825
|
+
}
|
|
1826
|
+
};
|
|
1827
|
+
|
|
1828
|
+
// src/module/param-macro.ts
|
|
1829
|
+
var ParamMacro = class {
|
|
1830
|
+
constructor(targets, options = {}) {
|
|
1831
|
+
this.disposed = false;
|
|
1832
|
+
this.applying = false;
|
|
1833
|
+
this.listeners = /* @__PURE__ */ new Set();
|
|
1834
|
+
const entries = [...targets];
|
|
1835
|
+
const seen = /* @__PURE__ */ new Set();
|
|
1836
|
+
for (const entry of entries) {
|
|
1837
|
+
if (seen.has(entry.target)) throw new Error("A macro cannot map one parameter twice.");
|
|
1838
|
+
seen.add(entry.target);
|
|
1839
|
+
const { min, max } = entry.target.descriptor.range;
|
|
1840
|
+
if (!Number.isFinite(entry.min) || !Number.isFinite(entry.max) || Math.min(entry.min, entry.max) < min || Math.max(entry.min, entry.max) > max) throw new RangeError("Macro mapping endpoints must fit the target legal range.");
|
|
1841
|
+
if (entry.target.snapshot.disposed) throw new Error("Macro target is disposed.");
|
|
1842
|
+
}
|
|
1843
|
+
this.current = options.value ?? 0;
|
|
1844
|
+
this.validate(this.current);
|
|
1845
|
+
this.targets = Object.freeze(entries.map((entry) => Object.freeze({ ...entry })));
|
|
1846
|
+
}
|
|
1847
|
+
get value() {
|
|
1848
|
+
return this.current;
|
|
1849
|
+
}
|
|
1850
|
+
setValue(value) {
|
|
1851
|
+
if (this.disposed) throw new Error("Macro is disposed.");
|
|
1852
|
+
if (this.applying) throw new Error("Macro updates cannot re-enter.");
|
|
1853
|
+
this.validate(value);
|
|
1854
|
+
const changes = this.targets.map((entry) => {
|
|
1855
|
+
if (entry.target.snapshot.disposed) throw new Error("Macro target is disposed.");
|
|
1856
|
+
return { target: entry.target, before: entry.target.snapshot.baseValue, after: entry.min + value * (entry.max - entry.min) };
|
|
1857
|
+
});
|
|
1858
|
+
this.applying = true;
|
|
1859
|
+
let applied = 0;
|
|
1860
|
+
try {
|
|
1861
|
+
for (const change of changes) {
|
|
1862
|
+
change.target.setBaseValue(change.after);
|
|
1863
|
+
applied++;
|
|
1864
|
+
if (this.disposed) throw new Error("Macro disposed during update.");
|
|
1865
|
+
}
|
|
1866
|
+
this.current = value;
|
|
1867
|
+
} catch (error) {
|
|
1868
|
+
for (const change of changes.slice(0, applied).reverse()) {
|
|
1869
|
+
try {
|
|
1870
|
+
change.target.setBaseValue(change.before);
|
|
1871
|
+
} catch {
|
|
1872
|
+
}
|
|
1873
|
+
}
|
|
1874
|
+
throw error;
|
|
1875
|
+
} finally {
|
|
1876
|
+
this.applying = false;
|
|
1877
|
+
}
|
|
1878
|
+
for (const entry of [...this.listeners]) {
|
|
1879
|
+
if (this.current !== value || this.disposed) break;
|
|
1880
|
+
if (!this.listeners.has(entry)) continue;
|
|
1881
|
+
try {
|
|
1882
|
+
const result = entry.listener(value);
|
|
1883
|
+
if (result != null && typeof result.then === "function") void Promise.resolve(result).catch(() => {
|
|
1884
|
+
});
|
|
1885
|
+
} catch {
|
|
1886
|
+
}
|
|
1887
|
+
}
|
|
1888
|
+
}
|
|
1889
|
+
subscribe(listener) {
|
|
1890
|
+
if (this.disposed) return () => {
|
|
1891
|
+
};
|
|
1892
|
+
const entry = { listener };
|
|
1893
|
+
this.listeners.add(entry);
|
|
1894
|
+
return () => {
|
|
1895
|
+
this.listeners.delete(entry);
|
|
1896
|
+
};
|
|
1897
|
+
}
|
|
1898
|
+
dispose() {
|
|
1899
|
+
this.disposed = true;
|
|
1900
|
+
this.listeners.clear();
|
|
1901
|
+
}
|
|
1902
|
+
validate(value) {
|
|
1903
|
+
if (!Number.isFinite(value) || value < 0 || value > 1) throw new RangeError("Macro value must be in [0, 1].");
|
|
1904
|
+
}
|
|
1905
|
+
};
|
|
1906
|
+
|
|
1907
|
+
// src/module/project-schema.ts
|
|
1908
|
+
var MAX_BYTES = 2 * 1024 * 1024;
|
|
1909
|
+
var MAX_NODES = 5e4;
|
|
1910
|
+
var MAX_ITEMS = 1e4;
|
|
1911
|
+
var forbiddenKeys = /* @__PURE__ */ new Set(["__proto__", "prototype", "constructor"]);
|
|
1912
|
+
function copyProjectJson(input) {
|
|
1913
|
+
const ancestors = /* @__PURE__ */ new Set();
|
|
1914
|
+
let nodes = 0;
|
|
1915
|
+
let bytes = 0;
|
|
1916
|
+
const copy = (value, depth) => {
|
|
1917
|
+
if (++nodes > MAX_NODES || depth > 32) throw new RangeError("Project JSON exceeds structural limits.");
|
|
1918
|
+
if (value === null || typeof value === "boolean") return value;
|
|
1919
|
+
if (typeof value === "string") {
|
|
1920
|
+
bytes += value.length * 2;
|
|
1921
|
+
if (value.length > 65536 || bytes > MAX_BYTES) throw new RangeError("Project JSON exceeds string limits.");
|
|
1922
|
+
return value;
|
|
1923
|
+
}
|
|
1924
|
+
if (typeof value === "number") {
|
|
1925
|
+
if (!Number.isFinite(value)) throw new TypeError("Project numbers must be finite.");
|
|
1926
|
+
return value;
|
|
1927
|
+
}
|
|
1928
|
+
if (typeof value !== "object") throw new TypeError("Project values must be plain JSON data.");
|
|
1929
|
+
if (ancestors.has(value)) throw new TypeError("Project JSON cannot contain object cycles.");
|
|
1930
|
+
const prototype = Object.getPrototypeOf(value);
|
|
1931
|
+
const array = Array.isArray(value);
|
|
1932
|
+
if (!array && prototype !== Object.prototype && prototype !== null) throw new TypeError("Project objects must have plain prototypes.");
|
|
1933
|
+
if (array && value.length > MAX_ITEMS) throw new RangeError("Project array exceeds item limit.");
|
|
1934
|
+
ancestors.add(value);
|
|
1935
|
+
try {
|
|
1936
|
+
const output = array ? [] : {};
|
|
1937
|
+
const keys = Reflect.ownKeys(value);
|
|
1938
|
+
if (keys.length > MAX_ITEMS + 1) throw new RangeError("Project object exceeds item limit.");
|
|
1939
|
+
for (const key of keys) {
|
|
1940
|
+
if (array && key === "length") continue;
|
|
1941
|
+
if (typeof key !== "string" || forbiddenKeys.has(key)) throw new TypeError("Project JSON contains an unsafe property key.");
|
|
1942
|
+
if (array && (!/^(0|[1-9][0-9]*)$/.test(key) || Number(key) >= value.length)) throw new TypeError("Project arrays must contain only indexed values.");
|
|
1943
|
+
bytes += key.length * 2;
|
|
1944
|
+
if (bytes > MAX_BYTES) throw new RangeError("Project JSON exceeds size limit.");
|
|
1945
|
+
const property = Object.getOwnPropertyDescriptor(value, key);
|
|
1946
|
+
if (!property.enumerable || !("value" in property)) throw new TypeError("Project JSON must not contain accessors or hidden properties.");
|
|
1947
|
+
output[key] = copy(property.value, depth + 1);
|
|
1948
|
+
}
|
|
1949
|
+
if (array && output.length !== value.length) throw new TypeError("Project arrays must not contain holes.");
|
|
1950
|
+
if (array && Object.keys(output).length !== value.length) throw new TypeError("Project arrays must not contain holes.");
|
|
1951
|
+
return Object.freeze(output);
|
|
1952
|
+
} finally {
|
|
1953
|
+
ancestors.delete(value);
|
|
1954
|
+
}
|
|
1955
|
+
};
|
|
1956
|
+
return copy(input, 0);
|
|
1957
|
+
}
|
|
1958
|
+
function object(value, path) {
|
|
1959
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) throw new TypeError(`${path} must be an object.`);
|
|
1960
|
+
return value;
|
|
1961
|
+
}
|
|
1962
|
+
function fields(data, allowed, path) {
|
|
1963
|
+
for (const key of Object.keys(data)) if (!allowed.includes(key)) throw new TypeError(`${path}.${key} is not a supported field.`);
|
|
1964
|
+
}
|
|
1965
|
+
function text2(value, path, max = 256) {
|
|
1966
|
+
if (typeof value !== "string" || !value || value !== value.trim() || value.length > max) throw new TypeError(`${path} must be a nonempty trimmed string of at most ${max} characters.`);
|
|
1967
|
+
return value;
|
|
1968
|
+
}
|
|
1969
|
+
function number(value, path, min = -Infinity, max = Infinity) {
|
|
1970
|
+
if (typeof value !== "number" || !Number.isFinite(value) || value < min || value > max) throw new TypeError(`${path} must be a finite number in range.`);
|
|
1971
|
+
return value;
|
|
1972
|
+
}
|
|
1973
|
+
function list(value, path) {
|
|
1974
|
+
if (value === void 0) return [];
|
|
1975
|
+
if (!Array.isArray(value)) throw new TypeError(`${path} must be an array.`);
|
|
1976
|
+
return value;
|
|
1977
|
+
}
|
|
1978
|
+
function enumeration(value, choices, path) {
|
|
1979
|
+
if (!choices.includes(value)) throw new TypeError(`${path} has an unsupported value.`);
|
|
1980
|
+
return value;
|
|
1981
|
+
}
|
|
1982
|
+
function optionalText(data, key, path) {
|
|
1983
|
+
return data[key] === void 0 ? void 0 : text2(data[key], `${path}.${key}`, key === "label" ? 1024 : 256);
|
|
1984
|
+
}
|
|
1985
|
+
function unique(items, path) {
|
|
1986
|
+
const result = /* @__PURE__ */ new Map();
|
|
1987
|
+
for (const item of items) {
|
|
1988
|
+
if (result.has(item.id)) throw new TypeError(`${path} contains duplicate ID ${item.id}.`);
|
|
1989
|
+
result.set(item.id, item);
|
|
1990
|
+
}
|
|
1991
|
+
return result;
|
|
1992
|
+
}
|
|
1993
|
+
function endpoint(value, path) {
|
|
1994
|
+
const data = object(value, path);
|
|
1995
|
+
fields(data, ["moduleId", "port"], path);
|
|
1996
|
+
return { moduleId: text2(data.moduleId, `${path}.moduleId`), port: text2(data.port, `${path}.port`) };
|
|
1997
|
+
}
|
|
1998
|
+
function parameter(value, path) {
|
|
1999
|
+
const data = object(value, path);
|
|
2000
|
+
fields(data, ["moduleId", "param"], path);
|
|
2001
|
+
return { moduleId: text2(data.moduleId, `${path}.moduleId`), param: text2(data.param, `${path}.param`) };
|
|
2002
|
+
}
|
|
2003
|
+
function layout(value) {
|
|
2004
|
+
const data = object(value ?? {}, "layout");
|
|
2005
|
+
fields(data, ["modules", "presentation", "comments", "groups", "style", "page"], "layout");
|
|
2006
|
+
const modules = positions(data.modules, "layout.modules");
|
|
2007
|
+
const presentation = data.presentation === void 0 ? void 0 : positions(data.presentation, "layout.presentation");
|
|
2008
|
+
const commentIds = /* @__PURE__ */ new Set();
|
|
2009
|
+
const comments = data.comments === void 0 ? void 0 : list(data.comments, "layout.comments").map((entry, index) => {
|
|
2010
|
+
const path = `layout.comments[${index}]`, item = object(entry, path);
|
|
2011
|
+
fields(item, ["id", "contextId", "text", "x", "y"], path);
|
|
2012
|
+
const id = text2(item.id, `${path}.id`);
|
|
2013
|
+
if (commentIds.has(id)) throw new TypeError(`Duplicate layout comment ${id}.`);
|
|
2014
|
+
commentIds.add(id);
|
|
2015
|
+
if (typeof item.text !== "string" || item.text.length > 4e3) throw new TypeError(`${path}.text must be a string of at most 4000 characters.`);
|
|
2016
|
+
return { id, contextId: text2(item.contextId, `${path}.contextId`), text: item.text, x: number(item.x, `${path}.x`, -1e7, 1e7), y: number(item.y, `${path}.y`, -1e7, 1e7) };
|
|
2017
|
+
});
|
|
2018
|
+
const groupIds = /* @__PURE__ */ new Set();
|
|
2019
|
+
const grouped = /* @__PURE__ */ new Set();
|
|
2020
|
+
const groups = data.groups === void 0 ? void 0 : list(data.groups, "layout.groups").map((entry, index) => {
|
|
2021
|
+
const path = `layout.groups[${index}]`, item = object(entry, path);
|
|
2022
|
+
fields(item, ["id", "moduleIds"], path);
|
|
2023
|
+
const id = text2(item.id, `${path}.id`);
|
|
2024
|
+
if (groupIds.has(id)) throw new TypeError(`Duplicate layout group ${id}.`);
|
|
2025
|
+
groupIds.add(id);
|
|
2026
|
+
const moduleIds = list(item.moduleIds, `${path}.moduleIds`).map((moduleId, member) => text2(moduleId, `${path}.moduleIds[${member}]`));
|
|
2027
|
+
if (moduleIds.length < 2) throw new TypeError(`${path} must group at least two modules.`);
|
|
2028
|
+
for (const moduleId of moduleIds) {
|
|
2029
|
+
if (grouped.has(moduleId)) throw new TypeError(`Module ${moduleId} belongs to more than one group.`);
|
|
2030
|
+
grouped.add(moduleId);
|
|
2031
|
+
}
|
|
2032
|
+
return { id, moduleIds };
|
|
2033
|
+
});
|
|
2034
|
+
const style = data.style === void 0 ? void 0 : customProperties(data.style, "layout.style");
|
|
2035
|
+
const page = data.page === void 0 ? void 0 : object(data.page, "layout.page");
|
|
2036
|
+
return {
|
|
2037
|
+
modules,
|
|
2038
|
+
...presentation ? { presentation } : {},
|
|
2039
|
+
...comments ? { comments } : {},
|
|
2040
|
+
...groups ? { groups } : {},
|
|
2041
|
+
...style && Object.keys(style).length ? { style } : {},
|
|
2042
|
+
...page ? { page } : {}
|
|
2043
|
+
};
|
|
2044
|
+
}
|
|
2045
|
+
var CUSTOM_PROPERTY = /^--[a-z0-9][a-z0-9-]{0,126}$/;
|
|
2046
|
+
function customProperties(value, path) {
|
|
2047
|
+
const entries = Object.entries(object(value, path));
|
|
2048
|
+
if (entries.length > 256) throw new RangeError(`${path} holds at most 256 properties.`);
|
|
2049
|
+
const out = {};
|
|
2050
|
+
for (const [name, item] of entries) {
|
|
2051
|
+
if (!CUSTOM_PROPERTY.test(name)) throw new TypeError(`${path} names must be custom properties: two hyphens, then lowercase letters, digits and hyphens.`);
|
|
2052
|
+
if (typeof item !== "string" || !item.trim() || item.length > 256) throw new TypeError(`${path}.${name} must be a nonempty string of at most 256 characters.`);
|
|
2053
|
+
out[name] = item;
|
|
2054
|
+
}
|
|
2055
|
+
return out;
|
|
2056
|
+
}
|
|
2057
|
+
function mappings(value) {
|
|
2058
|
+
if (value === void 0) return void 0;
|
|
2059
|
+
const ids = /* @__PURE__ */ new Set();
|
|
2060
|
+
return list(value, "mappings").map((entry, index) => {
|
|
2061
|
+
const path = `mappings[${index}]`, item = object(entry, path);
|
|
2062
|
+
fields(item, ["id", "source", "target", "min", "max"], path);
|
|
2063
|
+
const id = text2(item.id, `${path}.id`);
|
|
2064
|
+
if (ids.has(id)) throw new TypeError(`Duplicate mapping ${id}.`);
|
|
2065
|
+
ids.add(id);
|
|
2066
|
+
const sourceData = object(item.source, `${path}.source`);
|
|
2067
|
+
const kind = enumeration(sourceData.kind, ["cc", "pitchbend", "program", "pressure", "key"], `${path}.source.kind`);
|
|
2068
|
+
const channel = () => sourceData.channel === void 0 ? {} : { channel: number(sourceData.channel, `${path}.source.channel`, 0, 15) };
|
|
2069
|
+
let source;
|
|
2070
|
+
if (kind === "cc") {
|
|
2071
|
+
fields(sourceData, ["kind", "controller", "channel"], `${path}.source`);
|
|
2072
|
+
source = { kind, controller: number(sourceData.controller, `${path}.source.controller`, 0, 127), ...channel() };
|
|
2073
|
+
} else if (kind === "key") {
|
|
2074
|
+
fields(sourceData, ["kind", "key"], `${path}.source`);
|
|
2075
|
+
source = { kind, key: text2(sourceData.key, `${path}.source.key`) };
|
|
2076
|
+
} else {
|
|
2077
|
+
fields(sourceData, ["kind", "channel"], `${path}.source`);
|
|
2078
|
+
source = { kind, ...channel() };
|
|
2079
|
+
}
|
|
2080
|
+
return {
|
|
2081
|
+
id,
|
|
2082
|
+
source,
|
|
2083
|
+
target: parameter(item.target, `${path}.target`),
|
|
2084
|
+
...item.min !== void 0 ? { min: number(item.min, `${path}.min`) } : {},
|
|
2085
|
+
...item.max !== void 0 ? { max: number(item.max, `${path}.max`) } : {}
|
|
2086
|
+
};
|
|
2087
|
+
});
|
|
2088
|
+
}
|
|
2089
|
+
function positions(value, name) {
|
|
2090
|
+
const seen = /* @__PURE__ */ new Set();
|
|
2091
|
+
return list(value, name).map((entry, index) => {
|
|
2092
|
+
const path = `${name}[${index}]`, item = object(entry, path);
|
|
2093
|
+
fields(item, ["moduleId", "x", "y", "collapsed", "width", "height"], path);
|
|
2094
|
+
const moduleId = text2(item.moduleId, `${path}.moduleId`);
|
|
2095
|
+
if (seen.has(moduleId)) throw new TypeError(`Duplicate layout module ${moduleId}.`);
|
|
2096
|
+
seen.add(moduleId);
|
|
2097
|
+
if (item.collapsed !== void 0 && typeof item.collapsed !== "boolean") throw new TypeError(`${path}.collapsed must be boolean.`);
|
|
2098
|
+
return {
|
|
2099
|
+
moduleId,
|
|
2100
|
+
x: number(item.x, `${path}.x`, -1e7, 1e7),
|
|
2101
|
+
y: number(item.y, `${path}.y`, -1e7, 1e7),
|
|
2102
|
+
...item.collapsed !== void 0 ? { collapsed: item.collapsed } : {},
|
|
2103
|
+
...item.width !== void 0 ? { width: number(item.width, `${path}.width`, 1, 1e5) } : {},
|
|
2104
|
+
...item.height !== void 0 ? { height: number(item.height, `${path}.height`, 1, 1e5) } : {}
|
|
2105
|
+
};
|
|
2106
|
+
});
|
|
2107
|
+
}
|
|
2108
|
+
function session(value) {
|
|
2109
|
+
const data = object(value ?? {}, "session");
|
|
2110
|
+
fields(data, ["positionSeconds", "rate", "loop", "tempo"], "session");
|
|
2111
|
+
let tempo;
|
|
2112
|
+
if (data.tempo !== void 0) {
|
|
2113
|
+
const value2 = object(data.tempo, "session.tempo");
|
|
2114
|
+
fields(value2, ["bpm", "beatsPerBar", "beatUnit"], "session.tempo");
|
|
2115
|
+
const beatUnit = number(value2.beatUnit ?? 4, "session.tempo.beatUnit", 1, 32);
|
|
2116
|
+
if (![1, 2, 4, 8, 16, 32].includes(beatUnit)) throw new TypeError("session.tempo.beatUnit must be 1, 2, 4, 8, 16 or 32.");
|
|
2117
|
+
const beatsPerBar = number(value2.beatsPerBar ?? 4, "session.tempo.beatsPerBar", 1, 64);
|
|
2118
|
+
if (!Number.isInteger(beatsPerBar)) throw new TypeError("session.tempo.beatsPerBar must be an integer.");
|
|
2119
|
+
tempo = { bpm: number(value2.bpm, "session.tempo.bpm", Number.MIN_VALUE, 1e3), beatsPerBar, beatUnit };
|
|
2120
|
+
}
|
|
2121
|
+
let loop;
|
|
2122
|
+
if (data.loop === null) loop = null;
|
|
2123
|
+
else if (data.loop !== void 0) {
|
|
2124
|
+
const region = object(data.loop, "session.loop");
|
|
2125
|
+
fields(region, ["startSeconds", "endSeconds"], "session.loop");
|
|
2126
|
+
const startSeconds = number(region.startSeconds, "session.loop.startSeconds", 0);
|
|
2127
|
+
const endSeconds = number(region.endSeconds, "session.loop.endSeconds", 0);
|
|
2128
|
+
if (endSeconds <= startSeconds) throw new TypeError("Session loop end must follow start.");
|
|
2129
|
+
loop = { startSeconds, endSeconds };
|
|
2130
|
+
}
|
|
2131
|
+
return {
|
|
2132
|
+
positionSeconds: number(data.positionSeconds ?? 0, "session.positionSeconds", 0),
|
|
2133
|
+
rate: number(data.rate ?? 1, "session.rate", Number.MIN_VALUE),
|
|
2134
|
+
...loop !== void 0 ? { loop } : {},
|
|
2135
|
+
...tempo ? { tempo } : {}
|
|
2136
|
+
};
|
|
2137
|
+
}
|
|
2138
|
+
function parseModuleProject(input) {
|
|
2139
|
+
if (typeof input === "string") {
|
|
2140
|
+
if (input.length > MAX_BYTES) throw new RangeError("Project JSON text exceeds size limit.");
|
|
2141
|
+
input = JSON.parse(input);
|
|
2142
|
+
}
|
|
2143
|
+
const data = object(copyProjectJson(input), "project");
|
|
2144
|
+
fields(data, ["schema", "version", "id", "contexts", "modules", "assets", "connections", "modulations", "macros", "automation", "session", "layout", "mappings"], "project");
|
|
2145
|
+
if (data.schema !== "webmusic.module-project" || data.version !== 1) throw new TypeError("Unsupported module project schema/version.");
|
|
2146
|
+
const contexts = list(data.contexts, "contexts").map((entry, index) => {
|
|
2147
|
+
const path = `contexts[${index}]`, item = object(entry, path);
|
|
2148
|
+
fields(item, ["id", "parentId", "ownerModuleId"], path);
|
|
2149
|
+
const parentId = optionalText(item, "parentId", path), ownerModuleId = optionalText(item, "ownerModuleId", path);
|
|
2150
|
+
return { id: text2(item.id, `${path}.id`), ...parentId ? { parentId } : {}, ...ownerModuleId ? { ownerModuleId } : {} };
|
|
2151
|
+
});
|
|
2152
|
+
const assets = list(data.assets, "assets").map((entry, index) => {
|
|
2153
|
+
const path = `assets[${index}]`, item = object(entry, path);
|
|
2154
|
+
fields(item, ["id", "uri", "mediaType", "integrity"], path);
|
|
2155
|
+
const mediaType = optionalText(item, "mediaType", path), integrity = optionalText(item, "integrity", path);
|
|
2156
|
+
return { id: text2(item.id, `${path}.id`), uri: text2(item.uri, `${path}.uri`, 8192), ...mediaType ? { mediaType } : {}, ...integrity ? { integrity } : {} };
|
|
2157
|
+
});
|
|
2158
|
+
const modules = list(data.modules, "modules").map((entry, index) => {
|
|
2159
|
+
const path = `modules[${index}]`, item = object(entry, path);
|
|
2160
|
+
fields(item, ["id", "plugin", "contextId", "label", "config", "assets", "params"], path);
|
|
2161
|
+
const plugin = object(item.plugin, `${path}.plugin`);
|
|
2162
|
+
fields(plugin, ["id", "version"], `${path}.plugin`);
|
|
2163
|
+
const label = optionalText(item, "label", path);
|
|
2164
|
+
const config = object(item.config ?? {}, `${path}.config`);
|
|
2165
|
+
let roles, params;
|
|
2166
|
+
if (item.assets !== void 0) {
|
|
2167
|
+
roles = {};
|
|
2168
|
+
for (const [role, value] of Object.entries(object(item.assets, `${path}.assets`))) roles[text2(role, `${path}.asset role`)] = text2(value, `${path}.assets.${role}`);
|
|
2169
|
+
}
|
|
2170
|
+
if (item.params !== void 0) {
|
|
2171
|
+
params = {};
|
|
2172
|
+
for (const [id, value] of Object.entries(object(item.params, `${path}.params`))) params[text2(id, `${path}.parameter ID`)] = number(value, `${path}.params.${id}`);
|
|
2173
|
+
}
|
|
2174
|
+
return {
|
|
2175
|
+
id: text2(item.id, `${path}.id`),
|
|
2176
|
+
contextId: text2(item.contextId, `${path}.contextId`),
|
|
2177
|
+
plugin: { id: text2(plugin.id, `${path}.plugin.id`), version: text2(plugin.version, `${path}.plugin.version`) },
|
|
2178
|
+
config,
|
|
2179
|
+
...label ? { label } : {},
|
|
2180
|
+
...roles ? { assets: roles } : {},
|
|
2181
|
+
...params ? { params } : {}
|
|
2182
|
+
};
|
|
2183
|
+
});
|
|
2184
|
+
const connections = list(data.connections, "connections").map((entry, index) => {
|
|
2185
|
+
const path = `connections[${index}]`, item = object(entry, path);
|
|
2186
|
+
fields(item, ["id", "kind", "from", "to", "mode"], path);
|
|
2187
|
+
const kind = enumeration(item.kind, ["audio", "control", "note", "transport", "source", "message"], `${path}.kind`);
|
|
2188
|
+
const mode = item.mode === void 0 ? void 0 : enumeration(item.mode, ["replace", "send", "tap"], `${path}.mode`);
|
|
2189
|
+
if (kind !== "audio" && mode !== void 0) throw new TypeError("Only audio connections have routing modes.");
|
|
2190
|
+
return { id: text2(item.id, `${path}.id`), kind, from: endpoint(item.from, `${path}.from`), to: endpoint(item.to, `${path}.to`), ...mode ? { mode } : {} };
|
|
2191
|
+
});
|
|
2192
|
+
const modulations = list(data.modulations, "modulations").map((entry, index) => {
|
|
2193
|
+
const path = `modulations[${index}]`, item = object(entry, path);
|
|
2194
|
+
fields(item, ["id", "source", "target", "depth", "polarity"], path);
|
|
2195
|
+
const polarity = item.polarity === void 0 ? void 0 : enumeration(item.polarity, ["bipolar", "unipolar"], `${path}.polarity`);
|
|
2196
|
+
return { id: text2(item.id, `${path}.id`), source: endpoint(item.source, `${path}.source`), target: parameter(item.target, `${path}.target`), depth: number(item.depth, `${path}.depth`), ...polarity ? { polarity } : {} };
|
|
2197
|
+
});
|
|
2198
|
+
const macros = list(data.macros, "macros").map((entry, index) => {
|
|
2199
|
+
const path = `macros[${index}]`, item = object(entry, path);
|
|
2200
|
+
fields(item, ["id", "label", "value", "targets"], path);
|
|
2201
|
+
const label = optionalText(item, "label", path);
|
|
2202
|
+
const targets = list(item.targets, `${path}.targets`).map((target, targetIndex) => {
|
|
2203
|
+
const place = `${path}.targets[${targetIndex}]`, targetData = object(target, place);
|
|
2204
|
+
fields(targetData, ["moduleId", "param", "min", "max"], place);
|
|
2205
|
+
return { ...parameter({ moduleId: targetData.moduleId, param: targetData.param }, place), min: number(targetData.min, `${place}.min`), max: number(targetData.max, `${place}.max`) };
|
|
2206
|
+
});
|
|
2207
|
+
return { id: text2(item.id, `${path}.id`), value: number(item.value, `${path}.value`, 0, 1), targets, ...label ? { label } : {} };
|
|
2208
|
+
});
|
|
2209
|
+
const automation = list(data.automation, "automation").map((entry, index) => {
|
|
2210
|
+
const path = `automation[${index}]`, item = object(entry, path);
|
|
2211
|
+
fields(item, ["id", "target", "points", "loop", "enabled", "initialValue"], path);
|
|
2212
|
+
if (item.enabled !== void 0 && typeof item.enabled !== "boolean") throw new TypeError(`${path}.enabled must be boolean.`);
|
|
2213
|
+
let previous = -Infinity;
|
|
2214
|
+
const points = list(item.points, `${path}.points`).map((point, pointIndex) => {
|
|
2215
|
+
const place = `${path}.points[${pointIndex}]`, event = object(point, place);
|
|
2216
|
+
fields(event, ["type", "value", "timeSeconds"], place);
|
|
2217
|
+
const type = enumeration(event.type, ["set", "linear", "exponential"], `${place}.type`);
|
|
2218
|
+
const timeSeconds = number(event.timeSeconds, `${place}.timeSeconds`, 0), value = number(event.value, `${place}.value`);
|
|
2219
|
+
if (timeSeconds < previous) throw new TypeError("Automation points must be ordered by time.");
|
|
2220
|
+
if (type === "exponential" && value <= 0) throw new TypeError("Exponential automation requires positive values.");
|
|
2221
|
+
previous = timeSeconds;
|
|
2222
|
+
return { type, value, timeSeconds };
|
|
2223
|
+
});
|
|
2224
|
+
const loop = item.loop === void 0 ? void 0 : enumeration(item.loop, ["repeat", "once"], `${path}.loop`);
|
|
2225
|
+
return { id: text2(item.id, `${path}.id`), target: parameter(item.target, `${path}.target`), points, ...loop ? { loop } : {}, ...item.enabled !== void 0 ? { enabled: item.enabled } : {}, ...item.initialValue !== void 0 ? { initialValue: number(item.initialValue, `${path}.initialValue`) } : {} };
|
|
2226
|
+
});
|
|
2227
|
+
const project = { schema: "webmusic.module-project", version: 1, id: text2(data.id, "project.id"), contexts, modules, assets, connections, modulations, macros, automation, session: session(data.session), layout: layout(data.layout) };
|
|
2228
|
+
const mapped = mappings(data.mappings);
|
|
2229
|
+
if (mapped) project.mappings = mapped;
|
|
2230
|
+
validateReferences(project);
|
|
2231
|
+
return copyProjectJson(project);
|
|
2232
|
+
}
|
|
2233
|
+
function validateReferences(project) {
|
|
2234
|
+
const contexts = unique(project.contexts, "contexts"), modules = unique(project.modules, "modules"), assets = unique(project.assets, "assets");
|
|
2235
|
+
for (const [name, entries] of [["connections", project.connections], ["modulations", project.modulations], ["macros", project.macros], ["automation", project.automation]]) unique(entries, name);
|
|
2236
|
+
if (project.contexts.filter((context) => !context.parentId).length !== 1) throw new TypeError("A project requires exactly one root context.");
|
|
2237
|
+
for (const context of project.contexts) {
|
|
2238
|
+
const visited = /* @__PURE__ */ new Set();
|
|
2239
|
+
let current = context;
|
|
2240
|
+
while (current) {
|
|
2241
|
+
if (visited.has(current.id)) throw new TypeError("Project context topology contains a cycle.");
|
|
2242
|
+
visited.add(current.id);
|
|
2243
|
+
if (visited.size > 128) throw new RangeError("Project context nesting exceeds 128 scopes.");
|
|
2244
|
+
if (current.parentId && !contexts.has(current.parentId)) throw new TypeError(`Missing parent context ${current.parentId}.`);
|
|
2245
|
+
current = current.parentId ? contexts.get(current.parentId) : void 0;
|
|
2246
|
+
}
|
|
2247
|
+
if (context.ownerModuleId) {
|
|
2248
|
+
const owner = modules.get(context.ownerModuleId);
|
|
2249
|
+
if (!context.parentId || !owner || owner.contextId !== context.parentId) throw new TypeError("A context owner must belong to its direct parent context.");
|
|
2250
|
+
}
|
|
2251
|
+
}
|
|
2252
|
+
const requireModule = (id) => {
|
|
2253
|
+
if (!modules.has(id)) throw new TypeError(`Project references missing module ${id}.`);
|
|
2254
|
+
};
|
|
2255
|
+
for (const module of project.modules) {
|
|
2256
|
+
if (!contexts.has(module.contextId)) throw new TypeError(`Module ${module.id} references missing context ${module.contextId}.`);
|
|
2257
|
+
for (const id of Object.values(module.assets ?? {})) if (!assets.has(id)) throw new TypeError(`Module ${module.id} references missing asset ${id}.`);
|
|
2258
|
+
}
|
|
2259
|
+
const routes = /* @__PURE__ */ new Map();
|
|
2260
|
+
const replacements = /* @__PURE__ */ new Set();
|
|
2261
|
+
for (const connection of project.connections) {
|
|
2262
|
+
requireModule(connection.from.moduleId);
|
|
2263
|
+
requireModule(connection.to.moduleId);
|
|
2264
|
+
if (connection.kind === "audio") {
|
|
2265
|
+
const next = routes.get(connection.from.moduleId) ?? /* @__PURE__ */ new Set();
|
|
2266
|
+
next.add(connection.to.moduleId);
|
|
2267
|
+
routes.set(connection.from.moduleId, next);
|
|
2268
|
+
if ((connection.mode ?? "replace") === "replace") {
|
|
2269
|
+
const key = JSON.stringify(connection.from);
|
|
2270
|
+
if (replacements.has(key)) throw new TypeError("An audio output can have only one replacement connection.");
|
|
2271
|
+
replacements.add(key);
|
|
2272
|
+
}
|
|
2273
|
+
}
|
|
2274
|
+
}
|
|
2275
|
+
const degrees = /* @__PURE__ */ new Map();
|
|
2276
|
+
for (const [source, targets] of routes) {
|
|
2277
|
+
if (!degrees.has(source)) degrees.set(source, 0);
|
|
2278
|
+
for (const target of targets) degrees.set(target, (degrees.get(target) ?? 0) + 1);
|
|
2279
|
+
}
|
|
2280
|
+
const ready = [...degrees.keys()].filter((id) => degrees.get(id) === 0);
|
|
2281
|
+
let consumed = 0;
|
|
2282
|
+
for (let index = 0; index < ready.length; index++) {
|
|
2283
|
+
const id = ready[index];
|
|
2284
|
+
consumed++;
|
|
2285
|
+
for (const target of routes.get(id) ?? []) {
|
|
2286
|
+
const remaining = degrees.get(target) - 1;
|
|
2287
|
+
degrees.set(target, remaining);
|
|
2288
|
+
if (remaining === 0) ready.push(target);
|
|
2289
|
+
}
|
|
2290
|
+
}
|
|
2291
|
+
if (consumed !== degrees.size) throw new TypeError("Project audio connections contain a feedback cycle.");
|
|
2292
|
+
for (const modulation of project.modulations) {
|
|
2293
|
+
requireModule(modulation.source.moduleId);
|
|
2294
|
+
requireModule(modulation.target.moduleId);
|
|
2295
|
+
}
|
|
2296
|
+
for (const macro of project.macros) {
|
|
2297
|
+
const targets = /* @__PURE__ */ new Set();
|
|
2298
|
+
for (const target of macro.targets) {
|
|
2299
|
+
requireModule(target.moduleId);
|
|
2300
|
+
const key = JSON.stringify([target.moduleId, target.param]);
|
|
2301
|
+
if (targets.has(key)) throw new TypeError("A macro cannot map one target twice.");
|
|
2302
|
+
targets.add(key);
|
|
2303
|
+
}
|
|
2304
|
+
}
|
|
2305
|
+
const automated = /* @__PURE__ */ new Set();
|
|
2306
|
+
for (const automation of project.automation) {
|
|
2307
|
+
requireModule(automation.target.moduleId);
|
|
2308
|
+
const key = JSON.stringify(automation.target);
|
|
2309
|
+
if (automated.has(key)) throw new TypeError("A parameter can have only one project automation track.");
|
|
2310
|
+
automated.add(key);
|
|
2311
|
+
}
|
|
2312
|
+
for (const position of project.layout.modules) requireModule(position.moduleId);
|
|
2313
|
+
for (const position of project.layout.presentation ?? []) requireModule(position.moduleId);
|
|
2314
|
+
for (const comment of project.layout.comments ?? []) if (!contexts.has(comment.contextId)) throw new TypeError(`Comment ${comment.id} references missing context ${comment.contextId}.`);
|
|
2315
|
+
for (const group of project.layout.groups ?? []) for (const moduleId of group.moduleIds) requireModule(moduleId);
|
|
2316
|
+
for (const mapping of project.mappings ?? []) requireModule(mapping.target.moduleId);
|
|
2317
|
+
}
|
|
2318
|
+
function serializeModuleProject(project, space) {
|
|
2319
|
+
const text3 = JSON.stringify(parseModuleProject(project), null, space);
|
|
2320
|
+
if (text3.length > MAX_BYTES) throw new RangeError("Project JSON text exceeds size limit.");
|
|
2321
|
+
return text3;
|
|
2322
|
+
}
|
|
2323
|
+
|
|
2324
|
+
// src/module/project-registry.ts
|
|
2325
|
+
var _entries;
|
|
2326
|
+
var ProjectPluginRegistry = class {
|
|
2327
|
+
constructor() {
|
|
2328
|
+
__privateAdd(this, _entries, /* @__PURE__ */ new Map());
|
|
2329
|
+
}
|
|
2330
|
+
get(id, version) {
|
|
2331
|
+
return __privateGet(this, _entries).get(id)?.get(version);
|
|
2332
|
+
}
|
|
2333
|
+
register(factory) {
|
|
2334
|
+
const id = factory.id, version = factory.version;
|
|
2335
|
+
for (const value of [id, version]) if (typeof value !== "string" || !value || value !== value.trim()) throw new TypeError("Plugin ID and version must be nonempty trimmed strings.");
|
|
2336
|
+
if (typeof factory.create !== "function") throw new TypeError("Plugin factory requires create().");
|
|
2337
|
+
const versions = __privateGet(this, _entries).get(id) ?? /* @__PURE__ */ new Map();
|
|
2338
|
+
if (versions.has(version)) throw new Error(`Plugin already registered: ${id}@${version}.`);
|
|
2339
|
+
const entry = Object.freeze({ id, version, create: factory.create.bind(factory) });
|
|
2340
|
+
versions.set(version, entry);
|
|
2341
|
+
__privateGet(this, _entries).set(id, versions);
|
|
2342
|
+
return createConnectionLease(() => {
|
|
2343
|
+
if (versions.get(version) !== entry) return;
|
|
2344
|
+
versions.delete(version);
|
|
2345
|
+
if (versions.size === 0) __privateGet(this, _entries).delete(id);
|
|
2346
|
+
});
|
|
2347
|
+
}
|
|
2348
|
+
};
|
|
2349
|
+
_entries = new WeakMap();
|
|
2350
|
+
|
|
2351
|
+
// src/module/project-runtime.ts
|
|
2352
|
+
function tapped(untap, connect) {
|
|
2353
|
+
let lease;
|
|
2354
|
+
try {
|
|
2355
|
+
lease = connect();
|
|
2356
|
+
} catch (error) {
|
|
2357
|
+
untap();
|
|
2358
|
+
throw error;
|
|
2359
|
+
}
|
|
2360
|
+
return createConnectionLease(() => {
|
|
2361
|
+
try {
|
|
2362
|
+
lease.release();
|
|
2363
|
+
} finally {
|
|
2364
|
+
untap();
|
|
2365
|
+
}
|
|
2366
|
+
});
|
|
2367
|
+
}
|
|
2368
|
+
function aborted() {
|
|
2369
|
+
const error = new Error("Project restoration was cancelled.");
|
|
2370
|
+
error.name = "AbortError";
|
|
2371
|
+
return error;
|
|
2372
|
+
}
|
|
2373
|
+
function assertSignal(signal) {
|
|
2374
|
+
if (signal.aborted) throw aborted();
|
|
2375
|
+
}
|
|
2376
|
+
async function abortable(value, signal, retireLate) {
|
|
2377
|
+
if (signal.aborted) {
|
|
2378
|
+
void Promise.resolve(value).then((result) => retireLate?.(result), () => {
|
|
2379
|
+
});
|
|
2380
|
+
throw aborted();
|
|
2381
|
+
}
|
|
2382
|
+
let finished = false;
|
|
2383
|
+
let acquired;
|
|
2384
|
+
let retired = false;
|
|
2385
|
+
const retire = (result) => {
|
|
2386
|
+
if (!retired) {
|
|
2387
|
+
retired = true;
|
|
2388
|
+
retireLate?.(result);
|
|
2389
|
+
}
|
|
2390
|
+
};
|
|
2391
|
+
let rejectAbort;
|
|
2392
|
+
const cancellation = new Promise((_resolve, reject) => {
|
|
2393
|
+
rejectAbort = reject;
|
|
2394
|
+
});
|
|
2395
|
+
const onAbort = () => rejectAbort(aborted());
|
|
2396
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
2397
|
+
const task = Promise.resolve(value);
|
|
2398
|
+
void task.then((result) => {
|
|
2399
|
+
acquired = { value: result };
|
|
2400
|
+
if (signal.aborted && !finished) retire(result);
|
|
2401
|
+
}, () => {
|
|
2402
|
+
});
|
|
2403
|
+
try {
|
|
2404
|
+
const result = await Promise.race([task, cancellation]);
|
|
2405
|
+
assertSignal(signal);
|
|
2406
|
+
finished = true;
|
|
2407
|
+
return result;
|
|
2408
|
+
} catch (error) {
|
|
2409
|
+
if (!finished && acquired) retire(acquired.value);
|
|
2410
|
+
throw error;
|
|
2411
|
+
} finally {
|
|
2412
|
+
signal.removeEventListener("abort", onAbort);
|
|
2413
|
+
}
|
|
2414
|
+
}
|
|
2415
|
+
var _options2, _listeners4, _ownedInstances, _graph, _pending, _generation, _revision, _disposed3, _editing, _editObserved, _preparedEdit, _committing, _observers, _sequence, _signalProbes, _stopPreload, _ModuleProjectRuntime_instances, wireProbe_fn, tap_fn, captureSnapshot_fn, exportGraph_fn, graphTempo_fn, prepareEdit_fn, assertEdit_fn, commitEdit_fn, finishEdit_fn, build_fn, createModule_fn, discardModule_fn, connect_fn, connectModulation_fn, validateAudioTopology_fn, parameter_fn, command_fn, activate_fn, disposeGraph_fn, disposeInstance_fn, retire_fn, cleanup_fn, report_fn, assertActive_fn, requireGraph_fn, requireEditable_fn, assertPending_fn, uncapturedParameterState_fn, observe_fn, changed_fn, notify_fn2, call_fn;
|
|
2416
|
+
var ModuleProjectRuntime = class {
|
|
2417
|
+
constructor(options) {
|
|
2418
|
+
__privateAdd(this, _ModuleProjectRuntime_instances);
|
|
2419
|
+
__privateAdd(this, _options2);
|
|
2420
|
+
__privateAdd(this, _listeners4, /* @__PURE__ */ new Set());
|
|
2421
|
+
__privateAdd(this, _ownedInstances, /* @__PURE__ */ new WeakSet());
|
|
2422
|
+
__privateAdd(this, _graph);
|
|
2423
|
+
__privateAdd(this, _pending);
|
|
2424
|
+
__privateAdd(this, _generation, 0);
|
|
2425
|
+
__privateAdd(this, _revision, 0);
|
|
2426
|
+
__privateAdd(this, _disposed3, false);
|
|
2427
|
+
__privateAdd(this, _editing, false);
|
|
2428
|
+
__privateAdd(this, _editObserved, false);
|
|
2429
|
+
__privateAdd(this, _preparedEdit);
|
|
2430
|
+
__privateAdd(this, _committing, false);
|
|
2431
|
+
__privateAdd(this, _observers, /* @__PURE__ */ new Set());
|
|
2432
|
+
__privateAdd(this, _sequence, 0);
|
|
2433
|
+
__privateAdd(this, _signalProbes, /* @__PURE__ */ new Set());
|
|
2434
|
+
__privateAdd(this, _stopPreload);
|
|
2435
|
+
__privateSet(this, _options2, options);
|
|
2436
|
+
const preload = options.preload;
|
|
2437
|
+
if (preload) {
|
|
2438
|
+
const given = typeof preload === "object" ? preload : {};
|
|
2439
|
+
__privateSet(this, _stopPreload, keepProjectSessionPreloaded(this, { ...given, onError: given.onError ?? options.onCleanupError }));
|
|
2440
|
+
}
|
|
2441
|
+
}
|
|
2442
|
+
/**
|
|
2443
|
+
* Observe every message and note as it is about to cross a cord of the
|
|
2444
|
+
* active graph (DEC-078). Delivery is unchanged: listeners run first, in
|
|
2445
|
+
* delivery order, and cannot stop or alter it; a throwing listener is
|
|
2446
|
+
* reported, not propagated. Returns an unsubscribe function.
|
|
2447
|
+
*/
|
|
2448
|
+
observe(listener) {
|
|
2449
|
+
__privateMethod(this, _ModuleProjectRuntime_instances, assertActive_fn).call(this);
|
|
2450
|
+
if (typeof listener !== "function") throw new TypeError("An observer must be a function.");
|
|
2451
|
+
__privateGet(this, _observers).add(listener);
|
|
2452
|
+
return () => {
|
|
2453
|
+
__privateGet(this, _observers).delete(listener);
|
|
2454
|
+
};
|
|
2455
|
+
}
|
|
2456
|
+
/**
|
|
2457
|
+
* Listen to an audio or control output with an analyser that follows the
|
|
2458
|
+
* endpoint across rebuilds; it never changes the output's routes. The probe
|
|
2459
|
+
* owns the analyser; release it to disconnect.
|
|
2460
|
+
*/
|
|
2461
|
+
probeSignal(endpoint2, options = {}) {
|
|
2462
|
+
__privateMethod(this, _ModuleProjectRuntime_instances, assertActive_fn).call(this);
|
|
2463
|
+
const analyser = __privateGet(this, _options2).audio.prepare().createAnalyser();
|
|
2464
|
+
analyser.fftSize = options.fftSize ?? 2048;
|
|
2465
|
+
const entry = { endpoint: { ...endpoint2 }, analyser };
|
|
2466
|
+
__privateGet(this, _signalProbes).add(entry);
|
|
2467
|
+
__privateMethod(this, _ModuleProjectRuntime_instances, wireProbe_fn).call(this, entry);
|
|
2468
|
+
let released = false;
|
|
2469
|
+
return Object.freeze({
|
|
2470
|
+
analyser,
|
|
2471
|
+
get connected() {
|
|
2472
|
+
return !released && entry.lease !== void 0;
|
|
2473
|
+
},
|
|
2474
|
+
release: () => {
|
|
2475
|
+
if (released) return;
|
|
2476
|
+
released = true;
|
|
2477
|
+
__privateGet(this, _signalProbes).delete(entry);
|
|
2478
|
+
if (entry.lease) __privateMethod(this, _ModuleProjectRuntime_instances, retire_fn).call(this, entry.lease);
|
|
2479
|
+
entry.lease = void 0;
|
|
2480
|
+
__privateMethod(this, _ModuleProjectRuntime_instances, cleanup_fn).call(this, () => analyser.disconnect());
|
|
2481
|
+
}
|
|
2482
|
+
});
|
|
2483
|
+
}
|
|
2484
|
+
get context() {
|
|
2485
|
+
return __privateGet(this, _graph)?.context;
|
|
2486
|
+
}
|
|
2487
|
+
get session() {
|
|
2488
|
+
return __privateGet(this, _graph)?.session;
|
|
2489
|
+
}
|
|
2490
|
+
get resources() {
|
|
2491
|
+
const graph = __privateGet(this, _graph);
|
|
2492
|
+
return graph && Object.freeze({ context: graph.context, session: graph.session, module: (id) => graph.handles.get(id) });
|
|
2493
|
+
}
|
|
2494
|
+
getModule(id) {
|
|
2495
|
+
return __privateGet(this, _graph)?.handles.get(id);
|
|
2496
|
+
}
|
|
2497
|
+
getMacro(id) {
|
|
2498
|
+
return __privateGet(this, _graph)?.macros.get(id);
|
|
2499
|
+
}
|
|
2500
|
+
snapshot() {
|
|
2501
|
+
const graph = __privateGet(this, _graph);
|
|
2502
|
+
return __privateMethod(this, _ModuleProjectRuntime_instances, captureSnapshot_fn).call(this, graph, graph ? __privateMethod(this, _ModuleProjectRuntime_instances, exportGraph_fn).call(this, graph) : void 0);
|
|
2503
|
+
}
|
|
2504
|
+
exportProject() {
|
|
2505
|
+
return __privateMethod(this, _ModuleProjectRuntime_instances, exportGraph_fn).call(this, __privateMethod(this, _ModuleProjectRuntime_instances, requireGraph_fn).call(this));
|
|
2506
|
+
}
|
|
2507
|
+
/** Build/prepare a private candidate. A failed factory, asset, parameter or route leaves the active graph intact. */
|
|
2508
|
+
async restore(input, options = {}) {
|
|
2509
|
+
__privateMethod(this, _ModuleProjectRuntime_instances, assertActive_fn).call(this);
|
|
2510
|
+
if (__privateGet(this, _committing)) throw new Error("Project restoration cannot re-enter activation.");
|
|
2511
|
+
const project = parseModuleProject(input);
|
|
2512
|
+
if (options.signal?.aborted) return { status: "cancelled", snapshot: this.snapshot() };
|
|
2513
|
+
const previousPending = __privateGet(this, _pending);
|
|
2514
|
+
const pending = { controller: new AbortController(), generation: ++__privateWrapper(this, _generation)._ };
|
|
2515
|
+
__privateSet(this, _pending, pending);
|
|
2516
|
+
previousPending?.controller.abort();
|
|
2517
|
+
const cancel = () => {
|
|
2518
|
+
if (__privateGet(this, _pending) === pending) this.cancelRestore();
|
|
2519
|
+
};
|
|
2520
|
+
options.signal?.addEventListener("abort", cancel, { once: true });
|
|
2521
|
+
__privateMethod(this, _ModuleProjectRuntime_instances, changed_fn).call(this);
|
|
2522
|
+
let candidate;
|
|
2523
|
+
try {
|
|
2524
|
+
__privateMethod(this, _ModuleProjectRuntime_instances, assertPending_fn).call(this, pending);
|
|
2525
|
+
candidate = await __privateMethod(this, _ModuleProjectRuntime_instances, build_fn).call(this, project, pending.controller);
|
|
2526
|
+
__privateMethod(this, _ModuleProjectRuntime_instances, assertPending_fn).call(this, pending);
|
|
2527
|
+
if (options.play) await __privateMethod(this, _ModuleProjectRuntime_instances, command_fn).call(this, candidate.session.play(), pending.controller.signal);
|
|
2528
|
+
__privateMethod(this, _ModuleProjectRuntime_instances, assertPending_fn).call(this, pending);
|
|
2529
|
+
const prepared = __privateMethod(this, _ModuleProjectRuntime_instances, captureSnapshot_fn).call(this, candidate, __privateMethod(this, _ModuleProjectRuntime_instances, exportGraph_fn).call(this, candidate));
|
|
2530
|
+
__privateMethod(this, _ModuleProjectRuntime_instances, assertPending_fn).call(this, pending);
|
|
2531
|
+
const snapshot = __privateMethod(this, _ModuleProjectRuntime_instances, activate_fn).call(this, candidate, pending, prepared);
|
|
2532
|
+
candidate = void 0;
|
|
2533
|
+
return { status: "committed", snapshot };
|
|
2534
|
+
} catch (error) {
|
|
2535
|
+
if (candidate) __privateMethod(this, _ModuleProjectRuntime_instances, disposeGraph_fn).call(this, candidate);
|
|
2536
|
+
if (__privateGet(this, _pending) !== pending || __privateGet(this, _disposed3)) return { status: "cancelled", snapshot: this.snapshot() };
|
|
2537
|
+
throw error;
|
|
2538
|
+
} finally {
|
|
2539
|
+
options.signal?.removeEventListener("abort", cancel);
|
|
2540
|
+
if (__privateGet(this, _pending) === pending) {
|
|
2541
|
+
__privateSet(this, _pending, void 0);
|
|
2542
|
+
__privateMethod(this, _ModuleProjectRuntime_instances, changed_fn).call(this);
|
|
2543
|
+
}
|
|
2544
|
+
}
|
|
2545
|
+
}
|
|
2546
|
+
retryMissing(options = {}) {
|
|
2547
|
+
return this.restore(this.exportProject(), options);
|
|
2548
|
+
}
|
|
2549
|
+
cancelRestore() {
|
|
2550
|
+
const pending = __privateGet(this, _pending);
|
|
2551
|
+
if (!pending) return;
|
|
2552
|
+
__privateSet(this, _pending, void 0);
|
|
2553
|
+
++__privateWrapper(this, _generation)._;
|
|
2554
|
+
pending.controller.abort();
|
|
2555
|
+
__privateMethod(this, _ModuleProjectRuntime_instances, changed_fn).call(this);
|
|
2556
|
+
}
|
|
2557
|
+
/** Presentation-only mutations preserve all instances, routes, voices and Session state. */
|
|
2558
|
+
updateLayout(layout2) {
|
|
2559
|
+
const graph = __privateMethod(this, _ModuleProjectRuntime_instances, requireEditable_fn).call(this);
|
|
2560
|
+
graph.project = parseModuleProject({ ...graph.project, layout: layout2 });
|
|
2561
|
+
__privateMethod(this, _ModuleProjectRuntime_instances, changed_fn).call(this);
|
|
2562
|
+
}
|
|
2563
|
+
/** Change the Session's tempo and meter (DEC-077) without rebuilding; timers that follow musical time adjust. */
|
|
2564
|
+
updateTempo(tempo) {
|
|
2565
|
+
const graph = __privateMethod(this, _ModuleProjectRuntime_instances, requireEditable_fn).call(this);
|
|
2566
|
+
const next = createMusicalTempo({ ...__privateMethod(this, _ModuleProjectRuntime_instances, graphTempo_fn).call(this, graph), ...tempo });
|
|
2567
|
+
graph.project = parseModuleProject({ ...graph.project, session: { ...graph.project.session, tempo: { ...next } } });
|
|
2568
|
+
graph.musicalListeners?.forEach((listener) => {
|
|
2569
|
+
try {
|
|
2570
|
+
listener();
|
|
2571
|
+
} catch (error) {
|
|
2572
|
+
__privateMethod(this, _ModuleProjectRuntime_instances, report_fn).call(this, error);
|
|
2573
|
+
}
|
|
2574
|
+
});
|
|
2575
|
+
__privateMethod(this, _ModuleProjectRuntime_instances, changed_fn).call(this);
|
|
2576
|
+
}
|
|
2577
|
+
/** Replace the control mappings (DEC-076); mappings are data and rebuild nothing. */
|
|
2578
|
+
updateMappings(mappings2) {
|
|
2579
|
+
const graph = __privateMethod(this, _ModuleProjectRuntime_instances, requireEditable_fn).call(this);
|
|
2580
|
+
graph.project = parseModuleProject({ ...graph.project, mappings: mappings2 });
|
|
2581
|
+
__privateMethod(this, _ModuleProjectRuntime_instances, changed_fn).call(this);
|
|
2582
|
+
}
|
|
2583
|
+
updatePresentation(options) {
|
|
2584
|
+
const graph = __privateMethod(this, _ModuleProjectRuntime_instances, requireEditable_fn).call(this);
|
|
2585
|
+
const records = new Map(graph.project.modules.map((record) => [record.id, record]));
|
|
2586
|
+
const modules = options.modules?.map((item) => {
|
|
2587
|
+
const record = records.get(item.id);
|
|
2588
|
+
if (!record) throw new Error(`Unknown presentation module ${item.id}.`);
|
|
2589
|
+
records.delete(item.id);
|
|
2590
|
+
const { label: _previousLabel, ...rest } = record;
|
|
2591
|
+
return { ...rest, ...item.label !== void 0 ? { label: item.label } : {} };
|
|
2592
|
+
});
|
|
2593
|
+
if (modules && records.size) throw new Error("Presentation updates must retain every module ID.");
|
|
2594
|
+
graph.project = parseModuleProject({ ...graph.project, ...modules ? { modules } : {}, ...options.layout ? { layout: options.layout } : {} });
|
|
2595
|
+
__privateMethod(this, _ModuleProjectRuntime_instances, changed_fn).call(this);
|
|
2596
|
+
}
|
|
2597
|
+
/** Add one explicit edge without recreating playing modules. The backend still validates context/routing. */
|
|
2598
|
+
async connect(connection) {
|
|
2599
|
+
const graph = __privateMethod(this, _ModuleProjectRuntime_instances, requireEditable_fn).call(this);
|
|
2600
|
+
const generation = __privateGet(this, _generation);
|
|
2601
|
+
__privateSet(this, _editing, true);
|
|
2602
|
+
let lease;
|
|
2603
|
+
let committed;
|
|
2604
|
+
try {
|
|
2605
|
+
const next = parseModuleProject({ ...graph.project, connections: [...graph.project.connections, connection] });
|
|
2606
|
+
__privateMethod(this, _ModuleProjectRuntime_instances, validateAudioTopology_fn).call(this, graph, next);
|
|
2607
|
+
__privateMethod(this, _ModuleProjectRuntime_instances, assertEdit_fn).call(this, graph, generation);
|
|
2608
|
+
const record = next.connections[next.connections.length - 1];
|
|
2609
|
+
const diagnostics = [...graph.diagnostics];
|
|
2610
|
+
lease = await __privateMethod(this, _ModuleProjectRuntime_instances, connect_fn).call(this, graph, record, diagnostics);
|
|
2611
|
+
__privateMethod(this, _ModuleProjectRuntime_instances, assertEdit_fn).call(this, graph, generation);
|
|
2612
|
+
const prepared = __privateMethod(this, _ModuleProjectRuntime_instances, prepareEdit_fn).call(this, graph, next, diagnostics);
|
|
2613
|
+
__privateMethod(this, _ModuleProjectRuntime_instances, assertEdit_fn).call(this, graph, generation);
|
|
2614
|
+
if (lease) graph.connectionLeases.set(record.id, lease);
|
|
2615
|
+
committed = __privateMethod(this, _ModuleProjectRuntime_instances, commitEdit_fn).call(this, graph, next, diagnostics, prepared);
|
|
2616
|
+
return committed;
|
|
2617
|
+
} catch (error) {
|
|
2618
|
+
__privateMethod(this, _ModuleProjectRuntime_instances, retire_fn).call(this, lease);
|
|
2619
|
+
throw error;
|
|
2620
|
+
} finally {
|
|
2621
|
+
__privateMethod(this, _ModuleProjectRuntime_instances, finishEdit_fn).call(this, committed);
|
|
2622
|
+
}
|
|
2623
|
+
}
|
|
2624
|
+
disconnect(id) {
|
|
2625
|
+
const graph = __privateMethod(this, _ModuleProjectRuntime_instances, requireEditable_fn).call(this);
|
|
2626
|
+
const generation = __privateGet(this, _generation);
|
|
2627
|
+
__privateSet(this, _editing, true);
|
|
2628
|
+
let committed;
|
|
2629
|
+
try {
|
|
2630
|
+
if (!graph.project.connections.some((connection) => connection.id === id)) throw new Error(`Unknown project connection ${id}.`);
|
|
2631
|
+
const next = parseModuleProject({ ...graph.project, connections: graph.project.connections.filter((connection) => connection.id !== id) });
|
|
2632
|
+
__privateMethod(this, _ModuleProjectRuntime_instances, validateAudioTopology_fn).call(this, graph, next);
|
|
2633
|
+
__privateMethod(this, _ModuleProjectRuntime_instances, assertEdit_fn).call(this, graph, generation);
|
|
2634
|
+
const diagnostics = graph.diagnostics.filter((item) => !(item.kind === "connection" && item.id === id));
|
|
2635
|
+
const prepared = __privateMethod(this, _ModuleProjectRuntime_instances, prepareEdit_fn).call(this, graph, next, diagnostics);
|
|
2636
|
+
__privateMethod(this, _ModuleProjectRuntime_instances, assertEdit_fn).call(this, graph, generation);
|
|
2637
|
+
const lease = graph.connectionLeases.get(id);
|
|
2638
|
+
graph.connectionLeases.delete(id);
|
|
2639
|
+
committed = __privateMethod(this, _ModuleProjectRuntime_instances, commitEdit_fn).call(this, graph, next, diagnostics, prepared);
|
|
2640
|
+
__privateMethod(this, _ModuleProjectRuntime_instances, retire_fn).call(this, lease);
|
|
2641
|
+
return committed;
|
|
2642
|
+
} finally {
|
|
2643
|
+
__privateMethod(this, _ModuleProjectRuntime_instances, finishEdit_fn).call(this, committed);
|
|
2644
|
+
}
|
|
2645
|
+
}
|
|
2646
|
+
async connectModulation(record) {
|
|
2647
|
+
const graph = __privateMethod(this, _ModuleProjectRuntime_instances, requireEditable_fn).call(this);
|
|
2648
|
+
const generation = __privateGet(this, _generation);
|
|
2649
|
+
__privateSet(this, _editing, true);
|
|
2650
|
+
let lease;
|
|
2651
|
+
let committed;
|
|
2652
|
+
try {
|
|
2653
|
+
const next = parseModuleProject({ ...graph.project, modulations: [...graph.project.modulations, record] });
|
|
2654
|
+
const modulation = next.modulations[next.modulations.length - 1];
|
|
2655
|
+
const diagnostics = [...graph.diagnostics];
|
|
2656
|
+
lease = __privateMethod(this, _ModuleProjectRuntime_instances, connectModulation_fn).call(this, graph, modulation, diagnostics);
|
|
2657
|
+
__privateMethod(this, _ModuleProjectRuntime_instances, assertEdit_fn).call(this, graph, generation);
|
|
2658
|
+
const mappings2 = new Map(graph.modulationLeases);
|
|
2659
|
+
if (lease) mappings2.set(modulation.id, lease);
|
|
2660
|
+
const prepared = __privateMethod(this, _ModuleProjectRuntime_instances, prepareEdit_fn).call(this, graph, next, diagnostics, mappings2);
|
|
2661
|
+
__privateMethod(this, _ModuleProjectRuntime_instances, assertEdit_fn).call(this, graph, generation);
|
|
2662
|
+
if (lease) graph.modulationLeases.set(modulation.id, lease);
|
|
2663
|
+
committed = __privateMethod(this, _ModuleProjectRuntime_instances, commitEdit_fn).call(this, graph, next, diagnostics, prepared);
|
|
2664
|
+
return committed;
|
|
2665
|
+
} catch (error) {
|
|
2666
|
+
__privateMethod(this, _ModuleProjectRuntime_instances, retire_fn).call(this, lease);
|
|
2667
|
+
throw error;
|
|
2668
|
+
} finally {
|
|
2669
|
+
__privateMethod(this, _ModuleProjectRuntime_instances, finishEdit_fn).call(this, committed);
|
|
2670
|
+
}
|
|
2671
|
+
}
|
|
2672
|
+
disconnectModulation(id) {
|
|
2673
|
+
const graph = __privateMethod(this, _ModuleProjectRuntime_instances, requireEditable_fn).call(this);
|
|
2674
|
+
const generation = __privateGet(this, _generation);
|
|
2675
|
+
__privateSet(this, _editing, true);
|
|
2676
|
+
let committed;
|
|
2677
|
+
try {
|
|
2678
|
+
if (!graph.project.modulations.some((record) => record.id === id)) throw new Error(`Unknown project modulation ${id}.`);
|
|
2679
|
+
const next = parseModuleProject({ ...graph.project, modulations: graph.project.modulations.filter((record) => record.id !== id) });
|
|
2680
|
+
const diagnostics = graph.diagnostics.filter((item) => !(item.kind === "modulation" && item.id === id));
|
|
2681
|
+
const prepared = __privateMethod(this, _ModuleProjectRuntime_instances, prepareEdit_fn).call(this, graph, next, diagnostics);
|
|
2682
|
+
__privateMethod(this, _ModuleProjectRuntime_instances, assertEdit_fn).call(this, graph, generation);
|
|
2683
|
+
const lease = graph.modulationLeases.get(id);
|
|
2684
|
+
graph.modulationLeases.delete(id);
|
|
2685
|
+
committed = __privateMethod(this, _ModuleProjectRuntime_instances, commitEdit_fn).call(this, graph, next, diagnostics, prepared);
|
|
2686
|
+
__privateMethod(this, _ModuleProjectRuntime_instances, retire_fn).call(this, lease);
|
|
2687
|
+
return committed;
|
|
2688
|
+
} finally {
|
|
2689
|
+
__privateMethod(this, _ModuleProjectRuntime_instances, finishEdit_fn).call(this, committed);
|
|
2690
|
+
}
|
|
2691
|
+
}
|
|
2692
|
+
/**
|
|
2693
|
+
* Add one module without rebuilding (DEC-094): every other instance, route
|
|
2694
|
+
* and voice, and the Session's state, stay as they are. The record joins a
|
|
2695
|
+
* context the project already has; `assets` adds the references it names,
|
|
2696
|
+
* and `layout` replaces the layout in the same edit, so the new box has its
|
|
2697
|
+
* place. A missing plugin leaves the record unresolved, as a restore does.
|
|
2698
|
+
* A failure leaves the graph as it was.
|
|
2699
|
+
*/
|
|
2700
|
+
async addModule(record, options = {}) {
|
|
2701
|
+
const graph = __privateMethod(this, _ModuleProjectRuntime_instances, requireEditable_fn).call(this);
|
|
2702
|
+
const generation = __privateGet(this, _generation);
|
|
2703
|
+
__privateSet(this, _editing, true);
|
|
2704
|
+
let committed;
|
|
2705
|
+
let added;
|
|
2706
|
+
try {
|
|
2707
|
+
const next = parseModuleProject({
|
|
2708
|
+
...graph.project,
|
|
2709
|
+
modules: [...graph.project.modules, record],
|
|
2710
|
+
...options.assets ? { assets: [...graph.project.assets, ...options.assets] } : {},
|
|
2711
|
+
...options.layout ? { layout: options.layout } : {}
|
|
2712
|
+
});
|
|
2713
|
+
const parsed = next.modules[next.modules.length - 1];
|
|
2714
|
+
const diagnostics = [...graph.diagnostics];
|
|
2715
|
+
added = parsed.id;
|
|
2716
|
+
await __privateMethod(this, _ModuleProjectRuntime_instances, createModule_fn).call(this, graph, parsed, graph.contexts.get(parsed.contextId), graph.controller.signal, new Map(next.assets.map((asset) => [asset.id, asset])), diagnostics);
|
|
2717
|
+
__privateMethod(this, _ModuleProjectRuntime_instances, assertEdit_fn).call(this, graph, generation);
|
|
2718
|
+
__privateMethod(this, _ModuleProjectRuntime_instances, validateAudioTopology_fn).call(this, graph, next);
|
|
2719
|
+
const prepared = __privateMethod(this, _ModuleProjectRuntime_instances, prepareEdit_fn).call(this, graph, next, diagnostics);
|
|
2720
|
+
__privateMethod(this, _ModuleProjectRuntime_instances, assertEdit_fn).call(this, graph, generation);
|
|
2721
|
+
committed = __privateMethod(this, _ModuleProjectRuntime_instances, commitEdit_fn).call(this, graph, next, diagnostics, prepared);
|
|
2722
|
+
added = void 0;
|
|
2723
|
+
for (const probe of __privateGet(this, _signalProbes)) __privateMethod(this, _ModuleProjectRuntime_instances, wireProbe_fn).call(this, probe);
|
|
2724
|
+
const instance = graph.instances.get(parsed.id);
|
|
2725
|
+
if (instance?.activate) __privateMethod(this, _ModuleProjectRuntime_instances, cleanup_fn).call(this, () => instance.activate());
|
|
2726
|
+
return committed;
|
|
2727
|
+
} catch (error) {
|
|
2728
|
+
if (added !== void 0 && !graph.disposed) __privateMethod(this, _ModuleProjectRuntime_instances, discardModule_fn).call(this, graph, added);
|
|
2729
|
+
throw error;
|
|
2730
|
+
} finally {
|
|
2731
|
+
__privateMethod(this, _ModuleProjectRuntime_instances, finishEdit_fn).call(this, committed);
|
|
2732
|
+
}
|
|
2733
|
+
}
|
|
2734
|
+
/**
|
|
2735
|
+
* Remove one module without rebuilding (DEC-094), with the cords,
|
|
2736
|
+
* modulations, automation and mappings that touch it and its place in the
|
|
2737
|
+
* layout (a group left with one member is dissolved); `layout` replaces the layout instead. Everything else keeps
|
|
2738
|
+
* playing. A module that owns a context, or that a macro targets, is not
|
|
2739
|
+
* removed this way: restore the edited project.
|
|
2740
|
+
*/
|
|
2741
|
+
removeModule(id, options = {}) {
|
|
2742
|
+
const graph = __privateMethod(this, _ModuleProjectRuntime_instances, requireEditable_fn).call(this);
|
|
2743
|
+
const generation = __privateGet(this, _generation);
|
|
2744
|
+
__privateSet(this, _editing, true);
|
|
2745
|
+
let committed;
|
|
2746
|
+
try {
|
|
2747
|
+
const current = graph.project;
|
|
2748
|
+
if (!current.modules.some((record) => record.id === id)) throw new Error(`Unknown project module ${id}.`);
|
|
2749
|
+
if (current.contexts.some((context) => context.ownerModuleId === id)) throw new Error(`Module ${id} owns a context: restore the edited project to remove it.`);
|
|
2750
|
+
if (current.macros.some((macro) => macro.targets.some((target) => target.moduleId === id))) throw new Error(`A macro targets module ${id}: restore the edited project to remove it.`);
|
|
2751
|
+
const connections = new Set(current.connections.filter((item) => item.from.moduleId === id || item.to.moduleId === id).map((item) => item.id));
|
|
2752
|
+
const modulations = new Set(current.modulations.filter((item) => item.source.moduleId === id || item.target.moduleId === id).map((item) => item.id));
|
|
2753
|
+
const automation = new Set(current.automation.filter((item) => item.target.moduleId === id).map((item) => item.id));
|
|
2754
|
+
const layout2 = options.layout ?? {
|
|
2755
|
+
...current.layout,
|
|
2756
|
+
modules: current.layout.modules.filter((item) => item.moduleId !== id),
|
|
2757
|
+
...current.layout.presentation ? { presentation: current.layout.presentation.filter((item) => item.moduleId !== id) } : {},
|
|
2758
|
+
// A group is at least two modules: one that would hold fewer is dissolved.
|
|
2759
|
+
...current.layout.groups ? { groups: current.layout.groups.map((group) => ({ ...group, moduleIds: group.moduleIds.filter((member) => member !== id) })).filter((group) => group.moduleIds.length > 1) } : {}
|
|
2760
|
+
};
|
|
2761
|
+
const next = parseModuleProject({
|
|
2762
|
+
...current,
|
|
2763
|
+
layout: layout2,
|
|
2764
|
+
modules: current.modules.filter((record) => record.id !== id),
|
|
2765
|
+
connections: current.connections.filter((item) => !connections.has(item.id)),
|
|
2766
|
+
modulations: current.modulations.filter((item) => !modulations.has(item.id)),
|
|
2767
|
+
automation: current.automation.filter((item) => !automation.has(item.id)),
|
|
2768
|
+
...current.mappings ? { mappings: current.mappings.filter((item) => item.target.moduleId !== id) } : {}
|
|
2769
|
+
});
|
|
2770
|
+
const remaining = new Map(graph.instances);
|
|
2771
|
+
remaining.delete(id);
|
|
2772
|
+
__privateMethod(this, _ModuleProjectRuntime_instances, validateAudioTopology_fn).call(this, { ...graph, instances: remaining }, next);
|
|
2773
|
+
__privateMethod(this, _ModuleProjectRuntime_instances, assertEdit_fn).call(this, graph, generation);
|
|
2774
|
+
const diagnostics = graph.diagnostics.filter((item) => !(item.kind === "module" && item.id === id) && !(item.kind === "connection" && connections.has(item.id)) && !(item.kind === "modulation" && modulations.has(item.id)) && !(item.kind === "automation" && automation.has(item.id)));
|
|
2775
|
+
const mappings2 = new Map([...graph.modulationLeases].filter(([key]) => !modulations.has(key)));
|
|
2776
|
+
const prepared = __privateMethod(this, _ModuleProjectRuntime_instances, prepareEdit_fn).call(this, graph, next, diagnostics, mappings2);
|
|
2777
|
+
__privateMethod(this, _ModuleProjectRuntime_instances, assertEdit_fn).call(this, graph, generation);
|
|
2778
|
+
const retired = [];
|
|
2779
|
+
for (const key of connections) {
|
|
2780
|
+
retired.push(graph.connectionLeases.get(key));
|
|
2781
|
+
graph.connectionLeases.delete(key);
|
|
2782
|
+
}
|
|
2783
|
+
for (const key of modulations) {
|
|
2784
|
+
retired.push(graph.modulationLeases.get(key));
|
|
2785
|
+
graph.modulationLeases.delete(key);
|
|
2786
|
+
}
|
|
2787
|
+
for (const key of automation) {
|
|
2788
|
+
retired.push(graph.automationLeases.get(key));
|
|
2789
|
+
graph.automationLeases.delete(key);
|
|
2790
|
+
}
|
|
2791
|
+
committed = __privateMethod(this, _ModuleProjectRuntime_instances, commitEdit_fn).call(this, graph, next, diagnostics, prepared);
|
|
2792
|
+
for (const lease of retired.reverse()) __privateMethod(this, _ModuleProjectRuntime_instances, retire_fn).call(this, lease);
|
|
2793
|
+
for (const probe of __privateGet(this, _signalProbes)) {
|
|
2794
|
+
if (probe.endpoint.moduleId !== id || !probe.lease) continue;
|
|
2795
|
+
__privateMethod(this, _ModuleProjectRuntime_instances, retire_fn).call(this, probe.lease);
|
|
2796
|
+
probe.lease = void 0;
|
|
2797
|
+
}
|
|
2798
|
+
__privateMethod(this, _ModuleProjectRuntime_instances, discardModule_fn).call(this, graph, id);
|
|
2799
|
+
return committed;
|
|
2800
|
+
} finally {
|
|
2801
|
+
__privateMethod(this, _ModuleProjectRuntime_instances, finishEdit_fn).call(this, committed);
|
|
2802
|
+
}
|
|
2803
|
+
}
|
|
2804
|
+
subscribe(listener) {
|
|
2805
|
+
__privateMethod(this, _ModuleProjectRuntime_instances, assertActive_fn).call(this);
|
|
2806
|
+
__privateGet(this, _listeners4).add(listener);
|
|
2807
|
+
__privateMethod(this, _ModuleProjectRuntime_instances, call_fn).call(this, listener);
|
|
2808
|
+
return () => {
|
|
2809
|
+
__privateGet(this, _listeners4).delete(listener);
|
|
2810
|
+
};
|
|
2811
|
+
}
|
|
2812
|
+
dispose() {
|
|
2813
|
+
var _a;
|
|
2814
|
+
if (__privateGet(this, _disposed3)) return;
|
|
2815
|
+
__privateSet(this, _disposed3, true);
|
|
2816
|
+
(_a = __privateGet(this, _stopPreload)) == null ? void 0 : _a.call(this);
|
|
2817
|
+
__privateSet(this, _stopPreload, void 0);
|
|
2818
|
+
const pending = __privateGet(this, _pending), graph = __privateGet(this, _graph);
|
|
2819
|
+
__privateSet(this, _pending, void 0);
|
|
2820
|
+
__privateSet(this, _graph, void 0);
|
|
2821
|
+
++__privateWrapper(this, _generation)._;
|
|
2822
|
+
pending?.controller.abort();
|
|
2823
|
+
if (graph) __privateMethod(this, _ModuleProjectRuntime_instances, disposeGraph_fn).call(this, graph);
|
|
2824
|
+
for (const probe of __privateGet(this, _signalProbes)) {
|
|
2825
|
+
if (probe.lease) __privateMethod(this, _ModuleProjectRuntime_instances, retire_fn).call(this, probe.lease);
|
|
2826
|
+
probe.lease = void 0;
|
|
2827
|
+
__privateMethod(this, _ModuleProjectRuntime_instances, cleanup_fn).call(this, () => probe.analyser.disconnect());
|
|
2828
|
+
}
|
|
2829
|
+
__privateGet(this, _signalProbes).clear();
|
|
2830
|
+
__privateGet(this, _observers).clear();
|
|
2831
|
+
__privateMethod(this, _ModuleProjectRuntime_instances, changed_fn).call(this);
|
|
2832
|
+
__privateGet(this, _listeners4).clear();
|
|
2833
|
+
}
|
|
2834
|
+
};
|
|
2835
|
+
_options2 = new WeakMap();
|
|
2836
|
+
_listeners4 = new WeakMap();
|
|
2837
|
+
_ownedInstances = new WeakMap();
|
|
2838
|
+
_graph = new WeakMap();
|
|
2839
|
+
_pending = new WeakMap();
|
|
2840
|
+
_generation = new WeakMap();
|
|
2841
|
+
_revision = new WeakMap();
|
|
2842
|
+
_disposed3 = new WeakMap();
|
|
2843
|
+
_editing = new WeakMap();
|
|
2844
|
+
_editObserved = new WeakMap();
|
|
2845
|
+
_preparedEdit = new WeakMap();
|
|
2846
|
+
_committing = new WeakMap();
|
|
2847
|
+
_observers = new WeakMap();
|
|
2848
|
+
_sequence = new WeakMap();
|
|
2849
|
+
_signalProbes = new WeakMap();
|
|
2850
|
+
_stopPreload = new WeakMap();
|
|
2851
|
+
_ModuleProjectRuntime_instances = new WeakSet();
|
|
2852
|
+
wireProbe_fn = function(entry) {
|
|
2853
|
+
const graph = __privateGet(this, _graph);
|
|
2854
|
+
if (entry.lease && entry.graph === graph) return;
|
|
2855
|
+
if (entry.lease) {
|
|
2856
|
+
__privateMethod(this, _ModuleProjectRuntime_instances, retire_fn).call(this, entry.lease);
|
|
2857
|
+
entry.lease = void 0;
|
|
2858
|
+
}
|
|
2859
|
+
entry.graph = graph;
|
|
2860
|
+
const instance = graph?.instances.get(entry.endpoint.moduleId);
|
|
2861
|
+
if (!instance) return;
|
|
2862
|
+
const port = instance.module.getPort?.(entry.endpoint.port);
|
|
2863
|
+
if (port && typeof port.send === "function") {
|
|
2864
|
+
__privateMethod(this, _ModuleProjectRuntime_instances, cleanup_fn).call(this, () => {
|
|
2865
|
+
entry.lease = port.send(entry.analyser);
|
|
2866
|
+
});
|
|
2867
|
+
return;
|
|
2868
|
+
}
|
|
2869
|
+
const control = instance.getControl?.(entry.endpoint.port);
|
|
2870
|
+
if (control) {
|
|
2871
|
+
__privateMethod(this, _ModuleProjectRuntime_instances, cleanup_fn).call(this, () => {
|
|
2872
|
+
control.output.connect(entry.analyser);
|
|
2873
|
+
entry.lease = createConnectionLease(() => {
|
|
2874
|
+
try {
|
|
2875
|
+
control.output.disconnect(entry.analyser);
|
|
2876
|
+
} catch {
|
|
2877
|
+
}
|
|
2878
|
+
});
|
|
2879
|
+
});
|
|
2880
|
+
}
|
|
2881
|
+
};
|
|
2882
|
+
/** A tap that reports a delivery along `connection` before it happens. */
|
|
2883
|
+
tap_fn = function(connection, kind) {
|
|
2884
|
+
return (value) => {
|
|
2885
|
+
if (!__privateGet(this, _observers).size) return;
|
|
2886
|
+
const base = { connectionId: connection.id, from: connection.from, to: connection.to, sequence: ++__privateWrapper(this, _sequence)._ };
|
|
2887
|
+
const event = kind === "message" ? Object.freeze({ ...base, kind, message: value, depth: currentMessageDepth() }) : Object.freeze({ ...base, kind, event: value });
|
|
2888
|
+
for (const observer of [...__privateGet(this, _observers)]) {
|
|
2889
|
+
try {
|
|
2890
|
+
observer(event);
|
|
2891
|
+
} catch (error) {
|
|
2892
|
+
__privateMethod(this, _ModuleProjectRuntime_instances, report_fn).call(this, error);
|
|
2893
|
+
}
|
|
2894
|
+
}
|
|
2895
|
+
};
|
|
2896
|
+
};
|
|
2897
|
+
captureSnapshot_fn = function(graph, project) {
|
|
2898
|
+
return Object.freeze({
|
|
2899
|
+
project,
|
|
2900
|
+
revision: __privateGet(this, _revision),
|
|
2901
|
+
restoring: __privateGet(this, _pending) !== void 0,
|
|
2902
|
+
disposed: __privateGet(this, _disposed3),
|
|
2903
|
+
modules: Object.freeze((graph?.project.modules ?? []).map((record) => {
|
|
2904
|
+
const instance = graph?.instances.get(record.id);
|
|
2905
|
+
const diagnostic = graph?.diagnostics.find((item) => item.kind === "module" && item.id === record.id)?.reason;
|
|
2906
|
+
const defaultOwnerModuleId = graph?.defaultOwners.get(record.contextId);
|
|
2907
|
+
return Object.freeze({
|
|
2908
|
+
record,
|
|
2909
|
+
status: instance ? "ready" : "unresolved",
|
|
2910
|
+
ports: Object.freeze((instance?.ports ?? []).map((port) => Object.freeze({ ...port }))),
|
|
2911
|
+
...defaultOwnerModuleId ? { defaultOwnerModuleId } : {},
|
|
2912
|
+
...diagnostic ? { diagnostic } : {}
|
|
2913
|
+
});
|
|
2914
|
+
})),
|
|
2915
|
+
unresolved: Object.freeze([...graph?.diagnostics ?? []])
|
|
2916
|
+
});
|
|
2917
|
+
};
|
|
2918
|
+
exportGraph_fn = function(graph) {
|
|
2919
|
+
const transport = graph.session.snapshot;
|
|
2920
|
+
return parseModuleProject({
|
|
2921
|
+
...graph.project,
|
|
2922
|
+
modules: graph.project.modules.map((record) => {
|
|
2923
|
+
const instance = graph.instances.get(record.id);
|
|
2924
|
+
if (!instance) return record;
|
|
2925
|
+
const params = { ...record.params };
|
|
2926
|
+
for (const handle of instance.module.params ?? []) params[handle.descriptor.id] = handle.snapshot.baseValue;
|
|
2927
|
+
return { ...record, config: instance.exportConfig?.() ?? record.config, params };
|
|
2928
|
+
}),
|
|
2929
|
+
macros: graph.project.macros.map((record) => ({ ...record, value: graph.macros.get(record.id)?.value ?? record.value })),
|
|
2930
|
+
modulations: graph.project.modulations.map((record) => ({ ...record, depth: graph.modulationLeases.get(record.id)?.depth ?? record.depth })),
|
|
2931
|
+
automation: graph.project.automation.map((record) => ({ ...record, ...graph.automationLeases.has(record.id) ? { enabled: graph.automationLeases.get(record.id).active, initialValue: graph.automationLeases.get(record.id).initialValue } : {} })),
|
|
2932
|
+
session: {
|
|
2933
|
+
positionSeconds: graph.session.clock.positionAt(transport.referenceTime),
|
|
2934
|
+
rate: transport.rate,
|
|
2935
|
+
loop: transport.loop,
|
|
2936
|
+
...graph.project.session.tempo ? { tempo: graph.project.session.tempo } : {}
|
|
2937
|
+
}
|
|
2938
|
+
});
|
|
2939
|
+
};
|
|
2940
|
+
graphTempo_fn = function(graph) {
|
|
2941
|
+
return createMusicalTempo(graph.project.session.tempo ?? {});
|
|
2942
|
+
};
|
|
2943
|
+
prepareEdit_fn = function(graph, project, diagnostics, modulationLeases = graph.modulationLeases) {
|
|
2944
|
+
const candidate = { ...graph, project, diagnostics, modulationLeases };
|
|
2945
|
+
const observed = __privateGet(this, _editObserved);
|
|
2946
|
+
__privateSet(this, _editObserved, false);
|
|
2947
|
+
try {
|
|
2948
|
+
const project2 = __privateMethod(this, _ModuleProjectRuntime_instances, exportGraph_fn).call(this, candidate);
|
|
2949
|
+
const snapshot = __privateMethod(this, _ModuleProjectRuntime_instances, captureSnapshot_fn).call(this, candidate, project2);
|
|
2950
|
+
__privateSet(this, _preparedEdit, { graph, project: project2 });
|
|
2951
|
+
return snapshot;
|
|
2952
|
+
} catch (error) {
|
|
2953
|
+
__privateGet(this, _editObserved) || __privateSet(this, _editObserved, observed);
|
|
2954
|
+
throw error;
|
|
2955
|
+
}
|
|
2956
|
+
};
|
|
2957
|
+
assertEdit_fn = function(graph, generation) {
|
|
2958
|
+
if (__privateGet(this, _graph) !== graph || __privateGet(this, _generation) !== generation || graph.disposed) throw aborted();
|
|
2959
|
+
};
|
|
2960
|
+
commitEdit_fn = function(graph, project, diagnostics, prepared) {
|
|
2961
|
+
graph.project = project;
|
|
2962
|
+
graph.diagnostics.splice(0, graph.diagnostics.length, ...diagnostics);
|
|
2963
|
+
return Object.freeze({ ...prepared, revision: ++__privateWrapper(this, _revision)._ });
|
|
2964
|
+
};
|
|
2965
|
+
finishEdit_fn = function(committed) {
|
|
2966
|
+
__privateSet(this, _editing, false);
|
|
2967
|
+
const observed = __privateGet(this, _editObserved);
|
|
2968
|
+
__privateSet(this, _editObserved, false);
|
|
2969
|
+
__privateSet(this, _preparedEdit, void 0);
|
|
2970
|
+
if (committed) {
|
|
2971
|
+
__privateMethod(this, _ModuleProjectRuntime_instances, notify_fn2).call(this, committed.revision, committed);
|
|
2972
|
+
if (observed && __privateGet(this, _graph)) __privateMethod(this, _ModuleProjectRuntime_instances, observe_fn).call(this, __privateGet(this, _graph));
|
|
2973
|
+
} else if (observed) __privateMethod(this, _ModuleProjectRuntime_instances, changed_fn).call(this);
|
|
2974
|
+
};
|
|
2975
|
+
build_fn = async function(project, controller) {
|
|
2976
|
+
const signal = controller.signal;
|
|
2977
|
+
assertSignal(signal);
|
|
2978
|
+
const audio = __privateGet(this, _options2).audio.prepare();
|
|
2979
|
+
assertSignal(signal);
|
|
2980
|
+
const gate = audio.createGain();
|
|
2981
|
+
gate.gain.value = 0;
|
|
2982
|
+
const root = project.contexts.find((context2) => !context2.parentId);
|
|
2983
|
+
const session2 = new SessionTransport(() => audio.currentTime);
|
|
2984
|
+
const musical = createMusicalTimeSource(session2, () => __privateMethod(this, _ModuleProjectRuntime_instances, graphTempo_fn).call(this, graph));
|
|
2985
|
+
const context = new ModuleContext({
|
|
2986
|
+
id: root.id,
|
|
2987
|
+
audio: __privateGet(this, _options2).audio,
|
|
2988
|
+
session: session2,
|
|
2989
|
+
destination: () => gate,
|
|
2990
|
+
capabilities: new ModuleCapabilities().with(MUSICAL_TIME, musical)
|
|
2991
|
+
});
|
|
2992
|
+
const graph = {
|
|
2993
|
+
project,
|
|
2994
|
+
controller,
|
|
2995
|
+
gate,
|
|
2996
|
+
session: session2,
|
|
2997
|
+
context,
|
|
2998
|
+
contexts: /* @__PURE__ */ new Map([[root.id, context]]),
|
|
2999
|
+
defaultOwners: /* @__PURE__ */ new Map([[root.id, void 0]]),
|
|
3000
|
+
instances: /* @__PURE__ */ new Map(),
|
|
3001
|
+
handles: /* @__PURE__ */ new Map(),
|
|
3002
|
+
macros: /* @__PURE__ */ new Map(),
|
|
3003
|
+
leases: [],
|
|
3004
|
+
moduleLeases: /* @__PURE__ */ new Map(),
|
|
3005
|
+
connectionLeases: /* @__PURE__ */ new Map(),
|
|
3006
|
+
modulationLeases: /* @__PURE__ */ new Map(),
|
|
3007
|
+
automationLeases: /* @__PURE__ */ new Map(),
|
|
3008
|
+
diagnostics: [],
|
|
3009
|
+
disposed: false
|
|
3010
|
+
};
|
|
3011
|
+
graph.musicalListeners = /* @__PURE__ */ new Set([() => musical.notify()]);
|
|
3012
|
+
try {
|
|
3013
|
+
const destination = __privateGet(this, _options2).destination?.() ?? audio.destination;
|
|
3014
|
+
if (destination.context !== audio || destination === gate) throw new Error("Project output destination must belong to its AudioContext.");
|
|
3015
|
+
gate.connect(destination);
|
|
3016
|
+
const assets = new Map(project.assets.map((asset) => [asset.id, asset]));
|
|
3017
|
+
const prepareContext = async (record) => {
|
|
3018
|
+
assertSignal(signal);
|
|
3019
|
+
const parent = record.parentId ? graph.contexts.get(record.parentId) : context;
|
|
3020
|
+
let scope = record.parentId ? void 0 : context;
|
|
3021
|
+
if (record.parentId && parent) {
|
|
3022
|
+
const fresh = parent.derive({ id: record.id, registry: new ModuleRegistry() });
|
|
3023
|
+
try {
|
|
3024
|
+
let defaultOwner = graph.defaultOwners.get(record.parentId);
|
|
3025
|
+
if (!record.ownerModuleId) scope = fresh;
|
|
3026
|
+
else {
|
|
3027
|
+
const owner = graph.instances.get(record.ownerModuleId);
|
|
3028
|
+
if (owner?.childContext) {
|
|
3029
|
+
scope = owner.childContext(fresh);
|
|
3030
|
+
if (scope && (scope.admit !== fresh.admit || scope.destination !== fresh.destination)) {
|
|
3031
|
+
defaultOwner = owner.ports?.some((port) => port.kind === "audio" && port.direction === "input") ? record.ownerModuleId : void 0;
|
|
3032
|
+
}
|
|
3033
|
+
}
|
|
3034
|
+
}
|
|
3035
|
+
if (scope && (scope.id !== fresh.id || scope.registry !== fresh.registry || scope.audio !== fresh.audio || scope.session !== fresh.session)) throw new Error("A project child context must preserve its ID, registry, audio and Session ownership.");
|
|
3036
|
+
if (scope) {
|
|
3037
|
+
graph.contexts.set(record.id, scope);
|
|
3038
|
+
graph.defaultOwners.set(record.id, defaultOwner);
|
|
3039
|
+
} else __privateMethod(this, _ModuleProjectRuntime_instances, cleanup_fn).call(this, () => fresh.registry.dispose());
|
|
3040
|
+
} catch (error) {
|
|
3041
|
+
__privateMethod(this, _ModuleProjectRuntime_instances, cleanup_fn).call(this, () => fresh.registry.dispose());
|
|
3042
|
+
throw error;
|
|
3043
|
+
}
|
|
3044
|
+
}
|
|
3045
|
+
if (!scope) graph.diagnostics.push({ kind: "context", id: record.id, reason: "Parent context or owner plugin is unresolved." });
|
|
3046
|
+
for (const moduleRecord of project.modules.filter((module) => module.contextId === record.id)) {
|
|
3047
|
+
await __privateMethod(this, _ModuleProjectRuntime_instances, createModule_fn).call(this, graph, moduleRecord, scope, signal, assets, graph.diagnostics);
|
|
3048
|
+
}
|
|
3049
|
+
for (const child of project.contexts.filter((child2) => child2.parentId === record.id)) await prepareContext(child);
|
|
3050
|
+
};
|
|
3051
|
+
await prepareContext(root);
|
|
3052
|
+
__privateMethod(this, _ModuleProjectRuntime_instances, validateAudioTopology_fn).call(this, graph, project);
|
|
3053
|
+
const normalRoutes = /* @__PURE__ */ new Map();
|
|
3054
|
+
for (const connection of project.connections) {
|
|
3055
|
+
if (connection.kind !== "audio" || (connection.mode ?? "replace") !== "replace") continue;
|
|
3056
|
+
const source = graph.instances.get(connection.from.moduleId)?.module.getPort?.(connection.from.port);
|
|
3057
|
+
if (!source) continue;
|
|
3058
|
+
if (normalRoutes.has(source)) throw new Error("Aliased audio output ports cannot have multiple replacement claims.");
|
|
3059
|
+
normalRoutes.set(source, source.defaultDestination);
|
|
3060
|
+
source.setDefault(void 0);
|
|
3061
|
+
}
|
|
3062
|
+
for (const connection of project.connections) {
|
|
3063
|
+
const lease = await __privateMethod(this, _ModuleProjectRuntime_instances, connect_fn).call(this, graph, connection, graph.diagnostics);
|
|
3064
|
+
if (lease) graph.connectionLeases.set(connection.id, lease);
|
|
3065
|
+
assertSignal(signal);
|
|
3066
|
+
}
|
|
3067
|
+
for (const [source, normal] of normalRoutes) source.setDefault(normal);
|
|
3068
|
+
for (const record of project.modulations) {
|
|
3069
|
+
const lease = __privateMethod(this, _ModuleProjectRuntime_instances, connectModulation_fn).call(this, graph, record, graph.diagnostics);
|
|
3070
|
+
if (lease) graph.modulationLeases.set(record.id, lease);
|
|
3071
|
+
assertSignal(signal);
|
|
3072
|
+
}
|
|
3073
|
+
for (const record of project.macros) {
|
|
3074
|
+
const targets = record.targets.map((target) => ({ target: __privateMethod(this, _ModuleProjectRuntime_instances, parameter_fn).call(this, graph, target), min: target.min, max: target.max }));
|
|
3075
|
+
if (targets.some((target) => !target.target)) {
|
|
3076
|
+
graph.diagnostics.push({ kind: "macro", id: record.id, reason: "Macro parameter is unresolved." });
|
|
3077
|
+
continue;
|
|
3078
|
+
}
|
|
3079
|
+
const macro = new ParamMacro(targets, { value: record.value });
|
|
3080
|
+
graph.macros.set(record.id, macro);
|
|
3081
|
+
graph.leases.push(createConnectionLease(macro.subscribe(() => __privateMethod(this, _ModuleProjectRuntime_instances, observe_fn).call(this, graph))));
|
|
3082
|
+
}
|
|
3083
|
+
await __privateMethod(this, _ModuleProjectRuntime_instances, command_fn).call(this, session2.setRate(project.session.rate), signal);
|
|
3084
|
+
if (project.session.loop) await __privateMethod(this, _ModuleProjectRuntime_instances, command_fn).call(this, session2.setLoop(project.session.loop), signal);
|
|
3085
|
+
await __privateMethod(this, _ModuleProjectRuntime_instances, command_fn).call(this, session2.seek(project.session.positionSeconds), signal);
|
|
3086
|
+
for (const record of project.automation) {
|
|
3087
|
+
if (record.enabled === false) continue;
|
|
3088
|
+
const target = __privateMethod(this, _ModuleProjectRuntime_instances, parameter_fn).call(this, graph, record.target);
|
|
3089
|
+
if (!target) {
|
|
3090
|
+
graph.diagnostics.push({ kind: "automation", id: record.id, reason: "Automation parameter is unresolved." });
|
|
3091
|
+
continue;
|
|
3092
|
+
}
|
|
3093
|
+
const lease = attachSessionAutomation(session2.scheduling, audio, target, record.points, {
|
|
3094
|
+
loop: record.loop,
|
|
3095
|
+
initialValue: record.initialValue,
|
|
3096
|
+
onRelease: () => __privateMethod(this, _ModuleProjectRuntime_instances, observe_fn).call(this, graph),
|
|
3097
|
+
onError: (error) => {
|
|
3098
|
+
graph.diagnostics.push({ kind: "automation", id: record.id, reason: `Automation stopped: ${String(error)}` });
|
|
3099
|
+
__privateMethod(this, _ModuleProjectRuntime_instances, observe_fn).call(this, graph);
|
|
3100
|
+
}
|
|
3101
|
+
});
|
|
3102
|
+
graph.automationLeases.set(record.id, lease);
|
|
3103
|
+
assertSignal(signal);
|
|
3104
|
+
}
|
|
3105
|
+
graph.leases.push(createConnectionLease(session2.subscribe(() => __privateMethod(this, _ModuleProjectRuntime_instances, observe_fn).call(this, graph))));
|
|
3106
|
+
return graph;
|
|
3107
|
+
} catch (error) {
|
|
3108
|
+
__privateMethod(this, _ModuleProjectRuntime_instances, disposeGraph_fn).call(this, graph);
|
|
3109
|
+
throw error;
|
|
3110
|
+
}
|
|
3111
|
+
};
|
|
3112
|
+
createModule_fn = async function(graph, moduleRecord, scope, signal, assets, diagnostics) {
|
|
3113
|
+
assertSignal(signal);
|
|
3114
|
+
const factory = __privateGet(this, _options2).plugins.get(moduleRecord.plugin.id, moduleRecord.plugin.version);
|
|
3115
|
+
if (!scope || !factory) {
|
|
3116
|
+
diagnostics.push({ kind: "module", id: moduleRecord.id, reason: !scope ? "Module context is unresolved." : `Missing plugin ${moduleRecord.plugin.id}@${moduleRecord.plugin.version}.` });
|
|
3117
|
+
return;
|
|
3118
|
+
}
|
|
3119
|
+
const asset = (role) => {
|
|
3120
|
+
const id = moduleRecord.assets?.[role];
|
|
3121
|
+
return id === void 0 ? void 0 : assets.get(id);
|
|
3122
|
+
};
|
|
3123
|
+
let instance;
|
|
3124
|
+
try {
|
|
3125
|
+
instance = await abortable(factory.create({
|
|
3126
|
+
record: moduleRecord,
|
|
3127
|
+
context: scope,
|
|
3128
|
+
signal,
|
|
3129
|
+
asset,
|
|
3130
|
+
resolveAsset: async (role) => {
|
|
3131
|
+
assertSignal(signal);
|
|
3132
|
+
const reference = asset(role);
|
|
3133
|
+
if (!reference) throw new Error(`Missing asset role ${role} for ${moduleRecord.id}.`);
|
|
3134
|
+
if (!__privateGet(this, _options2).resolveAsset) throw new Error("No project asset resolver was supplied.");
|
|
3135
|
+
return abortable(Promise.resolve(__privateGet(this, _options2).resolveAsset(reference, signal)), signal);
|
|
3136
|
+
}
|
|
3137
|
+
}), signal, (value) => {
|
|
3138
|
+
if (!__privateGet(this, _ownedInstances).has(value)) __privateMethod(this, _ModuleProjectRuntime_instances, disposeInstance_fn).call(this, value);
|
|
3139
|
+
});
|
|
3140
|
+
} catch (error) {
|
|
3141
|
+
if (signal.aborted || !isProjectModuleUnavailable(error)) throw error;
|
|
3142
|
+
diagnostics.push({ kind: "module", id: moduleRecord.id, reason: error.message });
|
|
3143
|
+
return;
|
|
3144
|
+
}
|
|
3145
|
+
if (__privateGet(this, _ownedInstances).has(instance)) throw new Error("Project factories must return fresh owned instances.");
|
|
3146
|
+
__privateGet(this, _ownedInstances).add(instance);
|
|
3147
|
+
graph.instances.set(moduleRecord.id, instance);
|
|
3148
|
+
const own = [];
|
|
3149
|
+
graph.moduleLeases.set(moduleRecord.id, own);
|
|
3150
|
+
const keep = (lease) => {
|
|
3151
|
+
own.push(lease);
|
|
3152
|
+
graph.leases.push(lease);
|
|
3153
|
+
};
|
|
3154
|
+
if (instance.module.id !== moduleRecord.id || typeof instance.dispose !== "function") throw new Error("Plugin instance must preserve its stable module ID and provide disposal.");
|
|
3155
|
+
graph.handles.set(moduleRecord.id, createModuleHandle(instance.module));
|
|
3156
|
+
const registered = scope.registry.get(moduleRecord.id);
|
|
3157
|
+
if (registered && registered !== instance.module) throw new Error(`Different module is already registered: ${moduleRecord.id}.`);
|
|
3158
|
+
if (!registered) keep(scope.registry.register(instance.module));
|
|
3159
|
+
await abortable(instance.module.prepare?.(), signal);
|
|
3160
|
+
for (const [id, value] of Object.entries(moduleRecord.params ?? {})) {
|
|
3161
|
+
const parameter2 = instance.module.params?.get(id);
|
|
3162
|
+
if (!parameter2) throw new Error(`Unknown saved parameter ${moduleRecord.id}.${id}.`);
|
|
3163
|
+
parameter2.setBaseValue(value);
|
|
3164
|
+
assertSignal(signal);
|
|
3165
|
+
}
|
|
3166
|
+
for (const parameter2 of instance.module.params ?? []) {
|
|
3167
|
+
let baseRevision = parameter2.snapshot.baseRevision;
|
|
3168
|
+
keep(createConnectionLease(parameter2.subscribe((snapshot) => {
|
|
3169
|
+
const baseChanged = snapshot.baseRevision !== baseRevision;
|
|
3170
|
+
baseRevision = snapshot.baseRevision;
|
|
3171
|
+
__privateMethod(this, _ModuleProjectRuntime_instances, observe_fn).call(this, graph, baseChanged || snapshot.disposed || __privateMethod(this, _ModuleProjectRuntime_instances, uncapturedParameterState_fn).call(this, graph, moduleRecord.id, parameter2.descriptor.id, snapshot.baseValue));
|
|
3172
|
+
})));
|
|
3173
|
+
}
|
|
3174
|
+
if (instance.module.subscribe) keep(createConnectionLease(instance.module.subscribe(() => __privateMethod(this, _ModuleProjectRuntime_instances, observe_fn).call(this, graph))));
|
|
3175
|
+
};
|
|
3176
|
+
/** Take one module out of a graph: the leases its creation took, then the instance. */
|
|
3177
|
+
discardModule_fn = function(graph, id) {
|
|
3178
|
+
const own = graph.moduleLeases.get(id) ?? [];
|
|
3179
|
+
graph.moduleLeases.delete(id);
|
|
3180
|
+
for (const lease of [...own].reverse()) __privateMethod(this, _ModuleProjectRuntime_instances, retire_fn).call(this, lease);
|
|
3181
|
+
if (own.length) {
|
|
3182
|
+
const gone = new Set(own);
|
|
3183
|
+
graph.leases.splice(0, graph.leases.length, ...graph.leases.filter((lease) => !gone.has(lease)));
|
|
3184
|
+
}
|
|
3185
|
+
const instance = graph.instances.get(id);
|
|
3186
|
+
graph.instances.delete(id);
|
|
3187
|
+
graph.handles.delete(id);
|
|
3188
|
+
if (instance) __privateMethod(this, _ModuleProjectRuntime_instances, disposeInstance_fn).call(this, instance);
|
|
3189
|
+
};
|
|
3190
|
+
connect_fn = async function(graph, connection, diagnostics) {
|
|
3191
|
+
assertSignal(graph.controller.signal);
|
|
3192
|
+
const from = graph.instances.get(connection.from.moduleId), to = graph.instances.get(connection.to.moduleId);
|
|
3193
|
+
if (connection.kind === "audio") {
|
|
3194
|
+
const source2 = from?.module.getPort?.(connection.from.port), target = to?.getInput?.(connection.to.port);
|
|
3195
|
+
if (source2 && target) return source2[connection.mode ?? "replace"](target);
|
|
3196
|
+
diagnostics.push({ kind: "connection", id: connection.id, reason: "Audio source or destination is unresolved." });
|
|
3197
|
+
if (source2 && (connection.mode ?? "replace") === "replace") {
|
|
3198
|
+
const silence = source2.context.createGain();
|
|
3199
|
+
let replacement;
|
|
3200
|
+
try {
|
|
3201
|
+
replacement = source2.replace(silence);
|
|
3202
|
+
} catch (error) {
|
|
3203
|
+
__privateMethod(this, _ModuleProjectRuntime_instances, cleanup_fn).call(this, () => silence.disconnect());
|
|
3204
|
+
throw error;
|
|
3205
|
+
}
|
|
3206
|
+
return createConnectionLease(() => {
|
|
3207
|
+
try {
|
|
3208
|
+
replacement.release();
|
|
3209
|
+
} finally {
|
|
3210
|
+
silence.disconnect();
|
|
3211
|
+
}
|
|
3212
|
+
});
|
|
3213
|
+
}
|
|
3214
|
+
return void 0;
|
|
3215
|
+
}
|
|
3216
|
+
if (connection.kind === "note" && from && to) {
|
|
3217
|
+
const source2 = getModulePort(from.module, connection.from.port, "note", "output");
|
|
3218
|
+
const target = getModulePort(to.module, connection.to.port, "note", "input");
|
|
3219
|
+
if (source2 && target) return tapped(source2.subscribe(__privateMethod(this, _ModuleProjectRuntime_instances, tap_fn).call(this, connection, "note")), () => target.connect(source2));
|
|
3220
|
+
}
|
|
3221
|
+
if (connection.kind === "message" && from && to) {
|
|
3222
|
+
const source2 = getModulePort(from.module, connection.from.port, "message", "output");
|
|
3223
|
+
if (source2 && connection.to.port.startsWith("param:")) {
|
|
3224
|
+
const handle = __privateMethod(this, _ModuleProjectRuntime_instances, parameter_fn).call(this, graph, { moduleId: connection.to.moduleId, param: connection.to.port.slice("param:".length) });
|
|
3225
|
+
if (handle) return connectMessageToParameter(source2, handle, graph.gate.context);
|
|
3226
|
+
}
|
|
3227
|
+
const target = getModulePort(to.module, connection.to.port, "message", "input");
|
|
3228
|
+
if (source2 && target) return tapped(source2.subscribe(__privateMethod(this, _ModuleProjectRuntime_instances, tap_fn).call(this, connection, "message")), () => target.connect(source2));
|
|
3229
|
+
}
|
|
3230
|
+
let source = from;
|
|
3231
|
+
if (connection.kind === "source" && from && to) {
|
|
3232
|
+
const carried = from.sourceOutput?.(connection.from.port) ?? from.module;
|
|
3233
|
+
if (carried !== from.module) source = { ...from, module: carried };
|
|
3234
|
+
const accepted = to.connectSource?.(connection.to.port, carried);
|
|
3235
|
+
if (accepted) return accepted;
|
|
3236
|
+
}
|
|
3237
|
+
if (source && to && __privateGet(this, _options2).connect) {
|
|
3238
|
+
const lease = await abortable(__privateGet(this, _options2).connect({ connection, from: source, to, context: graph.context, signal: graph.controller.signal }), graph.controller.signal, (value) => __privateMethod(this, _ModuleProjectRuntime_instances, retire_fn).call(this, value));
|
|
3239
|
+
if (lease !== void 0) {
|
|
3240
|
+
if (!lease || lease.active !== true || typeof lease.release !== "function") {
|
|
3241
|
+
__privateMethod(this, _ModuleProjectRuntime_instances, retire_fn).call(this, lease);
|
|
3242
|
+
throw new Error("A project connector must return an active connection lease.");
|
|
3243
|
+
}
|
|
3244
|
+
return lease;
|
|
3245
|
+
}
|
|
3246
|
+
}
|
|
3247
|
+
diagnostics.push({ kind: "connection", id: connection.id, reason: "Signal endpoints or a matching connector are unavailable." });
|
|
3248
|
+
return void 0;
|
|
3249
|
+
};
|
|
3250
|
+
connectModulation_fn = function(graph, record, diagnostics) {
|
|
3251
|
+
const source = graph.instances.get(record.source.moduleId)?.getControl?.(record.source.port);
|
|
3252
|
+
const target = __privateMethod(this, _ModuleProjectRuntime_instances, parameter_fn).call(this, graph, record.target);
|
|
3253
|
+
if (!source || !target) {
|
|
3254
|
+
diagnostics.push({ kind: "modulation", id: record.id, reason: "Control source or parameter is unresolved." });
|
|
3255
|
+
return void 0;
|
|
3256
|
+
}
|
|
3257
|
+
return target.connectModulation(source, { depth: record.depth, polarity: record.polarity });
|
|
3258
|
+
};
|
|
3259
|
+
validateAudioTopology_fn = function(graph, project) {
|
|
3260
|
+
const routes = /* @__PURE__ */ new Map();
|
|
3261
|
+
const replaced = new Set(project.connections.filter((connection) => connection.kind === "audio" && (connection.mode ?? "replace") === "replace").map((connection) => JSON.stringify(connection.from)));
|
|
3262
|
+
const add = (from, to) => {
|
|
3263
|
+
const targets = routes.get(from) ?? /* @__PURE__ */ new Set();
|
|
3264
|
+
targets.add(to);
|
|
3265
|
+
routes.set(from, targets);
|
|
3266
|
+
};
|
|
3267
|
+
for (const record of project.modules) {
|
|
3268
|
+
const owner = graph.defaultOwners.get(record.contextId), instance = graph.instances.get(record.id);
|
|
3269
|
+
if (!owner || !instance) continue;
|
|
3270
|
+
for (const descriptor of instance.ports ?? []) {
|
|
3271
|
+
if (descriptor.kind !== "audio" || descriptor.direction !== "output") continue;
|
|
3272
|
+
if (replaced.has(JSON.stringify({ moduleId: record.id, port: descriptor.id }))) continue;
|
|
3273
|
+
if (instance.module.getPort?.(descriptor.id)?.defaultDestination) add(record.id, owner);
|
|
3274
|
+
}
|
|
3275
|
+
}
|
|
3276
|
+
for (const connection of project.connections) {
|
|
3277
|
+
if (connection.kind !== "audio") continue;
|
|
3278
|
+
if (graph.instances.get(connection.from.moduleId)?.module.getPort?.(connection.from.port) && graph.instances.get(connection.to.moduleId)?.getInput?.(connection.to.port)) add(connection.from.moduleId, connection.to.moduleId);
|
|
3279
|
+
}
|
|
3280
|
+
const degrees = /* @__PURE__ */ new Map();
|
|
3281
|
+
for (const [source, targets] of routes) {
|
|
3282
|
+
if (!degrees.has(source)) degrees.set(source, 0);
|
|
3283
|
+
for (const target of targets) degrees.set(target, (degrees.get(target) ?? 0) + 1);
|
|
3284
|
+
}
|
|
3285
|
+
const ready = [...degrees.keys()].filter((id) => degrees.get(id) === 0);
|
|
3286
|
+
for (let index = 0; index < ready.length; index++) for (const target of routes.get(ready[index]) ?? []) {
|
|
3287
|
+
const remaining = degrees.get(target) - 1;
|
|
3288
|
+
degrees.set(target, remaining);
|
|
3289
|
+
if (remaining === 0) ready.push(target);
|
|
3290
|
+
}
|
|
3291
|
+
if (ready.length !== degrees.size) throw new Error("Project routes would create feedback through a nested default bus.");
|
|
3292
|
+
};
|
|
3293
|
+
parameter_fn = function(graph, target) {
|
|
3294
|
+
return graph.instances.get(target.moduleId)?.module.params?.get(target.param);
|
|
3295
|
+
};
|
|
3296
|
+
command_fn = async function(command, signal) {
|
|
3297
|
+
const result = await abortable(command, signal);
|
|
3298
|
+
if (result.status !== "committed") throw new Error("Project Session preparation was superseded.");
|
|
3299
|
+
};
|
|
3300
|
+
activate_fn = function(candidate, pending, prepared) {
|
|
3301
|
+
__privateMethod(this, _ModuleProjectRuntime_instances, assertPending_fn).call(this, pending);
|
|
3302
|
+
const previous = __privateGet(this, _graph);
|
|
3303
|
+
const time = candidate.gate.context.currentTime;
|
|
3304
|
+
let committed;
|
|
3305
|
+
__privateSet(this, _committing, true);
|
|
3306
|
+
try {
|
|
3307
|
+
candidate.gate.gain.setValueAtTime(1, time);
|
|
3308
|
+
__privateMethod(this, _ModuleProjectRuntime_instances, assertPending_fn).call(this, pending);
|
|
3309
|
+
previous?.gate.gain.setValueAtTime(0, time);
|
|
3310
|
+
__privateMethod(this, _ModuleProjectRuntime_instances, assertPending_fn).call(this, pending);
|
|
3311
|
+
committed = Object.freeze({ ...prepared, revision: __privateGet(this, _revision) + 1, restoring: false, disposed: false });
|
|
3312
|
+
__privateSet(this, _graph, candidate);
|
|
3313
|
+
__privateSet(this, _pending, void 0);
|
|
3314
|
+
__privateSet(this, _revision, committed.revision);
|
|
3315
|
+
} catch (error) {
|
|
3316
|
+
try {
|
|
3317
|
+
candidate.gate.gain.setValueAtTime(0, time);
|
|
3318
|
+
} catch (cleanup) {
|
|
3319
|
+
__privateMethod(this, _ModuleProjectRuntime_instances, report_fn).call(this, cleanup);
|
|
3320
|
+
}
|
|
3321
|
+
try {
|
|
3322
|
+
previous?.gate.gain.setValueAtTime(1, time);
|
|
3323
|
+
} catch (cleanup) {
|
|
3324
|
+
__privateMethod(this, _ModuleProjectRuntime_instances, report_fn).call(this, cleanup);
|
|
3325
|
+
}
|
|
3326
|
+
throw error;
|
|
3327
|
+
} finally {
|
|
3328
|
+
__privateSet(this, _committing, false);
|
|
3329
|
+
}
|
|
3330
|
+
if (previous) __privateMethod(this, _ModuleProjectRuntime_instances, disposeGraph_fn).call(this, previous);
|
|
3331
|
+
for (const probe of __privateGet(this, _signalProbes)) __privateMethod(this, _ModuleProjectRuntime_instances, wireProbe_fn).call(this, probe);
|
|
3332
|
+
for (const instance of candidate.instances.values()) {
|
|
3333
|
+
if (instance.activate) __privateMethod(this, _ModuleProjectRuntime_instances, cleanup_fn).call(this, () => instance.activate());
|
|
3334
|
+
}
|
|
3335
|
+
__privateMethod(this, _ModuleProjectRuntime_instances, notify_fn2).call(this, committed.revision, committed);
|
|
3336
|
+
return committed;
|
|
3337
|
+
};
|
|
3338
|
+
disposeGraph_fn = function(graph) {
|
|
3339
|
+
if (graph.disposed) return;
|
|
3340
|
+
graph.disposed = true;
|
|
3341
|
+
__privateMethod(this, _ModuleProjectRuntime_instances, cleanup_fn).call(this, () => {
|
|
3342
|
+
graph.gate.gain.value = 0;
|
|
3343
|
+
});
|
|
3344
|
+
__privateMethod(this, _ModuleProjectRuntime_instances, cleanup_fn).call(this, () => graph.controller.abort());
|
|
3345
|
+
__privateMethod(this, _ModuleProjectRuntime_instances, cleanup_fn).call(this, () => graph.session.dispose());
|
|
3346
|
+
for (const lease of [...graph.connectionLeases.values()].reverse()) __privateMethod(this, _ModuleProjectRuntime_instances, retire_fn).call(this, lease);
|
|
3347
|
+
for (const lease of graph.modulationLeases.values()) __privateMethod(this, _ModuleProjectRuntime_instances, retire_fn).call(this, lease);
|
|
3348
|
+
for (const lease of graph.automationLeases.values()) __privateMethod(this, _ModuleProjectRuntime_instances, retire_fn).call(this, lease);
|
|
3349
|
+
for (const lease of graph.leases.reverse()) __privateMethod(this, _ModuleProjectRuntime_instances, retire_fn).call(this, lease);
|
|
3350
|
+
for (const macro of graph.macros.values()) __privateMethod(this, _ModuleProjectRuntime_instances, cleanup_fn).call(this, () => macro.dispose());
|
|
3351
|
+
for (const instance of [...graph.instances.values()].reverse()) __privateMethod(this, _ModuleProjectRuntime_instances, disposeInstance_fn).call(this, instance);
|
|
3352
|
+
for (const context of [...graph.contexts.values()].reverse()) __privateMethod(this, _ModuleProjectRuntime_instances, cleanup_fn).call(this, () => context.registry.dispose());
|
|
3353
|
+
__privateMethod(this, _ModuleProjectRuntime_instances, cleanup_fn).call(this, () => graph.gate.disconnect());
|
|
3354
|
+
};
|
|
3355
|
+
disposeInstance_fn = function(instance) {
|
|
3356
|
+
__privateMethod(this, _ModuleProjectRuntime_instances, cleanup_fn).call(this, () => instance?.dispose?.());
|
|
3357
|
+
};
|
|
3358
|
+
retire_fn = function(lease) {
|
|
3359
|
+
__privateMethod(this, _ModuleProjectRuntime_instances, cleanup_fn).call(this, () => lease?.release());
|
|
3360
|
+
};
|
|
3361
|
+
cleanup_fn = function(callback) {
|
|
3362
|
+
try {
|
|
3363
|
+
callback();
|
|
3364
|
+
} catch (error) {
|
|
3365
|
+
__privateMethod(this, _ModuleProjectRuntime_instances, report_fn).call(this, error);
|
|
3366
|
+
}
|
|
3367
|
+
};
|
|
3368
|
+
report_fn = function(error) {
|
|
3369
|
+
try {
|
|
3370
|
+
const result = __privateGet(this, _options2).onCleanupError?.(error);
|
|
3371
|
+
if (result && typeof result.then === "function") void Promise.resolve(result).catch(() => {
|
|
3372
|
+
});
|
|
3373
|
+
} catch {
|
|
3374
|
+
}
|
|
3375
|
+
};
|
|
3376
|
+
assertActive_fn = function() {
|
|
3377
|
+
if (__privateGet(this, _disposed3)) throw new Error("ModuleProjectRuntime is disposed.");
|
|
3378
|
+
};
|
|
3379
|
+
requireGraph_fn = function() {
|
|
3380
|
+
__privateMethod(this, _ModuleProjectRuntime_instances, assertActive_fn).call(this);
|
|
3381
|
+
if (!__privateGet(this, _graph)) throw new Error("No module project is active.");
|
|
3382
|
+
return __privateGet(this, _graph);
|
|
3383
|
+
};
|
|
3384
|
+
requireEditable_fn = function() {
|
|
3385
|
+
const graph = __privateMethod(this, _ModuleProjectRuntime_instances, requireGraph_fn).call(this);
|
|
3386
|
+
if (__privateGet(this, _pending) || __privateGet(this, _editing) || __privateGet(this, _committing)) throw new Error("Project edits cannot overlap restore or another edit.");
|
|
3387
|
+
return graph;
|
|
3388
|
+
};
|
|
3389
|
+
assertPending_fn = function(pending) {
|
|
3390
|
+
if (__privateGet(this, _disposed3) || __privateGet(this, _pending) !== pending || pending.generation !== __privateGet(this, _generation)) throw aborted();
|
|
3391
|
+
assertSignal(pending.controller.signal);
|
|
3392
|
+
};
|
|
3393
|
+
uncapturedParameterState_fn = function(graph, moduleId, parameterId, baseValue) {
|
|
3394
|
+
const captured = __privateGet(this, _preparedEdit);
|
|
3395
|
+
if (!captured || captured.graph !== graph) return true;
|
|
3396
|
+
return captured.project.modules.find((record) => record.id === moduleId)?.params?.[parameterId] !== baseValue || captured.project.modulations.some((record) => {
|
|
3397
|
+
const mapping = graph.modulationLeases.get(record.id);
|
|
3398
|
+
return mapping !== void 0 && mapping.depth !== record.depth;
|
|
3399
|
+
}) || captured.project.automation.some((record) => {
|
|
3400
|
+
const automation = graph.automationLeases.get(record.id);
|
|
3401
|
+
return automation !== void 0 && automation.active !== (record.enabled !== false);
|
|
3402
|
+
});
|
|
3403
|
+
};
|
|
3404
|
+
observe_fn = function(graph, affectsSavedState = true) {
|
|
3405
|
+
if (__privateGet(this, _graph) !== graph || graph.disposed) return;
|
|
3406
|
+
if (__privateGet(this, _editing)) {
|
|
3407
|
+
if (affectsSavedState) __privateSet(this, _editObserved, true);
|
|
3408
|
+
} else __privateMethod(this, _ModuleProjectRuntime_instances, changed_fn).call(this);
|
|
3409
|
+
};
|
|
3410
|
+
changed_fn = function() {
|
|
3411
|
+
__privateMethod(this, _ModuleProjectRuntime_instances, notify_fn2).call(this, ++__privateWrapper(this, _revision)._);
|
|
3412
|
+
};
|
|
3413
|
+
notify_fn2 = function(revision, snapshot) {
|
|
3414
|
+
for (const listener of [...__privateGet(this, _listeners4)]) {
|
|
3415
|
+
if (revision !== __privateGet(this, _revision)) break;
|
|
3416
|
+
if (__privateGet(this, _listeners4).has(listener)) __privateMethod(this, _ModuleProjectRuntime_instances, call_fn).call(this, listener, snapshot);
|
|
3417
|
+
}
|
|
3418
|
+
};
|
|
3419
|
+
call_fn = function(listener, snapshot) {
|
|
3420
|
+
try {
|
|
3421
|
+
const result = listener(snapshot ?? this.snapshot());
|
|
3422
|
+
if (result && typeof result.then === "function") void Promise.resolve(result).catch((error) => __privateMethod(this, _ModuleProjectRuntime_instances, report_fn).call(this, error));
|
|
3423
|
+
} catch (error) {
|
|
3424
|
+
__privateMethod(this, _ModuleProjectRuntime_instances, report_fn).call(this, error);
|
|
3425
|
+
}
|
|
3426
|
+
};
|
|
3427
|
+
function keepProjectSessionPreloaded(runtime, options = {}) {
|
|
3428
|
+
const onError = options.onError ?? (() => {
|
|
3429
|
+
});
|
|
3430
|
+
let released = false, scheduled = false;
|
|
3431
|
+
let session2 = runtime.session;
|
|
3432
|
+
let unwatch;
|
|
3433
|
+
let waits = 0;
|
|
3434
|
+
let waiting;
|
|
3435
|
+
const failed = (error, current) => {
|
|
3436
|
+
if (released || runtime.session !== current) return;
|
|
3437
|
+
if (!options.transient?.(error) || waits >= 20) {
|
|
3438
|
+
onError(error);
|
|
3439
|
+
return;
|
|
3440
|
+
}
|
|
3441
|
+
waits += 1;
|
|
3442
|
+
waiting = setTimeout(() => {
|
|
3443
|
+
waiting = void 0;
|
|
3444
|
+
schedule();
|
|
3445
|
+
}, 100);
|
|
3446
|
+
};
|
|
3447
|
+
const run = () => {
|
|
3448
|
+
scheduled = false;
|
|
3449
|
+
const current = runtime.session;
|
|
3450
|
+
if (released || !current) return;
|
|
3451
|
+
let pending;
|
|
3452
|
+
try {
|
|
3453
|
+
pending = current.preload();
|
|
3454
|
+
} catch (error) {
|
|
3455
|
+
failed(error, current);
|
|
3456
|
+
return;
|
|
3457
|
+
}
|
|
3458
|
+
void pending.then(() => {
|
|
3459
|
+
waits = 0;
|
|
3460
|
+
}, (error) => failed(error, current));
|
|
3461
|
+
};
|
|
3462
|
+
const schedule = () => {
|
|
3463
|
+
if (!scheduled && !released) {
|
|
3464
|
+
scheduled = true;
|
|
3465
|
+
queueMicrotask(run);
|
|
3466
|
+
}
|
|
3467
|
+
};
|
|
3468
|
+
const watch = () => {
|
|
3469
|
+
unwatch?.();
|
|
3470
|
+
session2 = runtime.session;
|
|
3471
|
+
unwatch = session2?.subscribe((event) => {
|
|
3472
|
+
if (event.type === "participants") schedule();
|
|
3473
|
+
});
|
|
3474
|
+
if (session2) schedule();
|
|
3475
|
+
};
|
|
3476
|
+
watch();
|
|
3477
|
+
const stop = runtime.subscribe(() => {
|
|
3478
|
+
if (runtime.session !== session2) watch();
|
|
3479
|
+
});
|
|
3480
|
+
return () => {
|
|
3481
|
+
released = true;
|
|
3482
|
+
if (waiting !== void 0) clearTimeout(waiting);
|
|
3483
|
+
stop();
|
|
3484
|
+
unwatch?.();
|
|
3485
|
+
unwatch = void 0;
|
|
3486
|
+
};
|
|
3487
|
+
}
|
|
3488
|
+
|
|
3489
|
+
// src/module/project-editor.ts
|
|
3490
|
+
function createProjectEditorBinding(runtime, options = {}) {
|
|
3491
|
+
const depth = options.modulationDepth ?? 1;
|
|
3492
|
+
if (!Number.isFinite(depth)) throw new RangeError("Editor modulation depth must be finite.");
|
|
3493
|
+
let sequence = 0;
|
|
3494
|
+
const snapshot = () => {
|
|
3495
|
+
const state = runtime.snapshot();
|
|
3496
|
+
const project = state.project;
|
|
3497
|
+
const parameterPorts = /* @__PURE__ */ new Map();
|
|
3498
|
+
const modules = state.modules.map((entry) => {
|
|
3499
|
+
const ports = [...entry.ports];
|
|
3500
|
+
if (entry.status === "unresolved") {
|
|
3501
|
+
const infer = (endpoint2, direction, kind) => {
|
|
3502
|
+
if (endpoint2.moduleId === entry.record.id && !ports.some((port) => port.id === endpoint2.port)) ports.push({ id: endpoint2.port, direction, kind });
|
|
3503
|
+
};
|
|
3504
|
+
for (const connection of project?.connections ?? []) {
|
|
3505
|
+
infer(connection.from, "output", connection.kind);
|
|
3506
|
+
infer(connection.to, "input", connection.kind);
|
|
3507
|
+
}
|
|
3508
|
+
for (const mapping of project?.modulations ?? []) {
|
|
3509
|
+
infer(mapping.source, "output", "control");
|
|
3510
|
+
}
|
|
3511
|
+
}
|
|
3512
|
+
const parameters = /* @__PURE__ */ new Map();
|
|
3513
|
+
for (const handle of runtime.getModule(entry.record.id)?.params ?? []) {
|
|
3514
|
+
if (handle.descriptor.modulation === "audio-rate" || handle.descriptor.modulation === "control-rate") {
|
|
3515
|
+
parameters.set(handle.descriptor.id, handle.descriptor.label ?? handle.descriptor.id);
|
|
3516
|
+
}
|
|
3517
|
+
}
|
|
3518
|
+
const available = new Set(parameters.keys());
|
|
3519
|
+
for (const mapping of project?.modulations ?? []) {
|
|
3520
|
+
if (mapping.target.moduleId === entry.record.id && !parameters.has(mapping.target.param)) parameters.set(mapping.target.param, mapping.target.param);
|
|
3521
|
+
}
|
|
3522
|
+
const occupied = new Set(ports.map((port) => port.id));
|
|
3523
|
+
const reserved = new Set([...parameters.keys()].map(parameterPort));
|
|
3524
|
+
const allocated = /* @__PURE__ */ new Map();
|
|
3525
|
+
parameterPorts.set(entry.record.id, allocated);
|
|
3526
|
+
for (const [parameterId, label] of [...parameters].sort(([a], [b]) => a < b ? -1 : a > b ? 1 : 0)) {
|
|
3527
|
+
const base = parameterPort(parameterId);
|
|
3528
|
+
let id = base, suffix = 0;
|
|
3529
|
+
while (occupied.has(id)) {
|
|
3530
|
+
do {
|
|
3531
|
+
id = `${base}#${++suffix}`;
|
|
3532
|
+
} while (reserved.has(id));
|
|
3533
|
+
}
|
|
3534
|
+
occupied.add(id);
|
|
3535
|
+
allocated.set(parameterId, id);
|
|
3536
|
+
if (available.has(parameterId) || entry.status === "unresolved") ports.push({ id, parameterId, label, direction: "input", kind: "control" });
|
|
3537
|
+
}
|
|
3538
|
+
const position = project?.layout.modules.find((layout2) => layout2.moduleId === entry.record.id);
|
|
3539
|
+
const size = position?.width !== void 0 && position.height !== void 0 ? { width: position.width, height: position.height } : void 0;
|
|
3540
|
+
return { id: entry.record.id, contextId: entry.record.contextId, label: entry.record.label ?? entry.record.id, ports, position, ...size ? { size } : {}, status: entry.status, diagnostic: entry.diagnostic };
|
|
3541
|
+
});
|
|
3542
|
+
const diagnostic = (kind, id) => state.unresolved.find((item) => item.kind === kind && item.id === id)?.reason;
|
|
3543
|
+
const connections = (project?.connections ?? []).map((record) => ({
|
|
3544
|
+
id: connectionId(record.id),
|
|
3545
|
+
recordId: record.id,
|
|
3546
|
+
from: record.from,
|
|
3547
|
+
to: record.to,
|
|
3548
|
+
mode: record.mode ?? (record.kind === "audio" ? "replace" : "connect"),
|
|
3549
|
+
status: diagnostic("connection", record.id) ? "unresolved" : "connected",
|
|
3550
|
+
diagnostic: diagnostic("connection", record.id)
|
|
3551
|
+
}));
|
|
3552
|
+
for (const record of project?.modulations ?? []) connections.push({
|
|
3553
|
+
id: modulationId(record.id),
|
|
3554
|
+
from: record.source,
|
|
3555
|
+
to: {
|
|
3556
|
+
moduleId: record.target.moduleId,
|
|
3557
|
+
port: parameterPorts.get(record.target.moduleId)?.get(record.target.param) ?? parameterPort(record.target.param)
|
|
3558
|
+
},
|
|
3559
|
+
mode: "connect",
|
|
3560
|
+
status: diagnostic("modulation", record.id) ? "unresolved" : "connected",
|
|
3561
|
+
diagnostic: diagnostic("modulation", record.id)
|
|
3562
|
+
});
|
|
3563
|
+
for (const entry of state.modules) {
|
|
3564
|
+
const owner = state.modules.find((module) => module.record.id === entry.defaultOwnerModuleId);
|
|
3565
|
+
if (!owner || owner.status !== "ready") continue;
|
|
3566
|
+
const inputs = owner.ports.filter((port) => port.kind === "audio" && port.direction === "input");
|
|
3567
|
+
if (inputs.length !== 1) continue;
|
|
3568
|
+
const record = entry.record;
|
|
3569
|
+
for (const port of entry.ports) {
|
|
3570
|
+
if (port.kind !== "audio" || port.direction !== "output") continue;
|
|
3571
|
+
const live = runtime.getModule(record.id)?.getPort?.(port.id);
|
|
3572
|
+
if (!live?.defaultDestination || live.replaced) continue;
|
|
3573
|
+
connections.push({
|
|
3574
|
+
id: `default:${JSON.stringify([record.id, port.id])}`,
|
|
3575
|
+
from: { moduleId: record.id, port: port.id },
|
|
3576
|
+
to: { moduleId: owner.record.id, port: inputs[0].id },
|
|
3577
|
+
mode: "default",
|
|
3578
|
+
status: "connected",
|
|
3579
|
+
editable: false
|
|
3580
|
+
});
|
|
3581
|
+
}
|
|
3582
|
+
}
|
|
3583
|
+
return { modules, connections, disabled: state.disposed || state.restoring || !project };
|
|
3584
|
+
};
|
|
3585
|
+
return Object.freeze({
|
|
3586
|
+
snapshot,
|
|
3587
|
+
async connect(request) {
|
|
3588
|
+
const state = snapshot();
|
|
3589
|
+
if (state.disabled) throw new Error("The project is not editable.");
|
|
3590
|
+
const fromModule = state.modules.find((module) => module.id === request.from.moduleId);
|
|
3591
|
+
const toModule = state.modules.find((module) => module.id === request.to.moduleId);
|
|
3592
|
+
if (fromModule?.status !== "ready" || toModule?.status !== "ready") throw new Error("The selected module is unavailable.");
|
|
3593
|
+
const source = fromModule.ports.find((port) => port.id === request.from.port);
|
|
3594
|
+
const target = toModule.ports.find((port) => port.id === request.to.port);
|
|
3595
|
+
if (!source || !target || source.direction !== "output" || target.direction !== "input" || source.kind !== target.kind) throw new Error("Choose compatible output and input ports.");
|
|
3596
|
+
const project = runtime.exportProject();
|
|
3597
|
+
let id;
|
|
3598
|
+
do {
|
|
3599
|
+
id = `patch-${++sequence}`;
|
|
3600
|
+
} while ([...project.connections, ...project.modulations].some((record) => record.id === id));
|
|
3601
|
+
if (source.kind !== "audio" && request.mode !== "connect") throw new Error("This signal requires connect mode.");
|
|
3602
|
+
if (source.kind === "control" && target.parameterId !== void 0) {
|
|
3603
|
+
if (request.mode !== "connect") throw new Error("A parameter mapping uses connect mode.");
|
|
3604
|
+
await runtime.connectModulation({ id, source: request.from, target: { moduleId: request.to.moduleId, param: target.parameterId }, depth });
|
|
3605
|
+
} else {
|
|
3606
|
+
if (source.kind === "audio" && request.mode === "connect") throw new Error("Audio patches require replace, send or tap mode.");
|
|
3607
|
+
await runtime.connect({ id, kind: source.kind, from: request.from, to: request.to, ...request.mode !== "connect" && { mode: request.mode } });
|
|
3608
|
+
}
|
|
3609
|
+
},
|
|
3610
|
+
disconnect(id) {
|
|
3611
|
+
if (id.startsWith("connection:")) runtime.disconnect(id.slice(11));
|
|
3612
|
+
else if (id.startsWith("modulation:")) runtime.disconnectModulation(id.slice(11));
|
|
3613
|
+
else throw new Error("Default routes cannot be removed by the editor.");
|
|
3614
|
+
},
|
|
3615
|
+
moveModule(id, position) {
|
|
3616
|
+
const project = runtime.exportProject();
|
|
3617
|
+
if (!project.modules.some((module) => module.id === id)) throw new Error(`Unknown module ${id}.`);
|
|
3618
|
+
const previous = project.layout.modules.find((layout2) => layout2.moduleId === id);
|
|
3619
|
+
runtime.updateLayout({ modules: [...project.layout.modules.filter((layout2) => layout2.moduleId !== id), { ...previous, moduleId: id, ...position }] });
|
|
3620
|
+
},
|
|
3621
|
+
subscribe: (notify) => runtime.subscribe(notify)
|
|
3622
|
+
});
|
|
3623
|
+
}
|
|
3624
|
+
var parameterPort = (id) => `param:${id}`;
|
|
3625
|
+
var connectionId = (id) => `connection:${id}`;
|
|
3626
|
+
var modulationId = (id) => `modulation:${id}`;
|
|
3627
|
+
|
|
3628
|
+
// src/module/rack.ts
|
|
3629
|
+
var nextRackId = 0;
|
|
3630
|
+
var unit = (value) => Math.max(0, Math.min(1, Number.isFinite(value) ? value : 0));
|
|
3631
|
+
var balance = (value) => Math.max(-1, Math.min(1, Number.isFinite(value) ? value : 0));
|
|
3632
|
+
var _upstream, _registry, _registration, _channels, _admitting, _listeners5, _onError, _masterLevel, _params, _control, _master, _chain, _port3, _route, _recipe, _ports2, _disposed4, _preparing, _RackModule_instances, edit_fn, applyMix_fn, refreshPorts_fn, notify_fn3, assertActive_fn2;
|
|
3633
|
+
var RackModule = class {
|
|
3634
|
+
constructor(upstream, options = {}) {
|
|
3635
|
+
__privateAdd(this, _RackModule_instances);
|
|
3636
|
+
__privateAdd(this, _upstream);
|
|
3637
|
+
__privateAdd(this, _registry, new ModuleRegistry());
|
|
3638
|
+
__privateAdd(this, _registration);
|
|
3639
|
+
__privateAdd(this, _channels, /* @__PURE__ */ new Map());
|
|
3640
|
+
__privateAdd(this, _admitting, /* @__PURE__ */ new Set());
|
|
3641
|
+
__privateAdd(this, _listeners5, /* @__PURE__ */ new Set());
|
|
3642
|
+
__privateAdd(this, _onError);
|
|
3643
|
+
__privateAdd(this, _masterLevel);
|
|
3644
|
+
__privateAdd(this, _params);
|
|
3645
|
+
__privateAdd(this, _control);
|
|
3646
|
+
__privateAdd(this, _master);
|
|
3647
|
+
__privateAdd(this, _chain);
|
|
3648
|
+
__privateAdd(this, _port3);
|
|
3649
|
+
__privateAdd(this, _route);
|
|
3650
|
+
__privateAdd(this, _recipe);
|
|
3651
|
+
__privateAdd(this, _ports2, Object.freeze([]));
|
|
3652
|
+
__privateAdd(this, _disposed4, false);
|
|
3653
|
+
__privateAdd(this, _preparing, false);
|
|
3654
|
+
this.id = options.id ?? `rack-${++nextRackId}`;
|
|
3655
|
+
__privateSet(this, _upstream, upstream);
|
|
3656
|
+
__privateSet(this, _recipe, options.effect);
|
|
3657
|
+
__privateSet(this, _onError, options.onError);
|
|
3658
|
+
__privateSet(this, _masterLevel, new ParamHandle(
|
|
3659
|
+
{ id: "master-volume", label: "Master volume", unit: "gain", range: { min: 0, max: 1 }, defaultValue: 1 },
|
|
3660
|
+
{ apply: (value) => {
|
|
3661
|
+
if (__privateGet(this, _master)) __privateGet(this, _master).gain.value = value;
|
|
3662
|
+
} }
|
|
3663
|
+
));
|
|
3664
|
+
__privateSet(this, _params, new ParamSurface([__privateGet(this, _masterLevel)]));
|
|
3665
|
+
__privateSet(this, _control, createControlPort(__privateGet(this, _params)));
|
|
3666
|
+
this.context = upstream.derive({
|
|
3667
|
+
id: `${this.id}:scope`,
|
|
3668
|
+
registry: __privateGet(this, _registry),
|
|
3669
|
+
target: this,
|
|
3670
|
+
admit: (module, port) => this.admit(module, port)
|
|
3671
|
+
});
|
|
3672
|
+
__privateMethod(this, _RackModule_instances, refreshPorts_fn).call(this);
|
|
3673
|
+
__privateSet(this, _registration, upstream.registry.register(this));
|
|
3674
|
+
}
|
|
3675
|
+
get disposed() {
|
|
3676
|
+
return __privateGet(this, _disposed4);
|
|
3677
|
+
}
|
|
3678
|
+
get params() {
|
|
3679
|
+
return __privateGet(this, _params);
|
|
3680
|
+
}
|
|
3681
|
+
get inserts() {
|
|
3682
|
+
return __privateGet(this, _chain);
|
|
3683
|
+
}
|
|
3684
|
+
/** Prepared master input for explicit audio patching; ownership stays with the Rack. */
|
|
3685
|
+
get input() {
|
|
3686
|
+
return __privateGet(this, _master);
|
|
3687
|
+
}
|
|
3688
|
+
get effect() {
|
|
3689
|
+
return __privateGet(this, _recipe);
|
|
3690
|
+
}
|
|
3691
|
+
get masterVolume() {
|
|
3692
|
+
return __privateGet(this, _masterLevel).snapshot.baseValue;
|
|
3693
|
+
}
|
|
3694
|
+
setEffect(effect) {
|
|
3695
|
+
__privateMethod(this, _RackModule_instances, assertActive_fn2).call(this);
|
|
3696
|
+
if (__privateGet(this, _preparing)) throw new Error("Effects cannot change during Rack construction.");
|
|
3697
|
+
__privateGet(this, _chain)?.setCompatibility(effect);
|
|
3698
|
+
__privateSet(this, _recipe, effect);
|
|
3699
|
+
__privateMethod(this, _RackModule_instances, notify_fn3).call(this);
|
|
3700
|
+
}
|
|
3701
|
+
setMasterVolume(value) {
|
|
3702
|
+
__privateGet(this, _masterLevel).setBaseValue(unit(value));
|
|
3703
|
+
__privateMethod(this, _RackModule_instances, notify_fn3).call(this);
|
|
3704
|
+
}
|
|
3705
|
+
list() {
|
|
3706
|
+
return Object.freeze([...__privateGet(this, _channels).values()].map(({ id, module, volume, pan, muted, solo }) => Object.freeze({ id, module, volume, pan, muted, solo })));
|
|
3707
|
+
}
|
|
3708
|
+
channel(id) {
|
|
3709
|
+
return this.list().find((channel) => channel.id === id);
|
|
3710
|
+
}
|
|
3711
|
+
/** Stable post-fader inserts owned by this Rack channel. */
|
|
3712
|
+
channelInserts(id) {
|
|
3713
|
+
return __privateGet(this, _channels).get(id)?.inserts;
|
|
3714
|
+
}
|
|
3715
|
+
setVolume(id, value) {
|
|
3716
|
+
__privateMethod(this, _RackModule_instances, edit_fn).call(this, id, (channel) => {
|
|
3717
|
+
channel.volume = unit(value);
|
|
3718
|
+
});
|
|
3719
|
+
}
|
|
3720
|
+
setPan(id, value) {
|
|
3721
|
+
__privateMethod(this, _RackModule_instances, edit_fn).call(this, id, (channel) => {
|
|
3722
|
+
channel.pan = balance(value);
|
|
3723
|
+
if (channel.panner) channel.panner.pan.value = channel.pan;
|
|
3724
|
+
});
|
|
3725
|
+
}
|
|
3726
|
+
mute(id, muted) {
|
|
3727
|
+
__privateMethod(this, _RackModule_instances, edit_fn).call(this, id, (channel) => {
|
|
3728
|
+
channel.muted = muted;
|
|
3729
|
+
});
|
|
3730
|
+
}
|
|
3731
|
+
solo(id, solo) {
|
|
3732
|
+
__privateMethod(this, _RackModule_instances, edit_fn).call(this, id, (channel) => {
|
|
3733
|
+
channel.solo = solo;
|
|
3734
|
+
});
|
|
3735
|
+
}
|
|
3736
|
+
/** `master` (aliases `output`, `main`) or `channel:<module id>` for a post-insert channel tap. */
|
|
3737
|
+
getPort(name) {
|
|
3738
|
+
if (name === "master" || name === "output" || name === "main") return __privateGet(this, _port3);
|
|
3739
|
+
return name.startsWith("channel:") ? __privateGet(this, _channels).get(name.slice("channel:".length))?.output : void 0;
|
|
3740
|
+
}
|
|
3741
|
+
ports() {
|
|
3742
|
+
return __privateGet(this, _ports2);
|
|
3743
|
+
}
|
|
3744
|
+
prepare() {
|
|
3745
|
+
__privateMethod(this, _RackModule_instances, assertActive_fn2).call(this);
|
|
3746
|
+
if (__privateGet(this, _chain)) return;
|
|
3747
|
+
if (__privateGet(this, _preparing)) throw new Error("Rack preparation cannot reenter audio graph construction.");
|
|
3748
|
+
const audio = __privateGet(this, _upstream).audio.prepare();
|
|
3749
|
+
const master = audio.createGain();
|
|
3750
|
+
__privateSet(this, _preparing, true);
|
|
3751
|
+
let chain;
|
|
3752
|
+
let port;
|
|
3753
|
+
let route;
|
|
3754
|
+
try {
|
|
3755
|
+
master.gain.value = this.masterVolume;
|
|
3756
|
+
chain = new InsertChain(audio);
|
|
3757
|
+
chain.setCompatibility(__privateGet(this, _recipe));
|
|
3758
|
+
master.connect(chain.input);
|
|
3759
|
+
port = new AudioOutputPort(chain.output, { id: "master" });
|
|
3760
|
+
route = routeModuleOutput(__privateGet(this, _upstream), this, port);
|
|
3761
|
+
__privateMethod(this, _RackModule_instances, assertActive_fn2).call(this);
|
|
3762
|
+
if (!route.active) throw new Error("Rack admission was cancelled.");
|
|
3763
|
+
__privateSet(this, _master, master);
|
|
3764
|
+
__privateSet(this, _chain, chain);
|
|
3765
|
+
__privateSet(this, _port3, port);
|
|
3766
|
+
__privateSet(this, _route, route);
|
|
3767
|
+
__privateMethod(this, _RackModule_instances, refreshPorts_fn).call(this);
|
|
3768
|
+
__privateMethod(this, _RackModule_instances, notify_fn3).call(this);
|
|
3769
|
+
} catch (error) {
|
|
3770
|
+
try {
|
|
3771
|
+
route?.release();
|
|
3772
|
+
} catch {
|
|
3773
|
+
}
|
|
3774
|
+
try {
|
|
3775
|
+
port?.dispose();
|
|
3776
|
+
} catch {
|
|
3777
|
+
}
|
|
3778
|
+
try {
|
|
3779
|
+
chain?.dispose();
|
|
3780
|
+
} catch {
|
|
3781
|
+
}
|
|
3782
|
+
try {
|
|
3783
|
+
master.disconnect();
|
|
3784
|
+
} catch {
|
|
3785
|
+
}
|
|
3786
|
+
throw error;
|
|
3787
|
+
} finally {
|
|
3788
|
+
__privateSet(this, _preparing, false);
|
|
3789
|
+
}
|
|
3790
|
+
}
|
|
3791
|
+
/**
|
|
3792
|
+
* Follow a republished upstream scope in place (DEC-065): a destination-only
|
|
3793
|
+
* change re-routes the prepared master and keeps every channel, level and
|
|
3794
|
+
* member. Returns false when the change needs a rebind.
|
|
3795
|
+
*/
|
|
3796
|
+
retarget(next) {
|
|
3797
|
+
__privateMethod(this, _RackModule_instances, assertActive_fn2).call(this);
|
|
3798
|
+
const change = moduleScopeChange(__privateGet(this, _upstream), next);
|
|
3799
|
+
if (change === "scope") return false;
|
|
3800
|
+
__privateSet(this, _upstream, next);
|
|
3801
|
+
if (change === "destination" && __privateGet(this, _route) && __privateGet(this, _port3)) {
|
|
3802
|
+
const previous = __privateGet(this, _route);
|
|
3803
|
+
__privateSet(this, _route, void 0);
|
|
3804
|
+
previous.release();
|
|
3805
|
+
__privateSet(this, _route, routeModuleOutput(next, this, __privateGet(this, _port3)));
|
|
3806
|
+
__privateMethod(this, _RackModule_instances, notify_fn3).call(this);
|
|
3807
|
+
}
|
|
3808
|
+
return true;
|
|
3809
|
+
}
|
|
3810
|
+
/** Give a module's output its own channel. The lease removes only that channel. */
|
|
3811
|
+
admit(module, port) {
|
|
3812
|
+
__privateMethod(this, _RackModule_instances, assertActive_fn2).call(this);
|
|
3813
|
+
if (module === this) throw new Error("A Rack cannot admit its own output.");
|
|
3814
|
+
const assertAvailable = () => {
|
|
3815
|
+
if (__privateGet(this, _channels).has(module.id) || __privateGet(this, _admitting).has(module.id)) throw new Error(`Module ${module.id} already has a Rack channel.`);
|
|
3816
|
+
};
|
|
3817
|
+
assertAvailable();
|
|
3818
|
+
this.prepare();
|
|
3819
|
+
__privateMethod(this, _RackModule_instances, assertActive_fn2).call(this);
|
|
3820
|
+
assertAvailable();
|
|
3821
|
+
const master = __privateGet(this, _master);
|
|
3822
|
+
if (port.context !== master.context) throw new Error("Rack admission requires the same AudioContext.");
|
|
3823
|
+
const previousDestination = port.defaultDestination;
|
|
3824
|
+
__privateGet(this, _admitting).add(module.id);
|
|
3825
|
+
let fader;
|
|
3826
|
+
let panner;
|
|
3827
|
+
let inserts;
|
|
3828
|
+
let output;
|
|
3829
|
+
let channel;
|
|
3830
|
+
try {
|
|
3831
|
+
const context = master.context;
|
|
3832
|
+
fader = context.createGain();
|
|
3833
|
+
panner = typeof context.createStereoPanner === "function" ? context.createStereoPanner() : void 0;
|
|
3834
|
+
inserts = new InsertChain(context, { onCleanupError: __privateGet(this, _onError) });
|
|
3835
|
+
if (panner) {
|
|
3836
|
+
fader.connect(panner);
|
|
3837
|
+
panner.connect(inserts.input);
|
|
3838
|
+
} else fader.connect(inserts.input);
|
|
3839
|
+
output = new AudioOutputPort(inserts.output, { id: `channel:${module.id}`, defaultDestination: master, onCleanupError: __privateGet(this, _onError) });
|
|
3840
|
+
port.setDefault(fader);
|
|
3841
|
+
__privateMethod(this, _RackModule_instances, assertActive_fn2).call(this);
|
|
3842
|
+
const channelFader = fader;
|
|
3843
|
+
const channelPanner = panner;
|
|
3844
|
+
const channelInserts = inserts;
|
|
3845
|
+
const channelOutput = output;
|
|
3846
|
+
channel = {
|
|
3847
|
+
id: module.id,
|
|
3848
|
+
module,
|
|
3849
|
+
fader,
|
|
3850
|
+
panner,
|
|
3851
|
+
inserts,
|
|
3852
|
+
output,
|
|
3853
|
+
volume: 1,
|
|
3854
|
+
pan: 0,
|
|
3855
|
+
muted: false,
|
|
3856
|
+
solo: false,
|
|
3857
|
+
lease: createConnectionLease(() => {
|
|
3858
|
+
if (__privateGet(this, _channels).get(module.id) === channel) __privateGet(this, _channels).delete(module.id);
|
|
3859
|
+
let failure;
|
|
3860
|
+
const clean = (callback) => {
|
|
3861
|
+
try {
|
|
3862
|
+
callback();
|
|
3863
|
+
} catch (error) {
|
|
3864
|
+
failure ?? (failure = error);
|
|
3865
|
+
}
|
|
3866
|
+
};
|
|
3867
|
+
clean(() => {
|
|
3868
|
+
if (!port.disposed && port.defaultDestination === channelFader) port.setDefault(void 0);
|
|
3869
|
+
});
|
|
3870
|
+
clean(() => channelOutput.dispose());
|
|
3871
|
+
clean(() => channelFader.disconnect(channelPanner ?? channelInserts.input));
|
|
3872
|
+
clean(() => channelPanner?.disconnect(channelInserts.input));
|
|
3873
|
+
clean(() => channelInserts.dispose());
|
|
3874
|
+
__privateMethod(this, _RackModule_instances, refreshPorts_fn).call(this);
|
|
3875
|
+
__privateMethod(this, _RackModule_instances, applyMix_fn).call(this);
|
|
3876
|
+
if (failure !== void 0) throw failure;
|
|
3877
|
+
})
|
|
3878
|
+
};
|
|
3879
|
+
__privateGet(this, _channels).set(module.id, channel);
|
|
3880
|
+
} catch (error) {
|
|
3881
|
+
try {
|
|
3882
|
+
if (fader && !port.disposed && port.defaultDestination === fader) {
|
|
3883
|
+
port.setDefault(__privateGet(this, _disposed4) ? void 0 : previousDestination);
|
|
3884
|
+
}
|
|
3885
|
+
} catch {
|
|
3886
|
+
}
|
|
3887
|
+
try {
|
|
3888
|
+
output?.dispose();
|
|
3889
|
+
} catch {
|
|
3890
|
+
}
|
|
3891
|
+
try {
|
|
3892
|
+
inserts?.dispose();
|
|
3893
|
+
} catch {
|
|
3894
|
+
}
|
|
3895
|
+
try {
|
|
3896
|
+
if (panner && inserts) panner.disconnect(inserts.input);
|
|
3897
|
+
} catch {
|
|
3898
|
+
}
|
|
3899
|
+
try {
|
|
3900
|
+
if (fader && inserts) fader.disconnect(panner ?? inserts.input);
|
|
3901
|
+
} catch {
|
|
3902
|
+
}
|
|
3903
|
+
throw error;
|
|
3904
|
+
} finally {
|
|
3905
|
+
__privateGet(this, _admitting).delete(module.id);
|
|
3906
|
+
}
|
|
3907
|
+
__privateMethod(this, _RackModule_instances, refreshPorts_fn).call(this);
|
|
3908
|
+
__privateMethod(this, _RackModule_instances, applyMix_fn).call(this);
|
|
3909
|
+
return channel.lease;
|
|
3910
|
+
}
|
|
3911
|
+
subscribe(listener) {
|
|
3912
|
+
__privateGet(this, _listeners5).add(listener);
|
|
3913
|
+
return () => {
|
|
3914
|
+
__privateGet(this, _listeners5).delete(listener);
|
|
3915
|
+
};
|
|
3916
|
+
}
|
|
3917
|
+
dispose() {
|
|
3918
|
+
if (__privateGet(this, _disposed4)) return;
|
|
3919
|
+
__privateSet(this, _disposed4, true);
|
|
3920
|
+
let failure;
|
|
3921
|
+
const clean = (callback) => {
|
|
3922
|
+
try {
|
|
3923
|
+
callback();
|
|
3924
|
+
} catch (error) {
|
|
3925
|
+
failure ?? (failure = error);
|
|
3926
|
+
}
|
|
3927
|
+
};
|
|
3928
|
+
for (const channel of [...__privateGet(this, _channels).values()]) clean(() => channel.lease.release());
|
|
3929
|
+
clean(() => __privateGet(this, _route)?.release());
|
|
3930
|
+
clean(() => __privateGet(this, _port3)?.dispose());
|
|
3931
|
+
clean(() => __privateGet(this, _chain)?.dispose());
|
|
3932
|
+
clean(() => __privateGet(this, _master)?.disconnect());
|
|
3933
|
+
clean(() => __privateGet(this, _masterLevel).dispose());
|
|
3934
|
+
clean(() => __privateGet(this, _registration).release());
|
|
3935
|
+
clean(() => __privateGet(this, _registry).dispose());
|
|
3936
|
+
__privateGet(this, _listeners5).clear();
|
|
3937
|
+
if (failure !== void 0) throw failure;
|
|
3938
|
+
}
|
|
3939
|
+
};
|
|
3940
|
+
_upstream = new WeakMap();
|
|
3941
|
+
_registry = new WeakMap();
|
|
3942
|
+
_registration = new WeakMap();
|
|
3943
|
+
_channels = new WeakMap();
|
|
3944
|
+
_admitting = new WeakMap();
|
|
3945
|
+
_listeners5 = new WeakMap();
|
|
3946
|
+
_onError = new WeakMap();
|
|
3947
|
+
_masterLevel = new WeakMap();
|
|
3948
|
+
_params = new WeakMap();
|
|
3949
|
+
_control = new WeakMap();
|
|
3950
|
+
_master = new WeakMap();
|
|
3951
|
+
_chain = new WeakMap();
|
|
3952
|
+
_port3 = new WeakMap();
|
|
3953
|
+
_route = new WeakMap();
|
|
3954
|
+
_recipe = new WeakMap();
|
|
3955
|
+
_ports2 = new WeakMap();
|
|
3956
|
+
_disposed4 = new WeakMap();
|
|
3957
|
+
_preparing = new WeakMap();
|
|
3958
|
+
_RackModule_instances = new WeakSet();
|
|
3959
|
+
edit_fn = function(id, change) {
|
|
3960
|
+
const channel = __privateGet(this, _channels).get(id);
|
|
3961
|
+
if (!channel) return;
|
|
3962
|
+
change(channel);
|
|
3963
|
+
__privateMethod(this, _RackModule_instances, applyMix_fn).call(this);
|
|
3964
|
+
};
|
|
3965
|
+
applyMix_fn = function() {
|
|
3966
|
+
const solo = [...__privateGet(this, _channels).values()].some((channel) => channel.solo);
|
|
3967
|
+
for (const channel of __privateGet(this, _channels).values()) {
|
|
3968
|
+
channel.fader.gain.value = channel.muted || solo && !channel.solo ? 0 : channel.volume;
|
|
3969
|
+
}
|
|
3970
|
+
__privateMethod(this, _RackModule_instances, notify_fn3).call(this);
|
|
3971
|
+
};
|
|
3972
|
+
refreshPorts_fn = function() {
|
|
3973
|
+
const ports = [__privateGet(this, _control)];
|
|
3974
|
+
if (__privateGet(this, _port3)) ports.push(__privateGet(this, _port3));
|
|
3975
|
+
for (const channel of __privateGet(this, _channels).values()) ports.push(channel.output);
|
|
3976
|
+
__privateSet(this, _ports2, Object.freeze(ports));
|
|
3977
|
+
};
|
|
3978
|
+
notify_fn3 = function() {
|
|
3979
|
+
var _a;
|
|
3980
|
+
for (const listener of [...__privateGet(this, _listeners5)]) {
|
|
3981
|
+
if (__privateGet(this, _disposed4)) break;
|
|
3982
|
+
try {
|
|
3983
|
+
listener();
|
|
3984
|
+
} catch (error) {
|
|
3985
|
+
try {
|
|
3986
|
+
(_a = __privateGet(this, _onError)) == null ? void 0 : _a.call(this, error);
|
|
3987
|
+
} catch {
|
|
3988
|
+
}
|
|
3989
|
+
}
|
|
3990
|
+
}
|
|
3991
|
+
};
|
|
3992
|
+
assertActive_fn2 = function() {
|
|
3993
|
+
if (__privateGet(this, _disposed4)) throw new Error("Rack module has been disposed.");
|
|
3994
|
+
};
|
|
3995
|
+
|
|
3996
|
+
// src/module/provider-scope.ts
|
|
3997
|
+
function createProviderScope(options) {
|
|
3998
|
+
const { parent } = options;
|
|
3999
|
+
const ownedAudio = parent ? void 0 : new ModuleAudioEnvironment(options.audio);
|
|
4000
|
+
const audio = parent?.audio ?? ownedAudio;
|
|
4001
|
+
const inherited = options.local ? void 0 : parent?.session;
|
|
4002
|
+
const ownedSession = inherited ? void 0 : new SessionTransport(options.session?.now ?? (() => audio.peek()?.currentTime ?? 0), options.session);
|
|
4003
|
+
const session2 = inherited ?? ownedSession;
|
|
4004
|
+
let context;
|
|
4005
|
+
try {
|
|
4006
|
+
const relation = {
|
|
4007
|
+
id: options.id,
|
|
4008
|
+
session: session2,
|
|
4009
|
+
registry: options.registry,
|
|
4010
|
+
...options.admit !== void 0 ? { admit: options.admit } : {},
|
|
4011
|
+
...options.target !== void 0 ? { target: options.target } : {}
|
|
4012
|
+
};
|
|
4013
|
+
context = parent ? parent.derive(relation) : new ModuleContext({
|
|
4014
|
+
...relation,
|
|
4015
|
+
audio,
|
|
4016
|
+
destination: options.destination,
|
|
4017
|
+
admit: options.admit ?? void 0,
|
|
4018
|
+
target: options.target ?? void 0
|
|
4019
|
+
});
|
|
4020
|
+
} catch (error) {
|
|
4021
|
+
ownedSession?.dispose();
|
|
4022
|
+
ownedAudio?.dispose();
|
|
4023
|
+
throw error;
|
|
4024
|
+
}
|
|
4025
|
+
let disposed = false;
|
|
4026
|
+
return Object.freeze({
|
|
4027
|
+
context,
|
|
4028
|
+
session: session2,
|
|
4029
|
+
ownedSession,
|
|
4030
|
+
ownedAudio,
|
|
4031
|
+
dispose() {
|
|
4032
|
+
if (disposed) return;
|
|
4033
|
+
disposed = true;
|
|
4034
|
+
let failure;
|
|
4035
|
+
try {
|
|
4036
|
+
ownedSession?.dispose();
|
|
4037
|
+
} catch (error) {
|
|
4038
|
+
failure = error;
|
|
4039
|
+
}
|
|
4040
|
+
try {
|
|
4041
|
+
ownedAudio?.dispose();
|
|
4042
|
+
} catch (error) {
|
|
4043
|
+
failure ?? (failure = error);
|
|
4044
|
+
}
|
|
4045
|
+
if (failure !== void 0) throw failure;
|
|
4046
|
+
}
|
|
4047
|
+
});
|
|
4048
|
+
}
|
|
4049
|
+
|
|
4050
|
+
// src/module/sinks.ts
|
|
4051
|
+
var _input, _ports3, _listeners6, _disposed5;
|
|
4052
|
+
var AudioSink = class {
|
|
4053
|
+
constructor(environment, id) {
|
|
4054
|
+
__privateAdd(this, _input);
|
|
4055
|
+
__privateAdd(this, _ports3, Object.freeze([]));
|
|
4056
|
+
__privateAdd(this, _listeners6, /* @__PURE__ */ new Set());
|
|
4057
|
+
__privateAdd(this, _disposed5, false);
|
|
4058
|
+
assertModuleId(id);
|
|
4059
|
+
this.id = id;
|
|
4060
|
+
this.environment = environment;
|
|
4061
|
+
}
|
|
4062
|
+
get disposed() {
|
|
4063
|
+
return __privateGet(this, _disposed5);
|
|
4064
|
+
}
|
|
4065
|
+
/** Present after {@link prepare}; producers connect to it. */
|
|
4066
|
+
get input() {
|
|
4067
|
+
return __privateGet(this, _input);
|
|
4068
|
+
}
|
|
4069
|
+
ports() {
|
|
4070
|
+
return __privateGet(this, _ports3);
|
|
4071
|
+
}
|
|
4072
|
+
prepare() {
|
|
4073
|
+
if (__privateGet(this, _disposed5)) throw new Error(`${this.constructor.name} is disposed`);
|
|
4074
|
+
if (__privateGet(this, _input)) return;
|
|
4075
|
+
const context = this.environment.prepare();
|
|
4076
|
+
__privateSet(this, _input, new AudioInputPort(this.createInput(context), { id: "input" }));
|
|
4077
|
+
__privateSet(this, _ports3, Object.freeze([__privateGet(this, _input)]));
|
|
4078
|
+
this.notify();
|
|
4079
|
+
}
|
|
4080
|
+
subscribe(listener) {
|
|
4081
|
+
__privateGet(this, _listeners6).add(listener);
|
|
4082
|
+
return () => {
|
|
4083
|
+
__privateGet(this, _listeners6).delete(listener);
|
|
4084
|
+
};
|
|
4085
|
+
}
|
|
4086
|
+
dispose() {
|
|
4087
|
+
if (__privateGet(this, _disposed5)) return;
|
|
4088
|
+
__privateSet(this, _disposed5, true);
|
|
4089
|
+
let failure;
|
|
4090
|
+
try {
|
|
4091
|
+
__privateGet(this, _input)?.dispose();
|
|
4092
|
+
} catch (error) {
|
|
4093
|
+
failure = error;
|
|
4094
|
+
}
|
|
4095
|
+
try {
|
|
4096
|
+
this.release();
|
|
4097
|
+
} catch (error) {
|
|
4098
|
+
failure ?? (failure = error);
|
|
4099
|
+
}
|
|
4100
|
+
this.notify();
|
|
4101
|
+
__privateGet(this, _listeners6).clear();
|
|
4102
|
+
if (failure !== void 0) throw failure;
|
|
4103
|
+
}
|
|
4104
|
+
release() {
|
|
4105
|
+
}
|
|
4106
|
+
notify() {
|
|
4107
|
+
for (const listener of [...__privateGet(this, _listeners6)]) {
|
|
4108
|
+
try {
|
|
4109
|
+
listener();
|
|
4110
|
+
} catch {
|
|
4111
|
+
}
|
|
4112
|
+
}
|
|
4113
|
+
}
|
|
4114
|
+
};
|
|
4115
|
+
_input = new WeakMap();
|
|
4116
|
+
_ports3 = new WeakMap();
|
|
4117
|
+
_listeners6 = new WeakMap();
|
|
4118
|
+
_disposed5 = new WeakMap();
|
|
4119
|
+
var DeviceSink = class extends AudioSink {
|
|
4120
|
+
constructor(environment, options = {}) {
|
|
4121
|
+
super(environment, options.id ?? "device");
|
|
4122
|
+
}
|
|
4123
|
+
get supportsDeviceSelection() {
|
|
4124
|
+
const context = this.environment.peek();
|
|
4125
|
+
return typeof context?.setSinkId === "function";
|
|
4126
|
+
}
|
|
4127
|
+
/** Route the whole environment to another output device; rejects where unsupported. */
|
|
4128
|
+
async selectDevice(deviceId) {
|
|
4129
|
+
this.prepare();
|
|
4130
|
+
const context = this.environment.peek();
|
|
4131
|
+
if (typeof context.setSinkId !== "function") throw new Error("Output device selection is unavailable in this environment");
|
|
4132
|
+
await context.setSinkId(deviceId);
|
|
4133
|
+
this.notify();
|
|
4134
|
+
}
|
|
4135
|
+
createInput(context) {
|
|
4136
|
+
return context.destination;
|
|
4137
|
+
}
|
|
4138
|
+
};
|
|
4139
|
+
var _options3, _node2, _recorder, _take;
|
|
4140
|
+
var CaptureSink = class extends AudioSink {
|
|
4141
|
+
constructor(environment, options = {}) {
|
|
4142
|
+
super(environment, options.id ?? "capture");
|
|
4143
|
+
__privateAdd(this, _options3);
|
|
4144
|
+
__privateAdd(this, _node2);
|
|
4145
|
+
__privateAdd(this, _recorder);
|
|
4146
|
+
__privateAdd(this, _take);
|
|
4147
|
+
__privateSet(this, _options3, options);
|
|
4148
|
+
}
|
|
4149
|
+
get recording() {
|
|
4150
|
+
return __privateGet(this, _recorder)?.state === "recording";
|
|
4151
|
+
}
|
|
4152
|
+
start() {
|
|
4153
|
+
this.prepare();
|
|
4154
|
+
if (this.recording) throw new Error("CaptureSink is already recording");
|
|
4155
|
+
const Recorder = __privateGet(this, _options3).recorder ?? globalThis.MediaRecorder;
|
|
4156
|
+
if (!Recorder) throw new Error("MediaRecorder is unavailable in this environment");
|
|
4157
|
+
const recorder = new Recorder(__privateGet(this, _node2).stream, __privateGet(this, _options3).mimeType ? { mimeType: __privateGet(this, _options3).mimeType } : void 0);
|
|
4158
|
+
const chunks = [];
|
|
4159
|
+
__privateSet(this, _take, new Promise((resolve, reject) => {
|
|
4160
|
+
recorder.addEventListener("dataavailable", (event) => {
|
|
4161
|
+
if (event.data.size > 0) chunks.push(event.data);
|
|
4162
|
+
});
|
|
4163
|
+
recorder.addEventListener("stop", () => resolve(new Blob(chunks, __privateGet(this, _options3).mimeType ? { type: __privateGet(this, _options3).mimeType } : void 0)));
|
|
4164
|
+
recorder.addEventListener("error", (event) => reject(event.error ?? new Error("Capture failed")));
|
|
4165
|
+
}));
|
|
4166
|
+
void __privateGet(this, _take).catch(() => {
|
|
4167
|
+
});
|
|
4168
|
+
__privateSet(this, _recorder, recorder);
|
|
4169
|
+
recorder.start();
|
|
4170
|
+
this.notify();
|
|
4171
|
+
}
|
|
4172
|
+
/** Finish the current take. */
|
|
4173
|
+
stop() {
|
|
4174
|
+
const recorder = __privateGet(this, _recorder);
|
|
4175
|
+
const take = __privateGet(this, _take);
|
|
4176
|
+
if (!recorder || !take) return Promise.reject(new Error("CaptureSink is not recording"));
|
|
4177
|
+
__privateSet(this, _recorder, void 0);
|
|
4178
|
+
__privateSet(this, _take, void 0);
|
|
4179
|
+
if (recorder.state !== "inactive") recorder.stop();
|
|
4180
|
+
this.notify();
|
|
4181
|
+
return take;
|
|
4182
|
+
}
|
|
4183
|
+
createInput(context) {
|
|
4184
|
+
const create = context.createMediaStreamDestination;
|
|
4185
|
+
if (typeof create !== "function") throw new Error("Audio capture requires a live AudioContext");
|
|
4186
|
+
__privateSet(this, _node2, create.call(context));
|
|
4187
|
+
return __privateGet(this, _node2);
|
|
4188
|
+
}
|
|
4189
|
+
release() {
|
|
4190
|
+
const recorder = __privateGet(this, _recorder);
|
|
4191
|
+
__privateSet(this, _recorder, void 0);
|
|
4192
|
+
__privateSet(this, _take, void 0);
|
|
4193
|
+
if (recorder && recorder.state !== "inactive") recorder.stop();
|
|
4194
|
+
}
|
|
4195
|
+
};
|
|
4196
|
+
_options3 = new WeakMap();
|
|
4197
|
+
_node2 = new WeakMap();
|
|
4198
|
+
_recorder = new WeakMap();
|
|
4199
|
+
_take = new WeakMap();
|
|
4200
|
+
var offlineContexts = /* @__PURE__ */ new WeakSet();
|
|
4201
|
+
function isOfflineModuleContext(context) {
|
|
4202
|
+
return !!context && offlineContexts.has(context);
|
|
4203
|
+
}
|
|
4204
|
+
var _running;
|
|
4205
|
+
var OfflineTicks = class {
|
|
4206
|
+
constructor() {
|
|
4207
|
+
__privateAdd(this, _running, /* @__PURE__ */ new Set());
|
|
4208
|
+
}
|
|
4209
|
+
create() {
|
|
4210
|
+
let callback;
|
|
4211
|
+
let disposed = false;
|
|
4212
|
+
const running = __privateGet(this, _running);
|
|
4213
|
+
const deliver = () => callback?.();
|
|
4214
|
+
return {
|
|
4215
|
+
start(onTick) {
|
|
4216
|
+
if (disposed) throw new Error("Tick source is disposed");
|
|
4217
|
+
callback = onTick;
|
|
4218
|
+
running.add(deliver);
|
|
4219
|
+
},
|
|
4220
|
+
stop() {
|
|
4221
|
+
running.delete(deliver);
|
|
4222
|
+
},
|
|
4223
|
+
get running() {
|
|
4224
|
+
return running.has(deliver);
|
|
4225
|
+
},
|
|
4226
|
+
workerBacked: false,
|
|
4227
|
+
dispose() {
|
|
4228
|
+
disposed = true;
|
|
4229
|
+
running.delete(deliver);
|
|
4230
|
+
callback = void 0;
|
|
4231
|
+
}
|
|
4232
|
+
};
|
|
4233
|
+
}
|
|
4234
|
+
get active() {
|
|
4235
|
+
return __privateGet(this, _running).size > 0;
|
|
4236
|
+
}
|
|
4237
|
+
tick() {
|
|
4238
|
+
for (const deliver of [...__privateGet(this, _running)]) {
|
|
4239
|
+
try {
|
|
4240
|
+
deliver();
|
|
4241
|
+
} catch {
|
|
4242
|
+
}
|
|
4243
|
+
}
|
|
4244
|
+
}
|
|
4245
|
+
};
|
|
4246
|
+
_running = new WeakMap();
|
|
4247
|
+
function createOfflineModuleEnvironment(options) {
|
|
4248
|
+
const channels = options.channels ?? 2;
|
|
4249
|
+
if (!Number.isInteger(channels) || channels < 1) throw new RangeError("channels must be a positive integer");
|
|
4250
|
+
if (!Number.isFinite(options.sampleRate) || options.sampleRate <= 0) throw new RangeError("sampleRate must be positive");
|
|
4251
|
+
let length = options.length;
|
|
4252
|
+
if (length === void 0) {
|
|
4253
|
+
const seconds = options.durationSeconds;
|
|
4254
|
+
if (seconds === void 0 || !Number.isFinite(seconds) || seconds <= 0) throw new RangeError("durationSeconds must be positive");
|
|
4255
|
+
length = Math.ceil(seconds * options.sampleRate);
|
|
4256
|
+
}
|
|
4257
|
+
if (!Number.isInteger(length) || length < 1) throw new RangeError("length must be a positive integer number of frames");
|
|
4258
|
+
const step = options.tickSeconds ?? 0.025;
|
|
4259
|
+
if (!Number.isFinite(step) || step <= 0) throw new RangeError("tickSeconds must be positive");
|
|
4260
|
+
const create = options.create ?? ((count, frames2, rate) => {
|
|
4261
|
+
const Offline = globalThis.OfflineAudioContext;
|
|
4262
|
+
if (!Offline) throw new Error("OfflineAudioContext is unavailable in this environment");
|
|
4263
|
+
return new Offline(count, frames2, rate);
|
|
4264
|
+
});
|
|
4265
|
+
const context = create(channels, length, options.sampleRate);
|
|
4266
|
+
offlineContexts.add(context);
|
|
4267
|
+
const ticks = new OfflineTicks();
|
|
4268
|
+
const nativeResume = typeof context.resume === "function" ? context.resume.bind(context) : void 0;
|
|
4269
|
+
const nativeState = Object.getOwnPropertyDescriptor(Object.getPrototypeOf(context) ?? {}, "state")?.get;
|
|
4270
|
+
let rendering = false;
|
|
4271
|
+
try {
|
|
4272
|
+
Object.defineProperty(context, "state", {
|
|
4273
|
+
configurable: true,
|
|
4274
|
+
get: () => rendering && nativeState ? nativeState.call(context) : "running"
|
|
4275
|
+
});
|
|
4276
|
+
Object.defineProperty(context, "resume", { configurable: true, value: () => rendering && nativeResume ? nativeResume() : Promise.resolve() });
|
|
4277
|
+
} catch {
|
|
4278
|
+
}
|
|
4279
|
+
const environment = new ModuleAudioEnvironment({ context, createTickSource: () => ticks.create() });
|
|
4280
|
+
const frames = length;
|
|
4281
|
+
let rendered;
|
|
4282
|
+
return Object.freeze({
|
|
4283
|
+
environment,
|
|
4284
|
+
context,
|
|
4285
|
+
render: () => rendered ?? (rendered = (() => {
|
|
4286
|
+
rendering = true;
|
|
4287
|
+
const end = frames / options.sampleRate;
|
|
4288
|
+
if (typeof context.suspend === "function" && nativeResume) {
|
|
4289
|
+
const quantum = 128 / options.sampleRate;
|
|
4290
|
+
let previous = -1;
|
|
4291
|
+
for (let time = step; time < end; time += step) {
|
|
4292
|
+
const at = Math.floor(time / quantum) * quantum;
|
|
4293
|
+
if (at <= previous) continue;
|
|
4294
|
+
previous = at;
|
|
4295
|
+
void context.suspend(at).then(() => {
|
|
4296
|
+
ticks.tick();
|
|
4297
|
+
return nativeResume();
|
|
4298
|
+
}).catch(() => {
|
|
4299
|
+
});
|
|
4300
|
+
}
|
|
4301
|
+
}
|
|
4302
|
+
ticks.tick();
|
|
4303
|
+
return context.startRendering();
|
|
4304
|
+
})())
|
|
4305
|
+
});
|
|
4306
|
+
}
|
|
4307
|
+
|
|
4308
|
+
// src/module/offline-render.ts
|
|
4309
|
+
async function renderModuleGraph(options) {
|
|
4310
|
+
const offline = createOfflineModuleEnvironment(options);
|
|
4311
|
+
const session2 = new SessionTransport(() => offline.context.currentTime, { driftCheckIntervalMs: 0 });
|
|
4312
|
+
const scope = new ModuleContext({ id: options.id ?? "offline", audio: offline.environment, session: session2 });
|
|
4313
|
+
let cleanup = void 0;
|
|
4314
|
+
try {
|
|
4315
|
+
cleanup = await options.build(scope);
|
|
4316
|
+
if (options.startSeconds) await session2.seek(options.startSeconds);
|
|
4317
|
+
await session2.play();
|
|
4318
|
+
return await offline.render();
|
|
4319
|
+
} finally {
|
|
4320
|
+
try {
|
|
4321
|
+
await session2.stop();
|
|
4322
|
+
} catch {
|
|
4323
|
+
}
|
|
4324
|
+
try {
|
|
4325
|
+
if (typeof cleanup === "function") cleanup();
|
|
4326
|
+
} finally {
|
|
4327
|
+
session2.dispose();
|
|
4328
|
+
}
|
|
4329
|
+
}
|
|
4330
|
+
}
|
|
4331
|
+
|
|
4332
|
+
export {
|
|
4333
|
+
PresetBank,
|
|
4334
|
+
captureParameters,
|
|
4335
|
+
applyParameters,
|
|
4336
|
+
controlFraction,
|
|
4337
|
+
controlSource,
|
|
4338
|
+
sourceMatches,
|
|
4339
|
+
describeSource,
|
|
4340
|
+
ControlMappings,
|
|
4341
|
+
VoiceAllocator,
|
|
4342
|
+
MessageDebugger,
|
|
4343
|
+
WAM_MODULE,
|
|
4344
|
+
wamPlugin,
|
|
4345
|
+
MAX_MC_CHANNELS,
|
|
4346
|
+
multichannelPlugin,
|
|
4347
|
+
ChannelPack,
|
|
4348
|
+
ChannelUnpack,
|
|
4349
|
+
NoteMaker,
|
|
4350
|
+
COMPONENT_BOUNDARY,
|
|
4351
|
+
POLY_VOICE,
|
|
4352
|
+
NestedComponent,
|
|
4353
|
+
PolyComponent,
|
|
4354
|
+
InletModule,
|
|
4355
|
+
OutletModule,
|
|
4356
|
+
VoiceNotes,
|
|
4357
|
+
PresetModule,
|
|
4358
|
+
nodeWeights,
|
|
4359
|
+
NodesModule,
|
|
4360
|
+
createStructurePlugins,
|
|
4361
|
+
SOURCE_SCHEMAS,
|
|
4362
|
+
SourcePublisher,
|
|
4363
|
+
checkModuleConformance,
|
|
4364
|
+
ControlLFO,
|
|
4365
|
+
ControlEnvelope,
|
|
4366
|
+
EnvelopeFollower,
|
|
4367
|
+
ParamMacro,
|
|
4368
|
+
copyProjectJson,
|
|
4369
|
+
parseModuleProject,
|
|
4370
|
+
serializeModuleProject,
|
|
4371
|
+
ProjectPluginRegistry,
|
|
4372
|
+
ModuleProjectRuntime,
|
|
4373
|
+
keepProjectSessionPreloaded,
|
|
4374
|
+
createProjectEditorBinding,
|
|
4375
|
+
RackModule,
|
|
4376
|
+
createProviderScope,
|
|
4377
|
+
DeviceSink,
|
|
4378
|
+
CaptureSink,
|
|
4379
|
+
isOfflineModuleContext,
|
|
4380
|
+
createOfflineModuleEnvironment,
|
|
4381
|
+
renderModuleGraph
|
|
4382
|
+
};
|
|
4383
|
+
//# sourceMappingURL=chunk-RCWFRZDR.js.map
|