-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathnuance.js
293 lines (256 loc) · 9.54 KB
/
nuance.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
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
var fs = require("fs");
var https = require('https');
var unirest = require("unirest");
/**
* A Nuance HTTP client for NodeJS.
*
* @constructor
* @author Elad Cohen
* @version 1.1.0
*/
var Nuance = function(appID, appKey){
var self = this;
var dictationBaseURL = "https://dictation.nuancemobility.net:443/NMDPAsrCmdServlet/dictation";
var ttsBaseURL = "https://tts.nuancemobility.net/NMDPTTSCmdServlet/tts";
var fileContent;
/**
* Change these headers to the headers that apply to you (if needed), or add additionalHeaders to the
* sendDictationRequest method.
* You may also use the setDictationHeaders method instead.
*/
var dictationHeaders = {
"Content-Type": "audio/amr",
"Accept": "text/plain",
"Accept-Topic": "Dictation"
};
/**
* Sends a TTS request to Nuance's service.
* The options may include:
* identifier - Required - A unique identifier for the user who sent request.
* text - Required - The text that you want to convert to speech (for example: "hello world").
* output - Required - The output file (for example: "speech.wav").
* outputFormat - Required - The codec you would like to use (for example: "wav").
* language - Required if voice was not supplied - The language you want to use (for example: "en_US").
* voice - Required if language was not supplied - The voice you want to use (for example: "Tom").
* success - Optional - The callback function to be called after the file was written.
*
* @param options
*/
self.sendTTSRequest = function(options){
if(!appID || !appKey){
if(typeof options.error === "function"){
options.error("Invalid appID or appKey.");
}
return;
}
if(!options || !options.text || !options.identifier || !options.output || !options.outputFormat || (!options.language && !options.voice)){
if(typeof options.error === "function"){
options.error("Invalid options.");
}
return;
}
var ttsURL = ttsBaseURL + "?appId=" + appID + "&appKey=" + appKey + "&id=" + options.identifier;
if(options.voice){
ttsURL += "&voice=" + options.voice;
}
else if(options.language){
ttsURL += "&ttsLang=" + options.language;
}
ttsURL += "&text=" + encodeURIComponent(options.text) + "&codec=" + options.outputFormat;
var file = fs.createWriteStream(options.output);
file.on('finish',()=>{
if(typeof options.success === "function"){
options.success();
}
});
file.on('error',(err)=>{
if(typeof options.error === "function"){
options.error(err);
}
});
https.get(ttsURL, function(response) {
if(response.statusCode && response.statusCode != 200){
if(typeof options.error === "function"){
options.error(response);
}
}
else{
response.pipe(file);
}
});
};
/**
* Sends the request and returns the string of the analyzed audio file.
* The options may include:
* identifier - Required - A unique identifier for the user who sent request.
* language - Required - The language the audio file was recorded with.
* path - Required - The audio file's path.
* additionalHeaders - Optional - Additional headers.
* success - Required - The callback function to be called after the file was written.
* error - Optional - The callback function to be called if an error was occurred.
*
* @param options
*/
self.sendDictationRequest = function(options){
if(!appID || !appKey){
if(typeof options.error === "function"){
options.error("Invalid appID or appKey.");
}
return;
}
if(!options || !options.language || !options.identifier || (!fileContent && !options.path) || typeof options.success !== 'function'){
if(typeof options.error === "function"){
options.error("Invalid options.");
}
return;
}
dictationHeaders["Accept-Language"] = options.language;
dictationHeaders["Content-Language"] = options.language;
if(typeof options.additionalHeaders === 'object'){
dictationHeaders = mergeAssociativeArrays(dictationHeaders, options.additionalHeaders);
}
dictationURL = dictationBaseURL + "?appId=" + appID + "&appKey=" + appKey + "&id=" + options.identifier;
if(options.path){
fs.readFile(options.path, [], function(err, data){
if(!err){
fileContent = data;
dictationHeaders["Content-Length"] = fileContent.length;
sendUnirestRequest(dictationURL, dictationHeaders, fileContent,
function(response){
options.success(response.split("\n"));
},
function(response){
if(typeof options.error === "function"){
options.error(response);
}
}
);
}
else if(typeof options.error === "function"){
options.error(err);
}
});
}
else{
sendUnirestRequest(dictationURL, dictationHeaders, fileContent,
function(response){
options.success(response.split("\n"));
},
function(response){
if(typeof options.error === "function"){
options.error(response);
}
}
);
}
};
/**
* Sends the request and returns the string of the analyzed audio file.
*
* @param language The language the audio file was recorded with.
* @param identifier A unique identifier for the user who uploaded the audio file.
* @param additionalHeaders Additional headers - optional.
* @param callback The callback function that the result will be sent to.
* @deprecated Use the sendDictationRequest method instead.
*/
self.sendRequest = function(language, identifier, additionalHeaders, callback){
self.sendDictationRequest({
"language": language,
"identifier": identifier,
"additionalHeaders": additionalHeaders,
"success": function(resp){
callback(undefined, resp);
},
"error": function(resp){
callback(resp);
}
});
};
/**
* Sets the instance's audio file content using the file's path that will be sent to Nuance.
*
* @param path The path to the file.
* @param callback The callback we'd like to call when done loading.
* @deprecated Use sendDictationRequest with the "path" parameter instead.
*/
self.loadFile = function(path, callback){
if(!path){
if(typeof callback === "function"){
callback(false);
}
return;
}
fs.readFile(path, [], function(err, data){
if(!err){
fileContent = data;
dictationHeaders["Content-Length"] = fileContent.length;
}
if(typeof callback === "function"){
callback(!err);
}
});
};
/**
* Sets the instance's file content.
* This function should be used only if you already have the file's content that have been read using the
* fs.readFile function. If you don't have the file's content, please use the loadFile funciton.
*
* @param value - Required - The file's content.
*/
self.setFileContent = function(value){
fileContent = value;
dictationHeaders["Content-Length"] = fileContent.length;
};
/**
* Sets the dictation headers that will be sent to the Nuance's dictation service.
*
* @param headers
*/
self.setDictationHeaders = function(headers){
if(typeof headers !== "object"){
return;
}
dictationHeaders = headers;
};
/**
* Sends a unirest request.
*
* @param url
* @param headers
* @param body
* @param successCallback
* @param errorCallback
*/
function sendUnirestRequest(url, headers, body, successCallback, errorCallback){
unirest.post(url)
.headers(headers)
.send(body)
.end(function(response){
if(response["code"] === 200){
response = response["body"];
if(typeof successCallback === "function"){
successCallback(response);
}
}
else if(typeof errorCallback === "function"){
errorCallback(response);
}
});
}
/**
* Merges 2 associative arrays into one.
*
* @param array1 The array we store the results into.
* @param array2 The array we add results from.
* @returns {*}
*/
function mergeAssociativeArrays(array1, array2){
for(var property in array2) {
if(array2.hasOwnProperty(property)){
array1[property] = array2[property];
}
}
return array1;
}
};
module.exports = Nuance;