@motionscript/browser 0.0.0-stage → 0.1.0-alpha.10
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 +192 -0
- package/LICENSE +201 -0
- package/NOTICE +49 -0
- package/README.md +140 -3
- package/dist/asset-bytes.d.ts +59 -0
- package/dist/asset-bytes.d.ts.map +1 -0
- package/dist/asset-bytes.js +78 -0
- package/dist/asset-bytes.js.map +1 -0
- package/dist/audio/bus-graph.d.ts +52 -0
- package/dist/audio/bus-graph.d.ts.map +1 -0
- package/dist/audio/bus-graph.js +108 -0
- package/dist/audio/bus-graph.js.map +1 -0
- package/dist/audio/effect-graph.d.ts +62 -0
- package/dist/audio/effect-graph.d.ts.map +1 -0
- package/dist/audio/effect-graph.js +234 -0
- package/dist/audio/effect-graph.js.map +1 -0
- package/dist/audio/index.d.ts +5 -0
- package/dist/audio/index.d.ts.map +1 -0
- package/dist/audio/index.js +11 -0
- package/dist/audio/index.js.map +1 -0
- 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 +59 -0
- package/dist/audio/mixer.d.ts.map +1 -0
- package/dist/audio/mixer.js +192 -0
- package/dist/audio/mixer.js.map +1 -0
- package/dist/audio/player.d.ts +55 -0
- package/dist/audio/player.d.ts.map +1 -0
- package/dist/audio/player.js +217 -0
- package/dist/audio/player.js.map +1 -0
- package/dist/browser/THIRD_PARTY_LICENSES.txt +784 -0
- package/dist/browser/audio.js +2 -0
- package/dist/browser/audio.js.map +7 -0
- package/dist/browser/chunks/chunk-F3AZKPHU.js +2 -0
- package/dist/browser/chunks/chunk-F3AZKPHU.js.map +7 -0
- package/dist/browser/chunks/chunk-KLXMRKTL.js +2 -0
- package/dist/browser/chunks/chunk-KLXMRKTL.js.map +7 -0
- package/dist/browser/chunks/chunk-L5UFQACH.js +163 -0
- package/dist/browser/chunks/chunk-L5UFQACH.js.map +7 -0
- package/dist/browser/chunks/mediabunny-mp3-encoder-ZCIHVVTJ.js +14 -0
- package/dist/browser/chunks/mediabunny-mp3-encoder-ZCIHVVTJ.js.map +7 -0
- package/dist/browser/esbuild.wasm +0 -0
- package/dist/browser/index.js +734 -0
- package/dist/browser/index.js.map +7 -0
- package/dist/browser/manifest.json +32 -0
- package/dist/browser/ms_renderer_bg.wasm +0 -0
- package/dist/concat.d.ts +121 -0
- package/dist/concat.d.ts.map +1 -0
- package/dist/concat.js +219 -0
- package/dist/concat.js.map +1 -0
- package/dist/deliver.d.ts +10 -0
- package/dist/deliver.d.ts.map +1 -0
- package/dist/deliver.js +22 -0
- package/dist/deliver.js.map +1 -0
- package/dist/dynamic-nodes.d.ts +9 -0
- package/dist/dynamic-nodes.d.ts.map +1 -0
- package/dist/dynamic-nodes.js +53 -0
- package/dist/dynamic-nodes.js.map +1 -0
- package/dist/engine.d.ts +87 -0
- package/dist/engine.d.ts.map +1 -0
- package/dist/engine.js +193 -0
- package/dist/engine.js.map +1 -0
- package/dist/export-audio.d.ts +36 -0
- package/dist/export-audio.d.ts.map +1 -0
- package/dist/export-audio.js +56 -0
- package/dist/export-audio.js.map +1 -0
- package/dist/exporter.d.ts +282 -0
- package/dist/exporter.d.ts.map +1 -0
- package/dist/exporter.js +308 -0
- package/dist/exporter.js.map +1 -0
- package/dist/formats.d.ts +36 -0
- package/dist/formats.d.ts.map +1 -0
- package/dist/formats.js +83 -0
- package/dist/formats.js.map +1 -0
- package/dist/frame-loop.d.ts +190 -0
- package/dist/frame-loop.d.ts.map +1 -0
- package/dist/frame-loop.js +304 -0
- package/dist/frame-loop.js.map +1 -0
- package/dist/index.d.ts +24 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +43 -0
- package/dist/index.js.map +1 -0
- package/dist/mix-timeline-audio.d.ts +42 -0
- package/dist/mix-timeline-audio.d.ts.map +1 -0
- package/dist/mix-timeline-audio.js +60 -0
- package/dist/mix-timeline-audio.js.map +1 -0
- package/dist/parallel/export-parallel.d.ts +32 -0
- package/dist/parallel/export-parallel.d.ts.map +1 -0
- package/dist/parallel/export-parallel.js +114 -0
- package/dist/parallel/export-parallel.js.map +1 -0
- package/dist/parallel/governor.d.ts +57 -0
- package/dist/parallel/governor.d.ts.map +1 -0
- package/dist/parallel/governor.js +79 -0
- package/dist/parallel/governor.js.map +1 -0
- package/dist/parallel/lane.d.ts +45 -0
- package/dist/parallel/lane.d.ts.map +1 -0
- package/dist/parallel/lane.js +2 -0
- package/dist/parallel/lane.js.map +1 -0
- package/dist/parallel/lanes.d.ts +20 -0
- package/dist/parallel/lanes.d.ts.map +1 -0
- package/dist/parallel/lanes.js +152 -0
- package/dist/parallel/lanes.js.map +1 -0
- package/dist/parallel/plan.d.ts +23 -0
- package/dist/parallel/plan.d.ts.map +1 -0
- package/dist/parallel/plan.js +30 -0
- package/dist/parallel/plan.js.map +1 -0
- package/dist/parallel/run-lanes.d.ts +38 -0
- package/dist/parallel/run-lanes.d.ts.map +1 -0
- package/dist/parallel/run-lanes.js +164 -0
- package/dist/parallel/run-lanes.js.map +1 -0
- package/dist/parallel/session.d.ts +9 -0
- package/dist/parallel/session.d.ts.map +1 -0
- package/dist/parallel/session.js +79 -0
- package/dist/parallel/session.js.map +1 -0
- package/dist/renderer.d.ts +22 -0
- package/dist/renderer.d.ts.map +1 -0
- package/dist/renderer.js +23 -0
- package/dist/renderer.js.map +1 -0
- package/dist/screenshot.d.ts +45 -0
- package/dist/screenshot.d.ts.map +1 -0
- package/dist/screenshot.js +31 -0
- package/dist/screenshot.js.map +1 -0
- package/dist/still.d.ts +159 -0
- package/dist/still.d.ts.map +1 -0
- package/dist/still.js +222 -0
- package/dist/still.js.map +1 -0
- package/dist/ticker.d.ts +74 -0
- package/dist/ticker.d.ts.map +1 -0
- package/dist/ticker.js +215 -0
- package/dist/ticker.js.map +1 -0
- package/package.json +75 -3
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Where a source URL's bytes come from: a host's local store, then the network.
|
|
3
|
+
* The renderer's storage adapter reads every asset through it (`loadBytes`).
|
|
4
|
+
*
|
|
5
|
+
* ## Why the web adapter needs this at all
|
|
6
|
+
*
|
|
7
|
+
* The storage adapter addresses every asset by URL and fetches it. That is
|
|
8
|
+
* right for an asset that lives somewhere, and wrong for one whose bytes the
|
|
9
|
+
* host already has in hand — a file the user has just chosen, a render a local
|
|
10
|
+
* model has just produced, a picture whose upload has not finished or cannot
|
|
11
|
+
* start because there is no network. In all three the adapter downloads bytes
|
|
12
|
+
* that never needed to travel.
|
|
13
|
+
*
|
|
14
|
+
* `NodeStorageAdapter` has had the answer since it was written: an injected
|
|
15
|
+
* `AssetLoader`, with an `inline` map consulted ahead of the disk. This is the
|
|
16
|
+
* same seam for the browser.
|
|
17
|
+
*
|
|
18
|
+
* ## Why it is module-level
|
|
19
|
+
*
|
|
20
|
+
* The storage adapter is constructed at several sites *inside* this package —
|
|
21
|
+
* sessions, the exporter, the audio mixer and the still renderer — none of
|
|
22
|
+
* which a host can reach. A constructor argument would
|
|
23
|
+
* therefore cover the preview and leave export and stills on the network,
|
|
24
|
+
* which is the worst of both: the picture a user sees would come from local
|
|
25
|
+
* bytes and the one they render would not.
|
|
26
|
+
*
|
|
27
|
+
* So this is an *injection point*, not a cache. The adapter still owns every
|
|
28
|
+
* decode, every cache, every size ceiling and every eviction rule; all this
|
|
29
|
+
* changes is where the compressed bytes came from. A source that answers `null`
|
|
30
|
+
* — which is what an unregistered URL, an evicted entry or a host with no local
|
|
31
|
+
* store does — leaves the adapter exactly as it was.
|
|
32
|
+
*/
|
|
33
|
+
/** Local bytes for a source URL, or `null` to go to the network. */
|
|
34
|
+
export type AssetByteSource = (src: string) => Promise<Blob | null>;
|
|
35
|
+
/**
|
|
36
|
+
* Installs the byte source for this browsing context, or clears it with `null`.
|
|
37
|
+
*
|
|
38
|
+
* Called once per window that renders. Later calls replace the previous source
|
|
39
|
+
* rather than stacking, because two sources disagreeing about one URL has no
|
|
40
|
+
* sensible answer.
|
|
41
|
+
*/
|
|
42
|
+
export declare function setAssetByteSource(next: AssetByteSource | null): void;
|
|
43
|
+
/**
|
|
44
|
+
* The bytes for `src`, or `null`.
|
|
45
|
+
*
|
|
46
|
+
* Swallows a throwing source on purpose. A host's local store failing is a
|
|
47
|
+
* cache miss, not a render failure — the caller falls through to `fetch`, which
|
|
48
|
+
* is what it would have done had the source never been installed.
|
|
49
|
+
*/
|
|
50
|
+
export declare function localAssetBytes(src: string): Promise<Blob | null>;
|
|
51
|
+
/**
|
|
52
|
+
* The network's bytes for `url`. A response that is not OK is a failed load,
|
|
53
|
+
* never bytes: a 404 page handed to the font manager marked a family loaded
|
|
54
|
+
* with no face in it.
|
|
55
|
+
*/
|
|
56
|
+
export declare function fetchAssetBytes(url: string): Promise<Blob>;
|
|
57
|
+
/** The bytes for `src`: the host's local store first, then the network. */
|
|
58
|
+
export declare function loadAssetBytes(src: string): Promise<Blob>;
|
|
59
|
+
//# sourceMappingURL=asset-bytes.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"asset-bytes.d.ts","sourceRoot":"","sources":["../src/asset-bytes.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;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;AAED,2EAA2E;AAC3E,wBAAsB,cAAc,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAE/D"}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Where a source URL's bytes come from: a host's local store, then the network.
|
|
3
|
+
* The renderer's storage adapter reads every asset through it (`loadBytes`).
|
|
4
|
+
*
|
|
5
|
+
* ## Why the web adapter needs this at all
|
|
6
|
+
*
|
|
7
|
+
* The storage adapter addresses every asset by URL and fetches it. That is
|
|
8
|
+
* right for an asset that lives somewhere, and wrong for one whose bytes the
|
|
9
|
+
* host already has in hand — a file the user has just chosen, a render a local
|
|
10
|
+
* model has just produced, a picture whose upload has not finished or cannot
|
|
11
|
+
* start because there is no network. In all three the adapter downloads bytes
|
|
12
|
+
* that never needed to travel.
|
|
13
|
+
*
|
|
14
|
+
* `NodeStorageAdapter` has had the answer since it was written: an injected
|
|
15
|
+
* `AssetLoader`, with an `inline` map consulted ahead of the disk. This is the
|
|
16
|
+
* same seam for the browser.
|
|
17
|
+
*
|
|
18
|
+
* ## Why it is module-level
|
|
19
|
+
*
|
|
20
|
+
* The storage adapter is constructed at several sites *inside* this package —
|
|
21
|
+
* sessions, the exporter, the audio mixer and the still renderer — none of
|
|
22
|
+
* which a host can reach. A constructor argument would
|
|
23
|
+
* therefore cover the preview and leave export and stills on the network,
|
|
24
|
+
* which is the worst of both: the picture a user sees would come from local
|
|
25
|
+
* bytes and the one they render would not.
|
|
26
|
+
*
|
|
27
|
+
* So this is an *injection point*, not a cache. The adapter still owns every
|
|
28
|
+
* decode, every cache, every size ceiling and every eviction rule; all this
|
|
29
|
+
* changes is where the compressed bytes came from. A source that answers `null`
|
|
30
|
+
* — which is what an unregistered URL, an evicted entry or a host with no local
|
|
31
|
+
* store does — leaves the adapter exactly as it was.
|
|
32
|
+
*/
|
|
33
|
+
let source = null;
|
|
34
|
+
/**
|
|
35
|
+
* Installs the byte source for this browsing context, or clears it with `null`.
|
|
36
|
+
*
|
|
37
|
+
* Called once per window that renders. Later calls replace the previous source
|
|
38
|
+
* rather than stacking, because two sources disagreeing about one URL has no
|
|
39
|
+
* sensible answer.
|
|
40
|
+
*/
|
|
41
|
+
export function setAssetByteSource(next) {
|
|
42
|
+
source = next;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* The bytes for `src`, or `null`.
|
|
46
|
+
*
|
|
47
|
+
* Swallows a throwing source on purpose. A host's local store failing is a
|
|
48
|
+
* cache miss, not a render failure — the caller falls through to `fetch`, which
|
|
49
|
+
* is what it would have done had the source never been installed.
|
|
50
|
+
*/
|
|
51
|
+
export async function localAssetBytes(src) {
|
|
52
|
+
if (!source)
|
|
53
|
+
return null;
|
|
54
|
+
try {
|
|
55
|
+
return await source(src);
|
|
56
|
+
}
|
|
57
|
+
catch {
|
|
58
|
+
return null;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* The network's bytes for `url`. A response that is not OK is a failed load,
|
|
63
|
+
* never bytes: a 404 page handed to the font manager marked a family loaded
|
|
64
|
+
* with no face in it.
|
|
65
|
+
*/
|
|
66
|
+
export async function fetchAssetBytes(url) {
|
|
67
|
+
const response = await fetch(url);
|
|
68
|
+
if (!response.ok) {
|
|
69
|
+
const status = response.statusText ? `${response.status} ${response.statusText}` : `${response.status}`;
|
|
70
|
+
throw new Error(`Fetching "${url}" failed: HTTP ${status}`);
|
|
71
|
+
}
|
|
72
|
+
return response.blob();
|
|
73
|
+
}
|
|
74
|
+
/** The bytes for `src`: the host's local store first, then the network. */
|
|
75
|
+
export async function loadAssetBytes(src) {
|
|
76
|
+
return (await localAssetBytes(src)) ?? fetchAssetBytes(src);
|
|
77
|
+
}
|
|
78
|
+
//# sourceMappingURL=asset-bytes.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"asset-bytes.js","sourceRoot":"","sources":["../src/asset-bytes.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AAKH,IAAI,MAAM,GAA2B,IAAI,CAAC;AAE1C;;;;;;GAMG;AACH,MAAM,UAAU,kBAAkB,CAAC,IAA4B;IAC3D,MAAM,GAAG,IAAI,CAAC;AAClB,CAAC;AAED;;;;;;GAMG;AACH,MAAM,CAAC,KAAK,UAAU,eAAe,CAAC,GAAW;IAC7C,IAAI,CAAC,MAAM;QAAE,OAAO,IAAI,CAAC;IACzB,IAAI,CAAC;QACD,OAAO,MAAM,MAAM,CAAC,GAAG,CAAC,CAAC;IAC7B,CAAC;IAAC,MAAM,CAAC;QACL,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;AAED,2EAA2E;AAC3E,MAAM,CAAC,KAAK,UAAU,cAAc,CAAC,GAAW;IAC5C,OAAO,CAAC,MAAM,eAAe,CAAC,GAAG,CAAC,CAAC,IAAI,eAAe,CAAC,GAAG,CAAC,CAAC;AAChE,CAAC","sourcesContent":["/**\n * Where a source URL's bytes come from: a host's local store, then the network.\n * The renderer's storage adapter reads every asset through it (`loadBytes`).\n *\n * ## Why the web adapter needs this at all\n *\n * The storage adapter addresses every asset by URL and fetches it. That is\n * right for an asset that lives somewhere, and wrong for one whose bytes the\n * host already has in hand — a file the user has just chosen, a render a local\n * model has just produced, a picture whose upload has not finished or cannot\n * start because there is no network. In all three the adapter downloads bytes\n * that never needed to travel.\n *\n * `NodeStorageAdapter` has had the answer since it was written: an injected\n * `AssetLoader`, with an `inline` map consulted ahead of the disk. This is the\n * same seam for the browser.\n *\n * ## Why it is module-level\n *\n * The storage adapter is constructed at several sites *inside* this package —\n * sessions, the exporter, the audio mixer and the still renderer — none of\n * which a host can reach. A constructor argument would\n * therefore cover the preview and leave export and stills on the network,\n * which is the worst of both: the picture a user sees would come from local\n * bytes and the one they render would not.\n *\n * So this is an *injection point*, not a cache. The adapter still owns every\n * decode, every cache, every size ceiling and every eviction rule; all this\n * changes is where the compressed bytes came from. A source that answers `null`\n * — which is what an unregistered URL, an evicted entry or a host with no local\n * store does — leaves the adapter exactly as it was.\n */\n\n/** Local bytes for a source URL, or `null` to go to the network. */\nexport type AssetByteSource = (src: string) => Promise<Blob | null>;\n\nlet source: AssetByteSource | null = null;\n\n/**\n * Installs the byte source for this browsing context, or clears it with `null`.\n *\n * Called once per window that renders. Later calls replace the previous source\n * rather than stacking, because two sources disagreeing about one URL has no\n * sensible answer.\n */\nexport function setAssetByteSource(next: AssetByteSource | null): void {\n source = next;\n}\n\n/**\n * The bytes for `src`, or `null`.\n *\n * Swallows a throwing source on purpose. A host's local store failing is a\n * cache miss, not a render failure — the caller falls through to `fetch`, which\n * is what it would have done had the source never been installed.\n */\nexport async function localAssetBytes(src: string): Promise<Blob | null> {\n if (!source) return null;\n try {\n return await source(src);\n } catch {\n return null;\n }\n}\n\n/**\n * The network's bytes for `url`. A response that is not OK is a failed load,\n * never bytes: a 404 page handed to the font manager marked a family loaded\n * with no face in it.\n */\nexport async function fetchAssetBytes(url: string): Promise<Blob> {\n const response = await fetch(url);\n if (!response.ok) {\n const status = response.statusText ? `${response.status} ${response.statusText}` : `${response.status}`;\n throw new Error(`Fetching \"${url}\" failed: HTTP ${status}`);\n }\n return response.blob();\n}\n\n/** The bytes for `src`: the host's local store first, then the network. */\nexport async function loadAssetBytes(src: string): Promise<Blob> {\n return (await localAssetBytes(src)) ?? fetchAssetBytes(src);\n}\n"]}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { type AudioBusStop } from "@motionscript/core";
|
|
2
|
+
/**
|
|
3
|
+
* The live chains for one audio context's buses.
|
|
4
|
+
*
|
|
5
|
+
* Held per context rather than per clip, because that is the whole difference
|
|
6
|
+
* between a bus and per-clip processing: every clip naming a bus connects into
|
|
7
|
+
* the *same* nodes, so a reverb on a bus is one tail the group shares rather
|
|
8
|
+
* than one tail each. Flattening the levels into per-clip gain would sound
|
|
9
|
+
* right; flattening the effects would not.
|
|
10
|
+
*/
|
|
11
|
+
export declare class AudioBusGraph {
|
|
12
|
+
private readonly ctx;
|
|
13
|
+
/** Where the last bus in a chain connects — the master, or the destination. */
|
|
14
|
+
private readonly destination;
|
|
15
|
+
constructor(ctx: BaseAudioContext,
|
|
16
|
+
/** Where the last bus in a chain connects — the master, or the destination. */
|
|
17
|
+
destination: AudioNode);
|
|
18
|
+
/**
|
|
19
|
+
* The input node a clip on `bus` should connect to, building the chain the
|
|
20
|
+
* first time it is asked for.
|
|
21
|
+
*
|
|
22
|
+
* `bus` is source-adjacent first, so `bus[0]` is the one the clip feeds and
|
|
23
|
+
* each stop after it is that bus's own parent. An empty or absent chain
|
|
24
|
+
* returns the destination, which is what every clip did before buses
|
|
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.
|
|
30
|
+
*/
|
|
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. */
|
|
33
|
+
dispose(): void;
|
|
34
|
+
private readonly inputs;
|
|
35
|
+
private readonly nodes;
|
|
36
|
+
private readonly oscillators;
|
|
37
|
+
/**
|
|
38
|
+
* Build (or reuse) the chain for `bus[index]`, connected to whatever
|
|
39
|
+
* `bus[index + 1]` resolves to.
|
|
40
|
+
*
|
|
41
|
+
* Recursive rather than iterative because a bus's *output* has to exist
|
|
42
|
+
* before its own nodes can connect to it, and the outermost bus is the last
|
|
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.
|
|
49
|
+
*/
|
|
50
|
+
private chainFrom;
|
|
51
|
+
}
|
|
52
|
+
//# sourceMappingURL=bus-graph.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bus-graph.d.ts","sourceRoot":"","sources":["../../src/audio/bus-graph.ts"],"names":[],"mappings":"AAAA,OAAO,EAAc,KAAK,YAAY,EAAoB,MAAM,oBAAoB,CAAC;AAGrF;;;;;;;;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;CAuCpB"}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { busStopKey } from "@motionscript/core";
|
|
2
|
+
import { applyTransitions, buildAudioEffectGraph } from "./effect-graph";
|
|
3
|
+
/**
|
|
4
|
+
* The live chains for one audio context's buses.
|
|
5
|
+
*
|
|
6
|
+
* Held per context rather than per clip, because that is the whole difference
|
|
7
|
+
* between a bus and per-clip processing: every clip naming a bus connects into
|
|
8
|
+
* the *same* nodes, so a reverb on a bus is one tail the group shares rather
|
|
9
|
+
* than one tail each. Flattening the levels into per-clip gain would sound
|
|
10
|
+
* right; flattening the effects would not.
|
|
11
|
+
*/
|
|
12
|
+
export class AudioBusGraph {
|
|
13
|
+
ctx;
|
|
14
|
+
destination;
|
|
15
|
+
constructor(ctx,
|
|
16
|
+
/** Where the last bus in a chain connects — the master, or the destination. */
|
|
17
|
+
destination) {
|
|
18
|
+
this.ctx = ctx;
|
|
19
|
+
this.destination = destination;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* The input node a clip on `bus` should connect to, building the chain the
|
|
23
|
+
* first time it is asked for.
|
|
24
|
+
*
|
|
25
|
+
* `bus` is source-adjacent first, so `bus[0]` is the one the clip feeds and
|
|
26
|
+
* each stop after it is that bus's own parent. An empty or absent chain
|
|
27
|
+
* returns the destination, which is what every clip did before buses
|
|
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.
|
|
33
|
+
*/
|
|
34
|
+
inputFor(bus, startTime, clipTime) {
|
|
35
|
+
if (!bus || bus.length === 0)
|
|
36
|
+
return this.destination;
|
|
37
|
+
return this.chainFrom(bus, 0, startTime, clipTime);
|
|
38
|
+
}
|
|
39
|
+
/** Disconnect every live bus node, so the next {@link inputFor} builds afresh. Safe to call more than once. */
|
|
40
|
+
dispose() {
|
|
41
|
+
for (const node of this.nodes) {
|
|
42
|
+
try {
|
|
43
|
+
node.disconnect();
|
|
44
|
+
}
|
|
45
|
+
catch { /* already torn down */ }
|
|
46
|
+
}
|
|
47
|
+
for (const osc of this.oscillators) {
|
|
48
|
+
try {
|
|
49
|
+
osc.stop();
|
|
50
|
+
}
|
|
51
|
+
catch { /* never started, or already stopped */ }
|
|
52
|
+
}
|
|
53
|
+
this.nodes.length = 0;
|
|
54
|
+
this.oscillators.length = 0;
|
|
55
|
+
this.inputs.clear();
|
|
56
|
+
}
|
|
57
|
+
inputs = new Map();
|
|
58
|
+
nodes = [];
|
|
59
|
+
oscillators = [];
|
|
60
|
+
/**
|
|
61
|
+
* Build (or reuse) the chain for `bus[index]`, connected to whatever
|
|
62
|
+
* `bus[index + 1]` resolves to.
|
|
63
|
+
*
|
|
64
|
+
* Recursive rather than iterative because a bus's *output* has to exist
|
|
65
|
+
* before its own nodes can connect to it, and the outermost bus is the last
|
|
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.
|
|
72
|
+
*/
|
|
73
|
+
chainFrom(bus, index, startTime, clipTime) {
|
|
74
|
+
if (index >= bus.length)
|
|
75
|
+
return this.destination;
|
|
76
|
+
const stop = bus[index];
|
|
77
|
+
const key = bus.slice(index).map(busStopKey).join(">");
|
|
78
|
+
const existing = this.inputs.get(key);
|
|
79
|
+
if (existing)
|
|
80
|
+
return existing;
|
|
81
|
+
const output = this.chainFrom(bus, index + 1, startTime, clipTime);
|
|
82
|
+
// A pass-through head every clip connects to, so the bus has one input
|
|
83
|
+
// whatever its effects turn out to be.
|
|
84
|
+
const input = this.ctx.createGain();
|
|
85
|
+
input.gain.value = 1;
|
|
86
|
+
// The level is a gain after the effects, as a sound's own level is, so a
|
|
87
|
+
// ramp on it is scheduled exactly as one in `effects` is.
|
|
88
|
+
const level = { type: "gain", value: stop.mute ? 0 : stop.volume };
|
|
89
|
+
const span = stop.endAt - stop.startAt;
|
|
90
|
+
const graph = buildAudioEffectGraph(this.ctx, input, [...(stop.effects ?? []), level], startTime, span, clipTime - stop.startAt);
|
|
91
|
+
let tail = graph.output;
|
|
92
|
+
if (stop.transitionIn || stop.transitionOut) {
|
|
93
|
+
const transition = this.ctx.createGain();
|
|
94
|
+
applyTransitions(transition.gain, span, stop.transitionIn, stop.transitionOut, startTime, clipTime - stop.startAt);
|
|
95
|
+
tail.connect(transition);
|
|
96
|
+
this.nodes.push(transition);
|
|
97
|
+
tail = transition;
|
|
98
|
+
}
|
|
99
|
+
tail.connect(output);
|
|
100
|
+
this.nodes.push(input, ...graph.nodes);
|
|
101
|
+
this.oscillators.push(...graph.oscillators);
|
|
102
|
+
for (const osc of graph.oscillators)
|
|
103
|
+
osc.start(startTime);
|
|
104
|
+
this.inputs.set(key, input);
|
|
105
|
+
return input;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
//# sourceMappingURL=bus-graph.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bus-graph.js","sourceRoot":"","sources":["../../src/audio/bus-graph.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAuC,MAAM,oBAAoB,CAAC;AACrF,OAAO,EAAE,gBAAgB,EAAE,qBAAqB,EAAE,MAAM,gBAAgB,CAAC;AAEzE;;;;;;;;GAQG;AACH,MAAM,OAAO,aAAa;IAED;IAEA;IAHrB,YACqB,GAAqB;IACtC,+EAA+E;IAC9D,WAAsB;QAFtB,QAAG,GAAH,GAAG,CAAkB;QAErB,gBAAW,GAAX,WAAW,CAAW;IACvC,CAAC;IAEL;;;;;;;;;;;;OAYG;IACH,QAAQ,CAAC,GAAwC,EAAE,SAAiB,E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AAO,CAAC,UAAU,CAAC,CAAC;YACzB,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;YAC5B,IAAI,GAAG,UAAU,CAAC;QACtB,CAAC;QACD,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;QAErB,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,KAAK,EAAE,GAAG,KAAK,CAAC,KAAK,CAAC,CAAC;QACvC,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC,GAAG,KAAK,CAAC,WAAW,CAAC,CAAC;QAC5C,KAAK,MAAM,GAAG,IAAI,KAAK,CAAC,WAAW;YAAE,GAAG,CAAC,KAAK,CAAC,SAAS,CAAC,CAAC;QAE1D,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;QAC5B,OAAO,KAAK,CAAC;IACjB,CAAC;CACJ","sourcesContent":["import { busStopKey, type AudioBusStop, type AudioEffect } from \"@motionscript/core\";\nimport { applyTransitions, buildAudioEffectGraph } from \"./effect-graph\";\n\n/**\n * The live chains for one audio context's buses.\n *\n * Held per context rather than per clip, because that is the whole difference\n * between a bus and per-clip processing: every clip naming a bus connects into\n * the *same* nodes, so a reverb on a bus is one tail the group shares rather\n * than one tail each. Flattening the levels into per-clip gain would sound\n * right; flattening the effects would not.\n */\nexport class AudioBusGraph {\n constructor(\n private readonly ctx: BaseAudioContext,\n /** Where the last bus in a chain connects — the master, or the destination. */\n private readonly destination: AudioNode,\n ) { }\n\n /**\n * The input node a clip on `bus` should connect to, building the chain the\n * first time it is asked for.\n *\n * `bus` is source-adjacent first, so `bus[0]` is the one the clip feeds and\n * each stop after it is that bus's own parent. An empty or absent chain\n * returns the destination, which is what every clip did before buses\n * existed.\n *\n * `startTime` is the context time a new chain's schedule starts from, and\n * `clipTime` the same instant in the stops' own seconds. A curve on a stop\n * runs over its `startAt`–`endAt`, so the two are what place it.\n */\n inputFor(bus: readonly AudioBusStop[] | undefined, startTime: number, clipTime: number): AudioNode {\n if (!bus || bus.length === 0) return this.destination;\n return this.chainFrom(bus, 0, startTime, clipTime);\n }\n\n /** Disconnect every live bus node, so the next {@link inputFor} builds afresh. Safe to call more than once. */\n dispose(): void {\n for (const node of this.nodes) {\n try { node.disconnect(); } catch { /* already torn down */ }\n }\n for (const osc of this.oscillators) {\n try { osc.stop(); } catch { /* never started, or already stopped */ }\n }\n this.nodes.length = 0;\n this.oscillators.length = 0;\n this.inputs.clear();\n }\n\n private readonly inputs = new Map<string, AudioNode>();\n private readonly nodes: AudioNode[] = [];\n private readonly oscillators: OscillatorNode[] = [];\n\n /**\n * Build (or reuse) the chain for `bus[index]`, connected to whatever\n * `bus[index + 1]` resolves to.\n *\n * Recursive rather than iterative because a bus's *output* has to exist\n * before its own nodes can connect to it, and the outermost bus is the last\n * entry.\n *\n * Reused by everything that changes what it sounds like, down to the master,\n * not by id alone: a bus edited under a playing device, or one id on two\n * tracks, is a different chain, and one built for the first would play the\n * second through stale levels and a curve anchored to the wrong span.\n */\n private chainFrom(bus: readonly AudioBusStop[], index: number, startTime: number, clipTime: number): AudioNode {\n if (index >= bus.length) return this.destination;\n\n const stop = bus[index];\n const key = bus.slice(index).map(busStopKey).join(\">\");\n const existing = this.inputs.get(key);\n if (existing) return existing;\n\n const output = this.chainFrom(bus, index + 1, startTime, clipTime);\n\n // A pass-through head every clip connects to, so the bus has one input\n // whatever its effects turn out to be.\n const input = this.ctx.createGain();\n input.gain.value = 1;\n\n // The level is a gain after the effects, as a sound's own level is, so a\n // ramp on it is scheduled exactly as one in `effects` is.\n const level: AudioEffect = { type: \"gain\", value: stop.mute ? 0 : stop.volume };\n const span = stop.endAt - stop.startAt;\n const graph = buildAudioEffectGraph(\n this.ctx, input, [...(stop.effects ?? []), level], startTime, span, clipTime - stop.startAt,\n );\n let tail = graph.output;\n if (stop.transitionIn || stop.transitionOut) {\n const transition = this.ctx.createGain();\n applyTransitions(transition.gain, span, stop.transitionIn, stop.transitionOut, startTime, clipTime - stop.startAt);\n tail.connect(transition);\n this.nodes.push(transition);\n tail = transition;\n }\n tail.connect(output);\n\n this.nodes.push(input, ...graph.nodes);\n this.oscillators.push(...graph.oscillators);\n for (const osc of graph.oscillators) osc.start(startTime);\n\n this.inputs.set(key, input);\n return input;\n }\n}\n"]}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { type AudioEffect, type AudioRequest, type AudioTransition, type Ramp } from "@motionscript/core";
|
|
2
|
+
/**
|
|
3
|
+
* The Web Audio graph for one source's effect chain.
|
|
4
|
+
*
|
|
5
|
+
* `output` is the last node, to connect downstream; `nodes` are every node to
|
|
6
|
+
* `disconnect()` on teardown; `oscillators` are tremolo LFOs the caller starts
|
|
7
|
+
* and stops alongside the source.
|
|
8
|
+
*/
|
|
9
|
+
export interface AudioEffectGraph {
|
|
10
|
+
output: AudioNode;
|
|
11
|
+
nodes: AudioNode[];
|
|
12
|
+
oscillators: OscillatorNode[];
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Builds the Web Audio graph for a clip's effect chain, after an
|
|
16
|
+
* already-created `source`. Shared by live playback ({@link WebAudioSink}) and
|
|
17
|
+
* offline export (`mixAudio`) so both sound identical.
|
|
18
|
+
*
|
|
19
|
+
* `startTime` is the context time the source starts; `clipDuration` the clip
|
|
20
|
+
* seconds ramps run over (a clip's, or a bus's span); `elapsed` the clip seconds
|
|
21
|
+
* already played at a mid-clip seek (0 for a clean start, negative before the
|
|
22
|
+
* span begins).
|
|
23
|
+
*/
|
|
24
|
+
export declare function buildAudioEffectGraph(ctx: BaseAudioContext, source: AudioNode, effects: readonly AudioEffect[], startTime?: number, clipDuration?: number, elapsed?: number): AudioEffectGraph;
|
|
25
|
+
/**
|
|
26
|
+
* Schedule `transitionIn`/`transitionOut` onto a gain over a span `length`
|
|
27
|
+
* seconds long, baked like an eased ramp. Between the two it holds at 1.
|
|
28
|
+
*/
|
|
29
|
+
export declare function applyTransitions(gain: AudioParam, length: number, transitionIn: AudioTransition | undefined, transitionOut: AudioTransition | undefined, startTime: number, elapsed: number): void;
|
|
30
|
+
/** How one request plays its source: the rate, and the lengths its ramps and transitions run over. */
|
|
31
|
+
export interface Playback {
|
|
32
|
+
/** The constant `playbackRate`; 1 under a ramp, which is scheduled instead. */
|
|
33
|
+
rate: number;
|
|
34
|
+
speedRamp?: Ramp;
|
|
35
|
+
/** Source seconds a speed ramp is placed on. */
|
|
36
|
+
sourceLength: number;
|
|
37
|
+
/** Clip seconds the volume and effect ramps run over. */
|
|
38
|
+
clipDuration: number;
|
|
39
|
+
/** What the transitions run over: the clip's length, unknown for an open one. */
|
|
40
|
+
transitionLength: number;
|
|
41
|
+
}
|
|
42
|
+
export declare function playbackOf(req: AudioRequest, bufferDuration: number): Playback;
|
|
43
|
+
/** Source seconds after `trimStart` that `clipElapsed` clip seconds have played. */
|
|
44
|
+
export declare function sourceOffsetOf(playback: Playback, clipElapsed: number): number;
|
|
45
|
+
/**
|
|
46
|
+
* A request's level, then its transitions, after `from`: the gains every
|
|
47
|
+
* request plays through on its way to its bus.
|
|
48
|
+
*/
|
|
49
|
+
export declare function buildLevel(ctx: BaseAudioContext, from: AudioNode, req: AudioRequest, playback: Playback, startTime: number, elapsed: number): {
|
|
50
|
+
output: AudioNode;
|
|
51
|
+
nodes: AudioNode[];
|
|
52
|
+
};
|
|
53
|
+
/**
|
|
54
|
+
* Schedule a speed ramp onto an `AudioBufferSourceNode.playbackRate`.
|
|
55
|
+
*
|
|
56
|
+
* The ramp runs over **source** seconds and Web Audio schedules in context
|
|
57
|
+
* seconds, so this walks clip time in small steps, converts each to a source
|
|
58
|
+
* offset through the integral inverse, and lays down the rate there. `elapsed`
|
|
59
|
+
* is clip seconds already played at a mid-clip seek.
|
|
60
|
+
*/
|
|
61
|
+
export declare function applySpeedToPlaybackRate(rate: AudioParam, speed: Ramp | undefined, sourceLength: number, clipDuration: number, startTime: number, elapsed: number): void;
|
|
62
|
+
//# sourceMappingURL=effect-graph.d.ts.map
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AAQ,EAAE,WAAW,EAAE,MAAM,GAAG,MAAM,CAI9E;AAED;;;GAGG;AACH,wBAAgB,UAAU,CACtB,GAAG,EAAE,gBAAgB,EACrB,IAAI,EAAE,SAAS,EACf,GAAG,EAAE,YAAY,EACjB,QAAQ,EAAE,QAAQ,EAClB,SAAS,EAAE,MAAM,EACjB,OAAO,EAAE,MAAM,GAChB;IAAE,MAAM,EAAE,SAAS,CAAC;IAAC,KAAK,EAAE,SAAS,EAAE,CAAA;CAAE,CAS3C;AAED;;;;;;;GAOG;AACH,wBAAgB,wBAAwB,CACpC,IAAI,EAAE,UAAU,EAChB,KAAK,EAAE,IAAI,GAAG,SAAS,EACvB,YAAY,EAAE,MAAM,EACpB,YAAY,EAAE,MAAM,EACpB,SAAS,EAAE,MAAM,EACjB,OAAO,EAAE,MAAM,GAChB,IAAI,CAaN"}
|
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
import { clipTimeAtSource, isRamp, resolveRamp, sampleRamp, sourceTimeAtClip, staticValue, transitionGainAt, transitionSpans, } from "@motionscript/core";
|
|
2
|
+
/** Sample points per second an eased segment or a transition is baked into: about every 5 ms. */
|
|
3
|
+
const CURVE_SAMPLES_PER_SECOND = 200;
|
|
4
|
+
/**
|
|
5
|
+
* Apply a {@link Param} to a Web Audio {@link AudioParam}: a number once, a
|
|
6
|
+
* ramp as a schedule — linear segments as `linearRampToValueAtTime`, eased
|
|
7
|
+
* ones baked into `setValueCurveAtTime`.
|
|
8
|
+
*
|
|
9
|
+
* `elapsed` is the clip seconds already played when a mid-clip seek starts the
|
|
10
|
+
* source, so "now" reads the ramp at the clip's current position. `clamp` keeps
|
|
11
|
+
* every scheduled value in a param's valid range.
|
|
12
|
+
*/
|
|
13
|
+
function applyParam(audioParam, param, startTime, clipDuration, elapsed, clamp = (v) => v) {
|
|
14
|
+
// The platform throws on a non-finite value.
|
|
15
|
+
const finite = (v) => {
|
|
16
|
+
const c = clamp(v);
|
|
17
|
+
return Number.isFinite(c) ? c : audioParam.value;
|
|
18
|
+
};
|
|
19
|
+
if (!isRamp(param)) {
|
|
20
|
+
audioParam.value = finite(param);
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
if (!Number.isFinite(clipDuration) || clipDuration <= 0) {
|
|
24
|
+
audioParam.value = finite(staticValue(param, audioParam.value));
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
const keys = resolveRamp(param, clipDuration);
|
|
28
|
+
if (keys.length === 0)
|
|
29
|
+
return;
|
|
30
|
+
const base = startTime - elapsed;
|
|
31
|
+
audioParam.setValueAtTime(finite(sampleRamp(param, Math.max(0, elapsed), clipDuration, audioParam.value)), startTime);
|
|
32
|
+
for (let i = 1; i < keys.length; i++) {
|
|
33
|
+
const previous = keys[i - 1];
|
|
34
|
+
const key = keys[i];
|
|
35
|
+
const segStart = base + previous.time;
|
|
36
|
+
const segEnd = base + key.time;
|
|
37
|
+
if (segEnd <= startTime)
|
|
38
|
+
continue;
|
|
39
|
+
const span = key.time - previous.time;
|
|
40
|
+
if (span <= 0) {
|
|
41
|
+
audioParam.setValueAtTime(finite(key.value), Math.max(segEnd, startTime));
|
|
42
|
+
continue;
|
|
43
|
+
}
|
|
44
|
+
// A segment already under way when the source starts carries on from
|
|
45
|
+
// the value set at `startTime` rather than restarting from its own.
|
|
46
|
+
const done = Math.max(0, (startTime - segStart) / span);
|
|
47
|
+
const curveStart = Math.max(segStart, startTime);
|
|
48
|
+
if (key.linear) {
|
|
49
|
+
if (done === 0)
|
|
50
|
+
audioParam.setValueAtTime(finite(previous.value), curveStart);
|
|
51
|
+
audioParam.linearRampToValueAtTime(finite(key.value), segEnd);
|
|
52
|
+
}
|
|
53
|
+
else {
|
|
54
|
+
const curveDur = segEnd - curveStart;
|
|
55
|
+
const count = Math.max(2, Math.ceil(curveDur * CURVE_SAMPLES_PER_SECOND));
|
|
56
|
+
const samples = new Float32Array(count);
|
|
57
|
+
for (let n = 0; n < count; n++) {
|
|
58
|
+
const local = done + (1 - done) * (n / (count - 1));
|
|
59
|
+
samples[n] = finite(previous.value + (key.value - previous.value) * key.ease(local));
|
|
60
|
+
}
|
|
61
|
+
audioParam.setValueCurveAtTime(samples, curveStart, curveDur);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Builds the Web Audio graph for a clip's effect chain, after an
|
|
67
|
+
* already-created `source`. Shared by live playback ({@link WebAudioSink}) and
|
|
68
|
+
* offline export (`mixAudio`) so both sound identical.
|
|
69
|
+
*
|
|
70
|
+
* `startTime` is the context time the source starts; `clipDuration` the clip
|
|
71
|
+
* seconds ramps run over (a clip's, or a bus's span); `elapsed` the clip seconds
|
|
72
|
+
* already played at a mid-clip seek (0 for a clean start, negative before the
|
|
73
|
+
* span begins).
|
|
74
|
+
*/
|
|
75
|
+
export function buildAudioEffectGraph(ctx, source, effects, startTime = ctx.currentTime, clipDuration = 0, elapsed = 0) {
|
|
76
|
+
const nodes = [];
|
|
77
|
+
const oscillators = [];
|
|
78
|
+
let cursor = source;
|
|
79
|
+
const series = (node) => {
|
|
80
|
+
cursor.connect(node);
|
|
81
|
+
nodes.push(node);
|
|
82
|
+
cursor = node;
|
|
83
|
+
};
|
|
84
|
+
const apply = (p, v, clamp) => applyParam(p, v, startTime, clipDuration, elapsed, clamp);
|
|
85
|
+
for (const effect of effects) {
|
|
86
|
+
switch (effect.type) {
|
|
87
|
+
case "gain": {
|
|
88
|
+
const gain = ctx.createGain();
|
|
89
|
+
apply(gain.gain, effect.value);
|
|
90
|
+
series(gain);
|
|
91
|
+
break;
|
|
92
|
+
}
|
|
93
|
+
case "highpass":
|
|
94
|
+
case "lowpass": {
|
|
95
|
+
const biquad = ctx.createBiquadFilter();
|
|
96
|
+
biquad.type = effect.type;
|
|
97
|
+
apply(biquad.frequency, effect.frequency);
|
|
98
|
+
if (effect.q !== undefined)
|
|
99
|
+
apply(biquad.Q, effect.q);
|
|
100
|
+
series(biquad);
|
|
101
|
+
break;
|
|
102
|
+
}
|
|
103
|
+
case "tremolo": {
|
|
104
|
+
// A carrier gain an LFO swings: with depth d the carrier sits at
|
|
105
|
+
// 1 - d/2 and the swing is d/2.
|
|
106
|
+
const carrier = ctx.createGain();
|
|
107
|
+
const lfo = ctx.createOscillator();
|
|
108
|
+
const lfoDepth = ctx.createGain();
|
|
109
|
+
apply(carrier.gain, effect.depth, (d) => 1 - clamp01(d) / 2);
|
|
110
|
+
apply(lfoDepth.gain, effect.depth, (d) => clamp01(d) / 2);
|
|
111
|
+
apply(lfo.frequency, effect.rate);
|
|
112
|
+
lfo.connect(lfoDepth);
|
|
113
|
+
lfoDepth.connect(carrier.gain);
|
|
114
|
+
nodes.push(lfoDepth);
|
|
115
|
+
oscillators.push(lfo);
|
|
116
|
+
series(carrier);
|
|
117
|
+
break;
|
|
118
|
+
}
|
|
119
|
+
case "echo": {
|
|
120
|
+
const split = cursor;
|
|
121
|
+
const merge = ctx.createGain();
|
|
122
|
+
const dry = ctx.createGain();
|
|
123
|
+
apply(dry.gain, effect.mix ?? 0.5, (m) => 1 - clamp01(m));
|
|
124
|
+
split.connect(dry);
|
|
125
|
+
dry.connect(merge);
|
|
126
|
+
// The delay line is sized once, so it has to hold the ramp's longest delay.
|
|
127
|
+
const delay = ctx.createDelay(Math.max(1, maxOf(effect.delay) + 1));
|
|
128
|
+
apply(delay.delayTime, effect.delay);
|
|
129
|
+
const feedbackGain = ctx.createGain();
|
|
130
|
+
apply(feedbackGain.gain, effect.feedback, (f) => Math.max(0, Math.min(0.99, f)));
|
|
131
|
+
const wet = ctx.createGain();
|
|
132
|
+
apply(wet.gain, effect.mix ?? 0.5, (m) => clamp01(m));
|
|
133
|
+
split.connect(delay);
|
|
134
|
+
delay.connect(feedbackGain);
|
|
135
|
+
feedbackGain.connect(delay);
|
|
136
|
+
delay.connect(wet);
|
|
137
|
+
wet.connect(merge);
|
|
138
|
+
nodes.push(dry, delay, feedbackGain, wet, merge);
|
|
139
|
+
cursor = merge;
|
|
140
|
+
break;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
return { output: cursor, nodes, oscillators };
|
|
145
|
+
}
|
|
146
|
+
function clamp01(v) {
|
|
147
|
+
return Math.max(0, Math.min(1, v));
|
|
148
|
+
}
|
|
149
|
+
function maxOf(p) {
|
|
150
|
+
return isRamp(p) ? Math.max(0, ...p.keys.map((k) => k.value)) : p;
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Schedule `transitionIn`/`transitionOut` onto a gain over a span `length`
|
|
154
|
+
* seconds long, baked like an eased ramp. Between the two it holds at 1.
|
|
155
|
+
*/
|
|
156
|
+
export function applyTransitions(gain, length, transitionIn, transitionOut, startTime, elapsed) {
|
|
157
|
+
if (!transitionIn && !transitionOut)
|
|
158
|
+
return;
|
|
159
|
+
const seek = Math.max(0, elapsed);
|
|
160
|
+
const at = (t) => transitionGainAt(t, length, transitionIn, transitionOut);
|
|
161
|
+
gain.setValueAtTime(at(seek), startTime);
|
|
162
|
+
const spans = transitionSpans(length, transitionIn, transitionOut);
|
|
163
|
+
const windows = [];
|
|
164
|
+
if (spans.in > 0)
|
|
165
|
+
windows.push([0, spans.in]);
|
|
166
|
+
if (spans.out > 0 && Number.isFinite(length))
|
|
167
|
+
windows.push([length - spans.out, length]);
|
|
168
|
+
const base = startTime - elapsed;
|
|
169
|
+
for (const [from, to] of windows) {
|
|
170
|
+
const begin = Math.max(from, seek);
|
|
171
|
+
const duration = to - begin;
|
|
172
|
+
if (duration <= 0)
|
|
173
|
+
continue;
|
|
174
|
+
const count = Math.max(2, Math.ceil(duration * CURVE_SAMPLES_PER_SECOND));
|
|
175
|
+
const samples = new Float32Array(count);
|
|
176
|
+
for (let n = 0; n < count; n++)
|
|
177
|
+
samples[n] = at(begin + duration * (n / (count - 1)));
|
|
178
|
+
gain.setValueCurveAtTime(samples, base + begin, duration);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
export function playbackOf(req, bufferDuration) {
|
|
182
|
+
const speed = req.speed ?? 1;
|
|
183
|
+
const speedRamp = isRamp(speed) ? speed : undefined;
|
|
184
|
+
const rate = speedRamp || !(Number.isFinite(speed) && speed > 0) ? 1 : speed;
|
|
185
|
+
// Placed on the same length core sized the request with, not the decoded buffer.
|
|
186
|
+
const sourceLength = req.sourceLength ?? Math.max(0, bufferDuration - req.trimStart);
|
|
187
|
+
const bounded = Number.isFinite(req.endAt);
|
|
188
|
+
const clipDuration = bounded ? Math.max(0, req.endAt - req.startAt) : clipTimeAtSource(speed, sourceLength);
|
|
189
|
+
return { rate, speedRamp, sourceLength, clipDuration, transitionLength: bounded ? clipDuration : Infinity };
|
|
190
|
+
}
|
|
191
|
+
/** Source seconds after `trimStart` that `clipElapsed` clip seconds have played. */
|
|
192
|
+
export function sourceOffsetOf(playback, clipElapsed) {
|
|
193
|
+
return playback.speedRamp
|
|
194
|
+
? sourceTimeAtClip(playback.speedRamp, playback.sourceLength, clipElapsed)
|
|
195
|
+
: clipElapsed * playback.rate;
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* A request's level, then its transitions, after `from`: the gains every
|
|
199
|
+
* request plays through on its way to its bus.
|
|
200
|
+
*/
|
|
201
|
+
export function buildLevel(ctx, from, req, playback, startTime, elapsed) {
|
|
202
|
+
const level = ctx.createGain();
|
|
203
|
+
applyParam(level.gain, req.volume ?? 1, startTime, playback.clipDuration, elapsed);
|
|
204
|
+
from.connect(level);
|
|
205
|
+
if (!req.transitionIn && !req.transitionOut)
|
|
206
|
+
return { output: level, nodes: [level] };
|
|
207
|
+
const transition = ctx.createGain();
|
|
208
|
+
applyTransitions(transition.gain, playback.transitionLength, req.transitionIn, req.transitionOut, startTime, elapsed);
|
|
209
|
+
level.connect(transition);
|
|
210
|
+
return { output: transition, nodes: [level, transition] };
|
|
211
|
+
}
|
|
212
|
+
/**
|
|
213
|
+
* Schedule a speed ramp onto an `AudioBufferSourceNode.playbackRate`.
|
|
214
|
+
*
|
|
215
|
+
* The ramp runs over **source** seconds and Web Audio schedules in context
|
|
216
|
+
* seconds, so this walks clip time in small steps, converts each to a source
|
|
217
|
+
* offset through the integral inverse, and lays down the rate there. `elapsed`
|
|
218
|
+
* is clip seconds already played at a mid-clip seek.
|
|
219
|
+
*/
|
|
220
|
+
export function applySpeedToPlaybackRate(rate, speed, sourceLength, clipDuration, startTime, elapsed) {
|
|
221
|
+
if (!speed)
|
|
222
|
+
return;
|
|
223
|
+
const step = 1 / CURVE_SAMPLES_PER_SECOND;
|
|
224
|
+
const seekAt = Math.max(0, elapsed);
|
|
225
|
+
const valueAt = (clipElapsed) => Math.max(1e-4, sampleRamp(speed, sourceTimeAtClip(speed, sourceLength, clipElapsed), sourceLength, 1));
|
|
226
|
+
rate.setValueAtTime(valueAt(seekAt), startTime);
|
|
227
|
+
for (let s = seekAt + step; s <= clipDuration + step; s += step) {
|
|
228
|
+
const clamped = Math.min(s, clipDuration);
|
|
229
|
+
rate.linearRampToValueAtTime(valueAt(clamped), startTime + (clamped - seekAt));
|
|
230
|
+
if (clamped >= clipDuration)
|
|
231
|
+
break;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
//# sourceMappingURL=effect-graph.js.map
|