|
1 | 1 | const fs = require("fs"); |
2 | 2 | const { createClient, LiveTTSEvents } = require("../../dist/main/index"); |
3 | 3 |
|
| 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 | + |
4 | 24 | const live = async () => { |
5 | 25 | const text = "Hello, how can I help you today?"; |
6 | 26 |
|
7 | 27 | const deepgram = createClient(process.env.DEEPGRAM_API_KEY); |
8 | 28 |
|
9 | | - const dgConnection = deepgram.speak.live({ model: "aura-asteria-en" }); |
| 29 | + const dgConnection = deepgram.speak.live({ model: "aura-asteria-en", encoding: "linear16" }); |
10 | 30 |
|
11 | | - let audioBuffer = Buffer.alloc(0); |
| 31 | + let audioBuffer = Buffer.from(wavHeader); |
12 | 32 |
|
13 | 33 | dgConnection.on(LiveTTSEvents.Open, () => { |
14 | 34 | console.log("Connection opened"); |
@@ -47,14 +67,14 @@ const live = async () => { |
47 | 67 |
|
48 | 68 | const writeFile = () => { |
49 | 69 | if (audioBuffer.length > 0) { |
50 | | - fs.writeFile("output.mp3", audioBuffer, (err) => { |
| 70 | + fs.writeFile("output.wav", audioBuffer, (err) => { |
51 | 71 | if (err) { |
52 | 72 | console.error("Error writing audio file:", err); |
53 | 73 | } else { |
54 | | - console.log("Audio file saved as output.mp3"); |
| 74 | + console.log("Audio file saved as output.wav"); |
55 | 75 | } |
56 | 76 | }); |
57 | | - audioBuffer = Buffer.alloc(0); // Reset buffer after writing |
| 77 | + audioBuffer = Buffer.from(wavHeader); // Reset buffer after writing |
58 | 78 | } |
59 | 79 | }; |
60 | 80 | }; |
|
0 commit comments