Voice-to-text dictation app with local (Nvidia Parakeet/Whisper) and cloud models (BYOK). Privacy-first and available cross-platform.
JavaScript aianthropiccross-platformgeminigroqlinuxmacosnvidiaopen-sourceopenaiparakeetspeech-to-texttranscribewhisperwindows
fix(audio): prevent empty recordings from crashing transcription (#891)
Alcaheste2df9a364899
diff --git a/src/helpers/audioManager.js b/src/helpers/audioManager.jsindex 80dad2c4f..6274b8680 100644--- a/src/helpers/audioManager.js+++ b/src/helpers/audioManager.js@@ -28,10 +28,12 @@ import { detectAgentName } from "../config/agentDetection";import { resolveDictationRouteKind, resolveDictationAgentReachability } from "./dictationRouting";import { resolvePrompt } from "../config/prompts";import { syncService } from "../services/SyncService.js";+import { evaluateFinishedRecording } from "./recordingValidation";import { matchesDictionaryPrompt } from "../utils/dictionaryEchoFilter.js";import { getDictionaryHintWords } from "../utils/snippets";const REASONING_CACHE_TTL = 30000; // 30 seconds+const RECORDING_TIMESLICE_MS = 250; // flush chunks periodically so short recordings still carry audio frames. See #871.const REALTIME_MODELS = new Set(["gpt-4o-mini-transcribe", "gpt-4o-transcribe"]);function dictationAgentReachable(settings) {@@ -402,6 +404,23 @@ registerProcessor("pcm-streaming-processor", PCMStreamingProcessor);}}+ // Briefly acquire and release the mic so the OS audio driver is warm before+ // the first real recording, reducing cold-start empty captures. See #871.+ async warmupMicDriver() {+ if (this.micDriverWarmedUp) return;+ // Skip while a recording is active so we don't double-acquire the mic. See #871.+ if (this.isRecording || this.isProcessing || this.mediaRecorder?.state === "recording") return;+ try {+ const constraints = await this.getAudioConstraints();+ const tempStream = await navigator.mediaDevices.getUserMedia(constraints);+ tempStream.getTracks().forEach((track) => track.stop());+ this.micDriverWarmedUp = true;+ logger.debug("Microphone driver pre-warmed", {}, "audio");+ } catch (e) {+ logger.debug("Mic driver warmup failed (non-critical)", { error: e.message }, "audio");+ }+ }+async startRecording(forceDefaultMic = false) {try {if (this.isRecording || this.isProcessing || this.mediaRecorder?.state === "recording") {@@ -463,11 +482,15 @@ registerProcessor("pcm-streaming-processor", PCMStreamingProcessor);this.mediaRecorder = new MediaRecorder(micStream);this.audioChunks = [];+ this._receivedAudioData = false;this.recordingStartTime = Date.now();this.recordingMimeType = this.mediaRecorder.mimeType || "audio/webm";this.mediaRecorder.ondataavailable = (event) => {- this.audioChunks.push(event.data);+ if (event.data && event.data.size > 0) {+ this._receivedAudioData = true;+ this.audioChunks.push(event.data);+ }};this.mediaRecorder.onstop = async () => {@@ -502,12 +525,36 @@ registerProcessor("pcm-streaming-processor", PCMStreamingProcessor);? (Date.now() - this.recordingStartTime) / 1000: null;this.recordingStartTime = null;++ // Drop header-only / no-frame recordings before they crash FFmpeg. See #871.+ const recordingCheck = evaluateFinishedRecording({+ blobSize: audioBlob.size,+ receivedAudioData: this._receivedAudioData,+ });+ if (!recordingCheck.usable) {+ logger.info(+ "Dropping degenerate recording before transcription",+ {+ blobSize: audioBlob.size,+ reason: recordingCheck.reason,+ receivedAudioData: this._receivedAudioData,+ },+ "audio"+ );+ this.isProcessing = false;+ this._localSpeechGateState = null;+ this.onStateChange?.({ isRecording: false, isProcessing: false });+ this.onTranscriptionComplete?.({ success: true, text: "" });+ micStream.getTracks().forEach((track) => track.stop());+ return;+ }+await this.processAudio(audioBlob, { durationSeconds });micStream.getTracks().forEach((track) => track.stop());};- this.mediaRecorder.start();+ this.mediaRecorder.start(RECORDING_TIMESLICE_MS);this.isRecording = true;this.onStateChange?.({ isRecording: true, isProcessing: false });diff --git a/src/helpers/recordingGuard.js b/src/helpers/recordingGuard.jsnew file mode 100644index 000000000..4fc4b8f89--- /dev/null+++ b/src/helpers/recordingGuard.js@@ -0,0 +1,13 @@+// A recorded WebM/Opus blob with no audio frames — produced when the dictation+// hotkey toggles recording on and off within milliseconds (an accidental+// double-tap, or the KDE double-trigger fixed in main.js) — is essentially just+// the container header, well under 256 bytes. Real speech, even a single short+// word, produces far more. We gate on size, not wall-clock duration: a genuinely+// short utterance can last under any reasonable time threshold yet still carry+// real audio, so a duration gate would silently drop it. See issue #864.+export const MIN_AUDIO_BYTES = 256;++export function isEmptyRecording(blobSize) {+ const size = typeof blobSize === "number" && Number.isFinite(blobSize) ? blobSize : 0;+ return size < MIN_AUDIO_BYTES;+}diff --git a/src/helpers/recordingValidation.js b/src/helpers/recordingValidation.jsnew file mode 100644index 000000000..0d28c0470--- /dev/null+++ b/src/helpers/recordingValidation.js@@ -0,0 +1,14 @@+import { isEmptyRecording } from "./recordingGuard.js";++// Decide whether a finished MediaRecorder session carries real audio before we+// hand it to the transcription backend. A fast tap can flush only the container+// header, or deliver no chunks at all, which crashes FFmpeg. See issue #871.+export function evaluateFinishedRecording({ blobSize, receivedAudioData } = {}) {+ if (!receivedAudioData) {+ return { usable: false, reason: "no-audio-data" };+ }+ if (isEmptyRecording(blobSize)) {+ return { usable: false, reason: "empty-container" };+ }+ return { usable: true, reason: null };+}diff --git a/src/hooks/useAudioRecording.js b/src/hooks/useAudioRecording.jsindex 93e5a3962..bec80f94c 100644--- a/src/hooks/useAudioRecording.js+++ b/src/hooks/useAudioRecording.js@@ -206,6 +206,8 @@ export const useAudioRecording = (toast, options = {}) => {const handleToggle = async ({ voiceAgentRequested = false } = {}) => {if (!audioManagerRef.current) return;+ // Lazily warm the mic driver on first dictation use, not at launch. See #871.+ audioManagerRef.current.warmupMicDriver?.();const currentState = audioManagerRef.current.getState();if (!currentState.isRecording && !currentState.isProcessing) {@@ -216,6 +218,7 @@ export const useAudioRecording = (toast, options = {}) => {};const handleStart = async () => {+ audioManagerRef.current?.warmupMicDriver?.();await performStartRecording();};diff --git a/test/helpers/recordingGuard.test.js b/test/helpers/recordingGuard.test.jsnew file mode 100644index 000000000..65369a62f--- /dev/null+++ b/test/helpers/recordingGuard.test.js@@ -0,0 +1,46 @@+const test = require("node:test");+const assert = require("node:assert/strict");++const load = () => import("../../src/helpers/recordingGuard.js");++test("flags an empty WebM container (issue #864 double-trigger, ~110 bytes)", async () => {+ const { isEmptyRecording } = await load();+ assert.equal(isEmptyRecording(110), true);+});++test("flags a zero-byte blob", async () => {+ const { isEmptyRecording } = await load();+ assert.equal(isEmptyRecording(0), true);+});++test("flags 255 bytes (just under the threshold)", async () => {+ const { isEmptyRecording } = await load();+ assert.equal(isEmptyRecording(255), true);+});++test("allows exactly 256 bytes (boundary)", async () => {+ const { isEmptyRecording } = await load();+ assert.equal(isEmptyRecording(256), false);+});++test("allows a short real utterance with real audio bytes (no silent data loss)", async () => {+ const { isEmptyRecording } = await load();+ assert.equal(isEmptyRecording(3000), false);+});++test("allows a normal full-length recording", async () => {+ const { isEmptyRecording } = await load();+ assert.equal(isEmptyRecording(1674472), false);+});++test("treats missing / null / NaN size as empty (defensive)", async () => {+ const { isEmptyRecording } = await load();+ assert.equal(isEmptyRecording(undefined), true);+ assert.equal(isEmptyRecording(null), true);+ assert.equal(isEmptyRecording(NaN), true);+});++test("MIN_AUDIO_BYTES is 256", async () => {+ const { MIN_AUDIO_BYTES } = await load();+ assert.equal(MIN_AUDIO_BYTES, 256);+});diff --git a/test/helpers/recordingValidation.test.js b/test/helpers/recordingValidation.test.jsnew file mode 100644index 000000000..ca8b16468--- /dev/null+++ b/test/helpers/recordingValidation.test.js@@ -0,0 +1,54 @@+const test = require("node:test");+const assert = require("node:assert/strict");++const load = () => import("../../src/helpers/recordingValidation.js");++test("allows a recording with audio frames and a real-sized blob", async () => {+ const { evaluateFinishedRecording } = await load();+ assert.deepEqual(evaluateFinishedRecording({ blobSize: 50000, receivedAudioData: true }), {+ usable: true,+ reason: null,+ });+});++test("rejects when no audio chunk was ever delivered (issue #871 cold start)", async () => {+ const { evaluateFinishedRecording } = await load();+ assert.deepEqual(evaluateFinishedRecording({ blobSize: 0, receivedAudioData: false }), {+ usable: false,+ reason: "no-audio-data",+ });+});++test("rejects a header-only ~110 byte blob even when a chunk arrived", async () => {+ const { evaluateFinishedRecording } = await load();+ assert.deepEqual(evaluateFinishedRecording({ blobSize: 110, receivedAudioData: true }), {+ usable: false,+ reason: "empty-container",+ });+});++test("allows exactly 256 bytes (boundary, mirrors MIN_AUDIO_BYTES)", async () => {+ const { evaluateFinishedRecording } = await load();+ assert.deepEqual(evaluateFinishedRecording({ blobSize: 256, receivedAudioData: true }), {+ usable: true,+ reason: null,+ });+});++test("rejects 255 bytes (just under the threshold)", async () => {+ const { evaluateFinishedRecording } = await load();+ assert.deepEqual(evaluateFinishedRecording({ blobSize: 255, receivedAudioData: true }), {+ usable: false,+ reason: "empty-container",+ });+});++test("treats missing / undefined args as unusable (defensive, does not throw)", async () => {+ const { evaluateFinishedRecording } = await load();+ assert.deepEqual(evaluateFinishedRecording(), { usable: false, reason: "no-audio-data" });+ assert.deepEqual(evaluateFinishedRecording({}), { usable: false, reason: "no-audio-data" });+ assert.deepEqual(evaluateFinishedRecording({ receivedAudioData: true }), {+ usable: false,+ reason: "empty-container",+ });+});