@kubohiroya/turbowarp-asset-cache 0.2.2 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -2
- package/dist/asset-cache.js +484 -63
- package/dist/composition.js +507 -63
- package/dist/types/audio-output.d.ts +5 -0
- package/dist/types/audio-playback.d.ts +46 -0
- package/dist/types/composition.d.ts +3 -0
- package/dist/types/extension.d.ts +4 -0
- package/docs/audio-playback.md +49 -0
- package/package.json +4 -9
package/README.md
CHANGED
|
@@ -48,13 +48,13 @@ The built JavaScript file is committed to this repository so users do not need t
|
|
|
48
48
|
The versioned npm package contains the same reviewed build:
|
|
49
49
|
|
|
50
50
|
```bash
|
|
51
|
-
pnpm add --save-exact @kubohiroya/turbowarp-asset-cache@0.
|
|
51
|
+
pnpm add --save-exact @kubohiroya/turbowarp-asset-cache@0.3.0
|
|
52
52
|
```
|
|
53
53
|
|
|
54
54
|
Load `node_modules/@kubohiroya/turbowarp-asset-cache/dist/asset-cache.js`, or use the version-pinned CDN URL:
|
|
55
55
|
|
|
56
56
|
```text
|
|
57
|
-
https://cdn.jsdelivr.net/npm/@kubohiroya/turbowarp-asset-cache@0.
|
|
57
|
+
https://cdn.jsdelivr.net/npm/@kubohiroya/turbowarp-asset-cache@0.3.0/dist/asset-cache.js
|
|
58
58
|
```
|
|
59
59
|
|
|
60
60
|
## Quick Start
|
package/dist/asset-cache.js
CHANGED
|
@@ -7,6 +7,421 @@
|
|
|
7
7
|
(function (Scratch) {
|
|
8
8
|
'use strict';
|
|
9
9
|
|
|
10
|
+
//#region src/audio-output.ts
|
|
11
|
+
/** Media volume handles attenuation and fades; this shared graph adds amplification. */
|
|
12
|
+
var shared;
|
|
13
|
+
var maximumFloat = 34028234663852886e22;
|
|
14
|
+
function createAudioAmplifier(audio) {
|
|
15
|
+
if (typeof globalThis.AudioContext !== "function") throw new Error("Audio amplification requires Web Audio support.");
|
|
16
|
+
if (!shared || shared.context.state === "closed") shared = {
|
|
17
|
+
context: new AudioContext(),
|
|
18
|
+
users: 0
|
|
19
|
+
};
|
|
20
|
+
const owner = shared;
|
|
21
|
+
owner.users++;
|
|
22
|
+
let source;
|
|
23
|
+
let gain;
|
|
24
|
+
let released = false;
|
|
25
|
+
const dispose = () => {
|
|
26
|
+
if (released) return;
|
|
27
|
+
released = true;
|
|
28
|
+
source?.disconnect();
|
|
29
|
+
gain?.disconnect();
|
|
30
|
+
owner.users--;
|
|
31
|
+
if (owner.users === 0) {
|
|
32
|
+
if (shared === owner) shared = void 0;
|
|
33
|
+
owner.context.close().catch(() => {});
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
try {
|
|
37
|
+
source = owner.context.createMediaElementSource(audio);
|
|
38
|
+
gain = owner.context.createGain();
|
|
39
|
+
source.connect(gain);
|
|
40
|
+
gain.connect(owner.context.destination);
|
|
41
|
+
} catch (error) {
|
|
42
|
+
dispose();
|
|
43
|
+
throw error;
|
|
44
|
+
}
|
|
45
|
+
const node = gain;
|
|
46
|
+
return {
|
|
47
|
+
setGain(value) {
|
|
48
|
+
node.gain.value = Math.min(maximumFloat, Math.max(1, value));
|
|
49
|
+
},
|
|
50
|
+
resume() {
|
|
51
|
+
return owner.context.state === "running" ? Promise.resolve() : owner.context.resume();
|
|
52
|
+
},
|
|
53
|
+
dispose
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
//#endregion
|
|
57
|
+
//#region src/audio-playback.ts
|
|
58
|
+
function playbackGain(value) {
|
|
59
|
+
const gain = value === void 0 ? 1 : value;
|
|
60
|
+
if (typeof gain !== "number" || !Number.isFinite(gain) || gain < 0) throw new TypeError("Audio gain must be a finite non-negative number.");
|
|
61
|
+
return gain;
|
|
62
|
+
}
|
|
63
|
+
function rate(value) {
|
|
64
|
+
if (typeof value !== "number" || !Number.isFinite(value) || value < .25 || value > 4) throw new TypeError("Audio playback rate must be from 0.25 to 4.");
|
|
65
|
+
return value;
|
|
66
|
+
}
|
|
67
|
+
function validateAudioPlaybackOptions(options) {
|
|
68
|
+
if (!options || typeof options !== "object" || Array.isArray(options) || Object.keys(options).some((key) => ![
|
|
69
|
+
"gain",
|
|
70
|
+
"muted",
|
|
71
|
+
"playbackRate",
|
|
72
|
+
"fadeIn",
|
|
73
|
+
"fadeOut"
|
|
74
|
+
].includes(key))) throw new TypeError("Invalid audio playback options.");
|
|
75
|
+
playbackGain(options.gain);
|
|
76
|
+
if (options.muted !== void 0 && typeof options.muted !== "boolean") throw new TypeError("Audio muted must be a boolean.");
|
|
77
|
+
rate(options.playbackRate ?? 1);
|
|
78
|
+
fadeSeconds(options.fadeIn, "fadeIn");
|
|
79
|
+
fadeSeconds(options.fadeOut, "fadeOut");
|
|
80
|
+
}
|
|
81
|
+
function fadeSeconds(value, label) {
|
|
82
|
+
const seconds = value === void 0 ? 0 : value;
|
|
83
|
+
if (typeof seconds !== "number" || !Number.isFinite(seconds) || seconds < 0) throw new TypeError(`Audio ${label} must be a finite non-negative number of seconds.`);
|
|
84
|
+
return seconds;
|
|
85
|
+
}
|
|
86
|
+
function aborted() {
|
|
87
|
+
const error = /* @__PURE__ */ new Error("Audio playback was cancelled.");
|
|
88
|
+
error.name = "AbortError";
|
|
89
|
+
return error;
|
|
90
|
+
}
|
|
91
|
+
/** One reusable native element; the caller owns source resolution and resource release. */
|
|
92
|
+
function createBrowserAudioPlayback(audio, options, release, playbackError) {
|
|
93
|
+
validateAudioPlaybackOptions(options);
|
|
94
|
+
let gain = playbackGain(options.gain);
|
|
95
|
+
audio.volume = Math.min(1, gain);
|
|
96
|
+
audio.muted = options.muted ?? false;
|
|
97
|
+
audio.playbackRate = options.playbackRate ?? 1;
|
|
98
|
+
audio.loop = false;
|
|
99
|
+
const defaultFadeIn = fadeSeconds(options.fadeIn, "fadeIn");
|
|
100
|
+
const defaultFadeOut = fadeSeconds(options.fadeOut, "fadeOut");
|
|
101
|
+
let activeFadeIn = defaultFadeIn;
|
|
102
|
+
let activeFadeOut = defaultFadeOut;
|
|
103
|
+
let envelope = 1;
|
|
104
|
+
let fadeStarted = 0;
|
|
105
|
+
let playing = false;
|
|
106
|
+
let fadeTimer;
|
|
107
|
+
let stopping;
|
|
108
|
+
let amplifier;
|
|
109
|
+
const pendingGain = /* @__PURE__ */ new Set();
|
|
110
|
+
let disposed = false;
|
|
111
|
+
let generation = 0;
|
|
112
|
+
let ended = Promise.resolve();
|
|
113
|
+
let settle;
|
|
114
|
+
let rejectStart;
|
|
115
|
+
let detachAbort;
|
|
116
|
+
const pendingMetadata = /* @__PURE__ */ new Set();
|
|
117
|
+
const requireActive = () => {
|
|
118
|
+
if (disposed) throw new Error("Audio playback is disposed.");
|
|
119
|
+
};
|
|
120
|
+
const clearFadeTimer = () => {
|
|
121
|
+
if (fadeTimer !== void 0) clearTimeout(fadeTimer);
|
|
122
|
+
fadeTimer = void 0;
|
|
123
|
+
};
|
|
124
|
+
const finish = (error) => {
|
|
125
|
+
playing = false;
|
|
126
|
+
for (const reject of pendingGain) reject(error ?? aborted());
|
|
127
|
+
pendingGain.clear();
|
|
128
|
+
clearFadeTimer();
|
|
129
|
+
const previousStop = stopping;
|
|
130
|
+
stopping = void 0;
|
|
131
|
+
previousStop?.detach();
|
|
132
|
+
previousStop?.reject(error ?? aborted());
|
|
133
|
+
generation++;
|
|
134
|
+
rejectStart?.(error ?? aborted());
|
|
135
|
+
rejectStart = void 0;
|
|
136
|
+
detachAbort?.();
|
|
137
|
+
detachAbort = void 0;
|
|
138
|
+
settle?.(error);
|
|
139
|
+
settle = void 0;
|
|
140
|
+
};
|
|
141
|
+
const writeVolume = () => {
|
|
142
|
+
audio.volume = Math.min(1, gain) * envelope;
|
|
143
|
+
amplifier?.setGain(gain);
|
|
144
|
+
};
|
|
145
|
+
const prepareAmplifier = () => {
|
|
146
|
+
if (!amplifier && gain > 1) amplifier = createAudioAmplifier(audio);
|
|
147
|
+
writeVolume();
|
|
148
|
+
};
|
|
149
|
+
const stopNow = () => {
|
|
150
|
+
const completed = stopping;
|
|
151
|
+
stopping = void 0;
|
|
152
|
+
completed?.detach();
|
|
153
|
+
audio.pause();
|
|
154
|
+
audio.loop = false;
|
|
155
|
+
audio.currentTime = 0;
|
|
156
|
+
finish();
|
|
157
|
+
envelope = 1;
|
|
158
|
+
writeVolume();
|
|
159
|
+
completed?.resolve();
|
|
160
|
+
};
|
|
161
|
+
const refreshFade = () => {
|
|
162
|
+
clearFadeTimer();
|
|
163
|
+
if (disposed || !playing && !stopping) return;
|
|
164
|
+
const now = performance.now();
|
|
165
|
+
let delay = 16;
|
|
166
|
+
if (stopping) {
|
|
167
|
+
const progress = Math.min(1, (now - stopping.started) / (stopping.seconds * 1e3));
|
|
168
|
+
envelope = stopping.from * (1 - progress);
|
|
169
|
+
writeVolume();
|
|
170
|
+
if (progress >= 1) {
|
|
171
|
+
stopNow();
|
|
172
|
+
return;
|
|
173
|
+
}
|
|
174
|
+
} else {
|
|
175
|
+
const fadeIn = activeFadeIn === 0 ? 1 : Math.min(1, (now - fadeStarted) / (activeFadeIn * 1e3));
|
|
176
|
+
const remaining = Number.isFinite(audio.duration) ? Math.max(0, audio.duration - audio.currentTime) / audio.playbackRate : Infinity;
|
|
177
|
+
const fadeOut = !audio.loop && activeFadeOut > 0 ? Math.min(1, remaining / activeFadeOut) : 1;
|
|
178
|
+
envelope = Math.max(0, Math.min(fadeIn, fadeOut));
|
|
179
|
+
writeVolume();
|
|
180
|
+
if (fadeIn >= 1) {
|
|
181
|
+
if (audio.loop || activeFadeOut === 0) return;
|
|
182
|
+
if (!Number.isFinite(remaining)) delay = 250;
|
|
183
|
+
else if (remaining > activeFadeOut) delay = Math.min(250, Math.max(16, (remaining - activeFadeOut) * 1e3));
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
fadeTimer = setTimeout(refreshFade, delay);
|
|
187
|
+
};
|
|
188
|
+
const onEnded = () => {
|
|
189
|
+
if (stopping) {
|
|
190
|
+
stopNow();
|
|
191
|
+
return;
|
|
192
|
+
}
|
|
193
|
+
finish();
|
|
194
|
+
if (activeFadeOut > 0 && !audio.loop) {
|
|
195
|
+
envelope = 0;
|
|
196
|
+
writeVolume();
|
|
197
|
+
}
|
|
198
|
+
};
|
|
199
|
+
const onError = () => {
|
|
200
|
+
audio.pause();
|
|
201
|
+
const error = playbackError(audio.error ?? /* @__PURE__ */ new Error("The browser audio element reported a playback error."));
|
|
202
|
+
finish(error);
|
|
203
|
+
for (const complete of pendingMetadata) complete(error);
|
|
204
|
+
};
|
|
205
|
+
audio.addEventListener("ended", onEnded);
|
|
206
|
+
audio.addEventListener("error", onError);
|
|
207
|
+
for (const event of [
|
|
208
|
+
"durationchange",
|
|
209
|
+
"ratechange",
|
|
210
|
+
"seeked"
|
|
211
|
+
]) audio.addEventListener(event, refreshFade);
|
|
212
|
+
return Object.freeze({
|
|
213
|
+
get ended() {
|
|
214
|
+
return ended;
|
|
215
|
+
},
|
|
216
|
+
get currentTime() {
|
|
217
|
+
return audio.currentTime;
|
|
218
|
+
},
|
|
219
|
+
get paused() {
|
|
220
|
+
return audio.paused;
|
|
221
|
+
},
|
|
222
|
+
get loop() {
|
|
223
|
+
return audio.loop;
|
|
224
|
+
},
|
|
225
|
+
get gain() {
|
|
226
|
+
return gain;
|
|
227
|
+
},
|
|
228
|
+
get muted() {
|
|
229
|
+
return audio.muted;
|
|
230
|
+
},
|
|
231
|
+
get playbackRate() {
|
|
232
|
+
return audio.playbackRate;
|
|
233
|
+
},
|
|
234
|
+
async play(playOptions = {}) {
|
|
235
|
+
requireActive();
|
|
236
|
+
if (!playOptions || typeof playOptions !== "object" || Array.isArray(playOptions) || Object.keys(playOptions).some((key) => ![
|
|
237
|
+
"restart",
|
|
238
|
+
"loop",
|
|
239
|
+
"signal",
|
|
240
|
+
"fadeIn",
|
|
241
|
+
"fadeOut",
|
|
242
|
+
"gain"
|
|
243
|
+
].includes(key)) || playOptions.restart !== void 0 && typeof playOptions.restart !== "boolean" || playOptions.loop !== void 0 && typeof playOptions.loop !== "boolean") throw new TypeError("Invalid audio play options.");
|
|
244
|
+
const fadeIn = fadeSeconds(playOptions.fadeIn === void 0 ? defaultFadeIn : playOptions.fadeIn, "fadeIn");
|
|
245
|
+
const fadeOut = fadeSeconds(playOptions.fadeOut === void 0 ? defaultFadeOut : playOptions.fadeOut, "fadeOut");
|
|
246
|
+
const nextGain = playOptions.gain === void 0 ? gain : playbackGain(playOptions.gain);
|
|
247
|
+
const signal = playOptions.signal;
|
|
248
|
+
if (signal?.aborted) throw aborted();
|
|
249
|
+
finish();
|
|
250
|
+
const run = generation;
|
|
251
|
+
gain = nextGain;
|
|
252
|
+
if (playOptions.restart) audio.currentTime = 0;
|
|
253
|
+
audio.loop = playOptions.loop ?? audio.loop;
|
|
254
|
+
activeFadeIn = fadeIn;
|
|
255
|
+
activeFadeOut = fadeOut;
|
|
256
|
+
envelope = fadeIn > 0 ? 0 : 1;
|
|
257
|
+
writeVolume();
|
|
258
|
+
ended = new Promise((resolve, reject) => {
|
|
259
|
+
settle = (error) => error === void 0 ? resolve() : reject(error);
|
|
260
|
+
});
|
|
261
|
+
ended.catch(() => {});
|
|
262
|
+
const onAbort = () => {
|
|
263
|
+
audio.pause();
|
|
264
|
+
finish(aborted());
|
|
265
|
+
};
|
|
266
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
267
|
+
detachAbort = () => signal?.removeEventListener("abort", onAbort);
|
|
268
|
+
try {
|
|
269
|
+
prepareAmplifier();
|
|
270
|
+
const started = audio.play();
|
|
271
|
+
const ready = amplifier ? Promise.all([started, amplifier.resume()]) : Promise.resolve(started);
|
|
272
|
+
await Promise.race([ready, new Promise((_resolve, reject) => {
|
|
273
|
+
rejectStart = reject;
|
|
274
|
+
})]);
|
|
275
|
+
if (run !== generation) throw aborted();
|
|
276
|
+
rejectStart = void 0;
|
|
277
|
+
playing = true;
|
|
278
|
+
fadeStarted = performance.now();
|
|
279
|
+
refreshFade();
|
|
280
|
+
} catch (cause) {
|
|
281
|
+
if (run !== generation) throw cause;
|
|
282
|
+
audio.pause();
|
|
283
|
+
const error = playbackError(cause);
|
|
284
|
+
finish(error);
|
|
285
|
+
throw error;
|
|
286
|
+
}
|
|
287
|
+
},
|
|
288
|
+
pause() {
|
|
289
|
+
requireActive();
|
|
290
|
+
audio.pause();
|
|
291
|
+
finish();
|
|
292
|
+
},
|
|
293
|
+
stop(stopOptions = {}) {
|
|
294
|
+
requireActive();
|
|
295
|
+
if (!stopOptions || typeof stopOptions !== "object" || Array.isArray(stopOptions) || Object.keys(stopOptions).some((key) => !["fadeOut", "signal"].includes(key))) throw new TypeError("Invalid audio stop options.");
|
|
296
|
+
const seconds = fadeSeconds(stopOptions.fadeOut === void 0 ? activeFadeOut : stopOptions.fadeOut, "fadeOut");
|
|
297
|
+
const signal = stopOptions.signal;
|
|
298
|
+
if (signal?.aborted) throw aborted();
|
|
299
|
+
if (seconds === 0 || audio.paused || !playing) {
|
|
300
|
+
stopNow();
|
|
301
|
+
return;
|
|
302
|
+
}
|
|
303
|
+
const previous = stopping;
|
|
304
|
+
stopping = void 0;
|
|
305
|
+
previous?.detach();
|
|
306
|
+
previous?.reject(aborted());
|
|
307
|
+
return new Promise((resolve, reject) => {
|
|
308
|
+
const onAbort = () => {
|
|
309
|
+
const current = stopping;
|
|
310
|
+
stopping = void 0;
|
|
311
|
+
current?.detach();
|
|
312
|
+
stopNow();
|
|
313
|
+
reject(aborted());
|
|
314
|
+
};
|
|
315
|
+
stopping = {
|
|
316
|
+
started: performance.now(),
|
|
317
|
+
seconds,
|
|
318
|
+
from: envelope,
|
|
319
|
+
resolve,
|
|
320
|
+
reject,
|
|
321
|
+
detach: () => signal?.removeEventListener("abort", onAbort)
|
|
322
|
+
};
|
|
323
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
324
|
+
if (signal?.aborted) onAbort();
|
|
325
|
+
else refreshFade();
|
|
326
|
+
});
|
|
327
|
+
},
|
|
328
|
+
async seek(seconds, signal) {
|
|
329
|
+
requireActive();
|
|
330
|
+
if (!Number.isFinite(seconds) || seconds < 0) throw new TypeError("Audio seek time must be non-negative.");
|
|
331
|
+
if (signal?.aborted) throw aborted();
|
|
332
|
+
if (audio.readyState < 1) await new Promise((resolve, reject) => {
|
|
333
|
+
const complete = (error) => {
|
|
334
|
+
clearTimeout(timer);
|
|
335
|
+
audio.removeEventListener("loadedmetadata", ready);
|
|
336
|
+
signal?.removeEventListener("abort", cancel);
|
|
337
|
+
pendingMetadata.delete(complete);
|
|
338
|
+
if (error === void 0) resolve();
|
|
339
|
+
else reject(error);
|
|
340
|
+
};
|
|
341
|
+
const ready = () => complete();
|
|
342
|
+
const cancel = () => complete(aborted());
|
|
343
|
+
const timer = setTimeout(() => complete(/* @__PURE__ */ new Error("Audio metadata did not become ready.")), 1e4);
|
|
344
|
+
pendingMetadata.add(complete);
|
|
345
|
+
audio.addEventListener("loadedmetadata", ready, { once: true });
|
|
346
|
+
signal?.addEventListener("abort", cancel, { once: true });
|
|
347
|
+
if (signal?.aborted) cancel();
|
|
348
|
+
else if (audio.readyState >= 1) ready();
|
|
349
|
+
});
|
|
350
|
+
requireActive();
|
|
351
|
+
if (signal?.aborted) throw aborted();
|
|
352
|
+
if (Number.isFinite(audio.duration) && seconds > audio.duration) throw new RangeError("Audio seek exceeds its duration.");
|
|
353
|
+
audio.currentTime = seconds;
|
|
354
|
+
refreshFade();
|
|
355
|
+
},
|
|
356
|
+
setGain(value) {
|
|
357
|
+
const normalized = playbackGain(value);
|
|
358
|
+
requireActive();
|
|
359
|
+
const previous = gain;
|
|
360
|
+
gain = normalized;
|
|
361
|
+
try {
|
|
362
|
+
if (playing) prepareAmplifier();
|
|
363
|
+
else writeVolume();
|
|
364
|
+
} catch (error) {
|
|
365
|
+
gain = previous;
|
|
366
|
+
writeVolume();
|
|
367
|
+
throw playbackError(error);
|
|
368
|
+
}
|
|
369
|
+
if (playing && amplifier) {
|
|
370
|
+
const run = generation;
|
|
371
|
+
let rejectResume;
|
|
372
|
+
const cancelled = new Promise((_resolve, reject) => {
|
|
373
|
+
rejectResume = reject;
|
|
374
|
+
});
|
|
375
|
+
pendingGain.add(rejectResume);
|
|
376
|
+
return Promise.race([amplifier.resume(), cancelled]).catch((cause) => {
|
|
377
|
+
if (run !== generation) throw cause;
|
|
378
|
+
audio.pause();
|
|
379
|
+
const error = playbackError(cause);
|
|
380
|
+
finish(error);
|
|
381
|
+
throw error;
|
|
382
|
+
}).finally(() => pendingGain.delete(rejectResume));
|
|
383
|
+
}
|
|
384
|
+
},
|
|
385
|
+
setMuted(value) {
|
|
386
|
+
if (typeof value !== "boolean") throw new TypeError("Audio muted must be a boolean.");
|
|
387
|
+
requireActive();
|
|
388
|
+
audio.muted = value;
|
|
389
|
+
},
|
|
390
|
+
setPlaybackRate(value) {
|
|
391
|
+
const normalized = rate(value);
|
|
392
|
+
requireActive();
|
|
393
|
+
audio.playbackRate = normalized;
|
|
394
|
+
refreshFade();
|
|
395
|
+
},
|
|
396
|
+
dispose() {
|
|
397
|
+
if (disposed) return;
|
|
398
|
+
disposed = true;
|
|
399
|
+
try {
|
|
400
|
+
audio.pause();
|
|
401
|
+
audio.currentTime = 0;
|
|
402
|
+
} catch {}
|
|
403
|
+
finish();
|
|
404
|
+
for (const complete of pendingMetadata) complete(aborted());
|
|
405
|
+
audio.removeEventListener("ended", onEnded);
|
|
406
|
+
audio.removeEventListener("error", onError);
|
|
407
|
+
for (const event of [
|
|
408
|
+
"durationchange",
|
|
409
|
+
"ratechange",
|
|
410
|
+
"seeked"
|
|
411
|
+
]) audio.removeEventListener(event, refreshFade);
|
|
412
|
+
try {
|
|
413
|
+
audio.removeAttribute?.("src");
|
|
414
|
+
audio.load?.();
|
|
415
|
+
} finally {
|
|
416
|
+
try {
|
|
417
|
+
amplifier?.dispose();
|
|
418
|
+
} finally {
|
|
419
|
+
release();
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
});
|
|
424
|
+
}
|
|
10
425
|
var block_definitions_default = {
|
|
11
426
|
extensionName: "Asset Cache",
|
|
12
427
|
blocks: [
|
|
@@ -1875,6 +2290,7 @@
|
|
|
1875
2290
|
_defineProperty(this, "textAssets", /* @__PURE__ */ new Map());
|
|
1876
2291
|
_defineProperty(this, "assetRegistry", /* @__PURE__ */ new Map());
|
|
1877
2292
|
_defineProperty(this, "displayedAssets", /* @__PURE__ */ new Map());
|
|
2293
|
+
_defineProperty(this, "explicitAudio", /* @__PURE__ */ new WeakSet());
|
|
1878
2294
|
_defineProperty(this, "playingAudio", /* @__PURE__ */ new Map());
|
|
1879
2295
|
_defineProperty(this, "audioVoiceStops", /* @__PURE__ */ new WeakMap());
|
|
1880
2296
|
_defineProperty(this, "registrationVersions", /* @__PURE__ */ new Map());
|
|
@@ -2246,6 +2662,50 @@
|
|
|
2246
2662
|
await playback.started;
|
|
2247
2663
|
return playback.voice;
|
|
2248
2664
|
}
|
|
2665
|
+
async createAudioPlayback(source, options = {}, onDispose = () => {}) {
|
|
2666
|
+
validateAudioPlaybackOptions(options);
|
|
2667
|
+
if (!source || typeof source !== "object" || Array.isArray(source) || Object.keys(source).length !== 1 || Object.hasOwn(source, "asset") === Object.hasOwn(source, "url")) throw new TypeError("Audio requires exactly one asset or URL.");
|
|
2668
|
+
if (Object.hasOwn(source, "url")) {
|
|
2669
|
+
if (typeof source.url !== "string" || !/^https?:\/\/[^\s]+$/u.test(source.url)) throw new TypeError("Audio URL must use HTTP(S) without credentials.");
|
|
2670
|
+
const url = new URL(source.url);
|
|
2671
|
+
if (!url.hostname || url.username || url.password) throw new TypeError("Invalid audio URL.");
|
|
2672
|
+
return this.prepareBrowserAudio("", source.url, options, onDispose, "createAudioPlayback");
|
|
2673
|
+
}
|
|
2674
|
+
const name = normalizeName(source.asset);
|
|
2675
|
+
const version = this.registrationVersions.get(name);
|
|
2676
|
+
const resource = await this.resolveAudioBytes(name);
|
|
2677
|
+
if (!this.assetRegistry.has(name) || version !== this.registrationVersions.get(name)) throw new Error("Audio source was released or replaced while preparing.");
|
|
2678
|
+
const bytes = resource.bytes instanceof Uint8Array ? new Uint8Array(resource.bytes).buffer : resource.bytes;
|
|
2679
|
+
const url = URL.createObjectURL(new Blob([bytes], { type: resource.mimeType }));
|
|
2680
|
+
return this.prepareBrowserAudio(name, url, options, () => {
|
|
2681
|
+
URL.revokeObjectURL(url);
|
|
2682
|
+
onDispose();
|
|
2683
|
+
}, "createAudioPlayback");
|
|
2684
|
+
}
|
|
2685
|
+
prepareBrowserAudio(name, url, options, release, operation) {
|
|
2686
|
+
let audio;
|
|
2687
|
+
let controller;
|
|
2688
|
+
try {
|
|
2689
|
+
const directAudio = operation === "createAudioPlayback" && /^https?:/u.test(url);
|
|
2690
|
+
audio = directAudio ? new Audio() : new Audio(url);
|
|
2691
|
+
if (directAudio) {
|
|
2692
|
+
audio.crossOrigin = "anonymous";
|
|
2693
|
+
audio.src = url;
|
|
2694
|
+
}
|
|
2695
|
+
controller = createBrowserAudioPlayback(audio, options, () => {
|
|
2696
|
+
this.playingAudio.delete(audio);
|
|
2697
|
+
this.audioVoiceStops.delete(audio);
|
|
2698
|
+
release();
|
|
2699
|
+
}, (cause) => this.playbackError(name, cause, operation));
|
|
2700
|
+
} catch (cause) {
|
|
2701
|
+
release();
|
|
2702
|
+
throw this.playbackError(name, cause, operation);
|
|
2703
|
+
}
|
|
2704
|
+
if (operation === "createAudioPlayback") this.explicitAudio.add(audio);
|
|
2705
|
+
this.playingAudio.set(audio, name);
|
|
2706
|
+
this.audioVoiceStops.set(audio, () => controller.dispose());
|
|
2707
|
+
return controller;
|
|
2708
|
+
}
|
|
2249
2709
|
stopSound(args) {
|
|
2250
2710
|
const name = normalizeName(args.NAME);
|
|
2251
2711
|
const kind = this.assetRegistry.get(name);
|
|
@@ -2255,12 +2715,12 @@
|
|
|
2255
2715
|
if (!asset) throw this.assetNotRegistered("stopSound", name);
|
|
2256
2716
|
asset.mimeType = normalizeMimeType(asset.mimeType, asset.url || name);
|
|
2257
2717
|
if (!asset.mimeType.startsWith("audio/")) throw this.assetTypeMismatch("stopSound", name, "audio", `external/${this.externalMediaKind(asset)}`);
|
|
2258
|
-
for (const [audio, assetName] of [...this.playingAudio]) if (assetName === name) this.stopExternalAudio(audio);
|
|
2718
|
+
for (const [audio, assetName] of [...this.playingAudio]) if (assetName === name && !this.explicitAudio.has(audio)) this.stopExternalAudio(audio);
|
|
2259
2719
|
return;
|
|
2260
2720
|
}
|
|
2261
2721
|
if (kind === "sound") {
|
|
2262
2722
|
const { target, sound, soundBank } = this.resolveSoundReference(name);
|
|
2263
|
-
for (const [audio, assetName] of [...this.playingAudio]) if (assetName === name) this.stopExternalAudio(audio);
|
|
2723
|
+
for (const [audio, assetName] of [...this.playingAudio]) if (assetName === name && !this.explicitAudio.has(audio)) this.stopExternalAudio(audio);
|
|
2264
2724
|
soundBank.stop(target, sound.soundId);
|
|
2265
2725
|
return;
|
|
2266
2726
|
}
|
|
@@ -2508,6 +2968,7 @@
|
|
|
2508
2968
|
}
|
|
2509
2969
|
unregisterAsset(name) {
|
|
2510
2970
|
this.cancelRegistrations(name);
|
|
2971
|
+
for (const [audio, assetName] of [...this.playingAudio]) if (assetName === name) this.stopExternalAudio(audio);
|
|
2511
2972
|
this.invalidateDOMImageResources(name);
|
|
2512
2973
|
const kind = this.assetRegistry.get(name);
|
|
2513
2974
|
if (!kind) return;
|
|
@@ -3323,73 +3784,33 @@
|
|
|
3323
3784
|
}
|
|
3324
3785
|
startBrowserAudioVoice(name, bytes, mimeType, initialGain, operation) {
|
|
3325
3786
|
const blobBytes = bytes instanceof Uint8Array ? new Uint8Array(bytes).buffer : bytes;
|
|
3326
|
-
const
|
|
3327
|
-
|
|
3328
|
-
try {
|
|
3329
|
-
audio = new Audio(objectUrl);
|
|
3330
|
-
audio.volume = initialGain;
|
|
3331
|
-
} catch (error) {
|
|
3332
|
-
URL.revokeObjectURL(objectUrl);
|
|
3333
|
-
throw this.playbackError(name, error, operation);
|
|
3334
|
-
}
|
|
3787
|
+
const url = URL.createObjectURL(new Blob([blobBytes], { type: mimeType }));
|
|
3788
|
+
const controller = this.prepareBrowserAudio(name, url, { gain: initialGain }, () => URL.revokeObjectURL(url), operation);
|
|
3335
3789
|
let active = true;
|
|
3336
|
-
let resolveEnded;
|
|
3337
|
-
let rejectEnded;
|
|
3338
|
-
const ended = new Promise((resolve, reject) => {
|
|
3339
|
-
resolveEnded = resolve;
|
|
3340
|
-
rejectEnded = reject;
|
|
3341
|
-
});
|
|
3342
|
-
ended.catch(() => {});
|
|
3343
|
-
const cleanup = (error) => {
|
|
3344
|
-
if (!active) return;
|
|
3345
|
-
active = false;
|
|
3346
|
-
audio.removeEventListener("ended", handleEnded);
|
|
3347
|
-
audio.removeEventListener("error", handleError);
|
|
3348
|
-
this.playingAudio.delete(audio);
|
|
3349
|
-
this.audioVoiceStops.delete(audio);
|
|
3350
|
-
URL.revokeObjectURL(objectUrl);
|
|
3351
|
-
if (error === void 0) resolveEnded();
|
|
3352
|
-
else rejectEnded(error);
|
|
3353
|
-
};
|
|
3354
|
-
const handleEnded = () => cleanup();
|
|
3355
|
-
const handleError = () => cleanup(operation === "createAudioVoice" ? this.playbackError(name, /* @__PURE__ */ new Error("The browser audio element reported a playback error."), operation) : void 0);
|
|
3356
3790
|
const stop = () => {
|
|
3357
3791
|
if (!active) return;
|
|
3358
|
-
|
|
3359
|
-
|
|
3360
|
-
audio.currentTime = 0;
|
|
3361
|
-
} catch {}
|
|
3362
|
-
cleanup();
|
|
3792
|
+
active = false;
|
|
3793
|
+
controller.dispose();
|
|
3363
3794
|
};
|
|
3364
|
-
const
|
|
3365
|
-
|
|
3366
|
-
|
|
3367
|
-
|
|
3368
|
-
if (active) audio.volume = gain;
|
|
3369
|
-
},
|
|
3370
|
-
stop
|
|
3795
|
+
const started = controller.play();
|
|
3796
|
+
const ended = controller.ended.then(() => stop(), (error) => {
|
|
3797
|
+
stop();
|
|
3798
|
+
if (operation === "createAudioVoice") throw error;
|
|
3371
3799
|
});
|
|
3372
|
-
|
|
3373
|
-
audio.addEventListener("error", handleError, { once: true });
|
|
3374
|
-
this.playingAudio.set(audio, name);
|
|
3375
|
-
this.audioVoiceStops.set(audio, stop);
|
|
3376
|
-
let playResult;
|
|
3377
|
-
try {
|
|
3378
|
-
playResult = audio.play();
|
|
3379
|
-
} catch (error) {
|
|
3380
|
-
const playbackError = this.playbackError(name, error, operation);
|
|
3381
|
-
cleanup(playbackError);
|
|
3382
|
-
return {
|
|
3383
|
-
voice,
|
|
3384
|
-
started: Promise.reject(playbackError)
|
|
3385
|
-
};
|
|
3386
|
-
}
|
|
3800
|
+
ended.catch(() => {});
|
|
3387
3801
|
return {
|
|
3388
|
-
voice
|
|
3389
|
-
|
|
3390
|
-
|
|
3391
|
-
|
|
3392
|
-
|
|
3802
|
+
voice: Object.freeze({
|
|
3803
|
+
ended,
|
|
3804
|
+
setGain(value) {
|
|
3805
|
+
const gain = normalizeAudioVoiceGain(value);
|
|
3806
|
+
if (active) controller.setGain(gain);
|
|
3807
|
+
},
|
|
3808
|
+
stop
|
|
3809
|
+
}),
|
|
3810
|
+
started: started.catch((error) => {
|
|
3811
|
+
const legacyStop = operation === "playSound" && !active && error instanceof Error && error.name === "AbortError";
|
|
3812
|
+
stop();
|
|
3813
|
+
if (!legacyStop) throw error;
|
|
3393
3814
|
})
|
|
3394
3815
|
};
|
|
3395
3816
|
}
|