@motionscript/browser 0.1.0-alpha.3 → 0.1.0-alpha.6
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/CHANGELOG.md +108 -0
- package/LICENSE +201 -201
- package/NOTICE +48 -0
- package/README.md +118 -69
- package/dist/asset-bytes.js.map +1 -1
- package/dist/audio/bus-graph.d.ts +2 -2
- package/dist/audio/bus-graph.d.ts.map +1 -1
- package/dist/audio/bus-graph.js +6 -6
- package/dist/audio/bus-graph.js.map +1 -1
- package/dist/audio/filter-graph.d.ts +5 -5
- package/dist/audio/filter-graph.d.ts.map +1 -1
- package/dist/audio/filter-graph.js +15 -15
- package/dist/audio/filter-graph.js.map +1 -1
- package/dist/audio/index.js.map +1 -1
- package/dist/audio/loop-region.d.ts +31 -0
- package/dist/audio/loop-region.d.ts.map +1 -0
- package/dist/audio/loop-region.js +43 -0
- package/dist/audio/loop-region.js.map +1 -0
- package/dist/audio/mixer.d.ts.map +1 -1
- package/dist/audio/mixer.js +26 -10
- package/dist/audio/mixer.js.map +1 -1
- package/dist/audio/player.d.ts +4 -4
- package/dist/audio/player.d.ts.map +1 -1
- package/dist/audio/player.js +15 -14
- package/dist/audio/player.js.map +1 -1
- package/dist/browser/THIRD_PARTY_LICENSES.txt +432 -0
- package/dist/browser/audio.js +1 -1
- package/dist/browser/chunks/chunk-2MAFJWBZ.js +2 -0
- package/dist/browser/chunks/chunk-2MAFJWBZ.js.map +7 -0
- package/dist/browser/index.js +130 -276
- package/dist/browser/index.js.map +4 -4
- package/dist/browser/manifest.json +19 -2
- package/dist/concat.js.map +1 -1
- package/dist/decode/image-levels.js.map +1 -1
- package/dist/decode/levels.js.map +1 -1
- package/dist/decode/video-levels.js.map +1 -1
- package/dist/dynamic-nodes.d.ts.map +1 -1
- package/dist/dynamic-nodes.js +6 -12
- package/dist/dynamic-nodes.js.map +1 -1
- package/dist/engine.d.ts +6 -6
- package/dist/engine.d.ts.map +1 -1
- package/dist/engine.js +1 -0
- package/dist/engine.js.map +1 -1
- package/dist/exporter.d.ts +8 -2
- package/dist/exporter.d.ts.map +1 -1
- package/dist/exporter.js +8 -7
- package/dist/exporter.js.map +1 -1
- package/dist/getter.js.map +1 -1
- package/dist/index.js.map +1 -1
- package/dist/measure-context.js.map +1 -1
- package/dist/mix-timeline-audio.d.ts +2 -2
- package/dist/mix-timeline-audio.d.ts.map +1 -1
- package/dist/mix-timeline-audio.js +1 -2
- package/dist/mix-timeline-audio.js.map +1 -1
- package/dist/render-context.js.map +1 -1
- package/dist/screenshot.d.ts +3 -3
- package/dist/screenshot.d.ts.map +1 -1
- package/dist/screenshot.js +1 -2
- package/dist/screenshot.js.map +1 -1
- package/dist/still.d.ts +6 -4
- package/dist/still.d.ts.map +1 -1
- package/dist/still.js +12 -9
- package/dist/still.js.map +1 -1
- package/dist/storage-adapter.d.ts.map +1 -1
- package/dist/storage-adapter.js +5 -4
- package/dist/storage-adapter.js.map +1 -1
- package/dist/three/renderer.js.map +1 -1
- package/dist/ticker.d.ts +22 -4
- package/dist/ticker.d.ts.map +1 -1
- package/dist/ticker.js +65 -28
- package/dist/ticker.js.map +1 -1
- package/package.json +7 -6
- package/dist/browser/chunks/chunk-M7XD73ED.js +0 -2
- package/dist/browser/chunks/chunk-M7XD73ED.js.map +0 -7
- package/dist/output.d.ts +0 -7
- package/dist/output.d.ts.map +0 -1
- package/dist/output.js +0 -6
- package/dist/output.js.map +0 -1
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The region a looping request repeats: from its in-point to its out-point, or to
|
|
3
|
+
* the end of the buffer when it has none. `null` for a request that does not loop,
|
|
4
|
+
* and for a degenerate region (an out-point at or before the in-point), where the
|
|
5
|
+
* device falls back to wrapping the whole buffer.
|
|
6
|
+
*
|
|
7
|
+
* Shared by the live device and the offline mixer so a preview and an export loop
|
|
8
|
+
* the same samples.
|
|
9
|
+
*/
|
|
10
|
+
export function loopRegionOf(request, bufferDuration) {
|
|
11
|
+
if (!request.loop)
|
|
12
|
+
return null;
|
|
13
|
+
const start = Math.min(Math.max(0, request.trimStart), bufferDuration);
|
|
14
|
+
const end = Math.min(request.trimEnd ?? bufferDuration, bufferDuration);
|
|
15
|
+
return end > start ? { start, end } : null;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Configure `source` to play `request`'s loop: on or off, and over the trimmed
|
|
19
|
+
* region rather than the whole buffer (`loopStart`/`loopEnd` default to the
|
|
20
|
+
* whole buffer, which is how a trim used to be lost).
|
|
21
|
+
*/
|
|
22
|
+
export function applyLoop(source, request) {
|
|
23
|
+
source.loop = request.loop;
|
|
24
|
+
const region = source.buffer ? loopRegionOf(request, source.buffer.duration) : null;
|
|
25
|
+
if (region) {
|
|
26
|
+
source.loopStart = region.start;
|
|
27
|
+
source.loopEnd = region.end;
|
|
28
|
+
}
|
|
29
|
+
return region;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Fold a source offset into a loop region.
|
|
33
|
+
*
|
|
34
|
+
* A seek into the middle of a looping clip lands at `trimStart + elapsed`, which
|
|
35
|
+
* can be many passes past the region's end; the device would not wrap an offset
|
|
36
|
+
* that starts outside the region, so it is wrapped here.
|
|
37
|
+
*/
|
|
38
|
+
export function wrapIntoRegion(region, offset) {
|
|
39
|
+
if (!region || offset < region.end)
|
|
40
|
+
return offset;
|
|
41
|
+
return region.start + ((offset - region.start) % (region.end - region.start));
|
|
42
|
+
}
|
|
43
|
+
//# sourceMappingURL=loop-region.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"loop-region.js","sourceRoot":"","sources":["../../src/audio/loop-region.ts"],"names":[],"mappings":"AAQA;;;;;;;;GAQG;AACH,MAAM,UAAU,YAAY,CAAC,OAAqB,EAAE,cAAsB;IACtE,IAAI,CAAC,OAAO,CAAC,IAAI;QAAE,OAAO,IAAI,CAAC;IAC/B,MAAM,KAAK,GAAG,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,OAAO,CAAC,SAAS,CAAC,EAAE,cAAc,CAAC,CAAC;IACvE,MAAM,GAAG,GAAG,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC,OAAO,IAAI,cAAc,EAAE,cAAc,CAAC,CAAC;IACxE,OAAO,GAAG,GAAG,KAAK,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,GAAG,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC;AAC/C,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,SAAS,CAAC,MAA6B,EAAE,OAAqB;IAC1E,MAAM,CAAC,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC;IAC3B,MAAM,MAAM,GAAG,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,YAAY,CAAC,OAAO,EAAE,MAAM,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC;IACpF,IAAI,MAAM,EAAE,CAAC;QACT,MAAM,CAAC,SAAS,GAAG,MAAM,CAAC,KAAK,CAAC;QAChC,MAAM,CAAC,OAAO,GAAG,MAAM,CAAC,GAAG,CAAC;IAChC,CAAC;IACD,OAAO,MAAM,CAAC;AAClB,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,cAAc,CAAC,MAAyB,EAAE,MAAc;IACpE,IAAI,CAAC,MAAM,IAAI,MAAM,GAAG,MAAM,CAAC,GAAG;QAAE,OAAO,MAAM,CAAC;IAClD,OAAO,MAAM,CAAC,KAAK,GAAG,CAAC,CAAC,MAAM,GAAG,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,MAAM,CAAC,GAAG,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC;AAClF,CAAC","sourcesContent":["import type { AudioRequest } from \"@motionscript/core\";\r\n\r\n/** The span of a decoded buffer a looping request repeats, in source seconds. */\r\nexport interface LoopRegion {\r\n start: number;\r\n end: number;\r\n}\r\n\r\n/**\r\n * The region a looping request repeats: from its in-point to its out-point, or to\r\n * the end of the buffer when it has none. `null` for a request that does not loop,\r\n * and for a degenerate region (an out-point at or before the in-point), where the\r\n * device falls back to wrapping the whole buffer.\r\n *\r\n * Shared by the live device and the offline mixer so a preview and an export loop\r\n * the same samples.\r\n */\r\nexport function loopRegionOf(request: AudioRequest, bufferDuration: number): LoopRegion | null {\r\n if (!request.loop) return null;\r\n const start = Math.min(Math.max(0, request.trimStart), bufferDuration);\r\n const end = Math.min(request.trimEnd ?? bufferDuration, bufferDuration);\r\n return end > start ? { start, end } : null;\r\n}\r\n\r\n/**\r\n * Configure `source` to play `request`'s loop: on or off, and over the trimmed\r\n * region rather than the whole buffer (`loopStart`/`loopEnd` default to the\r\n * whole buffer, which is how a trim used to be lost).\r\n */\r\nexport function applyLoop(source: AudioBufferSourceNode, request: AudioRequest): LoopRegion | null {\r\n source.loop = request.loop;\r\n const region = source.buffer ? loopRegionOf(request, source.buffer.duration) : null;\r\n if (region) {\r\n source.loopStart = region.start;\r\n source.loopEnd = region.end;\r\n }\r\n return region;\r\n}\r\n\r\n/**\r\n * Fold a source offset into a loop region.\r\n *\r\n * A seek into the middle of a looping clip lands at `trimStart + elapsed`, which\r\n * can be many passes past the region's end; the device would not wrap an offset\r\n * that starts outside the region, so it is wrapped here.\r\n */\r\nexport function wrapIntoRegion(region: LoopRegion | null, offset: number): number {\r\n if (!region || offset < region.end) return offset;\r\n return region.start + ((offset - region.start) % (region.end - region.start));\r\n}\r\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mixer.d.ts","sourceRoot":"","sources":["../../src/audio/mixer.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"mixer.d.ts","sourceRoot":"","sources":["../../src/audio/mixer.ts"],"names":[],"mappings":"AAAA,OAAO,EAA2B,KAAK,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAChF,OAAO,KAAK,EAAE,UAAU,EAAE,qBAAqB,EAAE,MAAM,kCAAkC,CAAC;AAW1F,yDAAyD;AACzD,eAAO,MAAM,mBAAmB,QAAQ,CAAC;AAEzC,MAAM,WAAW,eAAe;IAC5B,+CAA+C;IAC/C,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB;;;;OAIG;IACH,OAAO,CAAC,EAAE,YAAY,CAAC;CAC1B;AA4BD;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAsB,QAAQ,CAC1B,QAAQ,EAAE,SAAS,qBAAqB,EAAE,EAC1C,aAAa,EAAE,MAAM,EACrB,OAAO,GAAE,eAAoB,GAC9B,OAAO,CAAC,WAAW,GAAG,IAAI,CAAC,CA8F7B;AAED;;;;;;;;GAQG;AACH,qBAAa,aAAc,YAAW,UAAU,CAAC,WAAW,CAAC;IAErD,OAAO,CAAC,QAAQ,CAAC,UAAU;IAC3B,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAC;gBADR,UAAU,GAAE,MAA4B,EACxC,OAAO,CAAC,EAAE,YAAY,YAAA;IAG3C,GAAG,CACC,QAAQ,EAAE,SAAS,qBAAqB,EAAE,EAC1C,aAAa,EAAE,MAAM,GACtB,OAAO,CAAC,WAAW,GAAG,IAAI,CAAC;CAMjC;AAED;;;;;;;GAOG;AACH,wBAAgB,SAAS,CAAC,MAAM,EAAE,WAAW,GAAG,UAAU,CAAC,WAAW,CAAC,CA6CtE"}
|
package/dist/audio/mixer.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { fetchAssetBytes } from "../asset-bytes";
|
|
1
|
+
import { sourceTimeAtClipElapsed } from "@motionscript/core";
|
|
2
|
+
import { fetchAssetBytes, localAssetBytes } from "../asset-bytes";
|
|
3
3
|
import { AudioBusGraph } from "./bus-graph";
|
|
4
|
+
import { applyLoop } from "./loop-region";
|
|
4
5
|
import { applySpeedToPlaybackRate, buildAudioFilterGraph, effectiveSpeed, speedCurveOf, } from "./filter-graph";
|
|
5
6
|
/** Sample rate used when the caller doesn't pick one. */
|
|
6
7
|
export const DEFAULT_SAMPLE_RATE = 44100;
|
|
@@ -20,10 +21,16 @@ function audioUrl(src, catalog) {
|
|
|
20
21
|
}
|
|
21
22
|
}
|
|
22
23
|
}
|
|
23
|
-
/**
|
|
24
|
+
/**
|
|
25
|
+
* Reads and decodes an audio source against a scratch `OfflineAudioContext`
|
|
26
|
+
* (decoding requires a context but doesn't render through it). The host's byte
|
|
27
|
+
* source is asked first, as for every other asset read: a mix that went to the
|
|
28
|
+
* network would fail where the preview played, offline or for bytes not yet
|
|
29
|
+
* uploaded.
|
|
30
|
+
*/
|
|
24
31
|
async function fetchAudioBuffer(src, ctx) {
|
|
25
|
-
const
|
|
26
|
-
return ctx.decodeAudioData(arrayBuffer);
|
|
32
|
+
const blob = (await localAssetBytes(src)) ?? (await fetchAssetBytes(src));
|
|
33
|
+
return ctx.decodeAudioData(await blob.arrayBuffer());
|
|
27
34
|
}
|
|
28
35
|
/**
|
|
29
36
|
* Render every scheduled request into one timeline-length buffer.
|
|
@@ -53,9 +60,18 @@ export async function mixAudio(requests, totalDuration, options = {}) {
|
|
|
53
60
|
return null;
|
|
54
61
|
const uniqueSrcs = [...new Set(requests.map(r => r.request.src))];
|
|
55
62
|
const scratchCtx = new OfflineAudioContext(2, frames, sampleRate);
|
|
63
|
+
// A source only a picture's own sound wants may hold no audio at all, which
|
|
64
|
+
// is silence rather than a failed mix — see `AudioRequest.optional`.
|
|
65
|
+
const asked = new Set(requests.filter(r => !r.request.optional).map(r => r.request.src));
|
|
56
66
|
const decoded = new Map();
|
|
57
67
|
await Promise.all(uniqueSrcs.map(async (src) => {
|
|
58
|
-
|
|
68
|
+
try {
|
|
69
|
+
decoded.set(src, await fetchAudioBuffer(audioUrl(src, options.catalog), scratchCtx));
|
|
70
|
+
}
|
|
71
|
+
catch (cause) {
|
|
72
|
+
if (asked.has(src))
|
|
73
|
+
throw cause;
|
|
74
|
+
}
|
|
59
75
|
}));
|
|
60
76
|
const mixCtx = new OfflineAudioContext(2, frames, sampleRate);
|
|
61
77
|
// The same shared-chain rule the live device uses, for the same reason: an
|
|
@@ -74,7 +90,7 @@ export async function mixAudio(requests, totalDuration, options = {}) {
|
|
|
74
90
|
continue;
|
|
75
91
|
const source = mixCtx.createBufferSource();
|
|
76
92
|
source.buffer = srcBuffer;
|
|
77
|
-
source
|
|
93
|
+
applyLoop(source, req);
|
|
78
94
|
const scalarSpeed = effectiveSpeed(req.filters);
|
|
79
95
|
const rate = Number.isFinite(scalarSpeed) && scalarSpeed > 0 ? scalarSpeed : 1;
|
|
80
96
|
const speedCurve = speedCurveOf(req.filters);
|
|
@@ -86,7 +102,7 @@ export async function mixAudio(requests, totalDuration, options = {}) {
|
|
|
86
102
|
gain.gain.value = Number.isFinite(req.volume) ? req.volume : 1;
|
|
87
103
|
// The trimmed source length the filter and speed curves are authored over.
|
|
88
104
|
const sourceLength = Math.max(0, srcBuffer.duration - req.trimStart);
|
|
89
|
-
// The clip's own
|
|
105
|
+
// The clip's own clip-time length — what a curve resolves against.
|
|
90
106
|
// Deliberately *not* `playDuration`, which is that length truncated by the
|
|
91
107
|
// timeline end: resolving against the truncated value would squeeze the
|
|
92
108
|
// whole curve into the surviving part instead of cutting it off.
|
|
@@ -110,9 +126,9 @@ export async function mixAudio(requests, totalDuration, options = {}) {
|
|
|
110
126
|
}
|
|
111
127
|
// start()'s 3rd arg is a source-buffer duration. A constant rate scales it
|
|
112
128
|
// linearly, but under a speed curve the buffer consumed over a span of
|
|
113
|
-
//
|
|
129
|
+
// clip time is the integral of the rate, not a multiple of it.
|
|
114
130
|
const consumed = speedCurve
|
|
115
|
-
?
|
|
131
|
+
? sourceTimeAtClipElapsed(speedCurve, sourceLength, playDuration)
|
|
116
132
|
: playDuration * rate;
|
|
117
133
|
source.start(globalStart, req.trimStart, consumed);
|
|
118
134
|
for (const osc of graph.oscillators)
|
package/dist/audio/mixer.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mixer.js","sourceRoot":"","sources":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AE,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,QAAQ,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;YAC7D,IAAI,CAAC,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,MAAM,GAAG,MAAM,CAAC,CAAC,CAAC,MAAM,GAAG,MAAM,EAAE,IAAI,CAAC,CAAC;YAC5E,MAAM,IAAI,cAAc,CAAC;QAC7B,CAAC;IACL,CAAC;IAED,OAAO,IAAI,UAAU,CAAC,GAAG,CAAC,CAAC;AAC/B,CAAC","sourcesContent":["import { sourceTimeAtSceneElapsed, type AssetCatalog } from \"@motionscript/core\";\nimport type { AudioMixer, ScheduledAudioRequest } from \"@motionscript/skia-render/export\";\nimport { fetchAssetBytes } from \"../asset-bytes\";\nimport { AudioBusGraph } from \"./bus-graph\";\nimport {\n applySpeedToPlaybackRate,\n buildAudioFilterGraph,\n effectiveSpeed,\n speedCurveOf,\n} from \"./filter-graph\";\n\n/** Sample rate used when the caller doesn't pick one. */\nexport const DEFAULT_SAMPLE_RATE = 44100;\n\nexport interface MixAudioOptions {\n /** Defaults to {@link DEFAULT_SAMPLE_RATE}. */\n sampleRate?: number;\n /**\n * Resolves a request's `src` to the address its bytes live at, the same job\n * `WebStorageAdapter.assetUrl` does for the live paths. Omitted, a `src` is\n * fetched as written — right only while the host keys its manifest by URL.\n */\n catalog?: AssetCatalog;\n}\n\n/** Audio first, then video: a clip's own soundtrack is requested under the video's key. */\nfunction audioUrl(src: string, catalog: AssetCatalog | undefined): string {\n if (!catalog) return src;\n try {\n return catalog.getAudioMeta(src).src || src;\n } catch {\n try {\n return catalog.getVideoMeta(src).src || src;\n } catch {\n return src;\n }\n }\n}\n\n/** Fetches and decodes an audio source against a scratch `OfflineAudioContext` (decoding requires a context but doesn't render through it). */\nasync function fetchAudioBuffer(src: string, ctx: OfflineAudioContext): Promise<AudioBuffer> {\n const arrayBuffer = await (await fetchAssetBytes(src)).arrayBuffer();\n return ctx.decodeAudioData(arrayBuffer);\n}\n\n/**\n * Render every scheduled request into one timeline-length buffer.\n *\n * An `OfflineAudioContext` graph — gain per request, looping/trimming, and the\n * same filter chain the live {@link WebAudioSink} builds — rendered in one\n * pass rather than scheduled per frame.\n *\n * Shared by the video exporter and by `mixTimelineAudio`, deliberately: a split\n * export and a whole one have to be the same mix, and two implementations of\n * \"stack these clips\" would eventually disagree.\n *\n * Curve-valued filter params and `speed` curves schedule here exactly as they do\n * in {@link WebAudioSink.playBuffer}, which is the reference implementation —\n * the two must be read side by side when either changes.\n *\n * One deliberate difference: this schedules at absolute context times, so clip\n * starts are sample-accurate here and quantized to `1/fps` in live playback,\n * which starts sources at `currentTime` on a clock tick.\n */\nexport async function mixAudio(\n requests: readonly ScheduledAudioRequest[],\n totalDuration: number,\n options: MixAudioOptions = {},\n): Promise<AudioBuffer | null> {\n if (requests.length === 0) return null;\n const sampleRate = options.sampleRate ?? DEFAULT_SAMPLE_RATE;\n const frames = Math.ceil(totalDuration * sampleRate);\n if (frames <= 0) return null;\n\n const uniqueSrcs = [...new Set(requests.map(r => r.request.src))];\n const scratchCtx = new OfflineAudioContext(2, frames, sampleRate);\n\n const decoded = new Map<string, AudioBuffer>();\n await Promise.all(uniqueSrcs.map(async (src) => {\n decoded.set(src, await fetchAudioBuffer(audioUrl(src, options.catalog), scratchCtx));\n }));\n\n const mixCtx = new OfflineAudioContext(2, frames, sampleRate);\n // The same shared-chain rule the live device uses, for the same reason: an\n // export that flattened buses into per-clip processing would not match what\n // the preview sounded like.\n const buses = new AudioBusGraph(mixCtx, mixCtx.destination);\n\n for (const { request: req, globalOffset } of requests) {\n const srcBuffer = decoded.get(req.src);\n if (!srcBuffer) continue;\n\n const globalStart = globalOffset + req.startAt;\n // `endAt` is `Infinity` for an open clip; either way the timeline bounds it.\n const globalEnd = Math.min(globalOffset + req.endAt, totalDuration);\n const playDuration = globalEnd - globalStart;\n if (playDuration <= 0) continue;\n\n const source = mixCtx.createBufferSource();\n source.buffer = srcBuffer;\n source.loop = req.loop;\n\n const scalarSpeed = effectiveSpeed(req.filters);\n const rate = Number.isFinite(scalarSpeed) && scalarSpeed > 0 ? scalarSpeed : 1;\n const speedCurve = speedCurveOf(req.filters);\n // With a curve the scalar is folded into the scheduled ramp below, so\n // setting it here as well would apply it twice.\n if (rate !== 1 && !speedCurve) source.playbackRate.value = rate;\n\n const gain = mixCtx.createGain();\n gain.gain.value = Number.isFinite(req.volume) ? req.volume : 1;\n\n // The trimmed source length the filter and speed curves are authored over.\n const sourceLength = Math.max(0, srcBuffer.duration - req.trimStart);\n // The clip's own scene-time length — what a curve resolves against.\n // Deliberately *not* `playDuration`, which is that length truncated by the\n // timeline end: resolving against the truncated value would squeeze the\n // whole curve into the surviving part instead of cutting it off.\n const clipDuration = Number.isFinite(req.endAt)\n ? Math.max(0, req.endAt - req.startAt)\n : sourceLength / rate;\n\n // Filter chain sits between the source and the per-request gain, matching\n // the live WebAudioSink graph so exports sound identical to preview.\n // Passing the start time and clip duration is what lets curve-valued params\n // schedule — with the defaults they collapse to a single static value.\n // `elapsed` is 0: unlike a live seek, the mix always starts a clip at its\n // own beginning.\n const graph = buildAudioFilterGraph(\n mixCtx, source, req.filters ?? [], globalStart, clipDuration, 0,\n );\n graph.output.connect(gain);\n // From the mix's start, whichever clip asks first: a bus's chain is shared,\n // so its schedule cannot begin at one clip's start. Context time 0 is\n // `-globalOffset` in this request's seconds, which are its stops' too.\n gain.connect(buses.inputFor(req.bus, 0, -globalOffset));\n\n if (speedCurve) {\n applySpeedToPlaybackRate(\n source.playbackRate, speedCurve, sourceLength, clipDuration, rate, globalStart, 0,\n );\n }\n\n // start()'s 3rd arg is a source-buffer duration. A constant rate scales it\n // linearly, but under a speed curve the buffer consumed over a span of\n // scene time is the integral of the rate, not a multiple of it.\n const consumed = speedCurve\n ? sourceTimeAtSceneElapsed(speedCurve, sourceLength, playDuration)\n : playDuration * rate;\n\n source.start(globalStart, req.trimStart, consumed);\n for (const osc of graph.oscillators) osc.start(globalStart);\n }\n\n return mixCtx.startRendering();\n}\n\n/**\n * Adapter presenting {@link mixAudio} as the exporter's injected mixer seam.\n *\n * The sample rate is a constructor argument rather than a third `mix` parameter\n * because {@link AudioMixer} — the seam `renderTimeline` calls through — takes\n * exactly two, so anything passed as a third would be silently dropped. Fixing\n * it here rather than widening the interface keeps the rate a property of *this*\n * mixer, which is whose business it is.\n */\nexport class WebAudioMixer implements AudioMixer<AudioBuffer> {\n constructor(\n private readonly sampleRate: number = DEFAULT_SAMPLE_RATE,\n private readonly catalog?: AssetCatalog,\n ) { }\n\n mix(\n requests: readonly ScheduledAudioRequest[],\n totalDuration: number,\n ): Promise<AudioBuffer | null> {\n return mixAudio(requests, totalDuration, {\n sampleRate: this.sampleRate,\n catalog: this.catalog,\n });\n }\n}\n\n/**\n * Encode an `AudioBuffer` as a 16-bit PCM WAV.\n *\n * The only other encoder in this package is the AAC track inside the mp4 muxer,\n * which is reachable solely by exporting a video — so without this a mixdown has\n * no file representation at all. WAV because it needs no codec support and is\n * lossless; a host wanting mp3/aac can re-encode from here.\n */\nexport function encodeWav(buffer: AudioBuffer): Uint8Array<ArrayBuffer> {\n const channelCount = buffer.numberOfChannels;\n const frameCount = buffer.length;\n const bytesPerSample = 2;\n const blockAlign = channelCount * bytesPerSample;\n const dataBytes = frameCount * blockAlign;\n\n const out = new ArrayBuffer(44 + dataBytes);\n const view = new DataView(out);\n\n const ascii = (offset: number, text: string) => {\n for (let i = 0; i < text.length; i++) view.setUint8(offset + i, text.charCodeAt(i));\n };\n\n ascii(0, \"RIFF\");\n view.setUint32(4, 36 + dataBytes, true);\n ascii(8, \"WAVE\");\n ascii(12, \"fmt \");\n view.setUint32(16, 16, true); // PCM header length\n view.setUint16(20, 1, true); // format: PCM\n view.setUint16(22, channelCount, true);\n view.setUint32(24, buffer.sampleRate, true);\n view.setUint32(28, buffer.sampleRate * blockAlign, true);\n view.setUint16(32, blockAlign, true);\n view.setUint16(34, 8 * bytesPerSample, true);\n ascii(36, \"data\");\n view.setUint32(40, dataBytes, true);\n\n // Pull each channel out once — getChannelData is a live view, but calling it\n // per sample inside the interleave loop is measurably slower.\n const channels: Float32Array[] = [];\n for (let c = 0; c < channelCount; c++) channels.push(buffer.getChannelData(c));\n\n let offset = 44;\n for (let frame = 0; frame < frameCount; frame++) {\n for (let c = 0; c < channelCount; c++) {\n // Clamp before scaling: the mix bus can exceed [-1,1] where clips\n // overlap, and letting that wrap turns a loud passage into noise.\n const sample = Math.max(-1, Math.min(1, channels[c][frame]));\n view.setInt16(offset, sample < 0 ? sample * 0x8000 : sample * 0x7fff, true);\n offset += bytesPerSample;\n }\n }\n\n return new Uint8Array(out);\n}\n"]}
|
|
1
|
+
{"version":3,"file":"mixer.js","sourceRoot":"","sources":["../../src/audio/mixer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,uBAAuB,EAAqB,MAAM,oBAAoB,CAAC;AAEhF,OAAO,EAAE,eAAe,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAC;AAClE,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAC5C,OAAO,EAAE,SAAS,EAAE,MAAM,eAAe,CAAC;AAC1C,OAAO,EACH,wBAAwB,EACxB,qBAAqB,EACrB,cAAc,EACd,YAAY,GACf,MAAM,gBAAgB,CAAC;AAExB,yDAAyD;AACzD,MAAM,CAAC,MAAM,mBAAmB,GAAG,KAAK,CAAC;AAazC,2FAA2F;AAC3F,SAAS,QAAQ,CAAC,GAAW,EAAE,OAAiC;IAC5D,IAAI,CAAC,OAAO;QAAE,OAAO,GAAG,CAAC;IACzB,IAAI,CAAC;QACD,OAAO,OAAO,CAAC,YAAY,CAAC,GAAG,CAAC,CAAC,GAAG,IAAI,GAAG,CAAC;IAChD,CAAC;IAAC,MAAM,CAAC;QACL,IAAI,CAAC;YACD,OAAO,OAAO,CAAC,YAAY,CAAC,GAAG,CAAC,CAAC,GAAG,IAAI,GAAG,CAAC;QACh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AAM,EAAE,IAAI,CAAC,CAAC;YAC5E,MAAM,IAAI,cAAc,CAAC;QAC7B,CAAC;IACL,CAAC;IAED,OAAO,IAAI,UAAU,CAAC,GAAG,CAAC,CAAC;AAC/B,CAAC","sourcesContent":["import { sourceTimeAtClipElapsed, type AssetCatalog } from \"@motionscript/core\";\r\nimport type { AudioMixer, ScheduledAudioRequest } from \"@motionscript/skia-render/export\";\r\nimport { fetchAssetBytes, localAssetBytes } from \"../asset-bytes\";\r\nimport { AudioBusGraph } from \"./bus-graph\";\r\nimport { applyLoop } from \"./loop-region\";\r\nimport {\r\n applySpeedToPlaybackRate,\r\n buildAudioFilterGraph,\r\n effectiveSpeed,\r\n speedCurveOf,\r\n} from \"./filter-graph\";\r\n\r\n/** Sample rate used when the caller doesn't pick one. */\r\nexport const DEFAULT_SAMPLE_RATE = 44100;\r\n\r\nexport interface MixAudioOptions {\r\n /** Defaults to {@link DEFAULT_SAMPLE_RATE}. */\r\n sampleRate?: number;\r\n /**\r\n * Resolves a request's `src` to the address its bytes live at, the same job\r\n * `WebStorageAdapter.assetUrl` does for the live paths. Omitted, a `src` is\r\n * fetched as written — right only while the host keys its manifest by URL.\r\n */\r\n catalog?: AssetCatalog;\r\n}\r\n\r\n/** Audio first, then video: a clip's own soundtrack is requested under the video's key. */\r\nfunction audioUrl(src: string, catalog: AssetCatalog | undefined): string {\r\n if (!catalog) return src;\r\n try {\r\n return catalog.getAudioMeta(src).src || src;\r\n } catch {\r\n try {\r\n return catalog.getVideoMeta(src).src || src;\r\n } catch {\r\n return src;\r\n }\r\n }\r\n}\r\n\r\n/**\r\n * Reads and decodes an audio source against a scratch `OfflineAudioContext`\r\n * (decoding requires a context but doesn't render through it). The host's byte\r\n * source is asked first, as for every other asset read: a mix that went to the\r\n * network would fail where the preview played, offline or for bytes not yet\r\n * uploaded.\r\n */\r\nasync function fetchAudioBuffer(src: string, ctx: OfflineAudioContext): Promise<AudioBuffer> {\r\n const blob = (await localAssetBytes(src)) ?? (await fetchAssetBytes(src));\r\n return ctx.decodeAudioData(await blob.arrayBuffer());\r\n}\r\n\r\n/**\r\n * Render every scheduled request into one timeline-length buffer.\r\n *\r\n * An `OfflineAudioContext` graph — gain per request, looping/trimming, and the\r\n * same filter chain the live {@link WebAudioSink} builds — rendered in one\r\n * pass rather than scheduled per frame.\r\n *\r\n * Shared by the video exporter and by `mixTimelineAudio`, deliberately: a split\r\n * export and a whole one have to be the same mix, and two implementations of\r\n * \"stack these clips\" would eventually disagree.\r\n *\r\n * Curve-valued filter params and `speed` curves schedule here exactly as they do\r\n * in {@link WebAudioSink.playBuffer}, which is the reference implementation —\r\n * the two must be read side by side when either changes.\r\n *\r\n * One deliberate difference: this schedules at absolute context times, so clip\r\n * starts are sample-accurate here and quantized to `1/fps` in live playback,\r\n * which starts sources at `currentTime` on a clock tick.\r\n */\r\nexport async function mixAudio(\r\n requests: readonly ScheduledAudioRequest[],\r\n totalDuration: number,\r\n options: MixAudioOptions = {},\r\n): Promise<AudioBuffer | null> {\r\n if (requests.length === 0) return null;\r\n const sampleRate = options.sampleRate ?? DEFAULT_SAMPLE_RATE;\r\n const frames = Math.ceil(totalDuration * sampleRate);\r\n if (frames <= 0) return null;\r\n\r\n const uniqueSrcs = [...new Set(requests.map(r => r.request.src))];\r\n const scratchCtx = new OfflineAudioContext(2, frames, sampleRate);\r\n\r\n // A source only a picture's own sound wants may hold no audio at all, which\r\n // is silence rather than a failed mix — see `AudioRequest.optional`.\r\n const asked = new Set(requests.filter(r => !r.request.optional).map(r => r.request.src));\r\n const decoded = new Map<string, AudioBuffer>();\r\n await Promise.all(uniqueSrcs.map(async (src) => {\r\n try {\r\n decoded.set(src, await fetchAudioBuffer(audioUrl(src, options.catalog), scratchCtx));\r\n } catch (cause) {\r\n if (asked.has(src)) throw cause;\r\n }\r\n }));\r\n\r\n const mixCtx = new OfflineAudioContext(2, frames, sampleRate);\r\n // The same shared-chain rule the live device uses, for the same reason: an\r\n // export that flattened buses into per-clip processing would not match what\r\n // the preview sounded like.\r\n const buses = new AudioBusGraph(mixCtx, mixCtx.destination);\r\n\r\n for (const { request: req, globalOffset } of requests) {\r\n const srcBuffer = decoded.get(req.src);\r\n if (!srcBuffer) continue;\r\n\r\n const globalStart = globalOffset + req.startAt;\r\n // `endAt` is `Infinity` for an open clip; either way the timeline bounds it.\r\n const globalEnd = Math.min(globalOffset + req.endAt, totalDuration);\r\n const playDuration = globalEnd - globalStart;\r\n if (playDuration <= 0) continue;\r\n\r\n const source = mixCtx.createBufferSource();\r\n source.buffer = srcBuffer;\r\n applyLoop(source, req);\r\n\r\n const scalarSpeed = effectiveSpeed(req.filters);\r\n const rate = Number.isFinite(scalarSpeed) && scalarSpeed > 0 ? scalarSpeed : 1;\r\n const speedCurve = speedCurveOf(req.filters);\r\n // With a curve the scalar is folded into the scheduled ramp below, so\r\n // setting it here as well would apply it twice.\r\n if (rate !== 1 && !speedCurve) source.playbackRate.value = rate;\r\n\r\n const gain = mixCtx.createGain();\r\n gain.gain.value = Number.isFinite(req.volume) ? req.volume : 1;\r\n\r\n // The trimmed source length the filter and speed curves are authored over.\r\n const sourceLength = Math.max(0, srcBuffer.duration - req.trimStart);\r\n // The clip's own clip-time length — what a curve resolves against.\r\n // Deliberately *not* `playDuration`, which is that length truncated by the\r\n // timeline end: resolving against the truncated value would squeeze the\r\n // whole curve into the surviving part instead of cutting it off.\r\n const clipDuration = Number.isFinite(req.endAt)\r\n ? Math.max(0, req.endAt - req.startAt)\r\n : sourceLength / rate;\r\n\r\n // Filter chain sits between the source and the per-request gain, matching\r\n // the live WebAudioSink graph so exports sound identical to preview.\r\n // Passing the start time and clip duration is what lets curve-valued params\r\n // schedule — with the defaults they collapse to a single static value.\r\n // `elapsed` is 0: unlike a live seek, the mix always starts a clip at its\r\n // own beginning.\r\n const graph = buildAudioFilterGraph(\r\n mixCtx, source, req.filters ?? [], globalStart, clipDuration, 0,\r\n );\r\n graph.output.connect(gain);\r\n // From the mix's start, whichever clip asks first: a bus's chain is shared,\r\n // so its schedule cannot begin at one clip's start. Context time 0 is\r\n // `-globalOffset` in this request's seconds, which are its stops' too.\r\n gain.connect(buses.inputFor(req.bus, 0, -globalOffset));\r\n\r\n if (speedCurve) {\r\n applySpeedToPlaybackRate(\r\n source.playbackRate, speedCurve, sourceLength, clipDuration, rate, globalStart, 0,\r\n );\r\n }\r\n\r\n // start()'s 3rd arg is a source-buffer duration. A constant rate scales it\r\n // linearly, but under a speed curve the buffer consumed over a span of\r\n // clip time is the integral of the rate, not a multiple of it.\r\n const consumed = speedCurve\r\n ? sourceTimeAtClipElapsed(speedCurve, sourceLength, playDuration)\r\n : playDuration * rate;\r\n\r\n source.start(globalStart, req.trimStart, consumed);\r\n for (const osc of graph.oscillators) osc.start(globalStart);\r\n }\r\n\r\n return mixCtx.startRendering();\r\n}\r\n\r\n/**\r\n * Adapter presenting {@link mixAudio} as the exporter's injected mixer seam.\r\n *\r\n * The sample rate is a constructor argument rather than a third `mix` parameter\r\n * because {@link AudioMixer} — the seam `renderTimeline` calls through — takes\r\n * exactly two, so anything passed as a third would be silently dropped. Fixing\r\n * it here rather than widening the interface keeps the rate a property of *this*\r\n * mixer, which is whose business it is.\r\n */\r\nexport class WebAudioMixer implements AudioMixer<AudioBuffer> {\r\n constructor(\r\n private readonly sampleRate: number = DEFAULT_SAMPLE_RATE,\r\n private readonly catalog?: AssetCatalog,\r\n ) { }\r\n\r\n mix(\r\n requests: readonly ScheduledAudioRequest[],\r\n totalDuration: number,\r\n ): Promise<AudioBuffer | null> {\r\n return mixAudio(requests, totalDuration, {\r\n sampleRate: this.sampleRate,\r\n catalog: this.catalog,\r\n });\r\n }\r\n}\r\n\r\n/**\r\n * Encode an `AudioBuffer` as a 16-bit PCM WAV.\r\n *\r\n * The only other encoder in this package is the AAC track inside the mp4 muxer,\r\n * which is reachable solely by exporting a video — so without this a mixdown has\r\n * no file representation at all. WAV because it needs no codec support and is\r\n * lossless; a host wanting mp3/aac can re-encode from here.\r\n */\r\nexport function encodeWav(buffer: AudioBuffer): Uint8Array<ArrayBuffer> {\r\n const channelCount = buffer.numberOfChannels;\r\n const frameCount = buffer.length;\r\n const bytesPerSample = 2;\r\n const blockAlign = channelCount * bytesPerSample;\r\n const dataBytes = frameCount * blockAlign;\r\n\r\n const out = new ArrayBuffer(44 + dataBytes);\r\n const view = new DataView(out);\r\n\r\n const ascii = (offset: number, text: string) => {\r\n for (let i = 0; i < text.length; i++) view.setUint8(offset + i, text.charCodeAt(i));\r\n };\r\n\r\n ascii(0, \"RIFF\");\r\n view.setUint32(4, 36 + dataBytes, true);\r\n ascii(8, \"WAVE\");\r\n ascii(12, \"fmt \");\r\n view.setUint32(16, 16, true); // PCM header length\r\n view.setUint16(20, 1, true); // format: PCM\r\n view.setUint16(22, channelCount, true);\r\n view.setUint32(24, buffer.sampleRate, true);\r\n view.setUint32(28, buffer.sampleRate * blockAlign, true);\r\n view.setUint16(32, blockAlign, true);\r\n view.setUint16(34, 8 * bytesPerSample, true);\r\n ascii(36, \"data\");\r\n view.setUint32(40, dataBytes, true);\r\n\r\n // Pull each channel out once — getChannelData is a live view, but calling it\r\n // per sample inside the interleave loop is measurably slower.\r\n const channels: Float32Array[] = [];\r\n for (let c = 0; c < channelCount; c++) channels.push(buffer.getChannelData(c));\r\n\r\n let offset = 44;\r\n for (let frame = 0; frame < frameCount; frame++) {\r\n for (let c = 0; c < channelCount; c++) {\r\n // Clamp before scaling: the mix bus can exceed [-1,1] where clips\r\n // overlap, and letting that wrap turns a loud passage into noise.\r\n const sample = Math.max(-1, Math.min(1, channels[c][frame]));\r\n view.setInt16(offset, sample < 0 ? sample * 0x8000 : sample * 0x7fff, true);\r\n offset += bytesPerSample;\r\n }\r\n }\r\n\r\n return new Uint8Array(out);\r\n}\r\n"]}
|
package/dist/audio/player.d.ts
CHANGED
|
@@ -4,7 +4,7 @@ import { AudioSink, AudioRequest } from "@motionscript/core";
|
|
|
4
4
|
* buffers, then schedules/plays {@link AudioRequest}s as `AudioBufferSourceNode`s
|
|
5
5
|
* through a shared master gain (global mute and volume). `schedule` registers the
|
|
6
6
|
* active set of requests for the current frame; `syncTo` then starts/stops
|
|
7
|
-
* sources to match `
|
|
7
|
+
* sources to match `clipTime`, so playback follows scrubbing and seeks.
|
|
8
8
|
*/
|
|
9
9
|
export declare class WebAudioSink extends AudioSink {
|
|
10
10
|
private context;
|
|
@@ -33,15 +33,15 @@ export declare class WebAudioSink extends AudioSink {
|
|
|
33
33
|
retain(keep: ReadonlySet<string>): void;
|
|
34
34
|
/** Replaces the active request set; stops any currently-playing source whose request is no longer scheduled. Actual start/stop-to-match-time happens in `syncTo`. */
|
|
35
35
|
schedule(requests: readonly AudioRequest[]): void;
|
|
36
|
-
/** Starts sources whose `[startAt, endAt)` window now contains `
|
|
37
|
-
syncTo(
|
|
36
|
+
/** Starts sources whose `[startAt, endAt)` window now contains `clipTime` (computing the correct buffer offset for the seek point) and stops any that have fallen outside it. */
|
|
37
|
+
syncTo(clipTime: number): void;
|
|
38
38
|
play(time: number, _speed: number, _reverse: boolean): Promise<void>;
|
|
39
39
|
stop(): void;
|
|
40
40
|
setMuted(muted: boolean): void;
|
|
41
41
|
setVolume(volume: number): void;
|
|
42
42
|
/** Mute and volume are held separately so unmuting restores the set level. */
|
|
43
43
|
private applyMasterGain;
|
|
44
|
-
/** Starts a buffer source mid-clip if `
|
|
44
|
+
/** Starts a buffer source mid-clip if `clipTime` lands after the request's start — `audioOffset` accounts for the request's trim, the elapsed time since `startAt`, and any speed (playbackRate) change. */
|
|
45
45
|
private playBuffer;
|
|
46
46
|
private stopSource;
|
|
47
47
|
dispose(): void;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"player.d.ts","sourceRoot":"","sources":["../../src/audio/player.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,YAAY,
|
|
1
|
+
{"version":3,"file":"player.d.ts","sourceRoot":"","sources":["../../src/audio/player.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAA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;IAqElB,OAAO,CAAC,UAAU;IAclB,OAAO,IAAI,IAAI;CAiBlB"}
|
package/dist/audio/player.js
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
|
-
import { AudioSink,
|
|
1
|
+
import { AudioSink, sourceTimeAtClipElapsed } from "@motionscript/core";
|
|
2
2
|
import { AudioBusGraph } from "./bus-graph";
|
|
3
|
+
import { applyLoop, wrapIntoRegion } from "./loop-region";
|
|
3
4
|
import { buildAudioFilterGraph, effectiveSpeed, speedCurveOf, applySpeedToPlaybackRate } from "./filter-graph";
|
|
4
5
|
/**
|
|
5
6
|
* Web Audio implementation of {@link AudioSink} — decodes and caches source
|
|
6
7
|
* buffers, then schedules/plays {@link AudioRequest}s as `AudioBufferSourceNode`s
|
|
7
8
|
* through a shared master gain (global mute and volume). `schedule` registers the
|
|
8
9
|
* active set of requests for the current frame; `syncTo` then starts/stops
|
|
9
|
-
* sources to match `
|
|
10
|
+
* sources to match `clipTime`, so playback follows scrubbing and seeks.
|
|
10
11
|
*/
|
|
11
12
|
export class WebAudioSink extends AudioSink {
|
|
12
13
|
context;
|
|
@@ -83,20 +84,20 @@ export class WebAudioSink extends AudioSink {
|
|
|
83
84
|
}
|
|
84
85
|
this.scheduled = requests.slice();
|
|
85
86
|
}
|
|
86
|
-
/** Starts sources whose `[startAt, endAt)` window now contains `
|
|
87
|
-
syncTo(
|
|
87
|
+
/** Starts sources whose `[startAt, endAt)` window now contains `clipTime` (computing the correct buffer offset for the seek point) and stops any that have fallen outside it. */
|
|
88
|
+
syncTo(clipTime) {
|
|
88
89
|
const liveIds = new Set();
|
|
89
90
|
for (const req of this.scheduled) {
|
|
90
91
|
// `endAt` is `Infinity` for an open clip, so the comparison needs no
|
|
91
92
|
// nullish fallback — see AudioRequest.endAt.
|
|
92
|
-
const isInWindow =
|
|
93
|
+
const isInWindow = clipTime >= req.startAt && clipTime < req.endAt;
|
|
93
94
|
if (!isInWindow)
|
|
94
95
|
continue;
|
|
95
96
|
liveIds.add(req.id);
|
|
96
97
|
if (!this.active.has(req.id)) {
|
|
97
98
|
const buffer = this.bufferCache.get(req.src);
|
|
98
99
|
if (buffer)
|
|
99
|
-
this.playBuffer(buffer, req,
|
|
100
|
+
this.playBuffer(buffer, req, clipTime);
|
|
100
101
|
}
|
|
101
102
|
}
|
|
102
103
|
for (const [id, active] of this.active) {
|
|
@@ -130,8 +131,8 @@ export class WebAudioSink extends AudioSink {
|
|
|
130
131
|
applyMasterGain() {
|
|
131
132
|
this.masterGain.gain.value = this.muted ? 0 : this.volume;
|
|
132
133
|
}
|
|
133
|
-
/** Starts a buffer source mid-clip if `
|
|
134
|
-
playBuffer(buffer, req,
|
|
134
|
+
/** Starts a buffer source mid-clip if `clipTime` lands after the request's start — `audioOffset` accounts for the request's trim, the elapsed time since `startAt`, and any speed (playbackRate) change. */
|
|
135
|
+
playBuffer(buffer, req, clipTime) {
|
|
135
136
|
const startTime = this.context.currentTime;
|
|
136
137
|
const gainNode = this.context.createGain();
|
|
137
138
|
// Guard against a missing/NaN volume — a non-finite AudioParam value throws.
|
|
@@ -139,19 +140,19 @@ export class WebAudioSink extends AudioSink {
|
|
|
139
140
|
// Into this clip's bus chain, or straight to the master when it has none.
|
|
140
141
|
// The chain is shared: two clips on one bus reach the same nodes, which is
|
|
141
142
|
// what makes a bus effect one tail rather than one per clip.
|
|
142
|
-
gainNode.connect(this.buses.inputFor(req.bus, startTime,
|
|
143
|
+
gainNode.connect(this.buses.inputFor(req.bus, startTime, clipTime));
|
|
143
144
|
const source = this.context.createBufferSource();
|
|
144
145
|
source.buffer = buffer;
|
|
145
|
-
|
|
146
|
+
const loopRegion = applyLoop(source, req);
|
|
146
147
|
const scalarSpeed = effectiveSpeed(req.filters);
|
|
147
148
|
const rate = Number.isFinite(scalarSpeed) && scalarSpeed > 0 ? scalarSpeed : 1;
|
|
148
149
|
const speedCurve = speedCurveOf(req.filters);
|
|
149
150
|
if (rate !== 1 && !speedCurve)
|
|
150
151
|
source.playbackRate.value = rate;
|
|
151
|
-
const elapsed = Math.max(0,
|
|
152
|
+
const elapsed = Math.max(0, clipTime - req.startAt);
|
|
152
153
|
// Trimmed source length the filter curves / speed curve are authored over.
|
|
153
154
|
const sourceLength = Math.max(0, buffer.duration - req.trimStart);
|
|
154
|
-
//
|
|
155
|
+
// Clip-time length of the clip. Prefer the bounded [startAt, endAt) span; if
|
|
155
156
|
// endAt is unbounded (an open/looping clip reaching the device unresolved),
|
|
156
157
|
// fall back to the source length scaled by speed so curve params still resolve
|
|
157
158
|
// (a clipDuration of 0 would otherwise silently drop every fade/sweep).
|
|
@@ -167,14 +168,14 @@ export class WebAudioSink extends AudioSink {
|
|
|
167
168
|
// speed curve uses the integral inverse to find the true source position.
|
|
168
169
|
let audioOffset;
|
|
169
170
|
if (speedCurve) {
|
|
170
|
-
audioOffset = req.trimStart +
|
|
171
|
+
audioOffset = req.trimStart + sourceTimeAtClipElapsed(speedCurve, sourceLength, elapsed);
|
|
171
172
|
// Schedule the time-varying playbackRate (folds in any scalar speed too).
|
|
172
173
|
applySpeedToPlaybackRate(source.playbackRate, speedCurve, sourceLength, clipDuration, rate, startTime, elapsed);
|
|
173
174
|
}
|
|
174
175
|
else {
|
|
175
176
|
audioOffset = req.trimStart + elapsed * rate;
|
|
176
177
|
}
|
|
177
|
-
source.start(startTime, audioOffset);
|
|
178
|
+
source.start(startTime, wrapIntoRegion(loopRegion, audioOffset));
|
|
178
179
|
for (const osc of graph.oscillators)
|
|
179
180
|
osc.start(startTime);
|
|
180
181
|
source.onended = () => {
|
package/dist/audio/player.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"player.js","sourceRoot":"","sources":["../../src/audio/player.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAgB,wBAAwB,EAAE,MAAM,oBAAoB,CAAC;AACvF,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAC5C,OAAO,EAAE,qBAAqB,EAAE,cAAc,EAAE,YAA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,CAAC;QACjC,CAAC;QAAC,MAAM,CAAC;YACL,uBAAuB;QAC3B,CAAC;QACD,IAAI,IAAI,CAAC,WAAW,IAAI,IAAI,CAAC,OAAO,CAAC,KAAK,KAAK,QAAQ,EAAE,CAAC;YACtD,IAAI,CAAC,OAAO,CAAC,KAAK,EAAE,CAAC;QACzB,CAAC;IACL,CAAC;CACJ","sourcesContent":["import { AudioSink, AudioRequest, sourceTimeAtSceneElapsed } from \"@motionscript/core\";\nimport { AudioBusGraph } from \"./bus-graph\";\nimport { buildAudioFilterGraph, effectiveSpeed, speedCurveOf, applySpeedToPlaybackRate } from \"./filter-graph\";\n\ntype ActiveSource = {\n source: AudioBufferSourceNode;\n gainNode: GainNode;\n request: AudioRequest;\n /** Filter-graph nodes to disconnect on teardown. */\n filterNodes: AudioNode[];\n /** Tremolo LFOs to stop on teardown. */\n oscillators: OscillatorNode[];\n};\n\n/**\n * Web Audio implementation of {@link AudioSink} — decodes and caches source\n * buffers, then schedules/plays {@link AudioRequest}s as `AudioBufferSourceNode`s\n * through a shared master gain (global mute and volume). `schedule` registers the\n * active set of requests for the current frame; `syncTo` then starts/stops\n * sources to match `sceneTime`, so playback follows scrubbing and seeks.\n */\nexport class WebAudioSink extends AudioSink {\n private context: AudioContext;\n private ownsContext: boolean;\n private bufferCache = new Map<string, AudioBuffer>();\n private decoding = new Map<string, Promise<void>>();\n private active = new Map<string, ActiveSource>();\n private scheduled: AudioRequest[] = [];\n private disposed: boolean = false;\n private masterGain: GainNode;\n /** The live bus chains, shared across every clip that names one. */\n private buses: AudioBusGraph;\n private muted = false;\n private volume = 1;\n\n constructor(context?: AudioContext) {\n super();\n this.context = context ?? new AudioContext();\n this.ownsContext = !context;\n this.masterGain = this.context.createGain();\n this.masterGain.connect(this.context.destination);\n this.buses = new AudioBusGraph(this.context, this.masterGain);\n }\n\n getContext(): AudioContext {\n return this.context;\n }\n\n /**\n * Resume the AudioContext. Must be called from within a user gesture\n * (e.g. a click handler) the first time, or browsers keep the context\n * suspended and no audio is heard.\n */\n unlock(): void {\n if (this.context.state === \"suspended\") {\n this.context.resume();\n }\n }\n\n has(src: string): boolean {\n return this.bufferCache.has(src);\n }\n\n async append(src: string, data: ArrayBuffer): Promise<void> {\n if (this.bufferCache.has(src)) return;\n const existing = this.decoding.get(src);\n if (existing) return existing;\n\n // decodeAudioData detaches `data` — that's fine, the caller in\n // AssetManager.fetchAudio discards its reference immediately after\n // awaiting append. Skipping the defensive .slice(0) saves a multi-MB\n // allocation per audio source.\n const job = this.context.decodeAudioData(data).then((buffer) => {\n this.bufferCache.set(src, buffer);\n }).finally(() => {\n this.decoding.delete(src);\n });\n this.decoding.set(src, job);\n await job;\n }\n\n /** Drops cached buffers and stops active sources for srcs/requests no longer referenced — called when the asset set changes (e.g. seeking across scene boundaries). */\n retain(keep: ReadonlySet<string>): void {\n for (const src of [...this.bufferCache.keys()]) {\n if (!keep.has(src)) this.bufferCache.delete(src);\n }\n for (const [id, active] of this.active) {\n if (!keep.has(active.request.src)) this.stopSource(id, active);\n }\n }\n\n /** Replaces the active request set; stops any currently-playing source whose request is no longer scheduled. Actual start/stop-to-match-time happens in `syncTo`. */\n schedule(requests: readonly AudioRequest[]): void {\n const next = new Set(requests.map(r => r.id));\n for (const [id, active] of this.active) {\n if (!next.has(id)) this.stopSource(id, active);\n }\n this.scheduled = requests.slice();\n }\n\n /** Starts sources whose `[startAt, endAt)` window now contains `sceneTime` (computing the correct buffer offset for the seek point) and stops any that have fallen outside it. */\n syncTo(sceneTime: number): void {\n const liveIds = new Set<string>();\n\n for (const req of this.scheduled) {\n // `endAt` is `Infinity` for an open clip, so the comparison needs no\n // nullish fallback — see AudioRequest.endAt.\n const isInWindow = sceneTime >= req.startAt && sceneTime < req.endAt;\n if (!isInWindow) continue;\n\n liveIds.add(req.id);\n if (!this.active.has(req.id)) {\n const buffer = this.bufferCache.get(req.src);\n if (buffer) this.playBuffer(buffer, req, sceneTime);\n }\n }\n\n for (const [id, active] of this.active) {\n if (!liveIds.has(id)) this.stopSource(id, active);\n }\n }\n\n async play(time: number, _speed: number, _reverse: boolean): Promise<void> {\n if (this.context.state === \"suspended\") {\n await this.context.resume();\n }\n this.syncTo(time);\n }\n\n stop(): void {\n for (const [id, active] of this.active) {\n this.stopSource(id, active);\n }\n // A bus's curves are scheduled against the clock of the run that built\n // it, so the next play, from wherever it lands, builds them again.\n this.buses.dispose();\n }\n\n setMuted(muted: boolean): void {\n this.muted = muted;\n this.applyMasterGain();\n }\n\n setVolume(volume: number): void {\n this.volume = Math.max(0, volume);\n this.applyMasterGain();\n }\n\n /** Mute and volume are held separately so unmuting restores the set level. */\n private applyMasterGain(): void {\n this.masterGain.gain.value = this.muted ? 0 : this.volume;\n }\n\n /** Starts a buffer source mid-clip if `sceneTime` lands after the request's start — `audioOffset` accounts for the request's trim, the elapsed time since `startAt`, and any speed (playbackRate) change. */\n private playBuffer(buffer: AudioBuffer, req: AudioRequest, sceneTime: number): void {\n const startTime = this.context.currentTime;\n const gainNode = this.context.createGain();\n // Guard against a missing/NaN volume — a non-finite AudioParam value throws.\n gainNode.gain.value = Number.isFinite(req.volume) ? req.volume : 1;\n // Into this clip's bus chain, or straight to the master when it has none.\n // The chain is shared: two clips on one bus reach the same nodes, which is\n // what makes a bus effect one tail rather than one per clip.\n gainNode.connect(this.buses.inputFor(req.bus, startTime, sceneTime));\n\n const source = this.context.createBufferSource();\n source.buffer = buffer;\n source.loop = req.loop;\n\n const scalarSpeed = effectiveSpeed(req.filters);\n const rate = Number.isFinite(scalarSpeed) && scalarSpeed > 0 ? scalarSpeed : 1;\n const speedCurve = speedCurveOf(req.filters);\n if (rate !== 1 && !speedCurve) source.playbackRate.value = rate;\n\n const elapsed = Math.max(0, sceneTime - req.startAt);\n // Trimmed source length the filter curves / speed curve are authored over.\n const sourceLength = Math.max(0, buffer.duration - req.trimStart);\n // Scene-time length of the clip. Prefer the bounded [startAt, endAt) span; if\n // endAt is unbounded (an open/looping clip reaching the device unresolved),\n // fall back to the source length scaled by speed so curve params still resolve\n // (a clipDuration of 0 would otherwise silently drop every fade/sweep).\n const clipDuration = isFinite(req.endAt)\n ? Math.max(0, req.endAt - req.startAt)\n : sourceLength / (rate || 1);\n\n // Insert the filter chain between the source and the per-request gain. Pass\n // the start time, clip duration, and elapsed so curve-valued params schedule\n // and align to a mid-clip seek.\n const graph = buildAudioFilterGraph(\n this.context, source, req.filters ?? [], startTime, clipDuration, elapsed,\n );\n graph.output.connect(gainNode);\n\n // Source-time offset for a mid-clip seek. Scalar speed scales linearly; a\n // speed curve uses the integral inverse to find the true source position.\n let audioOffset: number;\n if (speedCurve) {\n audioOffset = req.trimStart + sourceTimeAtSceneElapsed(speedCurve, sourceLength, elapsed);\n // Schedule the time-varying playbackRate (folds in any scalar speed too).\n applySpeedToPlaybackRate(\n source.playbackRate, speedCurve, sourceLength, clipDuration, rate, startTime, elapsed,\n );\n } else {\n audioOffset = req.trimStart + elapsed * rate;\n }\n\n source.start(startTime, audioOffset);\n for (const osc of graph.oscillators) osc.start(startTime);\n\n source.onended = () => {\n if (this.active.get(req.id)?.source === source) {\n this.active.delete(req.id);\n }\n };\n\n this.active.set(req.id, {\n source,\n gainNode,\n request: req,\n filterNodes: graph.nodes,\n oscillators: graph.oscillators,\n });\n }\n\n private stopSource(id: string, active: ActiveSource): void {\n try {\n active.source.onended = null;\n active.source.stop();\n for (const osc of active.oscillators) osc.stop();\n active.source.disconnect();\n for (const node of active.filterNodes) node.disconnect();\n active.gainNode.disconnect();\n } catch {\n // already stopped\n }\n this.active.delete(id);\n }\n\n dispose(): void {\n if (this.disposed) return;\n this.disposed = true;\n super.dispose();\n this.bufferCache.clear();\n this.decoding.clear();\n this.scheduled.length = 0;\n this.buses.dispose();\n try {\n this.masterGain.disconnect();\n } catch {\n // already disconnected\n }\n if (this.ownsContext && this.context.state !== \"closed\") {\n this.context.close();\n }\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"player.js","sourceRoot":"","sources":["../../src/audio/player.ts"],"names":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CAAC,OAAO,EAAE,CAAC;QAChB,IAAI,CAAC,WAAW,CAAC,KAAK,EAAE,CAAC;QACzB,IAAI,CAAC,QAAQ,CAAC,KAAK,EAAE,CAAC;QACtB,IAAI,CAAC,SAAS,CAAC,MAAM,GAAG,CAAC,CAAC;QAC1B,IAAI,CAAC,KAAK,CAAC,OAAO,EAAE,CAAC;QACrB,IAAI,CAAC;YACD,IAAI,CAAC,UAAU,CAAC,UAAU,EAAE,CAAC;QACjC,CAAC;QAAC,MAAM,CAAC;YACL,uBAAuB;QAC3B,CAAC;QACD,IAAI,IAAI,CAAC,WAAW,IAAI,IAAI,CAAC,OAAO,CAAC,KAAK,KAAK,QAAQ,EAAE,CAAC;YACtD,IAAI,CAAC,OAAO,CAAC,KAAK,EAAE,CAAC;QACzB,CAAC;IACL,CAAC;CACJ","sourcesContent":["import { AudioSink, AudioRequest, sourceTimeAtClipElapsed } from \"@motionscript/core\";\r\nimport { AudioBusGraph } from \"./bus-graph\";\r\nimport { applyLoop, wrapIntoRegion } from \"./loop-region\";\r\nimport { buildAudioFilterGraph, effectiveSpeed, speedCurveOf, applySpeedToPlaybackRate } from \"./filter-graph\";\r\n\r\ntype ActiveSource = {\r\n source: AudioBufferSourceNode;\r\n gainNode: GainNode;\r\n request: AudioRequest;\r\n /** Filter-graph nodes to disconnect on teardown. */\r\n filterNodes: AudioNode[];\r\n /** Tremolo LFOs to stop on teardown. */\r\n oscillators: OscillatorNode[];\r\n};\r\n\r\n/**\r\n * Web Audio implementation of {@link AudioSink} — decodes and caches source\r\n * buffers, then schedules/plays {@link AudioRequest}s as `AudioBufferSourceNode`s\r\n * through a shared master gain (global mute and volume). `schedule` registers the\r\n * active set of requests for the current frame; `syncTo` then starts/stops\r\n * sources to match `clipTime`, so playback follows scrubbing and seeks.\r\n */\r\nexport class WebAudioSink extends AudioSink {\r\n private context: AudioContext;\r\n private ownsContext: boolean;\r\n private bufferCache = new Map<string, AudioBuffer>();\r\n private decoding = new Map<string, Promise<void>>();\r\n private active = new Map<string, ActiveSource>();\r\n private scheduled: AudioRequest[] = [];\r\n private disposed: boolean = false;\r\n private masterGain: GainNode;\r\n /** The live bus chains, shared across every clip that names one. */\r\n private buses: AudioBusGraph;\r\n private muted = false;\r\n private volume = 1;\r\n\r\n constructor(context?: AudioContext) {\r\n super();\r\n this.context = context ?? new AudioContext();\r\n this.ownsContext = !context;\r\n this.masterGain = this.context.createGain();\r\n this.masterGain.connect(this.context.destination);\r\n this.buses = new AudioBusGraph(this.context, this.masterGain);\r\n }\r\n\r\n getContext(): AudioContext {\r\n return this.context;\r\n }\r\n\r\n /**\r\n * Resume the AudioContext. Must be called from within a user gesture\r\n * (e.g. a click handler) the first time, or browsers keep the context\r\n * suspended and no audio is heard.\r\n */\r\n unlock(): void {\r\n if (this.context.state === \"suspended\") {\r\n this.context.resume();\r\n }\r\n }\r\n\r\n has(src: string): boolean {\r\n return this.bufferCache.has(src);\r\n }\r\n\r\n async append(src: string, data: ArrayBuffer): Promise<void> {\r\n if (this.bufferCache.has(src)) return;\r\n const existing = this.decoding.get(src);\r\n if (existing) return existing;\r\n\r\n // decodeAudioData detaches `data` — that's fine, the caller in\r\n // AssetManager.fetchAudio discards its reference immediately after\r\n // awaiting append. Skipping the defensive .slice(0) saves a multi-MB\r\n // allocation per audio source.\r\n const job = this.context.decodeAudioData(data).then((buffer) => {\r\n this.bufferCache.set(src, buffer);\r\n }).finally(() => {\r\n this.decoding.delete(src);\r\n });\r\n this.decoding.set(src, job);\r\n await job;\r\n }\r\n\r\n /** Drops cached buffers and stops active sources for srcs/requests no longer referenced — called when the asset set changes (e.g. seeking across scene boundaries). */\r\n retain(keep: ReadonlySet<string>): void {\r\n for (const src of [...this.bufferCache.keys()]) {\r\n if (!keep.has(src)) this.bufferCache.delete(src);\r\n }\r\n for (const [id, active] of this.active) {\r\n if (!keep.has(active.request.src)) this.stopSource(id, active);\r\n }\r\n }\r\n\r\n /** Replaces the active request set; stops any currently-playing source whose request is no longer scheduled. Actual start/stop-to-match-time happens in `syncTo`. */\r\n schedule(requests: readonly AudioRequest[]): void {\r\n const next = new Set(requests.map(r => r.id));\r\n for (const [id, active] of this.active) {\r\n if (!next.has(id)) this.stopSource(id, active);\r\n }\r\n this.scheduled = requests.slice();\r\n }\r\n\r\n /** Starts sources whose `[startAt, endAt)` window now contains `clipTime` (computing the correct buffer offset for the seek point) and stops any that have fallen outside it. */\r\n syncTo(clipTime: number): void {\r\n const liveIds = new Set<string>();\r\n\r\n for (const req of this.scheduled) {\r\n // `endAt` is `Infinity` for an open clip, so the comparison needs no\r\n // nullish fallback — see AudioRequest.endAt.\r\n const isInWindow = clipTime >= req.startAt && clipTime < req.endAt;\r\n if (!isInWindow) continue;\r\n\r\n liveIds.add(req.id);\r\n if (!this.active.has(req.id)) {\r\n const buffer = this.bufferCache.get(req.src);\r\n if (buffer) this.playBuffer(buffer, req, clipTime);\r\n }\r\n }\r\n\r\n for (const [id, active] of this.active) {\r\n if (!liveIds.has(id)) this.stopSource(id, active);\r\n }\r\n }\r\n\r\n async play(time: number, _speed: number, _reverse: boolean): Promise<void> {\r\n if (this.context.state === \"suspended\") {\r\n await this.context.resume();\r\n }\r\n this.syncTo(time);\r\n }\r\n\r\n stop(): void {\r\n for (const [id, active] of this.active) {\r\n this.stopSource(id, active);\r\n }\r\n // A bus's curves are scheduled against the clock of the run that built\r\n // it, so the next play, from wherever it lands, builds them again.\r\n this.buses.dispose();\r\n }\r\n\r\n setMuted(muted: boolean): void {\r\n this.muted = muted;\r\n this.applyMasterGain();\r\n }\r\n\r\n setVolume(volume: number): void {\r\n this.volume = Math.max(0, volume);\r\n this.applyMasterGain();\r\n }\r\n\r\n /** Mute and volume are held separately so unmuting restores the set level. */\r\n private applyMasterGain(): void {\r\n this.masterGain.gain.value = this.muted ? 0 : this.volume;\r\n }\r\n\r\n /** Starts a buffer source mid-clip if `clipTime` lands after the request's start — `audioOffset` accounts for the request's trim, the elapsed time since `startAt`, and any speed (playbackRate) change. */\r\n private playBuffer(buffer: AudioBuffer, req: AudioRequest, clipTime: number): void {\r\n const startTime = this.context.currentTime;\r\n const gainNode = this.context.createGain();\r\n // Guard against a missing/NaN volume — a non-finite AudioParam value throws.\r\n gainNode.gain.value = Number.isFinite(req.volume) ? req.volume : 1;\r\n // Into this clip's bus chain, or straight to the master when it has none.\r\n // The chain is shared: two clips on one bus reach the same nodes, which is\r\n // what makes a bus effect one tail rather than one per clip.\r\n gainNode.connect(this.buses.inputFor(req.bus, startTime, clipTime));\r\n\r\n const source = this.context.createBufferSource();\r\n source.buffer = buffer;\r\n const loopRegion = applyLoop(source, req);\r\n\r\n const scalarSpeed = effectiveSpeed(req.filters);\r\n const rate = Number.isFinite(scalarSpeed) && scalarSpeed > 0 ? scalarSpeed : 1;\r\n const speedCurve = speedCurveOf(req.filters);\r\n if (rate !== 1 && !speedCurve) source.playbackRate.value = rate;\r\n\r\n const elapsed = Math.max(0, clipTime - req.startAt);\r\n // Trimmed source length the filter curves / speed curve are authored over.\r\n const sourceLength = Math.max(0, buffer.duration - req.trimStart);\r\n // Clip-time length of the clip. Prefer the bounded [startAt, endAt) span; if\r\n // endAt is unbounded (an open/looping clip reaching the device unresolved),\r\n // fall back to the source length scaled by speed so curve params still resolve\r\n // (a clipDuration of 0 would otherwise silently drop every fade/sweep).\r\n const clipDuration = isFinite(req.endAt)\r\n ? Math.max(0, req.endAt - req.startAt)\r\n : sourceLength / (rate || 1);\r\n\r\n // Insert the filter chain between the source and the per-request gain. Pass\r\n // the start time, clip duration, and elapsed so curve-valued params schedule\r\n // and align to a mid-clip seek.\r\n const graph = buildAudioFilterGraph(\r\n this.context, source, req.filters ?? [], startTime, clipDuration, elapsed,\r\n );\r\n graph.output.connect(gainNode);\r\n\r\n // Source-time offset for a mid-clip seek. Scalar speed scales linearly; a\r\n // speed curve uses the integral inverse to find the true source position.\r\n let audioOffset: number;\r\n if (speedCurve) {\r\n audioOffset = req.trimStart + sourceTimeAtClipElapsed(speedCurve, sourceLength, elapsed);\r\n // Schedule the time-varying playbackRate (folds in any scalar speed too).\r\n applySpeedToPlaybackRate(\r\n source.playbackRate, speedCurve, sourceLength, clipDuration, rate, startTime, elapsed,\r\n );\r\n } else {\r\n audioOffset = req.trimStart + elapsed * rate;\r\n }\r\n\r\n source.start(startTime, wrapIntoRegion(loopRegion, audioOffset));\r\n for (const osc of graph.oscillators) osc.start(startTime);\r\n\r\n source.onended = () => {\r\n if (this.active.get(req.id)?.source === source) {\r\n this.active.delete(req.id);\r\n }\r\n };\r\n\r\n this.active.set(req.id, {\r\n source,\r\n gainNode,\r\n request: req,\r\n filterNodes: graph.nodes,\r\n oscillators: graph.oscillators,\r\n });\r\n }\r\n\r\n private stopSource(id: string, active: ActiveSource): void {\r\n try {\r\n active.source.onended = null;\r\n active.source.stop();\r\n for (const osc of active.oscillators) osc.stop();\r\n active.source.disconnect();\r\n for (const node of active.filterNodes) node.disconnect();\r\n active.gainNode.disconnect();\r\n } catch {\r\n // already stopped\r\n }\r\n this.active.delete(id);\r\n }\r\n\r\n dispose(): void {\r\n if (this.disposed) return;\r\n this.disposed = true;\r\n super.dispose();\r\n this.bufferCache.clear();\r\n this.decoding.clear();\r\n this.scheduled.length = 0;\r\n this.buses.dispose();\r\n try {\r\n this.masterGain.disconnect();\r\n } catch {\r\n // already disconnected\r\n }\r\n if (this.ownsContext && this.context.state !== \"closed\") {\r\n this.context.close();\r\n }\r\n }\r\n}\r\n"]}
|