Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@tilde-nlp/asr-api-client

Package Overview
Dependencies
Maintainers
5
Versions
41
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@tilde-nlp/asr-api-client - npm Package Compare versions

Comparing version 3.0.1 to 3.0.2

lib/shared/audio-device.helper.d.ts

2

index.js

@@ -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(745),n=i(761);t.AsrClient=class{get isRecording(){return this._isRecording}constructor(e){var t,i,s;this._isRecording=!1,this.mimeType="audio/pcm",this.url=e.url,this.vocabulary=e.vocabulary,this.visualizerConfigBase=e.visualizerConfig,this.timeSlice=e.timeSlice>0?e.timeSlice:200,this.channelCount=null!==(t=e.channelCount)&&void 0!==t?t:1,this.eosTimeoutMs=null!==(i=e.eosTimeoutMs)&&void 0!==i?i:200,this.isDebug=null!==(s=e.debug)&&void 0!==s&&s,this.onResult=e.onResult,this.onMessage=e.onMessage,this.onError=e.onError,this.onStopped=e.onStopped,this.onRecordingStartStop=e.onRecordingStartStop,this.getToken=e.getToken}beginVoiceRecognition(){this.speechWebSocket||this.setUpMic().then((()=>{this.setupSpeechWebSocket().then((()=>{var e;null===(e=this.mediaRecorder)||void 0===e||e.start(this.timeSlice),this.setIsRecording(!0)}))}),(()=>{}))}listenToWebSocket(){this.setupSpeechWebSocket()}endVoiceRecognition(e=!1){this.stopRecording(),e&&this.closeWebSocket()}setUpMic(){return new Promise(((e,t)=>{navigator.mediaDevices.getUserMedia({audio:!0}).then((t=>{var i;if(this.audioContext=new AudioContext,null===(i=this.visualizerConfigBase)||void 0===i?void 0:i.visualizerId){const e={audioContext:this.audioContext,stream:t,visualizerId:this.visualizerConfigBase.visualizerId,strokeStyle:this.visualizerConfigBase.strokeStyle,lineWidth:this.visualizerConfigBase.lineWidth,visualizationType:this.visualizerConfigBase.visualizationType};this.audioVisualizer=new s.AudioVisualizer(e)}this.mediaRecorder=new MediaStreamRecorder(t),this.mediaRecorder.mimeType=this.mimeType,this.mediaRecorder.audioChannels=this.channelCount,this.mediaRecorder.ondataavailable=e=>{e.arrayBuffer().then((t=>{this.debug("On data available callback called",e);var i=new Uint8Array(t);this.sendMessageIfSocketOpen(i)}))},e()})).catch((e=>{this.onError&&(this.onError(e),t(e))}))}))}stopRecording(){var e,t,i,s;this.debug("Stopping recording"),null===(e=this.mediaRecorder)||void 0===e||e.stop(),this.setIsRecording(!1),null===(i=null===(t=this.inputStream)||void 0===t?void 0:t.mediaStream)||void 0===i||i.getAudioTracks().forEach((e=>e.stop())),null===(s=this.audioVisualizer)||void 0===s||s.stop(),this.audioVisualizer=void 0,this.debug("Recording stopped"),this.sendEndOfStream()}async setupSpeechWebSocket(){const e=await this.getTokenFromConfig();return new Promise((t=>{this.speechWebSocket=e?new WebSocket(`${this.url}`,e):new WebSocket(`${this.url}`),this.speechWebSocket.binaryType="arraybuffer";const i={"enable-final-postprocess":["numbers","punct"]};this.vocabulary&&(i["expand-vocab"]=this.vocabulary),this.speechWebSocket.onopen=async()=>{const e=JSON.stringify(i);this.speechWebSocket.send(e),t()},this.speechWebSocket.onmessage=e=>{let t;try{switch(t=JSON.parse(e.data),t.status){case o.RecognitionStatus.Success:t.result&&this.handleSpeechRecognitionResult(t.result);break;case o.RecognitionStatus.StreamIssue:case o.RecognitionStatus.Aborted:case o.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 getTokenFromConfig(){return this.getToken?await this.getToken():null}handleError(){this.speechWebSocket.close(3e3,"Server issue"),this.beginVoiceRecognition()}handleSpeechRecognitionResult(e){this.onResult&&this.onResult(e)}setIsRecording(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}sendEndOfStream(){const e=()=>{this.debug("sending EOS"),this.sendMessageIfSocketOpen(n.END_OF_STREAM_MESSAGE),this.EOSsent=!0,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"),e()}),this.eosTimeoutMs);e()}sendMessageIfSocketOpen(e){this.speechWebSocket&&this.speechWebSocket.readyState===this.speechWebSocket.OPEN&&this.speechWebSocket.send(e)}debug(e,...t){this.isDebug&&console.debug(e,t)}}},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.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(){if(this.visualizerId){const s=document.getElementById(this.visualizerId);if(s){var e=s.offsetWidth,t=s.height,i=s.getContext("2d");null==i||i.clearRect(0,0,e,t)}}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)}draw(){const e=document.getElementById(this.visualizerId);if(!e)return;this.audioVisualizerAnimReqId=requestAnimationFrame((()=>{this.draw()}));const t=this.analyser.frequencyBinCount,i=new Uint8Array(t),o=e.getContext("2d"),n=e.offsetWidth,r=e.offsetHeight;o&&(o.strokeStyle=this.strokeStyle,this.visualizationType===s.VisualizationType.Linear?this.drawLinearType(i,o,n,r,t):this.visualizationType===s.VisualizationType.Waves&&this.drawWavesType(i,o,n,r),o.stroke())}drawLinearType(e,t,i,s,o){this.analyser.getByteTimeDomainData(e),t.fillStyle="rgb(255, 255, 255)",t.fillRect(0,0,i,s),t.lineWidth=2,t.beginPath();const n=i/o;let r=0;for(let i=0;i<o;i++){const o=e[i]/128*s/2;0===i?t.moveTo(r,o):t.lineTo(r,o),r+=n}t.lineTo(i,s/2)}drawWavesType(e,t,i,s){var o;this.analyser.getByteFrequencyData(e),t.clearRect(0,0,i,s);const n=null!==(o=this.lineWidth)&&void 0!==o?o:i/e.length;t.lineWidth=n,t.beginPath();let r=0;for(let i=0;i<e.length;i++){const o=e[i]/255*s;t.imageSmoothingEnabled=!1,t.moveTo(r,s),t.lineTo(r,s-o),r+=n}}}},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"},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)},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)},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)},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)},436:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0})},73:function(e,t){var i,s,o,n,r=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 r(this,s,"f").url}get binaryType(){var e;return null!==(e=r(this,s,"f").binaryType)&&void 0!==e?e:"arraybuffer"}get initialMessage(){return r(this,s,"f").initialMessage}get onMessage(){return r(this,s,"f").onMessage}get onStopped(){return r(this,s,"f").onStopped}get isDebug(){return r(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(r(this,o,"f"))throw new Error("Websocket is already opened, cannot open new one");const e=await r(this,i,"m",n).call(this);return new Promise((t=>{a(this,o,e?new WebSocket(`${this.url}`,e):new WebSocket(`${this.url}`),"f"),r(this,o,"f").binaryType=this.binaryType,r(this,o,"f").onopen=async()=>{if(this.initialMessage){const e=JSON.stringify(this.initialMessage);r(this,o,"f").send(e)}t()},r(this,o,"f").onmessage=e=>{this.onMessage(e)},r(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."),r(this,o,"f").close()}debug(e,...t){this.isDebug&&console.debug(e,t)}},s=new WeakMap,o=new WeakMap,i=new WeakSet,n=async function(){return r(this,s,"f").getToken?await r(this,s,"f").getToken():null}}},t={};return function i(s){var o=t[s];if(void 0!==o)return o.exports;var n=t[s]={exports:{}};return e[s].call(n.exports,n,n.exports,i),n.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(745),n=i(761);t.AsrClient=class{get audioVisualizer(){return this._audioVisualizer}get audioContext(){return this._audioContext}get stream(){return this._stream}get isRecording(){return this._isRecording}get volume(){return this._volume}constructor(e){var t,i,s;this.volumeFftSize=256,this._isRecording=!1,this._volume=0,this.mimeType="audio/pcm",this.url=e.url,this.vocabulary=e.vocabulary,this.visualizerConfigBase=e.visualizerConfig,this.timeSlice=e.timeSlice>0?e.timeSlice:200,this.channelCount=null!==(t=e.channelCount)&&void 0!==t?t:1,this.eosTimeoutMs=null!==(i=e.eosTimeoutMs)&&void 0!==i?i:200,this.isDebug=null!==(s=e.debug)&&void 0!==s&&s,this.deviceId=e.deviceId,this.onResult=e.onResult,this.onMessage=e.onMessage,this.onError=e.onError,this.onStopped=e.onStopped,this.onRecordingStartStop=e.onRecordingStartStop,this.getToken=e.getToken}beginVoiceRecognition(){this.speechWebSocket||this.setUpMic().then((()=>{this.setupSpeechWebSocket().then((()=>{var e;null===(e=this.mediaRecorder)||void 0===e||e.start(this.timeSlice),this.setIsRecording(!0)}))}),(()=>{}))}listenToWebSocket(){this.setupSpeechWebSocket()}endVoiceRecognition(e=!1){this.stopRecording(),e&&this.closeWebSocket()}setUpMic(){return new Promise(((e,t)=>{navigator.mediaDevices.getUserMedia({audio:{deviceId:{ideal:this.deviceId}}}).then((t=>{var i;if(this._stream=t,this._audioContext=new AudioContext,null===(i=this.visualizerConfigBase)||void 0===i?void 0:i.visualizerId){const e={audioContext:this._audioContext,stream:t,visualizerId:this.visualizerConfigBase.visualizerId,strokeStyle:this.visualizerConfigBase.strokeStyle,lineWidth:this.visualizerConfigBase.lineWidth,visualizationType:this.visualizerConfigBase.visualizationType};this._audioVisualizer=new s.AudioVisualizer(e)}this.createVolumeAnalayzer(),this.mediaRecorder=new MediaStreamRecorder(t),this.mediaRecorder.mimeType=this.mimeType,this.mediaRecorder.audioChannels=this.channelCount,this.mediaRecorder.ondataavailable=e=>{e.arrayBuffer().then((t=>{this.debug("On data available callback called",e);var i=new Uint8Array(t);this.sendMessageIfSocketOpen(i)}))},e()})).catch((e=>{this.onError&&(this.onError(e),t(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.sendEndOfStream()}async setupSpeechWebSocket(){const e=await this.getTokenFromConfig();return new Promise((t=>{this.speechWebSocket=e?new WebSocket(`${this.url}`,e):new WebSocket(`${this.url}`),this.speechWebSocket.binaryType="arraybuffer";const i={"enable-final-postprocess":["numbers","punct"]};this.vocabulary&&(i["expand-vocab"]=this.vocabulary),this.speechWebSocket.onopen=async()=>{const e=JSON.stringify(i);this.speechWebSocket.send(e),t()},this.speechWebSocket.onmessage=e=>{let t;try{switch(t=JSON.parse(e.data),t.status){case o.RecognitionStatus.Success:t.result&&this.handleSpeechRecognitionResult(t.result);break;case o.RecognitionStatus.StreamIssue:case o.RecognitionStatus.Aborted:case o.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 getTokenFromConfig(){return this.getToken?await this.getToken():null}handleError(){this.speechWebSocket.close(3e3,"Server issue"),this.beginVoiceRecognition()}handleSpeechRecognitionResult(e){this.onResult&&this.onResult(e)}setIsRecording(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}sendEndOfStream(){const e=()=>{this.debug("sending EOS"),this.sendMessageIfSocketOpen(n.END_OF_STREAM_MESSAGE),this.EOSsent=!0,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"),e()}),this.eosTimeoutMs);e()}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;const e=this.audioContext.createMediaStreamSource(this.stream);this.volumeAnalyser=this.audioContext.createAnalyser(),this.volumeAnalyser.fftSize=this.volumeFftSize,this.volumeAnalyser.smoothingTimeConstant=.1,e.connect(this.volumeAnalyser),console.log(this.volumeAnalyser.maxDecibels,this.volumeAnalyser.minDecibels),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}}},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.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(){if(this.visualizerId){const s=document.getElementById(this.visualizerId);if(s){var e=s.offsetWidth,t=s.height,i=s.getContext("2d");null==i||i.clearRect(0,0,e,t)}}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)}draw(){const e=document.getElementById(this.visualizerId);if(!e)return;this.audioVisualizerAnimReqId=requestAnimationFrame((()=>{this.draw()}));const t=this.analyser.frequencyBinCount,i=new Uint8Array(t),o=e.getContext("2d"),n=e.offsetWidth,r=e.offsetHeight;o&&(o.strokeStyle=this.strokeStyle,this.visualizationType===s.VisualizationType.Linear?this.drawLinearType(i,o,n,r,t):this.visualizationType===s.VisualizationType.Waves&&this.drawWavesType(i,o,n,r),o.stroke())}drawLinearType(e,t,i,s,o){this.analyser.getByteTimeDomainData(e),t.fillStyle="rgb(255, 255, 255)",t.fillRect(0,0,i,s),t.lineWidth=2,t.beginPath();const n=i/o;let r=0;for(let i=0;i<o;i++){const o=e[i]/128*s/2;0===i?t.moveTo(r,o):t.lineTo(r,o),r+=n}t.lineTo(i,s/2)}drawWavesType(e,t,i,s){var o;this.analyser.getByteFrequencyData(e),t.clearRect(0,0,i,s);const n=null!==(o=this.lineWidth)&&void 0!==o?o:i/e.length;t.lineWidth=n,t.beginPath();let r=0;for(let i=0;i<e.length;i++){const o=e[i]/255*s;t.imageSmoothingEnabled=!1,t.moveTo(r,s),t.lineTo(r,s-o),r+=n}}}},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"},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)},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)},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,n,r=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 r(this,s,"f").url}get binaryType(){var e;return null!==(e=r(this,s,"f").binaryType)&&void 0!==e?e:"arraybuffer"}get initialMessage(){return r(this,s,"f").initialMessage}get onMessage(){return r(this,s,"f").onMessage}get onStopped(){return r(this,s,"f").onStopped}get isDebug(){return r(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(r(this,o,"f"))throw new Error("Websocket is already opened, cannot open new one");const e=await r(this,i,"m",n).call(this);return new Promise((t=>{a(this,o,e?new WebSocket(`${this.url}`,e):new WebSocket(`${this.url}`),"f"),r(this,o,"f").binaryType=this.binaryType,r(this,o,"f").onopen=async()=>{if(this.initialMessage){const e=JSON.stringify(this.initialMessage);r(this,o,"f").send(e)}t()},r(this,o,"f").onmessage=e=>{this.onMessage(e)},r(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."),r(this,o,"f").close()}debug(e,...t){this.isDebug&&console.debug(e,t)}},s=new WeakMap,o=new WeakMap,i=new WeakSet,n=async function(){return r(this,s,"f").getToken?await r(this,s,"f").getToken():null}}},t={};return function i(s){var o=t[s];if(void 0!==o)return o.exports;var n=t[s]={exports:{}};return e[s].call(n.exports,n,n.exports,i),n.exports}(156)})()));
import { AudioVisualizer } from "./audio-visualizer.model";
import { AsrClientConfiguration } from "./interfaces";
export declare class AsrClient {
audioVisualizer?: AudioVisualizer;
audioContext?: AudioContext;
private readonly volumeFftSize;
private _audioVisualizer?;
get audioVisualizer(): AudioVisualizer;
private _audioContext?;
get audioContext(): AudioContext;
private _stream?;
get stream(): MediaStream;
private _isRecording;
get isRecording(): boolean;
private _volume;
get volume(): number;
private speechWebSocket?;
private mediaRecorder?;
private inputStream?;
private EOSsent;
private volumeAnalyser;
private volumeIntervalId;
private readonly timeSlice;

@@ -17,2 +25,3 @@ private readonly channelCount;

private readonly isDebug;
private readonly deviceId;
private readonly url;

@@ -49,2 +58,4 @@ private readonly vocabulary;

private debug;
private createVolumeAnalayzer;
private updateVolume;
}

@@ -21,2 +21,7 @@ import { AsrClient } from "../asr-client.model";

/**
* Audio input device id. You can get available Ids by using navigator.mediaDevices.enumerateDevices().
* If not set, it will use what browser gives by default.
*/
deviceId?: string;
/**
* Callback for handling recognized speech.

@@ -23,0 +28,0 @@ */

export * from "./websocket-settings.interface";
export * from "./websocket.helper";
export * from "./audio-device.helper";
{
"name": "@tilde-nlp/asr-api-client",
"version": "3.0.1",
"version": "3.0.2",
"description": "Library with services for communicating with Tilde TSP platform",

@@ -5,0 +5,0 @@ "private": false,

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc