The all-in-one local AI studio for your desktop: chat, image and video generation and a coding agent in one free, open source app. Windows and Linux. No Docker, no terminal, no cloud required.
TypeScript agentai-chatai-studioall-in-onecoding-agentdesktop-appfluximage-generationlinuxllmlocal-ailocal-llmopen-sourceprivacyself-hostedstable-diffusionuncensoreduncensored-aivideo-generationwindows
fix(stt): a mic below 16 kHz produced a WAV whose header lied
Davide8b30789dc1f
diff --git a/src/api/__tests__/pcm-to-wav.test.ts b/src/api/__tests__/pcm-to-wav.test.tsnew file mode 100644index 00000000..456ac505--- /dev/null+++ b/src/api/__tests__/pcm-to-wav.test.ts@@ -0,0 +1,89 @@+/**+ * The WAV the microphone path hands to whisper.+ *+ * The module's own header explains why LU encodes PCM by hand instead of using+ * MediaRecorder: a malformed blob is the "mic on but no text" bug. Nothing+ * tested the encoder itself.+ */+import { describe, it, expect } from 'vitest'+import { pcmToWav } from '../voice'++async function header(blob: Blob) {+ const buf = await blob.arrayBuffer()+ const v = new DataView(buf)+ const str = (off: number, len: number) =>+ String.fromCharCode(...new Uint8Array(buf, off, len))+ return {+ riff: str(0, 4),+ wave: str(8, 4),+ fmt: str(12, 4),+ audioFormat: v.getUint16(20, true),+ channels: v.getUint16(22, true),+ sampleRate: v.getUint32(24, true),+ byteRate: v.getUint32(28, true),+ blockAlign: v.getUint16(32, true),+ bits: v.getUint16(34, true),+ data: str(36, 4),+ dataLen: v.getUint32(40, true),+ totalLen: buf.byteLength,+ }+}++const tone = (n: number) =>+ Float32Array.from({ length: n }, (_, i) => Math.sin((i / 20) * Math.PI * 2) * 0.5)++describe('the header describes the samples that are actually in the file', () => {+ it('48 kHz input is downsampled and declared as 16 kHz', async () => {+ const h = await header(pcmToWav(tone(48000), 48000))+ expect(h.sampleRate).toBe(16000)+ // One second in, one second out.+ expect(h.dataLen / 2).toBe(16000)+ })++ it('a 16 kHz mic passes through and stays 16 kHz', async () => {+ const h = await header(pcmToWav(tone(16000), 16000))+ expect(h.sampleRate).toBe(16000)+ expect(h.dataLen / 2).toBe(16000)+ })++ it('an 8 kHz mic is declared as 8 kHz, not as 16 kHz', async () => {+ // A Bluetooth headset in HFP mode captures at 8 kHz. The samples are NOT+ // resampled (downsampleTo only ever lowers), so claiming 16 kHz made+ // whisper read the take at double speed.+ const h = await header(pcmToWav(tone(8000), 8000))+ expect(h.sampleRate).toBe(8000)+ expect(h.dataLen / 2).toBe(8000)+ // Rate-derived fields have to follow, or the file is inconsistent.+ expect(h.byteRate).toBe(8000 * 2)+ })++ it('keeps the rest of the header canonical for PCM mono 16-bit', async () => {+ const h = await header(pcmToWav(tone(1000), 44100))+ expect(h.riff).toBe('RIFF')+ expect(h.wave).toBe('WAVE')+ expect(h.fmt).toBe('fmt ')+ expect(h.data).toBe('data')+ expect(h.audioFormat).toBe(1)+ expect(h.channels).toBe(1)+ expect(h.bits).toBe(16)+ expect(h.blockAlign).toBe(2)+ expect(h.byteRate).toBe(h.sampleRate * 2)+ expect(h.totalLen).toBe(44 + h.dataLen)+ })++ it('an empty take produces a valid, empty file rather than junk', async () => {+ const h = await header(pcmToWav(new Float32Array(0), 48000))+ expect(h.riff).toBe('RIFF')+ expect(h.dataLen).toBe(0)+ expect(h.totalLen).toBe(44)+ })++ it('clamps out-of-range samples instead of wrapping them', async () => {+ const blob = pcmToWav(Float32Array.from([2, -2, 0]), 16000)+ const buf = await blob.arrayBuffer()+ const v = new DataView(buf)+ expect(v.getInt16(44, true)).toBe(32767)+ expect(v.getInt16(46, true)).toBe(-32768)+ expect(v.getInt16(48, true)).toBe(0)+ })+})diff --git a/src/api/voice.ts b/src/api/voice.tsindex de74c5b1..b4d946f9 100644--- a/src/api/voice.ts+++ b/src/api/voice.ts@@ -486,6 +486,24 @@ function downsampleTo(input: Float32Array, inRate: number, outRate: number): Floreturn out;}+/** Downsample to 16 kHz when the source is higher, then encode.+ *+ * The header carries the rate the samples are ACTUALLY at. It used to be+ * hard-coded to 16 kHz while downsampleTo returns its input untouched when the+ * source is already at or below the target — so a mic running below 16 kHz+ * produced a WAV whose header lied. A Bluetooth headset in HFP mode captures+ * at 8 kHz, which is the common case on Windows the moment the headset's+ * microphone is selected; whisper then read the take at double speed and the+ * transcript came back wrong or empty ("mic on but no text"). faster-whisper+ * resamples from whatever the header declares, so declaring it honestly is the+ * whole fix.+ */+export function pcmToWav(samples: Float32Array, inputRate: number): Blob {+ const rate = Math.min(inputRate, STT_TARGET_RATE);+ const ds = downsampleTo(samples, inputRate, STT_TARGET_RATE);+ return encodeWav(floatTo16BitPCM(ds), rate);+}+function encodeWav(samples: Int16Array, sampleRate: number): Blob {const buffer = new ArrayBuffer(44 + samples.length * 2);const view = new DataView(buffer);@@ -528,8 +546,7 @@ export function createAudioRecorder(): AudioRecorder {const merged = new Float32Array(total);let o = 0;for (const c of pcmChunks) { merged.set(c, o); o += c.length; }- const ds = downsampleTo(merged, inputRate, STT_TARGET_RATE);- return encodeWav(floatTo16BitPCM(ds), STT_TARGET_RATE);+ return pcmToWav(merged, inputRate);};return {