-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathElevenLabsServer.js
42 lines (34 loc) · 1.12 KB
/
ElevenLabsServer.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
const axios = require('axios');
const ENV = require('./env');
const ELEVENLABS_API_KEY = ENV.ELEVENLABS_API_KEY;
//Speak text with ElevenLabs
const Speak = async (req, res) => {
console.log("Speak");
const text = req.body.text;
var voiceId;
if(req.body.voiceId == null || req.body.voiceId == "")
voiceId = '21m00Tcm4TlvDq8ikWAM'; // default voice
else
voiceId = req.body.voiceId;
console.log("VoiceId " + voiceId);
const headers = {
'Accept': 'audio/mpeg',
'xi-api-key': ELEVENLABS_API_KEY,
'Content-Type': 'application/json'
};
const body = JSON.stringify({
text: text,
model_id: 'eleven_monolingual_v1',
voice_settings: {
stability: 0.5,
similarity_boost: 0.5
}
});
const response = await axios.post(`https://api.elevenlabs.io/v1/text-to-speech/${voiceId}/stream`, body, {
headers: headers,
responseType: 'arraybuffer' // This is important for handling binary data
});
const audio = Buffer.from(response.data, 'binary');
res.send(audio);
};
module.exports = Speak;