Skip to content

Commit c53bdbd

Browse files
committed
fix(audio): measure the recording's loudness again when Retry reloads it
1 parent cf63ba7 commit c53bdbd

2 files changed

Lines changed: 45 additions & 1 deletion

File tree

‎src/components/ai-edition/VirtualPreview.mediaError.test.tsx‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,3 +465,31 @@ describe("VirtualPreview media-error recovery (issue #395)", () => {
465465
expect(video.loadCalls).toBe(0);
466466
});
467467
});
468+
469+
describe("VirtualPreview loudness measurement across a Retry", () => {
470+
it("measures the recording again when Retry reloads it", async () => {
471+
// The first request came back failed while the file was missing; without a fresh
472+
// one after Retry the voice would stay unlevelled (0 dB) for the whole session.
473+
const getLoudnessGain = vi
474+
.fn<(path: string) => Promise<{ success: boolean; gainDb: number }>>()
475+
.mockResolvedValueOnce({ success: false, gainDb: 0 })
476+
.mockResolvedValueOnce({ success: true, gainDb: 6 });
477+
vi.stubGlobal("electronAPI", { getLoudnessGain });
478+
const sources: VideoSource[] = [
479+
{ id: "a1", src: "file:///tmp/a1.mp4", filePath: "/tmp/a1.mp4", label: "a1" },
480+
];
481+
const { bumpRetryToken } = mount([clip("clip_1", "a1", 0)], sources);
482+
await act(async () => {});
483+
expect(getLoudnessGain).toHaveBeenCalledTimes(1);
484+
485+
// A re-render with the same token must not ask again.
486+
bumpRetryToken(0);
487+
await act(async () => {});
488+
expect(getLoudnessGain).toHaveBeenCalledTimes(1);
489+
490+
bumpRetryToken(1);
491+
await act(async () => {});
492+
expect(getLoudnessGain).toHaveBeenCalledTimes(2);
493+
expect(getLoudnessGain).toHaveBeenLastCalledWith("/tmp/a1.mp4");
494+
});
495+
});

‎src/components/ai-edition/VirtualPreview.tsx‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,9 +405,25 @@ export function VirtualPreview({
405405
]
406406
.filter((path): path is string => Boolean(path))
407407
.join("\n");
408+
const activeVoicePath = activeSource?.filePath;
409+
const measuredForRetryRef = useRef(retryToken);
408410
useEffect(() => {
409411
const getLoudnessGain = window.electronAPI?.getLoudnessGain;
410412
if (!getLoudnessGain) return;
413+
// Retry reloads the file after it was unavailable, and its first measurement may have
414+
// failed with it. Forget that answer and ask again; it plays at 0 dB until the new one.
415+
if (retryToken !== measuredForRetryRef.current) {
416+
measuredForRetryRef.current = retryToken;
417+
if (activeVoicePath) {
418+
requestedLoudnessRef.current.delete(activeVoicePath);
419+
setLoudnessGainDbByPath((previous) => {
420+
if (!previous.has(activeVoicePath)) return previous;
421+
const next = new Map(previous);
422+
next.delete(activeVoicePath);
423+
return next;
424+
});
425+
}
426+
}
411427
for (const path of voicePathsKey.split("\n")) {
412428
if (!path || requestedLoudnessRef.current.has(path)) continue;
413429
requestedLoudnessRef.current.add(path);
@@ -419,7 +435,7 @@ export function VirtualPreview({
419435
() => undefined,
420436
);
421437
}
422-
}, [voicePathsKey]);
438+
}, [voicePathsKey, retryToken, activeVoicePath]);
423439
const voiceGainDb = activeSource?.filePath
424440
? (loudnessGainDbByPath.get(activeSource.filePath) ?? 0)
425441
: 0;

0 commit comments

Comments
 (0)