@motionscript/browser 0.1.0-alpha.8 → 0.1.0-alpha.9
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/LICENSE +201 -201
- package/NOTICE +49 -49
- package/README.md +141 -141
- package/dist/asset-bytes.js.map +1 -1
- package/dist/audio/bus-graph.js.map +1 -1
- package/dist/audio/effect-graph.js.map +1 -1
- package/dist/audio/index.js.map +1 -1
- package/dist/audio/loop-region.js.map +1 -1
- package/dist/audio/mixer.js.map +1 -1
- package/dist/audio/player.js.map +1 -1
- package/dist/browser/audio.js +1 -1
- package/dist/browser/chunks/{chunk-IILVN4SK.js → chunk-F3AZKPHU.js} +1 -1
- package/dist/browser/chunks/chunk-F3AZKPHU.js.map +7 -0
- package/dist/browser/index.js +2 -2
- package/dist/browser/index.js.map +3 -3
- package/dist/browser/ms_renderer_bg.wasm +0 -0
- package/dist/concat.js.map +1 -1
- package/dist/dynamic-nodes.js.map +1 -1
- package/dist/engine.js.map +1 -1
- package/dist/export-audio.js.map +1 -1
- package/dist/exporter.js.map +1 -1
- package/dist/frame-loop.js.map +1 -1
- package/dist/index.js.map +1 -1
- package/dist/mix-timeline-audio.js.map +1 -1
- package/dist/parallel/export-parallel.js.map +1 -1
- package/dist/parallel/lane.js.map +1 -1
- package/dist/parallel/run-lanes.js.map +1 -1
- package/dist/parallel/session.js.map +1 -1
- package/dist/renderer.js.map +1 -1
- package/dist/screenshot.js.map +1 -1
- package/dist/still.js.map +1 -1
- package/dist/ticker.js.map +1 -1
- package/package.json +4 -4
- package/dist/audio/filter-graph.d.ts +0 -59
- package/dist/audio/filter-graph.d.ts.map +0 -1
- package/dist/audio/filter-graph.js +0 -241
- package/dist/audio/filter-graph.js.map +0 -1
- package/dist/browser/chunks/chunk-IILVN4SK.js.map +0 -7
- package/dist/canvaskit-renderer.d.ts +0 -8
- package/dist/canvaskit-renderer.d.ts.map +0 -1
- package/dist/canvaskit-renderer.js +0 -23
- package/dist/canvaskit-renderer.js.map +0 -1
- package/dist/decode/image-levels.d.ts +0 -137
- package/dist/decode/image-levels.d.ts.map +0 -1
- package/dist/decode/image-levels.js +0 -477
- package/dist/decode/image-levels.js.map +0 -1
- package/dist/decode/levels.d.ts +0 -37
- package/dist/decode/levels.d.ts.map +0 -1
- package/dist/decode/levels.js +0 -68
- package/dist/decode/levels.js.map +0 -1
- package/dist/decode/video-levels.d.ts +0 -37
- package/dist/decode/video-levels.d.ts.map +0 -1
- package/dist/decode/video-levels.js +0 -95
- package/dist/decode/video-levels.js.map +0 -1
- package/dist/getter.d.ts +0 -13
- package/dist/getter.d.ts.map +0 -1
- package/dist/getter.js +0 -39
- package/dist/getter.js.map +0 -1
- package/dist/measure-context.d.ts +0 -12
- package/dist/measure-context.d.ts.map +0 -1
- package/dist/measure-context.js +0 -12
- package/dist/measure-context.js.map +0 -1
- package/dist/render-context.d.ts +0 -34
- package/dist/render-context.d.ts.map +0 -1
- package/dist/render-context.js +0 -63
- package/dist/render-context.js.map +0 -1
- package/dist/storage-adapter.d.ts +0 -367
- package/dist/storage-adapter.d.ts.map +0 -1
- package/dist/storage-adapter.js +0 -1280
- package/dist/storage-adapter.js.map +0 -1
- package/dist/three/renderer.d.ts +0 -47
- package/dist/three/renderer.d.ts.map +0 -1
- package/dist/three/renderer.js +0 -182
- package/dist/three/renderer.js.map +0 -1
package/README.md
CHANGED
|
@@ -1,141 +1,141 @@
|
|
|
1
|
-
# @motionscript/browser
|
|
2
|
-
|
|
3
|
-
The browser platform for [Motion Script](https://motionscript.dev): it draws
|
|
4
|
-
every frame with WebGPU through [`@motionscript/renderer`](https://github.com/motionscript-dev/motionscript/tree/main/packages/renderer), which it
|
|
5
|
-
builds in. It implements the
|
|
6
|
-
`platform`/`render` abstractions from [`@motionscript/core`](https://github.com/motionscript-dev/motionscript/tree/main/packages/core) so
|
|
7
|
-
documents can be drawn to a canvas, played back with synchronized audio, and
|
|
8
|
-
exported to video all in the browser.
|
|
9
|
-
|
|
10
|
-
How a frame is drawn (shapes, fills, strokes, effects, text, 3D) lives in the
|
|
11
|
-
renderer (Rust/wgpu compiled to WebAssembly). This package supplies the browser's
|
|
12
|
-
half: the export frame loop, encoding, Web Audio, the clock and the entry points.
|
|
13
|
-
A browser without WebGPU cannot draw: every entry that draws rejects with
|
|
14
|
-
`WebGpuUnavailableError` (check `isWgpuSupported()` first to say so up front).
|
|
15
|
-
|
|
16
|
-
## What's in here
|
|
17
|
-
|
|
18
|
-
- **Engine**: `createBrowserEngine` wires every platform seam below into a
|
|
19
|
-
`@motionscript/core` `Engine`. Await it once at app start and share it.
|
|
20
|
-
- **Exporter**: `exportVideo` renders a document offline and muxes video and
|
|
21
|
-
audio into an MP4 (H.264 + AAC, the default) or a WebM (VP9 + Opus) using
|
|
22
|
-
[mediabunny](https://github.com/Vanilagy/mediabunny). `exportAudio` writes the
|
|
23
|
-
timeline's mixed audio alone as WAV (the default), MP3 or M4A without rendering
|
|
24
|
-
a frame, and throws `NoAudioError` when nothing plays. `EXPORT_FORMATS` gives
|
|
25
|
-
each format's kind, extension and MIME type. MP3 loads a WASM build of LAME on
|
|
26
|
-
first use, in a worker started from a `blob:` URL, so a host's CSP needs
|
|
27
|
-
`worker-src blob:` for it.
|
|
28
|
-
- **Stills**: `StillRenderer` (from `createStillRenderer`) is a long-lived
|
|
29
|
-
renderer that repaints single frames into a canvas the host owns, for
|
|
30
|
-
thumbnails and previews; `exportScreenshot` is the one-shot wrapper over it.
|
|
31
|
-
- **Concat**: `concatVideoSegments` joins separately encoded MP4 (or WebM)
|
|
32
|
-
segments into one file of the same container without re-encoding, so an export can be split across workers. A segment is
|
|
33
|
-
`exportVideo({ frames: { start, end } })` (or `session.export({ frames })`):
|
|
34
|
-
that range alone, timestamped from 0 and silent.
|
|
35
|
-
`mixTimelineAudio` mixes the whole timeline's audio once for the joined file.
|
|
36
|
-
Every segment must share one encoder configuration; a mismatch throws
|
|
37
|
-
`ConcatMismatchError`.
|
|
38
|
-
- **Parallel export**: `exportVideoParallel` takes `exportVideo`'s parameters
|
|
39
|
-
plus `lanes`, the renderers to spread the frames over: `localLane()` for
|
|
40
|
-
this page, and `messagePortLane(port)` for another realm (a second window or
|
|
41
|
-
process) that answers with `serveExportLane(port, resolve)`. It cuts the
|
|
42
|
-
timeline into key-frame-aligned frame ranges of the whole project, starts on
|
|
43
|
-
the first lane alone and admits the next only while that raises throughput
|
|
44
|
-
(every lane's encoder shares the browser's one GPU process, so light scenes
|
|
45
|
-
gain nothing from a second lane), mixes the audio once while the frames
|
|
46
|
-
render, joins each range as soon as the ones before it are done (into
|
|
47
|
-
`stream` when given) and reports progress by frames drawn.
|
|
48
|
-
`createSegmentJoiner` is the incremental join underneath.
|
|
49
|
-
- **Audio**: `WebAudioPlayer` drives playback through the Web Audio API;
|
|
50
|
-
`mixAudio` and `encodeWav` (also on the `@motionscript/browser/audio`
|
|
51
|
-
subpath, which skips the renderer) mix down offline.
|
|
52
|
-
- **Assets**: `setAssetByteSource` lets a host answer for an asset's bytes (a file
|
|
53
|
-
the user just chose, say) before the renderer's storage goes to the network; it
|
|
54
|
-
applies to previews, exports and stills alike. `loadAssetBytes` reads through it.
|
|
55
|
-
- **Source-string nodes**: `createBrowserEngine({ nodes })` also accepts TS source
|
|
56
|
-
strings (array form only), each default-exporting a component (`component2d({ key })`),
|
|
57
|
-
compiled with `esbuild-wasm` on first use. A script is written with calls: JSX
|
|
58
|
-
neither compiles nor links. `dynamicNodes` overrides the SDK, extra importable
|
|
59
|
-
`modules` or the compiler, each field defaulting on its own.
|
|
60
|
-
- **Clock**: `WebTicker` synchronizes scene playback to `requestAnimationFrame` while calculating dt from audio context to ensure audio and animation line up.
|
|
61
|
-
- **Renderer**: `isWgpuSupported`, `onWgpuEvent` (device loss and recovery), and
|
|
62
|
-
the WGSL shader tools `ShaderPreview`, `validateShader` and `WGSL_PRELUDE`, from
|
|
63
|
-
`@motionscript/renderer`.
|
|
64
|
-
|
|
65
|
-
## Usage
|
|
66
|
-
|
|
67
|
-
```bash
|
|
68
|
-
npm install @motionscript/browser @motionscript/core
|
|
69
|
-
```
|
|
70
|
-
|
|
71
|
-
```ts
|
|
72
|
-
import { createBrowserEngine } from '@motionscript/browser';
|
|
73
|
-
|
|
74
|
-
const engine = await createBrowserEngine({ nodes: [MyNode] });
|
|
75
|
-
const session = engine.open({ document, target: canvasEl });
|
|
76
|
-
session.playback!.play();
|
|
77
|
-
|
|
78
|
-
const mp4 = await session.export();
|
|
79
|
-
```
|
|
80
|
-
|
|
81
|
-
The renderer's `ms_renderer_bg.wasm` resolves beside its module, which a bundler
|
|
82
|
-
handles; elsewhere serve it and pass its URL as `rendererWasmUrl` (every entry
|
|
83
|
-
that draws takes it). `session.export()` resolves to the MP4 bytes unless
|
|
84
|
-
`encode: { download: true }` asks for a browser download; `encode: { format }`
|
|
85
|
-
picks another format (`'webm'`, or `'wav'`, `'mp3'` or `'m4a'` for audio alone).
|
|
86
|
-
|
|
87
|
-
## Render issues and GPU faults
|
|
88
|
-
|
|
89
|
-
What the renderer drew but not as the document says (a feature it lacks, a shader
|
|
90
|
-
that does not compile, a lost GPU device) is a typed `RenderIssue`: a session
|
|
91
|
-
lists it in `buildErrors`, and `exportVideo`, `exportVideoParallel`,
|
|
92
|
-
`session.export()`, `session.image()` and `StillRenderer.render` hand it to
|
|
93
|
-
`onIssue`. A lost device or a crashed backend fails those instead with a
|
|
94
|
-
`RenderBackendError` (from `@motionscript/core`; `retryable` when the backend
|
|
95
|
-
recovered on a new device).
|
|
96
|
-
|
|
97
|
-
A shader stored in the retired SkSL language (`language: "sksl"`, from documents
|
|
98
|
-
written before WGSL was the only one) draws nothing and is reported as a `shader`
|
|
99
|
-
issue asking for a WGSL port.
|
|
100
|
-
|
|
101
|
-
## CDN build
|
|
102
|
-
|
|
103
|
-
`pnpm build` ends by writing `dist/browser/` (generated, never edited): one minified
|
|
104
|
-
ES module per `exports` entry (`index.js`, `audio.js`), shared and lazy chunks, `esbuild.wasm`, `ms_renderer_bg.wasm`, and a
|
|
105
|
-
`manifest.json` mapping each export and binary asset to a package-relative path.
|
|
106
|
-
The renderer is bundled in; the peers stay imports, so a host's import map names each
|
|
107
|
-
of these bare specifiers once, to one copy:
|
|
108
|
-
|
|
109
|
-
```
|
|
110
|
-
@motionscript/core
|
|
111
|
-
@motionscript/core/sdk
|
|
112
|
-
```
|
|
113
|
-
|
|
114
|
-
Against a CDN module URL the default `ms_renderer_bg.wasm` and `esbuild.wasm` URLs do
|
|
115
|
-
not resolve, so a host passes `rendererWasmUrl` and, if it compiles source-string nodes,
|
|
116
|
-
`compilerWasmUrl`. Both binaries are listed in the packages' manifests.
|
|
117
|
-
|
|
118
|
-
See the [docs](https://motionscript.dev/docs) for the full feature set and API
|
|
119
|
-
reference.
|
|
120
|
-
|
|
121
|
-
## Development
|
|
122
|
-
|
|
123
|
-
From the monorepo root:
|
|
124
|
-
|
|
125
|
-
```bash
|
|
126
|
-
pnpm --filter @motionscript/browser build # eslint, tsc, tsc-alias, then the CDN build
|
|
127
|
-
pnpm --filter @motionscript/browser test # vitest: real Chromium, plus a Node project
|
|
128
|
-
```
|
|
129
|
-
|
|
130
|
-
The tests run in Chromium on SwiftShader's WebGPU adapter through
|
|
131
|
-
`@vitest/browser-playwright` (headed with `HEADED=1`, which Linux needs, under
|
|
132
|
-
`xvfb-run`: a headless page there reads a WebGPU canvas back empty), so install it
|
|
133
|
-
once with `pnpm --filter @motionscript/browser exec playwright install --with-deps chromium`.
|
|
134
|
-
They import the **built** `dist/` of core and the renderer: run `pnpm build:lib` (Rust
|
|
135
|
-
needed) first on a fresh checkout. Files named `*.node.test.ts` run in Node and ask what a consumer's bundler
|
|
136
|
-
keeps of this package (`tree-shaking`, `cdn-build`). Suites that need an H.264 or AAC
|
|
137
|
-
encoder skip on a Chromium without one; `REQUIRE_ENCODERS=1` makes them fail instead.
|
|
138
|
-
|
|
139
|
-
See [CLAUDE.md](https://github.com/motionscript-dev/motionscript/blob/main/packages/browser/CLAUDE.md) for the traps when editing here,
|
|
140
|
-
[ARCHITECTURE.md](https://github.com/motionscript-dev/motionscript/blob/main/ARCHITECTURE.md) for the layering and
|
|
141
|
-
[CONTRIBUTING.md](https://github.com/motionscript-dev/motionscript/blob/main/CONTRIBUTING.md) for the workflow.
|
|
1
|
+
# @motionscript/browser
|
|
2
|
+
|
|
3
|
+
The browser platform for [Motion Script](https://motionscript.dev): it draws
|
|
4
|
+
every frame with WebGPU through [`@motionscript/renderer`](https://github.com/motionscript-dev/motionscript/tree/main/packages/renderer), which it
|
|
5
|
+
builds in. It implements the
|
|
6
|
+
`platform`/`render` abstractions from [`@motionscript/core`](https://github.com/motionscript-dev/motionscript/tree/main/packages/core) so
|
|
7
|
+
documents can be drawn to a canvas, played back with synchronized audio, and
|
|
8
|
+
exported to video all in the browser.
|
|
9
|
+
|
|
10
|
+
How a frame is drawn (shapes, fills, strokes, effects, text, 3D) lives in the
|
|
11
|
+
renderer (Rust/wgpu compiled to WebAssembly). This package supplies the browser's
|
|
12
|
+
half: the export frame loop, encoding, Web Audio, the clock and the entry points.
|
|
13
|
+
A browser without WebGPU cannot draw: every entry that draws rejects with
|
|
14
|
+
`WebGpuUnavailableError` (check `isWgpuSupported()` first to say so up front).
|
|
15
|
+
|
|
16
|
+
## What's in here
|
|
17
|
+
|
|
18
|
+
- **Engine**: `createBrowserEngine` wires every platform seam below into a
|
|
19
|
+
`@motionscript/core` `Engine`. Await it once at app start and share it.
|
|
20
|
+
- **Exporter**: `exportVideo` renders a document offline and muxes video and
|
|
21
|
+
audio into an MP4 (H.264 + AAC, the default) or a WebM (VP9 + Opus) using
|
|
22
|
+
[mediabunny](https://github.com/Vanilagy/mediabunny). `exportAudio` writes the
|
|
23
|
+
timeline's mixed audio alone as WAV (the default), MP3 or M4A without rendering
|
|
24
|
+
a frame, and throws `NoAudioError` when nothing plays. `EXPORT_FORMATS` gives
|
|
25
|
+
each format's kind, extension and MIME type. MP3 loads a WASM build of LAME on
|
|
26
|
+
first use, in a worker started from a `blob:` URL, so a host's CSP needs
|
|
27
|
+
`worker-src blob:` for it.
|
|
28
|
+
- **Stills**: `StillRenderer` (from `createStillRenderer`) is a long-lived
|
|
29
|
+
renderer that repaints single frames into a canvas the host owns, for
|
|
30
|
+
thumbnails and previews; `exportScreenshot` is the one-shot wrapper over it.
|
|
31
|
+
- **Concat**: `concatVideoSegments` joins separately encoded MP4 (or WebM)
|
|
32
|
+
segments into one file of the same container without re-encoding, so an export can be split across workers. A segment is
|
|
33
|
+
`exportVideo({ frames: { start, end } })` (or `session.export({ frames })`):
|
|
34
|
+
that range alone, timestamped from 0 and silent.
|
|
35
|
+
`mixTimelineAudio` mixes the whole timeline's audio once for the joined file.
|
|
36
|
+
Every segment must share one encoder configuration; a mismatch throws
|
|
37
|
+
`ConcatMismatchError`.
|
|
38
|
+
- **Parallel export**: `exportVideoParallel` takes `exportVideo`'s parameters
|
|
39
|
+
plus `lanes`, the renderers to spread the frames over: `localLane()` for
|
|
40
|
+
this page, and `messagePortLane(port)` for another realm (a second window or
|
|
41
|
+
process) that answers with `serveExportLane(port, resolve)`. It cuts the
|
|
42
|
+
timeline into key-frame-aligned frame ranges of the whole project, starts on
|
|
43
|
+
the first lane alone and admits the next only while that raises throughput
|
|
44
|
+
(every lane's encoder shares the browser's one GPU process, so light scenes
|
|
45
|
+
gain nothing from a second lane), mixes the audio once while the frames
|
|
46
|
+
render, joins each range as soon as the ones before it are done (into
|
|
47
|
+
`stream` when given) and reports progress by frames drawn.
|
|
48
|
+
`createSegmentJoiner` is the incremental join underneath.
|
|
49
|
+
- **Audio**: `WebAudioPlayer` drives playback through the Web Audio API;
|
|
50
|
+
`mixAudio` and `encodeWav` (also on the `@motionscript/browser/audio`
|
|
51
|
+
subpath, which skips the renderer) mix down offline.
|
|
52
|
+
- **Assets**: `setAssetByteSource` lets a host answer for an asset's bytes (a file
|
|
53
|
+
the user just chose, say) before the renderer's storage goes to the network; it
|
|
54
|
+
applies to previews, exports and stills alike. `loadAssetBytes` reads through it.
|
|
55
|
+
- **Source-string nodes**: `createBrowserEngine({ nodes })` also accepts TS source
|
|
56
|
+
strings (array form only), each default-exporting a component (`component2d({ key })`),
|
|
57
|
+
compiled with `esbuild-wasm` on first use. A script is written with calls: JSX
|
|
58
|
+
neither compiles nor links. `dynamicNodes` overrides the SDK, extra importable
|
|
59
|
+
`modules` or the compiler, each field defaulting on its own.
|
|
60
|
+
- **Clock**: `WebTicker` synchronizes scene playback to `requestAnimationFrame` while calculating dt from audio context to ensure audio and animation line up.
|
|
61
|
+
- **Renderer**: `isWgpuSupported`, `onWgpuEvent` (device loss and recovery), and
|
|
62
|
+
the WGSL shader tools `ShaderPreview`, `validateShader` and `WGSL_PRELUDE`, from
|
|
63
|
+
`@motionscript/renderer`.
|
|
64
|
+
|
|
65
|
+
## Usage
|
|
66
|
+
|
|
67
|
+
```bash
|
|
68
|
+
npm install @motionscript/browser @motionscript/core
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
```ts
|
|
72
|
+
import { createBrowserEngine } from '@motionscript/browser';
|
|
73
|
+
|
|
74
|
+
const engine = await createBrowserEngine({ nodes: [MyNode] });
|
|
75
|
+
const session = engine.open({ document, target: canvasEl });
|
|
76
|
+
session.playback!.play();
|
|
77
|
+
|
|
78
|
+
const mp4 = await session.export();
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
The renderer's `ms_renderer_bg.wasm` resolves beside its module, which a bundler
|
|
82
|
+
handles; elsewhere serve it and pass its URL as `rendererWasmUrl` (every entry
|
|
83
|
+
that draws takes it). `session.export()` resolves to the MP4 bytes unless
|
|
84
|
+
`encode: { download: true }` asks for a browser download; `encode: { format }`
|
|
85
|
+
picks another format (`'webm'`, or `'wav'`, `'mp3'` or `'m4a'` for audio alone).
|
|
86
|
+
|
|
87
|
+
## Render issues and GPU faults
|
|
88
|
+
|
|
89
|
+
What the renderer drew but not as the document says (a feature it lacks, a shader
|
|
90
|
+
that does not compile, a lost GPU device) is a typed `RenderIssue`: a session
|
|
91
|
+
lists it in `buildErrors`, and `exportVideo`, `exportVideoParallel`,
|
|
92
|
+
`session.export()`, `session.image()` and `StillRenderer.render` hand it to
|
|
93
|
+
`onIssue`. A lost device or a crashed backend fails those instead with a
|
|
94
|
+
`RenderBackendError` (from `@motionscript/core`; `retryable` when the backend
|
|
95
|
+
recovered on a new device).
|
|
96
|
+
|
|
97
|
+
A shader stored in the retired SkSL language (`language: "sksl"`, from documents
|
|
98
|
+
written before WGSL was the only one) draws nothing and is reported as a `shader`
|
|
99
|
+
issue asking for a WGSL port.
|
|
100
|
+
|
|
101
|
+
## CDN build
|
|
102
|
+
|
|
103
|
+
`pnpm build` ends by writing `dist/browser/` (generated, never edited): one minified
|
|
104
|
+
ES module per `exports` entry (`index.js`, `audio.js`), shared and lazy chunks, `esbuild.wasm`, `ms_renderer_bg.wasm`, and a
|
|
105
|
+
`manifest.json` mapping each export and binary asset to a package-relative path.
|
|
106
|
+
The renderer is bundled in; the peers stay imports, so a host's import map names each
|
|
107
|
+
of these bare specifiers once, to one copy:
|
|
108
|
+
|
|
109
|
+
```
|
|
110
|
+
@motionscript/core
|
|
111
|
+
@motionscript/core/sdk
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
Against a CDN module URL the default `ms_renderer_bg.wasm` and `esbuild.wasm` URLs do
|
|
115
|
+
not resolve, so a host passes `rendererWasmUrl` and, if it compiles source-string nodes,
|
|
116
|
+
`compilerWasmUrl`. Both binaries are listed in the packages' manifests.
|
|
117
|
+
|
|
118
|
+
See the [docs](https://motionscript.dev/docs) for the full feature set and API
|
|
119
|
+
reference.
|
|
120
|
+
|
|
121
|
+
## Development
|
|
122
|
+
|
|
123
|
+
From the monorepo root:
|
|
124
|
+
|
|
125
|
+
```bash
|
|
126
|
+
pnpm --filter @motionscript/browser build # eslint, tsc, tsc-alias, then the CDN build
|
|
127
|
+
pnpm --filter @motionscript/browser test # vitest: real Chromium, plus a Node project
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
The tests run in Chromium on SwiftShader's WebGPU adapter through
|
|
131
|
+
`@vitest/browser-playwright` (headed with `HEADED=1`, which Linux needs, under
|
|
132
|
+
`xvfb-run`: a headless page there reads a WebGPU canvas back empty), so install it
|
|
133
|
+
once with `pnpm --filter @motionscript/browser exec playwright install --with-deps chromium`.
|
|
134
|
+
They import the **built** `dist/` of core and the renderer: run `pnpm build:lib` (Rust
|
|
135
|
+
needed) first on a fresh checkout. Files named `*.node.test.ts` run in Node and ask what a consumer's bundler
|
|
136
|
+
keeps of this package (`tree-shaking`, `cdn-build`). Suites that need an H.264 or AAC
|
|
137
|
+
encoder skip on a Chromium without one; `REQUIRE_ENCODERS=1` makes them fail instead.
|
|
138
|
+
|
|
139
|
+
See [CLAUDE.md](https://github.com/motionscript-dev/motionscript/blob/main/packages/browser/CLAUDE.md) for the traps when editing here,
|
|
140
|
+
[ARCHITECTURE.md](https://github.com/motionscript-dev/motionscript/blob/main/ARCHITECTURE.md) for the layering and
|
|
141
|
+
[CONTRIBUTING.md](https://github.com/motionscript-dev/motionscript/blob/main/CONTRIBUTING.md) for the workflow.
|
package/dist/asset-bytes.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"asset-bytes.js","sourceRoot":"","sources":["../src/asset-bytes.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AAKH,IAAI,MAAM,GAA2B,IAAI,CAAC;AAE1C;;;;;;GAMG;AACH,MAAM,UAAU,kBAAkB,CAAC,IAA4B;IAC3D,MAAM,GAAG,IAAI,CAAC;AAClB,CAAC;AAED;;;;;;GAMG;AACH,MAAM,CAAC,KAAK,UAAU,eAAe,CAAC,GAAW;IAC7C,IAAI,CAAC,MAAM;QAAE,OAAO,IAAI,CAAC;IACzB,IAAI,CAAC;QACD,OAAO,MAAM,MAAM,CAAC,GAAG,CAAC,CAAC;IAC7B,CAAC;IAAC,MAAM,CAAC;QACL,OA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ED;;;;GAIG;AACH,MAAM,CAAC,KAAK,UAAU,eAAe,CAAC,GAAW;IAC7C,MAAM,QAAQ,GAAG,MAAM,KAAK,CAAC,GAAG,CAAC,CAAC;IAClC,IAAI,CAAC,QAAQ,CAAC,EAAE,EAAE,CAAC;QACf,MAAM,MAAM,GAAG,QAAQ,CAAC,UAAU,CAAC,CAAC,CAAC,GAAG,QAAQ,CAAC,MAAM,IAAI,QAAQ,CAAC,UAAU,EAAE,CAAC,CAAC,CAAC,GAAG,QAAQ,CAAC,MAAM,EAAE,CAAC;QACxG,MAAM,IAAI,KAAK,CAAC,aAAa,GAAG,kBAAkB,MAAM,EAAE,CAAC,CAAC;IAChE,CAAC;IACD,OAAO,QAAQ,CAAC,IAAI,EAAE,CAAC;AAC3B,CAAC;AAED,2EAA2E;AAC3E,MAAM,CAAC,KAAK,UAAU,cAAc,CAAC,GAAW;IAC5C,OAAO,CAAC,MAAM,eAAe,CAAC,GAAG,CAAC,CAAC,IAAI,eAAe,CAAC,GAAG,CAAC,CAAC;AAChE,CAAC","sourcesContent":["/**\n * Where a source URL's bytes come from: a host's local store, then the network.\n * The renderer's storage adapter reads every asset through it (`loadBytes`).\n *\n * ## Why the web adapter needs this at all\n *\n * The storage adapter addresses every asset by URL and fetches it. That is\n * right for an asset that lives somewhere, and wrong for one whose bytes the\n * host already has in hand — a file the user has just chosen, a render a local\n * model has just produced, a picture whose upload has not finished or cannot\n * start because there is no network. In all three the adapter downloads bytes\n * that never needed to travel.\n *\n * `NodeStorageAdapter` has had the answer since it was written: an injected\n * `AssetLoader`, with an `inline` map consulted ahead of the disk. This is the\n * same seam for the browser.\n *\n * ## Why it is module-level\n *\n * The storage adapter is constructed at several sites *inside* this package —\n * sessions, the exporter, the audio mixer and the still renderer — none of\n * which a host can reach. A constructor argument would\n * therefore cover the preview and leave export and stills on the network,\n * which is the worst of both: the picture a user sees would come from local\n * bytes and the one they render would not.\n *\n * So this is an *injection point*, not a cache. The adapter still owns every\n * decode, every cache, every size ceiling and every eviction rule; all this\n * changes is where the compressed bytes came from. A source that answers `null`\n * — which is what an unregistered URL, an evicted entry or a host with no local\n * store does — leaves the adapter exactly as it was.\n */\n\n/** Local bytes for a source URL, or `null` to go to the network. */\nexport type AssetByteSource = (src: string) => Promise<Blob | null>;\n\nlet source: AssetByteSource | null = null;\n\n/**\n * Installs the byte source for this browsing context, or clears it with `null`.\n *\n * Called once per window that renders. Later calls replace the previous source\n * rather than stacking, because two sources disagreeing about one URL has no\n * sensible answer.\n */\nexport function setAssetByteSource(next: AssetByteSource | null): void {\n source = next;\n}\n\n/**\n * The bytes for `src`, or `null`.\n *\n * Swallows a throwing source on purpose. A host's local store failing is a\n * cache miss, not a render failure — the caller falls through to `fetch`, which\n * is what it would have done had the source never been installed.\n */\nexport async function localAssetBytes(src: string): Promise<Blob | null> {\n if (!source) return null;\n try {\n return await source(src);\n } catch {\n return null;\n }\n}\n\n/**\n * The network's bytes for `url`. A response that is not OK is a failed load,\n * never bytes: a 404 page handed to the font manager marked a family loaded\n * with no face in it.\n */\nexport async function fetchAssetBytes(url: string): Promise<Blob> {\n const response = await fetch(url);\n if (!response.ok) {\n const status = response.statusText ? `${response.status} ${response.statusText}` : `${response.status}`;\n throw new Error(`Fetching \"${url}\" failed: HTTP ${status}`);\n }\n return response.blob();\n}\n\n/** The bytes for `src`: the host's local store first, then the network. */\nexport async function loadAssetBytes(src: string): Promise<Blob> {\n return (await localAssetBytes(src)) ?? fetchAssetBytes(src);\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"bus-graph.js","sourceRoot":"","sources":["../../src/audio/bus-graph.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAuC,MAAM,oBAAoB,CAAC;AACrF,OAAO,EAAE,gBAAgB,EAAE,qBAAqB,EAAE,MAAM,gBAAgB,CAAC;AAEzE;;;;;;;;GAQG;AACH,MAAM,OAAO,aAAa;IAED;IAEA;IAHrB,YACqB,GAAqB;IACtC,+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AC,UAAU,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,CAAC,YAAY,EAAE,IAAI,CAAC,aAAa,EAAE,SAAS,EAAE,QAAQ,GAAG,IAAI,CAAC,OAAO,CAAC,CAAC;YACnH,IAAI,CAAC,OAAO,CAAC,UAAU,CAAC,CAAC;YACzB,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;YAC5B,IAAI,GAAG,UAAU,CAAC;QACtB,CAAC;QACD,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;QAErB,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,KAAK,EAAE,GAAG,KAAK,CAAC,KAAK,CAAC,CAAC;QACvC,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC,GAAG,KAAK,CAAC,WAAW,CAAC,CAAC;QAC5C,KAAK,MAAM,GAAG,IAAI,KAAK,CAAC,WAAW;YAAE,GAAG,CAAC,KAAK,CAAC,SAAS,CAAC,CAAC;QAE1D,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;QAC5B,OAAO,KAAK,CAAC;IACjB,CAAC;CACJ","sourcesContent":["import { busStopKey, type AudioBusStop, type AudioEffect } from \"@motionscript/core\";\
|
|
1
|
+
{"version":3,"file":"bus-graph.js","sourceRoot":"","sources":["../../src/audio/bus-graph.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAuC,MAAM,oBAAoB,CAAC;AACrF,OAAO,EAAE,gBAAgB,EAAE,qBAAqB,EAAE,MAAM,gBAAgB,CAAC;AAEzE;;;;;;;;GAQG;AACH,MAAM,OAAO,aAAa;IAED;IAEA;IAHrB,YACqB,GAAqB;IACtC,+EAA+E;IAC9D,WAAsB;QAFtB,QAAG,GAAH,GAAG,CAAkB;QAErB,gBAAW,GAAX,WAAW,CAAW;IACvC,CAAC;IAEL;;;;;;;;;;;;OAYG;IACH,QAAQ,CAAC,GAAwC,EAAE,SAAiB,EAAE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,CAAC,UAAU,CAAC,CAAC;YACzB,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;YAC5B,IAAI,GAAG,UAAU,CAAC;QACtB,CAAC;QACD,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;QAErB,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,KAAK,EAAE,GAAG,KAAK,CAAC,KAAK,CAAC,CAAC;QACvC,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC,GAAG,KAAK,CAAC,WAAW,CAAC,CAAC;QAC5C,KAAK,MAAM,GAAG,IAAI,KAAK,CAAC,WAAW;YAAE,GAAG,CAAC,KAAK,CAAC,SAAS,CAAC,CAAC;QAE1D,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;QAC5B,OAAO,KAAK,CAAC;IACjB,CAAC;CACJ","sourcesContent":["import { busStopKey, type AudioBusStop, type AudioEffect } from \"@motionscript/core\";\nimport { applyTransitions, buildAudioEffectGraph } from \"./effect-graph\";\n\n/**\n * The live chains for one audio context's buses.\n *\n * Held per context rather than per clip, because that is the whole difference\n * between a bus and per-clip processing: every clip naming a bus connects into\n * the *same* nodes, so a reverb on a bus is one tail the group shares rather\n * than one tail each. Flattening the levels into per-clip gain would sound\n * right; flattening the effects would not.\n */\nexport class AudioBusGraph {\n constructor(\n private readonly ctx: BaseAudioContext,\n /** Where the last bus in a chain connects — the master, or the destination. */\n private readonly destination: AudioNode,\n ) { }\n\n /**\n * The input node a clip on `bus` should connect to, building the chain the\n * first time it is asked for.\n *\n * `bus` is source-adjacent first, so `bus[0]` is the one the clip feeds and\n * each stop after it is that bus's own parent. An empty or absent chain\n * returns the destination, which is what every clip did before buses\n * existed.\n *\n * `startTime` is the context time a new chain's schedule starts from, and\n * `clipTime` the same instant in the stops' own seconds. A curve on a stop\n * runs over its `startAt`–`endAt`, so the two are what place it.\n */\n inputFor(bus: readonly AudioBusStop[] | undefined, startTime: number, clipTime: number): AudioNode {\n if (!bus || bus.length === 0) return this.destination;\n return this.chainFrom(bus, 0, startTime, clipTime);\n }\n\n /** Disconnect every live bus node, so the next {@link inputFor} builds afresh. Safe to call more than once. */\n dispose(): void {\n for (const node of this.nodes) {\n try { node.disconnect(); } catch { /* already torn down */ }\n }\n for (const osc of this.oscillators) {\n try { osc.stop(); } catch { /* never started, or already stopped */ }\n }\n this.nodes.length = 0;\n this.oscillators.length = 0;\n this.inputs.clear();\n }\n\n private readonly inputs = new Map<string, AudioNode>();\n private readonly nodes: AudioNode[] = [];\n private readonly oscillators: OscillatorNode[] = [];\n\n /**\n * Build (or reuse) the chain for `bus[index]`, connected to whatever\n * `bus[index + 1]` resolves to.\n *\n * Recursive rather than iterative because a bus's *output* has to exist\n * before its own nodes can connect to it, and the outermost bus is the last\n * entry.\n *\n * Reused by everything that changes what it sounds like, down to the master,\n * not by id alone: a bus edited under a playing device, or one id on two\n * tracks, is a different chain, and one built for the first would play the\n * second through stale levels and a curve anchored to the wrong span.\n */\n private chainFrom(bus: readonly AudioBusStop[], index: number, startTime: number, clipTime: number): AudioNode {\n if (index >= bus.length) return this.destination;\n\n const stop = bus[index];\n const key = bus.slice(index).map(busStopKey).join(\">\");\n const existing = this.inputs.get(key);\n if (existing) return existing;\n\n const output = this.chainFrom(bus, index + 1, startTime, clipTime);\n\n // A pass-through head every clip connects to, so the bus has one input\n // whatever its effects turn out to be.\n const input = this.ctx.createGain();\n input.gain.value = 1;\n\n // The level is a gain after the effects, as a sound's own level is, so a\n // ramp on it is scheduled exactly as one in `effects` is.\n const level: AudioEffect = { type: \"gain\", value: stop.mute ? 0 : stop.volume };\n const span = stop.endAt - stop.startAt;\n const graph = buildAudioEffectGraph(\n this.ctx, input, [...(stop.effects ?? []), level], startTime, span, clipTime - stop.startAt,\n );\n let tail = graph.output;\n if (stop.transitionIn || stop.transitionOut) {\n const transition = this.ctx.createGain();\n applyTransitions(transition.gain, span, stop.transitionIn, stop.transitionOut, startTime, clipTime - stop.startAt);\n tail.connect(transition);\n this.nodes.push(transition);\n tail = transition;\n }\n tail.connect(output);\n\n this.nodes.push(input, ...graph.nodes);\n this.oscillators.push(...graph.oscillators);\n for (const osc of graph.oscillators) osc.start(startTime);\n\n this.inputs.set(key, input);\n return input;\n }\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"effect-graph.js","sourceRoot":"","sources":["../../src/audio/effect-graph.ts"],"names":[],"mappings":"AAAA,OAAO,EACH,gBAAgB,EAChB,MAAM,EACN,WAAW,EACX,UAAU,EACV,gBAAgB,EAChB,WAAW,EACX,gBAAgB,EAChB,eAAe,GAMlB,MAAM,oBAAoB,CAAC;AAe5B,iGAAiG;AACjG,MAAM,wBAAwB,GAAG,GAAG,CAAC;AAErC;;;;;;;;GAQG;AACH,SAAS,UAAU,CACf,UAAsB,EACtB,KAAY,EACZ,SAAiB,EACjB,YAAoB,EACpB,OAAe,EACf,QAA+B,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;IAEvC,6CAA6C;IAC7C,MAAM,MAAM,GAAG,CAAC,CAAS,EAAU,EAAE;QACjC,MAAM,CAAC,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC;QACnB,OAAO,MAAM,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,UAAU,CAAC,KAAK,CAAC;IACrD,CAAC,CAAC;IAEF,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,CAAC;QACjB,UAAU,CAAC,KAAK,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC;QACjC,OAAO;IACX,CAAC;IACD,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,YAAY,CAAC,IAAI,YAAY,IAAI,CAAC,EAAE,CAAC;QACtD,UAAU,CAAC,KAAK,GAAG,MAAM,CAAC,WAAW,CAAC,KAAK,EAAE,UAAU,CAAC,KAAK,CAAC,CAAC,CAAC;QAChE,OAAO;IACX,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EAAE,CAAC;QAC9D,MAAM,OAAO,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,YAAY,CAAC,CAAC;QAC1C,IAAI,CAAC,uBAAuB,CAAC,OAAO,CAAC,OAAO,CAAC,EAAE,SAAS,GAAG,CAAC,OAAO,GAAG,MAAM,CAAC,CAAC,CAAC;QAC/E,IAAI,OAAO,IAAI,YAAY;YAAE,MAAM;IACvC,CAAC;AACL,CAAC","sourcesContent":["import {\r\n clipTimeAtSource,\r\n isRamp,\r\n resolveRamp,\r\n sampleRamp,\r\n sourceTimeAtClip,\r\n staticValue,\r\n transitionGainAt,\r\n transitionSpans,\r\n type AudioEffect,\r\n type AudioRequest,\r\n type AudioTransition,\r\n type Param,\r\n type Ramp,\r\n} from \"@motionscript/core\";\r\n\r\n/**\r\n * The Web Audio graph for one source's effect chain.\r\n *\r\n * `output` is the last node, to connect downstream; `nodes` are every node to\r\n * `disconnect()` on teardown; `oscillators` are tremolo LFOs the caller starts\r\n * and stops alongside the source.\r\n */\r\nexport interface AudioEffectGraph {\r\n output: AudioNode;\r\n nodes: AudioNode[];\r\n oscillators: OscillatorNode[];\r\n}\r\n\r\n/** Sample points per second an eased segment or a transition is baked into: about every 5 ms. */\r\nconst CURVE_SAMPLES_PER_SECOND = 200;\r\n\r\n/**\r\n * Apply a {@link Param} to a Web Audio {@link AudioParam}: a number once, a\r\n * ramp as a schedule — linear segments as `linearRampToValueAtTime`, eased\r\n * ones baked into `setValueCurveAtTime`.\r\n *\r\n * `elapsed` is the clip seconds already played when a mid-clip seek starts the\r\n * source, so \"now\" reads the ramp at the clip's current position. `clamp` keeps\r\n * every scheduled value in a param's valid range.\r\n */\r\nfunction applyParam(\r\n audioParam: AudioParam,\r\n param: Param,\r\n startTime: number,\r\n clipDuration: number,\r\n elapsed: number,\r\n clamp: (v: number) => number = (v) => v,\r\n): void {\r\n // The platform throws on a non-finite value.\r\n const finite = (v: number): number => {\r\n const c = clamp(v);\r\n return Number.isFinite(c) ? c : audioParam.value;\r\n };\r\n\r\n if (!isRamp(param)) {\r\n audioParam.value = finite(param);\r\n return;\r\n }\r\n if (!Number.isFinite(clipDuration) || clipDuration <= 0) {\r\n audioParam.value = finite(staticValue(param, audioParam.value));\r\n return;\r\n }\r\n\r\n const keys = resolveRamp(param, clipDuration);\r\n if (keys.length === 0) return;\r\n const base = startTime - elapsed;\r\n audioParam.setValueAtTime(finite(sampleRamp(param, Math.max(0, elapsed), clipDuration, audioParam.value)), startTime);\r\n\r\n for (let i = 1; i < keys.length; i++) {\r\n const previous = keys[i - 1];\r\n const key = keys[i];\r\n const segStart = base + previous.time;\r\n const segEnd = base + key.time;\r\n if (segEnd <= startTime) continue;\r\n\r\n const span = key.time - previous.time;\r\n if (span <= 0) {\r\n audioParam.setValueAtTime(finite(key.value), Math.max(segEnd, startTime));\r\n continue;\r\n }\r\n\r\n // A segment already under way when the source starts carries on from\r\n // the value set at `startTime` rather than restarting from its own.\r\n const done = Math.max(0, (startTime - segStart) / span);\r\n const curveStart = Math.max(segStart, startTime);\r\n if (key.linear) {\r\n if (done === 0) audioParam.setValueAtTime(finite(previous.value), curveStart);\r\n audioParam.linearRampToValueAtTime(finite(key.value), segEnd);\r\n } else {\r\n const curveDur = segEnd - curveStart;\r\n const count = Math.max(2, Math.ceil(curveDur * CURVE_SAMPLES_PER_SECOND));\r\n const samples = new Float32Array(count);\r\n for (let n = 0; n < count; n++) {\r\n const local = done + (1 - done) * (n / (count - 1));\r\n samples[n] = finite(previous.value + (key.value - previous.value) * key.ease(local));\r\n }\r\n audioParam.setValueCurveAtTime(samples, curveStart, curveDur);\r\n }\r\n }\r\n}\r\n\r\n/**\r\n * Builds the Web Audio graph for a clip's effect chain, after an\r\n * already-created `source`. Shared by live playback ({@link WebAudioSink}) and\r\n * offline export (`mixAudio`) so both sound identical.\r\n *\r\n * `startTime` is the context time the source starts; `clipDuration` the clip\r\n * seconds ramps run over (a clip's, or a bus's span); `elapsed` the clip seconds\r\n * already played at a mid-clip seek (0 for a clean start, negative before the\r\n * span begins).\r\n */\r\nexport function buildAudioEffectGraph(\r\n ctx: BaseAudioContext,\r\n source: AudioNode,\r\n effects: readonly AudioEffect[],\r\n startTime: number = ctx.currentTime,\r\n clipDuration: number = 0,\r\n elapsed: number = 0,\r\n): AudioEffectGraph {\r\n const nodes: AudioNode[] = [];\r\n const oscillators: OscillatorNode[] = [];\r\n let cursor: AudioNode = source;\r\n\r\n const series = (node: AudioNode): void => {\r\n cursor.connect(node);\r\n nodes.push(node);\r\n cursor = node;\r\n };\r\n\r\n const apply = (p: AudioParam, v: Param, clamp?: (n: number) => number): void =>\r\n applyParam(p, v, startTime, clipDuration, elapsed, clamp);\r\n\r\n for (const effect of effects) {\r\n switch (effect.type) {\r\n case \"gain\": {\r\n const gain = ctx.createGain();\r\n apply(gain.gain, effect.value);\r\n series(gain);\r\n break;\r\n }\r\n case \"highpass\":\r\n case \"lowpass\": {\r\n const biquad = ctx.createBiquadFilter();\r\n biquad.type = effect.type;\r\n apply(biquad.frequency, effect.frequency);\r\n if (effect.q !== undefined) apply(biquad.Q, effect.q);\r\n series(biquad);\r\n break;\r\n }\r\n case \"tremolo\": {\r\n // A carrier gain an LFO swings: with depth d the carrier sits at\r\n // 1 - d/2 and the swing is d/2.\r\n const carrier = ctx.createGain();\r\n const lfo = ctx.createOscillator();\r\n const lfoDepth = ctx.createGain();\r\n\r\n apply(carrier.gain, effect.depth, (d) => 1 - clamp01(d) / 2);\r\n apply(lfoDepth.gain, effect.depth, (d) => clamp01(d) / 2);\r\n apply(lfo.frequency, effect.rate);\r\n\r\n lfo.connect(lfoDepth);\r\n lfoDepth.connect(carrier.gain);\r\n\r\n nodes.push(lfoDepth);\r\n oscillators.push(lfo);\r\n series(carrier);\r\n break;\r\n }\r\n case \"echo\": {\r\n const split = cursor;\r\n const merge = ctx.createGain();\r\n\r\n const dry = ctx.createGain();\r\n apply(dry.gain, effect.mix ?? 0.5, (m) => 1 - clamp01(m));\r\n split.connect(dry);\r\n dry.connect(merge);\r\n\r\n // The delay line is sized once, so it has to hold the ramp's longest delay.\r\n const delay = ctx.createDelay(Math.max(1, maxOf(effect.delay) + 1));\r\n apply(delay.delayTime, effect.delay);\r\n const feedbackGain = ctx.createGain();\r\n apply(feedbackGain.gain, effect.feedback, (f) => Math.max(0, Math.min(0.99, f)));\r\n const wet = ctx.createGain();\r\n apply(wet.gain, effect.mix ?? 0.5, (m) => clamp01(m));\r\n\r\n split.connect(delay);\r\n delay.connect(feedbackGain);\r\n feedbackGain.connect(delay);\r\n delay.connect(wet);\r\n wet.connect(merge);\r\n\r\n nodes.push(dry, delay, feedbackGain, wet, merge);\r\n cursor = merge;\r\n break;\r\n }\r\n }\r\n }\r\n\r\n return { output: cursor, nodes, oscillators };\r\n}\r\n\r\nfunction clamp01(v: number): number {\r\n return Math.max(0, Math.min(1, v));\r\n}\r\n\r\nfunction maxOf(p: Param): number {\r\n return isRamp(p) ? Math.max(0, ...p.keys.map((k) => k.value)) : p;\r\n}\r\n\r\n/**\r\n * Schedule `transitionIn`/`transitionOut` onto a gain over a span `length`\r\n * seconds long, baked like an eased ramp. Between the two it holds at 1.\r\n */\r\nexport function applyTransitions(\r\n gain: AudioParam,\r\n length: number,\r\n transitionIn: AudioTransition | undefined,\r\n transitionOut: AudioTransition | undefined,\r\n startTime: number,\r\n elapsed: number,\r\n): void {\r\n if (!transitionIn && !transitionOut) return;\r\n const seek = Math.max(0, elapsed);\r\n const at = (t: number): number => transitionGainAt(t, length, transitionIn, transitionOut);\r\n gain.setValueAtTime(at(seek), startTime);\r\n\r\n const spans = transitionSpans(length, transitionIn, transitionOut);\r\n const windows: [number, number][] = [];\r\n if (spans.in > 0) windows.push([0, spans.in]);\r\n if (spans.out > 0 && Number.isFinite(length)) windows.push([length - spans.out, length]);\r\n const base = startTime - elapsed;\r\n for (const [from, to] of windows) {\r\n const begin = Math.max(from, seek);\r\n const duration = to - begin;\r\n if (duration <= 0) continue;\r\n const count = Math.max(2, Math.ceil(duration * CURVE_SAMPLES_PER_SECOND));\r\n const samples = new Float32Array(count);\r\n for (let n = 0; n < count; n++) samples[n] = at(begin + duration * (n / (count - 1)));\r\n gain.setValueCurveAtTime(samples, base + begin, duration);\r\n }\r\n}\r\n\r\n/** How one request plays its source: the rate, and the lengths its ramps and transitions run over. */\r\nexport interface Playback {\r\n /** The constant `playbackRate`; 1 under a ramp, which is scheduled instead. */\r\n rate: number;\r\n speedRamp?: Ramp;\r\n /** Source seconds a speed ramp is placed on. */\r\n sourceLength: number;\r\n /** Clip seconds the volume and effect ramps run over. */\r\n clipDuration: number;\r\n /** What the transitions run over: the clip's length, unknown for an open one. */\r\n transitionLength: number;\r\n}\r\n\r\nexport function playbackOf(req: AudioRequest, bufferDuration: number): Playback {\r\n const speed = req.speed ?? 1;\r\n const speedRamp = isRamp(speed) ? speed : undefined;\r\n const rate = speedRamp || !(Number.isFinite(speed) && (speed as number) > 0) ? 1 : (speed as number);\r\n // Placed on the same length core sized the request with, not the decoded buffer.\r\n const sourceLength = req.sourceLength ?? Math.max(0, bufferDuration - req.trimStart);\r\n const bounded = Number.isFinite(req.endAt);\r\n const clipDuration = bounded ? Math.max(0, req.endAt - req.startAt) : clipTimeAtSource(speed, sourceLength);\r\n return { rate, speedRamp, sourceLength, clipDuration, transitionLength: bounded ? clipDuration : Infinity };\r\n}\r\n\r\n/** Source seconds after `trimStart` that `clipElapsed` clip seconds have played. */\r\nexport function sourceOffsetOf(playback: Playback, clipElapsed: number): number {\r\n return playback.speedRamp\r\n ? sourceTimeAtClip(playback.speedRamp, playback.sourceLength, clipElapsed)\r\n : clipElapsed * playback.rate;\r\n}\r\n\r\n/**\r\n * A request's level, then its transitions, after `from`: the gains every\r\n * request plays through on its way to its bus.\r\n */\r\nexport function buildLevel(\r\n ctx: BaseAudioContext,\r\n from: AudioNode,\r\n req: AudioRequest,\r\n playback: Playback,\r\n startTime: number,\r\n elapsed: number,\r\n): { output: AudioNode; nodes: AudioNode[] } {\r\n const level = ctx.createGain();\r\n applyParam(level.gain, req.volume ?? 1, startTime, playback.clipDuration, elapsed);\r\n from.connect(level);\r\n if (!req.transitionIn && !req.transitionOut) return { output: level, nodes: [level] };\r\n const transition = ctx.createGain();\r\n applyTransitions(transition.gain, playback.transitionLength, req.transitionIn, req.transitionOut, startTime, elapsed);\r\n level.connect(transition);\r\n return { output: transition, nodes: [level, transition] };\r\n}\r\n\r\n/**\r\n * Schedule a speed ramp onto an `AudioBufferSourceNode.playbackRate`.\r\n *\r\n * The ramp runs over **source** seconds and Web Audio schedules in context\r\n * seconds, so this walks clip time in small steps, converts each to a source\r\n * offset through the integral inverse, and lays down the rate there. `elapsed`\r\n * is clip seconds already played at a mid-clip seek.\r\n */\r\nexport function applySpeedToPlaybackRate(\r\n rate: AudioParam,\r\n speed: Ramp | undefined,\r\n sourceLength: number,\r\n clipDuration: number,\r\n startTime: number,\r\n elapsed: number,\r\n): void {\r\n if (!speed) return;\r\n const step = 1 / CURVE_SAMPLES_PER_SECOND;\r\n const seekAt = Math.max(0, elapsed);\r\n const valueAt = (clipElapsed: number): number =>\r\n Math.max(1e-4, sampleRamp(speed, sourceTimeAtClip(speed, sourceLength, clipElapsed), sourceLength, 1));\r\n rate.setValueAtTime(valueAt(seekAt), startTime);\r\n\r\n for (let s = seekAt + step; s <= clipDuration + step; s += step) {\r\n const clamped = Math.min(s, clipDuration);\r\n rate.linearRampToValueAtTime(valueAt(clamped), startTime + (clamped - seekAt));\r\n if (clamped >= clipDuration) break;\r\n }\r\n}\r\n"]}
|
|
1
|
+
{"version":3,"file":"effect-graph.js","sourceRoot":"","sources":["../../src/audio/effect-graph.ts"],"names":[],"mappings":"AAAA,OAAO,EACH,gBAAgB,EAChB,MAAM,EACN,WAAW,EACX,UAAU,EACV,gBAAgB,EAChB,WAAW,EACX,gBAAgB,EAChB,eAAe,GAMlB,MAAM,oBAAoB,CAAC;AAe5B,iGAAiG;AACjG,MAAM,wBAAwB,GAAG,GAAG,CAAC;AAErC;;;;;;;;GAQ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EAAE,UAAU,CAAC,EAAE,CAAC;AAC9D,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,wBAAwB,CACpC,IAAgB,EAChB,KAAuB,EACvB,YAAoB,EACpB,YAAoB,EACpB,SAAiB,EACjB,OAAe;IAEf,IAAI,CAAC,KAAK;QAAE,OAAO;IACnB,MAAM,IAAI,GAAG,CAAC,GAAG,wBAAwB,CAAC;IAC1C,MAAM,MAAM,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,OAAO,CAAC,CAAC;IACpC,MAAM,OAAO,GAAG,CAAC,WAAmB,EAAU,EAAE,CAC5C,IAAI,CAAC,GAAG,CAAC,IAAI,EAAE,UAAU,CAAC,KAAK,EAAE,gBAAgB,CAAC,KAAK,EAAE,YAAY,EAAE,WAAW,CAAC,EAAE,YAAY,EAAE,CAAC,CAAC,CAAC,CAAC;IAC3G,IAAI,CAAC,cAAc,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE,SAAS,CAAC,CAAC;IAEhD,KAAK,IAAI,CAAC,GAAG,MAAM,GAAG,IAAI,EAAE,CAAC,IAAI,YAAY,GAAG,IAAI,EAAE,CAAC,IAAI,IAAI,EAAE,CAAC;QAC9D,MAAM,OAAO,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,YAAY,CAAC,CAAC;QAC1C,IAAI,CAAC,uBAAuB,CAAC,OAAO,CAAC,OAAO,CAAC,EAAE,SAAS,GAAG,CAAC,OAAO,GAAG,MAAM,CAAC,CAAC,CAAC;QAC/E,IAAI,OAAO,IAAI,YAAY;YAAE,MAAM;IACvC,CAAC;AACL,CAAC","sourcesContent":["import {\n clipTimeAtSource,\n isRamp,\n resolveRamp,\n sampleRamp,\n sourceTimeAtClip,\n staticValue,\n transitionGainAt,\n transitionSpans,\n type AudioEffect,\n type AudioRequest,\n type AudioTransition,\n type Param,\n type Ramp,\n} from \"@motionscript/core\";\n\n/**\n * The Web Audio graph for one source's effect chain.\n *\n * `output` is the last node, to connect downstream; `nodes` are every node to\n * `disconnect()` on teardown; `oscillators` are tremolo LFOs the caller starts\n * and stops alongside the source.\n */\nexport interface AudioEffectGraph {\n output: AudioNode;\n nodes: AudioNode[];\n oscillators: OscillatorNode[];\n}\n\n/** Sample points per second an eased segment or a transition is baked into: about every 5 ms. */\nconst CURVE_SAMPLES_PER_SECOND = 200;\n\n/**\n * Apply a {@link Param} to a Web Audio {@link AudioParam}: a number once, a\n * ramp as a schedule — linear segments as `linearRampToValueAtTime`, eased\n * ones baked into `setValueCurveAtTime`.\n *\n * `elapsed` is the clip seconds already played when a mid-clip seek starts the\n * source, so \"now\" reads the ramp at the clip's current position. `clamp` keeps\n * every scheduled value in a param's valid range.\n */\nfunction applyParam(\n audioParam: AudioParam,\n param: Param,\n startTime: number,\n clipDuration: number,\n elapsed: number,\n clamp: (v: number) => number = (v) => v,\n): void {\n // The platform throws on a non-finite value.\n const finite = (v: number): number => {\n const c = clamp(v);\n return Number.isFinite(c) ? c : audioParam.value;\n };\n\n if (!isRamp(param)) {\n audioParam.value = finite(param);\n return;\n }\n if (!Number.isFinite(clipDuration) || clipDuration <= 0) {\n audioParam.value = finite(staticValue(param, audioParam.value));\n return;\n }\n\n const keys = resolveRamp(param, clipDuration);\n if (keys.length === 0) return;\n const base = startTime - elapsed;\n audioParam.setValueAtTime(finite(sampleRamp(param, Math.max(0, elapsed), clipDuration, audioParam.value)), startTime);\n\n for (let i = 1; i < keys.length; i++) {\n const previous = keys[i - 1];\n const key = keys[i];\n const segStart = base + previous.time;\n const segEnd = base + key.time;\n if (segEnd <= startTime) continue;\n\n const span = key.time - previous.time;\n if (span <= 0) {\n audioParam.setValueAtTime(finite(key.value), Math.max(segEnd, startTime));\n continue;\n }\n\n // A segment already under way when the source starts carries on from\n // the value set at `startTime` rather than restarting from its own.\n const done = Math.max(0, (startTime - segStart) / span);\n const curveStart = Math.max(segStart, startTime);\n if (key.linear) {\n if (done === 0) audioParam.setValueAtTime(finite(previous.value), curveStart);\n audioParam.linearRampToValueAtTime(finite(key.value), segEnd);\n } else {\n const curveDur = segEnd - curveStart;\n const count = Math.max(2, Math.ceil(curveDur * CURVE_SAMPLES_PER_SECOND));\n const samples = new Float32Array(count);\n for (let n = 0; n < count; n++) {\n const local = done + (1 - done) * (n / (count - 1));\n samples[n] = finite(previous.value + (key.value - previous.value) * key.ease(local));\n }\n audioParam.setValueCurveAtTime(samples, curveStart, curveDur);\n }\n }\n}\n\n/**\n * Builds the Web Audio graph for a clip's effect chain, after an\n * already-created `source`. Shared by live playback ({@link WebAudioSink}) and\n * offline export (`mixAudio`) so both sound identical.\n *\n * `startTime` is the context time the source starts; `clipDuration` the clip\n * seconds ramps run over (a clip's, or a bus's span); `elapsed` the clip seconds\n * already played at a mid-clip seek (0 for a clean start, negative before the\n * span begins).\n */\nexport function buildAudioEffectGraph(\n ctx: BaseAudioContext,\n source: AudioNode,\n effects: readonly AudioEffect[],\n startTime: number = ctx.currentTime,\n clipDuration: number = 0,\n elapsed: number = 0,\n): AudioEffectGraph {\n const nodes: AudioNode[] = [];\n const oscillators: OscillatorNode[] = [];\n let cursor: AudioNode = source;\n\n const series = (node: AudioNode): void => {\n cursor.connect(node);\n nodes.push(node);\n cursor = node;\n };\n\n const apply = (p: AudioParam, v: Param, clamp?: (n: number) => number): void =>\n applyParam(p, v, startTime, clipDuration, elapsed, clamp);\n\n for (const effect of effects) {\n switch (effect.type) {\n case \"gain\": {\n const gain = ctx.createGain();\n apply(gain.gain, effect.value);\n series(gain);\n break;\n }\n case \"highpass\":\n case \"lowpass\": {\n const biquad = ctx.createBiquadFilter();\n biquad.type = effect.type;\n apply(biquad.frequency, effect.frequency);\n if (effect.q !== undefined) apply(biquad.Q, effect.q);\n series(biquad);\n break;\n }\n case \"tremolo\": {\n // A carrier gain an LFO swings: with depth d the carrier sits at\n // 1 - d/2 and the swing is d/2.\n const carrier = ctx.createGain();\n const lfo = ctx.createOscillator();\n const lfoDepth = ctx.createGain();\n\n apply(carrier.gain, effect.depth, (d) => 1 - clamp01(d) / 2);\n apply(lfoDepth.gain, effect.depth, (d) => clamp01(d) / 2);\n apply(lfo.frequency, effect.rate);\n\n lfo.connect(lfoDepth);\n lfoDepth.connect(carrier.gain);\n\n nodes.push(lfoDepth);\n oscillators.push(lfo);\n series(carrier);\n break;\n }\n case \"echo\": {\n const split = cursor;\n const merge = ctx.createGain();\n\n const dry = ctx.createGain();\n apply(dry.gain, effect.mix ?? 0.5, (m) => 1 - clamp01(m));\n split.connect(dry);\n dry.connect(merge);\n\n // The delay line is sized once, so it has to hold the ramp's longest delay.\n const delay = ctx.createDelay(Math.max(1, maxOf(effect.delay) + 1));\n apply(delay.delayTime, effect.delay);\n const feedbackGain = ctx.createGain();\n apply(feedbackGain.gain, effect.feedback, (f) => Math.max(0, Math.min(0.99, f)));\n const wet = ctx.createGain();\n apply(wet.gain, effect.mix ?? 0.5, (m) => clamp01(m));\n\n split.connect(delay);\n delay.connect(feedbackGain);\n feedbackGain.connect(delay);\n delay.connect(wet);\n wet.connect(merge);\n\n nodes.push(dry, delay, feedbackGain, wet, merge);\n cursor = merge;\n break;\n }\n }\n }\n\n return { output: cursor, nodes, oscillators };\n}\n\nfunction clamp01(v: number): number {\n return Math.max(0, Math.min(1, v));\n}\n\nfunction maxOf(p: Param): number {\n return isRamp(p) ? Math.max(0, ...p.keys.map((k) => k.value)) : p;\n}\n\n/**\n * Schedule `transitionIn`/`transitionOut` onto a gain over a span `length`\n * seconds long, baked like an eased ramp. Between the two it holds at 1.\n */\nexport function applyTransitions(\n gain: AudioParam,\n length: number,\n transitionIn: AudioTransition | undefined,\n transitionOut: AudioTransition | undefined,\n startTime: number,\n elapsed: number,\n): void {\n if (!transitionIn && !transitionOut) return;\n const seek = Math.max(0, elapsed);\n const at = (t: number): number => transitionGainAt(t, length, transitionIn, transitionOut);\n gain.setValueAtTime(at(seek), startTime);\n\n const spans = transitionSpans(length, transitionIn, transitionOut);\n const windows: [number, number][] = [];\n if (spans.in > 0) windows.push([0, spans.in]);\n if (spans.out > 0 && Number.isFinite(length)) windows.push([length - spans.out, length]);\n const base = startTime - elapsed;\n for (const [from, to] of windows) {\n const begin = Math.max(from, seek);\n const duration = to - begin;\n if (duration <= 0) continue;\n const count = Math.max(2, Math.ceil(duration * CURVE_SAMPLES_PER_SECOND));\n const samples = new Float32Array(count);\n for (let n = 0; n < count; n++) samples[n] = at(begin + duration * (n / (count - 1)));\n gain.setValueCurveAtTime(samples, base + begin, duration);\n }\n}\n\n/** How one request plays its source: the rate, and the lengths its ramps and transitions run over. */\nexport interface Playback {\n /** The constant `playbackRate`; 1 under a ramp, which is scheduled instead. */\n rate: number;\n speedRamp?: Ramp;\n /** Source seconds a speed ramp is placed on. */\n sourceLength: number;\n /** Clip seconds the volume and effect ramps run over. */\n clipDuration: number;\n /** What the transitions run over: the clip's length, unknown for an open one. */\n transitionLength: number;\n}\n\nexport function playbackOf(req: AudioRequest, bufferDuration: number): Playback {\n const speed = req.speed ?? 1;\n const speedRamp = isRamp(speed) ? speed : undefined;\n const rate = speedRamp || !(Number.isFinite(speed) && (speed as number) > 0) ? 1 : (speed as number);\n // Placed on the same length core sized the request with, not the decoded buffer.\n const sourceLength = req.sourceLength ?? Math.max(0, bufferDuration - req.trimStart);\n const bounded = Number.isFinite(req.endAt);\n const clipDuration = bounded ? Math.max(0, req.endAt - req.startAt) : clipTimeAtSource(speed, sourceLength);\n return { rate, speedRamp, sourceLength, clipDuration, transitionLength: bounded ? clipDuration : Infinity };\n}\n\n/** Source seconds after `trimStart` that `clipElapsed` clip seconds have played. */\nexport function sourceOffsetOf(playback: Playback, clipElapsed: number): number {\n return playback.speedRamp\n ? sourceTimeAtClip(playback.speedRamp, playback.sourceLength, clipElapsed)\n : clipElapsed * playback.rate;\n}\n\n/**\n * A request's level, then its transitions, after `from`: the gains every\n * request plays through on its way to its bus.\n */\nexport function buildLevel(\n ctx: BaseAudioContext,\n from: AudioNode,\n req: AudioRequest,\n playback: Playback,\n startTime: number,\n elapsed: number,\n): { output: AudioNode; nodes: AudioNode[] } {\n const level = ctx.createGain();\n applyParam(level.gain, req.volume ?? 1, startTime, playback.clipDuration, elapsed);\n from.connect(level);\n if (!req.transitionIn && !req.transitionOut) return { output: level, nodes: [level] };\n const transition = ctx.createGain();\n applyTransitions(transition.gain, playback.transitionLength, req.transitionIn, req.transitionOut, startTime, elapsed);\n level.connect(transition);\n return { output: transition, nodes: [level, transition] };\n}\n\n/**\n * Schedule a speed ramp onto an `AudioBufferSourceNode.playbackRate`.\n *\n * The ramp runs over **source** seconds and Web Audio schedules in context\n * seconds, so this walks clip time in small steps, converts each to a source\n * offset through the integral inverse, and lays down the rate there. `elapsed`\n * is clip seconds already played at a mid-clip seek.\n */\nexport function applySpeedToPlaybackRate(\n rate: AudioParam,\n speed: Ramp | undefined,\n sourceLength: number,\n clipDuration: number,\n startTime: number,\n elapsed: number,\n): void {\n if (!speed) return;\n const step = 1 / CURVE_SAMPLES_PER_SECOND;\n const seekAt = Math.max(0, elapsed);\n const valueAt = (clipElapsed: number): number =>\n Math.max(1e-4, sampleRamp(speed, sourceTimeAtClip(speed, sourceLength, clipElapsed), sourceLength, 1));\n rate.setValueAtTime(valueAt(seekAt), startTime);\n\n for (let s = seekAt + step; s <= clipDuration + step; s += step) {\n const clamped = Math.min(s, clipDuration);\n rate.linearRampToValueAtTime(valueAt(clamped), startTime + (clamped - seekAt));\n if (clamped >= clipDuration) break;\n }\n}\n"]}
|
package/dist/audio/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/audio/index.ts"],"names":[],"mappings":"AAAA,4EAA4E;AAC5E,qBAAqB;AACrB,EAAE;AACF,iFAAiF;AACjF,qEAAqE;AAErE,OAAO,EAAE,QAAQ,EAAE,SAAS,EAAE,aAAa,EAAE,mBAAmB,EAAE,MAAM,SAAS,CAAC;AAGlF,OAAO,EAAE,YAAY,EAAE,YAAY,IAAI,cAAc,EAAE,MAAM,UAAU,CAAC;AAExE,+EAA+E;AAC/E,yEAAyE;AACzE,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC","sourcesContent":["// @motionscript/browser/audio — stacked-audio playback and mixdown, with no\
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/audio/index.ts"],"names":[],"mappings":"AAAA,4EAA4E;AAC5E,qBAAqB;AACrB,EAAE;AACF,iFAAiF;AACjF,qEAAqE;AAErE,OAAO,EAAE,QAAQ,EAAE,SAAS,EAAE,aAAa,EAAE,mBAAmB,EAAE,MAAM,SAAS,CAAC;AAGlF,OAAO,EAAE,YAAY,EAAE,YAAY,IAAI,cAAc,EAAE,MAAM,UAAU,CAAC;AAExE,+EAA+E;AAC/E,yEAAyE;AACzE,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC","sourcesContent":["// @motionscript/browser/audio — stacked-audio playback and mixdown, with no\n// renderer attached.\n//\n// A separate entry point from the package barrel on purpose: nothing here draws,\n// so a host building an audio timeline should not load the renderer.\n\nexport { mixAudio, encodeWav, WebAudioMixer, DEFAULT_SAMPLE_RATE } from \"./mixer\";\nexport type { MixAudioOptions } from \"./mixer\";\n\nexport { WebAudioSink, WebAudioSink as WebAudioPlayer } from \"./player\";\n\n// The bus layer both of those share. Exported so a host writing its own device\n// or mixer builds the same chains, rather than a second set that drifts.\nexport { AudioBusGraph } from \"./bus-graph\";\n"]}
|
|
@@ -1 +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\";\
|
|
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\";\n\n/** The span of a decoded buffer a looping request repeats, in source seconds. */\nexport interface LoopRegion {\n start: number;\n end: number;\n}\n\n/**\n * The region a looping request repeats: from its in-point to its out-point, or to\n * the end of the buffer when it has none. `null` for a request that does not loop,\n * and for a degenerate region (an out-point at or before the in-point), where the\n * device falls back to wrapping the whole buffer.\n *\n * Shared by the live device and the offline mixer so a preview and an export loop\n * the same samples.\n */\nexport function loopRegionOf(request: AudioRequest, bufferDuration: number): LoopRegion | null {\n if (!request.loop) return null;\n const start = Math.min(Math.max(0, request.trimStart), bufferDuration);\n const end = Math.min(request.trimEnd ?? bufferDuration, bufferDuration);\n return end > start ? { start, end } : null;\n}\n\n/**\n * Configure `source` to play `request`'s loop: on or off, and over the trimmed\n * region rather than the whole buffer (`loopStart`/`loopEnd` default to the\n * whole buffer, which is how a trim used to be lost).\n */\nexport function applyLoop(source: AudioBufferSourceNode, request: AudioRequest): LoopRegion | null {\n source.loop = request.loop;\n const region = source.buffer ? loopRegionOf(request, source.buffer.duration) : null;\n if (region) {\n source.loopStart = region.start;\n source.loopEnd = region.end;\n }\n return region;\n}\n\n/**\n * Fold a source offset into a loop region.\n *\n * A seek into the middle of a looping clip lands at `trimStart + elapsed`, which\n * can be many passes past the region's end; the device would not wrap an offset\n * that starts outside the region, so it is wrapped here.\n */\nexport function wrapIntoRegion(region: LoopRegion | null, offset: number): number {\n if (!region || offset < region.end) return offset;\n return region.start + ((offset - region.start) % (region.end - region.start));\n}\n"]}
|
package/dist/audio/mixer.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mixer.js","sourceRoot":"","sources":["../../src/audio/mixer.ts"],"names":[],"mappings":"AAEA,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,EAAE,wBAAwB,EAAE,qBAAqB,EAAE,UAAU,EAAE,UAAU,EAAE,cAAc,EAAE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AAC,EAAE,EAAE,SAAS,EAAE,IAAI,CAAC,CAAC;IAEpC,6EAA6E;IAC7E,8DAA8D;IAC9D,MAAM,QAAQ,GAAmB,EAAE,CAAC;IACpC,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,YAAY,EAAE,CAAC,EAAE;QAAE,QAAQ,CAAC,IAAI,CAAC,MAAM,CAAC,cAAc,CAAC,CAAC,CAAC,CAAC,CAAC;IAE/E,IAAI,MAAM,GAAG,EAAE,CAAC;IAChB,KAAK,IAAI,KAAK,GAAG,CAAC,EAAE,KAAK,GAAG,UAAU,EAAE,KAAK,EAAE,EAAE,CAAC;QAC9C,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,YAAY,EAAE,CAAC,EAAE,EAAE,CAAC;YACpC,kEAAkE;YAClE,kEAAkE;YAClE,MAAM,MAAM,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,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 type { AssetCatalog } from \"@motionscript/core\";\r\nimport type { AudioMixer, ScheduledAudioRequest } from \"../frame-loop\";\r\nimport { fetchAssetBytes, localAssetBytes } from \"../asset-bytes\";\r\nimport { AudioBusGraph } from \"./bus-graph\";\r\nimport { applyLoop } from \"./loop-region\";\r\nimport { applySpeedToPlaybackRate, buildAudioEffectGraph, buildLevel, playbackOf, sourceOffsetOf } from \"./effect-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, as the storage\r\n * adapter 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 // Ramps resolve against the clip's own length, deliberately *not*\r\n // `playDuration`, which is that length truncated by the timeline end:\r\n // resolving against the truncated value would squeeze the whole ramp\r\n // into the surviving part instead of cutting it off. `elapsed` is 0:\r\n // unlike a live seek, the mix always starts a clip at its beginning.\r\n const playback = playbackOf(req, srcBuffer.duration);\r\n if (playback.rate !== 1) source.playbackRate.value = playback.rate;\r\n applySpeedToPlaybackRate(\r\n source.playbackRate, playback.speedRamp, playback.sourceLength, playback.clipDuration, globalStart, 0,\r\n );\r\n\r\n // The same chain as the live WebAudioSink graph, so exports sound like the preview.\r\n const graph = buildAudioEffectGraph(mixCtx, source, req.effects ?? [], globalStart, playback.clipDuration, 0);\r\n const level = buildLevel(mixCtx, graph.output, req, playback, globalStart, 0);\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 level.output.connect(buses.inputFor(req.bus, 0, -globalOffset));\r\n\r\n // start()'s 3rd arg is a source-buffer duration: under a speed ramp the\r\n // buffer consumed over a span of clip time is the integral of the rate,\r\n // not a multiple of it.\r\n const consumed = sourceOffsetOf(playback, playDuration);\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"]}
|
|
1
|
+
{"version":3,"file":"mixer.js","sourceRoot":"","sources":["../../src/audio/mixer.ts"],"names":[],"mappings":"AAEA,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,EAAE,wBAAwB,EAAE,qBAAqB,EAAE,UAAU,EAAE,UAAU,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAC;AAEzH,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;QAChD,CAAC;QAAC,MAAM,CAAC;YACL,OAAO,GAAG,CAAC;QACf,CAAC;IACL,CAAC;AACL,CAAC;AAED;;;;;;GAMG;AACH,KAAK,UAAU,gBAAgB,CAAC,GAAW,EAAE,GAAwB;IACjE,MAAM,IAAI,GAAG,CAAC,MAAM,eAAe,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,MAAM,eAAe,CAAC,GAAG,CAAC,CAAC,CAAC;IAC1E,OAAO,GAAG,CAAC,eAAe,CAAC,MAAM,IAAI,CAAC,WAAW,EAAE,CAAC,CAAC;AACzD,CAAC;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,M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M,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 type { AssetCatalog } from \"@motionscript/core\";\nimport type { AudioMixer, ScheduledAudioRequest } from \"../frame-loop\";\nimport { fetchAssetBytes, localAssetBytes } from \"../asset-bytes\";\nimport { AudioBusGraph } from \"./bus-graph\";\nimport { applyLoop } from \"./loop-region\";\nimport { applySpeedToPlaybackRate, buildAudioEffectGraph, buildLevel, playbackOf, sourceOffsetOf } from \"./effect-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, as the storage\n * adapter 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/**\n * Reads and decodes an audio source against a scratch `OfflineAudioContext`\n * (decoding requires a context but doesn't render through it). The host's byte\n * source is asked first, as for every other asset read: a mix that went to the\n * network would fail where the preview played, offline or for bytes not yet\n * uploaded.\n */\nasync function fetchAudioBuffer(src: string, ctx: OfflineAudioContext): Promise<AudioBuffer> {\n const blob = (await localAssetBytes(src)) ?? (await fetchAssetBytes(src));\n return ctx.decodeAudioData(await blob.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 // A source only a picture's own sound wants may hold no audio at all, which\n // is silence rather than a failed mix — see `AudioRequest.optional`.\n const asked = new Set(requests.filter(r => !r.request.optional).map(r => r.request.src));\n const decoded = new Map<string, AudioBuffer>();\n await Promise.all(uniqueSrcs.map(async (src) => {\n try {\n decoded.set(src, await fetchAudioBuffer(audioUrl(src, options.catalog), scratchCtx));\n } catch (cause) {\n if (asked.has(src)) throw cause;\n }\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 applyLoop(source, req);\n\n // Ramps resolve against the clip's own length, deliberately *not*\n // `playDuration`, which is that length truncated by the timeline end:\n // resolving against the truncated value would squeeze the whole ramp\n // into the surviving part instead of cutting it off. `elapsed` is 0:\n // unlike a live seek, the mix always starts a clip at its beginning.\n const playback = playbackOf(req, srcBuffer.duration);\n if (playback.rate !== 1) source.playbackRate.value = playback.rate;\n applySpeedToPlaybackRate(\n source.playbackRate, playback.speedRamp, playback.sourceLength, playback.clipDuration, globalStart, 0,\n );\n\n // The same chain as the live WebAudioSink graph, so exports sound like the preview.\n const graph = buildAudioEffectGraph(mixCtx, source, req.effects ?? [], globalStart, playback.clipDuration, 0);\n const level = buildLevel(mixCtx, graph.output, req, playback, globalStart, 0);\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 level.output.connect(buses.inputFor(req.bus, 0, -globalOffset));\n\n // start()'s 3rd arg is a source-buffer duration: under a speed ramp the\n // buffer consumed over a span of clip time is the integral of the rate,\n // not a multiple of it.\n const consumed = sourceOffsetOf(playback, playDuration);\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"]}
|