@webmusic/bridge 0.2.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 +66 -70
- package/dist/THIRD_PARTY_NOTICES.txt +1605 -0
- package/dist/build-receipt.json +3 -3
- package/dist/chunk-XQXQSBVS.js +408 -0
- package/dist/chunk-XQXQSBVS.js.map +1 -0
- package/dist/component/global.cjs +19 -0
- package/dist/component/global.cjs.map +1 -0
- package/dist/component/global.d.ts +9 -0
- package/dist/component/global.js +1 -0
- package/dist/component/global.js.map +1 -0
- package/dist/component.cjs +3091 -0
- package/dist/component.cjs.map +1 -0
- package/dist/component.d.cts +860 -0
- package/dist/component.d.ts +860 -0
- package/dist/component.global.js +3541 -0
- package/dist/component.global.js.map +1 -0
- package/dist/component.js +2672 -0
- package/dist/component.js.map +1 -0
- package/dist/index.cjs +447 -486
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +65 -297
- package/dist/index.d.ts +65 -297
- package/dist/index.js +66 -488
- package/dist/index.js.map +1 -1
- package/package.json +32 -9
|
@@ -0,0 +1,2672 @@
|
|
|
1
|
+
import {
|
|
2
|
+
__privateAdd,
|
|
3
|
+
__privateGet,
|
|
4
|
+
__privateMethod,
|
|
5
|
+
__privateSet,
|
|
6
|
+
createWebMusicProjectPlugins
|
|
7
|
+
} from "./chunk-XQXQSBVS.js";
|
|
8
|
+
|
|
9
|
+
// src/component/controls.ts
|
|
10
|
+
import { Button, Choice, Matrix, MessageBox, NumberValue, Toggle, ValueList } from "@webmusic/kernel/message";
|
|
11
|
+
import {
|
|
12
|
+
NodesModule,
|
|
13
|
+
PresetModule,
|
|
14
|
+
bang,
|
|
15
|
+
floatAtom,
|
|
16
|
+
floatMessage,
|
|
17
|
+
intAtom,
|
|
18
|
+
intMessage,
|
|
19
|
+
message,
|
|
20
|
+
parseMessage
|
|
21
|
+
} from "@webmusic/kernel/module";
|
|
22
|
+
import {
|
|
23
|
+
mountButton,
|
|
24
|
+
mountDial,
|
|
25
|
+
mountMatrix,
|
|
26
|
+
mountMenu,
|
|
27
|
+
mountMessage,
|
|
28
|
+
mountMultislider,
|
|
29
|
+
mountNodes,
|
|
30
|
+
mountNumber,
|
|
31
|
+
mountPresetGrid,
|
|
32
|
+
mountSlider,
|
|
33
|
+
mountTabs,
|
|
34
|
+
mountToggle
|
|
35
|
+
} from "@webmusic/ui/controls";
|
|
36
|
+
var number = (value, fallback = 0) => typeof value === "number" && Number.isFinite(value) ? value : fallback;
|
|
37
|
+
var bound = (value) => typeof value === "number" && Number.isFinite(value) ? value : void 0;
|
|
38
|
+
var numberMessage = (value, float) => float ? floatMessage(value) : intMessage(Math.round(value));
|
|
39
|
+
var CONTROL_KINDS = Object.freeze(["button", "toggle", "number", "slider", "dial", "message", "menu", "tab", "multislider", "matrix", "preset", "nodes"]);
|
|
40
|
+
var KIND_OF_TYPE = Object.freeze({
|
|
41
|
+
[Button.type]: "button",
|
|
42
|
+
[Toggle.type]: "toggle",
|
|
43
|
+
[NumberValue.type]: (config) => config.control === "slider" || config.control === "dial" ? config.control : "number",
|
|
44
|
+
[Choice.type]: (config) => config.control === "tab" ? "tab" : "menu",
|
|
45
|
+
[MessageBox.type]: "message",
|
|
46
|
+
[ValueList.type]: "multislider",
|
|
47
|
+
[Matrix.type]: "matrix",
|
|
48
|
+
[PresetModule.type]: "preset",
|
|
49
|
+
[NodesModule.type]: "nodes"
|
|
50
|
+
});
|
|
51
|
+
function controlKindOf(record) {
|
|
52
|
+
const kind = KIND_OF_TYPE[record.plugin.id];
|
|
53
|
+
return typeof kind === "function" ? kind(record.config) : kind;
|
|
54
|
+
}
|
|
55
|
+
var CELL = 32;
|
|
56
|
+
function controlSize(kind, config = {}) {
|
|
57
|
+
const count = (value, fallback, most) => typeof value === "number" && Number.isInteger(value) && value >= 1 ? Math.min(most, value) : fallback;
|
|
58
|
+
const items = Array.isArray(config.items) ? config.items.map(String) : [];
|
|
59
|
+
const wide = (characters) => Math.ceil(characters * 7.5) + 18;
|
|
60
|
+
const within = (value, least, most) => Math.min(most, Math.max(least, value));
|
|
61
|
+
switch (kind) {
|
|
62
|
+
case "number":
|
|
63
|
+
return { width: 96, height: CELL };
|
|
64
|
+
case "slider":
|
|
65
|
+
case "menu":
|
|
66
|
+
return { width: 128, height: CELL };
|
|
67
|
+
case "dial":
|
|
68
|
+
return { width: 48, height: 64 };
|
|
69
|
+
case "message":
|
|
70
|
+
return { width: within(wide(typeof config.text === "string" ? config.text.length : 0), 48, 320), height: CELL };
|
|
71
|
+
case "tab":
|
|
72
|
+
return { width: within(items.reduce((sum, item) => sum + wide(item.length), 0), 64, 480), height: CELL };
|
|
73
|
+
case "multislider":
|
|
74
|
+
return { width: 128, height: 96 };
|
|
75
|
+
case "matrix":
|
|
76
|
+
return { width: count(config.columns, 4, 32) * CELL, height: count(config.rows, 4, 32) * CELL };
|
|
77
|
+
case "preset": {
|
|
78
|
+
const slots = count(config.count, 16, 64);
|
|
79
|
+
return { width: Math.min(8, slots) * CELL, height: Math.ceil(slots / 8) * CELL };
|
|
80
|
+
}
|
|
81
|
+
case "nodes":
|
|
82
|
+
return { width: 160, height: 160 };
|
|
83
|
+
default:
|
|
84
|
+
return { width: CELL, height: CELL };
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
function mountModuleControl(host, kind, options) {
|
|
88
|
+
let iface;
|
|
89
|
+
let release;
|
|
90
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
91
|
+
const snapshot = () => {
|
|
92
|
+
const value = iface?.snapshot();
|
|
93
|
+
return value && typeof value === "object" && !Array.isArray(value) ? value : {};
|
|
94
|
+
};
|
|
95
|
+
const send = (value) => iface?.interact(value);
|
|
96
|
+
const subscribe = (notify) => {
|
|
97
|
+
listeners.add(notify);
|
|
98
|
+
return () => {
|
|
99
|
+
listeners.delete(notify);
|
|
100
|
+
};
|
|
101
|
+
};
|
|
102
|
+
const disabled = () => !iface;
|
|
103
|
+
const common = { label: options.label, fill: true, stylesheet: "shared", onError: options.onError ?? ((error) => console.error(error)) };
|
|
104
|
+
let flashes = 0;
|
|
105
|
+
let handle;
|
|
106
|
+
switch (kind) {
|
|
107
|
+
case "button":
|
|
108
|
+
handle = mountButton(host, { snapshot: () => ({ disabled: disabled() }), press: () => send(bang()), subscribe }, common);
|
|
109
|
+
break;
|
|
110
|
+
case "toggle":
|
|
111
|
+
handle = mountToggle(host, { snapshot: () => ({ on: snapshot().on === true, disabled: disabled() }), setOn: (on) => send(intMessage(on ? 1 : 0)), subscribe }, common);
|
|
112
|
+
break;
|
|
113
|
+
case "number":
|
|
114
|
+
handle = mountNumber(host, { snapshot: () => {
|
|
115
|
+
const state = snapshot();
|
|
116
|
+
return { value: number(state.value), min: bound(state.min), max: bound(state.max), float: state.float === true, disabled: disabled() };
|
|
117
|
+
}, setValue: (value) => send(numberMessage(value, snapshot().float === true)), subscribe }, common);
|
|
118
|
+
break;
|
|
119
|
+
case "slider":
|
|
120
|
+
case "dial": {
|
|
121
|
+
const binding = { snapshot: () => {
|
|
122
|
+
const state = snapshot();
|
|
123
|
+
return { value: number(state.value), min: number(state.min), max: number(state.max, 127), float: state.float === true, disabled: disabled() };
|
|
124
|
+
}, setValue: (value) => send(numberMessage(value, snapshot().float === true)), subscribe };
|
|
125
|
+
handle = kind === "slider" ? mountSlider(host, binding, { ...common, orientation: "auto" }) : mountDial(host, binding, common);
|
|
126
|
+
break;
|
|
127
|
+
}
|
|
128
|
+
case "message":
|
|
129
|
+
handle = mountMessage(host, { snapshot: () => ({ text: String(snapshot().text ?? ""), disabled: disabled() }), press: () => send(bang()), subscribe }, common);
|
|
130
|
+
break;
|
|
131
|
+
case "menu":
|
|
132
|
+
case "tab": {
|
|
133
|
+
const binding = { snapshot: () => {
|
|
134
|
+
const state = snapshot();
|
|
135
|
+
return { items: Array.isArray(state.items) ? state.items.map(String) : [], index: number(state.index), disabled: disabled() };
|
|
136
|
+
}, select: (index) => send(intMessage(index)), subscribe };
|
|
137
|
+
handle = kind === "menu" ? mountMenu(host, binding, { ...common, cardHost: options.cardHost }) : mountTabs(host, binding, common);
|
|
138
|
+
break;
|
|
139
|
+
}
|
|
140
|
+
case "multislider":
|
|
141
|
+
handle = mountMultislider(host, { snapshot: () => {
|
|
142
|
+
const state = snapshot();
|
|
143
|
+
return { values: Array.isArray(state.values) ? state.values.map((item) => number(item)) : [], min: number(state.min), max: number(state.max, 1), float: state.float === true, disabled: disabled() };
|
|
144
|
+
}, setValues: (values) => {
|
|
145
|
+
const float = snapshot().float === true;
|
|
146
|
+
send(message("list", values.map((value) => float ? floatAtom(value) : intAtom(value))));
|
|
147
|
+
}, subscribe }, common);
|
|
148
|
+
break;
|
|
149
|
+
case "matrix":
|
|
150
|
+
handle = mountMatrix(host, { snapshot: () => {
|
|
151
|
+
const state = snapshot();
|
|
152
|
+
return { columns: number(state.columns, 4), rows: number(state.rows, 4), cells: Array.isArray(state.cells) ? state.cells.map((item) => number(item)) : [], disabled: disabled() };
|
|
153
|
+
}, setCell: (column, row, on) => send(message("list", [intAtom(column), intAtom(row), intAtom(on ? 1 : 0)])), subscribe }, common);
|
|
154
|
+
break;
|
|
155
|
+
case "preset":
|
|
156
|
+
handle = mountPresetGrid(host, { snapshot: () => {
|
|
157
|
+
const state = snapshot();
|
|
158
|
+
return { count: number(state.count, 16), stored: Array.isArray(state.stored) ? state.stored.map((item) => number(item)) : [], current: number(state.current), disabled: disabled() };
|
|
159
|
+
}, recall: (slot) => send(intMessage(slot)), store: (slot) => send(parseMessage(`store ${slot}`)), clear: (slot) => send(parseMessage(`clear ${slot}`)), subscribe }, common);
|
|
160
|
+
break;
|
|
161
|
+
case "nodes":
|
|
162
|
+
handle = mountNodes(host, {
|
|
163
|
+
snapshot: () => {
|
|
164
|
+
const state = snapshot();
|
|
165
|
+
const nodes = Array.isArray(state.nodes) ? state.nodes : [];
|
|
166
|
+
const cursor = state.cursor ?? { x: 0.5, y: 0.5 };
|
|
167
|
+
return { nodes, cursor, weights: Array.isArray(state.weights) ? state.weights.map((item) => number(item)) : [], disabled: disabled() };
|
|
168
|
+
},
|
|
169
|
+
setCursor: (x, y) => send(parseMessage(`cursor ${x.toFixed(4)} ${y.toFixed(4)}`)),
|
|
170
|
+
moveNode: (index, x, y) => send(parseMessage(`node ${index} ${x.toFixed(4)} ${y.toFixed(4)}`)),
|
|
171
|
+
subscribe
|
|
172
|
+
}, common);
|
|
173
|
+
break;
|
|
174
|
+
default:
|
|
175
|
+
throw new Error(`No control for ${kind}.`);
|
|
176
|
+
}
|
|
177
|
+
const changed = () => {
|
|
178
|
+
const count = number(snapshot().flashes, flashes);
|
|
179
|
+
if (count > flashes && "flash" in handle) handle.flash();
|
|
180
|
+
flashes = count;
|
|
181
|
+
for (const listener of [...listeners]) listener();
|
|
182
|
+
};
|
|
183
|
+
return {
|
|
184
|
+
refresh(next) {
|
|
185
|
+
if (next === iface) return;
|
|
186
|
+
release?.();
|
|
187
|
+
iface = next;
|
|
188
|
+
flashes = number(snapshot().flashes, 0);
|
|
189
|
+
release = next?.subscribe(changed);
|
|
190
|
+
for (const listener of [...listeners]) listener();
|
|
191
|
+
},
|
|
192
|
+
destroy() {
|
|
193
|
+
release?.();
|
|
194
|
+
listeners.clear();
|
|
195
|
+
handle.destroy();
|
|
196
|
+
}
|
|
197
|
+
};
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
// src/component/manifest.ts
|
|
201
|
+
import {
|
|
202
|
+
defineAudioLevelAnalyzerElement,
|
|
203
|
+
defineAudioMeterElement,
|
|
204
|
+
defineAudioOscilloscopeElement,
|
|
205
|
+
defineAudioSpectrumAnalyzerElement,
|
|
206
|
+
defineAudioTransientAnalyzerElement
|
|
207
|
+
} from "@webmusic/audio/analyze/element";
|
|
208
|
+
import { AUDIO_EFFECT_TYPES, defineAudioEffectElement, defineAudioMixerElement, defineAudioPlayerElement, defineAudioPlaylistElement, defineAudioRecorderElement } from "@webmusic/audio/play/element";
|
|
209
|
+
import { defineAudioClipViewElement, defineAudioLiveViewElement, defineAudioViewElement } from "@webmusic/audio/view/element";
|
|
210
|
+
import { BufferModule } from "@webmusic/audio/play/headless";
|
|
211
|
+
import { InletModule, NestedComponent, OutletModule, numberedPort, parseModuleProject } from "@webmusic/kernel/module";
|
|
212
|
+
import {
|
|
213
|
+
defineChordAnalysisElement,
|
|
214
|
+
defineIntervalAnalysisElement,
|
|
215
|
+
defineRhythmAnalysisElement,
|
|
216
|
+
defineScaleAnalysisElement
|
|
217
|
+
} from "@webmusic/score/analyze/element";
|
|
218
|
+
import {
|
|
219
|
+
RACK_PART_SOUNDS,
|
|
220
|
+
SCORE_EFFECT_TYPES,
|
|
221
|
+
defineNoteInputElement,
|
|
222
|
+
defineRackControlElement,
|
|
223
|
+
defineRackPartElement,
|
|
224
|
+
defineScoreEffectElement,
|
|
225
|
+
defineScorePlayerElement,
|
|
226
|
+
defineScoreRecorderElement,
|
|
227
|
+
defineSynthPanelElement
|
|
228
|
+
} from "@webmusic/score/play/element";
|
|
229
|
+
import { Effect } from "@webmusic/score/play/headless";
|
|
230
|
+
import { definePitchViewElement, defineScoreGridViewElement, defineScoreViewElement, defineSheetViewElement } from "@webmusic/score/view/element";
|
|
231
|
+
function componentAssetRoles(manifest, sources) {
|
|
232
|
+
const wanted = manifest.asset;
|
|
233
|
+
if (!wanted) return void 0;
|
|
234
|
+
if (wanted.kind === "entries") {
|
|
235
|
+
const entries3 = sources.entries ?? (sources.clip === void 0 ? [] : [sources.clip]);
|
|
236
|
+
if (entries3.length === 0) return void 0;
|
|
237
|
+
return Object.freeze(Object.fromEntries(entries3.map((id2, index) => [`${wanted.role}${index + 1}`, id2])));
|
|
238
|
+
}
|
|
239
|
+
const id = sources[wanted.kind];
|
|
240
|
+
return id === void 0 ? void 0 : Object.freeze({ [wanted.role]: id });
|
|
241
|
+
}
|
|
242
|
+
var KEYS_PLUGIN = "webmusic.surface.keys";
|
|
243
|
+
var SCOPE_PLUGIN = "webmusic.surface.scope";
|
|
244
|
+
var METER_PLUGIN = "webmusic.surface.meter";
|
|
245
|
+
var SPECTRUM_PLUGIN = "webmusic.surface.spectrum";
|
|
246
|
+
var ROLL_PLUGIN = "webmusic.surface.roll";
|
|
247
|
+
var CLIP_PLUGIN = "webmusic.component.audio-clip-view";
|
|
248
|
+
var LEVEL_PLUGIN = "webmusic.component.audio-level-analyzer";
|
|
249
|
+
var audioIn = (fftSize) => ({ id: "input", family: "audio", direction: "input", bind: "analyser", fftSize });
|
|
250
|
+
var sourceIn = { id: "source", family: "source", direction: "input", bind: "inputModule" };
|
|
251
|
+
var pluginOf = (tag) => `webmusic.component.${tag}`;
|
|
252
|
+
var audioOut = { id: "output", family: "audio", direction: "output", bind: "context" };
|
|
253
|
+
var channelsIn = (count) => Array.from({ length: count }, (_, index) => ({ id: `in${index + 1}`, family: "audio", direction: "input", bind: "admit", channel: String(index + 1) }));
|
|
254
|
+
var DESK_INPUTS = { name: "inputs", label: "inputs", kind: "number", fallback: 4, min: 1, max: 16 };
|
|
255
|
+
var deskPorts = (config) => {
|
|
256
|
+
const value = parameterValue(DESK_INPUTS, config);
|
|
257
|
+
const count = Math.min(DESK_INPUTS.max, Math.max(DESK_INPUTS.min, Math.round(value)));
|
|
258
|
+
return Object.freeze([...channelsIn(count), audioOut]);
|
|
259
|
+
};
|
|
260
|
+
var SCORE_EFFECTS = SCORE_EFFECT_TYPES;
|
|
261
|
+
var OFFICIAL_COMPONENTS = Object.freeze([
|
|
262
|
+
{
|
|
263
|
+
name: "keys",
|
|
264
|
+
summary: "Piano keyboard: play notes into a synth",
|
|
265
|
+
tag: "score-note-input",
|
|
266
|
+
plugin: KEYS_PLUGIN,
|
|
267
|
+
ports: [{ id: "notes", family: "note", direction: "output", bind: "module" }],
|
|
268
|
+
parameters: [
|
|
269
|
+
{ name: "low", label: "low", kind: "number", fallback: 48, min: 0, max: 127, attribute: "start" },
|
|
270
|
+
{ name: "high", label: "high", kind: "number", fallback: 72, min: 0, max: 127, attribute: "end" }
|
|
271
|
+
],
|
|
272
|
+
attributes: { layout: "piano", routing: "manual", "aria-label": "Keyboard" },
|
|
273
|
+
// The box is a piano whose notes the host forwards: its fit and width are a piano's.
|
|
274
|
+
locked: ["layout", "routing"],
|
|
275
|
+
width: 320,
|
|
276
|
+
fit: { kind: "token", property: "--wm-note-height", content: '[part~="root"]', surface: '[part~="viewport"]', least: 24 },
|
|
277
|
+
define: () => defineNoteInputElement()
|
|
278
|
+
},
|
|
279
|
+
{
|
|
280
|
+
name: "roll",
|
|
281
|
+
summary: "Piano roll following a score",
|
|
282
|
+
tag: "score-grid-view",
|
|
283
|
+
plugin: ROLL_PLUGIN,
|
|
284
|
+
ports: [{ id: "source", family: "source", direction: "input", bind: "inputModule" }],
|
|
285
|
+
parameters: [],
|
|
286
|
+
attributes: { height: "176", "note-height": "6" },
|
|
287
|
+
edits: "score",
|
|
288
|
+
scrolls: true,
|
|
289
|
+
width: 320,
|
|
290
|
+
define: () => defineScoreGridViewElement()
|
|
291
|
+
},
|
|
292
|
+
{
|
|
293
|
+
name: "scope",
|
|
294
|
+
summary: "Oscilloscope of the incoming audio",
|
|
295
|
+
tag: "audio-oscilloscope",
|
|
296
|
+
plugin: SCOPE_PLUGIN,
|
|
297
|
+
ports: [audioIn(2048)],
|
|
298
|
+
parameters: [{ name: "timebaseMs", label: "milliseconds", kind: "number", fallback: 10, min: 1, max: 40, attribute: "timebase-ms" }],
|
|
299
|
+
width: 288,
|
|
300
|
+
define: () => defineAudioOscilloscopeElement()
|
|
301
|
+
},
|
|
302
|
+
{
|
|
303
|
+
name: "meter",
|
|
304
|
+
summary: "Level meter of the incoming audio",
|
|
305
|
+
tag: "audio-meter",
|
|
306
|
+
plugin: METER_PLUGIN,
|
|
307
|
+
ports: [audioIn(2048)],
|
|
308
|
+
parameters: [],
|
|
309
|
+
width: 224,
|
|
310
|
+
define: () => defineAudioMeterElement()
|
|
311
|
+
},
|
|
312
|
+
{
|
|
313
|
+
name: "spectrum",
|
|
314
|
+
summary: "Spectrum of the incoming audio",
|
|
315
|
+
tag: "audio-spectrum-analyzer",
|
|
316
|
+
plugin: SPECTRUM_PLUGIN,
|
|
317
|
+
ports: [audioIn(4096)],
|
|
318
|
+
parameters: [],
|
|
319
|
+
width: 288,
|
|
320
|
+
define: () => defineAudioSpectrumAnalyzerElement()
|
|
321
|
+
},
|
|
322
|
+
{
|
|
323
|
+
name: "level",
|
|
324
|
+
summary: "RMS, peak and crest of the incoming audio",
|
|
325
|
+
tag: "audio-level-analyzer",
|
|
326
|
+
plugin: LEVEL_PLUGIN,
|
|
327
|
+
ports: [audioIn(2048)],
|
|
328
|
+
parameters: [{ name: "thresholdDbfs", label: "threshold dBFS", kind: "number", fallback: -12, min: -60, max: 0, attribute: "threshold-dbfs" }],
|
|
329
|
+
width: 288,
|
|
330
|
+
define: () => defineAudioLevelAnalyzerElement()
|
|
331
|
+
},
|
|
332
|
+
// A buffer's waveform: `buffer` holds the samples and shows nothing (DEC-081).
|
|
333
|
+
{
|
|
334
|
+
name: "clip",
|
|
335
|
+
summary: "Show and edit the samples of a named buffer",
|
|
336
|
+
tag: "audio-clip-view",
|
|
337
|
+
plugin: CLIP_PLUGIN,
|
|
338
|
+
ports: [],
|
|
339
|
+
parameters: [{ name: "buffer", label: "name", kind: "word", fallback: "", buffer: true }],
|
|
340
|
+
attributes: { height: "120" },
|
|
341
|
+
scrolls: true,
|
|
342
|
+
width: 320,
|
|
343
|
+
define: () => defineAudioClipViewElement()
|
|
344
|
+
},
|
|
345
|
+
// Placed after the first seven: the packages' other Components that take
|
|
346
|
+
// their data from a cord.
|
|
347
|
+
{
|
|
348
|
+
name: "sheet",
|
|
349
|
+
summary: "Engraved sheet music following a score",
|
|
350
|
+
tag: "score-sheet-view",
|
|
351
|
+
plugin: pluginOf("score-sheet-view"),
|
|
352
|
+
ports: [sourceIn],
|
|
353
|
+
parameters: [],
|
|
354
|
+
attributes: { layout: "flow", height: "160" },
|
|
355
|
+
edits: "score",
|
|
356
|
+
scrolls: true,
|
|
357
|
+
width: 320,
|
|
358
|
+
define: () => defineSheetViewElement()
|
|
359
|
+
},
|
|
360
|
+
{
|
|
361
|
+
name: "view",
|
|
362
|
+
summary: "Staff, piano roll or waterfall of a score",
|
|
363
|
+
tag: "score-view",
|
|
364
|
+
plugin: pluginOf("score-view"),
|
|
365
|
+
ports: [sourceIn],
|
|
366
|
+
parameters: [{ name: "type", label: "type", kind: "word", fallback: "staff", choices: ["staff", "piano-roll", "waterfall"], attribute: "type" }],
|
|
367
|
+
attributes: { height: "120" },
|
|
368
|
+
scrolls: true,
|
|
369
|
+
width: 320,
|
|
370
|
+
define: () => defineScoreViewElement()
|
|
371
|
+
},
|
|
372
|
+
{
|
|
373
|
+
name: "pitch",
|
|
374
|
+
summary: "The pitches sounding now, on keys, a staff or frets",
|
|
375
|
+
tag: "score-pitch-view",
|
|
376
|
+
plugin: pluginOf("score-pitch-view"),
|
|
377
|
+
ports: [sourceIn],
|
|
378
|
+
parameters: [{ name: "type", label: "type", kind: "word", fallback: "keyboard", choices: ["keyboard", "staff", "fretboard"], attribute: "type" }],
|
|
379
|
+
width: 320,
|
|
380
|
+
define: () => definePitchViewElement()
|
|
381
|
+
},
|
|
382
|
+
{
|
|
383
|
+
name: "chords",
|
|
384
|
+
summary: "Chords of a score as it plays",
|
|
385
|
+
tag: "score-chord-analysis",
|
|
386
|
+
plugin: pluginOf("score-chord-analysis"),
|
|
387
|
+
ports: [sourceIn],
|
|
388
|
+
parameters: [],
|
|
389
|
+
scrolls: true,
|
|
390
|
+
width: 320,
|
|
391
|
+
define: () => defineChordAnalysisElement()
|
|
392
|
+
},
|
|
393
|
+
{
|
|
394
|
+
name: "intervals",
|
|
395
|
+
summary: "Intervals of a score as it plays",
|
|
396
|
+
tag: "score-interval-analysis",
|
|
397
|
+
plugin: pluginOf("score-interval-analysis"),
|
|
398
|
+
ports: [sourceIn],
|
|
399
|
+
parameters: [],
|
|
400
|
+
scrolls: true,
|
|
401
|
+
width: 320,
|
|
402
|
+
define: () => defineIntervalAnalysisElement()
|
|
403
|
+
},
|
|
404
|
+
{
|
|
405
|
+
name: "rhythm",
|
|
406
|
+
summary: "Rhythm of a score against its beats",
|
|
407
|
+
tag: "score-rhythm-analysis",
|
|
408
|
+
plugin: pluginOf("score-rhythm-analysis"),
|
|
409
|
+
ports: [sourceIn],
|
|
410
|
+
parameters: [],
|
|
411
|
+
scrolls: true,
|
|
412
|
+
width: 320,
|
|
413
|
+
define: () => defineRhythmAnalysisElement()
|
|
414
|
+
},
|
|
415
|
+
{
|
|
416
|
+
name: "degrees",
|
|
417
|
+
summary: "Pitches of a score as degrees of a scale",
|
|
418
|
+
tag: "score-scale-analysis",
|
|
419
|
+
plugin: pluginOf("score-scale-analysis"),
|
|
420
|
+
ports: [sourceIn],
|
|
421
|
+
parameters: [
|
|
422
|
+
{
|
|
423
|
+
name: "scale",
|
|
424
|
+
label: "scale",
|
|
425
|
+
kind: "word",
|
|
426
|
+
fallback: "major",
|
|
427
|
+
choices: ["major", "natural-minor", "harmonic-minor", "melodic-minor-ascending", "melodic-minor-descending"],
|
|
428
|
+
attribute: "scale"
|
|
429
|
+
},
|
|
430
|
+
{ name: "tonic", label: "tonic", kind: "word", fallback: "", attribute: "tonic" }
|
|
431
|
+
],
|
|
432
|
+
scrolls: true,
|
|
433
|
+
width: 320,
|
|
434
|
+
define: () => defineScaleAnalysisElement()
|
|
435
|
+
},
|
|
436
|
+
{
|
|
437
|
+
name: "transients",
|
|
438
|
+
summary: "Onsets in the incoming audio",
|
|
439
|
+
tag: "audio-transient-analyzer",
|
|
440
|
+
plugin: pluginOf("audio-transient-analyzer"),
|
|
441
|
+
ports: [audioIn(2048)],
|
|
442
|
+
parameters: [],
|
|
443
|
+
width: 288,
|
|
444
|
+
define: () => defineAudioTransientAnalyzerElement()
|
|
445
|
+
},
|
|
446
|
+
{
|
|
447
|
+
name: "live",
|
|
448
|
+
summary: "Scrolling waveform or spectrogram of the incoming audio",
|
|
449
|
+
tag: "audio-live-view",
|
|
450
|
+
plugin: pluginOf("audio-live-view"),
|
|
451
|
+
ports: [audioIn(2048)],
|
|
452
|
+
parameters: [{ name: "type", label: "type", kind: "word", fallback: "waveform", choices: ["waveform", "spectrogram"], attribute: "type" }],
|
|
453
|
+
attributes: { height: "64" },
|
|
454
|
+
width: 320,
|
|
455
|
+
define: () => defineAudioLiveViewElement()
|
|
456
|
+
},
|
|
457
|
+
// Players and desks (DEC-084): each plays in the graph's Session through the
|
|
458
|
+
// module context its box lends it, so its transport and the graph's are one.
|
|
459
|
+
{
|
|
460
|
+
name: "player",
|
|
461
|
+
summary: "Play a score with its own transport, in the graph's Session",
|
|
462
|
+
tag: "score-player",
|
|
463
|
+
plugin: pluginOf("score-player"),
|
|
464
|
+
ports: [audioOut, { id: "source", family: "source", direction: "output", bind: "module" }],
|
|
465
|
+
parameters: [],
|
|
466
|
+
asset: { role: "source", kind: "score", property: "score" },
|
|
467
|
+
attributes: { controls: "show", "aria-label": "Score player" },
|
|
468
|
+
width: 320,
|
|
469
|
+
define: () => defineScorePlayerElement()
|
|
470
|
+
},
|
|
471
|
+
{
|
|
472
|
+
name: "track",
|
|
473
|
+
summary: "Play a recording with its own transport, in the graph's Session",
|
|
474
|
+
tag: "audio-player",
|
|
475
|
+
plugin: pluginOf("audio-player"),
|
|
476
|
+
ports: [audioOut, { id: "source", family: "source", direction: "output", bind: "module" }],
|
|
477
|
+
parameters: [],
|
|
478
|
+
asset: { role: "source", kind: "clip", property: "clip" },
|
|
479
|
+
attributes: { controls: "show", "aria-label": "Audio player" },
|
|
480
|
+
width: 320,
|
|
481
|
+
define: () => defineAudioPlayerElement()
|
|
482
|
+
},
|
|
483
|
+
// A transport alone (DEC-098), named `playbar` because `transport` is the word editors give the object that
|
|
484
|
+
// reads musical time: a player told to conduct the Session it is lent, so the box plays, pauses and
|
|
485
|
+
// seeks whatever the graph holds, a clock with nothing timed on it included. A component draws no transport of
|
|
486
|
+
// its own; one that is played by hand places this, a `player` or a `track`.
|
|
487
|
+
{
|
|
488
|
+
name: "playbar",
|
|
489
|
+
summary: "Play, pause and seek the graph's Session",
|
|
490
|
+
tag: "score-player",
|
|
491
|
+
plugin: pluginOf("score-player-conduct"),
|
|
492
|
+
ports: [],
|
|
493
|
+
parameters: [],
|
|
494
|
+
scope: true,
|
|
495
|
+
attributes: { controls: "show", session: "conduct", "time-control": "full", "aria-label": "Transport" },
|
|
496
|
+
locked: ["session"],
|
|
497
|
+
width: 320,
|
|
498
|
+
define: () => defineScorePlayerElement()
|
|
499
|
+
},
|
|
500
|
+
{
|
|
501
|
+
name: "rack",
|
|
502
|
+
summary: "Mixing desk: a fader, mute and solo for each cord in, and a master",
|
|
503
|
+
tag: "score-rack-control",
|
|
504
|
+
plugin: pluginOf("score-rack-control"),
|
|
505
|
+
ports: deskPorts({}),
|
|
506
|
+
portsFor: deskPorts,
|
|
507
|
+
parameters: [DESK_INPUTS],
|
|
508
|
+
attributes: { "aria-label": "Rack" },
|
|
509
|
+
width: 424,
|
|
510
|
+
define: () => defineRackControlElement()
|
|
511
|
+
},
|
|
512
|
+
{
|
|
513
|
+
name: "mixer",
|
|
514
|
+
summary: "Audio mixer: a fader, mute and solo for each cord in, and a master",
|
|
515
|
+
tag: "audio-mixer",
|
|
516
|
+
plugin: pluginOf("audio-mixer"),
|
|
517
|
+
ports: deskPorts({}),
|
|
518
|
+
portsFor: deskPorts,
|
|
519
|
+
parameters: [DESK_INPUTS],
|
|
520
|
+
attributes: { "aria-label": "Mixer" },
|
|
521
|
+
width: 424,
|
|
522
|
+
define: () => defineAudioMixerElement()
|
|
523
|
+
},
|
|
524
|
+
// A recorder takes the notes cabled to it and plays its take into the graph.
|
|
525
|
+
{
|
|
526
|
+
name: "recorder",
|
|
527
|
+
summary: "Record the incoming notes as a score, replay and export the take",
|
|
528
|
+
tag: "score-recorder",
|
|
529
|
+
plugin: pluginOf("score-recorder"),
|
|
530
|
+
ports: [{ id: "notes", family: "note", direction: "input", bind: "inputModule" }, audioOut],
|
|
531
|
+
parameters: [],
|
|
532
|
+
attributes: { "aria-label": "Score recorder" },
|
|
533
|
+
width: 320,
|
|
534
|
+
define: () => defineScoreRecorderElement()
|
|
535
|
+
},
|
|
536
|
+
// A panel attached to the module its cord comes from; without one it shows no controls.
|
|
537
|
+
{
|
|
538
|
+
name: "panel",
|
|
539
|
+
summary: "Sound, envelope and effect controls of the player cabled to it",
|
|
540
|
+
tag: "score-synth-panel",
|
|
541
|
+
plugin: pluginOf("score-synth-panel"),
|
|
542
|
+
ports: [{ id: "target", family: "source", direction: "input", bind: "moduleTarget" }],
|
|
543
|
+
parameters: [],
|
|
544
|
+
attributes: { sections: "sound,envelope,effects", "aria-label": "Synth panel" },
|
|
545
|
+
width: 320,
|
|
546
|
+
define: () => defineSynthPanelElement()
|
|
547
|
+
},
|
|
548
|
+
// A read-only waveform of a named buffer; `clip` is the one that edits. The Element's spectrogram and meter types
|
|
549
|
+
// need data a buffer does not carry, so the type stays `waveform`.
|
|
550
|
+
// A read-only drawing: of a named buffer, or of the track cabled to it, following its playhead.
|
|
551
|
+
{
|
|
552
|
+
name: "wave",
|
|
553
|
+
summary: "Waveform or spectrogram of a named buffer, or of the track cabled to it",
|
|
554
|
+
tag: "audio-view",
|
|
555
|
+
plugin: pluginOf("audio-view"),
|
|
556
|
+
// Its source is its player (DEC-094): the module the cord carries, whose owner the view follows.
|
|
557
|
+
ports: [sourceIn],
|
|
558
|
+
parameters: [
|
|
559
|
+
{ name: "buffer", label: "name", kind: "word", fallback: "", buffer: "clip" },
|
|
560
|
+
{ name: "type", label: "type", kind: "word", fallback: "waveform", choices: ["waveform", "spectrogram"], attribute: "type" }
|
|
561
|
+
],
|
|
562
|
+
spectrogram: true,
|
|
563
|
+
attributes: { height: "96", "aria-label": "Audio view" },
|
|
564
|
+
width: 320,
|
|
565
|
+
define: () => defineAudioViewElement()
|
|
566
|
+
},
|
|
567
|
+
// The two companions of `track`: cabled from it, they play through it and its Session.
|
|
568
|
+
{
|
|
569
|
+
name: "take",
|
|
570
|
+
summary: "Record the microphone; the take goes to a named buffer and plays through the track cabled to it",
|
|
571
|
+
tag: "audio-recorder",
|
|
572
|
+
plugin: pluginOf("audio-recorder"),
|
|
573
|
+
ports: [{ id: "player", family: "source", direction: "input", bind: "player" }],
|
|
574
|
+
parameters: [{ name: "buffer", label: "name", kind: "word", fallback: "", buffer: "take" }],
|
|
575
|
+
scope: true,
|
|
576
|
+
attributes: { "aria-label": "Audio recorder" },
|
|
577
|
+
width: 320,
|
|
578
|
+
define: () => defineAudioRecorderElement()
|
|
579
|
+
},
|
|
580
|
+
{
|
|
581
|
+
name: "playlist",
|
|
582
|
+
summary: "A queue of recordings, played in order through the track cabled to it",
|
|
583
|
+
tag: "audio-playlist",
|
|
584
|
+
plugin: pluginOf("audio-playlist"),
|
|
585
|
+
ports: [{ id: "player", family: "source", direction: "input", bind: "player" }, audioOut],
|
|
586
|
+
parameters: [],
|
|
587
|
+
asset: { role: "entry", kind: "entries", property: "entries" },
|
|
588
|
+
attributes: { "aria-label": "Playlist" },
|
|
589
|
+
width: 320,
|
|
590
|
+
define: () => defineAudioPlaylistElement()
|
|
591
|
+
},
|
|
592
|
+
// The panel's insert mode: the audio cabled in passes through its effects, then its EQ.
|
|
593
|
+
{
|
|
594
|
+
name: "eq",
|
|
595
|
+
summary: "Insert: the effects it names, then a three-band equalizer, with their controls",
|
|
596
|
+
tag: "score-synth-panel",
|
|
597
|
+
plugin: pluginOf("score-synth-panel-eq"),
|
|
598
|
+
ports: [{ id: "input", family: "audio", direction: "input", bind: "insert" }, { id: "output", family: "audio", direction: "output", bind: "insert" }],
|
|
599
|
+
parameters: [{ name: "effects", label: "effects", kind: "word", fallback: "", list: SCORE_EFFECTS }],
|
|
600
|
+
attributes: { mode: "insert", sections: "effects,eq", "aria-label": "Equalizer" },
|
|
601
|
+
locked: ["mode"],
|
|
602
|
+
width: 320,
|
|
603
|
+
configure: (element, config) => {
|
|
604
|
+
const names = String(config.effects ?? "").split(",").map((name) => name.trim()).filter((name) => SCORE_EFFECTS.includes(name));
|
|
605
|
+
element.effects = names.map((name) => {
|
|
606
|
+
const recipe = Effect[name]();
|
|
607
|
+
recipe.label = name;
|
|
608
|
+
return recipe;
|
|
609
|
+
});
|
|
610
|
+
},
|
|
611
|
+
define: () => defineSynthPanelElement()
|
|
612
|
+
},
|
|
613
|
+
// Declarations wrapped as components (DEC-089). An effect is one insert on the player its cord comes from.
|
|
614
|
+
{
|
|
615
|
+
name: "effect",
|
|
616
|
+
summary: "One insert on the score player cabled to it; its controls are the player's panel",
|
|
617
|
+
tag: "score-effect",
|
|
618
|
+
plugin: pluginOf("score-effect"),
|
|
619
|
+
ports: [{ id: "target", family: "source", direction: "input", bind: "moduleTarget" }],
|
|
620
|
+
parameters: [{ name: "type", label: "type", kind: "word", fallback: "gain", choices: SCORE_EFFECTS, attribute: "type" }],
|
|
621
|
+
attributes: { "aria-label": "Effect" },
|
|
622
|
+
width: 160,
|
|
623
|
+
define: () => defineScoreEffectElement()
|
|
624
|
+
},
|
|
625
|
+
{
|
|
626
|
+
name: "audio-effect",
|
|
627
|
+
summary: "One insert on the track cabled to it",
|
|
628
|
+
tag: "audio-effect",
|
|
629
|
+
plugin: pluginOf("audio-effect"),
|
|
630
|
+
ports: [{ id: "target", family: "source", direction: "input", bind: "moduleTarget" }],
|
|
631
|
+
parameters: [{ name: "type", label: "type", kind: "word", fallback: "gain", choices: AUDIO_EFFECT_TYPES, attribute: "type" }],
|
|
632
|
+
attributes: { "aria-label": "Audio effect" },
|
|
633
|
+
width: 160,
|
|
634
|
+
define: () => defineAudioEffectElement()
|
|
635
|
+
},
|
|
636
|
+
// One voice of a desk: placed in a desk of its own, which gives it a strip and plays it into the box's output.
|
|
637
|
+
{
|
|
638
|
+
name: "part",
|
|
639
|
+
summary: "One voice: a score and an oscillator, with its own fader, in the graph's Session",
|
|
640
|
+
tag: "score-rack-part",
|
|
641
|
+
plugin: pluginOf("score-rack-part"),
|
|
642
|
+
ports: [audioOut, { id: "source", family: "source", direction: "output", bind: "module" }],
|
|
643
|
+
parameters: [{ name: "sound", label: "sound", kind: "word", fallback: "triangle", choices: RACK_PART_SOUNDS, attribute: "sound" }],
|
|
644
|
+
asset: { role: "source", kind: "score", property: "score" },
|
|
645
|
+
container: { tag: "score-rack-control", attributes: { master: "false", "aria-label": "Part" }, define: () => defineRackControlElement() },
|
|
646
|
+
width: 96,
|
|
647
|
+
define: () => defineRackPartElement()
|
|
648
|
+
}
|
|
649
|
+
]);
|
|
650
|
+
function componentManifestOf(plugin) {
|
|
651
|
+
return OFFICIAL_COMPONENTS.find((manifest) => manifest.plugin === plugin);
|
|
652
|
+
}
|
|
653
|
+
function manifestFor(manifest, config) {
|
|
654
|
+
if (!manifest.portsFor) return manifest;
|
|
655
|
+
return Object.freeze({ ...manifest, ports: manifest.portsFor(config) });
|
|
656
|
+
}
|
|
657
|
+
function findComponentManifest(name) {
|
|
658
|
+
return OFFICIAL_COMPONENTS.find((manifest) => manifest.name === name);
|
|
659
|
+
}
|
|
660
|
+
var PORT_FAMILIES = ["message", "audio", "note", "source"];
|
|
661
|
+
function componentPorts(record) {
|
|
662
|
+
const found = componentManifestOf(record.plugin.id);
|
|
663
|
+
const manifest = found && manifestFor(found, record.config);
|
|
664
|
+
if (manifest) return Object.freeze(manifest.ports.map(({ id, family, direction }) => Object.freeze({ id, family, direction })));
|
|
665
|
+
if (record.plugin.id !== NestedComponent.type) return void 0;
|
|
666
|
+
const side = (value, direction) => Array.isArray(value) ? value.flatMap((kind, index) => PORT_FAMILIES.includes(kind) ? [Object.freeze({ id: numberedPort(direction === "input" ? "in" : "out", index), family: kind, direction })] : []) : [];
|
|
667
|
+
return Object.freeze([...side(record.config.inlets, "input"), ...side(record.config.outlets, "output")]);
|
|
668
|
+
}
|
|
669
|
+
function isListener(plugin) {
|
|
670
|
+
const manifest = componentManifestOf(plugin);
|
|
671
|
+
return !!manifest && manifest.ports.length > 0 && manifest.ports.every((port) => port.bind === "analyser");
|
|
672
|
+
}
|
|
673
|
+
function parameterValue(parameter, config) {
|
|
674
|
+
const value = config[parameter.name];
|
|
675
|
+
if (parameter.kind === "number") return typeof value === "number" && Number.isFinite(value) ? value : parameter.fallback;
|
|
676
|
+
if (typeof value !== "string") return parameter.fallback;
|
|
677
|
+
if (parameter.list) return listed(parameter.list, value) ?? parameter.fallback;
|
|
678
|
+
return !parameter.choices || parameter.choices.includes(value) ? value : parameter.fallback;
|
|
679
|
+
}
|
|
680
|
+
function listed(words, value) {
|
|
681
|
+
const items = value.split(",").map((item) => item.trim()).filter(Boolean);
|
|
682
|
+
return items.every((item) => words.includes(item)) ? items.join(",") : void 0;
|
|
683
|
+
}
|
|
684
|
+
function configOfArguments(manifest, words) {
|
|
685
|
+
const config = {};
|
|
686
|
+
let error;
|
|
687
|
+
if (words.length > manifest.parameters.length) error = `${manifest.name} takes ${manifest.parameters.length || "no"} argument${manifest.parameters.length === 1 ? "" : "s"}.`;
|
|
688
|
+
manifest.parameters.forEach((parameter, index) => {
|
|
689
|
+
const word = words[index];
|
|
690
|
+
if (word === void 0) {
|
|
691
|
+
config[parameter.name] = parameter.fallback;
|
|
692
|
+
return;
|
|
693
|
+
}
|
|
694
|
+
if (parameter.kind === "word") {
|
|
695
|
+
if (parameter.list) {
|
|
696
|
+
const value = listed(parameter.list, word);
|
|
697
|
+
if (value === void 0) error ?? (error = `${manifest.name} ${parameter.label} must list ${parameter.list.join(", ")}, separated by commas.`);
|
|
698
|
+
config[parameter.name] = value ?? parameter.fallback;
|
|
699
|
+
} else if (parameter.choices && !parameter.choices.includes(word)) {
|
|
700
|
+
error ?? (error = `${manifest.name} ${parameter.label} must be one of ${parameter.choices.join(", ")}.`);
|
|
701
|
+
config[parameter.name] = parameter.fallback;
|
|
702
|
+
} else config[parameter.name] = word;
|
|
703
|
+
return;
|
|
704
|
+
}
|
|
705
|
+
const number2 = Number(word);
|
|
706
|
+
if (!Number.isFinite(number2)) {
|
|
707
|
+
error ?? (error = `${parameter.label} must be a number.`);
|
|
708
|
+
config[parameter.name] = parameter.fallback;
|
|
709
|
+
return;
|
|
710
|
+
}
|
|
711
|
+
const min = parameter.min ?? -Infinity, max = parameter.max ?? Infinity;
|
|
712
|
+
if (number2 < min || number2 > max) error ?? (error = `${manifest.name} ${parameter.label} must be within ${min}\u2013${max}.`);
|
|
713
|
+
config[parameter.name] = Math.min(max, Math.max(min, number2));
|
|
714
|
+
});
|
|
715
|
+
return error ? { config, error } : { config };
|
|
716
|
+
}
|
|
717
|
+
function componentUsage(manifest) {
|
|
718
|
+
return [manifest.name, ...manifest.parameters.map((parameter) => parameter.fallback === "" ? `[${parameter.label}]` : `[${parameter.label}=${parameter.fallback}]`)].join(" ");
|
|
719
|
+
}
|
|
720
|
+
var EDGE_FAMILIES = ["message", "audio", "note", "source"];
|
|
721
|
+
function projectManifest(input, contextId) {
|
|
722
|
+
const project = parseModuleProject(input);
|
|
723
|
+
const top = new Set(contextId !== void 0 ? [contextId] : project.contexts.filter((context) => context.parentId === void 0).map((context) => context.id));
|
|
724
|
+
const ports = /* @__PURE__ */ new Map();
|
|
725
|
+
for (const record of project.modules) {
|
|
726
|
+
const direction = record.plugin.id === InletModule.type ? "input" : record.plugin.id === OutletModule.type ? "output" : void 0;
|
|
727
|
+
if (!top.has(record.contextId) || !direction) continue;
|
|
728
|
+
const family = record.config.kind === void 0 ? "message" : record.config.kind;
|
|
729
|
+
if (!EDGE_FAMILIES.includes(family)) continue;
|
|
730
|
+
const index = record.config.index;
|
|
731
|
+
const number2 = typeof index === "number" && Number.isFinite(index) ? Math.max(0, Math.trunc(index)) : 0;
|
|
732
|
+
const id = numberedPort(direction === "input" ? "in" : "out", number2);
|
|
733
|
+
if (!ports.has(id)) ports.set(id, Object.freeze({ id, family, direction }));
|
|
734
|
+
}
|
|
735
|
+
const order = (port) => (port.direction === "input" ? 0 : 1e6) + Number(port.id.replace(/\D+/g, ""));
|
|
736
|
+
return Object.freeze({
|
|
737
|
+
ports: Object.freeze([...ports.values()].sort((a, b) => order(a) - order(b))),
|
|
738
|
+
assets: Object.freeze([...project.assets])
|
|
739
|
+
});
|
|
740
|
+
}
|
|
741
|
+
var BUFFER_PLUGIN = BufferModule.type;
|
|
742
|
+
var COMPONENT_PLUGIN_VERSION = "1.0.0";
|
|
743
|
+
function migrateBufferViews(input) {
|
|
744
|
+
const project = parseModuleProject(input);
|
|
745
|
+
const byId = new Map(project.modules.map((record) => [record.id, record]));
|
|
746
|
+
const presentation = project.layout.presentation ?? [];
|
|
747
|
+
if (!presentation.some((item) => byId.get(item.moduleId)?.plugin.id === BUFFER_PLUGIN)) return project;
|
|
748
|
+
const ids = new Set(project.modules.map((record) => record.id));
|
|
749
|
+
const positions = new Map(project.layout.modules.map((item) => [item.moduleId, item]));
|
|
750
|
+
const modules = [...project.modules];
|
|
751
|
+
const layout = [...project.layout.modules];
|
|
752
|
+
const clips = /* @__PURE__ */ new Map();
|
|
753
|
+
for (const item of presentation) {
|
|
754
|
+
const record = byId.get(item.moduleId);
|
|
755
|
+
if (record?.plugin.id !== BUFFER_PLUGIN || clips.has(record.id)) continue;
|
|
756
|
+
const name = record.config.name;
|
|
757
|
+
if (typeof name !== "string" || !name) continue;
|
|
758
|
+
let id = `${record.id}-clip`;
|
|
759
|
+
while (ids.has(id)) id = `${id}_`;
|
|
760
|
+
ids.add(id);
|
|
761
|
+
clips.set(record.id, id);
|
|
762
|
+
modules.push({ id, contextId: record.contextId, plugin: { id: CLIP_PLUGIN, version: COMPONENT_PLUGIN_VERSION }, label: `clip ${name}`, config: { buffer: name } });
|
|
763
|
+
const at = positions.get(record.id);
|
|
764
|
+
layout.push({ moduleId: id, x: at?.x ?? 0, y: (at?.y ?? 0) + (at?.height ?? 32) + 16 });
|
|
765
|
+
}
|
|
766
|
+
return parseModuleProject({
|
|
767
|
+
...project,
|
|
768
|
+
modules,
|
|
769
|
+
layout: {
|
|
770
|
+
...project.layout,
|
|
771
|
+
modules: layout,
|
|
772
|
+
// A buffer that names nothing has no waveform to show: it leaves the presentation.
|
|
773
|
+
presentation: presentation.flatMap((item) => {
|
|
774
|
+
if (byId.get(item.moduleId)?.plugin.id !== BUFFER_PLUGIN) return [item];
|
|
775
|
+
const clip = clips.get(item.moduleId);
|
|
776
|
+
return clip ? [{ ...item, moduleId: clip }] : [];
|
|
777
|
+
})
|
|
778
|
+
}
|
|
779
|
+
});
|
|
780
|
+
}
|
|
781
|
+
|
|
782
|
+
// src/component/settings.ts
|
|
783
|
+
var GRAPH_ATTRIBUTES = Object.freeze([
|
|
784
|
+
"id",
|
|
785
|
+
"class",
|
|
786
|
+
"style",
|
|
787
|
+
"slot",
|
|
788
|
+
"part",
|
|
789
|
+
"is",
|
|
790
|
+
"src",
|
|
791
|
+
"format",
|
|
792
|
+
"peaks-src",
|
|
793
|
+
"input",
|
|
794
|
+
"port",
|
|
795
|
+
"target",
|
|
796
|
+
"session",
|
|
797
|
+
"sound-font",
|
|
798
|
+
"device-id"
|
|
799
|
+
]);
|
|
800
|
+
var TOKEN_NAME = /^--wm-[a-z0-9-]+$/;
|
|
801
|
+
var LOADS = /(?:url|src|image-set|image|cross-fade|element|paint)\s*\(/i;
|
|
802
|
+
var MAX_VALUE = 256;
|
|
803
|
+
var registryOf = (registry) => registry ?? globalThis.customElements;
|
|
804
|
+
function settableAttributes(manifest, registry) {
|
|
805
|
+
const observed = registryOf(registry)?.get(manifest.tag)?.observedAttributes ?? [];
|
|
806
|
+
const taken = /* @__PURE__ */ new Set([...GRAPH_ATTRIBUTES, ...manifest.locked ?? [], ...manifest.parameters.flatMap((parameter) => parameter.attribute ? [parameter.attribute] : [])]);
|
|
807
|
+
return observed.filter((name) => !taken.has(name));
|
|
808
|
+
}
|
|
809
|
+
function isStyleSetting(name, value) {
|
|
810
|
+
return TOKEN_NAME.test(name) && typeof value === "string" && value.trim() !== "" && value.length <= MAX_VALUE && !/[;{}<>]/.test(value) && !LOADS.test(value);
|
|
811
|
+
}
|
|
812
|
+
var entries = (value) => value !== null && typeof value === "object" && !Array.isArray(value) ? Object.entries(value) : [];
|
|
813
|
+
function styleSource(config) {
|
|
814
|
+
return config.styling === "inherit" ? "inherit" : "own";
|
|
815
|
+
}
|
|
816
|
+
function withStyleSource(config, source) {
|
|
817
|
+
const { styling: _previous, ...rest } = config;
|
|
818
|
+
return source === "inherit" ? { ...rest, styling: "inherit" } : rest;
|
|
819
|
+
}
|
|
820
|
+
function isColorAttribute(name) {
|
|
821
|
+
return name === "color" || name.endsWith("-color");
|
|
822
|
+
}
|
|
823
|
+
function componentSettings(manifest, config, registry) {
|
|
824
|
+
const settable = new Set(settableAttributes(manifest, registry));
|
|
825
|
+
const own = styleSource(config) === "own";
|
|
826
|
+
const attributes = {};
|
|
827
|
+
for (const [name, value] of entries(config.attributes)) {
|
|
828
|
+
if (settable.has(name) && typeof value === "string" && value.length <= MAX_VALUE && (own || !isColorAttribute(name))) attributes[name] = value;
|
|
829
|
+
}
|
|
830
|
+
const style = {};
|
|
831
|
+
if (own) {
|
|
832
|
+
for (const [name, value] of entries(config.style)) if (isStyleSetting(name, value)) style[name] = value.trim();
|
|
833
|
+
}
|
|
834
|
+
return { attributes, style };
|
|
835
|
+
}
|
|
836
|
+
function withComponentSetting(config, kind, name, value) {
|
|
837
|
+
const group = Object.fromEntries(entries(config[kind]).filter(([key, item]) => key !== name && typeof item === "string"));
|
|
838
|
+
if (value !== void 0) group[name] = value;
|
|
839
|
+
const { [kind]: _previous, ...rest } = config;
|
|
840
|
+
return Object.keys(group).length ? { ...rest, [kind]: group } : rest;
|
|
841
|
+
}
|
|
842
|
+
|
|
843
|
+
// src/component/document.ts
|
|
844
|
+
import { scoreFromJSON } from "@webmusic/score";
|
|
845
|
+
var SAVED_SCORE_FORMAT = "webmusic.score+json";
|
|
846
|
+
var SAVED_SCORE_ENCODING = "gzip+base64";
|
|
847
|
+
var CHUNK = 32768;
|
|
848
|
+
var MAX_ENCODED = 1e6;
|
|
849
|
+
var MAX_INFLATED = 24 * 1024 * 1024;
|
|
850
|
+
var streams = () => globalThis;
|
|
851
|
+
function canSaveScores() {
|
|
852
|
+
return typeof streams().CompressionStream === "function" && typeof streams().DecompressionStream === "function";
|
|
853
|
+
}
|
|
854
|
+
async function through(bytes, stream, limit) {
|
|
855
|
+
const writer = stream.writable.getWriter();
|
|
856
|
+
void writer.write(bytes).then(() => writer.close()).catch(() => {
|
|
857
|
+
});
|
|
858
|
+
const reader = stream.readable.getReader();
|
|
859
|
+
const parts = [];
|
|
860
|
+
let size = 0;
|
|
861
|
+
for (; ; ) {
|
|
862
|
+
const { done, value } = await reader.read();
|
|
863
|
+
if (done) break;
|
|
864
|
+
size += value.length;
|
|
865
|
+
if (size > limit) {
|
|
866
|
+
void reader.cancel().catch(() => {
|
|
867
|
+
});
|
|
868
|
+
throw new RangeError("A saved score exceeds its size limit.");
|
|
869
|
+
}
|
|
870
|
+
parts.push(value);
|
|
871
|
+
}
|
|
872
|
+
const out = new Uint8Array(size);
|
|
873
|
+
let at = 0;
|
|
874
|
+
for (const part of parts) {
|
|
875
|
+
out.set(part, at);
|
|
876
|
+
at += part.length;
|
|
877
|
+
}
|
|
878
|
+
return out;
|
|
879
|
+
}
|
|
880
|
+
function toBase64(bytes) {
|
|
881
|
+
let binary = "";
|
|
882
|
+
for (let at = 0; at < bytes.length; at += 32768) binary += String.fromCharCode(...bytes.subarray(at, at + 32768));
|
|
883
|
+
return btoa(binary);
|
|
884
|
+
}
|
|
885
|
+
function fromBase64(text) {
|
|
886
|
+
const binary = atob(text);
|
|
887
|
+
const bytes = new Uint8Array(binary.length);
|
|
888
|
+
for (let at = 0; at < binary.length; at += 1) bytes[at] = binary.charCodeAt(at);
|
|
889
|
+
return bytes;
|
|
890
|
+
}
|
|
891
|
+
async function encodeScore(score) {
|
|
892
|
+
const Compression = streams().CompressionStream;
|
|
893
|
+
if (!Compression || !canSaveScores()) return void 0;
|
|
894
|
+
const json2 = new TextEncoder().encode(JSON.stringify(score.toJSON()));
|
|
895
|
+
if (json2.length > MAX_INFLATED) return void 0;
|
|
896
|
+
const text = toBase64(await through(json2, new Compression("gzip"), MAX_ENCODED));
|
|
897
|
+
if (text.length > MAX_ENCODED) return void 0;
|
|
898
|
+
const data = [];
|
|
899
|
+
for (let at = 0; at < text.length; at += CHUNK) data.push(text.slice(at, at + CHUNK));
|
|
900
|
+
return Object.freeze({ format: SAVED_SCORE_FORMAT, encoding: SAVED_SCORE_ENCODING, data: Object.freeze(data) });
|
|
901
|
+
}
|
|
902
|
+
async function decodeScore(value) {
|
|
903
|
+
if (value === null || typeof value !== "object" || Array.isArray(value)) throw new TypeError("A saved score must be an object.");
|
|
904
|
+
const { format, encoding, data } = value;
|
|
905
|
+
if (format !== SAVED_SCORE_FORMAT) throw new TypeError(`A saved score must be in the format ${SAVED_SCORE_FORMAT}.`);
|
|
906
|
+
if (encoding !== SAVED_SCORE_ENCODING) throw new TypeError(`A saved score must be encoded as ${SAVED_SCORE_ENCODING}.`);
|
|
907
|
+
if (!Array.isArray(data) || !data.length || data.some((part) => typeof part !== "string")) throw new TypeError("A saved score must hold its data as strings.");
|
|
908
|
+
let length = 0;
|
|
909
|
+
for (const part of data) if ((length += part.length) > MAX_ENCODED) throw new RangeError("A saved score exceeds its size limit.");
|
|
910
|
+
const Decompression = streams().DecompressionStream;
|
|
911
|
+
if (!Decompression) throw new Error("This browser cannot inflate a saved score.");
|
|
912
|
+
let packed;
|
|
913
|
+
try {
|
|
914
|
+
packed = fromBase64(data.join(""));
|
|
915
|
+
} catch {
|
|
916
|
+
throw new TypeError("A saved score must be base64 text.");
|
|
917
|
+
}
|
|
918
|
+
const json2 = await through(packed, new Decompression("gzip"), MAX_INFLATED).catch((error) => {
|
|
919
|
+
if (error instanceof RangeError) throw error;
|
|
920
|
+
return void 0;
|
|
921
|
+
});
|
|
922
|
+
if (!json2) throw new TypeError("A saved score does not inflate.");
|
|
923
|
+
return scoreFromJSON(JSON.parse(new TextDecoder().decode(json2)));
|
|
924
|
+
}
|
|
925
|
+
|
|
926
|
+
// src/component/theme.ts
|
|
927
|
+
import { parseModuleProject as parseModuleProject2 } from "@webmusic/kernel/module";
|
|
928
|
+
import { THEME_TOKENS, themeToken } from "@webmusic/ui";
|
|
929
|
+
var entries2 = (value) => value !== null && typeof value === "object" && !Array.isArray(value) ? Object.entries(value) : [];
|
|
930
|
+
function styleSettings(style) {
|
|
931
|
+
const out = {};
|
|
932
|
+
for (const [name, value] of entries2(style)) if (isStyleSetting(name, value)) out[name] = value.trim();
|
|
933
|
+
return out;
|
|
934
|
+
}
|
|
935
|
+
function ownStyle(config) {
|
|
936
|
+
return styleSource(config) === "inherit" ? {} : styleSettings(config.style);
|
|
937
|
+
}
|
|
938
|
+
function componentTheme(project) {
|
|
939
|
+
return styleSettings(project.layout.style);
|
|
940
|
+
}
|
|
941
|
+
function withComponentTheme(project, name, value) {
|
|
942
|
+
if (value !== void 0 && !isStyleSetting(name, value)) throw new TypeError(`${name} cannot be themed with that value: a theme sets --wm- tokens to values that load nothing.`);
|
|
943
|
+
const style = Object.fromEntries(Object.entries(project.layout.style ?? {}).filter(([key]) => key !== name));
|
|
944
|
+
if (value !== void 0) style[name] = value.trim();
|
|
945
|
+
const { style: _previous, ...layout } = project.layout;
|
|
946
|
+
return parseModuleProject2({ ...project, layout: Object.keys(style).length ? { ...layout, style } : layout });
|
|
947
|
+
}
|
|
948
|
+
function styleDeclarations(style) {
|
|
949
|
+
return Object.entries(styleSettings(style)).map(([name, value]) => `${name}: ${value};`).join(" ");
|
|
950
|
+
}
|
|
951
|
+
var written = /* @__PURE__ */ new WeakMap();
|
|
952
|
+
function applyComponentStyle(element, style) {
|
|
953
|
+
const next = styleSettings(style);
|
|
954
|
+
const before = written.get(element) ?? /* @__PURE__ */ new Set();
|
|
955
|
+
for (const name of before) if (!(name in next)) element.style.removeProperty(name);
|
|
956
|
+
for (const [name, value] of Object.entries(next)) element.style.setProperty(name, value);
|
|
957
|
+
written.set(element, new Set(Object.keys(next)));
|
|
958
|
+
}
|
|
959
|
+
|
|
960
|
+
// src/component/surfaces.ts
|
|
961
|
+
import { AudioClip } from "@webmusic/audio";
|
|
962
|
+
import {
|
|
963
|
+
AudioOutputPort,
|
|
964
|
+
NoteInputPort,
|
|
965
|
+
NoteOutputPort,
|
|
966
|
+
SourceRelay,
|
|
967
|
+
createConnectionLease,
|
|
968
|
+
routeModuleOutput,
|
|
969
|
+
sourceOrigin,
|
|
970
|
+
ProjectModuleUnavailableError
|
|
971
|
+
} from "@webmusic/kernel/module";
|
|
972
|
+
import { Score } from "@webmusic/score";
|
|
973
|
+
import { createScoreEditor } from "@webmusic/score/view/headless";
|
|
974
|
+
var PLUGIN_VERSION = "1.0.0";
|
|
975
|
+
var _values, _listeners, _Provided_instances, notify_fn;
|
|
976
|
+
var Provided = class {
|
|
977
|
+
constructor() {
|
|
978
|
+
__privateAdd(this, _Provided_instances);
|
|
979
|
+
__privateAdd(this, _values, []);
|
|
980
|
+
__privateAdd(this, _listeners, /* @__PURE__ */ new Set());
|
|
981
|
+
}
|
|
982
|
+
get current() {
|
|
983
|
+
return __privateGet(this, _values)[__privateGet(this, _values).length - 1]?.value;
|
|
984
|
+
}
|
|
985
|
+
provide(value) {
|
|
986
|
+
const entry = { value };
|
|
987
|
+
__privateGet(this, _values).push(entry);
|
|
988
|
+
__privateMethod(this, _Provided_instances, notify_fn).call(this);
|
|
989
|
+
return () => {
|
|
990
|
+
const index = __privateGet(this, _values).indexOf(entry);
|
|
991
|
+
if (index < 0) return;
|
|
992
|
+
__privateGet(this, _values).splice(index, 1);
|
|
993
|
+
__privateMethod(this, _Provided_instances, notify_fn).call(this);
|
|
994
|
+
};
|
|
995
|
+
}
|
|
996
|
+
subscribe(listener) {
|
|
997
|
+
__privateGet(this, _listeners).add(listener);
|
|
998
|
+
return () => {
|
|
999
|
+
__privateGet(this, _listeners).delete(listener);
|
|
1000
|
+
};
|
|
1001
|
+
}
|
|
1002
|
+
};
|
|
1003
|
+
_values = new WeakMap();
|
|
1004
|
+
_listeners = new WeakMap();
|
|
1005
|
+
_Provided_instances = new WeakSet();
|
|
1006
|
+
notify_fn = function() {
|
|
1007
|
+
for (const listener of [...__privateGet(this, _listeners)]) {
|
|
1008
|
+
try {
|
|
1009
|
+
listener();
|
|
1010
|
+
} catch (error) {
|
|
1011
|
+
queueMicrotask(() => {
|
|
1012
|
+
throw error;
|
|
1013
|
+
});
|
|
1014
|
+
}
|
|
1015
|
+
}
|
|
1016
|
+
};
|
|
1017
|
+
var SurfaceSlot = class {
|
|
1018
|
+
constructor() {
|
|
1019
|
+
/** The analyser an audio input hands the Element's `analyser`. */
|
|
1020
|
+
this.analyser = new Provided();
|
|
1021
|
+
/** The module a source input hands the Element's `inputModule`, through its `source` cord. */
|
|
1022
|
+
this.source = new Provided();
|
|
1023
|
+
/** The note output of the newest module; the Element's `module` plays into it. */
|
|
1024
|
+
this.notes = new Provided();
|
|
1025
|
+
/** The module context lent to a player or desk: the graph's Session, with this box's output as its destination. */
|
|
1026
|
+
this.context = new Provided();
|
|
1027
|
+
/** A desk's inputs, which the Element admits as channels. */
|
|
1028
|
+
this.channels = new Provided();
|
|
1029
|
+
/** A module whose `notes` output repeats the notes cabled to this box; the Element's `inputModule` listens to it. */
|
|
1030
|
+
this.noteSource = new Provided();
|
|
1031
|
+
/** The asset the box loaded for the Element: a Score, an AudioClip, or a playlist's entries. */
|
|
1032
|
+
this.asset = new Provided();
|
|
1033
|
+
/** The Element's own module, provided by the host; a source cord from this box carries it. */
|
|
1034
|
+
this.element = new Provided();
|
|
1035
|
+
/** An insert's ends: the graph's AudioContext, the node the box's input feeds and the node its output plays from. */
|
|
1036
|
+
this.insert = new Provided();
|
|
1037
|
+
/** The Element itself, provided by the host, for a companion that links to its player. */
|
|
1038
|
+
this.elementOwner = new Provided();
|
|
1039
|
+
/** The Element of the box a `source` cord comes from, for a `player` binding. */
|
|
1040
|
+
this.sourceOwner = new Provided();
|
|
1041
|
+
/** True when a source cord from this box carries the Element's module rather than the box's. */
|
|
1042
|
+
this.forwardsElement = false;
|
|
1043
|
+
/** The Score this box plays, when it plays one: what its document starts from. */
|
|
1044
|
+
this.score = new Provided();
|
|
1045
|
+
/** The document this box's score became once a view asked to edit it (DEC-086): the box plays it. */
|
|
1046
|
+
this.document = new Provided();
|
|
1047
|
+
/**
|
|
1048
|
+
* For a box with a source input: lends the document of the box its cord
|
|
1049
|
+
* comes from, making it on the first call. A view calls it when it edits;
|
|
1050
|
+
* it returns nothing while that box has no score.
|
|
1051
|
+
*/
|
|
1052
|
+
this.lender = new Provided();
|
|
1053
|
+
}
|
|
1054
|
+
};
|
|
1055
|
+
var _slots, _byModule, _documents, _alive, _pending, _listeners2, _ComponentSurfaces_instances, edited_fn, keep_fn, encode_fn, notify_fn2, boxOf_fn, create_fn, build_fn;
|
|
1056
|
+
var ComponentSurfaces = class {
|
|
1057
|
+
constructor() {
|
|
1058
|
+
__privateAdd(this, _ComponentSurfaces_instances);
|
|
1059
|
+
__privateAdd(this, _slots, /* @__PURE__ */ new Map());
|
|
1060
|
+
__privateAdd(this, _byModule, /* @__PURE__ */ new WeakMap());
|
|
1061
|
+
__privateAdd(this, _documents, /* @__PURE__ */ new Map());
|
|
1062
|
+
__privateAdd(this, _alive, /* @__PURE__ */ new Map());
|
|
1063
|
+
__privateAdd(this, _pending, /* @__PURE__ */ new Set());
|
|
1064
|
+
__privateAdd(this, _listeners2, /* @__PURE__ */ new Set());
|
|
1065
|
+
/**
|
|
1066
|
+
* The runtime connector for `source` cords: a source input follows the
|
|
1067
|
+
* upstream module. From a player's box it follows the player's own module,
|
|
1068
|
+
* which the host provides once the Element has one.
|
|
1069
|
+
*/
|
|
1070
|
+
this.connect = ({ connection, from }) => {
|
|
1071
|
+
if (connection.kind !== "source") return void 0;
|
|
1072
|
+
const reached = this.slot(connection.to.moduleId);
|
|
1073
|
+
const target = reached.source;
|
|
1074
|
+
const sourceId = connection.from.moduleId;
|
|
1075
|
+
const upstream = __privateGet(this, _slots).get(sourceId);
|
|
1076
|
+
const origin = this.slot(sourceId);
|
|
1077
|
+
let offered;
|
|
1078
|
+
const offer = () => {
|
|
1079
|
+
const previous = offered;
|
|
1080
|
+
offered = reached.lender.provide(() => this.document(sourceId));
|
|
1081
|
+
previous?.();
|
|
1082
|
+
};
|
|
1083
|
+
offer();
|
|
1084
|
+
const unoffers = [origin.score.subscribe(offer), origin.document.subscribe(offer)];
|
|
1085
|
+
const withdraw = () => {
|
|
1086
|
+
for (const unoffer of unoffers) unoffer();
|
|
1087
|
+
offered?.();
|
|
1088
|
+
offered = void 0;
|
|
1089
|
+
};
|
|
1090
|
+
if (!upstream?.forwardsElement) {
|
|
1091
|
+
const release = target.provide(from.module);
|
|
1092
|
+
const relay = from.module instanceof SourceRelay ? from.module : void 0;
|
|
1093
|
+
if (!relay) return createConnectionLease(() => {
|
|
1094
|
+
withdraw();
|
|
1095
|
+
release();
|
|
1096
|
+
});
|
|
1097
|
+
let owned;
|
|
1098
|
+
const own = () => {
|
|
1099
|
+
owned?.();
|
|
1100
|
+
const element = __privateMethod(this, _ComponentSurfaces_instances, boxOf_fn).call(this, relay)?.elementOwner.current;
|
|
1101
|
+
owned = element ? reached.sourceOwner.provide(element) : void 0;
|
|
1102
|
+
};
|
|
1103
|
+
own();
|
|
1104
|
+
const stop = relay.subscribe(own);
|
|
1105
|
+
return createConnectionLease(() => {
|
|
1106
|
+
withdraw();
|
|
1107
|
+
stop();
|
|
1108
|
+
owned?.();
|
|
1109
|
+
owned = void 0;
|
|
1110
|
+
release();
|
|
1111
|
+
});
|
|
1112
|
+
}
|
|
1113
|
+
const follow = (source, into) => {
|
|
1114
|
+
let provided;
|
|
1115
|
+
const apply = () => {
|
|
1116
|
+
provided?.();
|
|
1117
|
+
const value = source.current;
|
|
1118
|
+
provided = value !== void 0 ? into.provide(value) : void 0;
|
|
1119
|
+
};
|
|
1120
|
+
apply();
|
|
1121
|
+
const unsubscribe = source.subscribe(apply);
|
|
1122
|
+
return () => {
|
|
1123
|
+
unsubscribe();
|
|
1124
|
+
provided?.();
|
|
1125
|
+
provided = void 0;
|
|
1126
|
+
};
|
|
1127
|
+
};
|
|
1128
|
+
const stops = [follow(upstream.element, target), follow(upstream.elementOwner, reached.sourceOwner)];
|
|
1129
|
+
return createConnectionLease(() => {
|
|
1130
|
+
withdraw();
|
|
1131
|
+
for (const stop of stops) stop();
|
|
1132
|
+
});
|
|
1133
|
+
};
|
|
1134
|
+
}
|
|
1135
|
+
/**
|
|
1136
|
+
* The document of the box `id`: the editor that owns the score it plays
|
|
1137
|
+
* (DEC-086). Made from the box's score the first time it is asked for and
|
|
1138
|
+
* kept while the box is in the graph, whatever is rebuilt; undefined while
|
|
1139
|
+
* the box has no score. `peek` only looks.
|
|
1140
|
+
*/
|
|
1141
|
+
document(id, peek = false) {
|
|
1142
|
+
const known = __privateGet(this, _documents).get(id);
|
|
1143
|
+
if (known || peek) return known?.editor;
|
|
1144
|
+
const score = this.slot(id).score.current;
|
|
1145
|
+
if (!score) return void 0;
|
|
1146
|
+
return __privateMethod(this, _ComponentSurfaces_instances, keep_fn).call(this, id, createScoreEditor(score)).editor;
|
|
1147
|
+
}
|
|
1148
|
+
/**
|
|
1149
|
+
* What the box `id` holds of the score it plays (DEC-087); undefined when
|
|
1150
|
+
* it plays none.
|
|
1151
|
+
*/
|
|
1152
|
+
documentState(id) {
|
|
1153
|
+
const kept = __privateGet(this, _documents).get(id);
|
|
1154
|
+
if (!kept) return __privateGet(this, _slots).get(id)?.score.current ? "original" : void 0;
|
|
1155
|
+
const contentId = kept.editor.getSnapshot().contentId;
|
|
1156
|
+
if (contentId === kept.initial) return kept.restored === void 0 ? "original" : "edited";
|
|
1157
|
+
return kept.saved?.contentId === contentId && kept.saved?.value === void 0 ? "unsaved" : "edited";
|
|
1158
|
+
}
|
|
1159
|
+
/**
|
|
1160
|
+
* `config` as the box `id` is saved (DEC-087): with its edited score under
|
|
1161
|
+
* `document`, and without one once the document is the asset again. A box
|
|
1162
|
+
* nobody edited keeps the config it has. The newest edit is encoded off
|
|
1163
|
+
* the call, so it is in the config once `settled` resolves; subscribers
|
|
1164
|
+
* are told when it is.
|
|
1165
|
+
*/
|
|
1166
|
+
withDocument(id, config) {
|
|
1167
|
+
const kept = __privateGet(this, _documents).get(id);
|
|
1168
|
+
if (!kept) return config;
|
|
1169
|
+
const { document: _previous, ...rest } = config;
|
|
1170
|
+
const value = kept.editor.getSnapshot().contentId === kept.initial ? kept.restored : kept.saved?.value ?? kept.restored;
|
|
1171
|
+
return value === void 0 ? rest : { ...rest, document: value };
|
|
1172
|
+
}
|
|
1173
|
+
/**
|
|
1174
|
+
* Start the box `id` from the score its `config` saved (DEC-087). A
|
|
1175
|
+
* document already edited here stays: it is newer than any config. Rejects
|
|
1176
|
+
* with `ProjectModuleUnavailableError` when the saved score cannot be read,
|
|
1177
|
+
* so the box stays unresolved with its config, and nothing is lost.
|
|
1178
|
+
*/
|
|
1179
|
+
async revive(id, config, signal) {
|
|
1180
|
+
const saved = config.document;
|
|
1181
|
+
if (saved === void 0 || __privateMethod(this, _ComponentSurfaces_instances, edited_fn).call(this, id)) return;
|
|
1182
|
+
let editor;
|
|
1183
|
+
try {
|
|
1184
|
+
editor = createScoreEditor(await decodeScore(saved));
|
|
1185
|
+
} catch (error) {
|
|
1186
|
+
throw new ProjectModuleUnavailableError(`The saved score of ${id} cannot be read: ${error instanceof Error ? error.message : String(error)}`, { cause: error });
|
|
1187
|
+
}
|
|
1188
|
+
signal?.throwIfAborted();
|
|
1189
|
+
if (__privateMethod(this, _ComponentSurfaces_instances, edited_fn).call(this, id)) return;
|
|
1190
|
+
const untouched = __privateGet(this, _documents).get(id);
|
|
1191
|
+
__privateMethod(this, _ComponentSurfaces_instances, keep_fn).call(this, id, editor, saved);
|
|
1192
|
+
untouched?.release();
|
|
1193
|
+
__privateMethod(this, _ComponentSurfaces_instances, notify_fn2).call(this);
|
|
1194
|
+
}
|
|
1195
|
+
/**
|
|
1196
|
+
* Give the box `id` its asset back (DEC-087): its edited document is let
|
|
1197
|
+
* go and a new one, made from the score the box loaded, takes its place, so
|
|
1198
|
+
* the box plays the asset at once and the views that edit borrow it. The
|
|
1199
|
+
* box then saves no score. Nothing happens while the box has no document.
|
|
1200
|
+
*/
|
|
1201
|
+
revert(id) {
|
|
1202
|
+
const kept = __privateGet(this, _documents).get(id);
|
|
1203
|
+
if (!kept) return;
|
|
1204
|
+
const score = __privateGet(this, _slots).get(id)?.score.current;
|
|
1205
|
+
if (!score) {
|
|
1206
|
+
this.forget(id);
|
|
1207
|
+
return;
|
|
1208
|
+
}
|
|
1209
|
+
__privateMethod(this, _ComponentSurfaces_instances, keep_fn).call(this, id, createScoreEditor(score));
|
|
1210
|
+
kept.release();
|
|
1211
|
+
__privateMethod(this, _ComponentSurfaces_instances, notify_fn2).call(this);
|
|
1212
|
+
}
|
|
1213
|
+
/**
|
|
1214
|
+
* Let go of the document of the box `id`, or of every box, and make none.
|
|
1215
|
+
* A host calls it before it shows another project, whose boxes may have
|
|
1216
|
+
* the same IDs; `revert` is the one for a box that stays.
|
|
1217
|
+
*/
|
|
1218
|
+
forget(id) {
|
|
1219
|
+
const ids = id === void 0 ? [...__privateGet(this, _documents).keys()] : __privateGet(this, _documents).has(id) ? [id] : [];
|
|
1220
|
+
for (const key of ids) {
|
|
1221
|
+
const kept = __privateGet(this, _documents).get(key);
|
|
1222
|
+
__privateGet(this, _documents).delete(key);
|
|
1223
|
+
kept.release();
|
|
1224
|
+
}
|
|
1225
|
+
if (ids.length) __privateMethod(this, _ComponentSurfaces_instances, notify_fn2).call(this);
|
|
1226
|
+
}
|
|
1227
|
+
/**
|
|
1228
|
+
* Count a module of the box `id` as in a graph; call the result when it is
|
|
1229
|
+
* disposed. A rebuild makes the new module before it disposes the old one,
|
|
1230
|
+
* so the document goes only when the box itself does, and a new box that
|
|
1231
|
+
* is given the same ID does not inherit it.
|
|
1232
|
+
*/
|
|
1233
|
+
enter(id) {
|
|
1234
|
+
__privateGet(this, _alive).set(id, (__privateGet(this, _alive).get(id) ?? 0) + 1);
|
|
1235
|
+
let left = false;
|
|
1236
|
+
return () => {
|
|
1237
|
+
if (left) return;
|
|
1238
|
+
left = true;
|
|
1239
|
+
const count = (__privateGet(this, _alive).get(id) ?? 1) - 1;
|
|
1240
|
+
if (count > 0) {
|
|
1241
|
+
__privateGet(this, _alive).set(id, count);
|
|
1242
|
+
return;
|
|
1243
|
+
}
|
|
1244
|
+
__privateGet(this, _alive).delete(id);
|
|
1245
|
+
this.forget(id);
|
|
1246
|
+
};
|
|
1247
|
+
}
|
|
1248
|
+
/** Told when what a box saves of its score changed: an edit, its encoding, a revert. */
|
|
1249
|
+
subscribe(listener) {
|
|
1250
|
+
__privateGet(this, _listeners2).add(listener);
|
|
1251
|
+
return () => {
|
|
1252
|
+
__privateGet(this, _listeners2).delete(listener);
|
|
1253
|
+
};
|
|
1254
|
+
}
|
|
1255
|
+
/** Resolves once every edit made so far is encoded, so `withDocument` holds it. */
|
|
1256
|
+
async settled() {
|
|
1257
|
+
while (__privateGet(this, _pending).size) await Promise.all([...__privateGet(this, _pending)]);
|
|
1258
|
+
}
|
|
1259
|
+
slot(id) {
|
|
1260
|
+
let slot = __privateGet(this, _slots).get(id);
|
|
1261
|
+
if (!slot) {
|
|
1262
|
+
const made = slot = new SurfaceSlot();
|
|
1263
|
+
__privateGet(this, _slots).set(id, made);
|
|
1264
|
+
made.element.subscribe(() => {
|
|
1265
|
+
const module = made.element.current;
|
|
1266
|
+
if (module) __privateGet(this, _byModule).set(module, made);
|
|
1267
|
+
});
|
|
1268
|
+
}
|
|
1269
|
+
return slot;
|
|
1270
|
+
}
|
|
1271
|
+
/** One plugin per official component's manifest. */
|
|
1272
|
+
plugins() {
|
|
1273
|
+
return Object.freeze(OFFICIAL_COMPONENTS.map((manifest) => ({
|
|
1274
|
+
id: manifest.plugin,
|
|
1275
|
+
version: PLUGIN_VERSION,
|
|
1276
|
+
create: (request) => __privateMethod(this, _ComponentSurfaces_instances, create_fn).call(this, manifest, request)
|
|
1277
|
+
})));
|
|
1278
|
+
}
|
|
1279
|
+
};
|
|
1280
|
+
_slots = new WeakMap();
|
|
1281
|
+
_byModule = new WeakMap();
|
|
1282
|
+
_documents = new WeakMap();
|
|
1283
|
+
_alive = new WeakMap();
|
|
1284
|
+
_pending = new WeakMap();
|
|
1285
|
+
_listeners2 = new WeakMap();
|
|
1286
|
+
_ComponentSurfaces_instances = new WeakSet();
|
|
1287
|
+
edited_fn = function(id) {
|
|
1288
|
+
const kept = __privateGet(this, _documents).get(id);
|
|
1289
|
+
return !!kept && (kept.restored !== void 0 || kept.editor.getSnapshot().contentId !== kept.initial);
|
|
1290
|
+
};
|
|
1291
|
+
keep_fn = function(id, editor, restored) {
|
|
1292
|
+
const initial = editor.getSnapshot().contentId;
|
|
1293
|
+
const kept = { editor, initial, ...restored !== void 0 ? { restored } : {}, seen: initial, encoding: false, release: () => {
|
|
1294
|
+
} };
|
|
1295
|
+
__privateGet(this, _documents).set(id, kept);
|
|
1296
|
+
const unsubscribe = editor.subscribe(() => __privateMethod(this, _ComponentSurfaces_instances, encode_fn).call(this, id, kept));
|
|
1297
|
+
const unprovide = this.slot(id).document.provide(editor);
|
|
1298
|
+
kept.release = () => {
|
|
1299
|
+
unsubscribe();
|
|
1300
|
+
unprovide();
|
|
1301
|
+
};
|
|
1302
|
+
return kept;
|
|
1303
|
+
};
|
|
1304
|
+
/** Encode the document's content when it changed; one at a time, the newest last. */
|
|
1305
|
+
encode_fn = function(id, kept) {
|
|
1306
|
+
if (__privateGet(this, _documents).get(id) !== kept) return;
|
|
1307
|
+
const { contentId, content, status } = kept.editor.getSnapshot();
|
|
1308
|
+
if (status === "disposed") return;
|
|
1309
|
+
if (contentId !== kept.seen) {
|
|
1310
|
+
kept.seen = contentId;
|
|
1311
|
+
__privateMethod(this, _ComponentSurfaces_instances, notify_fn2).call(this);
|
|
1312
|
+
}
|
|
1313
|
+
if (contentId === kept.initial || kept.encoding || kept.saved?.contentId === contentId) return;
|
|
1314
|
+
kept.encoding = true;
|
|
1315
|
+
const work = encodeScore(content).catch(() => void 0).then((value) => {
|
|
1316
|
+
kept.encoding = false;
|
|
1317
|
+
__privateGet(this, _pending).delete(work);
|
|
1318
|
+
kept.saved = { contentId, value };
|
|
1319
|
+
if (__privateGet(this, _documents).get(id) !== kept) return;
|
|
1320
|
+
__privateMethod(this, _ComponentSurfaces_instances, notify_fn2).call(this);
|
|
1321
|
+
__privateMethod(this, _ComponentSurfaces_instances, encode_fn).call(this, id, kept);
|
|
1322
|
+
});
|
|
1323
|
+
__privateGet(this, _pending).add(work);
|
|
1324
|
+
};
|
|
1325
|
+
notify_fn2 = function() {
|
|
1326
|
+
for (const listener of [...__privateGet(this, _listeners2)]) {
|
|
1327
|
+
try {
|
|
1328
|
+
listener();
|
|
1329
|
+
} catch (error) {
|
|
1330
|
+
queueMicrotask(() => {
|
|
1331
|
+
throw error;
|
|
1332
|
+
});
|
|
1333
|
+
}
|
|
1334
|
+
}
|
|
1335
|
+
};
|
|
1336
|
+
/** The box whose Element's module is `module`'s origin, through any relays. */
|
|
1337
|
+
boxOf_fn = function(module) {
|
|
1338
|
+
const origin = sourceOrigin(module);
|
|
1339
|
+
return origin ? __privateGet(this, _byModule).get(origin) : void 0;
|
|
1340
|
+
};
|
|
1341
|
+
/** Synchronous unless the box has an asset to load; the asset loads first, so a failed load leaves nothing behind. */
|
|
1342
|
+
create_fn = function(manifest, request) {
|
|
1343
|
+
manifest = manifestFor(manifest, request.record.config);
|
|
1344
|
+
const wanted = manifest.asset;
|
|
1345
|
+
if (wanted?.kind === "entries") {
|
|
1346
|
+
const roles = Object.keys(request.record.assets ?? {}).filter((role) => role.startsWith(wanted.role)).sort((a, b) => a.localeCompare(b, void 0, { numeric: true }));
|
|
1347
|
+
if (!roles.length) return __privateMethod(this, _ComponentSurfaces_instances, build_fn).call(this, manifest, request, void 0);
|
|
1348
|
+
return Promise.all(roles.map((role) => request.resolveAsset(role))).then((clips) => {
|
|
1349
|
+
request.signal.throwIfAborted();
|
|
1350
|
+
const entries3 = clips.map((clip, index) => {
|
|
1351
|
+
const role = roles[index];
|
|
1352
|
+
if (!(clip instanceof AudioClip)) throw new TypeError(`The ${manifest.name} asset ${role} must resolve to an AudioClip.`);
|
|
1353
|
+
let label = (request.asset(role)?.uri ?? role).split(/[?#]/)[0].split("/").pop() || role;
|
|
1354
|
+
try {
|
|
1355
|
+
label = decodeURIComponent(label);
|
|
1356
|
+
} catch {
|
|
1357
|
+
}
|
|
1358
|
+
return Object.freeze({ id: role, label, clip });
|
|
1359
|
+
});
|
|
1360
|
+
return __privateMethod(this, _ComponentSurfaces_instances, build_fn).call(this, manifest, request, Object.freeze(entries3));
|
|
1361
|
+
});
|
|
1362
|
+
}
|
|
1363
|
+
if (!wanted || !request.asset(wanted.role)) return __privateMethod(this, _ComponentSurfaces_instances, build_fn).call(this, manifest, request, void 0);
|
|
1364
|
+
return request.resolveAsset(wanted.role).then(async (asset) => {
|
|
1365
|
+
request.signal.throwIfAborted();
|
|
1366
|
+
const loaded = wanted.kind === "score" ? asset instanceof Score && asset : asset instanceof AudioClip && asset;
|
|
1367
|
+
if (!loaded) throw new TypeError(`The ${manifest.name} asset must resolve to ${wanted.kind === "score" ? "a Score" : "an AudioClip"}.`);
|
|
1368
|
+
if (!(loaded instanceof Score)) return __privateMethod(this, _ComponentSurfaces_instances, build_fn).call(this, manifest, request, loaded);
|
|
1369
|
+
const leave = this.enter(request.record.id);
|
|
1370
|
+
try {
|
|
1371
|
+
await this.revive(request.record.id, request.record.config, request.signal);
|
|
1372
|
+
return __privateMethod(this, _ComponentSurfaces_instances, build_fn).call(this, manifest, request, loaded, leave);
|
|
1373
|
+
} catch (error) {
|
|
1374
|
+
leave();
|
|
1375
|
+
throw error;
|
|
1376
|
+
}
|
|
1377
|
+
});
|
|
1378
|
+
};
|
|
1379
|
+
build_fn = function(manifest, { record, context }, asset, leave) {
|
|
1380
|
+
const slot = this.slot(record.id);
|
|
1381
|
+
const ports = [];
|
|
1382
|
+
const inputs = /* @__PURE__ */ new Map();
|
|
1383
|
+
const outputs = /* @__PURE__ */ new Map();
|
|
1384
|
+
const notePorts = [];
|
|
1385
|
+
const noteInputs = [];
|
|
1386
|
+
const channels = [];
|
|
1387
|
+
const releases = [];
|
|
1388
|
+
const routes = [];
|
|
1389
|
+
let insertEnds;
|
|
1390
|
+
let carried;
|
|
1391
|
+
for (const port of manifest.ports) {
|
|
1392
|
+
if (port.bind === "context") {
|
|
1393
|
+
const bus = context.audio.prepare().createGain();
|
|
1394
|
+
const output = new AudioOutputPort(bus, { id: port.id });
|
|
1395
|
+
outputs.set(port.id, output);
|
|
1396
|
+
const lent = context.derive({ id: `${record.id}:component`, destination: () => bus, admit: null, target: null });
|
|
1397
|
+
routes.push(() => routeModuleOutput(context, module, output));
|
|
1398
|
+
releases.push(() => {
|
|
1399
|
+
output.dispose();
|
|
1400
|
+
bus.disconnect();
|
|
1401
|
+
}, slot.context.provide(lent));
|
|
1402
|
+
ports.push({ id: port.id, kind: "audio", direction: "output" });
|
|
1403
|
+
} else if (port.bind === "admit") {
|
|
1404
|
+
const input = context.audio.prepare().createGain();
|
|
1405
|
+
inputs.set(port.id, input);
|
|
1406
|
+
const output = new AudioOutputPort(input, { id: port.id });
|
|
1407
|
+
channels.push(Object.freeze({ module: Object.freeze({ id: port.channel }), port: output }));
|
|
1408
|
+
releases.push(() => {
|
|
1409
|
+
output.dispose();
|
|
1410
|
+
input.disconnect();
|
|
1411
|
+
});
|
|
1412
|
+
ports.push({ id: port.id, kind: "audio", direction: "input" });
|
|
1413
|
+
} else if (port.family === "source" && port.direction === "output") {
|
|
1414
|
+
slot.forwardsElement = true;
|
|
1415
|
+
if (!carried) {
|
|
1416
|
+
const relay = new SourceRelay(record.id);
|
|
1417
|
+
let followed;
|
|
1418
|
+
const follow = () => {
|
|
1419
|
+
const module2 = slot.element.current;
|
|
1420
|
+
const previous = followed;
|
|
1421
|
+
followed = module2 ? relay.follow(module2) : void 0;
|
|
1422
|
+
previous?.release();
|
|
1423
|
+
};
|
|
1424
|
+
follow();
|
|
1425
|
+
const stop = slot.element.subscribe(follow);
|
|
1426
|
+
releases.push(() => {
|
|
1427
|
+
stop();
|
|
1428
|
+
followed?.release();
|
|
1429
|
+
relay.dispose();
|
|
1430
|
+
});
|
|
1431
|
+
carried = relay;
|
|
1432
|
+
}
|
|
1433
|
+
ports.push({ id: port.id, kind: "source", direction: "output" });
|
|
1434
|
+
} else if (port.bind === "analyser") {
|
|
1435
|
+
const analyser = context.audio.prepare().createAnalyser();
|
|
1436
|
+
analyser.fftSize = port.fftSize;
|
|
1437
|
+
inputs.set(port.id, analyser);
|
|
1438
|
+
const release = slot.analyser.provide(analyser);
|
|
1439
|
+
releases.push(() => {
|
|
1440
|
+
release();
|
|
1441
|
+
analyser.disconnect();
|
|
1442
|
+
});
|
|
1443
|
+
ports.push({ id: port.id, kind: "audio", direction: "input" });
|
|
1444
|
+
} else if (port.family === "note" && port.direction === "input") {
|
|
1445
|
+
const relay = new NoteOutputPort({ id: "notes" });
|
|
1446
|
+
const input = new NoteInputPort({ accept: (event) => {
|
|
1447
|
+
if (relay.disposed) return;
|
|
1448
|
+
if (event.gate !== "on" && !relay.held.some((held2) => held2.occurrenceId === event.occurrenceId)) return;
|
|
1449
|
+
relay.emit(event);
|
|
1450
|
+
} }, { id: port.id, multiplicity: "sum" });
|
|
1451
|
+
noteInputs.push(input);
|
|
1452
|
+
const release = slot.noteSource.provide(Object.freeze({ id: `${record.id}:notes`, ports: () => [relay] }));
|
|
1453
|
+
releases.push(() => {
|
|
1454
|
+
release();
|
|
1455
|
+
input.dispose();
|
|
1456
|
+
relay.dispose();
|
|
1457
|
+
});
|
|
1458
|
+
ports.push({ id: port.id, kind: "note", direction: "input" });
|
|
1459
|
+
} else if (port.bind === "inputModule" || port.bind === "moduleTarget" || port.bind === "player") {
|
|
1460
|
+
ports.push({ id: port.id, kind: "source", direction: "input" });
|
|
1461
|
+
} else if (port.bind === "insert") {
|
|
1462
|
+
if (!insertEnds) {
|
|
1463
|
+
const audio = context.audio.prepare();
|
|
1464
|
+
const from = audio.createGain(), to = audio.createGain();
|
|
1465
|
+
insertEnds = { context: audio, input: from, output: to };
|
|
1466
|
+
}
|
|
1467
|
+
if (port.direction === "input") inputs.set(port.id, insertEnds.input);
|
|
1468
|
+
else {
|
|
1469
|
+
const output = new AudioOutputPort(insertEnds.output, { id: port.id });
|
|
1470
|
+
outputs.set(port.id, output);
|
|
1471
|
+
routes.push(() => routeModuleOutput(context, module, output));
|
|
1472
|
+
releases.push(() => output.dispose());
|
|
1473
|
+
}
|
|
1474
|
+
ports.push({ id: port.id, kind: "audio", direction: port.direction });
|
|
1475
|
+
} else {
|
|
1476
|
+
const notes = new NoteOutputPort({ id: port.id });
|
|
1477
|
+
const forward = (event) => {
|
|
1478
|
+
if (notes.disposed) return;
|
|
1479
|
+
if (event.gate !== "on" && !notes.held.some((held2) => held2.occurrenceId === event.occurrenceId)) return;
|
|
1480
|
+
notes.emit(event);
|
|
1481
|
+
};
|
|
1482
|
+
const release = slot.notes.provide(forward);
|
|
1483
|
+
notePorts.push(notes);
|
|
1484
|
+
releases.push(() => {
|
|
1485
|
+
release();
|
|
1486
|
+
notes.dispose();
|
|
1487
|
+
});
|
|
1488
|
+
ports.push({ id: port.id, kind: "note", direction: "output" });
|
|
1489
|
+
}
|
|
1490
|
+
}
|
|
1491
|
+
const module = {
|
|
1492
|
+
id: record.id,
|
|
1493
|
+
...notePorts.length || noteInputs.length ? { ports: () => [...notePorts, ...noteInputs] } : {},
|
|
1494
|
+
...outputs.size ? { getPort: (name) => outputs.get(name) } : {}
|
|
1495
|
+
};
|
|
1496
|
+
const registration = notePorts.length || noteInputs.length ? context.registry.register(module) : void 0;
|
|
1497
|
+
const leases = routes.map((route) => route());
|
|
1498
|
+
if (insertEnds) {
|
|
1499
|
+
const ends = insertEnds;
|
|
1500
|
+
releases.push(() => {
|
|
1501
|
+
ends.input.disconnect();
|
|
1502
|
+
ends.output.disconnect();
|
|
1503
|
+
}, slot.insert.provide(Object.freeze(ends)));
|
|
1504
|
+
}
|
|
1505
|
+
if (manifest.scope && !manifest.ports.some((port) => port.bind === "context")) releases.push(slot.context.provide(context.derive({ id: `${record.id}:component`, admit: null, target: null })));
|
|
1506
|
+
if (channels.length) releases.push(slot.channels.provide(Object.freeze(channels)));
|
|
1507
|
+
if (asset !== void 0) releases.push(slot.asset.provide(asset));
|
|
1508
|
+
if (asset instanceof Score) releases.push(slot.score.provide(asset));
|
|
1509
|
+
const carries = carried;
|
|
1510
|
+
return {
|
|
1511
|
+
module,
|
|
1512
|
+
ports,
|
|
1513
|
+
...inputs.size ? { getInput: (name) => inputs.get(name) } : {},
|
|
1514
|
+
...carries ? { sourceOutput: () => carries } : {},
|
|
1515
|
+
...leave ? { exportConfig: () => this.withDocument(record.id, record.config) } : {},
|
|
1516
|
+
dispose: () => {
|
|
1517
|
+
for (const lease of leases) lease.release();
|
|
1518
|
+
for (const release of releases.reverse()) release();
|
|
1519
|
+
registration?.release();
|
|
1520
|
+
leave?.();
|
|
1521
|
+
}
|
|
1522
|
+
};
|
|
1523
|
+
};
|
|
1524
|
+
|
|
1525
|
+
// src/component/views.ts
|
|
1526
|
+
import { clipOfBuffer } from "@webmusic/audio/play/headless";
|
|
1527
|
+
import { defineAudioClipViewElement as defineAudioClipViewElement2 } from "@webmusic/audio/view/element";
|
|
1528
|
+
import { createAudioClipEditor, renderAudioClipEditor } from "@webmusic/audio/view/headless";
|
|
1529
|
+
import { computeSpectrogram } from "@webmusic/audio/analyze";
|
|
1530
|
+
import { elementFitOf, fitElementHeight, notifyModuleContext, observeElementModule, provideModuleContext } from "@webmusic/kernel/element";
|
|
1531
|
+
var UNLINKED = Object.freeze({ player: void 0 });
|
|
1532
|
+
var listen = (element, listener) => observeElementModule(element, () => listener());
|
|
1533
|
+
var admitting = (module) => typeof module?.admit === "function";
|
|
1534
|
+
function defineComponentElements() {
|
|
1535
|
+
for (const manifest of OFFICIAL_COMPONENTS) manifest.define();
|
|
1536
|
+
}
|
|
1537
|
+
function createComponentElement(manifest, config, document = globalThis.document) {
|
|
1538
|
+
manifest.define();
|
|
1539
|
+
const element = document.createElement(manifest.tag);
|
|
1540
|
+
for (const [name, value] of Object.entries(manifest.attributes ?? {})) element.setAttribute(name, value);
|
|
1541
|
+
configureComponentElement(element, manifest, config);
|
|
1542
|
+
const container = manifest.container;
|
|
1543
|
+
if (!container) return element;
|
|
1544
|
+
container.define();
|
|
1545
|
+
const outer = document.createElement(container.tag);
|
|
1546
|
+
for (const [name, value] of Object.entries(container.attributes ?? {})) outer.setAttribute(name, value);
|
|
1547
|
+
outer.append(element);
|
|
1548
|
+
return outer;
|
|
1549
|
+
}
|
|
1550
|
+
function componentTarget(element, manifest) {
|
|
1551
|
+
return manifest.container && element.localName === manifest.container.tag ? element.querySelector(`:scope > ${manifest.tag}`) ?? element : element;
|
|
1552
|
+
}
|
|
1553
|
+
var applied = /* @__PURE__ */ new WeakMap();
|
|
1554
|
+
var held = /* @__PURE__ */ new WeakMap();
|
|
1555
|
+
function configureComponentElement(placed, manifest, config) {
|
|
1556
|
+
const element = componentTarget(placed, manifest);
|
|
1557
|
+
const settings = componentSettings(manifest, config, element.ownerDocument.defaultView?.customElements);
|
|
1558
|
+
const before = applied.get(element);
|
|
1559
|
+
const hold = held.get(element);
|
|
1560
|
+
for (const name of Object.keys(before?.attributes ?? {})) {
|
|
1561
|
+
if (name in settings.attributes || name === hold?.attribute) continue;
|
|
1562
|
+
const own = manifest.attributes?.[name];
|
|
1563
|
+
if (own === void 0) element.removeAttribute(name);
|
|
1564
|
+
else element.setAttribute(name, own);
|
|
1565
|
+
}
|
|
1566
|
+
for (const [name, value] of Object.entries(settings.attributes)) {
|
|
1567
|
+
if (name !== hold?.attribute && element.getAttribute(name) !== value) element.setAttribute(name, value);
|
|
1568
|
+
}
|
|
1569
|
+
for (const name of Object.keys(before?.style ?? {})) {
|
|
1570
|
+
if (!(name in settings.style) && name !== hold?.property) element.style.removeProperty(name);
|
|
1571
|
+
}
|
|
1572
|
+
for (const [name, value] of Object.entries(settings.style)) {
|
|
1573
|
+
if (name !== hold?.property && element.style.getPropertyValue(name) !== value) element.style.setProperty(name, value);
|
|
1574
|
+
}
|
|
1575
|
+
applied.set(element, settings);
|
|
1576
|
+
for (const parameter of manifest.parameters) {
|
|
1577
|
+
if (!parameter.attribute) continue;
|
|
1578
|
+
const value = String(parameterValue(parameter, config));
|
|
1579
|
+
if (element.getAttribute(parameter.attribute) !== value) element.setAttribute(parameter.attribute, value);
|
|
1580
|
+
}
|
|
1581
|
+
if (manifest.configure) {
|
|
1582
|
+
const values = { ...config };
|
|
1583
|
+
for (const parameter of manifest.parameters) values[parameter.name] = parameterValue(parameter, config);
|
|
1584
|
+
manifest.configure(element, values);
|
|
1585
|
+
}
|
|
1586
|
+
}
|
|
1587
|
+
function rebindsOnConfig(manifest) {
|
|
1588
|
+
return !!manifest.portsFor || manifest.parameters.some((parameter) => parameter.buffer);
|
|
1589
|
+
}
|
|
1590
|
+
function bindComponentElement(element, manifest, slot, config, options = {}) {
|
|
1591
|
+
const placed = element;
|
|
1592
|
+
element = componentTarget(placed, manifest);
|
|
1593
|
+
const view = element;
|
|
1594
|
+
const releases = [];
|
|
1595
|
+
const report = (error) => {
|
|
1596
|
+
if (options.onError) options.onError(error);
|
|
1597
|
+
else queueMicrotask(() => {
|
|
1598
|
+
throw error;
|
|
1599
|
+
});
|
|
1600
|
+
};
|
|
1601
|
+
if (manifest.scope || manifest.ports.some((port) => port.bind === "context")) {
|
|
1602
|
+
const box = placed.parentElement;
|
|
1603
|
+
if (!box) throw new Error(`The ${manifest.name} component must be inside its box before it is bound.`);
|
|
1604
|
+
releases.push(provideModuleContext(box, () => slot.context.current));
|
|
1605
|
+
releases.push(slot.context.subscribe(() => notifyModuleContext(box)));
|
|
1606
|
+
}
|
|
1607
|
+
if (manifest.ports.some((port) => port.bind === "insert")) releases.push(bindInsert(view, slot, report));
|
|
1608
|
+
let refreshSpectrogram = () => {
|
|
1609
|
+
};
|
|
1610
|
+
if (manifest.spectrogram) {
|
|
1611
|
+
const spectrogram = bindSpectrogram(view, slot, report);
|
|
1612
|
+
refreshSpectrogram = spectrogram.refresh;
|
|
1613
|
+
releases.push(spectrogram.release);
|
|
1614
|
+
}
|
|
1615
|
+
if (manifest.ports.some((port) => port.bind === "admit")) {
|
|
1616
|
+
let admittedTo, admitted;
|
|
1617
|
+
const leases = [];
|
|
1618
|
+
const retire = () => {
|
|
1619
|
+
for (const lease of leases.splice(0)) {
|
|
1620
|
+
try {
|
|
1621
|
+
lease.release();
|
|
1622
|
+
} catch (error) {
|
|
1623
|
+
report(error);
|
|
1624
|
+
}
|
|
1625
|
+
}
|
|
1626
|
+
admittedTo = admitted = void 0;
|
|
1627
|
+
};
|
|
1628
|
+
let busy = false, again = false;
|
|
1629
|
+
const admit = () => {
|
|
1630
|
+
if (busy) {
|
|
1631
|
+
again = true;
|
|
1632
|
+
return;
|
|
1633
|
+
}
|
|
1634
|
+
busy = true;
|
|
1635
|
+
try {
|
|
1636
|
+
do {
|
|
1637
|
+
again = false;
|
|
1638
|
+
const module = view.module, channels = slot.channels.current;
|
|
1639
|
+
if (module === admittedTo && channels === admitted) continue;
|
|
1640
|
+
retire();
|
|
1641
|
+
if (!admitting(module) || !channels) continue;
|
|
1642
|
+
admittedTo = module;
|
|
1643
|
+
admitted = channels;
|
|
1644
|
+
for (const channel of channels) {
|
|
1645
|
+
try {
|
|
1646
|
+
leases.push(module.admit(channel.module, channel.port));
|
|
1647
|
+
} catch (error) {
|
|
1648
|
+
report(error);
|
|
1649
|
+
}
|
|
1650
|
+
}
|
|
1651
|
+
} while (again);
|
|
1652
|
+
} finally {
|
|
1653
|
+
busy = false;
|
|
1654
|
+
}
|
|
1655
|
+
};
|
|
1656
|
+
admit();
|
|
1657
|
+
releases.push(slot.channels.subscribe(admit));
|
|
1658
|
+
const unlisten = listen(element, admit);
|
|
1659
|
+
releases.push(() => {
|
|
1660
|
+
unlisten();
|
|
1661
|
+
retire();
|
|
1662
|
+
});
|
|
1663
|
+
}
|
|
1664
|
+
if (slot.forwardsElement || manifest.ports.some((port) => port.family === "source" && port.direction === "output")) {
|
|
1665
|
+
let provided;
|
|
1666
|
+
const forward = () => {
|
|
1667
|
+
const module = view.module;
|
|
1668
|
+
if (module === provided?.module) return;
|
|
1669
|
+
provided?.release();
|
|
1670
|
+
provided = module ? { module, release: slot.element.provide(module) } : void 0;
|
|
1671
|
+
};
|
|
1672
|
+
forward();
|
|
1673
|
+
const unlisten = listen(element, forward);
|
|
1674
|
+
const releaseOwner = slot.elementOwner.provide(element);
|
|
1675
|
+
releases.push(() => {
|
|
1676
|
+
unlisten();
|
|
1677
|
+
releaseOwner();
|
|
1678
|
+
provided?.release();
|
|
1679
|
+
provided = void 0;
|
|
1680
|
+
});
|
|
1681
|
+
}
|
|
1682
|
+
if (manifest.asset) {
|
|
1683
|
+
const property = manifest.asset.property;
|
|
1684
|
+
const plays = manifest.asset.kind;
|
|
1685
|
+
const assign = () => {
|
|
1686
|
+
const document = plays === "score" ? slot.document.current : void 0;
|
|
1687
|
+
if (document) {
|
|
1688
|
+
if (view.editor === document) return;
|
|
1689
|
+
try {
|
|
1690
|
+
if (view[property] !== void 0) view[property] = void 0;
|
|
1691
|
+
view.editor = document;
|
|
1692
|
+
} catch (error) {
|
|
1693
|
+
report(error);
|
|
1694
|
+
}
|
|
1695
|
+
return;
|
|
1696
|
+
}
|
|
1697
|
+
if (plays === "score" && view.editor !== void 0) {
|
|
1698
|
+
try {
|
|
1699
|
+
view.editor = void 0;
|
|
1700
|
+
} catch (error) {
|
|
1701
|
+
report(error);
|
|
1702
|
+
}
|
|
1703
|
+
}
|
|
1704
|
+
const value = slot.asset.current;
|
|
1705
|
+
if (value !== void 0 && view[property] !== value) view[property] = value;
|
|
1706
|
+
};
|
|
1707
|
+
assign();
|
|
1708
|
+
releases.push(slot.asset.subscribe(assign));
|
|
1709
|
+
if (plays === "score") releases.push(slot.document.subscribe(assign));
|
|
1710
|
+
}
|
|
1711
|
+
if (manifest.edits === "score") {
|
|
1712
|
+
const lend = () => {
|
|
1713
|
+
const on = element.hasAttribute("editable") && element.getAttribute("editable") !== "false";
|
|
1714
|
+
const editor = on ? slot.lender.current?.() : void 0;
|
|
1715
|
+
if (view.editor === editor) return;
|
|
1716
|
+
try {
|
|
1717
|
+
view.editor = editor;
|
|
1718
|
+
} catch (error) {
|
|
1719
|
+
report(error);
|
|
1720
|
+
}
|
|
1721
|
+
};
|
|
1722
|
+
lend();
|
|
1723
|
+
releases.push(slot.lender.subscribe(lend));
|
|
1724
|
+
const Observer = element.ownerDocument.defaultView?.MutationObserver;
|
|
1725
|
+
const observer = Observer ? new Observer(lend) : void 0;
|
|
1726
|
+
observer?.observe(element, { attributes: true, attributeFilter: ["editable"] });
|
|
1727
|
+
releases.push(() => {
|
|
1728
|
+
observer?.disconnect();
|
|
1729
|
+
if (view.editor !== void 0) {
|
|
1730
|
+
try {
|
|
1731
|
+
view.editor = void 0;
|
|
1732
|
+
} catch (error) {
|
|
1733
|
+
report(error);
|
|
1734
|
+
}
|
|
1735
|
+
}
|
|
1736
|
+
});
|
|
1737
|
+
}
|
|
1738
|
+
for (const port of manifest.ports) {
|
|
1739
|
+
if (port.bind === "context" || port.bind === "admit" || port.bind === "insert" || port.family === "source" && port.direction === "output") continue;
|
|
1740
|
+
if (port.bind === "player") {
|
|
1741
|
+
const apply = () => {
|
|
1742
|
+
view.player = slot.sourceOwner.current ?? UNLINKED;
|
|
1743
|
+
};
|
|
1744
|
+
apply();
|
|
1745
|
+
releases.push(slot.sourceOwner.subscribe(apply));
|
|
1746
|
+
} else if (port.bind === "analyser") {
|
|
1747
|
+
const apply = () => {
|
|
1748
|
+
view.analyser = slot.analyser.current;
|
|
1749
|
+
};
|
|
1750
|
+
apply();
|
|
1751
|
+
releases.push(slot.analyser.subscribe(apply));
|
|
1752
|
+
} else if (port.bind === "inputModule" || port.bind === "moduleTarget") {
|
|
1753
|
+
const provided = port.family === "note" ? slot.noteSource : slot.source;
|
|
1754
|
+
const member = port.bind;
|
|
1755
|
+
const apply = () => {
|
|
1756
|
+
view[member] = provided.current ?? null;
|
|
1757
|
+
};
|
|
1758
|
+
apply();
|
|
1759
|
+
releases.push(provided.subscribe(apply));
|
|
1760
|
+
} else {
|
|
1761
|
+
const output = view.module?.ports?.().find((item) => item.family === "note" && item.direction === "output");
|
|
1762
|
+
const unsubscribe = output?.subscribe((event) => {
|
|
1763
|
+
try {
|
|
1764
|
+
slot.notes.current?.(event);
|
|
1765
|
+
} catch (error) {
|
|
1766
|
+
queueMicrotask(() => {
|
|
1767
|
+
throw error;
|
|
1768
|
+
});
|
|
1769
|
+
}
|
|
1770
|
+
});
|
|
1771
|
+
if (unsubscribe) releases.push(unsubscribe);
|
|
1772
|
+
}
|
|
1773
|
+
}
|
|
1774
|
+
for (const parameter of manifest.parameters) {
|
|
1775
|
+
if (!parameter.buffer || !options.buffers) continue;
|
|
1776
|
+
const name = String(parameterValue(parameter, config));
|
|
1777
|
+
if (!name) continue;
|
|
1778
|
+
const { store, context } = options.buffers;
|
|
1779
|
+
if (parameter.buffer === "clip") {
|
|
1780
|
+
const show = () => {
|
|
1781
|
+
const buffer = store.get(name);
|
|
1782
|
+
view.clip = buffer ? clipOfBuffer(buffer) : void 0;
|
|
1783
|
+
refreshSpectrogram();
|
|
1784
|
+
};
|
|
1785
|
+
show();
|
|
1786
|
+
const unsubscribe = store.subscribe(name, show);
|
|
1787
|
+
releases.push(() => {
|
|
1788
|
+
unsubscribe();
|
|
1789
|
+
view.clip = void 0;
|
|
1790
|
+
});
|
|
1791
|
+
continue;
|
|
1792
|
+
}
|
|
1793
|
+
if (parameter.buffer === "take") {
|
|
1794
|
+
const keep = (event) => {
|
|
1795
|
+
const clip = event.detail?.clip;
|
|
1796
|
+
if (!clip) return;
|
|
1797
|
+
try {
|
|
1798
|
+
store.set(name, clip.toAudioBuffer(context));
|
|
1799
|
+
} catch (error) {
|
|
1800
|
+
report(error);
|
|
1801
|
+
}
|
|
1802
|
+
};
|
|
1803
|
+
element.addEventListener("webaudio:recorded", keep);
|
|
1804
|
+
releases.push(() => element.removeEventListener("webaudio:recorded", keep));
|
|
1805
|
+
continue;
|
|
1806
|
+
}
|
|
1807
|
+
const editable = options.editableBuffers && componentSettings(manifest, config, element.ownerDocument.defaultView?.customElements).attributes.editable !== "false";
|
|
1808
|
+
if (name) releases.push(bindBufferView(element, name, options.buffers.store, options.buffers.context, { editable, onError: options.onError }));
|
|
1809
|
+
}
|
|
1810
|
+
const gesture = () => options.onGesture?.();
|
|
1811
|
+
placed.addEventListener("pointerdown", gesture);
|
|
1812
|
+
placed.addEventListener("keydown", gesture);
|
|
1813
|
+
return () => {
|
|
1814
|
+
placed.removeEventListener("pointerdown", gesture);
|
|
1815
|
+
placed.removeEventListener("keydown", gesture);
|
|
1816
|
+
for (const release of releases.reverse()) release();
|
|
1817
|
+
};
|
|
1818
|
+
}
|
|
1819
|
+
function bindInsert(view, slot, report) {
|
|
1820
|
+
const element = view;
|
|
1821
|
+
let joined;
|
|
1822
|
+
let released = false;
|
|
1823
|
+
const part = () => {
|
|
1824
|
+
if (!joined) return;
|
|
1825
|
+
const { from, into, out, to } = joined;
|
|
1826
|
+
joined = void 0;
|
|
1827
|
+
try {
|
|
1828
|
+
from.disconnect(into);
|
|
1829
|
+
} catch {
|
|
1830
|
+
}
|
|
1831
|
+
try {
|
|
1832
|
+
out.disconnect(to);
|
|
1833
|
+
} catch {
|
|
1834
|
+
}
|
|
1835
|
+
};
|
|
1836
|
+
const join = () => {
|
|
1837
|
+
if (released) return;
|
|
1838
|
+
const ends = slot.insert.current;
|
|
1839
|
+
if (!ends) {
|
|
1840
|
+
part();
|
|
1841
|
+
return;
|
|
1842
|
+
}
|
|
1843
|
+
if (element.context !== ends.context) {
|
|
1844
|
+
try {
|
|
1845
|
+
element.context = ends.context;
|
|
1846
|
+
} catch (error) {
|
|
1847
|
+
report(error);
|
|
1848
|
+
return;
|
|
1849
|
+
}
|
|
1850
|
+
}
|
|
1851
|
+
const into = element.input, out = element.output;
|
|
1852
|
+
if (!into || !out) {
|
|
1853
|
+
part();
|
|
1854
|
+
return;
|
|
1855
|
+
}
|
|
1856
|
+
if (joined?.from === ends.input && joined.into === into && joined.out === out && joined.to === ends.output) return;
|
|
1857
|
+
part();
|
|
1858
|
+
try {
|
|
1859
|
+
ends.input.connect(into);
|
|
1860
|
+
out.connect(ends.output);
|
|
1861
|
+
joined = { from: ends.input, into, out, to: ends.output };
|
|
1862
|
+
} catch (error) {
|
|
1863
|
+
report(error);
|
|
1864
|
+
}
|
|
1865
|
+
};
|
|
1866
|
+
join();
|
|
1867
|
+
const unsubscribe = slot.insert.subscribe(join);
|
|
1868
|
+
element.addEventListener("webscore:chainchange", join);
|
|
1869
|
+
return () => {
|
|
1870
|
+
released = true;
|
|
1871
|
+
unsubscribe();
|
|
1872
|
+
element.removeEventListener("webscore:chainchange", join);
|
|
1873
|
+
part();
|
|
1874
|
+
};
|
|
1875
|
+
}
|
|
1876
|
+
var SPECTROGRAM_FRAMES = 2048;
|
|
1877
|
+
var SPECTROGRAM_FFT = 1024;
|
|
1878
|
+
function bindSpectrogram(view, slot, report) {
|
|
1879
|
+
const element = view;
|
|
1880
|
+
let drawn;
|
|
1881
|
+
let watched;
|
|
1882
|
+
const sourceEvents = ["webaudio:sourcechange", "webaudio:loaded", "webaudio:playerchange"];
|
|
1883
|
+
const update = () => {
|
|
1884
|
+
const owner = slot.sourceOwner.current;
|
|
1885
|
+
if (owner !== watched) {
|
|
1886
|
+
for (const name of sourceEvents) watched?.removeEventListener(name, update);
|
|
1887
|
+
watched = owner;
|
|
1888
|
+
for (const name of sourceEvents) watched?.addEventListener(name, update);
|
|
1889
|
+
}
|
|
1890
|
+
const clip = element.getAttribute("type") === "spectrogram" ? element.resolvedClip ?? element.clip ?? owner?.clip : void 0;
|
|
1891
|
+
if (clip === drawn) return;
|
|
1892
|
+
drawn = clip;
|
|
1893
|
+
try {
|
|
1894
|
+
const channel = clip?.channelData(0);
|
|
1895
|
+
if (!clip || !channel) {
|
|
1896
|
+
if (element.spectrogram !== void 0) element.spectrogram = void 0;
|
|
1897
|
+
return;
|
|
1898
|
+
}
|
|
1899
|
+
element.spectrogram = computeSpectrogram(channel, clip.sampleRate, { fftSize: SPECTROGRAM_FFT, maxFrames: SPECTROGRAM_FRAMES });
|
|
1900
|
+
} catch (error) {
|
|
1901
|
+
report(error);
|
|
1902
|
+
}
|
|
1903
|
+
};
|
|
1904
|
+
update();
|
|
1905
|
+
const unsubscribe = slot.sourceOwner.subscribe(update);
|
|
1906
|
+
const Observer = element.ownerDocument.defaultView?.MutationObserver;
|
|
1907
|
+
const observer = Observer ? new Observer(update) : void 0;
|
|
1908
|
+
observer?.observe(element, { attributes: true, attributeFilter: ["type"] });
|
|
1909
|
+
return { refresh: update, release: () => {
|
|
1910
|
+
unsubscribe();
|
|
1911
|
+
observer?.disconnect();
|
|
1912
|
+
for (const name of sourceEvents) watched?.removeEventListener(name, update);
|
|
1913
|
+
if (drawn !== void 0) {
|
|
1914
|
+
try {
|
|
1915
|
+
element.spectrogram = void 0;
|
|
1916
|
+
} catch (error) {
|
|
1917
|
+
report(error);
|
|
1918
|
+
}
|
|
1919
|
+
}
|
|
1920
|
+
} };
|
|
1921
|
+
}
|
|
1922
|
+
function fitComponentElement(element, manifest, box = element.parentElement) {
|
|
1923
|
+
const fit = manifest.fit ?? elementFitOf(element);
|
|
1924
|
+
if (!box || typeof element.ownerDocument.defaultView?.ResizeObserver !== "function") return () => {
|
|
1925
|
+
};
|
|
1926
|
+
if (!fit) return fitElementHeight(element, box, { contain: true });
|
|
1927
|
+
const setAtStart = applied.get(element)?.attributes.height;
|
|
1928
|
+
held.set(element, fit.kind === "token" ? { property: fit.property } : { attribute: "height" });
|
|
1929
|
+
const release = fitElementHeight(element, box, { fit, contain: true, restore: (initial) => fit.kind === "token" ? applied.get(element)?.style[fit.property] ?? null : applied.get(element)?.attributes.height ?? (setAtStart === void 0 ? initial : manifest.attributes?.height ?? null) });
|
|
1930
|
+
return () => {
|
|
1931
|
+
held.delete(element);
|
|
1932
|
+
release();
|
|
1933
|
+
};
|
|
1934
|
+
}
|
|
1935
|
+
function bindBufferView(element, name, buffers, context, options = {}) {
|
|
1936
|
+
defineAudioClipViewElement2();
|
|
1937
|
+
const view = element;
|
|
1938
|
+
let editor;
|
|
1939
|
+
let written2;
|
|
1940
|
+
let job = 0;
|
|
1941
|
+
view.setAttribute("aria-label", `Buffer ${name}`);
|
|
1942
|
+
if (options.editable) {
|
|
1943
|
+
view.setAttribute("tools", "delete trim silence gain fade zoom history");
|
|
1944
|
+
view.editable = true;
|
|
1945
|
+
}
|
|
1946
|
+
const show = () => {
|
|
1947
|
+
const buffer = buffers.get(name);
|
|
1948
|
+
if (buffer && buffer === written2) return;
|
|
1949
|
+
editor?.dispose();
|
|
1950
|
+
editor = buffer ? createAudioClipEditor(clipOfBuffer(buffer)) : void 0;
|
|
1951
|
+
view.editor = editor;
|
|
1952
|
+
};
|
|
1953
|
+
const commit = () => {
|
|
1954
|
+
const current = editor;
|
|
1955
|
+
if (!current) return;
|
|
1956
|
+
const mine = ++job;
|
|
1957
|
+
void renderAudioClipEditor(current).then((clip) => {
|
|
1958
|
+
if (mine !== job || current !== editor) return;
|
|
1959
|
+
written2 = clip.toAudioBuffer(context);
|
|
1960
|
+
buffers.set(name, written2);
|
|
1961
|
+
}).catch((error) => options.onError?.(error));
|
|
1962
|
+
};
|
|
1963
|
+
show();
|
|
1964
|
+
const unsubscribe = buffers.subscribe(name, show);
|
|
1965
|
+
view.addEventListener("webaudio:editcommit", commit);
|
|
1966
|
+
return () => {
|
|
1967
|
+
job++;
|
|
1968
|
+
unsubscribe();
|
|
1969
|
+
view.removeEventListener("webaudio:editcommit", commit);
|
|
1970
|
+
view.editor = void 0;
|
|
1971
|
+
editor?.dispose();
|
|
1972
|
+
};
|
|
1973
|
+
}
|
|
1974
|
+
|
|
1975
|
+
// src/component/plugins.ts
|
|
1976
|
+
import { loadClipFromUrl } from "@webmusic/audio/play";
|
|
1977
|
+
import {
|
|
1978
|
+
multichannelPlugin,
|
|
1979
|
+
wamPlugin
|
|
1980
|
+
} from "@webmusic/kernel/module";
|
|
1981
|
+
import { loadScoreFromUrl } from "@webmusic/score/io";
|
|
1982
|
+
function createComponentPlugins(options = {}) {
|
|
1983
|
+
const bridge = createWebMusicProjectPlugins({ modules: options.modules, midi: options.midi }).map((factory) => factory.id === "webmusic.score-player" ? withSourceOutlet(factory, options.surfaces) : factory);
|
|
1984
|
+
const effect = bridge.find((factory) => factory.id === "webmusic.effect");
|
|
1985
|
+
return Object.freeze([
|
|
1986
|
+
...bridge,
|
|
1987
|
+
multichannelPlugin(effect),
|
|
1988
|
+
...options.surfaces ? options.surfaces.plugins() : [],
|
|
1989
|
+
...options.wam ? [wamPlugin(options.wam)] : [],
|
|
1990
|
+
...options.extra ?? []
|
|
1991
|
+
]);
|
|
1992
|
+
}
|
|
1993
|
+
function withSourceOutlet(factory, surfaces) {
|
|
1994
|
+
const source = { id: "source", kind: "source", direction: "output" };
|
|
1995
|
+
if (!surfaces) return Object.freeze({ ...factory, create: async (request) => {
|
|
1996
|
+
const instance = await factory.create(request);
|
|
1997
|
+
return { ...instance, ports: [...instance.ports ?? [], source] };
|
|
1998
|
+
} });
|
|
1999
|
+
return Object.freeze({ ...factory, create: async (request) => {
|
|
2000
|
+
const { id, config } = request.record;
|
|
2001
|
+
const leave = surfaces.enter(id);
|
|
2002
|
+
let instance;
|
|
2003
|
+
try {
|
|
2004
|
+
await surfaces.revive(id, config, request.signal);
|
|
2005
|
+
instance = await factory.create(request);
|
|
2006
|
+
const created = instance;
|
|
2007
|
+
const ports = [...created.ports ?? [], source];
|
|
2008
|
+
const module = created.module;
|
|
2009
|
+
const score = module.player?.playback.snapshot().score;
|
|
2010
|
+
if (!score || typeof module.setDocumentSource !== "function") return { ...created, ports, dispose: () => {
|
|
2011
|
+
leave();
|
|
2012
|
+
return created.dispose();
|
|
2013
|
+
} };
|
|
2014
|
+
const slot = surfaces.slot(id);
|
|
2015
|
+
let playing;
|
|
2016
|
+
const play = () => {
|
|
2017
|
+
const document = slot.document.current;
|
|
2018
|
+
if (document === playing) return;
|
|
2019
|
+
playing = document;
|
|
2020
|
+
module.setDocumentSource(document);
|
|
2021
|
+
};
|
|
2022
|
+
const releaseScore = slot.score.provide(score);
|
|
2023
|
+
play();
|
|
2024
|
+
const unsubscribe = slot.document.subscribe(play);
|
|
2025
|
+
return {
|
|
2026
|
+
...created,
|
|
2027
|
+
ports,
|
|
2028
|
+
exportConfig: () => surfaces.withDocument(id, created.exportConfig?.() ?? config),
|
|
2029
|
+
dispose: () => {
|
|
2030
|
+
unsubscribe();
|
|
2031
|
+
releaseScore();
|
|
2032
|
+
leave();
|
|
2033
|
+
return created.dispose();
|
|
2034
|
+
}
|
|
2035
|
+
};
|
|
2036
|
+
} catch (error) {
|
|
2037
|
+
leave();
|
|
2038
|
+
try {
|
|
2039
|
+
instance?.dispose();
|
|
2040
|
+
} catch {
|
|
2041
|
+
}
|
|
2042
|
+
throw error;
|
|
2043
|
+
}
|
|
2044
|
+
} });
|
|
2045
|
+
}
|
|
2046
|
+
var SCORE = /\.(mid|midi|mxl|musicxml|xml)$/i;
|
|
2047
|
+
var AUDIO = /\.(wav|mp3|ogg|oga|flac|m4a|aac|webm)$/i;
|
|
2048
|
+
async function resolveComponentAsset(asset, signal, base, embedded) {
|
|
2049
|
+
signal.throwIfAborted();
|
|
2050
|
+
const carried = embedded?.[asset.uri];
|
|
2051
|
+
const url = carried ?? new URL(asset.uri, base ?? globalThis.location?.href ?? "http://localhost/").href;
|
|
2052
|
+
const inline = url.startsWith("data:");
|
|
2053
|
+
const path = inline ? "" : new URL(url).pathname;
|
|
2054
|
+
const type = asset.mediaType ?? (inline ? /^data:([^;,]+)/.exec(url)?.[1] ?? "" : "");
|
|
2055
|
+
if (SCORE.test(path) || /midi|musicxml/.test(type)) return loadScoreFromUrl(url);
|
|
2056
|
+
if (AUDIO.test(path) || type.startsWith("audio/")) return loadClipFromUrl(url, { signal });
|
|
2057
|
+
throw new Error(`No loader for the asset ${inline ? `${carried === void 0 ? "" : `${asset.uri}, `}embedded as ${type || "an unknown type"}` : asset.uri}.`);
|
|
2058
|
+
}
|
|
2059
|
+
|
|
2060
|
+
// src/component/mount.ts
|
|
2061
|
+
import { sharedBuffersFor } from "@webmusic/audio/play/headless";
|
|
2062
|
+
import {
|
|
2063
|
+
ModuleAudioEnvironment,
|
|
2064
|
+
ModuleProjectRuntime,
|
|
2065
|
+
NestedComponent as NestedComponent2,
|
|
2066
|
+
PolyComponent,
|
|
2067
|
+
keepProjectSessionPreloaded,
|
|
2068
|
+
ProjectPluginRegistry,
|
|
2069
|
+
moduleControlOf,
|
|
2070
|
+
parseModuleProject as parseModuleProject3
|
|
2071
|
+
} from "@webmusic/kernel/module";
|
|
2072
|
+
var COMPONENT_FRAME = Object.freeze({ rule: 1, maxStageHeight: 640, controlSize: 32 });
|
|
2073
|
+
var { rule: RULE, maxStageHeight: MAX_STAGE, controlSize: CONTROL_SIZE } = COMPONENT_FRAME;
|
|
2074
|
+
function componentFrame(boxes) {
|
|
2075
|
+
const left = boxes.length ? Math.min(...boxes.map((box) => box.x)) : 0, top = boxes.length ? Math.min(...boxes.map((box) => box.y)) : 0;
|
|
2076
|
+
const width = Math.max(0, ...boxes.map((box) => box.x + box.width - left)), contentHeight = Math.max(0, ...boxes.map((box) => box.y + box.height - top));
|
|
2077
|
+
const height = Math.min(contentHeight, MAX_STAGE);
|
|
2078
|
+
return Object.freeze({
|
|
2079
|
+
contentHeight,
|
|
2080
|
+
stage: Object.freeze({ x: left, y: top, width, height }),
|
|
2081
|
+
frame: Object.freeze({ x: left - RULE, y: top - RULE, width: width + 2 * RULE, height: height + 2 * RULE })
|
|
2082
|
+
});
|
|
2083
|
+
}
|
|
2084
|
+
var STYLE = `
|
|
2085
|
+
:host { display: block; max-width: 100%; }
|
|
2086
|
+
.wmc { box-sizing: border-box; display: flex; flex-direction: column; width: max-content; max-width: 100%; color: var(--wm-foreground, light-dark(#111, #eee)); background: var(--wm-background, var(--wm-surface, light-dark(#fff, #111))); border: var(--wm-component-border, ${RULE}px solid var(--wm-border, light-dark(#111, #eee))); border-radius: var(--wm-component-radius, var(--wm-control-radius, 0)); font: 12px/1.4 var(--wm-font-family, ui-monospace, monospace); }
|
|
2087
|
+
.wmc__stage { position: relative; overflow: auto; scrollbar-width: none; }
|
|
2088
|
+
.wmc__extent { pointer-events: none; }
|
|
2089
|
+
.wmc__stage::-webkit-scrollbar { display: none; }
|
|
2090
|
+
.wmc__item { position: absolute; box-sizing: border-box; }
|
|
2091
|
+
/* A nested component's contents sit inside its cell, framed as Studio frames them. */
|
|
2092
|
+
.wmc__nested { border: 1px solid var(--wm-border, light-dark(#111, #eee)); }
|
|
2093
|
+
/* Each Element is a block, as in Studio's boxes, so the width it is given holds. A control lays out
|
|
2094
|
+
its own cells to fill its box: made a block, its button, switch or grid shrinks to nothing. */
|
|
2095
|
+
.wmc__item:not([data-kind="control"]) > :not(style) { display: block; }
|
|
2096
|
+
/* The box carries the width, natural or resized, and the Element fills it: an Element may own its inline width. */
|
|
2097
|
+
.wmc__item > :not(style) { box-sizing: border-box; width: 100%; }
|
|
2098
|
+
.wmc__item[data-sized] { overflow: hidden; }
|
|
2099
|
+
/* Placed by the host in a page of its own (DEC-092): in the flow, a component as wide as its place. */
|
|
2100
|
+
.wmc__placed { position: relative; box-sizing: border-box; max-width: 100%; }
|
|
2101
|
+
.wmc__placed > :not(style) { box-sizing: border-box; width: 100%; }
|
|
2102
|
+
.wmc__placed:not([data-kind="control"]) > :not(style) { display: block; }
|
|
2103
|
+
.wmc__placed[data-kind="component"] { width: 100% !important; }
|
|
2104
|
+
/* A drawing in a resized box may be shorter than it would choose to be. */
|
|
2105
|
+
.wmc__item[data-sized] [part~="plot"] { min-height: 32px; }
|
|
2106
|
+
`;
|
|
2107
|
+
var isShell = (record) => record?.plugin.id === NestedComponent2.type || record?.plugin.id === PolyComponent.type;
|
|
2108
|
+
function contentKind(record) {
|
|
2109
|
+
if (componentManifestOf(record.plugin.id)) return "component";
|
|
2110
|
+
if (controlKindOf(record)) return "control";
|
|
2111
|
+
return isShell(record) && record.config.embed === true ? "nested" : void 0;
|
|
2112
|
+
}
|
|
2113
|
+
function componentContents(input, shell) {
|
|
2114
|
+
const project = parseModuleProject3(input);
|
|
2115
|
+
const byId = new Map(project.modules.map((record) => [record.id, record]));
|
|
2116
|
+
const contexts = new Map(project.contexts.map((context) => [context.id, context]));
|
|
2117
|
+
const owner = (contextId) => {
|
|
2118
|
+
for (let context = contexts.get(contextId); context; context = context.parentId ? contexts.get(context.parentId) : void 0) {
|
|
2119
|
+
if (context.ownerModuleId && isShell(byId.get(context.ownerModuleId))) return context.ownerModuleId;
|
|
2120
|
+
}
|
|
2121
|
+
return void 0;
|
|
2122
|
+
};
|
|
2123
|
+
const members = project.modules.filter((record) => owner(record.contextId) === shell && !record.id.includes("~v"));
|
|
2124
|
+
const shown = new Map(members.flatMap((record) => {
|
|
2125
|
+
const kind = contentKind(record);
|
|
2126
|
+
return kind ? [[record.id, kind]] : [];
|
|
2127
|
+
}));
|
|
2128
|
+
const placed = (project.layout.presentation ?? []).filter((item) => shown.has(item.moduleId));
|
|
2129
|
+
if (placed.length) {
|
|
2130
|
+
return placed.map((item) => ({
|
|
2131
|
+
record: byId.get(item.moduleId),
|
|
2132
|
+
x: item.x,
|
|
2133
|
+
y: item.y,
|
|
2134
|
+
kind: shown.get(item.moduleId),
|
|
2135
|
+
...item.width !== void 0 ? { width: item.width } : {},
|
|
2136
|
+
...item.height !== void 0 ? { height: item.height } : {}
|
|
2137
|
+
}));
|
|
2138
|
+
}
|
|
2139
|
+
const positions = new Map(project.layout.modules.map((item) => [item.moduleId, item]));
|
|
2140
|
+
return members.filter((record) => shown.has(record.id)).map((record) => {
|
|
2141
|
+
const at = positions.get(record.id);
|
|
2142
|
+
return {
|
|
2143
|
+
record,
|
|
2144
|
+
x: at?.x ?? 0,
|
|
2145
|
+
y: at?.y ?? 0,
|
|
2146
|
+
kind: shown.get(record.id),
|
|
2147
|
+
...at?.width !== void 0 ? { width: at.width } : {},
|
|
2148
|
+
...at?.height !== void 0 ? { height: at.height } : {}
|
|
2149
|
+
};
|
|
2150
|
+
});
|
|
2151
|
+
}
|
|
2152
|
+
var notYetApplied = (error) => error?.name === "ScoreDocumentApplicationError" && error.code === "not-ready";
|
|
2153
|
+
function keepComponentSessionPreloaded(runtime, onError = () => {
|
|
2154
|
+
}) {
|
|
2155
|
+
return keepProjectSessionPreloaded(runtime, { transient: notYetApplied, onError });
|
|
2156
|
+
}
|
|
2157
|
+
function mountComponent(host, input, options = {}) {
|
|
2158
|
+
const project = migrateBufferViews(parseModuleProject3(input));
|
|
2159
|
+
const document = host.ownerDocument ?? globalThis.document;
|
|
2160
|
+
const report = options.onError ?? ((error) => console.error(error));
|
|
2161
|
+
const surfaces = new ComponentSurfaces();
|
|
2162
|
+
const audio = options.audio ?? new ModuleAudioEnvironment();
|
|
2163
|
+
const registry = new ProjectPluginRegistry();
|
|
2164
|
+
const registrations = (options.plugins ?? createComponentPlugins({ ...options, surfaces, modules: { onError: report, ...options.modules } })).map((plugin) => registry.register(plugin));
|
|
2165
|
+
const runtime = new ModuleProjectRuntime({
|
|
2166
|
+
audio,
|
|
2167
|
+
plugins: registry,
|
|
2168
|
+
connect: surfaces.connect,
|
|
2169
|
+
onCleanupError: report,
|
|
2170
|
+
// The runtime preloads the Session it owns as members join (DEC-069, DEC-094).
|
|
2171
|
+
preload: { transient: notYetApplied, onError: report },
|
|
2172
|
+
resolveAsset: options.resolveAsset ?? ((asset, signal) => resolveComponentAsset(asset, signal, options.base, options.embedded))
|
|
2173
|
+
});
|
|
2174
|
+
defineComponentElements();
|
|
2175
|
+
const root = document.createElement("div");
|
|
2176
|
+
root.className = "wmc";
|
|
2177
|
+
root.setAttribute("role", "region");
|
|
2178
|
+
root.setAttribute("aria-label", options.label ?? project.id);
|
|
2179
|
+
const style = document.createElement("style");
|
|
2180
|
+
const shadowed = typeof host.host === "object" && host.host !== null;
|
|
2181
|
+
const themed = styleDeclarations(project.layout.style);
|
|
2182
|
+
let styling = options.styling === "inherit" ? "inherit" : "own";
|
|
2183
|
+
const configOf = (record) => styling === "own" ? record.config : { ...record.config, styling: "inherit" };
|
|
2184
|
+
const styled = [];
|
|
2185
|
+
const placing = typeof options.place === "function";
|
|
2186
|
+
const themeHost = placing && typeof host.style === "object" ? host : root;
|
|
2187
|
+
const sized = `${shadowed ? ":host" : ".wmc"} { --wm-control-size: ${CONTROL_SIZE}px; }
|
|
2188
|
+
`;
|
|
2189
|
+
const applyTheme = () => {
|
|
2190
|
+
const own = styling === "own";
|
|
2191
|
+
style.textContent = `${STYLE}${own && !placing ? sized : ""}${shadowed && themed && own ? `:host { ${themed} }
|
|
2192
|
+
` : ""}`;
|
|
2193
|
+
if (!shadowed) applyComponentStyle(themeHost, styling === "own" ? project.layout.style : void 0);
|
|
2194
|
+
};
|
|
2195
|
+
applyTheme();
|
|
2196
|
+
const stage = document.createElement("div");
|
|
2197
|
+
stage.className = "wmc__stage";
|
|
2198
|
+
root.append(stage);
|
|
2199
|
+
host.append(style);
|
|
2200
|
+
if (!placing) host.append(root);
|
|
2201
|
+
const resume = () => {
|
|
2202
|
+
const context = audio.peek();
|
|
2203
|
+
if (context?.state === "suspended" && typeof context.resume === "function") void context.resume();
|
|
2204
|
+
};
|
|
2205
|
+
const gestures = placing ? host : root;
|
|
2206
|
+
gestures.addEventListener("pointerdown", resume);
|
|
2207
|
+
gestures.addEventListener("keydown", resume);
|
|
2208
|
+
const controls = /* @__PURE__ */ new Map();
|
|
2209
|
+
const releases = [];
|
|
2210
|
+
const buffers = () => {
|
|
2211
|
+
const context = audio.prepare();
|
|
2212
|
+
return { store: sharedBuffersFor(context), context };
|
|
2213
|
+
};
|
|
2214
|
+
const shown = componentContents(project);
|
|
2215
|
+
const { x: minX, y: minY } = componentFrame(shown.map((item) => ({ x: item.x, y: item.y, width: 0, height: 0 }))).stage;
|
|
2216
|
+
const extent = document.createElement("div");
|
|
2217
|
+
extent.className = "wmc__extent";
|
|
2218
|
+
stage.append(extent);
|
|
2219
|
+
const cells = [];
|
|
2220
|
+
const fit = () => {
|
|
2221
|
+
let width = 0, height = 0;
|
|
2222
|
+
for (const item of cells) {
|
|
2223
|
+
width = Math.max(width, item.x + (item.cell.offsetWidth || item.width));
|
|
2224
|
+
height = Math.max(height, item.y + (item.cell.offsetHeight || item.height));
|
|
2225
|
+
}
|
|
2226
|
+
extent.style.width = `${width}px`;
|
|
2227
|
+
extent.style.height = `${height}px`;
|
|
2228
|
+
stage.style.height = `${Math.min(height, MAX_STAGE)}px`;
|
|
2229
|
+
};
|
|
2230
|
+
const placeLevel = (contents, container, left, top, topLevel) => {
|
|
2231
|
+
for (const { record, x, y, width: sizedWidth, height: sizedHeight, kind } of contents) {
|
|
2232
|
+
const cell = document.createElement("div");
|
|
2233
|
+
cell.className = "wmc__item";
|
|
2234
|
+
cell.dataset.kind = kind;
|
|
2235
|
+
cell.style.left = `${x - left}px`;
|
|
2236
|
+
cell.style.top = `${y - top}px`;
|
|
2237
|
+
const place = topLevel && placing ? options.place(record) : void 0;
|
|
2238
|
+
if (topLevel && placing && !place) continue;
|
|
2239
|
+
if (place) {
|
|
2240
|
+
cell.className = "wmc__placed";
|
|
2241
|
+
cell.style.left = cell.style.top = "";
|
|
2242
|
+
releases.push(() => cell.remove());
|
|
2243
|
+
}
|
|
2244
|
+
const stage2 = place ?? container;
|
|
2245
|
+
const at = { x: x - left, y: y - top };
|
|
2246
|
+
const keep = (item) => {
|
|
2247
|
+
if (topLevel) cells.push(item);
|
|
2248
|
+
};
|
|
2249
|
+
if (kind === "nested") {
|
|
2250
|
+
const inner = componentContents(project, record.id);
|
|
2251
|
+
const { x: innerLeft, y: innerTop } = componentFrame(inner.map((item) => ({ x: item.x, y: item.y, width: 0, height: 0 }))).stage;
|
|
2252
|
+
cell.classList.add("wmc__nested");
|
|
2253
|
+
cell.setAttribute("role", "group");
|
|
2254
|
+
cell.setAttribute("aria-label", record.label ?? record.id);
|
|
2255
|
+
stage2.append(cell);
|
|
2256
|
+
placeLevel(inner, cell, innerLeft, innerTop, false);
|
|
2257
|
+
const width = Math.max(0, ...inner.map((item) => item.x - innerLeft + (item.width ?? 0)));
|
|
2258
|
+
const height = Math.max(0, ...inner.map((item) => item.y - innerTop + (item.height ?? 0)));
|
|
2259
|
+
const measure = () => {
|
|
2260
|
+
let right = width, bottom = height;
|
|
2261
|
+
for (const child of cell.children) {
|
|
2262
|
+
const element = child;
|
|
2263
|
+
right = Math.max(right, element.offsetLeft + element.offsetWidth);
|
|
2264
|
+
bottom = Math.max(bottom, element.offsetTop + element.offsetHeight);
|
|
2265
|
+
}
|
|
2266
|
+
cell.style.width = `${right}px`;
|
|
2267
|
+
cell.style.height = `${bottom}px`;
|
|
2268
|
+
};
|
|
2269
|
+
measure();
|
|
2270
|
+
const observer = typeof ResizeObserver === "function" ? new ResizeObserver(measure) : void 0;
|
|
2271
|
+
for (const child of cell.children) observer?.observe(child);
|
|
2272
|
+
releases.push(() => observer?.disconnect());
|
|
2273
|
+
keep({ cell, ...at, width, height });
|
|
2274
|
+
continue;
|
|
2275
|
+
}
|
|
2276
|
+
const sized2 = sizedWidth !== void 0 && sizedHeight !== void 0;
|
|
2277
|
+
if (sized2) {
|
|
2278
|
+
cell.style.width = `${sizedWidth}px`;
|
|
2279
|
+
cell.style.height = `${sizedHeight}px`;
|
|
2280
|
+
cell.dataset.sized = "true";
|
|
2281
|
+
}
|
|
2282
|
+
const found = componentManifestOf(record.plugin.id);
|
|
2283
|
+
const manifest = found && manifestFor(found, configOf(record));
|
|
2284
|
+
if (manifest) {
|
|
2285
|
+
const element = createComponentElement(manifest, configOf(record), document);
|
|
2286
|
+
styled.push(() => configureComponentElement(element, manifest, configOf(record)));
|
|
2287
|
+
if (!sized2) cell.style.width = `${manifest.width}px`;
|
|
2288
|
+
cell.append(element);
|
|
2289
|
+
keep({ cell, ...at, width: sizedWidth ?? manifest.width, height: sizedHeight ?? 180 });
|
|
2290
|
+
const needsBuffers = manifest.parameters.some((parameter) => parameter.buffer);
|
|
2291
|
+
releases.push(bindComponentElement(element, manifest, surfaces.slot(record.id), record.config, {
|
|
2292
|
+
onGesture: resume,
|
|
2293
|
+
onError: report,
|
|
2294
|
+
editableBuffers: options.editableBuffers,
|
|
2295
|
+
...needsBuffers ? { buffers: buffers() } : {}
|
|
2296
|
+
}));
|
|
2297
|
+
stage2.append(cell);
|
|
2298
|
+
if (sized2) releases.push(fitComponentElement(element, manifest, cell));
|
|
2299
|
+
} else {
|
|
2300
|
+
const kind2 = controlKindOf(record);
|
|
2301
|
+
const label = record.label ?? kind2;
|
|
2302
|
+
const natural = controlSize(kind2, record.config);
|
|
2303
|
+
stage2.append(cell);
|
|
2304
|
+
if (!sized2) {
|
|
2305
|
+
cell.style.width = `${natural.width}px`;
|
|
2306
|
+
cell.style.height = `${natural.height}px`;
|
|
2307
|
+
}
|
|
2308
|
+
keep({ cell, ...at, width: sizedWidth ?? natural.width, height: sizedHeight ?? natural.height });
|
|
2309
|
+
const restyle = () => applyComponentStyle(cell, ownStyle(configOf(record)));
|
|
2310
|
+
restyle();
|
|
2311
|
+
styled.push(restyle);
|
|
2312
|
+
controls.set(record.id, mountModuleControl(cell, kind2, { label, cardHost: place ? cell : root, onError: report }));
|
|
2313
|
+
}
|
|
2314
|
+
}
|
|
2315
|
+
};
|
|
2316
|
+
placeLevel(shown, stage, minX, minY, true);
|
|
2317
|
+
fit();
|
|
2318
|
+
const resized = typeof ResizeObserver === "function" ? new ResizeObserver(fit) : void 0;
|
|
2319
|
+
for (const { cell } of cells) resized?.observe(cell);
|
|
2320
|
+
const refresh = () => {
|
|
2321
|
+
for (const [id, control] of controls) control.refresh(moduleControlOf(runtime.getModule(id)));
|
|
2322
|
+
};
|
|
2323
|
+
const stopRuntime = runtime.subscribe(refresh);
|
|
2324
|
+
let destroyed = false;
|
|
2325
|
+
const ready = runtime.restore(options.keepPosition ? project : { ...project, session: { ...project.session, positionSeconds: 0 } }).then(() => refresh());
|
|
2326
|
+
ready.catch(report);
|
|
2327
|
+
const handle = {
|
|
2328
|
+
runtime,
|
|
2329
|
+
ready,
|
|
2330
|
+
setStyling(source) {
|
|
2331
|
+
const next = source === "inherit" ? "inherit" : "own";
|
|
2332
|
+
if (destroyed || next === styling) return;
|
|
2333
|
+
styling = next;
|
|
2334
|
+
applyTheme();
|
|
2335
|
+
for (const restyle of styled) restyle();
|
|
2336
|
+
},
|
|
2337
|
+
async save() {
|
|
2338
|
+
await ready;
|
|
2339
|
+
await surfaces.settled();
|
|
2340
|
+
return runtime.exportProject();
|
|
2341
|
+
},
|
|
2342
|
+
async play() {
|
|
2343
|
+
await ready;
|
|
2344
|
+
const context = audio.prepare();
|
|
2345
|
+
if (context.state === "suspended" && typeof context.resume === "function") await context.resume();
|
|
2346
|
+
const session = runtime.session;
|
|
2347
|
+
if (!session) return;
|
|
2348
|
+
if (session.snapshot.ended) await session.seek(0);
|
|
2349
|
+
await session.play();
|
|
2350
|
+
},
|
|
2351
|
+
async pause() {
|
|
2352
|
+
await runtime.session?.pause();
|
|
2353
|
+
},
|
|
2354
|
+
destroy() {
|
|
2355
|
+
if (destroyed) return;
|
|
2356
|
+
destroyed = true;
|
|
2357
|
+
resized?.disconnect();
|
|
2358
|
+
stopRuntime();
|
|
2359
|
+
for (const control of controls.values()) control.destroy();
|
|
2360
|
+
root.remove();
|
|
2361
|
+
style.remove();
|
|
2362
|
+
for (const release of releases) {
|
|
2363
|
+
try {
|
|
2364
|
+
release();
|
|
2365
|
+
} catch (error) {
|
|
2366
|
+
report(error);
|
|
2367
|
+
}
|
|
2368
|
+
}
|
|
2369
|
+
runtime.dispose();
|
|
2370
|
+
for (const registration of registrations) registration.release();
|
|
2371
|
+
if (!options.audio) audio.dispose();
|
|
2372
|
+
root.removeEventListener("pointerdown", resume);
|
|
2373
|
+
root.removeEventListener("keydown", resume);
|
|
2374
|
+
}
|
|
2375
|
+
};
|
|
2376
|
+
return handle;
|
|
2377
|
+
}
|
|
2378
|
+
function defineComponentElement(tag, project, options = {}) {
|
|
2379
|
+
var _handle;
|
|
2380
|
+
const existing = customElements.get(tag);
|
|
2381
|
+
if (existing) return existing;
|
|
2382
|
+
const document = parseModuleProject3(project);
|
|
2383
|
+
const source = (element) => element.getAttribute("styling") === "inherit" ? "inherit" : "own";
|
|
2384
|
+
class WebMusicComponentElement extends HTMLElement {
|
|
2385
|
+
constructor() {
|
|
2386
|
+
super(...arguments);
|
|
2387
|
+
__privateAdd(this, _handle);
|
|
2388
|
+
}
|
|
2389
|
+
attributeChangedCallback() {
|
|
2390
|
+
__privateGet(this, _handle)?.setStyling(source(this));
|
|
2391
|
+
}
|
|
2392
|
+
/** The running component, while connected. */
|
|
2393
|
+
get component() {
|
|
2394
|
+
return __privateGet(this, _handle);
|
|
2395
|
+
}
|
|
2396
|
+
connectedCallback() {
|
|
2397
|
+
if (__privateGet(this, _handle)) return;
|
|
2398
|
+
const root = this.shadowRoot ?? this.attachShadow({ mode: "open" });
|
|
2399
|
+
__privateSet(this, _handle, mountComponent(root, document, {
|
|
2400
|
+
...options,
|
|
2401
|
+
label: this.getAttribute("aria-label") ?? options.label,
|
|
2402
|
+
styling: this.hasAttribute("styling") ? source(this) : options.styling
|
|
2403
|
+
}));
|
|
2404
|
+
}
|
|
2405
|
+
disconnectedCallback() {
|
|
2406
|
+
__privateGet(this, _handle)?.destroy();
|
|
2407
|
+
__privateSet(this, _handle, void 0);
|
|
2408
|
+
}
|
|
2409
|
+
}
|
|
2410
|
+
_handle = new WeakMap();
|
|
2411
|
+
/** `styling="inherit"` sets the component's own theme and styles aside for those of what holds it (DEC-090). */
|
|
2412
|
+
WebMusicComponentElement.observedAttributes = ["styling"];
|
|
2413
|
+
customElements.define(tag, WebMusicComponentElement);
|
|
2414
|
+
return WebMusicComponentElement;
|
|
2415
|
+
}
|
|
2416
|
+
|
|
2417
|
+
// src/component/export.ts
|
|
2418
|
+
import { parseModuleProject as parseModuleProject4 } from "@webmusic/kernel/module";
|
|
2419
|
+
import { processorPlugin } from "@webmusic/kernel/processor";
|
|
2420
|
+
|
|
2421
|
+
// src/version.ts
|
|
2422
|
+
var BRIDGE_VERSION = "0.4.0";
|
|
2423
|
+
|
|
2424
|
+
// src/component/export.ts
|
|
2425
|
+
var TAG = /^[a-z][a-z0-9._]*-[a-z0-9._-]*$/;
|
|
2426
|
+
var RESERVED = /* @__PURE__ */ new Set(["annotation-xml", "color-profile", "font-face", "font-face-src", "font-face-uri", "font-face-format", "font-face-name", "missing-glyph"]);
|
|
2427
|
+
function checkTag(tag) {
|
|
2428
|
+
if (!TAG.test(tag) || RESERVED.has(tag)) throw new TypeError(`${tag} is not a custom element name: use lowercase letters with a hyphen, such as arabesque-player.`);
|
|
2429
|
+
}
|
|
2430
|
+
function standardPluginIds() {
|
|
2431
|
+
return new Set(createComponentPlugins({ surfaces: new ComponentSurfaces(), midi: void 0 }).map((plugin) => plugin.id));
|
|
2432
|
+
}
|
|
2433
|
+
function componentResources(input, provided = []) {
|
|
2434
|
+
const project = parseModuleProject4(input);
|
|
2435
|
+
const known = standardPluginIds();
|
|
2436
|
+
const plugins = [...new Set(project.modules.map((module) => module.plugin.id))].filter((id) => !known.has(id) && !provided.includes(id)).sort();
|
|
2437
|
+
return Object.freeze({ assets: Object.freeze(project.assets.map((asset) => asset.uri)), plugins: Object.freeze(plugins) });
|
|
2438
|
+
}
|
|
2439
|
+
var json = (project) => JSON.stringify({ ...project, session: { ...project.session, positionSeconds: 0 } }, null, 2);
|
|
2440
|
+
var scriptSafe = (text) => text.replace(/</g, "\\u003c").replace(/[\u2028\u2029]/g, (character) => character === "\u2028" ? "\\u2028" : "\\u2029");
|
|
2441
|
+
function resourceComment(resources) {
|
|
2442
|
+
const lines = [
|
|
2443
|
+
...resources.assets.length ? ["// Assets it loads, relative to the page:", ...resources.assets.map((uri) => `// ${uri}`)] : [],
|
|
2444
|
+
...resources.plugins.length ? ["// Plugins to supply through `extra` (they stay unresolved until you do):", ...resources.plugins.map((id) => `// ${id}`)] : []
|
|
2445
|
+
];
|
|
2446
|
+
return lines.length ? `${lines.join("\n")}
|
|
2447
|
+
` : "";
|
|
2448
|
+
}
|
|
2449
|
+
function componentModuleSource(input, options) {
|
|
2450
|
+
checkTag(options.tag);
|
|
2451
|
+
const project = parseModuleProject4(input);
|
|
2452
|
+
return `// <${options.tag}> \u2014 a WebMusic component.
|
|
2453
|
+
// Needs @webmusic/kernel, @webmusic/ui, @webmusic/score, @webmusic/audio and
|
|
2454
|
+
// @webmusic/bridge. Import this module once, then write <${options.tag}></${options.tag}>.
|
|
2455
|
+
${resourceComment(componentResources(project, options.provided))}import {defineComponentElement} from '@webmusic/bridge/component';
|
|
2456
|
+
|
|
2457
|
+
export const project = ${json(project)};
|
|
2458
|
+
|
|
2459
|
+
export default defineComponentElement(${JSON.stringify(options.tag)}, project);
|
|
2460
|
+
`;
|
|
2461
|
+
}
|
|
2462
|
+
function componentPageSource(input, options) {
|
|
2463
|
+
checkTag(options.tag);
|
|
2464
|
+
const project = parseModuleProject4(input);
|
|
2465
|
+
const title = (options.title ?? project.id).replace(/[<&]/g, (character) => character === "<" ? "<" : "&");
|
|
2466
|
+
const moduleUrl = options.moduleUrl ?? `https://cdn.jsdelivr.net/npm/@webmusic/bridge@${options.version ?? BRIDGE_VERSION}/component/+esm`;
|
|
2467
|
+
const resources = componentResources(project, options.provided);
|
|
2468
|
+
const base = options.assetBase === void 0 ? "" : `, {base: ${JSON.stringify(options.assetBase)}}`;
|
|
2469
|
+
return `<!doctype html>
|
|
2470
|
+
<html lang="en">
|
|
2471
|
+
<head>
|
|
2472
|
+
<meta charset="utf-8">
|
|
2473
|
+
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
2474
|
+
<title>${title}</title>
|
|
2475
|
+
<style>
|
|
2476
|
+
:root { color-scheme: light dark; }
|
|
2477
|
+
body { margin: 0; padding: 16px; background: light-dark(#fff, #111); color: light-dark(#111, #eee); }
|
|
2478
|
+
</style>
|
|
2479
|
+
</head>
|
|
2480
|
+
<body>
|
|
2481
|
+
<${options.tag}></${options.tag}>
|
|
2482
|
+
<script type="module">
|
|
2483
|
+
${scriptSafe(resourceComment(resources))}import {defineComponentElement} from ${JSON.stringify(moduleUrl)};
|
|
2484
|
+
const project = ${scriptSafe(json(project))};
|
|
2485
|
+
defineComponentElement(${JSON.stringify(options.tag)}, project${base});
|
|
2486
|
+
</script>
|
|
2487
|
+
</body>
|
|
2488
|
+
</html>
|
|
2489
|
+
`;
|
|
2490
|
+
}
|
|
2491
|
+
var MEDIA_TYPES = Object.freeze({
|
|
2492
|
+
mid: "audio/midi",
|
|
2493
|
+
midi: "audio/midi",
|
|
2494
|
+
mxl: "application/vnd.recordare.musicxml",
|
|
2495
|
+
musicxml: "application/vnd.recordare.musicxml+xml",
|
|
2496
|
+
xml: "application/vnd.recordare.musicxml+xml",
|
|
2497
|
+
wav: "audio/wav",
|
|
2498
|
+
mp3: "audio/mpeg",
|
|
2499
|
+
ogg: "audio/ogg",
|
|
2500
|
+
oga: "audio/ogg",
|
|
2501
|
+
flac: "audio/flac",
|
|
2502
|
+
m4a: "audio/mp4",
|
|
2503
|
+
aac: "audio/aac",
|
|
2504
|
+
webm: "audio/webm"
|
|
2505
|
+
});
|
|
2506
|
+
function componentAssetMediaType(uri) {
|
|
2507
|
+
const extension = /\.([a-z0-9]+)(?:[?#].*)?$/i.exec(uri)?.[1]?.toLowerCase();
|
|
2508
|
+
return extension ? MEDIA_TYPES[extension] : void 0;
|
|
2509
|
+
}
|
|
2510
|
+
function componentAssetDataUri(uri, bytes, type) {
|
|
2511
|
+
const mediaType = type || componentAssetMediaType(uri) || "application/octet-stream";
|
|
2512
|
+
let binary = "";
|
|
2513
|
+
for (let offset = 0; offset < bytes.length; offset += 32768) binary += String.fromCharCode(...bytes.subarray(offset, offset + 32768));
|
|
2514
|
+
return `data:${mediaType};base64,${btoa(binary)}`;
|
|
2515
|
+
}
|
|
2516
|
+
var blobUrls = /* @__PURE__ */ new Map();
|
|
2517
|
+
function embeddedProcessorPlugin(definition, worklet) {
|
|
2518
|
+
if (typeof worklet !== "string" || !worklet.trim()) throw new TypeError("An embedded processor needs its worklet module as text.");
|
|
2519
|
+
const key = `${definition.id}@${definition.version}`;
|
|
2520
|
+
return processorPlugin(definition, { source: {
|
|
2521
|
+
get module() {
|
|
2522
|
+
let url = blobUrls.get(key);
|
|
2523
|
+
if (!url) {
|
|
2524
|
+
url = URL.createObjectURL(new Blob([worklet], { type: "text/javascript" }));
|
|
2525
|
+
blobUrls.set(key, url);
|
|
2526
|
+
}
|
|
2527
|
+
return url;
|
|
2528
|
+
}
|
|
2529
|
+
} });
|
|
2530
|
+
}
|
|
2531
|
+
var inlineScript = (text) => text.replace(/<\/(script)/gi, "<\\/$1").replace(/<!--/g, "<\\!--");
|
|
2532
|
+
function standalone(input, options) {
|
|
2533
|
+
checkTag(options.tag);
|
|
2534
|
+
if (typeof options.runtime !== "string" || !options.runtime.includes("WebMusicComponent")) {
|
|
2535
|
+
throw new TypeError("A complete export needs the text of @webmusic/bridge/component/global as `runtime`.");
|
|
2536
|
+
}
|
|
2537
|
+
const project = parseModuleProject4(input);
|
|
2538
|
+
const runtime = options.runtime.replace(/\n?\/\/# sourceMappingURL=\S*\s*$/, "\n");
|
|
2539
|
+
const embedded = {};
|
|
2540
|
+
for (const [uri, data] of Object.entries(options.assets ?? {})) {
|
|
2541
|
+
if (typeof data !== "string" || !data.startsWith("data:")) throw new TypeError(`The embedded asset ${uri} must be a data: URI.`);
|
|
2542
|
+
if (project.assets.some((asset) => asset.uri === uri)) embedded[uri] = data;
|
|
2543
|
+
}
|
|
2544
|
+
const processors = options.processors ?? [];
|
|
2545
|
+
const provided = [...options.provided ?? [], ...processors.map((processor) => processor.definition.id)];
|
|
2546
|
+
const resources = componentResources(project, provided);
|
|
2547
|
+
const outside = { assets: resources.assets.filter((uri) => !(uri in embedded)), plugins: resources.plugins };
|
|
2548
|
+
const carriesAssets = Object.keys(embedded).length > 0;
|
|
2549
|
+
const settings = [
|
|
2550
|
+
...carriesAssets ? ["embedded: assets"] : [],
|
|
2551
|
+
...processors.length ? ["extra: processors.map(processor => WebMusicComponent.embeddedProcessorPlugin(processor.definition, processor.worklet))"] : []
|
|
2552
|
+
];
|
|
2553
|
+
const extra = settings.length ? `, {${settings.join(", ")}}` : "";
|
|
2554
|
+
const carried = `${carriesAssets ? `const assets = ${JSON.stringify(embedded)};
|
|
2555
|
+
` : ""}${processors.length ? `const processors = ${JSON.stringify(processors.map((processor) => ({ definition: processor.definition, worklet: processor.worklet })))};
|
|
2556
|
+
` : ""}`;
|
|
2557
|
+
return { project, outside, extra, carried, runtime };
|
|
2558
|
+
}
|
|
2559
|
+
function componentStandalonePageSource(input, options) {
|
|
2560
|
+
const { project, outside, extra, carried, runtime } = standalone(input, options);
|
|
2561
|
+
const title = (options.title ?? project.id).replace(/[<&]/g, (character) => character === "<" ? "<" : "&");
|
|
2562
|
+
return `<!doctype html>
|
|
2563
|
+
<html lang="en">
|
|
2564
|
+
<head>
|
|
2565
|
+
<meta charset="utf-8">
|
|
2566
|
+
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
2567
|
+
<title>${title}</title>
|
|
2568
|
+
<style>
|
|
2569
|
+
:root { color-scheme: light dark; }
|
|
2570
|
+
body { margin: 0; padding: 16px; background: light-dark(#fff, #111); color: light-dark(#111, #eee); }
|
|
2571
|
+
</style>
|
|
2572
|
+
</head>
|
|
2573
|
+
<body>
|
|
2574
|
+
<${options.tag}></${options.tag}>
|
|
2575
|
+
<script>
|
|
2576
|
+
${inlineScript(runtime)}
|
|
2577
|
+
</script>
|
|
2578
|
+
<script>
|
|
2579
|
+
${scriptSafe(resourceComment(outside))}(() => {
|
|
2580
|
+
const project = ${scriptSafe(json(project))};
|
|
2581
|
+
${scriptSafe(carried)}WebMusicComponent.defineComponentElement(${JSON.stringify(options.tag)}, project${extra});
|
|
2582
|
+
})();
|
|
2583
|
+
</script>
|
|
2584
|
+
</body>
|
|
2585
|
+
</html>
|
|
2586
|
+
`;
|
|
2587
|
+
}
|
|
2588
|
+
function componentStandaloneModuleSource(input, options) {
|
|
2589
|
+
const { project, outside, extra, carried, runtime } = standalone(input, options);
|
|
2590
|
+
return `// <${options.tag}> \u2014 a WebMusic component, complete in this file.
|
|
2591
|
+
// Import this module once, then write <${options.tag}></${options.tag}>. Nothing else is installed.
|
|
2592
|
+
${resourceComment(outside)}${runtime}
|
|
2593
|
+
export const project = ${json(project)};
|
|
2594
|
+
${carried}
|
|
2595
|
+
export default WebMusicComponent.defineComponentElement(${JSON.stringify(options.tag)}, project${extra});
|
|
2596
|
+
`;
|
|
2597
|
+
}
|
|
2598
|
+
export {
|
|
2599
|
+
CLIP_PLUGIN,
|
|
2600
|
+
COMPONENT_FRAME,
|
|
2601
|
+
COMPONENT_PLUGIN_VERSION,
|
|
2602
|
+
CONTROL_KINDS,
|
|
2603
|
+
ComponentSurfaces,
|
|
2604
|
+
GRAPH_ATTRIBUTES,
|
|
2605
|
+
KEYS_PLUGIN,
|
|
2606
|
+
LEVEL_PLUGIN,
|
|
2607
|
+
METER_PLUGIN,
|
|
2608
|
+
OFFICIAL_COMPONENTS,
|
|
2609
|
+
ROLL_PLUGIN,
|
|
2610
|
+
SAVED_SCORE_ENCODING,
|
|
2611
|
+
SAVED_SCORE_FORMAT,
|
|
2612
|
+
SCOPE_PLUGIN,
|
|
2613
|
+
SPECTRUM_PLUGIN,
|
|
2614
|
+
SurfaceSlot,
|
|
2615
|
+
THEME_TOKENS,
|
|
2616
|
+
applyComponentStyle,
|
|
2617
|
+
bindBufferView,
|
|
2618
|
+
bindComponentElement,
|
|
2619
|
+
canSaveScores,
|
|
2620
|
+
componentAssetDataUri,
|
|
2621
|
+
componentAssetMediaType,
|
|
2622
|
+
componentAssetRoles,
|
|
2623
|
+
componentContents,
|
|
2624
|
+
componentFrame,
|
|
2625
|
+
componentManifestOf,
|
|
2626
|
+
componentModuleSource,
|
|
2627
|
+
componentPageSource,
|
|
2628
|
+
componentPorts,
|
|
2629
|
+
componentResources,
|
|
2630
|
+
componentSettings,
|
|
2631
|
+
componentStandaloneModuleSource,
|
|
2632
|
+
componentStandalonePageSource,
|
|
2633
|
+
componentTarget,
|
|
2634
|
+
componentTheme,
|
|
2635
|
+
componentUsage,
|
|
2636
|
+
configOfArguments,
|
|
2637
|
+
configureComponentElement,
|
|
2638
|
+
controlKindOf,
|
|
2639
|
+
controlSize,
|
|
2640
|
+
createComponentElement,
|
|
2641
|
+
createComponentPlugins,
|
|
2642
|
+
decodeScore,
|
|
2643
|
+
defineComponentElement,
|
|
2644
|
+
defineComponentElements,
|
|
2645
|
+
embeddedProcessorPlugin,
|
|
2646
|
+
encodeScore,
|
|
2647
|
+
findComponentManifest,
|
|
2648
|
+
fitComponentElement,
|
|
2649
|
+
isColorAttribute,
|
|
2650
|
+
isListener,
|
|
2651
|
+
isStyleSetting,
|
|
2652
|
+
keepComponentSessionPreloaded,
|
|
2653
|
+
manifestFor,
|
|
2654
|
+
migrateBufferViews,
|
|
2655
|
+
mountComponent,
|
|
2656
|
+
mountModuleControl,
|
|
2657
|
+
ownStyle,
|
|
2658
|
+
parameterValue,
|
|
2659
|
+
projectManifest,
|
|
2660
|
+
rebindsOnConfig,
|
|
2661
|
+
resolveComponentAsset,
|
|
2662
|
+
settableAttributes,
|
|
2663
|
+
standardPluginIds,
|
|
2664
|
+
styleDeclarations,
|
|
2665
|
+
styleSettings,
|
|
2666
|
+
styleSource,
|
|
2667
|
+
themeToken,
|
|
2668
|
+
withComponentSetting,
|
|
2669
|
+
withComponentTheme,
|
|
2670
|
+
withStyleSource
|
|
2671
|
+
};
|
|
2672
|
+
//# sourceMappingURL=component.js.map
|