Record your screen, ship a demo. Free and open-source, GPU-accelerated, no watermarks, no subscriptions. Windows, macOS, Linux. Actively maintained.
TypeScript cross-platformelectronffmpegopen-sourceproduct-demorustscreen-capturescreen-recorderscreen-recordingscreen-studioscreencastspeech-to-texttypescriptvideo-editingvideo-editorwaylandwgpuwhisper
fix(editor): play imported audio contiguously in preview to match the export
Benjamin Freeman3f8ac2a359b6
diff --git a/src/components/ai-edition/VirtualPreview.audio.test.ts b/src/components/ai-edition/VirtualPreview.audio.test.tsindex aeb2781e8..0ac1c1b31 100644--- a/src/components/ai-edition/VirtualPreview.audio.test.ts+++ b/src/components/ai-edition/VirtualPreview.audio.test.ts@@ -1,5 +1,6 @@import { describe, expect, it } from "vitest";-import type { AxcutAudioTrack } from "@/lib/ai-edition/schema";+import { projectRawTimelineSecToPlayback } from "@/lib/ai-edition/document/timeline";+import type { AxcutAudioTrack, AxcutClip, AxcutTrimRange } from "@/lib/ai-edition/schema";import {applyPreviewAudioSettings,type PreviewAudioGraph,@@ -96,18 +97,27 @@ describe("resolveTimelineAudioPlayback", () => {label: "",};+ // Second arg is the track head projected to output seconds; with no trims it is+ // just `timelineStartSec` (10), so these read the same as before the output-space+ // change — the projection is exercised separately below.it("maps the playhead to a source position offset by the trim in-point", () => {// 3s into the track's span → 2 (trimStart) + 3 = 5s of source.- expect(resolveTimelineAudioPlayback(13, track)).toEqual({ targetTimeSec: 5, shouldPlay: true });+ expect(resolveTimelineAudioPlayback(13, 10, track)).toEqual({+ targetTimeSec: 5,+ shouldPlay: true,+ });});it("does not play before the track starts, parked at the in-point", () => {- expect(resolveTimelineAudioPlayback(9, track)).toEqual({ targetTimeSec: 2, shouldPlay: false });+ expect(resolveTimelineAudioPlayback(9, 10, track)).toEqual({+ targetTimeSec: 2,+ shouldPlay: false,+ });});it("does not play past the window end, parked at the out-point", () => {- // Window is 6s (2..8), so timeline 10..16. At 16 it has just ended.- expect(resolveTimelineAudioPlayback(16, track)).toEqual({+ // Window is 6s (2..8), so output 10..16. At 16 it has just ended.+ expect(resolveTimelineAudioPlayback(16, 10, track)).toEqual({targetTimeSec: 8,shouldPlay: false,});@@ -116,11 +126,62 @@ describe("resolveTimelineAudioPlayback", () => {it("uses the asset duration as the out-point when the tail isn't trimmed", () => {const untrimmed = { ...track, trimStartSec: 0, trimEndSec: undefined, durationSec: 5 };// Span is 10..15. At 14 → 4s of source, still playing.- expect(resolveTimelineAudioPlayback(14, untrimmed)).toEqual({+ expect(resolveTimelineAudioPlayback(14, 10, untrimmed)).toEqual({targetTimeSec: 4,shouldPlay: true,});// At 15 the 5s file is over.- expect(resolveTimelineAudioPlayback(15, untrimmed).shouldPlay).toBe(false);+ expect(resolveTimelineAudioPlayback(15, 10, untrimmed).shouldPlay).toBe(false);+ });++ // The regression: an interior trim must NOT skip the track's own content, so the+ // preview stays byte-for-byte with `audio::mix_external_tracks`, which overlays the+ // decoded window contiguously. Same scenario as Etienne's review and the projection's+ // own test: a 10s clip with raw 2..4 cut, background track at raw head 0 spanning 0..10.+ it("plays contiguously across an interior trim, matching the export", () => {+ const clip: AxcutClip = {+ id: "clip_1",+ assetId: "asset_1",+ sourceStartSec: 0,+ sourceEndSec: 10,+ timelineStartSec: 0,+ timelineEndSec: 10,+ wordRefs: [],+ origin: "user",+ reason: "",+ };+ const trim: AxcutTrimRange = {+ id: "trim_1",+ assetId: "asset_1",+ startSec: 2,+ endSec: 4,+ origin: "user",+ reason: "",+ };+ const bgm: AxcutAudioTrack = {+ id: "bgm",+ assetId: "a2",+ timelineStartSec: 0,+ durationSec: 10,+ trimStartSec: 0,+ trimEndSec: 10,+ gainDb: 0,+ label: "",+ };+ const project = (rawSec: number) => projectRawTimelineSecToPlayback([clip], [trim], rawSec);+ const outputStart = project(bgm.timelineStartSec); // 0++ // Raw playhead 5 sits 1s past the 2s cut → output 3. The track is a contiguous+ // block, so it must be at source 3 — NOT source 5, which the old raw-space+ // `local` produced (the 2s desync).+ expect(resolveTimelineAudioPlayback(project(5), outputStart, bgm)).toEqual({+ targetTimeSec: 3,+ shouldPlay: true,+ });+ // Just before the cut is unaffected: raw 1 → output 1 → source 1.+ expect(resolveTimelineAudioPlayback(project(1), outputStart, bgm).targetTimeSec).toBeCloseTo(+ 1,+ 6,+ );});});diff --git a/src/components/ai-edition/VirtualPreview.tsx b/src/components/ai-edition/VirtualPreview.tsxindex a064821eb..a75af9f7c 100644--- a/src/components/ai-edition/VirtualPreview.tsx+++ b/src/components/ai-edition/VirtualPreview.tsx@@ -4,7 +4,10 @@ import {DEFAULT_CROP_REGION,MAX_NATIVE_PLAYBACK_RATE,} from "@/components/video-editor/types";-import { resolvePlaybackSegments } from "@/lib/ai-edition/document/timeline";+import {+ projectRawTimelineSecToPlayback,+ resolvePlaybackSegments,+} from "@/lib/ai-edition/document/timeline";import type {AxcutAudioTrack,AxcutClip,@@ -71,28 +74,35 @@ export function resolveAudioTrackPlayback(}/**- * Where an imported audio track (issue #350) should sit against the RAW virtual- * timeline clock, and whether it should be playing there.+ * Where an imported audio track (issue #350) should sit against the playback+ * clock, and whether it should be playing there. Both arguments are in+ * trim-compressed OUTPUT-programme seconds: `outputTimeSec` is the playhead and+ * `outputStartSec` is the track's head, each already projected from raw through+ * the trims by `projectRawTimelineSecToPlayback` in the caller.*- * A track occupies `[timelineStartSec, timelineStartSec + (trimEnd - trimStart)]`- * on the timeline; its source position is `trimStart` plus however far the- * playhead is into that span. Outside the span it parks at the nearer trim edge+ * The track plays as one CONTIGUOUS block: `[outputStartSec, outputStartSec ++ * (trimEnd - trimStart)]`, its source position `trimStart` plus how far the+ * playhead is past the head. Outside that span it parks at the nearer trim edge* and stays paused, the same discipline `resolveAudioTrackPlayback` uses so the* rAF never seeks an element into nothing.*- * This is the RAW/document timeline the ruler, playhead and lane all use — NOT- * the trim-compressed output timeline — because that is where the track was- * placed (`useTimeline.addAudioTrack` seeds `timelineStartSec` from the playhead).- * The preview's playhead JUMPS across a trim, so a track at a raw head past a cut- * still fires at the right wall-clock moment here. The export mixes onto the- * compressed programme, so it projects the raw head through the trims first- * (`projectRawTimelineSecToPlayback` in sceneDescription) to land at the same place.+ * Working in output space is what keeps the preview identical to the export: the+ * native `audio::mix_external_tracks` overlays the decoded window+ * `[trimStart, trimEnd]` contiguously at its projected offset, so an interior+ * trim shortens the programme UNDER the track without cutting the track's own+ * content. Deriving `local` from the RAW playhead instead (which jumps across a+ * cut) made the element skip that much source and end early — the preview/export+ * desync this fixes.*/-export function resolveTimelineAudioPlayback(virtualTimeSec: number, track: AxcutAudioTrack) {+export function resolveTimelineAudioPlayback(+ outputTimeSec: number,+ outputStartSec: number,+ track: AxcutAudioTrack,+) {const trimStart = Math.max(0, track.trimStartSec);const trimEnd = track.trimEndSec ?? track.durationSec;const windowLen = Math.max(0, trimEnd - trimStart);- const local = virtualTimeSec - track.timelineStartSec;+ const local = outputTimeSec - outputStartSec;return {targetTimeSec: Math.min(Math.max(trimStart, trimStart + local), trimEnd),shouldPlay: local >= 0 && local < windowLen,@@ -492,6 +502,11 @@ export function VirtualPreview({// mutation.const clipsRef = useRef(clips);clipsRef.current = clips;+ // 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;// Trim-narrowed (`resolvePlaybackSegments`) — used ONLY to detect "has the <video>'s own// currentTime drifted into a trim" and where to jump it back out to. Everything ELSE in// this component (`clips`/`clipsRef` above, virtualTimeSec, zoom/speed region lookups,@@ -574,17 +589,30 @@ export function VirtualPreview({audio.pause();}}- // Imported audio tracks (issue #350): position each against the RAW- // virtual timeline it was placed on, play it only inside its window, and- // set its level from the track gain. When the WebAudio graph is up the level+ // Imported audio tracks (issue #350): project the playhead raw→output+ // once, then position each track as a contiguous block against that+ // output clock (see `resolveTimelineAudioPlayback`) so an interior trim+ // shortens the programme without cutting the track — identical to the+ // export's `mix_external_tracks`. Play it only inside its window, and set+ // its level from the track gain. When the WebAudio graph is up the level// rides a per-track gain node (which CAN boost past 0 dB, and the output node// applies the global gain on top, matching the export); the `.volume` path is// the fallback for when the graph is unavailable — there a boost caps at 0 dB.const globalGain = audioGainScalar(audioGainDbRef.current);+ const outputTimeSec = projectRawTimelineSecToPlayback(+ clipsRef.current,+ trimRangesRef.current,+ virtualTimeSecRef.current,+ );for (const track of audioTracksRef.current) {const el = audioTrackElsRef.current.get(track.id);if (!el) continue;- const trackTarget = resolveTimelineAudioPlayback(virtualTimeSecRef.current, track);+ const outputStartSec = projectRawTimelineSecToPlayback(+ clipsRef.current,+ trimRangesRef.current,+ track.timelineStartSec,+ );+ const trackTarget = resolveTimelineAudioPlayback(outputTimeSec, outputStartSec, track);if (el.playbackRate !== v.playbackRate) el.playbackRate = v.playbackRate;const trackGainNode = audioTrackGainNodesRef.current.get(track.id);if (trackGainNode) {