@motionscript/browser 0.1.0-alpha.7 → 0.1.0-alpha.8
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 +6 -0
- package/NOTICE +15 -14
- package/README.md +66 -43
- package/dist/asset-bytes.d.ts +7 -4
- package/dist/asset-bytes.d.ts.map +1 -1
- package/dist/asset-bytes.js +9 -4
- package/dist/asset-bytes.js.map +1 -1
- package/dist/audio/bus-graph.d.ts.map +1 -1
- package/dist/audio/bus-graph.js +15 -6
- package/dist/audio/bus-graph.js.map +1 -1
- package/dist/audio/effect-graph.d.ts +62 -0
- package/dist/audio/effect-graph.d.ts.map +1 -0
- package/dist/audio/effect-graph.js +234 -0
- package/dist/audio/effect-graph.js.map +1 -0
- package/dist/audio/index.d.ts.map +1 -1
- package/dist/audio/index.js +2 -3
- package/dist/audio/index.js.map +1 -1
- package/dist/audio/mixer.d.ts +4 -4
- package/dist/audio/mixer.d.ts.map +1 -1
- package/dist/audio/mixer.js +18 -38
- package/dist/audio/mixer.js.map +1 -1
- package/dist/audio/player.d.ts +12 -6
- package/dist/audio/player.d.ts.map +1 -1
- package/dist/audio/player.js +49 -62
- package/dist/audio/player.js.map +1 -1
- package/dist/browser/THIRD_PARTY_LICENSES.txt +379 -27
- package/dist/browser/audio.js +1 -1
- package/dist/browser/chunks/chunk-IILVN4SK.js +2 -0
- package/dist/browser/chunks/chunk-IILVN4SK.js.map +7 -0
- package/dist/browser/chunks/chunk-KLXMRKTL.js +2 -0
- package/dist/browser/chunks/chunk-L5UFQACH.js +163 -0
- package/dist/browser/chunks/chunk-L5UFQACH.js.map +7 -0
- package/dist/browser/chunks/mediabunny-mp3-encoder-ZCIHVVTJ.js +14 -0
- package/dist/browser/chunks/mediabunny-mp3-encoder-ZCIHVVTJ.js.map +7 -0
- package/dist/browser/index.js +51 -1343
- package/dist/browser/index.js.map +4 -4
- package/dist/browser/manifest.json +8 -8
- package/dist/browser/ms_renderer_bg.wasm +0 -0
- package/dist/canvaskit-renderer.d.ts +8 -0
- package/dist/canvaskit-renderer.d.ts.map +1 -0
- package/dist/canvaskit-renderer.js +23 -0
- package/dist/canvaskit-renderer.js.map +1 -0
- package/dist/concat.d.ts +33 -7
- package/dist/concat.d.ts.map +1 -1
- package/dist/concat.js +117 -72
- package/dist/concat.js.map +1 -1
- package/dist/deliver.d.ts +10 -0
- package/dist/deliver.d.ts.map +1 -0
- package/dist/deliver.js +22 -0
- package/dist/deliver.js.map +1 -0
- package/dist/dynamic-nodes.d.ts.map +1 -1
- package/dist/dynamic-nodes.js +2 -3
- package/dist/dynamic-nodes.js.map +1 -1
- package/dist/engine.d.ts +37 -9
- package/dist/engine.d.ts.map +1 -1
- package/dist/engine.js +41 -32
- package/dist/engine.js.map +1 -1
- package/dist/export-audio.d.ts +36 -0
- package/dist/export-audio.d.ts.map +1 -0
- package/dist/export-audio.js +56 -0
- package/dist/export-audio.js.map +1 -0
- package/dist/exporter.d.ts +125 -14
- package/dist/exporter.d.ts.map +1 -1
- package/dist/exporter.js +133 -89
- package/dist/exporter.js.map +1 -1
- package/dist/formats.d.ts +36 -0
- package/dist/formats.d.ts.map +1 -0
- package/dist/formats.js +83 -0
- package/dist/formats.js.map +1 -0
- package/dist/frame-loop.d.ts +190 -0
- package/dist/frame-loop.d.ts.map +1 -0
- package/dist/frame-loop.js +304 -0
- package/dist/frame-loop.js.map +1 -0
- package/dist/getter.d.ts +4 -1
- package/dist/getter.d.ts.map +1 -1
- package/dist/getter.js +7 -6
- package/dist/getter.js.map +1 -1
- package/dist/index.d.ts +12 -11
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +17 -21
- package/dist/index.js.map +1 -1
- package/dist/mix-timeline-audio.d.ts +5 -5
- package/dist/mix-timeline-audio.d.ts.map +1 -1
- package/dist/mix-timeline-audio.js +10 -12
- package/dist/mix-timeline-audio.js.map +1 -1
- package/dist/parallel/export-parallel.d.ts +32 -0
- package/dist/parallel/export-parallel.d.ts.map +1 -0
- package/dist/parallel/export-parallel.js +114 -0
- package/dist/parallel/export-parallel.js.map +1 -0
- package/dist/parallel/governor.d.ts +57 -0
- package/dist/parallel/governor.d.ts.map +1 -0
- package/dist/parallel/governor.js +79 -0
- package/dist/parallel/governor.js.map +1 -0
- package/dist/parallel/lane.d.ts +45 -0
- package/dist/parallel/lane.d.ts.map +1 -0
- package/dist/parallel/lane.js +2 -0
- package/dist/parallel/lane.js.map +1 -0
- package/dist/parallel/lanes.d.ts +20 -0
- package/dist/parallel/lanes.d.ts.map +1 -0
- package/dist/parallel/lanes.js +152 -0
- package/dist/parallel/lanes.js.map +1 -0
- package/dist/parallel/plan.d.ts +23 -0
- package/dist/parallel/plan.d.ts.map +1 -0
- package/dist/parallel/plan.js +30 -0
- package/dist/parallel/plan.js.map +1 -0
- package/dist/parallel/run-lanes.d.ts +38 -0
- package/dist/parallel/run-lanes.d.ts.map +1 -0
- package/dist/parallel/run-lanes.js +164 -0
- package/dist/parallel/run-lanes.js.map +1 -0
- package/dist/parallel/session.d.ts +9 -0
- package/dist/parallel/session.d.ts.map +1 -0
- package/dist/parallel/session.js +79 -0
- package/dist/parallel/session.js.map +1 -0
- package/dist/render-context.d.ts +2 -0
- package/dist/render-context.d.ts.map +1 -1
- package/dist/render-context.js +8 -4
- package/dist/render-context.js.map +1 -1
- package/dist/renderer.d.ts +22 -0
- package/dist/renderer.d.ts.map +1 -0
- package/dist/renderer.js +23 -0
- package/dist/renderer.js.map +1 -0
- package/dist/screenshot.d.ts +4 -4
- package/dist/screenshot.d.ts.map +1 -1
- package/dist/screenshot.js +3 -3
- package/dist/screenshot.js.map +1 -1
- package/dist/still.d.ts +17 -19
- package/dist/still.d.ts.map +1 -1
- package/dist/still.js +27 -34
- package/dist/still.js.map +1 -1
- package/dist/ticker.d.ts +22 -8
- package/dist/ticker.d.ts.map +1 -1
- package/dist/ticker.js +55 -20
- package/dist/ticker.js.map +1 -1
- package/package.json +9 -10
- package/dist/browser/chunks/GLTFLoader-PGXD4V5B.js +0 -2
- package/dist/browser/chunks/GLTFLoader-PGXD4V5B.js.map +0 -7
- package/dist/browser/chunks/SkeletonUtils-XYDMTGTM.js +0 -2
- package/dist/browser/chunks/chunk-2MAFJWBZ.js +0 -2
- package/dist/browser/chunks/chunk-2MAFJWBZ.js.map +0 -7
- package/dist/browser/chunks/chunk-CTLBXZSU.js +0 -4119
- package/dist/browser/chunks/chunk-CTLBXZSU.js.map +0 -7
- package/dist/browser/chunks/chunk-FQ32PDIU.js +0 -2
- package/dist/browser/chunks/chunk-FQ32PDIU.js.map +0 -7
- package/dist/browser/chunks/chunk-FY3BTKY4.js +0 -2
- package/dist/browser/chunks/chunk-FY3BTKY4.js.map +0 -7
- package/dist/browser/chunks/three.module-Z4EOVUWF.js +0 -2
- package/dist/browser/chunks/three.module-Z4EOVUWF.js.map +0 -7
- /package/dist/browser/chunks/{SkeletonUtils-XYDMTGTM.js.map → chunk-KLXMRKTL.js.map} +0 -0
package/dist/audio/player.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { AudioSink
|
|
1
|
+
import { AudioSink } from "@motionscript/core";
|
|
2
2
|
import { AudioBusGraph } from "./bus-graph";
|
|
3
3
|
import { applyLoop, wrapIntoRegion } from "./loop-region";
|
|
4
|
-
import {
|
|
4
|
+
import { applySpeedToPlaybackRate, buildAudioEffectGraph, buildLevel, playbackOf, sourceOffsetOf } from "./effect-graph";
|
|
5
5
|
/**
|
|
6
6
|
* Web Audio implementation of {@link AudioSink} — decodes and caches source
|
|
7
7
|
* buffers, then schedules/plays {@link AudioRequest}s as `AudioBufferSourceNode`s
|
|
@@ -10,28 +10,41 @@ import { buildAudioFilterGraph, effectiveSpeed, speedCurveOf, applySpeedToPlayba
|
|
|
10
10
|
* sources to match `clipTime`, so playback follows scrubbing and seeks.
|
|
11
11
|
*/
|
|
12
12
|
export class WebAudioSink extends AudioSink {
|
|
13
|
-
|
|
14
|
-
|
|
13
|
+
borrowed;
|
|
14
|
+
_context = null;
|
|
15
15
|
bufferCache = new Map();
|
|
16
16
|
decoding = new Map();
|
|
17
17
|
active = new Map();
|
|
18
18
|
scheduled = [];
|
|
19
19
|
disposed = false;
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
buses;
|
|
20
|
+
/** The master gain and the live bus chains (shared across every clip that names one), built with the context. */
|
|
21
|
+
_graph = null;
|
|
23
22
|
muted = false;
|
|
24
23
|
volume = 1;
|
|
25
24
|
constructor(context) {
|
|
26
25
|
super();
|
|
27
|
-
this.
|
|
28
|
-
this.ownsContext = !context;
|
|
29
|
-
this.masterGain = this.context.createGain();
|
|
30
|
-
this.masterGain.connect(this.context.destination);
|
|
31
|
-
this.buses = new AudioBusGraph(this.context, this.masterGain);
|
|
26
|
+
this.borrowed = context;
|
|
32
27
|
}
|
|
28
|
+
/**
|
|
29
|
+
* The context, created on first use rather than with the sink: a new
|
|
30
|
+
* AudioContext opens the audio device (50–65 ms on a desktop), and a
|
|
31
|
+
* session that never plays or decodes sound never needs one.
|
|
32
|
+
*/
|
|
33
33
|
getContext() {
|
|
34
|
-
|
|
34
|
+
this._context ??= this.borrowed ?? new AudioContext();
|
|
35
|
+
return this._context;
|
|
36
|
+
}
|
|
37
|
+
get context() {
|
|
38
|
+
return this.getContext();
|
|
39
|
+
}
|
|
40
|
+
get graph() {
|
|
41
|
+
if (!this._graph) {
|
|
42
|
+
const master = this.context.createGain();
|
|
43
|
+
master.connect(this.context.destination);
|
|
44
|
+
this._graph = { master, buses: new AudioBusGraph(this.context, master) };
|
|
45
|
+
this.applyMasterGain();
|
|
46
|
+
}
|
|
47
|
+
return this._graph;
|
|
35
48
|
}
|
|
36
49
|
/**
|
|
37
50
|
* Resume the AudioContext. Must be called from within a user gesture
|
|
@@ -117,7 +130,7 @@ export class WebAudioSink extends AudioSink {
|
|
|
117
130
|
}
|
|
118
131
|
// A bus's curves are scheduled against the clock of the run that built
|
|
119
132
|
// it, so the next play, from wherever it lands, builds them again.
|
|
120
|
-
this.buses.dispose();
|
|
133
|
+
this._graph?.buses.dispose();
|
|
121
134
|
}
|
|
122
135
|
setMuted(muted) {
|
|
123
136
|
this.muted = muted;
|
|
@@ -129,52 +142,27 @@ export class WebAudioSink extends AudioSink {
|
|
|
129
142
|
}
|
|
130
143
|
/** Mute and volume are held separately so unmuting restores the set level. */
|
|
131
144
|
applyMasterGain() {
|
|
132
|
-
|
|
145
|
+
if (this._graph)
|
|
146
|
+
this._graph.master.gain.value = this.muted ? 0 : this.volume;
|
|
133
147
|
}
|
|
134
|
-
/** Starts a buffer source mid-clip if `clipTime` lands after the request's start —
|
|
148
|
+
/** Starts a buffer source mid-clip if `clipTime` lands after the request's start — the offset accounts for the request's trim, the elapsed time since `startAt`, and its speed. */
|
|
135
149
|
playBuffer(buffer, req, clipTime) {
|
|
136
150
|
const startTime = this.context.currentTime;
|
|
137
|
-
const
|
|
138
|
-
|
|
139
|
-
gainNode.gain.value = Number.isFinite(req.volume) ? req.volume : 1;
|
|
140
|
-
// Into this clip's bus chain, or straight to the master when it has none.
|
|
141
|
-
// The chain is shared: two clips on one bus reach the same nodes, which is
|
|
142
|
-
// what makes a bus effect one tail rather than one per clip.
|
|
143
|
-
gainNode.connect(this.buses.inputFor(req.bus, startTime, clipTime));
|
|
151
|
+
const playback = playbackOf(req, buffer.duration);
|
|
152
|
+
const elapsed = Math.max(0, clipTime - req.startAt);
|
|
144
153
|
const source = this.context.createBufferSource();
|
|
145
154
|
source.buffer = buffer;
|
|
146
155
|
const loopRegion = applyLoop(source, req);
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
//
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
// fall back to the source length scaled by speed so curve params still resolve
|
|
158
|
-
// (a clipDuration of 0 would otherwise silently drop every fade/sweep).
|
|
159
|
-
const clipDuration = isFinite(req.endAt)
|
|
160
|
-
? Math.max(0, req.endAt - req.startAt)
|
|
161
|
-
: sourceLength / (rate || 1);
|
|
162
|
-
// Insert the filter chain between the source and the per-request gain. Pass
|
|
163
|
-
// the start time, clip duration, and elapsed so curve-valued params schedule
|
|
164
|
-
// and align to a mid-clip seek.
|
|
165
|
-
const graph = buildAudioFilterGraph(this.context, source, req.filters ?? [], startTime, clipDuration, elapsed);
|
|
166
|
-
graph.output.connect(gainNode);
|
|
167
|
-
// Source-time offset for a mid-clip seek. Scalar speed scales linearly; a
|
|
168
|
-
// speed curve uses the integral inverse to find the true source position.
|
|
169
|
-
let audioOffset;
|
|
170
|
-
if (speedCurve) {
|
|
171
|
-
audioOffset = req.trimStart + sourceTimeAtClipElapsed(speedCurve, sourceLength, elapsed);
|
|
172
|
-
// Schedule the time-varying playbackRate (folds in any scalar speed too).
|
|
173
|
-
applySpeedToPlaybackRate(source.playbackRate, speedCurve, sourceLength, clipDuration, rate, startTime, elapsed);
|
|
174
|
-
}
|
|
175
|
-
else {
|
|
176
|
-
audioOffset = req.trimStart + elapsed * rate;
|
|
177
|
-
}
|
|
156
|
+
if (playback.rate !== 1)
|
|
157
|
+
source.playbackRate.value = playback.rate;
|
|
158
|
+
applySpeedToPlaybackRate(source.playbackRate, playback.speedRamp, playback.sourceLength, playback.clipDuration, startTime, elapsed);
|
|
159
|
+
const graph = buildAudioEffectGraph(this.context, source, req.effects ?? [], startTime, playback.clipDuration, elapsed);
|
|
160
|
+
const level = buildLevel(this.context, graph.output, req, playback, startTime, elapsed);
|
|
161
|
+
// Into this clip's bus chain, or straight to the master when it has none.
|
|
162
|
+
// The chain is shared: two clips on one bus reach the same nodes, which is
|
|
163
|
+
// what makes a bus effect one tail rather than one per clip.
|
|
164
|
+
level.output.connect(this.graph.buses.inputFor(req.bus, startTime, clipTime));
|
|
165
|
+
const audioOffset = req.trimStart + sourceOffsetOf(playback, elapsed);
|
|
178
166
|
source.start(startTime, wrapIntoRegion(loopRegion, audioOffset));
|
|
179
167
|
for (const osc of graph.oscillators)
|
|
180
168
|
osc.start(startTime);
|
|
@@ -185,9 +173,8 @@ export class WebAudioSink extends AudioSink {
|
|
|
185
173
|
};
|
|
186
174
|
this.active.set(req.id, {
|
|
187
175
|
source,
|
|
188
|
-
gainNode,
|
|
189
176
|
request: req,
|
|
190
|
-
|
|
177
|
+
nodes: [...graph.nodes, ...level.nodes],
|
|
191
178
|
oscillators: graph.oscillators,
|
|
192
179
|
});
|
|
193
180
|
}
|
|
@@ -198,9 +185,8 @@ export class WebAudioSink extends AudioSink {
|
|
|
198
185
|
for (const osc of active.oscillators)
|
|
199
186
|
osc.stop();
|
|
200
187
|
active.source.disconnect();
|
|
201
|
-
for (const node of active.
|
|
188
|
+
for (const node of active.nodes)
|
|
202
189
|
node.disconnect();
|
|
203
|
-
active.gainNode.disconnect();
|
|
204
190
|
}
|
|
205
191
|
catch {
|
|
206
192
|
// already stopped
|
|
@@ -215,15 +201,16 @@ export class WebAudioSink extends AudioSink {
|
|
|
215
201
|
this.bufferCache.clear();
|
|
216
202
|
this.decoding.clear();
|
|
217
203
|
this.scheduled.length = 0;
|
|
218
|
-
this.buses.dispose();
|
|
204
|
+
this._graph?.buses.dispose();
|
|
219
205
|
try {
|
|
220
|
-
this.
|
|
206
|
+
this._graph?.master.disconnect();
|
|
221
207
|
}
|
|
222
208
|
catch {
|
|
223
209
|
// already disconnected
|
|
224
210
|
}
|
|
225
|
-
|
|
226
|
-
|
|
211
|
+
const owned = this.borrowed ? null : this._context;
|
|
212
|
+
if (owned && owned.state !== "closed") {
|
|
213
|
+
owned.close();
|
|
227
214
|
}
|
|
228
215
|
}
|
|
229
216
|
}
|
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,uBAAuB,EAAE,MAAM,oBAAoB,CAAC;AACtF,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAC5C,OAA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AAC,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"]}
|
|
1
|
+
{"version":3,"file":"player.js","sourceRoot":"","sources":["../../src/audio/player.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAgB,MAAM,oBAAoB,CAAC;AAC7D,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAC5C,OAAO,EAAE,SAAS,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAC1D,OAAO,EAAE,wBAAwB,EAAE,qBAAqB,EAAE,UAAU,EAAE,UAAU,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAC;AAWzH;;;;;;GAMG;AACH,MAAM,OAAO,YAAa,SAAQ,SAAS;IACtB,QAAQ,CAA2B;IAC5C,QAAQ,GAAwB,IAAI,CAAC;IACrC,WAAW,GAAG,IAAI,GAAG,E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C,MAAM,CAAC,EAAE,CAAC,CAAC;IAC3B,CAAC;IAED,OAAO;QACH,IAAI,IAAI,CAAC,QAAQ;YAAE,OAAO;QAC1B,IAAI,CAAC,QAAQ,GAAG,IAAI,CAAC;QACrB,KAAK,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,MAAM,EAAE,KAAK,CAAC,OAAO,EAAE,CAAC;QAC7B,IAAI,CAAC;YACD,IAAI,CAAC,MAAM,EAAE,MAAM,CAAC,UAAU,EAAE,CAAC;QACrC,CAAC;QAAC,MAAM,CAAC;YACL,uBAAuB;QAC3B,CAAC;QACD,MAAM,KAAK,GAAG,IAAI,CAAC,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,QAAQ,CAAC;QACnD,IAAI,KAAK,IAAI,KAAK,CAAC,KAAK,KAAK,QAAQ,EAAE,CAAC;YACpC,KAAK,CAAC,KAAK,EAAE,CAAC;QAClB,CAAC;IACL,CAAC;CACJ","sourcesContent":["import { AudioSink, AudioRequest } from \"@motionscript/core\";\r\nimport { AudioBusGraph } from \"./bus-graph\";\r\nimport { applyLoop, wrapIntoRegion } from \"./loop-region\";\r\nimport { applySpeedToPlaybackRate, buildAudioEffectGraph, buildLevel, playbackOf, sourceOffsetOf } from \"./effect-graph\";\r\n\r\ntype ActiveSource = {\r\n source: AudioBufferSourceNode;\r\n request: AudioRequest;\r\n /** Effect and level nodes to disconnect on teardown. */\r\n nodes: 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 readonly borrowed: AudioContext | undefined;\r\n private _context: AudioContext | null = null;\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 /** The master gain and the live bus chains (shared across every clip that names one), built with the context. */\r\n private _graph: { master: GainNode; buses: AudioBusGraph } | null = null;\r\n private muted = false;\r\n private volume = 1;\r\n\r\n constructor(context?: AudioContext) {\r\n super();\r\n this.borrowed = context;\r\n }\r\n\r\n /**\r\n * The context, created on first use rather than with the sink: a new\r\n * AudioContext opens the audio device (50–65 ms on a desktop), and a\r\n * session that never plays or decodes sound never needs one.\r\n */\r\n getContext(): AudioContext {\r\n this._context ??= this.borrowed ?? new AudioContext();\r\n return this._context;\r\n }\r\n\r\n private get context(): AudioContext {\r\n return this.getContext();\r\n }\r\n\r\n private get graph(): { master: GainNode; buses: AudioBusGraph } {\r\n if (!this._graph) {\r\n const master = this.context.createGain();\r\n master.connect(this.context.destination);\r\n this._graph = { master, buses: new AudioBusGraph(this.context, master) };\r\n this.applyMasterGain();\r\n }\r\n return this._graph;\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._graph?.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 if (this._graph) this._graph.master.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 — the offset accounts for the request's trim, the elapsed time since `startAt`, and its speed. */\r\n private playBuffer(buffer: AudioBuffer, req: AudioRequest, clipTime: number): void {\r\n const startTime = this.context.currentTime;\r\n const playback = playbackOf(req, buffer.duration);\r\n const elapsed = Math.max(0, clipTime - req.startAt);\r\n\r\n const source = this.context.createBufferSource();\r\n source.buffer = buffer;\r\n const loopRegion = applyLoop(source, req);\r\n if (playback.rate !== 1) source.playbackRate.value = playback.rate;\r\n applySpeedToPlaybackRate(\r\n source.playbackRate, playback.speedRamp, playback.sourceLength, playback.clipDuration, startTime, elapsed,\r\n );\r\n\r\n const graph = buildAudioEffectGraph(\r\n this.context, source, req.effects ?? [], startTime, playback.clipDuration, elapsed,\r\n );\r\n const level = buildLevel(this.context, graph.output, req, playback, startTime, elapsed);\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 level.output.connect(this.graph.buses.inputFor(req.bus, startTime, clipTime));\r\n\r\n const audioOffset = req.trimStart + sourceOffsetOf(playback, elapsed);\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 request: req,\r\n nodes: [...graph.nodes, ...level.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.nodes) node.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._graph?.buses.dispose();\r\n try {\r\n this._graph?.master.disconnect();\r\n } catch {\r\n // already disconnected\r\n }\r\n const owned = this.borrowed ? null : this._context;\r\n if (owned && owned.state !== \"closed\") {\r\n owned.close();\r\n }\r\n }\r\n}\r\n"]}
|