Skip to content

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

test(editor): pin the preview's gain scalar the way the export's is pinned

EtienneLescotcdc79bab9817
diff --git a/src/components/ai-edition/VirtualPreview.audio.test.ts b/src/components/ai-edition/VirtualPreview.audio.test.ts
index 31158ebd4..208bf05a2 100644
--- a/src/components/ai-edition/VirtualPreview.audio.test.ts
+++ b/src/components/ai-edition/VirtualPreview.audio.test.ts
@@ -1,5 +1,17 @@
import { describe, expect, it } from "vitest";
-import { resolveAudioTrackPlayback } from "./VirtualPreview";
+import {
+ applyPreviewAudioSettings,
+ type PreviewAudioGraph,
+ resolveAudioTrackPlayback,
+} from "./VirtualPreview";
+
+/** Minimal stand-in: the function only ever touches `gain.gain.value`. */
+function fakeGraph(): PreviewAudioGraph {
+ return {
+ context: {} as AudioContext,
+ gain: { gain: { value: Number.NaN } } as GainNode,
+ };
+}
describe("resolveAudioTrackPlayback", () => {
it("mirrors the video's time", () => {
@@ -32,3 +44,39 @@ describe("resolveAudioTrackPlayback", () => {
expect(resolveAudioTrackPlayback(-0.5, 10)).toEqual({ targetTimeSec: 0, shouldPlay: false });
});
});
+
+describe("applyPreviewAudioSettings", () => {
+ // This is the PR's parity claim, on the preview side. `finish_audio` applies
+ // `10f32.powf(gain_db / 20.0)` per sample natively and has its own test pinning that
+ // identity; if these two ever disagree, the editor stops meaning what it plays.
+ it("feeds the gain node the same scalar the export applies", () => {
+ for (const gainDb of [-12, -6.0206, 0, 6.0206, 12]) {
+ const graph = fakeGraph();
+ applyPreviewAudioSettings(graph, [], gainDb);
+ expect(graph.gain.gain.value).toBeCloseTo(10 ** (gainDb / 20), 6);
+ }
+ });
+
+ it("caps the element-volume fallback at unity, which is why the gain node exists", () => {
+ const element = { volume: Number.NaN } as HTMLAudioElement;
+
+ // Attenuation survives the fallback intact...
+ applyPreviewAudioSettings(null, [element, null], -6.0206);
+ expect(element.volume).toBeCloseTo(0.5, 4);
+
+ // ...but `HTMLMediaElement.volume` has no headroom above 1, so a boost is lost
+ // wherever WebAudio is unavailable. Degraded on purpose, not silent.
+ applyPreviewAudioSettings(null, [element], 6.0206);
+ expect(element.volume).toBe(1);
+ });
+
+ it("leaves the elements alone once the graph is carrying the gain", () => {
+ // Their audio no longer reaches the default output, so `volume` would only
+ // scale the signal a second time on its way into the node.
+ const graph = fakeGraph();
+ const element = { volume: 0.25 } as HTMLAudioElement;
+ applyPreviewAudioSettings(graph, [element], -6.0206);
+ expect(element.volume).toBe(0.25);
+ expect(graph.gain.gain.value).toBeCloseTo(0.5, 4);
+ });
+});
diff --git a/src/components/ai-edition/VirtualPreview.tsx b/src/components/ai-edition/VirtualPreview.tsx
index 3f3dd524d..6a49ce61a 100644
--- a/src/components/ai-edition/VirtualPreview.tsx
+++ b/src/components/ai-edition/VirtualPreview.tsx
@@ -64,7 +64,7 @@ export function resolveAudioTrackPlayback(
};
}
-interface PreviewAudioGraph {
+export interface PreviewAudioGraph {
context: AudioContext;
gain: GainNode;
}
@@ -80,7 +80,7 @@ interface PreviewAudioGraph {
* offline stage measured over the whole programme (a loudness normaliser) cannot exist
* here at all, because the preview never holds that programme.
*/
-function applyPreviewAudioSettings(
+export function applyPreviewAudioSettings(
graph: PreviewAudioGraph | null,
elements: Array<HTMLAudioElement | null>,
gainDb: number,