Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 36 additions & 2 deletions pm-stack/voice-line-worker/src/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,33 @@ function base64ToBytes(b64: string): Uint8Array {
return out;
}

export function wavEncodePcm16(pcm: Uint8Array, sampleRate: number): Uint8Array {
// Wrap raw PCM16LE mono in a 44-byte RIFF/WAVE header. Whisper
// rejects headerless PCM with AiError "Invalid input" (code 8001) —
// found live during the task 16482 deploy proof, 2026-11-16: the
// binding call threw, the utterance died silently, no ledger row.
const out = new Uint8Array(44 + pcm.byteLength);
const view = new DataView(out.buffer);
const writeAscii = (off: number, text: string) => {
for (let i = 0; i < text.length; i++) view.setUint8(off + i, text.charCodeAt(i));
};
writeAscii(0, "RIFF");
view.setUint32(4, 36 + pcm.byteLength, true);
writeAscii(8, "WAVE");
writeAscii(12, "fmt ");
view.setUint32(16, 16, true);
view.setUint16(20, 1, true);
view.setUint16(22, 1, true);
view.setUint32(24, sampleRate, true);
view.setUint32(28, sampleRate * 2, true);
view.setUint16(32, 2, true);
view.setUint16(34, 16, true);
writeAscii(36, "data");
view.setUint32(40, pcm.byteLength, true);
out.set(pcm, 44);
return out;
}

export class VoiceSession {
private chunks: Uint8Array[] = [];
private startedAt = new Date().toISOString();
Expand Down Expand Up @@ -86,7 +113,14 @@ export class VoiceSession {
this.startedAt = new Date().toISOString();
this.send(ws, { type: "ready" });
} else if (control.type === "stop") {
await this.processUtterance(ws);
try {
await this.processUtterance(ws);
} catch (err) {
this.send(ws, {
type: "error",
error: `processing failed: ${err instanceof Error ? err.message : String(err)}`,
});
}
} else {
this.send(ws, { type: "error", error: `unknown control type: ${control.type ?? "(none)"}` });
}
Expand All @@ -113,7 +147,7 @@ export class VoiceSession {
// Speech-to-text: ONLY the trimmed audio is submitted (never billed
// for silence — #9800).
const stt = (await this.env.AI.run(STT_MODEL, {
audio: bytesToBase64(trimmed.audio),
audio: bytesToBase64(wavEncodePcm16(trimmed.audio, SAMPLE_RATE)),
})) as { text?: string };
const transcript = (stt.text ?? "").trim();
this.send(ws, {
Expand Down
37 changes: 37 additions & 0 deletions pm-stack/voice-line-worker/test/wav.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// lane-scratch: code-c /tmp/lane-code-c-20261116-0412 task 16482
import { describe, expect, test } from "bun:test";
import { wavEncodePcm16 } from "../src/session";

// Oracle: Whisper (Workers AI) accepts a RIFF/WAVE container and rejects
// headerless PCM with AiError "Invalid input" (code 8001) — verified live
// against @cf/openai/whisper-large-v3-turbo during the #9800 deploy proof
// (task 16482, 2026-11-16). The encoder must produce a standards-shaped
// 44-byte PCM16 mono header and preserve the payload byte-for-byte.
describe("wavEncodePcm16", () => {
test("wraps PCM in a 44-byte RIFF/WAVE header", () => {
const pcm = new Uint8Array([0x01, 0x00, 0xff, 0x7f, 0x00, 0x80]);
const wav = wavEncodePcm16(pcm, 16000);
expect(wav.byteLength).toBe(44 + pcm.byteLength);
const ascii = (off: number, n: number) =>
String.fromCharCode(...wav.subarray(off, off + n));
expect(ascii(0, 4)).toBe("RIFF");
expect(ascii(8, 4)).toBe("WAVE");
expect(ascii(12, 4)).toBe("fmt ");
expect(ascii(36, 4)).toBe("data");
const view = new DataView(wav.buffer);
expect(view.getUint32(4, true)).toBe(36 + pcm.byteLength);
expect(view.getUint16(20, true)).toBe(1); // PCM format
expect(view.getUint16(22, true)).toBe(1); // mono
expect(view.getUint32(24, true)).toBe(16000); // sample rate
expect(view.getUint32(28, true)).toBe(32000); // byte rate
expect(view.getUint16(34, true)).toBe(16); // bits per sample
expect(view.getUint32(40, true)).toBe(pcm.byteLength);
expect([...wav.subarray(44)]).toEqual([...pcm]);
});

test("empty PCM still yields a valid header", () => {
const wav = wavEncodePcm16(new Uint8Array(0), 16000);
expect(wav.byteLength).toBe(44);
expect(new DataView(wav.buffer).getUint32(40, true)).toBe(0);
});
});
2 changes: 1 addition & 1 deletion pm-stack/voice-line-worker/wrangler.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ new_sqlite_classes = ["VoiceSession"]
# Turso session ledger (#9800). TURSO_TOKEN and STATUS_INGEST_TOKEN are
# secrets (wrangler secret put), never vars in this file.
[vars]
TURSO_URL = "REPLACE_WITH_TURSO_DATABASE_URL"
TURSO_URL = "https://voice-line-ledger-dezren39.aws-us-east-1.turso.io"