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.97 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(){},O=function(e){return new t(e)};O.IsOpen=function(){var e=O.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},O.BufferSize=4096,O.Destroy=function(){for(var e in console.log("Recorder Destroy"),n)n[e]()};var n={};O.BindDestroy=function(e,t){n[e]=t},O.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&&(O.Scope=t,O.Ctx&&"closed"!=O.Ctx.state||(O.Ctx=new e,O.BindDestroy("Ctx",function(){var e=O.Ctx;e&&e.close&&e.close()})),!0)},O.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,l=o;l<e.length;l++)f+=e[l].length;f=Math.max(0,f-Math.floor(s));var u=t/n;1<u?f=Math.floor(f/u):(u=1,n=t),f+=i.length;for(var v=new Int16Array(f),p=0,l=0;l<i.length;l++)v[p]=i[l],p++;for(var m=e.length;o<m;o++){for(var h=e[o],l=s,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}s=l-d}i=null;var y=v.length%c;if(0<y){var I=2*(v.length-y);i=new Int16Array(v.buffer.slice(I)),v=new Int16Array(v.buffer.slice(0,I))}return{index:o,offset:s,frameNext:i,sampleRate:n,data:v}},O.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,O.Traffic&&O.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}O.Sync={O:9,C:9},O.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=O.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(O.IsOpen())a();else if(O.Support()){var f=function(e){(O.Stream=e)._call={},c()||setTimeout(function(){c()||(O.IsOpen()?(function(){var e=O.Ctx,t=O.Stream,n=t._m=e.createMediaStreamSource(t),a=t._p=(e.createScriptProcessor||e.createJavaScriptNode).call(e,O.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,s=0;s<a;s++){var i=Math.max(-1,Math.min(1,n[s]));i=i<0?32768*i:32767*i,r[s]=i,o+=Math.abs(i)}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=O.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=O.Sync;if(this._O=0,this._O_!=t.O)return console.warn("close被忽略"),void e();t.C++;var n,a=O.Stream;if(a){(n=O.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 s=r[o];s.stop&&s.stop()}a.stop&&a.stop()}O.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 r=this,o=r.set,s=r.engineCtx,n=r.srcSampleRate,a=e.length,i=O.PowerLevel(t,a),c=r.buffers,f=c.length;c.push(e);var l=Date.now(),u=Math.round(a/n*1e3);r.envInLast=l,1==r.buffers.length&&(r.envInFirst=l-u);var v=r.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=!o.disableEnvInFix;if(console.warn("["+l+"]"+(y?"":"未")+"补偿"+_+"ms"),r.envInFix+=_,y){var I=new Int16Array(_*n/1e3);a+=I.length,c.push(I)}}}var w=r.recSize,x=a,M=w+x;if(r.recSize=M,s){var R=O.SampleData(c,n,o.sampleRate,s.chunkInfo);s.chunkInfo=R,M=(w=s.pcmSize)+(x=R.data.length),s.pcmSize=M,c=s.pcmDatas,f=c.length,c.push(R.data),n=R.sampleRate}var D=Math.round(M/n*1e3),C=c.length,b=function(){for(var e=T?0:-x,t=0,n=f;n<C;n++){var a=c[n];null==a?t=1:(e+=a.length,s&&a.length&&r[o.type+"_encode"](s,a))}t||(s?s.pcmSize+=e:r.recSize+=e)},T=o.onProcess(c,i,D,n,f,b);if(!0===T){var z=0;for(h=f;h<C;h++)null==c[h]?z=1:c[h]=new Int16Array(0);z?console.warn("异步模式下不能清除buffers"):s?s.pcmSize-=x:r.recSize-=x}else b()},start:function(){if(O.IsOpen()){console.log("["+Date.now()+"]Start");var e=this,t=(e.set,O.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 O.Stream._call[this.id])},resume:function(){var n=this;n.state&&(n.state=1,n.envResume(),O.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 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 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)},i)}a=Date.now();var v=O.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){i(e)})})}else i("未加载"+o.type+"编码器");else i("音频被释放");else i("未采集到录音")}},v.Recorder&&v.Recorder.Destroy(),(v.Recorder=O).LM="2020-1-8 10:53:14",O.TrafficImgUrl="//ia.51.la/go1?id=20469973&pvFlag=1",O.Traffic=function(){var e=O.TrafficImgUrl;if(e){var t=O.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,s=8==a.bitRate?8:16,i=r*(s/8),c=new ArrayBuffer(44+i),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+i),u("WAVE"),u("fmt "),p(16),v(1),v(1),p(o),p(o*(s/8)),v(s/8),v(s),u("data"),p(i),8==s)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"}))}}();