Skip to content

Commit bb5c8b2

Browse files
committed
fix: handle blob response, append wav header
1 parent 298df5c commit bb5c8b2

2 files changed

Lines changed: 29 additions & 5 deletions

File tree

examples/node-speak-live/index.js

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,34 @@
11
const fs = require("fs");
22
const { createClient, LiveTTSEvents } = require("../../dist/main/index");
33

4+
// Add a wav audio container header to the file if you want to play the audio
5+
// using the AudioContext or media player like VLC, Media Player, or Apple Music
6+
// Without this header in the Chrome browser case, the audio will not play.
7+
// prettier-ignore
8+
const wavHeader = [
9+
0x52, 0x49, 0x46, 0x46, // "RIFF"
10+
0x00, 0x00, 0x00, 0x00, // Placeholder for file size
11+
0x57, 0x41, 0x56, 0x45, // "WAVE"
12+
0x66, 0x6D, 0x74, 0x20, // "fmt "
13+
0x10, 0x00, 0x00, 0x00, // Chunk size (16)
14+
0x01, 0x00, // Audio format (1 for PCM)
15+
0x01, 0x00, // Number of channels (1)
16+
0x80, 0xBB, 0x00, 0x00, // Sample rate (48000)
17+
0x00, 0xEE, 0x02, 0x00, // Byte rate (48000 * 2)
18+
0x02, 0x00, // Block align (2)
19+
0x10, 0x00, // Bits per sample (16)
20+
0x64, 0x61, 0x74, 0x61, // "data"
21+
0x00, 0x00, 0x00, 0x00 // Placeholder for data size
22+
];
23+
424
const live = async () => {
525
const text = "Hello, how can I help you today?";
626

727
const deepgram = createClient(process.env.DEEPGRAM_API_KEY);
828

9-
const dgConnection = deepgram.speak.live({ model: "aura-asteria-en" });
29+
const dgConnection = deepgram.speak.live({ model: "aura-asteria-en", encoding: "linear16" });
1030

11-
let audioBuffer = Buffer.alloc(0);
31+
let audioBuffer = Buffer.from(wavHeader);
1232

1333
dgConnection.on(LiveTTSEvents.Open, () => {
1434
console.log("Connection opened");
@@ -47,14 +67,14 @@ const live = async () => {
4767

4868
const writeFile = () => {
4969
if (audioBuffer.length > 0) {
50-
fs.writeFile("output.mp3", audioBuffer, (err) => {
70+
fs.writeFile("output.wav", audioBuffer, (err) => {
5171
if (err) {
5272
console.error("Error writing audio file:", err);
5373
} else {
54-
console.log("Audio file saved as output.mp3");
74+
console.log("Audio file saved as output.wav");
5575
}
5676
});
57-
audioBuffer = Buffer.alloc(0); // Reset buffer after writing
77+
audioBuffer = Buffer.from(wavHeader); // Reset buffer after writing
5878
}
5979
};
6080
};

src/packages/SpeakLiveClient.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,10 @@ export class SpeakLiveClient extends AbstractLiveClient {
148148
error,
149149
});
150150
}
151+
} else if (event.data instanceof Blob) {
152+
event.data.arrayBuffer().then((buffer) => {
153+
this.handleBinaryMessage(Buffer.from(buffer));
154+
});
151155
} else if (event.data instanceof ArrayBuffer) {
152156
this.handleBinaryMessage(Buffer.from(event.data));
153157
} else if (Buffer.isBuffer(event.data)) {

0 commit comments

Comments
 (0)