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(audio): measure the recording's loudness again when Retry reloads it

EtienneLescote8c3d4c9dbd1
diff --git a/src/components/ai-edition/VirtualPreview.mediaError.test.tsx b/src/components/ai-edition/VirtualPreview.mediaError.test.tsx
index 363308f13..52fea3a8b 100644
--- a/src/components/ai-edition/VirtualPreview.mediaError.test.tsx
+++ b/src/components/ai-edition/VirtualPreview.mediaError.test.tsx
@@ -465,3 +465,31 @@ describe("VirtualPreview media-error recovery (issue #395)", () => {
expect(video.loadCalls).toBe(0);
});
});
+
+describe("VirtualPreview loudness measurement across a Retry", () => {
+ it("measures the recording again when Retry reloads it", async () => {
+ // The first request came back failed while the file was missing; without a fresh
+ // one after Retry the voice would stay unlevelled (0 dB) for the whole session.
+ const getLoudnessGain = vi
+ .fn<(path: string) => Promise<{ success: boolean; gainDb: number }>>()
+ .mockResolvedValueOnce({ success: false, gainDb: 0 })
+ .mockResolvedValueOnce({ success: true, gainDb: 6 });
+ vi.stubGlobal("electronAPI", { getLoudnessGain });
+ const sources: VideoSource[] = [
+ { id: "a1", src: "file:///tmp/a1.mp4", filePath: "/tmp/a1.mp4", label: "a1" },
+ ];
+ const { bumpRetryToken } = mount([clip("clip_1", "a1", 0)], sources);
+ await act(async () => {});
+ expect(getLoudnessGain).toHaveBeenCalledTimes(1);
+
+ // A re-render with the same token must not ask again.
+ bumpRetryToken(0);
+ await act(async () => {});
+ expect(getLoudnessGain).toHaveBeenCalledTimes(1);
+
+ bumpRetryToken(1);
+ await act(async () => {});
+ expect(getLoudnessGain).toHaveBeenCalledTimes(2);
+ expect(getLoudnessGain).toHaveBeenLastCalledWith("/tmp/a1.mp4");
+ });
+});
diff --git a/src/components/ai-edition/VirtualPreview.tsx b/src/components/ai-edition/VirtualPreview.tsx
index 1741debd6..aeab91aed 100644
--- a/src/components/ai-edition/VirtualPreview.tsx
+++ b/src/components/ai-edition/VirtualPreview.tsx
@@ -405,9 +405,25 @@ export function VirtualPreview({
]
.filter((path): path is string => Boolean(path))
.join("\n");
+ const activeVoicePath = activeSource?.filePath;
+ const measuredForRetryRef = useRef(retryToken);
useEffect(() => {
const getLoudnessGain = window.electronAPI?.getLoudnessGain;
if (!getLoudnessGain) return;
+ // Retry reloads the file after it was unavailable, and its first measurement may have
+ // failed with it. Forget that answer and ask again; it plays at 0 dB until the new one.
+ if (retryToken !== measuredForRetryRef.current) {
+ measuredForRetryRef.current = retryToken;
+ if (activeVoicePath) {
+ requestedLoudnessRef.current.delete(activeVoicePath);
+ setLoudnessGainDbByPath((previous) => {
+ if (!previous.has(activeVoicePath)) return previous;
+ const next = new Map(previous);
+ next.delete(activeVoicePath);
+ return next;
+ });
+ }
+ }
for (const path of voicePathsKey.split("\n")) {
if (!path || requestedLoudnessRef.current.has(path)) continue;
requestedLoudnessRef.current.add(path);
@@ -419,7 +435,7 @@ export function VirtualPreview({
() => undefined,
);
}
- }, [voicePathsKey]);
+ }, [voicePathsKey, retryToken, activeVoicePath]);
const voiceGainDb = activeSource?.filePath
? (loudnessGainDbByPath.get(activeSource.filePath) ?? 0)
: 0;