UNPKG

@bddh/starling-realtime-client

Version:

2 lines (1 loc) 3.57 kB
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("./_virtual/_rollupPluginBabelHelpers.js");function t(e,t,r,o){try{for(var a=(o=o||{})&&"number"==typeof o.index?o.index:0,n=o&&"number"==typeof o.offset?o.offset:0,i=0,s=a;s<e.length;s++)i+=e[s].length;i=Math.max(0,i-Math.floor(n));var u=t/r;u>1?i=Math.floor(i/u):(u=1,r=t);for(var c=new Int16Array(i),d=0,h=e.length;a<h;a++){for(var p=e[a],l=n,f=p.length;l<f;){var m=Math.floor(l),v=Math.ceil(l),x=l-m;c[d]=p[m]+(p[v]-p[m])*x,d++,l+=u}n=l-f}return{index:a,offset:n,sampleRate:r,data:c}}catch(e){console.log("转音错误了",e)}}function r(e){for(var t=e,r=new Int16Array(e.length),o=0;o<t.length;o++){var a=Math.max(-1,Math.min(1,t[o]));r[o]=a<0?32768*a:32767*a}return r}var o=function(o,a){function n(t){e.classCallCheck(this,n),e.defineProperty(this,"onAudioData",void 0),e.defineProperty(this,"onError",void 0),e.defineProperty(this,"audioContext",null),e.defineProperty(this,"processor",null),e.defineProperty(this,"mediaStream",null),e.defineProperty(this,"recording",!1),e.defineProperty(this,"audio3AConstraints",void 0),e.defineProperty(this,"asrSample",void 0),this.onAudioData=t.onAudioData,this.onError=t.onError,this.audio3AConstraints=t.audio3AConstraints,this.asrSample=t.asrSample,this.preWarmStream()}return e.createClass(n,[{key:"preWarmStream",value:function(){return(o=o||e.asyncToGenerator(e.regeneratorRuntime().mark((function t(){var r,o,a;return e.regeneratorRuntime().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(t.prev=0,o=window.AudioContext||window.webkitAudioContext){t.next=4;break}throw new Error("当前环境不支持web api: AudioContext");case 4:if(null!==(r=navigator.mediaDevices)&&void 0!==r&&r.getUserMedia){t.next=6;break}throw new Error("当前环境不支持web api: getUserMedia");case 6:return this.audioContext=new o({sampleRate:this.asrSample||16e3}),t.next=9,navigator.mediaDevices.getUserMedia({audio:e.objectSpread2({echoCancellation:!0,noiseSuppression:!0,autoGainControl:!0},this.audio3AConstraints),video:!1});case 9:this.mediaStream=t.sent,a=this.audioContext.createMediaStreamSource(this.mediaStream),this.processor=this.audioContext.createScriptProcessor(1024,1,1),a.connect(this.processor),this.processor.connect(this.audioContext.destination),this.processor.onaudioprocess=function(){},t.next=20;break;case 17:t.prev=17,t.t0=t.catch(0),console.error("preWarmStream error",t.t0);case 20:case"end":return t.stop()}}),t,this,[[0,17]])})))).apply(this,arguments)}},{key:"start",value:function(){return(a=a||e.asyncToGenerator(e.regeneratorRuntime().mark((function o(){var a=this;return e.regeneratorRuntime().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(!this.recording){e.next=2;break}return e.abrupt("return");case 2:if(this.audioContext&&this.processor){e.next=4;break}return e.abrupt("return");case 4:try{this.processor.onaudioprocess=function(e){if(a.recording){var o=t([r(e.inputBuffer.getChannelData(0))],a.audioContext.sampleRate,a.asrSample||16e3,null);o&&o.data&&a.onAudioData(o.data.buffer)}},this.recording=!0}catch(e){this.onError&&this.onError(e)}case 5:case"end":return e.stop()}}),o,this)})))).apply(this,arguments)}},{key:"stop",value:function(){this.recording=!1,this.processor.onaudioprocess=function(){}}},{key:"destroy",value:function(){this.recording=!1,this.processor&&(this.processor.disconnect(),this.processor.onaudioprocess=null),this.audioContext&&this.audioContext.close(),this.mediaStream&&this.mediaStream.getTracks().forEach((function(e){e.stop()}))}}]),n}();exports.default=o;