-
Notifications
You must be signed in to change notification settings - Fork 1
/
microphone.min.js
1 lines (1 loc) · 11.6 KB
/
microphone.min.js
1
function Microphone(){var e,t,r,n,o,s,l,h,u,f,c,g,v,A,w,p,F,y,T=window.AudioContext||window.webkitAudioContext;function b(i){console.log("gotStream called"),t=new T,o=[],u=new FFT(c,n),(s=t.createScriptProcessor(l,1,1)).onaudioprocess=function(e){o=e.inputBuffer.getChannelData(0),A&&(p.push(new Float32Array(o)),F.push(new Float32Array(o)),w+=l)},r=t.createMediaStreamSource(i),s.connect(t.destination),console.log("gotStream finished"),e=!0}function S(e){alert("Error capturing audio.")}function m(e){var t="A#1",r=58.2705;for(var n in y)if(Math.abs(y[n]-e)<=Math.abs(y[t]-e)&&(t=n,r=y[n]),y[n]>e)break;return[t,r]}function D(){var e=function(e){for(i=0;e[i]>=0&&i<l;i++);for(i=i;e[i]<0&&i<l;i++);var t=e[i],r=i;for(i=i;i<h;i++)e[i]>t&&(t=e[i],r=i);return r}(function(e){var t,r,n=new Array(l);for(t=0;t<l;t++)for(n[t]=0,r=0;r<l-t;r++)n[t]+=e[r]*e[r+t];return n}(o));return n/e}function M(){u.forward(o);for(var e=0,t=(f=u.spectrum)[0],r=0;++e<f.length;)f[e]>t&&(r=e,t=f[e]);return interpolatedBin=function(e,t){var r=Math.abs(e[t-1]),n=Math.abs(e[t]),i=Math.abs(e[t+1]);return r>i?(a=n/r,d=a/(1+a),t-1+d):(a=i/n,d=a/(1+a),t+d)}(f,r),interpolatedBin*g}function P(e,t){for(var r=new Float32Array(t),n=0,i=e.length,a=0;a<i;a++){var o=e[a];r.set(o,n),n+=o.length}return r}function C(e,t,r){for(var n=r.length,i=0;i<n;i++)e.setUint8(t+i,r.charCodeAt(i))}function B(e){document.body.removeChild(e.target)}this.initialize=function(){e=!1,t=null,r=null,n=(new T).sampleRate,o=null,s=null,40,h=n/40,u=null,f=null,g=n/2/((c=l=1024)/2),v=!1,A=!1,w=0,p=[],F=[],y={"A#1":58.2705,B1:61.7354,C2:65.4064,"C#2":69.2957,D2:73.4162,"D#2":77.7817,E2:82.4069,F2:87.3071,"F#2":92.4986,G2:97.9989,"G#2":103.826,A2:110,"A#2":116.542,B2:123.471,C3:130.813,"C#3":138.591,D3:146.832,"D#3":155.563,E3:164.814,F3:174.614,"F#3":184.997,G3:195.998,"G#3":207.652,A3:220,"A#3":233.082,B3:246.942,C4:261.626,"C#4":277.183,D4:293.665,"D#4":311.127,E4:329.628,F4:349.228,"F#4":369.994,G4:391.995,"G#4":415.305,A4:440,"A#4":466.164,B4:493.883,C5:523.251,"C#5":554.365,D5:587.33,"D#5":622.254,E5:659.255,F5:698.456,"F#5":739.989,G5:783.991,"G#5":830.609,A5:880,"A#5":932.328,B5:987.767,C6:1046.5,"C#6":1108.73,D6:1174.66,"D#6":1244.51,E6:1318.51,F6:1396.91,"F#6":1479.98,G6:1567.98,"G#6":1661.22,A6:1760,"A#6":1864.66,B6:1975.53,C7:2093,"C#7":2217.46,D7:2349.32,"D#7":2489.02,E7:2637.02,F7:2793.83,"F#7":2959.96,G7:3135.96,"G#7":3322.44,A7:3520,"A#7":3729.31,B7:3951.07,C8:4186.01},console.log("Beginning!"),navigator.mediaDevices.getUserMedia?navigator.mediaDevices.getUserMedia({audio:{echoCancellation:!1,autoGainControl:!1,noiseSuppression:!1,googEchoCancellation:!1,googAutoGainControl:!1,googAutoGainControl2:!1,googNoiseSuppression:!1,googHighpassFilter:!1}}).then(b).catch(S):alert("Sorry, your browser does not support getUserMedia")},this.isInitialized=function(){return!!e},this.startListening=function(){if(!e)throw"Not initialized";if(console.log("Now listening"),!v){v=!0;let e=t.createBiquadFilter();e.type="highshelf",e.frequency.value=3e3,e.gain.value=-24;let n=t.createBiquadFilter();n.type="lowshelf",n.frequency.value=30,n.gain.value=-24,r.connect(e),e.connect(n),n.connect(s)}},this.stopListening=function(){console.log("Done listening"),v&&!A&&(v=!1,r.disconnect())},this.startRecording=function(){if(!e||!v)throw"Microphone not initialized / not processing";console.log("Now recording"),A||(A=!0)},this.stopRecording=function(){console.log("Done recording"),A&&(A=!1,function(){var e=P(p,w),t=P(F,w),r=function(e,t){for(var r=e.length+t.length,n=new Float32Array(r),i=0,a=0;a<r;)n[a++]=e[i],n[a++]=t[i],i++;return n}(e,t),i=new ArrayBuffer(44+2*r.length),a=new DataView(i);C(a,0,"RIFF"),a.setUint32(4,44+2*r.length,!0),C(a,8,"WAVE"),C(a,12,"fmt "),a.setUint32(16,16,!0),a.setUint16(20,1,!0),a.setUint16(22,2,!0),a.setUint32(24,n,!0),a.setUint32(28,4*n,!0),a.setUint16(32,4,!0),a.setUint16(34,16,!0),C(a,36,"data"),a.setUint32(40,2*r.length,!0);for(var o=r.length,s=44,l=0;l<o;l++)a.setInt16(s,32767*r[l],!0),s+=2;var h=new Blob([a],{type:"audio/wav"}),u=(window.URL||window.webkitURL).createObjectURL(h),f=window.document.createElement("a");f.href=u,f.download="recording.wav";var c=document.createEvent("Event");c.initEvent("click",!0,!0),f.dispatchEvent(c)}())},this.getMaxInputAmplitude=function(){for(var e=Math.pow(10,-6),t=o[0],r=0;r<o.length;r++)o[r]>t&&(t=o[r]);return 20*Math.log(Math.max(t,e))},this.getFreq=function(e){if(!v)throw"Cannot compute frequency from null input";return 1==e?D():2==e?M():void 0},this.getNote=function(e){if(!v)throw"Cannot compute frequency from null input";return 1==e?m(D())[0]:2==e?m(M())[0]:void 0},this.getNoteCents=function(e){if(!v)throw"Cannot compute frequency from null input";return 1==e?(t=D(),r=m(t),n=r[1],i=Math.log(t/Math.round(n))/Math.log(2)*1200,[r[0],i]):2==e?function(){var e=M(),t=m(e),r=t[1],n=Math.log(e/Math.round(r))/Math.log(2)*1200;return[t[0],n]}():void 0;var t,r,n,i},this.logData=function(){var e,t,r;e=o,u.forward(e),t=u.spectrum;var n="";for(n+="------- Time Domain Data -------\n",r=0;r<e.length;r++)n+=e[r]+"\n";for(n+="\n------- Frequency Domain Data -------\n",r=0;r<t.length;r++)n+=t[r]+"\n";var i=new Blob([n],{type:"text/plain"}),a=document.createElement("a");a.download="jslog.txt",a.innerHTML="Download File",null!=window.webkitURL?a.href=window.webkitURL.createObjectURL(i):(a.href=window.URL.createObjectURL(i),a.onclick=B,a.style.display="none",document.body.appendChild(a)),a.click()}}var DSP={LEFT:0,RIGHT:1,MIX:2,SINE:1,TRIANGLE:2,SAW:3,SQUARE:4,LOWPASS:0,HIGHPASS:1,BANDPASS:2,NOTCH:3,BARTLETT:1,BARTLETTHANN:2,BLACKMAN:3,COSINE:4,GAUSS:5,HAMMING:6,HANN:7,LANCZOS:8,RECTANGULAR:9,TRIANGULAR:10,OFF:0,FW:1,BW:2,FWBW:3,TWO_PI:2*Math.PI};function setupTypedArray(e,t){"function"!=typeof this[e]&&"object"!=typeof this[e]&&("function"==typeof this[t]&&"object"!=typeof this[t]?this[e]=this[t]:this[e]=function(e){return e instanceof Array?e:"number"==typeof e?new Array(e):void 0})}function FourierTransform(e,t){this.bufferSize=e,this.sampleRate=t,this.bandwidth=2/e*t/2,this.spectrum=new Float32Array(e/2),this.real=new Float32Array(e),this.imag=new Float32Array(e),this.peakBand=0,this.peak=0,this.getBandFrequency=function(e){return this.bandwidth*e+this.bandwidth/2},this.calculateSpectrum=function(){for(var t,r,n,i=this.spectrum,a=this.real,o=this.imag,s=2/this.bufferSize,l=Math.sqrt,h=0,u=e/2;h<u;h++)(n=s*l((t=a[h])*t+(r=o[h])*r))>this.peak&&(this.peakBand=h,this.peak=n),i[h]=n}}function DFT(e,t){FourierTransform.call(this,e,t);var r=e/2*e,n=2*Math.PI;this.sinTable=new Float32Array(r),this.cosTable=new Float32Array(r);for(var i=0;i<r;i++)this.sinTable[i]=Math.sin(i*n/e),this.cosTable[i]=Math.cos(i*n/e)}function FFT(e,t){FourierTransform.call(this,e,t),this.reverseTable=new Uint32Array(e);for(var r,n=1,i=e>>1;n<e;){for(r=0;r<n;r++)this.reverseTable[r+n]=this.reverseTable[r]+i;n<<=1,i>>=1}for(this.sinTable=new Float32Array(e),this.cosTable=new Float32Array(e),r=0;r<e;r++)this.sinTable[r]=Math.sin(-Math.PI/r),this.cosTable[r]=Math.cos(-Math.PI/r)}function RFFT(e,t){FourierTransform.call(this,e,t),this.trans=new Float32Array(e),this.reverseTable=new Uint32Array(e),this.reverseBinPermute=function(e,t){var r,n=this.bufferSize,i=n>>>1,a=n-1,o=1,s=0;e[0]=t[0];do{for(s+=i,e[o]=t[s],e[s]=t[o],o++,r=i<<1;!((s^=r>>=1)&r););s>=o&&(e[o]=t[s],e[s]=t[o],e[a-o]=t[a-s],e[a-s]=t[a-o]),o++}while(o<i);e[a]=t[a]},this.generateReverseTable=function(){var e,t=this.bufferSize,r=t>>>1,n=t-1,i=1,a=0;this.reverseTable[0]=0;do{for(a+=r,this.reverseTable[i]=a,this.reverseTable[a]=i,i++,e=r<<1;!((a^=e>>=1)&e););a>=i&&(this.reverseTable[i]=a,this.reverseTable[a]=i,this.reverseTable[n-i]=n-a,this.reverseTable[n-a]=n-i),i++}while(i<r);this.reverseTable[n]=n},this.generateReverseTable()}setupTypedArray("Float32Array","WebGLFloatArray"),setupTypedArray("Int32Array","WebGLIntArray"),setupTypedArray("Uint16Array","WebGLUnsignedShortArray"),setupTypedArray("Uint8Array","WebGLUnsignedByteArray"),DSP.invert=function(e){for(var t=0,r=e.length;t<r;t++)e[t]*=-1;return e},DSP.interleave=function(e,t){if(e.length!==t.length)throw"Can not interleave. Channel lengths differ.";for(var r=new Float32Array(2*e.length),n=0,i=e.length;n<i;n++)r[2*n]=e[n],r[2*n+1]=t[n];return r},DSP.deinterleave=function(){var e,t,r,n=[];return n[DSP.MIX]=function(e){for(var t=0,n=e.length/2;t<n;t++)r[t]=(e[2*t]+e[2*t+1])/2;return r},n[DSP.LEFT]=function(t){for(var r=0,n=t.length/2;r<n;r++)e[r]=t[2*r];return e},n[DSP.RIGHT]=function(e){for(var r=0,n=e.length/2;r<n;r++)t[r]=e[2*r+1];return t},function(i,a){return e=e||new Float32Array(a.length/2),t=t||new Float32Array(a.length/2),r=r||new Float32Array(a.length/2),a.length/2!==e.length&&(e=new Float32Array(a.length/2),t=new Float32Array(a.length/2),r=new Float32Array(a.length/2)),n[i](a)}}(),DSP.getChannel=DSP.deinterleave,DSP.mixSampleBuffers=function(e,t,r,n){for(var i=new Float32Array(e),a=0;a<e.length;a++)i[a]+=(r?-t[a]:t[a])/n;return i},DSP.LPF=0,DSP.HPF=1,DSP.BPF_CONSTANT_SKIRT=2,DSP.BPF_CONSTANT_PEAK=3,DSP.NOTCH=4,DSP.APF=5,DSP.PEAKING_EQ=6,DSP.LOW_SHELF=7,DSP.HIGH_SHELF=8,DSP.Q=1,DSP.BW=2,DSP.S=3,DSP.RMS=function(e){for(var t=0,r=0,n=e.length;r<n;r++)t+=e[r]*e[r];return Math.sqrt(t/n)},DSP.Peak=function(e){for(var t=0,r=0,n=e.length;r<n;r++)t=Math.abs(e[r])>t?Math.abs(e[r]):t;return t},DFT.prototype.forward=function(e){for(var t,r,n=this.real,i=this.imag,a=0;a<this.bufferSize/2;a++){t=0,r=0;for(var o=0;o<e.length;o++)t+=this.cosTable[a*o]*e[o],r+=this.sinTable[a*o]*e[o];n[a]=t,i[a]=r}return this.calculateSpectrum()},FFT.prototype.forward=function(e){var t=this.bufferSize,r=this.cosTable,n=this.sinTable,i=this.reverseTable,a=this.real,o=this.imag,s=(this.spectrum,Math.floor(Math.log(t)/Math.LN2));if(Math.pow(2,s)!==t)throw"Invalid buffer size, must be a power of 2.";if(t!==e.length)throw"Supplied buffer is not the same size as defined FFT. FFT Size: "+t+" Buffer Size: "+e.length;var l,h,u,f,c,g,d,v,A,w=1;for(A=0;A<t;A++)a[A]=e[i[A]],o[A]=0;for(;w<t;){l=r[w],h=n[w],u=1,f=0;for(var p=0;p<w;p++){for(A=p;A<t;)g=u*a[c=A+w]-f*o[c],d=u*o[c]+f*a[c],a[c]=a[A]-g,o[c]=o[A]-d,a[A]+=g,o[A]+=d,A+=w<<1;u=(v=u)*l-f*h,f=v*h+f*l}w<<=1}return this.calculateSpectrum()},FFT.prototype.inverse=function(e,t){var r=this.bufferSize,n=this.cosTable,i=this.sinTable,a=this.reverseTable;this.spectrum;e=e||this.real,t=t||this.imag;var o,s,l,h,u,f,c,g,d,v=1;for(d=0;d<r;d++)t[d]*=-1;var A=new Float32Array(r),w=new Float32Array(r);for(d=0;d<e.length;d++)A[d]=e[a[d]],w[d]=t[a[d]];for(e=A,t=w;v<r;){o=n[v],s=i[v],l=1,h=0;for(var p=0;p<v;p++){for(d=p;d<r;)f=l*e[u=d+v]-h*t[u],c=l*t[u]+h*e[u],e[u]=e[d]-f,t[u]=t[d]-c,e[d]+=f,t[d]+=c,d+=v<<1;l=(g=l)*o-h*s,h=g*s+h*o}v<<=1}var F=new Float32Array(r);for(d=0;d<r;d++)F[d]=e[d]/r;return F},RFFT.prototype.forward=function(e){var t,r,n,i,a,o,s,l,h,u,f,c,g,d,v,A,w,p,F,y,T,b,S,m,D,M,P=this.bufferSize,C=this.spectrum,B=this.trans,R=2*Math.PI,U=Math.sqrt,L=P>>>1,E=2/P;this.reverseBinPermute(B,e);for(var G=0,N=4;G<P;N*=4){for(var I=G;I<P;I+=N)w=B[I]-B[I+1],B[I]+=B[I+1],B[I+1]=w;G=2*(N-1)}for(t=2,i=P>>>1;i>>>=1;){G=0,N=(t<<=1)<<1,r=t>>>2,n=t>>>3;do{if(1!==r)for(I=G;I<P;I+=N)c=(f=(u=(h=I)+r)+r)+r,a=B[f]+B[c],B[c]-=B[f],B[f]=B[h]-a,B[h]+=a,h+=n,u+=n,c+=n,a=B[f+=n]+B[c],o=B[f]-B[c],a=-a*Math.SQRT1_2,o*=Math.SQRT1_2,w=B[u],B[c]=a+w,B[f]=a-w,B[u]=B[h]-o,B[h]+=o;else for(I=G;I<P;I+=N)c=(f=(u=(h=I)+r)+r)+r,a=B[f]+B[c],B[c]-=B[f],B[f]=B[h]-a,B[h]+=a;G=(N<<1)-t,N<<=2}while(G<P);b=R/t;for(var k=1;k<n;k++){S=k*b,F=Math.sin(S),y=4*(p=Math.cos(S))*(p*p-.75),T=4*F*(.75-F*F),G=0,N=t<<1;do{for(I=G;I<P;I+=N)c=(f=(u=(h=I+k)+r)+r)+r,A=(v=(d=(g=I+r-k)+r)+r)+r,o=B[v]*p-B[f]*F,a=B[v]*F+B[f]*p,l=B[A]*y-B[c]*T,s=B[A]*T+B[c]*y,w=o-l,o+=l,l=w,B[A]=o+B[d],B[f]=o-B[d],w=s-a,a+=s,s=w,B[c]=s+B[u],B[v]=s-B[u],B[d]=B[h]-a,B[h]+=a,B[u]=l+B[g],B[g]-=l;G=(N<<1)-t,N<<=2}while(G<P)}}for(;--L;)(M=E*U((m=B[L])*m+(D=B[P-L-1])*D))>this.peak&&(this.peakBand=L,this.peak=M),C[L]=M;return C[0]=E*B[0],C};