@avoid.quest/audio 0.1.1-beta.0 → 0.1.1-beta.2
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 +4 -2
- package/dist/effects/index.d.ts +3 -16
- package/dist/effects/index.js +10 -22
- package/dist/effects/index.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/midi/index.d.ts +1 -1
- package/dist/midi/index.js +5 -7
- package/dist/midi/index.js.map +1 -1
- package/dist/native-Bern4_PB.js.map +1 -1
- package/dist/{pcm-buffer-DSzGHbon.js → pcm-buffer-BsaH-L59.js} +15 -26
- package/dist/pcm-buffer-BsaH-L59.js.map +1 -0
- package/dist/playback/index.d.ts +74 -43
- package/dist/playback/index.js +122 -118
- package/dist/playback/index.js.map +1 -1
- package/dist/queue/index.d.ts +0 -6
- package/dist/queue/index.js +3 -9
- package/dist/queue/index.js.map +1 -1
- package/dist/recognize/index.d.ts +3 -19
- package/dist/recognize/index.js +1 -1
- package/dist/recognize/index.js.map +1 -1
- package/dist/routing/index.d.ts +5 -4
- package/dist/routing/index.js +56 -58
- package/dist/routing/index.js.map +1 -1
- package/dist/{shazam-gBrX1NEY.d.ts → shazam-cT7xEIzJ.d.ts} +1 -1
- package/dist/sync/index.d.ts +8 -7
- package/dist/sync/index.js +39 -32
- package/dist/sync/index.js.map +1 -1
- package/dist/{worklet-loader-f741D1tk.js → worklet-loader-B_rkf9ao.js} +50 -61
- package/dist/worklet-loader-B_rkf9ao.js.map +1 -0
- package/dist/{worklet-loader-BabwPALw.d.ts → worklet-loader-BvDJuua5.d.ts} +6 -20
- package/dist/worklets/couch-live.js +18 -8
- package/dist/worklets/couch-live.js.map +1 -1
- package/dist/worklets/meter.js.map +1 -1
- package/dist/worklets/pcm-capture.d.ts +0 -2
- package/dist/worklets/pcm-capture.js +0 -2
- package/dist/worklets/pcm-capture.js.map +1 -1
- package/package.json +18 -10
- package/types/hls-unavailable.d.ts +14 -0
- package/dist/pcm-buffer-DSzGHbon.js.map +0 -1
- package/dist/worklet-loader-f741D1tk.js.map +0 -1
package/dist/playback/index.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
import { Context, Duration, Effect,
|
|
1
|
+
import { Context, Duration, Effect, FiberSet, Layer, Option, Result, Schedule, Scope } from "effect";
|
|
2
2
|
import { a as AudioPlaybackError, l as AudioUnsupportedError, n as AudioDecodeError, t as AudioCaptureError } from "../errors-BT9HxDLn.js";
|
|
3
3
|
import { a as clampFinite, p as scheduleParam } from "../native-Bern4_PB.js";
|
|
4
|
-
import {
|
|
5
|
-
import { i as
|
|
4
|
+
import { constVoid } from "effect/Function";
|
|
5
|
+
import { i as prepareShazamPcm, n as createPcmRingBuffer, r as generateShazamSignatureFromFloat32 } from "../pcm-buffer-BsaH-L59.js";
|
|
6
|
+
import { i as loadAudioWorklet, r as createWorkletModuleUrl } from "../worklet-loader-B_rkf9ao.js";
|
|
6
7
|
// src/playback/audio-context.ts
|
|
7
8
|
var AudioContextService = class extends Context.Service()("@avoid.quest/audio/AudioContextService") {};
|
|
8
9
|
function createAudioContextOptions(options = {}) {
|
|
@@ -18,11 +19,10 @@ function normalizeAudioContextState(state) {
|
|
|
18
19
|
}
|
|
19
20
|
const normalizedState = (ctx) => normalizeAudioContextState(ctx.state);
|
|
20
21
|
function snapshotAudioContext(context) {
|
|
21
|
-
const ext = context;
|
|
22
22
|
return {
|
|
23
23
|
baseLatency: typeof context.baseLatency === "number" ? context.baseLatency : void 0,
|
|
24
24
|
currentTime: context.currentTime,
|
|
25
|
-
outputLatency: typeof
|
|
25
|
+
outputLatency: typeof context.outputLatency === "number" ? context.outputLatency : void 0,
|
|
26
26
|
sampleRate: context.sampleRate,
|
|
27
27
|
state: normalizedState(context)
|
|
28
28
|
};
|
|
@@ -63,7 +63,7 @@ const GESTURE_EVENTS = [
|
|
|
63
63
|
"click",
|
|
64
64
|
"keydown"
|
|
65
65
|
];
|
|
66
|
-
const noop = () =>
|
|
66
|
+
const noop = () => void 0;
|
|
67
67
|
function attachGestureUnlock(context, target = globalThis.window) {
|
|
68
68
|
const isTerminal = () => normalizedState(context) !== "suspended";
|
|
69
69
|
if (!target || isTerminal()) return noop;
|
|
@@ -102,13 +102,18 @@ function closeContextSafely(context) {
|
|
|
102
102
|
}
|
|
103
103
|
}).pipe(Effect.catch(() => Effect.void));
|
|
104
104
|
}
|
|
105
|
+
const isAudioContextConstructor = (value) => typeof value === "function";
|
|
106
|
+
const legacyAudioContext = () => {
|
|
107
|
+
const scope = globalThis;
|
|
108
|
+
const legacy = "webkitAudioContext" in scope ? scope.webkitAudioContext : void 0;
|
|
109
|
+
return isAudioContextConstructor(legacy) ? legacy : void 0;
|
|
110
|
+
};
|
|
105
111
|
function resolveContext(options) {
|
|
106
112
|
if (options.context) return Effect.succeed(options.context);
|
|
107
113
|
const ctxOpts = createAudioContextOptions(options);
|
|
108
114
|
const { createContext } = options;
|
|
109
115
|
if (createContext) return Effect.sync(() => createContext(ctxOpts));
|
|
110
|
-
const
|
|
111
|
-
const Ctor = g.AudioContext ?? g.webkitAudioContext;
|
|
116
|
+
const Ctor = globalThis.AudioContext ?? legacyAudioContext();
|
|
112
117
|
return typeof Ctor === "function" ? Effect.sync(() => new Ctor(ctxOpts)) : Effect.fail(new AudioUnsupportedError({
|
|
113
118
|
feature: "AudioContext",
|
|
114
119
|
message: "AudioContext is not supported in this environment."
|
|
@@ -209,25 +214,23 @@ const DISPLAY_CONSTRAINTS = {
|
|
|
209
214
|
},
|
|
210
215
|
windowAudio: "exclude"
|
|
211
216
|
};
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
217
|
+
const requestDisplayAudio = Effect.fn("InputCapture.requestDisplayAudio")(function* (options = {}) {
|
|
218
|
+
const devices = options.mediaDevices ?? globalThis.navigator?.mediaDevices;
|
|
219
|
+
if (typeof devices?.getDisplayMedia !== "function") return yield* Effect.fail(capErr("UNSUPPORTED", "getDisplayMedia unavailable."));
|
|
220
|
+
const stream = yield* acquireStreamWithAbort(() => devices.getDisplayMedia(DISPLAY_CONSTRAINTS), options.signal, "Display audio capture cancelled.", "DISPLAY_AUDIO_FAILED", "Display audio capture failed");
|
|
221
|
+
if (!stream.getAudioTracks().some((t) => t.readyState === "live")) {
|
|
222
|
+
stopTracks(stream);
|
|
223
|
+
return yield* Effect.fail(capErr("NO_AUDIO_TRACK", "No live audio track."));
|
|
224
|
+
}
|
|
225
|
+
if (options.requireBrowserTab ?? true) {
|
|
226
|
+
const surface = stream.getVideoTracks()[0]?.getSettings().displaySurface;
|
|
227
|
+
if (surface !== void 0 && surface !== "browser") {
|
|
218
228
|
stopTracks(stream);
|
|
219
|
-
return yield* Effect.fail(capErr("
|
|
229
|
+
return yield* Effect.fail(capErr("INVALID_SURFACE", `Expected browser tab, got "${surface}".`));
|
|
220
230
|
}
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
stopTracks(stream);
|
|
225
|
-
return yield* Effect.fail(capErr("INVALID_SURFACE", `Expected browser tab, got "${surface}".`));
|
|
226
|
-
}
|
|
227
|
-
}
|
|
228
|
-
return stream;
|
|
229
|
-
});
|
|
230
|
-
}
|
|
231
|
+
}
|
|
232
|
+
return stream;
|
|
233
|
+
});
|
|
231
234
|
function resolveInputStream(options) {
|
|
232
235
|
if (options.signal?.aborted) return Effect.fail(capErr("ABORTED", "Input capture cancelled.", true));
|
|
233
236
|
if (options.stream) return Effect.succeed(options.stream);
|
|
@@ -315,9 +318,8 @@ const DEFAULT_STALL_WATCHDOG_MS = 6e3;
|
|
|
315
318
|
const DEFAULT_MAX_RECOVERY_DELAY_MS = 3e4;
|
|
316
319
|
const clampPlaybackRate = (rate) => clampFinite(rate, MIN_PLAYBACK_RATE, 2, 1);
|
|
317
320
|
function applyPreservesPitch(element, preserve) {
|
|
318
|
-
|
|
319
|
-
if ("
|
|
320
|
-
if ("webkitPreservesPitch" in ext) ext.webkitPreservesPitch = preserve;
|
|
321
|
+
if ("preservesPitch" in element) element.preservesPitch = preserve;
|
|
322
|
+
if ("webkitPreservesPitch" in element) element.webkitPreservesPitch = preserve;
|
|
321
323
|
return preserve;
|
|
322
324
|
}
|
|
323
325
|
function isHlsUrl(url, depth = 0) {
|
|
@@ -349,6 +351,8 @@ const pbErr = (code, message, recoverable, cause) => new AudioPlaybackError({
|
|
|
349
351
|
recoverable
|
|
350
352
|
});
|
|
351
353
|
const failSuperseded = () => Effect.fail(pbErr("MEDIA_LOAD_SUPERSEDED", "Media load superseded.", true));
|
|
354
|
+
const HTML_MEDIA_ELEMENT_TAGS = /* @__PURE__ */ new Set(["[object HTMLAudioElement]", "[object HTMLVideoElement]"]);
|
|
355
|
+
const isHtmlMediaElement = (element) => HTML_MEDIA_ELEMENT_TAGS.has(Object.prototype.toString.call(element));
|
|
352
356
|
const hasEventTarget = (el) => typeof el.addEventListener === "function" && typeof el.removeEventListener === "function";
|
|
353
357
|
function bindEvents(target, entries, opts) {
|
|
354
358
|
for (const [type, fn] of entries) target.addEventListener(type, fn, opts);
|
|
@@ -356,7 +360,7 @@ function bindEvents(target, entries, opts) {
|
|
|
356
360
|
for (const [type, fn] of entries) target.removeEventListener(type, fn);
|
|
357
361
|
};
|
|
358
362
|
}
|
|
359
|
-
function createRecoveryRunner(
|
|
363
|
+
function createRecoveryRunner(fork, schedule) {
|
|
360
364
|
let step;
|
|
361
365
|
let fiber;
|
|
362
366
|
return {
|
|
@@ -367,7 +371,7 @@ function createRecoveryRunner(scope, schedule) {
|
|
|
367
371
|
},
|
|
368
372
|
trigger: (isStale, onRetry, onExhausted) => {
|
|
369
373
|
if (isStale() || fiber !== void 0) return;
|
|
370
|
-
fiber =
|
|
374
|
+
fiber = fork(Effect.gen(function* () {
|
|
371
375
|
step ??= yield* Schedule.toStepWithSleep(schedule);
|
|
372
376
|
const attempt = yield* step(void 0).pipe(Effect.option);
|
|
373
377
|
if (isStale()) return;
|
|
@@ -377,7 +381,7 @@ function createRecoveryRunner(scope, schedule) {
|
|
|
377
381
|
} else yield* onRetry();
|
|
378
382
|
}).pipe(Effect.ensuring(Effect.sync(() => {
|
|
379
383
|
fiber = void 0;
|
|
380
|
-
}))))
|
|
384
|
+
}))));
|
|
381
385
|
}
|
|
382
386
|
};
|
|
383
387
|
}
|
|
@@ -418,14 +422,14 @@ function attachHlsInstance(HlsCtor, element, url, baseConfig, loadOptions, hlsRe
|
|
|
418
422
|
hls.loadSource(url);
|
|
419
423
|
return hls;
|
|
420
424
|
}
|
|
421
|
-
function waitForMediaReady(element, timeoutMs, isStale, hasMetadata, registerCancel,
|
|
425
|
+
function waitForMediaReady(element, timeoutMs, isStale, hasMetadata, registerCancel, onManifestParsed) {
|
|
422
426
|
if (isStale()) return failSuperseded();
|
|
423
427
|
if (hasMetadata() || !hasEventTarget(element)) return Effect.void;
|
|
424
428
|
return Effect.callback((resume) => {
|
|
425
429
|
let settled = false;
|
|
426
430
|
const cleanup = () => {
|
|
427
431
|
unbind();
|
|
428
|
-
|
|
432
|
+
offManifestParsed?.();
|
|
429
433
|
unregisterCancel();
|
|
430
434
|
};
|
|
431
435
|
const settle = (e) => {
|
|
@@ -443,7 +447,7 @@ function waitForMediaReady(element, timeoutMs, isStale, hasMetadata, registerCan
|
|
|
443
447
|
["playing", onReady],
|
|
444
448
|
["error", onError]
|
|
445
449
|
], { once: true });
|
|
446
|
-
|
|
450
|
+
const offManifestParsed = onManifestParsed?.(onReady);
|
|
447
451
|
return Effect.sync(() => {
|
|
448
452
|
settled = true;
|
|
449
453
|
cleanup();
|
|
@@ -453,11 +457,11 @@ function waitForMediaReady(element, timeoutMs, isStale, hasMetadata, registerCan
|
|
|
453
457
|
orElse: () => Effect.fail(pbErr("MEDIA_READY_TIMEOUT", `Media not ready within ${timeoutMs} ms.`, true))
|
|
454
458
|
}));
|
|
455
459
|
}
|
|
456
|
-
function forkReadyWatchdog(
|
|
457
|
-
return
|
|
460
|
+
function forkReadyWatchdog(fork, ready, onFailure) {
|
|
461
|
+
return fork(ready.pipe(Effect.match({
|
|
458
462
|
onFailure,
|
|
459
|
-
onSuccess:
|
|
460
|
-
})))
|
|
463
|
+
onSuccess: constVoid
|
|
464
|
+
})));
|
|
461
465
|
}
|
|
462
466
|
const applySeekSafe = (element, sec) => {
|
|
463
467
|
if (!Number.isFinite(sec) || sec < 0) return;
|
|
@@ -537,13 +541,14 @@ function createStallWatchdog(element, stallWatchdogMs, stallRecovery, getState,
|
|
|
537
541
|
};
|
|
538
542
|
}
|
|
539
543
|
// src/playback/media-engine.ts
|
|
540
|
-
|
|
544
|
+
const loadBundledHls = () => import("hls.js");
|
|
545
|
+
function buildMediaEngine(fork, options) {
|
|
541
546
|
const { element } = options;
|
|
542
547
|
const readyTimeoutDefault = options.mediaReadyTimeoutMs ?? 2e4;
|
|
543
548
|
const stallWatchdogMs = options.stallWatchdogMs ?? 6e3;
|
|
544
549
|
const recoverySchedule = createStallRecoverySchedule(options.stallRecovery);
|
|
545
|
-
const loadHlsModule = options.loadHls ?? (() => import("hls.js"));
|
|
546
550
|
let hlsInstance;
|
|
551
|
+
let currentProtocol;
|
|
547
552
|
let currentUrl;
|
|
548
553
|
let mode = "idle";
|
|
549
554
|
let playbackRate = clampPlaybackRate(element.playbackRate || 1);
|
|
@@ -553,8 +558,8 @@ function buildMediaEngine(scope, options) {
|
|
|
553
558
|
let pendingSeekCleanup;
|
|
554
559
|
let readyWatchdogFiber;
|
|
555
560
|
let cancelPendingReadyWait;
|
|
556
|
-
const hlsRecovery = createRecoveryRunner(
|
|
557
|
-
const stallRecovery = createRecoveryRunner(
|
|
561
|
+
const hlsRecovery = createRecoveryRunner(fork, recoverySchedule);
|
|
562
|
+
const stallRecovery = createRecoveryRunner(fork, recoverySchedule);
|
|
558
563
|
const hasMetadata = () => typeof element.readyState === "number" && element.readyState >= 1;
|
|
559
564
|
const applyRate = () => {
|
|
560
565
|
element.defaultPlaybackRate = playbackRate;
|
|
@@ -589,7 +594,11 @@ function buildMediaEngine(scope, options) {
|
|
|
589
594
|
await element.play();
|
|
590
595
|
}
|
|
591
596
|
});
|
|
592
|
-
const
|
|
597
|
+
const stallWatchdog = createStallWatchdog(element, stallWatchdogMs, stallRecovery, () => ({
|
|
598
|
+
disposed,
|
|
599
|
+
gen: loadGeneration,
|
|
600
|
+
hasUrl: currentUrl !== void 0
|
|
601
|
+
}), Effect.fn("MediaEngine.recoverStall")(function* (gen) {
|
|
593
602
|
if (mode === "hls.js" && hlsInstance) {
|
|
594
603
|
hlsInstance.startLoad();
|
|
595
604
|
stallWatchdog.arm();
|
|
@@ -603,12 +612,7 @@ function buildMediaEngine(scope, options) {
|
|
|
603
612
|
yield* play().pipe(Effect.timeoutOption(Duration.millis(Math.max(1, stallWatchdogMs))), Effect.catch(() => Effect.void));
|
|
604
613
|
stallWatchdog.arm();
|
|
605
614
|
}
|
|
606
|
-
});
|
|
607
|
-
const stallWatchdog = createStallWatchdog(element, stallWatchdogMs, stallRecovery, () => ({
|
|
608
|
-
disposed,
|
|
609
|
-
gen: loadGeneration,
|
|
610
|
-
hasUrl: currentUrl !== void 0
|
|
611
|
-
}), recoverStall, options.onStall, options.onStreamError);
|
|
615
|
+
}), options.onStall, options.onStreamError);
|
|
612
616
|
const resetPlayback = () => {
|
|
613
617
|
cancelPendingReadyWait?.();
|
|
614
618
|
cancelPendingReadyWait = void 0;
|
|
@@ -620,32 +624,45 @@ function buildMediaEngine(scope, options) {
|
|
|
620
624
|
hlsInstance?.destroy();
|
|
621
625
|
hlsInstance = void 0;
|
|
622
626
|
};
|
|
623
|
-
const
|
|
627
|
+
const startHls = Effect.fn("MediaEngine.startHls")(function* (loadModule, media, url, loadOptions, gen) {
|
|
628
|
+
const { default: HlsCtor } = yield* Effect.tryPromise({
|
|
629
|
+
catch: (e) => pbErr("HLS_IMPORT_FAILED", `Load hls.js: ${String(e)}`, false, `${e}`),
|
|
630
|
+
try: () => loadModule()
|
|
631
|
+
});
|
|
632
|
+
if (disposed || gen !== loadGeneration) return yield* failSuperseded();
|
|
633
|
+
if (typeof HlsCtor?.isSupported !== "function" || !HlsCtor.isSupported()) return;
|
|
634
|
+
const player = attachHlsInstance(HlsCtor, media, url, options.hlsConfig, loadOptions, hlsRecovery, (hls) => !disposed && hlsInstance === hls, clearReady, options.onStreamError);
|
|
635
|
+
const parsed = HlsCtor.Events.MANIFEST_PARSED;
|
|
636
|
+
return {
|
|
637
|
+
onManifestParsed: (listener) => {
|
|
638
|
+
player.on(parsed, listener);
|
|
639
|
+
return () => player.off(parsed, listener);
|
|
640
|
+
},
|
|
641
|
+
player
|
|
642
|
+
};
|
|
643
|
+
});
|
|
644
|
+
const load = Effect.fn("MediaEngine.load")(function* (url, loadOptions = {}) {
|
|
645
|
+
if (disposed) return yield* pbErr("MEDIA_ENGINE_DISPOSED", "Media engine has been disposed.", false);
|
|
624
646
|
loadGeneration += 1;
|
|
625
647
|
const gen = loadGeneration;
|
|
626
648
|
resetPlayback();
|
|
627
649
|
currentUrl = url;
|
|
650
|
+
currentProtocol = loadOptions.protocol;
|
|
628
651
|
stallWatchdog.resetProgress();
|
|
629
652
|
const readyMs = loadOptions.readyTimeoutMs ?? readyTimeoutDefault;
|
|
630
653
|
if (loadOptions.crossOrigin !== void 0) element.crossOrigin = loadOptions.crossOrigin;
|
|
631
654
|
const allowNative = loadOptions.allowNativeHls ?? options.allowNativeHls ?? true;
|
|
632
|
-
const hlsStream = isHlsUrl(url);
|
|
633
|
-
let
|
|
634
|
-
let manifestEvent;
|
|
655
|
+
const hlsStream = loadOptions.protocol === void 0 ? isHlsUrl(url) : loadOptions.protocol === "hls";
|
|
656
|
+
let started;
|
|
635
657
|
if (hlsStream && !shouldUseNativeHls(element, allowNative, options.userAgent)) {
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
try: () => loadHlsModule()
|
|
639
|
-
});
|
|
640
|
-
if (typeof HlsCtor?.isSupported === "function" && HlsCtor.isSupported()) {
|
|
641
|
-
activeHls = attachHlsInstance(HlsCtor, element, url, options.hlsConfig, loadOptions, hlsRecovery, (hls) => !disposed && hlsInstance === hls, clearReady, options.onStreamError);
|
|
642
|
-
hlsInstance = activeHls;
|
|
643
|
-
mode = "hls.js";
|
|
644
|
-
syncRateAndPitch();
|
|
645
|
-
manifestEvent = HlsCtor.Events.MANIFEST_PARSED;
|
|
646
|
-
}
|
|
658
|
+
if (options.loadHls) started = yield* startHls(options.loadHls, element, url, loadOptions, gen);
|
|
659
|
+
else if (isHtmlMediaElement(element)) started = yield* startHls(loadBundledHls, element, url, loadOptions, gen);
|
|
647
660
|
}
|
|
648
|
-
if (
|
|
661
|
+
if (started) {
|
|
662
|
+
hlsInstance = started.player;
|
|
663
|
+
mode = "hls.js";
|
|
664
|
+
syncRateAndPitch();
|
|
665
|
+
} else {
|
|
649
666
|
mode = hlsStream ? "native-hls" : "native";
|
|
650
667
|
loadNative(url, loadOptions.startPositionSeconds);
|
|
651
668
|
}
|
|
@@ -654,9 +671,9 @@ function buildMediaEngine(scope, options) {
|
|
|
654
671
|
return () => {
|
|
655
672
|
if (cancelPendingReadyWait === cancel) cancelPendingReadyWait = void 0;
|
|
656
673
|
};
|
|
657
|
-
},
|
|
674
|
+
}, started?.onManifestParsed);
|
|
658
675
|
if (loadOptions.waitForReady) yield* ready;
|
|
659
|
-
else if (readyMs > 0 && !disposed) readyWatchdogFiber = forkReadyWatchdog(
|
|
676
|
+
else if (readyMs > 0 && !disposed) readyWatchdogFiber = forkReadyWatchdog(fork, ready, (failure) => {
|
|
660
677
|
if (!disposed && gen === loadGeneration && failure.code !== "MEDIA_LOAD_SUPERSEDED" && !hasMetadata()) options.onStreamError?.(element.currentTime, failure);
|
|
661
678
|
});
|
|
662
679
|
});
|
|
@@ -670,6 +687,7 @@ function buildMediaEngine(scope, options) {
|
|
|
670
687
|
element.removeAttribute("src");
|
|
671
688
|
element.load?.();
|
|
672
689
|
mode = "idle";
|
|
690
|
+
currentProtocol = void 0;
|
|
673
691
|
currentUrl = void 0;
|
|
674
692
|
});
|
|
675
693
|
return {
|
|
@@ -692,10 +710,13 @@ function buildMediaEngine(scope, options) {
|
|
|
692
710
|
element.pause();
|
|
693
711
|
}),
|
|
694
712
|
play,
|
|
695
|
-
refreshUrl:
|
|
713
|
+
refreshUrl: Effect.fn("MediaEngine.refreshUrl")(function* (url, seekPos) {
|
|
696
714
|
const wasPlaying = !element.paused;
|
|
697
715
|
const at = seekPos ?? element.currentTime;
|
|
698
|
-
yield* load(url, {
|
|
716
|
+
yield* load(url, {
|
|
717
|
+
protocol: currentProtocol,
|
|
718
|
+
startPositionSeconds: Number.isFinite(at) && at > 0 ? at : void 0
|
|
719
|
+
});
|
|
699
720
|
if (wasPlaying) yield* play();
|
|
700
721
|
}),
|
|
701
722
|
runWithStallRecovery: (effect) => effect.pipe(Effect.retry({
|
|
@@ -716,17 +737,15 @@ function buildMediaEngine(scope, options) {
|
|
|
716
737
|
setVolume: (vol) => Effect.sync(() => element.volume = clampFinite(vol, 0, 1, 1))
|
|
717
738
|
};
|
|
718
739
|
}
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
return engine;
|
|
725
|
-
});
|
|
726
|
-
}
|
|
740
|
+
const createMediaEngine = Effect.fn("MediaEngine.create")(function* (options) {
|
|
741
|
+
const engine = buildMediaEngine(yield* FiberSet.makeRuntime(), options);
|
|
742
|
+
yield* Effect.addFinalizer(() => engine.dispose());
|
|
743
|
+
return engine;
|
|
744
|
+
});
|
|
727
745
|
// src/playback/media-source.ts
|
|
728
746
|
const elementSources = /* @__PURE__ */ new WeakMap();
|
|
729
747
|
const destinationRouted = /* @__PURE__ */ new WeakSet();
|
|
748
|
+
const isElementHolder = (target) => "element" in target;
|
|
730
749
|
function sourceError(code, message, cause) {
|
|
731
750
|
return new AudioPlaybackError({
|
|
732
751
|
...cause === void 0 ? {} : { cause: String(cause) },
|
|
@@ -736,15 +755,13 @@ function sourceError(code, message, cause) {
|
|
|
736
755
|
});
|
|
737
756
|
}
|
|
738
757
|
/**
|
|
739
|
-
*
|
|
740
|
-
*
|
|
741
|
-
*
|
|
742
|
-
* `AudioContext` fails with `MEDIA_SOURCE_CONTEXT_MISMATCH`. Cross-origin
|
|
743
|
-
* media needs CORS + `crossOrigin: "anonymous"`, otherwise the graph is silent.
|
|
758
|
+
* Memoized because browsers allow one source node per element, so a second
|
|
759
|
+
* `AudioContext` fails with `MEDIA_SOURCE_CONTEXT_MISMATCH`. Cross-origin media
|
|
760
|
+
* needs CORS and `crossOrigin: "anonymous"`, otherwise the graph is silent.
|
|
744
761
|
*/
|
|
745
762
|
function getMediaElementSourceNode(context, target, options = {}) {
|
|
746
763
|
return Effect.suspend(() => {
|
|
747
|
-
const element =
|
|
764
|
+
const element = isElementHolder(target) ? target.element : target;
|
|
748
765
|
let node = elementSources.get(element);
|
|
749
766
|
if (node && node.context !== context) return Effect.fail(sourceError("MEDIA_SOURCE_CONTEXT_MISMATCH", "This media element is already bound to a different AudioContext."));
|
|
750
767
|
if (!node) {
|
|
@@ -771,11 +788,9 @@ function getMediaElementSourceNode(context, target, options = {}) {
|
|
|
771
788
|
// src/worklets/pcm-capture.ts
|
|
772
789
|
const PCM_CAPTURE_PROCESSOR_NAME = "avoid-pcm-capture";
|
|
773
790
|
const PCM_CAPTURE_DEFAULT_CHUNK_FRAMES = 2048;
|
|
774
|
-
/** Main-thread → processor message that ends the processor's lifetime. */
|
|
775
791
|
const PCM_CAPTURE_STOP_MESSAGE = "stop";
|
|
776
792
|
const PCM_CAPTURE_MIN_CHUNK_FRAMES = 128;
|
|
777
793
|
const PCM_CAPTURE_MAX_CHUNK_FRAMES = 16384;
|
|
778
|
-
/** Clamps a requested chunk size to whole frames within `[128, 16384]`. */
|
|
779
794
|
function clampPcmChunkFrames(chunkFrames) {
|
|
780
795
|
if (chunkFrames === void 0 || !Number.isFinite(chunkFrames)) return PCM_CAPTURE_DEFAULT_CHUNK_FRAMES;
|
|
781
796
|
return Math.max(PCM_CAPTURE_MIN_CHUNK_FRAMES, Math.min(PCM_CAPTURE_MAX_CHUNK_FRAMES, Math.round(chunkFrames)));
|
|
@@ -818,7 +833,6 @@ if (typeof registerProcessor === "function") registerProcessor(PCM_CAPTURE_PROCE
|
|
|
818
833
|
}
|
|
819
834
|
});
|
|
820
835
|
// src/playback/pcm-capture.ts
|
|
821
|
-
/** Loads the standalone `@avoid.quest/audio/worklets/pcm-capture` module. */
|
|
822
836
|
function loadPcmCaptureWorklet(context, options = {}) {
|
|
823
837
|
return loadAudioWorklet(context, {
|
|
824
838
|
...options,
|
|
@@ -888,29 +902,21 @@ function buildPcmCapture(context, source, options) {
|
|
|
888
902
|
toShazamPcm: (seconds) => prepareShazamPcm(snapshot(seconds), sampleRate)
|
|
889
903
|
}, teardown];
|
|
890
904
|
}
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
code: "PCM_CAPTURE_FAILED",
|
|
907
|
-
message: `PCM capture setup failed: ${String(error)}`
|
|
908
|
-
}),
|
|
909
|
-
try: () => buildPcmCapture(context, source, options)
|
|
910
|
-
}), ([, teardown]) => Effect.sync(teardown));
|
|
911
|
-
return capture;
|
|
912
|
-
});
|
|
913
|
-
}
|
|
905
|
+
const acquirePcmCapture = Effect.fn("PcmCapture.acquire")(function* (context, source, options = {}) {
|
|
906
|
+
if (typeof globalThis.AudioWorkletNode !== "function") return yield* Effect.fail(new AudioUnsupportedError({
|
|
907
|
+
feature: "AudioWorkletNode",
|
|
908
|
+
message: "PCM capture requires AudioWorkletNode support."
|
|
909
|
+
}));
|
|
910
|
+
yield* loadPcmCaptureWorklet(context, options.workletOptions);
|
|
911
|
+
const [capture] = yield* Effect.acquireRelease(Effect.try({
|
|
912
|
+
catch: (error) => new AudioCaptureError({
|
|
913
|
+
code: "PCM_CAPTURE_FAILED",
|
|
914
|
+
message: `PCM capture setup failed: ${String(error)}`
|
|
915
|
+
}),
|
|
916
|
+
try: () => buildPcmCapture(context, source, options)
|
|
917
|
+
}), ([, teardown]) => Effect.sync(teardown));
|
|
918
|
+
return capture;
|
|
919
|
+
});
|
|
914
920
|
// src/playback/media-session.ts
|
|
915
921
|
const SILENT_WAV_SAMPLE_RATE = 8e3;
|
|
916
922
|
const SILENT_WAV_DURATION_SECONDS = 10;
|
|
@@ -955,7 +961,7 @@ function createSilentWavBytes(durationSeconds = 10, sampleRate = SILENT_WAV_SAMP
|
|
|
955
961
|
}
|
|
956
962
|
function createSilentWavBlob(durationSeconds = 10, sampleRate = SILENT_WAV_SAMPLE_RATE) {
|
|
957
963
|
const bytes = createSilentWavBytes(durationSeconds, sampleRate);
|
|
958
|
-
return new Blob([bytes
|
|
964
|
+
return new Blob([bytes], { type: "audio/wav" });
|
|
959
965
|
}
|
|
960
966
|
const INERT_HANDLER = () => {};
|
|
961
967
|
const CORE_ACTIONS = [
|
|
@@ -1054,13 +1060,11 @@ function buildMediaSessionController(options) {
|
|
|
1054
1060
|
stopKeepAlive
|
|
1055
1061
|
};
|
|
1056
1062
|
}
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
});
|
|
1063
|
-
}
|
|
1063
|
+
const createMediaSessionController = Effect.fn("MediaSession.createController")(function* (options = {}) {
|
|
1064
|
+
const controller = buildMediaSessionController(options);
|
|
1065
|
+
yield* Effect.addFinalizer(() => Effect.sync(controller.dispose));
|
|
1066
|
+
return controller;
|
|
1067
|
+
});
|
|
1064
1068
|
// src/playback/sampler.ts
|
|
1065
1069
|
const SAMPLE_MIME_TYPES = {
|
|
1066
1070
|
mp3: "audio/mpeg",
|
|
@@ -1129,7 +1133,7 @@ function createSampler(context, options = {}) {
|
|
|
1129
1133
|
source.disconnect();
|
|
1130
1134
|
voice.disconnect();
|
|
1131
1135
|
};
|
|
1132
|
-
source.
|
|
1136
|
+
source.addEventListener("ended", cleanup, { once: true });
|
|
1133
1137
|
source.start(now, offset, duration + release);
|
|
1134
1138
|
return {
|
|
1135
1139
|
durationSeconds: duration,
|