-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathelevenlabs.js
42 lines (33 loc) · 912 Bytes
/
elevenlabs.js
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
35
36
37
38
39
40
41
42
import fs from "fs";
import util from "util";
import { v4 as uuidv4 } from 'uuid';
import axios from 'axios';
import stream from 'stream';
export { exportSpeech, initModule }
const pipeline = util.promisify(stream.pipeline);
let xApiKey;
const initModule = async(apikey) => {
xApiKey = apikey;
}
const exportSpeech = async (text, ttsParams) => {
let options = {
"text": text,
"voice_settings": {
"stability": 0.7,
"similarity_boost": 1
},
"model_id": ttsParams.model,
}
const res = await axios.post(`https://api.elevenlabs.io/v1/text-to-speech/${ttsParams.voiceId}`, options, {
headers: {
'content-type': 'application/json',
'xi-api-key': xApiKey
},
responseType: 'stream'
}).catch((e) => {
console.dir(e);
})
let uuid = uuidv4();
await pipeline(res.data, fs.createWriteStream(`./voiceMsgs/${uuid}.mp3`));
return `${uuid}.mp3`;
}