forked from xiangyuecn/Recorder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
recorder.wav.min.js
6 lines (6 loc) · 7.88 KB
/
recorder.wav.min.js
1
2
3
4
5
6
/*
录音
https://github.com/xiangyuecn/Recorder
src: recorder-core.js,engine/wav.js
*/
!function(v){"use strict";var p=function(){},z=function(e){return new t(e)};z.IsOpen=function(){var e=z.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},z.BufferSize=4096,z.Destroy=function(){for(var e in console.log("Recorder Destroy"),n)n[e]()};var n={};z.BindDestroy=function(e,t){n[e]=t},z.Support=function(){var e=v.AudioContext;if(e||(e=v.webkitAudioContext),!e)return!1;var t=navigator.mediaDevices||{};return t.getUserMedia||(t=navigator).getUserMedia||(t.getUserMedia=t.webkitGetUserMedia||t.mozGetUserMedia||t.msGetUserMedia),!!t.getUserMedia&&(z.Scope=t,z.Ctx&&"closed"!=z.Ctx.state||(z.Ctx=new e,z.BindDestroy("Ctx",function(){var e=z.Ctx;e&&e.close&&e.close()})),!0)},z.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}},z.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,z.Traffic&&z.Traffic();var t={type:"mp3",bitRate:16,sampleRate:16e3,onProcess:p};for(var n in e)t[n]=e[n];this.set=t,this._S=9}z.Sync={O:9,C:9},z.prototype=t.prototype={open:function(e,n){var t=this;e=e||p,n=n||p;var a=function(){e(),t._SO=0},r=function(e,t){/Permission|Allow/i.test(e)?n("用户拒绝了录音权限",!0):!1===v.isSecureContext?n("无权录音(需https)"):/Found/i.test(e)?n(t+",无可用麦克风"):n(t)},o=z.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(z.IsOpen())a();else if(z.Support()){var f=function(e){(z.Stream=e)._call={},c()||setTimeout(function(){c()||(z.IsOpen()?(function(){var e=z.Ctx,t=z.Stream,n=t._m=e.createMediaStreamSource(t),a=t._p=(e.createScriptProcessor||e.createJavaScriptNode).call(e,z.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)},l=function(e){var t=e.name||e.message||e.code+":"+e;console.error(e),r(t,"无法录音:"+t)},u=z.Scope.getUserMedia({audio:!0},f,l);u&&u.then&&u.then(f)[e&&"catch"](l)}else r("","此浏览器不支持录音")},close:function(e){e=e||p,this._stop();var t=z.Sync;if(this._O=0,this._O_!=t.O)return console.warn("close被忽略"),void e();t.C++;var n,a=z.Stream;if(a){(n=z.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()}z.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 a=this,r=a.set,o=a.engineCtx,n=a.srcSampleRate,i=e.length,s=z.PowerLevel(t,i),c=a.buffers,f=c.length;c.push(e);var l=Date.now(),u=Math.round(i/n*1e3);a.envInLast=l,1==a.buffers.length&&(a.envInFirst=l-u);var v=a.envInFixTs;v.splice(0,0,{t:l,d:u});for(var p=l,m=0,h=0;h<v.length;h++){var d=v[h];if(3e3<l-d.t){v.length=h;break}p=d.t,m+=d.d}var g=v[1],S=l-p;if(S/3<S-m&&(g&&1e3<S||6<=v.length)){var _=l-g.t-u;if(u/5<_){var y=!r.disableEnvInFix;if(console.warn("["+l+"]"+(y?"":"未")+"补偿"+_+"ms"),a.envInFix+=_,y){var I=new Int16Array(_*n/1e3);i+=I.length,c.push(I)}}}var w=a.recSize,x=i,M=w+x;if(a.recSize=M,o){var R=z.SampleData(c,n,r.sampleRate,o.chunkInfo);o.chunkInfo=R,M=(w=o.pcmSize)+(x=R.data.length),o.pcmSize=M,c=o.pcmDatas,f=c.length,c.push(R.data),n=R.sampleRate}var D=Math.round(M/n*1e3),C=c.length,T=function(){for(var e=b?0:-x,t=f;t<C;t++){var n=c[t];e+=n.length,o&&n.length&&a[r.type+"_encode"](o,n)}o?o.pcmSize+=e:a.recSize+=e},b=r.onProcess(c,s,D,n,f,T);if(!0===b){for(h=f;h<C;h++)c[h]=new Int16Array(0);o?o.pcmSize-=x:a.recSize-=x}else T()},start:function(){if(z.IsOpen()){console.log("["+Date.now()+"]Start");var e=this,t=(e.set,z.Ctx);if(e._stop(),e.state=0,e.envStart(0,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 z.Stream._call[this.id])},resume:function(){var n=this;n.state&&(n.state=1,n.envResume(),z.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){console.log("["+Date.now()+"]结束 编码"+(Date.now()-a)+"ms 音频"+t+"ms/"+e.size+"b"),e.size<Math.max(100,t/2)?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]){var l=r.engineCtx;if(r[o.type+"_complete"]&&l){l.pcmDatas;var u=Math.round(l.pcmSize/o.sampleRate*1e3);return a=Date.now(),void r[o.type+"_complete"](l,function(e){c(e,u)},s)}a=Date.now();var v=z.SampleData(r.buffers,r.srcSampleRate,o.sampleRate);o.sampleRate=v.sampleRate;var p=v.data;u=Math.round(p.length/o.sampleRate*1e3),console.log("采样"+f+"->"+p.length+" 花:"+(Date.now()-a)+"ms"),setTimeout(function(){a=Date.now(),r[o.type](p,function(e){c(e,u)},function(e){s(e)})})}else s("未加载"+o.type+"编码器");else s("音频被释放");else s("未采集到录音")}},v.Recorder&&v.Recorder.Destroy(),(v.Recorder=z).LM="2020-1-8 10:53:14",z.TrafficImgUrl="//ia.51.la/go1?id=20469973&pvFlag=1",z.Traffic=function(){var e=z.TrafficImgUrl;if(e){var t=z.Traffic,n=location.href.replace(/#.*/,"");if(!t[n]){t[n]=1;var a=new Image;a.src=e,console.log("Traffic Analysis Image: Recorder.TrafficImgUrl="+e)}}}}(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"}))}}();