function CircularBuffer(n){var t=this;data=new Float32Array(n);front=0;end=0;full=!1;t.size=function(){return full?data.length:end>=front?end-front:data.length-front+end};t.available=function(){return data.length-t.size()};t.capacity=function(){return data.length};t.add=function(n){var i=0,r,u,f;if(n.length===0)return 0;for(r=Math.min(n.length,t.available()),u=Math.min(data.length-end,r),i=0;i<u;i++)data[end+i]=n[i]/32768;for(f=r-u,i=0;i<f;i++)data[i]=n[u+i]/32768;return end=(end+r)%data.length,end===front&&(full=!0),r};t.remove=function(n,i){var f=n.length/i,r=0,u=0,e=0,o=0,h=0,s,c;if(f===0||f>=t.size())return 0;for(s=Math.min(f,data.length-front),r=0;r<s;r++)for(e=data[front+r],o=data[(front+r+1)%data.length],u=0;u<i;u++)n[h++]=(i-u)/i*e+u/i*o;for(c=f-s,r=0;r<c;r++)for(e=data[r],o=data[r+1],u=0;u<i;u++)n[h++]=(i-u)/i*e+u/i*o;return front=(front+f)%data.length,full=!1,n.length}}function base64ArrayBuffer(n){for(var o="",i="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",r=new Uint8Array(n),l=r.length,c=l%3,s=l-c,u,f,h,a,t,e=0;e<s;e=e+3)t=r[e]<<16|r[e+1]<<8|r[e+2],u=(t&16515072)>>18,f=(t&258048)>>12,h=(t&4032)>>6,a=t&63,o+=i[u]+i[f]+i[h]+i[a];return c===1?(t=r[s],u=(t&252)>>2,f=(t&3)<<4,o+=i[u]+i[f]+"=="):c===2&&(t=r[s]<<8|r[s+1],u=(t&64512)>>10,f=(t&1008)>>4,h=(t&15)<<2,o+=i[u]+i[f]+i[h]+"="),o}function stopped(n){postMessage({event:"stopped",error:n});close()}function getAudioBlockLoop(){if(run){var n=new XMLHttpRequest;n.onreadystatechange=function(){var t,i,r,u;n.readyState===4&&(n.status===200?(t=n.response,save&&console.log("g4:"+JSON.stringify({event:"receivedData",data:base64ArrayBuffer(t)})),i=new Int16Array(t),r=bulkBuffer.size(),bulkBuffer.add(i),u=bulkBuffer.size(),postMessage({event:"bufferSizeChanged",size:bulkBuffer.size()/bulkBuffer.capacity()}),u>=idealBufferSampleSize&&r<idealBufferSampleSize&&postMessage({event:"midBuffer"}),run?setTimeout(getAudioBlockLoop,0):stopped(!1)):stopped(!0))};n.responseType="arraybuffer";n.open("GET","/sdk?func=getAudioStream",!0);n.send()}else stopped(!1)}var run=!1,sampleRate=8e3,effectiveRate=8e3,upScaler=1,bulkBuffer=new CircularBuffer(0),operationalUnit=0,UNIT_LENGTH=.025,idealBufferSampleSize=0,save=!1,MIN_SAMPLE_RATE=22050;onmessage=function(n){var i=n.data,r,t;i.event==="dataNeeded"?(r={event:"dataDequeued",data:new Float32Array(operationalUnit*upScaler),sampleRate:effectiveRate,bufferTime:UNIT_LENGTH},bulkBuffer.remove(r.data,upScaler),postMessage(r),bulkBuffer.size()<operationalUnit&&postMessage({event:"lowBuffer"}),postMessage({event:"bufferSizeChanged",size:bulkBuffer.size()/bulkBuffer.capacity(),sampleRate:sampleRate})):i.event==="startRequested"?(run=!0,save=i.save,t=new XMLHttpRequest,t.onreadystatechange=function(){if(t.readyState===4)if(t.status===200){var n=JSON.parse(t.responseText);if(n.ResultCode===0){for(sampleRate=n.SampleRate,operationalUnit=Math.round(sampleRate*UNIT_LENGTH),upScaler=1;upScaler*sampleRate<MIN_SAMPLE_RATE;)upScaler+=1;effectiveRate=upScaler*sampleRate;idealBufferSampleSize=i.bufferSize*sampleRate;bulkBuffer=new CircularBuffer(idealBufferSampleSize*2);postMessage({event:"started",sampleRate:sampleRate,channels:1,bitDepth:16});getAudioBlockLoop();return}stopped(!0)}else stopped(!0)},t.open("GET","/sdk?func=startAudioStream",!0),t.send()):i.event==="stopRequested"&&(run=!1)};