UNPKG

benz-recorderjs

Version:

Record audio from a node in the Web Audio API

2 lines (1 loc) 3.05 kB
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):t.Recorder=e()}(this,function(){var c=function(){var u,h=0,l=[],p=[];function g(t,e){for(var n=new Float32Array(e),a=0,o=0;o<t.length;o++)n.set(t[o],a),a+=t[o].length;return n}function w(t,e,n){for(var a=0;a<n.length;a++)t.setUint8(e+a,n.charCodeAt(a))}self.onmessage=function(t){switch(t.data.command){case"init":d=t.data.config,u=d.sampleRate;break;case"record":f=t.data.buffer,l.push(f[0]),p.push(f[1]),h+=f[0].length;break;case"exportWAV":n=t.data.type,a=g(l,h),o=g(p,h),i=function(t,e){for(var n=t.length+e.length,a=new Float32Array(n),o=0,s=0;o<n;)a[o++]=t[s],a[o++]=e[s],s++;return a}(a,o),r=new ArrayBuffer(44+2*i.length),w(c=new DataView(r),0,"RIFF"),c.setUint32(4,36+2*i.length,!0),w(c,8,"WAVE"),w(c,12,"fmt "),c.setUint32(16,16,!0),c.setUint16(20,1,!0),c.setUint16(22,2,!0),c.setUint32(24,u,!0),c.setUint32(28,4*u,!0),c.setUint16(32,4,!0),c.setUint16(34,16,!0),w(c,36,"data"),c.setUint32(40,2*i.length,!0),function(t,e,n){for(var a=0;a<n.length;a++,e+=2){var o=Math.max(-1,Math.min(1,n[a]));t.setInt16(e,o<0?32768*o:32767*o,!0)}}(c,44,i),s=new Blob([c],{type:n}),self.postMessage({type:"blob",data:s});break;case"getBuffer":(e=[]).push(g(l,h)),e.push(g(p,h)),self.postMessage({type:"buffer",data:e});break;case"clear":h=0,l=[],p=[]}var e,n,a,o,s,i,r,c,f,d}}.toString().replace(/^\s*function.*?\(\)\s*{/,"").replace(/}\s*$/,""),t=function(t,e){var n=e||{},a=n.bufferLen||4096;this.context=t.context,this.node=(this.context.createScriptProcessor||this.context.createJavaScriptNode).call(this.context,a,2,2);var o=new Worker((window.URL||window.webkitURL).createObjectURL(new Blob([c],{type:"text/javascript"})));o.onmessage=function(t){"blob"===t.data.type?i(t.data.data):s(t.data.data)},o.postMessage({command:"init",config:{sampleRate:this.context.sampleRate}});var s,i,r=!1;this.node.onaudioprocess=function(t){r&&o.postMessage({command:"record",buffer:[t.inputBuffer.getChannelData(0),t.inputBuffer.getChannelData(1)]})},this.configure=function(t){for(var e in t)t.hasOwnProperty(e)&&(n[e]=t[e])},this.record=function(){r=!0},this.stop=function(){r=!1},this.clear=function(){o.postMessage({command:"clear"})},this.getBuffer=function(t){s=t||n.callback,o.postMessage({command:"getBuffer"})},this.exportWAV=function(t,e){if(i=t||n.callback,e=e||n.type||"audio/wav",!i)throw new Error("Callback not set");o.postMessage({command:"exportWAV",type:e})},this.release=function(){this.stop(),this.clear(),this.configure=this.record=this.stop=this.clear=this.getBuffer=this.exportWAV=function(){},t.disconnect(this.node),this.node.onaudioprocess=null,this.node.disconnect(),o.terminate()},t.connect(this.node),this.node.connect(this.context.destination)};return t.forceDownload=function(t,e){var n=(window.URL||window.webkitURL).createObjectURL(t),a=window.document.createElement("a");a.href=n,a.download=e||"output.wav";var o=document.createEvent("Event");o.initEvent("click",!0,!0),a.dispatchEvent(o)},t});