@@ -3,41 +3,38 @@ import fs from 'fs';
3
3
import dotenv from 'dotenv' ;
4
4
dotenv . config ( ) ;
5
5
6
+ const API_BASE_URL = 'https://api.sws.speechify.com' ;
7
+
6
8
export async function generateAudio (
7
9
voice_id : string ,
8
10
person : string ,
9
11
line : string ,
10
12
index : number
11
13
) {
12
- const response = await fetch ( 'https://api.neets.ai/ v1/tts' , {
14
+ const response = await fetch ( ` ${ API_BASE_URL } / v1/tts` , {
13
15
method : 'POST' ,
14
16
headers : {
15
17
'Content-Type' : 'application/json' ,
16
- 'X-API-Key ' : process . env . NEETS_API_KEY ?? '' ,
18
+ 'Authorization ' : `Bearer ${ process . env . SPEECHIFY_API_KEY } ` ,
17
19
} ,
18
20
body : JSON . stringify ( {
19
21
text : line ,
20
22
voice_id : voice_id ,
21
- params : {
22
- model : 'ar-diff-50k' ,
23
- } ,
23
+ output_format : 'mp3' ,
24
+ sample_rate : 24000 ,
25
+ speed : 1.0
24
26
} ) ,
25
27
} ) ;
26
28
27
29
if ( ! response . ok ) {
28
30
throw new Error ( `Server responded with status code ${ response . status } ` ) ;
29
31
}
30
32
31
- const audioStream = fs . createWriteStream (
32
- `public/voice/${ person } -${ index } .mp3`
33
- ) ;
33
+ // Get the audio buffer from the response
34
+ const audioBuffer = await response . buffer ( ) ;
34
35
35
- response . body . pipe ( audioStream ) ;
36
+ // Write the buffer to a file
37
+ fs . writeFileSync ( `public/voice/${ person } -${ index } .mp3` , audioBuffer ) ;
36
38
37
- return new Promise ( ( resolve , reject ) => {
38
- audioStream . on ( 'finish' , ( ) => {
39
- resolve ( 'Audio file saved as output.mp3' ) ;
40
- } ) ;
41
- audioStream . on ( 'error' , reject ) ;
42
- } ) ;
39
+ return Promise . resolve ( 'Audio file saved successfully' ) ;
43
40
}
0 commit comments