-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
60 lines (44 loc) · 1.55 KB
/
index.js
File metadata and controls
60 lines (44 loc) · 1.55 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
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
const express = require('express');
const app = express();
const fs = require('fs');
const TextToSpeechV1 = require('ibm-watson/text-to-speech/v1');
const { IamAuthenticator } = require('ibm-watson/auth');
const apikey = 'tF_Af-jOywiwBIYAXBWlLJC8ajbJUvp_8q6ClwXDDdPy';
const textToSpeech = new TextToSpeechV1({
authenticator: new IamAuthenticator({
apikey,
disableSslVerification: true
}),
url: 'https://api.us-south.text-to-speech.watson.cloud.ibm.com',
disableSslVerification: true
});
const text = 'Boa noite Brunão e Fernandão. Este é um exemplo.';
const voice = 'pt-BR_IsabelaV3Voice';
app.use(express.json());
const PORT = process.env.PORT || 443;
const fileFormat = 'mp3';
const params = {
text,
voice,
accept: `audio/${fileFormat}`
};
const fileName = `hello_world.${fileFormat}`;
app.listen(PORT, () => {
console.clear();
console.log('\n Starting server...');
console.log('\x1b[31m' + ' server.js' + '\x1b[0m' + ' listening to port ' + '\x1b[33m' + PORT + '\x1b[0m');
console.log('\x1b[96m' + ' http://localhost:' + PORT + '\x1b[0m');
console.log('\n /**logs**/\n');
textToSpeech.synthesize(params)
.then(response => {
// only necessary for wav formats,
// otherwise `response.result` can be directly piped to a file
return textToSpeech.repairWavHeaderStream(response.result);
})
.then(buffer => {
fs.writeFileSync(fileName, buffer);
})
.catch(err => {
console.log('error:', err);
});
});