-
Notifications
You must be signed in to change notification settings - Fork 1
fix: add PCM to WAV encoding and TTS MIME support #123
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,57 @@ | ||||||||||||||||||||||||
| import type { SynthesizedAudio } from "../types.js"; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| const WAV_HEADER_BYTES = 44; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| function writeAscii(view: DataView, offset: number, value: string) { | ||||||||||||||||||||||||
| for (let i = 0; i < value.length; i++) { | ||||||||||||||||||||||||
| view.setUint8(offset + i, value.charCodeAt(i)); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||
| * Wrap raw PCM16LE bytes in a WAV container so browsers can play the result. | ||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||
| export function encodePcm16AsWav( | ||||||||||||||||||||||||
| pcmBase64: string, | ||||||||||||||||||||||||
| { | ||||||||||||||||||||||||
| channelCount = 1, | ||||||||||||||||||||||||
| sampleRate = 24_000, | ||||||||||||||||||||||||
| bytesPerSample = 2, | ||||||||||||||||||||||||
| }: { | ||||||||||||||||||||||||
| channelCount?: number; | ||||||||||||||||||||||||
| sampleRate?: number; | ||||||||||||||||||||||||
| bytesPerSample?: number; | ||||||||||||||||||||||||
| } = {}, | ||||||||||||||||||||||||
| ): SynthesizedAudio { | ||||||||||||||||||||||||
|
Comment on lines
+23
to
+25
|
||||||||||||||||||||||||
| bytesPerSample?: number; | |
| } = {}, | |
| ): SynthesizedAudio { | |
| bytesPerSample?: 2; | |
| } = {}, | |
| ): SynthesizedAudio { | |
| if (bytesPerSample !== 2) { | |
| throw new Error( | |
| `encodePcm16AsWav only supports 16-bit PCM input (bytesPerSample must be 2, received ${bytesPerSample}).`, | |
| ); | |
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| import { describe, expect, it } from "vitest"; | ||
| import { encodePcm16AsWav } from "../src/executor/wav.js"; | ||
|
|
||
| describe("encodePcm16AsWav", () => { | ||
| it("wraps PCM bytes in a WAV container with the expected header", () => { | ||
| const pcmBytes = Uint8Array.from([0x01, 0x02, 0x03, 0x04]); | ||
| const result = encodePcm16AsWav( | ||
| Buffer.from(pcmBytes).toString("base64"), | ||
| ); | ||
| const wavBytes = Buffer.from(result.audioBase64, "base64"); | ||
|
|
||
| expect(result.mimeType).toBe("audio/wav"); | ||
| expect(wavBytes.toString("ascii", 0, 4)).toBe("RIFF"); | ||
| expect(wavBytes.toString("ascii", 8, 12)).toBe("WAVE"); | ||
| expect(wavBytes.toString("ascii", 12, 16)).toBe("fmt "); | ||
| expect(wavBytes.readUInt16LE(20)).toBe(1); | ||
| expect(wavBytes.readUInt16LE(22)).toBe(1); | ||
| expect(wavBytes.readUInt32LE(24)).toBe(24_000); | ||
| expect(wavBytes.readUInt16LE(34)).toBe(16); | ||
| expect(wavBytes.toString("ascii", 36, 40)).toBe("data"); | ||
| expect(wavBytes.readUInt32LE(40)).toBe(pcmBytes.length); | ||
| expect([...wavBytes.subarray(44)]).toEqual([...pcmBytes]); | ||
| }); | ||
| }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
normalizeGeminiAudioResponse currently only accepts "audio/wav"/"audio/wave" and raw PCM (missing/"audio/l16"/"audio/pcm"). If Gemini returns other common but browser-playable audio types (e.g. audio/mpeg) or WAV aliases (e.g. audio/x-wav, audio/vnd.wave), this will throw and break TTS despite the PR goal of propagating MIME types. Consider normalizing additional WAV aliases and passing through supported codecs (or explicitly documenting/handling them) instead of throwing.