Skip to content

Node-based terminal manager for AI coding agents — tmux-backed terminals and parallel agent sessions as draggable nodes on an infinite pan/zoom canvas. macOS, Linux, and a browser Server Edition.

feat(speech): pcm/wav codecs (desktop-dictation Task 2)

enesa37e28a63c24
diff --git a/src/core/speech/pcm.test.ts b/src/core/speech/pcm.test.ts
new file mode 100644
index 000000000..ba06e36ca
--- /dev/null
+++ b/src/core/speech/pcm.test.ts
@@ -0,0 +1,35 @@
+import { describe, expect, it } from 'vitest'
+import { pcmToWav, float32ToInt16, int16ToFloat32, decodePcmPayload } from './pcm'
+
+describe('pcm helpers', () => {
+ it('writes a valid 16-bit mono WAV header', () => {
+ const wav = pcmToWav(new Float32Array([0, 0.5, -0.5, 1]), 16000)
+ const dv = new DataView(wav.buffer, wav.byteOffset)
+ expect(String.fromCharCode(...wav.subarray(0, 4))).toBe('RIFF')
+ expect(String.fromCharCode(...wav.subarray(8, 12))).toBe('WAVE')
+ expect(dv.getUint16(22, true)).toBe(1) // mono
+ expect(dv.getUint32(24, true)).toBe(16000) // sample rate
+ expect(dv.getUint16(34, true)).toBe(16) // bits per sample
+ expect(dv.getUint32(40, true)).toBe(8) // data bytes: 4 samples * 2
+ expect(wav.length).toBe(44 + 8)
+ })
+
+ it('round-trips float32 ↔ int16 with clamping', () => {
+ const src = new Float32Array([0, 0.5, -0.5, 1, -1, 1.5, -1.5])
+ const i16 = float32ToInt16(src)
+ expect(i16[5]).toBe(32767) // clamped
+ expect(i16[6]).toBe(-32768)
+ const back = int16ToFloat32(i16)
+ expect(back[1]).toBeCloseTo(0.5, 2)
+ expect(back[3]).toBeCloseTo(1, 2)
+ })
+
+ it('decodes both payload encodings to the same audio', () => {
+ const src = new Float32Array([0.25, -0.25, 0.75])
+ const fromBuffer = decodePcmPayload(src.buffer.slice(0))
+ expect(Array.from(fromBuffer)).toEqual(Array.from(src))
+ const b64 = Buffer.from(float32ToInt16(src).buffer).toString('base64')
+ const fromB64 = decodePcmPayload(b64)
+ expect(fromB64[2]).toBeCloseTo(0.75, 2)
+ })
+})
diff --git a/src/core/speech/pcm.ts b/src/core/speech/pcm.ts
new file mode 100644
index 000000000..d2acc8079
--- /dev/null
+++ b/src/core/speech/pcm.ts
@@ -0,0 +1,53 @@
+/** Pure PCM helpers for the speech pipeline. Two wire encodings exist:
+ * Electron IPC carries raw Float32 ArrayBuffers (structured clone), the ws
+ * bridge carries base64 int16 (half the bytes over JSON). Both normalize to
+ * Float32 here; whisper.cpp consumes Float32 directly and the Cloud engine
+ * wraps it into a 16-bit WAV. */
+
+export function float32ToInt16(pcm: Float32Array): Int16Array {
+ const out = new Int16Array(pcm.length)
+ for (let i = 0; i < pcm.length; i++) {
+ const v = Math.max(-1, Math.min(1, pcm[i]))
+ out[i] = Math.round(v < 0 ? v * 32768 : v * 32767)
+ }
+ return out
+}
+
+export function int16ToFloat32(pcm: Int16Array): Float32Array {
+ const out = new Float32Array(pcm.length)
+ for (let i = 0; i < pcm.length; i++) out[i] = pcm[i] / (pcm[i] < 0 ? 32768 : 32767)
+ return out
+}
+
+export function pcmToWav(pcm: Float32Array, sampleRate = 16000): Uint8Array {
+ const samples = float32ToInt16(pcm)
+ const dataBytes = samples.length * 2
+ const out = new Uint8Array(44 + dataBytes)
+ const dv = new DataView(out.buffer)
+ const ascii = (offset: number, s: string) => {
+ for (let i = 0; i < s.length; i++) out[offset + i] = s.charCodeAt(i)
+ }
+ ascii(0, 'RIFF')
+ dv.setUint32(4, 36 + dataBytes, true)
+ ascii(8, 'WAVE')
+ ascii(12, 'fmt ')
+ dv.setUint32(16, 16, true) // fmt chunk size
+ dv.setUint16(20, 1, true) // PCM
+ dv.setUint16(22, 1, true) // mono
+ dv.setUint32(24, sampleRate, true)
+ dv.setUint32(28, sampleRate * 2, true) // byte rate
+ dv.setUint16(32, 2, true) // block align
+ dv.setUint16(34, 16, true) // bits per sample
+ ascii(36, 'data')
+ dv.setUint32(40, dataBytes, true)
+ out.set(new Uint8Array(samples.buffer), 44)
+ return out
+}
+
+export function decodePcmPayload(payload: ArrayBuffer | string): Float32Array {
+ if (typeof payload === 'string') {
+ const raw = Buffer.from(payload, 'base64')
+ return int16ToFloat32(new Int16Array(raw.buffer, raw.byteOffset, raw.byteLength / 2))
+ }
+ return new Float32Array(payload)
+}