Code-first video & image, rendered natively on WebGPU.
fix(compositions): count audio layer volume keys from the layer's start
okanasl5c71a34c349c
diff --git a/.changeset/audio-volume-keys-layer-clock.md b/.changeset/audio-volume-keys-layer-clock.mdnew file mode 100644index 00000000..27e6bbc1--- /dev/null+++ b/.changeset/audio-volume-keys-layer-clock.md@@ -0,0 +1,5 @@+---+"@framefields/compositions": patch+---++An audio layer's `volume` and `muted` keyframes count from the layer's own first frame, as a picture layer's do. They were read on the composition's clock, so any audio layer that didn't start at frame 0 was past its last key from its first sample: a fade-in `[[0, 0], [6, 1], [len - 6, 1], [len, 0]]` left every later clip silent, and keys had to be offset by `startFrame` to play. Render QA already read the keys as layer-local and flagged that workaround as `animation_truncated`; the mix and QA now agree.diff --git a/packages/compositions/src/utils/audio-extractor.ts b/packages/compositions/src/utils/audio-extractor.tsindex 48b7b8d0..c604e8e6 100644--- a/packages/compositions/src/utils/audio-extractor.ts+++ b/packages/compositions/src/utils/audio-extractor.ts@@ -687,9 +687,16 @@ export async function mixAudioTracks(segmentStartSample + clipSamplesCount,),);- // The layer's own clock: its in-point moves the source, not its animation.+ // The layer's own clock: its in-point moves the source, not its+ // animation, and a CompositorLayer's keyframes count from its+ // own first frame (as its picture's do), not the composition's.+ const layerClockFrames =+ op.op === "CompositorLayer" ? localClockFrames : 0;const elapsedMs =- (totalClockFrames / fps + totalSeekSec - layerTrimSec) * 1000;+ ((totalClockFrames - layerClockFrames) / fps ++ totalSeekSec -+ layerTrimSec) *+ 1000;const durationMs =getActiveMediaMetadata(node)?.durationMs ?? undefined;await pluginProcessor(slicedChannels, targetSampleRate, node, {diff --git a/packages/framefields/src/audio-layer-timing.test.ts b/packages/framefields/src/audio-layer-timing.test.tsindex 2d12f022..222b49d6 100644--- a/packages/framefields/src/audio-layer-timing.test.ts+++ b/packages/framefields/src/audio-layer-timing.test.ts@@ -2,6 +2,7 @@* An audio layer's in-point and volume keyframes reach the mix: `trimStartSec`* picks where the source starts (as it does for a video layer's picture), and* a volume ramp is followed across the whole layer, not frozen at its start.+ * Its keyframes count from the layer's own first frame, as a picture layer's do.*/import fs from "node:fs/promises";import os from "node:os";@@ -19,7 +20,10 @@ beforeAll(async () => {const n = 2 * SR;const ch = new Float32Array(n);for (let i = SR; i < n; i++) ch[i] = Math.sin((2 * Math.PI * 440 * i) / SR);- source = path.join(await fs.mkdtemp(path.join(os.tmpdir(), "audio-layer-")), "step.wav");+ source = path.join(+ await fs.mkdtemp(path.join(os.tmpdir(), "audio-layer-")),+ "step.wav",+ );await fs.writeFile(source, encodeStereoWav([ch, ch], { sampleRate: SR }));});@@ -31,8 +35,8 @@ function rms(ch: Float32Array, fromSec: number, toSec: number): number {return Math.sqrt(s / (b - a));}-async function mix(layer: ReturnType<typeof Layer.audio>) {- const comp = new Composition({ width: 64, height: 64, fps: 30, durationMs: 1000 });+async function mix(layer: ReturnType<typeof Layer.audio>, durationMs = 1000) {+ const comp = new Composition({ width: 64, height: 64, fps: 30, durationMs });comp.add(layer);// renderAudio is the mix renderVideo muxes (layer processors included).const { channels } = await new HeadlessMediaRenderer().renderAudio(@@ -44,7 +48,9 @@ async function mix(layer: ReturnType<typeof Layer.audio>) {describe("audio layer timing", () => {it("starts the source at the layer's trimStartSec", async () => {- const trimmed = await mix(Layer.audio(source, { trimStartSec: 1, durationFrames: 30 }));+ const trimmed = await mix(+ Layer.audio(source, { trimStartSec: 1, durationFrames: 30 }),+ );const untrimmed = await mix(Layer.audio(source, { durationFrames: 30 }));// The tone begins at source 1.0 s: trimmed, it fills the layer from its// first frame; untrimmed, the layer only hears the silence before it.@@ -55,7 +61,10 @@ describe("audio layer timing", () => {it("follows a volume ramp across the layer", async () => {const ch = await mix(Layer.audio(source, { trimStartSec: 1, durationFrames: 30 }).animate(- LayerAnimation.keys("volume", [[0, 0], [15, 1]]),+ LayerAnimation.keys("volume", [+ [0, 0],+ [15, 1],+ ]),),);const full = rms(ch, 0.6, 0.9);@@ -66,4 +75,27 @@ describe("audio layer timing", () => {expect(rms(ch, 0.24, 0.26)).toBeGreaterThan(full * 0.35);expect(rms(ch, 0.24, 0.26)).toBeLessThan(full * 0.65);});++ it("counts volume keyframes from the layer's own start", async () => {+ // The layer enters at frame 30 (1.0 s); its fade-in runs over its own+ // first 15 frames, i.e. 1.0–1.5 s of the composition.+ const ch = await mix(+ Layer.audio(source, {+ trimStartSec: 1,+ startFrame: 30,+ durationFrames: 30,+ }).animate(+ LayerAnimation.keys("volume", [+ [0, 0],+ [15, 1],+ ]),+ ),+ 2000,+ );+ const full = rms(ch, 1.6, 1.9);+ expect(full).toBeGreaterThan(0.4);+ expect(rms(ch, 1.0, 1.03)).toBeLessThan(full * 0.1);+ expect(rms(ch, 1.24, 1.26)).toBeGreaterThan(full * 0.35);+ expect(rms(ch, 1.24, 1.26)).toBeLessThan(full * 0.65);+ });});