@tilde-nlp/asr-api-client
Advanced tools
Comparing version 4.0.6 to 4.0.7
@@ -1,1 +0,1 @@ | ||
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports["asr-api-client"]=t():e["asr-api-client"]=t()}(self,(()=>(()=>{"use strict";var e={156:function(e,t,i){var s=this&&this.__createBinding||(Object.create?function(e,t,i,s){void 0===s&&(s=i);var o=Object.getOwnPropertyDescriptor(t,i);o&&!("get"in o?!t.__esModule:o.writable||o.configurable)||(o={enumerable:!0,get:function(){return t[i]}}),Object.defineProperty(e,s,o)}:function(e,t,i,s){void 0===s&&(s=i),e[s]=t[i]}),o=this&&this.__exportStar||function(e,t){for(var i in e)"default"===i||Object.prototype.hasOwnProperty.call(t,i)||s(t,e,i)};Object.defineProperty(t,"__esModule",{value:!0}),o(i(632),t),o(i(388),t)},447:(e,t,i)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.AsrClient=void 0;const s=i(712),o=i(747),r=i(745),n=i(910),a=i(761);t.AsrClient=class{get audioVisualizer(){return this._audioVisualizer}get audioContext(){return this._audioContext}get scriptProcessorNode(){return this._scriptProcessorNode}get stream(){return this._stream}get inputStream(){return this._inputStream}get isRecording(){return this._isRecording}get sampleRate(){var e;return null===(e=this.audioContext)||void 0===e?void 0:e.sampleRate}get volume(){return this._volume}constructor(e){var t,i,s,o;this.volumeFftSize=256,this._audioContext=new AudioContext,this._isRecording=!1,this._volume=0,this.vocabulary=e.vocabulary,this.visualizerConfigBase=e.visualizerConfig,this.timeSlice=e.timeSlice>0?e.timeSlice:200,this.eosTimeoutMs=null!==(t=e.eosTimeoutMs)&&void 0!==t?t:200,this.isDebug=null!==(i=e.debug)&&void 0!==i&&i,this.deviceId=e.deviceId,this.websocketProtocol=null!==(s=e.websocketProtocol)&&void 0!==s?s:"soap",this.shouldTranscribeRecorded=!e.recordOnly,this.replaceSampleRateInUrl=null===(o=e.replaceSampleRateInUrl)||void 0===o||o,this.url=e.url,this.onResult=e.onResult,this.onDataAvailable=e.onDataAvailable,this.onMessage=e.onMessage,this.onError=e.onError,this.onStopped=e.onStopped,this.onRecordingStartStop=e.onRecordingStartStop,this.getToken=e.getToken}async beginVoiceRecognition(){this.speechWebSocket||(await this.setUpMic(),this.shouldTranscribeRecorded?(await this.setupSpeechWebSocket(),this.startMediaRecorder()):this.startMediaRecorder())}listenToWebSocket(){this.setupSpeechWebSocket()}endVoiceRecognition(e=!1){this.stopRecording(),e&&this.closeWebSocket()}pause(){var e;this.isRecording&&(this.setIsRecording(!1),null===(e=this.mediaRecorder)||void 0===e||e.pause(),this.sendEndOfStream())}resume(){var e;this.mediaRecorder?(this.setupSpeechWebSocket(),null===(e=this.mediaRecorder)||void 0===e||e.resume(),this.setIsRecording(!0)):alert("No media recorder found, can't resume")}startMediaRecorder(){var e;null===(e=this.mediaRecorder)||void 0===e||e.start(this.timeSlice),this.setIsRecording(!0)}async setUpMic(){var e;try{if(this._stream=await navigator.mediaDevices.getUserMedia({audio:{deviceId:{ideal:this.deviceId}}}),this._audioContext=new AudioContext,null===(e=this.visualizerConfigBase)||void 0===e?void 0:e.visualizerId){const e={audioContext:this._audioContext,stream:this.stream,visualizerId:this.visualizerConfigBase.visualizerId,strokeStyle:this.visualizerConfigBase.strokeStyle,lineWidth:this.visualizerConfigBase.lineWidth,visualizationType:this.visualizerConfigBase.visualizationType};this._audioVisualizer=new s.AudioVisualizer(e)}this._inputStream=this.audioContext.createMediaStreamSource(this.stream),this.createVolumeAnalayzer(),this.connectAudioProcessor(),this.mediaRecorder=new MediaRecorder(this.stream,{mimeType:"audio/webm"}),this.onDataAvailable&&(this.mediaRecorder.ondataavailable=e=>this.onDataAvailable(e))}catch(e){this.onError&&this.onError(e)}}stopRecording(){var e,t;this.debug("Stopping recording"),null===(e=this.mediaRecorder)||void 0===e||e.stop(),this.setIsRecording(!1),null===(t=this._audioVisualizer)||void 0===t||t.stop(),this._audioVisualizer=void 0,this._stream=void 0,this.debug("Recording stopped"),this.volumeIntervalId&&clearInterval(this.volumeIntervalId),this.sendEndOfStream(!0)}async setupSpeechWebSocket(){if(this.speechWebSocket&&this.speechWebSocket.readyState===this.speechWebSocket.OPEN)return void this.debug("Websocket is already opened, not creating new one.");const e=await this.getProtocolsWithToken();return new Promise((t=>{const i=this.replaceParamsInUrl(this.url);this.speechWebSocket=e?new WebSocket(`${i}`,e):new WebSocket(`${i}`),this.speechWebSocket.binaryType="arraybuffer";const s={"enable-final-postprocess":["numbers","punct"]};this.vocabulary&&(s["expand-vocab"]=this.vocabulary),this.speechWebSocket.onopen=async()=>{const e=JSON.stringify(s);this.speechWebSocket.send(e),t()},this.speechWebSocket.onmessage=e=>{let t;try{switch(t=JSON.parse(e.data),t.status){case r.RecognitionStatus.Success:t.result&&this.handleSpeechRecognitionResult(t.result);break;case r.RecognitionStatus.StreamIssue:case r.RecognitionStatus.Aborted:case r.RecognitionStatus.Busy:this.handleError();break;default:this.onMessage&&this.onMessage(t.status)}}catch(t){this.handleSpeechRecognitionResult(e.data)}this.EOSsent&&(null==t?void 0:t.adaptation_state)&&(this.debug("EOS message is sent and received message with adaptation_state property, closing ws"),this.closeWebSocket())},this.speechWebSocket.onclose=e=>{this.debug("ws on close callback"),this.speechWebSocket=void 0,this.onStopped&&this.onStopped()}}))}async getProtocolsWithToken(){if(this.getToken){const e=await this.getToken();return[this.websocketProtocol,e]}}handleError(){this.speechWebSocket.close(3e3,"Server issue"),this.beginVoiceRecognition()}handleSpeechRecognitionResult(e){this.onResult&&this.onResult(e)}setIsRecording(e){this._isRecording!==e&&(this.debug("changing isRecording",e),this._isRecording=e,this.onRecordingStartStop&&this.onRecordingStartStop(this.isRecording,this))}closeWebSocket(){var e;this.debug("Closing web socket"),null===(e=this.speechWebSocket)||void 0===e||e.close(),this.EOSsent=!1,this.cleanupAudioResources(),this.closeAudioContext()}sendEndOfStream(e=!1){const t=()=>{this.debug("sending EOS"),this.sendMessageIfSocketOpen(a.END_OF_STREAM_MESSAGE),this.EOSsent=e,this.debug("EOS sent")};if(this.eosTimeoutMs)return this.debug(`Waiting for EOS message timeout: ${this.eosTimeoutMs}`),void setTimeout((()=>{this.debug("Entered EOS timeout callback"),t()}),this.eosTimeoutMs);t()}sendMessageIfSocketOpen(e){this.speechWebSocket&&this.speechWebSocket.readyState===this.speechWebSocket.OPEN&&this.speechWebSocket.send(e)}debug(e,...t){this.isDebug&&console.debug(e,t)}createVolumeAnalayzer(){this._volume=0,this.volumeAnalyser=this.audioContext.createAnalyser(),this.volumeAnalyser.fftSize=this.volumeFftSize,this.volumeAnalyser.smoothingTimeConstant=.1,this.inputStream.connect(this.volumeAnalyser),this.volumeIntervalId=setInterval((()=>{this.updateVolume()}),100)}updateVolume(){const e=this.volumeAnalyser.frequencyBinCount,t=new Uint8Array(e);this.volumeAnalyser.getByteFrequencyData(t);const i=t.reduce(((e,t)=>e+t),0)/t.length;this._volume=i/this.volumeFftSize*100}connectAudioProcessor(){if(this.shouldTranscribeRecorded){const e=e=>{this.isRecording&&this.sendMessageIfSocketOpen(n.AudioProcessorHelper.audioProcessingEventToPcmChunk(e))};this._scriptProcessorNode=n.AudioProcessorHelper.createAudioProcessor(this.audioContext,this.inputStream,e)}}closeAudioContext(){this._audioContext&&(this._audioContext.close(),this._audioContext=void 0)}cleanupAudioResources(){this._scriptProcessorNode&&(this._scriptProcessorNode.disconnect(),this._scriptProcessorNode=null),this.volumeAnalyser&&this.volumeAnalyser.disconnect(),this._inputStream&&(this._inputStream.disconnect(),this._inputStream=null)}replaceParamsInUrl(e){return this.replaceSampleRateInUrl&&(e=e.replace(o.SAMPLE_RATE_PLACEHOLDER,this.sampleRate.toString())),e}}},712:(e,t,i)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.AudioVisualizer=void 0;const s=i(767);t.AudioVisualizer=class{constructor(e){var t,i;this.audioContext=e.audioContext,this.visualizerId=e.visualizerId,this.stream=e.stream,this.strokeStyle=null!==(t=e.strokeStyle)&&void 0!==t?t:"#811331",this.lineWidth=e.lineWidth,this.visualizationType=null!==(i=e.visualizationType)&&void 0!==i?i:s.VisualizationType.Waves}visualizeAudio(){if(this.stream){if(this.visualizerId){const e=this.audioContext.createMediaStreamSource(this.stream);this.canvas=document.getElementById(this.visualizerId),this.canvasContext=this.canvas.getContext("2d"),this.canvasContext.strokeStyle=this.strokeStyle,this.analyser=this.audioContext.createAnalyser(),this.visualizationType==s.VisualizationType.Linear?this.analyser.fftSize=2048:this.analyser.fftSize=512,this.analyser.smoothingTimeConstant=.95,e.connect(this.analyser)}this.draw()}else console.error("Audio visualizer has no stream! Try creating new visualizer")}stop(){var e;if(this.visualizerId&&!this.canvas){var t=this.canvas.offsetWidth,i=this.canvas.offsetHeight;null===(e=this.canvasContext)||void 0===e||e.clearRect(0,0,t,i)}this.audioVisualizerAnimReqId&&cancelAnimationFrame(this.audioVisualizerAnimReqId),this.stream&&(this.stream.getAudioTracks().forEach((e=>e.stop())),this.analyser=void 0,this.audioContext=void 0,this.stream=void 0,this.audioVisualizerAnimReqId=void 0,this.canvas=null,this.canvasContext=null)}draw(){if(!this.canvas||!this.canvasContext)return;this.audioVisualizerAnimReqId=requestAnimationFrame((()=>{this.draw()}));const e=this.analyser.frequencyBinCount,t=new Uint8Array(e),i=this.canvas.offsetHeight,o=this.canvas.offsetWidth;this.canvas.height=i,this.canvas.width=o,this.canvasContext.clearRect(0,0,o,i),this.visualizationType===s.VisualizationType.Linear?this.drawLinearType(t,this.canvasContext,o,i):this.visualizationType===s.VisualizationType.Waves&&this.drawWavesType(t,this.canvasContext,o,i),this.canvasContext.stroke()}drawLinearType(e,t,i,s){this.analyser.getByteTimeDomainData(e);const o=this.analyser.frequencyBinCount;let r=0;for(let t=0;t<o;t++){let i=e[t]/128*s/2;0===t?this.canvasContext.moveTo(r,i):this.canvasContext.lineTo(r,i),r+=2}}drawWavesType(e,t,i,s){var o;this.analyser.getByteFrequencyData(e);const r=null!==(o=this.lineWidth)&&void 0!==o?o:i/e.length;t.lineWidth=r,t.beginPath();let n=0;for(let i=0;i<e.length;i++){const o=e[i]/255*s;t.imageSmoothingEnabled=!1,t.moveTo(n,s),t.lineTo(n,s-o),n+=r}}}},622:function(e,t,i){var s=this&&this.__createBinding||(Object.create?function(e,t,i,s){void 0===s&&(s=i);var o=Object.getOwnPropertyDescriptor(t,i);o&&!("get"in o?!t.__esModule:o.writable||o.configurable)||(o={enumerable:!0,get:function(){return t[i]}}),Object.defineProperty(e,s,o)}:function(e,t,i,s){void 0===s&&(s=i),e[s]=t[i]}),o=this&&this.__exportStar||function(e,t){for(var i in e)"default"===i||Object.prototype.hasOwnProperty.call(t,i)||s(t,e,i)};Object.defineProperty(t,"__esModule",{value:!0}),o(i(747),t)},747:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.SAMPLE_RATE_PLACEHOLDER=void 0,t.SAMPLE_RATE_PLACEHOLDER="{{sampleRate}}"},745:function(e,t,i){var s=this&&this.__createBinding||(Object.create?function(e,t,i,s){void 0===s&&(s=i);var o=Object.getOwnPropertyDescriptor(t,i);o&&!("get"in o?!t.__esModule:o.writable||o.configurable)||(o={enumerable:!0,get:function(){return t[i]}}),Object.defineProperty(e,s,o)}:function(e,t,i,s){void 0===s&&(s=i),e[s]=t[i]}),o=this&&this.__exportStar||function(e,t){for(var i in e)"default"===i||Object.prototype.hasOwnProperty.call(t,i)||s(t,e,i)};Object.defineProperty(t,"__esModule",{value:!0}),o(i(270),t)},270:(e,t)=>{var i;Object.defineProperty(t,"__esModule",{value:!0}),t.RecognitionStatus=void 0,(i=t.RecognitionStatus||(t.RecognitionStatus={}))[i.Success=0]="Success",i[i.NoSpeech=1]="NoSpeech",i[i.Aborted=2]="Aborted",i[i.StreamIssue=5]="StreamIssue",i[i.LoadLimitReached=9]="LoadLimitReached",i[i.AuthenticationFailed=10]="AuthenticationFailed",i[i.Busy=11]="Busy"},683:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.AudioProcessorHelper=void 0,t.AudioProcessorHelper=class{static createAudioProcessor(e,t,i){const s=e.createScriptProcessor(4096,1,1);return s.onaudioprocess=e=>{i&&i(e)},t.connect(s),s.connect(e.destination),s}static audioProcessingEventToPcmChunk(e){const t=e.inputBuffer.getChannelData(0),i=t.length,s=new Uint8Array(2*i);for(let e=0;e<i;e++){const i=t[e],o=i<0?32768*i:32767*i;s[2*e]=255&o,s[2*e+1]=o>>8&255}return s}}},981:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.sha1Async=void 0,t.sha1Async=async e=>{const t=(new TextEncoder).encode(e),i=await crypto.subtle.digest("SHA-1",t);return Array.from(new Uint8Array(i)).map((e=>e.toString(16).padStart(2,"0"))).join("")}},910:function(e,t,i){var s=this&&this.__createBinding||(Object.create?function(e,t,i,s){void 0===s&&(s=i);var o=Object.getOwnPropertyDescriptor(t,i);o&&!("get"in o?!t.__esModule:o.writable||o.configurable)||(o={enumerable:!0,get:function(){return t[i]}}),Object.defineProperty(e,s,o)}:function(e,t,i,s){void 0===s&&(s=i),e[s]=t[i]}),o=this&&this.__exportStar||function(e,t){for(var i in e)"default"===i||Object.prototype.hasOwnProperty.call(t,i)||s(t,e,i)};Object.defineProperty(t,"__esModule",{value:!0}),o(i(981),t),o(i(683),t)},632:function(e,t,i){var s=this&&this.__createBinding||(Object.create?function(e,t,i,s){void 0===s&&(s=i);var o=Object.getOwnPropertyDescriptor(t,i);o&&!("get"in o?!t.__esModule:o.writable||o.configurable)||(o={enumerable:!0,get:function(){return t[i]}}),Object.defineProperty(e,s,o)}:function(e,t,i,s){void 0===s&&(s=i),e[s]=t[i]}),o=this&&this.__exportStar||function(e,t){for(var i in e)"default"===i||Object.prototype.hasOwnProperty.call(t,i)||s(t,e,i)};Object.defineProperty(t,"__esModule",{value:!0}),o(i(447),t),o(i(545),t),o(i(745),t),o(i(910),t),o(i(761),t),o(i(622),t)},967:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0})},545:function(e,t,i){var s=this&&this.__createBinding||(Object.create?function(e,t,i,s){void 0===s&&(s=i);var o=Object.getOwnPropertyDescriptor(t,i);o&&!("get"in o?!t.__esModule:o.writable||o.configurable)||(o={enumerable:!0,get:function(){return t[i]}}),Object.defineProperty(e,s,o)}:function(e,t,i,s){void 0===s&&(s=i),e[s]=t[i]}),o=this&&this.__exportStar||function(e,t){for(var i in e)"default"===i||Object.prototype.hasOwnProperty.call(t,i)||s(t,e,i)};Object.defineProperty(t,"__esModule",{value:!0}),o(i(967),t),o(i(826),t),o(i(972),t),o(i(362),t),o(i(767),t)},362:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0})},826:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0})},972:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0})},767:(e,t)=>{var i;Object.defineProperty(t,"__esModule",{value:!0}),t.VisualizationType=void 0,(i=t.VisualizationType||(t.VisualizationType={})).Waves="0",i.Linear="1"},210:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.END_OF_STREAM_MESSAGE=void 0,t.END_OF_STREAM_MESSAGE="EOS"},761:function(e,t,i){var s=this&&this.__createBinding||(Object.create?function(e,t,i,s){void 0===s&&(s=i);var o=Object.getOwnPropertyDescriptor(t,i);o&&!("get"in o?!t.__esModule:o.writable||o.configurable)||(o={enumerable:!0,get:function(){return t[i]}}),Object.defineProperty(e,s,o)}:function(e,t,i,s){void 0===s&&(s=i),e[s]=t[i]}),o=this&&this.__exportStar||function(e,t){for(var i in e)"default"===i||Object.prototype.hasOwnProperty.call(t,i)||s(t,e,i)};Object.defineProperty(t,"__esModule",{value:!0}),o(i(210),t)},187:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.AudioDeviceHelper=void 0;class i{static async getListOfMicrophones(e=!1){return e&&await i.askPermissionForAudio(),navigator.mediaDevices.enumerateDevices().then((e=>e.filter((e=>"audiooutput"===e.kind))))}static async askPermissionForAudio(){try{return(await navigator.mediaDevices.getUserMedia({audio:!0})).getTracks().forEach((e=>e.stop())),!0}catch(e){return!1}}}t.AudioDeviceHelper=i},388:function(e,t,i){var s=this&&this.__createBinding||(Object.create?function(e,t,i,s){void 0===s&&(s=i);var o=Object.getOwnPropertyDescriptor(t,i);o&&!("get"in o?!t.__esModule:o.writable||o.configurable)||(o={enumerable:!0,get:function(){return t[i]}}),Object.defineProperty(e,s,o)}:function(e,t,i,s){void 0===s&&(s=i),e[s]=t[i]}),o=this&&this.__exportStar||function(e,t){for(var i in e)"default"===i||Object.prototype.hasOwnProperty.call(t,i)||s(t,e,i)};Object.defineProperty(t,"__esModule",{value:!0}),o(i(436),t),o(i(73),t),o(i(187),t)},436:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0})},73:function(e,t){var i,s,o,r,n=this&&this.__classPrivateFieldGet||function(e,t,i,s){if("a"===i&&!s)throw new TypeError("Private accessor was defined without a getter");if("function"==typeof t?e!==t||!s:!t.has(e))throw new TypeError("Cannot read private member from an object whose class did not declare it");return"m"===i?s:"a"===i?s.call(e):s?s.value:t.get(e)},a=this&&this.__classPrivateFieldSet||function(e,t,i,s,o){if("m"===s)throw new TypeError("Private method is not writable");if("a"===s&&!o)throw new TypeError("Private accessor was defined without a setter");if("function"==typeof t?e!==t||!o:!t.has(e))throw new TypeError("Cannot write private member to an object whose class did not declare it");return"a"===s?o.call(e,i):o?o.value=i:t.set(e,i),i};Object.defineProperty(t,"__esModule",{value:!0}),t.WebsocketHelper=void 0,t.WebsocketHelper=class{get url(){return n(this,s,"f").url}get binaryType(){var e;return null!==(e=n(this,s,"f").binaryType)&&void 0!==e?e:"arraybuffer"}get initialMessage(){return n(this,s,"f").initialMessage}get onMessage(){return n(this,s,"f").onMessage}get onStopped(){return n(this,s,"f").onStopped}get isDebug(){return n(this,s,"f").debug}constructor(e){i.add(this),s.set(this,void 0),o.set(this,void 0),a(this,s,e,"f")}async open(){if(n(this,o,"f"))throw new Error("Websocket is already opened, cannot open new one");const e=await n(this,i,"m",r).call(this);return new Promise((t=>{a(this,o,e?new WebSocket(`${this.url}`,e):new WebSocket(`${this.url}`),"f"),n(this,o,"f").binaryType=this.binaryType,n(this,o,"f").onopen=async()=>{if(this.initialMessage){const e=JSON.stringify(this.initialMessage);n(this,o,"f").send(e)}t()},n(this,o,"f").onmessage=e=>{this.onMessage(e)},n(this,o,"f").onclose=e=>{this.debug("Closing websocket",e),a(this,o,void 0,"f"),this.onStopped&&this.onStopped()}}))}stop(){this.debug("User has called close method. Closing websocket."),n(this,o,"f").close()}debug(e,...t){this.isDebug&&console.debug(e,t)}},s=new WeakMap,o=new WeakMap,i=new WeakSet,r=async function(){return n(this,s,"f").getToken?await n(this,s,"f").getToken():null}}},t={};return function i(s){var o=t[s];if(void 0!==o)return o.exports;var r=t[s]={exports:{}};return e[s].call(r.exports,r,r.exports,i),r.exports}(156)})())); | ||
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports["asr-api-client"]=t():e["asr-api-client"]=t()}(self,(()=>(()=>{"use strict";var e={156:function(e,t,i){var s=this&&this.__createBinding||(Object.create?function(e,t,i,s){void 0===s&&(s=i);var o=Object.getOwnPropertyDescriptor(t,i);o&&!("get"in o?!t.__esModule:o.writable||o.configurable)||(o={enumerable:!0,get:function(){return t[i]}}),Object.defineProperty(e,s,o)}:function(e,t,i,s){void 0===s&&(s=i),e[s]=t[i]}),o=this&&this.__exportStar||function(e,t){for(var i in e)"default"===i||Object.prototype.hasOwnProperty.call(t,i)||s(t,e,i)};Object.defineProperty(t,"__esModule",{value:!0}),o(i(632),t),o(i(388),t)},447:(e,t,i)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.AsrClient=void 0;const s=i(712),o=i(747),r=i(745),n=i(910),a=i(761);t.AsrClient=class{get audioVisualizer(){return this._audioVisualizer}get audioContext(){return this._audioContext}get scriptProcessorNode(){return this._scriptProcessorNode}get stream(){return this._stream}get inputStream(){return this._inputStream}get isRecording(){return this._isRecording}get sampleRate(){var e;return null===(e=this.audioContext)||void 0===e?void 0:e.sampleRate}get volume(){return this._volume}constructor(e){var t,i,s,o;this.volumeFftSize=256,this._audioContext=new AudioContext,this._isRecording=!1,this._volume=0,this.vocabulary=e.vocabulary,this.visualizerConfigBase=e.visualizerConfig,this.timeSlice=e.timeSlice>0?e.timeSlice:200,this.eosTimeoutMs=null!==(t=e.eosTimeoutMs)&&void 0!==t?t:200,this.isDebug=null!==(i=e.debug)&&void 0!==i&&i,this.deviceId=e.deviceId,this.websocketProtocol=null!==(s=e.websocketProtocol)&&void 0!==s?s:"soap",this.shouldTranscribeRecorded=!e.recordOnly,this.replaceSampleRateInUrl=null===(o=e.replaceSampleRateInUrl)||void 0===o||o,this.url=e.url,this.onResult=e.onResult,this.onDataAvailable=e.onDataAvailable,this.onMessage=e.onMessage,this.onError=e.onError,this.onStopped=e.onStopped,this.onRecordingStartStop=e.onRecordingStartStop,this.getToken=e.getToken}async beginVoiceRecognition(){this.speechWebSocket||(await this.setUpMic(),this.shouldTranscribeRecorded?(await this.setupSpeechWebSocket(),this.startMediaRecorder()):this.startMediaRecorder())}listenToWebSocket(){this.setupSpeechWebSocket()}endVoiceRecognition(e=!1){this.stopRecording(),e&&this.closeWebSocket()}pause(){var e;this.isRecording&&(this.setIsRecording(!1),null===(e=this.mediaRecorder)||void 0===e||e.pause(),this.sendEndOfStream())}resume(){var e;this.mediaRecorder?(this.setupSpeechWebSocket(),null===(e=this.mediaRecorder)||void 0===e||e.resume(),this.setIsRecording(!0)):alert("No media recorder found, can't resume")}startMediaRecorder(){var e;null===(e=this.mediaRecorder)||void 0===e||e.start(this.timeSlice),this.setIsRecording(!0)}async setUpMic(){var e;try{if(this._stream=await navigator.mediaDevices.getUserMedia({audio:{deviceId:{ideal:this.deviceId}}}),this._audioContext=new AudioContext,null===(e=this.visualizerConfigBase)||void 0===e?void 0:e.visualizerId){const e={audioContext:this._audioContext,stream:this.stream,visualizerId:this.visualizerConfigBase.visualizerId,strokeStyle:this.visualizerConfigBase.strokeStyle,lineWidth:this.visualizerConfigBase.lineWidth,visualizationType:this.visualizerConfigBase.visualizationType};this._audioVisualizer=new s.AudioVisualizer(e)}this._inputStream=this.audioContext.createMediaStreamSource(this.stream),this.createVolumeAnalayzer(),this.connectAudioProcessor(),this.mediaRecorder=new MediaRecorder(this.stream,{mimeType:"audio/webm"}),this.onDataAvailable&&(this.mediaRecorder.ondataavailable=e=>this.onDataAvailable(e))}catch(e){this.onError&&this.onError(e)}}stopRecording(){var e,t;this.debug("Stopping recording"),null===(e=this.mediaRecorder)||void 0===e||e.stop(),this.setIsRecording(!1),null===(t=this._audioVisualizer)||void 0===t||t.stop(),this._audioVisualizer=void 0,this._stream=void 0,this.debug("Recording stopped"),this.volumeIntervalId&&clearInterval(this.volumeIntervalId),this.sendEndOfStream(!0)}async setupSpeechWebSocket(){if(this.speechWebSocket&&this.speechWebSocket.readyState===this.speechWebSocket.OPEN)return void this.debug("Websocket is already opened, not creating new one.");const e=await this.getProtocolsWithToken();return new Promise((t=>{const i=this.replaceParamsInUrl(this.url);this.speechWebSocket=e?new WebSocket(`${i}`,e):new WebSocket(`${i}`),this.speechWebSocket.binaryType="arraybuffer";const s={"enable-final-postprocess":["numbers","punct"]};this.vocabulary&&(s["expand-vocab"]=this.vocabulary),this.speechWebSocket.onopen=async()=>{const e=JSON.stringify(s);this.speechWebSocket.send(e),t()},this.speechWebSocket.onmessage=e=>{let t;try{switch(t=JSON.parse(e.data),t.status){case r.RecognitionStatus.Success:t.result&&this.handleSpeechRecognitionResult(t.result);break;case r.RecognitionStatus.StreamIssue:case r.RecognitionStatus.Aborted:case r.RecognitionStatus.Busy:this.handleError();break;default:this.onMessage&&this.onMessage(t.status)}}catch(t){this.handleSpeechRecognitionResult(e.data)}this.EOSsent&&(null==t?void 0:t.adaptation_state)&&(this.debug("EOS message is sent and received message with adaptation_state property, closing ws"),this.closeWebSocket())},this.speechWebSocket.onclose=e=>{this.debug("ws on close callback"),this.speechWebSocket=void 0,this.onStopped&&this.onStopped()}}))}async getProtocolsWithToken(){if(this.getToken){const e=await this.getToken();return[this.websocketProtocol,e]}}handleError(){this.speechWebSocket.close(3e3,"Server issue"),this.beginVoiceRecognition()}handleSpeechRecognitionResult(e){this.onResult&&this.onResult(e)}setIsRecording(e){this._isRecording!==e&&(this.debug("changing isRecording",e),this._isRecording=e,this.onRecordingStartStop&&this.onRecordingStartStop(this.isRecording,this))}closeWebSocket(){var e;this.debug("Closing web socket"),null===(e=this.speechWebSocket)||void 0===e||e.close(),this.EOSsent=!1,this.cleanupAudioResources(),this.closeAudioContext()}sendEndOfStream(e=!1){const t=()=>{this.debug("sending EOS"),this.sendMessageIfSocketOpen(a.END_OF_STREAM_MESSAGE),this.EOSsent=e,this.debug("EOS sent")};if(this.eosTimeoutMs)return this.debug(`Waiting for EOS message timeout: ${this.eosTimeoutMs}`),void setTimeout((()=>{this.debug("Entered EOS timeout callback"),t()}),this.eosTimeoutMs);t()}sendMessageIfSocketOpen(e){this.speechWebSocket&&this.speechWebSocket.readyState===this.speechWebSocket.OPEN&&this.speechWebSocket.send(e)}debug(e,...t){this.isDebug&&console.debug(e,t)}createVolumeAnalayzer(){this._volume=0,this.volumeAnalyser=this.audioContext.createAnalyser(),this.volumeAnalyser.fftSize=this.volumeFftSize,this.volumeAnalyser.smoothingTimeConstant=.1,this.inputStream.connect(this.volumeAnalyser),this.volumeIntervalId=setInterval((()=>{this.updateVolume()}),100)}updateVolume(){const e=this.volumeAnalyser.frequencyBinCount,t=new Uint8Array(e);this.volumeAnalyser.getByteFrequencyData(t);const i=t.reduce(((e,t)=>e+t),0)/t.length;this._volume=i/this.volumeFftSize*100}connectAudioProcessor(){if(this.shouldTranscribeRecorded){const e=e=>{this.isRecording&&this.sendMessageIfSocketOpen(n.AudioProcessorHelper.audioProcessingEventToPcmChunk(e))};this._scriptProcessorNode=n.AudioProcessorHelper.createAudioProcessor(this.audioContext,this.inputStream,e)}}closeAudioContext(){this._audioContext&&(this._audioContext.close(),this._audioContext=void 0)}cleanupAudioResources(){this._scriptProcessorNode&&(this._scriptProcessorNode.disconnect(),this._scriptProcessorNode=null),this.volumeAnalyser&&this.volumeAnalyser.disconnect(),this._inputStream&&(this._inputStream.disconnect(),this._inputStream=null)}replaceParamsInUrl(e){return this.replaceSampleRateInUrl&&(e=e.replace(o.SAMPLE_RATE_PLACEHOLDER,this.sampleRate.toString())),e}}},712:(e,t,i)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.AudioVisualizer=void 0;const s=i(767);t.AudioVisualizer=class{constructor(e){var t,i;this.audioContext=e.audioContext,this.visualizerId=e.visualizerId,this.stream=e.stream,this.strokeStyle=null!==(t=e.strokeStyle)&&void 0!==t?t:"#811331",this.lineWidth=e.lineWidth,this.visualizationType=null!==(i=e.visualizationType)&&void 0!==i?i:s.VisualizationType.Waves}visualizeAudio(){if(this.stream){if(this.visualizerId){const e=this.audioContext.createMediaStreamSource(this.stream);this.canvas=document.getElementById(this.visualizerId),this.canvasContext=this.canvas.getContext("2d"),this.canvasContext.strokeStyle=this.strokeStyle,this.analyser=this.audioContext.createAnalyser(),this.visualizationType==s.VisualizationType.Linear?this.analyser.fftSize=2048:this.analyser.fftSize=512,this.analyser.smoothingTimeConstant=.95,e.connect(this.analyser)}this.draw()}else console.error("Audio visualizer has no stream! Try creating new visualizer")}stop(){var e;if(this.visualizerId&&this.canvas){var t=this.canvas.offsetWidth,i=this.canvas.offsetHeight;null===(e=this.canvasContext)||void 0===e||e.clearRect(0,0,t,i)}this.audioVisualizerAnimReqId&&cancelAnimationFrame(this.audioVisualizerAnimReqId),this.stream&&(this.stream.getAudioTracks().forEach((e=>e.stop())),this.analyser=void 0,this.audioContext=void 0,this.stream=void 0,this.audioVisualizerAnimReqId=void 0,this.canvas=null,this.canvasContext=null)}draw(){if(!this.canvas||!this.canvasContext)return;this.audioVisualizerAnimReqId=requestAnimationFrame((()=>{this.draw()}));const e=this.analyser.frequencyBinCount,t=new Uint8Array(e),i=this.canvas.offsetHeight,o=this.canvas.offsetWidth;this.canvas.height=i,this.canvas.width=o,this.canvasContext.clearRect(0,0,o,i),this.visualizationType===s.VisualizationType.Linear?this.drawLinearType(t,this.canvasContext,o,i):this.visualizationType===s.VisualizationType.Waves&&this.drawWavesType(t,this.canvasContext,o,i),this.canvasContext.stroke()}drawLinearType(e,t,i,s){this.analyser.getByteTimeDomainData(e);const o=this.analyser.frequencyBinCount;let r=0;for(let t=0;t<o;t++){let i=e[t]/128*s/2;0===t?this.canvasContext.moveTo(r,i):this.canvasContext.lineTo(r,i),r+=2}}drawWavesType(e,t,i,s){var o;this.analyser.getByteFrequencyData(e);const r=null!==(o=this.lineWidth)&&void 0!==o?o:i/e.length;t.lineWidth=r,t.beginPath();let n=0;for(let i=0;i<e.length;i++){const o=e[i]/255*s;t.imageSmoothingEnabled=!1,t.moveTo(n,s),t.lineTo(n,s-o),n+=r}}}},622:function(e,t,i){var s=this&&this.__createBinding||(Object.create?function(e,t,i,s){void 0===s&&(s=i);var o=Object.getOwnPropertyDescriptor(t,i);o&&!("get"in o?!t.__esModule:o.writable||o.configurable)||(o={enumerable:!0,get:function(){return t[i]}}),Object.defineProperty(e,s,o)}:function(e,t,i,s){void 0===s&&(s=i),e[s]=t[i]}),o=this&&this.__exportStar||function(e,t){for(var i in e)"default"===i||Object.prototype.hasOwnProperty.call(t,i)||s(t,e,i)};Object.defineProperty(t,"__esModule",{value:!0}),o(i(747),t)},747:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.SAMPLE_RATE_PLACEHOLDER=void 0,t.SAMPLE_RATE_PLACEHOLDER="{{sampleRate}}"},745:function(e,t,i){var s=this&&this.__createBinding||(Object.create?function(e,t,i,s){void 0===s&&(s=i);var o=Object.getOwnPropertyDescriptor(t,i);o&&!("get"in o?!t.__esModule:o.writable||o.configurable)||(o={enumerable:!0,get:function(){return t[i]}}),Object.defineProperty(e,s,o)}:function(e,t,i,s){void 0===s&&(s=i),e[s]=t[i]}),o=this&&this.__exportStar||function(e,t){for(var i in e)"default"===i||Object.prototype.hasOwnProperty.call(t,i)||s(t,e,i)};Object.defineProperty(t,"__esModule",{value:!0}),o(i(270),t)},270:(e,t)=>{var i;Object.defineProperty(t,"__esModule",{value:!0}),t.RecognitionStatus=void 0,(i=t.RecognitionStatus||(t.RecognitionStatus={}))[i.Success=0]="Success",i[i.NoSpeech=1]="NoSpeech",i[i.Aborted=2]="Aborted",i[i.StreamIssue=5]="StreamIssue",i[i.LoadLimitReached=9]="LoadLimitReached",i[i.AuthenticationFailed=10]="AuthenticationFailed",i[i.Busy=11]="Busy"},683:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.AudioProcessorHelper=void 0,t.AudioProcessorHelper=class{static createAudioProcessor(e,t,i){const s=e.createScriptProcessor(4096,1,1);return s.onaudioprocess=e=>{i&&i(e)},t.connect(s),s.connect(e.destination),s}static audioProcessingEventToPcmChunk(e){const t=e.inputBuffer.getChannelData(0),i=t.length,s=new Uint8Array(2*i);for(let e=0;e<i;e++){const i=t[e],o=i<0?32768*i:32767*i;s[2*e]=255&o,s[2*e+1]=o>>8&255}return s}}},981:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.sha1Async=void 0,t.sha1Async=async e=>{const t=(new TextEncoder).encode(e),i=await crypto.subtle.digest("SHA-1",t);return Array.from(new Uint8Array(i)).map((e=>e.toString(16).padStart(2,"0"))).join("")}},910:function(e,t,i){var s=this&&this.__createBinding||(Object.create?function(e,t,i,s){void 0===s&&(s=i);var o=Object.getOwnPropertyDescriptor(t,i);o&&!("get"in o?!t.__esModule:o.writable||o.configurable)||(o={enumerable:!0,get:function(){return t[i]}}),Object.defineProperty(e,s,o)}:function(e,t,i,s){void 0===s&&(s=i),e[s]=t[i]}),o=this&&this.__exportStar||function(e,t){for(var i in e)"default"===i||Object.prototype.hasOwnProperty.call(t,i)||s(t,e,i)};Object.defineProperty(t,"__esModule",{value:!0}),o(i(981),t),o(i(683),t)},632:function(e,t,i){var s=this&&this.__createBinding||(Object.create?function(e,t,i,s){void 0===s&&(s=i);var o=Object.getOwnPropertyDescriptor(t,i);o&&!("get"in o?!t.__esModule:o.writable||o.configurable)||(o={enumerable:!0,get:function(){return t[i]}}),Object.defineProperty(e,s,o)}:function(e,t,i,s){void 0===s&&(s=i),e[s]=t[i]}),o=this&&this.__exportStar||function(e,t){for(var i in e)"default"===i||Object.prototype.hasOwnProperty.call(t,i)||s(t,e,i)};Object.defineProperty(t,"__esModule",{value:!0}),o(i(447),t),o(i(545),t),o(i(745),t),o(i(910),t),o(i(761),t),o(i(622),t)},967:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0})},545:function(e,t,i){var s=this&&this.__createBinding||(Object.create?function(e,t,i,s){void 0===s&&(s=i);var o=Object.getOwnPropertyDescriptor(t,i);o&&!("get"in o?!t.__esModule:o.writable||o.configurable)||(o={enumerable:!0,get:function(){return t[i]}}),Object.defineProperty(e,s,o)}:function(e,t,i,s){void 0===s&&(s=i),e[s]=t[i]}),o=this&&this.__exportStar||function(e,t){for(var i in e)"default"===i||Object.prototype.hasOwnProperty.call(t,i)||s(t,e,i)};Object.defineProperty(t,"__esModule",{value:!0}),o(i(967),t),o(i(826),t),o(i(972),t),o(i(362),t),o(i(767),t)},362:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0})},826:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0})},972:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0})},767:(e,t)=>{var i;Object.defineProperty(t,"__esModule",{value:!0}),t.VisualizationType=void 0,(i=t.VisualizationType||(t.VisualizationType={})).Waves="0",i.Linear="1"},210:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.END_OF_STREAM_MESSAGE=void 0,t.END_OF_STREAM_MESSAGE="EOS"},761:function(e,t,i){var s=this&&this.__createBinding||(Object.create?function(e,t,i,s){void 0===s&&(s=i);var o=Object.getOwnPropertyDescriptor(t,i);o&&!("get"in o?!t.__esModule:o.writable||o.configurable)||(o={enumerable:!0,get:function(){return t[i]}}),Object.defineProperty(e,s,o)}:function(e,t,i,s){void 0===s&&(s=i),e[s]=t[i]}),o=this&&this.__exportStar||function(e,t){for(var i in e)"default"===i||Object.prototype.hasOwnProperty.call(t,i)||s(t,e,i)};Object.defineProperty(t,"__esModule",{value:!0}),o(i(210),t)},187:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.AudioDeviceHelper=void 0;class i{static async getListOfMicrophones(e=!1){return e&&await i.askPermissionForAudio(),navigator.mediaDevices.enumerateDevices().then((e=>e.filter((e=>"audiooutput"===e.kind))))}static async askPermissionForAudio(){try{return(await navigator.mediaDevices.getUserMedia({audio:!0})).getTracks().forEach((e=>e.stop())),!0}catch(e){return!1}}}t.AudioDeviceHelper=i},388:function(e,t,i){var s=this&&this.__createBinding||(Object.create?function(e,t,i,s){void 0===s&&(s=i);var o=Object.getOwnPropertyDescriptor(t,i);o&&!("get"in o?!t.__esModule:o.writable||o.configurable)||(o={enumerable:!0,get:function(){return t[i]}}),Object.defineProperty(e,s,o)}:function(e,t,i,s){void 0===s&&(s=i),e[s]=t[i]}),o=this&&this.__exportStar||function(e,t){for(var i in e)"default"===i||Object.prototype.hasOwnProperty.call(t,i)||s(t,e,i)};Object.defineProperty(t,"__esModule",{value:!0}),o(i(436),t),o(i(73),t),o(i(187),t)},436:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0})},73:function(e,t){var i,s,o,r,n=this&&this.__classPrivateFieldGet||function(e,t,i,s){if("a"===i&&!s)throw new TypeError("Private accessor was defined without a getter");if("function"==typeof t?e!==t||!s:!t.has(e))throw new TypeError("Cannot read private member from an object whose class did not declare it");return"m"===i?s:"a"===i?s.call(e):s?s.value:t.get(e)},a=this&&this.__classPrivateFieldSet||function(e,t,i,s,o){if("m"===s)throw new TypeError("Private method is not writable");if("a"===s&&!o)throw new TypeError("Private accessor was defined without a setter");if("function"==typeof t?e!==t||!o:!t.has(e))throw new TypeError("Cannot write private member to an object whose class did not declare it");return"a"===s?o.call(e,i):o?o.value=i:t.set(e,i),i};Object.defineProperty(t,"__esModule",{value:!0}),t.WebsocketHelper=void 0,t.WebsocketHelper=class{get url(){return n(this,s,"f").url}get binaryType(){var e;return null!==(e=n(this,s,"f").binaryType)&&void 0!==e?e:"arraybuffer"}get initialMessage(){return n(this,s,"f").initialMessage}get onMessage(){return n(this,s,"f").onMessage}get onStopped(){return n(this,s,"f").onStopped}get isDebug(){return n(this,s,"f").debug}constructor(e){i.add(this),s.set(this,void 0),o.set(this,void 0),a(this,s,e,"f")}async open(){if(n(this,o,"f"))throw new Error("Websocket is already opened, cannot open new one");const e=await n(this,i,"m",r).call(this);return new Promise((t=>{a(this,o,e?new WebSocket(`${this.url}`,e):new WebSocket(`${this.url}`),"f"),n(this,o,"f").binaryType=this.binaryType,n(this,o,"f").onopen=async()=>{if(this.initialMessage){const e=JSON.stringify(this.initialMessage);n(this,o,"f").send(e)}t()},n(this,o,"f").onmessage=e=>{this.onMessage(e)},n(this,o,"f").onclose=e=>{this.debug("Closing websocket",e),a(this,o,void 0,"f"),this.onStopped&&this.onStopped()}}))}stop(){this.debug("User has called close method. Closing websocket."),n(this,o,"f").close()}debug(e,...t){this.isDebug&&console.debug(e,t)}},s=new WeakMap,o=new WeakMap,i=new WeakSet,r=async function(){return n(this,s,"f").getToken?await n(this,s,"f").getToken():null}}},t={};return function i(s){var o=t[s];if(void 0!==o)return o.exports;var r=t[s]={exports:{}};return e[s].call(r.exports,r,r.exports,i),r.exports}(156)})())); |
{ | ||
"name": "@tilde-nlp/asr-api-client", | ||
"version": "4.0.6", | ||
"version": "4.0.7", | ||
"description": "Library with services for communicating with Tilde TSP platform", | ||
@@ -5,0 +5,0 @@ "private": false, |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Native code
Supply chain riskContains native code (e.g., compiled binaries or shared libraries). Including native code can obscure malicious behavior.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Native code
Supply chain riskContains native code (e.g., compiled binaries or shared libraries). Including native code can obscure malicious behavior.
Found 1 instance in 1 package