@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.
Files changed (90) hide show
  1. package/CHANGELOG.md +148 -0
  2. package/LICENSE +201 -201
  3. package/NOTICE +48 -0
  4. package/README.md +118 -69
  5. package/dist/asset-bytes.d.ts +7 -1
  6. package/dist/asset-bytes.d.ts.map +1 -1
  7. package/dist/asset-bytes.js +14 -1
  8. package/dist/asset-bytes.js.map +1 -1
  9. package/dist/audio/bus-graph.d.ts +12 -3
  10. package/dist/audio/bus-graph.d.ts.map +1 -1
  11. package/dist/audio/bus-graph.js +24 -29
  12. package/dist/audio/bus-graph.js.map +1 -1
  13. package/dist/audio/filter-graph.d.ts +6 -5
  14. package/dist/audio/filter-graph.d.ts.map +1 -1
  15. package/dist/audio/filter-graph.js +29 -23
  16. package/dist/audio/filter-graph.js.map +1 -1
  17. package/dist/audio/index.js.map +1 -1
  18. package/dist/audio/loop-region.d.ts +31 -0
  19. package/dist/audio/loop-region.d.ts.map +1 -0
  20. package/dist/audio/loop-region.js +43 -0
  21. package/dist/audio/loop-region.js.map +1 -0
  22. package/dist/audio/mixer.d.ts.map +1 -1
  23. package/dist/audio/mixer.js +30 -11
  24. package/dist/audio/mixer.js.map +1 -1
  25. package/dist/audio/player.d.ts +4 -4
  26. package/dist/audio/player.d.ts.map +1 -1
  27. package/dist/audio/player.js +19 -15
  28. package/dist/audio/player.js.map +1 -1
  29. package/dist/browser/THIRD_PARTY_LICENSES.txt +432 -0
  30. package/dist/browser/audio.js +1 -1
  31. package/dist/browser/chunks/chunk-2MAFJWBZ.js +2 -0
  32. package/dist/browser/chunks/chunk-2MAFJWBZ.js.map +7 -0
  33. package/dist/browser/index.js +133 -279
  34. package/dist/browser/index.js.map +4 -4
  35. package/dist/browser/manifest.json +19 -2
  36. package/dist/concat.js.map +1 -1
  37. package/dist/decode/image-levels.js.map +1 -1
  38. package/dist/decode/levels.js.map +1 -1
  39. package/dist/decode/video-levels.js.map +1 -1
  40. package/dist/dynamic-nodes.d.ts.map +1 -1
  41. package/dist/dynamic-nodes.js +5 -1
  42. package/dist/dynamic-nodes.js.map +1 -1
  43. package/dist/engine.d.ts.map +1 -1
  44. package/dist/engine.js +18 -5
  45. package/dist/engine.js.map +1 -1
  46. package/dist/exporter.d.ts +10 -2
  47. package/dist/exporter.d.ts.map +1 -1
  48. package/dist/exporter.js +8 -7
  49. package/dist/exporter.js.map +1 -1
  50. package/dist/getter.js.map +1 -1
  51. package/dist/index.d.ts +1 -0
  52. package/dist/index.d.ts.map +1 -1
  53. package/dist/index.js +2 -27
  54. package/dist/index.js.map +1 -1
  55. package/dist/measure-context.js.map +1 -1
  56. package/dist/mix-timeline-audio.d.ts +2 -2
  57. package/dist/mix-timeline-audio.d.ts.map +1 -1
  58. package/dist/mix-timeline-audio.js +1 -2
  59. package/dist/mix-timeline-audio.js.map +1 -1
  60. package/dist/render-context.d.ts +5 -3
  61. package/dist/render-context.d.ts.map +1 -1
  62. package/dist/render-context.js +7 -3
  63. package/dist/render-context.js.map +1 -1
  64. package/dist/screenshot.d.ts +3 -3
  65. package/dist/screenshot.d.ts.map +1 -1
  66. package/dist/screenshot.js +1 -2
  67. package/dist/screenshot.js.map +1 -1
  68. package/dist/still.d.ts +18 -8
  69. package/dist/still.d.ts.map +1 -1
  70. package/dist/still.js +28 -21
  71. package/dist/still.js.map +1 -1
  72. package/dist/storage-adapter.d.ts +1 -1
  73. package/dist/storage-adapter.d.ts.map +1 -1
  74. package/dist/storage-adapter.js +12 -10
  75. package/dist/storage-adapter.js.map +1 -1
  76. package/dist/three/renderer.d.ts +10 -11
  77. package/dist/three/renderer.d.ts.map +1 -1
  78. package/dist/three/renderer.js +18 -13
  79. package/dist/three/renderer.js.map +1 -1
  80. package/dist/ticker.d.ts +22 -4
  81. package/dist/ticker.d.ts.map +1 -1
  82. package/dist/ticker.js +65 -28
  83. package/dist/ticker.js.map +1 -1
  84. package/package.json +8 -6
  85. package/dist/browser/chunks/chunk-NSFLRI4P.js +0 -2
  86. package/dist/browser/chunks/chunk-NSFLRI4P.js.map +0 -7
  87. package/dist/output.d.ts +0 -7
  88. package/dist/output.d.ts.map +0 -1
  89. package/dist/output.js +0 -6
  90. 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`](../canvaskit). It implements the
5
- `platform`/`render` abstractions from [`@motionscript/core`](../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
- ## What's in here
10
-
11
- - **Engine**: `createBrowserEngine` wires every platform seam below into a
12
- `@motionscript/core` `Engine`. Await it once at app start and share it.
13
- - **Render context**: `WebRenderContext` draws the `@motionscript/core` scene
14
- graph (shapes, fills, strokes, effects, text, video, masks) onto a CanvasKit
15
- surface.
16
- - **Exporter**: `exportVideo` renders a document offline and muxes video and
17
- audio into an MP4 using [mediabunny](https://github.com/Vanilagy/mediabunny).
18
- - **Stills**: `StillRenderer` (from `createStillRenderer`) is a long-lived
19
- renderer that repaints single frames into a canvas the host owns, for
20
- thumbnails and previews; `exportScreenshot` is the one-shot wrapper over it.
21
- - **Concat**: `concatVideoSegments` joins separately encoded MP4 segments
22
- without re-encoding, so an export can be split across workers.
23
- `mixTimelineAudio` mixes the whole timeline's audio once for the joined file.
24
- Every segment must share one encoder configuration; a mismatch throws
25
- `ConcatMismatchError`.
26
- - **3D**: importing this package registers the WebGL renderer that makes
27
- `Canvas3D` draw (three.js, loaded lazily). Import from the package root,
28
- not a deep path, or 3D renders empty.
29
- - **Audio**: `WebAudioPlayer` drives playback through the Web Audio API;
30
- `mixAudio` and `encodeWav` (also on the `@motionscript/browser/audio`
31
- subpath, which skips CanvasKit) mix down offline.
32
- - **Storage / assets**: `WebStorageAdapter` and `WebMeasureContext` implement the
33
- asset-loading and text-measurement abstractions `@motionscript/core` needs.
34
- - **Clock**: `WebTicker` synchronizes scene playback to `requestAnimationFrame` while calculating dt from audio context to ensure audio and animation line up.
35
- - **CanvasKit access**: `getCanvasKit` loads and caches the CanvasKit/Skia
36
- WebAssembly module used for rendering.
37
-
38
- ## Usage
39
-
40
- ```bash
41
- npm install @motionscript/browser @motionscript/core @motionscript/canvaskit
42
- ```
43
-
44
- ```ts
45
- import { createBrowserEngine } from '@motionscript/browser';
46
-
47
- const engine = await createBrowserEngine({ wasmUrl: '/canvaskit.wasm', nodes: [MyNode] });
48
- const session = engine.open({ document, target: canvasEl });
49
- session.playback!.play();
50
-
51
- const mp4 = await session.export();
52
- ```
53
-
54
- Serve `canvaskit.wasm` from `@motionscript/canvaskit` as a static asset and pass
55
- its URL as `wasmUrl`.
56
-
57
- See the [docs](https://motionscript.dev/docs) for the full feature set and API
58
- reference.
59
-
60
- ## Development
61
-
62
- From the monorepo root:
63
-
64
- ```bash
65
- pnpm --filter @motionscript/browser build
66
- pnpm --filter @motionscript/browser test
67
- ```
68
-
69
- See [CONTRIBUTING.md](../../CONTRIBUTING.md) for the architecture overview.
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.
@@ -1,5 +1,5 @@
1
1
  /**
2
- * Where a source URL's bytes may come from, other than the network.
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"}
@@ -1,5 +1,5 @@
1
1
  /**
2
- * Where a source URL's bytes may come from, other than the network.
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
@@ -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 may come from, other than 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"]}
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 { AudioBusStop } from "@motionscript/core";
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,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAGvD;;;;;;;;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;;;;;;;;OAQG;IACH,QAAQ,CAAC,GAAG,EAAE,SAAS,YAAY,EAAE,GAAG,SAAS,EAAE,SAAS,EAAE,MAAM,GAAG,SAAS;IAKhF,mEAAmE;IACnE,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;;;;;;;OAOG;IACH,OAAO,CAAC,SAAS;CA+BpB"}
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"}
@@ -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 existing = this.inputs.get(stop.id);
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
- const level = this.ctx.createGain();
76
- level.gain.value = stop.mute ? 0 : levelOf(stop);
77
- // `clipDuration` is 0 and `elapsed` 0: a bus is not a clip, so a curve on
78
- // one is authored against the timeline rather than against a source's
79
- // length. `applyParam` resolves a curve of its own duration from that.
80
- const graph = buildAudioFilterGraph(this.ctx, input, stop.filters ?? [], startTime, 0, 0);
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(stop.id, input);
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":"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","sourcesContent":["import type { AudioBusStop } from \"@motionscript/core\";\nimport { buildAudioFilterGraph } from \"./filter-graph\";\n\n/**\n * The live chains for one audio context's buses.\n *\n * Held per context rather than per clip, because that is the whole difference\n * between a bus and per-clip processing: every clip naming a bus connects into\n * the *same* nodes, so a reverb on a bus is one tail the group shares rather\n * than one tail each. Flattening the levels into per-clip gain would sound\n * right; flattening the effects would not.\n */\nexport class AudioBusGraph {\n constructor(\n private readonly ctx: BaseAudioContext,\n /** Where the last bus in a chain connects — the master, or the destination. */\n private readonly destination: AudioNode,\n ) { }\n\n /**\n * The input node a clip on `bus` should connect to, building the chain the\n * first time it is asked for.\n *\n * `bus` is source-adjacent first, so `bus[0]` is the one the clip feeds and\n * each stop after it is that bus's own parent. An empty or absent chain\n * returns the destination, which is what every clip did before buses\n * existed.\n */\n inputFor(bus: readonly AudioBusStop[] | undefined, startTime: number): AudioNode {\n if (!bus || bus.length === 0) return this.destination;\n return this.chainFrom(bus, 0, startTime);\n }\n\n /** Disconnect every live bus node. Safe to call more than once. */\n dispose(): void {\n for (const node of this.nodes) {\n try { node.disconnect(); } catch { /* already torn down */ }\n }\n for (const osc of this.oscillators) {\n try { osc.stop(); } catch { /* never started, or already stopped */ }\n }\n this.nodes.length = 0;\n this.oscillators.length = 0;\n this.inputs.clear();\n }\n\n private readonly inputs = new Map<string, AudioNode>();\n private readonly nodes: AudioNode[] = [];\n private readonly oscillators: OscillatorNode[] = [];\n\n /**\n * Build (or reuse) the chain for `bus[index]`, connected to whatever\n * `bus[index + 1]` resolves to.\n *\n * Recursive rather than iterative because a bus's *output* has to exist\n * before its own nodes can connect to it, and the outermost bus is the last\n * entry.\n */\n private chainFrom(bus: readonly AudioBusStop[], index: number, startTime: number): AudioNode {\n if (index >= bus.length) return this.destination;\n\n const stop = bus[index];\n const existing = this.inputs.get(stop.id);\n if (existing) return existing;\n\n const output = this.chainFrom(bus, index + 1, startTime);\n\n // A pass-through head every clip connects to, so the bus has one input\n // whatever its filters turn out to be.\n const input = this.ctx.createGain();\n input.gain.value = 1;\n\n const level = this.ctx.createGain();\n level.gain.value = stop.mute ? 0 : levelOf(stop);\n\n // `clipDuration` is 0 and `elapsed` 0: a bus is not a clip, so a curve on\n // one is authored against the timeline rather than against a source's\n // length. `applyParam` resolves a curve of its own duration from that.\n const graph = buildAudioFilterGraph(this.ctx, input, stop.filters ?? [], startTime, 0, 0);\n graph.output.connect(level);\n level.connect(output);\n\n this.nodes.push(input, level, ...graph.nodes);\n this.oscillators.push(...graph.oscillators);\n for (const osc of graph.oscillators) osc.start(startTime);\n\n this.inputs.set(stop.id, input);\n return input;\n }\n}\n\n/**\n * A bus's gain as a plain number.\n *\n * A curve-valued bus level is scheduled by the filter chain a bus carries, not\n * here: this node is created once for the whole bus and shared by every clip on\n * it, so there is no single clip start to resolve a curve against. Authoring a\n * bus fade as `filters: AudioFilters.gain(fadeOut(1))` puts it on the chain,\n * where it is scheduled against the context clock like any other.\n */\nfunction levelOf(stop: AudioBusStop): number {\n const value = typeof stop.volume === \"number\" ? stop.volume : 1;\n return Number.isFinite(value) ? value : 1;\n}\n"]}
1
+ {"version":3,"file":"bus-graph.js","sourceRoot":"","sources":["../../src/audio/bus-graph.ts"],"names":[],"mappings":"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","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's scene-time length; `elapsed` is scene-seconds already played at a mid-clip
28
- * seek (0 for a clean start). These let curve-valued params schedule and align.
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 scene-time forward in small steps, convert each to a
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 scene-seconds already played at a mid-clip seek, so the schedule
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, sceneDuration: number, scalar: number, startTime: number, elapsed: number): void;
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,EAA4C,MAAM,oBAAoB,CAAC;AAE/F;;;;;;;;;GASG;AACH,MAAM,WAAW,gBAAgB;IAC7B,MAAM,EAAE,SAAS,CAAC;IAClB,KAAK,EAAE,SAAS,EAAE,CAAC;IACnB,WAAW,EAAE,cAAc,EAAE,CAAC;CACjC;AAyFD;;;;;;;;;;;;;;;GAeG;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,aAAa,EAAE,MAAM,EACrB,MAAM,EAAE,MAAM,EACd,SAAS,EAAE,MAAM,EACjB,OAAO,EAAE,MAAM,GAChB,IAAI,CAgBN"}
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, sourceTimeAtSceneElapsed } from "@motionscript/core";
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` (scene-seconds already played when a mid-clip seek starts the source)
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
- audioParam.setValueAtTime(finite(seg.from), Math.max(segStart, startTime));
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 count = Math.max(2, Math.ceil(span * CURVE_SAMPLES_PER_SECOND));
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
- // setValueCurveAtTime can't start in the past; clamp the start forward.
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's scene-time length; `elapsed` is scene-seconds already played at a mid-clip
86
- * seek (0 for a clean start). These let curve-valued params schedule and align.
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 scene-time forward in small steps, convert each to a
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 scene-seconds already played at a mid-clip seek, so the schedule
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, sceneDuration, scalar, startTime, elapsed) {
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; // scene-time sampling step
221
- let scene = Math.max(0, elapsed);
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 = sourceTimeAtSceneElapsed(curve, sourceLength, scene);
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 = scene + step; s <= sceneDuration + step; s += step) {
226
- const clampedScene = Math.min(s, sceneDuration);
227
- const srcT = sourceTimeAtSceneElapsed(curve, sourceLength, clampedScene);
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 + (clampedScene - scene);
235
+ const ctxTime = startTime + (clampedElapsed - seekAt);
230
236
  rate.linearRampToValueAtTime(value, ctxTime);
231
- if (clampedScene >= sceneDuration)
237
+ if (clampedElapsed >= clipDuration)
232
238
  break;
233
239
  }
234
240
  }