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(editor): record voiceovers against the uncut timeline (#1195)

mikemikimike2159f42ca184
diff --git a/src/components/ai-edition/NativeCompositorOverlay.test.tsx b/src/components/ai-edition/NativeCompositorOverlay.test.tsx
index ff3522665..0f1d5c16e 100644
--- a/src/components/ai-edition/NativeCompositorOverlay.test.tsx
+++ b/src/components/ai-edition/NativeCompositorOverlay.test.tsx
@@ -18,6 +18,7 @@ import { publishNativePosition } from "@/native/nativeSync";
const native = vi.hoisted(() => ({
setActiveClip: vi.fn(async () => ({ ok: true })),
setNativePlaying: vi.fn(),
+ setNativeScene: vi.fn(),
}));
vi.mock("@/native", () => ({
@@ -25,7 +26,7 @@ vi.mock("@/native", () => ({
setActiveClip: native.setActiveClip,
setCurrentNativeViewId: vi.fn(),
setNativePlaying: native.setNativePlaying,
- setNativeScene: vi.fn(),
+ setNativeScene: native.setNativeScene,
subscribeNativeCompositor: () => () => undefined,
useIsCpuCompositor: () => false,
useNativeCompositorView: () => ({ viewId: 7, error: null }),
@@ -94,6 +95,29 @@ function makeDocument(): AxcutDocument {
};
}
+function makeTrimmedDocument(): AxcutDocument {
+ const document = makeDocument();
+ return {
+ ...document,
+ timeline: {
+ ...document.timeline,
+ clips: [{ ...FIRST, sourceEndSec: 10, timelineEndSec: 10 }],
+ trimRanges: [
+ {
+ id: "trim_a",
+ assetId: "asset_take",
+ clipId: "clip_a",
+ startSec: 4,
+ endSec: 6,
+ reason: "",
+ origin: "user",
+ },
+ ],
+ },
+ legacyEditor: { speedRegions: [{ id: "speed_a", startMs: 0, endMs: 10_000, speed: 2 }] },
+ };
+}
+
function setPlayhead(currentTimeSec: number, playing: boolean) {
act(() => {
useProjectStore.setState({ currentTimeSec, playing });
@@ -248,4 +272,34 @@ describe("NativeCompositorOverlay while playing", () => {
expect(native.setNativePlaying).toHaveBeenCalledWith(false);
expect(native.setActiveClip).toHaveBeenCalledTimes(1);
});
+
+ it("uses an untrimmed 1x scene while recording and restores the edited scene afterward", async () => {
+ useProjectStore.setState({ document: makeTrimmedDocument(), currentTimeSec: 8 });
+ const view = render(<NativeCompositorOverlay />);
+ await act(async () => {
+ await Promise.resolve();
+ });
+ native.setActiveClip.mockClear();
+ native.setNativeScene.mockClear();
+
+ view.rerender(<NativeCompositorOverlay playUneditedTimeline />);
+ await act(async () => {
+ await Promise.resolve();
+ });
+ const recordingScene = JSON.parse(native.setNativeScene.mock.lastCall?.[0] ?? "{}");
+ expect(recordingScene.clips).toHaveLength(1);
+ expect(recordingScene.speedRegions).toEqual([]);
+ expect(native.setActiveClip).toHaveBeenCalledWith(7, "/take.mp4", "", 0, 0, 8);
+
+ native.setActiveClip.mockClear();
+ native.setNativeScene.mockClear();
+ view.rerender(<NativeCompositorOverlay />);
+ await act(async () => {
+ await Promise.resolve();
+ });
+ const editedScene = JSON.parse(native.setNativeScene.mock.lastCall?.[0] ?? "{}");
+ expect(editedScene.clips).toHaveLength(2);
+ expect(editedScene.speedRegions.length).toBeGreaterThan(0);
+ expect(native.setActiveClip).toHaveBeenCalledWith(7, "/take.mp4", "", 0, 1, 8);
+ });
});
diff --git a/src/components/ai-edition/NativeCompositorOverlay.tsx b/src/components/ai-edition/NativeCompositorOverlay.tsx
index d60bbcd20..8d5326989 100644
--- a/src/components/ai-edition/NativeCompositorOverlay.tsx
+++ b/src/components/ai-edition/NativeCompositorOverlay.tsx
@@ -55,7 +55,11 @@ import {
* calques interactifs (zoom gimbal, annotations, drag webcam) qui restent des
* éléments DOM cliquables au-dessus.
*/
-export function NativeCompositorOverlay() {
+export function NativeCompositorOverlay({
+ playUneditedTimeline = false,
+}: {
+ playUneditedTimeline?: boolean;
+}) {
const canvasRef = useRef<HTMLCanvasElement>(null);
const previousActiveClipIdRef = useRef<string | null>(null);
const document = useProjectStore((s) => s.document);
@@ -70,22 +74,25 @@ export function NativeCompositorOverlay() {
() => 0,
);
- // `currentTimeSec` est en temps RAW/document (la règle où les trims occupent encore leur
- // place — même référentiel que le playhead V4 et l'overlay webcam web). Le natif, lui, joue
- // les segments COMPACTÉS (`resolveVisibleClips`, trims retirés) — le même flux que la SCÈNE
- // envoyée via `setNativeScene`. `resolveNativePosition` fait le pont : il mappe le playhead
- // RAW → temps source via le layout RAW (`document.timeline.clips`) PUIS localise le segment
- // compacté correspondant, d'où sortent le `clipIndex` natif et la bonne paire écran/webcam.
- // Sans ça (ancien `resolveNativePlaybackPosition(nativeClips, currentTimeSec)`), un playhead
- // RAW lu contre des clips compactés désignait le mauvais clip après un trim → mauvaise caméra
- // + décalage écran/cam.
- const nativeClips = useMemo(() => {
- if (!document) return [];
- return resolveVisibleClips(document);
- }, [document]);
+ // `currentTimeSec` stays on the raw document ruler. During normal playback the native scene
+ // uses trim-compressed segments; while recording, `playbackDocument` removes trims and speed
+ // regions so the compositor shows the same unedited timeline as the video clock.
+ const playbackDocument = useMemo(() => {
+ if (!document || !playUneditedTimeline) return document;
+ return {
+ ...document,
+ timeline: { ...document.timeline, trimRanges: [] },
+ legacyEditor: document.legacyEditor ? { ...document.legacyEditor, speedRegions: [] } : null,
+ };
+ }, [document, playUneditedTimeline]);
+ const nativeClips = useMemo(
+ () => (playbackDocument ? resolveVisibleClips(playbackDocument) : []),
+ [playbackDocument],
+ );
const activePosition = useMemo(
- () => resolveNativePosition(currentTimeSec, nativeClips, document?.timeline.clips ?? []),
- [nativeClips, currentTimeSec, document],
+ () =>
+ resolveNativePosition(currentTimeSec, nativeClips, playbackDocument?.timeline.clips ?? []),
+ [nativeClips, currentTimeSec, playbackDocument],
);
const activeClip = activePosition?.clip ?? null;
@@ -149,12 +156,12 @@ export function NativeCompositorOverlay() {
// quand on pousse.
// biome-ignore lint/correctness/useExhaustiveDependencies: size revision
useEffect(() => {
- if (viewId === null || !document) {
+ if (viewId === null || !playbackDocument) {
return;
}
try {
const webcamSourceSize = cameraPath ? getWebcamNativeSize(cameraPath) : null;
- const scene = buildSceneDescription(document, webcamSourceSize);
+ const scene = buildSceneDescription(playbackDocument, webcamSourceSize);
setNativeScene(JSON.stringify(scene));
} catch (error) {
console.warn("[compositor-view] build/push scene failed:", error);
@@ -164,7 +171,7 @@ export function NativeCompositorOverlay() {
// re-read fresh via getWebcamNativeSize() above on every run (biome flags this as
// an "unnecessary" dependency, but removing it would mean a probed webcam size
// arriving after mount never gets pushed to native).
- }, [viewId, document, sources, _webcamSizeRevision]);
+ }, [viewId, playbackDocument, sources, _webcamSizeRevision]);
// SYNCHRO COMPLETE DES PARAMS, en un seul endroit.
//
@@ -195,14 +202,17 @@ export function NativeCompositorOverlay() {
const activeSourceTimeSec = activePosition?.sourceTimeSec ?? null;
// Matched on the raw ruler, like `currentTimeSec` and the regions' pills.
const speedRegions = useMemo(
- () => (document ? readSpeedRegions<SpeedRegion>(document) : []),
- [document],
+ () => (playbackDocument ? readSpeedRegions<SpeedRegion>(playbackDocument) : []),
+ [playbackDocument],
);
- const playbackSpeed = findActiveSpeedRegion(speedRegions, currentTimeSec * 1000)?.speed ?? 1;
+ const playbackSpeed = playUneditedTimeline
+ ? 1
+ : (findActiveSpeedRegion(speedRegions, currentTimeSec * 1000)?.speed ?? 1);
const pendingTargetClipIdRef = useRef<string | null>(null);
const playing = useProjectStore((s) => s.playing);
const wasPlayingRef = useRef(playing);
+ const previousUneditedTimelineRef = useRef(playUneditedTimeline);
// Change les décodeurs screen/webcam quand le playhead entre dans un autre clip, à l'arrêt.
//
@@ -214,6 +224,8 @@ export function NativeCompositorOverlay() {
useEffect(() => {
const justPaused = wasPlayingRef.current && !playing;
wasPlayingRef.current = playing;
+ const playbackModeChanged = previousUneditedTimelineRef.current !== playUneditedTimeline;
+ previousUneditedTimelineRef.current = playUneditedTimeline;
if (
viewId === null ||
!document ||
@@ -230,7 +242,7 @@ export function NativeCompositorOverlay() {
// y retourne, sans quoi le recalage en temps (`setNativeTime`) chercherait dans le
// mauvais fichier.
const nativeElsewhere = justPaused && native !== null && native.clipIndex !== activeClipIndex;
- if (!clipChanged && !nativeElsewhere) {
+ if (!clipChanged && !playbackModeChanged && !nativeElsewhere) {
return;
}
const asset = document.assets.find((candidate) => candidate.id === activeClip.assetId);
@@ -305,6 +317,7 @@ export function NativeCompositorOverlay() {
activeClipIndex,
activeSourceTimeSec,
playing,
+ playUneditedTimeline,
nativeClips,
playbackSpeed,
]);
diff --git a/src/components/ai-edition/NewEditorShell.tsx b/src/components/ai-edition/NewEditorShell.tsx
index f26a56c72..d87a215ea 100644
--- a/src/components/ai-edition/NewEditorShell.tsx
+++ b/src/components/ai-edition/NewEditorShell.tsx
@@ -106,15 +106,22 @@ const NO_AUDIO_TRACKS: AxcutAudioTrack[] = [];
function NativePlaybackSync({
visibleClips,
clips,
+ playUneditedTimeline,
}: {
visibleClips: AxcutClip[];
clips: AxcutClip[];
+ playUneditedTimeline: boolean;
}) {
const playing = useProjectStore((s) => s.playing);
const currentTimeSec = useProjectStore((s) => s.currentTimeSec);
- // visibleClips = trim-compressed native stream; `clips` = RAW layout currentTimeSec
- // is measured against. resolveNativePosition needs both (see timelineMap).
- useNativePlaybackSync(playing, currentTimeSec, visibleClips, clips);
+ // The native stream follows the edited timeline except while a voiceover records.
+ // `currentTimeSec` always stays on the raw ruler, including during playback.
+ useNativePlaybackSync(
+ playing,
+ currentTimeSec,
+ playUneditedTimeline ? clips : visibleClips,
+ clips,
+ );
return null;
}
@@ -1012,12 +1019,20 @@ export function NewEditorShell() {
// where it is passed to the preview.
const [voiceoverRecording, setVoiceoverRecording] = useState(false);
- const handleVoiceoverRecordingStart = useCallback(() => {
+ const handleVoiceoverRecordingStart = useCallback(async () => {
// Re-capture: the user may have scrubbed between opening the dialog and
// hitting Record, and playback starts from wherever the playhead is now.
- voiceoverStartSecRef.current = useProjectStore.getState().currentTimeSec;
+ const doc = useProjectStore.getState().document;
+ const playhead = useProjectStore.getState().currentTimeSec;
+ voiceoverStartSecRef.current = playhead;
+ if (doc) {
+ const total = doc.timeline.clips.reduce((max, c) => Math.max(max, c.timelineEndSec), 0);
+ setVoiceoverFlow({ maxDurationSec: Math.max(0.5, total - playhead) });
+ }
setVoiceoverRecording(true);
- if (videoElement?.paused) void videoElement.play().catch(() => undefined);
+ if (!videoElement) throw new Error("The preview is not ready.");
+ videoElement.playbackRate = 1;
+ if (videoElement.paused) await videoElement.play();
}, [videoElement]);
const handleVoiceoverRecordingStop = useCallback(() => {
@@ -1028,7 +1043,8 @@ export function NewEditorShell() {
const handleVoiceoverReady = useCallback(
async (assetId: string, durationSec: number) => {
setVoiceoverFlow(null);
- await tl.addAudioTrack(assetId, voiceoverStartSecRef.current, {
+ const startSec = voiceoverStartSecRef.current;
+ await tl.addAudioTrack(assetId, startSec, {
kind: "voiceover",
durationSec,
spanSec: durationSec,
@@ -1558,7 +1574,11 @@ export function NewEditorShell() {
className={v4.app}
style={{ gridTemplateRows: `58px 1fr ${showTimeline ? timelineRow : "0px"}` }}
>
- <NativePlaybackSync visibleClips={visibleClips} clips={clips} />
+ <NativePlaybackSync
+ visibleClips={visibleClips}
+ clips={clips}
+ playUneditedTimeline={voiceoverRecording}
+ />
<EditorTopBar
mode={mode}
onModeChange={setMode}
@@ -1653,6 +1673,7 @@ export function NewEditorShell() {
clips={clips}
zoomRegions={tl.zoomRegions}
speedRegions={tl.speedRegions}
+ playUneditedTimeline={voiceoverRecording}
cameraFullscreenRegions={tl.cameraFullscreenRegions}
trimRanges={tl.trimRanges}
selectedZoomRegionId={tl.selection?.kind === "zoom" ? tl.selection.id : null}
diff --git a/src/components/ai-edition/NewEditorShell.voiceover.test.tsx b/src/components/ai-edition/NewEditorShell.voiceover.test.tsx
new file mode 100644
index 000000000..8f43df0f3
--- /dev/null
+++ b/src/components/ai-edition/NewEditorShell.voiceover.test.tsx
@@ -0,0 +1,251 @@
+// @vitest-environment jsdom
+import "@testing-library/jest-dom";
+import { act, cleanup, fireEvent, render, screen, waitFor } from "@testing-library/react";
+import { createElement } from "react";
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+import { TooltipProvider } from "@/components/ui/tooltip";
+import { EditorDialogsProvider } from "@/contexts/EditorDialogsContext";
+import type { AxcutAsset, AxcutClip, AxcutDocument } from "@/lib/ai-edition/schema";
+import { createEmptyDocument } from "@/lib/ai-edition/schema";
+import { useProjectStore } from "@/lib/ai-edition/store/projectStore";
+import { buildSceneDescription } from "@/native/sceneDescription";
+
+const mocks = vi.hoisted(() => ({
+ nativePlaybackSync: vi.fn(),
+ previewPlay: vi.fn(async () => undefined),
+}));
+
+vi.mock("@/contexts/ShortcutsContext", async () => {
+ const { DEFAULT_SHORTCUTS } = await import("@/lib/shortcuts");
+ return {
+ useShortcuts: () => ({
+ shortcuts: DEFAULT_SHORTCUTS,
+ isMac: false,
+ isConfigOpen: false,
+ openConfig: vi.fn(),
+ closeConfig: vi.fn(),
+ setShortcuts: vi.fn(),
+ persistShortcuts: () => Promise.resolve(true),
+ }),
+ };
+});
+
+vi.mock("@/contexts/I18nContext", () => ({
+ useI18n: () => ({ locale: "en", setLocale: vi.fn() }),
+ useScopedT: () => (key: string) => key,
+}));
+
+vi.mock("@/native/useNativePlaybackSync", () => ({
+ useNativePlaybackSync: mocks.nativePlaybackSync,
+}));
+
+vi.mock("./Preview", async () => {
+ const { createElement, useEffect } = await import("react");
+ return {
+ Preview: ({
+ playUneditedTimeline,
+ onVideoElement,
+ }: {
+ playUneditedTimeline?: boolean;
+ onVideoElement: (element: HTMLVideoElement | null) => void;
+ }) => {
+ useEffect(() => {
+ const element = document.createElement("video");
+ Object.defineProperty(element, "paused", { configurable: true, value: true });
+ element.play = mocks.previewPlay;
+ element.pause = vi.fn();
+ onVideoElement(element);
+ return () => onVideoElement(null);
+ }, [onVideoElement]);
+ return createElement("div", {
+ "data-testid": "preview-recording-mode",
+ "data-unedited": String(playUneditedTimeline ?? false),
+ });
+ },
+ };
+});
+
+vi.mock("./v4/V4Timeline", () => ({
+ V4Timeline: ({ onAddVoiceover }: { onAddVoiceover: () => void }) =>
+ createElement("button", { type: "button", onClick: onAddVoiceover }, "open-voiceover"),
+}));
+
+vi.mock("./v4/AddAudioLayerDialog", () => ({
+ AddAudioLayerDialog: ({
+ open,
+ onComplete,
+ onRecordingStart,
+ onRecordingStop,
+ }: {
+ open: boolean;
+ onComplete: (assetId: string, durationSec: number) => void;
+ onRecordingStart: () => void | Promise<void>;
+ onRecordingStop: () => void;
+ }) =>
+ open
+ ? createElement(
+ "div",
+ null,
+ createElement("button", { type: "button", onClick: onRecordingStart }, "start-recording"),
+ createElement("button", { type: "button", onClick: onRecordingStop }, "stop-recording"),
+ createElement(
+ "button",
+ { type: "button", onClick: () => onComplete("voice", 4) },
+ "complete-recording",
+ ),
+ )
+ : null,
+}));
+
+import { NewEditorShell } from "./NewEditorShell";
+
+function makeDocument(): AxcutDocument {
+ const document = createEmptyDocument({ projectId: "project_voiceover", title: "Voiceover" });
+ document.project.primaryAssetId = "screen";
+ const screenAsset: AxcutAsset = {
+ id: "screen",
+ kind: "video",
+ label: "screen",
+ originalPath: "/screen.mp4",
+ durationSec: 10,
+ cameraTrack: null,
+ };
+ const voiceAsset: AxcutAsset = {
+ id: "voice",
+ kind: "audio",
+ label: "voice",
+ originalPath: "/voice.webm",
+ durationSec: 4,
+ cameraTrack: null,
+ };
+ const clip: AxcutClip = {
+ id: "clip",
+ assetId: screenAsset.id,
+ sourceStartSec: 0,
+ sourceEndSec: 10,
+ timelineStartSec: 0,
+ timelineEndSec: 10,
+ wordRefs: [],
+ origin: "system",
+ reason: "",
+ };
+ document.assets = [screenAsset, voiceAsset];
+ document.timeline.clips = [clip];
+ document.timeline.trimRanges = [
+ {
+ id: "trim",
+ assetId: screenAsset.id,
+ clipId: clip.id,
+ startSec: 3,
+ endSec: 5,
+ reason: "",
+ origin: "user",
+ },
+ ];
+ return document;
+}
+
+function renderShell() {
+ return render(
+ <TooltipProvider>
+ <EditorDialogsProvider>
+ <NewEditorShell />
+ </EditorDialogsProvider>
+ </TooltipProvider>,
+ );
+}
+
+describe("NewEditorShell voiceover placement", () => {
+ beforeEach(() => {
+ localStorage.clear();
+ vi.clearAllMocks();
+ (window as unknown as { electronAPI?: unknown }).electronAPI = {
+ onAiEditionChatEvent: () => () => undefined,
+ sendAiEditionChatPrompt: () => undefined,
+ setTitleBarOverlay: () => undefined,
+ setHasUnsavedChanges: () => undefined,
+ onRequestCloseConfirm: () => () => undefined,
+ onRequestSaveBeforeClose: () => () => undefined,
+ sendCloseConfirmResponse: () => undefined,
+ findRecordingCamera: () => Promise.resolve(null),
+ preparePreviewAudioTrack: () => Promise.resolve(null),
+ isAppPackaged: () => false,
+ };
+ Element.prototype.scrollTo = () => undefined;
+ (globalThis as unknown as { ResizeObserver?: unknown }).ResizeObserver = class {
+ observe = vi.fn();
+ unobserve = vi.fn();
+ disconnect = vi.fn();
+ };
+ const document = makeDocument();
+ const saveDocument = vi.fn(async (next: AxcutDocument) => {
+ useProjectStore.setState({
+ document: next,
+ revision: 2,
+ dirty: true,
+ lastSavedAt: new Date(),
+ });
+ return true;
+ });
+ useProjectStore.setState({
+ projectId: document.project.id,
+ document,
+ revision: 1,
+ status: "ready",
+ error: null,
+ sourceDurationSec: 10,
+ currentTimeSec: 2,
+ playing: false,
+ dirty: false,
+ lastSavedAt: new Date(),
+ saveDocument,
+ });
+ });
+
+ afterEach(() => {
+ cleanup();
+ localStorage.clear();
+ useProjectStore.getState().clear();
+ });
+
+ it("adds a full-duration plain track across a cut and lets the cut mute its middle", async () => {
+ const document = useProjectStore.getState().document;
+ if (!document) throw new Error("document was not initialized");
+ const rawClips = document.timeline.clips;
+ renderShell();
+
+ expect(mocks.nativePlaybackSync.mock.lastCall?.[2]).toHaveLength(2);
+ fireEvent.click(screen.getByRole("button", { name: "open-voiceover" }));
+ fireEvent.click(screen.getByRole("button", { name: "start-recording" }));
+
+ expect(screen.getByTestId("preview-recording-mode")).toHaveAttribute("data-unedited", "true");
+ expect(mocks.previewPlay).toHaveBeenCalledOnce();
+ expect(mocks.nativePlaybackSync).toHaveBeenLastCalledWith(false, 2, rawClips, rawClips);
+
+ act(() => {
+ useProjectStore.setState({ currentTimeSec: 4 });
+ });
+ fireEvent.click(screen.getByRole("button", { name: "stop-recording" }));
+ expect(mocks.nativePlaybackSync.mock.lastCall?.[2]).toHaveLength(2);
+ fireEvent.click(screen.getByRole("button", { name: "complete-recording" }));
+
+ await waitFor(() => {
+ expect(useProjectStore.getState().document?.audioTracks).toHaveLength(1);
+ });
+ const updatedDocument = useProjectStore.getState().document;
+ if (!updatedDocument) throw new Error("voiceover document was not saved");
+ const [track] = updatedDocument.audioTracks;
+ expect(track).toMatchObject({
+ assetId: "voice",
+ kind: "voiceover",
+ startMs: 2_000,
+ endMs: 6_000,
+ durationSec: 4,
+ });
+ expect(track).not.toHaveProperty("recordingPlaybackMap");
+ expect(buildSceneDescription(updatedDocument).audioTracks).toMatchObject([
+ { startSec: 2, trimStartSec: 0, trimEndSec: 1, kind: "voiceover" },
+ { startSec: 3, trimStartSec: 3, trimEndSec: 4, kind: "voiceover" },
+ ]);
+ });
+});
diff --git a/src/components/ai-edition/Preview.tsx b/src/components/ai-edition/Preview.tsx
index f4ad0a5c5..c809a329b 100644
--- a/src/components/ai-edition/Preview.tsx
+++ b/src/components/ai-edition/Preview.tsx
@@ -37,6 +37,7 @@ interface PreviewProps {
clips: AxcutClip[];
zoomRegions?: AxcutZoomRegion[];
speedRegions?: SpeedRegion[];
+ playUneditedTimeline?: boolean;
cameraFullscreenRegions?: CameraFullscreenRegion[];
trimRanges?: AxcutTrimRange[];
transcripts?: AxcutTranscript[];
@@ -72,6 +73,7 @@ export function Preview({
clips,
zoomRegions,
speedRegions,
+ playUneditedTimeline,
cameraFullscreenRegions,
trimRanges,
transcripts,
@@ -215,6 +217,7 @@ export function Preview({
clips={clips}
zoomRegions={zoomRegions}
speedRegions={speedRegions}
+ playUneditedTimeline={playUneditedTimeline}
cameraFullscreenRegions={cameraFullscreenRegions}
trimRanges={trimRanges}
transcripts={transcripts}
diff --git a/src/components/ai-edition/PreviewCanvas.tsx b/src/components/ai-edition/PreviewCanvas.tsx
index c817fde3c..557593f4c 100644
--- a/src/components/ai-edition/PreviewCanvas.tsx
+++ b/src/components/ai-edition/PreviewCanvas.tsx
@@ -85,6 +85,7 @@ interface PreviewCanvasProps {
clips: AxcutClip[];
zoomRegions?: AxcutZoomRegion[];
speedRegions?: SpeedRegion[];
+ playUneditedTimeline?: boolean;
cameraFullscreenRegions?: CameraFullscreenRegion[];
trimRanges?: AxcutTrimRange[];
/** Relayed to `VirtualPreview` so a clip carrying added words plays their extensions. */
@@ -124,6 +125,7 @@ interface PreviewCanvasProps {
// (the <video> contain-fits its true ratio within a box sized for the wrong
// one), on top of the intentional `settings.padding` margin.
const SCREEN_SOURCE_SIZE = { width: 1920, height: 1080 };
+const NO_SPEED_REGIONS: SpeedRegion[] = [];
export function PreviewCanvas(props: PreviewCanvasProps) {
const te = useScopedT("editor");
@@ -314,14 +316,17 @@ export function PreviewCanvas(props: PreviewCanvasProps) {
// Full Camera: during a cameraFullscreen region the webcam takes the whole
// frame and eases back. Same `computeCameraFullscreenRect` call as both
// exporters and the native compositor, so all four animate identically.
+ const cameraFullscreenSpeedRegions = props.playUneditedTimeline
+ ? NO_SPEED_REGIONS
+ : props.speedRegions;
const cameraFullscreenProgress = useMemo(
() =>
computeCameraFullscreenProgress(
props.cameraFullscreenRegions ?? [],
Math.round(props.currentTimeSec * 1000),
- props.speedRegions,
+ cameraFullscreenSpeedRegions,
),
- [props.cameraFullscreenRegions, props.currentTimeSec, props.speedRegions],
+ [props.cameraFullscreenRegions, props.currentTimeSec, cameraFullscreenSpeedRegions],
);
const effectiveLayout = useMemo<WebcamCompositeLayout | null>(() => {
if (!layout?.webcamRect || cameraFullscreenProgress <= 0) return layout;
@@ -443,7 +448,7 @@ export function PreviewCanvas(props: PreviewCanvasProps) {
interactive-only layers (ZoomFocusOverlay, AnnotationLayer, webcam drag
hitbox) still render on top as normal DOM so they stay clickable. No more
dual preview path. */}
- <NativeCompositorOverlay />
+ <NativeCompositorOverlay playUneditedTimeline={props.playUneditedTimeline} />
{layout?.screenRect ? (
<div className={styles.screenStage} style={screenStyle}>
{(() => {
diff --git a/src/components/ai-edition/VirtualPreview.playback.test.tsx b/src/components/ai-edition/VirtualPreview.playback.test.tsx
index c2be74392..becfb077a 100644
--- a/src/components/ai-edition/VirtualPreview.playback.test.tsx
+++ b/src/components/ai-edition/VirtualPreview.playback.test.tsx
@@ -60,6 +60,7 @@ function clip(
* recorded rather than performed (jsdom implements neither). */
function driveVideo(element: HTMLVideoElement) {
let currentTime = 0;
+ let playbackRate = 1;
let paused = true;
const pauseCalls: number[] = [];
Object.defineProperty(element, "currentTime", {
@@ -70,6 +71,13 @@ function driveVideo(element: HTMLVideoElement) {
},
});
Object.defineProperty(element, "paused", { configurable: true, get: () => paused });
+ Object.defineProperty(element, "playbackRate", {
+ configurable: true,
+ get: () => playbackRate,
+ set: (next: number) => {
+ playbackRate = next;
+ },
+ });
Object.defineProperty(element, "readyState", { configurable: true, get: () => 4 });
Object.defineProperty(element, "duration", { configurable: true, get: () => 10 });
element.play = vi.fn(() => {
@@ -94,7 +102,12 @@ function driveVideo(element: HTMLVideoElement) {
};
}
-function mount(clips: AxcutClip[], trimRanges: AxcutTrimRange[] = []) {
+function mount(
+ clips: AxcutClip[],
+ trimRanges: AxcutTrimRange[] = [],
+ speedRegions: SpeedRegion[] = [],
+ playUneditedTimeline = false,
+) {
const onTimeChange = vi.fn();
const sources: VideoSource[] = [{ id: "a1", src: "file:///tmp/a1.mp4", label: "a1" }];
const { container } = render(
@@ -102,6 +115,8 @@ function mount(clips: AxcutClip[], trimRanges: AxcutTrimRange[] = []) {
videoSources={sources}
clips={clips}
trimRanges={trimRanges}
+ speedRegions={speedRegions}
+ playUneditedTimeline={playUneditedTimeline}
onTimeChange={onTimeChange}
/>,
);
@@ -207,6 +222,31 @@ describe("VirtualPreview playback across a clip boundary", () => {
expect(video.currentTime).toBe(6); // jumped to where clip_1's content resumes
expect(video.pauseCalls).toHaveLength(0);
});
+
+ it("plays through cuts at 1x while recording a voiceover", () => {
+ const clips = [clip("clip_1", "a1", 0, 10, 0)];
+ const trims: AxcutTrimRange[] = [
+ {
+ id: "trim_1",
+ assetId: "a1",
+ clipId: "clip_1",
+ startSec: 4,
+ endSec: 6,
+ origin: "user",
+ reason: "",
+ },
+ ];
+ const speeds: SpeedRegion[] = [{ id: "speed_1", startMs: 0, endMs: 10_000, speed: 2 }];
+ const { onTimeChange, video, element } = mount(clips, trims, speeds, true);
+
+ video.play();
+ video.seekTo(5);
+ tick();
+
+ expect(video.currentTime).toBe(5);
+ expect(element.playbackRate).toBe(1);
+ expect(reportedTimes(onTimeChange).at(-1)).toBeCloseTo(5, 5);
+ });
});
// Issue #350 — imported audio tracks follow the RAW virtual playhead. The
diff --git a/src/components/ai-edition/VirtualPreview.tsx b/src/components/ai-edition/VirtualPreview.tsx
index 19c0fb117..3481e2368 100644
--- a/src/components/ai-edition/VirtualPreview.tsx
+++ b/src/components/ai-edition/VirtualPreview.tsx
@@ -452,6 +452,8 @@ interface VirtualPreviewProps {
zoomRegions?: AxcutZoomRegion[];
speedRegions?: SpeedRegion[];
trimRanges?: AxcutTrimRange[];
+ /** Play the raw timeline continuously while capturing a voiceover. */
+ playUneditedTimeline?: boolean;
seekTarget?: { timeSec: number; isSource?: boolean; requestId: number } | null;
onTimeChange?: (timeSec: number) => void;
onLoadedMetadata?: (
@@ -484,6 +486,9 @@ interface VirtualPreviewProps {
cropRegion?: CropRegion | null;
}
+const NO_TRIM_RANGES: AxcutTrimRange[] = [];
+const NO_SPEED_REGIONS: SpeedRegion[] = [];
+
export function VirtualPreview({
videoSources,
audioTracks = [],
@@ -492,6 +497,7 @@ export function VirtualPreview({
zoomRegions = [],
speedRegions = [],
trimRanges = [],
+ playUneditedTimeline = false,
seekTarget,
onTimeChange,
onLoadedMetadata,
@@ -503,6 +509,8 @@ export function VirtualPreview({
cropRegion,
}: VirtualPreviewProps) {
const { settings } = useEditorSettings();
+ const playbackTrimRanges = playUneditedTimeline ? NO_TRIM_RANGES : trimRanges;
+ const playbackSpeedRegions = playUneditedTimeline ? NO_SPEED_REGIONS : speedRegions;
// ponytail: an oversized, offset video inside .videoFrame's overflow:hidden
// box — the same "scale + negative-position the full frame, let the
// container clip the rest" technique the export renderer uses via a Pixi
@@ -866,8 +874,8 @@ export function VirtualPreview({
// Same reason as `clipsRef`: the rAF projects the playhead and each imported
// audio track's head raw→output every frame (see the audio-track loop), and
// must see the live trims, not the set captured when the loop was created.
- const trimRangesRef = useRef(trimRanges);
- trimRangesRef.current = trimRanges;
+ const trimRangesRef = useRef(playbackTrimRanges);
+ trimRangesRef.current = playbackTrimRanges;
// 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
@@ -877,18 +885,21 @@ export function VirtualPreview({
const takePiecesRef = useRef<Map<string, TakePiece[]>>(new Map());
// Each take keyed by the fragment whose element plays it, as the export mixes it.
const takesRef = useRef<Map<string, AxcutAudioTrack>>(new Map());
- const removedRef = useRef(removedRawSpans(clips, trimRanges));
- removedRef.current = useMemo(() => removedRawSpans(clips, trimRanges), [clips, trimRanges]);
+ const removedRef = useRef(removedRawSpans(clips, playbackTrimRanges));
+ removedRef.current = useMemo(
+ () => removedRawSpans(clips, playbackTrimRanges),
+ [clips, playbackTrimRanges],
+ );
const takeWalks = useMemo(() => {
const pieces = new Map<string, TakePiece[]>();
- const removed = removedRawSpans(clips, trimRanges);
+ const removed = removedRawSpans(clips, playbackTrimRanges);
const takes = takesByHeadFragment(audioTracks);
for (const pill of takes.values()) {
if (pill.kind !== "voiceover" || pill.loop) continue;
pieces.set(trackGroupId(pill), takeProgramme(pill, removed));
}
return { pieces, takes };
- }, [audioTracks, clips, trimRanges]);
+ }, [audioTracks, clips, playbackTrimRanges]);
takePiecesRef.current = takeWalks.pieces;
takesRef.current = takeWalks.takes;
// Trim-narrowed (`resolvePlaybackSegments`) — used ONLY to detect "has the <video>'s own
@@ -902,8 +913,8 @@ export function VirtualPreview({
// source time to a RAW virtual time that jumps discontinuously by exactly the trim's
// width the moment the video itself jumps — matching the marker's own pixel span.
const playbackClips = useMemo(
- () => resolvePlaybackSegments(clips, trimRanges),
- [clips, trimRanges],
+ () => resolvePlaybackSegments(clips, playbackTrimRanges),
+ [clips, playbackTrimRanges],
);
const playbackClipsRef = useRef(playbackClips);
playbackClipsRef.current = playbackClips;
@@ -915,8 +926,8 @@ export function VirtualPreview({
virtualTimeSecRef.current = virtualTimeSec;
const virtualDurationSecRef = useRef(virtualDurationSec);
virtualDurationSecRef.current = virtualDurationSec;
- const speedRegionsRef = useRef(speedRegions);
- speedRegionsRef.current = speedRegions;
+ const speedRegionsRef = useRef(playbackSpeedRegions);
+ speedRegionsRef.current = playbackSpeedRegions;
// Imported audio tracks (issue #350): the rAF reads these through refs, same as
// everything else it touches, so a track added/edited mid-playback is picked up
// without re-creating the loop. `audioTrackElsRef` maps a track id to its mounted
diff --git a/src/components/ai-edition/v4/AddAudioLayerDialog.test.tsx b/src/components/ai-edition/v4/AddAudioLayerDialog.test.tsx
index 696452ed6..47277c782 100644
--- a/src/components/ai-edition/v4/AddAudioLayerDialog.test.tsx
+++ b/src/components/ai-edition/v4/AddAudioLayerDialog.test.tsx
@@ -1,9 +1,12 @@
// @vitest-environment jsdom
import "@testing-library/jest-dom";
import { fireEvent, render, screen } from "@testing-library/react";
+import { toast } from "sonner";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { AddAudioLayerDialog } from "./AddAudioLayerDialog";
+vi.mock("sonner", () => ({ toast: { error: vi.fn() } }));
+
vi.mock("@/contexts/I18nContext", () => ({
useScopedT: () => (key: string) => key,
useI18n: () => ({ locale: "en", setLocale: () => undefined }),
@@ -24,6 +27,7 @@ vi.mock("@/lib/ai-edition/timeline/duration", () => ({
class FakeRecorder {
static instances: FakeRecorder[] = [];
state: "inactive" | "recording" = "inactive";
+ started = false;
stopped = false;
ondataavailable: ((e: { data: Blob }) => void) | null = null;
onstop: (() => void) | null = null;
@@ -35,6 +39,7 @@ class FakeRecorder {
return true;
}
start() {
+ this.started = true;
this.state = "recording";
}
stop() {
@@ -50,6 +55,7 @@ beforeEach(() => {
FakeRecorder.instances = [];
stopTrack.mockClear();
addAudioAsset.mockReset();
+ vi.mocked(toast.error).mockClear();
vi.stubGlobal("MediaRecorder", FakeRecorder);
Object.defineProperty(navigator, "mediaDevices", {
configurable: true,
@@ -85,6 +91,37 @@ describe("AddAudioLayerDialog", () => {
await vi.waitFor(() => expect(props.onRecordingStart).toHaveBeenCalledTimes(1));
});
+ it("waits for preview playback before starting the recorder", async () => {
+ let allowStart: (() => void) | undefined;
+ const onRecordingStart = vi.fn(
+ () =>
+ new Promise<void>((resolve) => {
+ allowStart = resolve;
+ }),
+ );
+ renderDialog({ onRecordingStart });
+
+ fireEvent.click(screen.getByText("audio.record"));
+ await vi.waitFor(() => expect(onRecordingStart).toHaveBeenCalledOnce());
+ expect(FakeRecorder.instances[0].started).toBe(false);
+
+ allowStart?.();
+ await vi.waitFor(() => expect(FakeRecorder.instances[0].started).toBe(true));
+ });
+
+ it("releases the microphone and aborts when preview playback fails", async () => {
+ const onRecordingStart = vi.fn().mockRejectedValue(new Error("playback failed"));
+ const { props } = renderDialog({ onRecordingStart });
+
+ fireEvent.click(screen.getByText("audio.record"));
+ await vi.waitFor(() => expect(toast.error).toHaveBeenCalledWith("audio.recordingUnavailable"));
+
+ expect(FakeRecorder.instances).toHaveLength(1);
+ expect(FakeRecorder.instances[0].started).toBe(false);
+ expect(stopTrack).toHaveBeenCalledOnce();
+ expect(props.onRecordingStop).toHaveBeenCalledOnce();
+ });
+
it("does not cover the video it is recording against", () => {
// It used to be a modal over a dimmed backdrop, which hid the one thing a
// voiceover needs you to watch. It is a docked bar now: no backdrop, and
diff --git a/src/components/ai-edition/v4/AddAudioLayerDialog.tsx b/src/components/ai-edition/v4/AddAudioLayerDialog.tsx
index c1112cb67..0ee457989 100644
--- a/src/components/ai-edition/v4/AddAudioLayerDialog.tsx
+++ b/src/components/ai-edition/v4/AddAudioLayerDialog.tsx
@@ -54,7 +54,7 @@ export function AddAudioLayerDialog({
maxDurationSec: number;
onClose: () => void;
onComplete: (assetId: string, durationSec: number) => void;
- onRecordingStart: () => void;
+ onRecordingStart: () => void | Promise<void>;
onRecordingStop: () => void;
}) {
const t = useScopedT("timeline");
@@ -212,13 +212,22 @@ export function AddAudioLayerDialog({
}
})();
};
+ try {
+ await onRecordingStart();
+ } catch {
+ for (const track of streamRef.current?.getTracks() ?? []) track.stop();
+ streamRef.current = null;
+ recorderRef.current = null;
+ onRecordingStop();
+ toast.error(t("audio.recordingUnavailable"));
+ return;
+ }
startedAtRef.current = performance.now();
discardRef.current = false;
recorder.start(250);
setRecording(true);
recordingRef.current = true;
setElapsedSec(0);
- onRecordingStart();
timerRef.current = setInterval(() => {
setElapsedSec((performance.now() - startedAtRef.current) / 1000);
}, 200);
diff --git a/src/native/sceneDescription.test.ts b/src/native/sceneDescription.test.ts
index c873190f6..761eadcb2 100644
--- a/src/native/sceneDescription.test.ts
+++ b/src/native/sceneDescription.test.ts
@@ -2735,6 +2735,23 @@ describe("buildSceneDescription.audioTracks", () => {
]);
});
+ it("applies cuts added after recording to a plain voiceover track", () => {
+ const afterRecording = cutDoc([voice]);
+ const recordedWithoutCuts = {
+ ...afterRecording,
+ timeline: { ...afterRecording.timeline, trimRanges: [] },
+ };
+
+ expect(recordedWithoutCuts.audioTracks[0]).not.toHaveProperty("recordingPlaybackMap");
+ expect(buildSceneDescription(recordedWithoutCuts).audioTracks).toMatchObject([
+ { startSec: 0, trimStartSec: 0, trimEndSec: 10, kind: "voiceover" },
+ ]);
+ expect(buildSceneDescription(afterRecording).audioTracks).toMatchObject([
+ { startSec: 0, trimStartSec: 0, trimEndSec: 4, kind: "voiceover" },
+ { startSec: 4, trimStartSec: 6, trimEndSec: 10, kind: "voiceover" },
+ ]);
+ });
+
it("keeps the fades on the take's outer edges across a split", () => {
const entries = buildSceneDescription(
cutDoc([{ ...voice, fadeInMs: 500, fadeOutMs: 500 }]),