UNPKG

@comsultia/speech2text

Version:

Google Speech to Text plugin using websockets to communicate with backend

1 lines 6.52 kB
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define("speech2text",[],t):"object"==typeof exports?exports.speech2text=t():e.speech2text=t()}(window,function(){return function(e){var t={};function o(n){if(t[n])return t[n].exports;var i=t[n]={i:n,l:!1,exports:{}};return e[n].call(i.exports,i,i.exports,o),i.l=!0,i.exports}return o.m=e,o.c=t,o.d=function(e,t,n){o.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:n})},o.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},o.t=function(e,t){if(1&t&&(e=o(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var n=Object.create(null);if(o.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var i in e)o.d(n,i,function(t){return e[t]}.bind(null,i));return n},o.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return o.d(t,"a",t),t},o.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},o.p="",o(o.s=0)}([function(e,t,o){"use strict";function n(e,t,o){return t in e?Object.defineProperty(e,t,{value:o,enumerable:!0,configurable:!0,writable:!0}):e[t]=o,e}o.r(t);const i=console.log,s="color: #018a01",r="color: #ff1e1e",c="color: #f98a00",l="color: #2177f9";function u(e,t,o){return t in e?Object.defineProperty(e,t,{value:o,enumerable:!0,configurable:!0,writable:!0}):e[t]=o,e}var a=class{constructor(e){this.config=e,u(this,"debug",void 0),u(this,"socket",null),u(this,"elementStart",void 0),u(this,"elementStop",void 0),u(this,"elementResult",void 0),u(this,"audioContext",null),u(this,"context",null),u(this,"processor",null),u(this,"bufferSize",1024),u(this,"globalStream",null),u(this,"input",null),u(this,"recognizeStream",null),u(this,"timer",0),u(this,"defaultTimeout",4e3),u(this,"init",()=>{this.socket=new WebSocket(this.config.url),this.openWebSocket()}),u(this,"initRecording",()=>{const e=this;this.audioContext=window.AudioContext||window.webkitAudioContext,this.context=new this.audioContext,this.context.createJavaScriptNode?this.processor=this.context.createJavaScriptNode(this.bufferSize,1,1):this.context.createScriptProcessor?this.processor=this.context.createScriptProcessor(this.bufferSize,1,1):this.config.onNotSupported&&this.config.onNotSupported(),this.processor.connect(this.context.destination),this.context.resume(),navigator.mediaDevices.getUserMedia({audio:!0,video:!1}).then(t=>{e.globalStream=t,e.elementStart.forEach(e=>{e.setAttribute("disabled","disabled")}),e.elementStop.forEach(e=>{e.removeAttribute("disabled")}),null!=e.context?(e.input=e.context.createMediaStreamSource(t),e.input.connect(e.processor),e.config.onStart&&e.config.onStart(),"suspended"===e.context.state&&this.context.resume(),e.processor.onaudioprocess=(t=>{e.socket.readyState===WebSocket.OPEN&&e.microphoneProcess(t)})):e.globalStream.getTracks()[0].stop()},e=>{this.config.onBlock&&(this.config.onBlock(),this.closeWebSocket())}),this.messageWebSocket()}),u(this,"openWebSocket",()=>{const e=this;this.config.onConnecting&&this.config.onConnecting(),this.socket.onopen=(()=>{e.debug.success("WebSocket open"),this.config.onConnection&&this.config.onConnection(),e.initRecording()}),this.messageWebSocket()}),u(this,"closeWebSocket",()=>{const e=this;this.elementStart.forEach(e=>{e.removeAttribute("disabled")}),this.elementStop.forEach(e=>{e.setAttribute("disabled","disabled")}),null!=this.socket&&this.socket.readyState===WebSocket.OPEN&&(clearTimeout(e.timer),this.debug.log("Closing..."),this.socket.close(),this.socket.onclose=(()=>{e.debug.result("WebSocket closed"),e.config.onEnd&&e.config.onEnd()})),null!=this.globalStream&&null!=this.globalStream&&void 0!==this.globalStream&&this.globalStream.getTracks()[0].stop(),null!=this.input&&null!=this.input&&void 0!==this.input&&this.input.disconnect(this.processor),null!=this.processor&&null!=this.processor&&void 0!==this.processor&&this.processor.disconnect(this.context.destination),null!=this.context&&null!=this.context&&void 0!==this.context&&this.context.close().then(()=>{e.input=null,e.processor=null,e.context=null,e.audioContext=null})}),u(this,"messageWebSocket",()=>{const e=this;this.socket.onmessage=(t=>{clearTimeout(e.timer),e.timer=setTimeout(()=>e.closeWebSocket(),e.defaultTimeout),"error"===t.data?(e.debug.warn("Error from Google Speech API:"),e.debug.object(t)):(e.debug.info("WebSocket is receiving message..."),e.debug.log(`message: ${t.data}`),this.elementResult.forEach(e=>{e.innerHTML=`<span>${t.data}</span>`}))}),this.socket.onerror=(t=>{e.debug.warn("WebSocket error:"),e.debug.object(t),e.config.onError&&e.config.onError()})}),u(this,"microphoneProcess",e=>{let t=e.inputBuffer.getChannelData(0),o=this.downsampleBuffer(t,44100,16e3);this.socket.send(o)}),u(this,"downsampleBuffer",(e,t,o)=>{if(o==t)return e;o>t&&this.debug.warn("downsampling rate show be smaller than original sample rate");let n=t/o,i=Math.round(e.length/n),s=new Int16Array(i),r=0,c=0;for(;r<s.length;){let t=Math.round((r+1)*n),o=0,i=0;for(let n=c;n<t&&n<e.length;n++)o+=e[n],i++;s[r]=32767*Math.min(1,o/i),r++,c=t}return s.buffer}),this.debug=new class{constructor(e){this.enabled=e,n(this,"log",e=>{this.enabled&&i(`[WS] ${e}`)}),n(this,"success",e=>{this.enabled&&i(`%c[WS] ${e}`,s)}),n(this,"info",e=>{this.enabled&&i(`%c[WS] ${e}`,c)}),n(this,"warn",e=>{this.enabled&&i(`%c[WS] ${e}`,r)}),n(this,"result",e=>{this.enabled&&i(`%c[WS] ${e}`,l)}),n(this,"object",e=>{this.enabled&&i(e)})}}(this.config.debug),null!=document.querySelector(this.config.elementStart)?null!=document.querySelector(this.config.elementStop)?null!=document.querySelector(this.config.elementResult)?(this.elementStart=document.querySelectorAll(this.config.elementStart),this.elementStop=document.querySelectorAll(this.config.elementStop),this.elementResult=document.querySelectorAll(this.config.elementResult),this.elementStart.forEach(e=>{e.addEventListener("click",()=>this.init())}),this.elementStop.forEach(e=>{e.addEventListener("click",()=>this.closeWebSocket())}),document.addEventListener("keydown",()=>this.closeWebSocket()),this.config.defaultTimeout&&(this.defaultTimeout=this.config.defaultTimeout)):this.debug.warn("element Result not found"):this.debug.warn("element Stop not found"):this.debug.warn("element Start not found")}};function h(e){return new a(e)}o.d(t,"init",function(){return h})}])});