@motionscript/browser 0.1.0-alpha.0 → 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.
Files changed (181) hide show
  1. package/CHANGELOG.md +187 -0
  2. package/NOTICE +49 -0
  3. package/README.md +96 -24
  4. package/dist/asset-bytes.d.ts +14 -5
  5. package/dist/asset-bytes.d.ts.map +1 -1
  6. package/dist/asset-bytes.js +23 -5
  7. package/dist/asset-bytes.js.map +1 -1
  8. package/dist/audio/bus-graph.d.ts +12 -3
  9. package/dist/audio/bus-graph.d.ts.map +1 -1
  10. package/dist/audio/bus-graph.js +35 -31
  11. package/dist/audio/bus-graph.js.map +1 -1
  12. package/dist/audio/effect-graph.d.ts +62 -0
  13. package/dist/audio/effect-graph.d.ts.map +1 -0
  14. package/dist/audio/effect-graph.js +234 -0
  15. package/dist/audio/effect-graph.js.map +1 -0
  16. package/dist/audio/index.d.ts.map +1 -1
  17. package/dist/audio/index.js +2 -3
  18. package/dist/audio/index.js.map +1 -1
  19. package/dist/audio/loop-region.d.ts +31 -0
  20. package/dist/audio/loop-region.d.ts.map +1 -0
  21. package/dist/audio/loop-region.js +43 -0
  22. package/dist/audio/loop-region.js.map +1 -0
  23. package/dist/audio/mixer.d.ts +4 -4
  24. package/dist/audio/mixer.d.ts.map +1 -1
  25. package/dist/audio/mixer.js +43 -44
  26. package/dist/audio/mixer.js.map +1 -1
  27. package/dist/audio/player.d.ts +15 -9
  28. package/dist/audio/player.d.ts.map +1 -1
  29. package/dist/audio/player.js +60 -69
  30. package/dist/audio/player.js.map +1 -1
  31. package/dist/browser/THIRD_PARTY_LICENSES.txt +784 -0
  32. package/dist/browser/audio.js +1 -1
  33. package/dist/browser/chunks/chunk-F3AZKPHU.js +2 -0
  34. package/dist/browser/chunks/chunk-F3AZKPHU.js.map +7 -0
  35. package/dist/browser/chunks/chunk-KLXMRKTL.js +2 -0
  36. package/dist/browser/chunks/chunk-L5UFQACH.js +163 -0
  37. package/dist/browser/chunks/chunk-L5UFQACH.js.map +7 -0
  38. package/dist/browser/chunks/mediabunny-mp3-encoder-ZCIHVVTJ.js +14 -0
  39. package/dist/browser/chunks/mediabunny-mp3-encoder-ZCIHVVTJ.js.map +7 -0
  40. package/dist/browser/index.js +52 -1490
  41. package/dist/browser/index.js.map +4 -4
  42. package/dist/browser/manifest.json +21 -4
  43. package/dist/browser/ms_renderer_bg.wasm +0 -0
  44. package/dist/concat.d.ts +33 -7
  45. package/dist/concat.d.ts.map +1 -1
  46. package/dist/concat.js +117 -72
  47. package/dist/concat.js.map +1 -1
  48. package/dist/deliver.d.ts +10 -0
  49. package/dist/deliver.d.ts.map +1 -0
  50. package/dist/deliver.js +22 -0
  51. package/dist/deliver.js.map +1 -0
  52. package/dist/dynamic-nodes.d.ts.map +1 -1
  53. package/dist/dynamic-nodes.js +9 -12
  54. package/dist/dynamic-nodes.js.map +1 -1
  55. package/dist/engine.d.ts +43 -15
  56. package/dist/engine.d.ts.map +1 -1
  57. package/dist/engine.js +58 -36
  58. package/dist/engine.js.map +1 -1
  59. package/dist/export-audio.d.ts +36 -0
  60. package/dist/export-audio.d.ts.map +1 -0
  61. package/dist/export-audio.js +56 -0
  62. package/dist/export-audio.js.map +1 -0
  63. package/dist/exporter.d.ts +134 -15
  64. package/dist/exporter.d.ts.map +1 -1
  65. package/dist/exporter.js +135 -90
  66. package/dist/exporter.js.map +1 -1
  67. package/dist/formats.d.ts +36 -0
  68. package/dist/formats.d.ts.map +1 -0
  69. package/dist/formats.js +83 -0
  70. package/dist/formats.js.map +1 -0
  71. package/dist/frame-loop.d.ts +190 -0
  72. package/dist/frame-loop.d.ts.map +1 -0
  73. package/dist/frame-loop.js +304 -0
  74. package/dist/frame-loop.js.map +1 -0
  75. package/dist/index.d.ts +12 -10
  76. package/dist/index.d.ts.map +1 -1
  77. package/dist/index.js +18 -47
  78. package/dist/index.js.map +1 -1
  79. package/dist/mix-timeline-audio.d.ts +7 -7
  80. package/dist/mix-timeline-audio.d.ts.map +1 -1
  81. package/dist/mix-timeline-audio.js +11 -14
  82. package/dist/mix-timeline-audio.js.map +1 -1
  83. package/dist/parallel/export-parallel.d.ts +32 -0
  84. package/dist/parallel/export-parallel.d.ts.map +1 -0
  85. package/dist/parallel/export-parallel.js +114 -0
  86. package/dist/parallel/export-parallel.js.map +1 -0
  87. package/dist/parallel/governor.d.ts +57 -0
  88. package/dist/parallel/governor.d.ts.map +1 -0
  89. package/dist/parallel/governor.js +79 -0
  90. package/dist/parallel/governor.js.map +1 -0
  91. package/dist/parallel/lane.d.ts +45 -0
  92. package/dist/parallel/lane.d.ts.map +1 -0
  93. package/dist/parallel/lane.js +2 -0
  94. package/dist/parallel/lane.js.map +1 -0
  95. package/dist/parallel/lanes.d.ts +20 -0
  96. package/dist/parallel/lanes.d.ts.map +1 -0
  97. package/dist/parallel/lanes.js +152 -0
  98. package/dist/parallel/lanes.js.map +1 -0
  99. package/dist/parallel/plan.d.ts +23 -0
  100. package/dist/parallel/plan.d.ts.map +1 -0
  101. package/dist/parallel/plan.js +30 -0
  102. package/dist/parallel/plan.js.map +1 -0
  103. package/dist/parallel/run-lanes.d.ts +38 -0
  104. package/dist/parallel/run-lanes.d.ts.map +1 -0
  105. package/dist/parallel/run-lanes.js +164 -0
  106. package/dist/parallel/run-lanes.js.map +1 -0
  107. package/dist/parallel/session.d.ts +9 -0
  108. package/dist/parallel/session.d.ts.map +1 -0
  109. package/dist/parallel/session.js +79 -0
  110. package/dist/parallel/session.js.map +1 -0
  111. package/dist/renderer.d.ts +22 -0
  112. package/dist/renderer.d.ts.map +1 -0
  113. package/dist/renderer.js +23 -0
  114. package/dist/renderer.js.map +1 -0
  115. package/dist/screenshot.d.ts +6 -6
  116. package/dist/screenshot.d.ts.map +1 -1
  117. package/dist/screenshot.js +4 -5
  118. package/dist/screenshot.js.map +1 -1
  119. package/dist/still.d.ts +32 -24
  120. package/dist/still.d.ts.map +1 -1
  121. package/dist/still.js +54 -54
  122. package/dist/still.js.map +1 -1
  123. package/dist/ticker.d.ts +41 -9
  124. package/dist/ticker.d.ts.map +1 -1
  125. package/dist/ticker.js +113 -41
  126. package/dist/ticker.js.map +1 -1
  127. package/package.json +11 -10
  128. package/dist/audio/filter-graph.d.ts +0 -58
  129. package/dist/audio/filter-graph.d.ts.map +0 -1
  130. package/dist/audio/filter-graph.js +0 -235
  131. package/dist/audio/filter-graph.js.map +0 -1
  132. package/dist/browser/chunks/GLTFLoader-PGXD4V5B.js +0 -2
  133. package/dist/browser/chunks/GLTFLoader-PGXD4V5B.js.map +0 -7
  134. package/dist/browser/chunks/SkeletonUtils-XYDMTGTM.js +0 -2
  135. package/dist/browser/chunks/chunk-CTLBXZSU.js +0 -4119
  136. package/dist/browser/chunks/chunk-CTLBXZSU.js.map +0 -7
  137. package/dist/browser/chunks/chunk-FQ32PDIU.js +0 -2
  138. package/dist/browser/chunks/chunk-FQ32PDIU.js.map +0 -7
  139. package/dist/browser/chunks/chunk-FY3BTKY4.js +0 -2
  140. package/dist/browser/chunks/chunk-FY3BTKY4.js.map +0 -7
  141. package/dist/browser/chunks/chunk-NSFLRI4P.js +0 -2
  142. package/dist/browser/chunks/chunk-NSFLRI4P.js.map +0 -7
  143. package/dist/browser/chunks/three.module-Z4EOVUWF.js +0 -2
  144. package/dist/browser/chunks/three.module-Z4EOVUWF.js.map +0 -7
  145. package/dist/decode/image-levels.d.ts +0 -137
  146. package/dist/decode/image-levels.d.ts.map +0 -1
  147. package/dist/decode/image-levels.js +0 -477
  148. package/dist/decode/image-levels.js.map +0 -1
  149. package/dist/decode/levels.d.ts +0 -37
  150. package/dist/decode/levels.d.ts.map +0 -1
  151. package/dist/decode/levels.js +0 -68
  152. package/dist/decode/levels.js.map +0 -1
  153. package/dist/decode/video-levels.d.ts +0 -37
  154. package/dist/decode/video-levels.d.ts.map +0 -1
  155. package/dist/decode/video-levels.js +0 -95
  156. package/dist/decode/video-levels.js.map +0 -1
  157. package/dist/getter.d.ts +0 -10
  158. package/dist/getter.d.ts.map +0 -1
  159. package/dist/getter.js +0 -38
  160. package/dist/getter.js.map +0 -1
  161. package/dist/measure-context.d.ts +0 -12
  162. package/dist/measure-context.d.ts.map +0 -1
  163. package/dist/measure-context.js +0 -12
  164. package/dist/measure-context.js.map +0 -1
  165. package/dist/output.d.ts +0 -7
  166. package/dist/output.d.ts.map +0 -1
  167. package/dist/output.js +0 -6
  168. package/dist/output.js.map +0 -1
  169. package/dist/render-context.d.ts +0 -30
  170. package/dist/render-context.d.ts.map +0 -1
  171. package/dist/render-context.js +0 -55
  172. package/dist/render-context.js.map +0 -1
  173. package/dist/storage-adapter.d.ts +0 -367
  174. package/dist/storage-adapter.d.ts.map +0 -1
  175. package/dist/storage-adapter.js +0 -1278
  176. package/dist/storage-adapter.js.map +0 -1
  177. package/dist/three/renderer.d.ts +0 -48
  178. package/dist/three/renderer.d.ts.map +0 -1
  179. package/dist/three/renderer.js +0 -177
  180. package/dist/three/renderer.js.map +0 -1
  181. /package/dist/browser/chunks/{SkeletonUtils-XYDMTGTM.js.map → chunk-KLXMRKTL.js.map} +0 -0
@@ -1,13 +1,13 @@
1
- import { type AssetCatalog } from "@motionscript/core";
2
- import type { AudioMixer, ScheduledAudioRequest } from "@motionscript/skia-render/export";
1
+ import type { AssetCatalog } from "@motionscript/core";
2
+ import type { AudioMixer, ScheduledAudioRequest } from "../frame-loop";
3
3
  /** Sample rate used when the caller doesn't pick one. */
4
4
  export declare const DEFAULT_SAMPLE_RATE = 44100;
5
5
  export interface MixAudioOptions {
6
6
  /** Defaults to {@link DEFAULT_SAMPLE_RATE}. */
7
7
  sampleRate?: number;
8
8
  /**
9
- * Resolves a request's `src` to the address its bytes live at, the same job
10
- * `WebStorageAdapter.assetUrl` does for the live paths. Omitted, a `src` is
9
+ * Resolves a request's `src` to the address its bytes live at, as the storage
10
+ * adapter does for the live paths. Omitted, a `src` is
11
11
  * fetched as written — right only while the host keys its manifest by URL.
12
12
  */
13
13
  catalog?: AssetCatalog;
@@ -1 +1 @@
1
- {"version":3,"file":"mixer.d.ts","sourceRoot":"","sources":["../../src/audio/mixer.ts"],"names":[],"mappings":"AAAA,OAAO,EAA4B,KAAK,YAAY,EAAE,MAAM,oBAAoB,CAAC;AACjF,OAAO,KAAK,EAAE,UAAU,EAAE,qBAAqB,EAAE,MAAM,kCAAkC,CAAC;AAS1F,yDAAyD;AACzD,eAAO,MAAM,mBAAmB,QAAQ,CAAC;AAEzC,MAAM,WAAW,eAAe;IAC5B,+CAA+C;IAC/C,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB;;;;OAIG;IACH,OAAO,CAAC,EAAE,YAAY,CAAC;CAC1B;AAuBD;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAsB,QAAQ,CAC1B,QAAQ,EAAE,SAAS,qBAAqB,EAAE,EAC1C,aAAa,EAAE,MAAM,EACrB,OAAO,GAAE,eAAoB,GAC9B,OAAO,CAAC,WAAW,GAAG,IAAI,CAAC,CAoF7B;AAED;;;;;;;;GAQG;AACH,qBAAa,aAAc,YAAW,UAAU,CAAC,WAAW,CAAC;IAErD,OAAO,CAAC,QAAQ,CAAC,UAAU;IAC3B,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAC;gBADR,UAAU,GAAE,MAA4B,EACxC,OAAO,CAAC,EAAE,YAAY,YAAA;IAG3C,GAAG,CACC,QAAQ,EAAE,SAAS,qBAAqB,EAAE,EAC1C,aAAa,EAAE,MAAM,GACtB,OAAO,CAAC,WAAW,GAAG,IAAI,CAAC;CAMjC;AAED;;;;;;;GAOG;AACH,wBAAgB,SAAS,CAAC,MAAM,EAAE,WAAW,GAAG,UAAU,CAAC,WAAW,CAAC,CA6CtE"}
1
+ {"version":3,"file":"mixer.d.ts","sourceRoot":"","sources":["../../src/audio/mixer.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AACvD,OAAO,KAAK,EAAE,UAAU,EAAE,qBAAqB,EAAE,MAAM,eAAe,CAAC;AAMvE,yDAAyD;AACzD,eAAO,MAAM,mBAAmB,QAAQ,CAAC;AAEzC,MAAM,WAAW,eAAe;IAC5B,+CAA+C;IAC/C,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB;;;;OAIG;IACH,OAAO,CAAC,EAAE,YAAY,CAAC;CAC1B;AA4BD;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAsB,QAAQ,CAC1B,QAAQ,EAAE,SAAS,qBAAqB,EAAE,EAC1C,aAAa,EAAE,MAAM,EACrB,OAAO,GAAE,eAAoB,GAC9B,OAAO,CAAC,WAAW,GAAG,IAAI,CAAC,CAsE7B;AAED;;;;;;;;GAQG;AACH,qBAAa,aAAc,YAAW,UAAU,CAAC,WAAW,CAAC;IAErD,OAAO,CAAC,QAAQ,CAAC,UAAU;IAC3B,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAC;gBADR,UAAU,GAAE,MAA4B,EACxC,OAAO,CAAC,EAAE,YAAY,YAAA;IAG3C,GAAG,CACC,QAAQ,EAAE,SAAS,qBAAqB,EAAE,EAC1C,aAAa,EAAE,MAAM,GACtB,OAAO,CAAC,WAAW,GAAG,IAAI,CAAC;CAMjC;AAED;;;;;;;GAOG;AACH,wBAAgB,SAAS,CAAC,MAAM,EAAE,WAAW,GAAG,UAAU,CAAC,WAAW,CAAC,CA6CtE"}
@@ -1,6 +1,7 @@
1
- import { sourceTimeAtSceneElapsed } from "@motionscript/core";
1
+ import { fetchAssetBytes, localAssetBytes } from "../asset-bytes";
2
2
  import { AudioBusGraph } from "./bus-graph";
3
- import { applySpeedToPlaybackRate, buildAudioFilterGraph, effectiveSpeed, speedCurveOf, } from "./filter-graph";
3
+ import { applyLoop } from "./loop-region";
4
+ import { applySpeedToPlaybackRate, buildAudioEffectGraph, buildLevel, playbackOf, sourceOffsetOf } from "./effect-graph";
4
5
  /** Sample rate used when the caller doesn't pick one. */
5
6
  export const DEFAULT_SAMPLE_RATE = 44100;
6
7
  /** Audio first, then video: a clip's own soundtrack is requested under the video's key. */
@@ -19,11 +20,16 @@ function audioUrl(src, catalog) {
19
20
  }
20
21
  }
21
22
  }
22
- /** Fetches and decodes an audio source against a scratch `OfflineAudioContext` (decoding requires a context but doesn't render through it). */
23
+ /**
24
+ * Reads and decodes an audio source against a scratch `OfflineAudioContext`
25
+ * (decoding requires a context but doesn't render through it). The host's byte
26
+ * source is asked first, as for every other asset read: a mix that went to the
27
+ * network would fail where the preview played, offline or for bytes not yet
28
+ * uploaded.
29
+ */
23
30
  async function fetchAudioBuffer(src, ctx) {
24
- const response = await fetch(src);
25
- const arrayBuffer = await response.arrayBuffer();
26
- return ctx.decodeAudioData(arrayBuffer);
31
+ const blob = (await localAssetBytes(src)) ?? (await fetchAssetBytes(src));
32
+ return ctx.decodeAudioData(await blob.arrayBuffer());
27
33
  }
28
34
  /**
29
35
  * Render every scheduled request into one timeline-length buffer.
@@ -53,9 +59,18 @@ export async function mixAudio(requests, totalDuration, options = {}) {
53
59
  return null;
54
60
  const uniqueSrcs = [...new Set(requests.map(r => r.request.src))];
55
61
  const scratchCtx = new OfflineAudioContext(2, frames, sampleRate);
62
+ // A source only a picture's own sound wants may hold no audio at all, which
63
+ // is silence rather than a failed mix — see `AudioRequest.optional`.
64
+ const asked = new Set(requests.filter(r => !r.request.optional).map(r => r.request.src));
56
65
  const decoded = new Map();
57
66
  await Promise.all(uniqueSrcs.map(async (src) => {
58
- decoded.set(src, await fetchAudioBuffer(audioUrl(src, options.catalog), scratchCtx));
67
+ try {
68
+ decoded.set(src, await fetchAudioBuffer(audioUrl(src, options.catalog), scratchCtx));
69
+ }
70
+ catch (cause) {
71
+ if (asked.has(src))
72
+ throw cause;
73
+ }
59
74
  }));
60
75
  const mixCtx = new OfflineAudioContext(2, frames, sampleRate);
61
76
  // The same shared-chain rule the live device uses, for the same reason: an
@@ -74,43 +89,27 @@ export async function mixAudio(requests, totalDuration, options = {}) {
74
89
  continue;
75
90
  const source = mixCtx.createBufferSource();
76
91
  source.buffer = srcBuffer;
77
- source.loop = req.loop;
78
- const scalarSpeed = effectiveSpeed(req.filters);
79
- const rate = Number.isFinite(scalarSpeed) && scalarSpeed > 0 ? scalarSpeed : 1;
80
- const speedCurve = speedCurveOf(req.filters);
81
- // With a curve the scalar is folded into the scheduled ramp below, so
82
- // setting it here as well would apply it twice.
83
- if (rate !== 1 && !speedCurve)
84
- source.playbackRate.value = rate;
85
- const gain = mixCtx.createGain();
86
- gain.gain.value = Number.isFinite(req.volume) ? req.volume : 1;
87
- // The trimmed source length the filter and speed curves are authored over.
88
- const sourceLength = Math.max(0, srcBuffer.duration - req.trimStart);
89
- // The clip's own scene-time length — what a curve resolves against.
90
- // Deliberately *not* `playDuration`, which is that length truncated by the
91
- // timeline end: resolving against the truncated value would squeeze the
92
- // whole curve into the surviving part instead of cutting it off.
93
- const clipDuration = Number.isFinite(req.endAt)
94
- ? Math.max(0, req.endAt - req.startAt)
95
- : sourceLength / rate;
96
- // Filter chain sits between the source and the per-request gain, matching
97
- // the live WebAudioSink graph so exports sound identical to preview.
98
- // Passing the start time and clip duration is what lets curve-valued params
99
- // schedule — with the defaults they collapse to a single static value.
100
- // `elapsed` is 0: unlike a live seek, the mix always starts a clip at its
101
- // own beginning.
102
- const graph = buildAudioFilterGraph(mixCtx, source, req.filters ?? [], globalStart, clipDuration, 0);
103
- graph.output.connect(gain);
104
- gain.connect(buses.inputFor(req.bus, globalStart));
105
- if (speedCurve) {
106
- applySpeedToPlaybackRate(source.playbackRate, speedCurve, sourceLength, clipDuration, rate, globalStart, 0);
107
- }
108
- // start()'s 3rd arg is a source-buffer duration. A constant rate scales it
109
- // linearly, but under a speed curve the buffer consumed over a span of
110
- // scene time is the integral of the rate, not a multiple of it.
111
- const consumed = speedCurve
112
- ? sourceTimeAtSceneElapsed(speedCurve, sourceLength, playDuration)
113
- : playDuration * rate;
92
+ applyLoop(source, req);
93
+ // Ramps resolve against the clip's own length, deliberately *not*
94
+ // `playDuration`, which is that length truncated by the timeline end:
95
+ // resolving against the truncated value would squeeze the whole ramp
96
+ // into the surviving part instead of cutting it off. `elapsed` is 0:
97
+ // unlike a live seek, the mix always starts a clip at its beginning.
98
+ const playback = playbackOf(req, srcBuffer.duration);
99
+ if (playback.rate !== 1)
100
+ source.playbackRate.value = playback.rate;
101
+ applySpeedToPlaybackRate(source.playbackRate, playback.speedRamp, playback.sourceLength, playback.clipDuration, globalStart, 0);
102
+ // The same chain as the live WebAudioSink graph, so exports sound like the preview.
103
+ const graph = buildAudioEffectGraph(mixCtx, source, req.effects ?? [], globalStart, playback.clipDuration, 0);
104
+ const level = buildLevel(mixCtx, graph.output, req, playback, globalStart, 0);
105
+ // From the mix's start, whichever clip asks first: a bus's chain is shared,
106
+ // so its schedule cannot begin at one clip's start. Context time 0 is
107
+ // `-globalOffset` in this request's seconds, which are its stops' too.
108
+ level.output.connect(buses.inputFor(req.bus, 0, -globalOffset));
109
+ // start()'s 3rd arg is a source-buffer duration: under a speed ramp the
110
+ // buffer consumed over a span of clip time is the integral of the rate,
111
+ // not a multiple of it.
112
+ const consumed = sourceOffsetOf(playback, playDuration);
114
113
  source.start(globalStart, req.trimStart, consumed);
115
114
  for (const osc of graph.oscillators)
116
115
  osc.start(globalStart);
@@ -1 +1 @@
1
- {"version":3,"file":"mixer.js","sourceRoot":"","sources":["../../src/audio/mixer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,wBAAwB,EAAqB,MAAM,oBAAoB,CAAC;AAEjF,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAC5C,OAAO,EACH,wBAAwB,EACxB,qBAAqB,EACrB,cAAc,EACd,YAAY,GACf,MAAM,gBAAgB,CAAC;AAExB,yDAAyD;AACzD,MAAM,CAAC,MAAM,mBAAmB,GAAG,KAAK,CAAC;AAazC,2FAA2F;AAC3F,SAAS,QAAQ,CAAC,GAAW,EAAE,OAAiC;IAC5D,IAAI,CAAC,OAAO;QAAE,OAAO,GAAG,CAAC;IACzB,IAAI,CA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CAAC,SAAS,CAAC,EAAE,EAAE,CAAC,GAAG,cAAc,EAAE,IAAI,CAAC,CAAC;IAC7C,KAAK,CAAC,EAAE,EAAE,MAAM,CAAC,CAAC;IAClB,IAAI,CAAC,SAAS,CAAC,EAAE,EAAE,SAAS,EAAE,IAAI,CAAC,CAAC;IAEpC,6EAA6E;IAC7E,8DAA8D;IAC9D,MAAM,QAAQ,GAAmB,EAAE,CAAC;IACpC,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,YAAY,EAAE,CAAC,EAAE;QAAE,QAAQ,CAAC,IAAI,CAAC,MAAM,CAAC,cAAc,CAAC,CAAC,CAAC,CAAC,CAAC;IAE/E,IAAI,MAAM,GAAG,EAAE,CAAC;IAChB,KAAK,IAAI,KAAK,GAAG,CAAC,EAAE,KAAK,GAAG,UAAU,EAAE,KAAK,EAAE,EAAE,CAAC;QAC9C,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,YAAY,EAAE,CAAC,EAAE,EAAE,CAAC;YACpC,kEAAkE;YAClE,kEAAkE;YAClE,MAAM,MAAM,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,QAAQ,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;YAC7D,IAAI,CAAC,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,MAAM,GAAG,MAAM,CAAC,CAAC,CAAC,MAAM,GAAG,MAAM,EAAE,IAAI,CAAC,CAAC;YAC5E,MAAM,IAAI,cAAc,CAAC;QAC7B,CAAC;IACL,CAAC;IAED,OAAO,IAAI,UAAU,CAAC,GAAG,CAAC,CAAC;AAC/B,CAAC","sourcesContent":["import { sourceTimeAtSceneElapsed, type AssetCatalog } from \"@motionscript/core\";\nimport type { AudioMixer, ScheduledAudioRequest } from \"@motionscript/skia-render/export\";\nimport { AudioBusGraph } from \"./bus-graph\";\nimport {\n applySpeedToPlaybackRate,\n buildAudioFilterGraph,\n effectiveSpeed,\n speedCurveOf,\n} from \"./filter-graph\";\n\n/** Sample rate used when the caller doesn't pick one. */\nexport const DEFAULT_SAMPLE_RATE = 44100;\n\nexport interface MixAudioOptions {\n /** Defaults to {@link DEFAULT_SAMPLE_RATE}. */\n sampleRate?: number;\n /**\n * Resolves a request's `src` to the address its bytes live at, the same job\n * `WebStorageAdapter.assetUrl` does for the live paths. Omitted, a `src` is\n * fetched as written — right only while the host keys its manifest by URL.\n */\n catalog?: AssetCatalog;\n}\n\n/** Audio first, then video: a clip's own soundtrack is requested under the video's key. */\nfunction audioUrl(src: string, catalog: AssetCatalog | undefined): string {\n if (!catalog) return src;\n try {\n return catalog.getAudioMeta(src).src || src;\n } catch {\n try {\n return catalog.getVideoMeta(src).src || src;\n } catch {\n return src;\n }\n }\n}\n\n/** Fetches and decodes an audio source against a scratch `OfflineAudioContext` (decoding requires a context but doesn't render through it). */\nasync function fetchAudioBuffer(src: string, ctx: OfflineAudioContext): Promise<AudioBuffer> {\n const response = await fetch(src);\n const arrayBuffer = await response.arrayBuffer();\n return ctx.decodeAudioData(arrayBuffer);\n}\n\n/**\n * Render every scheduled request into one timeline-length buffer.\n *\n * An `OfflineAudioContext` graph — gain per request, looping/trimming, and the\n * same filter chain the live {@link WebAudioSink} builds — rendered in one\n * pass rather than scheduled per frame.\n *\n * Shared by the video exporter and by `mixTimelineAudio`, deliberately: a split\n * export and a whole one have to be the same mix, and two implementations of\n * \"stack these clips\" would eventually disagree.\n *\n * Curve-valued filter params and `speed` curves schedule here exactly as they do\n * in {@link WebAudioSink.playBuffer}, which is the reference implementation —\n * the two must be read side by side when either changes.\n *\n * One deliberate difference: this schedules at absolute context times, so clip\n * starts are sample-accurate here and quantized to `1/fps` in live playback,\n * which starts sources at `currentTime` on a clock tick.\n */\nexport async function mixAudio(\n requests: readonly ScheduledAudioRequest[],\n totalDuration: number,\n options: MixAudioOptions = {},\n): Promise<AudioBuffer | null> {\n if (requests.length === 0) return null;\n const sampleRate = options.sampleRate ?? DEFAULT_SAMPLE_RATE;\n const frames = Math.ceil(totalDuration * sampleRate);\n if (frames <= 0) return null;\n\n const uniqueSrcs = [...new Set(requests.map(r => r.request.src))];\n const scratchCtx = new OfflineAudioContext(2, frames, sampleRate);\n\n const decoded = new Map<string, AudioBuffer>();\n await Promise.all(uniqueSrcs.map(async (src) => {\n decoded.set(src, await fetchAudioBuffer(audioUrl(src, options.catalog), scratchCtx));\n }));\n\n const mixCtx = new OfflineAudioContext(2, frames, sampleRate);\n // The same shared-chain rule the live device uses, for the same reason: an\n // export that flattened buses into per-clip processing would not match what\n // the preview sounded like.\n const buses = new AudioBusGraph(mixCtx, mixCtx.destination);\n\n for (const { request: req, globalOffset } of requests) {\n const srcBuffer = decoded.get(req.src);\n if (!srcBuffer) continue;\n\n const globalStart = globalOffset + req.startAt;\n // `endAt` is `Infinity` for an open clip; either way the timeline bounds it.\n const globalEnd = Math.min(globalOffset + req.endAt, totalDuration);\n const playDuration = globalEnd - globalStart;\n if (playDuration <= 0) continue;\n\n const source = mixCtx.createBufferSource();\n source.buffer = srcBuffer;\n source.loop = req.loop;\n\n const scalarSpeed = effectiveSpeed(req.filters);\n const rate = Number.isFinite(scalarSpeed) && scalarSpeed > 0 ? scalarSpeed : 1;\n const speedCurve = speedCurveOf(req.filters);\n // With a curve the scalar is folded into the scheduled ramp below, so\n // setting it here as well would apply it twice.\n if (rate !== 1 && !speedCurve) source.playbackRate.value = rate;\n\n const gain = mixCtx.createGain();\n gain.gain.value = Number.isFinite(req.volume) ? req.volume : 1;\n\n // The trimmed source length the filter and speed curves are authored over.\n const sourceLength = Math.max(0, srcBuffer.duration - req.trimStart);\n // The clip's own scene-time length — what a curve resolves against.\n // Deliberately *not* `playDuration`, which is that length truncated by the\n // timeline end: resolving against the truncated value would squeeze the\n // whole curve into the surviving part instead of cutting it off.\n const clipDuration = Number.isFinite(req.endAt)\n ? Math.max(0, req.endAt - req.startAt)\n : sourceLength / rate;\n\n // Filter chain sits between the source and the per-request gain, matching\n // the live WebAudioSink graph so exports sound identical to preview.\n // Passing the start time and clip duration is what lets curve-valued params\n // schedule — with the defaults they collapse to a single static value.\n // `elapsed` is 0: unlike a live seek, the mix always starts a clip at its\n // own beginning.\n const graph = buildAudioFilterGraph(\n mixCtx, source, req.filters ?? [], globalStart, clipDuration, 0,\n );\n graph.output.connect(gain);\n gain.connect(buses.inputFor(req.bus, globalStart));\n\n if (speedCurve) {\n applySpeedToPlaybackRate(\n source.playbackRate, speedCurve, sourceLength, clipDuration, rate, globalStart, 0,\n );\n }\n\n // start()'s 3rd arg is a source-buffer duration. A constant rate scales it\n // linearly, but under a speed curve the buffer consumed over a span of\n // scene time is the integral of the rate, not a multiple of it.\n const consumed = speedCurve\n ? sourceTimeAtSceneElapsed(speedCurve, sourceLength, playDuration)\n : playDuration * rate;\n\n source.start(globalStart, req.trimStart, consumed);\n for (const osc of graph.oscillators) osc.start(globalStart);\n }\n\n return mixCtx.startRendering();\n}\n\n/**\n * Adapter presenting {@link mixAudio} as the exporter's injected mixer seam.\n *\n * The sample rate is a constructor argument rather than a third `mix` parameter\n * because {@link AudioMixer} — the seam `renderTimeline` calls through — takes\n * exactly two, so anything passed as a third would be silently dropped. Fixing\n * it here rather than widening the interface keeps the rate a property of *this*\n * mixer, which is whose business it is.\n */\nexport class WebAudioMixer implements AudioMixer<AudioBuffer> {\n constructor(\n private readonly sampleRate: number = DEFAULT_SAMPLE_RATE,\n private readonly catalog?: AssetCatalog,\n ) { }\n\n mix(\n requests: readonly ScheduledAudioRequest[],\n totalDuration: number,\n ): Promise<AudioBuffer | null> {\n return mixAudio(requests, totalDuration, {\n sampleRate: this.sampleRate,\n catalog: this.catalog,\n });\n }\n}\n\n/**\n * Encode an `AudioBuffer` as a 16-bit PCM WAV.\n *\n * The only other encoder in this package is the AAC track inside the mp4 muxer,\n * which is reachable solely by exporting a video — so without this a mixdown has\n * no file representation at all. WAV because it needs no codec support and is\n * lossless; a host wanting mp3/aac can re-encode from here.\n */\nexport function encodeWav(buffer: AudioBuffer): Uint8Array<ArrayBuffer> {\n const channelCount = buffer.numberOfChannels;\n const frameCount = buffer.length;\n const bytesPerSample = 2;\n const blockAlign = channelCount * bytesPerSample;\n const dataBytes = frameCount * blockAlign;\n\n const out = new ArrayBuffer(44 + dataBytes);\n const view = new DataView(out);\n\n const ascii = (offset: number, text: string) => {\n for (let i = 0; i < text.length; i++) view.setUint8(offset + i, text.charCodeAt(i));\n };\n\n ascii(0, \"RIFF\");\n view.setUint32(4, 36 + dataBytes, true);\n ascii(8, \"WAVE\");\n ascii(12, \"fmt \");\n view.setUint32(16, 16, true); // PCM header length\n view.setUint16(20, 1, true); // format: PCM\n view.setUint16(22, channelCount, true);\n view.setUint32(24, buffer.sampleRate, true);\n view.setUint32(28, buffer.sampleRate * blockAlign, true);\n view.setUint16(32, blockAlign, true);\n view.setUint16(34, 8 * bytesPerSample, true);\n ascii(36, \"data\");\n view.setUint32(40, dataBytes, true);\n\n // Pull each channel out once — getChannelData is a live view, but calling it\n // per sample inside the interleave loop is measurably slower.\n const channels: Float32Array[] = [];\n for (let c = 0; c < channelCount; c++) channels.push(buffer.getChannelData(c));\n\n let offset = 44;\n for (let frame = 0; frame < frameCount; frame++) {\n for (let c = 0; c < channelCount; c++) {\n // Clamp before scaling: the mix bus can exceed [-1,1] where clips\n // overlap, and letting that wrap turns a loud passage into noise.\n const sample = Math.max(-1, Math.min(1, channels[c][frame]));\n view.setInt16(offset, sample < 0 ? sample * 0x8000 : sample * 0x7fff, true);\n offset += bytesPerSample;\n }\n }\n\n return new Uint8Array(out);\n}\n"]}
1
+ {"version":3,"file":"mixer.js","sourceRoot":"","sources":["../../src/audio/mixer.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,eAAe,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAC;AAClE,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAC5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AAE,EAAE,CAAC,GAAG,cAAc,EAAE,IAAI,CAAC,CAAC;IAC7C,KAAK,CAAC,EAAE,EAAE,MAAM,CAAC,CAAC;IAClB,IAAI,CAAC,SAAS,CAAC,EAAE,EAAE,SAAS,EAAE,IAAI,CAAC,CAAC;IAEpC,6EAA6E;IAC7E,8DAA8D;IAC9D,MAAM,QAAQ,GAAmB,EAAE,CAAC;IACpC,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,YAAY,EAAE,CAAC,EAAE;QAAE,QAAQ,CAAC,IAAI,CAAC,MAAM,CAAC,cAAc,CAAC,CAAC,CAAC,CAAC,CAAC;IAE/E,IAAI,MAAM,GAAG,EAAE,CAAC;IAChB,KAAK,IAAI,KAAK,GAAG,CAAC,EAAE,KAAK,GAAG,UAAU,EAAE,KAAK,EAAE,EAAE,CAAC;QAC9C,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,YAAY,EAAE,CAAC,EAAE,EAAE,CAAC;YACpC,kEAAkE;YAClE,kEAAkE;YAClE,MAAM,MAAM,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,QAAQ,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;YAC7D,IAAI,CAAC,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,MAAM,GAAG,MAAM,CAAC,CAAC,CAAC,MAAM,GAAG,MAAM,EAAE,IAAI,CAAC,CAAC;YAC5E,MAAM,IAAI,cAAc,CAAC;QAC7B,CAAC;IACL,CAAC;IAED,OAAO,IAAI,UAAU,CAAC,GAAG,CAAC,CAAC;AAC/B,CAAC","sourcesContent":["import type { AssetCatalog } from \"@motionscript/core\";\nimport type { AudioMixer, ScheduledAudioRequest } from \"../frame-loop\";\nimport { fetchAssetBytes, localAssetBytes } from \"../asset-bytes\";\nimport { AudioBusGraph } from \"./bus-graph\";\nimport { applyLoop } from \"./loop-region\";\nimport { applySpeedToPlaybackRate, buildAudioEffectGraph, buildLevel, playbackOf, sourceOffsetOf } from \"./effect-graph\";\n\n/** Sample rate used when the caller doesn't pick one. */\nexport const DEFAULT_SAMPLE_RATE = 44100;\n\nexport interface MixAudioOptions {\n /** Defaults to {@link DEFAULT_SAMPLE_RATE}. */\n sampleRate?: number;\n /**\n * Resolves a request's `src` to the address its bytes live at, as the storage\n * adapter does for the live paths. Omitted, a `src` is\n * fetched as written — right only while the host keys its manifest by URL.\n */\n catalog?: AssetCatalog;\n}\n\n/** Audio first, then video: a clip's own soundtrack is requested under the video's key. */\nfunction audioUrl(src: string, catalog: AssetCatalog | undefined): string {\n if (!catalog) return src;\n try {\n return catalog.getAudioMeta(src).src || src;\n } catch {\n try {\n return catalog.getVideoMeta(src).src || src;\n } catch {\n return src;\n }\n }\n}\n\n/**\n * Reads and decodes an audio source against a scratch `OfflineAudioContext`\n * (decoding requires a context but doesn't render through it). The host's byte\n * source is asked first, as for every other asset read: a mix that went to the\n * network would fail where the preview played, offline or for bytes not yet\n * uploaded.\n */\nasync function fetchAudioBuffer(src: string, ctx: OfflineAudioContext): Promise<AudioBuffer> {\n const blob = (await localAssetBytes(src)) ?? (await fetchAssetBytes(src));\n return ctx.decodeAudioData(await blob.arrayBuffer());\n}\n\n/**\n * Render every scheduled request into one timeline-length buffer.\n *\n * An `OfflineAudioContext` graph — gain per request, looping/trimming, and the\n * same filter chain the live {@link WebAudioSink} builds — rendered in one\n * pass rather than scheduled per frame.\n *\n * Shared by the video exporter and by `mixTimelineAudio`, deliberately: a split\n * export and a whole one have to be the same mix, and two implementations of\n * \"stack these clips\" would eventually disagree.\n *\n * Curve-valued filter params and `speed` curves schedule here exactly as they do\n * in {@link WebAudioSink.playBuffer}, which is the reference implementation —\n * the two must be read side by side when either changes.\n *\n * One deliberate difference: this schedules at absolute context times, so clip\n * starts are sample-accurate here and quantized to `1/fps` in live playback,\n * which starts sources at `currentTime` on a clock tick.\n */\nexport async function mixAudio(\n requests: readonly ScheduledAudioRequest[],\n totalDuration: number,\n options: MixAudioOptions = {},\n): Promise<AudioBuffer | null> {\n if (requests.length === 0) return null;\n const sampleRate = options.sampleRate ?? DEFAULT_SAMPLE_RATE;\n const frames = Math.ceil(totalDuration * sampleRate);\n if (frames <= 0) return null;\n\n const uniqueSrcs = [...new Set(requests.map(r => r.request.src))];\n const scratchCtx = new OfflineAudioContext(2, frames, sampleRate);\n\n // A source only a picture's own sound wants may hold no audio at all, which\n // is silence rather than a failed mix — see `AudioRequest.optional`.\n const asked = new Set(requests.filter(r => !r.request.optional).map(r => r.request.src));\n const decoded = new Map<string, AudioBuffer>();\n await Promise.all(uniqueSrcs.map(async (src) => {\n try {\n decoded.set(src, await fetchAudioBuffer(audioUrl(src, options.catalog), scratchCtx));\n } catch (cause) {\n if (asked.has(src)) throw cause;\n }\n }));\n\n const mixCtx = new OfflineAudioContext(2, frames, sampleRate);\n // The same shared-chain rule the live device uses, for the same reason: an\n // export that flattened buses into per-clip processing would not match what\n // the preview sounded like.\n const buses = new AudioBusGraph(mixCtx, mixCtx.destination);\n\n for (const { request: req, globalOffset } of requests) {\n const srcBuffer = decoded.get(req.src);\n if (!srcBuffer) continue;\n\n const globalStart = globalOffset + req.startAt;\n // `endAt` is `Infinity` for an open clip; either way the timeline bounds it.\n const globalEnd = Math.min(globalOffset + req.endAt, totalDuration);\n const playDuration = globalEnd - globalStart;\n if (playDuration <= 0) continue;\n\n const source = mixCtx.createBufferSource();\n source.buffer = srcBuffer;\n applyLoop(source, req);\n\n // Ramps resolve against the clip's own length, deliberately *not*\n // `playDuration`, which is that length truncated by the timeline end:\n // resolving against the truncated value would squeeze the whole ramp\n // into the surviving part instead of cutting it off. `elapsed` is 0:\n // unlike a live seek, the mix always starts a clip at its beginning.\n const playback = playbackOf(req, srcBuffer.duration);\n if (playback.rate !== 1) source.playbackRate.value = playback.rate;\n applySpeedToPlaybackRate(\n source.playbackRate, playback.speedRamp, playback.sourceLength, playback.clipDuration, globalStart, 0,\n );\n\n // The same chain as the live WebAudioSink graph, so exports sound like the preview.\n const graph = buildAudioEffectGraph(mixCtx, source, req.effects ?? [], globalStart, playback.clipDuration, 0);\n const level = buildLevel(mixCtx, graph.output, req, playback, globalStart, 0);\n // From the mix's start, whichever clip asks first: a bus's chain is shared,\n // so its schedule cannot begin at one clip's start. Context time 0 is\n // `-globalOffset` in this request's seconds, which are its stops' too.\n level.output.connect(buses.inputFor(req.bus, 0, -globalOffset));\n\n // start()'s 3rd arg is a source-buffer duration: under a speed ramp the\n // buffer consumed over a span of clip time is the integral of the rate,\n // not a multiple of it.\n const consumed = sourceOffsetOf(playback, playDuration);\n\n source.start(globalStart, req.trimStart, consumed);\n for (const osc of graph.oscillators) osc.start(globalStart);\n }\n\n return mixCtx.startRendering();\n}\n\n/**\n * Adapter presenting {@link mixAudio} as the exporter's injected mixer seam.\n *\n * The sample rate is a constructor argument rather than a third `mix` parameter\n * because {@link AudioMixer} — the seam `renderTimeline` calls through — takes\n * exactly two, so anything passed as a third would be silently dropped. Fixing\n * it here rather than widening the interface keeps the rate a property of *this*\n * mixer, which is whose business it is.\n */\nexport class WebAudioMixer implements AudioMixer<AudioBuffer> {\n constructor(\n private readonly sampleRate: number = DEFAULT_SAMPLE_RATE,\n private readonly catalog?: AssetCatalog,\n ) { }\n\n mix(\n requests: readonly ScheduledAudioRequest[],\n totalDuration: number,\n ): Promise<AudioBuffer | null> {\n return mixAudio(requests, totalDuration, {\n sampleRate: this.sampleRate,\n catalog: this.catalog,\n });\n }\n}\n\n/**\n * Encode an `AudioBuffer` as a 16-bit PCM WAV.\n *\n * The only other encoder in this package is the AAC track inside the mp4 muxer,\n * which is reachable solely by exporting a video — so without this a mixdown has\n * no file representation at all. WAV because it needs no codec support and is\n * lossless; a host wanting mp3/aac can re-encode from here.\n */\nexport function encodeWav(buffer: AudioBuffer): Uint8Array<ArrayBuffer> {\n const channelCount = buffer.numberOfChannels;\n const frameCount = buffer.length;\n const bytesPerSample = 2;\n const blockAlign = channelCount * bytesPerSample;\n const dataBytes = frameCount * blockAlign;\n\n const out = new ArrayBuffer(44 + dataBytes);\n const view = new DataView(out);\n\n const ascii = (offset: number, text: string) => {\n for (let i = 0; i < text.length; i++) view.setUint8(offset + i, text.charCodeAt(i));\n };\n\n ascii(0, \"RIFF\");\n view.setUint32(4, 36 + dataBytes, true);\n ascii(8, \"WAVE\");\n ascii(12, \"fmt \");\n view.setUint32(16, 16, true); // PCM header length\n view.setUint16(20, 1, true); // format: PCM\n view.setUint16(22, channelCount, true);\n view.setUint32(24, buffer.sampleRate, true);\n view.setUint32(28, buffer.sampleRate * blockAlign, true);\n view.setUint16(32, blockAlign, true);\n view.setUint16(34, 8 * bytesPerSample, true);\n ascii(36, \"data\");\n view.setUint32(40, dataBytes, true);\n\n // Pull each channel out once — getChannelData is a live view, but calling it\n // per sample inside the interleave loop is measurably slower.\n const channels: Float32Array[] = [];\n for (let c = 0; c < channelCount; c++) channels.push(buffer.getChannelData(c));\n\n let offset = 44;\n for (let frame = 0; frame < frameCount; frame++) {\n for (let c = 0; c < channelCount; c++) {\n // Clamp before scaling: the mix bus can exceed [-1,1] where clips\n // overlap, and letting that wrap turns a loud passage into noise.\n const sample = Math.max(-1, Math.min(1, channels[c][frame]));\n view.setInt16(offset, sample < 0 ? sample * 0x8000 : sample * 0x7fff, true);\n offset += bytesPerSample;\n }\n }\n\n return new Uint8Array(out);\n}\n"]}
@@ -4,23 +4,29 @@ import { AudioSink, AudioRequest } from "@motionscript/core";
4
4
  * buffers, then schedules/plays {@link AudioRequest}s as `AudioBufferSourceNode`s
5
5
  * through a shared master gain (global mute and volume). `schedule` registers the
6
6
  * active set of requests for the current frame; `syncTo` then starts/stops
7
- * sources to match `sceneTime`, so playback follows scrubbing and seeks.
7
+ * sources to match `clipTime`, so playback follows scrubbing and seeks.
8
8
  */
9
9
  export declare class WebAudioSink extends AudioSink {
10
- private context;
11
- private ownsContext;
10
+ private readonly borrowed;
11
+ private _context;
12
12
  private bufferCache;
13
13
  private decoding;
14
14
  private active;
15
15
  private scheduled;
16
16
  private disposed;
17
- private masterGain;
18
- /** The live bus chains, shared across every clip that names one. */
19
- private buses;
17
+ /** The master gain and the live bus chains (shared across every clip that names one), built with the context. */
18
+ private _graph;
20
19
  private muted;
21
20
  private volume;
22
21
  constructor(context?: AudioContext);
22
+ /**
23
+ * The context, created on first use rather than with the sink: a new
24
+ * AudioContext opens the audio device (50–65 ms on a desktop), and a
25
+ * session that never plays or decodes sound never needs one.
26
+ */
23
27
  getContext(): AudioContext;
28
+ private get context();
29
+ private get graph();
24
30
  /**
25
31
  * Resume the AudioContext. Must be called from within a user gesture
26
32
  * (e.g. a click handler) the first time, or browsers keep the context
@@ -33,15 +39,15 @@ export declare class WebAudioSink extends AudioSink {
33
39
  retain(keep: ReadonlySet<string>): void;
34
40
  /** Replaces the active request set; stops any currently-playing source whose request is no longer scheduled. Actual start/stop-to-match-time happens in `syncTo`. */
35
41
  schedule(requests: readonly AudioRequest[]): void;
36
- /** Starts sources whose `[startAt, endAt)` window now contains `sceneTime` (computing the correct buffer offset for the seek point) and stops any that have fallen outside it. */
37
- syncTo(sceneTime: number): void;
42
+ /** Starts sources whose `[startAt, endAt)` window now contains `clipTime` (computing the correct buffer offset for the seek point) and stops any that have fallen outside it. */
43
+ syncTo(clipTime: number): void;
38
44
  play(time: number, _speed: number, _reverse: boolean): Promise<void>;
39
45
  stop(): void;
40
46
  setMuted(muted: boolean): void;
41
47
  setVolume(volume: number): void;
42
48
  /** Mute and volume are held separately so unmuting restores the set level. */
43
49
  private applyMasterGain;
44
- /** Starts a buffer source mid-clip if `sceneTime` lands after the request's start — `audioOffset` accounts for the request's trim, the elapsed time since `startAt`, and any speed (playbackRate) change. */
50
+ /** Starts a buffer source mid-clip if `clipTime` lands after the request's start — the offset accounts for the request's trim, the elapsed time since `startAt`, and its speed. */
45
51
  private playBuffer;
46
52
  private stopSource;
47
53
  dispose():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AqBxB,IAAI,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,OAAO,GAAG,OAAO,CAAC,IAAI,CAAC;IAO1E,IAAI,IAAI,IAAI;IASZ,QAAQ,CAAC,KAAK,EAAE,OAAO,GAAG,IAAI;IAK9B,SAAS,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI;IAK/B,8EAA8E;IAC9E,OAAO,CAAC,eAAe;IAIvB,mLAAmL;IACnL,OAAO,CAAC,UAAU;IAwClB,OAAO,CAAC,UAAU;IAalB,OAAO,IAAI,IAAI;CAkBlB"}
@@ -1,36 +1,50 @@
1
- import { AudioSink, sourceTimeAtSceneElapsed } from "@motionscript/core";
1
+ import { AudioSink } from "@motionscript/core";
2
2
  import { AudioBusGraph } from "./bus-graph";
3
- import { buildAudioFilterGraph, effectiveSpeed, speedCurveOf, applySpeedToPlaybackRate } from "./filter-graph";
3
+ import { applyLoop, wrapIntoRegion } from "./loop-region";
4
+ import { applySpeedToPlaybackRate, buildAudioEffectGraph, buildLevel, playbackOf, sourceOffsetOf } from "./effect-graph";
4
5
  /**
5
6
  * Web Audio implementation of {@link AudioSink} — decodes and caches source
6
7
  * buffers, then schedules/plays {@link AudioRequest}s as `AudioBufferSourceNode`s
7
8
  * through a shared master gain (global mute and volume). `schedule` registers the
8
9
  * active set of requests for the current frame; `syncTo` then starts/stops
9
- * sources to match `sceneTime`, so playback follows scrubbing and seeks.
10
+ * sources to match `clipTime`, so playback follows scrubbing and seeks.
10
11
  */
11
12
  export class WebAudioSink extends AudioSink {
12
- context;
13
- ownsContext;
13
+ borrowed;
14
+ _context = null;
14
15
  bufferCache = new Map();
15
16
  decoding = new Map();
16
17
  active = new Map();
17
18
  scheduled = [];
18
19
  disposed = false;
19
- masterGain;
20
- /** The live bus chains, shared across every clip that names one. */
21
- buses;
20
+ /** The master gain and the live bus chains (shared across every clip that names one), built with the context. */
21
+ _graph = null;
22
22
  muted = false;
23
23
  volume = 1;
24
24
  constructor(context) {
25
25
  super();
26
- this.context = context ?? new AudioContext();
27
- this.ownsContext = !context;
28
- this.masterGain = this.context.createGain();
29
- this.masterGain.connect(this.context.destination);
30
- this.buses = new AudioBusGraph(this.context, this.masterGain);
26
+ this.borrowed = context;
31
27
  }
28
+ /**
29
+ * The context, created on first use rather than with the sink: a new
30
+ * AudioContext opens the audio device (50–65 ms on a desktop), and a
31
+ * session that never plays or decodes sound never needs one.
32
+ */
32
33
  getContext() {
33
- return this.context;
34
+ this._context ??= this.borrowed ?? new AudioContext();
35
+ return this._context;
36
+ }
37
+ get context() {
38
+ return this.getContext();
39
+ }
40
+ get graph() {
41
+ if (!this._graph) {
42
+ const master = this.context.createGain();
43
+ master.connect(this.context.destination);
44
+ this._graph = { master, buses: new AudioBusGraph(this.context, master) };
45
+ this.applyMasterGain();
46
+ }
47
+ return this._graph;
34
48
  }
35
49
  /**
36
50
  * Resume the AudioContext. Must be called from within a user gesture
@@ -83,20 +97,20 @@ export class WebAudioSink extends AudioSink {
83
97
  }
84
98
  this.scheduled = requests.slice();
85
99
  }
86
- /** Starts sources whose `[startAt, endAt)` window now contains `sceneTime` (computing the correct buffer offset for the seek point) and stops any that have fallen outside it. */
87
- syncTo(sceneTime) {
100
+ /** Starts sources whose `[startAt, endAt)` window now contains `clipTime` (computing the correct buffer offset for the seek point) and stops any that have fallen outside it. */
101
+ syncTo(clipTime) {
88
102
  const liveIds = new Set();
89
103
  for (const req of this.scheduled) {
90
104
  // `endAt` is `Infinity` for an open clip, so the comparison needs no
91
105
  // nullish fallback — see AudioRequest.endAt.
92
- const isInWindow = sceneTime >= req.startAt && sceneTime < req.endAt;
106
+ const isInWindow = clipTime >= req.startAt && clipTime < req.endAt;
93
107
  if (!isInWindow)
94
108
  continue;
95
109
  liveIds.add(req.id);
96
110
  if (!this.active.has(req.id)) {
97
111
  const buffer = this.bufferCache.get(req.src);
98
112
  if (buffer)
99
- this.playBuffer(buffer, req, sceneTime);
113
+ this.playBuffer(buffer, req, clipTime);
100
114
  }
101
115
  }
102
116
  for (const [id, active] of this.active) {
@@ -114,6 +128,9 @@ export class WebAudioSink extends AudioSink {
114
128
  for (const [id, active] of this.active) {
115
129
  this.stopSource(id, active);
116
130
  }
131
+ // A bus's curves are scheduled against the clock of the run that built
132
+ // it, so the next play, from wherever it lands, builds them again.
133
+ this._graph?.buses.dispose();
117
134
  }
118
135
  setMuted(muted) {
119
136
  this.muted = muted;
@@ -125,53 +142,28 @@ export class WebAudioSink extends AudioSink {
125
142
  }
126
143
  /** Mute and volume are held separately so unmuting restores the set level. */
127
144
  applyMasterGain() {
128
- this.masterGain.gain.value = this.muted ? 0 : this.volume;
145
+ if (this._graph)
146
+ this._graph.master.gain.value = this.muted ? 0 : this.volume;
129
147
  }
130
- /** Starts a buffer source mid-clip if `sceneTime` lands after the request's start — `audioOffset` accounts for the request's trim, the elapsed time since `startAt`, and any speed (playbackRate) change. */
131
- playBuffer(buffer, req, sceneTime) {
132
- const gainNode = this.context.createGain();
133
- // Guard against a missing/NaN volume — a non-finite AudioParam value throws.
134
- gainNode.gain.value = Number.isFinite(req.volume) ? req.volume : 1;
148
+ /** Starts a buffer source mid-clip if `clipTime` lands after the request's start — the offset accounts for the request's trim, the elapsed time since `startAt`, and its speed. */
149
+ playBuffer(buffer, req, clipTime) {
150
+ const startTime = this.context.currentTime;
151
+ const playback = playbackOf(req, buffer.duration);
152
+ const elapsed = Math.max(0, clipTime - req.startAt);
153
+ const source = this.context.createBufferSource();
154
+ source.buffer = buffer;
155
+ const loopRegion = applyLoop(source, req);
156
+ if (playback.rate !== 1)
157
+ source.playbackRate.value = playback.rate;
158
+ applySpeedToPlaybackRate(source.playbackRate, playback.speedRamp, playback.sourceLength, playback.clipDuration, startTime, elapsed);
159
+ const graph = buildAudioEffectGraph(this.context, source, req.effects ?? [], startTime, playback.clipDuration, elapsed);
160
+ const level = buildLevel(this.context, graph.output, req, playback, startTime, elapsed);
135
161
  // Into this clip's bus chain, or straight to the master when it has none.
136
162
  // The chain is shared: two clips on one bus reach the same nodes, which is
137
163
  // what makes a bus effect one tail rather than one per clip.
138
- gainNode.connect(this.buses.inputFor(req.bus, this.context.currentTime));
139
- const source = this.context.createBufferSource();
140
- source.buffer = buffer;
141
- source.loop = req.loop;
142
- const scalarSpeed = effectiveSpeed(req.filters);
143
- const rate = Number.isFinite(scalarSpeed) && scalarSpeed > 0 ? scalarSpeed : 1;
144
- const speedCurve = speedCurveOf(req.filters);
145
- if (rate !== 1 && !speedCurve)
146
- source.playbackRate.value = rate;
147
- const elapsed = Math.max(0, sceneTime - req.startAt);
148
- // Trimmed source length the filter curves / speed curve are authored over.
149
- const sourceLength = Math.max(0, buffer.duration - req.trimStart);
150
- // Scene-time length of the clip. Prefer the bounded [startAt, endAt) span; if
151
- // endAt is unbounded (an open/looping clip reaching the device unresolved),
152
- // fall back to the source length scaled by speed so curve params still resolve
153
- // (a clipDuration of 0 would otherwise silently drop every fade/sweep).
154
- const clipDuration = isFinite(req.endAt)
155
- ? Math.max(0, req.endAt - req.startAt)
156
- : sourceLength / (rate || 1);
157
- // Insert the filter chain between the source and the per-request gain. Pass
158
- // the start time, clip duration, and elapsed so curve-valued params schedule
159
- // and align to a mid-clip seek.
160
- const startTime = this.context.currentTime;
161
- const graph = buildAudioFilterGraph(this.context, source, req.filters ?? [], startTime, clipDuration, elapsed);
162
- graph.output.connect(gainNode);
163
- // Source-time offset for a mid-clip seek. Scalar speed scales linearly; a
164
- // speed curve uses the integral inverse to find the true source position.
165
- let audioOffset;
166
- if (speedCurve) {
167
- audioOffset = req.trimStart + sourceTimeAtSceneElapsed(speedCurve, sourceLength, elapsed);
168
- // Schedule the time-varying playbackRate (folds in any scalar speed too).
169
- applySpeedToPlaybackRate(source.playbackRate, speedCurve, sourceLength, clipDuration, rate, startTime, elapsed);
170
- }
171
- else {
172
- audioOffset = req.trimStart + elapsed * rate;
173
- }
174
- source.start(startTime, audioOffset);
164
+ level.output.connect(this.graph.buses.inputFor(req.bus, startTime, clipTime));
165
+ const audioOffset = req.trimStart + sourceOffsetOf(playback, elapsed);
166
+ source.start(startTime, wrapIntoRegion(loopRegion, audioOffset));
175
167
  for (const osc of graph.oscillators)
176
168
  osc.start(startTime);
177
169
  source.onended = () => {
@@ -181,9 +173,8 @@ export class WebAudioSink extends AudioSink {
181
173
  };
182
174
  this.active.set(req.id, {
183
175
  source,
184
- gainNode,
185
176
  request: req,
186
- filterNodes: graph.nodes,
177
+ nodes: [...graph.nodes, ...level.nodes],
187
178
  oscillators: graph.oscillators,
188
179
  });
189
180
  }
@@ -194,9 +185,8 @@ export class WebAudioSink extends AudioSink {
194
185
  for (const osc of active.oscillators)
195
186
  osc.stop();
196
187
  active.source.disconnect();
197
- for (const node of active.filterNodes)
188
+ for (const node of active.nodes)
198
189
  node.disconnect();
199
- active.gainNode.disconnect();
200
190
  }
201
191
  catch {
202
192
  // already stopped
@@ -211,15 +201,16 @@ export class WebAudioSink extends AudioSink {
211
201
  this.bufferCache.clear();
212
202
  this.decoding.clear();
213
203
  this.scheduled.length = 0;
214
- this.buses.dispose();
204
+ this._graph?.buses.dispose();
215
205
  try {
216
- this.masterGain.disconnect();
206
+ this._graph?.master.disconnect();
217
207
  }
218
208
  catch {
219
209
  // already disconnected
220
210
  }
221
- if (this.ownsContext && this.context.state !== "closed") {
222
- this.context.close();
211
+ const owned = this.borrowed ? null : this._context;
212
+ if (owned && owned.state !== "closed") {
213
+ owned.close();
223
214
  }
224
215
  }
225
216
  }
@@ -1 +1 @@
1
- {"version":3,"file":"player.js","sourceRoot":"","sources":["../../src/audio/player.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAgB,wBAAwB,EAAE,MAAM,oBAAoB,CAAC;AACvF,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAC5C,OAAO,EAAE,qBAAqB,EAAE,cAAc,EAAE,YAAY,EAAE,wBAAwB,EAAE,MAAM,gBAAgB,CAAC;AAY/G;;;;;;GAMG;AACH,MAAM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AAQ,EAAE,CAAC;YACtD,IAAI,CAAC,OAAO,CAAC,KAAK,EAAE,CAAC;QACzB,CAAC;IACL,CAAC;CACJ","sourcesContent":["import { AudioSink, AudioRequest, sourceTimeAtSceneElapsed } from \"@motionscript/core\";\nimport { AudioBusGraph } from \"./bus-graph\";\nimport { buildAudioFilterGraph, effectiveSpeed, speedCurveOf, applySpeedToPlaybackRate } from \"./filter-graph\";\n\ntype ActiveSource = {\n source: AudioBufferSourceNode;\n gainNode: GainNode;\n request: AudioRequest;\n /** Filter-graph nodes to disconnect on teardown. */\n filterNodes: AudioNode[];\n /** Tremolo LFOs to stop on teardown. */\n oscillators: OscillatorNode[];\n};\n\n/**\n * Web Audio implementation of {@link AudioSink} — decodes and caches source\n * buffers, then schedules/plays {@link AudioRequest}s as `AudioBufferSourceNode`s\n * through a shared master gain (global mute and volume). `schedule` registers the\n * active set of requests for the current frame; `syncTo` then starts/stops\n * sources to match `sceneTime`, so playback follows scrubbing and seeks.\n */\nexport class WebAudioSink extends AudioSink {\n private context: AudioContext;\n private ownsContext: boolean;\n private bufferCache = new Map<string, AudioBuffer>();\n private decoding = new Map<string, Promise<void>>();\n private active = new Map<string, ActiveSource>();\n private scheduled: AudioRequest[] = [];\n private disposed: boolean = false;\n private masterGain: GainNode;\n /** The live bus chains, shared across every clip that names one. */\n private buses: AudioBusGraph;\n private muted = false;\n private volume = 1;\n\n constructor(context?: AudioContext) {\n super();\n this.context = context ?? new AudioContext();\n this.ownsContext = !context;\n this.masterGain = this.context.createGain();\n this.masterGain.connect(this.context.destination);\n this.buses = new AudioBusGraph(this.context, this.masterGain);\n }\n\n getContext(): AudioContext {\n return this.context;\n }\n\n /**\n * Resume the AudioContext. Must be called from within a user gesture\n * (e.g. a click handler) the first time, or browsers keep the context\n * suspended and no audio is heard.\n */\n unlock(): void {\n if (this.context.state === \"suspended\") {\n this.context.resume();\n }\n }\n\n has(src: string): boolean {\n return this.bufferCache.has(src);\n }\n\n async append(src: string, data: ArrayBuffer): Promise<void> {\n if (this.bufferCache.has(src)) return;\n const existing = this.decoding.get(src);\n if (existing) return existing;\n\n // decodeAudioData detaches `data` — that's fine, the caller in\n // AssetManager.fetchAudio discards its reference immediately after\n // awaiting append. Skipping the defensive .slice(0) saves a multi-MB\n // allocation per audio source.\n const job = this.context.decodeAudioData(data).then((buffer) => {\n this.bufferCache.set(src, buffer);\n }).finally(() => {\n this.decoding.delete(src);\n });\n this.decoding.set(src, job);\n await job;\n }\n\n /** Drops cached buffers and stops active sources for srcs/requests no longer referenced — called when the asset set changes (e.g. seeking across scene boundaries). */\n retain(keep: ReadonlySet<string>): void {\n for (const src of [...this.bufferCache.keys()]) {\n if (!keep.has(src)) this.bufferCache.delete(src);\n }\n for (const [id, active] of this.active) {\n if (!keep.has(active.request.src)) this.stopSource(id, active);\n }\n }\n\n /** Replaces the active request set; stops any currently-playing source whose request is no longer scheduled. Actual start/stop-to-match-time happens in `syncTo`. */\n schedule(requests: readonly AudioRequest[]): void {\n const next = new Set(requests.map(r => r.id));\n for (const [id, active] of this.active) {\n if (!next.has(id)) this.stopSource(id, active);\n }\n this.scheduled = requests.slice();\n }\n\n /** Starts sources whose `[startAt, endAt)` window now contains `sceneTime` (computing the correct buffer offset for the seek point) and stops any that have fallen outside it. */\n syncTo(sceneTime: number): void {\n const liveIds = new Set<string>();\n\n for (const req of this.scheduled) {\n // `endAt` is `Infinity` for an open clip, so the comparison needs no\n // nullish fallback — see AudioRequest.endAt.\n const isInWindow = sceneTime >= req.startAt && sceneTime < req.endAt;\n if (!isInWindow) continue;\n\n liveIds.add(req.id);\n if (!this.active.has(req.id)) {\n const buffer = this.bufferCache.get(req.src);\n if (buffer) this.playBuffer(buffer, req, sceneTime);\n }\n }\n\n for (const [id, active] of this.active) {\n if (!liveIds.has(id)) this.stopSource(id, active);\n }\n }\n\n async play(time: number, _speed: number, _reverse: boolean): Promise<void> {\n if (this.context.state === \"suspended\") {\n await this.context.resume();\n }\n this.syncTo(time);\n }\n\n stop(): void {\n for (const [id, active] of this.active) {\n this.stopSource(id, active);\n }\n }\n\n setMuted(muted: boolean): void {\n this.muted = muted;\n this.applyMasterGain();\n }\n\n setVolume(volume: number): void {\n this.volume = Math.max(0, volume);\n this.applyMasterGain();\n }\n\n /** Mute and volume are held separately so unmuting restores the set level. */\n private applyMasterGain(): void {\n this.masterGain.gain.value = this.muted ? 0 : this.volume;\n }\n\n /** Starts a buffer source mid-clip if `sceneTime` lands after the request's start — `audioOffset` accounts for the request's trim, the elapsed time since `startAt`, and any speed (playbackRate) change. */\n private playBuffer(buffer: AudioBuffer, req: AudioRequest, sceneTime: number): void {\n const gainNode = this.context.createGain();\n // Guard against a missing/NaN volume — a non-finite AudioParam value throws.\n gainNode.gain.value = Number.isFinite(req.volume) ? req.volume : 1;\n // Into this clip's bus chain, or straight to the master when it has none.\n // The chain is shared: two clips on one bus reach the same nodes, which is\n // what makes a bus effect one tail rather than one per clip.\n gainNode.connect(this.buses.inputFor(req.bus, this.context.currentTime));\n\n const source = this.context.createBufferSource();\n source.buffer = buffer;\n source.loop = req.loop;\n\n const scalarSpeed = effectiveSpeed(req.filters);\n const rate = Number.isFinite(scalarSpeed) && scalarSpeed > 0 ? scalarSpeed : 1;\n const speedCurve = speedCurveOf(req.filters);\n if (rate !== 1 && !speedCurve) source.playbackRate.value = rate;\n\n const elapsed = Math.max(0, sceneTime - req.startAt);\n // Trimmed source length the filter curves / speed curve are authored over.\n const sourceLength = Math.max(0, buffer.duration - req.trimStart);\n // Scene-time length of the clip. Prefer the bounded [startAt, endAt) span; if\n // endAt is unbounded (an open/looping clip reaching the device unresolved),\n // fall back to the source length scaled by speed so curve params still resolve\n // (a clipDuration of 0 would otherwise silently drop every fade/sweep).\n const clipDuration = isFinite(req.endAt)\n ? Math.max(0, req.endAt - req.startAt)\n : sourceLength / (rate || 1);\n\n // Insert the filter chain between the source and the per-request gain. Pass\n // the start time, clip duration, and elapsed so curve-valued params schedule\n // and align to a mid-clip seek.\n const startTime = this.context.currentTime;\n const graph = buildAudioFilterGraph(\n this.context, source, req.filters ?? [], startTime, clipDuration, elapsed,\n );\n graph.output.connect(gainNode);\n\n // Source-time offset for a mid-clip seek. Scalar speed scales linearly; a\n // speed curve uses the integral inverse to find the true source position.\n let audioOffset: number;\n if (speedCurve) {\n audioOffset = req.trimStart + sourceTimeAtSceneElapsed(speedCurve, sourceLength, elapsed);\n // Schedule the time-varying playbackRate (folds in any scalar speed too).\n applySpeedToPlaybackRate(\n source.playbackRate, speedCurve, sourceLength, clipDuration, rate, startTime, elapsed,\n );\n } else {\n audioOffset = req.trimStart + elapsed * rate;\n }\n\n source.start(startTime, audioOffset);\n for (const osc of graph.oscillators) osc.start(startTime);\n\n source.onended = () => {\n if (this.active.get(req.id)?.source === source) {\n this.active.delete(req.id);\n }\n };\n\n this.active.set(req.id, {\n source,\n gainNode,\n request: req,\n filterNodes: graph.nodes,\n oscillators: graph.oscillators,\n });\n }\n\n private stopSource(id: string, active: ActiveSource): void {\n try {\n active.source.onended = null;\n active.source.stop();\n for (const osc of active.oscillators) osc.stop();\n active.source.disconnect();\n for (const node of active.filterNodes) node.disconnect();\n active.gainNode.disconnect();\n } catch {\n // already stopped\n }\n this.active.delete(id);\n }\n\n dispose(): void {\n if (this.disposed) return;\n this.disposed = true;\n super.dispose();\n this.bufferCache.clear();\n this.decoding.clear();\n this.scheduled.length = 0;\n this.buses.dispose();\n try {\n this.masterGain.disconnect();\n } catch {\n // already disconnected\n }\n if (this.ownsContext && this.context.state !== \"closed\") {\n this.context.close();\n }\n }\n}\n"]}
1
+ {"version":3,"file":"player.js","sourceRoot":"","sources":["../../src/audio/player.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAgB,MAAM,oBAAoB,CAAC;AAC7D,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAC5C,OAAO,EAAE,SAAS,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAC1D,OAAO,EAAE,wBAAwB,EAAE,qBAAqB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CAAC,MAAM,CAAC,UAAU,EAAE,CAAC;YAC3B,KAAK,MAAM,IAAI,IAAI,MAAM,CAAC,KAAK;gBAAE,IAAI,CAAC,UAAU,EAAE,CAAC;QACvD,CAAC;QAAC,MAAM,CAAC;YACL,kBAAkB;QACtB,CAAC;QACD,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;IAC3B,CAAC;IAED,OAAO;QACH,IAAI,IAAI,CAAC,QAAQ;YAAE,OAAO;QAC1B,IAAI,CAAC,QAAQ,GAAG,IAAI,CAAC;QACrB,KAAK,CAAC,OAAO,EAAE,CAAC;QAChB,IAAI,CAAC,WAAW,CAAC,KAAK,EAAE,CAAC;QACzB,IAAI,CAAC,QAAQ,CAAC,KAAK,EAAE,CAAC;QACtB,IAAI,CAAC,SAAS,CAAC,MAAM,GAAG,CAAC,CAAC;QAC1B,IAAI,CAAC,MAAM,EAAE,KAAK,CAAC,OAAO,EAAE,CAAC;QAC7B,IAAI,CAAC;YACD,IAAI,CAAC,MAAM,EAAE,MAAM,CAAC,UAAU,EAAE,CAAC;QACrC,CAAC;QAAC,MAAM,CAAC;YACL,uBAAuB;QAC3B,CAAC;QACD,MAAM,KAAK,GAAG,IAAI,CAAC,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,QAAQ,CAAC;QACnD,IAAI,KAAK,IAAI,KAAK,CAAC,KAAK,KAAK,QAAQ,EAAE,CAAC;YACpC,KAAK,CAAC,KAAK,EAAE,CAAC;QAClB,CAAC;IACL,CAAC;CACJ","sourcesContent":["import { AudioSink, AudioRequest } from \"@motionscript/core\";\nimport { AudioBusGraph } from \"./bus-graph\";\nimport { applyLoop, wrapIntoRegion } from \"./loop-region\";\nimport { applySpeedToPlaybackRate, buildAudioEffectGraph, buildLevel, playbackOf, sourceOffsetOf } from \"./effect-graph\";\n\ntype ActiveSource = {\n source: AudioBufferSourceNode;\n request: AudioRequest;\n /** Effect and level nodes to disconnect on teardown. */\n nodes: AudioNode[];\n /** Tremolo LFOs to stop on teardown. */\n oscillators: OscillatorNode[];\n};\n\n/**\n * Web Audio implementation of {@link AudioSink} — decodes and caches source\n * buffers, then schedules/plays {@link AudioRequest}s as `AudioBufferSourceNode`s\n * through a shared master gain (global mute and volume). `schedule` registers the\n * active set of requests for the current frame; `syncTo` then starts/stops\n * sources to match `clipTime`, so playback follows scrubbing and seeks.\n */\nexport class WebAudioSink extends AudioSink {\n private readonly borrowed: AudioContext | undefined;\n private _context: AudioContext | null = null;\n private bufferCache = new Map<string, AudioBuffer>();\n private decoding = new Map<string, Promise<void>>();\n private active = new Map<string, ActiveSource>();\n private scheduled: AudioRequest[] = [];\n private disposed: boolean = false;\n /** The master gain and the live bus chains (shared across every clip that names one), built with the context. */\n private _graph: { master: GainNode; buses: AudioBusGraph } | null = null;\n private muted = false;\n private volume = 1;\n\n constructor(context?: AudioContext) {\n super();\n this.borrowed = context;\n }\n\n /**\n * The context, created on first use rather than with the sink: a new\n * AudioContext opens the audio device (50–65 ms on a desktop), and a\n * session that never plays or decodes sound never needs one.\n */\n getContext(): AudioContext {\n this._context ??= this.borrowed ?? new AudioContext();\n return this._context;\n }\n\n private get context(): AudioContext {\n return this.getContext();\n }\n\n private get graph(): { master: GainNode; buses: AudioBusGraph } {\n if (!this._graph) {\n const master = this.context.createGain();\n master.connect(this.context.destination);\n this._graph = { master, buses: new AudioBusGraph(this.context, master) };\n this.applyMasterGain();\n }\n return this._graph;\n }\n\n /**\n * Resume the AudioContext. Must be called from within a user gesture\n * (e.g. a click handler) the first time, or browsers keep the context\n * suspended and no audio is heard.\n */\n unlock(): void {\n if (this.context.state === \"suspended\") {\n this.context.resume();\n }\n }\n\n has(src: string): boolean {\n return this.bufferCache.has(src);\n }\n\n async append(src: string, data: ArrayBuffer): Promise<void> {\n if (this.bufferCache.has(src)) return;\n const existing = this.decoding.get(src);\n if (existing) return existing;\n\n // decodeAudioData detaches `data` — that's fine, the caller in\n // AssetManager.fetchAudio discards its reference immediately after\n // awaiting append. Skipping the defensive .slice(0) saves a multi-MB\n // allocation per audio source.\n const job = this.context.decodeAudioData(data).then((buffer) => {\n this.bufferCache.set(src, buffer);\n }).finally(() => {\n this.decoding.delete(src);\n });\n this.decoding.set(src, job);\n await job;\n }\n\n /** Drops cached buffers and stops active sources for srcs/requests no longer referenced — called when the asset set changes (e.g. seeking across scene boundaries). */\n retain(keep: ReadonlySet<string>): void {\n for (const src of [...this.bufferCache.keys()]) {\n if (!keep.has(src)) this.bufferCache.delete(src);\n }\n for (const [id, active] of this.active) {\n if (!keep.has(active.request.src)) this.stopSource(id, active);\n }\n }\n\n /** Replaces the active request set; stops any currently-playing source whose request is no longer scheduled. Actual start/stop-to-match-time happens in `syncTo`. */\n schedule(requests: readonly AudioRequest[]): void {\n const next = new Set(requests.map(r => r.id));\n for (const [id, active] of this.active) {\n if (!next.has(id)) this.stopSource(id, active);\n }\n this.scheduled = requests.slice();\n }\n\n /** Starts sources whose `[startAt, endAt)` window now contains `clipTime` (computing the correct buffer offset for the seek point) and stops any that have fallen outside it. */\n syncTo(clipTime: number): void {\n const liveIds = new Set<string>();\n\n for (const req of this.scheduled) {\n // `endAt` is `Infinity` for an open clip, so the comparison needs no\n // nullish fallback — see AudioRequest.endAt.\n const isInWindow = clipTime >= req.startAt && clipTime < req.endAt;\n if (!isInWindow) continue;\n\n liveIds.add(req.id);\n if (!this.active.has(req.id)) {\n const buffer = this.bufferCache.get(req.src);\n if (buffer) this.playBuffer(buffer, req, clipTime);\n }\n }\n\n for (const [id, active] of this.active) {\n if (!liveIds.has(id)) this.stopSource(id, active);\n }\n }\n\n async play(time: number, _speed: number, _reverse: boolean): Promise<void> {\n if (this.context.state === \"suspended\") {\n await this.context.resume();\n }\n this.syncTo(time);\n }\n\n stop(): void {\n for (const [id, active] of this.active) {\n this.stopSource(id, active);\n }\n // A bus's curves are scheduled against the clock of the run that built\n // it, so the next play, from wherever it lands, builds them again.\n this._graph?.buses.dispose();\n }\n\n setMuted(muted: boolean): void {\n this.muted = muted;\n this.applyMasterGain();\n }\n\n setVolume(volume: number): void {\n this.volume = Math.max(0, volume);\n this.applyMasterGain();\n }\n\n /** Mute and volume are held separately so unmuting restores the set level. */\n private applyMasterGain(): void {\n if (this._graph) this._graph.master.gain.value = this.muted ? 0 : this.volume;\n }\n\n /** Starts a buffer source mid-clip if `clipTime` lands after the request's start — the offset accounts for the request's trim, the elapsed time since `startAt`, and its speed. */\n private playBuffer(buffer: AudioBuffer, req: AudioRequest, clipTime: number): void {\n const startTime = this.context.currentTime;\n const playback = playbackOf(req, buffer.duration);\n const elapsed = Math.max(0, clipTime - req.startAt);\n\n const source = this.context.createBufferSource();\n source.buffer = buffer;\n const loopRegion = applyLoop(source, req);\n if (playback.rate !== 1) source.playbackRate.value = playback.rate;\n applySpeedToPlaybackRate(\n source.playbackRate, playback.speedRamp, playback.sourceLength, playback.clipDuration, startTime, elapsed,\n );\n\n const graph = buildAudioEffectGraph(\n this.context, source, req.effects ?? [], startTime, playback.clipDuration, elapsed,\n );\n const level = buildLevel(this.context, graph.output, req, playback, startTime, elapsed);\n // Into this clip's bus chain, or straight to the master when it has none.\n // The chain is shared: two clips on one bus reach the same nodes, which is\n // what makes a bus effect one tail rather than one per clip.\n level.output.connect(this.graph.buses.inputFor(req.bus, startTime, clipTime));\n\n const audioOffset = req.trimStart + sourceOffsetOf(playback, elapsed);\n source.start(startTime, wrapIntoRegion(loopRegion, audioOffset));\n for (const osc of graph.oscillators) osc.start(startTime);\n\n source.onended = () => {\n if (this.active.get(req.id)?.source === source) {\n this.active.delete(req.id);\n }\n };\n\n this.active.set(req.id, {\n source,\n request: req,\n nodes: [...graph.nodes, ...level.nodes],\n oscillators: graph.oscillators,\n });\n }\n\n private stopSource(id: string, active: ActiveSource): void {\n try {\n active.source.onended = null;\n active.source.stop();\n for (const osc of active.oscillators) osc.stop();\n active.source.disconnect();\n for (const node of active.nodes) node.disconnect();\n } catch {\n // already stopped\n }\n this.active.delete(id);\n }\n\n dispose(): void {\n if (this.disposed) return;\n this.disposed = true;\n super.dispose();\n this.bufferCache.clear();\n this.decoding.clear();\n this.scheduled.length = 0;\n this._graph?.buses.dispose();\n try {\n this._graph?.master.disconnect();\n } catch {\n // already disconnected\n }\n const owned = this.borrowed ? null : this._context;\n if (owned && owned.state !== \"closed\") {\n owned.close();\n }\n }\n}\n"]}