Write HTML. Render video. Built for agents.
JavaScript aiai-agentsanimationclaude-codeffmpegframeworkgsaphtmlhtml-to-videomcpmotion-graphicspuppeteerrenderingtypescriptvideo-editor
feat(audio): play automation envelopes in preview and bake them at render (#3016)
Vance Ingalls23ba58cfd33a
diff --git a/packages/core/src/audio/audioFxAutomation.test.ts b/packages/core/src/audio/audioFxAutomation.test.tsnew file mode 100644index 0000000000..d8748f9f45--- /dev/null+++ b/packages/core/src/audio/audioFxAutomation.test.ts@@ -0,0 +1,353 @@+// @vitest-environment happy-dom+import { describe, expect, it } from "vitest";+import {+ cancelParamLane,+ scheduleChainAutomation,+ scheduleParamLane,+ volumeLane,+ type AutomationTiming,+} from "./audioFxAutomation.js";+import type { FxParamTarget } from "./audioFxGraph.js";+import type { HfAutomationLane } from "../audioAutomation.js";+import type { HfAudioFxChain } from "../audioFx.js";++/**+ * happy-dom has no Web Audio. These record what the scheduler asks an+ * AudioParam to do — the shape of the envelope handed to the audio thread —+ * which is the part that has to be right; whether Chrome then plays a ramp+ * accurately is not ours to test.+ */+type Call =+ | { op: "set"; value: number; time: number }+ | { op: "ramp"; value: number; time: number }+ | { op: "curve"; values: number[]; time: number; duration: number }+ | { op: "cancel"; time: number }+ | { op: "hold"; time: number };++class FakeParam {+ calls: Call[] = [];+ value = 0;+ setValueAtTime(value: number, time: number): void {+ this.calls.push({ op: "set", value, time });+ }+ linearRampToValueAtTime(value: number, time: number): void {+ this.calls.push({ op: "ramp", value, time });+ }+ setValueCurveAtTime(values: Float32Array, time: number, duration: number): void {+ this.calls.push({ op: "curve", values: Array.from(values), time, duration });+ }+ cancelScheduledValues(time: number): void {+ this.calls.push({ op: "cancel", time });+ }+ cancelAndHoldAtTime(time: number): void {+ this.calls.push({ op: "hold", time });+ }+}++const fake = (): { target: FxParamTarget; param: FakeParam } => {+ const param = new FakeParam();+ return { target: { param: param as unknown as AudioParam }, param };+};++const at = (elapsed = 0, rate = 1, scheduledAt = 10): AutomationTiming => ({+ scheduledAt,+ elapsed,+ rate,+});++const ramp: HfAutomationLane = {+ target: "volume",+ points: [+ { t: 1, v: 0.2 },+ { t: 3, v: 0.8 },+ ],+};++describe("scheduleParamLane", () => {+ it("seeds the current value, then ramps to each later point", () => {+ const { target, param } = fake();+ scheduleParamLane([target], ramp, "linear", at(0));+ expect(param.calls).toEqual([+ { op: "cancel", time: 10 },+ // Before the first point the envelope holds that point's value.+ { op: "set", value: 0.2, time: 10 },+ { op: "ramp", value: 0.8, time: 13 },+ ]);+ });++ it("enters a segment mid-way when the playhead landed inside it", () => {+ const { target, param } = fake();+ scheduleParamLane([target], ramp, "linear", at(2));+ // Half way along a 0.2 → 0.8 ramp.+ expect(param.calls[1]).toEqual({ op: "set", value: 0.5, time: 10 });+ expect(param.calls[2]).toEqual({ op: "ramp", value: 0.8, time: 11 });+ });++ it("holds the last value once the envelope is behind the playhead", () => {+ const { target, param } = fake();+ scheduleParamLane([target], ramp, "linear", at(9));+ expect(param.calls).toEqual([+ { op: "cancel", time: 10 },+ { op: "set", value: 0.8, time: 10 },+ ]);+ });++ it("waits for a clip that has not started yet", () => {+ const { target, param } = fake();+ scheduleParamLane([target], ramp, "linear", at(-2));+ // Clip time 1 is three seconds of context time away.+ expect(param.calls[1]).toEqual({ op: "set", value: 0.2, time: 10 });+ expect(param.calls[2]).toEqual({ op: "ramp", value: 0.8, time: 15 });+ });++ it("compresses the envelope by the playback rate", () => {+ const { target, param } = fake();+ scheduleParamLane([target], ramp, "linear", at(0, 2));+ // Clip time 3 arrives after 1.5 s of context time at double speed.+ expect(param.calls[2]).toEqual({ op: "ramp", value: 0.8, time: 11.5 });+ });++ it("sets a constant lane once instead of scheduling it", () => {+ const { target, param } = fake();+ scheduleParamLane(+ [target],+ {+ target: "volume",+ points: [+ { t: 0, v: 0.4 },+ { t: 5, v: 0.4 },+ ],+ },+ "linear",+ at(0),+ );+ expect(param.calls).toEqual([+ { op: "cancel", time: 10 },+ { op: "set", value: 0.4, time: 10 },+ ]);+ });++ it("samples a curved segment rather than ramping through the bend", () => {+ const { target, param } = fake();+ scheduleParamLane(+ [target],+ {+ target: "volume",+ points: [+ { t: 0, v: 0, curve: 1 },+ { t: 2, v: 1 },+ ],+ },+ "linear",+ at(0),+ );+ const curve = param.calls.find((c) => c.op === "curve");+ expect(curve).toBeTruthy();+ if (curve?.op !== "curve") throw new Error("expected a curve");+ expect(curve.time).toBe(10);+ expect(curve.duration).toBe(2);+ expect(curve.values[0]).toBeCloseTo(0, 6);+ expect(curve.values[curve.values.length - 1]).toBeCloseTo(1, 6);+ // Curved, so the midpoint is not halfway.+ expect(curve.values[Math.floor(curve.values.length / 2)]).toBeLessThan(0.4);+ });++ it("samples a log-scaled sweep, which a linear ramp would get wrong", () => {+ const { target, param } = fake();+ scheduleParamLane(+ [target],+ {+ target: "fx.n1.frequency",+ points: [+ { t: 0, v: 200 },+ { t: 2, v: 8000 },+ ],+ },+ "log",+ at(0),+ );+ const curve = param.calls.find((c) => c.op === "curve");+ if (curve?.op !== "curve") throw new Error("expected a curve");+ // Halfway is the geometric mean, not the arithmetic one. The array has an+ // even length so no sample sits exactly on it; both neighbours bracket it.+ const half = curve.values.length / 2;+ const geometric = Math.sqrt(200 * 8000);+ expect(curve.values[half - 1] ?? 0).toBeLessThan(geometric);+ expect(curve.values[half] ?? 0).toBeGreaterThan(geometric);+ expect((curve.values[half] ?? 0) / geometric).toBeCloseTo(1, 1);+ // A linear ramp would have been at 4100 by now — nowhere near.+ expect(curve.values[half] ?? 0).toBeLessThan(2000);+ });++ it("does not seed on top of a curve that starts at the same instant", () => {+ const { target, param } = fake();+ scheduleParamLane(+ [target],+ {+ target: "volume",+ points: [+ { t: 0, v: 0, curve: 1 },+ { t: 2, v: 1 },+ ],+ },+ "linear",+ at(0),+ );+ // A value curve may not overlap another event, so there is no set at 10.+ expect(param.calls.filter((c) => c.op === "set")).toEqual([]);+ expect(param.calls[1]?.op).toBe("curve");+ });++ it("writes every AudioParam behind one knob, each through its own mapping", () => {+ const wet = new FakeParam();+ const dry = new FakeParam();+ scheduleParamLane(+ [+ { param: wet as unknown as AudioParam },+ { param: dry as unknown as AudioParam, map: (v) => 1 - v },+ ],+ {+ target: "fx.n1.mix",+ points: [+ { t: 0, v: 0.25 },+ { t: 4, v: 0.75 },+ ],+ },+ "linear",+ at(0),+ );+ expect(wet.calls[1]).toEqual({ op: "set", value: 0.25, time: 10 });+ // The dry side moves the opposite way. A mapping may be non-linear, so the+ // segment is sampled rather than ramped.+ const dryCurve = dry.calls.find((c) => c.op === "curve");+ if (dryCurve?.op !== "curve") throw new Error("expected a curve");+ expect(dryCurve.values[0]).toBeCloseTo(0.75, 6);+ expect(dryCurve.values[dryCurve.values.length - 1]).toBeCloseTo(0.25, 6);+ });++ it("ignores an empty lane", () => {+ const { target, param } = fake();+ scheduleParamLane([target], { target: "volume", points: [] }, "linear", at(0));+ expect(param.calls).toEqual([]);+ });+});++describe("cancelParamLane", () => {+ it("holds the value the envelope had reached rather than snapping back", () => {+ const { target, param } = fake();+ cancelParamLane([target], 12);+ expect(param.calls).toEqual([{ op: "hold", time: 12 }]);+ });+});++describe("scheduleChainAutomation", () => {+ const chain: HfAudioFxChain = {+ version: 1,+ nodes: [{ type: "peaking", id: "n1", enabled: true, params: {} }],+ };++ const nodeWith = (automation: Record<string, FxParamTarget[]> | undefined) => [+ {+ id: "n1",+ handle: {+ input: {} as AudioNode,+ output: {} as AudioNode,+ update: () => {},+ dispose: () => {},+ automation,+ },+ },+ ];++ it("routes a lane to the AudioParam its node exposes", () => {+ const param = new FakeParam();+ const nodes = nodeWith({ frequency: [{ param: param as unknown as AudioParam }] });+ const scheduled = scheduleChainAutomation(+ {+ version: 1,+ lanes: [+ {+ target: "fx.n1.frequency",+ points: [+ { t: 0, v: 200 },+ { t: 2, v: 8000 },+ ],+ },+ ],+ },+ chain,+ nodes,+ at(0),+ );+ expect(scheduled.length).toBe(1);+ expect(param.calls.length).toBeGreaterThan(1);+ });++ it("skips a lane whose node exposes nothing, instead of failing", () => {+ const scheduled = scheduleChainAutomation(+ {+ version: 1,+ lanes: [+ {+ target: "fx.n1.frequency",+ points: [+ { t: 0, v: 200 },+ { t: 2, v: 900 },+ ],+ },+ ],+ },+ chain,+ nodeWith(undefined),+ at(0),+ );+ expect(scheduled).toEqual([]);+ });++ it("skips a lane addressed to a node that is not in the chain", () => {+ const param = new FakeParam();+ const scheduled = scheduleChainAutomation(+ {+ version: 1,+ lanes: [+ {+ target: "fx.gone.frequency",+ points: [+ { t: 0, v: 200 },+ { t: 2, v: 900 },+ ],+ },+ ],+ },+ chain,+ nodeWith({ frequency: [{ param: param as unknown as AudioParam }] }),+ at(0),+ );+ expect(scheduled).toEqual([]);+ expect(param.calls).toEqual([]);+ });++ it("leaves the volume lane alone — the transport owns the fader", () => {+ const param = new FakeParam();+ const automation = {+ version: 1,+ lanes: [+ {+ target: "volume",+ points: [+ { t: 0, v: 1 },+ { t: 2, v: 0 },+ ],+ },+ ],+ };+ const scheduled = scheduleChainAutomation(+ automation,+ chain,+ nodeWith({ frequency: [{ param: param as unknown as AudioParam }] }),+ at(0),+ );+ expect(scheduled).toEqual([]);+ expect(volumeLane(automation)?.points.length).toBe(2);+ });+});diff --git a/packages/core/src/audio/audioFxAutomation.ts b/packages/core/src/audio/audioFxAutomation.tsnew file mode 100644index 0000000000..766c14ece8--- /dev/null+++ b/packages/core/src/audio/audioFxAutomation.ts@@ -0,0 +1,213 @@+/**+ * Plays automation envelopes through native AudioParam scheduling.+ *+ * Nothing here runs per frame. The envelope is handed to the audio thread once,+ * as ramps and curves, so it stays sample-accurate no matter what the main+ * thread is doing — and the offline render schedules it the same way, which is+ * why preview and render agree by construction rather than by tolerance.+ */++import {+ isConstantLane,+ resolveAutomationRange,+ sampleAutomationCurve,+ sampleAutomationLane,+ type HfAutomation,+ type HfAutomationLane,+} from "../audioAutomation.js";+import { parseAutomationTarget, VOLUME_TARGET } from "../audioAutomation.js";+import type { HfAudioFxChain } from "../audioFx.js";+import type { FxNodeHandle, FxParamTarget } from "./audioFxGraph.js";++/** Where the clip sits in context time when its source is scheduled. */+export interface AutomationTiming {+ /** Context time the source was scheduled at. */+ scheduledAt: number;+ /** Clip-local seconds already elapsed at that moment; negative before it starts. */+ elapsed: number;+ /** Playback rate, which compresses clip seconds into context seconds. */+ rate: number;+}++/**+ * Sampling density for segments that cannot be expressed as a ramp. Dense+ * enough that a curve is inaudible from a continuous one, capped so a long+ * segment cannot allocate without bound.+ */+const CURVE_POINTS_PER_SEC = 100;+const MIN_CURVE_POINTS = 8;+const MAX_CURVE_POINTS = 4096;++const identity = (v: number): number => v;++type Op =+ | { kind: "set"; time: number; value: number }+ | { kind: "ramp"; time: number; value: number }+ | { kind: "curve"; time: number; duration: number; values: Float32Array };++/**+ * A segment is a straight line only when nothing bends it: no curvature on the+ * point it leaves, a linear parameter scale, and no unit mapping that could be+ * non-linear. Everything else is sampled.+ */+function isStraight(+ curve: number | undefined,+ scale: "linear" | "log",+ map: FxParamTarget["map"],+): boolean {+ return !curve && scale === "linear" && !map;+}++function curvePointCount(duration: number): number {+ const wanted = Math.ceil(duration * CURVE_POINTS_PER_SEC);+ return Math.min(MAX_CURVE_POINTS, Math.max(MIN_CURVE_POINTS, wanted));+}++/**+ * One segment of the envelope, as the event that plays it. Returns null when+ * the segment is already behind the playhead.+ */+function segmentOp(+ lane: HfAutomationLane,+ index: number,+ scale: "linear" | "log",+ timing: AutomationTiming,+ map: FxParamTarget["map"],+): Op | null {+ const { scheduledAt, elapsed, rate } = timing;+ const apply = map ?? identity;+ const toCtx = (t: number): number => scheduledAt + (t - elapsed) / rate;+ const a = lane.points[index]!;+ const b = lane.points[index + 1]!;++ const toTime = toCtx(b.t);+ if (toTime <= scheduledAt) return null;+ const fromTime = Math.max(scheduledAt, toCtx(a.t));+ const duration = toTime - fromTime;+ if (duration <= 0) return null;++ if (isStraight(a.curve, scale, map)) {+ return { kind: "ramp", time: toTime, value: apply(b.v) };+ }+ // Sample from wherever the segment is entered, which is mid-segment when the+ // playhead landed inside it.+ const count = curvePointCount(duration);+ const raw = sampleAutomationCurve(lane, Math.max(a.t, elapsed), b.t, count, scale);+ const values = new Float32Array(count);+ for (let k = 0; k < count; k += 1) values[k] = apply(raw[k] ?? 0);+ return { kind: "curve", time: fromTime, duration, values };+}++/** Turn a lane into the events one AudioParam should receive. */+function planOps(+ lane: HfAutomationLane,+ scale: "linear" | "log",+ timing: AutomationTiming,+ map: FxParamTarget["map"],+): Op[] {+ const ops: Op[] = [];+ for (let i = 0; i + 1 < lane.points.length; i += 1) {+ const op = segmentOp(lane, i, scale, timing, map);+ if (op) ops.push(op);+ }++ // Hold the envelope's value at the moment playback starts. This is what makes+ // a lane correct before its first point and after its last, and it gives a+ // following ramp something to ramp from.+ //+ // A value curve may not overlap another event, so the seed is dropped when a+ // curve already begins at that instant — the curve sets the value itself.+ const first = ops[0];+ if (!(first?.kind === "curve" && first.time <= timing.scheduledAt)) {+ ops.unshift({+ kind: "set",+ time: timing.scheduledAt,+ value: (map ?? identity)(sampleAutomationLane(lane, timing.elapsed, scale)),+ });+ }+ return ops;+}++function emit(param: AudioParam, ops: readonly Op[]): void {+ for (const op of ops) {+ if (op.kind === "set") param.setValueAtTime(op.value, op.time);+ else if (op.kind === "ramp") param.linearRampToValueAtTime(op.value, op.time);+ else param.setValueCurveAtTime(op.values, op.time, op.duration);+ }+}++/**+ * Schedule one lane onto every AudioParam behind its knob.+ *+ * Existing automation from an earlier schedule is cancelled first, so calling+ * this again after an edit replaces the envelope rather than layering on it.+ */+export function scheduleParamLane(+ targets: readonly FxParamTarget[],+ lane: HfAutomationLane,+ scale: "linear" | "log",+ timing: AutomationTiming,+): void {+ if (lane.points.length === 0) return;+ for (const target of targets) {+ target.param.cancelScheduledValues(timing.scheduledAt);+ if (isConstantLane(lane)) {+ const value = (target.map ?? identity)(lane.points[0]!.v);+ target.param.setValueAtTime(value, timing.scheduledAt);+ continue;+ }+ emit(target.param, planOps(lane, scale, timing, target.map));+ }+}++/** Stop an envelope, leaving the parameter wherever it currently sits. */+export function cancelParamLane(targets: readonly FxParamTarget[], from: number): void {+ for (const target of targets) {+ // Keep the value the ramp had reached rather than snapping back to the+ // last explicitly-set one, which would click.+ if (typeof target.param.cancelAndHoldAtTime === "function") {+ target.param.cancelAndHoldAtTime(from);+ } else {+ target.param.cancelScheduledValues(from);+ }+ }+}++/** One built effect, paired with the chain node id lanes address it by. */+export interface AutomatableNode {+ id?: string;+ handle: FxNodeHandle;+}++/**+ * Schedule every FX lane in an automation set against a built chain.+ *+ * Lanes with nowhere to write are skipped rather than reported: a one-pole+ * filter exposes no frequency param, and a worklet effect exposes none at all.+ * Returns the targets that were scheduled, so they can be cancelled together.+ */+export function scheduleChainAutomation(+ automation: HfAutomation,+ chain: HfAudioFxChain,+ nodes: readonly AutomatableNode[],+ timing: AutomationTiming,+): FxParamTarget[] {+ const byId = new Map(nodes.filter((n) => n.id).map((n) => [n.id as string, n.handle]));+ const scheduled: FxParamTarget[] = [];+ for (const lane of automation.lanes) {+ const parsed = parseAutomationTarget(lane.target);+ if (!parsed || parsed.kind !== "fx") continue;+ const targets = byId.get(parsed.nodeId)?.automation?.[parsed.param];+ if (!targets || targets.length === 0) continue;+ const range = resolveAutomationRange(lane.target, chain);+ if (!range) continue;+ scheduleParamLane(targets, lane, range.scale, timing);+ scheduled.push(...targets);+ }+ return scheduled;+}++/** The track's volume lane, if it has one. */+export function volumeLane(automation: HfAutomation): HfAutomationLane | null {+ return automation.lanes.find((lane) => lane.target === VOLUME_TARGET) ?? null;+}diff --git a/packages/core/src/audio/audioFxGraph.ts b/packages/core/src/audio/audioFxGraph.tsindex dc3f5a7111..8c2ce8dfb1 100644--- a/packages/core/src/audio/audioFxGraph.ts+++ b/packages/core/src/audio/audioFxGraph.ts@@ -435,6 +435,8 @@ export function buildFxNode(export interface FxChainHandle {input: AudioNode;output: AudioNode;+ /** Built effects in chain order, carrying the node ids lanes address. */+ nodes: { id?: string; type: string; handle: FxNodeHandle }[];/** Re-parameterise in place when the shape is unchanged; false if a rebuild is needed. */update(chain: HfAudioFxChain): boolean;dispose(): void;@@ -469,7 +471,7 @@ function shapeOf(chain: HfAudioFxChain): string {export function buildFxChain(ctx: BaseAudioContext, chain: HfAudioFxChain): FxChainHandle {const input = ctx.createGain();const output = ctx.createGain();- const handles: { type: string; handle: FxNodeHandle }[] = [];+ const handles: { id?: string; type: string; handle: FxNodeHandle }[] = [];let tail: AudioNode = input;for (const node of chain.nodes) {@@ -477,7 +479,7 @@ export function buildFxChain(ctx: BaseAudioContext, chain: HfAudioFxChain): FxChconst handle = buildFxNode(ctx, node.type, node.params ?? {});tail.connect(handle.input);tail = handle.output;- handles.push({ type: node.type, handle });+ handles.push({ ...(node.id ? { id: node.id } : {}), type: node.type, handle });}tail.connect(output);@@ -486,6 +488,7 @@ export function buildFxChain(ctx: BaseAudioContext, chain: HfAudioFxChain): FxChreturn {input,output,+ nodes: handles,update(next) {if (shapeOf(next) !== shape) return false;const active = next.nodes.filter((node) => node.enabled !== false);diff --git a/packages/core/src/runtime/audioFx.ts b/packages/core/src/runtime/audioFx.tsindex 0c6290a5e3..224692d597 100644--- a/packages/core/src/runtime/audioFx.ts+++ b/packages/core/src/runtime/audioFx.ts@@ -11,6 +11,18 @@*/import { HF_AUDIO_FX_ATTR, parseAudioFxChain, type HfAudioFxChain } from "../audioFx.js";+import {+ HF_AUDIO_AUTOMATION_ATTR,+ parseAutomation,+ resolveAutomation,+ type HfAutomation,+} from "../audioAutomation.js";+import {+ cancelParamLane,+ scheduleChainAutomation,+ type AutomationTiming,+} from "../audio/audioFxAutomation.js";+import type { FxParamTarget } from "../audio/audioFxGraph.js";import {audioFxWorkletsReady,buildFxChain,@@ -20,6 +32,24 @@ import {import type { FxChainHandle } from "../audio/audioFxGraph.js";const EMPTY: HfAudioFxChain = { version: 1, nodes: [] };+const NO_AUTOMATION: HfAutomation = { version: 1, lanes: [] };++function readAutomation(+ el: { getAttribute?(name: string): string | null },+ chain: HfAudioFxChain,+): HfAutomation {+ const raw =+ (typeof el.getAttribute === "function" ? el.getAttribute(HF_AUDIO_AUTOMATION_ATTR) : null) ??+ "";+ if (!raw) return NO_AUTOMATION;+ try {+ return resolveAutomation(parseAutomation(raw), chain);+ } catch {+ // Unreadable automation plays the track flat rather than silencing it,+ // matching how an unreadable chain plays dry. The render refuses instead.+ return NO_AUTOMATION;+ }+}function readChain(el: { getAttribute?(name: string): string | null }): {chain: HfAudioFxChain;@@ -38,6 +68,18 @@ function readChain(el: { getAttribute?(name: string): string | null }): {}}+/**+ * An element's automation lanes, bound to whatever chain it carries.+ *+ * FX lanes need the chain to resolve their target's range, so they are dropped+ * for an element with no chain; a volume lane is always readable.+ */+export function readElementAutomation(el: {+ getAttribute?(name: string): string | null;+}): HfAutomation {+ return readAutomation(el, readChain(el).chain);+}+/*** Splice an element's FX chain between a decoded source and its gain stage.*@@ -48,12 +90,16 @@ function readChain(el: { getAttribute?(name: string): string | null }): {** Returns null when the element carries no chain, leaving the original* source-to-gain connection in place.+ *+ * With `timing`, the element's automation lanes are scheduled onto the built+ * effects as AudioParam ramps, and rescheduled when the attribute is edited.*/export function attachElementFxChain(ctx: BaseAudioContext,el: { getAttribute?(name: string): string | null },source: AudioNode,destination: AudioNode,+ timing?: AutomationTiming,): { dispose(): void } | null {const { chain } = readChain(el);if (chain.nodes.length === 0) {@@ -103,6 +149,31 @@ export function attachElementFxChain(source.connect(handle.input);handle.output.connect(destination);+ let automated: FxParamTarget[] = timing+ ? scheduleChainAutomation(readAutomation(el, chain), chain, handle.nodes, timing)+ : [];++ /**+ * Re-aim the envelope at the live playhead. An edit lands mid-playback, so+ * the clip has advanced past the offset the source was scheduled with.+ */+ const timingNow = (): AutomationTiming | null => {+ if (!timing) return null;+ const now = typeof ctx.currentTime === "number" ? ctx.currentTime : timing.scheduledAt;+ return {+ scheduledAt: now,+ elapsed: timing.elapsed + (now - timing.scheduledAt) * timing.rate,+ rate: timing.rate,+ };+ };++ const rescheduleAutomation = (next: HfAudioFxChain): void => {+ const at = timingNow();+ if (!at) return;+ cancelParamLane(automated, at.scheduledAt);+ automated = scheduleChainAutomation(readAutomation(el, next), next, handle.nodes, at);+ };+// Follow the attribute while the source plays, so dragging a knob is heard// without rescheduling the track. Values-only changes re-parameterise the// running graph and land on the next 128-sample quantum; a shape change@@ -116,14 +187,24 @@ export function attachElementFxChain() {observer = new MutationObserver(() => {const next = readChain(el);- if (next.chain.nodes.length > 0) handle.update(next.chain);+ if (next.chain.nodes.length === 0) return;+ handle.update(next.chain);+ // Values pushed by `update` would fight a running envelope, so the lanes+ // are re-scheduled on top of them from the current playhead.+ rescheduleAutomation(next.chain);+ });+ observer.observe(target, {+ attributes: true,+ attributeFilter: [HF_AUDIO_FX_ATTR, HF_AUDIO_AUTOMATION_ATTR],});- observer.observe(target, { attributes: true, attributeFilter: [HF_AUDIO_FX_ATTR] });}return {dispose: () => {observer?.disconnect();+ if (automated.length > 0) {+ cancelParamLane(automated, typeof ctx.currentTime === "number" ? ctx.currentTime : 0);+ }handle.dispose();},};diff --git a/packages/core/src/runtime/webAudioTransport.ts b/packages/core/src/runtime/webAudioTransport.tsindex 33d9c93f75..9bbe6071ca 100644--- a/packages/core/src/runtime/webAudioTransport.ts+++ b/packages/core/src/runtime/webAudioTransport.ts@@ -1,4 +1,10 @@-import { attachElementFxChain } from "./audioFx.js";+import { attachElementFxChain, readElementAutomation } from "./audioFx.js";+import {+ scheduleParamLane,+ volumeLane,+ type AutomationTiming,+} from "../audio/audioFxAutomation.js";+import { VOLUME_RANGE } from "../audioAutomation.js";import { swallow } from "./diagnostics";import { getDebugSurface } from "./globals.js";@@ -57,6 +63,20 @@ function startBoundedSource(return true;}+/**+ * The volume lane rides the fader, after the effects — where a DAW puts it,+ * and the order the render bakes it in.+ */+function scheduleVolumeLane(+ el: HTMLMediaElement,+ gainNode: GainNode,+ timing: AutomationTiming,+): void {+ const lane = volumeLane(readElementAutomation(el));+ if (!lane) return;+ scheduleParamLane([{ param: gainNode.gain }], lane, VOLUME_RANGE.scale, timing);+}+export type ScheduledSource = {el: HTMLMediaElement;sourceNode: AudioBufferSourceNode;@@ -188,15 +208,19 @@ export class WebAudioTransport {const gainNode = this._ctx.createGain();gainNode.gain.value = volume;+ const elapsed = compositionTime - compositionStart;+ const scheduledAt = this._ctx.currentTime;+ const timing: AutomationTiming = { scheduledAt, elapsed, rate: safeRate };+// Splice the element's FX chain between the decoded source and its gain,// so effects see the raw signal and volume automation rides on their// output — the same order the offline render uses. Preview and render run// the identical graph builders, so what is heard here is what is written.- const fx = attachElementFxChain(this._ctx, el, sourceNode, gainNode);+ const fx = attachElementFxChain(this._ctx, el, sourceNode, gainNode, timing);gainNode.connect(this._masterGain);- const elapsed = compositionTime - compositionStart;- const scheduledAt = this._ctx.currentTime;+ scheduleVolumeLane(el, gainNode, timing);+this._rate = safeRate;this._rateAnchorCtx = scheduledAt;this._rateAnchorComp = compositionTime;diff --git a/packages/core/stubs/audio-fx-runtime-entry.ts b/packages/core/stubs/audio-fx-runtime-entry.tsindex 4bf75d16a1..668171bb25 100644--- a/packages/core/stubs/audio-fx-runtime-entry.ts+++ b/packages/core/stubs/audio-fx-runtime-entry.ts@@ -7,8 +7,8 @@* ends share a single implementation rather than two that have to be kept in* agreement.*- * Exposes `window.__HF_AUDIO_FX.render(pcm, sampleRate, chain)`, which returns- * the processed samples.+ * Exposes `window.__HF_AUDIO_FX.render(pcm, sampleRate, chain, automation)`,+ * which returns the processed samples.*/import {@@ -16,6 +16,8 @@ import {chainNeedsWorklets,ensureAudioFxWorklets,} from "../src/audio/audioFxGraph.js";+import { scheduleChainAutomation } from "../src/audio/audioFxAutomation.js";+import { parseAutomation, resolveAutomation } from "../src/audioAutomation.js";import { parseAudioFxChain, type HfAudioFxChain } from "../src/audioFx.js";declare global {@@ -25,6 +27,7 @@ declare global {planes: Float32Array[],sampleRate: number,chainJson: string,+ automationJson?: string,): Promise<Float32Array[]>;};}@@ -42,6 +45,7 @@ declare global {* mix, so how far a tail may run past a clip's end is a product decision rather* than something to pick here.*/+/** The clip's audio as an AudioBuffer, a plane per channel. */function toBuffer(ctx: OfflineAudioContext,@@ -71,6 +75,7 @@ async function render(planes: Float32Array[],sampleRate: number,chainJson: string,+ automationJson?: string,): Promise<Float32Array[]> {const chain: HfAudioFxChain = parseAudioFxChain(chainJson);const channels = Math.max(1, planes.length);@@ -83,6 +88,19 @@ async function render(source.buffer = toBuffer(ctx, planes, channels, frames, sampleRate);const fx = buildFxChain(ctx, chain);++ // The input WAV is the clip's own audio from its first sample, so clip-local+ // time is offline time — the envelope needs no offset here. Same scheduler as+ // preview, which is what makes the two agree.+ if (automationJson) {+ const automation = resolveAutomation(parseAutomation(automationJson), chain);+ scheduleChainAutomation(automation, chain, fx.nodes, {+ scheduledAt: 0,+ elapsed: 0,+ rate: 1,+ });+ }+source.connect(fx.input);fx.output.connect(ctx.destination);source.start();diff --git a/packages/engine/src/services/audioAutomationRender.test.ts b/packages/engine/src/services/audioAutomationRender.test.tsnew file mode 100644index 0000000000..b88c1349d1--- /dev/null+++ b/packages/engine/src/services/audioAutomationRender.test.ts@@ -0,0 +1,99 @@+import { describe, expect, it } from "vitest";+import { parseAudioElements, volumeLaneKeyframes } from "./audioMixer.js";+import type { HfAutomationLane } from "@hyperframes/core/audio-automation";++const lanes = (points: HfAutomationLane["points"], target = "volume") => ({+ lanes: [{ target, points }],+});++describe("volumeLaneKeyframes", () => {+ it("keeps a straight fade at two keyframes, in composition time", () => {+ const out = volumeLaneKeyframes(+ lanes([+ { t: 0, v: 1 },+ { t: 2, v: 0 },+ ]),+ 5,+ 2,+ );+ expect(out).toEqual([+ { time: 5, volume: 1 },+ { time: 7, volume: 0 },+ ]);+ });++ it("holds the first value before the envelope starts", () => {+ const out = volumeLaneKeyframes(+ lanes([+ { t: 1, v: 0.3 },+ { t: 2, v: 1 },+ ]),+ 0,+ 2,+ );+ // Not `data-volume` at t=0 — the lane's own first value.+ expect(out?.[0]).toEqual({ time: 0, volume: 0.3 });+ });++ it("holds the last value out to the clip end", () => {+ const out = volumeLaneKeyframes(+ lanes([+ { t: 0, v: 1 },+ { t: 1, v: 0.5 },+ ]),+ 0,+ 4,+ );+ expect(out?.at(-1)).toEqual({ time: 4, volume: 0.5 });+ });++ it("samples a bent segment, which the linear baker would straighten", () => {+ const straight = volumeLaneKeyframes(+ lanes([+ { t: 0, v: 0 },+ { t: 2, v: 1 },+ ]),+ 0,+ 2,+ );+ const bent = volumeLaneKeyframes(+ lanes([+ { t: 0, v: 0, curve: 1 },+ { t: 2, v: 1 },+ ]),+ 0,+ 2,+ );+ expect(straight?.length).toBe(2);+ expect(bent?.length ?? 0).toBeGreaterThan(30);+ // Same endpoints, different path between them.+ expect(bent?.[0]).toEqual({ time: 0, volume: 0 });+ expect(bent?.at(-1)).toEqual({ time: 2, volume: 1 });+ const mid = bent?.find((k) => Math.abs(k.time - 1) < 0.02);+ expect(mid?.volume ?? 1).toBeLessThan(0.4);+ });++ it("has nothing to bake for a track whose only lane is an FX one", () => {+ expect(volumeLaneKeyframes(lanes([{ t: 0, v: 200 }], "fx.n1.frequency"), 0, 2)).toBeNull();+ });+});++describe("parseAudioElements", () => {+ it("carries the automation attribute through to the mixer", () => {+ const automation = '{"version":1,"lanes":[{"target":"volume","points":[{"t":0,"v":1}]}]}';+ const html = `<!DOCTYPE html><html><body>+ <div id="root" data-composition-id="main" data-start="0" data-width="1920" data-height="1080" data-duration="10">+ <audio id="bgm" src="a.wav" data-start="0" data-duration="10" data-automation='${automation}'></audio>+ </div></body></html>`;+ const [el] = parseAudioElements(html);+ expect(el?.automation).toBe(automation);+ });++ it("leaves automation unset when the element has none", () => {+ const html = `<!DOCTYPE html><html><body>+ <div id="root" data-composition-id="main" data-start="0" data-width="1920" data-height="1080" data-duration="10">+ <audio id="bgm" src="a.wav" data-start="0" data-duration="10"></audio>+ </div></body></html>`;+ expect(parseAudioElements(html)[0]?.automation).toBeUndefined();+ });+});diff --git a/packages/engine/src/services/audioFxRender.test.ts b/packages/engine/src/services/audioFxRender.test.tsindex 872b18e92e..0456c9488c 100644--- a/packages/engine/src/services/audioFxRender.test.ts+++ b/packages/engine/src/services/audioFxRender.test.ts@@ -67,6 +67,15 @@ function tone(path: string, seconds = 0.3, freq = 440): void {writeWav(path, s, SR);}+/** RMS of one slice, for comparing how loud a moment is against another. */+const sliceRms = (s: Float32Array, from: number, to: number): number => {+ const a = Math.max(0, Math.floor(from * SR));+ const b = Math.min(s.length, Math.floor(to * SR));+ let sum = 0;+ for (let i = a; i < b; i++) sum += (s[i] ?? 0) * (s[i] ?? 0);+ return Math.sqrt(sum / Math.max(1, b - a));+};+const rms = (s: Float32Array): number =>Math.sqrt(s.reduce((a, x) => a + x * x, 0) / Math.max(1, s.length));const db = (x: number): number => 20 * Math.log10(x + 1e-30);@@ -235,6 +244,45 @@ describe.skipIf(!HAS_BROWSER)("browser render", () => {expect(db(rms(readWav(outPath).samples))).toBeLessThan(db(rms(readWav(input).samples)) - 3);}, 180_000);+ it("sweeps a filter across the clip when a lane automates it", async () => {+ // A 2 kHz tone under a lowpass whose cutoff rises from below it to well+ // above: the start should be attenuated and the end should not. This is+ // the whole point of the render path — the envelope has to be *scheduled*+ // offline, not merely parsed.+ const input = join(dir, "sweep-in.wav");+ tone(input, 1.5, 2000);+ const outPath = join(dir, "sweep-out.wav");+ await applyAudioFxChain(+ input,+ {+ version: 1,+ nodes: [{ type: "lowpass", id: "n1", enabled: true, params: { frequency: 300, q: 0.707 } }],+ },+ outPath,+ {+ trackId: "t",+ automation: {+ version: 1,+ lanes: [+ {+ target: "fx.n1.frequency",+ points: [+ { t: 0, v: 300 },+ { t: 1.5, v: 16000 },+ ],+ },+ ],+ },+ },+ );+ const after = readWav(outPath).samples;+ const head = db(sliceRms(after, 0.05, 0.25));+ const tail = db(sliceRms(after, 1.2, 1.45));+ // Opening the filter past the tone has to leave it far louder than when+ // the cutoff sat an octave and a half below it.+ expect(tail).toBeGreaterThan(head + 15);+ }, 180_000);+it("renders a multi-effect chain including reverb", async () => {const input = join(dir, "in.wav");tone(input);diff --git a/packages/engine/src/services/audioFxRender.ts b/packages/engine/src/services/audioFxRender.tsindex 6376f6ae01..80ce1cd8ef 100644--- a/packages/engine/src/services/audioFxRender.ts+++ b/packages/engine/src/services/audioFxRender.ts@@ -18,6 +18,7 @@ import { join } from "node:path";import { pathToFileURL } from "node:url";import { getAudioFxRuntimeScript } from "@hyperframes/core/audio-fx-runtime";import { enabledAudioFxNodes, type HfAudioFxChain } from "@hyperframes/core/audio-fx";+import { serializeAutomation, type HfAutomation } from "@hyperframes/core/audio-automation";import { acquireBrowser } from "./browserManager.js";export class AudioFxRenderError extends Error {@@ -195,7 +196,7 @@ export async function applyAudioFxChain(inputWav: string,chain: HfAudioFxChain,outputWav: string,- options: { trackId: string; signal?: AbortSignal },+ options: { trackId: string; signal?: AbortSignal; automation?: HfAutomation },): Promise<string> {if (enabledAudioFxNodes(chain).length === 0) return inputWav;if (!existsSync(inputWav)) {@@ -227,7 +228,12 @@ export async function applyAudioFxChain(await page.addScriptTag({ content: getAudioFxRuntimeScript() });const rendered = (await page.evaluate(- async ([channelB64, rate, chainJson]: [string[], number, string]) => {+ async ([channelB64, rate, chainJson, automationJson]: [+ string[],+ number,+ string,+ string,+ ]) => {const decode = (b64: string): Float32Array => {const bin = atob(b64);const bytes = new Uint8Array(bin.length);@@ -237,12 +243,22 @@ export async function applyAudioFxChain(const api = (window as unknown as {__HF_AUDIO_FX?: {- render(p: Float32Array[], r: number, c: string): Promise<Float32Array[]>;+ render(+ p: Float32Array[],+ r: number,+ c: string,+ a?: string,+ ): Promise<Float32Array[]>;};}).__HF_AUDIO_FX;if (!api) throw new Error("audio FX runtime failed to load");- const out = await api.render(channelB64.map(decode), rate, chainJson);+ const out = await api.render(+ channelB64.map(decode),+ rate,+ chainJson,+ automationJson || undefined,+ );const encode = (plane: Float32Array): string => {const u8 = new Uint8Array(plane.buffer, plane.byteOffset, plane.length * 4);let s = "";@@ -260,7 +276,8 @@ export async function applyAudioFxChain(),sampleRate,JSON.stringify(chain),- ] as [string[], number, string],+ options.automation ? serializeAutomation(options.automation) : "",+ ] as [string[], number, string, string],)) as string[];// byteOffset and byteLength matter: Node pools small allocations, so a@@ -289,4 +306,4 @@ export async function applyAudioFxChain(}}-export type { HfAudioFxChain };+export type { HfAudioFxChain, HfAutomation };diff --git a/packages/engine/src/services/audioMixer.ts b/packages/engine/src/services/audioMixer.tsindex c08d092445..8c623c72c6 100644--- a/packages/engine/src/services/audioMixer.ts+++ b/packages/engine/src/services/audioMixer.ts@@ -30,7 +30,16 @@ import type {} from "./audioMixer.types.js";import { applyVolumeEnvelopeToWav } from "./audioVolumeEnvelope.js";import { HF_AUDIO_FX_ATTR, parseAudioFxChain } from "@hyperframes/core/audio-fx";+import {+ HF_AUDIO_AUTOMATION_ATTR,+ parseAutomation,+ resolveAutomation,+ sampleAutomationLane,+ VOLUME_TARGET,+ type HfAutomationLane,+} from "@hyperframes/core/audio-automation";import { applyAudioFxChain, AudioFxRenderError } from "./audioFxRender.js";+import type { AudioVolumeKeyframe } from "./audioMixer.types.js";export type { AudioElement, MixResult } from "./audioMixer.types.js";@@ -331,6 +340,56 @@ function ffmpegFailure(};}+/** Extra samples per second inside a segment the baker cannot draw straight. */+const CURVED_SEGMENT_SAMPLES_PER_SEC = 30;++/**+ * A volume lane as keyframes for the PCM baker.+ *+ * The baker interpolates linearly between keyframes, so a straight segment+ * needs only its two ends — a simple fade stays two keyframes. A bent one is+ * sampled, or the bake would quietly straighten it.+ *+ * Times come out in composition seconds, which is what the baker subtracts the+ * track start from. Returns null when the track has no volume lane.+ */+export function volumeLaneKeyframes(+ automation: { lanes: HfAutomationLane[] },+ trackStart: number,+ duration: number,+): AudioVolumeKeyframe[] | null {+ const lane = automation.lanes.find((l) => l.target === VOLUME_TARGET);+ if (!lane || lane.points.length === 0) return null;++ const out: AudioVolumeKeyframe[] = [];+ const push = (t: number, v: number): void => {+ out.push({ time: trackStart + t, volume: v });+ };++ // The envelope holds its first value before the first point, rather than+ // falling back to `data-volume`.+ const first = lane.points[0]!;+ if (first.t > 0) push(0, first.v);++ for (let i = 0; i < lane.points.length; i += 1) {+ const a = lane.points[i]!;+ push(a.t, a.v);+ const b = lane.points[i + 1];+ if (!b || !a.curve) continue;+ const steps = Math.max(2, Math.ceil((b.t - a.t) * CURVED_SEGMENT_SAMPLES_PER_SEC));+ for (let k = 1; k < steps; k += 1) {+ const t = a.t + ((b.t - a.t) * k) / steps;+ push(t, sampleAutomationLane(lane, t));+ }+ }++ // Hold the last value to the clip's end, so the baker does not ramp away+ // from it toward whatever it would otherwise assume.+ const last = lane.points[lane.points.length - 1]!;+ if (duration > last.t) push(duration, last.v);+ return out;+}+export function parseAudioElements(html: string): AudioElement[] {const elements: AudioElement[] = [];const { document } = parseHTML(unwrapTemplate(html));@@ -363,12 +422,14 @@ export function parseAudioElements(html: string): AudioElement[] {};// <audio> and <video data-has-audio> tracks differ only in the emitted id+// and `type`; everything else (timing, layer, volume) is read identically.const build = (el: RefResolverEl, id: string, type: AudioElement["type"]): AudioElement => {const mediaStartAttr = el.getAttribute("data-media-start");const layerAttr = el.getAttribute("data-layer");const volumeAttr = el.getAttribute("data-volume");const fxChain = el.getAttribute(HF_AUDIO_FX_ATTR);+ const automation = el.getAttribute(HF_AUDIO_AUTOMATION_ATTR);return {id,src: el.getAttribute("src") as string,@@ -378,6 +439,7 @@ export function parseAudioElements(html: string): AudioElement[] {layer: layerAttr ? parseInt(layerAttr) : 0,volume: volumeAttr ? parseFloat(volumeAttr) : 1.0,...(fxChain ? { fxChain } : {}),+ ...(automation ? { automation } : {}),type,};};@@ -875,6 +937,13 @@ export async function processCompositionAudio(// envelope belongs on their output. A missing or broken chain is fatal// for the whole mix rather than a per-track warning — quietly rendering// the dry signal ships a mix that sounds plausible and is wrong.+ const automation = element.automation+ ? resolveAutomation(+ parseAutomation(element.automation),+ element.fxChain ? parseAudioFxChain(element.fxChain) : undefined,+ )+ : null;+if (element.fxChain) {// The chain is serialised into the attribute, the same way colour// grading carries its config, so there is no side-car file to find,@@ -884,7 +953,11 @@ export async function processCompositionAudio(audioSrcPath,chain,join(workDir, `${element.id}-fx.wav`),- { trackId: element.id, signal: effectiveSignal },+ {+ trackId: element.id,+ signal: effectiveSignal,+ ...(automation ? { automation } : {}),+ },);}@@ -892,11 +965,19 @@ export async function processCompositionAudio(// (sample-accurate, no keyframe ceiling). If the WAV isn't the expected// 16-bit PCM, fall back to the ffmpeg expression path by leaving the// keyframes on the track for buildVolumeExpression to handle.+ //+ // A volume lane supersedes keyframes probed from the timeline: the two+ // would fight, and the lane is the explicit one. `lint` warns when a+ // track carries both.+ const laneKeyframes = automation+ ? volumeLaneKeyframes(automation, element.start, element.end - element.start)+ : null;+ const envelopeKeyframes = laneKeyframes ?? element.volumeKeyframes;let bakedEnvelope = false;- if (element.volumeKeyframes && element.volumeKeyframes.length > 0) {+ if (envelopeKeyframes && envelopeKeyframes.length > 0) {bakedEnvelope = applyVolumeEnvelopeToWav(audioSrcPath,- element.volumeKeyframes,+ envelopeKeyframes,element.start,element.volume ?? 1.0,);@@ -910,7 +991,7 @@ export async function processCompositionAudio(duration: element.end - element.start,// Gain is already in the samples when baked, so mix at unity.volume: bakedEnvelope ? 1.0 : (element.volume ?? 1.0),- volumeKeyframes: bakedEnvelope ? undefined : element.volumeKeyframes,+ volumeKeyframes: bakedEnvelope ? undefined : (envelopeKeyframes ?? undefined),});} catch (err: unknown) {// An FX failure is fatal for the whole mix. Every other failure modediff --git a/packages/engine/src/services/audioMixer.types.ts b/packages/engine/src/services/audioMixer.types.tsindex 0316d01e7d..1709ecbf95 100644--- a/packages/engine/src/services/audioMixer.types.ts+++ b/packages/engine/src/services/audioMixer.types.ts@@ -14,6 +14,8 @@ export interface AudioElement {volumeKeyframes?: AudioVolumeKeyframe[];/** Serialised FX chain JSON from `data-fx-chain`, when set. */fxChain?: string;+ /** Serialised automation JSON from `data-automation`, when set. */+ automation?: string;type: "audio" | "video";}