Skip to content

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

fix(audio): honor RIFF chunk padding in WAV readers (#2485)

Henry Sueae6b5e34131
diff --git a/src/helpers/ffmpegUtils.js b/src/helpers/ffmpegUtils.js
index 8d45a99a2..1e48f585e 100644
--- a/src/helpers/ffmpegUtils.js
+++ b/src/helpers/ffmpegUtils.js
@@ -277,7 +277,8 @@ function parseWavFormat(wavBuffer) {
};
}
- offset += 8 + chunkSize;
+ // RIFF chunks are word-aligned; chunkSize excludes the padding byte.
+ offset += 8 + chunkSize + (chunkSize % 2);
}
return null;
@@ -317,7 +318,8 @@ function wavToFloat32Samples(wavBuffer) {
break;
}
- offset += 8 + chunkSize;
+ // RIFF chunks are word-aligned; chunkSize excludes the padding byte.
+ offset += 8 + chunkSize + (chunkSize % 2);
}
if (dataOffset < 0) {
diff --git a/test/helpers/ffmpegUtils.test.js b/test/helpers/ffmpegUtils.test.js
index 14f591cc5..97665b12f 100644
--- a/test/helpers/ffmpegUtils.test.js
+++ b/test/helpers/ffmpegUtils.test.js
@@ -1,8 +1,13 @@
const test = require("node:test");
const assert = require("node:assert/strict");
-const { parseFfmpegDuration, isPcm16Mono16kWav } = require("../../src/helpers/ffmpegUtils");
-const { wavHeader } = require("./harness/wavFixtures");
+const {
+ parseFfmpegDuration,
+ parseWavFormat,
+ isPcm16Mono16kWav,
+ wavToFloat32Samples,
+} = require("../../src/helpers/ffmpegUtils");
+const { wavHeader, pcm16Mono16kWav, insertWavChunk } = require("./harness/wavFixtures");
test("parseFfmpegDuration reads the input duration from ffmpeg output", () => {
const stderr = "Input #0, mp3, from 'recording.mp3':\n Duration: 01:13:00.25, start: 0.000000";
@@ -21,3 +26,39 @@ test("isPcm16Mono16kWav accepts only what the local engines decode as-is", () =>
assert.equal(isPcm16Mono16kWav(wavHeader({ audioFormat: 3, bitsPerSample: 32 })), false);
assert.equal(isPcm16Mono16kWav(Buffer.from("not a wav")), false);
});
+
+const samples = [-32768, -16384, 0, 16384, 32767];
+const wav = pcm16Mono16kWav(samples);
+for (const [name, input] of [
+ ["no metadata", wav],
+ ["zero-length metadata", insertWavChunk(wav, "JUNK", Buffer.alloc(0), 12)],
+ ["even-length metadata", insertWavChunk(wav, "JUNK", Buffer.from([1, 2]), 12)],
+ ["odd-length metadata before fmt", insertWavChunk(wav, "JUNK", Buffer.from([42]), 12)],
+ ["odd-length metadata before data", insertWavChunk(wav, "JUNK", Buffer.from([42]), 36)],
+ ["three-byte metadata", insertWavChunk(wav, "JUNK", Buffer.from([1, 2, 3]), 36)],
+ [
+ "consecutive odd-length chunks",
+ insertWavChunk(
+ insertWavChunk(wav, "JUNK", Buffer.from([42]), 12),
+ "JUNK",
+ Buffer.from([7]),
+ 12
+ ),
+ ],
+]) {
+ test(`WAV chunk traversal preserves format and samples with ${name}`, () => {
+ assert.deepEqual(parseWavFormat(input), {
+ audioFormat: 1,
+ channels: 1,
+ sampleRate: 16000,
+ bitsPerSample: 16,
+ });
+ assert.equal(isPcm16Mono16kWav(input), true);
+ const decoded = wavToFloat32Samples(input);
+ assert.equal(decoded.length, samples.length * 4);
+ assert.deepEqual(
+ samples.map((_, index) => decoded.readFloatLE(index * 4)),
+ samples.map((sample) => sample / 32768)
+ );
+ });
+}
diff --git a/test/helpers/harness/wavFixtures.js b/test/helpers/harness/wavFixtures.js
index 16573388b..49199818c 100644
--- a/test/helpers/harness/wavFixtures.js
+++ b/test/helpers/harness/wavFixtures.js
@@ -31,4 +31,14 @@ function pcm16Mono16kWav(samples = [0, 100, -100, 200]) {
return pcm16ToWav(Buffer.from(Int16Array.from(samples).buffer));
}
-module.exports = { wavHeader, pcm16Mono16kWav };
+function insertWavChunk(wav, id, payload, offset) {
+ const chunk = Buffer.alloc(8 + payload.length + (payload.length % 2));
+ chunk.write(id, 0, 4, "ascii");
+ chunk.writeUInt32LE(payload.length, 4);
+ payload.copy(chunk, 8);
+ const result = Buffer.concat([wav.subarray(0, offset), chunk, wav.subarray(offset)]);
+ result.writeUInt32LE(result.length - 8, 4);
+ return result;
+}
+
+module.exports = { wavHeader, pcm16Mono16kWav, insertWavChunk };
diff --git a/test/helpers/parakeetWavNormalization.test.js b/test/helpers/parakeetWavNormalization.test.js
index f33a13644..84b06c7fa 100644
--- a/test/helpers/parakeetWavNormalization.test.js
+++ b/test/helpers/parakeetWavNormalization.test.js
@@ -5,6 +5,7 @@ const fs = require("node:fs");
const os = require("node:os");
const path = require("node:path");
const { getRequiredModelFiles } = require("../../src/helpers/parakeetModelInfo");
+const { insertWavChunk } = require("./harness/wavFixtures");
const MODEL_NAME = "parakeet-tdt-0.6b-v3";
@@ -58,10 +59,11 @@ function createPcm16Wav(sampleValue, sampleCount = 160) {
return wav;
}
-test("transcribes audible mono 16 kHz float32 WAV input", async () => {
+test("transcribes audible mono 16 kHz WAV input", async (t) => {
const tempHome = fs.mkdtempSync(path.join(os.tmpdir(), "openwhispr-parakeet-wav-test-"));
const originalLoad = Module._load;
let ffmpegUtils;
+ let conversions = 0;
Module._load = function loadWithElectronStub(request, parent, isMain) {
if (request === "electron") {
return {
@@ -76,6 +78,7 @@ test("transcribes audible mono 16 kHz float32 WAV input", async () => {
...ffmpegUtils,
getFFmpegPath: () => "/mock/ffmpeg",
convertToWav: async (_inputPath, outputPath) => {
+ conversions += 1;
fs.writeFileSync(outputPath, createPcm16Wav(0.5));
},
};
@@ -107,12 +110,35 @@ test("transcribes audible mono 16 kHz float32 WAV input", async () => {
return { text: "audible", elapsed: 0 };
},
};
+ // Subtests share the manager, so each starts from a clean slate.
+ t.beforeEach(() => {
+ conversions = 0;
+ receivedSamples = undefined;
+ });
- const result = await manager.transcribe(createFloat32Wav(0.5), { modelName: MODEL_NAME });
+ await t.test("float32 input still uses normalization", async () => {
+ const result = await manager.transcribe(createFloat32Wav(0.5), { modelName: MODEL_NAME });
+ assert.equal(result.text, "audible");
+ assert.equal(conversions, 1);
+ assert.ok(receivedSamples);
+ assert.ok(Math.abs(receivedSamples.readFloatLE(0) - 0.5) < 0.001);
+ });
- assert.equal(result.text, "audible");
- assert.ok(receivedSamples);
- assert.ok(Math.abs(receivedSamples.readFloatLE(0) - 0.5) < 0.001);
+ for (const [name, offset] of [
+ ["fmt", 12],
+ ["data", 36],
+ ]) {
+ await t.test(`PCM16 input with odd metadata before ${name} skips conversion`, async () => {
+ const input = insertWavChunk(createPcm16Wav(0.5), "JUNK", Buffer.from([42]), offset);
+ const result = await manager.transcribe(input, { modelName: MODEL_NAME });
+ assert.equal(result.text, "audible");
+ assert.equal(conversions, 0);
+ assert.equal(receivedSamples.length, 160 * 4);
+ for (let index = 0; index < 160; index += 1) {
+ assert.equal(receivedSamples.readFloatLE(index * 4), Math.round(0.5 * 32767) / 32768);
+ }
+ });
+ }
} finally {
fs.rmSync(tempHome, { recursive: true, force: true });
}