-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTTS.js
More file actions
65 lines (59 loc) · 2.35 KB
/
Copy pathTTS.js
File metadata and controls
65 lines (59 loc) · 2.35 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
61
62
63
64
65
module.exports = function(RED) {
function TextToSpeechNode(config){
var http=require('https');
var fs=require('fs');
this.context = {global:RED.settings.functionGlobalContext || {}};
RED.nodes.createNode(this,config);
var node = this;
this.name=config.name;
this.text=config.text;
this.tmp_dir=config.tmp_dir;
this.blackflag=config.blackflag;
var wav=new Buffer(100000);
this.on('input', function(msg) {
if(msg.payload.text)
this.text=msg.payload.text;
if(msg.tmp_dir)
this.tmp_dir=msg.tmp_dir;
console.log("tmp_dir "+this.tmp_dir);
console.log("msg.text "+msg.text);
console.log("this.text "+this.text);
var headers={ 'Content-Type': 'text/plain', 'Accept': 'audio/x-wav','Content-Length':this.text.length };
headers.Authorization="Bearer "+this.context.global[this.blackflag];
console.log(headers);
var options = {
hostname: 'api.att.com',
port: 443,
path: '/speech/v3/textToSpeech',
method: 'POST'
};
var file_name="";
file_name+=this.tmp_dir;
var date=new Date().getTime();
var short_name="";
short_name+=date.toString()+".wav";
file_name+="/"+short_name;
options.headers=headers;
var req=http.request(options,function(res){
var i=0;
res.on('data',function(chunk){
fs.appendFileSync(file_name,chunk);
console.log("got a chunk");
if(i==0){
msg.payload.audio_file=short_name;
node.send(msg);
i=1;
}
});
});
req.on('error',function(e){
console.log('problem with request: ' +e.message);
});
console.log(this.text);
req.write(this.text);
req.end();
// do something with 'msg'
});
}
RED.nodes.registerType("T SPEECH",TextToSpeechNode);
}