@motionscript/browser 0.0.0-stage → 0.1.0-alpha.0

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 (120) hide show
  1. package/CHANGELOG.md +5 -0
  2. package/LICENSE +201 -0
  3. package/README.md +68 -3
  4. package/dist/asset-bytes.d.ts +50 -0
  5. package/dist/asset-bytes.d.ts.map +1 -0
  6. package/dist/asset-bytes.js +60 -0
  7. package/dist/asset-bytes.js.map +1 -0
  8. package/dist/audio/bus-graph.d.ts +43 -0
  9. package/dist/audio/bus-graph.d.ts.map +1 -0
  10. package/dist/audio/bus-graph.js +104 -0
  11. package/dist/audio/bus-graph.js.map +1 -0
  12. package/dist/audio/filter-graph.d.ts +58 -0
  13. package/dist/audio/filter-graph.d.ts.map +1 -0
  14. package/dist/audio/filter-graph.js +235 -0
  15. package/dist/audio/filter-graph.js.map +1 -0
  16. package/dist/audio/index.d.ts +5 -0
  17. package/dist/audio/index.d.ts.map +1 -0
  18. package/dist/audio/index.js +12 -0
  19. package/dist/audio/index.js.map +1 -0
  20. package/dist/audio/mixer.d.ts +59 -0
  21. package/dist/audio/mixer.d.ts.map +1 -0
  22. package/dist/audio/mixer.js +193 -0
  23. package/dist/audio/mixer.js.map +1 -0
  24. package/dist/audio/player.d.ts +49 -0
  25. package/dist/audio/player.d.ts.map +1 -0
  26. package/dist/audio/player.js +226 -0
  27. package/dist/audio/player.js.map +1 -0
  28. package/dist/browser/audio.js +2 -0
  29. package/dist/browser/audio.js.map +7 -0
  30. package/dist/browser/chunks/GLTFLoader-PGXD4V5B.js +2 -0
  31. package/dist/browser/chunks/GLTFLoader-PGXD4V5B.js.map +7 -0
  32. package/dist/browser/chunks/SkeletonUtils-XYDMTGTM.js +2 -0
  33. package/dist/browser/chunks/SkeletonUtils-XYDMTGTM.js.map +7 -0
  34. package/dist/browser/chunks/chunk-CTLBXZSU.js +4119 -0
  35. package/dist/browser/chunks/chunk-CTLBXZSU.js.map +7 -0
  36. package/dist/browser/chunks/chunk-FQ32PDIU.js +2 -0
  37. package/dist/browser/chunks/chunk-FQ32PDIU.js.map +7 -0
  38. package/dist/browser/chunks/chunk-FY3BTKY4.js +2 -0
  39. package/dist/browser/chunks/chunk-FY3BTKY4.js.map +7 -0
  40. package/dist/browser/chunks/chunk-NSFLRI4P.js +2 -0
  41. package/dist/browser/chunks/chunk-NSFLRI4P.js.map +7 -0
  42. package/dist/browser/chunks/three.module-Z4EOVUWF.js +2 -0
  43. package/dist/browser/chunks/three.module-Z4EOVUWF.js.map +7 -0
  44. package/dist/browser/esbuild.wasm +0 -0
  45. package/dist/browser/index.js +2172 -0
  46. package/dist/browser/index.js.map +7 -0
  47. package/dist/browser/manifest.json +15 -0
  48. package/dist/concat.d.ts +95 -0
  49. package/dist/concat.d.ts.map +1 -0
  50. package/dist/concat.js +174 -0
  51. package/dist/concat.js.map +1 -0
  52. package/dist/decode/image-levels.d.ts +137 -0
  53. package/dist/decode/image-levels.d.ts.map +1 -0
  54. package/dist/decode/image-levels.js +477 -0
  55. package/dist/decode/image-levels.js.map +1 -0
  56. package/dist/decode/levels.d.ts +37 -0
  57. package/dist/decode/levels.d.ts.map +1 -0
  58. package/dist/decode/levels.js +68 -0
  59. package/dist/decode/levels.js.map +1 -0
  60. package/dist/decode/video-levels.d.ts +37 -0
  61. package/dist/decode/video-levels.d.ts.map +1 -0
  62. package/dist/decode/video-levels.js +95 -0
  63. package/dist/decode/video-levels.js.map +1 -0
  64. package/dist/dynamic-nodes.d.ts +9 -0
  65. package/dist/dynamic-nodes.d.ts.map +1 -0
  66. package/dist/dynamic-nodes.js +56 -0
  67. package/dist/dynamic-nodes.js.map +1 -0
  68. package/dist/engine.d.ts +59 -0
  69. package/dist/engine.d.ts.map +1 -0
  70. package/dist/engine.js +171 -0
  71. package/dist/engine.js.map +1 -0
  72. package/dist/exporter.d.ts +163 -0
  73. package/dist/exporter.d.ts.map +1 -0
  74. package/dist/exporter.js +263 -0
  75. package/dist/exporter.js.map +1 -0
  76. package/dist/getter.d.ts +10 -0
  77. package/dist/getter.d.ts.map +1 -0
  78. package/dist/getter.js +38 -0
  79. package/dist/getter.js.map +1 -0
  80. package/dist/index.d.ts +22 -0
  81. package/dist/index.d.ts.map +1 -0
  82. package/dist/index.js +72 -0
  83. package/dist/index.js.map +1 -0
  84. package/dist/measure-context.d.ts +12 -0
  85. package/dist/measure-context.d.ts.map +1 -0
  86. package/dist/measure-context.js +12 -0
  87. package/dist/measure-context.js.map +1 -0
  88. package/dist/mix-timeline-audio.d.ts +42 -0
  89. package/dist/mix-timeline-audio.d.ts.map +1 -0
  90. package/dist/mix-timeline-audio.js +63 -0
  91. package/dist/mix-timeline-audio.js.map +1 -0
  92. package/dist/output.d.ts +7 -0
  93. package/dist/output.d.ts.map +1 -0
  94. package/dist/output.js +6 -0
  95. package/dist/output.js.map +1 -0
  96. package/dist/render-context.d.ts +30 -0
  97. package/dist/render-context.d.ts.map +1 -0
  98. package/dist/render-context.js +55 -0
  99. package/dist/render-context.js.map +1 -0
  100. package/dist/screenshot.d.ts +45 -0
  101. package/dist/screenshot.d.ts.map +1 -0
  102. package/dist/screenshot.js +32 -0
  103. package/dist/screenshot.js.map +1 -0
  104. package/dist/still.d.ts +151 -0
  105. package/dist/still.d.ts.map +1 -0
  106. package/dist/still.js +222 -0
  107. package/dist/still.js.map +1 -0
  108. package/dist/storage-adapter.d.ts +367 -0
  109. package/dist/storage-adapter.d.ts.map +1 -0
  110. package/dist/storage-adapter.js +1278 -0
  111. package/dist/storage-adapter.js.map +1 -0
  112. package/dist/three/renderer.d.ts +48 -0
  113. package/dist/three/renderer.d.ts.map +1 -0
  114. package/dist/three/renderer.js +177 -0
  115. package/dist/three/renderer.js.map +1 -0
  116. package/dist/ticker.d.ts +42 -0
  117. package/dist/ticker.d.ts.map +1 -0
  118. package/dist/ticker.js +143 -0
  119. package/dist/ticker.js.map +1 -0
  120. package/package.json +74 -3
@@ -0,0 +1,235 @@
1
+ import { isCurve, sourceTimeAtSceneElapsed } from "@motionscript/core";
2
+ /**
3
+ * How many sample points per second to bake an eased curve segment into for
4
+ * `setValueCurveAtTime`. 200/s (≈ every 5ms) is smooth to the ear and cheap.
5
+ */
6
+ const CURVE_SAMPLES_PER_SECOND = 200;
7
+ /**
8
+ * Apply a (possibly time-varying) {@link Param} to a Web Audio {@link AudioParam}.
9
+ *
10
+ * - **number** → set once (today's behavior; zero scheduling overhead).
11
+ * - **Curve** → resolve against `clipDuration` and schedule each segment:
12
+ * - linear segments via `setValueAtTime` + `linearRampToValueAtTime`,
13
+ * - eased segments baked into a `Float32Array` via `setValueCurveAtTime`.
14
+ *
15
+ * `elapsed` (scene-seconds already played when a mid-clip seek starts the source)
16
+ * shifts the schedule back by `elapsed` so "now" reads the curve's value at the
17
+ * clip's *current* position, not its start. A `clamp(v)` hook lets a param keep a
18
+ * valid range (e.g. echo mix 0–1) at every sampled point.
19
+ */
20
+ function applyParam(ctx, audioParam, param, startTime, clipDuration, elapsed, clamp = (v) => v) {
21
+ // Coerce to a finite value before touching an AudioParam — the platform throws
22
+ // on NaN/Infinity. Falls back to the current param value (never undefined here).
23
+ const finite = (v) => {
24
+ const c = clamp(v);
25
+ return Number.isFinite(c) ? c : audioParam.value;
26
+ };
27
+ if (!isCurve(param)) {
28
+ audioParam.value = finite(param);
29
+ return;
30
+ }
31
+ // A non-finite clip length yields no usable schedule; hold the curve's start value.
32
+ if (!Number.isFinite(clipDuration) || clipDuration <= 0) {
33
+ audioParam.value = finite(param.staticValue(audioParam.value));
34
+ return;
35
+ }
36
+ const base = startTime - elapsed; // map curve t=0 to here so seeks line up
37
+ const resolved = param.resolve(clipDuration, audioParam.value);
38
+ if (resolved.length === 0)
39
+ return;
40
+ // Anchor the starting value at `startTime` (the seek point) so there is no
41
+ // jump from the param's default before the first scheduled segment runs.
42
+ const startValue = param.sampleAt(Math.max(0, elapsed), clipDuration, audioParam.value);
43
+ audioParam.setValueAtTime(finite(startValue), startTime);
44
+ for (const seg of resolved) {
45
+ const segStart = base + seg.startTime;
46
+ const segEnd = base + seg.endTime;
47
+ // Skip segments wholly in the past relative to the seek point, or any with a
48
+ // non-finite boundary (degenerate resolve) that would throw on schedule.
49
+ if (segEnd <= startTime || !Number.isFinite(segStart) || !Number.isFinite(segEnd))
50
+ continue;
51
+ const span = seg.endTime - seg.startTime;
52
+ if (span <= 0) {
53
+ audioParam.setValueAtTime(finite(seg.to), Math.max(segStart, startTime));
54
+ continue;
55
+ }
56
+ if (seg.isLinear) {
57
+ audioParam.setValueAtTime(finite(seg.from), Math.max(segStart, startTime));
58
+ audioParam.linearRampToValueAtTime(finite(seg.to), segEnd);
59
+ }
60
+ else {
61
+ const count = Math.max(2, Math.ceil(span * CURVE_SAMPLES_PER_SECOND));
62
+ const samples = new Float32Array(count);
63
+ for (let i = 0; i < count; i++) {
64
+ const local = i / (count - 1);
65
+ samples[i] = finite(seg.from + (seg.to - seg.from) * seg.ease(local));
66
+ }
67
+ // setValueCurveAtTime can't start in the past; clamp the start forward.
68
+ const curveStart = Math.max(segStart, startTime);
69
+ const curveDur = segEnd - curveStart;
70
+ if (curveDur > 0)
71
+ audioParam.setValueCurveAtTime(samples, curveStart, curveDur);
72
+ }
73
+ }
74
+ }
75
+ /**
76
+ * Builds the Web Audio graph for a clip's filter chain, returning the node to
77
+ * connect downstream. Shared by live playback ({@link WebAudioSink}) and
78
+ * offline export (`mixAudio`) so both paths sound identical.
79
+ *
80
+ * Filters apply in array order (index 0 closest to the source). `speed` filters
81
+ * are intentionally ignored here — they're realized via `source.playbackRate`
82
+ * by the caller (and affect timeline scheduling), not as a graph node.
83
+ *
84
+ * `startTime` is the AudioContext time the source starts; `clipDuration` is the
85
+ * clip's scene-time length; `elapsed` is scene-seconds already played at a mid-clip
86
+ * seek (0 for a clean start). These let curve-valued params schedule and align.
87
+ *
88
+ * Pass the already-created `source` so its `playbackRate` can be set by the
89
+ * caller; this function only wires nodes *after* the source.
90
+ */
91
+ export function buildAudioFilterGraph(ctx, source, filters, startTime = ctx.currentTime, clipDuration = 0, elapsed = 0) {
92
+ const nodes = [];
93
+ const oscillators = [];
94
+ let cursor = source;
95
+ const series = (node) => {
96
+ cursor.connect(node);
97
+ nodes.push(node);
98
+ cursor = node;
99
+ };
100
+ const apply = (p, v, clamp) => applyParam(ctx, p, v, startTime, clipDuration, elapsed, clamp);
101
+ for (const filter of filters) {
102
+ switch (filter.type) {
103
+ case "gain": {
104
+ const gain = ctx.createGain();
105
+ apply(gain.gain, filter.value);
106
+ series(gain);
107
+ break;
108
+ }
109
+ case "highpass":
110
+ case "lowpass": {
111
+ const biquad = ctx.createBiquadFilter();
112
+ biquad.type = filter.type === "highpass" ? "highpass" : "lowpass";
113
+ apply(biquad.frequency, filter.frequency);
114
+ if (filter.q !== undefined)
115
+ apply(biquad.Q, filter.q);
116
+ series(biquad);
117
+ break;
118
+ }
119
+ case "tremolo": {
120
+ // Carrier gain whose value is modulated by an LFO. With depth d the
121
+ // carrier base = (1 - d/2) and the LFO swing gain = d/2. The clamp
122
+ // hook applies these transforms whether depth is scalar or a curve.
123
+ const carrier = ctx.createGain();
124
+ const lfo = ctx.createOscillator();
125
+ const lfoDepth = ctx.createGain();
126
+ apply(carrier.gain, filter.depth, (d) => 1 - clamp01(d) / 2);
127
+ apply(lfoDepth.gain, filter.depth, (d) => clamp01(d) / 2);
128
+ apply(lfo.frequency, filter.rate);
129
+ lfo.connect(lfoDepth);
130
+ lfoDepth.connect(carrier.gain);
131
+ nodes.push(lfoDepth);
132
+ oscillators.push(lfo);
133
+ series(carrier);
134
+ break;
135
+ }
136
+ case "echo": {
137
+ // Dry + wet split. The wet path is a delay with feedback; both sum
138
+ // into a merge node that becomes the new cursor.
139
+ const split = cursor;
140
+ const merge = ctx.createGain();
141
+ const dry = ctx.createGain();
142
+ apply(dry.gain, filter.mix ?? 0.5, (m) => 1 - clamp01(m));
143
+ split.connect(dry);
144
+ dry.connect(merge);
145
+ // Delay buffer must accommodate the max delay; size from the static value.
146
+ const maxDelay = staticOf(filter.delay);
147
+ const delay = ctx.createDelay(Math.max(1, maxDelay + 1));
148
+ apply(delay.delayTime, filter.delay);
149
+ const feedbackGain = ctx.createGain();
150
+ apply(feedbackGain.gain, filter.feedback, (f) => Math.max(0, Math.min(0.99, f)));
151
+ const wet = ctx.createGain();
152
+ apply(wet.gain, filter.mix ?? 0.5, (m) => clamp01(m));
153
+ split.connect(delay);
154
+ delay.connect(feedbackGain);
155
+ feedbackGain.connect(delay); // feedback loop
156
+ delay.connect(wet);
157
+ wet.connect(merge);
158
+ nodes.push(dry, delay, feedbackGain, wet, merge);
159
+ cursor = merge;
160
+ break;
161
+ }
162
+ case "speed":
163
+ // Realized via source.playbackRate by the caller; no graph node.
164
+ break;
165
+ }
166
+ }
167
+ return { output: cursor, nodes, oscillators };
168
+ }
169
+ /** Clamp to [0, 1]. */
170
+ function clamp01(v) {
171
+ return Math.max(0, Math.min(1, v));
172
+ }
173
+ /** The static/representative number of a param (number itself, or curve's t=0 value). */
174
+ function staticOf(p) {
175
+ return isCurve(p) ? p.staticValue(0) : p;
176
+ }
177
+ /**
178
+ * Net *constant* playback-rate multiplier from any scalar `speed` filters in the
179
+ * chain (product of all), defaulting to 1. Curve-valued speeds contribute 1 here
180
+ * and are scheduled separately by {@link applySpeedToPlaybackRate}. Mirrors
181
+ * `Sound.effectiveSpeed()` so the renderer applies the same rate the core timeline
182
+ * reserved time for.
183
+ */
184
+ export function effectiveSpeed(filters) {
185
+ if (!filters)
186
+ return 1;
187
+ let speed = 1;
188
+ for (const f of filters) {
189
+ if (f.type === "speed" && !isCurve(f.value) && f.value > 0)
190
+ speed *= f.value;
191
+ }
192
+ return speed;
193
+ }
194
+ /** The first curve-valued `speed` filter in the chain, if any. */
195
+ export function speedCurveOf(filters) {
196
+ if (!filters)
197
+ return undefined;
198
+ for (const f of filters) {
199
+ if (f.type === "speed" && isCurve(f.value))
200
+ return f.value;
201
+ }
202
+ return undefined;
203
+ }
204
+ /**
205
+ * Schedule a time-varying speed curve onto an `AudioBufferSourceNode.playbackRate`.
206
+ *
207
+ * The curve is authored over **source time** (rate as a function of how far through
208
+ * the source we've played). `setValueAtTime`/`linearRampToValueAtTime` schedule in
209
+ * **context time**, so we walk scene-time forward in small steps, convert each to a
210
+ * source-time offset via the integral inverse, read the curve there, and lay down a
211
+ * value point at the matching context time. `scalar` folds in any constant speed
212
+ * filters (they scale the whole rate uniformly).
213
+ *
214
+ * `elapsed` is scene-seconds already played at a mid-clip seek, so the schedule
215
+ * starts at the seek point and aligns the playbackRate to the clip's true position.
216
+ */
217
+ export function applySpeedToPlaybackRate(rate, curve, sourceLength, sceneDuration, scalar, startTime, elapsed) {
218
+ if (!curve)
219
+ return;
220
+ const step = 1 / CURVE_SAMPLES_PER_SECOND; // scene-time sampling step
221
+ let scene = Math.max(0, elapsed);
222
+ // Seed the value exactly at the seek point.
223
+ const seedSrc = sourceTimeAtSceneElapsed(curve, sourceLength, scene);
224
+ rate.setValueAtTime(Math.max(1e-4, curve.sampleAt(seedSrc, sourceLength, 1) * scalar), startTime);
225
+ for (let s = scene + step; s <= sceneDuration + step; s += step) {
226
+ const clampedScene = Math.min(s, sceneDuration);
227
+ const srcT = sourceTimeAtSceneElapsed(curve, sourceLength, clampedScene);
228
+ const value = Math.max(1e-4, curve.sampleAt(srcT, sourceLength, 1) * scalar);
229
+ const ctxTime = startTime + (clampedScene - scene);
230
+ rate.linearRampToValueAtTime(value, ctxTime);
231
+ if (clampedScene >= sceneDuration)
232
+ break;
233
+ }
234
+ }
235
+ //# sourceMappingURL=filter-graph.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"filter-graph.js","sourceRoot":"","sources":["../../src/audio/filter-graph.ts"],"names":[],"mappings":"AAAA,OAAO,EAA0B,OAAO,EAAE,wBAAwB,EAAE,MAAM,oBAAoB,CAAC;AAkB/F;;;GAGG;AACH,MAAM,wBAAwB,GAAG,GAAG,CAAC;AAErC;;;;;;;;;;;;GAYG;AACH,SAAS,UAAU,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GAAG,CAAC,IAAI,EAAE,KAAK,CAAC,QAAQ,CAAC,OAAO,EAAE,YAAY,EAAE,CAAC,CAAC,GAAG,MAAM,CAAC,EAAE,SAAS,CAAC,CAAC;IAElG,KAAK,IAAI,CAAC,GAAG,KAAK,GAAG,IAAI,EAAE,CAAC,IAAI,aAAa,GAAG,IAAI,EAAE,CAAC,IAAI,IAAI,EAAE,CAAC;QAC9D,MAAM,YAAY,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,aAAa,CAAC,CAAC;QAChD,MAAM,IAAI,GAAG,wBAAwB,CAAC,KAAK,EAAE,YAAY,EAAE,YAAY,CAAC,CAAC;QACzE,MAAM,KAAK,GAAG,IAAI,CAAC,GAAG,CAAC,IAAI,EAAE,KAAK,CAAC,QAAQ,CAAC,IAAI,EAAE,YAAY,EAAE,CAAC,CAAC,GAAG,MAAM,CAAC,CAAC;QAC7E,MAAM,OAAO,GAAG,SAAS,GAAG,CAAC,YAAY,GAAG,KAAK,CAAC,CAAC;QACnD,IAAI,CAAC,uBAAuB,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;QAC7C,IAAI,YAAY,IAAI,aAAa;YAAE,MAAM;IAC7C,CAAC;AACL,CAAC","sourcesContent":["import { AudioFilterItem, Param, isCurve, sourceTimeAtSceneElapsed } from \"@motionscript/core\";\n\n/**\n * Result of building an audio-filter graph for one source.\n *\n * - `output` is the last node in the chain; connect it downstream (e.g. to the\n * per-request gain node).\n * - `nodes` are every intermediate node, so callers can `disconnect()` them on\n * teardown.\n * - `oscillators` are LFOs (from `tremolo`) the caller must `start()` — and may\n * `stop()` — alongside the source, since oscillators need an explicit start.\n */\nexport interface AudioFilterGraph {\n output: AudioNode;\n nodes: AudioNode[];\n oscillators: OscillatorNode[];\n}\n\n/**\n * How many sample points per second to bake an eased curve segment into for\n * `setValueCurveAtTime`. 200/s (≈ every 5ms) is smooth to the ear and cheap.\n */\nconst CURVE_SAMPLES_PER_SECOND = 200;\n\n/**\n * Apply a (possibly time-varying) {@link Param} to a Web Audio {@link AudioParam}.\n *\n * - **number** → set once (today's behavior; zero scheduling overhead).\n * - **Curve** → resolve against `clipDuration` and schedule each segment:\n * - linear segments via `setValueAtTime` + `linearRampToValueAtTime`,\n * - eased segments baked into a `Float32Array` via `setValueCurveAtTime`.\n *\n * `elapsed` (scene-seconds already played when a mid-clip seek starts the source)\n * shifts the schedule back by `elapsed` so \"now\" reads the curve's value at the\n * clip's *current* position, not its start. A `clamp(v)` hook lets a param keep a\n * valid range (e.g. echo mix 0–1) at every sampled point.\n */\nfunction applyParam(\n ctx: BaseAudioContext,\n audioParam: AudioParam,\n param: Param,\n startTime: number,\n clipDuration: number,\n elapsed: number,\n clamp: (v: number) => number = (v) => v,\n): void {\n // Coerce to a finite value before touching an AudioParam — the platform throws\n // on NaN/Infinity. Falls back to the current param value (never undefined here).\n const finite = (v: number): number => {\n const c = clamp(v);\n return Number.isFinite(c) ? c : audioParam.value;\n };\n\n if (!isCurve(param)) {\n audioParam.value = finite(param);\n return;\n }\n\n // A non-finite clip length yields no usable schedule; hold the curve's start value.\n if (!Number.isFinite(clipDuration) || clipDuration <= 0) {\n audioParam.value = finite(param.staticValue(audioParam.value));\n return;\n }\n\n const base = startTime - elapsed; // map curve t=0 to here so seeks line up\n const resolved = param.resolve(clipDuration, audioParam.value);\n if (resolved.length === 0) return;\n\n // Anchor the starting value at `startTime` (the seek point) so there is no\n // jump from the param's default before the first scheduled segment runs.\n const startValue = param.sampleAt(Math.max(0, elapsed), clipDuration, audioParam.value);\n audioParam.setValueAtTime(finite(startValue), startTime);\n\n for (const seg of resolved) {\n const segStart = base + seg.startTime;\n const segEnd = base + seg.endTime;\n // Skip segments wholly in the past relative to the seek point, or any with a\n // non-finite boundary (degenerate resolve) that would throw on schedule.\n if (segEnd <= startTime || !Number.isFinite(segStart) || !Number.isFinite(segEnd)) continue;\n\n const span = seg.endTime - seg.startTime;\n\n if (span <= 0) {\n audioParam.setValueAtTime(finite(seg.to), Math.max(segStart, startTime));\n continue;\n }\n\n if (seg.isLinear) {\n audioParam.setValueAtTime(finite(seg.from), Math.max(segStart, startTime));\n audioParam.linearRampToValueAtTime(finite(seg.to), segEnd);\n } else {\n const count = Math.max(2, Math.ceil(span * CURVE_SAMPLES_PER_SECOND));\n const samples = new Float32Array(count);\n for (let i = 0; i < count; i++) {\n const local = i / (count - 1);\n samples[i] = finite(seg.from + (seg.to - seg.from) * seg.ease(local));\n }\n // setValueCurveAtTime can't start in the past; clamp the start forward.\n const curveStart = Math.max(segStart, startTime);\n const curveDur = segEnd - curveStart;\n if (curveDur > 0) audioParam.setValueCurveAtTime(samples, curveStart, curveDur);\n }\n }\n}\n\n/**\n * Builds the Web Audio graph for a clip's filter chain, returning the node to\n * connect downstream. Shared by live playback ({@link WebAudioSink}) and\n * offline export (`mixAudio`) so both paths sound identical.\n *\n * Filters apply in array order (index 0 closest to the source). `speed` filters\n * are intentionally ignored here — they're realized via `source.playbackRate`\n * by the caller (and affect timeline scheduling), not as a graph node.\n *\n * `startTime` is the AudioContext time the source starts; `clipDuration` is the\n * clip's scene-time length; `elapsed` is scene-seconds already played at a mid-clip\n * seek (0 for a clean start). These let curve-valued params schedule and align.\n *\n * Pass the already-created `source` so its `playbackRate` can be set by the\n * caller; this function only wires nodes *after* the source.\n */\nexport function buildAudioFilterGraph(\n ctx: BaseAudioContext,\n source: AudioNode,\n filters: readonly AudioFilterItem[],\n startTime: number = ctx.currentTime,\n clipDuration: number = 0,\n elapsed: number = 0,\n): AudioFilterGraph {\n const nodes: AudioNode[] = [];\n const oscillators: OscillatorNode[] = [];\n let cursor: AudioNode = source;\n\n const series = (node: AudioNode): void => {\n cursor.connect(node);\n nodes.push(node);\n cursor = node;\n };\n\n const apply = (p: AudioParam, v: Param, clamp?: (n: number) => number): void =>\n applyParam(ctx, p, v, startTime, clipDuration, elapsed, clamp);\n\n for (const filter of filters) {\n switch (filter.type) {\n case \"gain\": {\n const gain = ctx.createGain();\n apply(gain.gain, filter.value);\n series(gain);\n break;\n }\n case \"highpass\":\n case \"lowpass\": {\n const biquad = ctx.createBiquadFilter();\n biquad.type = filter.type === \"highpass\" ? \"highpass\" : \"lowpass\";\n apply(biquad.frequency, filter.frequency);\n if (filter.q !== undefined) apply(biquad.Q, filter.q);\n series(biquad);\n break;\n }\n case \"tremolo\": {\n // Carrier gain whose value is modulated by an LFO. With depth d the\n // carrier base = (1 - d/2) and the LFO swing gain = d/2. The clamp\n // hook applies these transforms whether depth is scalar or a curve.\n const carrier = ctx.createGain();\n const lfo = ctx.createOscillator();\n const lfoDepth = ctx.createGain();\n\n apply(carrier.gain, filter.depth, (d) => 1 - clamp01(d) / 2);\n apply(lfoDepth.gain, filter.depth, (d) => clamp01(d) / 2);\n apply(lfo.frequency, filter.rate);\n\n lfo.connect(lfoDepth);\n lfoDepth.connect(carrier.gain);\n\n nodes.push(lfoDepth);\n oscillators.push(lfo);\n series(carrier);\n break;\n }\n case \"echo\": {\n // Dry + wet split. The wet path is a delay with feedback; both sum\n // into a merge node that becomes the new cursor.\n const split = cursor;\n const merge = ctx.createGain();\n\n const dry = ctx.createGain();\n apply(dry.gain, filter.mix ?? 0.5, (m) => 1 - clamp01(m));\n split.connect(dry);\n dry.connect(merge);\n\n // Delay buffer must accommodate the max delay; size from the static value.\n const maxDelay = staticOf(filter.delay);\n const delay = ctx.createDelay(Math.max(1, maxDelay + 1));\n apply(delay.delayTime, filter.delay);\n const feedbackGain = ctx.createGain();\n apply(feedbackGain.gain, filter.feedback, (f) => Math.max(0, Math.min(0.99, f)));\n const wet = ctx.createGain();\n apply(wet.gain, filter.mix ?? 0.5, (m) => clamp01(m));\n\n split.connect(delay);\n delay.connect(feedbackGain);\n feedbackGain.connect(delay); // feedback loop\n delay.connect(wet);\n wet.connect(merge);\n\n nodes.push(dry, delay, feedbackGain, wet, merge);\n cursor = merge;\n break;\n }\n case \"speed\":\n // Realized via source.playbackRate by the caller; no graph node.\n break;\n }\n }\n\n return { output: cursor, nodes, oscillators };\n}\n\n/** Clamp to [0, 1]. */\nfunction clamp01(v: number): number {\n return Math.max(0, Math.min(1, v));\n}\n\n/** The static/representative number of a param (number itself, or curve's t=0 value). */\nfunction staticOf(p: Param): number {\n return isCurve(p) ? p.staticValue(0) : p;\n}\n\n/**\n * Net *constant* playback-rate multiplier from any scalar `speed` filters in the\n * chain (product of all), defaulting to 1. Curve-valued speeds contribute 1 here\n * and are scheduled separately by {@link applySpeedToPlaybackRate}. Mirrors\n * `Sound.effectiveSpeed()` so the renderer applies the same rate the core timeline\n * reserved time for.\n */\nexport function effectiveSpeed(filters: readonly AudioFilterItem[] | undefined): number {\n if (!filters) return 1;\n let speed = 1;\n for (const f of filters) {\n if (f.type === \"speed\" && !isCurve(f.value) && f.value > 0) speed *= f.value;\n }\n return speed;\n}\n\n/** The first curve-valued `speed` filter in the chain, if any. */\nexport function speedCurveOf(filters: readonly AudioFilterItem[] | undefined) {\n if (!filters) return undefined;\n for (const f of filters) {\n if (f.type === \"speed\" && isCurve(f.value)) return f.value;\n }\n return undefined;\n}\n\n/**\n * Schedule a time-varying speed curve onto an `AudioBufferSourceNode.playbackRate`.\n *\n * The curve is authored over **source time** (rate as a function of how far through\n * the source we've played). `setValueAtTime`/`linearRampToValueAtTime` schedule in\n * **context time**, so we walk scene-time forward in small steps, convert each to a\n * source-time offset via the integral inverse, read the curve there, and lay down a\n * value point at the matching context time. `scalar` folds in any constant speed\n * filters (they scale the whole rate uniformly).\n *\n * `elapsed` is scene-seconds already played at a mid-clip seek, so the schedule\n * starts at the seek point and aligns the playbackRate to the clip's true position.\n */\nexport function applySpeedToPlaybackRate(\n rate: AudioParam,\n curve: ReturnType<typeof speedCurveOf>,\n sourceLength: number,\n sceneDuration: number,\n scalar: number,\n startTime: number,\n elapsed: number,\n): void {\n if (!curve) return;\n const step = 1 / CURVE_SAMPLES_PER_SECOND; // scene-time sampling step\n let scene = Math.max(0, elapsed);\n // Seed the value exactly at the seek point.\n const seedSrc = sourceTimeAtSceneElapsed(curve, sourceLength, scene);\n rate.setValueAtTime(Math.max(1e-4, curve.sampleAt(seedSrc, sourceLength, 1) * scalar), startTime);\n\n for (let s = scene + step; s <= sceneDuration + step; s += step) {\n const clampedScene = Math.min(s, sceneDuration);\n const srcT = sourceTimeAtSceneElapsed(curve, sourceLength, clampedScene);\n const value = Math.max(1e-4, curve.sampleAt(srcT, sourceLength, 1) * scalar);\n const ctxTime = startTime + (clampedScene - scene);\n rate.linearRampToValueAtTime(value, ctxTime);\n if (clampedScene >= sceneDuration) break;\n }\n}\n"]}
@@ -0,0 +1,5 @@
1
+ export { mixAudio, encodeWav, WebAudioMixer, DEFAULT_SAMPLE_RATE } from "./mixer";
2
+ export type { MixAudioOptions } from "./mixer";
3
+ export { WebAudioSink, WebAudioSink as WebAudioPlayer } from "./player";
4
+ export { AudioBusGraph } from "./bus-graph";
5
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/audio/index.ts"],"names":[],"mappings":"AAOA,OAAO,EAAE,QAAQ,EAAE,SAAS,EAAE,aAAa,EAAE,mBAAmB,EAAE,MAAM,SAAS,CAAC;AAClF,YAAY,EAAE,eAAe,EAAE,MAAM,SAAS,CAAC;AAE/C,OAAO,EAAE,YAAY,EAAE,YAAY,IAAI,cAAc,EAAE,MAAM,UAAU,CAAC;AAIxE,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC"}
@@ -0,0 +1,12 @@
1
+ // @motionscript/browser/audio — stacked-audio playback and mixdown, with no
2
+ // renderer attached.
3
+ //
4
+ // A separate entry point from the package barrel on purpose: that barrel loads
5
+ // the CanvasKit/Skia backend at module scope, and nothing here draws anything.
6
+ // A host building an audio timeline should not pay for the WASM module.
7
+ export { mixAudio, encodeWav, WebAudioMixer, DEFAULT_SAMPLE_RATE } from "./mixer";
8
+ export { WebAudioSink, WebAudioSink as WebAudioPlayer } from "./player";
9
+ // The bus layer both of those share. Exported so a host writing its own device
10
+ // or mixer builds the same chains, rather than a second set that drifts.
11
+ export { AudioBusGraph } from "./bus-graph";
12
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/audio/index.ts"],"names":[],"mappings":"AAAA,4EAA4E;AAC5E,qBAAqB;AACrB,EAAE;AACF,+EAA+E;AAC/E,+EAA+E;AAC/E,wEAAwE;AAExE,OAAO,EAAE,QAAQ,EAAE,SAAS,EAAE,aAAa,EAAE,mBAAmB,EAAE,MAAM,SAAS,CAAC;AAGlF,OAAO,EAAE,YAAY,EAAE,YAAY,IAAI,cAAc,EAAE,MAAM,UAAU,CAAC;AAExE,+EAA+E;AAC/E,yEAAyE;AACzE,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC","sourcesContent":["// @motionscript/browser/audio — stacked-audio playback and mixdown, with no\n// renderer attached.\n//\n// A separate entry point from the package barrel on purpose: that barrel loads\n// the CanvasKit/Skia backend at module scope, and nothing here draws anything.\n// A host building an audio timeline should not pay for the WASM module.\n\nexport { mixAudio, encodeWav, WebAudioMixer, DEFAULT_SAMPLE_RATE } from \"./mixer\";\nexport type { MixAudioOptions } from \"./mixer\";\n\nexport { WebAudioSink, WebAudioSink as WebAudioPlayer } from \"./player\";\n\n// The bus layer both of those share. Exported so a host writing its own device\n// or mixer builds the same chains, rather than a second set that drifts.\nexport { AudioBusGraph } from \"./bus-graph\";\n"]}
@@ -0,0 +1,59 @@
1
+ import { type AssetCatalog } from "@motionscript/core";
2
+ import type { AudioMixer, ScheduledAudioRequest } from "@motionscript/skia-render/export";
3
+ /** Sample rate used when the caller doesn't pick one. */
4
+ export declare const DEFAULT_SAMPLE_RATE = 44100;
5
+ export interface MixAudioOptions {
6
+ /** Defaults to {@link DEFAULT_SAMPLE_RATE}. */
7
+ sampleRate?: number;
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
11
+ * fetched as written — right only while the host keys its manifest by URL.
12
+ */
13
+ catalog?: AssetCatalog;
14
+ }
15
+ /**
16
+ * Render every scheduled request into one timeline-length buffer.
17
+ *
18
+ * An `OfflineAudioContext` graph — gain per request, looping/trimming, and the
19
+ * same filter chain the live {@link WebAudioSink} builds — rendered in one
20
+ * pass rather than scheduled per frame.
21
+ *
22
+ * Shared by the video exporter and by `mixTimelineAudio`, deliberately: a split
23
+ * export and a whole one have to be the same mix, and two implementations of
24
+ * "stack these clips" would eventually disagree.
25
+ *
26
+ * Curve-valued filter params and `speed` curves schedule here exactly as they do
27
+ * in {@link WebAudioSink.playBuffer}, which is the reference implementation —
28
+ * the two must be read side by side when either changes.
29
+ *
30
+ * One deliberate difference: this schedules at absolute context times, so clip
31
+ * starts are sample-accurate here and quantized to `1/fps` in live playback,
32
+ * which starts sources at `currentTime` on a clock tick.
33
+ */
34
+ export declare function mixAudio(requests: readonly ScheduledAudioRequest[], totalDuration: number, options?: MixAudioOptions): Promise<AudioBuffer | null>;
35
+ /**
36
+ * Adapter presenting {@link mixAudio} as the exporter's injected mixer seam.
37
+ *
38
+ * The sample rate is a constructor argument rather than a third `mix` parameter
39
+ * because {@link AudioMixer} — the seam `renderTimeline` calls through — takes
40
+ * exactly two, so anything passed as a third would be silently dropped. Fixing
41
+ * it here rather than widening the interface keeps the rate a property of *this*
42
+ * mixer, which is whose business it is.
43
+ */
44
+ export declare class WebAudioMixer implements AudioMixer<AudioBuffer> {
45
+ private readonly sampleRate;
46
+ private readonly catalog?;
47
+ constructor(sampleRate?: number, catalog?: AssetCatalog | undefined);
48
+ mix(requests: readonly ScheduledAudioRequest[], totalDuration: number): Promise<AudioBuffer | null>;
49
+ }
50
+ /**
51
+ * Encode an `AudioBuffer` as a 16-bit PCM WAV.
52
+ *
53
+ * The only other encoder in this package is the AAC track inside the mp4 muxer,
54
+ * which is reachable solely by exporting a video — so without this a mixdown has
55
+ * no file representation at all. WAV because it needs no codec support and is
56
+ * lossless; a host wanting mp3/aac can re-encode from here.
57
+ */
58
+ export declare function encodeWav(buffer: AudioBuffer): Uint8Array<ArrayBuffer>;
59
+ //# sourceMappingURL=mixer.d.ts.map
@@ -0,0 +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"}
@@ -0,0 +1,193 @@
1
+ import { sourceTimeAtSceneElapsed } from "@motionscript/core";
2
+ import { AudioBusGraph } from "./bus-graph";
3
+ import { applySpeedToPlaybackRate, buildAudioFilterGraph, effectiveSpeed, speedCurveOf, } from "./filter-graph";
4
+ /** Sample rate used when the caller doesn't pick one. */
5
+ export const DEFAULT_SAMPLE_RATE = 44100;
6
+ /** Audio first, then video: a clip's own soundtrack is requested under the video's key. */
7
+ function audioUrl(src, catalog) {
8
+ if (!catalog)
9
+ return src;
10
+ try {
11
+ return catalog.getAudioMeta(src).src || src;
12
+ }
13
+ catch {
14
+ try {
15
+ return catalog.getVideoMeta(src).src || src;
16
+ }
17
+ catch {
18
+ return src;
19
+ }
20
+ }
21
+ }
22
+ /** Fetches and decodes an audio source against a scratch `OfflineAudioContext` (decoding requires a context but doesn't render through it). */
23
+ async function fetchAudioBuffer(src, ctx) {
24
+ const response = await fetch(src);
25
+ const arrayBuffer = await response.arrayBuffer();
26
+ return ctx.decodeAudioData(arrayBuffer);
27
+ }
28
+ /**
29
+ * Render every scheduled request into one timeline-length buffer.
30
+ *
31
+ * An `OfflineAudioContext` graph — gain per request, looping/trimming, and the
32
+ * same filter chain the live {@link WebAudioSink} builds — rendered in one
33
+ * pass rather than scheduled per frame.
34
+ *
35
+ * Shared by the video exporter and by `mixTimelineAudio`, deliberately: a split
36
+ * export and a whole one have to be the same mix, and two implementations of
37
+ * "stack these clips" would eventually disagree.
38
+ *
39
+ * Curve-valued filter params and `speed` curves schedule here exactly as they do
40
+ * in {@link WebAudioSink.playBuffer}, which is the reference implementation —
41
+ * the two must be read side by side when either changes.
42
+ *
43
+ * One deliberate difference: this schedules at absolute context times, so clip
44
+ * starts are sample-accurate here and quantized to `1/fps` in live playback,
45
+ * which starts sources at `currentTime` on a clock tick.
46
+ */
47
+ export async function mixAudio(requests, totalDuration, options = {}) {
48
+ if (requests.length === 0)
49
+ return null;
50
+ const sampleRate = options.sampleRate ?? DEFAULT_SAMPLE_RATE;
51
+ const frames = Math.ceil(totalDuration * sampleRate);
52
+ if (frames <= 0)
53
+ return null;
54
+ const uniqueSrcs = [...new Set(requests.map(r => r.request.src))];
55
+ const scratchCtx = new OfflineAudioContext(2, frames, sampleRate);
56
+ const decoded = new Map();
57
+ await Promise.all(uniqueSrcs.map(async (src) => {
58
+ decoded.set(src, await fetchAudioBuffer(audioUrl(src, options.catalog), scratchCtx));
59
+ }));
60
+ const mixCtx = new OfflineAudioContext(2, frames, sampleRate);
61
+ // The same shared-chain rule the live device uses, for the same reason: an
62
+ // export that flattened buses into per-clip processing would not match what
63
+ // the preview sounded like.
64
+ const buses = new AudioBusGraph(mixCtx, mixCtx.destination);
65
+ for (const { request: req, globalOffset } of requests) {
66
+ const srcBuffer = decoded.get(req.src);
67
+ if (!srcBuffer)
68
+ continue;
69
+ const globalStart = globalOffset + req.startAt;
70
+ // `endAt` is `Infinity` for an open clip; either way the timeline bounds it.
71
+ const globalEnd = Math.min(globalOffset + req.endAt, totalDuration);
72
+ const playDuration = globalEnd - globalStart;
73
+ if (playDuration <= 0)
74
+ continue;
75
+ const source = mixCtx.createBufferSource();
76
+ 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;
114
+ source.start(globalStart, req.trimStart, consumed);
115
+ for (const osc of graph.oscillators)
116
+ osc.start(globalStart);
117
+ }
118
+ return mixCtx.startRendering();
119
+ }
120
+ /**
121
+ * Adapter presenting {@link mixAudio} as the exporter's injected mixer seam.
122
+ *
123
+ * The sample rate is a constructor argument rather than a third `mix` parameter
124
+ * because {@link AudioMixer} — the seam `renderTimeline` calls through — takes
125
+ * exactly two, so anything passed as a third would be silently dropped. Fixing
126
+ * it here rather than widening the interface keeps the rate a property of *this*
127
+ * mixer, which is whose business it is.
128
+ */
129
+ export class WebAudioMixer {
130
+ sampleRate;
131
+ catalog;
132
+ constructor(sampleRate = DEFAULT_SAMPLE_RATE, catalog) {
133
+ this.sampleRate = sampleRate;
134
+ this.catalog = catalog;
135
+ }
136
+ mix(requests, totalDuration) {
137
+ return mixAudio(requests, totalDuration, {
138
+ sampleRate: this.sampleRate,
139
+ catalog: this.catalog,
140
+ });
141
+ }
142
+ }
143
+ /**
144
+ * Encode an `AudioBuffer` as a 16-bit PCM WAV.
145
+ *
146
+ * The only other encoder in this package is the AAC track inside the mp4 muxer,
147
+ * which is reachable solely by exporting a video — so without this a mixdown has
148
+ * no file representation at all. WAV because it needs no codec support and is
149
+ * lossless; a host wanting mp3/aac can re-encode from here.
150
+ */
151
+ export function encodeWav(buffer) {
152
+ const channelCount = buffer.numberOfChannels;
153
+ const frameCount = buffer.length;
154
+ const bytesPerSample = 2;
155
+ const blockAlign = channelCount * bytesPerSample;
156
+ const dataBytes = frameCount * blockAlign;
157
+ const out = new ArrayBuffer(44 + dataBytes);
158
+ const view = new DataView(out);
159
+ const ascii = (offset, text) => {
160
+ for (let i = 0; i < text.length; i++)
161
+ view.setUint8(offset + i, text.charCodeAt(i));
162
+ };
163
+ ascii(0, "RIFF");
164
+ view.setUint32(4, 36 + dataBytes, true);
165
+ ascii(8, "WAVE");
166
+ ascii(12, "fmt ");
167
+ view.setUint32(16, 16, true); // PCM header length
168
+ view.setUint16(20, 1, true); // format: PCM
169
+ view.setUint16(22, channelCount, true);
170
+ view.setUint32(24, buffer.sampleRate, true);
171
+ view.setUint32(28, buffer.sampleRate * blockAlign, true);
172
+ view.setUint16(32, blockAlign, true);
173
+ view.setUint16(34, 8 * bytesPerSample, true);
174
+ ascii(36, "data");
175
+ view.setUint32(40, dataBytes, true);
176
+ // Pull each channel out once — getChannelData is a live view, but calling it
177
+ // per sample inside the interleave loop is measurably slower.
178
+ const channels = [];
179
+ for (let c = 0; c < channelCount; c++)
180
+ channels.push(buffer.getChannelData(c));
181
+ let offset = 44;
182
+ for (let frame = 0; frame < frameCount; frame++) {
183
+ for (let c = 0; c < channelCount; c++) {
184
+ // Clamp before scaling: the mix bus can exceed [-1,1] where clips
185
+ // overlap, and letting that wrap turns a loud passage into noise.
186
+ const sample = Math.max(-1, Math.min(1, channels[c][frame]));
187
+ view.setInt16(offset, sample < 0 ? sample * 0x8000 : sample * 0x7fff, true);
188
+ offset += bytesPerSample;
189
+ }
190
+ }
191
+ return new Uint8Array(out);
192
+ }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E,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"]}