forked from xiangyuecn/Recorder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
recorder.wav.min.js
6 lines (6 loc) · 5.14 KB
/
recorder.wav.min.js
1
2
3
4
5
6
/*
录音
https://github.com/xiangyuecn/Recorder
src: recorder-core.js,engine/wav.js
*/
!function(a){"use strict";a.RecorderLM="2019-8-19 23:13:37";var o=function(){};function l(e){return new t(e)}function t(e){var t={type:"mp3",bitRate:16,sampleRate:16e3,bufferSize:4096,onProcess:o};for(var a in e)t[a]=e[a];this.set=t}l.IsOpen=function(){var e=l.Stream;if(e){var t=e.getTracks();if(0<t.length)return"live"==t[0].readyState}return!1},l.Support=function(){var e=a.AudioContext;if(e||(e=a.webkitAudioContext),!e)return!1;var t=navigator.mediaDevices||{};return t.getUserMedia||(t=navigator).getUserMedia||(t.getUserMedia=t.webkitGetUserMedia||t.mozGetUserMedia||t.msGetUserMedia),!!t.getUserMedia&&(l.Scope=t,l.Ctx&&"closed"!=l.Ctx.state||(l.Ctx=new e),!0)},l.SampleData=function(e,t,a,n){n||(n={});for(var r=n.index||0,o=n.offset||0,s=0,i=r;i<e.length;i++)s+=e[i].length;s=Math.max(0,s-Math.floor(o));var c=t/a;1<c?s=Math.floor(s/c):(c=1,a=t);for(var f=new Int16Array(s),p=0,u=e.length;r<u;r++){for(var l=e[r],m=(i=o,l.length);i<m;){var h=Math.floor(i),v=Math.ceil(i),d=i-h;f[p]=l[h]+(l[v]-l[h])*d,p++,i+=c}o=i-m}return{index:r,offset:o,sampleRate:a,data:f}},l.prototype=t.prototype={open:function(t,n){if(t=t||o,n=n||o,l.IsOpen())t();else if(l.Support()){var e=function(e){l.Stream=e,setTimeout(function(){l.IsOpen()?t():n("录音功能无效:无音频流")},100)},a=function(e){var t=e.name||e.message||"";console.error(e);var a=/Permission|Allow/i.test(t);n(a?"用户拒绝了录音权限":"无法录音:"+t,a)},r=l.Scope.getUserMedia({audio:!0},e,a);r&&r.then&&r.then(e)[t&&"catch"](a)}else n("此浏览器不支持录音",!1)},close:function(e){e=e||o;this._stop();var t=l.Stream;if(t)for(var a=t.getTracks(),n=0;n<a.length;n++)a[n].stop();l.Stream=0,e()},mock:function(e,t){var a=this;return a._stop(),a.isMock=1,a.buffers=[e],a.recSize=e.length,a.srcSampleRate=t,a},envStart:function(e,t){var a=this,n=a.set;if(a.isMock=e?1:0,a.buffers=[],a.recSize=0,n.sampleRate=Math.min(t,n.sampleRate),a.srcSampleRate=t,a.engineCtx=0,a[n.type+"_start"]){var r=a.engineCtx=a[n.type+"_start"](n);r&&(r.pcmDatas=[],r.pcmSize=0)}},envIn:function(e,t){var a=this,n=a.set,r=a.engineCtx,o=e.length;a.recSize+=o;var s=a.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=a.srcSampleRate,p=a.recSize;if(r){var u=l.SampleData(s,f,n.sampleRate,r.chunkInfo);r.chunkInfo=u,r.pcmSize+=u.data.length,p=r.pcmSize,(s=r.pcmDatas).push(u.data),f=u.sampleRate,a[n.type+"_encode"](r,u.data)}return{bf:s,pl:i,dt:Math.round(p/f*1e3),sr:f}},start:function(){if(l.IsOpen()){console.log("["+Date.now()+"]Start");var e=this,t=(e.set,l.Ctx);e._stop(),e.state=0,e.envStart(0,t.sampleRate),"suspended"==t.state?t.resume().then(function(){console.log("ctx resume"),e._start()}):e._start()}else console.error("未open")},_start:function(){var c=this,f=c.set,e=(c.engineCtx,l.Ctx),t=c.media=e.createMediaStreamSource(l.Stream),a=c.process=(e.createScriptProcessor||e.createJavaScriptNode).call(e,f.bufferSize,1,1);a.onaudioprocess=function(e){if(1==c.state){for(var t=e.inputBuffer.getChannelData(0),a=t.length,n=new Int16Array(a),r=0,o=0;o<a;o++){var s=Math.max(-1,Math.min(1,t[o]));s=s<0?32768*s:32767*s,n[o]=s,r+=Math.abs(s)}var i=c.envIn(n,r);f.onProcess(i.bf,i.pl,i.dt,i.sr)}},t.connect(a),a.connect(e.destination),c.state=1},pause:function(e){this.state&&(this.state=e||2)},resume:function(){this.pause(1)},_stop:function(e){var t=this,a=t.set;t.state&&(t.state=0,t.media.disconnect(),t.process.disconnect()),!e&&t[a.type+"_stop"]&&(t[a.type+"_stop"](t.engineCtx),t.engineCtx=0)},stop:function(a,t){console.log("["+Date.now()+"]Stop");var n,r=this,o=r.set,s=function(e){t&&t(e),r._stop()},i=function(e,t){console.log("["+Date.now()+"]End",t,"编码耗时:"+(Date.now()-n),e),e.size<500?s("生成的"+o.type+"无效"):(a&&a(e,t),r._stop())};if(!r.isMock){if(!r.state)return void s("未开始录音");r._stop(!0)}var e=r.recSize;if(e)if(r[o.type]){var c=r.engineCtx;if(r[o.type+"_complete"]&&c){c.pcmDatas;var f=Math.round(c.pcmSize/o.sampleRate*1e3);return n=Date.now(),void r[o.type+"_complete"](c,function(e){i(e,f)},s)}n=Date.now();var p=l.SampleData(r.buffers,r.srcSampleRate,o.sampleRate);o.sampleRate=p.sampleRate;var u=p.data;f=Math.round(u.length/o.sampleRate*1e3);console.log("采样"+e+"->"+u.length+" 花:"+(Date.now()-n)+"ms"),setTimeout(function(){n=Date.now(),r[o.type](u,function(e){i(e,f)},function(e){s(e)})})}else s("未加载"+o.type+"编码器");else s("未采集到录音")}},a.Recorder=l}(window),function(){"use strict";Recorder.prototype.enc_wav={stable:!0,testmsg:"比特率取值范围8位、16位"},Recorder.prototype.wav=function(e,t,a){var n=this.set,r=e.length,o=n.sampleRate,s=8==n.bitRate?8:16,i=r*(s/8),c=new ArrayBuffer(44+i),f=new DataView(c),p=0,u=function(e){for(var t=0;t<e.length;t++,p++)f.setUint8(p,e.charCodeAt(t))},l=function(e){f.setUint16(p,e,!0),p+=2},m=function(e){f.setUint32(p,e,!0),p+=4};if(u("RIFF"),m(36+i),u("WAVE"),u("fmt "),m(16),l(1),l(1),m(o),m(o*(s/8)),l(s/8),l(s),u("data"),m(i),8==s)for(var h=0;h<r;h++,p++){var v=128+(e[h]>>8);f.setInt8(p,v,!0)}else for(h=0;h<r;h++,p+=2)f.setInt16(p,e[h],!0);t(new Blob([f.buffer],{type:"audio/wav"}))}}();