forked from xiangyuecn/Recorder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
recorder.wav.min.js
6 lines (6 loc) · 8.68 KB
/
recorder.wav.min.js
1
2
3
4
5
6
/*
录音
https://github.com/xiangyuecn/Recorder
src: recorder-core.js,engine/wav.js
*/
!function(p){"use strict";var m=function(){},F=function(e){return new t(e)};F.IsOpen=function(){var e=F.Stream;if(e){var t=e.getTracks&&e.getTracks()||e.audioTracks||[],n=t[0];if(n){var a=n.readyState;return"live"==a||a==n.LIVE}}return!1},F.BufferSize=4096,F.Destroy=function(){for(var e in console.log("Recorder Destroy"),n)n[e]()};var n={};F.BindDestroy=function(e,t){n[e]=t},F.Support=function(){var e=p.AudioContext;if(e||(e=p.webkitAudioContext),!e)return!1;var t=navigator.mediaDevices||{};return t.getUserMedia||(t=navigator).getUserMedia||(t.getUserMedia=t.webkitGetUserMedia||t.mozGetUserMedia||t.msGetUserMedia),!!t.getUserMedia&&(F.Scope=t,F.Ctx&&"closed"!=F.Ctx.state||(F.Ctx=new e,F.BindDestroy("Ctx",function(){var e=F.Ctx;e&&e.close&&e.close()})),!0)},F.SampleData=function(e,t,n,a,r){a||(a={});var o=a.index||0,i=a.offset||0,s=a.frameNext||[];r||(r={});var c=r.frameSize||1;r.frameType&&(c="mp3"==r.frameType?1152:1);for(var f=0,l=o;l<e.length;l++)f+=e[l].length;f=Math.max(0,f-Math.floor(i));var u=t/n;1<u?f=Math.floor(f/u):(u=1,n=t),f+=s.length;for(var v=new Int16Array(f),p=0,l=0;l<s.length;l++)v[p]=s[l],p++;for(var m=e.length;o<m;o++){for(var h=e[o],l=i,d=h.length;l<d;){var g=Math.floor(l),S=Math.ceil(l),_=l-g;v[p]=h[g]+(h[S]-h[g])*_,p++,l+=u}i=l-d}s=null;var y=v.length%c;if(0<y){var I=2*(v.length-y);s=new Int16Array(v.buffer.slice(I)),v=new Int16Array(v.buffer.slice(0,I))}return{index:o,offset:i,frameNext:s,sampleRate:n,data:v}},F.PowerLevel=function(e,t){var n=e/t||0;return n<1251?Math.round(n/1250*10):Math.round(Math.min(100,Math.max(0,100*(1+Math.log(n/1e4)/Math.log(10)))))};var a=0;function t(e){this.id=++a,F.Traffic&&F.Traffic();var t={type:"mp3",bitRate:16,sampleRate:16e3,onProcess:m};for(var n in e)t[n]=e[n];this.set=t,this._S=9}F.Sync={O:9,C:9},F.prototype=t.prototype={open:function(e,n){var t=this;e=e||m,n=n||m;var a=function(){e(),t._SO=0},r=function(e,t){/Permission|Allow/i.test(e)?n("用户拒绝了录音权限",!0):!1===p.isSecureContext?n("无权录音(需https)"):/Found/i.test(e)?n(t+",无可用麦克风"):n(t)},o=F.Sync,i=++o.O,s=o.C;t._O=t._O_=i,t._SO=t._S;var c=function(){if(s!=o.C||!t._O){var e="open被取消";return i==o.O?t.close():e="open被中断",n(e),!0}};if(F.IsOpen())a();else if(F.Support()){var f=t.envCheck({envName:"H5",canProcess:!0});if(f)n("不能录音:"+f);else{var l=function(e){(F.Stream=e)._call={},c()||setTimeout(function(){c()||(F.IsOpen()?(function(){var e=F.Ctx,t=F.Stream,n=t._m=e.createMediaStreamSource(t),a=t._p=(e.createScriptProcessor||e.createJavaScriptNode).call(e,F.BufferSize,1,1);n.connect(a),a.connect(e.destination);var f=t._call;a.onaudioprocess=function(e){for(var t in f){for(var n=e.inputBuffer.getChannelData(0),a=n.length,r=new Int16Array(a),o=0,i=0;i<a;i++){var s=Math.max(-1,Math.min(1,n[i]));s=s<0?32768*s:32767*s,r[i]=s,o+=Math.abs(s)}for(var c in f)f[c](r,o);return}}}(),a()):n("录音功能无效:无音频流"))},100)},u=function(e){var t=e.name||e.message||e.code+":"+e;console.error(e),r(t,"无法录音:"+t)},v=F.Scope.getUserMedia({audio:!0},l,u);v&&v.then&&v.then(l)[e&&"catch"](u)}}else r("","此浏览器不支持录音")},close:function(e){e=e||m,this._stop();var t=F.Sync;if(this._O=0,this._O_!=t.O)return console.warn("close被忽略"),void e();t.C++;var n,a=F.Stream;if(a){(n=F.Stream)._m&&(n._m.disconnect(),n._p.disconnect(),n._p.onaudioprocess=n._p=n._m=null);for(var r=a.getTracks&&a.getTracks()||a.audioTracks||[],o=0;o<r.length;o++){var i=r[o];i.stop&&i.stop()}a.stop&&a.stop()}F.Stream=0,e()},mock:function(e,t){var n=this;return n._stop(),n.isMock=1,n.mockEnvInfo=null,n.buffers=[e],n.recSize=e.length,n.srcSampleRate=t,n},envCheck:function(e){var t,n=this.set;return t||(this[n.type+"_envCheck"]?t=this[n.type+"_envCheck"](e,n):n.takeoffEncodeChunk&&(t=n.type+"类型不支持设置takeoffEncodeChunk")),t||""},envStart:function(e,t){var n=this,a=n.set;if(n.isMock=e?1:0,n.mockEnvInfo=e,n.buffers=[],n.recSize=0,n.envInLast=0,n.envInFirst=0,n.envInFix=0,n.envInFixTs=[],a.sampleRate=Math.min(t,a.sampleRate),n.srcSampleRate=t,n.engineCtx=0,n[a.type+"_start"]){var r=n.engineCtx=n[a.type+"_start"](a);r&&(r.pcmDatas=[],r.pcmSize=0)}},envResume:function(){this.envInFixTs=[]},envIn:function(e,t){var r=this,o=r.set,i=r.engineCtx,n=r.srcSampleRate,a=e.length,s=F.PowerLevel(t,a),c=r.buffers,f=c.length;c.push(e);var l=c,u=f,v=Date.now(),p=Math.round(a/n*1e3);r.envInLast=v,1==r.buffers.length&&(r.envInFirst=v-p);var m=r.envInFixTs;m.splice(0,0,{t:v,d:p});for(var h=v,d=0,g=0;g<m.length;g++){var S=m[g];if(3e3<v-S.t){m.length=g;break}h=S.t,d+=S.d}var _=m[1],y=v-h;if(y/3<y-d&&(_&&1e3<y||6<=m.length)){var I=v-_.t-p;if(p/5<I){var w=!o.disableEnvInFix;if(console.warn("["+v+"]"+(w?"":"未")+"补偿"+I+"ms"),r.envInFix+=I,w){var x=new Int16Array(I*n/1e3);a+=x.length,c.push(x)}}}var M=r.recSize,k=a,C=M+k;if(r.recSize=C,i){var R=F.SampleData(c,n,o.sampleRate,i.chunkInfo);i.chunkInfo=R,C=(M=i.pcmSize)+(k=R.data.length),i.pcmSize=C,c=i.pcmDatas,f=c.length,c.push(R.data),n=R.sampleRate}var b=Math.round(C/n*1e3),D=c.length,T=l.length,z=function(){for(var e=O?0:-k,t=null==c[0],n=f;n<D;n++){var a=c[n];null==a?t=1:(e+=a.length,i&&a.length&&r[o.type+"_encode"](i,a))}if(t&&i)for(n=u,l[0]&&(n=0);n<T;n++)l[n]=null;t&&(e=O?k:0,c[0]=null),i?i.pcmSize+=e:r.recSize+=e},O=o.onProcess(c,s,b,n,f,z);if(!0===O){var U=0;for(g=f;g<D;g++)null==c[g]?U=1:c[g]=new Int16Array(0);U?console.warn("未进入异步前不能清除buffers"):i?i.pcmSize-=k:r.recSize-=k}else z()},start:function(){if(F.IsOpen()){console.log("["+Date.now()+"]Start");var e=this,t=(e.set,F.Ctx);if(e._stop(),e.state=0,e.envStart(null,t.sampleRate),e._SO&&e._SO+1!=e._S)console.warn("start被中断");else{e._SO=0;var n=function(){e.state=1,e.resume()};"suspended"==t.state?t.resume().then(function(){console.log("ctx resume"),n()}):n()}}else console.error("未open")},pause:function(){this.state&&(this.state=2,delete F.Stream._call[this.id])},resume:function(){var n=this;n.state&&(n.state=1,n.envResume(),F.Stream._call[n.id]=function(e,t){1==n.state&&n.envIn(e,t)})},_stop:function(e){var t=this,n=t.set;t.isMock||t._S++,t.state&&(t.pause(),t.state=0),!e&&t[n.type+"_stop"]&&(t[n.type+"_stop"](t.engineCtx),t.engineCtx=0)},stop:function(n,t,e){var a,r=this,o=r.set;console.log("["+Date.now()+"]Stop "+(r.envInLast?r.envInLast-r.envInFirst+"ms 补"+r.envInFix+"ms":"-"));var i=function(){r._stop(),e&&r.close()},s=function(e){t&&t(e),i()},c=function(e,t){if(console.log("["+Date.now()+"]结束 编码"+(Date.now()-a)+"ms 音频"+t+"ms/"+e.size+"b"),o.takeoffEncodeChunk)console.warn("启用takeoffEncodeChunk后stop返回的blob长度为0不提供音频数据");else if(e.size<Math.max(100,t/2))return void s("生成的"+o.type+"无效");n&&n(e,t),i()};if(!r.isMock){if(!r.state)return void s("未开始录音");r._stop(!0)}var f=r.recSize;if(f)if(r.buffers[0])if(r[o.type]){if(r.isMock){var l=r.envCheck(r.mockEnvInfo||{envName:"mock",canProcess:!1});if(l)return void s("录音错误:"+l)}var u=r.engineCtx;if(r[o.type+"_complete"]&&u){var v=Math.round(u.pcmSize/o.sampleRate*1e3);return a=Date.now(),void r[o.type+"_complete"](u,function(e){c(e,v)},s)}a=Date.now();var p=F.SampleData(r.buffers,r.srcSampleRate,o.sampleRate);o.sampleRate=p.sampleRate;var m=p.data;v=Math.round(m.length/o.sampleRate*1e3),console.log("采样"+f+"->"+m.length+" 花:"+(Date.now()-a)+"ms"),setTimeout(function(){a=Date.now(),r[o.type](m,function(e){c(e,v)},function(e){s(e)})})}else s("未加载"+o.type+"编码器");else s("音频被释放");else s("未采集到录音")}},p.Recorder&&p.Recorder.Destroy(),(p.Recorder=F).LM="2020-5-17 08:21:54",F.TrafficImgUrl="//ia.51.la/go1?id=20469973&pvFlag=1",F.Traffic=function(){var e=F.TrafficImgUrl;if(e){var t=F.Traffic,n=location.href.replace(/#.*/,"");if(0==e.indexOf("//")&&(e=/^https:/i.test(n)?"https:"+e:"http:"+e),!t[n]){t[n]=1;var a=new Image;a.src=e,console.log("Traffic Analysis Image: Recorder.TrafficImgUrl="+F.TrafficImgUrl)}}}}(window),"function"==typeof define&&define.amd&&define(function(){return Recorder}),"object"==typeof module&&module.exports&&(module.exports=Recorder),function(){"use strict";Recorder.prototype.enc_wav={stable:!0,testmsg:"比特率取值范围8位、16位"},Recorder.prototype.wav=function(e,t,n){var a=this.set,r=e.length,o=a.sampleRate,i=8==a.bitRate?8:16,s=r*(i/8),c=new ArrayBuffer(44+s),f=new DataView(c),l=0,u=function(e){for(var t=0;t<e.length;t++,l++)f.setUint8(l,e.charCodeAt(t))},v=function(e){f.setUint16(l,e,!0),l+=2},p=function(e){f.setUint32(l,e,!0),l+=4};if(u("RIFF"),p(36+s),u("WAVE"),u("fmt "),p(16),v(1),v(1),p(o),p(o*(i/8)),v(i/8),v(i),u("data"),p(s),8==i)for(var m=0;m<r;m++,l++){var h=128+(e[m]>>8);f.setInt8(l,h,!0)}else for(m=0;m<r;m++,l+=2)f.setInt16(l,e[m],!0);t(new Blob([f.buffer],{type:"audio/wav"}))}}();