Skip to content

Voice-to-text dictation app with local (Nvidia Parakeet/Whisper) and cloud models (BYOK). Privacy-first and available cross-platform.

fix: segment long audio for Parakeet to prevent empty transcriptions

Gabriel Steinebb9120047b9
diff --git a/src/helpers/ffmpegUtils.js b/src/helpers/ffmpegUtils.js
index f72e34109..800bfaf91 100644
--- a/src/helpers/ffmpegUtils.js
+++ b/src/helpers/ffmpegUtils.js
@@ -246,6 +246,19 @@ function wavToFloat32Samples(wavBuffer) {
return float32;
}
+function computeFloat32RMS(float32Buffer) {
+ const numSamples = float32Buffer.length / 4;
+ if (numSamples === 0) return 0;
+
+ let sumSquares = 0;
+ for (let i = 0; i < numSamples; i++) {
+ const val = float32Buffer.readFloatLE(i * 4);
+ sumSquares += val * val;
+ }
+
+ return Math.sqrt(sumSquares / numSamples);
+}
+
function clearCache() {
cachedFFmpegPath = null;
}
@@ -255,5 +268,6 @@ module.exports = {
isWavFormat,
convertToWav,
wavToFloat32Samples,
+ computeFloat32RMS,
clearCache,
};
diff --git a/src/helpers/parakeetServer.js b/src/helpers/parakeetServer.js
index f02a53e58..a56545b88 100644
--- a/src/helpers/parakeetServer.js
+++ b/src/helpers/parakeetServer.js
@@ -2,10 +2,16 @@ const fs = require("fs");
const path = require("path");
const debugLogger = require("./debugLogger");
const { getModelsDirForService } = require("./modelDirUtils");
-const { getFFmpegPath, isWavFormat, convertToWav, wavToFloat32Samples } = require("./ffmpegUtils");
+const { getFFmpegPath, isWavFormat, convertToWav, wavToFloat32Samples, computeFloat32RMS } = require("./ffmpegUtils");
const { getSafeTempDir } = require("./safeTempDir");
const ParakeetWsServer = require("./parakeetWsServer");
+const SAMPLE_RATE = 16000;
+const BYTES_PER_SAMPLE = 4; // float32
+const MAX_SEGMENT_SECONDS = 30;
+const MAX_SEGMENT_BYTES = MAX_SEGMENT_SECONDS * SAMPLE_RATE * BYTES_PER_SAMPLE;
+const SILENCE_RMS_THRESHOLD = 0.001;
+
class ParakeetServerManager {
constructor() {
this.wsServer = new ParakeetWsServer();
@@ -93,9 +99,38 @@ class ParakeetServerManager {
if (!this.wsServer.ready || this.wsServer.modelName !== modelName) {
await this.wsServer.start(modelName, modelDir);
}
+
const samples = wavToFloat32Samples(wavBuffer);
- const result = await this.wsServer.transcribe(samples, 16000);
- return { ...result, language };
+ const durationSeconds = samples.length / BYTES_PER_SAMPLE / SAMPLE_RATE;
+
+ const rms = computeFloat32RMS(samples);
+ debugLogger.debug("Parakeet audio analysis", { durationSeconds, rms });
+ if (rms < SILENCE_RMS_THRESHOLD) {
+ return { text: "", elapsed: 0, language };
+ }
+
+ if (samples.length <= MAX_SEGMENT_BYTES) {
+ const result = await this.wsServer.transcribe(samples, SAMPLE_RATE);
+ return { ...result, language };
+ }
+
+ debugLogger.debug("Parakeet segmenting long audio", {
+ durationSeconds,
+ segmentCount: Math.ceil(samples.length / MAX_SEGMENT_BYTES),
+ });
+
+ const texts = [];
+ let totalElapsed = 0;
+
+ for (let offset = 0; offset < samples.length; offset += MAX_SEGMENT_BYTES) {
+ const end = Math.min(offset + MAX_SEGMENT_BYTES, samples.length);
+ const segment = samples.subarray(offset, end);
+ const result = await this.wsServer.transcribe(segment, SAMPLE_RATE);
+ totalElapsed += result.elapsed || 0;
+ if (result.text) texts.push(result.text);
+ }
+
+ return { text: texts.join(" "), elapsed: totalElapsed, language };
} finally {
this._cleanupFiles(filesToCleanup);
}