@hyperframes/core 0.9.94 → 0.9.96
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/dist/atomicFile.js +14 -9
- package/dist/audibleVideo.d.ts +10 -0
- package/dist/audibleVideo.js +37 -0
- package/dist/compiler/htmlBundler.js +2 -0
- package/dist/compiler/index.d.ts +1 -0
- package/dist/compiler/index.js +1 -0
- package/dist/compiler/inlineSubCompositions.js +2 -1
- package/dist/compositionSource.d.ts +1 -0
- package/dist/compositionSource.js +22 -1
- package/dist/generated/runtime-inline.js +1 -1
- package/dist/hyperframe.runtime.iife.js +67 -67
- package/dist/index.d.ts +1 -1
- package/dist/runtime/adapters/seek-dispatch.d.ts +40 -0
- package/dist/runtime/adapters/seek-dispatch.js +164 -0
- package/dist/runtime/assetUrl.d.ts +6 -0
- package/dist/runtime/assetUrl.js +8 -0
- package/dist/runtime/audioFx.d.ts +77 -0
- package/dist/runtime/audioFx.js +277 -0
- package/dist/runtime/bridge.d.ts +6 -0
- package/dist/runtime/bridge.js +20 -0
- package/dist/runtime/decodesFirst.d.ts +4 -0
- package/dist/runtime/decodesFirst.js +14 -0
- package/dist/runtime/drawnBox.d.ts +29 -0
- package/dist/runtime/drawnBox.js +100 -0
- package/dist/runtime/editingDraft.d.ts +0 -11
- package/dist/runtime/editingDraft.js +1 -61
- package/dist/runtime/editingElement.js +3 -3
- package/dist/runtime/editingGestureStart.d.ts +2 -6
- package/dist/runtime/editingGestureStart.js +1 -40
- package/dist/runtime/editingHandles.d.ts +4 -0
- package/dist/runtime/editingHandles.js +9 -0
- package/dist/runtime/editingPort.d.ts +5 -0
- package/dist/runtime/editingPort.js +5 -1
- package/dist/runtime/media.d.ts +113 -0
- package/dist/runtime/media.js +820 -0
- package/dist/runtime/mediaLoadFailure.d.ts +8 -0
- package/dist/runtime/mediaLoadFailure.js +58 -0
- package/dist/runtime/mediaProxy.d.ts +91 -0
- package/dist/runtime/mediaProxy.js +803 -0
- package/dist/runtime/playbackHealth.d.ts +31 -0
- package/dist/runtime/playbackHealth.js +137 -0
- package/dist/runtime/preloadMedia.d.ts +16 -0
- package/dist/runtime/preloadMedia.js +266 -0
- package/dist/runtime/previewCopies.d.ts +13 -0
- package/dist/runtime/previewCopies.js +80 -0
- package/dist/runtime/proxyWait.d.ts +3 -0
- package/dist/runtime/proxyWait.js +51 -0
- package/dist/runtime/runtimeLoad.d.ts +3 -0
- package/dist/runtime/runtimeLoad.js +13 -0
- package/dist/runtime/state.d.ts +7 -0
- package/dist/runtime/state.js +3 -0
- package/dist/runtime/timedClipHide.d.ts +12 -0
- package/dist/runtime/timedClipHide.js +94 -0
- package/dist/runtime/types.d.ts +29 -18
- package/dist/runtime/webAudioTransport.d.ts +241 -0
- package/dist/runtime/webAudioTransport.js +1162 -0
- package/dist/safePath.d.ts +2 -0
- package/dist/safePath.js +41 -13
- package/dist/studioPreviewMark.js +2 -4
- package/dist/utils/inlineTextStyleRead.js +2 -4
- package/package.json +16 -4
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export type { PlaybackHeldBy, RuntimeTimelineClipIdentity } from "./runtime/types.js";
|
|
1
|
+
export type { PlaybackHealthReport, PlaybackHeldBy, RuntimeTimelineClipIdentity, } from "./runtime/types.js";
|
|
2
2
|
export type { ExecutionMode, Orientation, Asset, TimelineElement, TimelineElementBase, TimelineMediaElement, TimelineTextElement, TimelineCompositionElement, TimelineElementType, MediaElementType, CanvasResolution, Fps, FpsInput, FpsParseResult, MediaFile, CompositionAPI, PlayerAPI, AddElementData, ValidationResult, CompositionAsset, Keyframe, KeyframeProperties, ElementKeyframes, StageZoom, StageZoomKeyframe, CompositionVariableType, CompositionVariableBase, StringVariable, NumberVariable, ColorVariable, BooleanVariable, EnumVariable, CompositionVariable, CompositionSpec, WaveformData, OutputResolutionCompatibility, OutputResolutionIssueKind, } from "./core.types.js";
|
|
3
3
|
export type { SlideshowManifest, SlideRef, SlideHotspot, SlideSequence, ResolvedSlide, ResolvedSlideSequence, ResolvedSlideshow, } from "./slideshow/index.js";
|
|
4
4
|
export { parseSlideshowManifest, resolveSlideshow } from "./slideshow/index.js";
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/** What a seek waits on, named when the barrier stops waiting for it. */
|
|
2
|
+
export type SeekHold = "video-frame" | "hf-seek" | "frame-source" | "scene-window" | "motion-blur" | "vfx" | "image-decode";
|
|
3
|
+
export interface HfSeekEventDetail {
|
|
4
|
+
time: number;
|
|
5
|
+
waitUntil: (promise: PromiseLike<unknown>) => void;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Join `promise` to the seek-completion barrier: `waitForSeekCompletion` will
|
|
9
|
+
* not resolve until it settles, and its rejection (the first one) becomes the
|
|
10
|
+
* barrier's.
|
|
11
|
+
*
|
|
12
|
+
* Exported because the `"hf-seek"` event's synchronous `waitUntil()` is not
|
|
13
|
+
* the only asynchronous seek work a frame can owe. `runtime/vfx.ts` registers
|
|
14
|
+
* its preview-side capture here, so the CLI seek paths that already await this
|
|
15
|
+
* barrier wait for a `self` chain's paint instead of racing it.
|
|
16
|
+
*/
|
|
17
|
+
export declare function registerSeekCompletion(promise: Promise<unknown>, hold: SeekHold): void;
|
|
18
|
+
export declare function dispatchSeekEvent(time: number): void;
|
|
19
|
+
/**
|
|
20
|
+
* Force-dispatch a `"hf-seek"` event even if `time` equals the last dispatched
|
|
21
|
+
* time, bypassing the dedup guard.
|
|
22
|
+
*
|
|
23
|
+
* Needed for the post-video-injection GPU re-render: the engine seeks to time
|
|
24
|
+
* T (GPU adapters render once, before video frames are injected), then injects
|
|
25
|
+
* the decoded `__render_frame__` images, then must re-render GPU compositions
|
|
26
|
+
* at the *same* T so they re-upload textures from the now-present frames. The
|
|
27
|
+
* normal dedup would swallow that second dispatch.
|
|
28
|
+
*/
|
|
29
|
+
export declare function forceDispatchSeekEvent(time: number): void;
|
|
30
|
+
export declare function isSeekCompletionBarrierActive(): boolean;
|
|
31
|
+
/** Settles with every pending completion, without consuming a failure the barrier reports. */
|
|
32
|
+
export declare function seekCompletionsSettled(): Promise<void>;
|
|
33
|
+
export declare function hasPendingSeekCompletion(): boolean;
|
|
34
|
+
/** `videoFrames: false` skips only video-frame holds. Past a caller's `timeoutMs` it throws, naming what is pending. */
|
|
35
|
+
export declare function waitForSeekCompletion(options?: {
|
|
36
|
+
videoFrames?: boolean;
|
|
37
|
+
timeoutMs?: number;
|
|
38
|
+
}): Promise<void>;
|
|
39
|
+
/** Reset internal state — used in tests to prevent cross-test contamination. */
|
|
40
|
+
export declare function resetSeekDispatchState(): void;
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import { wallClearTimeout, wallSetTimeout } from "../wallClock.js";
|
|
2
|
+
import { swallow } from "../diagnostics.js";
|
|
3
|
+
/**
|
|
4
|
+
* Shared, deduplicated `"hf-seek"` CustomEvent dispatcher for GPU adapters.
|
|
5
|
+
*
|
|
6
|
+
* Both the Three.js and TypeGPU adapters dispatch the same `"hf-seek"` event
|
|
7
|
+
* so that compositions need not know which GPU library they're paired with.
|
|
8
|
+
* Without deduplication, a seek to time T would fire two events (one from each
|
|
9
|
+
* adapter), doubling per-scrub work in any composition that has both present.
|
|
10
|
+
*
|
|
11
|
+
* This module deduplicates by tracking the last dispatched time. If the same
|
|
12
|
+
* time value is dispatched twice in the same synchronous call stack (e.g. two
|
|
13
|
+
* adapters both calling `dispatchSeekEvent(5.0)` without yielding), only the
|
|
14
|
+
* first call fires the event.
|
|
15
|
+
*
|
|
16
|
+
* The deduplication is intentionally coarse (exact float equality). Adapter
|
|
17
|
+
* seek paths clamp and normalise time before calling this function, so the
|
|
18
|
+
* values that arrive here are already stable.
|
|
19
|
+
*/
|
|
20
|
+
let _lastDispatchedTime = -1;
|
|
21
|
+
let _pendingCompletions = new Map();
|
|
22
|
+
let _pendingFailure;
|
|
23
|
+
let _activeCompletionBarriers = 0;
|
|
24
|
+
function dispatch(time) {
|
|
25
|
+
const pending = [];
|
|
26
|
+
let accepting = true;
|
|
27
|
+
const detail = {
|
|
28
|
+
time,
|
|
29
|
+
waitUntil: (promise) => {
|
|
30
|
+
if (!accepting) {
|
|
31
|
+
throw new Error("hf-seek waitUntil() must be called synchronously from the event listener");
|
|
32
|
+
}
|
|
33
|
+
pending.push(promise);
|
|
34
|
+
},
|
|
35
|
+
};
|
|
36
|
+
try {
|
|
37
|
+
window.dispatchEvent(new CustomEvent("hf-seek", { detail }));
|
|
38
|
+
}
|
|
39
|
+
catch (err) {
|
|
40
|
+
swallow("runtime.adapters.seek-dispatch.site1", err);
|
|
41
|
+
}
|
|
42
|
+
finally {
|
|
43
|
+
accepting = false;
|
|
44
|
+
}
|
|
45
|
+
if (pending.length === 0)
|
|
46
|
+
return;
|
|
47
|
+
registerSeekCompletion(Promise.all(pending), "hf-seek");
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Join `promise` to the seek-completion barrier: `waitForSeekCompletion` will
|
|
51
|
+
* not resolve until it settles, and its rejection (the first one) becomes the
|
|
52
|
+
* barrier's.
|
|
53
|
+
*
|
|
54
|
+
* Exported because the `"hf-seek"` event's synchronous `waitUntil()` is not
|
|
55
|
+
* the only asynchronous seek work a frame can owe. `runtime/vfx.ts` registers
|
|
56
|
+
* its preview-side capture here, so the CLI seek paths that already await this
|
|
57
|
+
* barrier wait for a `self` chain's paint instead of racing it.
|
|
58
|
+
*/
|
|
59
|
+
export function registerSeekCompletion(promise, hold) {
|
|
60
|
+
const completion = promise
|
|
61
|
+
.then(() => ({ status: "fulfilled" }))
|
|
62
|
+
.catch((reason) => ({ status: "rejected", reason }));
|
|
63
|
+
_pendingCompletions.set(completion, hold);
|
|
64
|
+
void completion.then((result) => {
|
|
65
|
+
if (!_pendingCompletions.delete(completion))
|
|
66
|
+
return;
|
|
67
|
+
if (result.status === "rejected" && _pendingFailure === undefined) {
|
|
68
|
+
_pendingFailure = { reason: result.reason };
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
export function dispatchSeekEvent(time) {
|
|
73
|
+
if (time === _lastDispatchedTime)
|
|
74
|
+
return;
|
|
75
|
+
_lastDispatchedTime = time;
|
|
76
|
+
dispatch(time);
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Force-dispatch a `"hf-seek"` event even if `time` equals the last dispatched
|
|
80
|
+
* time, bypassing the dedup guard.
|
|
81
|
+
*
|
|
82
|
+
* Needed for the post-video-injection GPU re-render: the engine seeks to time
|
|
83
|
+
* T (GPU adapters render once, before video frames are injected), then injects
|
|
84
|
+
* the decoded `__render_frame__` images, then must re-render GPU compositions
|
|
85
|
+
* at the *same* T so they re-upload textures from the now-present frames. The
|
|
86
|
+
* normal dedup would swallow that second dispatch.
|
|
87
|
+
*/
|
|
88
|
+
export function forceDispatchSeekEvent(time) {
|
|
89
|
+
_lastDispatchedTime = time;
|
|
90
|
+
dispatch(time);
|
|
91
|
+
}
|
|
92
|
+
export function isSeekCompletionBarrierActive() {
|
|
93
|
+
return _activeCompletionBarriers > 0;
|
|
94
|
+
}
|
|
95
|
+
/** Settles with every pending completion, without consuming a failure the barrier reports. */
|
|
96
|
+
export async function seekCompletionsSettled() {
|
|
97
|
+
for (let pending = [..._pendingCompletions.keys()]; pending.length > 0; pending = [..._pendingCompletions.keys()]) {
|
|
98
|
+
await Promise.all(pending);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
export function hasPendingSeekCompletion() {
|
|
102
|
+
return _pendingCompletions.size > 0;
|
|
103
|
+
}
|
|
104
|
+
/** All of `pending` settled, or null once `deadline` (epoch ms) passes first. */
|
|
105
|
+
async function settledBy(pending, deadline) {
|
|
106
|
+
if (deadline === undefined)
|
|
107
|
+
return Promise.all(pending);
|
|
108
|
+
let timer;
|
|
109
|
+
const late = new Promise((resolve) => {
|
|
110
|
+
timer = wallSetTimeout(() => resolve(null), Math.max(0, deadline - Date.now()));
|
|
111
|
+
});
|
|
112
|
+
try {
|
|
113
|
+
return await Promise.race([Promise.all(pending), late]);
|
|
114
|
+
}
|
|
115
|
+
finally {
|
|
116
|
+
wallClearTimeout(timer);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
/** `videoFrames: false` skips only video-frame holds. Past a caller's `timeoutMs` it throws, naming what is pending. */
|
|
120
|
+
export async function waitForSeekCompletion(options) {
|
|
121
|
+
const awaited = () => [..._pendingCompletions].filter(([, hold]) => options?.videoFrames !== false || hold !== "video-frame");
|
|
122
|
+
const timeoutMs = options?.timeoutMs;
|
|
123
|
+
const deadline = timeoutMs === undefined ? undefined : Date.now() + timeoutMs;
|
|
124
|
+
_activeCompletionBarriers += 1;
|
|
125
|
+
let failed = _pendingFailure;
|
|
126
|
+
try {
|
|
127
|
+
// Let concurrently started barriers snapshot the same retained failure
|
|
128
|
+
// before either one consumes it.
|
|
129
|
+
await Promise.resolve();
|
|
130
|
+
for (let pending = awaited(); pending.length > 0; pending = awaited()) {
|
|
131
|
+
// Holds that keep arriving past the deadline must not outrun it.
|
|
132
|
+
const results = deadline === undefined || Date.now() < deadline
|
|
133
|
+
? await settledBy(pending.map(([completion]) => completion), deadline)
|
|
134
|
+
: null;
|
|
135
|
+
if (!results) {
|
|
136
|
+
const holds = [...new Set(awaited().map(([, hold]) => hold))].join(", ");
|
|
137
|
+
throw Object.assign(new Error(`seek did not settle within ${timeoutMs} ms: still waiting on ${holds}`), { name: "SeekNotSettledError" });
|
|
138
|
+
}
|
|
139
|
+
const rejected = results.find((result) => result.status === "rejected");
|
|
140
|
+
if (failed === undefined && rejected?.status === "rejected") {
|
|
141
|
+
failed = { reason: rejected.reason };
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
const retainedFailure = _pendingFailure;
|
|
145
|
+
if (failed === undefined) {
|
|
146
|
+
failed = retainedFailure;
|
|
147
|
+
}
|
|
148
|
+
if (_pendingFailure === retainedFailure) {
|
|
149
|
+
_pendingFailure = undefined;
|
|
150
|
+
}
|
|
151
|
+
if (failed)
|
|
152
|
+
throw failed.reason;
|
|
153
|
+
}
|
|
154
|
+
finally {
|
|
155
|
+
_activeCompletionBarriers -= 1;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
/** Reset internal state — used in tests to prevent cross-test contamination. */
|
|
159
|
+
export function resetSeekDispatchState() {
|
|
160
|
+
_lastDispatchedTime = -1;
|
|
161
|
+
_pendingCompletions = new Map();
|
|
162
|
+
_pendingFailure = undefined;
|
|
163
|
+
_activeCompletionBarriers = 0;
|
|
164
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Resolves a path the calling composition wrote relative to its own file. At the top level, and
|
|
3
|
+
* for a composition opened on its own, that file is the page. Inside a mounted sub-composition
|
|
4
|
+
* the scoped wrapper (compiler/compositionScoping.ts) replaces this with one bound to that file.
|
|
5
|
+
*/
|
|
6
|
+
export declare function assetUrl(path: string): string;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Resolves a path the calling composition wrote relative to its own file. At the top level, and
|
|
3
|
+
* for a composition opened on its own, that file is the page. Inside a mounted sub-composition
|
|
4
|
+
* the scoped wrapper (compiler/compositionScoping.ts) replaces this with one bound to that file.
|
|
5
|
+
*/
|
|
6
|
+
export function assetUrl(path) {
|
|
7
|
+
return new URL(path, document.baseURI).href;
|
|
8
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Live audio FX in preview.
|
|
3
|
+
*
|
|
4
|
+
* The transport plays each track from a decoded AudioBuffer and mutes the
|
|
5
|
+
* `<audio>` element to avoid doubling, so effects are spliced into that graph
|
|
6
|
+
* rather than captured off the element — capturing it would process a stream
|
|
7
|
+
* nothing is listening to.
|
|
8
|
+
*
|
|
9
|
+
* Preview and the offline render call the same graph builders, so what is heard
|
|
10
|
+
* while scrubbing is what gets written.
|
|
11
|
+
*/
|
|
12
|
+
import { type HfAutomation } from "../audioAutomation.js";
|
|
13
|
+
import { type AutomationTiming } from "../audio/audioFxAutomation.js";
|
|
14
|
+
import type { FxParamTarget } from "../audio/audioFxGraph.js";
|
|
15
|
+
/**
|
|
16
|
+
* An element's automation lanes, bound to whatever chain it carries.
|
|
17
|
+
*
|
|
18
|
+
* FX lanes need the chain to resolve their target's range, so they are dropped
|
|
19
|
+
* for an element with no chain; a volume lane is always readable.
|
|
20
|
+
*/
|
|
21
|
+
export declare function readElementAutomation(el: {
|
|
22
|
+
getAttribute?(name: string): string | null;
|
|
23
|
+
}): HfAutomation;
|
|
24
|
+
/**
|
|
25
|
+
* Splice an element's FX chain between a decoded source and its gain stage.
|
|
26
|
+
*
|
|
27
|
+
* The transport plays audio from a decoded AudioBuffer rather than from the
|
|
28
|
+
* `<audio>` element (the element is muted to avoid doubling), so this is the
|
|
29
|
+
* point where effects belong — capturing the element would process a stream
|
|
30
|
+
* nothing is listening to.
|
|
31
|
+
*
|
|
32
|
+
* A track with no chain is wired straight through, but still watched: adding its
|
|
33
|
+
* first effect is then heard without rescheduling the source.
|
|
34
|
+
*
|
|
35
|
+
* With `timing`, the element's automation lanes are scheduled onto the built
|
|
36
|
+
* effects as AudioParam ramps, and rescheduled when the attribute is edited or
|
|
37
|
+
* `setRate` reports the transport changed speed.
|
|
38
|
+
*/
|
|
39
|
+
export interface ElementFxHandle {
|
|
40
|
+
dispose(): void;
|
|
41
|
+
/**
|
|
42
|
+
* Re-book every envelope against a fresh reference frame.
|
|
43
|
+
*
|
|
44
|
+
* For a graph that OUTLIVES the source feeding it — a group bus, which is
|
|
45
|
+
* built once and kept for the session — a replay or a seek starts a new pass
|
|
46
|
+
* over the same chain. Without re-anchoring, the envelopes stay committed to
|
|
47
|
+
* the first pass's absolute context times: past their last point that is a
|
|
48
|
+
* stuck value, which for a fade-out is silence for the rest of the session.
|
|
49
|
+
*/
|
|
50
|
+
reanchor(timing: AutomationTiming): void;
|
|
51
|
+
/** Re-aim every booked envelope at `timing` without clearing effect state: the same pass, re-measured. */
|
|
52
|
+
retime(timing: AutomationTiming): void;
|
|
53
|
+
/**
|
|
54
|
+
* Re-aim every booked envelope at a new playback rate.
|
|
55
|
+
*
|
|
56
|
+
* Lanes are committed to absolute context times, so a param scheduled at 1×
|
|
57
|
+
* keeps its original wall-clock plan while the audio underneath runs at the
|
|
58
|
+
* new speed: a lowpass sweeping over 10 clip-seconds, switched to 2×, eats
|
|
59
|
+
* 20 s of material in 10 s of wall clock with the sweep unchanged.
|
|
60
|
+
*/
|
|
61
|
+
setRate(rate: number): void;
|
|
62
|
+
}
|
|
63
|
+
/** The attribute a chain's lanes are read from; the master bus has none. */
|
|
64
|
+
export interface FxAttributes {
|
|
65
|
+
automation: string | null;
|
|
66
|
+
}
|
|
67
|
+
/** Where a volume lane plays, and the `data-volume` it multiplies (`gainAhead`: played upstream). */
|
|
68
|
+
export interface VolumeTarget {
|
|
69
|
+
param: AudioParam;
|
|
70
|
+
gain?: () => number;
|
|
71
|
+
gainAhead?: boolean;
|
|
72
|
+
}
|
|
73
|
+
/** The element's volume lane on `target`, as a target to cancel later; none when it has no lane. */
|
|
74
|
+
export declare function scheduleVolume(target: VolumeTarget, automation: HfAutomation, at: AutomationTiming): FxParamTarget[];
|
|
75
|
+
export declare function attachElementFxChain(ctx: BaseAudioContext, el: {
|
|
76
|
+
getAttribute?(name: string): string | null;
|
|
77
|
+
}, source: AudioNode, destination: AudioNode, timing?: AutomationTiming, attrs?: FxAttributes, volume?: VolumeTarget): ElementFxHandle | null;
|
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Live audio FX in preview.
|
|
3
|
+
*
|
|
4
|
+
* The transport plays each track from a decoded AudioBuffer and mutes the
|
|
5
|
+
* `<audio>` element to avoid doubling, so effects are spliced into that graph
|
|
6
|
+
* rather than captured off the element — capturing it would process a stream
|
|
7
|
+
* nothing is listening to.
|
|
8
|
+
*
|
|
9
|
+
* Preview and the offline render call the same graph builders, so what is heard
|
|
10
|
+
* while scrubbing is what gets written.
|
|
11
|
+
*/
|
|
12
|
+
import { HF_AUDIO_FX_ATTR, parseAudioFxChain } from "../audioFx.js";
|
|
13
|
+
import { gainedVolumePoints, HF_AUDIO_AUTOMATION_ATTR, parseAutomation, resolveAutomation, VOLUME_RANGE, } from "../audioAutomation.js";
|
|
14
|
+
import { cancelParamLane, clearParamLane, scheduleChainAutomation, scheduleParamLane, volumeLane, } from "../audio/audioFxAutomation.js";
|
|
15
|
+
import { clampAudioGain } from "../audioGain.js";
|
|
16
|
+
import { audioFxWorkletsReady, buildFxChain, chainNeedsWorklets, ensureAudioFxWorklets, } from "../audio/audioFxGraph.js";
|
|
17
|
+
const EMPTY = { version: 1, nodes: [] };
|
|
18
|
+
const NO_AUTOMATION = { version: 1, lanes: [] };
|
|
19
|
+
function readAutomation(el, chain, attr = HF_AUDIO_AUTOMATION_ATTR) {
|
|
20
|
+
const raw = (attr && typeof el.getAttribute === "function" ? el.getAttribute(attr) : null) ?? "";
|
|
21
|
+
if (!raw)
|
|
22
|
+
return NO_AUTOMATION;
|
|
23
|
+
try {
|
|
24
|
+
return resolveAutomation(parseAutomation(raw), chain);
|
|
25
|
+
}
|
|
26
|
+
catch {
|
|
27
|
+
// Unreadable automation plays the track flat rather than silencing it,
|
|
28
|
+
// matching how an unreadable chain plays dry. The render refuses instead.
|
|
29
|
+
return NO_AUTOMATION;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
function readChain(el) {
|
|
33
|
+
// Callers include the transport, whose element may be any media-like object;
|
|
34
|
+
// anything without getAttribute simply has no chain.
|
|
35
|
+
const raw = (typeof el.getAttribute === "function" ? el.getAttribute(HF_AUDIO_FX_ATTR) : null) ?? "";
|
|
36
|
+
if (!raw)
|
|
37
|
+
return { chain: EMPTY, raw: "" };
|
|
38
|
+
try {
|
|
39
|
+
return { chain: parseAudioFxChain(raw), raw };
|
|
40
|
+
}
|
|
41
|
+
catch {
|
|
42
|
+
// An unreadable chain plays dry rather than silencing the track.
|
|
43
|
+
return { chain: EMPTY, raw: "" };
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* An element's automation lanes, bound to whatever chain it carries.
|
|
48
|
+
*
|
|
49
|
+
* FX lanes need the chain to resolve their target's range, so they are dropped
|
|
50
|
+
* for an element with no chain; a volume lane is always readable.
|
|
51
|
+
*/
|
|
52
|
+
export function readElementAutomation(el) {
|
|
53
|
+
return readAutomation(el, readChain(el).chain);
|
|
54
|
+
}
|
|
55
|
+
/** The element's volume lane on `target`, as a target to cancel later; none when it has no lane. */
|
|
56
|
+
export function scheduleVolume(target, automation, at) {
|
|
57
|
+
const lane = volumeLane(automation);
|
|
58
|
+
if (!lane)
|
|
59
|
+
return [];
|
|
60
|
+
const points = gainedVolumePoints(lane, target.gain?.() ?? 1, target.gainAhead);
|
|
61
|
+
scheduleParamLane([{ param: target.param }], { ...lane, points }, VOLUME_RANGE.scale, at);
|
|
62
|
+
return [{ param: target.param }];
|
|
63
|
+
}
|
|
64
|
+
function restoreStaticVolume(target, at) {
|
|
65
|
+
clearParamLane([target]);
|
|
66
|
+
target.param.setValueAtTime(target.gainAhead ? 1 : clampAudioGain(target.gain?.() ?? 1), at.scheduledAt);
|
|
67
|
+
}
|
|
68
|
+
export function attachElementFxChain(ctx, el, source, destination, timing, attrs = { automation: HF_AUDIO_AUTOMATION_ATTR }, volume) {
|
|
69
|
+
const { chain } = readChain(el);
|
|
70
|
+
// Null means the source runs straight into its gain: an empty chain, or one
|
|
71
|
+
// that could not be realised. Mutable because a structural edit swaps the
|
|
72
|
+
// whole graph rather than re-parameterising it.
|
|
73
|
+
let handle = null;
|
|
74
|
+
let automated = [];
|
|
75
|
+
let disposed = false;
|
|
76
|
+
/** Bumped per attach, so a worklet wait that resolves late cannot revive a
|
|
77
|
+
* chain the element has since moved on from. */
|
|
78
|
+
let workletGeneration = 0;
|
|
79
|
+
/** Take the current graph out of the path, leaving the source connected dry. */
|
|
80
|
+
const detach = () => {
|
|
81
|
+
try {
|
|
82
|
+
if (handle) {
|
|
83
|
+
source.disconnect(handle.input);
|
|
84
|
+
handle.output.disconnect(destination);
|
|
85
|
+
handle.dispose();
|
|
86
|
+
}
|
|
87
|
+
else {
|
|
88
|
+
source.disconnect(destination);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
catch {
|
|
92
|
+
// Already disconnected; nothing to unwind.
|
|
93
|
+
}
|
|
94
|
+
handle = null;
|
|
95
|
+
};
|
|
96
|
+
/**
|
|
97
|
+
* Put `next` in the signal path.
|
|
98
|
+
*
|
|
99
|
+
* A chain that cannot be realised — an unregistered worklet, an unknown
|
|
100
|
+
* effect — plays dry rather than silencing the track.
|
|
101
|
+
*/
|
|
102
|
+
const attach = (next, elapsed) => {
|
|
103
|
+
if (next.nodes.length === 0) {
|
|
104
|
+
source.connect(destination);
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
// An AudioWorkletNode cannot be constructed before its processor is
|
|
108
|
+
// registered — it throws, and the whole chain is lost. So a chain holding a
|
|
109
|
+
// limiter, compressor, gate or bitcrush plays dry until the module lands and
|
|
110
|
+
// then takes the ordinary rebuild path.
|
|
111
|
+
//
|
|
112
|
+
// That wait used to return early from the whole function, which left the
|
|
113
|
+
// track with no automation scheduled and no observer on the attribute: adding
|
|
114
|
+
// a compressor to a carved bed killed the carve's envelopes and froze every
|
|
115
|
+
// later edit until the composition reloaded. Rebuilding through the same path
|
|
116
|
+
// an edit uses is what keeps those two working.
|
|
117
|
+
if (chainNeedsWorklets(next) && !audioFxWorkletsReady(ctx)) {
|
|
118
|
+
source.connect(destination);
|
|
119
|
+
const generation = ++workletGeneration;
|
|
120
|
+
void ensureAudioFxWorklets(ctx)
|
|
121
|
+
.then(() => {
|
|
122
|
+
// Stale if the element was disposed or the chain changed while waiting.
|
|
123
|
+
if (disposed || generation !== workletGeneration)
|
|
124
|
+
return;
|
|
125
|
+
rebuild(readChain(el).chain);
|
|
126
|
+
})
|
|
127
|
+
.catch(() => undefined);
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
try {
|
|
131
|
+
// Where the clip has got to, so a modulated effect resumes at the phase the
|
|
132
|
+
// render would be at rather than restarting its LFO from zero.
|
|
133
|
+
const built = buildFxChain(ctx, next, elapsed);
|
|
134
|
+
source.connect(built.input);
|
|
135
|
+
built.output.connect(destination);
|
|
136
|
+
handle = built;
|
|
137
|
+
}
|
|
138
|
+
catch {
|
|
139
|
+
source.connect(destination);
|
|
140
|
+
}
|
|
141
|
+
};
|
|
142
|
+
const scheduleFor = (next, at) => {
|
|
143
|
+
if (!at) {
|
|
144
|
+
automated = [];
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
const automation = readAutomation(el, next, attrs.automation);
|
|
148
|
+
const fxTargets = handle
|
|
149
|
+
? scheduleChainAutomation(automation, next, handle.nodes, at, handle.presets)
|
|
150
|
+
: [];
|
|
151
|
+
const volumeTargets = volume ? scheduleVolume(volume, automation, at) : [];
|
|
152
|
+
if (volume &&
|
|
153
|
+
volumeTargets.length === 0 &&
|
|
154
|
+
automated.some((target) => target.param === volume.param)) {
|
|
155
|
+
restoreStaticVolume(volume, at);
|
|
156
|
+
}
|
|
157
|
+
automated = [...fxTargets, ...volumeTargets];
|
|
158
|
+
};
|
|
159
|
+
// The reference frame every later reschedule measures from. Mutable because a
|
|
160
|
+
// rate change rebases it: `elapsed` has to stop advancing at the old rate the
|
|
161
|
+
// instant the new one takes effect, or every subsequent edit re-aims the
|
|
162
|
+
// envelope at the wrong clip position.
|
|
163
|
+
let frame = timing ? { ...timing } : null;
|
|
164
|
+
// `timingNow` is not in scope yet, and does not need to be: nothing has played
|
|
165
|
+
// between the frame being taken and this line.
|
|
166
|
+
attach(chain, frame?.elapsed ?? 0);
|
|
167
|
+
scheduleFor(chain, frame);
|
|
168
|
+
/**
|
|
169
|
+
* Re-aim the envelope at the live playhead. An edit lands mid-playback, so
|
|
170
|
+
* the clip has advanced past the offset the source was scheduled with.
|
|
171
|
+
*/
|
|
172
|
+
const timingNow = () => {
|
|
173
|
+
if (!frame)
|
|
174
|
+
return null;
|
|
175
|
+
const now = typeof ctx.currentTime === "number" ? ctx.currentTime : frame.scheduledAt;
|
|
176
|
+
return {
|
|
177
|
+
scheduledAt: now,
|
|
178
|
+
elapsed: frame.elapsed + (now - frame.scheduledAt) * frame.rate,
|
|
179
|
+
rate: frame.rate,
|
|
180
|
+
};
|
|
181
|
+
};
|
|
182
|
+
const rescheduleAutomation = (next) => {
|
|
183
|
+
const at = timingNow();
|
|
184
|
+
if (!at)
|
|
185
|
+
return;
|
|
186
|
+
cancelParamLane(automated);
|
|
187
|
+
scheduleFor(next, at);
|
|
188
|
+
};
|
|
189
|
+
/**
|
|
190
|
+
* Rebuild the graph for a shape change — an effect added, removed, bypassed,
|
|
191
|
+
* or a filter's pole count switched — while the source keeps playing.
|
|
192
|
+
*
|
|
193
|
+
* The source node is untouched, so the audio does not restart; only the
|
|
194
|
+
* effects between it and its gain are replaced. Doing this here is what keeps
|
|
195
|
+
* a structural edit from needing a composition reload, which is what made the
|
|
196
|
+
* audio audibly chop.
|
|
197
|
+
*/
|
|
198
|
+
const rebuild = (next) => {
|
|
199
|
+
const at = timingNow();
|
|
200
|
+
cancelParamLane(automated);
|
|
201
|
+
detach();
|
|
202
|
+
attach(next, at?.elapsed ?? 0);
|
|
203
|
+
scheduleFor(next, at);
|
|
204
|
+
};
|
|
205
|
+
// Follow the attribute while the source plays, so editing a chain is heard
|
|
206
|
+
// without rescheduling the track. A values-only change re-parameterises the
|
|
207
|
+
// running graph and lands on the next 128-sample quantum; anything structural
|
|
208
|
+
// swaps the effects between the source and its gain, leaving the source — and
|
|
209
|
+
// so the playing audio — alone.
|
|
210
|
+
let observer = null;
|
|
211
|
+
const target = el;
|
|
212
|
+
if (typeof MutationObserver !== "undefined" &&
|
|
213
|
+
typeof target?.nodeType === "number") {
|
|
214
|
+
observer = new MutationObserver(() => {
|
|
215
|
+
const next = readChain(el);
|
|
216
|
+
// Clear the booked envelopes before touching the graph. `update` writes each
|
|
217
|
+
// knob straight onto its AudioParam, and a write landing inside a running
|
|
218
|
+
// curve is refused with NotSupportedError unless the param is cancelled
|
|
219
|
+
// first — so an edit made while one was playing threw instead of applying,
|
|
220
|
+
// and the console filled with uncaught errors. The reschedule below puts the
|
|
221
|
+
// envelope back from the current playhead.
|
|
222
|
+
if (automated.length > 0)
|
|
223
|
+
clearParamLane(automated);
|
|
224
|
+
// `update` reports false when the change is structural rather than a new
|
|
225
|
+
// set of values, which is the signal to swap the graph.
|
|
226
|
+
if (!handle || !handle.update(next.chain))
|
|
227
|
+
rebuild(next.chain);
|
|
228
|
+
else
|
|
229
|
+
rescheduleAutomation(next.chain);
|
|
230
|
+
});
|
|
231
|
+
observer.observe(target, {
|
|
232
|
+
attributes: true,
|
|
233
|
+
attributeFilter: [
|
|
234
|
+
HF_AUDIO_FX_ATTR,
|
|
235
|
+
...(attrs.automation ? [attrs.automation] : []),
|
|
236
|
+
// The lane's ceiling depends on the gain it multiplies.
|
|
237
|
+
...(volume?.gain ? ["data-volume"] : []),
|
|
238
|
+
],
|
|
239
|
+
});
|
|
240
|
+
}
|
|
241
|
+
const retime = (next) => {
|
|
242
|
+
if (disposed)
|
|
243
|
+
return;
|
|
244
|
+
const at = timingNow();
|
|
245
|
+
if (at)
|
|
246
|
+
cancelParamLane(automated);
|
|
247
|
+
frame = { ...next };
|
|
248
|
+
scheduleFor(readChain(el).chain, frame);
|
|
249
|
+
};
|
|
250
|
+
return {
|
|
251
|
+
reanchor: (next) => {
|
|
252
|
+
if (disposed)
|
|
253
|
+
return;
|
|
254
|
+
handle?.reset();
|
|
255
|
+
retime(next);
|
|
256
|
+
},
|
|
257
|
+
retime,
|
|
258
|
+
setRate: (rate) => {
|
|
259
|
+
const at = timingNow();
|
|
260
|
+
if (disposed || !at || !Number.isFinite(rate) || rate <= 0 || rate === at.rate)
|
|
261
|
+
return;
|
|
262
|
+
// Rebased at the playhead the OLD rate carried us to, then replayed from
|
|
263
|
+
// there at the new one.
|
|
264
|
+
frame = { ...at, rate };
|
|
265
|
+
cancelParamLane(automated);
|
|
266
|
+
scheduleFor(readChain(el).chain, frame);
|
|
267
|
+
},
|
|
268
|
+
dispose: () => {
|
|
269
|
+
disposed = true;
|
|
270
|
+
observer?.disconnect();
|
|
271
|
+
if (automated.length > 0) {
|
|
272
|
+
cancelParamLane(automated);
|
|
273
|
+
}
|
|
274
|
+
handle?.dispose();
|
|
275
|
+
},
|
|
276
|
+
};
|
|
277
|
+
}
|
package/dist/runtime/bridge.d.ts
CHANGED
|
@@ -12,6 +12,12 @@ type BridgeDeps = {
|
|
|
12
12
|
onSetMediaOutputMuted: (muted: boolean) => void;
|
|
13
13
|
onSetNativeMediaSyncDisabled: (disabled: boolean) => void;
|
|
14
14
|
onSetWebAudioMediaDisabled: (disabled: boolean) => void;
|
|
15
|
+
onSetParentAudio?: (clips: string[]) => void;
|
|
16
|
+
onAudioClock?: (sample: {
|
|
17
|
+
time: number;
|
|
18
|
+
rate: number;
|
|
19
|
+
at: number;
|
|
20
|
+
} | null) => void;
|
|
15
21
|
onSetPlaybackRate: (rate: number) => void;
|
|
16
22
|
onSetIdleHeartbeat: (slow: boolean) => void;
|
|
17
23
|
onSetRootDuration: (durationSeconds: number) => void;
|
package/dist/runtime/bridge.js
CHANGED
|
@@ -21,6 +21,8 @@ const ANOTHER_SITE_DRIVES = new Set([
|
|
|
21
21
|
"set-playback-rate",
|
|
22
22
|
"set-idle-heartbeat",
|
|
23
23
|
"set-display-scale",
|
|
24
|
+
"set-parent-audio",
|
|
25
|
+
"audio-clock",
|
|
24
26
|
]);
|
|
25
27
|
let markedForAnotherSite = false;
|
|
26
28
|
const forAnotherSite = () => (markedForAnotherSite ||= typeof document !== "undefined" && servedToAnotherSite(document));
|
|
@@ -41,6 +43,8 @@ const FLASH_DEFAULT_DURATION_MS = 800;
|
|
|
41
43
|
const FLASH_MAX_DURATION_MS = 5000;
|
|
42
44
|
const MAX_FLASH_SELECTORS = 100;
|
|
43
45
|
const MAX_SELECTOR_LENGTH = 1000;
|
|
46
|
+
const MAX_PARENT_AUDIO_CLIPS = 1000;
|
|
47
|
+
const MAX_PARENT_AUDIO_KEY = 8192;
|
|
44
48
|
function isFiniteNumber(value) {
|
|
45
49
|
return typeof value === "number" && Number.isFinite(value);
|
|
46
50
|
}
|
|
@@ -118,6 +122,22 @@ const CONTROL_HANDLERS = new Map(Object.entries({
|
|
|
118
122
|
"set-media-output-muted": (data, deps) => deps.onSetMediaOutputMuted(data.muted === true),
|
|
119
123
|
"set-native-media-sync-disabled": (data, deps) => deps.onSetNativeMediaSyncDisabled(data.disabled === true),
|
|
120
124
|
"set-web-audio-media-disabled": (data, deps) => deps.onSetWebAudioMediaDisabled(data.disabled === true),
|
|
125
|
+
"set-parent-audio": (data, deps) => {
|
|
126
|
+
const clips = data.clips;
|
|
127
|
+
if (!Array.isArray(clips) || clips.length > MAX_PARENT_AUDIO_CLIPS)
|
|
128
|
+
return;
|
|
129
|
+
if (!clips.every((clip) => typeof clip === "string" && clip.length <= MAX_PARENT_AUDIO_KEY))
|
|
130
|
+
return;
|
|
131
|
+
deps.onSetParentAudio?.(clips);
|
|
132
|
+
},
|
|
133
|
+
"audio-clock": (data, deps) => {
|
|
134
|
+
if (data.time === null)
|
|
135
|
+
return deps.onAudioClock?.(null);
|
|
136
|
+
const { time, rate, at } = data;
|
|
137
|
+
if (!isFiniteNumber(time) || !isFiniteNumber(rate) || !isFiniteNumber(at) || rate <= 0)
|
|
138
|
+
return;
|
|
139
|
+
deps.onAudioClock?.({ time: Math.max(0, time), rate, at });
|
|
140
|
+
},
|
|
121
141
|
"set-playback-rate": (data, deps) => {
|
|
122
142
|
const rate = readNumber(data.playbackRate, 1);
|
|
123
143
|
if (rate !== null)
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { WebAudioTransport } from "./webAudioTransport.js";
|
|
2
|
+
export { bytesOfSource } from "./preloadMedia.js";
|
|
3
|
+
/** WebKit restarts a media element's audio 150 to 400 ms late; a decoded buffer on the context clock starts on time. */
|
|
4
|
+
export declare function decodesFirst(el: HTMLMediaElement, webAudio: WebAudioTransport): boolean;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { isAudioElement } from "./domRealm.js";
|
|
2
|
+
import { readElementPlaybackRate, readElementRateSpec } from "./media.js";
|
|
3
|
+
import { isWebKit } from "./mediaClock.js";
|
|
4
|
+
import { isLongSource } from "./preloadMedia.js";
|
|
5
|
+
export { bytesOfSource } from "./preloadMedia.js";
|
|
6
|
+
/** WebKit restarts a media element's audio 150 to 400 ms late; a decoded buffer on the context clock starts on time. */
|
|
7
|
+
export function decodesFirst(el, webAudio) {
|
|
8
|
+
return (isWebKit() &&
|
|
9
|
+
isAudioElement(el) &&
|
|
10
|
+
!isLongSource(el) &&
|
|
11
|
+
webAudio.fitsDecodeBudget(el) &&
|
|
12
|
+
typeof readElementRateSpec(el) === "number" &&
|
|
13
|
+
readElementPlaybackRate(el) === 1);
|
|
14
|
+
}
|