Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 99 additions & 0 deletions src/lib/ai-edition/captions/captionLane.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 }]);
});
});
51 changes: 47 additions & 4 deletions src/lib/ai-edition/captions/cues.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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.
*
Expand All @@ -167,19 +178,38 @@ 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;
// Through `placementRawSec`, never the subtraction it used to write here: a clip
// 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) });
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
}
// Onto the ruler the viewer actually sees. Expanding BOTH ends does the whole job:
return out;
Expand All @@ -199,15 +229,27 @@ 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.
document.audioTracks ?? [],
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
Expand All @@ -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);
Expand Down
38 changes: 38 additions & 0 deletions src/lib/ai-edition/document/timeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<number>([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;
Expand Down
3 changes: 3 additions & 0 deletions src/lib/ai-edition/timeline/aggregated-transcript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down Expand Up @@ -338,6 +340,7 @@ export function voiceoverPlacements(
sourceStartSec: piece.sourceStartSec,
sourceEndSec: piece.sourceEndSec,
timelineStartSec: piece.rawStartSec,
takeId: pill.id,
})),
);
}
Expand Down
Loading