-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
65 lines (50 loc) · 1.73 KB
/
server.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import axios from 'axios'
import path from 'path'
import { fileURLToPath } from 'url'
import fs from 'fs'
import 'dotenv/config'
const api = process.env.ELE_API
const voices_id = process.env.VOICES_ID
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// function makeid(length) {
// let result = '';
// const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
// const charactersLength = characters.length;
// let counter = 0;
// while (counter < length) {
// result += characters.charAt(Math.floor(Math.random() * charactersLength));
// counter += 1;
// }
// return result;
// }
const voices = async (text) => {
const apiUrl = 'https://api.elevenlabs.io/v1/text-to-speech/TznT71uWj2FUVm3YzCc9/stream?optimize_streaming_latency=0';
const headers = {
'accept': '*/*',
'xi-api-key': api,
'Content-Type': 'application/json',
};
const requestData = {
text: text,
model_id: 'eleven_monolingual_v1',
voice_settings: {
stability: 0,
similarity_boost: 0,
style: 0,
use_speaker_boost: true,
},
};
try {
// Mengirim permintaan POST ke API
const response = await axios.post(apiUrl, requestData, { headers, responseType: 'stream' });
// Menyimpan respons MP3 ke file
const mp3FileName = 'output.mp3';
const writer = fs.createWriteStream(mp3FileName);
response.data.pipe(writer);
return mp3FileName;
} catch (error) {
console.error('Terjadi kesalahan:', error);
}
}
export default voices