@motionscript/browser 0.1.0-alpha.0 → 0.1.0-alpha.5
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 +148 -0
- package/LICENSE +201 -201
- package/NOTICE +48 -0
- package/README.md +118 -69
- package/dist/asset-bytes.d.ts +7 -1
- package/dist/asset-bytes.d.ts.map +1 -1
- package/dist/asset-bytes.js +14 -1
- package/dist/asset-bytes.js.map +1 -1
- package/dist/audio/bus-graph.d.ts +12 -3
- package/dist/audio/bus-graph.d.ts.map +1 -1
- package/dist/audio/bus-graph.js +24 -29
- package/dist/audio/bus-graph.js.map +1 -1
- package/dist/audio/filter-graph.d.ts +6 -5
- package/dist/audio/filter-graph.d.ts.map +1 -1
- package/dist/audio/filter-graph.js +29 -23
- package/dist/audio/filter-graph.js.map +1 -1
- package/dist/audio/index.js.map +1 -1
- package/dist/audio/loop-region.d.ts +31 -0
- package/dist/audio/loop-region.d.ts.map +1 -0
- package/dist/audio/loop-region.js +43 -0
- package/dist/audio/loop-region.js.map +1 -0
- package/dist/audio/mixer.d.ts.map +1 -1
- package/dist/audio/mixer.js +30 -11
- package/dist/audio/mixer.js.map +1 -1
- package/dist/audio/player.d.ts +4 -4
- package/dist/audio/player.d.ts.map +1 -1
- package/dist/audio/player.js +19 -15
- package/dist/audio/player.js.map +1 -1
- package/dist/browser/THIRD_PARTY_LICENSES.txt +432 -0
- package/dist/browser/audio.js +1 -1
- package/dist/browser/chunks/chunk-2MAFJWBZ.js +2 -0
- package/dist/browser/chunks/chunk-2MAFJWBZ.js.map +7 -0
- package/dist/browser/index.js +133 -279
- package/dist/browser/index.js.map +4 -4
- package/dist/browser/manifest.json +19 -2
- package/dist/concat.js.map +1 -1
- package/dist/decode/image-levels.js.map +1 -1
- package/dist/decode/levels.js.map +1 -1
- package/dist/decode/video-levels.js.map +1 -1
- package/dist/dynamic-nodes.d.ts.map +1 -1
- package/dist/dynamic-nodes.js +5 -1
- package/dist/dynamic-nodes.js.map +1 -1
- package/dist/engine.d.ts.map +1 -1
- package/dist/engine.js +18 -5
- package/dist/engine.js.map +1 -1
- package/dist/exporter.d.ts +10 -2
- package/dist/exporter.d.ts.map +1 -1
- package/dist/exporter.js +8 -7
- package/dist/exporter.js.map +1 -1
- package/dist/getter.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -27
- package/dist/index.js.map +1 -1
- package/dist/measure-context.js.map +1 -1
- package/dist/mix-timeline-audio.d.ts +2 -2
- package/dist/mix-timeline-audio.d.ts.map +1 -1
- package/dist/mix-timeline-audio.js +1 -2
- package/dist/mix-timeline-audio.js.map +1 -1
- package/dist/render-context.d.ts +5 -3
- package/dist/render-context.d.ts.map +1 -1
- package/dist/render-context.js +7 -3
- package/dist/render-context.js.map +1 -1
- package/dist/screenshot.d.ts +3 -3
- package/dist/screenshot.d.ts.map +1 -1
- package/dist/screenshot.js +1 -2
- package/dist/screenshot.js.map +1 -1
- package/dist/still.d.ts +18 -8
- package/dist/still.d.ts.map +1 -1
- package/dist/still.js +28 -21
- package/dist/still.js.map +1 -1
- package/dist/storage-adapter.d.ts +1 -1
- package/dist/storage-adapter.d.ts.map +1 -1
- package/dist/storage-adapter.js +12 -10
- package/dist/storage-adapter.js.map +1 -1
- package/dist/three/renderer.d.ts +10 -11
- package/dist/three/renderer.d.ts.map +1 -1
- package/dist/three/renderer.js +18 -13
- package/dist/three/renderer.js.map +1 -1
- package/dist/ticker.d.ts +22 -4
- package/dist/ticker.d.ts.map +1 -1
- package/dist/ticker.js +65 -28
- package/dist/ticker.js.map +1 -1
- package/package.json +8 -6
- package/dist/browser/chunks/chunk-NSFLRI4P.js +0 -2
- package/dist/browser/chunks/chunk-NSFLRI4P.js.map +0 -7
- package/dist/output.d.ts +0 -7
- package/dist/output.d.ts.map +0 -1
- package/dist/output.js +0 -6
- package/dist/output.js.map +0 -1
package/README.md
CHANGED
|
@@ -1,69 +1,118 @@
|
|
|
1
|
-
# @motionscript/browser
|
|
2
|
-
|
|
3
|
-
The browser rendering backend for [Motion Script](https://motionscript.dev),
|
|
4
|
-
built on Skia via [`@motionscript/canvaskit`](
|
|
5
|
-
`platform`/`render` abstractions from [`@motionscript/core`](
|
|
6
|
-
documents can be drawn to a canvas, played back with synchronized audio, and
|
|
7
|
-
exported to video all in the browser.
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
- **
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
1
|
+
# @motionscript/browser
|
|
2
|
+
|
|
3
|
+
The browser rendering backend for [Motion Script](https://motionscript.dev),
|
|
4
|
+
built on Skia via [`@motionscript/canvaskit`](https://github.com/motionscript-dev/motionscript/tree/main/packages/canvaskit). It implements the
|
|
5
|
+
`platform`/`render` abstractions from [`@motionscript/core`](https://github.com/motionscript-dev/motionscript/tree/main/packages/core) so
|
|
6
|
+
documents can be drawn to a canvas, played back with synchronized audio, and
|
|
7
|
+
exported to video all in the browser.
|
|
8
|
+
|
|
9
|
+
How a frame is drawn (shapes, fills, strokes, effects, text, the 3D replay) lives in
|
|
10
|
+
[`@motionscript/skia-render`](https://github.com/motionscript-dev/motionscript/tree/main/packages/skia-render) and is shared with every other backend.
|
|
11
|
+
This package supplies only the browser's half: a WebGL surface over a `<canvas>`,
|
|
12
|
+
media decode and storage, encoding, Web Audio, the clock, and the WebGL renderer 3D
|
|
13
|
+
draws with.
|
|
14
|
+
|
|
15
|
+
## What's in here
|
|
16
|
+
|
|
17
|
+
- **Engine**: `createBrowserEngine` wires every platform seam below into a
|
|
18
|
+
`@motionscript/core` `Engine`. Await it once at app start and share it.
|
|
19
|
+
- **Render context**: `WebRenderContext` draws the `@motionscript/core` scene
|
|
20
|
+
graph (shapes, fills, strokes, effects, text, video, masks) onto a CanvasKit
|
|
21
|
+
surface.
|
|
22
|
+
- **Exporter**: `exportVideo` renders a document offline and muxes video and
|
|
23
|
+
audio into an MP4 using [mediabunny](https://github.com/Vanilagy/mediabunny).
|
|
24
|
+
- **Stills**: `StillRenderer` (from `createStillRenderer`) is a long-lived
|
|
25
|
+
renderer that repaints single frames into a canvas the host owns, for
|
|
26
|
+
thumbnails and previews; `exportScreenshot` is the one-shot wrapper over it.
|
|
27
|
+
- **Concat**: `concatVideoSegments` joins separately encoded MP4 segments
|
|
28
|
+
without re-encoding, so an export can be split across workers. A segment is
|
|
29
|
+
`exportVideo({ frames: { start, end } })` (or `session.export({ frames })`):
|
|
30
|
+
that range alone, timestamped from 0 and silent.
|
|
31
|
+
`mixTimelineAudio` mixes the whole timeline's audio once for the joined file.
|
|
32
|
+
Every segment must share one encoder configuration; a mismatch throws
|
|
33
|
+
`ConcatMismatchError`.
|
|
34
|
+
- **3D**: every engine session, still renderer and export installs the WebGL
|
|
35
|
+
renderer that makes `Canvas3D` draw (three.js, loaded lazily). Importing the
|
|
36
|
+
package registers nothing, so a bundler may tree-shake freely.
|
|
37
|
+
- **Audio**: `WebAudioPlayer` drives playback through the Web Audio API;
|
|
38
|
+
`mixAudio` and `encodeWav` (also on the `@motionscript/browser/audio`
|
|
39
|
+
subpath, which skips CanvasKit) mix down offline.
|
|
40
|
+
- **Storage / assets**: `WebStorageAdapter` and `WebMeasureContext` implement the
|
|
41
|
+
asset-loading and text-measurement abstractions `@motionscript/core` needs. The
|
|
42
|
+
adapter decodes images and video at the resolution each draw covers, in
|
|
43
|
+
levels (`src/decode/`); see
|
|
44
|
+
[decode resolution](https://github.com/motionscript-dev/motionscript/blob/main/packages/skia-render/docs/decode-resolution.md).
|
|
45
|
+
`setAssetByteSource` lets a host answer for an asset's bytes (a file the user just
|
|
46
|
+
chose, say) before the adapter goes to the network; it applies to previews,
|
|
47
|
+
exports and stills alike.
|
|
48
|
+
- **Source-string nodes**: `createBrowserEngine({ nodes })` also accepts TS/JSX source
|
|
49
|
+
strings (array form only), each declaring its key with `@node(...)`, compiled with
|
|
50
|
+
`esbuild-wasm` on first use. `dynamicNodes` overrides the SDK, JSX runtime,
|
|
51
|
+
extra importable `modules` or the compiler, each field defaulting on its own.
|
|
52
|
+
- **Clock**: `WebTicker` synchronizes scene playback to `requestAnimationFrame` while calculating dt from audio context to ensure audio and animation line up.
|
|
53
|
+
- **CanvasKit access**: `getCanvasKit` loads and caches the CanvasKit/Skia
|
|
54
|
+
WebAssembly module used for rendering.
|
|
55
|
+
|
|
56
|
+
## Usage
|
|
57
|
+
|
|
58
|
+
```bash
|
|
59
|
+
npm install @motionscript/browser @motionscript/core @motionscript/canvaskit
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
```ts
|
|
63
|
+
import { createBrowserEngine } from '@motionscript/browser';
|
|
64
|
+
|
|
65
|
+
const engine = await createBrowserEngine({ wasmUrl: '/canvaskit.wasm', nodes: [MyNode] });
|
|
66
|
+
const session = engine.open({ document, target: canvasEl });
|
|
67
|
+
session.playback!.play();
|
|
68
|
+
|
|
69
|
+
const mp4 = await session.export();
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
Serve `canvaskit.wasm` from `@motionscript/canvaskit` as a static asset and pass
|
|
73
|
+
its URL as `wasmUrl`. `getCanvasKit` memoizes: the first URL wins, and a failed
|
|
74
|
+
init stays failed. `session.export()` resolves to the MP4 bytes unless
|
|
75
|
+
`encode: { download: true }` asks for a browser download.
|
|
76
|
+
|
|
77
|
+
## CDN build
|
|
78
|
+
|
|
79
|
+
`pnpm build` ends by writing `dist/browser/` (generated, never edited): one minified
|
|
80
|
+
ES module per `exports` entry (`index.js`, `audio.js`), shared and lazy chunks
|
|
81
|
+
(three.js and its glTF loader arrive only through `import()`), `esbuild.wasm`, and a
|
|
82
|
+
`manifest.json` mapping each export and binary asset to a package-relative path.
|
|
83
|
+
The peers stay imports, so a host's import map names each of these bare specifiers
|
|
84
|
+
once, to one copy:
|
|
85
|
+
|
|
86
|
+
```
|
|
87
|
+
@motionscript/core
|
|
88
|
+
@motionscript/core/sdk
|
|
89
|
+
@motionscript/core/jsx/jsx-runtime
|
|
90
|
+
@motionscript/canvaskit
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
Against a CDN module URL the default `canvaskit.wasm` and `esbuild.wasm` URLs do not
|
|
94
|
+
resolve, so a host passes `wasmUrl` and, if it compiles source-string nodes,
|
|
95
|
+
`compilerWasmUrl`. Both binaries are listed in the packages' manifests.
|
|
96
|
+
|
|
97
|
+
See the [docs](https://motionscript.dev/docs) for the full feature set and API
|
|
98
|
+
reference.
|
|
99
|
+
|
|
100
|
+
## Development
|
|
101
|
+
|
|
102
|
+
From the monorepo root:
|
|
103
|
+
|
|
104
|
+
```bash
|
|
105
|
+
pnpm --filter @motionscript/browser build # eslint, tsc, tsc-alias, then the CDN build
|
|
106
|
+
pnpm --filter @motionscript/browser test # vitest: real Chromium, plus a Node project
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
The tests run in headless Chromium through `@vitest/browser-playwright`, so install it
|
|
110
|
+
once with `pnpm --filter @motionscript/browser exec playwright install --with-deps chromium`.
|
|
111
|
+
They import `@motionscript/core`'s **built** `dist/`: run `pnpm build:lib` first on a fresh
|
|
112
|
+
checkout. Files named `*.node.test.ts` run in Node and ask what a consumer's bundler
|
|
113
|
+
keeps of this package (`tree-shaking`, `cdn-build`). Suites that need an H.264 or AAC
|
|
114
|
+
encoder skip on a Chromium without one; `REQUIRE_ENCODERS=1` makes them fail instead.
|
|
115
|
+
|
|
116
|
+
See [CLAUDE.md](https://github.com/motionscript-dev/motionscript/blob/main/packages/browser/CLAUDE.md) for the traps when editing here,
|
|
117
|
+
[ARCHITECTURE.md](https://github.com/motionscript-dev/motionscript/blob/main/ARCHITECTURE.md) for the layering and
|
|
118
|
+
[CONTRIBUTING.md](https://github.com/motionscript-dev/motionscript/blob/main/CONTRIBUTING.md) for the workflow.
|
package/dist/asset-bytes.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Where a source URL's bytes
|
|
2
|
+
* Where a source URL's bytes come from: a host's local store, then the network.
|
|
3
3
|
*
|
|
4
4
|
* ## Why the web adapter needs this at all
|
|
5
5
|
*
|
|
@@ -47,4 +47,10 @@ export declare function setAssetByteSource(next: AssetByteSource | null): void;
|
|
|
47
47
|
* is what it would have done had the source never been installed.
|
|
48
48
|
*/
|
|
49
49
|
export declare function localAssetBytes(src: string): Promise<Blob | null>;
|
|
50
|
+
/**
|
|
51
|
+
* The network's bytes for `url`. A response that is not OK is a failed load,
|
|
52
|
+
* never bytes: a 404 page handed to the font manager marked a family loaded
|
|
53
|
+
* with no face in it.
|
|
54
|
+
*/
|
|
55
|
+
export declare function fetchAssetBytes(url: string): Promise<Blob>;
|
|
50
56
|
//# sourceMappingURL=asset-bytes.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"asset-bytes.d.ts","sourceRoot":"","sources":["../src/asset-bytes.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AAEH,oEAAoE;AACpE,MAAM,MAAM,eAAe,GAAG,CAAC,GAAG,EAAE,MAAM,KAAK,OAAO,CAAC,IAAI,GAAG,IAAI,CAAC,CAAC;AAIpE;;;;;;GAMG;AACH,wBAAgB,kBAAkB,CAAC,IAAI,EAAE,eAAe,GAAG,IAAI,GAAG,IAAI,CAErE;AAED;;;;;;GAMG;AACH,wBAAsB,eAAe,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,GAAG,IAAI,CAAC,CAOvE"}
|
|
1
|
+
{"version":3,"file":"asset-bytes.d.ts","sourceRoot":"","sources":["../src/asset-bytes.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AAEH,oEAAoE;AACpE,MAAM,MAAM,eAAe,GAAG,CAAC,GAAG,EAAE,MAAM,KAAK,OAAO,CAAC,IAAI,GAAG,IAAI,CAAC,CAAC;AAIpE;;;;;;GAMG;AACH,wBAAgB,kBAAkB,CAAC,IAAI,EAAE,eAAe,GAAG,IAAI,GAAG,IAAI,CAErE;AAED;;;;;;GAMG;AACH,wBAAsB,eAAe,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,GAAG,IAAI,CAAC,CAOvE;AAED;;;;GAIG;AACH,wBAAsB,eAAe,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAOhE"}
|
package/dist/asset-bytes.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Where a source URL's bytes
|
|
2
|
+
* Where a source URL's bytes come from: a host's local store, then the network.
|
|
3
3
|
*
|
|
4
4
|
* ## Why the web adapter needs this at all
|
|
5
5
|
*
|
|
@@ -57,4 +57,17 @@ export async function localAssetBytes(src) {
|
|
|
57
57
|
return null;
|
|
58
58
|
}
|
|
59
59
|
}
|
|
60
|
+
/**
|
|
61
|
+
* The network's bytes for `url`. A response that is not OK is a failed load,
|
|
62
|
+
* never bytes: a 404 page handed to the font manager marked a family loaded
|
|
63
|
+
* with no face in it.
|
|
64
|
+
*/
|
|
65
|
+
export async function fetchAssetBytes(url) {
|
|
66
|
+
const response = await fetch(url);
|
|
67
|
+
if (!response.ok) {
|
|
68
|
+
const status = response.statusText ? `${response.status} ${response.statusText}` : `${response.status}`;
|
|
69
|
+
throw new Error(`Fetching "${url}" failed: HTTP ${status}`);
|
|
70
|
+
}
|
|
71
|
+
return response.blob();
|
|
72
|
+
}
|
|
60
73
|
//# sourceMappingURL=asset-bytes.js.map
|
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;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;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,OAAO,IAAI,CAAC;IAChB,CAAC;AACL,CAAC","sourcesContent":["/**\n * Where a source URL's bytes
|
|
1
|
+
{"version":3,"file":"asset-bytes.js","sourceRoot":"","sources":["../src/asset-bytes.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;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,OAAO,IAAI,CAAC;IAChB,CAAC;AACL,CAAC;AAED;;;;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","sourcesContent":["/**\r\n * Where a source URL's bytes come from: a host's local store, then the network.\r\n *\r\n * ## Why the web adapter needs this at all\r\n *\r\n * `WebStorageAdapter` addresses every asset by URL and fetches it. That is\r\n * right for an asset that lives somewhere, and wrong for one whose bytes the\r\n * host already has in hand — a file the user has just chosen, a render a local\r\n * model has just produced, a picture whose upload has not finished or cannot\r\n * start because there is no network. In all three the adapter downloads bytes\r\n * that never needed to travel.\r\n *\r\n * `NodeStorageAdapter` has had the answer since it was written: an injected\r\n * `AssetLoader`, with an `inline` map consulted ahead of the disk. This is the\r\n * same seam for the browser.\r\n *\r\n * ## Why it is module-level\r\n *\r\n * `WebStorageAdapter` is constructed at five sites *inside* these packages —\r\n * the React player backend, the exporter, the audio mixer, and both still\r\n * renderers — none of which a host can reach. A constructor argument would\r\n * therefore cover the preview and leave export and stills on the network,\r\n * which is the worst of both: the picture a user sees would come from local\r\n * bytes and the one they render would not.\r\n *\r\n * So this is an *injection point*, not a cache. The adapter still owns every\r\n * decode, every cache, every size ceiling and every eviction rule; all this\r\n * changes is where the compressed bytes came from. A source that answers `null`\r\n * — which is what an unregistered URL, an evicted entry or a host with no local\r\n * store does — leaves the adapter exactly as it was.\r\n */\r\n\r\n/** Local bytes for a source URL, or `null` to go to the network. */\r\nexport type AssetByteSource = (src: string) => Promise<Blob | null>;\r\n\r\nlet source: AssetByteSource | null = null;\r\n\r\n/**\r\n * Installs the byte source for this browsing context, or clears it with `null`.\r\n *\r\n * Called once per window that renders. Later calls replace the previous source\r\n * rather than stacking, because two sources disagreeing about one URL has no\r\n * sensible answer.\r\n */\r\nexport function setAssetByteSource(next: AssetByteSource | null): void {\r\n source = next;\r\n}\r\n\r\n/**\r\n * The bytes for `src`, or `null`.\r\n *\r\n * Swallows a throwing source on purpose. A host's local store failing is a\r\n * cache miss, not a render failure — the caller falls through to `fetch`, which\r\n * is what it would have done had the source never been installed.\r\n */\r\nexport async function localAssetBytes(src: string): Promise<Blob | null> {\r\n if (!source) return null;\r\n try {\r\n return await source(src);\r\n } catch {\r\n return null;\r\n }\r\n}\r\n\r\n/**\r\n * The network's bytes for `url`. A response that is not OK is a failed load,\r\n * never bytes: a 404 page handed to the font manager marked a family loaded\r\n * with no face in it.\r\n */\r\nexport async function fetchAssetBytes(url: string): Promise<Blob> {\r\n const response = await fetch(url);\r\n if (!response.ok) {\r\n const status = response.statusText ? `${response.status} ${response.statusText}` : `${response.status}`;\r\n throw new Error(`Fetching \"${url}\" failed: HTTP ${status}`);\r\n }\r\n return response.blob();\r\n}\r\n"]}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import { type AudioBusStop } from "@motionscript/core";
|
|
2
2
|
/**
|
|
3
3
|
* The live chains for one audio context's buses.
|
|
4
4
|
*
|
|
@@ -23,9 +23,13 @@ export declare class AudioBusGraph {
|
|
|
23
23
|
* each stop after it is that bus's own parent. An empty or absent chain
|
|
24
24
|
* returns the destination, which is what every clip did before buses
|
|
25
25
|
* existed.
|
|
26
|
+
*
|
|
27
|
+
* `startTime` is the context time a new chain's schedule starts from, and
|
|
28
|
+
* `clipTime` the same instant in the stops' own seconds. A curve on a stop
|
|
29
|
+
* runs over its `startAt`–`endAt`, so the two are what place it.
|
|
26
30
|
*/
|
|
27
|
-
inputFor(bus: readonly AudioBusStop[] | undefined, startTime: number): AudioNode;
|
|
28
|
-
/** Disconnect every live bus node. Safe to call more than once. */
|
|
31
|
+
inputFor(bus: readonly AudioBusStop[] | undefined, startTime: number, clipTime: number): AudioNode;
|
|
32
|
+
/** Disconnect every live bus node, so the next {@link inputFor} builds afresh. Safe to call more than once. */
|
|
29
33
|
dispose(): void;
|
|
30
34
|
private readonly inputs;
|
|
31
35
|
private readonly nodes;
|
|
@@ -37,6 +41,11 @@ export declare class AudioBusGraph {
|
|
|
37
41
|
* Recursive rather than iterative because a bus's *output* has to exist
|
|
38
42
|
* before its own nodes can connect to it, and the outermost bus is the last
|
|
39
43
|
* entry.
|
|
44
|
+
*
|
|
45
|
+
* Reused by everything that changes what it sounds like, down to the master,
|
|
46
|
+
* not by id alone: a bus edited under a playing device, or one id on two
|
|
47
|
+
* tracks, is a different chain, and one built for the first would play the
|
|
48
|
+
* second through stale levels and a curve anchored to the wrong span.
|
|
40
49
|
*/
|
|
41
50
|
private chainFrom;
|
|
42
51
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"bus-graph.d.ts","sourceRoot":"","sources":["../../src/audio/bus-graph.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"bus-graph.d.ts","sourceRoot":"","sources":["../../src/audio/bus-graph.ts"],"names":[],"mappings":"AAAA,OAAO,EAAc,KAAK,YAAY,EAAwB,MAAM,oBAAoB,CAAC;AAGzF;;;;;;;;GAQG;AACH,qBAAa,aAAa;IAElB,OAAO,CAAC,QAAQ,CAAC,GAAG;IACpB,+EAA+E;IAC/E,OAAO,CAAC,QAAQ,CAAC,WAAW;gBAFX,GAAG,EAAE,gBAAgB;IACtC,+EAA+E;IAC9D,WAAW,EAAE,SAAS;IAG3C;;;;;;;;;;;;OAYG;IACH,QAAQ,CAAC,GAAG,EAAE,SAAS,YAAY,EAAE,GAAG,SAAS,EAAE,SAAS,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,SAAS;IAKlG,+GAA+G;IAC/G,OAAO,IAAI,IAAI;IAYf,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAgC;IACvD,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAmB;IACzC,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAwB;IAEpD;;;;;;;;;;;;OAYG;IACH,OAAO,CAAC,SAAS;CA+BpB"}
|
package/dist/audio/bus-graph.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { busStopKey } from "@motionscript/core";
|
|
1
2
|
import { buildAudioFilterGraph } from "./filter-graph";
|
|
2
3
|
/**
|
|
3
4
|
* The live chains for one audio context's buses.
|
|
@@ -25,13 +26,17 @@ export class AudioBusGraph {
|
|
|
25
26
|
* each stop after it is that bus's own parent. An empty or absent chain
|
|
26
27
|
* returns the destination, which is what every clip did before buses
|
|
27
28
|
* existed.
|
|
29
|
+
*
|
|
30
|
+
* `startTime` is the context time a new chain's schedule starts from, and
|
|
31
|
+
* `clipTime` the same instant in the stops' own seconds. A curve on a stop
|
|
32
|
+
* runs over its `startAt`–`endAt`, so the two are what place it.
|
|
28
33
|
*/
|
|
29
|
-
inputFor(bus, startTime) {
|
|
34
|
+
inputFor(bus, startTime, clipTime) {
|
|
30
35
|
if (!bus || bus.length === 0)
|
|
31
36
|
return this.destination;
|
|
32
|
-
return this.chainFrom(bus, 0, startTime);
|
|
37
|
+
return this.chainFrom(bus, 0, startTime, clipTime);
|
|
33
38
|
}
|
|
34
|
-
/** Disconnect every live bus node. Safe to call more than once. */
|
|
39
|
+
/** Disconnect every live bus node, so the next {@link inputFor} builds afresh. Safe to call more than once. */
|
|
35
40
|
dispose() {
|
|
36
41
|
for (const node of this.nodes) {
|
|
37
42
|
try {
|
|
@@ -59,46 +64,36 @@ export class AudioBusGraph {
|
|
|
59
64
|
* Recursive rather than iterative because a bus's *output* has to exist
|
|
60
65
|
* before its own nodes can connect to it, and the outermost bus is the last
|
|
61
66
|
* entry.
|
|
67
|
+
*
|
|
68
|
+
* Reused by everything that changes what it sounds like, down to the master,
|
|
69
|
+
* not by id alone: a bus edited under a playing device, or one id on two
|
|
70
|
+
* tracks, is a different chain, and one built for the first would play the
|
|
71
|
+
* second through stale levels and a curve anchored to the wrong span.
|
|
62
72
|
*/
|
|
63
|
-
chainFrom(bus, index, startTime) {
|
|
73
|
+
chainFrom(bus, index, startTime, clipTime) {
|
|
64
74
|
if (index >= bus.length)
|
|
65
75
|
return this.destination;
|
|
66
76
|
const stop = bus[index];
|
|
67
|
-
const
|
|
77
|
+
const key = bus.slice(index).map(busStopKey).join(">");
|
|
78
|
+
const existing = this.inputs.get(key);
|
|
68
79
|
if (existing)
|
|
69
80
|
return existing;
|
|
70
|
-
const output = this.chainFrom(bus, index + 1, startTime);
|
|
81
|
+
const output = this.chainFrom(bus, index + 1, startTime, clipTime);
|
|
71
82
|
// A pass-through head every clip connects to, so the bus has one input
|
|
72
83
|
// whatever its filters turn out to be.
|
|
73
84
|
const input = this.ctx.createGain();
|
|
74
85
|
input.gain.value = 1;
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
graph.output.connect(level);
|
|
82
|
-
level.connect(output);
|
|
83
|
-
this.nodes.push(input, level, ...graph.nodes);
|
|
86
|
+
// The level is a gain after the filters, as a sound's own level is, so a
|
|
87
|
+
// curve on it is scheduled exactly as one in `filters` is.
|
|
88
|
+
const level = { type: "gain", value: stop.mute ? 0 : stop.volume };
|
|
89
|
+
const graph = buildAudioFilterGraph(this.ctx, input, [...(stop.filters ?? []), level], startTime, stop.endAt - stop.startAt, clipTime - stop.startAt);
|
|
90
|
+
graph.output.connect(output);
|
|
91
|
+
this.nodes.push(input, ...graph.nodes);
|
|
84
92
|
this.oscillators.push(...graph.oscillators);
|
|
85
93
|
for (const osc of graph.oscillators)
|
|
86
94
|
osc.start(startTime);
|
|
87
|
-
this.inputs.set(
|
|
95
|
+
this.inputs.set(key, input);
|
|
88
96
|
return input;
|
|
89
97
|
}
|
|
90
98
|
}
|
|
91
|
-
/**
|
|
92
|
-
* A bus's gain as a plain number.
|
|
93
|
-
*
|
|
94
|
-
* A curve-valued bus level is scheduled by the filter chain a bus carries, not
|
|
95
|
-
* here: this node is created once for the whole bus and shared by every clip on
|
|
96
|
-
* it, so there is no single clip start to resolve a curve against. Authoring a
|
|
97
|
-
* bus fade as `filters: AudioFilters.gain(fadeOut(1))` puts it on the chain,
|
|
98
|
-
* where it is scheduled against the context clock like any other.
|
|
99
|
-
*/
|
|
100
|
-
function levelOf(stop) {
|
|
101
|
-
const value = typeof stop.volume === "number" ? stop.volume : 1;
|
|
102
|
-
return Number.isFinite(value) ? value : 1;
|
|
103
|
-
}
|
|
104
99
|
//# sourceMappingURL=bus-graph.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"bus-graph.js","sourceRoot":"","sources":["../../src/audio/bus-graph.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"bus-graph.js","sourceRoot":"","sources":["../../src/audio/bus-graph.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAA2C,MAAM,oBAAoB,CAAC;AACzF,OAAO,EAAE,qBAAqB,EAAE,MAAM,gBAAgB,CAAC;AAEvD;;;;;;;;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,QAAgB;QAClF,IAAI,CAAC,GAAG,IAAI,GAAG,CAAC,MAAM,KAAK,CAAC;YAAE,OAAO,IAAI,CAAC,WAAW,CAAC;QACtD,OAAO,IAAI,CAAC,SAAS,CAAC,GAAG,EAAE,CAAC,EAAE,SAAS,EAAE,QAAQ,CAAC,CAAC;IACvD,CAAC;IAED,+GAA+G;IAC/G,OAAO;QACH,KAAK,MAAM,IAAI,IAAI,IAAI,CAAC,KAAK,EAAE,CAAC;YAC5B,IAAI,CAAC;gBAAC,IAAI,CAAC,UAAU,EAAE,CAAC;YAAC,CAAC;YAAC,MAAM,CAAC,CAAC,uBAAuB,CAAC,CAAC;QAChE,CAAC;QACD,KAAK,MAAM,GAAG,IAAI,IAAI,CAAC,WAAW,EAAE,CAAC;YACjC,IAAI,CAAC;gBAAC,GAAG,CAAC,IAAI,EAAE,CAAC;YAAC,CAAC;YAAC,MAAM,CAAC,CAAC,uCAAuC,CAAC,CAAC;QACzE,CAAC;QACD,IAAI,CAAC,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC;QACtB,IAAI,CAAC,WAAW,CAAC,MAAM,GAAG,CAAC,CAAC;QAC5B,IAAI,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC;IACxB,CAAC;IAEgB,MAAM,GAAG,IAAI,GAAG,EAAqB,CAAC;IACtC,KAAK,GAAgB,EAAE,CAAC;IACxB,WAAW,GAAqB,EAAE,CAAC;IAEpD;;;;;;;;;;;;OAYG;IACK,SAAS,CAAC,GAA4B,EAAE,KAAa,EAAE,SAAiB,EAAE,QAAgB;QAC9F,IAAI,KAAK,IAAI,GAAG,CAAC,MAAM;YAAE,OAAO,IAAI,CAAC,WAAW,CAAC;QAEjD,MAAM,IAAI,GAAG,GAAG,CAAC,KAAK,CAAC,CAAC;QACxB,MAAM,GAAG,GAAG,GAAG,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;QACvD,MAAM,QAAQ,GAAG,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;QACtC,IAAI,QAAQ;YAAE,OAAO,QAAQ,CAAC;QAE9B,MAAM,MAAM,GAAG,IAAI,CAAC,SAAS,CAAC,GAAG,EAAE,KAAK,GAAG,CAAC,EAAE,SAAS,EAAE,QAAQ,CAAC,CAAC;QAEnE,uEAAuE;QACvE,uCAAuC;QACvC,MAAM,KAAK,GAAG,IAAI,CAAC,GAAG,CAAC,UAAU,EAAE,CAAC;QACpC,KAAK,CAAC,IAAI,CAAC,KAAK,GAAG,CAAC,CAAC;QAErB,yEAAyE;QACzE,2DAA2D;QAC3D,MAAM,KAAK,GAAoB,EAAE,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,MAAM,EAAE,CAAC;QACpF,MAAM,KAAK,GAAG,qBAAqB,CAC/B,IAAI,CAAC,GAAG,EAAE,KAAK,EAAE,CAAC,GAAG,CAAC,IAAI,CAAC,OAAO,IAAI,EAAE,CAAC,EAAE,KAAK,CAAC,EACjD,SAAS,EAAE,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC,OAAO,EAAE,QAAQ,GAAG,IAAI,CAAC,OAAO,CAChE,CAAC;QACF,KAAK,CAAC,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;QAE7B,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 AudioFilterItem } from \"@motionscript/core\";\r\nimport { buildAudioFilterGraph } from \"./filter-graph\";\r\n\r\n/**\r\n * The live chains for one audio context's buses.\r\n *\r\n * Held per context rather than per clip, because that is the whole difference\r\n * between a bus and per-clip processing: every clip naming a bus connects into\r\n * the *same* nodes, so a reverb on a bus is one tail the group shares rather\r\n * than one tail each. Flattening the levels into per-clip gain would sound\r\n * right; flattening the effects would not.\r\n */\r\nexport class AudioBusGraph {\r\n constructor(\r\n private readonly ctx: BaseAudioContext,\r\n /** Where the last bus in a chain connects — the master, or the destination. */\r\n private readonly destination: AudioNode,\r\n ) { }\r\n\r\n /**\r\n * The input node a clip on `bus` should connect to, building the chain the\r\n * first time it is asked for.\r\n *\r\n * `bus` is source-adjacent first, so `bus[0]` is the one the clip feeds and\r\n * each stop after it is that bus's own parent. An empty or absent chain\r\n * returns the destination, which is what every clip did before buses\r\n * existed.\r\n *\r\n * `startTime` is the context time a new chain's schedule starts from, and\r\n * `clipTime` the same instant in the stops' own seconds. A curve on a stop\r\n * runs over its `startAt`–`endAt`, so the two are what place it.\r\n */\r\n inputFor(bus: readonly AudioBusStop[] | undefined, startTime: number, clipTime: number): AudioNode {\r\n if (!bus || bus.length === 0) return this.destination;\r\n return this.chainFrom(bus, 0, startTime, clipTime);\r\n }\r\n\r\n /** Disconnect every live bus node, so the next {@link inputFor} builds afresh. Safe to call more than once. */\r\n dispose(): void {\r\n for (const node of this.nodes) {\r\n try { node.disconnect(); } catch { /* already torn down */ }\r\n }\r\n for (const osc of this.oscillators) {\r\n try { osc.stop(); } catch { /* never started, or already stopped */ }\r\n }\r\n this.nodes.length = 0;\r\n this.oscillators.length = 0;\r\n this.inputs.clear();\r\n }\r\n\r\n private readonly inputs = new Map<string, AudioNode>();\r\n private readonly nodes: AudioNode[] = [];\r\n private readonly oscillators: OscillatorNode[] = [];\r\n\r\n /**\r\n * Build (or reuse) the chain for `bus[index]`, connected to whatever\r\n * `bus[index + 1]` resolves to.\r\n *\r\n * Recursive rather than iterative because a bus's *output* has to exist\r\n * before its own nodes can connect to it, and the outermost bus is the last\r\n * entry.\r\n *\r\n * Reused by everything that changes what it sounds like, down to the master,\r\n * not by id alone: a bus edited under a playing device, or one id on two\r\n * tracks, is a different chain, and one built for the first would play the\r\n * second through stale levels and a curve anchored to the wrong span.\r\n */\r\n private chainFrom(bus: readonly AudioBusStop[], index: number, startTime: number, clipTime: number): AudioNode {\r\n if (index >= bus.length) return this.destination;\r\n\r\n const stop = bus[index];\r\n const key = bus.slice(index).map(busStopKey).join(\">\");\r\n const existing = this.inputs.get(key);\r\n if (existing) return existing;\r\n\r\n const output = this.chainFrom(bus, index + 1, startTime, clipTime);\r\n\r\n // A pass-through head every clip connects to, so the bus has one input\r\n // whatever its filters turn out to be.\r\n const input = this.ctx.createGain();\r\n input.gain.value = 1;\r\n\r\n // The level is a gain after the filters, as a sound's own level is, so a\r\n // curve on it is scheduled exactly as one in `filters` is.\r\n const level: AudioFilterItem = { type: \"gain\", value: stop.mute ? 0 : stop.volume };\r\n const graph = buildAudioFilterGraph(\r\n this.ctx, input, [...(stop.filters ?? []), level],\r\n startTime, stop.endAt - stop.startAt, clipTime - stop.startAt,\r\n );\r\n graph.output.connect(output);\r\n\r\n this.nodes.push(input, ...graph.nodes);\r\n this.oscillators.push(...graph.oscillators);\r\n for (const osc of graph.oscillators) osc.start(startTime);\r\n\r\n this.inputs.set(key, input);\r\n return input;\r\n }\r\n}\r\n"]}
|
|
@@ -24,8 +24,9 @@ export interface AudioFilterGraph {
|
|
|
24
24
|
* by the caller (and affect timeline scheduling), not as a graph node.
|
|
25
25
|
*
|
|
26
26
|
* `startTime` is the AudioContext time the source starts; `clipDuration` is the
|
|
27
|
-
* clip
|
|
28
|
-
* seek (0 for a clean start
|
|
27
|
+
* clip-time length the curves run over (a clip's, or a bus's span); `elapsed` is
|
|
28
|
+
* clip-seconds already played at a mid-clip seek (0 for a clean start, negative
|
|
29
|
+
* before the span begins). These let curve-valued params schedule and align.
|
|
29
30
|
*
|
|
30
31
|
* Pass the already-created `source` so its `playbackRate` can be set by the
|
|
31
32
|
* caller; this function only wires nodes *after* the source.
|
|
@@ -46,13 +47,13 @@ export declare function speedCurveOf(filters: readonly AudioFilterItem[] | undef
|
|
|
46
47
|
*
|
|
47
48
|
* The curve is authored over **source time** (rate as a function of how far through
|
|
48
49
|
* the source we've played). `setValueAtTime`/`linearRampToValueAtTime` schedule in
|
|
49
|
-
* **context time**, so we walk
|
|
50
|
+
* **context time**, so we walk clip-time forward in small steps, convert each to a
|
|
50
51
|
* source-time offset via the integral inverse, read the curve there, and lay down a
|
|
51
52
|
* value point at the matching context time. `scalar` folds in any constant speed
|
|
52
53
|
* filters (they scale the whole rate uniformly).
|
|
53
54
|
*
|
|
54
|
-
* `elapsed` is
|
|
55
|
+
* `elapsed` is clip-seconds already played at a mid-clip seek, so the schedule
|
|
55
56
|
* starts at the seek point and aligns the playbackRate to the clip's true position.
|
|
56
57
|
*/
|
|
57
|
-
export declare function applySpeedToPlaybackRate(rate: AudioParam, curve: ReturnType<typeof speedCurveOf>, sourceLength: number,
|
|
58
|
+
export declare function applySpeedToPlaybackRate(rate: AudioParam, curve: ReturnType<typeof speedCurveOf>, sourceLength: number, clipDuration: number, scalar: number, startTime: number, elapsed: number): void;
|
|
58
59
|
//# sourceMappingURL=filter-graph.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"filter-graph.d.ts","sourceRoot":"","sources":["../../src/audio/filter-graph.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,
|
|
1
|
+
{"version":3,"file":"filter-graph.d.ts","sourceRoot":"","sources":["../../src/audio/filter-graph.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAA2C,MAAM,oBAAoB,CAAC;AAE9F;;;;;;;;;GASG;AACH,MAAM,WAAW,gBAAgB;IAC7B,MAAM,EAAE,SAAS,CAAC;IAClB,KAAK,EAAE,SAAS,EAAE,CAAC;IACnB,WAAW,EAAE,cAAc,EAAE,CAAC;CACjC;AA8FD;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,qBAAqB,CACjC,GAAG,EAAE,gBAAgB,EACrB,MAAM,EAAE,SAAS,EACjB,OAAO,EAAE,SAAS,eAAe,EAAE,EACnC,SAAS,GAAE,MAAwB,EACnC,YAAY,GAAE,MAAU,EACxB,OAAO,GAAE,MAAU,GACpB,gBAAgB,CAwFlB;AAYD;;;;;;GAMG;AACH,wBAAgB,cAAc,CAAC,OAAO,EAAE,SAAS,eAAe,EAAE,GAAG,SAAS,GAAG,MAAM,CAOtF;AAED,kEAAkE;AAClE,wBAAgB,YAAY,CAAC,OAAO,EAAE,SAAS,eAAe,EAAE,GAAG,SAAS,kDAM3E;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,wBAAwB,CACpC,IAAI,EAAE,UAAU,EAChB,KAAK,EAAE,UAAU,CAAC,OAAO,YAAY,CAAC,EACtC,YAAY,EAAE,MAAM,EACpB,YAAY,EAAE,MAAM,EACpB,MAAM,EAAE,MAAM,EACd,SAAS,EAAE,MAAM,EACjB,OAAO,EAAE,MAAM,GAChB,IAAI,CAgBN"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { isCurve,
|
|
1
|
+
import { isCurve, sourceTimeAtClipElapsed } from "@motionscript/core";
|
|
2
2
|
/**
|
|
3
3
|
* How many sample points per second to bake an eased curve segment into for
|
|
4
4
|
* `setValueCurveAtTime`. 200/s (≈ every 5ms) is smooth to the ear and cheap.
|
|
@@ -12,7 +12,7 @@ const CURVE_SAMPLES_PER_SECOND = 200;
|
|
|
12
12
|
* - linear segments via `setValueAtTime` + `linearRampToValueAtTime`,
|
|
13
13
|
* - eased segments baked into a `Float32Array` via `setValueCurveAtTime`.
|
|
14
14
|
*
|
|
15
|
-
* `elapsed` (
|
|
15
|
+
* `elapsed` (clip-seconds already played when a mid-clip seek starts the source)
|
|
16
16
|
* shifts the schedule back by `elapsed` so "now" reads the curve's value at the
|
|
17
17
|
* clip's *current* position, not its start. A `clamp(v)` hook lets a param keep a
|
|
18
18
|
* valid range (e.g. echo mix 0–1) at every sampled point.
|
|
@@ -53,22 +53,27 @@ function applyParam(ctx, audioParam, param, startTime, clipDuration, elapsed, cl
|
|
|
53
53
|
audioParam.setValueAtTime(finite(seg.to), Math.max(segStart, startTime));
|
|
54
54
|
continue;
|
|
55
55
|
}
|
|
56
|
+
// How far through this segment the seek lands. A segment already under way
|
|
57
|
+
// carries on from the anchor rather than starting over from its `from` —
|
|
58
|
+
// the case a bus hits whenever its first clip enters partway through it.
|
|
59
|
+
const done = Math.max(0, (startTime - segStart) / span);
|
|
60
|
+
const curveStart = Math.max(segStart, startTime);
|
|
56
61
|
if (seg.isLinear) {
|
|
57
|
-
|
|
62
|
+
if (done === 0)
|
|
63
|
+
audioParam.setValueAtTime(finite(seg.from), curveStart);
|
|
58
64
|
audioParam.linearRampToValueAtTime(finite(seg.to), segEnd);
|
|
59
65
|
}
|
|
60
66
|
else {
|
|
61
|
-
const
|
|
67
|
+
const curveDur = segEnd - curveStart;
|
|
68
|
+
if (curveDur <= 0)
|
|
69
|
+
continue;
|
|
70
|
+
const count = Math.max(2, Math.ceil(curveDur * CURVE_SAMPLES_PER_SECOND));
|
|
62
71
|
const samples = new Float32Array(count);
|
|
63
72
|
for (let i = 0; i < count; i++) {
|
|
64
|
-
const local = i / (count - 1);
|
|
73
|
+
const local = done + (1 - done) * (i / (count - 1));
|
|
65
74
|
samples[i] = finite(seg.from + (seg.to - seg.from) * seg.ease(local));
|
|
66
75
|
}
|
|
67
|
-
|
|
68
|
-
const curveStart = Math.max(segStart, startTime);
|
|
69
|
-
const curveDur = segEnd - curveStart;
|
|
70
|
-
if (curveDur > 0)
|
|
71
|
-
audioParam.setValueCurveAtTime(samples, curveStart, curveDur);
|
|
76
|
+
audioParam.setValueCurveAtTime(samples, curveStart, curveDur);
|
|
72
77
|
}
|
|
73
78
|
}
|
|
74
79
|
}
|
|
@@ -82,8 +87,9 @@ function applyParam(ctx, audioParam, param, startTime, clipDuration, elapsed, cl
|
|
|
82
87
|
* by the caller (and affect timeline scheduling), not as a graph node.
|
|
83
88
|
*
|
|
84
89
|
* `startTime` is the AudioContext time the source starts; `clipDuration` is the
|
|
85
|
-
* clip
|
|
86
|
-
* seek (0 for a clean start
|
|
90
|
+
* clip-time length the curves run over (a clip's, or a bus's span); `elapsed` is
|
|
91
|
+
* clip-seconds already played at a mid-clip seek (0 for a clean start, negative
|
|
92
|
+
* before the span begins). These let curve-valued params schedule and align.
|
|
87
93
|
*
|
|
88
94
|
* Pass the already-created `source` so its `playbackRate` can be set by the
|
|
89
95
|
* caller; this function only wires nodes *after* the source.
|
|
@@ -206,29 +212,29 @@ export function speedCurveOf(filters) {
|
|
|
206
212
|
*
|
|
207
213
|
* The curve is authored over **source time** (rate as a function of how far through
|
|
208
214
|
* the source we've played). `setValueAtTime`/`linearRampToValueAtTime` schedule in
|
|
209
|
-
* **context time**, so we walk
|
|
215
|
+
* **context time**, so we walk clip-time forward in small steps, convert each to a
|
|
210
216
|
* source-time offset via the integral inverse, read the curve there, and lay down a
|
|
211
217
|
* value point at the matching context time. `scalar` folds in any constant speed
|
|
212
218
|
* filters (they scale the whole rate uniformly).
|
|
213
219
|
*
|
|
214
|
-
* `elapsed` is
|
|
220
|
+
* `elapsed` is clip-seconds already played at a mid-clip seek, so the schedule
|
|
215
221
|
* starts at the seek point and aligns the playbackRate to the clip's true position.
|
|
216
222
|
*/
|
|
217
|
-
export function applySpeedToPlaybackRate(rate, curve, sourceLength,
|
|
223
|
+
export function applySpeedToPlaybackRate(rate, curve, sourceLength, clipDuration, scalar, startTime, elapsed) {
|
|
218
224
|
if (!curve)
|
|
219
225
|
return;
|
|
220
|
-
const step = 1 / CURVE_SAMPLES_PER_SECOND; //
|
|
221
|
-
|
|
226
|
+
const step = 1 / CURVE_SAMPLES_PER_SECOND; // clip-time sampling step
|
|
227
|
+
const seekAt = Math.max(0, elapsed);
|
|
222
228
|
// Seed the value exactly at the seek point.
|
|
223
|
-
const seedSrc =
|
|
229
|
+
const seedSrc = sourceTimeAtClipElapsed(curve, sourceLength, seekAt);
|
|
224
230
|
rate.setValueAtTime(Math.max(1e-4, curve.sampleAt(seedSrc, sourceLength, 1) * scalar), startTime);
|
|
225
|
-
for (let s =
|
|
226
|
-
const
|
|
227
|
-
const srcT =
|
|
231
|
+
for (let s = seekAt + step; s <= clipDuration + step; s += step) {
|
|
232
|
+
const clampedElapsed = Math.min(s, clipDuration);
|
|
233
|
+
const srcT = sourceTimeAtClipElapsed(curve, sourceLength, clampedElapsed);
|
|
228
234
|
const value = Math.max(1e-4, curve.sampleAt(srcT, sourceLength, 1) * scalar);
|
|
229
|
-
const ctxTime = startTime + (
|
|
235
|
+
const ctxTime = startTime + (clampedElapsed - seekAt);
|
|
230
236
|
rate.linearRampToValueAtTime(value, ctxTime);
|
|
231
|
-
if (
|
|
237
|
+
if (clampedElapsed >= clipDuration)
|
|
232
238
|
break;
|
|
233
239
|
}
|
|
234
240
|
}
|