UNPKG

audio-inspect

Version:

Lightweight yet powerful audio analysis library

2 lines (1 loc) 3.04 kB
var u=class e extends Error{constructor(a,r,n,o){super(r,{cause:n});this.code=a;this.details=o;this.cause=n,Error.captureStackTrace&&Error.captureStackTrace(this,e)}name="AudioInspectError";timestamp=new Date;cause;toJSON(){return{name:this.name,code:this.code,message:this.message,details:this.details,timestamp:this.timestamp,stack:this.stack,cause:this.cause}}};function D(e,i=0){if(i===-1){if(e.numberOfChannels===0)throw new u("INVALID_INPUT","No channels available");for(let n=0;n<e.numberOfChannels;n++)if(!e.channelData[n])throw new u("INVALID_INPUT",`Channel ${n} data does not exist for averaging`);let r=new Float32Array(e.length);for(let n=0;n<e.length;n++){let o=0;for(let s=0;s<e.numberOfChannels;s++){let c=e.channelData[s];c&&n<c.length&&(o+=c[n]??0)}r[n]=o/e.numberOfChannels}return r}if(i<-1||i>=e.numberOfChannels)throw new u("INVALID_INPUT",`Invalid channel number: ${i}`);let a=e.channelData[i];if(!a)throw new u("INVALID_INPUT",`Channel ${i} data does not exist`);return a}function E(e){return e==null||isNaN(e)||!isFinite(e)?0:e}function O(e,i,a,r){let n=new Float32Array(r);for(let o=0;o<r&&a+o<e.length;o++){let s=1;switch(i){case"hann":s=.5*(1-Math.cos(2*Math.PI*o/(r-1)));break;case"hamming":s=.54-.46*Math.cos(2*Math.PI*o/(r-1));break;case"rectangular":default:s=1}let c=E(e[a+o]??0);n[o]=c*s}return n}function W(e,i={}){let{frameSize:a=Math.floor(e.sampleRate*.025),hopSize:r=Math.floor(e.sampleRate*.01),channel:n=0,normalized:o=!1,windowFunction:s="rectangular"}=i;if(a<=0||!Number.isInteger(a))throw new u("INVALID_INPUT","frameSize\u306F\u6B63\u306E\u6574\u6570\u3067\u3042\u308B\u5FC5\u8981\u304C\u3042\u308A\u307E\u3059");if(r<=0||!Number.isInteger(r))throw new u("INVALID_INPUT","hopSize\u306F\u6B63\u306E\u6574\u6570\u3067\u3042\u308B\u5FC5\u8981\u304C\u3042\u308A\u307E\u3059");r>a&&console.warn("[audio-inspect] hopSize\u304CframeSize\u3088\u308A\u5927\u304D\u3044\u305F\u3081\u3001\u30D5\u30EC\u30FC\u30E0\u9593\u306B\u30AE\u30E3\u30C3\u30D7\u304C\u751F\u3058\u307E\u3059");let c=D(e,n),g=c.length;if(g===0)return{times:new Float32Array(0),energies:new Float32Array(0),totalEnergy:0,statistics:{mean:0,std:0,max:0,min:0}};let m=Math.max(0,Math.floor((g-a)/r)+1);if(m===0){let t=C(c,0,g,s);return{times:new Float32Array([g/2/e.sampleRate]),energies:new Float32Array([t]),totalEnergy:t,statistics:{mean:t,std:0,max:t,min:t}}}let A=new Float32Array(m),b=new Float32Array(m),l=0,d=-1/0,y=1/0;for(let t=0;t<m;t++){let f=t*r,p=O(c,s,f,a),h=0;for(let x=0;x<p.length;x++){let F=p[x];F!==void 0&&(h+=F*F)}A[t]=(f+a/2)/e.sampleRate,b[t]=h,l+=h,d=Math.max(d,h),y=Math.min(y,h)}let w=l/m,I=0;for(let t=0;t<m;t++){let f=b[t];if(f!==void 0){let p=f-w;I+=p*p}}let M=Math.sqrt(I/m);if(o&&l>1e-10){for(let t=0;t<b.length;t++){let f=b[t];f!==void 0&&(b[t]=f/l)}return{times:A,energies:b,totalEnergy:1,statistics:{mean:w/l,std:M/l,max:d/l,min:y/l}}}return{times:A,energies:b,totalEnergy:l,statistics:{mean:w,std:M,max:d,min:y}}}function C(e,i,a,r){let n=O(e,r,i,a),o=0;for(let s of n)o+=s*s;return o}export{W as getEnergy};