forked from MuGuiLin/VoiceDictation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvoice.js
384 lines (359 loc) · 15.3 KB
/
voice.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
; (function (window, Voice) {
"use strict";
if (typeof define === 'function' && define.amd) {
define(Voice());
} else if (typeof exports === 'object') {
module.exports = Voice();
} else {
window.Voice = Voice();
};
}(typeof window !== "undefined" ? window : this, () => {
"use strict";
return class IatRecorder {
createWorkerFromExternalURL(url, callback) {
GM_xmlhttpRequest({
method: 'GET',
url: url,
onload: function(response) {
var script, dataURL, worker = null;
if (response.status === 200) {
script = response.responseText;
dataURL = 'data:text/javascript;base64,' + btoa(script);
worker = new unsafeWindow.Worker(dataURL);
}
callback(worker);
},
onerror: function() {
callback(null);
}
});
}
constructor(opts = {}) {
// 服务接口认证信息(语音听写(流式版)WebAPI)
this.appId = opts.appId || '';
this.apiKey = opts.apiKey || '';
this.apiSecret = opts.apiSecret || '';
// 识别监听方法
this.onTextChange = opts.onTextChange || Function();
this.onWillStatusChange = opts.onWillStatusChange || Function();
// 方言/语种
this.status = 'null'
this.language = opts.language || 'zh_cn'
this.accent = opts.accent || 'mandarin';
// 流媒体
this.streamRef = [];
// 记录音频数据
this.audioData = [];
// 记录听写结果
this.resultText = '';
// wpgs下的听写结果需要中间状态辅助记录
this.resultTextTemp = '';
// 音频数据多线程
this.init();
};
// WebSocket请求地址鉴权
getWebSocketUrl() {
return new Promise((resolve, reject) => {
// 请求地址根据语种不同变化
try {
var url = 'wss://iat-api.xfyun.cn/v2/iat'
var host = 'iat-api.xfyun.cn'
var date = new Date().toGMTString()
var algorithm = 'hmac-sha256'
var headers = 'host date request-line'
var signatureOrigin = `host: ${host}\ndate: ${date}\nGET /v2/iat HTTP/1.1`
var signatureSha = CryptoJS.HmacSHA256(signatureOrigin, this.apiSecret)
var signature = CryptoJS.enc.Base64.stringify(signatureSha)
var authorizationOrigin = `api_key="${this.apiKey}", algorithm="${algorithm}", headers="${headers}", signature="${signature}"`
var authorization = btoa(authorizationOrigin);
resolve(`${url}?authorization=${authorization}&date=${date}&host=${host}`);
} catch (error) {
reject(error);
};
});
};
// 操作初始化
init() {
var self = this;
try {
if (!self.appId || !self.apiKey || !self.apiSecret) {
alert('请正确配置【迅飞语音听写(流式版)WebAPI】服务接口认证信息!');
} else {
this.createWorkerFromExternalURL('https://cdn.jsdelivr.net/gh/hou000123/VoiceDictation/js/transcode.worker.js', function(worker) {
self.webWorker = worker// logic goes here
self.webWorker.onmessage = function (event) {
self.audioData.push(...event.data);
};
});
}
} catch (error) {
alert('对不起:请在服务器环境下运行!');
console.error('请在服务器如:WAMP、XAMPP、Phpstudy、http-server、WebServer等环境中运行!', error);
};
console.log("%c ❤️使用说明:https://blog.csdn.net/muguli2008/article/details/106734113", "font-size:32px; color:blue; font-weight: bold;");
};
// 修改录音听写状态
setStatus(status) {
this.onWillStatusChange && this.status !== status && this.onWillStatusChange(this.status, status);
this.status = status;
};
// 设置识别结果内容
setResultText({ resultText, resultTextTemp } = {}) {
this.onTextChange && this.onTextChange(resultTextTemp || resultText || '');
resultText !== undefined && (this.resultText = resultText);
resultTextTemp !== undefined && (this.resultTextTemp = resultTextTemp);
};
// 修改听写参数
setParams({ language, accent } = {}) {
language && (this.language = language)
accent && (this.accent = accent)
};
// 对处理后的音频数据进行base64编码,
toBase64(buffer) {
var binary = '';
var bytes = new Uint8Array(buffer);
var len = bytes.byteLength;
for (var i = 0; i < len; i++) {
binary += String.fromCharCode(bytes[i]);
}
return window.btoa(binary);
};
// 连接WebSocket
connectWebSocket() {
return this.getWebSocketUrl().then(url => {
let iatWS;
if ('WebSocket' in window) {
iatWS = new WebSocket(url);
} else if ('MozWebSocket' in window) {
iatWS = new MozWebSocket(url);
} else {
alert('浏览器不支持WebSocket!');
return false;
}
this.webSocket = iatWS;
this.setStatus('init');
iatWS.onopen = e => {
this.setStatus('ing');
// 重新开始录音
setTimeout(() => {
this.webSocketSend();
}, 500);
};
iatWS.onmessage = e => {
this.webSocketRes(e.data);
};
iatWS.onerror = e => {
this.recorderStop(e);
};
iatWS.onclose = e => {
this.recorderStop(e);
};
})
};
// 初始化浏览器录音
recorderInit() {
// 创建音频环境
try {
this.audioContext = this.audioContext ? this.audioContext : new (window.AudioContext || window.webkitAudioContext)();
this.audioContext.resume();
if (!this.audioContext) {
alert('浏览器不支持webAudioApi相关接口');
return false;
}
} catch (e) {
if (!this.audioContext) {
alert('浏览器不支持webAudioApi相关接口');
return false;
}
};
// 获取浏览器录音权限成功时回调
let getMediaSuccess = _ => {
// 创建一个用于通过JavaScript直接处理音频
this.scriptProcessor = this.audioContext.createScriptProcessor(0, 1, 1);
this.scriptProcessor.onaudioprocess = e => {
if (this.status === 'ing') {
// 多线程音频数据处理
try {
this.webWorker.postMessage(e.inputBuffer.getChannelData(0));
} catch (error) { }
}
}
// 创建一个新的MediaStreamAudioSourceNode 对象,使来自MediaStream的音频可以被播放和操作
this.mediaSource = this.audioContext.createMediaStreamSource(this.streamRef);
this.mediaSource.connect(this.scriptProcessor);
this.scriptProcessor.connect(this.audioContext.destination);
this.connectWebSocket();
};
// 获取浏览器录音权限失败时回调
let getMediaFail = (e) => {
alert('对不起:录音权限获取失败!');
this.audioContext && this.audioContext.close();
this.audioContext = undefined;
// 关闭websocket
if (this.webSocket && this.webSocket.readyState === 1) {
this.webSocket.close();
}
};
navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia;
// 获取浏览器录音权限
if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia && navigator.mediaDevices.getDisplayMedia ) {
navigator.mediaDevices.getDisplayMedia({
video: true ,audio: true
}).then(stream => {
this.streamRef = stream;
getMediaSuccess();
}).catch(e => {
getMediaFail(e);
})
} else if (navigator.getUserMedia) {
navigator.getUserMedia({
audio: true
}, (stream) => {
this.streamRef = stream;
getMediaSuccess();
}, function (e) {
getMediaFail(e);
})
} else {
if (navigator.userAgent.toLowerCase().match(/chrome/) && location.origin.indexOf('https://') < 0) {
console.error('获取浏览器录音功能,因安全性问题,需要在localhost 或 127.0.0.1 或 https 下才能获取权限!');
} else {
alert('对不起:未识别到录音设备!');
}
this.audioContext && this.audioContext.close();
return false;
};
};
// 向webSocket发送数据(音频二进制数据经过Base64处理)
webSocketSend() {
if (this.webSocket.readyState !== 1) {
return false;
}
// 音频数据
let audioData = this.audioData.splice(0, 1280);
var params = {
common: {
app_id: this.appId,
},
business: {
language: this.language, //小语种可在控制台--语音听写(流式)--方言/语种处添加试用
domain: 'iat',
accent: this.accent, //中文方言可在控制台--语音听写(流式)--方言/语种处添加试用
vad_eos: 5000,
dwa: 'wpgs' //为使该功能生效,需到控制台开通动态修正功能(该功能免费)
},
data: {
status: 0,
format: 'audio/L16;rate=16000',
encoding: 'raw',
audio: this.toBase64(audioData)
}
};
// 发送数据
this.webSocket.send(JSON.stringify(params));
this.handlerInterval = setInterval(() => {
// websocket未连接
if (this.webSocket.readyState !== 1) {
this.audioData = [];
clearInterval(this.handlerInterval);
return false;
};
if (this.audioData.length === 0) {
if (this.status === 'end') {
this.webSocket.send(
JSON.stringify({
data: {
status: 2,
format: 'audio/L16;rate=16000',
encoding: 'raw',
audio: ''
}
})
);
this.audioData = [];
clearInterval(this.handlerInterval);
}
return false;
};
// 中间帧
this.webSocket.send(
JSON.stringify({
data: {
status: 1,
format: 'audio/L16;rate=16000',
encoding: 'raw',
audio: this.toBase64(this.audioData.splice(0, 1280))
}
})
);
}, 40);
};
// 识别结束 webSocket返回数据
webSocketRes(resultData) {
let jsonData = JSON.parse(resultData);
if (jsonData.data && jsonData.data.result) {
let data = jsonData.data.result;
let str = '';
let ws = data.ws;
for (let i = 0; i < ws.length; i++) {
str = str + ws[i].cw[0].w;
}
// 开启wpgs会有此字段(前提:在控制台开通动态修正功能)
// 取值为 "apd"时表示该片结果是追加到前面的最终结果;取值为"rpl" 时表示替换前面的部分结果,替换范围为rg字段
if (data.pgs) {
if (data.pgs === 'apd') {
// 将resultTextTemp同步给resultText
this.setResultText({
resultText: this.resultTextTemp
});
}
// 将结果存储在resultTextTemp中
this.setResultText({
resultTextTemp: this.resultText + str
});
} else {
this.setResultText({
resultText: this.resultText + str
});
}
}
if (jsonData.code === 0 && jsonData.data.status === 2) {
this.webSocket.close();
}
if (jsonData.code !== 0) {
this.webSocket.close();
}
};
// 启动录音
recorderStart() {
if (!this.audioContext) {
this.recorderInit();
} else {
this.audioContext.resume();
this.connectWebSocket();
}
};
// 停止录音
recorderStop() {
if (!(/Safari/.test(navigator.userAgent) && !/Chrome/.test(navigator.userAgen))) {
// safari下suspend后再次resume录音内容将是空白,设置safari下不做suspend
this.audioContext && this.audioContext.suspend();
}
this.setStatus('end');
try {
// this.streamRef.getTracks().map(track => track.stop()) || his.streamRef.getAudioTracks()[0].stop();
} catch (error) {
console.error('暂停失败!');
}
};
// 开始
start() {
this.recorderStart();
this.setResultText({ resultText: '', resultTextTemp: '' });
};
// 停止
stop() {
this.recorderStop();
};
};
}));