diff --git a/src/lib/ai-edition/captions/captionLane.test.ts b/src/lib/ai-edition/captions/captionLane.test.ts index 566f8781f5..d18b69f5b3 100644 --- a/src/lib/ai-edition/captions/captionLane.test.ts +++ b/src/lib/ai-edition/captions/captionLane.test.ts @@ -4,7 +4,9 @@ // 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"; import { DEFAULT_CAPTION_SETTINGS, @@ -142,3 +144,100 @@ 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 }]); + }); +}); + +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 }]); + }); + + 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 bde765a465..d9f8ba2490 100644 --- a/src/lib/ai-edition/captions/cues.ts +++ b/src/lib/ai-edition/captions/cues.ts @@ -22,6 +22,11 @@ import { splitMergedCaptionsByWordBounds, } from "@/lib/captioning/annotationsFromCaptions"; import type { CaptionSegment } from "@/lib/captioning/transcribe"; +import { + projectPlaybackSecToRawTimeline, + projectRawTimelineSecToPlayback, + readSpeedRegions, +} from "../document/timeline"; import type { AxcutDocument, AxcutTranscript } from "../schema"; import { lanePlacements, @@ -151,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. * @@ -167,11 +178,26 @@ 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 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. 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); + } const s = Math.max(startSec, clip.sourceStartSec); const e = Math.min(endSec, clipSourceEnd); if (e <= s) continue; @@ -179,7 +205,11 @@ 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) => + 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: return out; @@ -199,8 +229,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 +239,17 @@ export function deriveCaptionCues( removedRawSpans(document.timeline.clips, document.timeline.trimRanges), ); if (placements.length === 0) return []; + 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 @@ -234,6 +276,7 @@ export function deriveCaptionCues( line.startSec, line.endSec, placements, + 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; 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, })), ); }