@motionscript/browser 0.1.0 → 0.1.1-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 (102) hide show
  1. package/CHANGELOG.md +19 -0
  2. package/dist/asset-bytes.d.ts +12 -50
  3. package/dist/asset-bytes.d.ts.map +1 -1
  4. package/dist/asset-bytes.js +20 -52
  5. package/dist/asset-bytes.js.map +1 -1
  6. package/dist/audio/bus-graph.d.ts +4 -39
  7. package/dist/audio/bus-graph.d.ts.map +1 -1
  8. package/dist/audio/bus-graph.js +4 -38
  9. package/dist/audio/bus-graph.js.map +1 -1
  10. package/dist/audio/effect-graph.d.ts +4 -29
  11. package/dist/audio/effect-graph.d.ts.map +1 -1
  12. package/dist/audio/effect-graph.js +4 -31
  13. package/dist/audio/effect-graph.js.map +1 -1
  14. package/dist/audio/loop-region.d.ts +3 -21
  15. package/dist/audio/loop-region.d.ts.map +1 -1
  16. package/dist/audio/loop-region.js +3 -21
  17. package/dist/audio/loop-region.js.map +1 -1
  18. package/dist/audio/mixer.d.ts +9 -41
  19. package/dist/audio/mixer.d.ts.map +1 -1
  20. package/dist/audio/mixer.js +11 -44
  21. package/dist/audio/mixer.js.map +1 -1
  22. package/dist/audio/player.d.ts +3 -17
  23. package/dist/audio/player.d.ts.map +1 -1
  24. package/dist/audio/player.js +4 -23
  25. package/dist/audio/player.js.map +1 -1
  26. package/dist/browser/audio.js +1 -1
  27. package/dist/browser/chunks/chunk-NDJYMKIV.js +2 -0
  28. package/dist/browser/chunks/chunk-NDJYMKIV.js.map +7 -0
  29. package/dist/browser/index.js +19 -19
  30. package/dist/browser/index.js.map +4 -4
  31. package/dist/browser/manifest.json +1 -1
  32. package/dist/browser/ms_renderer_bg.wasm +0 -0
  33. package/dist/concat.d.ts +25 -84
  34. package/dist/concat.d.ts.map +1 -1
  35. package/dist/concat.js +19 -69
  36. package/dist/concat.js.map +1 -1
  37. package/dist/dynamic-nodes.d.ts +1 -6
  38. package/dist/dynamic-nodes.d.ts.map +1 -1
  39. package/dist/dynamic-nodes.js +3 -24
  40. package/dist/dynamic-nodes.js.map +1 -1
  41. package/dist/engine.d.ts +9 -42
  42. package/dist/engine.d.ts.map +1 -1
  43. package/dist/engine.js +10 -44
  44. package/dist/engine.js.map +1 -1
  45. package/dist/export-audio.d.ts +1 -6
  46. package/dist/export-audio.d.ts.map +1 -1
  47. package/dist/export-audio.js +1 -6
  48. package/dist/export-audio.js.map +1 -1
  49. package/dist/exporter.d.ts +28 -141
  50. package/dist/exporter.d.ts.map +1 -1
  51. package/dist/exporter.js +15 -90
  52. package/dist/exporter.js.map +1 -1
  53. package/dist/frame-loop.d.ts +19 -105
  54. package/dist/frame-loop.d.ts.map +1 -1
  55. package/dist/frame-loop.js +20 -82
  56. package/dist/frame-loop.js.map +1 -1
  57. package/dist/index.d.ts +1 -0
  58. package/dist/index.d.ts.map +1 -1
  59. package/dist/index.js +7 -15
  60. package/dist/index.js.map +1 -1
  61. package/dist/mix-timeline-audio.d.ts +7 -24
  62. package/dist/mix-timeline-audio.d.ts.map +1 -1
  63. package/dist/mix-timeline-audio.js +7 -23
  64. package/dist/mix-timeline-audio.js.map +1 -1
  65. package/dist/native-export.d.ts +29 -0
  66. package/dist/native-export.d.ts.map +1 -0
  67. package/dist/native-export.js +99 -0
  68. package/dist/native-export.js.map +1 -0
  69. package/dist/parallel/export-parallel.d.ts +2 -16
  70. package/dist/parallel/export-parallel.d.ts.map +1 -1
  71. package/dist/parallel/export-parallel.js +1 -11
  72. package/dist/parallel/export-parallel.js.map +1 -1
  73. package/dist/parallel/governor.d.ts +2 -15
  74. package/dist/parallel/governor.d.ts.map +1 -1
  75. package/dist/parallel/governor.js +1 -5
  76. package/dist/parallel/governor.js.map +1 -1
  77. package/dist/parallel/lane.d.ts +1 -5
  78. package/dist/parallel/lane.d.ts.map +1 -1
  79. package/dist/parallel/lane.js.map +1 -1
  80. package/dist/parallel/plan.d.ts +3 -7
  81. package/dist/parallel/plan.d.ts.map +1 -1
  82. package/dist/parallel/plan.js +1 -6
  83. package/dist/parallel/plan.js.map +1 -1
  84. package/dist/parallel/run-lanes.d.ts +4 -18
  85. package/dist/parallel/run-lanes.d.ts.map +1 -1
  86. package/dist/parallel/run-lanes.js +1 -12
  87. package/dist/parallel/run-lanes.js.map +1 -1
  88. package/dist/screenshot.d.ts +1 -12
  89. package/dist/screenshot.d.ts.map +1 -1
  90. package/dist/screenshot.js +1 -12
  91. package/dist/screenshot.js.map +1 -1
  92. package/dist/still.d.ts +17 -74
  93. package/dist/still.d.ts.map +1 -1
  94. package/dist/still.js +13 -50
  95. package/dist/still.js.map +1 -1
  96. package/dist/ticker.d.ts +10 -39
  97. package/dist/ticker.d.ts.map +1 -1
  98. package/dist/ticker.js +13 -45
  99. package/dist/ticker.js.map +1 -1
  100. package/package.json +4 -4
  101. package/dist/browser/chunks/chunk-F3AZKPHU.js +0 -2
  102. package/dist/browser/chunks/chunk-F3AZKPHU.js.map +0 -7
@@ -1 +1 @@
1
- {"version":3,"file":"effect-graph.js","sourceRoot":"","sources":["../../src/audio/effect-graph.ts"],"names":[],"mappings":"AAAA,OAAO,EACH,gBAAgB,EAChB,MAAM,EACN,WAAW,EACX,UAAU,EACV,gBAAgB,EAChB,WAAW,EACX,gBAAgB,EAChB,eAAe,GAMlB,MAAM,oBAAoB,CAAC;AAe5B,iGAAiG;AACjG,MAAM,wBAAwB,GAAG,GAAG,CAAC;AAErC;;;;;;;;GAQG;AACH,SAAS,UAAU,CACf,UAAsB,EACtB,KAAY,EACZ,SAAiB,EACjB,YAAoB,EACpB,OAAe,EACf,QAA+B,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;IAEvC,6CAA6C;IAC7C,MAAM,MAAM,GAAG,CAAC,CAAS,EAAU,EAAE;QACjC,MAAM,CAAC,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC;QACnB,OAAO,MAAM,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC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AO,CAAC,CAAC;IACpC,MAAM,OAAO,GAAG,CAAC,WAAmB,EAAU,EAAE,CAC5C,IAAI,CAAC,GAAG,CAAC,IAAI,EAAE,UAAU,CAAC,KAAK,EAAE,gBAAgB,CAAC,KAAK,EAAE,YAAY,EAAE,WAAW,CAAC,EAAE,YAAY,EAAE,CAAC,CAAC,CAAC,CAAC;IAC3G,IAAI,CAAC,cAAc,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE,SAAS,CAAC,CAAC;IAEhD,KAAK,IAAI,CAAC,GAAG,MAAM,GAAG,IAAI,EAAE,CAAC,IAAI,YAAY,GAAG,IAAI,EAAE,CAAC,IAAI,IAAI,EAAE,CAAC;QAC9D,MAAM,OAAO,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,YAAY,CAAC,CAAC;QAC1C,IAAI,CAAC,uBAAuB,CAAC,OAAO,CAAC,OAAO,CAAC,EAAE,SAAS,GAAG,CAAC,OAAO,GAAG,MAAM,CAAC,CAAC,CAAC;QAC/E,IAAI,OAAO,IAAI,YAAY;YAAE,MAAM;IACvC,CAAC;AACL,CAAC","sourcesContent":["import {\n clipTimeAtSource,\n isRamp,\n resolveRamp,\n sampleRamp,\n sourceTimeAtClip,\n staticValue,\n transitionGainAt,\n transitionSpans,\n type AudioEffect,\n type AudioRequest,\n type AudioTransition,\n type Param,\n type Ramp,\n} from \"@motionscript/core\";\n\n/**\n * The Web Audio graph for one source's effect chain.\n *\n * `output` is the last node, to connect downstream; `nodes` are every node to\n * `disconnect()` on teardown; `oscillators` are tremolo LFOs the caller starts\n * and stops alongside the source.\n */\nexport interface AudioEffectGraph {\n output: AudioNode;\n nodes: AudioNode[];\n oscillators: OscillatorNode[];\n}\n\n/** Sample points per second an eased segment or a transition is baked into: about every 5 ms. */\nconst CURVE_SAMPLES_PER_SECOND = 200;\n\n/**\n * Apply a {@link Param} to a Web Audio {@link AudioParam}: a number once, a\n * ramp as a schedule — linear segments as `linearRampToValueAtTime`, eased\n * ones baked into `setValueCurveAtTime`.\n *\n * `elapsed` is the clip seconds already played when a mid-clip seek starts the\n * source, so \"now\" reads the ramp at the clip's current position. `clamp` keeps\n * every scheduled value in a param's valid range.\n */\nfunction applyParam(\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 // The platform throws on a non-finite value.\n const finite = (v: number): number => {\n const c = clamp(v);\n return Number.isFinite(c) ? c : audioParam.value;\n };\n\n if (!isRamp(param)) {\n audioParam.value = finite(param);\n return;\n }\n if (!Number.isFinite(clipDuration) || clipDuration <= 0) {\n audioParam.value = finite(staticValue(param, audioParam.value));\n return;\n }\n\n const keys = resolveRamp(param, clipDuration);\n if (keys.length === 0) return;\n const base = startTime - elapsed;\n audioParam.setValueAtTime(finite(sampleRamp(param, Math.max(0, elapsed), clipDuration, audioParam.value)), startTime);\n\n for (let i = 1; i < keys.length; i++) {\n const previous = keys[i - 1];\n const key = keys[i];\n const segStart = base + previous.time;\n const segEnd = base + key.time;\n if (segEnd <= startTime) continue;\n\n const span = key.time - previous.time;\n if (span <= 0) {\n audioParam.setValueAtTime(finite(key.value), Math.max(segEnd, startTime));\n continue;\n }\n\n // A segment already under way when the source starts carries on from\n // the value set at `startTime` rather than restarting from its own.\n const done = Math.max(0, (startTime - segStart) / span);\n const curveStart = Math.max(segStart, startTime);\n if (key.linear) {\n if (done === 0) audioParam.setValueAtTime(finite(previous.value), curveStart);\n audioParam.linearRampToValueAtTime(finite(key.value), segEnd);\n } else {\n const curveDur = segEnd - curveStart;\n const count = Math.max(2, Math.ceil(curveDur * CURVE_SAMPLES_PER_SECOND));\n const samples = new Float32Array(count);\n for (let n = 0; n < count; n++) {\n const local = done + (1 - done) * (n / (count - 1));\n samples[n] = finite(previous.value + (key.value - previous.value) * key.ease(local));\n }\n audioParam.setValueCurveAtTime(samples, curveStart, curveDur);\n }\n }\n}\n\n/**\n * Builds the Web Audio graph for a clip's effect chain, after an\n * already-created `source`. Shared by live playback ({@link WebAudioSink}) and\n * offline export (`mixAudio`) so both sound identical.\n *\n * `startTime` is the context time the source starts; `clipDuration` the clip\n * seconds ramps run over (a clip's, or a bus's span); `elapsed` the clip seconds\n * already played at a mid-clip seek (0 for a clean start, negative before the\n * span begins).\n */\nexport function buildAudioEffectGraph(\n ctx: BaseAudioContext,\n source: AudioNode,\n effects: readonly AudioEffect[],\n startTime: number = ctx.currentTime,\n clipDuration: number = 0,\n elapsed: number = 0,\n): AudioEffectGraph {\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(p, v, startTime, clipDuration, elapsed, clamp);\n\n for (const effect of effects) {\n switch (effect.type) {\n case \"gain\": {\n const gain = ctx.createGain();\n apply(gain.gain, effect.value);\n series(gain);\n break;\n }\n case \"highpass\":\n case \"lowpass\": {\n const biquad = ctx.createBiquadFilter();\n biquad.type = effect.type;\n apply(biquad.frequency, effect.frequency);\n if (effect.q !== undefined) apply(biquad.Q, effect.q);\n series(biquad);\n break;\n }\n case \"tremolo\": {\n // A carrier gain an LFO swings: with depth d the carrier sits at\n // 1 - d/2 and the swing is d/2.\n const carrier = ctx.createGain();\n const lfo = ctx.createOscillator();\n const lfoDepth = ctx.createGain();\n\n apply(carrier.gain, effect.depth, (d) => 1 - clamp01(d) / 2);\n apply(lfoDepth.gain, effect.depth, (d) => clamp01(d) / 2);\n apply(lfo.frequency, effect.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 const split = cursor;\n const merge = ctx.createGain();\n\n const dry = ctx.createGain();\n apply(dry.gain, effect.mix ?? 0.5, (m) => 1 - clamp01(m));\n split.connect(dry);\n dry.connect(merge);\n\n // The delay line is sized once, so it has to hold the ramp's longest delay.\n const delay = ctx.createDelay(Math.max(1, maxOf(effect.delay) + 1));\n apply(delay.delayTime, effect.delay);\n const feedbackGain = ctx.createGain();\n apply(feedbackGain.gain, effect.feedback, (f) => Math.max(0, Math.min(0.99, f)));\n const wet = ctx.createGain();\n apply(wet.gain, effect.mix ?? 0.5, (m) => clamp01(m));\n\n split.connect(delay);\n delay.connect(feedbackGain);\n feedbackGain.connect(delay);\n delay.connect(wet);\n wet.connect(merge);\n\n nodes.push(dry, delay, feedbackGain, wet, merge);\n cursor = merge;\n break;\n }\n }\n }\n\n return { output: cursor, nodes, oscillators };\n}\n\nfunction clamp01(v: number): number {\n return Math.max(0, Math.min(1, v));\n}\n\nfunction maxOf(p: Param): number {\n return isRamp(p) ? Math.max(0, ...p.keys.map((k) => k.value)) : p;\n}\n\n/**\n * Schedule `transitionIn`/`transitionOut` onto a gain over a span `length`\n * seconds long, baked like an eased ramp. Between the two it holds at 1.\n */\nexport function applyTransitions(\n gain: AudioParam,\n length: number,\n transitionIn: AudioTransition | undefined,\n transitionOut: AudioTransition | undefined,\n startTime: number,\n elapsed: number,\n): void {\n if (!transitionIn && !transitionOut) return;\n const seek = Math.max(0, elapsed);\n const at = (t: number): number => transitionGainAt(t, length, transitionIn, transitionOut);\n gain.setValueAtTime(at(seek), startTime);\n\n const spans = transitionSpans(length, transitionIn, transitionOut);\n const windows: [number, number][] = [];\n if (spans.in > 0) windows.push([0, spans.in]);\n if (spans.out > 0 && Number.isFinite(length)) windows.push([length - spans.out, length]);\n const base = startTime - elapsed;\n for (const [from, to] of windows) {\n const begin = Math.max(from, seek);\n const duration = to - begin;\n if (duration <= 0) continue;\n const count = Math.max(2, Math.ceil(duration * CURVE_SAMPLES_PER_SECOND));\n const samples = new Float32Array(count);\n for (let n = 0; n < count; n++) samples[n] = at(begin + duration * (n / (count - 1)));\n gain.setValueCurveAtTime(samples, base + begin, duration);\n }\n}\n\n/** How one request plays its source: the rate, and the lengths its ramps and transitions run over. */\nexport interface Playback {\n /** The constant `playbackRate`; 1 under a ramp, which is scheduled instead. */\n rate: number;\n speedRamp?: Ramp;\n /** Source seconds a speed ramp is placed on. */\n sourceLength: number;\n /** Clip seconds the volume and effect ramps run over. */\n clipDuration: number;\n /** What the transitions run over: the clip's length, unknown for an open one. */\n transitionLength: number;\n}\n\nexport function playbackOf(req: AudioRequest, bufferDuration: number): Playback {\n const speed = req.speed ?? 1;\n const speedRamp = isRamp(speed) ? speed : undefined;\n const rate = speedRamp || !(Number.isFinite(speed) && (speed as number) > 0) ? 1 : (speed as number);\n // Placed on the same length core sized the request with, not the decoded buffer.\n const sourceLength = req.sourceLength ?? Math.max(0, bufferDuration - req.trimStart);\n const bounded = Number.isFinite(req.endAt);\n const clipDuration = bounded ? Math.max(0, req.endAt - req.startAt) : clipTimeAtSource(speed, sourceLength);\n return { rate, speedRamp, sourceLength, clipDuration, transitionLength: bounded ? clipDuration : Infinity };\n}\n\n/** Source seconds after `trimStart` that `clipElapsed` clip seconds have played. */\nexport function sourceOffsetOf(playback: Playback, clipElapsed: number): number {\n return playback.speedRamp\n ? sourceTimeAtClip(playback.speedRamp, playback.sourceLength, clipElapsed)\n : clipElapsed * playback.rate;\n}\n\n/**\n * A request's level, then its transitions, after `from`: the gains every\n * request plays through on its way to its bus.\n */\nexport function buildLevel(\n ctx: BaseAudioContext,\n from: AudioNode,\n req: AudioRequest,\n playback: Playback,\n startTime: number,\n elapsed: number,\n): { output: AudioNode; nodes: AudioNode[] } {\n const level = ctx.createGain();\n applyParam(level.gain, req.volume ?? 1, startTime, playback.clipDuration, elapsed);\n from.connect(level);\n if (!req.transitionIn && !req.transitionOut) return { output: level, nodes: [level] };\n const transition = ctx.createGain();\n applyTransitions(transition.gain, playback.transitionLength, req.transitionIn, req.transitionOut, startTime, elapsed);\n level.connect(transition);\n return { output: transition, nodes: [level, transition] };\n}\n\n/**\n * Schedule a speed ramp onto an `AudioBufferSourceNode.playbackRate`.\n *\n * The ramp runs over **source** seconds and Web Audio schedules in context\n * seconds, so this walks clip time in small steps, converts each to a source\n * offset through the integral inverse, and lays down the rate there. `elapsed`\n * is clip seconds already played at a mid-clip seek.\n */\nexport function applySpeedToPlaybackRate(\n rate: AudioParam,\n speed: Ramp | undefined,\n sourceLength: number,\n clipDuration: number,\n startTime: number,\n elapsed: number,\n): void {\n if (!speed) return;\n const step = 1 / CURVE_SAMPLES_PER_SECOND;\n const seekAt = Math.max(0, elapsed);\n const valueAt = (clipElapsed: number): number =>\n Math.max(1e-4, sampleRamp(speed, sourceTimeAtClip(speed, sourceLength, clipElapsed), sourceLength, 1));\n rate.setValueAtTime(valueAt(seekAt), startTime);\n\n for (let s = seekAt + step; s <= clipDuration + step; s += step) {\n const clamped = Math.min(s, clipDuration);\n rate.linearRampToValueAtTime(valueAt(clamped), startTime + (clamped - seekAt));\n if (clamped >= clipDuration) break;\n }\n}\n"]}
1
+ {"version":3,"file":"effect-graph.js","sourceRoot":"","sources":["../../src/audio/effect-graph.ts"],"names":[],"mappings":"AAAA,OAAO,EACH,gBAAgB,EAChB,MAAM,EACN,WAAW,EACX,UAAU,EACV,gBAAgB,EAChB,WAAW,EACX,gBAAgB,EAChB,eAAe,GAMlB,MAAM,oBAAoB,CAAC;AAS5B,iGAAiG;AACjG,MAAM,wBAAwB,GAAG,GAAG,CAAC;AAErC,+TAA+T;AAC/T,SAAS,UAAU,CACf,UAAsB,EACtB,KAAY,EACZ,SAAiB,EACjB,YAAoB,EACpB,OAAe,EACf,QAA+B,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;IAEvC,6CAA6C;IAC7C,MAAM,MAAM,GAAG,CAAC,CAAS,EAAU,EAAE;QACjC,MAAM,CAAC,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC;QACnB,OAAO,MAAM,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,UAAU,CAAC,KAAK,CAAC;IACrD,CAAC,CA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C5C,IAAI,CAAC,GAAG,CAAC,IAAI,EAAE,UAAU,CAAC,KAAK,EAAE,gBAAgB,CAAC,KAAK,EAAE,YAAY,EAAE,WAAW,CAAC,EAAE,YAAY,EAAE,CAAC,CAAC,CAAC,CAAC;IAC3G,IAAI,CAAC,cAAc,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE,SAAS,CAAC,CAAC;IAEhD,KAAK,IAAI,CAAC,GAAG,MAAM,GAAG,IAAI,EAAE,CAAC,IAAI,YAAY,GAAG,IAAI,EAAE,CAAC,IAAI,IAAI,EAAE,CAAC;QAC9D,MAAM,OAAO,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,YAAY,CAAC,CAAC;QAC1C,IAAI,CAAC,uBAAuB,CAAC,OAAO,CAAC,OAAO,CAAC,EAAE,SAAS,GAAG,CAAC,OAAO,GAAG,MAAM,CAAC,CAAC,CAAC;QAC/E,IAAI,OAAO,IAAI,YAAY;YAAE,MAAM;IACvC,CAAC;AACL,CAAC","sourcesContent":["import {\n clipTimeAtSource,\n isRamp,\n resolveRamp,\n sampleRamp,\n sourceTimeAtClip,\n staticValue,\n transitionGainAt,\n transitionSpans,\n type AudioEffect,\n type AudioRequest,\n type AudioTransition,\n type Param,\n type Ramp,\n} from \"@motionscript/core\";\n\n/** The Web Audio graph for one source's effect chain: `output` is the last node to connect downstream, `nodes` every node to `disconnect()` on teardown, `oscillators` tremolo LFOs the caller starts and stops with the source. */\nexport interface AudioEffectGraph {\n output: AudioNode;\n nodes: AudioNode[];\n oscillators: OscillatorNode[];\n}\n\n/** Sample points per second an eased segment or a transition is baked into: about every 5 ms. */\nconst CURVE_SAMPLES_PER_SECOND = 200;\n\n/** Applies a {@link Param} to a Web Audio {@link AudioParam}: a number once, a ramp as a schedule (linear segments via `linearRampToValueAtTime`, eased ones baked into `setValueCurveAtTime`). `elapsed` is the clip seconds already played at a mid-clip seek; `clamp` keeps scheduled values in the param's valid range. */\nfunction applyParam(\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 // The platform throws on a non-finite value.\n const finite = (v: number): number => {\n const c = clamp(v);\n return Number.isFinite(c) ? c : audioParam.value;\n };\n\n if (!isRamp(param)) {\n audioParam.value = finite(param);\n return;\n }\n if (!Number.isFinite(clipDuration) || clipDuration <= 0) {\n audioParam.value = finite(staticValue(param, audioParam.value));\n return;\n }\n\n const keys = resolveRamp(param, clipDuration);\n if (keys.length === 0) return;\n const base = startTime - elapsed;\n audioParam.setValueAtTime(finite(sampleRamp(param, Math.max(0, elapsed), clipDuration, audioParam.value)), startTime);\n\n for (let i = 1; i < keys.length; i++) {\n const previous = keys[i - 1];\n const key = keys[i];\n const segStart = base + previous.time;\n const segEnd = base + key.time;\n if (segEnd <= startTime) continue;\n\n const span = key.time - previous.time;\n if (span <= 0) {\n audioParam.setValueAtTime(finite(key.value), Math.max(segEnd, startTime));\n continue;\n }\n\n // A segment already under way when the source starts carries on from\n // the value set at `startTime` rather than restarting from its own.\n const done = Math.max(0, (startTime - segStart) / span);\n const curveStart = Math.max(segStart, startTime);\n if (key.linear) {\n if (done === 0) audioParam.setValueAtTime(finite(previous.value), curveStart);\n audioParam.linearRampToValueAtTime(finite(key.value), segEnd);\n } else {\n const curveDur = segEnd - curveStart;\n const count = Math.max(2, Math.ceil(curveDur * CURVE_SAMPLES_PER_SECOND));\n const samples = new Float32Array(count);\n for (let n = 0; n < count; n++) {\n const local = done + (1 - done) * (n / (count - 1));\n samples[n] = finite(previous.value + (key.value - previous.value) * key.ease(local));\n }\n audioParam.setValueCurveAtTime(samples, curveStart, curveDur);\n }\n }\n}\n\n/** Builds the Web Audio graph for a clip's effect chain after an already-created `source`, shared by live playback ({@link WebAudioSink}) and offline export (`mixAudio`) so both sound identical. `startTime` is the context time the source starts; `clipDuration` the clip seconds ramps run over (a clip's, or a bus's span); `elapsed` the clip seconds already played at a mid-clip seek (0 for a clean start, negative before the span begins). */\nexport function buildAudioEffectGraph(\n ctx: BaseAudioContext,\n source: AudioNode,\n effects: readonly AudioEffect[],\n startTime: number = ctx.currentTime,\n clipDuration: number = 0,\n elapsed: number = 0,\n): AudioEffectGraph {\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(p, v, startTime, clipDuration, elapsed, clamp);\n\n for (const effect of effects) {\n switch (effect.type) {\n case \"gain\": {\n const gain = ctx.createGain();\n apply(gain.gain, effect.value);\n series(gain);\n break;\n }\n case \"highpass\":\n case \"lowpass\": {\n const biquad = ctx.createBiquadFilter();\n biquad.type = effect.type;\n apply(biquad.frequency, effect.frequency);\n if (effect.q !== undefined) apply(biquad.Q, effect.q);\n series(biquad);\n break;\n }\n case \"tremolo\": {\n // A carrier gain an LFO swings: with depth d the carrier sits at\n // 1 - d/2 and the swing is d/2.\n const carrier = ctx.createGain();\n const lfo = ctx.createOscillator();\n const lfoDepth = ctx.createGain();\n\n apply(carrier.gain, effect.depth, (d) => 1 - clamp01(d) / 2);\n apply(lfoDepth.gain, effect.depth, (d) => clamp01(d) / 2);\n apply(lfo.frequency, effect.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 const split = cursor;\n const merge = ctx.createGain();\n\n const dry = ctx.createGain();\n apply(dry.gain, effect.mix ?? 0.5, (m) => 1 - clamp01(m));\n split.connect(dry);\n dry.connect(merge);\n\n // The delay line is sized once, so it has to hold the ramp's longest delay.\n const delay = ctx.createDelay(Math.max(1, maxOf(effect.delay) + 1));\n apply(delay.delayTime, effect.delay);\n const feedbackGain = ctx.createGain();\n apply(feedbackGain.gain, effect.feedback, (f) => Math.max(0, Math.min(0.99, f)));\n const wet = ctx.createGain();\n apply(wet.gain, effect.mix ?? 0.5, (m) => clamp01(m));\n\n split.connect(delay);\n delay.connect(feedbackGain);\n feedbackGain.connect(delay);\n delay.connect(wet);\n wet.connect(merge);\n\n nodes.push(dry, delay, feedbackGain, wet, merge);\n cursor = merge;\n break;\n }\n }\n }\n\n return { output: cursor, nodes, oscillators };\n}\n\nfunction clamp01(v: number): number {\n return Math.max(0, Math.min(1, v));\n}\n\nfunction maxOf(p: Param): number {\n return isRamp(p) ? Math.max(0, ...p.keys.map((k) => k.value)) : p;\n}\n\n/** Schedules `transitionIn`/`transitionOut` onto a gain over a span `length` seconds long, baked like an eased ramp; holds at 1 between them. */\nexport function applyTransitions(\n gain: AudioParam,\n length: number,\n transitionIn: AudioTransition | undefined,\n transitionOut: AudioTransition | undefined,\n startTime: number,\n elapsed: number,\n): void {\n if (!transitionIn && !transitionOut) return;\n const seek = Math.max(0, elapsed);\n const at = (t: number): number => transitionGainAt(t, length, transitionIn, transitionOut);\n gain.setValueAtTime(at(seek), startTime);\n\n const spans = transitionSpans(length, transitionIn, transitionOut);\n const windows: [number, number][] = [];\n if (spans.in > 0) windows.push([0, spans.in]);\n if (spans.out > 0 && Number.isFinite(length)) windows.push([length - spans.out, length]);\n const base = startTime - elapsed;\n for (const [from, to] of windows) {\n const begin = Math.max(from, seek);\n const duration = to - begin;\n if (duration <= 0) continue;\n const count = Math.max(2, Math.ceil(duration * CURVE_SAMPLES_PER_SECOND));\n const samples = new Float32Array(count);\n for (let n = 0; n < count; n++) samples[n] = at(begin + duration * (n / (count - 1)));\n gain.setValueCurveAtTime(samples, base + begin, duration);\n }\n}\n\n/** How one request plays its source: the rate, and the lengths its ramps and transitions run over. */\nexport interface Playback {\n /** The constant `playbackRate`; 1 under a ramp, which is scheduled instead. */\n rate: number;\n speedRamp?: Ramp;\n /** Source seconds a speed ramp is placed on. */\n sourceLength: number;\n /** Clip seconds the volume and effect ramps run over. */\n clipDuration: number;\n /** What the transitions run over: the clip's length, unknown for an open one. */\n transitionLength: number;\n}\n\nexport function playbackOf(req: AudioRequest, bufferDuration: number): Playback {\n const speed = req.speed ?? 1;\n const speedRamp = isRamp(speed) ? speed : undefined;\n const rate = speedRamp || !(Number.isFinite(speed) && (speed as number) > 0) ? 1 : (speed as number);\n // Placed on the same length core sized the request with, not the decoded buffer.\n const sourceLength = req.sourceLength ?? Math.max(0, bufferDuration - req.trimStart);\n const bounded = Number.isFinite(req.endAt);\n const clipDuration = bounded ? Math.max(0, req.endAt - req.startAt) : clipTimeAtSource(speed, sourceLength);\n return { rate, speedRamp, sourceLength, clipDuration, transitionLength: bounded ? clipDuration : Infinity };\n}\n\n/** Source seconds after `trimStart` that `clipElapsed` clip seconds have played. */\nexport function sourceOffsetOf(playback: Playback, clipElapsed: number): number {\n return playback.speedRamp\n ? sourceTimeAtClip(playback.speedRamp, playback.sourceLength, clipElapsed)\n : clipElapsed * playback.rate;\n}\n\n/**\n * A request's level, then its transitions, after `from`: the gains every\n * request plays through on its way to its bus.\n */\nexport function buildLevel(\n ctx: BaseAudioContext,\n from: AudioNode,\n req: AudioRequest,\n playback: Playback,\n startTime: number,\n elapsed: number,\n): { output: AudioNode; nodes: AudioNode[] } {\n const level = ctx.createGain();\n applyParam(level.gain, req.volume ?? 1, startTime, playback.clipDuration, elapsed);\n from.connect(level);\n if (!req.transitionIn && !req.transitionOut) return { output: level, nodes: [level] };\n const transition = ctx.createGain();\n applyTransitions(transition.gain, playback.transitionLength, req.transitionIn, req.transitionOut, startTime, elapsed);\n level.connect(transition);\n return { output: transition, nodes: [level, transition] };\n}\n\n/** Schedules a speed ramp onto `playbackRate`. The ramp runs over **source** seconds and Web Audio schedules in context seconds, so this walks clip time in small steps and converts each through the integral inverse. `elapsed` is clip seconds already played at a mid-clip seek. */\nexport function applySpeedToPlaybackRate(\n rate: AudioParam,\n speed: Ramp | undefined,\n sourceLength: number,\n clipDuration: number,\n startTime: number,\n elapsed: number,\n): void {\n if (!speed) return;\n const step = 1 / CURVE_SAMPLES_PER_SECOND;\n const seekAt = Math.max(0, elapsed);\n const valueAt = (clipElapsed: number): number =>\n Math.max(1e-4, sampleRamp(speed, sourceTimeAtClip(speed, sourceLength, clipElapsed), sourceLength, 1));\n rate.setValueAtTime(valueAt(seekAt), startTime);\n\n for (let s = seekAt + step; s <= clipDuration + step; s += step) {\n const clamped = Math.min(s, clipDuration);\n rate.linearRampToValueAtTime(valueAt(clamped), startTime + (clamped - seekAt));\n if (clamped >= clipDuration) break;\n }\n}\n"]}
@@ -4,28 +4,10 @@ export interface LoopRegion {
4
4
  start: number;
5
5
  end: number;
6
6
  }
7
- /**
8
- * The region a looping request repeats: from its in-point to its out-point, or to
9
- * the end of the buffer when it has none. `null` for a request that does not loop,
10
- * and for a degenerate region (an out-point at or before the in-point), where the
11
- * device falls back to wrapping the whole buffer.
12
- *
13
- * Shared by the live device and the offline mixer so a preview and an export loop
14
- * the same samples.
15
- */
7
+ /** The region a looping request repeats (in-point to out-point, else the end of the buffer); `null` when it does not loop or is degenerate (out-point at or before in-point), where the device wraps the whole buffer. Shared with the offline mixer so preview and export loop the same samples. */
16
8
  export declare function loopRegionOf(request: AudioRequest, bufferDuration: number): LoopRegion | null;
17
- /**
18
- * Configure `source` to play `request`'s loop: on or off, and over the trimmed
19
- * region rather than the whole buffer (`loopStart`/`loopEnd` default to the
20
- * whole buffer, which is how a trim used to be lost).
21
- */
9
+ /** Configures `source` to loop `request`'s trimmed region rather than the whole buffer (`loopStart`/`loopEnd` default to the whole buffer, which loses a trim). */
22
10
  export declare function applyLoop(source: AudioBufferSourceNode, request: AudioRequest): LoopRegion | null;
23
- /**
24
- * Fold a source offset into a loop region.
25
- *
26
- * A seek into the middle of a looping clip lands at `trimStart + elapsed`, which
27
- * can be many passes past the region's end; the device would not wrap an offset
28
- * that starts outside the region, so it is wrapped here.
29
- */
11
+ /** Folds a source offset into a loop region: a seek into a looping clip lands at `trimStart + elapsed`, possibly many passes past the end, and the device does not wrap an offset that starts outside the region. */
30
12
  export declare function wrapIntoRegion(region: LoopRegion | null, offset: number): number;
31
13
  //# sourceMappingURL=loop-region.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"loop-region.d.ts","sourceRoot":"","sources":["../../src/audio/loop-region.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAEvD,iFAAiF;AACjF,MAAM,WAAW,UAAU;IACvB,KAAK,EAAE,MAAM,CAAC;IACd,GAAG,EAAE,MAAM,CAAC;CACf;AAED;;;;;;;;GAQG;AACH,wBAAgB,YAAY,CAAC,OAAO,EAAE,YAAY,EAAE,cAAc,EAAE,MAAM,GAAG,UAAU,GAAG,IAAI,CAK7F;AAED;;;;GAIG;AACH,wBAAgB,SAAS,CAAC,MAAM,EAAE,qBAAqB,EAAE,OAAO,EAAE,YAAY,GAAG,UAAU,GAAG,IAAI,CAQjG;AAED;;;;;;GAMG;AACH,wBAAgB,cAAc,CAAC,MAAM,EAAE,UAAU,GAAG,IAAI,EAAE,MAAM,EAAE,MAAM,GAAG,MAAM,CAGhF"}
1
+ {"version":3,"file":"loop-region.d.ts","sourceRoot":"","sources":["../../src/audio/loop-region.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAEvD,iFAAiF;AACjF,MAAM,WAAW,UAAU;IACvB,KAAK,EAAE,MAAM,CAAC;IACd,GAAG,EAAE,MAAM,CAAC;CACf;AAED,qSAAqS;AACrS,wBAAgB,YAAY,CAAC,OAAO,EAAE,YAAY,EAAE,cAAc,EAAE,MAAM,GAAG,UAAU,GAAG,IAAI,CAK7F;AAED,mKAAmK;AACnK,wBAAgB,SAAS,CAAC,MAAM,EAAE,qBAAqB,EAAE,OAAO,EAAE,YAAY,GAAG,UAAU,GAAG,IAAI,CAQjG;AAED,qNAAqN;AACrN,wBAAgB,cAAc,CAAC,MAAM,EAAE,UAAU,GAAG,IAAI,EAAE,MAAM,EAAE,MAAM,GAAG,MAAM,CAGhF"}
@@ -1,12 +1,4 @@
1
- /**
2
- * The region a looping request repeats: from its in-point to its out-point, or to
3
- * the end of the buffer when it has none. `null` for a request that does not loop,
4
- * and for a degenerate region (an out-point at or before the in-point), where the
5
- * device falls back to wrapping the whole buffer.
6
- *
7
- * Shared by the live device and the offline mixer so a preview and an export loop
8
- * the same samples.
9
- */
1
+ /** The region a looping request repeats (in-point to out-point, else the end of the buffer); `null` when it does not loop or is degenerate (out-point at or before in-point), where the device wraps the whole buffer. Shared with the offline mixer so preview and export loop the same samples. */
10
2
  export function loopRegionOf(request, bufferDuration) {
11
3
  if (!request.loop)
12
4
  return null;
@@ -14,11 +6,7 @@ export function loopRegionOf(request, bufferDuration) {
14
6
  const end = Math.min(request.trimEnd ?? bufferDuration, bufferDuration);
15
7
  return end > start ? { start, end } : null;
16
8
  }
17
- /**
18
- * Configure `source` to play `request`'s loop: on or off, and over the trimmed
19
- * region rather than the whole buffer (`loopStart`/`loopEnd` default to the
20
- * whole buffer, which is how a trim used to be lost).
21
- */
9
+ /** Configures `source` to loop `request`'s trimmed region rather than the whole buffer (`loopStart`/`loopEnd` default to the whole buffer, which loses a trim). */
22
10
  export function applyLoop(source, request) {
23
11
  source.loop = request.loop;
24
12
  const region = source.buffer ? loopRegionOf(request, source.buffer.duration) : null;
@@ -28,13 +16,7 @@ export function applyLoop(source, request) {
28
16
  }
29
17
  return region;
30
18
  }
31
- /**
32
- * Fold a source offset into a loop region.
33
- *
34
- * A seek into the middle of a looping clip lands at `trimStart + elapsed`, which
35
- * can be many passes past the region's end; the device would not wrap an offset
36
- * that starts outside the region, so it is wrapped here.
37
- */
19
+ /** Folds a source offset into a loop region: a seek into a looping clip lands at `trimStart + elapsed`, possibly many passes past the end, and the device does not wrap an offset that starts outside the region. */
38
20
  export function wrapIntoRegion(region, offset) {
39
21
  if (!region || offset < region.end)
40
22
  return offset;
@@ -1 +1 @@
1
- {"version":3,"file":"loop-region.js","sourceRoot":"","sources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AC,GAAG,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC;AAClF,CAAC","sourcesContent":["import type { AudioRequest } from \"@motionscript/core\";\n\n/** The span of a decoded buffer a looping request repeats, in source seconds. */\nexport interface LoopRegion {\n start: number;\n end: number;\n}\n\n/**\n * The region a looping request repeats: from its in-point to its out-point, or to\n * the end of the buffer when it has none. `null` for a request that does not loop,\n * and for a degenerate region (an out-point at or before the in-point), where the\n * device falls back to wrapping the whole buffer.\n *\n * Shared by the live device and the offline mixer so a preview and an export loop\n * the same samples.\n */\nexport function loopRegionOf(request: AudioRequest, bufferDuration: number): LoopRegion | null {\n if (!request.loop) return null;\n const start = Math.min(Math.max(0, request.trimStart), bufferDuration);\n const end = Math.min(request.trimEnd ?? bufferDuration, bufferDuration);\n return end > start ? { start, end } : null;\n}\n\n/**\n * Configure `source` to play `request`'s loop: on or off, and over the trimmed\n * region rather than the whole buffer (`loopStart`/`loopEnd` default to the\n * whole buffer, which is how a trim used to be lost).\n */\nexport function applyLoop(source: AudioBufferSourceNode, request: AudioRequest): LoopRegion | null {\n source.loop = request.loop;\n const region = source.buffer ? loopRegionOf(request, source.buffer.duration) : null;\n if (region) {\n source.loopStart = region.start;\n source.loopEnd = region.end;\n }\n return region;\n}\n\n/**\n * Fold a source offset into a loop region.\n *\n * A seek into the middle of a looping clip lands at `trimStart + elapsed`, which\n * can be many passes past the region's end; the device would not wrap an offset\n * that starts outside the region, so it is wrapped here.\n */\nexport function wrapIntoRegion(region: LoopRegion | null, offset: number): number {\n if (!region || offset < region.end) return offset;\n return region.start + ((offset - region.start) % (region.end - region.start));\n}\n"]}
1
+ {"version":3,"file":"loop-region.js","sourceRoot":"","sources":["../../src/audio/loop-region.ts"],"names":[],"mappings":"AAQA,qSAAqS;AACrS,MAAM,UAAU,YAAY,CAAC,OAAqB,EAAE,cAAsB;IACtE,IAAI,CAAC,OAAO,CAAC,IAAI;QAAE,OAAO,IAAI,CAAC;IAC/B,MAAM,KAAK,GAAG,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,OAAO,CAAC,SAAS,CAAC,EAAE,cAAc,CAAC,CAAC;IACvE,MAAM,GAAG,GAAG,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC,OAAO,IAAI,cAAc,EAAE,cAAc,CAAC,CAAC;IACxE,OAAO,GAAG,GAAG,KAAK,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,GAAG,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC;AAC/C,CAAC;AAED,mKAAmK;AACnK,MAAM,UAAU,SAAS,CAAC,MAA6B,EAAE,OAAqB;IAC1E,MAAM,CAAC,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC;IAC3B,MAAM,MAAM,GAAG,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,YAAY,CAAC,OAAO,EAAE,MAAM,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC;IACpF,IAAI,MAAM,EAAE,CAAC;QACT,MAAM,CAAC,SAAS,GAAG,MAAM,CAAC,KAAK,CAAC;QAChC,MAAM,CAAC,OAAO,GAAG,MAAM,CAAC,GAAG,CAAC;IAChC,CAAC;IACD,OAAO,MAAM,CAAC;AAClB,CAAC;AAED,qNAAqN;AACrN,MAAM,UAAU,cAAc,CAAC,MAAyB,EAAE,MAAc;IACpE,IAAI,CAAC,MAAM,IAAI,MAAM,GAAG,MAAM,CAAC,GAAG;QAAE,OAAO,MAAM,CAAC;IAClD,OAAO,MAAM,CAAC,KAAK,GAAG,CAAC,CAAC,MAAM,GAAG,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,MAAM,CAAC,GAAG,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC;AAClF,CAAC","sourcesContent":["import type { AudioRequest } from \"@motionscript/core\";\n\n/** The span of a decoded buffer a looping request repeats, in source seconds. */\nexport interface LoopRegion {\n start: number;\n end: number;\n}\n\n/** The region a looping request repeats (in-point to out-point, else the end of the buffer); `null` when it does not loop or is degenerate (out-point at or before in-point), where the device wraps the whole buffer. Shared with the offline mixer so preview and export loop the same samples. */\nexport function loopRegionOf(request: AudioRequest, bufferDuration: number): LoopRegion | null {\n if (!request.loop) return null;\n const start = Math.min(Math.max(0, request.trimStart), bufferDuration);\n const end = Math.min(request.trimEnd ?? bufferDuration, bufferDuration);\n return end > start ? { start, end } : null;\n}\n\n/** Configures `source` to loop `request`'s trimmed region rather than the whole buffer (`loopStart`/`loopEnd` default to the whole buffer, which loses a trim). */\nexport function applyLoop(source: AudioBufferSourceNode, request: AudioRequest): LoopRegion | null {\n source.loop = request.loop;\n const region = source.buffer ? loopRegionOf(request, source.buffer.duration) : null;\n if (region) {\n source.loopStart = region.start;\n source.loopEnd = region.end;\n }\n return region;\n}\n\n/** Folds a source offset into a loop region: a seek into a looping clip lands at `trimStart + elapsed`, possibly many passes past the end, and the device does not wrap an offset that starts outside the region. */\nexport function wrapIntoRegion(region: LoopRegion | null, offset: number): number {\n if (!region || offset < region.end) return offset;\n return region.start + ((offset - region.start) % (region.end - region.start));\n}\n"]}
@@ -1,59 +1,27 @@
1
1
  import type { AssetCatalog } from "@motionscript/core";
2
2
  import type { AudioMixer, ScheduledAudioRequest } from "../frame-loop";
3
- /** Sample rate used when the caller doesn't pick one. */
4
3
  export declare const DEFAULT_SAMPLE_RATE = 44100;
5
4
  export interface MixAudioOptions {
6
- /** Defaults to {@link DEFAULT_SAMPLE_RATE}. */
7
5
  sampleRate?: number;
8
- /**
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
- * fetched as written — right only while the host keys its manifest by URL.
12
- */
6
+ /** Resolves a request's `src` to the address its bytes live at, as the storage adapter does for live paths. Omitted, a `src` is fetched as written, right only while the host keys its manifest by URL. */
13
7
  catalog?: AssetCatalog;
14
8
  }
15
9
  /**
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.
10
+ * Renders every scheduled request into one timeline-length buffer through an `OfflineAudioContext` graph (gain per
11
+ * request, looping/trimming, the same filter chain as the live {@link WebAudioSink}) in one pass. Shared by the video
12
+ * exporter and `mixTimelineAudio` so a split export and a whole one are the same mix. Curve-valued filter params and
13
+ * `speed` curves schedule exactly as in {@link WebAudioSink.playBuffer}, the reference: read the two side by side when
14
+ * either changes. One difference: this schedules at absolute context times, so clip starts are sample-accurate here
15
+ * and quantized to `1/fps` live.
33
16
  */
34
17
  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
- */
18
+ /** Adapter presenting {@link mixAudio} as the exporter's mixer seam. The sample rate is a constructor argument because {@link AudioMixer}, the seam `renderTimeline` calls through, takes exactly two parameters and a third would be silently dropped. */
44
19
  export declare class WebAudioMixer implements AudioMixer<AudioBuffer> {
45
20
  private readonly sampleRate;
46
21
  private readonly catalog?;
47
22
  constructor(sampleRate?: number, catalog?: AssetCatalog | undefined);
48
23
  mix(requests: readonly ScheduledAudioRequest[], totalDuration: number): Promise<AudioBuffer | null>;
49
24
  }
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
- */
25
+ /** Encodes an `AudioBuffer` as a 16-bit PCM WAV: no codec support needed and lossless, and the only other encoder here (AAC in the mp4 muxer) is reachable solely by exporting a video. */
58
26
  export declare function encodeWav(buffer: AudioBuffer): Uint8Array<ArrayBuffer>;
59
27
  //# sourceMappingURL=mixer.d.ts.map
@@ -1 +1 @@
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,EACx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6CtE"}
@@ -1,8 +1,7 @@
1
- import { fetchAssetBytes, localAssetBytes } from "../asset-bytes";
1
+ import { loadAssetBytes } from "../asset-bytes";
2
2
  import { AudioBusGraph } from "./bus-graph";
3
3
  import { applyLoop } from "./loop-region";
4
4
  import { applySpeedToPlaybackRate, buildAudioEffectGraph, buildLevel, playbackOf, sourceOffsetOf } from "./effect-graph";
5
- /** Sample rate used when the caller doesn't pick one. */
6
5
  export const DEFAULT_SAMPLE_RATE = 44100;
7
6
  /** Audio first, then video: a clip's own soundtrack is requested under the video's key. */
8
7
  function audioUrl(src, catalog) {
@@ -20,35 +19,18 @@ function audioUrl(src, catalog) {
20
19
  }
21
20
  }
22
21
  }
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
- */
22
+ /** Reads and decodes an audio source against a scratch `OfflineAudioContext`. The host's byte source is asked first, as for every asset read: a mix that went to the network would fail where the preview played (offline, or bytes not yet uploaded). */
30
23
  async function fetchAudioBuffer(src, ctx) {
31
- const blob = (await localAssetBytes(src)) ?? (await fetchAssetBytes(src));
24
+ const blob = await loadAssetBytes(src);
32
25
  return ctx.decodeAudioData(await blob.arrayBuffer());
33
26
  }
34
27
  /**
35
- * Render every scheduled request into one timeline-length buffer.
36
- *
37
- * An `OfflineAudioContext` graph — gain per request, looping/trimming, and the
38
- * same filter chain the live {@link WebAudioSink} builds — rendered in one
39
- * pass rather than scheduled per frame.
40
- *
41
- * Shared by the video exporter and by `mixTimelineAudio`, deliberately: a split
42
- * export and a whole one have to be the same mix, and two implementations of
43
- * "stack these clips" would eventually disagree.
44
- *
45
- * Curve-valued filter params and `speed` curves schedule here exactly as they do
46
- * in {@link WebAudioSink.playBuffer}, which is the reference implementation —
47
- * the two must be read side by side when either changes.
48
- *
49
- * One deliberate difference: this schedules at absolute context times, so clip
50
- * starts are sample-accurate here and quantized to `1/fps` in live playback,
51
- * which starts sources at `currentTime` on a clock tick.
28
+ * Renders every scheduled request into one timeline-length buffer through an `OfflineAudioContext` graph (gain per
29
+ * request, looping/trimming, the same filter chain as the live {@link WebAudioSink}) in one pass. Shared by the video
30
+ * exporter and `mixTimelineAudio` so a split export and a whole one are the same mix. Curve-valued filter params and
31
+ * `speed` curves schedule exactly as in {@link WebAudioSink.playBuffer}, the reference: read the two side by side when
32
+ * either changes. One difference: this schedules at absolute context times, so clip starts are sample-accurate here
33
+ * and quantized to `1/fps` live.
52
34
  */
53
35
  export async function mixAudio(requests, totalDuration, options = {}) {
54
36
  if (requests.length === 0)
@@ -116,15 +98,7 @@ export async function mixAudio(requests, totalDuration, options = {}) {
116
98
  }
117
99
  return mixCtx.startRendering();
118
100
  }
119
- /**
120
- * Adapter presenting {@link mixAudio} as the exporter's injected mixer seam.
121
- *
122
- * The sample rate is a constructor argument rather than a third `mix` parameter
123
- * because {@link AudioMixer} — the seam `renderTimeline` calls through — takes
124
- * exactly two, so anything passed as a third would be silently dropped. Fixing
125
- * it here rather than widening the interface keeps the rate a property of *this*
126
- * mixer, which is whose business it is.
127
- */
101
+ /** Adapter presenting {@link mixAudio} as the exporter's mixer seam. The sample rate is a constructor argument because {@link AudioMixer}, the seam `renderTimeline` calls through, takes exactly two parameters and a third would be silently dropped. */
128
102
  export class WebAudioMixer {
129
103
  sampleRate;
130
104
  catalog;
@@ -139,14 +113,7 @@ export class WebAudioMixer {
139
113
  });
140
114
  }
141
115
  }
142
- /**
143
- * Encode an `AudioBuffer` as a 16-bit PCM WAV.
144
- *
145
- * The only other encoder in this package is the AAC track inside the mp4 muxer,
146
- * which is reachable solely by exporting a video — so without this a mixdown has
147
- * no file representation at all. WAV because it needs no codec support and is
148
- * lossless; a host wanting mp3/aac can re-encode from here.
149
- */
116
+ /** Encodes an `AudioBuffer` as a 16-bit PCM WAV: no codec support needed and lossless, and the only other encoder here (AAC in the mp4 muxer) is reachable solely by exporting a video. */
150
117
  export function encodeWav(buffer) {
151
118
  const channelCount = buffer.numberOfChannels;
152
119
  const frameCount = buffer.length;
@@ -1 +1 @@
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;AAC5C,OAAO,EAAE,SAAS,EAAE,MAAM,eAAe,CAAC;AAC1C,OAAO,EAAE,wBAAwB,EAAE,qBAAqB,EAAE,UAAU,EAAE,UAAU,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAC;AAEzH,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,CAAC;QACD,OAAO,OAAO,CAAC,YAAY,CAAC,GAAG,CAAC,CAAC,GAAG,IAAI,GAAG,CAAC;IAChD,CAAC;IAAC,MAAM,CAAC;QACL,IAAI,CAAC;YACD,OAAO,OAAO,CAAC,YAAY,CAAC,GAAG,CAAC,CAAC,GAAG,IAAI,GAAG,CAAC;QAChD,CAAC;QAAC,MAAM,CAAC;YACL,OAAO,GAAG,CAAC;QACf,CAAC;IACL,CAAC;AACL,CAAC;AAED;;;;;;GAMG;AACH,KAAK,UAAU,gBAAgB,CAAC,GAAW,EAAE,GAAwB;IACjE,MAAM,IAAI,GAAG,CAAC,MAAM,eAAe,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,MAAM,eAAe,CAAC,GAAG,CAAC,CAAC,CAAC;IAC1E,OAAO,GAAG,CAAC,eAAe,CAAC,MAAM,IAAI,CAAC,WAAW,EAAE,CAAC,CAAC;AACzD,CAAC;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH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AAM,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"]}
1
+ {"version":3,"file":"mixer.js","source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Bytes } from \"../asset-bytes\";\nimport { AudioBusGraph } from \"./bus-graph\";\nimport { applyLoop } from \"./loop-region\";\nimport { applySpeedToPlaybackRate, buildAudioEffectGraph, buildLevel, playbackOf, sourceOffsetOf } from \"./effect-graph\";\n\nexport const DEFAULT_SAMPLE_RATE = 44100;\n\nexport interface MixAudioOptions {\n sampleRate?: number;\n /** Resolves a request's `src` to the address its bytes live at, as the storage adapter does for live paths. Omitted, a `src` is fetched as written, right only while the host keys its manifest by URL. */\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/** Reads and decodes an audio source against a scratch `OfflineAudioContext`. The host's byte source is asked first, as for every asset read: a mix that went to the network would fail where the preview played (offline, or bytes not yet uploaded). */\nasync function fetchAudioBuffer(src: string, ctx: OfflineAudioContext): Promise<AudioBuffer> {\n const blob = await loadAssetBytes(src);\n return ctx.decodeAudioData(await blob.arrayBuffer());\n}\n\n/**\n * Renders every scheduled request into one timeline-length buffer through an `OfflineAudioContext` graph (gain per\n * request, looping/trimming, the same filter chain as the live {@link WebAudioSink}) in one pass. Shared by the video\n * exporter and `mixTimelineAudio` so a split export and a whole one are the same mix. Curve-valued filter params and\n * `speed` curves schedule exactly as in {@link WebAudioSink.playBuffer}, the reference: read the two side by side when\n * either changes. One difference: this schedules at absolute context times, so clip starts are sample-accurate here\n * and quantized to `1/fps` live.\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/** Adapter presenting {@link mixAudio} as the exporter's mixer seam. The sample rate is a constructor argument because {@link AudioMixer}, the seam `renderTimeline` calls through, takes exactly two parameters and a third would be silently dropped. */\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/** Encodes an `AudioBuffer` as a 16-bit PCM WAV: no codec support needed and lossless, and the only other encoder here (AAC in the mp4 muxer) is reachable solely by exporting a video. */\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,11 +1,5 @@
1
1
  import { AudioSink, AudioRequest } from "@motionscript/core";
2
- /**
3
- * Web Audio implementation of {@link AudioSink} — decodes and caches source
4
- * buffers, then schedules/plays {@link AudioRequest}s as `AudioBufferSourceNode`s
5
- * through a shared master gain (global mute and volume). `schedule` registers the
6
- * active set of requests for the current frame; `syncTo` then starts/stops
7
- * sources to match `clipTime`, so playback follows scrubbing and seeks.
8
- */
2
+ /** Web Audio implementation of {@link AudioSink}: decodes and caches source buffers, then plays {@link AudioRequest}s as `AudioBufferSourceNode`s through a shared master gain (global mute and volume). `syncTo` starts/stops sources to match `clipTime`, so playback follows scrubbing and seeks. */
9
3
  export declare class WebAudioSink extends AudioSink {
10
4
  private readonly borrowed;
11
5
  private _context;
@@ -19,19 +13,11 @@ export declare class WebAudioSink extends AudioSink {
19
13
  private muted;
20
14
  private volume;
21
15
  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
- */
16
+ /** Created on first use rather than with the sink: a new AudioContext opens the audio device (50–65 ms on desktop), which a session that never plays never needs. */
27
17
  getContext(): AudioContext;
28
18
  private get context();
29
19
  private get graph();
30
- /**
31
- * Resume the AudioContext. Must be called from within a user gesture
32
- * (e.g. a click handler) the first time, or browsers keep the context
33
- * suspended and no audio is heard.
34
- */
20
+ /** Resumes the AudioContext; the first call must come from a user gesture or browsers keep it suspended and nothing is heard. */
35
21
  unlock(): void;
36
22
  has(src: string): boolean;
37
23
  append(src: string, data: ArrayBuffer): Promise<void>;
@@ -1 +1 @@
1
- {"version":3,"file":"player.d.ts","sourceRoot":"","sources":["../../src/audio/player.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAc7D;;;;;;GAMG;AACH,qBAAa,YAAa,SAAQ,SAAS;IACvC,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAA2B;IACpD,OAAO,CAAC,QAAQ,CAA6B;IAC7C,OAAO,CAAC,WAAW,CAAkC;IACrD,OAAO,CAAC,QAAQ,CAAoC;IACpD,OAAO,CAAC,MAAM,CAAmC;IACjD,OAAO,CAAC,SAAS,CAAsB;IACvC,OAAO,CAAC,QAAQ,CAAkB;IAClC,iHAAiH;IACjH,OAAO,CAAC,MAAM,CAA2D;IACzE,OAAO,CAAC,KAAK,CAAS;IACtB,OAAO,CAAC,MAAM,CAAK;gBAEP,OAAO,CAAC,EAAE,YAAY;IAKlC;;;;OAIG;IACH,UAAU,IAAI,YAAY;IAK1B,OAAO,KAAK,OAAO,GAElB;IAED,OAAO,KAAK,KAAK,GAQhB;IAED;;;;OAIG;IACH,MAAM,IAAI,IAAI;IAMd,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO;IAInB,MAAM,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,WAAW,GAAG,OAAO,CAAC,IAAI,CAAC;IAkB3D,uKAAuK;IACvK,MAAM,CAAC,IAAI,EAAE,WAAW,CAAC,MAAM,CAAC,GAAG,IAAI;IASvC,qKAAqK;IACrK,QAAQ,CAA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AO;IAInB,MAAM,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,WAAW,GAAG,OAAO,CAAC,IAAI,CAAC;IAe3D,uKAAuK;IACvK,MAAM,CAAC,IAAI,EAAE,WAAW,CAAC,MAAM,CAAC,GAAG,IAAI;IASvC,qKAAqK;IACrK,QAAQ,CAAC,QAAQ,EAAE,SAAS,YAAY,EAAE,GAAG,IAAI;IAQjD,iLAAiL;IACjL,MAAM,CAAC,QAAQ,EAAE,MAAM,GAAG,IAAI;IAqBxB,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;IAYlB,OAAO,IAAI,IAAI;CAiBlB"}
@@ -2,13 +2,7 @@ import { AudioSink } from "@motionscript/core";
2
2
  import { AudioBusGraph } from "./bus-graph";
3
3
  import { applyLoop, wrapIntoRegion } from "./loop-region";
4
4
  import { applySpeedToPlaybackRate, buildAudioEffectGraph, buildLevel, playbackOf, sourceOffsetOf } from "./effect-graph";
5
- /**
6
- * Web Audio implementation of {@link AudioSink} — decodes and caches source
7
- * buffers, then schedules/plays {@link AudioRequest}s as `AudioBufferSourceNode`s
8
- * through a shared master gain (global mute and volume). `schedule` registers the
9
- * active set of requests for the current frame; `syncTo` then starts/stops
10
- * sources to match `clipTime`, so playback follows scrubbing and seeks.
11
- */
5
+ /** Web Audio implementation of {@link AudioSink}: decodes and caches source buffers, then plays {@link AudioRequest}s as `AudioBufferSourceNode`s through a shared master gain (global mute and volume). `syncTo` starts/stops sources to match `clipTime`, so playback follows scrubbing and seeks. */
12
6
  export class WebAudioSink extends AudioSink {
13
7
  borrowed;
14
8
  _context = null;
@@ -25,11 +19,7 @@ export class WebAudioSink extends AudioSink {
25
19
  super();
26
20
  this.borrowed = context;
27
21
  }
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
- */
22
+ /** Created on first use rather than with the sink: a new AudioContext opens the audio device (50–65 ms on desktop), which a session that never plays never needs. */
33
23
  getContext() {
34
24
  this._context ??= this.borrowed ?? new AudioContext();
35
25
  return this._context;
@@ -46,11 +36,7 @@ export class WebAudioSink extends AudioSink {
46
36
  }
47
37
  return this._graph;
48
38
  }
49
- /**
50
- * Resume the AudioContext. Must be called from within a user gesture
51
- * (e.g. a click handler) the first time, or browsers keep the context
52
- * suspended and no audio is heard.
53
- */
39
+ /** Resumes the AudioContext; the first call must come from a user gesture or browsers keep it suspended and nothing is heard. */
54
40
  unlock() {
55
41
  if (this.context.state === "suspended") {
56
42
  this.context.resume();
@@ -65,10 +51,7 @@ export class WebAudioSink extends AudioSink {
65
51
  const existing = this.decoding.get(src);
66
52
  if (existing)
67
53
  return existing;
68
- // decodeAudioData detaches `data` — that's fine, the caller in
69
- // AssetManager.fetchAudio discards its reference immediately after
70
- // awaiting append. Skipping the defensive .slice(0) saves a multi-MB
71
- // allocation per audio source.
54
+ // decodeAudioData detaches `data`; the AssetManager caller discards its reference after awaiting append, so skipping a defensive .slice(0) saves a multi-MB allocation per source.
72
55
  const job = this.context.decodeAudioData(data).then((buffer) => {
73
56
  this.bufferCache.set(src, buffer);
74
57
  }).finally(() => {
@@ -189,7 +172,6 @@ export class WebAudioSink extends AudioSink {
189
172
  node.disconnect();
190
173
  }
191
174
  catch {
192
- // already stopped
193
175
  }
194
176
  this.active.delete(id);
195
177
  }
@@ -206,7 +188,6 @@ export class WebAudioSink extends AudioSink {
206
188
  this._graph?.master.disconnect();
207
189
  }
208
190
  catch {
209
- // already disconnected
210
191
  }
211
192
  const owned = this.borrowed ? null : this._context;
212
193
  if (owned && owned.state !== "closed") {