-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtts.mjs
More file actions
34 lines (29 loc) · 1.02 KB
/
tts.mjs
File metadata and controls
34 lines (29 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import { writeFile } from 'node:fs/promises';
async function textToSpeech(apiKey, voice, text, outputPath) {
const response = await fetch(
`https://api.narakeet.com/text-to-speech/mp3?voice=${voice}`,
{
method: 'POST',
headers: {
'Accept': 'application/octet-stream',
'Content-Type': 'text/plain',
'x-api-key': apiKey,
},
body: text,
signal: AbortSignal.timeout(30000),
},
);
if (!response.ok) {
const errorBody = await response.text();
throw new Error(`API error ${response.status}: ${errorBody}`);
}
const buffer = Buffer.from(await response.arrayBuffer());
await writeFile(outputPath, buffer);
}
const apiKey = process.env.NARAKEET_API_KEY;
if (!apiKey) {
console.log('Please set NARAKEET_API_KEY environment variable');
process.exit(1);
}
await textToSpeech(apiKey, 'mickey', 'Hi there from Node.js', 'output.mp3');
console.log('File saved at: output.mp3');