Skip to content

Code-first video & image, rendered natively on WebGPU.

fix(audio-fade): place fade baseTime on the original media timeline

okanasl8d130786d005
diff --git a/nodes/node-audio-fade/src/renderers/audio-processor.test.ts b/nodes/node-audio-fade/src/renderers/audio-processor.test.ts
index 7a347dc5..b99ec869 100644
--- a/nodes/node-audio-fade/src/renderers/audio-processor.test.ts
+++ b/nodes/node-audio-fade/src/renderers/audio-processor.test.ts
@@ -153,8 +153,7 @@ describe.skipIf(!maybeDevice)("Fade Audio Processor", () => {
expect(channels[0][44099]).toBeCloseTo(0.0, 3);
});
- // Known failure: the processor ignores ctx.elapsedMs, so every cut segment fades in from 0.
- it.fails("should not apply fade in to a cut segment that starts after fadeInDuration (elapsedMs >= fadeInDuration)", async () => {
+ it("should not apply fade in to a cut segment that starts after fadeInDuration (elapsedMs >= fadeInDuration)", async () => {
// fadeInDuration = 0.5s. Cut segment starts at 1.0s (elapsedMs = 1000)
const virtualMedia = createVirtualMedia(3000, {
fadeInDuration: 0.5,
@@ -162,7 +161,8 @@ describe.skipIf(!maybeDevice)("Fade Audio Processor", () => {
fadeInCurve: "linear",
});
- const channels = constantChannels(22050); // 0.5s chunk
+ // 0.5 amplitude keeps output below the 0.9 soft-clip knee, so output = 0.5 * gain exactly.
+ const channels = constantChannels(22050, 0.5); // 0.5s chunk
await fadeAudioProcessor(channels, SAMPLE_RATE, virtualMedia, {
device,
frame: 0,
@@ -171,14 +171,13 @@ describe.skipIf(!maybeDevice)("Fade Audio Processor", () => {
elapsedMs: 1000, // starts at 1.0s of original media (after 0.5s fade in)
});
- // Samples should remain at 1.0 throughout (100% gain, no fade-in)
- expect(channels[0][0]).toBe(1.0);
- expect(channels[0][11025]).toBe(1.0);
- expect(channels[0][22049]).toBe(1.0);
+ // Samples should remain at 0.5 throughout (100% gain, no fade-in)
+ expect(channels[0][0]).toBe(0.5);
+ expect(channels[0][11025]).toBe(0.5);
+ expect(channels[0][22049]).toBe(0.5);
});
- // Known failure: the processor ignores ctx.elapsedMs, so every cut segment fades in from 0.
- it.fails("should evaluate partial fade in when cut segment starts inside fadeInDuration", async () => {
+ it("should evaluate partial fade in when cut segment starts inside fadeInDuration", async () => {
// fadeInDuration = 1.0s. Cut segment starts at 0.5s (elapsedMs = 500)
const virtualMedia = createVirtualMedia(3000, {
fadeInDuration: 1.0,
@@ -186,7 +185,8 @@ describe.skipIf(!maybeDevice)("Fade Audio Processor", () => {
fadeInCurve: "linear",
});
- const channels = constantChannels(22050); // 0.5s chunk (from 0.5s to 1.0s of original media)
+ // 0.5 amplitude keeps output below the 0.9 soft-clip knee, so output = 0.5 * gain exactly.
+ const channels = constantChannels(22050, 0.5); // 0.5s chunk (from 0.5s to 1.0s of original media)
await fadeAudioProcessor(channels, SAMPLE_RATE, virtualMedia, {
device,
frame: 0,
@@ -196,10 +196,10 @@ describe.skipIf(!maybeDevice)("Fade Audio Processor", () => {
});
// First sample of chunk (0.5s mark of media) should start at gain = 0.5
- expect(channels[0][0]).toBeCloseTo(0.5, 3);
+ expect(channels[0][0]).toBeCloseTo(0.25, 5);
// End sample of chunk (1.0s mark of media) should reach gain = 1.0
- expect(channels[0][22049]).toBeCloseTo(1.0, 3);
+ expect(channels[0][22049]).toBeCloseTo(0.5, 3);
});
// Known failure: the fade shader soft-clips above 0.9, so a 1.0 input ends at ~0.976.
diff --git a/nodes/node-audio-fade/src/renderers/audio-processor.ts b/nodes/node-audio-fade/src/renderers/audio-processor.ts
index 9e1ccee3..68c0f520 100644
--- a/nodes/node-audio-fade/src/renderers/audio-processor.ts
+++ b/nodes/node-audio-fade/src/renderers/audio-processor.ts
@@ -182,6 +182,9 @@ export const fadeAudioProcessor: AudioProcessor = async (
const nodeId = (op.id as string) || "audio-fade";
const frame = ctx.frame ?? 0;
const fps = ctx.fps ?? 24;
+ // Where this buffer starts in the original media. A cut segment carries its
+ // offset in elapsedMs; without it the buffer is assumed to start at the clip start.
+ const segmentStartSec = Math.max(0, (ctx.elapsedMs ?? 0) / 1000);
await WebGPUAudioProcessor.process(
ctx.device,
@@ -194,8 +197,10 @@ export const fadeAudioProcessor: AudioProcessor = async (
FADE_SHADER_TEMPLATE,
(chunkTimeSec) => {
// chunkTimeSec is (frame / fps + chunkOffset / sampleRate).
- // Subtracting frame / fps yields chunkOffset / sampleRate, which is time local to clip buffer.
- const localTimeSec = Math.max(0, chunkTimeSec - frame / fps);
+ // Subtracting frame / fps yields chunkOffset / sampleRate; adding the segment start
+ // puts it on the original media's timeline, the same one clipDuration is measured on.
+ const mediaTimeSec =
+ segmentStartSec + Math.max(0, chunkTimeSec - frame / fps);
return [
sampleRate,
fadeIn,
@@ -205,7 +210,7 @@ export const fadeAudioProcessor: AudioProcessor = async (
clipDuration,
hasFadeInDurationSig,
hasFadeOutDurationSig,
- localTimeSec, // baseTime (local to clip start!)
+ mediaTimeSec, // baseTime (position in the original media)
frame,
numSamples,
numChannels,