UNPKG

audio-inspect

Version:

Lightweight yet powerful audio analysis library

2 lines (1 loc) 3.5 kB
"use strict";var I=Object.defineProperty;var S=Object.getOwnPropertyDescriptor;var N=Object.getOwnPropertyNames;var P=Object.prototype.hasOwnProperty;var _=(e,t)=>{for(var r in t)I(e,r,{get:t[r],enumerable:!0})},T=(e,t,r,o)=>{if(t&&typeof t=="object"||typeof t=="function")for(let n of N(t))!P.call(e,n)&&n!==r&&I(e,n,{get:()=>t[n],enumerable:!(o=S(t,n))||o.enumerable});return e};var v=e=>T(I({},"__esModule",{value:!0}),e);var z={};_(z,{getEnergy:()=>q});module.exports=v(z);var u=class e extends Error{constructor(r,o,n,s){super(o,{cause:n});this.code=r;this.details=s;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 E(e,t=0){if(t===-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 o=new Float32Array(e.length);for(let n=0;n<e.length;n++){let s=0;for(let i=0;i<e.numberOfChannels;i++){let c=e.channelData[i];c&&n<c.length&&(s+=c[n]??0)}o[n]=s/e.numberOfChannels}return o}if(t<-1||t>=e.numberOfChannels)throw new u("INVALID_INPUT",`Invalid channel number: ${t}`);let r=e.channelData[t];if(!r)throw new u("INVALID_INPUT",`Channel ${t} data does not exist`);return r}function O(e){return e==null||isNaN(e)||!isFinite(e)?0:e}function C(e,t,r,o){let n=new Float32Array(o);for(let s=0;s<o&&r+s<e.length;s++){let i=1;switch(t){case"hann":i=.5*(1-Math.cos(2*Math.PI*s/(o-1)));break;case"hamming":i=.54-.46*Math.cos(2*Math.PI*s/(o-1));break;case"rectangular":default:i=1}let c=O(e[r+s]??0);n[s]=c*i}return n}function q(e,t={}){let{frameSize:r=Math.floor(e.sampleRate*.025),hopSize:o=Math.floor(e.sampleRate*.01),channel:n=0,normalized:s=!1,windowFunction:i="rectangular"}=t;if(r<=0||!Number.isInteger(r))throw new u("INVALID_INPUT","frameSize\u306F\u6B63\u306E\u6574\u6570\u3067\u3042\u308B\u5FC5\u8981\u304C\u3042\u308A\u307E\u3059");if(o<=0||!Number.isInteger(o))throw new u("INVALID_INPUT","hopSize\u306F\u6B63\u306E\u6574\u6570\u3067\u3042\u308B\u5FC5\u8981\u304C\u3042\u308A\u307E\u3059");o>r&&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=E(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-r)/o)+1);if(m===0){let a=k(c,0,g,i);return{times:new Float32Array([g/2/e.sampleRate]),energies:new Float32Array([a]),totalEnergy:a,statistics:{mean:a,std:0,max:a,min:a}}}let A=new Float32Array(m),b=new Float32Array(m),l=0,d=-1/0,y=1/0;for(let a=0;a<m;a++){let f=a*o,p=C(c,i,f,r),h=0;for(let x=0;x<p.length;x++){let F=p[x];F!==void 0&&(h+=F*F)}A[a]=(f+r/2)/e.sampleRate,b[a]=h,l+=h,d=Math.max(d,h),y=Math.min(y,h)}let w=l/m,M=0;for(let a=0;a<m;a++){let f=b[a];if(f!==void 0){let p=f-w;M+=p*p}}let D=Math.sqrt(M/m);if(s&&l>1e-10){for(let a=0;a<b.length;a++){let f=b[a];f!==void 0&&(b[a]=f/l)}return{times:A,energies:b,totalEnergy:1,statistics:{mean:w/l,std:D/l,max:d/l,min:y/l}}}return{times:A,energies:b,totalEnergy:l,statistics:{mean:w,std:D,max:d,min:y}}}function k(e,t,r,o){let n=C(e,o,t,r),s=0;for(let i of n)s+=i*i;return s}