@motionscript/browser 0.1.0-alpha.3 → 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 +95 -0
- package/LICENSE +201 -201
- package/NOTICE +48 -0
- package/README.md +118 -69
- package/dist/asset-bytes.js.map +1 -1
- package/dist/audio/bus-graph.d.ts +2 -2
- package/dist/audio/bus-graph.d.ts.map +1 -1
- package/dist/audio/bus-graph.js +6 -6
- package/dist/audio/bus-graph.js.map +1 -1
- package/dist/audio/filter-graph.d.ts +5 -5
- package/dist/audio/filter-graph.d.ts.map +1 -1
- package/dist/audio/filter-graph.js +15 -15
- package/dist/audio/filter-graph.js.map +1 -1
- package/dist/audio/index.js.map +1 -1
- package/dist/audio/loop-region.d.ts +31 -0
- package/dist/audio/loop-region.d.ts.map +1 -0
- package/dist/audio/loop-region.js +43 -0
- package/dist/audio/loop-region.js.map +1 -0
- package/dist/audio/mixer.d.ts.map +1 -1
- package/dist/audio/mixer.js +26 -10
- package/dist/audio/mixer.js.map +1 -1
- package/dist/audio/player.d.ts +4 -4
- package/dist/audio/player.d.ts.map +1 -1
- package/dist/audio/player.js +15 -14
- package/dist/audio/player.js.map +1 -1
- package/dist/browser/THIRD_PARTY_LICENSES.txt +432 -0
- package/dist/browser/audio.js +1 -1
- package/dist/browser/chunks/chunk-2MAFJWBZ.js +2 -0
- package/dist/browser/chunks/chunk-2MAFJWBZ.js.map +7 -0
- package/dist/browser/index.js +130 -276
- package/dist/browser/index.js.map +4 -4
- package/dist/browser/manifest.json +19 -2
- package/dist/concat.js.map +1 -1
- package/dist/decode/image-levels.js.map +1 -1
- package/dist/decode/levels.js.map +1 -1
- package/dist/decode/video-levels.js.map +1 -1
- package/dist/dynamic-nodes.js.map +1 -1
- package/dist/engine.d.ts.map +1 -1
- package/dist/engine.js +1 -0
- package/dist/engine.js.map +1 -1
- package/dist/exporter.d.ts +8 -2
- package/dist/exporter.d.ts.map +1 -1
- package/dist/exporter.js +8 -7
- package/dist/exporter.js.map +1 -1
- package/dist/getter.js.map +1 -1
- package/dist/index.js.map +1 -1
- package/dist/measure-context.js.map +1 -1
- package/dist/mix-timeline-audio.d.ts +2 -2
- package/dist/mix-timeline-audio.d.ts.map +1 -1
- package/dist/mix-timeline-audio.js +1 -2
- package/dist/mix-timeline-audio.js.map +1 -1
- package/dist/render-context.js.map +1 -1
- package/dist/screenshot.d.ts +3 -3
- package/dist/screenshot.d.ts.map +1 -1
- package/dist/screenshot.js +1 -2
- package/dist/screenshot.js.map +1 -1
- package/dist/still.d.ts +6 -4
- package/dist/still.d.ts.map +1 -1
- package/dist/still.js +12 -9
- package/dist/still.js.map +1 -1
- package/dist/storage-adapter.d.ts.map +1 -1
- package/dist/storage-adapter.js +5 -4
- package/dist/storage-adapter.js.map +1 -1
- package/dist/three/renderer.js.map +1 -1
- package/dist/ticker.d.ts +22 -4
- package/dist/ticker.d.ts.map +1 -1
- package/dist/ticker.js +65 -28
- package/dist/ticker.js.map +1 -1
- package/package.json +7 -6
- package/dist/browser/chunks/chunk-M7XD73ED.js +0 -2
- package/dist/browser/chunks/chunk-M7XD73ED.js.map +0 -7
- package/dist/output.d.ts +0 -7
- package/dist/output.d.ts.map +0 -1
- package/dist/output.js +0 -6
- package/dist/output.js.map +0 -1
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.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;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":["/**\n * Where a source URL's bytes come from: a host's local store, then the network.\n *\n * ## Why the web adapter needs this at all\n *\n * `WebStorageAdapter` 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 * `WebStorageAdapter` is constructed at five sites *inside* these packages —\n * the React player backend, the exporter, the audio mixer, and both still\n * renderers — none of 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"]}
|
|
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"]}
|
|
@@ -25,10 +25,10 @@ export declare class AudioBusGraph {
|
|
|
25
25
|
* existed.
|
|
26
26
|
*
|
|
27
27
|
* `startTime` is the context time a new chain's schedule starts from, and
|
|
28
|
-
* `
|
|
28
|
+
* `clipTime` the same instant in the stops' own seconds. A curve on a stop
|
|
29
29
|
* runs over its `startAt`–`endAt`, so the two are what place it.
|
|
30
30
|
*/
|
|
31
|
-
inputFor(bus: readonly AudioBusStop[] | undefined, startTime: number,
|
|
31
|
+
inputFor(bus: readonly AudioBusStop[] | undefined, startTime: number, clipTime: number): AudioNode;
|
|
32
32
|
/** Disconnect every live bus node, so the next {@link inputFor} builds afresh. Safe to call more than once. */
|
|
33
33
|
dispose(): void;
|
|
34
34
|
private readonly inputs;
|
|
@@ -1 +1 @@
|
|
|
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,
|
|
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
|
@@ -28,13 +28,13 @@ export class AudioBusGraph {
|
|
|
28
28
|
* existed.
|
|
29
29
|
*
|
|
30
30
|
* `startTime` is the context time a new chain's schedule starts from, and
|
|
31
|
-
* `
|
|
31
|
+
* `clipTime` the same instant in the stops' own seconds. A curve on a stop
|
|
32
32
|
* runs over its `startAt`–`endAt`, so the two are what place it.
|
|
33
33
|
*/
|
|
34
|
-
inputFor(bus, startTime,
|
|
34
|
+
inputFor(bus, startTime, clipTime) {
|
|
35
35
|
if (!bus || bus.length === 0)
|
|
36
36
|
return this.destination;
|
|
37
|
-
return this.chainFrom(bus, 0, startTime,
|
|
37
|
+
return this.chainFrom(bus, 0, startTime, clipTime);
|
|
38
38
|
}
|
|
39
39
|
/** Disconnect every live bus node, so the next {@link inputFor} builds afresh. Safe to call more than once. */
|
|
40
40
|
dispose() {
|
|
@@ -70,7 +70,7 @@ export class AudioBusGraph {
|
|
|
70
70
|
* tracks, is a different chain, and one built for the first would play the
|
|
71
71
|
* second through stale levels and a curve anchored to the wrong span.
|
|
72
72
|
*/
|
|
73
|
-
chainFrom(bus, index, startTime,
|
|
73
|
+
chainFrom(bus, index, startTime, clipTime) {
|
|
74
74
|
if (index >= bus.length)
|
|
75
75
|
return this.destination;
|
|
76
76
|
const stop = bus[index];
|
|
@@ -78,7 +78,7 @@ export class AudioBusGraph {
|
|
|
78
78
|
const existing = this.inputs.get(key);
|
|
79
79
|
if (existing)
|
|
80
80
|
return existing;
|
|
81
|
-
const output = this.chainFrom(bus, index + 1, startTime,
|
|
81
|
+
const output = this.chainFrom(bus, index + 1, startTime, clipTime);
|
|
82
82
|
// A pass-through head every clip connects to, so the bus has one input
|
|
83
83
|
// whatever its filters turn out to be.
|
|
84
84
|
const input = this.ctx.createGain();
|
|
@@ -86,7 +86,7 @@ export class AudioBusGraph {
|
|
|
86
86
|
// The level is a gain after the filters, as a sound's own level is, so a
|
|
87
87
|
// curve on it is scheduled exactly as one in `filters` is.
|
|
88
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,
|
|
89
|
+
const graph = buildAudioFilterGraph(this.ctx, input, [...(stop.filters ?? []), level], startTime, stop.endAt - stop.startAt, clipTime - stop.startAt);
|
|
90
90
|
graph.output.connect(output);
|
|
91
91
|
this.nodes.push(input, ...graph.nodes);
|
|
92
92
|
this.oscillators.push(...graph.oscillators);
|
|
@@ -1 +1 @@
|
|
|
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,M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C;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,8 @@ 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
|
-
*
|
|
28
|
-
*
|
|
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
29
|
* before the span begins). These let curve-valued params schedule and align.
|
|
30
30
|
*
|
|
31
31
|
* Pass the already-created `source` so its `playbackRate` can be set by the
|
|
@@ -47,13 +47,13 @@ export declare function speedCurveOf(filters: readonly AudioFilterItem[] | undef
|
|
|
47
47
|
*
|
|
48
48
|
* The curve is authored over **source time** (rate as a function of how far through
|
|
49
49
|
* the source we've played). `setValueAtTime`/`linearRampToValueAtTime` schedule in
|
|
50
|
-
* **context time**, so we walk
|
|
50
|
+
* **context time**, so we walk clip-time forward in small steps, convert each to a
|
|
51
51
|
* source-time offset via the integral inverse, read the curve there, and lay down a
|
|
52
52
|
* value point at the matching context time. `scalar` folds in any constant speed
|
|
53
53
|
* filters (they scale the whole rate uniformly).
|
|
54
54
|
*
|
|
55
|
-
* `elapsed` is
|
|
55
|
+
* `elapsed` is clip-seconds already played at a mid-clip seek, so the schedule
|
|
56
56
|
* starts at the seek point and aligns the playbackRate to the clip's true position.
|
|
57
57
|
*/
|
|
58
|
-
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;
|
|
59
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.
|
|
@@ -87,8 +87,8 @@ function applyParam(ctx, audioParam, param, startTime, clipDuration, elapsed, cl
|
|
|
87
87
|
* by the caller (and affect timeline scheduling), not as a graph node.
|
|
88
88
|
*
|
|
89
89
|
* `startTime` is the AudioContext time the source starts; `clipDuration` is the
|
|
90
|
-
*
|
|
91
|
-
*
|
|
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
92
|
* before the span begins). These let curve-valued params schedule and align.
|
|
93
93
|
*
|
|
94
94
|
* Pass the already-created `source` so its `playbackRate` can be set by the
|
|
@@ -212,29 +212,29 @@ export function speedCurveOf(filters) {
|
|
|
212
212
|
*
|
|
213
213
|
* The curve is authored over **source time** (rate as a function of how far through
|
|
214
214
|
* the source we've played). `setValueAtTime`/`linearRampToValueAtTime` schedule in
|
|
215
|
-
* **context time**, so we walk
|
|
215
|
+
* **context time**, so we walk clip-time forward in small steps, convert each to a
|
|
216
216
|
* source-time offset via the integral inverse, read the curve there, and lay down a
|
|
217
217
|
* value point at the matching context time. `scalar` folds in any constant speed
|
|
218
218
|
* filters (they scale the whole rate uniformly).
|
|
219
219
|
*
|
|
220
|
-
* `elapsed` is
|
|
220
|
+
* `elapsed` is clip-seconds already played at a mid-clip seek, so the schedule
|
|
221
221
|
* starts at the seek point and aligns the playbackRate to the clip's true position.
|
|
222
222
|
*/
|
|
223
|
-
export function applySpeedToPlaybackRate(rate, curve, sourceLength,
|
|
223
|
+
export function applySpeedToPlaybackRate(rate, curve, sourceLength, clipDuration, scalar, startTime, elapsed) {
|
|
224
224
|
if (!curve)
|
|
225
225
|
return;
|
|
226
|
-
const step = 1 / CURVE_SAMPLES_PER_SECOND; //
|
|
227
|
-
|
|
226
|
+
const step = 1 / CURVE_SAMPLES_PER_SECOND; // clip-time sampling step
|
|
227
|
+
const seekAt = Math.max(0, elapsed);
|
|
228
228
|
// Seed the value exactly at the seek point.
|
|
229
|
-
const seedSrc =
|
|
229
|
+
const seedSrc = sourceTimeAtClipElapsed(curve, sourceLength, seekAt);
|
|
230
230
|
rate.setValueAtTime(Math.max(1e-4, curve.sampleAt(seedSrc, sourceLength, 1) * scalar), startTime);
|
|
231
|
-
for (let s =
|
|
232
|
-
const
|
|
233
|
-
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);
|
|
234
234
|
const value = Math.max(1e-4, curve.sampleAt(srcT, sourceLength, 1) * scalar);
|
|
235
|
-
const ctxTime = startTime + (
|
|
235
|
+
const ctxTime = startTime + (clampedElapsed - seekAt);
|
|
236
236
|
rate.linearRampToValueAtTime(value, ctxTime);
|
|
237
|
-
if (
|
|
237
|
+
if (clampedElapsed >= clipDuration)
|
|
238
238
|
break;
|
|
239
239
|
}
|
|
240
240
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"filter-graph.js","sourceRoot":"","sources":["../../src/audio/filter-graph.ts"],"names":[],"mappings":"AAAA,OAAO,EAA0B,OAAO,EAAE,wBAAwB,EAAE,MAAM,oBAAoB,CAAC;AAkB/F;;;GAGG;AACH,MAAM,wBAAwB,GAAG,GAAG,CAAC;AAErC;;;;;;;;;;;;GAYG;AACH,SAAS,UAAU,CACf,GAAqB,EACrB,UAAsB,EACtB,KAAY,EACZ,SAAiB,EACjB,YAAoB,EACpB,OAAe,EACf,QAA+B,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;IAEvC,+EAA+E;IAC/E,iFAAiF;IACjF,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,OAAO,CAAC,KAAK,CAAC,EAAE,CAAC;QAClB,UAAU,CAAC,KAAK,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC;QACjC,OAAO;IACX,CAAC;IAED,oFAAoF;IACpF,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,YAAY,CAAC,IAAI,YAAY,IAAI,CAAC,EAAE,CAAC;QACtD,UAAU,CAAC,KAAK,GAAG,MAAM,CAAC,KAAK,CAAC,WAAW,CAAC,UAAU,CAAC,KAAK,CAAC,CAAC,CAAC;QAC/D,OAAO;IACX,CAAC;IAED,MAAM,IAAI,GAAG,SAAS,GAAG,OAAO,CAAC,CAAC,yCA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AG,KAAK,CAAC,CAAC;QACnD,IAAI,CAAC,uBAAuB,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;QAC7C,IAAI,YAAY,IAAI,aAAa;YAAE,MAAM;IAC7C,CAAC;AACL,CAAC","sourcesContent":["import { AudioFilterItem, Param, isCurve, sourceTimeAtSceneElapsed } from \"@motionscript/core\";\n\n/**\n * Result of building an audio-filter graph for one source.\n *\n * - `output` is the last node in the chain; connect it downstream (e.g. to the\n * per-request gain node).\n * - `nodes` are every intermediate node, so callers can `disconnect()` them on\n * teardown.\n * - `oscillators` are LFOs (from `tremolo`) the caller must `start()` — and may\n * `stop()` — alongside the source, since oscillators need an explicit start.\n */\nexport interface AudioFilterGraph {\n output: AudioNode;\n nodes: AudioNode[];\n oscillators: OscillatorNode[];\n}\n\n/**\n * How many sample points per second to bake an eased curve segment into for\n * `setValueCurveAtTime`. 200/s (≈ every 5ms) is smooth to the ear and cheap.\n */\nconst CURVE_SAMPLES_PER_SECOND = 200;\n\n/**\n * Apply a (possibly time-varying) {@link Param} to a Web Audio {@link AudioParam}.\n *\n * - **number** → set once (today's behavior; zero scheduling overhead).\n * - **Curve** → resolve against `clipDuration` and schedule each segment:\n * - linear segments via `setValueAtTime` + `linearRampToValueAtTime`,\n * - eased segments baked into a `Float32Array` via `setValueCurveAtTime`.\n *\n * `elapsed` (scene-seconds already played when a mid-clip seek starts the source)\n * shifts the schedule back by `elapsed` so \"now\" reads the curve's value at the\n * clip's *current* position, not its start. A `clamp(v)` hook lets a param keep a\n * valid range (e.g. echo mix 0–1) at every sampled point.\n */\nfunction applyParam(\n ctx: BaseAudioContext,\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 // Coerce to a finite value before touching an AudioParam — the platform throws\n // on NaN/Infinity. Falls back to the current param value (never undefined here).\n const finite = (v: number): number => {\n const c = clamp(v);\n return Number.isFinite(c) ? c : audioParam.value;\n };\n\n if (!isCurve(param)) {\n audioParam.value = finite(param);\n return;\n }\n\n // A non-finite clip length yields no usable schedule; hold the curve's start value.\n if (!Number.isFinite(clipDuration) || clipDuration <= 0) {\n audioParam.value = finite(param.staticValue(audioParam.value));\n return;\n }\n\n const base = startTime - elapsed; // map curve t=0 to here so seeks line up\n const resolved = param.resolve(clipDuration, audioParam.value);\n if (resolved.length === 0) return;\n\n // Anchor the starting value at `startTime` (the seek point) so there is no\n // jump from the param's default before the first scheduled segment runs.\n const startValue = param.sampleAt(Math.max(0, elapsed), clipDuration, audioParam.value);\n audioParam.setValueAtTime(finite(startValue), startTime);\n\n for (const seg of resolved) {\n const segStart = base + seg.startTime;\n const segEnd = base + seg.endTime;\n // Skip segments wholly in the past relative to the seek point, or any with a\n // non-finite boundary (degenerate resolve) that would throw on schedule.\n if (segEnd <= startTime || !Number.isFinite(segStart) || !Number.isFinite(segEnd)) continue;\n\n const span = seg.endTime - seg.startTime;\n\n if (span <= 0) {\n audioParam.setValueAtTime(finite(seg.to), Math.max(segStart, startTime));\n continue;\n }\n\n // How far through this segment the seek lands. A segment already under way\n // carries on from the anchor rather than starting over from its `from` —\n // the case a bus hits whenever its first clip enters partway through it.\n const done = Math.max(0, (startTime - segStart) / span);\n const curveStart = Math.max(segStart, startTime);\n\n if (seg.isLinear) {\n if (done === 0) audioParam.setValueAtTime(finite(seg.from), curveStart);\n audioParam.linearRampToValueAtTime(finite(seg.to), segEnd);\n } else {\n const curveDur = segEnd - curveStart;\n if (curveDur <= 0) continue;\n const count = Math.max(2, Math.ceil(curveDur * CURVE_SAMPLES_PER_SECOND));\n const samples = new Float32Array(count);\n for (let i = 0; i < count; i++) {\n const local = done + (1 - done) * (i / (count - 1));\n samples[i] = finite(seg.from + (seg.to - seg.from) * seg.ease(local));\n }\n audioParam.setValueCurveAtTime(samples, curveStart, curveDur);\n }\n }\n}\n\n/**\n * Builds the Web Audio graph for a clip's filter chain, returning the node to\n * connect downstream. Shared by live playback ({@link WebAudioSink}) and\n * offline export (`mixAudio`) so both paths sound identical.\n *\n * Filters apply in array order (index 0 closest to the source). `speed` filters\n * are intentionally ignored here — they're realized via `source.playbackRate`\n * by the caller (and affect timeline scheduling), not as a graph node.\n *\n * `startTime` is the AudioContext time the source starts; `clipDuration` is the\n * scene-time length the curves run over (a clip's, or a bus's span); `elapsed` is\n * scene-seconds already played at a mid-clip seek (0 for a clean start, negative\n * before the span begins). These let curve-valued params schedule and align.\n *\n * Pass the already-created `source` so its `playbackRate` can be set by the\n * caller; this function only wires nodes *after* the source.\n */\nexport function buildAudioFilterGraph(\n ctx: BaseAudioContext,\n source: AudioNode,\n filters: readonly AudioFilterItem[],\n startTime: number = ctx.currentTime,\n clipDuration: number = 0,\n elapsed: number = 0,\n): AudioFilterGraph {\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(ctx, p, v, startTime, clipDuration, elapsed, clamp);\n\n for (const filter of filters) {\n switch (filter.type) {\n case \"gain\": {\n const gain = ctx.createGain();\n apply(gain.gain, filter.value);\n series(gain);\n break;\n }\n case \"highpass\":\n case \"lowpass\": {\n const biquad = ctx.createBiquadFilter();\n biquad.type = filter.type === \"highpass\" ? \"highpass\" : \"lowpass\";\n apply(biquad.frequency, filter.frequency);\n if (filter.q !== undefined) apply(biquad.Q, filter.q);\n series(biquad);\n break;\n }\n case \"tremolo\": {\n // Carrier gain whose value is modulated by an LFO. With depth d the\n // carrier base = (1 - d/2) and the LFO swing gain = d/2. The clamp\n // hook applies these transforms whether depth is scalar or a curve.\n const carrier = ctx.createGain();\n const lfo = ctx.createOscillator();\n const lfoDepth = ctx.createGain();\n\n apply(carrier.gain, filter.depth, (d) => 1 - clamp01(d) / 2);\n apply(lfoDepth.gain, filter.depth, (d) => clamp01(d) / 2);\n apply(lfo.frequency, filter.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 // Dry + wet split. The wet path is a delay with feedback; both sum\n // into a merge node that becomes the new cursor.\n const split = cursor;\n const merge = ctx.createGain();\n\n const dry = ctx.createGain();\n apply(dry.gain, filter.mix ?? 0.5, (m) => 1 - clamp01(m));\n split.connect(dry);\n dry.connect(merge);\n\n // Delay buffer must accommodate the max delay; size from the static value.\n const maxDelay = staticOf(filter.delay);\n const delay = ctx.createDelay(Math.max(1, maxDelay + 1));\n apply(delay.delayTime, filter.delay);\n const feedbackGain = ctx.createGain();\n apply(feedbackGain.gain, filter.feedback, (f) => Math.max(0, Math.min(0.99, f)));\n const wet = ctx.createGain();\n apply(wet.gain, filter.mix ?? 0.5, (m) => clamp01(m));\n\n split.connect(delay);\n delay.connect(feedbackGain);\n feedbackGain.connect(delay); // feedback loop\n delay.connect(wet);\n wet.connect(merge);\n\n nodes.push(dry, delay, feedbackGain, wet, merge);\n cursor = merge;\n break;\n }\n case \"speed\":\n // Realized via source.playbackRate by the caller; no graph node.\n break;\n }\n }\n\n return { output: cursor, nodes, oscillators };\n}\n\n/** Clamp to [0, 1]. */\nfunction clamp01(v: number): number {\n return Math.max(0, Math.min(1, v));\n}\n\n/** The static/representative number of a param (number itself, or curve's t=0 value). */\nfunction staticOf(p: Param): number {\n return isCurve(p) ? p.staticValue(0) : p;\n}\n\n/**\n * Net *constant* playback-rate multiplier from any scalar `speed` filters in the\n * chain (product of all), defaulting to 1. Curve-valued speeds contribute 1 here\n * and are scheduled separately by {@link applySpeedToPlaybackRate}. Mirrors\n * `Sound.effectiveSpeed()` so the renderer applies the same rate the core timeline\n * reserved time for.\n */\nexport function effectiveSpeed(filters: readonly AudioFilterItem[] | undefined): number {\n if (!filters) return 1;\n let speed = 1;\n for (const f of filters) {\n if (f.type === \"speed\" && !isCurve(f.value) && f.value > 0) speed *= f.value;\n }\n return speed;\n}\n\n/** The first curve-valued `speed` filter in the chain, if any. */\nexport function speedCurveOf(filters: readonly AudioFilterItem[] | undefined) {\n if (!filters) return undefined;\n for (const f of filters) {\n if (f.type === \"speed\" && isCurve(f.value)) return f.value;\n }\n return undefined;\n}\n\n/**\n * Schedule a time-varying speed curve onto an `AudioBufferSourceNode.playbackRate`.\n *\n * The curve is authored over **source time** (rate as a function of how far through\n * the source we've played). `setValueAtTime`/`linearRampToValueAtTime` schedule in\n * **context time**, so we walk scene-time forward in small steps, convert each to a\n * source-time offset via the integral inverse, read the curve there, and lay down a\n * value point at the matching context time. `scalar` folds in any constant speed\n * filters (they scale the whole rate uniformly).\n *\n * `elapsed` is scene-seconds already played at a mid-clip seek, so the schedule\n * starts at the seek point and aligns the playbackRate to the clip's true position.\n */\nexport function applySpeedToPlaybackRate(\n rate: AudioParam,\n curve: ReturnType<typeof speedCurveOf>,\n sourceLength: number,\n sceneDuration: number,\n scalar: number,\n startTime: number,\n elapsed: number,\n): void {\n if (!curve) return;\n const step = 1 / CURVE_SAMPLES_PER_SECOND; // scene-time sampling step\n let scene = Math.max(0, elapsed);\n // Seed the value exactly at the seek point.\n const seedSrc = sourceTimeAtSceneElapsed(curve, sourceLength, scene);\n rate.setValueAtTime(Math.max(1e-4, curve.sampleAt(seedSrc, sourceLength, 1) * scalar), startTime);\n\n for (let s = scene + step; s <= sceneDuration + step; s += step) {\n const clampedScene = Math.min(s, sceneDuration);\n const srcT = sourceTimeAtSceneElapsed(curve, sourceLength, clampedScene);\n const value = Math.max(1e-4, curve.sampleAt(srcT, sourceLength, 1) * scalar);\n const ctxTime = startTime + (clampedScene - scene);\n rate.linearRampToValueAtTime(value, ctxTime);\n if (clampedScene >= sceneDuration) break;\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"filter-graph.js","sourceRoot":"","sources":["../../src/audio/filter-graph.ts"],"names":[],"mappings":"AAAA,OAAO,EAA0B,OAAO,EAAE,uBAAuB,EAAE,MAAM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AAe;IAEf,IAAI,CAAC,KAAK;QAAE,OAAO;IACnB,MAAM,IAAI,GAAG,CAAC,GAAG,wBAAwB,CAAC,CAAC,0BAA0B;IACrE,MAAM,MAAM,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,OAAO,CAAC,CAAC;IACpC,4CAA4C;IAC5C,MAAM,OAAO,GAAG,uBAAuB,CAAC,KAAK,EAAE,YAAY,EAAE,MAAM,CAAC,CAAC;IACrE,IAAI,CAAC,cAAc,CAAC,IAAI,CAAC,GAAG,CAAC,IAAI,EAAE,KAAK,CAAC,QAAQ,CAAC,OAAO,EAAE,YAAY,EAAE,CAAC,CAAC,GAAG,MAAM,CAAC,EAAE,SAAS,CAAC,CAAC;IAElG,KAAK,IAAI,CAAC,GAAG,MAAM,GAAG,IAAI,EAAE,CAAC,IAAI,YAAY,GAAG,IAAI,EAAE,CAAC,IAAI,IAAI,EAAE,CAAC;QAC9D,MAAM,cAAc,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,YAAY,CAAC,CAAC;QACjD,MAAM,IAAI,GAAG,uBAAuB,CAAC,KAAK,EAAE,YAAY,EAAE,cAAc,CAAC,CAAC;QAC1E,MAAM,KAAK,GAAG,IAAI,CAAC,GAAG,CAAC,IAAI,EAAE,KAAK,CAAC,QAAQ,CAAC,IAAI,EAAE,YAAY,EAAE,CAAC,CAAC,GAAG,MAAM,CAAC,CAAC;QAC7E,MAAM,OAAO,GAAG,SAAS,GAAG,CAAC,cAAc,GAAG,MAAM,CAAC,CAAC;QACtD,IAAI,CAAC,uBAAuB,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;QAC7C,IAAI,cAAc,IAAI,YAAY;YAAE,MAAM;IAC9C,CAAC;AACL,CAAC","sourcesContent":["import { AudioFilterItem, Param, isCurve, sourceTimeAtClipElapsed } from \"@motionscript/core\";\r\n\r\n/**\r\n * Result of building an audio-filter graph for one source.\r\n *\r\n * - `output` is the last node in the chain; connect it downstream (e.g. to the\r\n * per-request gain node).\r\n * - `nodes` are every intermediate node, so callers can `disconnect()` them on\r\n * teardown.\r\n * - `oscillators` are LFOs (from `tremolo`) the caller must `start()` — and may\r\n * `stop()` — alongside the source, since oscillators need an explicit start.\r\n */\r\nexport interface AudioFilterGraph {\r\n output: AudioNode;\r\n nodes: AudioNode[];\r\n oscillators: OscillatorNode[];\r\n}\r\n\r\n/**\r\n * How many sample points per second to bake an eased curve segment into for\r\n * `setValueCurveAtTime`. 200/s (≈ every 5ms) is smooth to the ear and cheap.\r\n */\r\nconst CURVE_SAMPLES_PER_SECOND = 200;\r\n\r\n/**\r\n * Apply a (possibly time-varying) {@link Param} to a Web Audio {@link AudioParam}.\r\n *\r\n * - **number** → set once (today's behavior; zero scheduling overhead).\r\n * - **Curve** → resolve against `clipDuration` and schedule each segment:\r\n * - linear segments via `setValueAtTime` + `linearRampToValueAtTime`,\r\n * - eased segments baked into a `Float32Array` via `setValueCurveAtTime`.\r\n *\r\n * `elapsed` (clip-seconds already played when a mid-clip seek starts the source)\r\n * shifts the schedule back by `elapsed` so \"now\" reads the curve's value at the\r\n * clip's *current* position, not its start. A `clamp(v)` hook lets a param keep a\r\n * valid range (e.g. echo mix 0–1) at every sampled point.\r\n */\r\nfunction applyParam(\r\n ctx: BaseAudioContext,\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 // Coerce to a finite value before touching an AudioParam — the platform throws\r\n // on NaN/Infinity. Falls back to the current param value (never undefined here).\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 (!isCurve(param)) {\r\n audioParam.value = finite(param);\r\n return;\r\n }\r\n\r\n // A non-finite clip length yields no usable schedule; hold the curve's start value.\r\n if (!Number.isFinite(clipDuration) || clipDuration <= 0) {\r\n audioParam.value = finite(param.staticValue(audioParam.value));\r\n return;\r\n }\r\n\r\n const base = startTime - elapsed; // map curve t=0 to here so seeks line up\r\n const resolved = param.resolve(clipDuration, audioParam.value);\r\n if (resolved.length === 0) return;\r\n\r\n // Anchor the starting value at `startTime` (the seek point) so there is no\r\n // jump from the param's default before the first scheduled segment runs.\r\n const startValue = param.sampleAt(Math.max(0, elapsed), clipDuration, audioParam.value);\r\n audioParam.setValueAtTime(finite(startValue), startTime);\r\n\r\n for (const seg of resolved) {\r\n const segStart = base + seg.startTime;\r\n const segEnd = base + seg.endTime;\r\n // Skip segments wholly in the past relative to the seek point, or any with a\r\n // non-finite boundary (degenerate resolve) that would throw on schedule.\r\n if (segEnd <= startTime || !Number.isFinite(segStart) || !Number.isFinite(segEnd)) continue;\r\n\r\n const span = seg.endTime - seg.startTime;\r\n\r\n if (span <= 0) {\r\n audioParam.setValueAtTime(finite(seg.to), Math.max(segStart, startTime));\r\n continue;\r\n }\r\n\r\n // How far through this segment the seek lands. A segment already under way\r\n // carries on from the anchor rather than starting over from its `from` —\r\n // the case a bus hits whenever its first clip enters partway through it.\r\n const done = Math.max(0, (startTime - segStart) / span);\r\n const curveStart = Math.max(segStart, startTime);\r\n\r\n if (seg.isLinear) {\r\n if (done === 0) audioParam.setValueAtTime(finite(seg.from), curveStart);\r\n audioParam.linearRampToValueAtTime(finite(seg.to), segEnd);\r\n } else {\r\n const curveDur = segEnd - curveStart;\r\n if (curveDur <= 0) continue;\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 i = 0; i < count; i++) {\r\n const local = done + (1 - done) * (i / (count - 1));\r\n samples[i] = finite(seg.from + (seg.to - seg.from) * seg.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 filter chain, returning the node to\r\n * connect downstream. Shared by live playback ({@link WebAudioSink}) and\r\n * offline export (`mixAudio`) so both paths sound identical.\r\n *\r\n * Filters apply in array order (index 0 closest to the source). `speed` filters\r\n * are intentionally ignored here — they're realized via `source.playbackRate`\r\n * by the caller (and affect timeline scheduling), not as a graph node.\r\n *\r\n * `startTime` is the AudioContext time the source starts; `clipDuration` is the\r\n * clip-time length the curves run over (a clip's, or a bus's span); `elapsed` is\r\n * clip-seconds already played at a mid-clip seek (0 for a clean start, negative\r\n * before the span begins). These let curve-valued params schedule and align.\r\n *\r\n * Pass the already-created `source` so its `playbackRate` can be set by the\r\n * caller; this function only wires nodes *after* the source.\r\n */\r\nexport function buildAudioFilterGraph(\r\n ctx: BaseAudioContext,\r\n source: AudioNode,\r\n filters: readonly AudioFilterItem[],\r\n startTime: number = ctx.currentTime,\r\n clipDuration: number = 0,\r\n elapsed: number = 0,\r\n): AudioFilterGraph {\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(ctx, p, v, startTime, clipDuration, elapsed, clamp);\r\n\r\n for (const filter of filters) {\r\n switch (filter.type) {\r\n case \"gain\": {\r\n const gain = ctx.createGain();\r\n apply(gain.gain, filter.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 = filter.type === \"highpass\" ? \"highpass\" : \"lowpass\";\r\n apply(biquad.frequency, filter.frequency);\r\n if (filter.q !== undefined) apply(biquad.Q, filter.q);\r\n series(biquad);\r\n break;\r\n }\r\n case \"tremolo\": {\r\n // Carrier gain whose value is modulated by an LFO. With depth d the\r\n // carrier base = (1 - d/2) and the LFO swing gain = d/2. The clamp\r\n // hook applies these transforms whether depth is scalar or a curve.\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, filter.depth, (d) => 1 - clamp01(d) / 2);\r\n apply(lfoDepth.gain, filter.depth, (d) => clamp01(d) / 2);\r\n apply(lfo.frequency, filter.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 // Dry + wet split. The wet path is a delay with feedback; both sum\r\n // into a merge node that becomes the new cursor.\r\n const split = cursor;\r\n const merge = ctx.createGain();\r\n\r\n const dry = ctx.createGain();\r\n apply(dry.gain, filter.mix ?? 0.5, (m) => 1 - clamp01(m));\r\n split.connect(dry);\r\n dry.connect(merge);\r\n\r\n // Delay buffer must accommodate the max delay; size from the static value.\r\n const maxDelay = staticOf(filter.delay);\r\n const delay = ctx.createDelay(Math.max(1, maxDelay + 1));\r\n apply(delay.delayTime, filter.delay);\r\n const feedbackGain = ctx.createGain();\r\n apply(feedbackGain.gain, filter.feedback, (f) => Math.max(0, Math.min(0.99, f)));\r\n const wet = ctx.createGain();\r\n apply(wet.gain, filter.mix ?? 0.5, (m) => clamp01(m));\r\n\r\n split.connect(delay);\r\n delay.connect(feedbackGain);\r\n feedbackGain.connect(delay); // feedback loop\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 case \"speed\":\r\n // Realized via source.playbackRate by the caller; no graph node.\r\n break;\r\n }\r\n }\r\n\r\n return { output: cursor, nodes, oscillators };\r\n}\r\n\r\n/** Clamp to [0, 1]. */\r\nfunction clamp01(v: number): number {\r\n return Math.max(0, Math.min(1, v));\r\n}\r\n\r\n/** The static/representative number of a param (number itself, or curve's t=0 value). */\r\nfunction staticOf(p: Param): number {\r\n return isCurve(p) ? p.staticValue(0) : p;\r\n}\r\n\r\n/**\r\n * Net *constant* playback-rate multiplier from any scalar `speed` filters in the\r\n * chain (product of all), defaulting to 1. Curve-valued speeds contribute 1 here\r\n * and are scheduled separately by {@link applySpeedToPlaybackRate}. Mirrors\r\n * `Sound.effectiveSpeed()` so the renderer applies the same rate the core timeline\r\n * reserved time for.\r\n */\r\nexport function effectiveSpeed(filters: readonly AudioFilterItem[] | undefined): number {\r\n if (!filters) return 1;\r\n let speed = 1;\r\n for (const f of filters) {\r\n if (f.type === \"speed\" && !isCurve(f.value) && f.value > 0) speed *= f.value;\r\n }\r\n return speed;\r\n}\r\n\r\n/** The first curve-valued `speed` filter in the chain, if any. */\r\nexport function speedCurveOf(filters: readonly AudioFilterItem[] | undefined) {\r\n if (!filters) return undefined;\r\n for (const f of filters) {\r\n if (f.type === \"speed\" && isCurve(f.value)) return f.value;\r\n }\r\n return undefined;\r\n}\r\n\r\n/**\r\n * Schedule a time-varying speed curve onto an `AudioBufferSourceNode.playbackRate`.\r\n *\r\n * The curve is authored over **source time** (rate as a function of how far through\r\n * the source we've played). `setValueAtTime`/`linearRampToValueAtTime` schedule in\r\n * **context time**, so we walk clip-time forward in small steps, convert each to a\r\n * source-time offset via the integral inverse, read the curve there, and lay down a\r\n * value point at the matching context time. `scalar` folds in any constant speed\r\n * filters (they scale the whole rate uniformly).\r\n *\r\n * `elapsed` is clip-seconds already played at a mid-clip seek, so the schedule\r\n * starts at the seek point and aligns the playbackRate to the clip's true position.\r\n */\r\nexport function applySpeedToPlaybackRate(\r\n rate: AudioParam,\r\n curve: ReturnType<typeof speedCurveOf>,\r\n sourceLength: number,\r\n clipDuration: number,\r\n scalar: number,\r\n startTime: number,\r\n elapsed: number,\r\n): void {\r\n if (!curve) return;\r\n const step = 1 / CURVE_SAMPLES_PER_SECOND; // clip-time sampling step\r\n const seekAt = Math.max(0, elapsed);\r\n // Seed the value exactly at the seek point.\r\n const seedSrc = sourceTimeAtClipElapsed(curve, sourceLength, seekAt);\r\n rate.setValueAtTime(Math.max(1e-4, curve.sampleAt(seedSrc, sourceLength, 1) * scalar), startTime);\r\n\r\n for (let s = seekAt + step; s <= clipDuration + step; s += step) {\r\n const clampedElapsed = Math.min(s, clipDuration);\r\n const srcT = sourceTimeAtClipElapsed(curve, sourceLength, clampedElapsed);\r\n const value = Math.max(1e-4, curve.sampleAt(srcT, sourceLength, 1) * scalar);\r\n const ctxTime = startTime + (clampedElapsed - seekAt);\r\n rate.linearRampToValueAtTime(value, ctxTime);\r\n if (clampedElapsed >= clipDuration) break;\r\n }\r\n}\r\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,+EAA+E;AAC/E,+EAA+E;AAC/E,wEAAwE;AAExE,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: that barrel loads\n// the CanvasKit/Skia backend at module scope, and nothing here draws anything.\n// A host building an audio timeline should not pay for the WASM module.\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
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/audio/index.ts"],"names":[],"mappings":"AAAA,4EAA4E;AAC5E,qBAAqB;AACrB,EAAE;AACF,+EAA+E;AAC/E,+EAA+E;AAC/E,wEAAwE;AAExE,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\r\n// renderer attached.\r\n//\r\n// A separate entry point from the package barrel on purpose: that barrel loads\r\n// the CanvasKit/Skia backend at module scope, and nothing here draws anything.\r\n// A host building an audio timeline should not pay for the WASM module.\r\n\r\nexport { mixAudio, encodeWav, WebAudioMixer, DEFAULT_SAMPLE_RATE } from \"./mixer\";\r\nexport type { MixAudioOptions } from \"./mixer\";\r\n\r\nexport { WebAudioSink, WebAudioSink as WebAudioPlayer } from \"./player\";\r\n\r\n// The bus layer both of those share. Exported so a host writing its own device\r\n// or mixer builds the same chains, rather than a second set that drifts.\r\nexport { AudioBusGraph } from \"./bus-graph\";\r\n"]}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { AudioRequest } from "@motionscript/core";
|
|
2
|
+
/** The span of a decoded buffer a looping request repeats, in source seconds. */
|
|
3
|
+
export interface LoopRegion {
|
|
4
|
+
start: number;
|
|
5
|
+
end: number;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* The region a looping request repeats: from its in-point to its out-point, or to
|
|
9
|
+
* the end of the buffer when it has none. `null` for a request that does not loop,
|
|
10
|
+
* and for a degenerate region (an out-point at or before the in-point), where the
|
|
11
|
+
* device falls back to wrapping the whole buffer.
|
|
12
|
+
*
|
|
13
|
+
* Shared by the live device and the offline mixer so a preview and an export loop
|
|
14
|
+
* the same samples.
|
|
15
|
+
*/
|
|
16
|
+
export declare function loopRegionOf(request: AudioRequest, bufferDuration: number): LoopRegion | null;
|
|
17
|
+
/**
|
|
18
|
+
* Configure `source` to play `request`'s loop: on or off, and over the trimmed
|
|
19
|
+
* region rather than the whole buffer (`loopStart`/`loopEnd` default to the
|
|
20
|
+
* whole buffer, which is how a trim used to be lost).
|
|
21
|
+
*/
|
|
22
|
+
export declare function applyLoop(source: AudioBufferSourceNode, request: AudioRequest): LoopRegion | null;
|
|
23
|
+
/**
|
|
24
|
+
* Fold a source offset into a loop region.
|
|
25
|
+
*
|
|
26
|
+
* A seek into the middle of a looping clip lands at `trimStart + elapsed`, which
|
|
27
|
+
* can be many passes past the region's end; the device would not wrap an offset
|
|
28
|
+
* that starts outside the region, so it is wrapped here.
|
|
29
|
+
*/
|
|
30
|
+
export declare function wrapIntoRegion(region: LoopRegion | null, offset: number): number;
|
|
31
|
+
//# sourceMappingURL=loop-region.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"loop-region.d.ts","sourceRoot":"","sources":["../../src/audio/loop-region.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAEvD,iFAAiF;AACjF,MAAM,WAAW,UAAU;IACvB,KAAK,EAAE,MAAM,CAAC;IACd,GAAG,EAAE,MAAM,CAAC;CACf;AAED;;;;;;;;GAQG;AACH,wBAAgB,YAAY,CAAC,OAAO,EAAE,YAAY,EAAE,cAAc,EAAE,MAAM,GAAG,UAAU,GAAG,IAAI,CAK7F;AAED;;;;GAIG;AACH,wBAAgB,SAAS,CAAC,MAAM,EAAE,qBAAqB,EAAE,OAAO,EAAE,YAAY,GAAG,UAAU,GAAG,IAAI,CAQjG;AAED;;;;;;GAMG;AACH,wBAAgB,cAAc,CAAC,MAAM,EAAE,UAAU,GAAG,IAAI,EAAE,MAAM,EAAE,MAAM,GAAG,MAAM,CAGhF"}
|