Skip to content

Record your screen, ship a demo. Free and open-source, GPU-accelerated, no watermarks, no subscriptions. Windows, macOS, Linux. Actively maintained.

feat(editor): honor imported-track boost in the preview (CodeRabbit #5)

Benjamin Freeman462d682431f6
diff --git a/src/components/ai-edition/VirtualPreview.playback.test.tsx b/src/components/ai-edition/VirtualPreview.playback.test.tsx
index ae195532f..13d5121cb 100644
--- a/src/components/ai-edition/VirtualPreview.playback.test.tsx
+++ b/src/components/ai-edition/VirtualPreview.playback.test.tsx
@@ -295,3 +295,76 @@ describe("VirtualPreview imported audio tracks", () => {
expect(audioEl.pause).toHaveBeenCalled();
});
});
+
+// Issue #350 — a track boosted past 0 dB must sound boosted in the preview too, not just
+// in the export. `element.volume` caps at 1, so the boost has to ride a WebAudio gain node.
+// jsdom has no WebAudio, so install a minimal fake context and watch the nodes it mints.
+class FakeAudioNode {
+ connect = vi.fn();
+ disconnect = vi.fn();
+}
+class FakeGainNode extends FakeAudioNode {
+ gain = { value: 1 };
+}
+let createdGains: FakeGainNode[] = [];
+class FakeAudioContext {
+ state = "running";
+ destination = new FakeAudioNode();
+ resume = vi.fn(() => Promise.resolve());
+ close = vi.fn(() => Promise.resolve());
+ createMediaElementSource = vi.fn(() => new FakeAudioNode());
+ createGain = vi.fn(() => {
+ const node = new FakeGainNode();
+ createdGains.push(node);
+ return node;
+ });
+}
+
+describe("VirtualPreview imported audio track boost", () => {
+ beforeEach(() => {
+ createdGains = [];
+ vi.stubGlobal("AudioContext", FakeAudioContext);
+ });
+
+ // +6.0206 dB is exactly ×2 in linear gain — a boost `element.volume` (max 1) could never
+ // reach. The graph mints the output gain first, then one gain per track, so the track's
+ // node is the last one created.
+ const boosted = {
+ id: "trk",
+ assetId: "aud",
+ timelineStartSec: 2,
+ durationSec: 10,
+ trimStartSec: 1,
+ trimEndSec: 3,
+ gainDb: 6.0206,
+ label: "",
+ };
+
+ it("drives a per-track gain node past unity instead of capping element.volume", () => {
+ const sources: VideoSource[] = [{ id: "a1", src: "file:///tmp/a1.mp4", label: "a1" }];
+ const audioSources: VideoSource[] = [{ id: "aud", src: "file:///tmp/vo.mp3", label: "vo" }];
+ const { container } = render(
+ <VirtualPreview
+ videoSources={sources}
+ audioTracks={[boosted]}
+ audioSources={audioSources}
+ clips={[clip("c1", "a1", 0, 10, 0)]}
+ onTimeChange={vi.fn()}
+ />,
+ );
+ const videoEl = container.querySelector("video");
+ if (!videoEl) throw new Error("no <video>");
+ driveVideo(videoEl as HTMLVideoElement);
+ act(() => fireEvent.loadedMetadata(videoEl));
+ const audioEl = container.querySelector<HTMLAudioElement>(
+ '[data-testid="preview-audio-track-trk"]',
+ );
+ if (!audioEl) throw new Error("no track <audio>");
+ driveAudioEl(audioEl);
+
+ tick(); // let the rAF stamp the live gain onto the node
+ const trackGain = createdGains.at(-1);
+ expect(trackGain?.gain.value).toBeCloseTo(2, 3); // boosted, NOT clamped to 1
+ expect(audioEl.volume).toBe(1); // volume left at unity so it doesn't double-attenuate
+ });
+});
diff --git a/src/components/ai-edition/VirtualPreview.tsx b/src/components/ai-edition/VirtualPreview.tsx
index 00b17bc0e..44bb0fd69 100644
--- a/src/components/ai-edition/VirtualPreview.tsx
+++ b/src/components/ai-edition/VirtualPreview.tsx
@@ -244,6 +244,11 @@ export function VirtualPreview({
const audioContextRef = useRef<AudioContext | null>(null);
const audioContextCloseTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
const audioSourceNodesRef = useRef(new WeakMap<HTMLAudioElement, MediaElementAudioSourceNode>());
+ // Per-track gain nodes for imported audio (issue #350). Keyed by track id so the rAF
+ // can set each track's level live (a boost past 0 dB, which `element.volume` can't do —
+ // same reason the primary/supplemental sum through a gain node). The graph effect owns
+ // their lifecycle; the map is cleared and rebuilt whenever the routing is torn down.
+ const audioTrackGainNodesRef = useRef<Map<string, GainNode>>(new Map());
const audioGraphRef = useRef<PreviewAudioGraph | null>(null);
const videoFrameRef = useRef<HTMLDivElement | null>(null);
@@ -310,10 +315,21 @@ export function VirtualPreview({
};
}, [activeSource?.filePath]);
+ // Which imported-track elements are actually mounted (a track is rendered only once its
+ // asset URL resolves — see the JSX). Re-routing the graph is keyed on this set, NOT on the
+ // tracks' gains: a level change is applied live on the existing node by the rAF, so it must
+ // not tear the graph down. Joined ids change only on a real mount/unmount.
+ const mountedAudioTrackKey = audioTracks
+ .filter((track) => audioSources.some((source) => source.id === track.assetId))
+ .map((track) => track.id)
+ .join(",");
+
// Sum the audio elements into one gain node so the output trim can boost past 0 dB,
// which `element.volume` cannot do. The primary media element carries track 1; on macOS
// the existing IPC helper extracts track 2 (normally the microphone) so both are audible
- // instead of Chromium silently choosing one.
+ // instead of Chromium silently choosing one. Imported tracks (issue #350) join the same
+ // graph through a per-track gain node so their boost survives the preview too.
+ // biome-ignore lint/correctness/useExhaustiveDependencies: mountedAudioTrackKey is the trigger for re-routing tracks; the elements are read from the ref.
useEffect(() => {
if (!primaryAudioEl || !audioProbeComplete) return;
if (supplementalAudioSrc && !supplementalAudioEl) return;
@@ -360,14 +376,48 @@ export function VirtualPreview({
// preview outright rather than degrade it.
}
}
+ // Imported tracks (issue #350): each mounted element gets source → per-track gain →
+ // the output gain, so the effective level is trackGain × outputGain — the same order
+ // the exporter mixes in (mix_external_tracks applies the track gain, finish_audio the
+ // output gain). The rAF sets each node's value; created here at unity as a safe default.
+ const trackGainNodes: GainNode[] = [];
+ audioTrackGainNodesRef.current = new Map();
+ for (const [trackId, element] of audioTrackElsRef.current) {
+ try {
+ let source = audioSourceNodesRef.current.get(element);
+ if (!source) {
+ source = graph.context.createMediaElementSource(element);
+ audioSourceNodesRef.current.set(element, source);
+ }
+ source.disconnect();
+ const trackGain = graph.context.createGain();
+ source.connect(trackGain);
+ trackGain.connect(graph.gain);
+ audioTrackGainNodesRef.current.set(trackId, trackGain);
+ connectedSources.push(source);
+ trackGainNodes.push(trackGain);
+ } catch {
+ // Same rationale as the primary/supplemental loop: routing THIS track failed, so
+ // leave the rest connected. The rAF falls back to `element.volume` for a track
+ // with no gain node (capped at 0 dB, but audible).
+ }
+ }
audioGraphRef.current = graph;
applyPreviewAudioSettings(graph, elements, audioGainDbRef.current);
return () => {
audioGraphRef.current = null;
for (const source of connectedSources) source.disconnect();
+ for (const trackGain of trackGainNodes) trackGain.disconnect();
+ audioTrackGainNodesRef.current = new Map();
graph.gain.disconnect();
};
- }, [primaryAudioEl, supplementalAudioEl, supplementalAudioSrc, audioProbeComplete]);
+ }, [
+ primaryAudioEl,
+ supplementalAudioEl,
+ supplementalAudioSrc,
+ audioProbeComplete,
+ mountedAudioTrackKey,
+ ]);
// Keep one AudioContext for the component. Closing and recreating it on an effect rerun
// permanently silences an HTMLAudioElement because createMediaElementSource may only be
@@ -390,6 +440,7 @@ export function VirtualPreview({
const context = audioContextRef.current;
audioContextRef.current = null;
audioSourceNodesRef.current = new WeakMap();
+ audioTrackGainNodesRef.current = new Map();
if (context) void context.close();
}, 0);
};
@@ -522,17 +573,23 @@ export function VirtualPreview({
}
// 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 × the global output gain. Using
- // `.volume` rather than a WebAudio node keeps the delicate
- // primary/supplemental graph untouched; a boost past 0 dB clamps in the
- // preview (volume maxes at 1) but is still written to the export.
+ // 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);
for (const track of audioTracksRef.current) {
const el = audioTrackElsRef.current.get(track.id);
if (!el) continue;
const trackTarget = resolveTimelineAudioPlayback(virtualTimeSecRef.current, track);
if (el.playbackRate !== v.playbackRate) el.playbackRate = v.playbackRate;
- el.volume = Math.min(1, audioGainScalar(track.gainDb) * globalGain);
+ const trackGainNode = audioTrackGainNodesRef.current.get(track.id);
+ if (trackGainNode) {
+ trackGainNode.gain.value = audioGainScalar(track.gainDb);
+ if (el.volume !== 1) el.volume = 1;
+ } else {
+ el.volume = Math.min(1, audioGainScalar(track.gainDb) * globalGain);
+ }
// Only re-seek on a real discontinuity (a scrub, a trim jump, a first
// play), NOT on the sub-frame drift of normal playback. The primary audio
// can afford a 25 ms leash because it syncs to the <video>'s own