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(audio): cover the mix gain per format and give the gain rule one home

EtienneLescot2f19652b04ee
diff --git a/electron/native/wgc-capture/src/audio_sample_utils_test.cpp b/electron/native/wgc-capture/src/audio_sample_utils_test.cpp
index 7b7177fad..b28f738c8 100644
--- a/electron/native/wgc-capture/src/audio_sample_utils_test.cpp
+++ b/electron/native/wgc-capture/src/audio_sample_utils_test.cpp
@@ -1446,6 +1446,95 @@ int main() {
}
}
+ // --- mixAudioInPlace, per output format -----------------------------------
+ //
+ // The gain rides in mixAudioInPlace on every format branch, but the
+ // AudioMixer sections above can only reach PCM16: production always mixes
+ // into the AAC input format. These call the branches directly, with the
+ // negative-destination-plus-boosted-source case that pins sum-then-clamp
+ // (clamp-then-sum reads the destination alone: 0.4, not 0.76).
+ {
+ const size_t frames = 4;
+ const AudioInputFormat f32 = makeFormat(MFAudioFormat_Float, 48000, 2, 32);
+ {
+ std::vector<BYTE> dest(frames * f32.blockAlign, 0);
+ std::vector<BYTE> src(frames * f32.blockAlign, 0);
+ auto* d = reinterpret_cast<float*>(dest.data());
+ auto* s = reinterpret_cast<float*>(src.data());
+ for (size_t i = 0; i < frames * 2; i += 1) {
+ d[i] = -0.5f;
+ s[i] = 0.9f;
+ }
+ mixAudioInPlace(dest, src.data(), static_cast<DWORD>(src.size()), f32, 1.4);
+ char detail[96]{};
+ sprintf_s(detail, "got=%.6f want=0.76", d[0]);
+ std::cout << "MIXFMT_RAW float-over-system " << detail << std::endl;
+ expect("mix-in-place-float-sums-before-clamp", std::abs(d[0] - 0.76) < 1e-3, detail);
+ for (size_t i = 0; i < frames * 2; i += 1) {
+ d[i] = 0.5f;
+ }
+ mixAudioInPlace(dest, src.data(), static_cast<DWORD>(src.size()), f32, 1.4);
+ sprintf_s(detail, "got=%.6f want=1.0", d[0]);
+ std::cout << "MIXFMT_RAW float-rail " << detail << std::endl;
+ expect("mix-in-place-float-clamps-at-rail", d[0] == 1.0f, detail);
+ }
+ // PCM16 is the production path, pinned here in its own units too:
+ // -16384 + 29491 * 1.4 = 24903.4 -> 24903, and 16384 + 29491 * 1.4
+ // clamps at 32767.
+ {
+ std::vector<BYTE> dest(frames * target48k.blockAlign, 0);
+ std::vector<BYTE> src(frames * target48k.blockAlign, 0);
+ auto* d = reinterpret_cast<int16_t*>(dest.data());
+ auto* s = reinterpret_cast<int16_t*>(src.data());
+ for (size_t i = 0; i < frames * 2; i += 1) {
+ d[i] = -16384;
+ s[i] = 29491;
+ }
+ mixAudioInPlace(
+ dest, src.data(), static_cast<DWORD>(src.size()), target48k, 1.4);
+ char detail[96]{};
+ sprintf_s(detail, "got=%d want=24903", d[0]);
+ std::cout << "MIXFMT_RAW pcm16-over-system " << detail << std::endl;
+ expect("mix-in-place-pcm16-sums-before-clamp", std::abs(d[0] - 24903) <= 1, detail);
+ for (size_t i = 0; i < frames * 2; i += 1) {
+ d[i] = 16384;
+ }
+ mixAudioInPlace(
+ dest, src.data(), static_cast<DWORD>(src.size()), target48k, 1.4);
+ sprintf_s(detail, "got=%d want=32767", d[0]);
+ std::cout << "MIXFMT_RAW pcm16-rail " << detail << std::endl;
+ expect("mix-in-place-pcm16-clamps-at-rail", d[0] == 32767, detail);
+ }
+ // Round numbers in int32 units: -1e9 + 2e9 * 1.4 = 1.8e9 (fits), and
+ // 1e9 + 2e9 * 1.4 = 3.8e9 clamps at INT32_MAX.
+ {
+ const AudioInputFormat p32 = makeFormat(MFAudioFormat_PCM, 48000, 2, 32);
+ std::vector<BYTE> dest(frames * p32.blockAlign, 0);
+ std::vector<BYTE> src(frames * p32.blockAlign, 0);
+ auto* d = reinterpret_cast<int32_t*>(dest.data());
+ auto* s = reinterpret_cast<int32_t*>(src.data());
+ for (size_t i = 0; i < frames * 2; i += 1) {
+ d[i] = -1000000000;
+ s[i] = 2000000000;
+ }
+ mixAudioInPlace(dest, src.data(), static_cast<DWORD>(src.size()), p32, 1.4);
+ char detail[96]{};
+ sprintf_s(detail, "got=%d want=1800000000", static_cast<int>(d[0]));
+ std::cout << "MIXFMT_RAW pcm32-over-system " << detail << std::endl;
+ expect("mix-in-place-pcm32-sums-before-clamp", d[0] == 1800000000LL, detail);
+ for (size_t i = 0; i < frames * 2; i += 1) {
+ d[i] = 1000000000;
+ }
+ mixAudioInPlace(dest, src.data(), static_cast<DWORD>(src.size()), p32, 1.4);
+ sprintf_s(detail, "got=%d want=2147483647", static_cast<int>(d[0]));
+ std::cout << "MIXFMT_RAW pcm32-rail " << detail << std::endl;
+ expect(
+ "mix-in-place-pcm32-clamps-at-rail",
+ d[0] == static_cast<int32_t>(2147483647LL),
+ detail);
+ }
+ }
+
HRESULT mfHr = CoInitializeEx(nullptr, COINIT_MULTITHREADED);
if (FAILED(mfHr) && mfHr != RPC_E_CHANGED_MODE) {
skip("mf-startup", "CoInitializeEx failed — no Media Foundation on this host");
diff --git a/src/hooks/useScreenRecorder.ts b/src/hooks/useScreenRecorder.ts
index eda9becaa..c3fd3b05b 100644
--- a/src/hooks/useScreenRecorder.ts
+++ b/src/hooks/useScreenRecorder.ts
@@ -2,7 +2,7 @@ import { fixWebmDuration } from "@fix-webm-duration/fix";
import { useCallback, useEffect, useRef, useState } from "react";
import { toast } from "sonner";
import { useScopedT } from "@/contexts/I18nContext";
-import { MIC_GAIN_BOOST, mixAudioTracks } from "@/lib/audioMix";
+import { mixAudioTracks, nativeMicrophoneGain } from "@/lib/audioMix";
import {
type NativeLinuxRecordingRequest,
portalOwnsSourceSelection,
@@ -1222,10 +1222,8 @@ export function useScreenRecorder(): UseScreenRecorderReturn {
enabled: microphoneEnabled,
deviceId: microphoneDeviceId,
deviceName: microphoneDeviceName,
- // Same rule as mixAudioTracks: boosted only when the mic has to
- // sit over system audio; at unity on its own so a hot mic does
- // not get +2.9 dB of unconditional gain in the native mixer.
- gain: systemAudioEnabled ? MIC_GAIN_BOOST : 1,
+ // Boosted only when the mic has to sit over system audio.
+ gain: nativeMicrophoneGain(systemAudioEnabled),
},
},
webcam: {
@@ -1387,7 +1385,7 @@ export function useScreenRecorder(): UseScreenRecorderReturn {
deviceId: microphoneDeviceId,
deviceName: microphoneDeviceName,
// Boosted only over system audio, like the Windows request above.
- gain: systemAudioEnabled ? MIC_GAIN_BOOST : 1,
+ gain: nativeMicrophoneGain(systemAudioEnabled),
},
},
webcam: {
@@ -1491,7 +1489,7 @@ export function useScreenRecorder(): UseScreenRecorderReturn {
// helper then fell back to the session default source.
...(microphoneDeviceName ? { deviceName: microphoneDeviceName } : {}),
// Boosted only over system audio, like the Windows request above.
- gain: systemAudioEnabled ? MIC_GAIN_BOOST : 1,
+ gain: nativeMicrophoneGain(systemAudioEnabled),
},
},
cursor: { mode: cursorCaptureMode },
diff --git a/src/lib/audioMix.test.ts b/src/lib/audioMix.test.ts
index d9ce11e78..ba2443f00 100644
--- a/src/lib/audioMix.test.ts
+++ b/src/lib/audioMix.test.ts
@@ -1,5 +1,5 @@
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
-import { MIC_FADE_IN_S, MIC_GAIN_BOOST, mixAudioTracks } from "./audioMix";
+import { MIC_FADE_IN_S, MIC_GAIN_BOOST, mixAudioTracks, nativeMicrophoneGain } from "./audioMix";
class FakeAudioParam {
value = 1;
@@ -64,6 +64,16 @@ const stubMediaStream = () => {
const track = (id: string) => ({ kind: "audio", id }) as unknown as MediaStreamTrack;
+describe("nativeMicrophoneGain", () => {
+ it("boosts the mic only when it has to sit over system audio", () => {
+ expect(nativeMicrophoneGain(true)).toBe(MIC_GAIN_BOOST);
+ });
+
+ it("rides at unity for mic-only recordings so a hot mic cannot clip in the native mixer", () => {
+ expect(nativeMicrophoneGain(false)).toBe(1);
+ });
+});
+
describe("mixAudioTracks", () => {
beforeEach(() => {
stubMediaStream();
diff --git a/src/lib/audioMix.ts b/src/lib/audioMix.ts
index 1329c59cd..d17980aac 100644
--- a/src/lib/audioMix.ts
+++ b/src/lib/audioMix.ts
@@ -5,6 +5,18 @@
*/
export const MIC_GAIN_BOOST = 1.4;
+/**
+ * The mic gain the native capture helpers should receive: boosted above unity
+ * only when the mic has to sit over system audio, at unity on its own so a mic
+ * that already runs hot does not get +2.9 dB it cannot spend — the native
+ * mixers hard-clamp at full scale. This is the same rule mixAudioTracks applies
+ * from track presence, expressed from the system-audio toggle the native
+ * request builders know.
+ */
+export function nativeMicrophoneGain(systemAudioEnabled: boolean): number {
+ return systemAudioEnabled ? MIC_GAIN_BOOST : 1;
+}
+
/**
* Duration of the mic fade-in applied at the start of a recording. Masks the
* click/pop that the mic produces at its first packet (echo-canceller warm-up,
@@ -52,7 +64,7 @@ export function mixAudioTracks({
systemSource.connect(destination);
}
// Unity when the mic is on its own; boosted only when competing with system audio.
- const micTargetGain = systemAudioTrack ? MIC_GAIN_BOOST : 1;
+ const micTargetGain = nativeMicrophoneGain(Boolean(systemAudioTrack));
const micSource = context.createMediaStreamSource(new MediaStream([micAudioTrack]));
const micGain = context.createGain();
micGain.gain.setValueAtTime(0, context.currentTime);