recorder.wav.min.js 24 KB

123456
  1. /*
  2. 录音
  3. https://github.com/xiangyuecn/Recorder
  4. src: recorder-core.js,engine/wav.js
  5. */
  6. !function(e){var t="object"==typeof window&&!!window.document,r=t?window:Object;!function(e,w){"use strict";var y=function(){},u=function(e){return"number"==typeof e},F=function(e){return new f(e)},b=F.LM="2024-04-09 19:15",S="https://github.com/xiangyuecn/Recorder",k="Recorder",x="getUserMedia",U="srcSampleRate",G="sampleRate",i="bitRate",I="catch",t=e[k];if(t&&t.LM==b)return t.CLog(t.i18n.$T("K8zP::重复导入{1}",0,k),3);F.IsOpen=function(){var e=F.Stream;if(e){var t=e.getTracks&&e.getTracks()||e.audioTracks||[],r=t[0];if(r){var n=r.readyState;return"live"==n||n==r.LIVE}}return!1},F.BufferSize=4096,F.Destroy=function(){for(var e in N(k+" Destroy"),D(),r)r[e]()};var r={};F.BindDestroy=function(e,t){r[e]=t},F.Support=function(){if(!w)return!1;var e=navigator.mediaDevices||{};return e[x]||(e=navigator)[x]||(e[x]=e.webkitGetUserMedia||e.mozGetUserMedia||e.msGetUserMedia),!!e[x]&&(F.Scope=e,!!F.GetContext())},F.GetContext=function(e){if(!w)return null;var t=window.AudioContext;if(t||(t=window.webkitAudioContext),!t)return null;var r=F.Ctx;if(r&&"closed"!=r.state||(r=F.Ctx=new t,F.NewCtxs=F.NewCtxs||[],F.BindDestroy("Ctx",function(){var e=F.Ctx;e&&e.close&&(a(e),F.Ctx=0);var t=F.NewCtxs;F.NewCtxs=[];for(var r=0;r<t.length;r++)a(t[r])})),e&&r.close)try{r=new t,F.NewCtxs.push(r)}catch(e){N("GetContext tryNew Error",1,e)}return r},F.CloseNewCtx=function(e){if(e&&e!=F.Ctx){a(e);for(var t=F.NewCtxs||[],r=t.length,n=0;n<t.length;n++)if(t[n]==e){t.splice(n,1);break}N(V("mSxV::剩{1}个GetContext未close",0,r+"-1="+t.length),t.length?3:0)}};var a=function(e){if(e&&e.close){e._isC=1;try{e.close()}catch(e){N("ctx close err",1,e)}}},L=F.ResumeCtx=function(r,n,a,o){var i=0,s=0,c=0,f=0,u="EventListener",l="ResumeCtx ",v=function(e,t){s&&p(),i||(i=1,e&&o(e,f),t&&a(f)),t&&(!r._LsSC&&r["add"+u]&&r["add"+u]("statechange",h),r._LsSC=1,c=1)},p=function(e){if(!e||!s){s=e?1:0;for(var t=["focus","mousedown","mouseup","touchstart","touchend"],r=0;r<t.length;r++)window[(e?"add":"remove")+u](t[r],h,!0)}},h=function(){var e=r.state,t=g(e);if(!i&&!n(t?++f:f))return v();t?(c&&N(l+"sc "+e,3),p(1),r.resume().then(function(){c&&N(l+"sc "+r.state),v(0,1)})[I](function(e){N(l+"error",1,e),g(r.state)||v(e.message||"error")})):"closed"==e?(c&&!r._isC&&N(l+"sc "+e,1),v("ctx closed")):v(0,1)};h()},g=F.CtxSpEnd=function(e){return"suspended"==e||"interrupted"==e},M=function(e){var t=e.state,r="ctx.state="+t;return g(t)&&(r+=V("nMIy::(注意:ctx不是running状态,rec.open和start至少要有一个在用户操作(触摸、点击等)时进行调用,否则将在rec.start时尝试进行ctx.resume,可能会产生兼容性问题(仅iOS),请参阅文档中runningContext配置)")),r},T="ConnectEnableWebM";F[T]=!0;var A="ConnectEnableWorklet";F[A]=!1;var R=function(e,c){var f=e.BufferSize||F.BufferSize,u=e.Stream,l=u._RC||u._c||F.GetContext(!0);u._c=l;var v,r,p,i=function(e){var t=u._m=l.createMediaStreamSource(u),r=l.destination,n="createMediaStreamDestination";l[n]&&(r=u._d=l[n]()),t.connect(e),e.connect(r)},h="",g=u._call,d=function(e){for(var t in g){for(var r=e.length,n=new Int16Array(r),a=0,o=0;o<r;o++){var i=Math.max(-1,Math.min(1,e[o]));i=i<0?32768*i:32767*i,n[o]=i,a+=Math.abs(i)}for(var s in g)g[s](n,a);return}},m="ScriptProcessor",C="audioWorklet",s=k+" "+C,_="RecProc",w="MediaRecorder",y=w+".WebM.PCM",b=l.createScriptProcessor||l.createJavaScriptNode,S=V("ZGlf::。由于{1}内部1秒375次回调,在移动端可能会有性能问题导致回调丢失录音变短,PC端无影响,暂不建议开启{1}。",0,C),x=function(){r=u.isWorklet=!1,n(u),N(V("7TU0::Connect采用老的{1},",0,m)+$.get(V(F[A]?"JwCL::但已设置{1}尝试启用{2}":"VGjB::可设置{1}尝试启用{2}",2),[k+"."+A+"=true",C])+h+S,3);var e=u._p=b.call(l,f,1,1);i(e),e.onaudioprocess=function(e){var t=e.inputBuffer.getChannelData(0);d(t)}},M=function(){v=u.isWebM=!1,O(u),r=u.isWorklet=!b||F[A];var t=window.AudioWorkletNode;if(r&&l[C]&&t){var n=function(){return r&&u._na},a=u._na=function(){""!==p&&(clearTimeout(p),p=setTimeout(function(){p=0,n()&&(N(V("MxX1::{1}未返回任何音频,恢复使用{2}",0,C,m),3),b&&x())},500))},o=function(){if(n()){var e=u._n=new t(l,_,{processorOptions:{bufferSize:f}});i(e),e.port.onmessage=function(e){p&&(clearTimeout(p),p=""),n()?d(e.data.val):r||N(V("XUap::{1}多余回调",0,C),3)},N(V("yOta::Connect采用{1},设置{2}可恢复老式{3}",0,C,k+"."+A+"=false",m)+h+S,3)}},e=function(){if(n())if(l[_])o();else{var e,t,r=(t="class "+_+" extends AudioWorkletProcessor{",t+="constructor "+(e=function(e){return e.toString().replace(/^function|DEL_/g,"").replace(/\$RA/g,s)})(function(e){DEL_super(e);var t=this,r=e.processorOptions.bufferSize;t.bufferSize=r,t.buffer=new Float32Array(2*r),t.pos=0,t.port.onmessage=function(e){e.data.kill&&(t.kill=!0,$C.log("$RA kill call"))},$C.log("$RA .ctor call",e)}),t+="process "+e(function(e,t,r){var n=this,a=n.bufferSize,o=n.buffer,i=n.pos;if((e=(e[0]||[])[0]||[]).length){o.set(e,i);var s=~~((i+=e.length)/a)*a;if(s){this.port.postMessage({val:o.slice(0,s)});var c=o.subarray(s,i);(o=new Float32Array(2*a)).set(c),i=c.length,n.buffer=o}n.pos=i}return!n.kill}),t=(t+='}try{registerProcessor("'+_+'", '+_+')}catch(e){$C.error("'+s+' Reg Error",e)}').replace(/\$C\./g,"console."),"data:text/javascript;base64,"+btoa(unescape(encodeURIComponent(t))));l[C].addModule(r).then(function(e){n()&&(l[_]=1,o(),p&&a())})[I](function(e){N(C+".addModule Error",1,e),n()&&x()})}};L(l,function(){return n()},e,e)}else x()};!function(){var e=window[w],t="ondataavailable",r="audio/webm; codecs=pcm";v=u.isWebM=F[T];var n=e&&t in e.prototype&&e.isTypeSupported(r);if(h=n?"":V("VwPd::(此浏览器不支持{1})",0,y),!c||!v||!n)return M();var a=function(){return v&&u._ra},o=(u._ra=function(){""!==p&&(clearTimeout(p),p=setTimeout(function(){a()&&(N(V("vHnb::{1}未返回任何音频,降级使用{2}",0,w,C),3),M())},500))},Object.assign({mimeType:r},F.ConnectWebMOptions)),i=u._r=new e(u,o),s=u._rd={sampleRate:l[G]};i[t]=function(e){var t=new FileReader;t.onloadend=function(){if(a()){var e=z(new Uint8Array(t.result),s);if(!e)return;if(-1==e)return void M();p&&(clearTimeout(p),p=""),d(e)}else v||N(V("O9P7::{1}多余回调",0,w),3)},t.readAsArrayBuffer(e.data)},i.start(~~(f/48)),N(V("LMEm::Connect采用{1},设置{2}可恢复使用{3}或老式{4}",0,y,k+"."+T+"=false",C,m))}()},o=function(e){e._na&&e._na(),e._ra&&e._ra()},n=function(e){e._na=null,e._n&&(e._n.port.postMessage({kill:!0}),e._n.disconnect(),e._n=null)},O=function(e){if(e._ra=null,e._r){try{e._r.stop()}catch(e){N("mr stop err",1,e)}e._r=null}},D=function(e){var t=(e=e||F)==F,r=e.Stream;r&&(r._m&&(r._m.disconnect(),r._m=null),!r._RC&&r._c&&F.CloseNewCtx(r._c),r._RC=null,r._c=null,r._d&&(s(r._d.stream),r._d=null),r._p&&(r._p.disconnect(),r._p.onaudioprocess=r._p=null),n(r),O(r),t&&s(r)),e.Stream=0},s=F.StopS_=function(e){for(var t=e.getTracks&&e.getTracks()||e.audioTracks||[],r=0;r<t.length;r++){var n=t[r];n.stop&&n.stop()}e.stop&&e.stop()};F.SampleData=function(e,t,r,n,a){var o="SampleData";n||(n={});var i=n.index||0,s=n.offset||0,c=n.filter;if(c&&c.fn&&c.sr!=t&&(c=null,N(V("d48C::{1}的filter采样率变了,重设滤波",0,o),3)),!c){var f=3*t/4<r?0:r/2*3/4;c={fn:f?F.IIRFilter(!0,t,f):0}}c.sr=t;var u=c.fn,l=n.frameNext||[];a||(a={});var v=a.frameSize||1;a.frameType&&(v="mp3"==a.frameType?1152:1);var p=e.length;p+1<i&&N(V("tlbC::{1}似乎传入了未重置chunk {2}",0,o,i+">"+p),3);for(var h=0,g=i;g<p;g++)h+=e[g].length;h=Math.max(0,h-Math.floor(s));var d=t/r;1<d?h=Math.floor(h/d):(d=1,r=t),h+=l.length;for(var m=new Int16Array(h),C=0,g=0;g<l.length;g++)m[C]=l[g],C++;for(;i<p;i++){for(var _=e[i],g=s,w=_.length,y=u&&u.Embed,b=0,S=0,x=0,M=0,k=0,I=0;k<w;k++,I++)if(I<w&&(y?(x=_[I],M=y.b0*x+y.b1*y.x1+y.b0*y.x2-y.a1*y.y1-y.a2*y.y2,y.x2=y.x1,y.x1=x,y.y2=y.y1,y.y1=M):M=u?u(_[I]):_[I]),b=S,S=M,0!=I){var L=Math.floor(g);if(k==L){var T=Math.ceil(g),A=g-L,R=b,O=T<w?S:R,D=R+(O-R)*A;32767<D?D=32767:D<-32768&&(D=-32768),m[C]=D,C++,g+=d}}else k--;s=Math.max(0,g-w)}l=null;var z=m.length%v;if(0<z){var E=2*(m.length-z);l=new Int16Array(m.buffer.slice(E)),m=new Int16Array(m.buffer.slice(0,E))}return{index:i,offset:s,filter:c,frameNext:l,sampleRate:r,data:m}},F.IIRFilter=function(e,t,r){var n=2*Math.PI*r/t,a=Math.sin(n),o=Math.cos(n),i=a/2,s=1+i,c=-2*o/s,f=(1-i)/s;if(e)var u=(1-o)/2/s,l=(1-o)/s;else var u=(1+o)/2/s,l=-(1+o)/s;var v=0,p=0,h=0,g=0,d=0,m=function(e){return h=u*e+l*v+u*p-c*g-f*d,p=v,v=e,d=g,g=h};return m.Embed={x1:0,x2:0,y1:0,y2:0,b0:u,b1:l,a1:c,a2:f},m},F.PowerLevel=function(e,t){var r=e/t||0;return r<1251?Math.round(r/1250*10):Math.round(Math.min(100,Math.max(0,100*(1+Math.log(r/1e4)/Math.log(10)))))},F.PowerDBFS=function(e){var t=Math.max(.1,e||0);return t=Math.min(t,32767),t=20*Math.log(t/32767)/Math.log(10),Math.max(-100,Math.round(t))},F.CLog=function(e,t){if("object"==typeof console){var r=new Date,n=("0"+r.getMinutes()).substr(-2)+":"+("0"+r.getSeconds()).substr(-2)+"."+("00"+r.getMilliseconds()).substr(-3),a=this&&this.envIn&&this.envCheck&&this.id,o=["["+n+" "+k+(a?":"+a:"")+"]"+e],i=arguments,s=F.CLog,c=2,f=s.log||console.log;for(u(t)?f=1==t?s.error||console.error:3==t?s.warn||console.warn:f:c=1;c<i.length;c++)o.push(i[c]);l?f&&f("[IsLoser]"+o[0],1<o.length?o:""):f.apply(console,o)}};var N=function(){F.CLog.apply(this,arguments)},l=!0;try{l=!console.log.apply}catch(e){}var c=0;function f(e){var t=this;t.id=++c,v();var r={type:"mp3",onProcess:y};for(var n in e)r[n]=e[n];var a=(t.set=r)[i],o=r[G];(a&&!u(a)||o&&!u(o))&&t.CLog(V.G("IllegalArgs-1",[V("VtS4::{1}和{2}必须是数值",0,G,i)]),1,e),r[i]=+a||16,r[G]=+o||16e3,t.state=0,t._S=9,t.Sync={O:9,C:9}}F.Sync={O:9,C:9},F.prototype=f.prototype={CLog:N,_streamStore:function(){return this.set.sourceStream?this:F},_streamCtx:function(){var e=this._streamStore().Stream;return e&&e._c},open:function(e,r){var n=this,a=n.set,o=n._streamStore(),i=0;e=e||y;var s=function(e,t){t=!!t,n.CLog(V("5tWi::录音open失败:")+e+",isUserNotAllow:"+t,1),i&&F.CloseNewCtx(i),r&&r(e,t)};n._streamTag=x;var c=function(){n.CLog("open ok, id:"+n.id+" stream:"+n._streamTag),e(),n._SO=0},f=o.Sync,u=++f.O,l=f.C;n._O=n._O_=u,n._SO=n._S;if(w){var t=n.envCheck({envName:"H5",canProcess:!0});if(t)s(V("A5bm::不能录音:")+t);else if(a.sourceStream){if(n._streamTag="set.sourceStream",!F.GetContext())return void s(V("1iU7::不支持此浏览器从流中获取录音"));D(o);var v=n.Stream=a.sourceStream;v._RC=a.runningContext,v._call={};try{R(o)}catch(e){return D(o),void s(V("BTW2::从流中打开录音失败:")+e.message)}c()}else{var p=function(e,t){try{window.top.a}catch(e){return void s(V("Nclz::无权录音(跨域,请尝试给iframe添加麦克风访问策略,如{1})",0,'allow="camera;microphone"'))}/Permission|Allow/i.test(e)?s(V("gyO5::用户拒绝了录音权限"),!0):!1===window.isSecureContext?s(V("oWNo::浏览器禁止不安全页面录音,可开启https解决")):/Found/i.test(e)?s(t+V("jBa9::,无可用麦克风")):s(t)};if(F.IsOpen())c();else if(F.Support()){var h=a.runningContext;h||(h=i=F.GetContext(!0));var g=function(t){setTimeout(function(){t._call={};var e=F.Stream;e&&(D(),t._call=e._call),(F.Stream=t)._c=h,t._RC=a.runningContext,function(){if(l!=f.C||!n._O){var e=V("dFm8::open被取消");return u==f.O?n.close():e=V("VtJO::open被中断"),s(e),!0}}()||(F.IsOpen()?(e&&n.CLog(V("upb8::发现同时多次调用open"),1),R(o,1),c()):s(V("Q1GA::录音功能无效:无音频流")))},100)},d=function(e){var t=e.name||e.message||e.code+":"+e;n.CLog(V("xEQR::请求录音权限错误"),1,e),p(t,V("bDOG::无法录音:")+t)},m=a.audioTrackSet||{};m[G]=h[G];var C={audio:m};try{var _=F.Scope[x](C,g,d)}catch(e){n.CLog(x,3,e),C={audio:!0},_=F.Scope[x](C,g,d)}n.CLog(x+"("+JSON.stringify(C)+") "+M(h)+V("RiWe::,未配置noiseSuppression和echoCancellation时浏览器可能会自动打开降噪和回声消除,移动端可能会降低系统播放音量(关闭录音后可恢复),请参阅文档中audioTrackSet配置")+"("+S+") LM:"+b+" UA:"+navigator.userAgent),_&&_.then&&_.then(g)[I](d)}else p("",V("COxc::此浏览器不支持录音"))}}else s(V.G("NonBrowser-1",["open"])+V("EMJq::,可尝试使用RecordApp解决方案")+"("+S+"/tree/master/app-support-sample)")},close:function(e){e=e||y;var t=this,r=t._streamStore();t._stop();var n=" stream:"+t._streamTag,a=r.Sync;if(t._O=0,t._O_!=a.O)return t.CLog(V("hWVz::close被忽略(因为同时open了多个rec,只有最后一个会真正close)")+n,3),void e();a.C++,D(r),t.CLog("close,"+n),e()},mock:function(e,t){var r=this;return r._stop(),r.isMock=1,r.mockEnvInfo=null,r.buffers=[e],r.recSize=e.length,r._setSrcSR(t),r._streamTag="mock",r},_setSrcSR:function(e){var t=this.set,r=t[G];e<r?t[G]=e:r=0,this[U]=e,this.CLog(U+": "+e+" set."+G+": "+t[G]+(r?" "+V("UHvm::忽略")+": "+r:""),r?3:0)},envCheck:function(e){var t,r=this.set,n="CPU_BE";if(t||F[n]||"function"!=typeof Int8Array||new Int8Array(new Int32Array([1]).buffer)[0]||(v(n),t=V("Essp::不支持{1}架构",0,n)),!t){var a=r.type,o=this[a+"_envCheck"];r.takeoffEncodeChunk&&(o?e.canProcess||(t=V("7uMV::{1}环境不支持实时处理",0,e.envName)):t=V("2XBl::{1}类型不支持设置takeoffEncodeChunk",0,a)+(this[a]?"":V("LG7e::(未加载编码器)"))),!t&&o&&(t=this[a+"_envCheck"](e,r))}return t||""},envStart:function(e,t){var r=this,n=r.set;if(r.isMock=e?1:0,r.mockEnvInfo=e,r.buffers=[],r.recSize=0,e&&(r._streamTag="env$"+e.envName),r.state=1,r.envInLast=0,r.envInFirst=0,r.envInFix=0,r.envInFixTs=[],r._setSrcSR(t),r.engineCtx=0,r[n.type+"_start"]){var a=r.engineCtx=r[n.type+"_start"](n);a&&(a.pcmDatas=[],a.pcmSize=0)}},envResume:function(){this.envInFixTs=[]},envIn:function(e,t){var a=this,o=a.set,i=a.engineCtx;if(1==a.state){var r=a[U],n=e.length,s=F.PowerLevel(t,n),c=a.buffers,f=c.length;c.push(e);var u=c,l=f,v=Date.now(),p=Math.round(n/r*1e3);a.envInLast=v,1==a.buffers.length&&(a.envInFirst=v-p);var h=a.envInFixTs;h.splice(0,0,{t:v,d:p});for(var g=v,d=0,m=0;m<h.length;m++){var C=h[m];if(3e3<v-C.t){h.length=m;break}g=C.t,d+=C.d}var _=h[1],w=v-g,y=w-d;if(w/3<y&&(_&&1e3<w||6<=h.length)){var b=v-_.t-p;if(p/5<b){var S=!o.disableEnvInFix;if(a.CLog("["+v+"]"+$.get(V(S?"4Kfd::补偿{1}ms":"bM5i::未补偿{1}ms",1),[b]),3),a.envInFix+=b,S){var x=new Int16Array(b*r/1e3);n+=x.length,c.push(x)}}}var M=a.recSize,k=n,I=M+k;if(a.recSize=I,i){var L=F.SampleData(c,r,o[G],i.chunkInfo);i.chunkInfo=L,M=i.pcmSize,k=L.data.length,I=M+k,i.pcmSize=I,c=i.pcmDatas,f=c.length,c.push(L.data),r=L[G]}var T=Math.round(I/r*1e3),A=c.length,R=u.length,O=function(){for(var e=D?0:-k,t=null==c[0],r=f;r<A;r++){var n=c[r];null==n?t=1:(e+=n.length,i&&n.length&&a[o.type+"_encode"](i,n))}if(t&&i){var r=l;for(u[0]&&(r=0);r<R;r++)u[r]=null}t&&(e=D?k:0,c[0]=null),i?i.pcmSize+=e:a.recSize+=e},D=0,z="rec.set.onProcess";try{D=o.onProcess(c,s,T,r,f,O)}catch(e){console.error(z+V("gFUF::回调出错是不允许的,需保证不会抛异常"),e)}var E=Date.now()-v;if(10<E&&1e3<a.envInFirst-v&&a.CLog(z+V("2ghS::低性能,耗时{1}ms",0,E),3),!0===D){for(var N=0,m=f;m<A;m++)null==c[m]?N=1:c[m]=new Int16Array(0);N?a.CLog(V("ufqH::未进入异步前不能清除buffers"),3):i?i.pcmSize-=k:a.recSize-=k}else O()}else a.state||a.CLog("envIn at state=0",3)},start:function(){var t=this,e=1;if(t.set.sourceStream?t.Stream||(e=0):F.IsOpen()||(e=0),e){var r=t._streamCtx();if(t.CLog(V("kLDN::start 开始录音,")+M(r)+" stream:"+t._streamTag),t._stop(),t.envStart(null,r[G]),t.state=3,t._SO&&t._SO+1!=t._S)t.CLog(V("Bp2y::start被中断"),3);else{t._SO=0;var n=function(){3==t.state&&(t.state=1,t.resume())},a="AudioContext resume: ";L(r,function(e){return e&&t.CLog(a+"wait..."),3==t.state},function(e){e&&t.CLog(a+r.state),n()},function(e){t.CLog(a+r.state+V("upkE::,可能无法录音:")+e,1),n()})}}else t.CLog(V("6WmN::start失败:未open"),1)},pause:function(){var e=this,t=e._streamStore().Stream;e.state&&(e.state=2,e.CLog("pause"),t&&delete t._call[e.id])},resume:function(){var r=this,t=r._streamStore().Stream,n="resume(wait ctx)";if(3==r.state)r.CLog(n);else if(r.state){r.state=1,r.CLog("resume"),r.envResume(),t&&(t._call[r.id]=function(e,t){1==r.state&&r.envIn(e,t)},o(t));var a=r._streamCtx();a&&L(a,function(e){return e&&r.CLog(n+"..."),1==r.state},function(e){e&&r.CLog(n+a.state),o(t)},function(e){r.CLog(n+a.state+"[err]"+e,1)})}},_stop:function(e){var t=this,r=t.set;t.isMock||t._S++,t.state&&(t.pause(),t.state=0),!e&&t[r.type+"_stop"]&&(t[r.type+"_stop"](t.engineCtx),t.engineCtx=0)},stop:function(l,t,e){var v,p=this,h=p.set,r=p.envInLast-p.envInFirst,n=r&&p.buffers.length;p.CLog(V("Xq4s::stop 和start时差:")+(r?r+"ms "+V("3CQP::补偿:")+p.envInFix+"ms envIn:"+n+" fps:"+(n/r*1e3).toFixed(1):"-")+" stream:"+p._streamTag+" ("+S+") LM:"+b);var g=function(){p._stop(),e&&p.close()},d=function(e){p.CLog(V("u8JG::结束录音失败:")+e,1),t&&t(e),g()},a=function(e,t,r){var n="arraybuffer",a="dataType",o="DefaultDataType",i=p[a]||F[o]||"blob",s=a+"="+i,c=e instanceof ArrayBuffer,f=0,u=c?e.byteLength:e.size;if(i==n?c||(f=1):"blob"==i?"function"!=typeof Blob?f=V.G("NonBrowser-1",[s])+V("1skY::,请设置{1}",0,k+"."+o+'="'+n+'"'):(c&&(e=new Blob([e],{type:t})),e instanceof Blob||(f=1),t=e.type||t):f=V.G("NotSupport-1",[s]),p.CLog(V("Wv7l::结束录音 编码花{1}ms 音频时长{2}ms 文件大小{3}b",0,Date.now()-v,r,u)+" "+s+","+t),f)d(1!=f?f:V("Vkbd::{1}编码器返回的不是{2}",0,h.type,i)+", "+s);else{if(h.takeoffEncodeChunk)p.CLog(V("QWnr::启用takeoffEncodeChunk后stop返回的blob长度为0不提供音频数据"),3);else if(u<Math.max(50,r/5))return void d(V("Sz2H::生成的{1}无效",0,h.type));l&&l(e,r,t),g()}};if(!p.isMock){var o=3==p.state;if(!p.state||o)return void d(V("wf9t::未开始录音")+(o?V("Dl2c::,开始录音前无用户交互导致AudioContext未运行"):""))}p._stop(!0);var i=p.recSize;if(i)if(p[h.type]){if(p.isMock){var s=p.envCheck(p.mockEnvInfo||{envName:"mock",canProcess:!1});if(s)return void d(V("AxOH::录音错误:")+s)}var c=p.engineCtx;if(p[h.type+"_complete"]&&c){var f=Math.round(c.pcmSize/h[G]*1e3);return v=Date.now(),void p[h.type+"_complete"](c,function(e,t){a(e,t,f)},d)}if(v=Date.now(),p.buffers[0]){var u=F.SampleData(p.buffers,p[U],h[G]);h[G]=u[G];var m=u.data,f=Math.round(m.length/h[G]*1e3);p.CLog(V("CxeT::采样:{1} 花:{2}ms",0,i+"->"+m.length,Date.now()-v)),setTimeout(function(){v=Date.now(),p[h.type](m,function(e,t){a(e,t,f)},function(e){d(e)})})}else d(V("xkKd::音频buffers被释放"))}else d(V("xGuI::未加载{1}编码器,请尝试到{2}的src/engine内找到{1}的编码器并加载",0,h.type,k));else d(V("Ltz3::未采集到录音"))}};var z=function(e,t){t.pos||(t.pos=[0],t.tracks={},t.bytes=[]);var r=t.tracks,n=[t.pos[0]],a=function(){t.pos[0]=n[0]},o=t.bytes.length,i=new Uint8Array(o+e.length);if(i.set(t.bytes),i.set(e,o),t.bytes=i,!t._ht){if(P(i,n),B(i,n),!E(P(i,n),[24,83,128,103]))return;for(P(i,n);n[0]<i.length;){var s=P(i,n),c=B(i,n),f=[0],u=0;if(!c)return;if(E(s,[22,84,174,107])){for(;f[0]<c.length;){var l=P(c,f),v=B(c,f),p=[0],h={channels:0,sampleRate:0};if(E(l,[174]))for(;p[0]<v.length;){var g=P(v,p),d=B(v,p),m=[0];if(E(g,[215])){var C=W(d);h.number=C,r[C]=h}else if(E(g,[131])){var C=W(d);1==C?h.type="video":2==C?(h.type="audio",u||(t.track0=h),h.idx=u++):h.type="Type-"+C}else if(E(g,[134])){for(var _="",w=0;w<d.length;w++)_+=String.fromCharCode(d[w]);h.codec=_}else if(E(g,[225]))for(;m[0]<d.length;){var y=P(d,m),b=B(d,m);if(E(y,[181])){var C=0,S=new Uint8Array(b.reverse()).buffer;4==b.length?C=new Float32Array(S)[0]:8==b.length?C=new Float64Array(S)[0]:N("WebM Track !Float",1,b),h[G]=Math.round(C)}else E(y,[98,100])?h.bitDepth=W(b):E(y,[159])&&(h.channels=W(b))}}}t._ht=1,N("WebM Tracks",r),a();break}}}var x=t.track0;if(x){if(16==x.bitDepth&&/FLOAT/i.test(x.codec)&&(x.bitDepth=32,N("WebM 16->32 bit",3)),x[G]!=t[G]||32!=x.bitDepth||x.channels<1||!/(\b|_)PCM\b/i.test(x.codec))return t.bytes=[],t.bad||N("WebM Track Unexpected",3,t),-(t.bad=1);for(var M=[],k=0;n[0]<i.length;){var l=P(i,n),v=B(i,n);if(!v)break;if(E(l,[163])){var I=15&v[0],h=r[I];if(h){if(0===h.idx){for(var L=new Uint8Array(v.length-4),w=4;w<v.length;w++)L[w-4]=v[w];M.push(L),k+=L.length}}else N("WebM !Track"+I,1,r)}a()}if(k){var T=new Uint8Array(i.length-t.pos[0]);T.set(i.subarray(t.pos[0])),t.bytes=T,t.pos[0]=0;for(var L=new Uint8Array(k),w=0,A=0;w<M.length;w++)L.set(M[w],A),A+=M[w].length;var S=new Float32Array(L.buffer);if(1<x.channels){for(var R=[],w=0;w<S.length;)R.push(S[w]),w+=x.channels;S=new Float32Array(R)}return S}}},E=function(e,t){if(!e||e.length!=t.length)return!1;if(1==e.length)return e[0]==t[0];for(var r=0;r<e.length;r++)if(e[r]!=t[r])return!1;return!0},W=function(e){for(var t="",r=0;r<e.length;r++){var n=e[r];t+=(n<16?"0":"")+n.toString(16)}return parseInt(t,16)||0},P=function(e,t,r){var n=t[0];if(!(n>=e.length)){var a=e[n],o=("0000000"+a.toString(2)).substr(-8),i=/^(0*1)(\d*)$/.exec(o);if(i){var s=i[1].length,c=[];if(!(n+s>e.length)){for(var f=0;f<s;f++)c[f]=e[n],n++;return r&&(c[0]=parseInt(i[2]||"0",2)),t[0]=n,c}}}},B=function(e,t){var r=P(e,t,1);if(r){var n=W(r),a=t[0],o=[];if(n<2147483647){if(a+n>e.length)return;for(var i=0;i<n;i++)o[i]=e[a],a++}return t[0]=a,o}},$=F.i18n={lang:"zh-CN",alias:{"zh-CN":"zh","en-US":"en"},locales:{},data:{},put:function(e,t){var r=k+".i18n.put: ",n=e.overwrite;n=null==n||n;var a=e.lang;if(!(a=$.alias[a]||a))throw new Error(r+"set.lang?");var o=$.locales[a];o||(o={},$.locales[a]=o);for(var i,s=/^([\w\-]+):/,c=0;c<t.length;c++){var f=t[c];if(i=s.exec(f)){var u=i[1],f=f.substr(u.length+1);!n&&o[u]||(o[u]=f)}else N(r+"'key:'? "+f,3,e)}},get:function(){return $.v_G.apply(null,arguments)},v_G:function(n,a,e){a=a||[],e=e||$.lang,e=$.alias[e]||e;var t=$.locales[e],o=t&&t[n]||"";return o||"zh"==e?($.lastLang=e,"=Empty"==o?"":o.replace(/\{(\d+)(\!?)\}/g,function(e,t,r){return e=a[(t=+t||0)-1],(t<1||t>a.length)&&(e="{?}",N("i18n["+n+"] no {"+t+"}: "+o,3)),r?"":e})):"en"==e?$.v_G(n,a,"zh"):$.v_G(n,a,"en")},$T:function(){return $.v_T.apply(null,arguments)},v_T:function(){for(var e,t=arguments,r="",n=[],a=0,o=k+".i18n.$T:",i=/^([\w\-]*):/,s=0;s<t.length;s++){var c=t[s];if(0==s){if(e=i.exec(c),!(r=e&&e[1]))throw new Error(o+"0 'key:'?");c=c.substr(r.length+1)}if(-1===a)n.push(c);else{if(a)throw new Error(o+" bad args");if(0===c)a=-1;else if(u(c)){if(c<1)throw new Error(o+" bad args");a=c}else{var f=1==s?"en":s?"":"zh";if((e=i.exec(c))&&(f=e[1]||f,c=c.substr(e[1].length+1)),!e||!f)throw new Error(o+s+" 'lang:'?");$.put({lang:f,overwrite:!1},[r+":"+c])}}}return r?0<a?r:$.v_G(r,n):""}},V=$.$T;V.G=$.get,V("NonBrowser-1::非浏览器环境,不支持{1}",1),V("IllegalArgs-1::参数错误:{1}",1),V("NeedImport-2::调用{1}需要先导入{2}",2),V("NotSupport-1::不支持:{1}",1),F.TrafficImgUrl="//ia.51.la/go1?id=20469973&pvFlag=1";var v=F.Traffic=function(e){if(w){e=e?"/"+k+"/Report/"+e:"";var t=F.TrafficImgUrl;if(t){var r=F.Traffic,n=/^(https?:..[^\/#]*\/?)[^#]*/i.exec(location.href)||[],a=n[1]||"http://file/",o=(n[0]||a)+e;if(0==t.indexOf("//")&&(t=/^https:/i.test(o)?"https:"+t:"http:"+t),e&&(t=t+"&cu="+encodeURIComponent(a+e)),!r[o]){r[o]=1;var i=new Image;i.src=t,N("Traffic Analysis Image: "+(e||k+".TrafficImgUrl="+F.TrafficImgUrl))}}}};t&&(N(V("8HO5::覆盖导入{1}",0,k),1),t.Destroy());e[k]=F}(r,t),"function"==typeof define&&define.amd&&define(function(){return r.Recorder}),"object"==typeof module&&module.exports&&(module.exports=r.Recorder)}(),function(e){var t="object"==typeof window&&!!window.document,r=(t?window:Object).Recorder,n=r.i18n;!function(p,e,h,t){"use strict";p.prototype.enc_wav={stable:!0,fast:!0,getTestMsg:function(){return h("gPSE::支持位数8位、16位(填在比特率里面),采样率取值无限制;此编码器仅在pcm数据前加了一个44字节的wav头,编码出来的16位wav文件去掉开头的44字节即可得到pcm(注:其他wav编码器可能不是44字节)")}};p.prototype.wav=function(e,t,r){var n=this.set;!function(e){var t=e.bitRate,r=8==t?8:16;t!=r&&p.CLog(h("wyw9::WAV Info: 不支持{1}位,已更新成{2}位",0,t,r),3);e.bitRate=r}(n);var a=e.length,o=n.sampleRate,i=n.bitRate,s=a*(i/8),c=p.wav_header(1,1,o,i,s),f=c.length,u=new Uint8Array(f+s);if(u.set(c),8==i)for(var l=0;l<a;l++){var v=128+(e[l]>>8);u[f++]=v}else(u=new Int16Array(u.buffer)).set(e,f/2);t(u.buffer,"audio/wav")},p.wav_header=function(e,t,r,n,a){var o=1==e?0:2,i=new ArrayBuffer(44+o),s=new DataView(i),c=0,f=function(e){for(var t=0;t<e.length;t++,c++)s.setUint8(c,e.charCodeAt(t))},u=function(e){s.setUint16(c,e,!0),c+=2},l=function(e){s.setUint32(c,e,!0),c+=4};return f("RIFF"),l(36+o+a),f("WAVE"),f("fmt "),l(16+o),u(e),u(t),l(r),l(r*(t*n/8)),u(t*n/8),u(n),1!=e&&u(0),f("data"),l(a),new Uint8Array(i)}}(r,0,n.$T)}();