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 2.0.2 to 2.0.3

tilde-nlp-asr-api-client-2.0.3.tgz

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={607:function(e,t,i){var o=this&&this.__createBinding||(Object.create?function(e,t,i,o){void 0===o&&(o=i);var s=Object.getOwnPropertyDescriptor(t,i);s&&!("get"in s?!t.__esModule:s.writable||s.configurable)||(s={enumerable:!0,get:function(){return t[i]}}),Object.defineProperty(e,o,s)}:function(e,t,i,o){void 0===o&&(o=i),e[o]=t[i]}),s=this&&this.__exportStar||function(e,t){for(var i in e)"default"===i||Object.prototype.hasOwnProperty.call(t,i)||o(t,e,i)};Object.defineProperty(t,"__esModule",{value:!0}),s(i(86),t),s(i(801),t),s(i(729),t),s(i(216),t)},86:(e,t,i)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.AsrClient=void 0;const o=i(326),s=i(729);t.AsrClient=class{get isRecording(){return this._isRecording}constructor(e){var t;this._isRecording=!1,this.mimeType="audio/pcm",this.url=e.url,this.visualizerConfigBase=e.visualizerConfig,this.timeSlice=e.timeSlice>0?e.timeSlice:200,this.channelCount=null!==(t=e.channelCount)&&void 0!==t?t:1,this.onResult=e.onResult,this.onMessage=e.onMessage,this.onError=e.onError,this.onStopped=e.onStopped,this.onRecordingStartStop=e.onRecordingStartStop}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(){var e;null===(e=this.speechWebSocket)||void 0===e||e.close()}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};this.audioVisualizer=new o.AudioVisualizer(e)}this.mediaRecorder=new MediaStreamRecorder(t),this.mediaRecorder.mimeType=this.mimeType,this.mediaRecorder.audioChannels=this.channelCount,this.mediaRecorder.ondataavailable=e=>{this.speechWebSocket&&this.speechWebSocket.readyState===this.speechWebSocket.OPEN&&e.arrayBuffer().then((e=>{var t=new Uint8Array(e);this.speechWebSocket.send(t)}))},e()})).catch((e=>{this.onError&&(this.onError(e),t(e))}))}))}stopAll(){var e,t,i,o;null===(e=this.mediaRecorder)||void 0===e||e.stop(),this.setIsRecording(!1),this.speechWebSocket=void 0,null===(i=null===(t=this.inputStream)||void 0===t?void 0:t.mediaStream)||void 0===i||i.getAudioTracks().forEach((e=>e.stop())),null===(o=this.audioVisualizer)||void 0===o||o.stop(),this.audioVisualizer=void 0,this.onStopped&&this.onStopped()}stopPartially(){this.speechWebSocket=void 0,this.onStopped&&this.onStopped()}setupSpeechWebSocket(){return new Promise((e=>{this.speechWebSocket=new WebSocket(`${this.url}`),this.speechWebSocket.binaryType="arraybuffer",this.speechWebSocket.onopen=async()=>{const t=JSON.stringify({"enable-final-postprocess":["numbers","punct"]});this.speechWebSocket.send(t),e()},this.speechWebSocket.onmessage=e=>{try{const t=JSON.parse(e.data);switch(t.status){case s.RecognitionStatus.Success:t.result&&this.handleSpeechRecognitionResult(t.result);break;case s.RecognitionStatus.StreamIssue:case s.RecognitionStatus.Aborted:case s.RecognitionStatus.Busy:this.handleError();break;default:this.onMessage&&this.onMessage(t.status)}}catch(t){this.handleSpeechRecognitionResult(e.data)}},this.speechWebSocket.onclose=e=>{3e3===e.code?this.stopPartially():this.stopAll()}}))}handleError(){this.speechWebSocket.close(3e3,"Server issue"),this.stopPartially(),this.beginVoiceRecognition()}handleSpeechRecognitionResult(e){this.onResult&&this.onResult(e)}setIsRecording(e){this._isRecording=e,this.onRecordingStartStop&&this.onRecordingStartStop(this.isRecording)}}},326:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.AudioVisualizer=void 0,t.AudioVisualizer=class{constructor(e){var t;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}visualizeAudio(){if(this.stream){if(this.visualizerId){const e=this.audioContext.createMediaStreamSource(this.stream);this.analyser=this.audioContext.createAnalyser(),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 o=document.getElementById(this.visualizerId);if(o){var e=o.offsetWidth,t=o.height,i=o.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(){var e;const t=this.analyser.frequencyBinCount,i=new Uint8Array(t),o=document.getElementById(this.visualizerId);if(o){const t=o.offsetWidth,s=o.offsetHeight,r=o.getContext("2d"),n=null!==(e=this.lineWidth)&&void 0!==e?e:t/i.length;if(r){r.lineWidth=n,r.strokeStyle=this.strokeStyle,this.analyser.getByteFrequencyData(i),r.clearRect(0,0,t,s),r.beginPath();let e=0;if(i){for(let t=0;t<i.length;t++){const o=i[t]/255*s;r.imageSmoothingEnabled=!1,r.moveTo(e,s),r.lineTo(e,s-o),e+=n}r.stroke()}}this.audioVisualizerAnimReqId=requestAnimationFrame((()=>{this.draw()}))}}}},729:function(e,t,i){var o=this&&this.__createBinding||(Object.create?function(e,t,i,o){void 0===o&&(o=i);var s=Object.getOwnPropertyDescriptor(t,i);s&&!("get"in s?!t.__esModule:s.writable||s.configurable)||(s={enumerable:!0,get:function(){return t[i]}}),Object.defineProperty(e,o,s)}:function(e,t,i,o){void 0===o&&(o=i),e[o]=t[i]}),s=this&&this.__exportStar||function(e,t){for(var i in e)"default"===i||Object.prototype.hasOwnProperty.call(t,i)||o(t,e,i)};Object.defineProperty(t,"__esModule",{value:!0}),s(i(883),t)},883:(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"},88:(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("")}},216:function(e,t,i){var o=this&&this.__createBinding||(Object.create?function(e,t,i,o){void 0===o&&(o=i);var s=Object.getOwnPropertyDescriptor(t,i);s&&!("get"in s?!t.__esModule:s.writable||s.configurable)||(s={enumerable:!0,get:function(){return t[i]}}),Object.defineProperty(e,o,s)}:function(e,t,i,o){void 0===o&&(o=i),e[o]=t[i]}),s=this&&this.__exportStar||function(e,t){for(var i in e)"default"===i||Object.prototype.hasOwnProperty.call(t,i)||o(t,e,i)};Object.defineProperty(t,"__esModule",{value:!0}),s(i(88),t)},17:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0})},801:function(e,t,i){var o=this&&this.__createBinding||(Object.create?function(e,t,i,o){void 0===o&&(o=i);var s=Object.getOwnPropertyDescriptor(t,i);s&&!("get"in s?!t.__esModule:s.writable||s.configurable)||(s={enumerable:!0,get:function(){return t[i]}}),Object.defineProperty(e,o,s)}:function(e,t,i,o){void 0===o&&(o=i),e[o]=t[i]}),s=this&&this.__exportStar||function(e,t){for(var i in e)"default"===i||Object.prototype.hasOwnProperty.call(t,i)||o(t,e,i)};Object.defineProperty(t,"__esModule",{value:!0}),s(i(17),t),s(i(208),t),s(i(957),t),s(i(289),t),s(i(310),t)},289:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0})},208:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0})},957:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0})},310:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0})}},t={};return function i(o){var s=t[o];if(void 0!==s)return s.exports;var r=t[o]={exports:{}};return e[o].call(r.exports,r,r.exports,i),r.exports}(607)})()));
!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={607:function(e,t,i){var o=this&&this.__createBinding||(Object.create?function(e,t,i,o){void 0===o&&(o=i);var s=Object.getOwnPropertyDescriptor(t,i);s&&!("get"in s?!t.__esModule:s.writable||s.configurable)||(s={enumerable:!0,get:function(){return t[i]}}),Object.defineProperty(e,o,s)}:function(e,t,i,o){void 0===o&&(o=i),e[o]=t[i]}),s=this&&this.__exportStar||function(e,t){for(var i in e)"default"===i||Object.prototype.hasOwnProperty.call(t,i)||o(t,e,i)};Object.defineProperty(t,"__esModule",{value:!0}),s(i(86),t),s(i(801),t),s(i(729),t),s(i(216),t)},86:(e,t,i)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.AsrClient=void 0;const o=i(326),s=i(729);t.AsrClient=class{get isRecording(){return this._isRecording}constructor(e){var t;this._isRecording=!1,this.mimeType="audio/pcm",this.url=e.url,this.visualizerConfigBase=e.visualizerConfig,this.timeSlice=e.timeSlice>0?e.timeSlice:200,this.channelCount=null!==(t=e.channelCount)&&void 0!==t?t:1,this.onResult=e.onResult,this.onMessage=e.onMessage,this.onError=e.onError,this.onStopped=e.onStopped,this.onRecordingStartStop=e.onRecordingStartStop}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(){var e;null===(e=this.speechWebSocket)||void 0===e||e.close()}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};this.audioVisualizer=new o.AudioVisualizer(e)}this.mediaRecorder=new MediaStreamRecorder(t),this.mediaRecorder.mimeType=this.mimeType,this.mediaRecorder.audioChannels=this.channelCount,this.mediaRecorder.ondataavailable=e=>{this.speechWebSocket&&this.speechWebSocket.readyState===this.speechWebSocket.OPEN&&e.arrayBuffer().then((e=>{var t=new Uint8Array(e);this.speechWebSocket.send(t)}))},e()})).catch((e=>{this.onError&&(this.onError(e),t(e))}))}))}stopAll(){var e,t,i,o;null===(e=this.mediaRecorder)||void 0===e||e.stop(),this.setIsRecording(!1),this.speechWebSocket=void 0,null===(i=null===(t=this.inputStream)||void 0===t?void 0:t.mediaStream)||void 0===i||i.getAudioTracks().forEach((e=>e.stop())),null===(o=this.audioVisualizer)||void 0===o||o.stop(),this.audioVisualizer=void 0,this.onStopped&&this.onStopped()}stopPartially(){this.speechWebSocket=void 0,this.onStopped&&this.onStopped()}setupSpeechWebSocket(){return new Promise((e=>{this.speechWebSocket=new WebSocket(`${this.url}`),this.speechWebSocket.binaryType="arraybuffer",this.speechWebSocket.onopen=async()=>{const t=JSON.stringify({"enable-final-postprocess":["numbers","punct"]});this.speechWebSocket.send(t),e()},this.speechWebSocket.onmessage=e=>{try{const t=JSON.parse(e.data);switch(t.status){case s.RecognitionStatus.Success:t.result&&this.handleSpeechRecognitionResult(t.result);break;case s.RecognitionStatus.StreamIssue:case s.RecognitionStatus.Aborted:case s.RecognitionStatus.Busy:this.handleError();break;default:this.onMessage&&this.onMessage(t.status)}}catch(t){this.handleSpeechRecognitionResult(e.data)}},this.speechWebSocket.onclose=e=>{3e3===e.code?this.stopPartially():this.stopAll()}}))}handleError(){this.speechWebSocket.close(3e3,"Server issue"),this.stopPartially(),this.beginVoiceRecognition()}handleSpeechRecognitionResult(e){this.onResult&&this.onResult(e)}setIsRecording(e){this._isRecording=e,this.onRecordingStartStop&&this.onRecordingStartStop(this.isRecording,this)}}},326:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.AudioVisualizer=void 0,t.AudioVisualizer=class{constructor(e){var t;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}visualizeAudio(){if(this.stream){if(this.visualizerId){const e=this.audioContext.createMediaStreamSource(this.stream);this.analyser=this.audioContext.createAnalyser(),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 o=document.getElementById(this.visualizerId);if(o){var e=o.offsetWidth,t=o.height,i=o.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(){var e;const t=this.analyser.frequencyBinCount,i=new Uint8Array(t),o=document.getElementById(this.visualizerId);if(o){const t=o.offsetWidth,s=o.offsetHeight,r=o.getContext("2d"),n=null!==(e=this.lineWidth)&&void 0!==e?e:t/i.length;if(r){r.lineWidth=n,r.strokeStyle=this.strokeStyle,this.analyser.getByteFrequencyData(i),r.clearRect(0,0,t,s),r.beginPath();let e=0;if(i){for(let t=0;t<i.length;t++){const o=i[t]/255*s;r.imageSmoothingEnabled=!1,r.moveTo(e,s),r.lineTo(e,s-o),e+=n}r.stroke()}}this.audioVisualizerAnimReqId=requestAnimationFrame((()=>{this.draw()}))}}}},729:function(e,t,i){var o=this&&this.__createBinding||(Object.create?function(e,t,i,o){void 0===o&&(o=i);var s=Object.getOwnPropertyDescriptor(t,i);s&&!("get"in s?!t.__esModule:s.writable||s.configurable)||(s={enumerable:!0,get:function(){return t[i]}}),Object.defineProperty(e,o,s)}:function(e,t,i,o){void 0===o&&(o=i),e[o]=t[i]}),s=this&&this.__exportStar||function(e,t){for(var i in e)"default"===i||Object.prototype.hasOwnProperty.call(t,i)||o(t,e,i)};Object.defineProperty(t,"__esModule",{value:!0}),s(i(883),t)},883:(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"},88:(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("")}},216:function(e,t,i){var o=this&&this.__createBinding||(Object.create?function(e,t,i,o){void 0===o&&(o=i);var s=Object.getOwnPropertyDescriptor(t,i);s&&!("get"in s?!t.__esModule:s.writable||s.configurable)||(s={enumerable:!0,get:function(){return t[i]}}),Object.defineProperty(e,o,s)}:function(e,t,i,o){void 0===o&&(o=i),e[o]=t[i]}),s=this&&this.__exportStar||function(e,t){for(var i in e)"default"===i||Object.prototype.hasOwnProperty.call(t,i)||o(t,e,i)};Object.defineProperty(t,"__esModule",{value:!0}),s(i(88),t)},17:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0})},801:function(e,t,i){var o=this&&this.__createBinding||(Object.create?function(e,t,i,o){void 0===o&&(o=i);var s=Object.getOwnPropertyDescriptor(t,i);s&&!("get"in s?!t.__esModule:s.writable||s.configurable)||(s={enumerable:!0,get:function(){return t[i]}}),Object.defineProperty(e,o,s)}:function(e,t,i,o){void 0===o&&(o=i),e[o]=t[i]}),s=this&&this.__exportStar||function(e,t){for(var i in e)"default"===i||Object.prototype.hasOwnProperty.call(t,i)||o(t,e,i)};Object.defineProperty(t,"__esModule",{value:!0}),s(i(17),t),s(i(208),t),s(i(957),t),s(i(289),t),s(i(310),t)},289:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0})},208:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0})},957:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0})},310:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0})}},t={};return function i(o){var s=t[o];if(void 0!==s)return s.exports;var r=t[o]={exports:{}};return e[o].call(r.exports,r,r.exports,i),r.exports}(607)})()));
{
"name": "@tilde-nlp/asr-api-client",
"version": "2.0.2",
"version": "2.0.3",
"description": "Library with services for communicating with Tilde TSP platform",

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

# asr-api-client library
Library is created for implementing dictation. There is no UI components as this package is meant to be only as service for comunication. It is written with typescript and also includes typescript models.
Library is created for implementing dictation. It is written with typescript and also includes typescript models.
## Real time oscilogram
Library also provides possibility to draw real time oscilogram that will be drawn into `<canvas>` element. To be able to create this visualization, you need to add `<canvas></canvas>` element to your html and pass elements ID to visualizer configuration.
## Examples
Here is small example to use service in html with plain javascript.
Couple small examples for easier integration. Remember to replace variable placeholders with your own values.
### Plain js without any visualization
```html

@@ -40,2 +46,52 @@ <!DOCTYPE html>

</html>
```
### Plain js with audio visualization
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Tilde speech recognition example</title>
<!-- Include mediarecorder api -->
<script src="https://cdn.webrtc-experiment.com/MediaStreamRecorder.js"> </script>
<!-- Include Tilde asr api client -->
<script src="https://unpkg.com/@tilde-nlp/asr-api-client@latest/index.js"></script>
</head>
<body>
<script language="javascript">
// set up config
var config = {
url: "wss://services.tilde.com/service/asr/ws/${SYSTEM_NAME}/?contentType=audio/x-raw&sampleRate=44100&channelCount=1&x-api-key=${API_KEY}",
onResult: result => { if (result.final) console.log(result) }, // partial or final result
onRecordingStartStop: (isRecording, ctx) => {
// start drawing audio oscilogram if neccesarry
if (isRecording) {
ctx.audioVisualizer?.visualizeAudio();
}
}, // boolean value emitted whenever isRecording changes
onError: error => console.error(error), // error callback
// configuration if you want to add real time oscillogram, if not, you do not have to set this value
visualizerConfig: {
// id of container where to put visualizer
visualizerId: "audio-visualizer",
// optional param - visualization color. Default value: "#811331"
strokeStyle: "#811331"
}
}
// Create asr client
var asrClient = new window["asr-api-client"].AsrClient(config);
// Start voice recognition
asrClient.beginVoiceRecognition();
</script>
</body>
<!--necessary if you need real time oscilogram-->
<canvas id="audio-visualizer"></canvas>
</html>
```
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