Skip to content

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

feat(timeline): tell the two insertion lanes apart, and pin the inert one

EtienneLescote5ba058cb015
diff --git a/src/lib/ai-edition/document/transcript.test.ts b/src/lib/ai-edition/document/transcript.test.ts
index 2f531542e..87c6b8213 100644
--- a/src/lib/ai-edition/document/transcript.test.ts
+++ b/src/lib/ai-edition/document/transcript.test.ts
@@ -714,6 +714,42 @@ describe("insert ranges", () => {
expect(insertRangesMatchWords(result)).toBe(true);
});
+ // The reason is user-visible on the region, and the two lanes do not hold the same
+ // thing: the film holds a FRAME, a take holds silence and no picture is involved
+ // (issue #560). The keying above is lane-agnostic and stays that way — one row per
+ // word per asset, whichever lane the asset is on.
+ it("names what is actually held, per lane", () => {
+ const film = insertDocumentWord(docWithClip(), "asset_1", "word_2", "after", "really");
+ expect(film.timeline.insertRanges[0].reason).toContain("Held frame");
+
+ // `createEmptyDocument` carries no assets, so the lane has to be given one to read.
+ const base = docWithClip();
+ const take = insertDocumentWord(
+ {
+ ...base,
+ assets: [
+ {
+ id: "asset_1",
+ kind: "audio" as const,
+ label: "take.mp3",
+ originalPath: "/take.mp3",
+ durationSec: 30,
+ cameraTrack: null,
+ },
+ ],
+ },
+ "asset_1",
+ "word_2",
+ "after",
+ "really",
+ );
+ expect(take.timeline.insertRanges[0].reason).toContain("Silence");
+ expect(take.timeline.insertRanges[0].reason).not.toContain("frame");
+ // Same row otherwise, and the invariant still holds on an audio asset.
+ expect(take.timeline.insertRanges[0]).toMatchObject({ atSec: 3, durationSec: 0.4 });
+ expect(insertRangesMatchWords(take)).toBe(true);
+ });
+
// The clips are the thing the first attempt broke. Nothing here may touch them.
it("leaves the clips exactly as they were", () => {
const before = docWithClip();
diff --git a/src/lib/ai-edition/document/transcript.ts b/src/lib/ai-edition/document/transcript.ts
index 331c2d7ec..87d6a5800 100644
--- a/src/lib/ai-edition/document/transcript.ts
+++ b/src/lib/ai-edition/document/transcript.ts
@@ -332,6 +332,10 @@ const MIN_PAUSE_SEC = 0.05;
* read back, for a test to hold this to.
*/
function withInsertRangesForWords(document: AxcutDocument, assetId: string): AxcutDocument {
+ // The reason is user-visible on the region, and it is not the same fact on both lanes:
+ // the film holds a FRAME, a take holds nothing but silence — no picture is involved
+ // (issue #560). Keying, below, stays lane-agnostic: one row per word per asset.
+ const isTake = document.assets.find((a) => a.id === assetId)?.kind === "audio";
const transcript = document.transcripts.find((t) => t.assetId === assetId);
const words = transcript?.words ?? [];
const wanted = new Map<string, number>();
@@ -371,7 +375,9 @@ function withInsertRangesForWords(document: AxcutDocument, assetId: string): Axc
atSec: word.endSec,
durationSec,
wordId,
- reason: `Held frame for the added word "${word.text}".`,
+ reason: isTake
+ ? `Silence for the added word "${word.text}".`
+ : `Held frame for the added word "${word.text}".`,
origin: "user",
});
}
diff --git a/src/lib/ai-edition/timeline/insert-mapping.test.ts b/src/lib/ai-edition/timeline/insert-mapping.test.ts
new file mode 100644
index 000000000..137c66332
--- /dev/null
+++ b/src/lib/ai-edition/timeline/insert-mapping.test.ts
@@ -0,0 +1,196 @@
+// Issue #560. An insertion made from the recording transcript buys the FILM time; one made
+// from a voiceover transcript buys the TAKE silence and leaves the picture alone. The row
+// looks identical either way — only the asset it names says which lane it is on.
+//
+// These also lock the inertness that is true today by ACCIDENT: a voiceover row reaches
+// `rulerInserts` and `resolvePlaybackSegments` and is ignored by both, purely because they
+// match on `clip.assetId`. That accident is the only reason writing one is harmless right
+// now, so it becomes a rule with a test before anything starts writing them.
+
+import { describe, expect, it } from "vitest";
+import { resolvePlaybackSegments } from "../document/timeline";
+import type { AxcutAudioTrack, AxcutClip, AxcutDocument, AxcutInsertRange } from "../schema";
+import { resolveInsertPlacement, takeInserts } from "./insert-mapping";
+import { rulerInserts } from "./inserted-time";
+
+const CLIPS: AxcutClip[] = [
+ {
+ id: "c1",
+ assetId: "rec",
+ sourceStartSec: 0,
+ sourceEndSec: 6,
+ timelineStartSec: 0,
+ timelineEndSec: 6,
+ wordRefs: [],
+ origin: "user",
+ reason: "",
+ },
+ {
+ id: "c2",
+ assetId: "rec",
+ sourceStartSec: 20,
+ sourceEndSec: 26,
+ timelineStartSec: 6,
+ timelineEndSec: 12,
+ wordRefs: [],
+ origin: "user",
+ reason: "",
+ },
+];
+
+const TAKE = {
+ id: "vo_frag",
+ trackId: "vo",
+ assetId: "aud",
+ kind: "voiceover",
+ startMs: 2000,
+ endMs: 10_000,
+ durationSec: 30,
+ offsetMs: 1000,
+ gainDb: 0,
+ loop: false,
+ fadeInMs: 0,
+ fadeOutMs: 0,
+ muted: false,
+ label: "",
+ origin: "user",
+} as unknown as AxcutAudioTrack;
+
+function insert(over: Partial<AxcutInsertRange> & { id: string }): AxcutInsertRange {
+ return {
+ assetId: "rec",
+ atSec: 3,
+ durationSec: 0.5,
+ wordId: `w_${over.id}`,
+ reason: "",
+ origin: "user",
+ ...over,
+ } as AxcutInsertRange;
+}
+
+function doc(inserts: AxcutInsertRange[], over: Partial<AxcutDocument> = {}): AxcutDocument {
+ return {
+ schemaVersion: 7,
+ project: { id: "p", title: "T", createdAt: "", updatedAt: "" },
+ assets: [
+ {
+ id: "rec",
+ kind: "video",
+ label: "r",
+ originalPath: "/r.mp4",
+ durationSec: 30,
+ cameraTrack: null,
+ },
+ {
+ id: "aud",
+ kind: "audio",
+ label: "a",
+ originalPath: "/a.mp3",
+ durationSec: 30,
+ cameraTrack: null,
+ },
+ ],
+ transcript: null,
+ transcripts: [],
+ timeline: {
+ clips: CLIPS,
+ gaps: [],
+ trimRanges: [],
+ muteRanges: [],
+ speedRanges: [],
+ captionRanges: [],
+ insertRanges: inserts,
+ },
+ annotations: [],
+ zoomRanges: [],
+ audioTracks: [TAKE],
+ legacyEditor: null,
+ ...over,
+ } as unknown as AxcutDocument;
+}
+
+describe("resolveInsertPlacement", () => {
+ it("gives a recording insert its raw moment, through the clip that plays it", () => {
+ const row = insert({ id: "i1", atSec: 3 });
+ expect(resolveInsertPlacement(row, doc([row]))).toEqual({ lane: "recording", atRawSec: 3 });
+ });
+
+ it("resolves through the SECOND clip when that is the one holding the source moment", () => {
+ // Two clips over one media at different source positions: source 22 is raw 8.
+ const row = insert({ id: "i1", atSec: 22 });
+ expect(resolveInsertPlacement(row, doc([row]))).toEqual({ lane: "recording", atRawSec: 8 });
+ });
+
+ it("leaves a voiceover insert UNPROJECTED, naming the take and a source second", () => {
+ // Deliberately not a raw moment: where it lands depends on the insertions before it
+ // inside the same take and on the cuts under it, and only the take's walk knows.
+ const row = insert({ id: "i1", assetId: "aud", atSec: 4 });
+ expect(resolveInsertPlacement(row, doc([row]))).toEqual({
+ lane: "voiceover",
+ trackGroupId: "vo",
+ atSourceSec: 4,
+ });
+ });
+
+ it("returns null when nothing carries the moment any more", () => {
+ // Past every clip's source window...
+ expect(resolveInsertPlacement(insert({ id: "i1", atSec: 40 }), doc([]))).toBeNull();
+ // ...outside the take's own window (offset 1s, span 8s → source 1..9)...
+ expect(
+ resolveInsertPlacement(insert({ id: "i2", assetId: "aud", atSec: 12 }), doc([])),
+ ).toBeNull();
+ // ...and when the take has been deleted outright.
+ expect(
+ resolveInsertPlacement(
+ insert({ id: "i3", assetId: "aud", atSec: 4 }),
+ doc([], { audioTracks: [] }),
+ ),
+ ).toBeNull();
+ });
+
+ it("names the take by its GROUP, so a split take resolves to one thing", () => {
+ const split = doc([], {
+ audioTracks: [
+ { ...TAKE, id: "f1", trackId: "vo", startMs: 2000, endMs: 6000, offsetMs: 1000 },
+ { ...TAKE, id: "f2", trackId: "vo", startMs: 6000, endMs: 10_000, offsetMs: 5000 },
+ ],
+ });
+ const row = insert({ id: "i1", assetId: "aud", atSec: 2 });
+ expect(resolveInsertPlacement(row, split)).toMatchObject({ trackGroupId: "vo" });
+ });
+});
+
+describe("takeInserts", () => {
+ it("collects one take's insertions in its own source order", () => {
+ const rows = [
+ insert({ id: "b", assetId: "aud", atSec: 6 }),
+ insert({ id: "a", assetId: "aud", atSec: 2 }),
+ insert({ id: "film", assetId: "rec", atSec: 3 }),
+ ];
+ expect(takeInserts(doc(rows), "vo").map((i) => [i.id, i.atSourceSec])).toEqual([
+ ["a", 2],
+ ["b", 6],
+ ]);
+ });
+
+ it("returns nothing for a take that has none", () => {
+ expect(takeInserts(doc([insert({ id: "film" })]), "vo")).toEqual([]);
+ });
+});
+
+describe("a voiceover insert is inert on the film, deliberately", () => {
+ const row = insert({ id: "i1", assetId: "aud", atSec: 4 });
+
+ it("produces no ruler insert, so the film's length does not move", () => {
+ expect(rulerInserts([row], CLIPS)).toEqual([]);
+ // And the recording's own row still does.
+ expect(rulerInserts([insert({ id: "i2", atSec: 3 })], CLIPS)).toHaveLength(1);
+ });
+
+ it("produces no held segment, so no clip freezes for it", () => {
+ const held = (rows: AxcutInsertRange[]) =>
+ resolvePlaybackSegments(CLIPS, [], rows).filter((s) => s.heldSec !== undefined);
+ expect(held([row])).toEqual([]);
+ expect(held([insert({ id: "i2", atSec: 3 })])).toHaveLength(1);
+ });
+});
diff --git a/src/lib/ai-edition/timeline/insert-mapping.ts b/src/lib/ai-edition/timeline/insert-mapping.ts
new file mode 100644
index 000000000..b21f2d31c
--- /dev/null
+++ b/src/lib/ai-edition/timeline/insert-mapping.ts
@@ -0,0 +1,100 @@
+// Where an insertion lives, resolved rather than stored (issue #560).
+//
+// An added word buys itself time. On the RECORDING lane that time is film: the clip holds
+// a frame and the ruler grows. On the VOICEOVER lane it is a silence inside the take: the
+// picture is not touched at all, and the narration that follows lands later against the
+// same image.
+//
+// The record says which by naming an asset, and the asset's `kind` says which lane it is
+// on. Nothing stores a container id, and that is deliberate: every candidate is ephemeral.
+// A voiceover fragment id is re-minted by `reanchorAudioTracks` on the first clip drag; a
+// clip id does not survive a split, which in this repo is `duplicateClip` plus two
+// `setClipSourceRange` calls that move `atSec` out of the half that was named. An
+// un-anchored region reaching every placement of its asset is the working state here — see
+// `duplicateClip`'s own comment about trims.
+//
+// The two lanes come back in DIFFERENT shapes on purpose. A recording insert can be given
+// its raw moment immediately, through the clip that plays that source second. A voiceover
+// insert cannot: its ruler position depends on the insertions before it inside the same
+// take AND on the cuts under it, and only the take's own walk can resolve that. Handing
+// back an unprojected result is what stops a caller from inventing a projection that would
+// disagree with the walk.
+
+import { trackGroupId } from "../document/audioTracks";
+import type { AxcutDocument, AxcutInsertRange } from "../schema";
+
+/** A pause in the film: the clip holds a frame at this raw moment. */
+export interface RecordingInsertPlacement {
+ lane: "recording";
+ /** Raw ruler second — insertions occupy zero raw time, so this is a point. */
+ atRawSec: number;
+}
+
+/** A silence inside a take: no picture, no ruler growth of its own. */
+export interface VoiceoverInsertPlacement {
+ lane: "voiceover";
+ /** The user-visible take, not one of its stored fragments. */
+ trackGroupId: string;
+ /** Deliberately in the take's SOURCE seconds — see the note above. */
+ atSourceSec: number;
+}
+
+export type InsertPlacement = RecordingInsertPlacement | VoiceoverInsertPlacement;
+
+/**
+ * Where this insertion belongs, or null when nothing carries it any more — a clip whose
+ * source range no longer contains the moment, or a take that has been deleted.
+ *
+ * The lane is read from the ASSET, never from the row: `kind: "audio"` is the only thing
+ * that distinguishes a take's transcript from the film's, and it is already the
+ * discriminator `lanePlacements` uses for the transcript tab.
+ */
+export function resolveInsertPlacement(
+ insert: AxcutInsertRange,
+ document: AxcutDocument,
+): InsertPlacement | null {
+ const asset = document.assets.find((a) => a.id === insert.assetId);
+ if (asset?.kind === "audio") {
+ // The first take drawing on this asset whose source window contains the moment.
+ // Inclusive at both edges, matching `rulerInserts`: a pause sits at the END of the
+ // word it follows, which is routinely a window's own boundary.
+ for (const track of document.audioTracks ?? []) {
+ if (track.kind !== "voiceover" || track.assetId !== insert.assetId) continue;
+ const startSec = track.offsetMs / 1000;
+ const endSec = startSec + Math.max(0, track.endMs - track.startMs) / 1000;
+ if (insert.atSec < startSec || insert.atSec > endSec) continue;
+ return { lane: "voiceover", trackGroupId: trackGroupId(track), atSourceSec: insert.atSec };
+ }
+ return null;
+ }
+
+ for (const clip of document.timeline.clips) {
+ if (clip.assetId !== insert.assetId) continue;
+ const sourceEnd = clip.sourceEndSec ?? Number.POSITIVE_INFINITY;
+ if (insert.atSec < clip.sourceStartSec || insert.atSec > sourceEnd) continue;
+ return {
+ lane: "recording",
+ atRawSec: clip.timelineStartSec + (insert.atSec - clip.sourceStartSec),
+ };
+ }
+ return null;
+}
+
+/** The insertions belonging to one take, in the take's own source order. */
+export function takeInserts(
+ document: AxcutDocument,
+ groupId: string,
+): Array<{ id: string; wordId: string; atSourceSec: number; durationSec: number }> {
+ const out: Array<{ id: string; wordId: string; atSourceSec: number; durationSec: number }> = [];
+ for (const insert of document.timeline.insertRanges ?? []) {
+ const placement = resolveInsertPlacement(insert, document);
+ if (placement?.lane !== "voiceover" || placement.trackGroupId !== groupId) continue;
+ out.push({
+ id: insert.id,
+ wordId: insert.wordId,
+ atSourceSec: placement.atSourceSec,
+ durationSec: insert.durationSec,
+ });
+ }
+ return out.sort((a, b) => a.atSourceSec - b.atSourceSec || a.id.localeCompare(b.id));
+}