Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions electron/app-settings.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,27 @@ describe("app settings store", () => {
);
});

it("persists the volume trims, reads missing keys as unity, and rejects out-of-range writes", () => {
const dir = temp();
const file = path.join(dir, "recording-settings.json");
const store = new AppSettingsStore(dir);
expect(store.getSnapshot().recording.micGainDb).toBe(0);
expect(store.getSnapshot().recording.systemAudioGainDb).toBe(0);
expect(
store.setRecordingPreferences({ micGainDb: -6, systemAudioGainDb: 3 }).recording,
).toMatchObject({ micGainDb: -6, systemAudioGainDb: 3 });
expect(new AppSettingsStore(dir).getSnapshot().recording).toMatchObject({
micGainDb: -6,
systemAudioGainDb: 3,
});

writeFileSync(file, JSON.stringify({ micGainDb: 99, systemAudioGainDb: "loud" }), "utf8");
expect(store.getSnapshot().recording.micGainDb).toBe(0);
expect(store.getSnapshot().recording.systemAudioGainDb).toBe(0);
expect(() => store.setRecordingPreferences({ micGainDb: 24 })).toThrow(TypeError);
expect(() => store.setRecordingPreferences({ systemAudioGainDb: -25 })).toThrow(TypeError);
});

it("stores the last source beside the recording preferences", () => {
const dir = temp();
const store = new AppSettingsStore(dir);
Expand Down
32 changes: 32 additions & 0 deletions electron/app-settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
type WebcamQualityId,
webcamQualityFrom,
} from "../src/hooks/webcamCaptureTarget";
import { RECORDING_GAIN_DB_MAX, RECORDING_GAIN_DB_MIN } from "../src/lib/audioMix";
import type { CursorCaptureMode } from "../src/lib/recordingSession";
import { renameSyncWithFallback } from "./atomic-rename";

Expand All @@ -19,6 +20,10 @@ export interface RecordingPreferences {
/** Capture resolution for the camera. See WEBCAM_QUALITY_PRESETS. */
camQuality: WebcamQualityId;
systemAudioEnabled: boolean;
/** User trim on the microphone, dB relative to the automatic mix gain. */
micGainDb: number;
/** User trim on the captured computer audio, dB. */
systemAudioGainDb: number;
cursorCaptureMode: CursorCaptureMode;
/** Display captures on macOS and Windows. Opt-in: on Windows the icons also leave the real desktop while recording. */
hideDesktopIcons: boolean;
Expand All @@ -41,6 +46,8 @@ export const DEFAULT_RECORDING_PREFERENCES: RecordingPreferences = {
camDeviceName: null,
camQuality: DEFAULT_WEBCAM_QUALITY,
systemAudioEnabled: false,
micGainDb: 0,
systemAudioGainDb: 0,
cursorCaptureMode: "editable-overlay",
hideDesktopIcons: false,
autoZoomEnabled: true,
Expand Down Expand Up @@ -101,6 +108,13 @@ function atomicWrite(userData: string, value: RawSettings): void {
const bool = (value: unknown, fallback: boolean) => (typeof value === "boolean" ? value : fallback);
const nullableString = (value: unknown, fallback: string | null) =>
value === null || typeof value === "string" ? value : fallback;
const gainDb = (value: unknown, fallback: number) =>
typeof value === "number" &&
Number.isFinite(value) &&
value >= RECORDING_GAIN_DB_MIN &&
value <= RECORDING_GAIN_DB_MAX
? value
: fallback;

function parseRecording(raw: RawSettings): RecordingPreferences {
return {
Expand All @@ -117,6 +131,13 @@ function parseRecording(raw: RawSettings): RecordingPreferences {
raw.systemAudioEnabled,
DEFAULT_RECORDING_PREFERENCES.systemAudioEnabled,
),
// Absent from settings files written before the volume trims existed;
// unity (0 dB) is what every such installation recorded at.
micGainDb: gainDb(raw.micGainDb, DEFAULT_RECORDING_PREFERENCES.micGainDb),
systemAudioGainDb: gainDb(
raw.systemAudioGainDb,
DEFAULT_RECORDING_PREFERENCES.systemAudioGainDb,
),
cursorCaptureMode:
raw.cursorCaptureMode === "system" || raw.cursorCaptureMode === "editable-overlay"
? raw.cursorCaptureMode
Expand Down Expand Up @@ -186,6 +207,17 @@ function validateRecordingPatch(patch: Partial<RecordingPreferences>): void {
if (key === "camQuality" && !WEBCAM_QUALITY_IDS.includes(value as WebcamQualityId)) {
throw new TypeError("camQuality is invalid");
}
if (
(key === "micGainDb" || key === "systemAudioGainDb") &&
(typeof value !== "number" ||
!Number.isFinite(value) ||
value < RECORDING_GAIN_DB_MIN ||
value > RECORDING_GAIN_DB_MAX)
) {
throw new TypeError(
`${key} must be a number in [${RECORDING_GAIN_DB_MIN}, ${RECORDING_GAIN_DB_MAX}]`,
);
}
}
}

Expand Down
15 changes: 13 additions & 2 deletions electron/ipc/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,10 @@ export interface RecordingPrefs {
/** Capture resolution for the camera. See WEBCAM_QUALITY_PRESETS. */
camQuality: WebcamQualityId;
systemAudioEnabled: boolean;
/** User trim on the microphone, dB relative to the automatic mix gain. */
micGainDb: number;
/** User trim on the captured computer audio, dB. */
systemAudioGainDb: number;
cursorCaptureMode: CursorCaptureMode;
hideDesktopIcons: boolean;
/** Whether a fresh take gets automatic zooms on import. Persisted; defaults on. */
Expand All @@ -750,6 +754,8 @@ const defaultRecordingPrefs: RecordingPrefs = {
camDeviceName: null,
camQuality: DEFAULT_WEBCAM_QUALITY,
systemAudioEnabled: false,
micGainDb: 0,
systemAudioGainDb: 0,
cursorCaptureMode: "editable-overlay",
hideDesktopIcons: false,
autoZoomEnabled: true,
Expand Down Expand Up @@ -1004,6 +1010,7 @@ function captureSettingsOf(request: NativeLinuxRecordingRequest): string {
fps: request.video?.fps ?? null,
bitrate: request.video?.bitrate ?? null,
system: request.audio?.system?.enabled ?? false,
systemGain: request.audio?.system?.gain ?? 1,
microphone: request.audio?.microphone?.enabled ?? false,
deviceName: request.audio?.microphone?.deviceName ?? null,
gain: request.audio?.microphone?.gain ?? null,
Expand Down Expand Up @@ -2795,7 +2802,7 @@ export function registerIpcHandlers(
fps: request.video.fps,
...(request.video.bitrate ? { bitrate: request.video.bitrate } : {}),
audio: {
system: { enabled: request.audio.system.enabled },
system: { enabled: request.audio.system.enabled, gain: request.audio.system.gain ?? 1 },
microphone: {
enabled: request.audio.microphone.enabled,
...(request.audio.microphone.deviceName
Expand Down Expand Up @@ -2899,7 +2906,10 @@ export function registerIpcHandlers(
fps: request.video.fps,
...(request.video.bitrate ? { bitrate: request.video.bitrate } : {}),
audio: {
system: { enabled: request.audio.system.enabled },
system: {
enabled: request.audio.system.enabled,
gain: request.audio.system.gain ?? 1,
},
microphone: {
enabled: request.audio.microphone.enabled,
...(request.audio.microphone.deviceName
Expand Down Expand Up @@ -3190,6 +3200,7 @@ export function registerIpcHandlers(
microphoneDeviceId: request.audio.microphone.deviceId ?? null,
microphoneDeviceName: request.audio.microphone.deviceName ?? null,
microphoneGain: request.audio.microphone.gain,
systemGain: request.audio.system.gain ?? 1,
webcamEnabled: request.webcam.enabled,
webcamDeviceId: request.webcam.deviceId ?? null,
webcamDeviceName: request.webcam.deviceName ?? null,
Expand Down
2 changes: 2 additions & 0 deletions electron/ipc/recordingPrefs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ const defaults: RecordingPrefs = {
camDeviceName: null,
camQuality: "2160p",
systemAudioEnabled: false,
micGainDb: 0,
systemAudioGainDb: 0,
cursorCaptureMode: "editable-overlay",
hideDesktopIcons: false,
autoZoomEnabled: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export interface LinuxCaptureConfig {
fps: number;
bitrate?: number;
audio: {
system: { enabled: boolean };
system: { enabled: boolean; gain?: number };
microphone: { enabled: boolean; deviceName?: string; gain: number };
};
maxCursorSamples: number;
Expand Down
7 changes: 5 additions & 2 deletions electron/native/pipewire-capture/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ struct AudioRequest {
#[serde(rename_all = "camelCase", default)]
struct SystemAudioRequest {
enabled: bool,
/// Linear multiplier the UI applies to computer-audio level.
gain: Option<f32>,
}

#[derive(Debug, Default, Deserialize)]
Expand Down Expand Up @@ -199,7 +201,8 @@ impl Request {
// The default SINK's monitor: what is being played, not what a
// microphone hears.
capture_sink: true,
gain: 1.0,
// An explicit 0 means silence; only an absent gain falls back to unity.
gain: audio.system.gain.unwrap_or(1.0),
bitrate: DEFAULT_AUDIO_BITRATE,
});
}
Expand All @@ -208,7 +211,7 @@ impl Request {
label: "microphone",
target: audio.microphone.device_name.clone(),
capture_sink: false,
gain: audio.microphone.gain.filter(|gain| *gain > 0.0).unwrap_or(1.0),
gain: audio.microphone.gain.unwrap_or(1.0),
bitrate: DEFAULT_AUDIO_BITRATE,
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ public final class AudioTrackMixer {
private let includesSystemAudio: Bool
private let includesMicrophone: Bool
private let microphoneGain: Float
private let systemGain: Float
private let outputFormatDescription: CMAudioFormatDescription?

private var sources = [SourceTimeline](repeating: SourceTimeline(), count: Source.allCases.count)
Expand Down Expand Up @@ -136,6 +137,7 @@ public final class AudioTrackMixer {
includesSystemAudio: Bool,
includesMicrophone: Bool,
microphoneGain: Double,
systemGain: Double = 1,
clock: @escaping TimelineClock
) {
self.input = input
Expand All @@ -146,6 +148,8 @@ public final class AudioTrackMixer {
// does this. A non-finite or negative value would poison every mixed sample.
let sanitized = microphoneGain.isFinite ? max(0, microphoneGain) : 1
self.microphoneGain = Float(sanitized)
let sanitizedSystem = systemGain.isFinite ? max(0, systemGain) : 1
self.systemGain = Float(sanitizedSystem)
self.outputFormatDescription = Self.makeOutputFormatDescription()
}

Expand Down Expand Up @@ -397,7 +401,7 @@ public final class AudioTrackMixer {
private func gain(for source: Source) -> Float {
switch source {
case .system:
return 1
return systemGain
case .microphone:
return microphoneGain
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ struct RecordingRequest: Decodable {
struct Audio: Decodable {
struct SystemAudio: Decodable {
let enabled: Bool
let gain: Double?
}

struct Microphone: Decodable {
Expand Down Expand Up @@ -973,6 +974,7 @@ final class ScreenCaptureRecorder: NSObject, SCStreamOutput, SCStreamDelegate {
includesSystemAudio: request.audio.system.enabled,
includesMicrophone: nativeMicrophoneEnabled,
microphoneGain: request.audio.microphone.gain,
systemGain: request.audio.system.gain ?? 1,
clock: { [weak self] in self?.timelineNow() ?? .invalid }
)
}
Expand Down
83 changes: 76 additions & 7 deletions electron/native/wgc-capture/src/audio_sample_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -879,6 +879,68 @@ void mixAudioInPlace(
}
}

// Mixes up to two gain-scaled sources into a destination that starts as
// silence, accumulating in the destination's native numeric domain and clamping
// the COMBINED result exactly once. Applying each gain through a separate
// mixAudioInPlace clamps a boosted source to the rail before an
// opposite-polarity source can cancel it (clamp-then-sum); the wider double
// accumulator here preserves that headroom (sum-then-clamp).
// The destination is zero-filled by the caller, so it contributes nothing.
void mixTwoSourcesIntoSilence(
std::vector<BYTE>& destination,
const BYTE* systemSource,
double systemGain,
const BYTE* microphoneSource,
double microphoneGain,
const AudioInputFormat& format) {
const size_t bytesPer = bytesPerSample(format);
if (destination.empty() || bytesPer == 0) {
return;
}
const auto readRaw = [&](const BYTE* src, size_t index) -> double {
if (!src) {
return 0.0;
}
if (isFloatFormat(format)) {
return static_cast<double>(reinterpret_cast<const float*>(src)[index]);
}
if (isPcmFormat(format, 16)) {
return static_cast<double>(reinterpret_cast<const int16_t*>(src)[index]);
}
if (isPcmFormat(format, 32)) {
return static_cast<double>(reinterpret_cast<const int32_t*>(src)[index]);
}
return 0.0;
};
const size_t sampleCount = destination.size() / bytesPer;
if (isFloatFormat(format)) {
auto* output = reinterpret_cast<float*>(destination.data());
for (size_t i = 0; i < sampleCount; ++i) {
const double sum =
readRaw(systemSource, i) * systemGain + readRaw(microphoneSource, i) * microphoneGain;
output[i] = static_cast<float>(std::clamp(sum, -1.0, 1.0));
}
return;
}
if (isPcmFormat(format, 16)) {
auto* output = reinterpret_cast<int16_t*>(destination.data());
for (size_t i = 0; i < sampleCount; ++i) {
const double sum =
readRaw(systemSource, i) * systemGain + readRaw(microphoneSource, i) * microphoneGain;
output[i] = clampTo<int16_t>(sum);
}
return;
}
if (isPcmFormat(format, 32)) {
auto* output = reinterpret_cast<int32_t*>(destination.data());
for (size_t i = 0; i < sampleCount; ++i) {
const double sum =
readRaw(systemSource, i) * systemGain + readRaw(microphoneSource, i) * microphoneGain;
output[i] = clampTo<int32_t>(sum);
}
}
}

namespace {

// How far behind real time AudioMixer writes; see mixLoop. Wider than the capture
Expand All @@ -898,13 +960,15 @@ AudioMixer::AudioMixer(
bool includeSystem,
bool includeMicrophone,
double microphoneGain,
double systemGain,
OutputCallback output)
: format_(format),
systemFormat_(systemFormat),
microphoneFormat_(microphoneFormat),
includeSystem_(includeSystem),
includeMicrophone_(includeMicrophone),
microphoneGain_(microphoneGain),
systemGain_(systemGain),
output_(std::move(output)) {}

AudioMixer::~AudioMixer() {
Expand Down Expand Up @@ -1121,7 +1185,8 @@ void AudioMixer::mixLoop() {
const size_t chunkBytes = static_cast<size_t>(chunkFrames) * format_.blockAlign;
const uint64_t cushionFrames = static_cast<uint64_t>(format_.sampleRate) * MixerCushionMs / 1000;
std::vector<BYTE> mixedChunk;
std::vector<BYTE> sourceChunk;
std::vector<BYTE> systemChunk;
std::vector<BYTE> microphoneChunk;
// This loop's copy of `clockStart_`, taken under the lock at the top of each
// pass: beginTimeline and a resume move it from other threads.
std::chrono::steady_clock::time_point clockStart;
Expand All @@ -1145,15 +1210,19 @@ void AudioMixer::mixLoop() {
std::scoped_lock lock(mutex_);
mixedChunk.assign(chunkBytes, 0);
if (includeSystem_) {
pop(systemQueue_, systemStarved_, sourceChunk, chunkBytes);
mixAudioInPlace(mixedChunk, sourceChunk.data(), static_cast<DWORD>(sourceChunk.size()), format_);
pop(systemQueue_, systemStarved_, systemChunk, chunkBytes);
}
if (includeMicrophone_) {
pop(microphoneQueue_, microphoneStarved_, sourceChunk, chunkBytes);
mixAudioInPlace(
mixedChunk, sourceChunk.data(), static_cast<DWORD>(sourceChunk.size()), format_,
microphoneGain_);
pop(microphoneQueue_, microphoneStarved_, microphoneChunk, chunkBytes);
}
// Accumulate both sources in a wider double domain and clamp the
// sum once, so a boosted stream cannot hit the rail before the
// other stream's opposite polarity is added.
mixTwoSourcesIntoSilence(
mixedChunk,
includeSystem_ ? systemChunk.data() : nullptr, systemGain_,
includeMicrophone_ ? microphoneChunk.data() : nullptr, microphoneGain_,
format_);
mixedFrames_ = emittedFrames_ + chunkFrames;
}

Expand Down
2 changes: 2 additions & 0 deletions electron/native/wgc-capture/src/audio_sample_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ class AudioMixer {
bool includeSystem,
bool includeMicrophone,
double microphoneGain,
double systemGain,
OutputCallback output);
~AudioMixer();

Expand Down Expand Up @@ -262,6 +263,7 @@ class AudioMixer {
bool includeSystem_ = false;
bool includeMicrophone_ = false;
double microphoneGain_ = 1.0;
double systemGain_ = 1.0;
OutputCallback output_;
std::mutex mutex_;
std::condition_variable cv_;
Expand Down
Loading
Loading