tts.min.js
7.3 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
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
/*
author:cyhu(viskey.hu@gmail.com) 2015.4.14
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the distribution.
3. The names of the authors may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
var brower_info = {
"sys": null,
"type": null,
"link": null
};
/* */
var print_log = true;
var websocket_url = 'ws://h5.xf-yun.com/tts.do';
var reconnection = false;
var reconnection_delay = 999999;
var force_new_connection = true;
var buffer_length = 0;
var tts_params_obj = null;
this.node = null;
var tts_session_id = null;
var tts_text_ready = null;
var func_on_tts_process = null;
var func_on_tts_result = null;
var browser_id = new Fingerprint().get();
var is_re_request = false;
var request_synid = null;
var ua = navigator.userAgent.toLowerCase();
if (((/msie/.test(ua) && !/opera/.test(ua)) || ((/msie/.test(ua) || /trident/.test(ua)) && !/opera/.test(ua))))
brower_info.type = 'ie';
else if (ua.match(/firefox\/([\d.]+)/)) {
brower_info.type = 'firefox';
} else if (ua.match(/chrome\/([\d.]+)/))
brower_info.type = 'chrome';
else if (ua.match(/opera.([\d.]+)/))
brower_info.type = 'opera';
else if (ua.match(/version\/([\d.]+).*safari/))
brower_info.type = 'safari';
else
brower_info.type = 'other';
if (!!ua.match(/AppleWebKit.*Mobile.*/) || !!ua.match(/AppleWebKit/))
brower_info.sys = 'mobile';
else if (!!ua.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/))
brower_info.sys = 'ios';
else if (ua.indexOf('Android') > -1 || ua.indexOf('Linux') > -1)
brower_info.sys = 'android';
else if (ua.indexOf('iPhone') > -1 || ua.indexOf('Mac') > -1)
brower_info.sys = 'iPhone';
else if (ua.indexOf('iPad') > -1)
brower_info.sys = 'iPad';
else if (navigator.userAgent.indexOf('MicroMessenger') > -1)
brower_info.sys = 'Weixin';
else
brower_info.sys = 'other';
//if(brower_info.type == 'ie' || ((brower_info.type == 'chrome' || brower_info.type == 'opera') && brower_info.sys != 'other'))
//{
brower_info.link = 'mp3';
//}else if(brower_info.sys == 'Weixin'){
//brower_info.link = 'mp3';
//}
function register_connection_listener() {
// console.log("connected to server ...");
window.socket.onopen = function() {
var json = {
"ver": "1.0",
"sub": "tts",
"cmd": "ssb",
"synid": tts_params_obj.synid,
"msg": "request",
"appid": tts_params_obj.appid,
"appkey": tts_params_obj.appkey,
"data": {
"params": tts_params_obj.params,
"text": tts_text_ready
}
};
var resultJson = JSON.stringify(json);
if (resultJson) {
window.socket.send(JSON.stringify(json));
}
}
window.socket.onclose = function(e) {
// console.log("client disconnect ...");
}
window.socket.onmessage = function(obj) {
var json = strToJson(obj.data);
var cmd = json.cmd;
var data = json.data;
is_re_request = true;
ttsGetAudioStream(data);
}
}
function LOGCAT(str) {
// if (print_log) console.log('[' + getCurrentTime() + '] ' + str);
}
function getCurrentTime() {
var now = new Date();
var year = now.getFullYear();
var month = now.getMonth() + 1;
var day = now.getDate();
var hh = now.getHours();
var mm = now.getMinutes();
var clock = year + "-";
if (month < 10)
clock += "0";
clock += month + "-";
if (day < 10)
clock += "0";
clock += day + " ";
if (hh < 10)
clock += "0";
clock += hh + ":";
if (mm < 10)
clock += '0';
clock += mm;
return (clock);
}
function sendRequestMessage() {
// console.log("connected to server ...");
var json = {
"ver": "1.0",
"sub": "tts",
"cmd": "ssb",
"synid": tts_params_obj.synid,
"msg": "request",
"appid": tts_params_obj.appid,
"appkey": tts_params_obj.appkey,
"data": {
"params": tts_params_obj.params,
"text": tts_text_ready
}
};
var reseltJson = JSON.stringify(json);
if (reseltJson) {
window.socket.send(JSON.stringify(json));
}
};
function initTtsParams(cfg) {
if (cfg.reconnection != undefined && cfg.reconnection != null) reconnection = cfg.reconnection;
if (cfg.reconnection_delay != undefined && cfg.reconnection_delay != null) reconnection_delay = cfg.reconnectionDelay;
}
function IFlyTtsSession(cfg) {
initTtsParams(cfg);
/*
* start a speech synthesize session, schedule: start->send text -> return audio url
* ->on end -> next session
* @params parameters for synthesize
* @content content for synthesize
* @funcOnResult on volume listener
* @funcOnProcess on process
*/
this.start = function(params, content, funcOnResult, funcOnProcess) {
iflytek_tts_start(params, content, funcOnResult, funcOnProcess);
}
/*
* stop synthsize session
*/
this.stop = function() {
}
/*
* cancel synthsize session
*/
this.cancel = function() {
iflytek_tts_cancel();
}
}
function iflytek_tts_start(params, content, funcOnResult, funcOnProcess) {
if (arguments.length < 3) {
LOGCAT("INPUT PARAMETERS ERROR");
return -1;
}
if (funcOnProcess != undefined) func_on_tts_process = funcOnProcess;
func_on_tts_result = funcOnResult;
tts_params_obj = params;
tts_text_ready = content;
if (window.socket == null || window.socket == undefined || window.socket.readyState != 1) {
// console.log(websocket_url);
window.socket = new WebSocket(websocket_url);
}
register_connection_listener();
if (is_re_request) {
sendRequestMessage();
};
}
function iflytek_tts_cancel() {
is_re_request = true;
LOGCAT("----------tts cancel----------");
}
/*
* request session end
*/
function ttsSessionEnd(wait_time) {
is_re_request = true;
LOGCAT("----------session end----------");
}
function strToJson(str) {
var json = eval('(' + str + ')');
return json;
}
function ttsGetAudioStream(data) {
if (data.audioKey !== null && data.audioKey !== "") {
func_on_tts_result(0, {
"audio_url": data.audioKey,
"duration": ""
});
//console.log("data.audioKey is null", data.audioKey);
} else {
//console.log("data.audioKey is null");
}
ttsSessionEnd(0);
}