@avoid.quest/audio 0.1.1-beta.1 → 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 +2 -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 +72 -43
- package/dist/playback/index.js +114 -117
- 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 +8 -9
- package/types/hls-unavailable.d.ts +10 -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,12 +541,12 @@ 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;
|
|
547
551
|
let currentProtocol;
|
|
548
552
|
let currentUrl;
|
|
@@ -554,8 +558,8 @@ function buildMediaEngine(scope, options) {
|
|
|
554
558
|
let pendingSeekCleanup;
|
|
555
559
|
let readyWatchdogFiber;
|
|
556
560
|
let cancelPendingReadyWait;
|
|
557
|
-
const hlsRecovery = createRecoveryRunner(
|
|
558
|
-
const stallRecovery = createRecoveryRunner(
|
|
561
|
+
const hlsRecovery = createRecoveryRunner(fork, recoverySchedule);
|
|
562
|
+
const stallRecovery = createRecoveryRunner(fork, recoverySchedule);
|
|
559
563
|
const hasMetadata = () => typeof element.readyState === "number" && element.readyState >= 1;
|
|
560
564
|
const applyRate = () => {
|
|
561
565
|
element.defaultPlaybackRate = playbackRate;
|
|
@@ -590,7 +594,11 @@ function buildMediaEngine(scope, options) {
|
|
|
590
594
|
await element.play();
|
|
591
595
|
}
|
|
592
596
|
});
|
|
593
|
-
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) {
|
|
594
602
|
if (mode === "hls.js" && hlsInstance) {
|
|
595
603
|
hlsInstance.startLoad();
|
|
596
604
|
stallWatchdog.arm();
|
|
@@ -604,12 +612,7 @@ function buildMediaEngine(scope, options) {
|
|
|
604
612
|
yield* play().pipe(Effect.timeoutOption(Duration.millis(Math.max(1, stallWatchdogMs))), Effect.catch(() => Effect.void));
|
|
605
613
|
stallWatchdog.arm();
|
|
606
614
|
}
|
|
607
|
-
});
|
|
608
|
-
const stallWatchdog = createStallWatchdog(element, stallWatchdogMs, stallRecovery, () => ({
|
|
609
|
-
disposed,
|
|
610
|
-
gen: loadGeneration,
|
|
611
|
-
hasUrl: currentUrl !== void 0
|
|
612
|
-
}), recoverStall, options.onStall, options.onStreamError);
|
|
615
|
+
}), options.onStall, options.onStreamError);
|
|
613
616
|
const resetPlayback = () => {
|
|
614
617
|
cancelPendingReadyWait?.();
|
|
615
618
|
cancelPendingReadyWait = void 0;
|
|
@@ -621,7 +624,25 @@ function buildMediaEngine(scope, options) {
|
|
|
621
624
|
hlsInstance?.destroy();
|
|
622
625
|
hlsInstance = void 0;
|
|
623
626
|
};
|
|
624
|
-
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);
|
|
625
646
|
loadGeneration += 1;
|
|
626
647
|
const gen = loadGeneration;
|
|
627
648
|
resetPlayback();
|
|
@@ -632,23 +653,16 @@ function buildMediaEngine(scope, options) {
|
|
|
632
653
|
if (loadOptions.crossOrigin !== void 0) element.crossOrigin = loadOptions.crossOrigin;
|
|
633
654
|
const allowNative = loadOptions.allowNativeHls ?? options.allowNativeHls ?? true;
|
|
634
655
|
const hlsStream = loadOptions.protocol === void 0 ? isHlsUrl(url) : loadOptions.protocol === "hls";
|
|
635
|
-
let
|
|
636
|
-
let manifestEvent;
|
|
656
|
+
let started;
|
|
637
657
|
if (hlsStream && !shouldUseNativeHls(element, allowNative, options.userAgent)) {
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
try: () => loadHlsModule()
|
|
641
|
-
});
|
|
642
|
-
if (disposed || gen !== loadGeneration) return yield* failSuperseded();
|
|
643
|
-
if (typeof HlsCtor?.isSupported === "function" && HlsCtor.isSupported()) {
|
|
644
|
-
activeHls = attachHlsInstance(HlsCtor, element, url, options.hlsConfig, loadOptions, hlsRecovery, (hls) => !disposed && hlsInstance === hls, clearReady, options.onStreamError);
|
|
645
|
-
hlsInstance = activeHls;
|
|
646
|
-
mode = "hls.js";
|
|
647
|
-
syncRateAndPitch();
|
|
648
|
-
manifestEvent = HlsCtor.Events.MANIFEST_PARSED;
|
|
649
|
-
}
|
|
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);
|
|
650
660
|
}
|
|
651
|
-
if (
|
|
661
|
+
if (started) {
|
|
662
|
+
hlsInstance = started.player;
|
|
663
|
+
mode = "hls.js";
|
|
664
|
+
syncRateAndPitch();
|
|
665
|
+
} else {
|
|
652
666
|
mode = hlsStream ? "native-hls" : "native";
|
|
653
667
|
loadNative(url, loadOptions.startPositionSeconds);
|
|
654
668
|
}
|
|
@@ -657,9 +671,9 @@ function buildMediaEngine(scope, options) {
|
|
|
657
671
|
return () => {
|
|
658
672
|
if (cancelPendingReadyWait === cancel) cancelPendingReadyWait = void 0;
|
|
659
673
|
};
|
|
660
|
-
},
|
|
674
|
+
}, started?.onManifestParsed);
|
|
661
675
|
if (loadOptions.waitForReady) yield* ready;
|
|
662
|
-
else if (readyMs > 0 && !disposed) readyWatchdogFiber = forkReadyWatchdog(
|
|
676
|
+
else if (readyMs > 0 && !disposed) readyWatchdogFiber = forkReadyWatchdog(fork, ready, (failure) => {
|
|
663
677
|
if (!disposed && gen === loadGeneration && failure.code !== "MEDIA_LOAD_SUPERSEDED" && !hasMetadata()) options.onStreamError?.(element.currentTime, failure);
|
|
664
678
|
});
|
|
665
679
|
});
|
|
@@ -696,7 +710,7 @@ function buildMediaEngine(scope, options) {
|
|
|
696
710
|
element.pause();
|
|
697
711
|
}),
|
|
698
712
|
play,
|
|
699
|
-
refreshUrl:
|
|
713
|
+
refreshUrl: Effect.fn("MediaEngine.refreshUrl")(function* (url, seekPos) {
|
|
700
714
|
const wasPlaying = !element.paused;
|
|
701
715
|
const at = seekPos ?? element.currentTime;
|
|
702
716
|
yield* load(url, {
|
|
@@ -723,17 +737,15 @@ function buildMediaEngine(scope, options) {
|
|
|
723
737
|
setVolume: (vol) => Effect.sync(() => element.volume = clampFinite(vol, 0, 1, 1))
|
|
724
738
|
};
|
|
725
739
|
}
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
return engine;
|
|
732
|
-
});
|
|
733
|
-
}
|
|
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
|
+
});
|
|
734
745
|
// src/playback/media-source.ts
|
|
735
746
|
const elementSources = /* @__PURE__ */ new WeakMap();
|
|
736
747
|
const destinationRouted = /* @__PURE__ */ new WeakSet();
|
|
748
|
+
const isElementHolder = (target) => "element" in target;
|
|
737
749
|
function sourceError(code, message, cause) {
|
|
738
750
|
return new AudioPlaybackError({
|
|
739
751
|
...cause === void 0 ? {} : { cause: String(cause) },
|
|
@@ -743,15 +755,13 @@ function sourceError(code, message, cause) {
|
|
|
743
755
|
});
|
|
744
756
|
}
|
|
745
757
|
/**
|
|
746
|
-
*
|
|
747
|
-
*
|
|
748
|
-
*
|
|
749
|
-
* `AudioContext` fails with `MEDIA_SOURCE_CONTEXT_MISMATCH`. Cross-origin
|
|
750
|
-
* 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.
|
|
751
761
|
*/
|
|
752
762
|
function getMediaElementSourceNode(context, target, options = {}) {
|
|
753
763
|
return Effect.suspend(() => {
|
|
754
|
-
const element =
|
|
764
|
+
const element = isElementHolder(target) ? target.element : target;
|
|
755
765
|
let node = elementSources.get(element);
|
|
756
766
|
if (node && node.context !== context) return Effect.fail(sourceError("MEDIA_SOURCE_CONTEXT_MISMATCH", "This media element is already bound to a different AudioContext."));
|
|
757
767
|
if (!node) {
|
|
@@ -778,11 +788,9 @@ function getMediaElementSourceNode(context, target, options = {}) {
|
|
|
778
788
|
// src/worklets/pcm-capture.ts
|
|
779
789
|
const PCM_CAPTURE_PROCESSOR_NAME = "avoid-pcm-capture";
|
|
780
790
|
const PCM_CAPTURE_DEFAULT_CHUNK_FRAMES = 2048;
|
|
781
|
-
/** Main-thread → processor message that ends the processor's lifetime. */
|
|
782
791
|
const PCM_CAPTURE_STOP_MESSAGE = "stop";
|
|
783
792
|
const PCM_CAPTURE_MIN_CHUNK_FRAMES = 128;
|
|
784
793
|
const PCM_CAPTURE_MAX_CHUNK_FRAMES = 16384;
|
|
785
|
-
/** Clamps a requested chunk size to whole frames within `[128, 16384]`. */
|
|
786
794
|
function clampPcmChunkFrames(chunkFrames) {
|
|
787
795
|
if (chunkFrames === void 0 || !Number.isFinite(chunkFrames)) return PCM_CAPTURE_DEFAULT_CHUNK_FRAMES;
|
|
788
796
|
return Math.max(PCM_CAPTURE_MIN_CHUNK_FRAMES, Math.min(PCM_CAPTURE_MAX_CHUNK_FRAMES, Math.round(chunkFrames)));
|
|
@@ -825,7 +833,6 @@ if (typeof registerProcessor === "function") registerProcessor(PCM_CAPTURE_PROCE
|
|
|
825
833
|
}
|
|
826
834
|
});
|
|
827
835
|
// src/playback/pcm-capture.ts
|
|
828
|
-
/** Loads the standalone `@avoid.quest/audio/worklets/pcm-capture` module. */
|
|
829
836
|
function loadPcmCaptureWorklet(context, options = {}) {
|
|
830
837
|
return loadAudioWorklet(context, {
|
|
831
838
|
...options,
|
|
@@ -895,29 +902,21 @@ function buildPcmCapture(context, source, options) {
|
|
|
895
902
|
toShazamPcm: (seconds) => prepareShazamPcm(snapshot(seconds), sampleRate)
|
|
896
903
|
}, teardown];
|
|
897
904
|
}
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
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
|
-
});
|
|
920
|
-
}
|
|
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
|
+
});
|
|
921
920
|
// src/playback/media-session.ts
|
|
922
921
|
const SILENT_WAV_SAMPLE_RATE = 8e3;
|
|
923
922
|
const SILENT_WAV_DURATION_SECONDS = 10;
|
|
@@ -962,7 +961,7 @@ function createSilentWavBytes(durationSeconds = 10, sampleRate = SILENT_WAV_SAMP
|
|
|
962
961
|
}
|
|
963
962
|
function createSilentWavBlob(durationSeconds = 10, sampleRate = SILENT_WAV_SAMPLE_RATE) {
|
|
964
963
|
const bytes = createSilentWavBytes(durationSeconds, sampleRate);
|
|
965
|
-
return new Blob([bytes
|
|
964
|
+
return new Blob([bytes], { type: "audio/wav" });
|
|
966
965
|
}
|
|
967
966
|
const INERT_HANDLER = () => {};
|
|
968
967
|
const CORE_ACTIONS = [
|
|
@@ -1061,13 +1060,11 @@ function buildMediaSessionController(options) {
|
|
|
1061
1060
|
stopKeepAlive
|
|
1062
1061
|
};
|
|
1063
1062
|
}
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
});
|
|
1070
|
-
}
|
|
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
|
+
});
|
|
1071
1068
|
// src/playback/sampler.ts
|
|
1072
1069
|
const SAMPLE_MIME_TYPES = {
|
|
1073
1070
|
mp3: "audio/mpeg",
|
|
@@ -1136,7 +1133,7 @@ function createSampler(context, options = {}) {
|
|
|
1136
1133
|
source.disconnect();
|
|
1137
1134
|
voice.disconnect();
|
|
1138
1135
|
};
|
|
1139
|
-
source.
|
|
1136
|
+
source.addEventListener("ended", cleanup, { once: true });
|
|
1140
1137
|
source.start(now, offset, duration + release);
|
|
1141
1138
|
return {
|
|
1142
1139
|
durationSeconds: duration,
|