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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
var path = require('path');
var INCLUDE_DIR = ENV.PATHS.INCLUDE_DIR;
var Fs = require( path.join(INCLUDE_DIR, 'common', 'fileUtils.js') );
var RandStringGen = require ( path.join(INCLUDE_DIR, 'common',
'randStringGen.js') );
var fs = require('fs');
var interfaces = require( path.join(__dirname, 'iface_obj.js') );
const rosSrvName = "/rapp/rapp_text_to_speech_espeak/text_to_speech_topic";
const audioOutFormat = "wav";
const audioOutPath = ENV.PATHS.SERVICES_CACHE_DIR;
const basenamePrefix = "tts_";
var randStrGen = new RandStringGen(5);
function svcImpl(req, resp, ros) {
const unqCallId = randStrGen.createUnique();
const filePath = path.join(audioOutPath,
basenamePrefix + unqCallId + '.' + audioOutFormat);
var rosMsg = new interfaces.ros_req();
rosMsg.audio_output = filePath;
rosMsg.language = req.body.language;
rosMsg.text = req.body.text;
function callback(data) {
randStrGen.removeCached(unqCallId);
var response = parseRosbridgeMsg(data, filePath);
resp.sendJson(response);
}
function onerror(e) {
Fs.rmFile(filePath);
randStrGen.removeCached(unqCallId);
var response = new interfaces.client_res();
response.error = e;
resp.sendJson(response);
}
ros.callService(rosSrvName, rosMsg, {success: callback, fail: onerror});
}
function parseRosbridgeMsg(rosbridge_msg, audioFilePath) {
const error = rosbridge_msg.error;
const audioFile = Fs.readFileSync(audioFilePath);
var response = new interfaces.client_res();
if (error) {
response.error = error;
return response;
}
if (audioFile) {
response.payload = audioFile.data.toString('base64');
response.basename = audioFile.basename;
response.encoding = 'base64';
Fs.rmFile(audioFilePath);
}
return response;
}
module.exports = svcImpl;