From 38883ac97e832348b86210471d49788986b418e9 Mon Sep 17 00:00:00 2001 From: EtienneLescot Date: Sun, 22 Nov 2026 23:22:39 +0100 Subject: [PATCH 1/3] fix(captions): time voiceover cues on the take's 1x playback under speed regions A voiceover plays at 1x from its projected head, but its caption cues were laid on the ruler at 1x and then projected through the speed regions with the picture, so they drifted from the speech. Voiceover cues now sit at the piece's projected head plus the source distance on the screen clock, mapped back to the ruler. Recording-lane cues keep the projection. Refs #1657 --- .../ai-edition/captions/captionLane.test.ts | 32 +++++++++++++++++++ src/lib/ai-edition/captions/cues.ts | 21 ++++++++++-- 2 files changed, 51 insertions(+), 2 deletions(-) diff --git a/src/lib/ai-edition/captions/captionLane.test.ts b/src/lib/ai-edition/captions/captionLane.test.ts index 566f8781f5..b60d19e39f 100644 --- a/src/lib/ai-edition/captions/captionLane.test.ts +++ b/src/lib/ai-edition/captions/captionLane.test.ts @@ -5,6 +5,7 @@ import { describe, expect, it } from "vitest"; import type { AxcutAudioTrack, AxcutClip, AxcutDocument, AxcutTranscript } from "../schema"; +import { screenTimeMs } from "../timeline/speed"; import { deriveCaptionCues } from "./cues"; import { DEFAULT_CAPTION_SETTINGS, @@ -142,3 +143,34 @@ describe("captionLane", () => { expect(texts(corrected, "voiceover")).toContain("Kubernetes words"); }); }); + +describe("captions under a speed region (#1657)", () => { + // A voiceover from 0 s that speaks at source 4–6 s, under a 2x region over ruler 0–6 s. + const speech: AxcutTranscript = { + assetId: "aud", + language: "en", + segments: [], + words: [{ id: "w", segmentId: "s", text: "spoken", startSec: 4, endSec: 6 }], + } as unknown as AxcutTranscript; + const SPEED = [{ id: "sp", startMs: 0, endMs: 6000, speed: 2 }]; + const sped = doc({ + transcripts: [{ ...speech, assetId: "rec" }, speech], + legacyEditor: { + speedRegions: SPEED, + } as unknown as AxcutDocument["legacyEditor"], + }); + // What the viewer sees: the ruler cue projected through the speed region. + const onScreen = (lane: "recording" | "voiceover") => + deriveCaptionCues(sped, on(lane), {}).map((c) => ({ + startMs: screenTimeMs(SPEED, c.startMs), + endMs: screenTimeMs(SPEED, c.endMs), + })); + + it("shows a voiceover cue while its 1x speech plays, not compressed to 2–3 s", () => { + expect(onScreen("voiceover")).toEqual([{ startMs: 4000, endMs: 6000 }]); + }); + + it("still projects a recording-lane cue with the sped-up picture", () => { + expect(onScreen("recording")).toEqual([{ startMs: 2000, endMs: 3000 }]); + }); +}); diff --git a/src/lib/ai-edition/captions/cues.ts b/src/lib/ai-edition/captions/cues.ts index bde765a465..19cbfbcbc0 100644 --- a/src/lib/ai-edition/captions/cues.ts +++ b/src/lib/ai-edition/captions/cues.ts @@ -22,6 +22,7 @@ import { splitMergedCaptionsByWordBounds, } from "@/lib/captioning/annotationsFromCaptions"; import type { CaptionSegment } from "@/lib/captioning/transcribe"; +import { readSpeedRegions } from "../document/timeline"; import type { AxcutDocument, AxcutTranscript } from "../schema"; import { lanePlacements, @@ -29,6 +30,7 @@ import { type TranscriptPlacement, } from "../timeline/aggregated-transcript"; import { removedRawSpans } from "../timeline/programme-time"; +import { type SpeedRegion, screenTimeMs, timelineTimeMs } from "../timeline/speed"; import { type CaptionAnchorV, type CaptionSettings, @@ -167,6 +169,11 @@ export function sourceSpanToTimelineSpans( * window and the ruler head, which both providers carry (issue #560). `AxcutClip` * stays structurally assignable, so every existing caller is unaffected. */ clips: TranscriptPlacement[], + /** Pass the speed regions for a VOICEOVER lane only (#1657). A take plays at 1x from + * its projected head, so a cue must sit at that head plus its source distance on the + * screen clock, then go back to the ruler the preview and the compositor project. A + * recording-lane cue keeps the plain ruler time: its picture and sound are stretched. */ + unstretchedSpeedRegions: readonly SpeedRegion[] = [], ): Array<{ startSec: number; endSec: number }> { const out: Array<{ startSec: number; endSec: number }> = []; for (const clip of clips) { @@ -179,7 +186,14 @@ export function sourceSpanToTimelineSpans( // carrying an added word plays its media in pieces, and the seconds after the // insertion sit further along the ruler than their distance from the clip's start. // Writing the short version here is what put every caption after an insertion early. - out.push({ startSec: placementRawSec(clip, s), endSec: placementRawSec(clip, e) }); + const rawSec = (sec: number) => { + const raw = placementRawSec(clip, sec); + if (unstretchedSpeedRegions.length === 0) return raw; + const headScreenMs = screenTimeMs(unstretchedSpeedRegions, clip.timelineStartSec * 1000); + const distanceMs = (raw - clip.timelineStartSec) * 1000; + return timelineTimeMs(unstretchedSpeedRegions, headScreenMs + distanceMs) / 1000; + }; + out.push({ startSec: rawSec(s), endSec: rawSec(e) }); } // Onto the ruler the viewer actually sees. Expanding BOTH ends does the whole job: return out; @@ -199,8 +213,9 @@ export function deriveCaptionCues( if (!document || !settings.enabled) return []; // The lane the captions are read FROM — resolved, so a stored "voiceover" whose last // pill has been deleted falls back here rather than exporting nothing (issue #560). + const lane = resolveCaptionLane(document, settings); const placements = lanePlacements( - resolveCaptionLane(document, settings), + lane, document.timeline.clips, // `?? []` because the key is additive: a document written before it — or hand-built, // never through the schema — simply has none. @@ -208,6 +223,7 @@ export function deriveCaptionCues( removedRawSpans(document.timeline.clips, document.timeline.trimRanges), ); if (placements.length === 0) return []; + const voiceoverSpeedRegions = lane === "voiceover" ? readSpeedRegions(document) : []; const transcripts = new Map(document.transcripts.map((t) => [t.assetId, t])); // What the film no longer contains is CLIPS-derived on both lanes, deliberately: a cut @@ -234,6 +250,7 @@ export function deriveCaptionCues( line.startSec, line.endSec, placements, + voiceoverSpeedRegions, )) { const startMs = Math.round(span.startSec * 1000); const endMs = Math.max(Math.round(span.endSec * 1000), startMs + 1); From 5e109790fbcf6f9a7b5a84a91ff1792a5fb646de Mon Sep 17 00:00:00 2001 From: EtienneLescot Date: Mon, 23 Nov 2026 02:02:36 +0100 Subject: [PATCH 2/3] fix(captions): time voiceover cues on the cut-and-speed clock the mixer plays them on A take piece plays at 1x from its head projected through the cuts and the speed regions, and stops where the next piece of the take starts. Voiceover cues now sit at that projected head plus their source distance, mapped back to the ruler through the same clock, so a cue no longer lands inside a cut while its speech is heard. A non-final piece's cue is clipped to what the mixer lets it play. Refs #1657 --- .../ai-edition/captions/captionLane.test.ts | 49 ++++++++++++++ src/lib/ai-edition/captions/cues.ts | 67 ++++++++++++++----- src/lib/ai-edition/document/timeline.ts | 38 +++++++++++ 3 files changed, 136 insertions(+), 18 deletions(-) diff --git a/src/lib/ai-edition/captions/captionLane.test.ts b/src/lib/ai-edition/captions/captionLane.test.ts index b60d19e39f..7401ea5d51 100644 --- a/src/lib/ai-edition/captions/captionLane.test.ts +++ b/src/lib/ai-edition/captions/captionLane.test.ts @@ -4,6 +4,7 @@ // disagreeing about which lane a project even has. import { describe, expect, it } from "vitest"; +import { projectRawTimelineSecToPlayback, readSpeedRegions } from "../document/timeline"; import type { AxcutAudioTrack, AxcutClip, AxcutDocument, AxcutTranscript } from "../schema"; import { screenTimeMs } from "../timeline/speed"; import { deriveCaptionCues } from "./cues"; @@ -174,3 +175,51 @@ describe("captions under a speed region (#1657)", () => { expect(onScreen("recording")).toEqual([{ startMs: 2000, endMs: 3000 }]); }); }); + +describe("voiceover captions under a cut and a speed region (#1657)", () => { + // A 12 s film with a cut at ruler 4–6 s, and a voiceover from 0 s. + const FILM: AxcutClip[] = [{ ...CLIPS[0], sourceEndSec: 12, timelineEndSec: 12 }]; + const CUT = [{ id: "t", assetId: "rec", clipId: "c1", startSec: 4, endSec: 6 }]; + const said = (startSec: number, endSec: number): AxcutTranscript => + ({ + assetId: "aud", + language: "en", + segments: [], + words: [{ id: "w", segmentId: "s", text: "spoken", startSec, endSec }], + }) as unknown as AxcutTranscript; + const film = (speech: AxcutTranscript, speed: unknown[], voiceEndMs: number) => + doc({ + transcripts: [speech], + timeline: { ...doc().timeline, clips: FILM, trimRanges: CUT as never }, + audioTracks: [{ ...VOICE, endMs: voiceEndMs, durationSec: voiceEndMs / 1000 }], + legacyEditor: { speedRegions: speed } as unknown as AxcutDocument["legacyEditor"], + }); + // Where the viewer sees each cue: the ruler cue through the cut and the speed region. + const heard = (d: AxcutDocument) => + deriveCaptionCues(d, on("voiceover"), {}).map((c) => { + const out = (ms: number) => + Math.round( + projectRawTimelineSecToPlayback( + d.timeline.clips, + d.timeline.trimRanges, + ms / 1000, + readSpeedRegions(d), + ) * 1000, + ); + return { startMs: out(c.startMs), endMs: out(c.endMs) }; + }); + + it("keeps a cue on the speech when the 1x speech runs past the cut", () => { + // 2x over 0–4 s: source 3–3.5 s is heard at output 3–3.5 s, which is past the cut. + const d = film(said(3, 3.5), [{ id: "sp", startMs: 0, endMs: 4000, speed: 2 }], 4000); + expect(heard(d)).toEqual([{ startMs: 3000, endMs: 3500 }]); + }); + + it("drops the speech a non-final piece never plays", () => { + // 2x over 0–10 s: the first piece stops at output 2 s, where the second one starts. + const speed = [{ id: "sp", startMs: 0, endMs: 10000, speed: 2 }]; + expect(heard(film(said(3, 3.5), speed, 10000))).toEqual([]); + // Speech straddling that stop keeps only what is heard. + expect(heard(film(said(1.5, 2.5), speed, 10000))).toEqual([{ startMs: 1500, endMs: 2000 }]); + }); +}); diff --git a/src/lib/ai-edition/captions/cues.ts b/src/lib/ai-edition/captions/cues.ts index 19cbfbcbc0..76c6d0e4ae 100644 --- a/src/lib/ai-edition/captions/cues.ts +++ b/src/lib/ai-edition/captions/cues.ts @@ -22,7 +22,11 @@ import { splitMergedCaptionsByWordBounds, } from "@/lib/captioning/annotationsFromCaptions"; import type { CaptionSegment } from "@/lib/captioning/transcribe"; -import { readSpeedRegions } from "../document/timeline"; +import { + projectPlaybackSecToRawTimeline, + projectRawTimelineSecToPlayback, + readSpeedRegions, +} from "../document/timeline"; import type { AxcutDocument, AxcutTranscript } from "../schema"; import { lanePlacements, @@ -30,7 +34,6 @@ import { type TranscriptPlacement, } from "../timeline/aggregated-transcript"; import { removedRawSpans } from "../timeline/programme-time"; -import { type SpeedRegion, screenTimeMs, timelineTimeMs } from "../timeline/speed"; import { type CaptionAnchorV, type CaptionSettings, @@ -153,6 +156,12 @@ function translatedWordStream( }); } +/** Raw ruler ↔ output seconds, through the cuts and the speed regions. */ +export interface VoiceoverClock { + toOutputSec(rawSec: number): number; + toRulerSec(outSec: number): number; +} + /** * Map a source-time span onto the ruler through the clips that play it. * @@ -169,16 +178,31 @@ export function sourceSpanToTimelineSpans( * window and the ruler head, which both providers carry (issue #560). `AxcutClip` * stays structurally assignable, so every existing caller is unaffected. */ clips: TranscriptPlacement[], - /** Pass the speed regions for a VOICEOVER lane only (#1657). A take plays at 1x from - * its projected head, so a cue must sit at that head plus its source distance on the - * screen clock, then go back to the ruler the preview and the compositor project. A - * recording-lane cue keeps the plain ruler time: its picture and sound are stretched. */ - unstretchedSpeedRegions: readonly SpeedRegion[] = [], + /** Pass for a VOICEOVER lane only (#1657): the programme's output clock, cuts and speed + * included, the one the mixer lays each take piece on. A piece plays at 1x from its + * projected head and stops where the next piece of its take starts, so a cue sits at + * that head plus its source distance, back on the ruler. A recording-lane cue keeps + * the plain ruler time: its picture and sound are stretched together. */ + voiceoverClock?: VoiceoverClock, ): Array<{ startSec: number; endSec: number }> { const out: Array<{ startSec: number; endSec: number }> = []; - for (const clip of clips) { + for (const [i, clip] of clips.entries()) { if (clip.assetId !== assetId) continue; - const clipSourceEnd = clip.sourceEndSec ?? Number.POSITIVE_INFINITY; + let clipSourceEnd = clip.sourceEndSec ?? Number.POSITIVE_INFINITY; + const headOutSec = voiceoverClock?.toOutputSec(clip.timelineStartSec) ?? 0; + const next = clips[i + 1]; + // The next piece of the SAME take: same file, same raw-to-source shift. The mixer cuts + // this piece off at its projected head, so the speech after that is never heard. + if ( + voiceoverClock && + next?.assetId === clip.assetId && + Math.abs( + next.timelineStartSec - next.sourceStartSec - (clip.timelineStartSec - clip.sourceStartSec), + ) < 1e-6 + ) { + const audibleSec = voiceoverClock.toOutputSec(next.timelineStartSec) - headOutSec; + clipSourceEnd = Math.min(clipSourceEnd, clip.sourceStartSec + audibleSec); + } const s = Math.max(startSec, clip.sourceStartSec); const e = Math.min(endSec, clipSourceEnd); if (e <= s) continue; @@ -186,13 +210,10 @@ export function sourceSpanToTimelineSpans( // carrying an added word plays its media in pieces, and the seconds after the // insertion sit further along the ruler than their distance from the clip's start. // Writing the short version here is what put every caption after an insertion early. - const rawSec = (sec: number) => { - const raw = placementRawSec(clip, sec); - if (unstretchedSpeedRegions.length === 0) return raw; - const headScreenMs = screenTimeMs(unstretchedSpeedRegions, clip.timelineStartSec * 1000); - const distanceMs = (raw - clip.timelineStartSec) * 1000; - return timelineTimeMs(unstretchedSpeedRegions, headScreenMs + distanceMs) / 1000; - }; + const rawSec = (sec: number) => + voiceoverClock + ? voiceoverClock.toRulerSec(headOutSec + (sec - clip.sourceStartSec)) + : placementRawSec(clip, sec); out.push({ startSec: rawSec(s), endSec: rawSec(e) }); } // Onto the ruler the viewer actually sees. Expanding BOTH ends does the whole job: @@ -223,7 +244,17 @@ export function deriveCaptionCues( removedRawSpans(document.timeline.clips, document.timeline.trimRanges), ); if (placements.length === 0) return []; - const voiceoverSpeedRegions = lane === "voiceover" ? readSpeedRegions(document) : []; + const speedRegions = readSpeedRegions(document); + const { clips, trimRanges } = document.timeline; + const voiceoverClock: VoiceoverClock | undefined = + lane === "voiceover" + ? { + toOutputSec: (raw) => + projectRawTimelineSecToPlayback(clips, trimRanges, raw, speedRegions), + toRulerSec: (out) => + projectPlaybackSecToRawTimeline(clips, trimRanges, out, speedRegions), + } + : undefined; const transcripts = new Map(document.transcripts.map((t) => [t.assetId, t])); // What the film no longer contains is CLIPS-derived on both lanes, deliberately: a cut @@ -250,7 +281,7 @@ export function deriveCaptionCues( line.startSec, line.endSec, placements, - voiceoverSpeedRegions, + voiceoverClock, )) { const startMs = Math.round(span.startSec * 1000); const endMs = Math.max(Math.round(span.endSec * 1000), startMs + 1); diff --git a/src/lib/ai-edition/document/timeline.ts b/src/lib/ai-edition/document/timeline.ts index da05eca5e3..01b9cff0fd 100644 --- a/src/lib/ai-edition/document/timeline.ts +++ b/src/lib/ai-edition/document/timeline.ts @@ -342,6 +342,44 @@ export function projectRawTimelineSecToPlayback( return landed ?? outCursor + Math.max(0, rawSec - lastRawEnd); } +/** + * Inverse of {@link projectRawTimelineSecToPlayback}: the raw ruler second that plays at output + * second `outSec`. A moment on a cut's edge lands on the kept side that follows it. + */ +export function projectPlaybackSecToRawTimeline( + clips: AxcutClip[], + trimRanges: AxcutTrimRange[], + outSec: number, + speedRegions: PlaybackSpeedRegion[] = [], +): number { + const ordered = [...clips].sort((a, b) => a.timelineStartSec - b.timelineStartSec); + let outCursor = 0; + let lastRawEnd = 0; + for (const seg of keptRawSpans(ordered, trimRanges)) { + const segOut = outputDurationOfRawSpan(seg.startSec, seg.endSec, speedRegions); + if (outSec < outCursor + segOut) { + // Walk the constant-speed pieces the forward projection sums. + const edges = new Set([seg.startSec, seg.endSec]); + for (const region of speedRegions) { + for (const edge of [region.startMs / 1000, region.endMs / 1000]) { + if (edge > seg.startSec && edge < seg.endSec) edges.add(edge); + } + } + const sorted = [...edges].sort((a, b) => a - b); + let left = Math.max(0, outSec - outCursor); + for (let i = 0; i < sorted.length - 1; i++) { + const pieceOut = outputDurationOfRawSpan(sorted[i], sorted[i + 1], speedRegions); + if (left < pieceOut) return sorted[i] + (left / pieceOut) * (sorted[i + 1] - sorted[i]); + left -= pieceOut; + } + return seg.endSec; + } + outCursor += segOut; + lastRawEnd = seg.endSec; + } + return lastRawEnd + Math.max(0, outSec - outCursor); +} + export function invertIntervals(intervals: Interval[], durationSec: number): Interval[] { const cuts: Interval[] = []; let cursor = 0; From 4374bd8e8271cd3c1c33422f55537bce2a65e2be Mon Sep 17 00:00:00 2001 From: EtienneLescot Date: Mon, 23 Nov 2026 02:32:06 +0100 Subject: [PATCH 3/3] fix(captions): stop a voiceover piece's caption only at the next piece of its own take The audible-window clip found the next piece of a take by asset and raw-to-source shift, so a second take of the same file at the same shift cut the first take's captions short while its speech still played. Placements now carry their take id, and only a piece of the same take ends a piece's caption. Refs #1657 --- .../ai-edition/captions/captionLane.test.ts | 18 ++++++++++++++++++ src/lib/ai-edition/captions/cues.ts | 13 ++++--------- .../timeline/aggregated-transcript.ts | 3 +++ 3 files changed, 25 insertions(+), 9 deletions(-) diff --git a/src/lib/ai-edition/captions/captionLane.test.ts b/src/lib/ai-edition/captions/captionLane.test.ts index 7401ea5d51..d18b69f5b3 100644 --- a/src/lib/ai-edition/captions/captionLane.test.ts +++ b/src/lib/ai-edition/captions/captionLane.test.ts @@ -222,4 +222,22 @@ describe("voiceover captions under a cut and a speed region (#1657)", () => { // Speech straddling that stop keeps only what is heard. expect(heard(film(said(1.5, 2.5), speed, 10000))).toEqual([{ startMs: 1500, endMs: 2000 }]); }); + + it("never stops a take at the head of another take of the same file", () => { + // Take A plays source 0–4 from 0 s, take B source 3–5 from 3 s: same file, same shift. + // Nothing stops A at B's head, so A's word at source 2 s is heard at output 2 s. + const d = film(said(2, 2.4), [{ id: "sp", startMs: 0, endMs: 4000, speed: 2 }], 4000); + d.audioTracks = [ + ...d.audioTracks, + { + ...d.audioTracks[0], + id: "vo2", + trackId: "vo2", + startMs: 3000, + endMs: 5000, + offsetMs: 3000, + }, + ]; + expect(heard(d)).toEqual([{ startMs: 2000, endMs: 2400 }]); + }); }); diff --git a/src/lib/ai-edition/captions/cues.ts b/src/lib/ai-edition/captions/cues.ts index 76c6d0e4ae..d9f8ba2490 100644 --- a/src/lib/ai-edition/captions/cues.ts +++ b/src/lib/ai-edition/captions/cues.ts @@ -191,15 +191,10 @@ export function sourceSpanToTimelineSpans( let clipSourceEnd = clip.sourceEndSec ?? Number.POSITIVE_INFINITY; const headOutSec = voiceoverClock?.toOutputSec(clip.timelineStartSec) ?? 0; const next = clips[i + 1]; - // The next piece of the SAME take: same file, same raw-to-source shift. The mixer cuts - // this piece off at its projected head, so the speech after that is never heard. - if ( - voiceoverClock && - next?.assetId === clip.assetId && - Math.abs( - next.timelineStartSec - next.sourceStartSec - (clip.timelineStartSec - clip.sourceStartSec), - ) < 1e-6 - ) { + // The next piece of the SAME take. The mixer cuts this piece off at that piece's + // projected head, so the speech after it is never heard. Another take of the same + // file plays on its own and stops nothing. + if (voiceoverClock && clip.takeId !== undefined && next?.takeId === clip.takeId) { const audibleSec = voiceoverClock.toOutputSec(next.timelineStartSec) - headOutSec; clipSourceEnd = Math.min(clipSourceEnd, clip.sourceStartSec + audibleSec); } diff --git a/src/lib/ai-edition/timeline/aggregated-transcript.ts b/src/lib/ai-edition/timeline/aggregated-transcript.ts index 1527a7f371..a416a4b4f3 100644 --- a/src/lib/ai-edition/timeline/aggregated-transcript.ts +++ b/src/lib/ai-edition/timeline/aggregated-transcript.ts @@ -43,6 +43,8 @@ export interface TranscriptPlacement { /** Where the window lands on the RAW ruler. Source time is per asset, so this is * the only thing that turns a word back into a moment the playhead can seek to. */ timelineStartSec: number; + /** The voiceover take this placement is a piece of. Absent on a recording clip. */ + takeId?: string; } /** Which lane's speech the transcript tab is reading. */ @@ -338,6 +340,7 @@ export function voiceoverPlacements( sourceStartSec: piece.sourceStartSec, sourceEndSec: piece.sourceEndSec, timelineStartSec: piece.rawStartSec, + takeId: pill.id, })), ); }