Skip to content

Record your screen, ship a demo. Free and open-source, GPU-accelerated, no watermarks, no subscriptions. Windows, macOS, Linux. Actively maintained.

fix(timeline): the projection counts the pauses, so audio stops landing early

EtienneLescot733e515d93ad
diff --git a/src/components/ai-edition/VirtualPreview.audio.test.ts b/src/components/ai-edition/VirtualPreview.audio.test.ts
index 39fea8528..9429ac708 100644
--- a/src/components/ai-edition/VirtualPreview.audio.test.ts
+++ b/src/components/ai-edition/VirtualPreview.audio.test.ts
@@ -188,7 +188,7 @@ describe("resolveTimelineAudioPlayback", () => {
durationSec: 10,
offsetMs: 0,
};
- const project = (rawSec: number) => projectRawTimelineSecToPlayback([clip], [trim], rawSec);
+ const project = (rawSec: number) => projectRawTimelineSecToPlayback([clip], [trim], rawSec, []);
const outputStart = project(bgm.startMs / 1000); // 0
// Raw playhead 5 sits 1s past the 2s cut → output 3. The track is a contiguous
@@ -225,7 +225,7 @@ describe("resolveTimelineAudioPlayback under a trim", () => {
origin: "user",
reason: "",
};
- const project = (rawSec: number) => projectRawTimelineSecToPlayback([clip], [trim], rawSec);
+ const project = (rawSec: number) => projectRawTimelineSecToPlayback([clip], [trim], rawSec, []);
const buried: AxcutAudioTrack = {
id: "buried",
diff --git a/src/components/ai-edition/VirtualPreview.tsx b/src/components/ai-edition/VirtualPreview.tsx
index 075446e01..c00574136 100644
--- a/src/components/ai-edition/VirtualPreview.tsx
+++ b/src/components/ai-edition/VirtualPreview.tsx
@@ -18,6 +18,7 @@ import type {
} from "@/lib/ai-edition/schema";
import { audioGainScalar } from "@/lib/ai-edition/store/editorSettings";
import { useEditorSettings } from "@/lib/ai-edition/store/useEditorSettings";
+import { rulerInserts } from "@/lib/ai-edition/timeline/inserted-time";
import type { PlaybackClockRef } from "@/lib/ai-edition/timeline/playback-clock";
import {
type RemovedRawSpan,
@@ -590,6 +591,10 @@ export function VirtualPreview({
trimRangesRef.current = trimRanges;
// What the film no longer contains, recomputed only when the cuts move — the rAF asks
// it once per voiceover per frame, and walking every trim there would be wasteful.
+ // The film's pauses, placed on the raw ruler once. The projection needs them or every
+ // track after a pause lands D seconds early — the bug this argument exists to close.
+ const filmInsertsRef = useRef(rulerInserts(insertRanges, clips));
+ filmInsertsRef.current = useMemo(() => rulerInserts(insertRanges, clips), [insertRanges, clips]);
const removedRef = useRef(removedRawSpans(clips, trimRanges));
removedRef.current = useMemo(() => removedRawSpans(clips, trimRanges), [clips, trimRanges]);
// Trim-narrowed (`resolvePlaybackSegments`) — used ONLY to detect "has the <video>'s own
@@ -693,6 +698,7 @@ export function VirtualPreview({
clipsRef.current,
trimRangesRef.current,
virtualTimeSecRef.current,
+ filmInsertsRef.current,
speedRegionsRef.current,
);
for (const track of audioTracksRef.current) {
@@ -702,6 +708,7 @@ export function VirtualPreview({
clipsRef.current,
trimRangesRef.current,
track.startMs / 1000,
+ filmInsertsRef.current,
speedRegionsRef.current,
);
// Length is measured WITHOUT speed, position WITH it. A trim REMOVES
@@ -716,11 +723,13 @@ export function VirtualPreview({
clipsRef.current,
trimRangesRef.current,
track.endMs / 1000,
+ filmInsertsRef.current,
) -
projectRawTimelineSecToPlayback(
clipsRef.current,
trimRangesRef.current,
track.startMs / 1000,
+ filmInsertsRef.current,
),
);
// A voiceover follows the cuts; a bed plays through them. Step 6 of #560
diff --git a/src/lib/ai-edition/document/timeline.test.ts b/src/lib/ai-edition/document/timeline.test.ts
index 4774c6517..eabccabd8 100644
--- a/src/lib/ai-edition/document/timeline.test.ts
+++ b/src/lib/ai-edition/document/timeline.test.ts
@@ -861,22 +861,22 @@ describe("projectRawTimelineSecToPlayback (issue #350 audio-track/trim sync)", (
const trim = makeTrim({ startSec: 2, endSec: 4 });
it("is the identity when there are no trims", () => {
- expect(projectRawTimelineSecToPlayback([clip], [], 6)).toBeCloseTo(6, 6);
+ expect(projectRawTimelineSecToPlayback([clip], [], 6, [])).toBeCloseTo(6, 6);
});
it("pulls a raw position after a cut earlier by the removed duration", () => {
// Raw 6 sits 2s past the 2s cut → output 4. This is the exact bug: the track was
// landing at 6 (delayed by the trim) instead of 4.
- expect(projectRawTimelineSecToPlayback([clip], [trim], 6)).toBeCloseTo(4, 6);
+ expect(projectRawTimelineSecToPlayback([clip], [trim], 6, [])).toBeCloseTo(4, 6);
});
it("is unaffected for a position before the cut", () => {
- expect(projectRawTimelineSecToPlayback([clip], [trim], 1)).toBeCloseTo(1, 6);
+ expect(projectRawTimelineSecToPlayback([clip], [trim], 1, [])).toBeCloseTo(1, 6);
});
it("collapses a position inside the trimmed gap to the end of the kept content before it", () => {
// Raw 3 is inside the removed 2..4 span → the next audible sample is at output 2.
- expect(projectRawTimelineSecToPlayback([clip], [trim], 3)).toBeCloseTo(2, 6);
+ expect(projectRawTimelineSecToPlayback([clip], [trim], 3, [])).toBeCloseTo(2, 6);
});
it("counts overlapping trims once (union, not sum)", () => {
@@ -886,7 +886,7 @@ describe("projectRawTimelineSecToPlayback (issue #350 audio-track/trim sync)", (
makeTrim({ id: "t1", startSec: 2, endSec: 5 }),
makeTrim({ id: "t2", startSec: 3, endSec: 4 }),
];
- expect(projectRawTimelineSecToPlayback([clip], trims, 6)).toBeCloseTo(3, 6);
+ expect(projectRawTimelineSecToPlayback([clip], trims, 6, [])).toBeCloseTo(3, 6);
});
it("removes a raw gap between clips (concatenated, like the programme)", () => {
@@ -906,7 +906,7 @@ describe("projectRawTimelineSecToPlayback (issue #350 audio-track/trim sync)", (
timelineStartSec: 15,
timelineEndSec: 25,
});
- expect(projectRawTimelineSecToPlayback([clipA, clipB], [], 20)).toBeCloseTo(15, 6);
+ expect(projectRawTimelineSecToPlayback([clipA, clipB], [], 20, [])).toBeCloseTo(15, 6);
});
it("sums cuts across multiple clips", () => {
@@ -930,7 +930,7 @@ describe("projectRawTimelineSecToPlayback (issue #350 audio-track/trim sync)", (
makeTrim({ id: "t2", startSec: 12, endSec: 14 }),
];
// Raw 18 is past both cuts (3s removed) → output 15.
- expect(projectRawTimelineSecToPlayback([clipA, clipB], trims, 18)).toBeCloseTo(15, 6);
+ expect(projectRawTimelineSecToPlayback([clipA, clipB], trims, 18, [])).toBeCloseTo(15, 6);
});
});
@@ -1714,16 +1714,16 @@ describe("projectRawTimelineSecToPlayback with speed regions", () => {
it("halves the time a 2x stretch takes to play", () => {
// Raw 4..8 at 2x plays in 2s, so raw 8 lands at output 6.
const speed = [{ startMs: 4000, endMs: 8000, speed: 2 }];
- expect(projectRawTimelineSecToPlayback([clip], [], 4, speed)).toBeCloseTo(4, 6);
- expect(projectRawTimelineSecToPlayback([clip], [], 6, speed)).toBeCloseTo(5, 6);
- expect(projectRawTimelineSecToPlayback([clip], [], 8, speed)).toBeCloseTo(6, 6);
+ expect(projectRawTimelineSecToPlayback([clip], [], 4, [], speed)).toBeCloseTo(4, 6);
+ expect(projectRawTimelineSecToPlayback([clip], [], 6, [], speed)).toBeCloseTo(5, 6);
+ expect(projectRawTimelineSecToPlayback([clip], [], 8, [], speed)).toBeCloseTo(6, 6);
// Everything after carries the compression with it.
- expect(projectRawTimelineSecToPlayback([clip], [], 12, speed)).toBeCloseTo(10, 6);
+ expect(projectRawTimelineSecToPlayback([clip], [], 12, [], speed)).toBeCloseTo(10, 6);
});
it("stretches a slow-motion region instead", () => {
const speed = [{ startMs: 0, endMs: 4000, speed: 0.5 }];
- expect(projectRawTimelineSecToPlayback([clip], [], 4, speed)).toBeCloseTo(8, 6);
+ expect(projectRawTimelineSecToPlayback([clip], [], 4, [], speed)).toBeCloseTo(8, 6);
});
it("composes with trims", () => {
@@ -1738,12 +1738,12 @@ describe("projectRawTimelineSecToPlayback with speed regions", () => {
reason: "",
};
const speed = [{ startMs: 6000, endMs: 10_000, speed: 2 }];
- expect(projectRawTimelineSecToPlayback([clip], [trim], 12, speed)).toBeCloseTo(8, 6);
+ expect(projectRawTimelineSecToPlayback([clip], [trim], 12, [], speed)).toBeCloseTo(8, 6);
});
it("ignores a nonsense rate rather than dividing by it", () => {
const speed = [{ startMs: 0, endMs: 4000, speed: 0 }];
- expect(projectRawTimelineSecToPlayback([clip], [], 4, speed)).toBeCloseTo(4, 6);
+ expect(projectRawTimelineSecToPlayback([clip], [], 4, [], speed)).toBeCloseTo(4, 6);
});
});
diff --git a/src/lib/ai-edition/document/timeline.ts b/src/lib/ai-edition/document/timeline.ts
index 463eb16fc..c437a3638 100644
--- a/src/lib/ai-edition/document/timeline.ts
+++ b/src/lib/ai-edition/document/timeline.ts
@@ -21,6 +21,7 @@ import type {
*/
export type PlaybackSegment = AxcutClip & { heldSec?: number };
+import type { RulerInsert } from "../timeline/inserted-time";
import { type Interval, subtractInterval } from "../timeline/intervals";
import { keptRawSpans } from "../timeline/programme-time";
import {
@@ -370,6 +371,15 @@ export function projectRawTimelineSecToPlayback(
clips: AxcutClip[],
trimRanges: AxcutTrimRange[],
rawSec: number,
+ /**
+ * The recording lane's pauses, already placed on the raw ruler by `rulerInserts`.
+ *
+ * REQUIRED, not optional, and every call site was migrated with it. An optional
+ * parameter would silently keep the early-audio bug alive at every site that had not
+ * been touched yet — which is the exact failure this argument exists to fix, and the
+ * kind that shows up as "the music starts a beat early" months later.
+ */
+ filmInserts: readonly RulerInsert[],
/**
* Speed regions on the raw ruler. Supplied by the AUDIO paths, which overlay
* a 1x track onto the finished programme and so need its real, speed-adjusted
@@ -383,19 +393,46 @@ export function projectRawTimelineSecToPlayback(
let lastRawEnd = 0; // raw end of the last kept segment, for the trailing overhang
let landed: number | null = null; // output(rawSec), once it falls in/before a kept segment
+ // A pause the film holds occupies ZERO raw seconds and D OUTPUT seconds — it is the one
+ // thing a flat kept-interval list cannot express, which is why it was left out and why
+ // every audio track after a pause has been landing D seconds early in both the preview
+ // and the export. Interleaved here rather than added afterwards, because where the pause
+ // sits inside the kept span decides which side of it `rawSec` falls on.
+ const holds = [...filmInserts].sort((a, b) => a.atRawSec - b.atRawSec);
+ let nextHold = 0;
+
// The kept stretches come from `keptRawSpans`, which is this walk — it was lifted out of
// here so the transcript lanes and the audio mix could ask the same question and get the
// same answer (issue #560). Trims only REMOVE, so a kept span's RAW length is what
// survives; how long it takes to PLAY is a separate question `outputDurationOfRawSpan`
// answers, because a speed region scales it.
for (const seg of keptRawSpans(ordered, trimRanges)) {
+ let from = seg.startSec;
+ // Every pause this segment carries, in order. A pause whose moment a trim removed is
+ // in no kept span at all and is never reached — the moment it holds is not in the
+ // film any more, so neither is the pause.
+ while (nextHold < holds.length && holds[nextHold].atRawSec < seg.startSec) nextHold++;
+ while (nextHold < holds.length && holds[nextHold].atRawSec < seg.endSec) {
+ const hold = holds[nextHold++];
+ const at = Math.max(from, hold.atRawSec);
+ if (landed === null && rawSec < at) {
+ const within = Math.min(Math.max(rawSec, from), at);
+ landed = outCursor + outputDurationOfRawSpan(from, within, speedRegions);
+ }
+ outCursor += outputDurationOfRawSpan(from, at, speedRegions);
+ from = at;
+ // Strictly after the pause's own moment, matching `expandRawSec`: a track whose
+ // head sits exactly there starts WITH the pause, not after it.
+ if (landed === null && rawSec <= at) landed = outCursor;
+ outCursor += hold.durationSec;
+ }
if (landed === null && rawSec < seg.endSec) {
// `rawSec` is inside this segment, or before it in a trimmed/gap region (then
// the span clamps to nothing → the output edge just before the gap).
- const within = Math.min(Math.max(rawSec, seg.startSec), seg.endSec);
- landed = outCursor + outputDurationOfRawSpan(seg.startSec, within, speedRegions);
+ const within = Math.min(Math.max(rawSec, from), seg.endSec);
+ landed = outCursor + outputDurationOfRawSpan(from, within, speedRegions);
}
- outCursor += outputDurationOfRawSpan(seg.startSec, seg.endSec, speedRegions);
+ outCursor += outputDurationOfRawSpan(from, seg.endSec, speedRegions);
lastRawEnd = seg.endSec;
}
// Past every kept frame: programme end plus whatever raw time hangs off the end (identity when
diff --git a/src/lib/ai-edition/timeline/programme-time.test.ts b/src/lib/ai-edition/timeline/programme-time.test.ts
index 5d6faa4ef..9942fb71d 100644
--- a/src/lib/ai-edition/timeline/programme-time.test.ts
+++ b/src/lib/ai-edition/timeline/programme-time.test.ts
@@ -125,7 +125,7 @@ describe("keptRawSpans agrees with playback", () => {
let before = 0;
for (const [i, span] of kept.entries()) {
expect(
- projectRawTimelineSecToPlayback(clips, trims, span.startSec),
+ projectRawTimelineSecToPlayback(clips, trims, span.startSec, []),
`seed ${seed} span ${i}`,
).toBeCloseTo(before, 6);
before += span.endSec - span.startSec;
@@ -161,13 +161,13 @@ describe("keptRawSpans agrees with playback", () => {
const trims = [trim({ id: "t1", clipId: "c1", startSec: 2, endSec: 4 })];
// Raw 2..4 is gone, so everything after it plays 2s earlier; inside the cut the
// playhead lands on the output edge just before it.
- expect(projectRawTimelineSecToPlayback(clips, trims, 1)).toBeCloseTo(1, 6);
- expect(projectRawTimelineSecToPlayback(clips, trims, 3)).toBeCloseTo(2, 6);
- expect(projectRawTimelineSecToPlayback(clips, trims, 6)).toBeCloseTo(4, 6);
- expect(projectRawTimelineSecToPlayback(clips, trims, 20)).toBeCloseTo(18, 6);
+ expect(projectRawTimelineSecToPlayback(clips, trims, 1, [])).toBeCloseTo(1, 6);
+ expect(projectRawTimelineSecToPlayback(clips, trims, 3, [])).toBeCloseTo(2, 6);
+ expect(projectRawTimelineSecToPlayback(clips, trims, 6, [])).toBeCloseTo(4, 6);
+ expect(projectRawTimelineSecToPlayback(clips, trims, 20, [])).toBeCloseTo(18, 6);
// Past the programme the projection is the identity, which is what lets a voiceover
// hang off the end and keep playing.
- expect(projectRawTimelineSecToPlayback(clips, trims, 25)).toBeCloseTo(23, 6);
+ expect(projectRawTimelineSecToPlayback(clips, trims, 25, [])).toBeCloseTo(23, 6);
});
});
@@ -274,3 +274,54 @@ describe("subtractRemoved", () => {
expect(subtractRemoved(18, 25, removed)).toEqual([{ startSec: 18, endSec: 25 }]);
});
});
+
+// ─── The pause the projection used to ignore ─────────────────────────────────
+// A pause occupies ZERO raw seconds and D OUTPUT seconds, which a flat kept-interval list
+// cannot express — so it was left out, and every audio track after a pause landed D seconds
+// early in both the preview and the export. `filmInserts` is required precisely so no call
+// site can quietly keep that bug.
+
+describe("projectRawTimelineSecToPlayback with the film's pauses", () => {
+ const clips = twoClips();
+ const pause = { id: "i1", wordId: "w1", atRawSec: 5, durationSec: 1 };
+
+ it("pushes everything after a pause later by exactly what it bought", () => {
+ expect(projectRawTimelineSecToPlayback(clips, [], 8, [])).toBeCloseTo(8, 6);
+ expect(projectRawTimelineSecToPlayback(clips, [], 8, [pause])).toBeCloseTo(9, 6);
+ });
+
+ it("leaves everything before it where it was", () => {
+ expect(projectRawTimelineSecToPlayback(clips, [], 3, [pause])).toBeCloseTo(3, 6);
+ });
+
+ it("starts a track whose head sits exactly on the pause WITH the pause", () => {
+ // Strict, matching `expandRawSec`: arriving at the pause's moment is the beginning
+ // of the hold, not the end of it.
+ expect(projectRawTimelineSecToPlayback(clips, [], 5, [pause])).toBeCloseTo(5, 6);
+ });
+
+ it("counts two pauses, in order", () => {
+ const second = { id: "i2", wordId: "w2", atRawSec: 7, durationSec: 0.5 };
+ expect(projectRawTimelineSecToPlayback(clips, [], 9, [pause, second])).toBeCloseTo(10.5, 6);
+ // Order of the argument must not matter: the walk sorts.
+ expect(projectRawTimelineSecToPlayback(clips, [], 9, [second, pause])).toBeCloseTo(10.5, 6);
+ });
+
+ it("never reaches a pause a trim removed", () => {
+ // The moment it holds is not in the film any more, so neither is the pause — the
+ // same rule `resolvePlaybackSegments` already follows.
+ const trims = [trim({ id: "t1", clipId: "c1", startSec: 4, endSec: 6 })];
+ expect(projectRawTimelineSecToPlayback(clips, trims, 8, [pause])).toBeCloseTo(
+ projectRawTimelineSecToPlayback(clips, trims, 8, []),
+ 6,
+ );
+ });
+
+ it("compresses the film around a pause but never the pause itself", () => {
+ // A voice plays at 1x. A 2x region halves the film either side; the second the pause
+ // bought is still a second.
+ const speed = [{ startMs: 0, endMs: 20_000, speed: 2 }];
+ expect(projectRawTimelineSecToPlayback(clips, [], 8, [], speed)).toBeCloseTo(4, 6);
+ expect(projectRawTimelineSecToPlayback(clips, [], 8, [pause], speed)).toBeCloseTo(5, 6);
+ });
+});
diff --git a/src/native/sceneDescription.ts b/src/native/sceneDescription.ts
index 8e939480d..b2d5a1d69 100644
--- a/src/native/sceneDescription.ts
+++ b/src/native/sceneDescription.ts
@@ -39,6 +39,7 @@ import type { AxcutClip, AxcutDocument } from "@/lib/ai-edition/schema";
import { getEditorSettings } from "@/lib/ai-edition/store/editorSettings";
import { assetCameraSource } from "@/lib/ai-edition/timeline/camera";
import { resolveClipSourceEndSec } from "@/lib/ai-edition/timeline/clipDuration";
+import { rulerInserts } from "@/lib/ai-edition/timeline/inserted-time";
import { removedRawSpans, subtractRemoved } from "@/lib/ai-edition/timeline/programme-time";
import { projectRegionsToSource } from "@/lib/ai-edition/timeline/timelineMap";
import {
@@ -563,6 +564,9 @@ export function buildSceneDescription(
).filter((r) => Number.isFinite(r.speed) && r.speed > 0);
// The one removed set, hoisted out of the map: every voiceover asks it the same
// question, and it does not depend on the track.
+ // Placed once: the projection below counts them, so a track after a pause lands where
+ // the ruler says rather than D seconds early.
+ const filmInserts = rulerInserts(document.timeline.insertRanges ?? [], projectedClips);
const removed = removedRawSpans(projectedClips, document.timeline.trimRanges);
const audioTracks = document.audioTracks.flatMap((track) => {
if (track.muted) return [];
@@ -574,6 +578,7 @@ export function buildSceneDescription(
projectedClips,
document.timeline.trimRanges,
track.startMs / 1000,
+ filmInserts,
rawSpeedRegions,
);
// Length is measured WITHOUT speed, position WITH it — the two do different
@@ -590,11 +595,13 @@ export function buildSceneDescription(
projectedClips,
document.timeline.trimRanges,
track.endMs / 1000,
+ filmInserts,
) -
projectRawTimelineSecToPlayback(
projectedClips,
document.timeline.trimRanges,
track.startMs / 1000,
+ filmInserts,
);
const spanSec = trimmedSpanSec;
if (spanSec <= 0) return [];
@@ -634,6 +641,7 @@ export function buildSceneDescription(
projectedClips,
document.timeline.trimRanges,
piece.startSec,
+ filmInserts,
rawSpeedRegions,
),
trimStartSec: offsetSec + (piece.startSec - trackRawStart),