forked from xiangyuecn/Recorder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecorder.wav.min.js
6 lines (6 loc) · 6.84 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 v=function(){},R=function(e){return new t(e)};R.IsOpen=function(){var e=R.Stream;if(e){var t=e.getTracks();if(0<t.length)return"live"==t[0].readyState}return!1},R.Destroy=function(){for(var e in console.log("Recorder Destroy"),n)n[e]()};var n={};function t(e){var t={type:"mp3",bitRate:16,sampleRate:16e3,bufferSize:4096,onProcess:v};for(var n in e)t[n]=e[n];this.set=t,this._S=9}R.BindDestroy=function(e,t){n[e]=t},R.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&&(R.Scope=t,R.Ctx&&"closed"!=R.Ctx.state||(R.Ctx=new e,R.BindDestroy("Ctx",function(){R.Ctx&&R.Ctx.close()})),!0)},R.SampleData=function(e,t,n,a,r){a||(a={});var o=a.index||0,s=a.offset||0,i=a.frameNext||[];r||(r={});var c=r.frameSize||1;r.frameType&&(c="mp3"==r.frameType?1152:1);for(var f=0,u=o;u<e.length;u++)f+=e[u].length;f=Math.max(0,f-Math.floor(s));var l=t/n;1<l?f=Math.floor(f/l):(l=1,n=t),f+=i.length;var p=new Int16Array(f),v=0;for(u=0;u<i.length;u++)p[v]=i[u],v++;for(var h=e.length;o<h;o++){for(var m=e[o],d=(u=s,m.length);u<d;){var S=Math.floor(u),g=Math.ceil(u),x=u-S;p[v]=m[S]+(m[g]-m[S])*x,v++,u+=l}s=u-d}i=null;var y=p.length%c;if(0<y){var M=2*(p.length-y);i=new Int16Array(p.buffer.slice(M)),p=new Int16Array(p.buffer.slice(0,M))}return{index:o,offset:s,frameNext:i,sampleRate:n,data:p}},R.Sync={O:9,C:9},R.prototype=t.prototype={open:function(e,n){var t=this;e=e||v,n=n||v;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=R.Sync,s=++o.O,i=o.C;t._O=t._O_=s,t._SO=t._S;var c=function(){if(i!=o.C||!t._O){var e="open被取消";return s==o.O?t.close():e="open被中断",n(e),!0}};if(R.IsOpen())a();else if(R.Support()){var f=function(e){R.Stream=e,c()||setTimeout(function(){c()||(R.IsOpen()?a():n("录音功能无效:无音频流"))},100)},u=function(e){var t=e.name||e.message||e.code+":"+e;console.error(e),r(t,"无法录音:"+t)},l=R.Scope.getUserMedia({audio:!0},f,u);l&&l.then&&l.then(f)[e&&"catch"](u)}else r("","此浏览器不支持录音")},close:function(e){e=e||v;this._stop();var t=R.Sync;if(this._O=0,this._O_!=t.O)return console.warn("close被忽略"),void e();t.C++;var n=R.Stream;if(n)for(var a=n.getTracks(),r=0;r<a.length;r++)a[r].stop();R.Stream=0,e()},mock:function(e,t){var n=this;return n._stop(),n.isMock=1,n.buffers=[e],n.recSize=e.length,n.srcSampleRate=t,n},envStart:function(e,t){var n=this,a=n.set;if(n.isMock=e?1:0,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 n=this,a=n.set,r=n.engineCtx,o=e.length;n.recSize+=o;var s=n.buffers;s.push(e);var i,c=t/o;i=c<1251?Math.round(c/1250*10):Math.round(Math.min(100,Math.max(0,100*(1+Math.log(c/1e4)/Math.log(10)))));var f=n.srcSampleRate,u=n.recSize,l=Date.now(),p=Math.round(o/f*1e3);n.envInLast=l,1==n.buffers.length&&(n.envInFirst=l-p);var v=n.envInFixTs;v.splice(0,0,{t:l,d:p});for(var h=l,m=0,d=0;d<v.length;d++){var S=v[d];if(3e3<l-S.t){v.length=d;break}h=S.t,m+=S.d}var g=v[1],x=l-h;if(x/3<x-m&&(g&&1e3<x||6<=v.length)){var y=l-g.t-p;if(p/5<y){var M=!a.disableEnvInFix;if(console.warn("["+l+"]"+(M?"":"未")+"补偿"+y+"ms"),n.envInFix+=y,M){var _=new Int16Array(y*f/1e3);n.recSize+=_.length,s.push(_)}}}if(r){var w=R.SampleData(s,f,a.sampleRate,r.chunkInfo);r.chunkInfo=w,r.pcmSize+=w.data.length,u=r.pcmSize,(s=r.pcmDatas).push(w.data),f=w.sampleRate,n[a.type+"_encode"](r,w.data)}var I=Math.round(u/f*1e3);a.onProcess(s,i,I,f)},start:function(){if(R.IsOpen()){console.log("["+Date.now()+"]Start");var e=this,t=(e.set,R.Ctx);e._stop(),e.state=0,e.envStart(0,t.sampleRate),e._SO&&e._SO+1!=e._S?console.warn("start被中断"):(e._SO=0,"suspended"==t.state?t.resume().then(function(){console.log("ctx resume"),e._start()}):e._start())}else console.error("未open")},_start:function(){var i=this,e=i.set,t=(i.engineCtx,R.Ctx),n=i.media=t.createMediaStreamSource(R.Stream),a=i.process=(t.createScriptProcessor||t.createJavaScriptNode).call(t,e.bufferSize,1,1);a.onaudioprocess=function(e){if(1==i.state){for(var t=e.inputBuffer.getChannelData(0),n=t.length,a=new Int16Array(n),r=0,o=0;o<n;o++){var s=Math.max(-1,Math.min(1,t[o]));s=s<0?32768*s:32767*s,a[o]=s,r+=Math.abs(s)}i.envIn(a,r)}},n.connect(a),a.connect(t.destination),i.state=1},pause:function(e){this.state&&(this.state=e||2)},resume:function(){this.pause(1),this.envResume()},_stop:function(e){var t=this,n=t.set;t.isMock||t._S++,t.state&&(t.state=0,t.media.disconnect(),t.process.disconnect()),!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 s=function(){r._stop(),e&&r.close()},i=function(e){t&&t(e),s()},c=function(e,t){console.log("["+Date.now()+"]结束 编码"+(Date.now()-a)+"ms 音频"+t+"ms/"+e.size+"b"),e.size<Math.max(100,t/2)?i("生成的"+o.type+"无效"):(n&&n(e,t),s())};if(!r.isMock){if(!r.state)return void i("未开始录音");r._stop(!0)}var f=r.recSize;if(f)if(r.buffers[0])if(r[o.type]){var u=r.engineCtx;if(r[o.type+"_complete"]&&u){u.pcmDatas;var l=Math.round(u.pcmSize/o.sampleRate*1e3);return a=Date.now(),void r[o.type+"_complete"](u,function(e){c(e,l)},i)}a=Date.now();var p=R.SampleData(r.buffers,r.srcSampleRate,o.sampleRate);o.sampleRate=p.sampleRate;var v=p.data;l=Math.round(v.length/o.sampleRate*1e3);console.log("采样"+f+"->"+v.length+" 花:"+(Date.now()-a)+"ms"),setTimeout(function(){a=Date.now(),r[o.type](v,function(e){c(e,l)},function(e){i(e)})})}else i("未加载"+o.type+"编码器");else i("音频被释放");else i("未采集到录音")}},p.Recorder&&p.Recorder.Destroy(),(p.Recorder=R).LM="2019-11-2 21:37:21"}(window),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,s=8==a.bitRate?8:16,i=r*(s/8),c=new ArrayBuffer(44+i),f=new DataView(c),u=0,l=function(e){for(var t=0;t<e.length;t++,u++)f.setUint8(u,e.charCodeAt(t))},p=function(e){f.setUint16(u,e,!0),u+=2},v=function(e){f.setUint32(u,e,!0),u+=4};if(l("RIFF"),v(36+i),l("WAVE"),l("fmt "),v(16),p(1),p(1),v(o),v(o*(s/8)),p(s/8),p(s),l("data"),v(i),8==s)for(var h=0;h<r;h++,u++){var m=128+(e[h]>>8);f.setInt8(u,m,!0)}else for(h=0;h<r;h++,u+=2)f.setInt16(u,e[h],!0);t(new Blob([f.buffer],{type:"audio/wav"}))}}();