window.speechcapture=function(){var a={WAV_BLOB:1,WEBAUDIO_AUDIOBUFFER:2,RAW_DATA:3,DETECTION_ONLY:4},b={SPEECH_STARTED:1,SPEECH_STOPPED:2,SPEECH_ERROR:3,CAPTURE_STARTED:4,CAPTURE_STOPPED:5,CAPTURE_ERROR:6,ENCODING_ERROR:7,SPEECH_MAX_LENGTH:8,SPEECH_MIN_LENGTH:9},c={NO_ERROR:0,INVALID_PARAMETER:1,MISSING_PARAMETER:2,NO_WEB_AUDIO_SUPPORT:3,CAPTURE_ALREADY_STARTED:4,AUDIOINPUT_NOT_AVAILABLE:5,RESAMPLING_UNSUPPORTED:6,RESAMPLING_ERROR:7,UNSPECIFIED:999},d={SAMPLERATE:16e3,AUDIOSOURCE_TYPE:7,CHANNELS:1,FORMAT:"PCM_16BIT",BUFFER_SIZE:16384,CONCATENATE_MAX_CHUNKS:1,DETECT_ONLY:!1,SPEECH_DETECTION_THRESHOLD:15,SPEECH_DETECTION_ALLOWED_DELAY:400,SPEECH_DETECTION_MIN_LENGTH:500,SPEECH_DETECTION_MAX_LENGTH:1e4,SPEECH_DETECTION_COMPRESS_PAUSES:!1,SPEECH_DETECTION_ANALYSIS_CHUNK_LENGTH:100,AUDIO_RESULT_TYPE:1,PREFER_GET_USER_MEDIA:!0,GETUSERMEDIA_ACTIVE:!0,AUDIOINPUT_PLUGIN_ACTIVE:!0,DEBUG_ALERTS:!1,DEBUG_CONSOLE:!1},e=function(e,f,g,h){if(Ha())T=c.CAPTURE_ALREADY_STARTED,fa(b.CAPTURE_ERROR);else{if(!f)throw T=c.MISSING_PARAMETER,"error: Mandatory parameter 'speechCapturedCB' is missing.";if("function"!=typeof f)throw T=c.INVALID_PARAMETER,"error: Parameter 'speechCapturedCB' must be of type function.";if(g&&"function"!=typeof g)throw T=c.INVALID_PARAMETER,"error: Parameter 'errorCB' must be of type function.";if(h&&"function"!=typeof h)throw T=c.INVALID_PARAMETER,"error: Parameter 'speechStatusCB' must be of type function.";if(e||(e={}),U={},U.audioinputPluginActive=e.audioInputPluginActive||d.AUDIOINPUT_PLUGIN_ACTIVE,U.sampleRate=e.sampleRate||d.SAMPLERATE,U.inputSampleRate=e.inputSampleRate||e.sampleRate||d.SAMPLERATE,U.bufferSize=e.bufferSize||d.BUFFER_SIZE,U.audioSourceType=e.audioSourceType||d.AUDIOSOURCE_TYPE,U.concatenateMaxChunks=d.CONCATENATE_MAX_CHUNKS,U.channels=d.CHANNELS,U.format=d.FORMAT,U.speechCapturedCB=f,U.errorCB=g||null,U.speechStatusCB=h||null,U.speechDetectionThreshold=e.speechDetectionThreshold||d.SPEECH_DETECTION_THRESHOLD,U.speechDetectionMinimum=e.speechDetectionMinimum||d.SPEECH_DETECTION_MIN_LENGTH,U.speechDetectionMaximum=e.speechDetectionMaximum||d.SPEECH_DETECTION_MAX_LENGTH,U.speechDetectionAllowedDelay=e.speechDetectionAllowedDelay||d.SPEECH_DETECTION_ALLOWED_DELAY,U.audioResultType=e.audioResultType||d.AUDIO_RESULT_TYPE,U.audioContext=e.audioContext||null,U.compressPauses=e.compressPauses||d.SPEECH_DETECTION_COMPRESS_PAUSES,U.analysisChunkLength=e.analysisChunkLength||d.SPEECH_DETECTION_ANALYSIS_CHUNK_LENGTH,U.debugAlerts=e.debugAlerts||d.DEBUG_ALERTS,U.debugConsole=e.debugConsole||d.DEBUG_CONSOLE,U.preferGUM=e.preferGUM||d.PREFER_GET_USER_MEDIA,U.getUserMediaActive=d.GETUSERMEDIA_ACTIVE,U.detectOnly=e.detectOnly||d.DETECT_ONLY,U.detectOnly&&(U.audioResultType=a.DETECTION_ONLY),(U.audioResultType===a.WEBAUDIO_AUDIOBUFFER||U.preferGUM||!U.audioinputPluginActive||!window.audioinput)&&!Da(U.audioContext,U.preferGUM)&&U.audioResultType===a.WEBAUDIO_AUDIOBUFFER)throw T=c.NO_WEB_AUDIO_SUPPORT,"error: audioResultType is WEBAUDIO_AUDIOBUFFER, but Web Audio not supported on this platform!";if(ba(U.inputSampleRate,U.bufferSize),oa(),V={sampleRate:U.inputSampleRate,bufferSize:U.bufferSize,channels:U.channels,format:U.format,audioSourceType:U.audioSourceType,streamToWebAudio:!1},Y)Ea();else{if(!U.audioinputPluginActive||!window.audioinput)throw T=c.AUDIOINPUT_NOT_AVAILABLE,"error: Nor getUserMedia or cordova-plugin-audioinput are available!";window.removeEventListener("audioinput",m,!1),window.addEventListener("audioinput",m,!1),window.removeEventListener("audioinputerror",n,!1),window.addEventListener("audioinputerror",n,!1),audioinput.start(V),ga(),fa(b.CAPTURE_STARTED)}}},f=function(){U.audioinputPluginActive&&window.audioinput&&audioinput.isCapturing()&&audioinput.stop(),z.length>0&&za(z),Fa(),oa(),Y=!1,_=null,X=null},g=function(){return Ha()},h=function(){return Q},i=function(){return U},j=function(){return B?parseFloat(B).toFixed(0):-1},k=function(){var a=!1;return U.audioinputPluginActive&&window.audioinput&&(a=audioinput.isCapturing()),{RealTime:{AmbientAverageLevel:parseFloat(F).toFixed(0),CurrentLevel:parseFloat(B).toFixed(0),CurrentThreshold:parseFloat(C).toFixed(0),CurrentSpeechChunks:A,CurrentSpeechBufferSize:z.length,CurrentSpeechLength:D,TotalNumOfSpeechChunks:x,InputQueueLength:y.length,InputDataTotal:aa},Events:{NumOfStart:t,NumOfContinue:s,NumOfStop:u,NumOfMin:w,NumOfMax:v},GetUserMedia:{Active:Y,CapturingStatus:$,SampleRate:U.inputSampleRate},AudioInput:{Active:a,Events:O,SampleRate:U.inputSampleRate},Cfg:U,Internals:{AnalysisBufferSize:G,AnalysisIterations:q,AnalysisBuffersPerIteration:H,AnalysisBufferLengthInS:parseFloat(P).toFixed(3),AudioInputFrequency:I,InputBufferLenInS:parseFloat(J).toFixed(3),MinLengthChunks:L,MaxLengthChunks:M,AllowedDelayChunks:K,GetNextBufferIterations:N,SampleRate:U.sampleRate}}},l=function(){return R},m=function(a){try{O++,a&&a.data&&y.push(new Float32Array(a.data))}catch(a){da(a)}},n=function(a){da(a)},o=function(){return T},p=50,q=0,r=0,s=0,t=0,u=0,v=0,w=0,x=0,y=[],z=[],A=0,B=-50,C=0,D=0,E=0,F=0,G=0,H=0,I=0,J=0,K=0,L=0,M=0,N=0,O=0,P=0,Q=!1,R=null,S=!1,T=c.NO_ERROR,U={},V={},W=null,X=null,Y=!1,Z=!1,$=!1,_=null,aa=0,ba=function(a,b){try{U.bufferSize=b,I=a/b,J=1/I,ca(b,J,U.analysisChunkLength)}catch(a){da("_calculateTimePeriods exception: "+a)}},ca=function(a,b,c){try{var d=1e3*b;H=Math.ceil(d/c),G=Math.ceil(a/H),P=b/H,K=Math.round(U.speechDetectionAllowedDelay/c),L=Math.round(U.speechDetectionMinimum/c),M=Math.round(U.speechDetectionMaximum/c),p=c}catch(a){da("_calculateAnalysisBuffers exception: "+a)}},da=function(a){var b={};a?a.message?b.message=a.message:b.message=a:b.message="An unhandled error has occurred.",U.errorCB&&U.errorCB(b),Ja(b.message),Ia(b.message)},ea=function(a){U.speechCapturedCB?U.speechCapturedCB(a,U.audioResultType):da("_callSpeechCapturedCB: No 'speechCapturedCB' callback defined!")},fa=function(a){U.speechStatusCB&&U.speechStatusCB(a)},ga=function(){try{if(N++,Ha()){var a=ha();a&&a.length>0&&ia(a),setTimeout(ga,p)}else Q&&xa(z)}catch(a){da("_getNextBuffer exception: "+a),fa(b.SPEECH_ERROR),oa()}},ha=function(){var a=new Float32Array(0),b=null;if(y.length>0)for(var c=0;cc&&(f=c);var g=a.slice(e,f);if(!ja(g))return}}catch(a){da("_iteratedAndMonitorInputBuffer exception: "+a),fa(b.SPEECH_ERROR)}},ja=function(a){try{return A+1>M?(wa(z),!1):(ka(a)?Q?va(a,!1):ua(a):(Q&&(D++,D>K?xa(z):U.compressPauses||va(a,!0)),la(B)),!0)}catch(a){return da("_monitor exception: "+a),fa(b.SPEECH_ERROR),oa(),!1}},ka=function(a){try{if(a&&a.length>0){q++;var c=ma(a);if(c!==-(1/0)&&(B=c,B>C))return x++,!0}}catch(a){da("_identifySpeech exception: "+a),fa(b.SPEECH_ERROR)}return!1},la=function(a){r++,E+=a,F=E/r,C=F+U.speechDetectionThreshold,C=C>0?0:C},ma=function(a){try{for(var e,f,g,c=0,d=a.length,h=0;hL){var d=c.slice(0);switch(U.audioResultType){case a.WEBAUDIO_AUDIOBUFFER:Ba(d);break;case a.RAW_DATA:ea(d);break;case a.DETECTION_ONLY:break;default:case a.WAV_BLOB:Aa(d)}}else w++,fa(b.SPEECH_MIN_LENGTH)},Aa=function(a){try{Ja("_createWAVAudioBuffer: "+a.length);var c=function(a){try{var c=null;if(1===U.channels)c=a.getChannelData(0);else{if(2!==U.channels)return da("_createWAVAudioBuffer doesn't support more than two (2) channels!"),void fa(b.ENCODING_ERROR);c=wavEncoder.interleave(a.getChannelData(0),a.getChannelData(1))}var d=wavEncoder.encode(c,U.sampleRate,U.channels);ea(new Blob([d],{type:"audio/wav"}))}catch(a){da("_createWAVAudioBuffer (with resampling) exception: "+a),fa(b.ENCODING_ERROR)}},d=function(a){try{var c=wavEncoder.encode(a,U.sampleRate,U.channels);ea(new Blob([c],{type:"audio/wav"}))}catch(a){da("_createWAVAudioBuffer exception: "+a),fa(b.ENCODING_ERROR)}};if(U.inputSampleRate!==U.sampleRate)if(S){var e=Ca(a);ReSampler.resampleAudioBuffer(e,U.sampleRate,c,da)}else da("_createWAVAudioBuffer (with resampling) failed since Web Audio API isn't supported. Remove the 'samplerate' from the start configuration in order to use the default samplerate of the platform and avoid this error."),fa(b.RESAMPLING_UNSUPPORTED);else d(a)}catch(a){da("_createWAVAudioBuffer exception: "+a),fa(b.ENCODING_ERROR)}},Ba=function(a){try{Ja("_createWebAudioBuffer: "+a.length);var c=Ca(a);if(c.inputSampleRate!==U.sampleRate)try{Ja("_createWebAudioBuffer - Resample audio from "+U.inputSampleRate+" to "+U.sampleRate+": "+a.length),ReSampler.resampleAudioBuffer(c,U.sampleRate,ea,da)}catch(a){da("_createWebAudioBuffer resampling exception: "+a),fa(b.RESAMPLING_ERROR)}else ea(c)}catch(a){da("_createWebAudioBuffer exception: "+a),fa(b.ENCODING_ERROR)}},Ca=function(a){var b=l().createBuffer(V.channels,a.length/V.channels,U.inputSampleRate);if(V.channels>1)for(var c=0;c