Open source Loom alternative. Beautiful, shareable screen recordings.
Rust appcapcossloommacnextjsnextjs15open-sourceossreactrecordrustscreen-capturescreen-recorderscreenshotsolidjstauritauri-apptypescriptvite
feat(chrome-extension): probe microphone signal in offscreen document
Richie McIlroye4ff1a298f0d
diff --git a/apps/chrome-extension/src/offscreen/recorder.ts b/apps/chrome-extension/src/offscreen/recorder.tsindex 66fe8e2cd..8551646cf 100644--- a/apps/chrome-extension/src/offscreen/recorder.ts+++ b/apps/chrome-extension/src/offscreen/recorder.ts@@ -45,6 +45,7 @@ import {import type {ConnectCameraPreviewRequest,ExtensionSettings,+ MicrophoneProbeResult,MicrophoneSettings,OffscreenRequest,OffscreenResponse,@@ -510,6 +511,68 @@ const getMicrophoneStream = async (}};+// How long a probe listens before declaring the mic silent, and the peak+// amplitude (time-domain, 0..1) that counts as sound. A muted/dead device+// flat-lines near 0; a working mic's noise floor clears this threshold, so it+// only flags an effectively-silent input.+const MIC_PROBE_WINDOW_MS = 1200;+const MIC_PROBE_SAMPLE_INTERVAL_MS = 50;+const MIC_SOUND_MIN_PEAK = 0.0015;++// Opens the selected mic and listens for any signal so the recorder can warn+// before starting. Resolves as soon as sound is heard; only a genuinely silent+// mic waits out the full window. Unlike a popup, the offscreen AudioContext is+// not blocked by the page autoplay policy, so the analyser actually runs.+const probeMicrophone = async (+ microphone: MicrophoneSettings,+): Promise<MicrophoneProbeResult> => {+ if (!microphone.enabled) return { available: false, hasSound: false };++ let stream: MediaStream;+ try {+ stream = await navigator.mediaDevices.getUserMedia({+ audio: getAudioConstraint(microphone),+ video: false,+ });+ } catch {+ return { available: false, hasSound: false };+ }++ const context = new AudioContext();+ try {+ if (context.state === "suspended") {+ await context.resume().catch(() => undefined);+ }+ const source = context.createMediaStreamSource(stream);+ const analyser = context.createAnalyser();+ analyser.fftSize = 2048;+ source.connect(analyser);+ const buffer = new Float32Array(analyser.fftSize);++ const deadline = performance.now() + MIC_PROBE_WINDOW_MS;+ let peak = 0;+ while (performance.now() < deadline) {+ analyser.getFloatTimeDomainData(buffer);+ for (let i = 0; i < buffer.length; i += 1) {+ const amplitude = Math.abs(buffer[i]);+ if (amplitude > peak) peak = amplitude;+ }+ if (peak >= MIC_SOUND_MIN_PEAK) break;+ await new Promise<void>((resolve) => {+ window.setTimeout(resolve, MIC_PROBE_SAMPLE_INTERVAL_MS);+ });+ }+ return { available: true, hasSound: peak >= MIC_SOUND_MIN_PEAK };+ } catch {+ // If the measurement itself fails, do not block the recording with a+ // false silence warning.+ return { available: true, hasSound: true };+ } finally {+ stopTracks(stream);+ await context.close().catch(() => undefined);+ }+};+const addAudioTracks = ({output,streams,@@ -1588,6 +1651,10 @@ const handleRequest = async (return { ok: true, devices: await enumerateMediaDevices() };}+ if (message.type === "probe-microphone") {+ return { ok: true, micProbe: await probeMicrophone(message.microphone) };+ }+return { ok: true, status };};