123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447 |
- import CryptoJS from "crypto-js";
- let transWorker = new Worker(new URL('./transcode.worker.js', import.meta.url));
- const APPID = "8c1c8431";
- const API_SECRET = "ZTVhNDAyZWIyYTNlODFmNTdmODI3MGM3";
- const API_KEY = "ebc9b65a1f14b924b0e95730aa52f9ea";
- function getWebSocketUrl() {
- return new Promise((resolve) => {
-
- var url = "wss://iat-api.xfyun.cn/v2/iat";
- var host = "iat-api.xfyun.cn";
- var apiKey = API_KEY;
- var apiSecret = API_SECRET;
- 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, apiSecret);
- var signature = CryptoJS.enc.Base64.stringify(signatureSha);
- var authorizationOrigin = `api_key="${apiKey}", algorithm="${algorithm}", headers="${headers}", signature="${signature}"`;
- var authorization = btoa(authorizationOrigin);
- url = `${url}?authorization=${authorization}&date=${date}&host=${host}`;
- resolve(url);
- });
- }
- export class IatRecorder {
- constructor({ language, accent, appId } = {}) {
- let self = this;
- this.status = "null";
- this.language = language || "zh_cn";
- this.accent = accent || "mandarin";
- this.appId = appId || APPID;
-
- this.audioData = [];
-
- this.resultText = "";
-
- this.resultTextTemp = "";
- transWorker.onmessage = function (event) {
- self.audioData.push(...event.data);
- };
- }
-
-
-
-
-
- setStatus(status) {
- this.onWillStatusChange &&
- this.status !== status &&
- this.onWillStatusChange(this.status, status, this.audioData);
- 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);
- }
-
- connectWebSocket() {
- return 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;
- }
- this.webSocket = iatWS;
- this.setStatus("init");
- iatWS.onopen = (e) => {
-
- this.setStatus("ing");
-
- setTimeout(() => {
- this.webSocketSend();
- }, 20);
- };
- iatWS.onmessage = (e) => {
-
- this.result(e.data);
- };
- iatWS.onerror = () => {
- this.recorderStop();
- };
- iatWS.onclose = () => {
- this.recorderStop();
- };
- });
- }
-
- recorderInit() {
- navigator.getUserMedia =
- navigator.getUserMedia ||
- navigator.webkitGetUserMedia ||
- navigator.mozGetUserMedia ||
- navigator.msGetUserMedia;
-
- try {
- this.audioContext = new (window.AudioContext ||
- window.webkitAudioContext)();
- this.audioContext.resume();
- if (!this.audioContext) {
- alert("浏览器不支持webAudioApi相关接口");
- return;
- }
- } catch (e) {
- if (!this.audioContext) {
- alert("浏览器不支持webAudioApi相关接口");
- return;
- }
- }
-
- if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
- navigator.mediaDevices
- .getUserMedia({
- audio: true,
- video: false,
- })
- .then((stream) => {
- getMediaSuccess(stream);
- })
- .catch((e) => {
- getMediaFail(e);
- });
- } else if (navigator.getUserMedia) {
- navigator.getUserMedia(
- {
- audio: true,
- video: false,
- },
- (stream) => {
- getMediaSuccess(stream);
- },
- function (e) {
- getMediaFail(e);
- }
- );
- } else {
- if (
- navigator.userAgent.toLowerCase().match(/chrome/) &&
- location.origin.indexOf("https://") < 0
- ) {
- alert(
- "chrome下获取浏览器录音功能,因为安全性问题,需要在localhost或127.0.0.1或https下才能获取权限"
- );
- } else {
- alert("无法获取浏览器录音功能,请升级浏览器或使用chrome");
- }
- this.audioContext && this.audioContext.close();
- return;
- }
-
- let getMediaSuccess = (stream) => {
-
-
- this.scriptProcessor = this.audioContext.createScriptProcessor(0, 1, 1);
- this.scriptProcessor.onaudioprocess = (e) => {
-
-
- if (this.status === "ing") {
- transWorker.postMessage(e.inputBuffer.getChannelData(0));
- }
- };
-
- this.mediaSource = this.audioContext.createMediaStreamSource(stream);
-
- 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;
-
- if (this.webSocket && this.webSocket.readyState === 1) {
- 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)
- )
- ) {
- this.audioContext && this.audioContext.suspend();
- }
-
- this.setStatus("end");
- }
-
-
-
-
-
-
- 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);
- }
-
- webSocketSend() {
- if (this.webSocket.readyState !== 1) {
- return;
- }
- 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: 500,
- 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(() => {
-
- if (this.webSocket.readyState !== 1) {
- this.audioData = [];
- clearInterval(this.handlerInterval);
- return;
- }
- 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;
- }
- audioData = this.audioData.splice(0, 1280);
-
- this.webSocket.send(
- JSON.stringify({
- data: {
- status: 1,
- format: "audio/L16;rate=16000",
- encoding: "raw",
- audio: this.toBase64(audioData),
- },
- })
- );
- let t = (new Date).valueOf()
- if (t % 11 === 0) {
-
- }
- }, 40);
- }
- result(resultData) {
-
- let jsonData = JSON.parse(resultData);
- if (jsonData.data && jsonData.data.result) {
- let data = jsonData.data.result;
- let str = "";
- let resultStr = "";
- let ws = data.ws;
- for (let i = 0; i < ws.length; i++) {
- str = str + ws[i].cw[0].w;
- }
-
-
-
- if (data.pgs) {
- if (data.pgs === "apd") {
-
- this.setResultText({
- resultText: this.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();
-
- }
- }
- clearResult() {
- this.setResultText({ resultText: "", resultTextTemp: "" });
- }
- start() {
- this.clearResult();
- this.recorderStart();
- }
- stop() {
- this.webSocket && this.webSocket.close();
- this.recorderStop();
- }
- reStart() {
- this.stop()
- setTimeout(() => {
- this.start()
- }, 20)
- }
- }
|