UNPKG

audio-inspect

Version:

Lightweight yet powerful audio analysis library

1 lines 58.1 kB
"use strict";var At=Object.create;var j=Object.defineProperty;var wt=Object.getOwnPropertyDescriptor;var St=Object.getOwnPropertyNames;var Mt=Object.getPrototypeOf,xt=Object.prototype.hasOwnProperty;var It=(e,r)=>{for(var t in r)j(e,t,{get:r[t],enumerable:!0})},pe=(e,r,t,n)=>{if(r&&typeof r=="object"||typeof r=="function")for(let a of St(r))!xt.call(e,a)&&a!==t&&j(e,a,{get:()=>r[a],enumerable:!(n=wt(r,a))||n.enumerable});return e};var vt=(e,r,t)=>(t=e!=null?At(Mt(e)):{},pe(r||!e||!e.__esModule?j(t,"default",{value:e,enumerable:!0}):t,e)),Tt=e=>pe(j({},"__esModule",{value:!0}),e);var xn={};It(xn,{getCQT:()=>ot,getCrestFactor:()=>mt,getEnergy:()=>st,getFFT:()=>V,getLUFS:()=>Ft,getLUFSRealtime:()=>he,getMFCC:()=>le,getMFCCWithDelta:()=>it,getMelSpectrogram:()=>at,getPeak:()=>W,getPeakAmplitude:()=>W,getPeaks:()=>Fe,getPeaksAnalysis:()=>ve,getRMS:()=>H,getRMSAnalysis:()=>Te,getSpectralCrest:()=>Je,getSpectralEntropy:()=>Ye,getSpectralFeatures:()=>Ze,getSpectrogram:()=>_e,getSpectrum:()=>Be,getStereoAnalysis:()=>ft,getTimeVaryingSpectralFeatures:()=>Xe,getTimeVaryingStereoAnalysis:()=>ht,getVAD:()=>pt,getWaveform:()=>we,getWaveformAnalysis:()=>Ie,getZeroCrossing:()=>Ae});module.exports=Tt(xn);var w=class e extends Error{name="AudioInspectError";code;cause;details;constructor(r,t,n,a){super(t,{cause:n}),this.code=r,this.cause=n,this.details=a,Error.captureStackTrace&&Error.captureStackTrace(this,e)}};function O(){if(typeof performance<"u"&&performance.now)return performance.now();if(typeof process<"u"&&process.hrtime){let[e,r]=process.hrtime();return e*1e3+r/1e6}return Date.now()}function Pt(e){return Array.isArray(e)}function z(e,r="mix"){if(e.numberOfChannels<=0)throw new w("INVALID_INPUT","No channels available");let t=Array.from({length:e.numberOfChannels},(o,i)=>i),n;if(r==="mix")n=t;else{if(r==="all")throw new w("INVALID_INPUT",'Channel selector "all" is not supported for scalar results; use "mix" or a single channel index');if(Pt(r)){if(r.length===0)throw new w("INVALID_INPUT","Channel selection array cannot be empty");if(r.length>1)throw new w("INVALID_INPUT",'Multi-channel selector arrays are not supported for scalar results; use "mix" or a single channel index');n=r.slice()}else if(typeof r=="number"){if(!Number.isInteger(r))throw new w("INVALID_INPUT",`Invalid channel number: ${r}`);n=[r]}else throw new w("INVALID_INPUT",`Invalid channel selector: ${String(r)}`)}for(let o of n){if(o<0||o>=e.numberOfChannels)throw new w("INVALID_INPUT",`Invalid channel number: ${o}`);if(!e.channelData[o])throw new w("INVALID_INPUT",`Channel ${o} data does not exist`)}if(n.length===1){let o=n[0];return e.channelData[o]}let a=new Float32Array(e.length);for(let o=0;o<e.length;o++){let i=0;for(let s of n){let l=e.channelData[s];l&&o<l.length&&(i+=l[o])}a[o]=i/n.length}return a}function Rt(e){return Number.isInteger(e)&&e>0&&(e&e-1)===0}function Q(e){if(!Number.isFinite(e)||e<=1)return 1;if(Rt(e))return e;let r=1;for(;r<e&&r<Number.MAX_SAFE_INTEGER;)r*=2;return r}function de(e,r,t){return r<0||r>=e.length?t:e[r]??t}function k(e,r=1,t=-1/0){return!Number.isFinite(e)||!Number.isFinite(r)||e<=0||r<=0?t:20*Math.log10(e/r)}function B(e,r=1,t=-1/0){return!Number.isFinite(e)||!Number.isFinite(r)||e<=0||r<=0?t:10*Math.log10(e/r)}function Ct(e){if(![2,4,8].includes(e))throw new w("INVALID_INPUT","Oversampling factor must be 2, 4, or 8. Sinc interpolation quality is not guaranteed for other values.")}function K(e,r={}){if(e.length===0)return 0;let{factor:t=4,interpolation:n="cubic"}=r;Ct(t);let a=0;if(t<=1||e.length===1){for(let i=0;i<e.length;i++){let s=Math.abs(e[i]);s>a&&(a=s)}return a}if(n==="sinc"){let i=l=>{if(Math.abs(l)<1e-10)return 1;let c=Math.PI*l;return Math.abs(l)<3?3*Math.sin(c/3)*Math.sin(c)/(c*c):0},s=(e.length-1)*t+1;for(let l=0;l<s;l++){let c=l/t,m=0;for(let u=-3;u<=3;u++){let h=Math.floor(c)+u;h>=0&&h<e.length&&(m+=e[h]*i(c-h))}let f=Math.abs(m);f>a&&(a=f)}return a}for(let i=0;i<e.length-1;i++){let s=e[i],l=e[i+1],c=s,m=l;n==="cubic"&&(c=e[Math.max(0,i-1)],m=e[Math.min(e.length-1,i+2)]);for(let f=0;f<t;f++){let u=f/t,h=n==="linear"?s+(l-s)*u:.5*(2*s+(-c+l)*u+(2*c-5*s+4*l-m)*u*u+(-c+3*s-3*l+m)*u*u*u),b=Math.abs(h);b>a&&(a=b)}}let o=Math.abs(e[e.length-1]);return o>a&&(a=o),a}var be=Object.freeze([Object.freeze([.001708984375,.010986328125,-.0196533203125,.033203125,-.0594482421875,.1373291015625,.97216796875,-.102294921875,.047607421875,-.026611328125,.014892578125,-.00830078125]),Object.freeze([-.0291748046875,.029296875,-.0517578125,.089111328125,-.16650390625,.465087890625,.77978515625,-.2003173828125,.1015625,-.0582275390625,.0330810546875,-.0189208984375]),Object.freeze([-.0189208984375,.0330810546875,-.0582275390625,.1015625,-.2003173828125,.77978515625,.465087890625,-.16650390625,.089111328125,-.0517578125,.029296875,-.0291748046875]),Object.freeze([-.00830078125,.014892578125,-.026611328125,.047607421875,-.102294921875,.97216796875,.1373291015625,-.0594482421875,.033203125,-.0196533203125,.010986328125,.001708984375])]);function Dt(e){if(e!==2&&e!==4)throw new w("INVALID_INPUT","BS.1770 true peak supports oversampling factor 2 or 4")}function zt(e){let r=0;for(let t=0;t<e.length;t++){let n=Math.abs(e[t]);n>r&&(r=n)}return r}function kt(e,r){let t=be[0];if(!t||t.length===0)return 0;let n=t.length,a=new Float64Array(n),o=0,i=0;for(let s=0;s<e.length;s++){a[o]=e[s];for(let l of r){let c=be[l];if(!c)continue;let m=0;for(let u=0;u<n;u++){let h=o-u;h<0&&(h+=n),m+=a[h]*c[u]}let f=Math.abs(m);f>i&&(i=f)}o=(o+1)%n}return i}function ge(e,r={}){if(e.length===0)return 0;let t=r.factor??4;Dt(t);let n=zt(e),o=kt(e,t===2?[0,2]:[0,1,2,3]);return o>n&&(n=o),n}function ye(e,r,t=!1){let n=[],a=e.length;if(a<3)return n;for(let o=1;o<a-1;o++){let i=Math.abs(e[o]),s=Math.abs(e[o-1]),l=Math.abs(e[o+1]);if(i>s&&i>l&&i>r){let c={position:o,amplitude:i};t&&(c.prominence=Nt(e,o,i)),n.push(c)}}return n}function Nt(e,r,t){let n=t;for(let o=r-1;o>=0;o--){let i=Math.abs(e[o]);if(i>t)break;n=Math.min(n,i)}let a=t;for(let o=r+1;o<e.length;o++){let i=Math.abs(e[o]);if(i>t)break;a=Math.min(a,i)}return t-Math.max(n,a)}function Fe(e,r={}){let{count:t=100,threshold:n=.1,channel:a="mix",minDistance:o=Math.floor(e.sampleRate/100)}=r;if(t<=0)throw new w("INVALID_INPUT","Peak count must be a positive integer");if(n<0||n>1)throw new w("INVALID_INPUT","Threshold must be in the range [0, 1]");let i=z(e,a);if(i.length===0)return{peaks:[],maxAmplitude:0,averageAmplitude:0};let s=ye(i,n);if(s.length===0)return{peaks:[],maxAmplitude:0,averageAmplitude:0};s.sort((u,h)=>h.amplitude-u.amplitude);let l=[],c=[];for(let u of s){if(l.length>=t)break;let h=u.position-o,b=u.position+o;c.some(([d,F])=>!(b<d||h>F))||(l.push({position:u.position,time:u.position/e.sampleRate,amplitude:u.amplitude}),c.push([h,b]))}l.sort((u,h)=>u.position-h.position);let m=s.length>0?s[0]?.amplitude??0:0,f=s.length>0?s.reduce((u,h)=>u+h.amplitude,0)/s.length:0;return{peaks:l,maxAmplitude:m,averageAmplitude:f}}var J=-1/0;function H(e,r={}){if(typeof r!="object"||r===null||Array.isArray(r))throw new w("INVALID_INPUT","RMS options must be an object");let t={channel:r.channel??"mix",asDB:r.asDB??!1,reference:r.reference??1,truePeak:r.truePeak??!1,oversamplingFactor:r.oversamplingFactor??4,interpolation:r.interpolation??"cubic"},n=z(e,t.channel);if(n.length===0)return t.asDB?J:0;let a=0,o=0;for(let s=0;s<n.length;s++){let l=n[s];Number.isFinite(l)&&(a+=l*l,o++)}if(o===0)return t.asDB?J:0;let i=Math.sqrt(a/o);return t.asDB?k(i,t.reference):i}function W(e,r={}){let t={channel:r.channel??"mix",asDB:r.asDB??!1,reference:r.reference??1,truePeak:r.truePeak??!1,oversamplingFactor:r.oversamplingFactor??4,interpolation:r.interpolation??"cubic"},n=z(e,t.channel);if(n.length===0)return t.asDB?J:0;let a;if(t.truePeak)a=K(n,{factor:t.oversamplingFactor,interpolation:t.interpolation});else{a=0;for(let o=0;o<n.length;o++)a=Math.max(a,Math.abs(n[o]))}return t.asDB?k(a,t.reference):a}function Ae(e,r="mix"){let t=z(e,r);if(t.length<2)return 0;let n=0;for(let a=1;a<t.length;a++){let o=t[a-1],i=t[a];(o>=0&&i<0||o<0&&i>=0)&&n++}return n/(t.length-1)}function we(e,r={}){let{framesPerSecond:t=60,channel:n="mix",method:a="rms"}=r,o=z(e,n),i=Math.ceil(e.duration*t),s=e.length>0?e.length:i>0?1:0,l=Math.min(i,s),c=l>0?Math.max(1,Math.floor(e.length/l)):0,m=[],f=0,u=0;for(let b=0;b<l;b++){let p=b*c,d=Math.min(p+c,o.length);if(d<=p){let S=m.length>0?de(m,m.length-1,{time:0,amplitude:0}).amplitude:0;m.push({time:(p+c/2)/e.sampleRate,amplitude:S});continue}let F=o.subarray(p,d),y;switch(a){case"peak":y=Me(F);break;case"average":y=xe(F);break;case"rms":default:y=Se(F);break}let g=(p+(d-p)/2)/e.sampleRate;m.push({time:g,amplitude:y}),f=Math.max(f,y),u+=y}let h=l>0?u/l:0;return{waveform:m,maxAmplitude:f,averageAmplitude:h,frameCount:l,samplesPerFrame:c}}function Se(e){if(e.length===0)return 0;let r=0;for(let t=0;t<e.length;t++){let n=e[t];r+=n*n}return Math.sqrt(r/e.length)}function Me(e){let r=0;for(let t=0;t<e.length;t++){let n=Math.abs(e[t]);r=Math.max(r,n)}return r}function xe(e){if(e.length===0)return 0;let r=0;for(let t=0;t<e.length;t++)r+=Math.abs(e[t]);return r/e.length}function Ie(e,r={}){let t=O(),{framesPerSecond:n=60,channel:a="mix",method:o="rms",onProgress:i}=r;i?.(0,"Starting waveform analysis");let s=z(e,a),l=Math.ceil(e.duration*n),c=e.length>0?e.length:l>0?1:0,m=Math.min(l,c),f=m>0?Math.max(1,Math.floor(e.length/m)):0;i?.(25,"Frame configuration complete");let u=new Float32Array(m),h=new Float32Array(m),b=0,p=0;i?.(50,"Starting amplitude calculation");for(let y=0;y<m;y++){let g=y*f,S=Math.min(g+f,s.length);if(S<=g){let I=y>0?u[y-1]:0;u[y]=I,h[y]=(g+f/2)/e.sampleRate;continue}let x=s.subarray(g,S),A;switch(o){case"peak":A=Me(x);break;case"average":A=xe(x);break;case"rms":default:A=Se(x);break}if(u[y]=A,h[y]=(g+(S-g)/2)/e.sampleRate,b=Math.max(b,A),p+=A,y%Math.max(1,Math.floor(m/20))===0){let I=50+y/m*45;i?.(Math.round(I),`Processing frame ${y+1}/${m}`)}}let d=m>0?p/m:0,F=O()-t;return i?.(100,"Processing complete"),{amplitudes:u,timestamps:h,frameCount:m,samplesPerFrame:f,framesPerSecond:n,maxAmplitude:b,averageAmplitude:d,sampleRate:e.sampleRate,duration:e.duration,processingTime:F}}function ve(e,r={}){let t=O(),{count:n=100,threshold:a=.1,channel:o="mix",minDistance:i=Math.floor(e.sampleRate/100),onProgress:s}=r;if(s?.(0,"Starting peaks analysis"),n<=0)throw new w("INVALID_INPUT","Peak count must be a positive integer");if(a<0||a>1)throw new w("INVALID_INPUT","Threshold must be in the range [0, 1]");let l=z(e,o);if(l.length===0)return{positions:new Float32Array(0),amplitudes:new Float32Array(0),times:new Float32Array(0),maxAmplitude:0,averageAmplitude:0,count:0,sampleRate:e.sampleRate,duration:e.duration,processingTime:O()-t};s?.(25,"Detecting peak candidates");let c=ye(l,a);if(c.length===0)return{positions:new Float32Array(0),amplitudes:new Float32Array(0),times:new Float32Array(0),maxAmplitude:0,averageAmplitude:0,count:0,sampleRate:e.sampleRate,duration:e.duration,processingTime:O()-t};s?.(50,"Sorting peaks"),c.sort((y,g)=>g.amplitude-y.amplitude),s?.(75,"Selecting peaks");let m=[],f=[];for(let y of c){if(m.length>=n)break;let g=y.position-i,S=y.position+i;f.some(([A,I])=>!(S<A||g>I))||(m.push({position:y.position,time:y.position/e.sampleRate,amplitude:y.amplitude}),f.push([g,S]))}m.sort((y,g)=>y.position-g.position);let u=new Float32Array(m.length),h=new Float32Array(m.length),b=new Float32Array(m.length);for(let y=0;y<m.length;y++){let g=m[y];g&&(u[y]=g.position,h[y]=g.amplitude,b[y]=g.time)}let p=c.length>0?c[0]?.amplitude??0:0,d=c.length>0?c.reduce((y,g)=>y+g.amplitude,0)/c.length:0,F=O()-t;return s?.(100,"Processing complete"),{positions:u,amplitudes:h,times:b,maxAmplitude:p,averageAmplitude:d,count:m.length,sampleRate:e.sampleRate,duration:e.duration,processingTime:F}}function Te(e,r={}){let t=O(),{channel:n="mix",asDB:a=!1,reference:o=1,onProgress:i}=r,s=typeof n=="number"?n:-1;i?.(0,"Starting RMS analysis");let l=z(e,n);if(l.length===0)return{value:a?-1/0:0,channel:s,sampleRate:e.sampleRate,duration:e.duration,processingTime:O()-t};i?.(50,"Computing RMS value");let c=0,m=0;for(let h=0;h<l.length;h++){let b=l[h];Number.isFinite(b)&&(c+=b*b,m++)}if(m===0){let h=a?-1/0:0,b=O()-t;return i?.(100,"Processing complete"),a?{value:h,channel:s,sampleRate:e.sampleRate,duration:e.duration,processingTime:b}:{value:h,valueDB:-1/0,channel:s,sampleRate:e.sampleRate,duration:e.duration,processingTime:b}}let f=Math.sqrt(c/m),u=O()-t;return i?.(100,"Processing complete"),a?{value:k(f,o),channel:s,sampleRate:e.sampleRate,duration:e.duration,processingTime:u}:{value:f,valueDB:k(f,o),channel:s,sampleRate:e.sampleRate,duration:e.duration,processingTime:u}}function Pe(e,r){let t=new Float32Array(e/2+1),n=r/e;for(let a=0;a<t.length;a++)t[a]=a*n;return t}var ee=class{constructor(r,t,n=!1){this.size=r;this.sampleRate=t;this.enableProfiling=n;this.complexInput=new Float32Array(this.size*2),this.frequencyBins=Pe(this.size,this.sampleRate),this.initializationPromise=this.initializeWebFFT()}fftInstance=null;initializationPromise=null;complexInput;frequencyBins;get name(){return"WebFFT"}async waitForInitialization(){this.initializationPromise&&await this.initializationPromise}async initializeWebFFT(){try{let t=(await import("webfft")).default;this.fftInstance=new t(this.size),this.enableProfiling&&this.fftInstance?.profile&&await this.fftInstance.profile()}catch(r){throw new w("UNSUPPORTED_FORMAT",`Failed to initialize WebFFT: ${r instanceof Error?r.message:String(r)}`)}}async fft(r){if(this.initializationPromise&&await this.initializationPromise,!this.fftInstance)throw new w("UNSUPPORTED_FORMAT","WebFFT initialization failed");if(r.length!==this.size)throw new w("INVALID_INPUT",`Invalid input size. Expected: ${this.size}, Actual: ${r.length}`);for(let o=0;o<this.size;o++)this.complexInput[o*2]=r[o],this.complexInput[o*2+1]=0;let t=this.fftInstance.fft(this.complexInput),n=new Float32Array(this.size/2+1),a=new Float32Array(this.size/2+1);for(let o=0;o<n.length;o++){let i=t[o*2],s=t[o*2+1];n[o]=Math.sqrt(i*i+s*s),a[o]=Math.atan2(s,i)}return{complex:t,magnitude:n,phase:a,frequencies:this.frequencyBins}}async profile(){if(this.initializationPromise&&await this.initializationPromise,!this.fftInstance||!this.fftInstance.profile)throw new w("UNSUPPORTED_FORMAT","WebFFT is not initialized");await this.fftInstance.profile()}dispose(){if(this.fftInstance)try{this.fftInstance.dispose&&this.fftInstance.dispose()}catch(r){console.warn("FFT instance disposal failed:",r)}finally{this.fftInstance=null,this.initializationPromise=null}}},te=class{constructor(r,t){this.size=r;this.sampleRate=t;if(!this.isPowerOfTwo(r))throw new w("INVALID_INPUT","FFT size must be a power of two");this.precomputeTables()}bitReversalTable;twiddleFactorsReal;twiddleFactorsImag;scratchReal;scratchImag;frequencyBins;get name(){return"Native FFT (Cooley-Tukey)"}isPowerOfTwo(r){return r>0&&(r&r-1)===0}precomputeTables(){this.bitReversalTable=new Uint32Array(this.size);let r=Math.log2(this.size);for(let n=0;n<this.size;n++){let a=0;for(let o=0;o<r;o++)a=a<<1|n>>o&1;this.bitReversalTable[n]=a}let t=this.size/2;this.twiddleFactorsReal=new Float32Array(t),this.twiddleFactorsImag=new Float32Array(t);for(let n=0;n<t;n++){let a=-2*Math.PI*n/this.size;this.twiddleFactorsReal[n]=Math.cos(a),this.twiddleFactorsImag[n]=Math.sin(a)}this.scratchReal=new Float32Array(this.size),this.scratchImag=new Float32Array(this.size),this.frequencyBins=Pe(this.size,this.sampleRate)}fft(r){if(r.length!==this.size)throw new w("INVALID_INPUT",`Invalid input size. Expected: ${this.size}, Actual: ${r.length}`);let t=this.scratchReal,n=this.scratchImag;for(let s=0;s<this.size;s++){let l=this.bitReversalTable[s];t[l]=r[s],n[l]=0}for(let s=1;s<this.size;s*=2){let l=s*2,c=this.size/l;for(let m=0;m<this.size;m+=l)for(let f=0;f<s;f++){let u=f*c,h=this.twiddleFactorsReal[u],b=this.twiddleFactorsImag[u],p=m+f,d=m+f+s,F=t[p],y=n[p],g=t[d],S=n[d],x=g*h-S*b,A=g*b+S*h;t[p]=F+x,n[p]=y+A,t[d]=F-x,n[d]=y-A}}let a=new Float32Array(this.size*2),o=new Float32Array(this.size/2+1),i=new Float32Array(this.size/2+1);for(let s=0;s<this.size;s++)if(a[s*2]=t[s],a[s*2+1]=n[s],s<=this.size/2){let l=t[s],c=n[s];o[s]=Math.sqrt(l*l+c*c),i[s]=Math.atan2(c,l)}return{complex:a,magnitude:o,phase:i,frequencies:this.frequencyBins}}dispose(){this.scratchReal=new Float32Array(0),this.scratchImag=new Float32Array(0),this.bitReversalTable=new Uint32Array(0),this.twiddleFactorsReal=new Float32Array(0),this.twiddleFactorsImag=new Float32Array(0),this.frequencyBins=new Float32Array(0)}},G=class{static async createProvider(r){switch(r.type){case"webfft":{let t=new ee(r.fftSize,r.sampleRate,r.enableProfiling);return await t.waitForInitialization(),t}case"native":return new te(r.fftSize,r.sampleRate);case"custom":if(!r.customProvider)throw new w("INVALID_INPUT","Custom provider is not specified");return r.customProvider;default:{let t=r.type;throw new w("UNSUPPORTED_FORMAT",`Unsupported FFT provider: ${String(t)}`)}}}static getAvailableProviders(){return["webfft","native"]}};function qt(e){let r=e.provider??"native",t=e.enableProfiling?1:0,n=e.fallbackToNative?1:0;return`${r}|${e.fftSize}|${e.sampleRate}|${t}|${n}`}var ne=class{providers=new Map;async getOrCreate(r){let t=qt(r),n=this.providers.get(t);if(n)return n;let a=await Re(r);return this.providers.set(t,a),a}clear(){for(let r of this.providers.values())r.dispose();this.providers.clear()}},Ot=new ne;async function E(e){let{cache:r,useSharedCache:t=!1,...n}=e,a=r??(t?Ot:void 0);if(a)return{provider:await a.getOrCreate(n),release:()=>{}};let o=await Re(n);return{provider:o,release:()=>{o.dispose()}}}async function Re(e){let{fftSize:r,sampleRate:t,provider:n="native",enableProfiling:a=!1,fallbackToNative:o=n==="webfft",resolver:i=G}=e,s=o&&n!=="native"?[n,"native"]:[n],l;for(let c of s)try{return await i.createProvider({type:c,fftSize:r,sampleRate:t,enableProfiling:a})}catch(m){l=m}throw new w("INITIALIZATION_FAILED","Failed to create FFT provider",l)}function De(e){return e==="none"?"rectangular":e}function Et(e,r,t){let n=t?.kaiserBeta??8.6,a=t?.tukeyAlpha??.5;return`${r}|${e}|${n}|${a}`}function Ce(e){let r=1,t=1,n=e*e/4;for(let a=1;a<50&&(t*=n/(a*a),r+=t,!(t<1e-10*r));a++);return r}function Lt(e,r,t){if(!Number.isInteger(e)||e<0)throw new w("INVALID_INPUT",`Invalid window length: ${e}`);if(e===0)return new Float32Array(0);let n=new Float32Array(e);if(e===1||r==="rectangular")return n.fill(1),n;switch(r){case"hann":{for(let a=0;a<e;a++)n[a]=.5*(1-Math.cos(2*Math.PI*a/(e-1)));return n}case"hamming":{for(let a=0;a<e;a++)n[a]=.54-.46*Math.cos(2*Math.PI*a/(e-1));return n}case"blackman":{for(let a=0;a<e;a++)n[a]=.42-.5*Math.cos(2*Math.PI*a/(e-1))+.08*Math.cos(4*Math.PI*a/(e-1));return n}case"bartlett":{for(let a=0;a<e;a++)n[a]=1-Math.abs((a-(e-1)/2)/((e-1)/2));return n}case"kaiser":{let a=t?.kaiserBeta??8.6,o=Ce(a);for(let i=0;i<e;i++){let s=2*i/(e-1)-1;n[i]=Ce(a*Math.sqrt(1-s*s))/o}return n}case"tukey":{let a=t?.tukeyAlpha??.5;if(a<=0)return n.fill(1),n;if(a>=1){for(let i=0;i<e;i++)n[i]=.5*(1-Math.cos(2*Math.PI*i/(e-1)));return n}let o=a*(e-1)/2;for(let i=0;i<e;i++)i<o?n[i]=.5*(1+Math.cos(Math.PI*(2*i/(a*(e-1))-1))):i<=(e-1)*(1-a/2)?n[i]=1:n[i]=.5*(1+Math.cos(Math.PI*(2*i/(a*(e-1))-2/a+1)));return n}default:throw new w("INVALID_INPUT",`Unknown window type: ${String(r)}`)}}var re=class{cache=new Map;getOrCreate(r,t,n){let a=De(t),o=Et(r,a,n),i=this.cache.get(o);if(i)return i;let s=Lt(r,a,n);return this.cache.set(o,s),s}clear(){this.cache.clear()}},ze=new re;function Z(e,r,t){return ze.getOrCreate(e,r,t)}function L(e){let{src:r,srcStart:t,frameLength:n,dst:a,windowType:o,windowParams:i,cache:s=ze}=e;if(a.fill(0),a.length===0||n<=0||t>=r.length)return a;let l=Math.max(0,t),c=Math.min(n,a.length),m=Math.max(0,Math.min(c,r.length-l));if(m===0)return a;let f=De(o);if(f==="rectangular"||c===1)return a.set(r.subarray(l,l+m),0),a;let u=s.getOrCreate(c,f,i);for(let h=0;h<m;h++)a[h]=r[l+h]*u[h];return a}var ae=new WeakMap;function Bt(e){return Array.isArray(e)?`[${e.map(r=>String(r)).join(",")}]`:String(e)}function _t(e){return[e.fftSize,e.windowFunction,Bt(e.channel),e.provider,e.enableProfiling?1:0,e.normalization].join("|")}function ke(e,r){if(!Number.isInteger(e)||e<=0||(e&e-1)!==0)throw new w("INVALID_INPUT",`${r} must be a power of two`)}function oe(e,r){if(!Number.isInteger(e)||e<=0)throw new w("INVALID_INPUT",`${r} must be a positive integer`)}function Ne(e,r,t){if(!Number.isFinite(r)||!Number.isFinite(t)||r<0)throw new w("INVALID_INPUT",`Invalid frequency range: min=${r}, max=${t}`);let n=e/2;if(r>=t||t>n)throw new w("INVALID_INPUT",`Frequency range must satisfy 0 <= min < max <= Nyquist (${n})`)}function qe(e,r,t){let n=e.findIndex(i=>i>=r);n<0&&(n=0);let a=e.findIndex(i=>i>t),o=a===-1?e.length:a;return n>=o?{startIndex:0,endIndex:0}:{startIndex:n,endIndex:o}}function Vt(e,r){if(r<=1||e==="none")return 1;let t=Z(r,e),n=0;for(let o=0;o<t.length;o++)n+=t[o]??0;let a=n/r;return a>0?a:1}function Oe(e,r,t,n){if(r==="none")return e;if(t<=0||e.length===0)return e.fill(0),e;let a=Vt(n,t),o=t*a;if(o<=0)return e.fill(0),e;let i=1/o,s=i*2,l=e.length-1;for(let c=0;c<e.length;c++){let m=c===0||c===l?i:s;e[c]=(e[c]??0)*m}return e}function Ee(e,r){if(r==="amplitude")return e.slice();let t=new Float32Array(e.length);for(let n=0;n<e.length;n++)t[n]=k(e[n]??0,1);return t}function Wt(e,r,t){return e===0?0:e<r?1:Math.floor((e-r)/t)+1}function Le(e,r){if(e==="dbfs"&&r!=="amplitude")throw new w("INVALID_INPUT",'scale="dbfs" requires normalization="amplitude"')}async function V(e,r={}){let{fftSize:t=2048,windowFunction:n="hann",channel:a="mix",provider:o="native",enableProfiling:i=!1,providerCache:s,normalization:l="amplitude"}=r;ke(t,"FFT size");let c=_t({fftSize:t,windowFunction:n,channel:a,provider:o,enableProfiling:i,normalization:l}),m=ae.get(e);m||(m=new Map,ae.set(e,m));let f=m.get(c);if(f)return f;let u=(async()=>{let b=z(e,a),p=new Float32Array(t),d=Math.min(b.length,t);L({src:b,srcStart:0,frameLength:d,dst:p,windowType:n});let{provider:F,release:y}=await E({fftSize:t,sampleRate:e.sampleRate,provider:o,enableProfiling:i,fallbackToNative:o==="webfft",cache:s});try{let g=await F.fft(p),S=new Float32Array(g.magnitude);return Oe(S,l,d,n),{...g,magnitude:S,fftSize:t,windowFunction:n,providerName:F.name,normalization:l}}finally{y()}})();m.set(c,u);let h=()=>{m.delete(c),m.size===0&&ae.delete(e)};return u.then(h,h),u}async function Be(e,r={}){let{fftSize:t=2048,minFrequency:n=0,maxFrequency:a=e.sampleRate/2,scale:o="dbfs",normalization:i="amplitude",...s}=r;Ne(e.sampleRate,n,a),Le(o,i);let l=await V(e,{...s,fftSize:t,normalization:i}),{startIndex:c,endIndex:m}=qe(l.frequencies,n,a),f=l.frequencies.slice(c,m),u=l.magnitude.slice(c,m);return{frequencies:f,values:Ee(u,o),scale:o}}async function _e(e,r={}){let{fftSize:t=2048,frameSize:n=t,hopSize:a=Math.max(1,Math.floor(n/2)),maxFrames:o,minFrequency:i=0,maxFrequency:s=e.sampleRate/2,scale:l="dbfs",normalization:c="amplitude",windowFunction:m="hann",channel:f="mix",provider:u="native",enableProfiling:h=!1,providerCache:b}=r;if(ke(t,"FFT size"),oe(n,"frameSize"),oe(a,"hopSize"),n>t)throw new w("INVALID_INPUT","frameSize must be <= fftSize");o!==void 0&&oe(o,"maxFrames"),Ne(e.sampleRate,i,s),Le(l,c);let p=z(e,f),d=Wt(p.length,n,a),F=o===void 0?d:Math.min(d,o);if(F===0)return{times:new Float32Array(0),frequencies:new Float32Array(0),frames:[],frameCount:0,frequencyBins:0,scale:l,latest:new Float32Array(0)};let y=new Float32Array(F),g=new Array(F),S=new Float32Array(t),x=new Float32Array(0),A=0,I=0,{provider:C,release:R}=await E({fftSize:t,sampleRate:e.sampleRate,provider:u,enableProfiling:h,fallbackToNative:u==="webfft",cache:b});try{for(let M=0;M<F;M++){let v=M*a,T=Math.min(n,Math.max(0,p.length-v));L({src:p,srcStart:v,frameLength:T,dst:S,windowType:m});let D=await C.fft(S),q=new Float32Array(D.magnitude);if(Oe(q,c,T,m),M===0){let _=qe(D.frequencies,i,s);A=_.startIndex,I=_.endIndex,x=D.frequencies.slice(A,I)}let N=q.slice(A,I);g[M]=Ee(N,l),y[M]=(v+n/2)/e.sampleRate}}finally{R()}let P=g[g.length-1]??new Float32Array(x.length);return{times:y,frequencies:x,frames:g,frameCount:g.length,frequencyBins:x.length,scale:l,latest:P}}function je(e,r,t,n){let a=0,o=0;for(let i=0;i<e.length&&i<r.length;i++){let s=r[i],l=e[i];s>=t&&s<=n&&(a+=s*l,o+=l)}return o>1e-10?a/o:0}function Qe(e,r,t,n,a){let o=0,i=0;for(let s=0;s<e.length&&s<r.length;s++){let l=r[s],c=e[s];if(l>=n&&l<=a){let m=l-t;o+=m*m*c,i+=c}}return i>1e-10?Math.sqrt(o/i):0}function Ke(e,r,t,n,a){let o=0;for(let l=0;l<e.length&&l<r.length;l++){let c=r[l],m=e[l];c>=n&&c<=a&&(o+=m*m)}let i=o*t,s=0;for(let l=0;l<e.length&&l<r.length;l++){let c=r[l],m=e[l];if(c>=n&&c<=a&&(s+=m*m,s>=i))return c}return a}function He(e,r,t){let n=0,a=0,o=0;for(let i=r;i<=t&&i<e.length;i++){let s=Math.max(e[i],1e-10);n+=Math.log(s),a+=s,o++}return o===0?0:(n=Math.exp(n/o),a=a/o,a>1e-10?n/a:0)}function Ge(e){if(e.length<2)return 0;let r=0;for(let t=1;t<e.length;t++){let n=e[t-1],a=e[t];(n>=0&&a<0||n<0&&a>=0)&&r++}return r/(e.length-1)}function Ut(e,r,t={normalize:!0}){if(!r||r.length===0)return 0;let n=Math.min(e.length,r.length);if(n===0)return 0;if(t.normalize!==!1){let o=0,i=0;for(let c=0;c<n;c++){let m=e[c],f=r[c];o+=m*m,i+=f*f}let s=Math.sqrt(o),l=Math.sqrt(i);if(s>1e-10&&l>1e-10){let c=1/s,m=1/l,f=0;for(let u=0;u<n;u++){let h=e[u]*c-r[u]*m;f+=h*h}return Math.sqrt(f)}}let a=0;for(let o=0;o<n;o++){let i=e[o]-r[o];a+=i*i}return Math.sqrt(a)}async function Ze(e,r={}){let{fftSize:t=2048,windowFunction:n="hann",channel:a="mix",provider:o="native",enableProfiling:i=!1,providerCache:s,minFrequency:l=0,maxFrequency:c=e.sampleRate/2,rolloffThreshold:m=.85}=r,f=z(e,a),u=await V(e,{fftSize:t,windowFunction:n,channel:a,provider:o,enableProfiling:i,providerCache:s}),h=Math.max(0,Math.floor(l*t/e.sampleRate)),b=Math.min(u.frequencies.length-1,Math.floor(c*t/e.sampleRate)),p=je(u.magnitude,u.frequencies,l,c),d=Qe(u.magnitude,u.frequencies,p,l,c),F=Ke(u.magnitude,u.frequencies,m,l,c),y=He(u.magnitude,h,b),g=Ge(f);return{spectralCentroid:p,spectralBandwidth:d,spectralRolloff:F,spectralFlatness:y,spectralFlux:0,zeroCrossingRate:g,frequencyRange:{min:l,max:c}}}async function Xe(e,r={}){let{frameSize:t=2048,hopSize:n=t/2,fftSize:a=t,windowFunction:o="hann",channel:i="mix",provider:s="native",enableProfiling:l=!1,providerCache:c,minFrequency:m=0,maxFrequency:f=e.sampleRate/2,rolloffThreshold:u=.85,numFrames:h}=r,b=z(e,i),p=Math.floor((b.length-t)/n)+1,d=h??(b.length>0?Math.max(1,p):0);if(d<=0||b.length===0)throw new w("INVALID_INPUT","Invalid frame count");let F=new Float32Array(d),y=new Float32Array(d),g=new Float32Array(d),S=new Float32Array(d),x=new Float32Array(d),A=new Float32Array(d),I=new Float32Array(d),C=new Float32Array(a),R,{provider:P,release:M}=await E({fftSize:a,sampleRate:e.sampleRate,provider:s,enableProfiling:l,fallbackToNative:s==="webfft",cache:c});try{for(let v=0;v<d;v++){let T=v*n,D=Math.min(T+t,b.length);F[v]=T/e.sampleRate;let q=b.subarray(T,D);L({src:b,srcStart:T,frameLength:Math.min(t,Math.max(0,b.length-T)),dst:C,windowType:o});let N=await P.fft(C),_=Math.max(0,Math.floor(m*a/e.sampleRate)),Y=Math.min(N.frequencies.length-1,Math.floor(f*a/e.sampleRate)),$=je(N.magnitude,N.frequencies,m,f);y[v]=$,g[v]=Qe(N.magnitude,N.frequencies,$,m,f),S[v]=Ke(N.magnitude,N.frequencies,u,m,f),x[v]=He(N.magnitude,_,Y),I[v]=Ge(q),A[v]=Ut(N.magnitude,R),R=new Float32Array(N.magnitude)}}finally{M()}return{times:F,spectralCentroid:y,spectralBandwidth:g,spectralRolloff:S,spectralFlatness:x,spectralFlux:A,zeroCrossingRate:I,frameInfo:{frameSize:t,hopSize:n,numFrames:d}}}function $t(e,r,t){let n=[],a=0;for(let c=r;c<=t&&c<e.length;c++){let m=e[c];if(m!==void 0){let f=m*m;n.push(f),a+=f}}if(n.length===0||a<=1e-10)return{entropy:0,entropyNorm:0};let o=n.map(c=>c/a),i=0;for(let c of o)c>1e-10&&(i-=c*Math.log2(c));let s=Math.log2(n.length),l=s>0?i/s:0;return{entropy:i,entropyNorm:l}}function jt(e,r,t){let n=0,a=0,o=0;for(let l=r;l<=t&&l<e.length;l++){let c=e[l];c!==void 0&&(n=Math.max(n,c),a+=c,o++)}if(o===0)return{crest:0,peak:0,average:0};let i=a/o;return{crest:i>1e-10?n/i:0,peak:n,average:i}}async function Ye(e,r={}){let{fftSize:t=2048,windowFunction:n="hann",channel:a="mix",provider:o="native",enableProfiling:i=!1,providerCache:s,minFrequency:l=0,maxFrequency:c=e.sampleRate/2}=r,m=await V(e,{fftSize:t,windowFunction:n,channel:a,provider:o,enableProfiling:i,providerCache:s}),f=Math.max(0,Math.floor(l*t/e.sampleRate)),u=Math.min(m.frequencies.length-1,Math.floor(c*t/e.sampleRate)),{entropy:h,entropyNorm:b}=$t(m.magnitude,f,u);return{entropy:h,entropyNorm:b,frequencyRange:{min:l,max:c}}}async function Je(e,r={}){let{fftSize:t=2048,windowFunction:n="hann",channel:a="mix",provider:o="native",enableProfiling:i=!1,providerCache:s,minFrequency:l=0,maxFrequency:c=e.sampleRate/2,asDB:m=!1}=r,f=await V(e,{fftSize:t,windowFunction:n,channel:a,provider:o,enableProfiling:i,providerCache:s}),u=Math.max(0,Math.floor(l*t/e.sampleRate)),h=Math.min(f.frequencies.length-1,Math.floor(c*t/e.sampleRate)),{crest:b,peak:p,average:d}=jt(f.magnitude,u,h),F={crest:b,peak:p,average:d,frequencyRange:{min:l,max:c}};return m&&(F.crestDB=k(b,1)),F}var Ve=new Map,We=new Map;function Qt(e,r,t,n,a){return`${e}|${r}|${t}|${n}|${a}`}function Kt(e,r,t,n,a){return`${e}|${r}|${t}|${n}|${a??"auto"}`}function Ue(e){return 2595*Math.log10(1+e/700)}function $e(e){return 700*(Math.pow(10,e/2595)-1)}function et(e,r,t,n,a){let o=Qt(e,r,t,n,a),i=Ve.get(o);if(i)return i;let s=[],l=t/2,c=Math.floor(r/2)+1,m=Ue(n),f=Ue(a),u=new Array(e+2);for(let d=0;d<e+2;d++)u[d]=m+(f-m)*d/(e+1);let h=new Float32Array(e),b=u.map(d=>{let F=$e(d);return Math.floor(F/l*(c-1))});for(let d=1;d<=e;d++){let F=new Float32Array(c),y=b[d-1],g=b[d],S=b[d+1];if(y===void 0||g===void 0||S===void 0)continue;let x=u[d];x!==void 0&&(h[d-1]=$e(x));for(let A=y;A<=S;A++)A<0||A>=c||A>=F.length||(A<g?g-y>0&&(F[A]=(A-y)/(g-y)):S-g>0&&(F[A]=(S-A)/(S-g)));s.push(F)}let p={filters:s,centerFrequencies:h};return Ve.set(o,p),p}function ie(e,r,t,n){let a=t.frameSizeMs??25,o=t.hopSizeMs??10,i=Math.round(a/1e3*e.sampleRate),s=Math.round(o/1e3*e.sampleRate),l=t.fftSize??Math.max(1024,Q(i));if(i<=0||s<=0)throw new w("INVALID_INPUT","Frame size or hop size is too small");if(l<=0||(l&l-1)!==0)throw new w("INVALID_INPUT","FFT size must be a power of two");let c=Math.floor((r-i)/s)+1,m=r>0?Math.max(1,c):0;if(m<=0)throw new w("INSUFFICIENT_DATA","Invalid frame count");return{frameSizeMs:a,hopSizeMs:o,frameSizeSamples:i,hopSizeSamples:s,fftSize:l,windowFunction:t.windowFunction??n,channel:t.channel??"mix",numFrames:m}}function tt(e){let r=e??2;if(!Number.isFinite(r)||r<=0)throw new w("INVALID_INPUT","power must be a positive finite number");return r}function nt(e,r){let t=new Float32Array(e.length);if(r===1)return t.set(e),t;if(r===2){for(let n=0;n<e.length;n++){let a=e[n];t[n]=a*a}return t}for(let n=0;n<e.length;n++){let a=e[n];t[n]=Math.pow(a,r)}return t}function rt(e,r,t){let n=new Float32Array(r.length);for(let a=0;a<r.length;a++){let o=r[a];if(!o)continue;let i=0;for(let s=0;s<o.length&&s<e.length;s++)i+=e[s]*o[s];n[a]=Math.max(i,t)}return n}function Ht(e,r,t,n,a){let o=Kt(e,r,t,n,a),i=We.get(o);if(i)return i;let s=r/2,l=r/e,c=Math.floor(e/2);if(!Number.isFinite(t)||t<=0)throw new w("INVALID_INPUT","fMin must be a positive number");if(!Number.isFinite(n)||n<=0)throw new w("INVALID_INPUT","binsPerOctave must be a positive number");if(a!==void 0&&(!Number.isFinite(a)||a<=0))throw new w("INVALID_INPUT","numBins must be a positive number");let m=Math.max(1,Math.floor(n*Math.log2(s/t))),f=Math.min(a??m,m),u=1/(2*n),h=[];for(let b=0;b<f;b++){let p=t*Math.pow(2,b/n);if(p<=0||p>=s)break;let d=Math.max(0,p*Math.pow(2,-u)),F=Math.min(s,p*Math.pow(2,u)),y=Math.max(1,Math.floor(d/l)),g=Math.min(c,Math.ceil(F/l));g<y||h.push({centerFrequency:p,lowerFrequency:d,upperFrequency:F,startIndex:y,endIndex:g})}if(h.length===0)throw new w("INVALID_INPUT","Failed to build CQT frequency bins. Review fMin/numBins settings.");return We.set(o,h),h}function Gt(e,r,t,n,a,o){let i=1/(2*a),s=0,l=0;for(let f=r.startIndex;f<=r.endIndex;f++){let u=f*t/n;if(u<=0)continue;let h=Math.abs(Math.log2(u/r.centerFrequency));if(h>i)continue;let b=1-h/i,p=e[f],d=o===1?p:o===2?p*p:Math.pow(p,o);s+=d*b,l+=b}if(l>1e-10)return s/l;let c=Math.min(Math.floor(r.centerFrequency*n/t),e.length-1),m=c>=0?e[c]:0;return o===1?m:o===2?m*m:Math.pow(m,o)}function Zt(e,r){let t=e.length,n=new Array(r);for(let a=0;a<r;a++){let o=0;for(let s=0;s<t;s++){let l=e[s];l!==void 0&&(o+=l*Math.cos(Math.PI*a*(s+.5)/t))}let i=Math.sqrt(a===0?1/t:2/t);n[a]=o*i}return n}function se(e,r){let t=new Float32Array(e.length);if(e.length===0)return t;t[0]=e[0];for(let n=1;n<e.length;n++)t[n]=e[n]-r*e[n-1];return t}async function at(e,r={}){let{numMelFilters:t=80,minFrequency:n=0,maxFrequency:a=e.sampleRate/2,preEmphasis:o=.97,provider:i="native",enableProfiling:s=!1,providerCache:l,power:c,logScale:m=!0,logEpsilon:f=1e-10}=r,u=tt(c),h=z(e,r.channel??"mix"),b=se(h,o),p=ie(e,b.length,r,"hamming"),d=et(t,p.fftSize,e.sampleRate,n,a),F=[],y=new Float32Array(p.numFrames),g=new Float32Array(p.fftSize),{provider:S,release:x}=await E({fftSize:p.fftSize,sampleRate:e.sampleRate,provider:i,enableProfiling:s,fallbackToNative:i==="webfft",cache:l});try{for(let A=0;A<p.numFrames;A++){let I=A*p.hopSizeSamples;y[A]=I/e.sampleRate,L({src:b,srcStart:I,frameLength:Math.min(p.frameSizeSamples,Math.max(0,b.length-I)),dst:g,windowType:p.windowFunction});let C=await S.fft(g),R=nt(C.magnitude,u),P=rt(R,d.filters,f),M=new Array(P.length);for(let v=0;v<P.length;v++){let T=P[v];M[v]=m?Math.log(Math.max(T,f)):T}F.push(M)}}finally{x()}return{melSpectrogram:F,times:y,melFrequencies:d.centerFrequencies,frameInfo:{frameSizeMs:p.frameSizeMs,hopSizeMs:p.hopSizeMs,numFrames:p.numFrames,numBins:d.filters.length},frequencyRange:{min:n,max:a}}}async function ot(e,r={}){let{fMin:t=32.70319566257483,binsPerOctave:n=12,numBins:a,preEmphasis:o=.97,provider:i="native",enableProfiling:s=!1,providerCache:l,power:c,logScale:m=!0,logEpsilon:f=1e-10}=r,u=tt(c),h=z(e,r.channel??"mix"),b=se(h,o),p=ie(e,b.length,r,"hann"),d=Ht(p.fftSize,e.sampleRate,t,n,a),F=new Float32Array(d.length);for(let I=0;I<d.length;I++)F[I]=d[I].centerFrequency;let y=[],g=new Float32Array(p.numFrames),S=new Float32Array(p.fftSize),{provider:x,release:A}=await E({fftSize:p.fftSize,sampleRate:e.sampleRate,provider:i,enableProfiling:s,fallbackToNative:i==="webfft",cache:l});try{for(let I=0;I<p.numFrames;I++){let C=I*p.hopSizeSamples;g[I]=C/e.sampleRate,L({src:b,srcStart:C,frameLength:Math.min(p.frameSizeSamples,Math.max(0,b.length-C)),dst:S,windowType:p.windowFunction});let R=await x.fft(S),P=new Array(d.length);for(let M=0;M<d.length;M++){let v=d[M];if(!v)continue;let T=Gt(R.magnitude,v,e.sampleRate,p.fftSize,n,u);P[M]=m?Math.log(Math.max(T,f)):T}y.push(P)}}finally{A()}return{cqt:y,times:g,frequencies:F,frameInfo:{frameSizeMs:p.frameSizeMs,hopSizeMs:p.hopSizeMs,numFrames:p.numFrames,numBins:F.length,binsPerOctave:n},frequencyRange:{min:d[0]?.lowerFrequency??t,max:d[d.length-1]?.upperFrequency??e.sampleRate/2}}}function Xt(e,r){return e.map((t,n)=>{if(r===0)return t;let a=1+r/2*Math.sin(Math.PI*n/r);return t*a})}async function le(e,r={}){let{numMelFilters:t=40,numMfccCoeffs:n=13,minFrequency:a=0,maxFrequency:o=e.sampleRate/2,preEmphasis:i=.97,lifterCoeff:s=22,provider:l="native",enableProfiling:c=!1,providerCache:m}=r,f=z(e,r.channel??"mix"),u=se(f,i),h=ie(e,u.length,r,"hamming"),b=et(t,h.fftSize,e.sampleRate,a,o),p=[],d=new Float32Array(h.numFrames),F=new Float32Array(h.fftSize),{provider:y,release:g}=await E({fftSize:h.fftSize,sampleRate:e.sampleRate,provider:l,enableProfiling:c,fallbackToNative:l==="webfft",cache:m});try{for(let S=0;S<h.numFrames;S++){let x=S*h.hopSizeSamples;d[S]=x/e.sampleRate,L({src:u,srcStart:x,frameLength:Math.min(h.frameSizeSamples,Math.max(0,u.length-x)),dst:F,windowType:h.windowFunction});let A=await y.fft(F),I=nt(A.magnitude,2),C=rt(I,b.filters,1e-10),R=new Array(C.length);for(let v=0;v<C.length;v++)R[v]=Math.log(Math.max(C[v],1e-10));let P=Zt(R,n),M=Xt(P,s);p.push(M)}}finally{g()}return{mfcc:p,times:d,frameInfo:{frameSizeMs:h.frameSizeMs,hopSizeMs:h.hopSizeMs,numFrames:h.numFrames,numCoeffs:n},frequencyRange:{min:a,max:o}}}function Yt(e,r=2){let t=e.length,n=e[0]?.length||0,a=[],o=[];for(let i=0;i<t;i++){let s=[];for(let l=0;l<n;l++){let c=0,m=0;for(let f=-r;f<=r;f++){let u=Math.max(0,Math.min(t-1,i+f)),h=e[u]?.[l]||0;c+=f*h,m+=f*f}s[l]=m>0?c/m:0}a.push(s)}for(let i=0;i<t;i++){let s=[];for(let l=0;l<n;l++){let c=0,m=0;for(let f=-r;f<=r;f++){let u=Math.max(0,Math.min(t-1,i+f)),h=a[u]?.[l]||0;c+=f*h,m+=f*f}s[l]=m>0?c/m:0}o.push(s)}return{delta:a,deltaDelta:o}}async function it(e,r={}){let{deltaWindowSize:t=2,computeDelta:n=!0,computeDeltaDelta:a=!0,...o}=r,i=await le(e,o),s={...i};if(n||a){let l=Yt(i.mfcc,t);n&&(s.delta=l.delta),a&&(s.deltaDelta=l.deltaDelta)}return s}function U(e,r){let{samples:t,frameSize:n,hopSize:a,sampleRate:o,padEnd:i=!0}=e;if(!Number.isInteger(n)||n<=0)throw new w("INVALID_INPUT","frameSize must be a positive integer");if(!Number.isInteger(a)||a<=0)throw new w("INVALID_INPUT","hopSize must be a positive integer");if(!Number.isFinite(o)||o<=0)throw new w("INVALID_INPUT","sampleRate must be positive");if(t.length===0)return 0;let s=new Float32Array(n),l=0,c=0;for(;c<t.length||l===0&&i;){let m=Math.min(c+n,t.length),f=Math.max(0,m-c);if(f<n&&!i&&l>0||(s.fill(0),f>0&&s.set(t.subarray(c,m),0),r({frame:s,frameIndex:l,startSample:c,timeSec:(c+n/2)/o}),l+=1,c+=a,!i&&c+n>t.length)||f<n&&i)break}return l}function Jt(e,r){let t=0;for(let n=0;n<e.length;n++){let a=e[n],o=r?a*r[n]:a;t+=o*o}return t}function st(e,r={}){let{frameSize:t=Math.floor(e.sampleRate*.025),hopSize:n=Math.floor(e.sampleRate*.01),channel:a="mix",normalized:o=!1,windowFunction:i="rectangular"}=r;if(!Number.isInteger(t)||t<=0)throw new w("INVALID_INPUT","frameSize must be a positive integer");if(!Number.isInteger(n)||n<=0)throw new w("INVALID_INPUT","hopSize must be a positive integer");let s=z(e,a);if(s.length===0)return{times:new Float32Array(0),energies:new Float32Array(0),totalEnergy:0,statistics:{mean:0,std:0,max:0,min:0}};let l=i==="rectangular"||i==="none"?null:Z(t,i),c=[],m=[],f=0,u=-1/0,h=1/0;U({samples:s,frameSize:t,hopSize:n,sampleRate:e.sampleRate,padEnd:!0},({frame:S,timeSec:x})=>{let A=Jt(S,l);c.push(x),m.push(A),f+=A,u=Math.max(u,A),h=Math.min(h,A)});let b=new Float32Array(m),p=new Float32Array(c),d=b.length,F=d>0?f/d:0,y=0;for(let S=0;S<d;S++){let x=b[S]-F;y+=x*x}let g=d>0?Math.sqrt(y/d):0;if(o&&f>1e-10){for(let S=0;S<b.length;S++)b[S]=b[S]/f;return{times:p,energies:b,totalEnergy:1,statistics:{mean:F/f,std:g/f,max:u/f,min:h/f}}}return{times:p,energies:b,totalEnergy:f,statistics:{mean:F,std:g,max:Number.isFinite(u)?u:0,min:Number.isFinite(h)?h:0}}}var lt=new Map;function en(e){if(e<=0||!isFinite(e))throw new w("INVALID_INPUT","Sample rate must be a positive finite value");if(e<8e3||e>384e3)throw new w("UNSUPPORTED_FORMAT",`Sample rate ${e} Hz is not supported`)}function tn(e){en(e);let r=lt.get(e);if(r)return r;let t=20.598997,n=107.65265,a=737.86223,o=12194.217,i=1.9997,s=[],l=2*e*Math.tan(Math.PI*t/e),c=2*e*Math.tan(Math.PI*n/e),m=2*e*Math.tan(Math.PI*a/e),f=2*e*Math.tan(Math.PI*o/e),u=Math.pow(10,i/20);{let p=l,d=p*p,F=Math.SQRT2,y=4*e*e+2*F*p*e+d,g=2*(d-4*e*e)/y,S=(4*e*e-2*F*p*e+d)/y;s.push({b0:4*e*e*u/y,b1:-8*e*e*u/y,b2:4*e*e*u/y,a0:1,a1:g,a2:S})}{let p=c,d=2*e+p,F=(p-2*e)/d;s.push({b0:2*e/d,b1:-2*e/d,b2:0,a0:1,a1:F,a2:0})}{let p=m,d=2*e+p,F=(p-2*e)/d;s.push({b0:2*e/d,b1:-2*e/d,b2:0,a0:1,a1:F,a2:0})}{let p=f,d=p*p,F=Math.SQRT2,y=4*e*e+2*F*p*e+d,g=2*(d-4*e*e)/y,S=(4*e*e-2*F*p*e+d)/y;s.push({b0:d/y,b1:2*d/y,b2:d/y,a0:1,a1:g,a2:S})}let b=1/nn(s,1e3,e).magnitude;return s.forEach(p=>{p.b0*=b,p.b1*=b,p.b2*=b}),lt.set(e,s),s}function nn(e,r,t){let n=2*Math.PI*r/t,a=1,o=0;for(let l of e){let c=Math.cos(n),m=Math.sin(n),f=Math.cos(2*n),u=Math.sin(2*n),h=l.b0+l.b1*c+l.b2*f,b=-l.b1*m-l.b2*u,p=l.a0+l.a1*c+l.a2*f,d=-l.a1*m-l.a2*u,F=p*p+d*d;if(F===0)throw new w("INVALID_INPUT","Division by zero occurred while calculating frequency response");let y=(h*p+b*d)/F,g=(b*p-h*d)/F,S=a*y-o*g,x=a*g+o*y;a=S,o=x}let i=Math.sqrt(a*a+o*o),s=Math.atan2(o,a);return{magnitude:i,phase:s}}function rn(e,r,t={x1:0,x2:0,y1:0,y2:0}){let{x1:n,x2:a,y1:o,y2:i}=t;for(let s=0;s<e.length;s++){let l=e[s],c=r.b0*l+r.b1*n+r.b2*a-r.a1*o-r.a2*i;e[s]=c,a=n,n=l,i=o,o=c}t.x1=n,t.x2=a,t.y1=o,t.y2=i}function ce(e,r){let t=tn(r),n=e.slice();for(let a of t)rn(n,a);return n}function ct(e,r,t="simple"){if(e.length===0)return{peak:0,rms:0,cfDb:-1/0,cfLinear:0};let a=e;t==="weighted"&&(a=ce(e,r));let o=0,i=0;for(let m=0;m<a.length;m++){let f=a[m],u=Math.abs(f);o=Math.max(o,u),i+=f*f}if(a.length===0)return{peak:0,rms:0,cfDb:-1/0,cfLinear:0};let s=Math.sqrt(i/a.length);if(o<1e-10&&s<1e-10)return{peak:0,rms:0,cfDb:-1/0,cfLinear:0};if(s<1e-10)return{peak:o,rms:s,cfDb:-1/0,cfLinear:0};let l=o/s,c=k(l,1);return{peak:o,rms:s,cfDb:c,cfLinear:l}}function mt(e,r={}){let{channel:n="mix",windowSize:a,hopSize:o,method:i="simple"}=r,s,l;if(i==="weighted"){let u=z(e,n),h=ce(u,e.sampleRate),b=0,p=0;for(let d=0;d<h.length;d++){let F=h[d],y=Math.abs(F);b=Math.max(b,y),p+=F*F}s=b,l=h.length>0?Math.sqrt(p/h.length):0}else{let u={channel:n,asDB:!1};s=W(e,u),l=H(e,u)}let c,m;s<1e-10&&l<1e-10||l<1e-10?(c=0,m=-1/0):(c=s/l,m=k(c,1));let f;if(typeof a=="number"&&typeof o=="number"){if(a<=0||o<=0)throw new w("INVALID_INPUT","windowSize and hopSize must be positive values");o>a&&console.warn("[audio-inspect] hopSize is larger than windowSize, so gaps will occur between analysis windows");let u=Math.floor(a*e.sampleRate),h=Math.floor(o*e.sampleRate);if(u===0||h===0)throw new w("INVALID_INPUT","Window size is too small for the sample rate");let b=z(e,n),p=b.length;if(p<u){let d=ct(b,e.sampleRate,i);f={times:new Float32Array([e.duration/2]),values:new Float32Array([d.cfDb]),valuesLinear:new Float32Array([d.cfLinear]),peaks:new Float32Array([d.peak]),rmsValues:new Float32Array([d.rms])}}else{let d=Math.floor((p-u)/h)+1,F=new Float32Array(d),y=new Float32Array(d),g=new Float32Array(d),S=new Float32Array(d),x=new Float32Array(d);for(let A=0;A<d;A++){let I=A*h,C=Math.min(I+u,p),R=b.subarray(I,C),P=ct(R,e.sampleRate,i);F[A]=(I+u/2)/e.sampleRate,y[A]=P.cfDb,g[A]=P.cfLinear,S[A]=P.peak,x[A]=P.rms}f={times:F,values:y,valuesLinear:g,peaks:S,rmsValues:x}}}return{crestFactor:m,crestFactorLinear:c,peak:s,rms:l,timeVarying:f}}function an(e){let r=2*Math.PI,t=(e+Math.PI)%r;return t<0&&(t+=r),t-Math.PI}async function X(e,r,t,n,a){let o=a??new Float32Array(t);return L({src:e,srcStart:r,frameLength:Math.min(t,Math.max(0,e.length-r)),dst:o,windowType:"hann"}),n.fft(o)}function on(e,r,t=44){let n=Math.min(e.length,r.length),a=-1/0,o=0;for(let i=-t;i<=t;i++){let s=0,l=0;for(let c=0;c<n;c++){let m=c+i;if(m>=0&&m<n){let f=e[c],u=r[m];s+=f*u,l++}}l>0&&(s/=l,s>a&&(a=s,o=i))}return o}function ut(e,r,t){let n=Math.min(e.length,r.length),a=t?new Float32Array(n):void 0,o=t?new Float32Array(n):void 0;if(n===0)return{correlation:0,width:0,balance:0,midSideRatio:0,energyL:0,energyR:0,mid:a,side:o};let i=0,s=0,l=0,c=0,m=0,f=0,u=0,h=0,b=0;for(let R=0;R<n;R++){let P=e[R],M=r[R];i+=P,s+=M,l+=P*M,c+=P*P,m+=M*M,f+=P*P,u+=M*M;let v=(P+M)*.5,T=(P-M)*.5;h+=v*v,b+=T*T,a&&o&&(a[R]=v,o[R]=T)}let p=i/n,d=s/n,F=l/n-p*d,y=Math.sqrt(Math.max(0,c/n-p*p)),g=Math.sqrt(Math.max(0,m/n-d*d)),S=y>1e-10&&g>1e-10?F/(y*g):0,x=Math.max(-1,Math.min(1,S)),A=h+b>1e-10?b/(h+b):0,I=f+u>1e-10?(u-f)/(f+u):0,C=b>1e-10?B(h/b,1):h>1e-10?1/0:0;return{correlation:x,width:A,balance:I,midSideRatio:C,energyL:f,energyR:u,mid:a,side:o}}async function sn(e,r,t,n,a){let o=Math.min(e.length,r.length),i=Math.floor(t/2)+1,s=new Float32Array(i),l=new Float32Array(i),c=new Float32Array(i),m=new Float32Array(i),f=new Float32Array(t),u=new Float32Array(t),h=0,b=0;for(;b<o||h===0;){let g=Math.min(b+t,o),[S,x]=await Promise.all([X(e,b,t,a,f),X(r,b,t,a,u)]);for(let A=0;A<i;A++){let I=S.complex[A*2]??0,C=S.complex[A*2+1]??0,R=x.complex[A*2]??0,P=x.complex[A*2+1]??0;s[A]=s[A]+I*R+C*P,l[A]=l[A]+C*R-I*P,c[A]=c[A]+I*I+C*C,m[A]=m[A]+R*R+P*P}if(h++,g>=o)break;b+=n}let p=new Float32Array(i),d=0;for(let g=0;g<i;g++){let S=c[g]*m[g];S>d&&(d=S)}let F=Math.max(1e-10,d*1e-4),y=Math.sqrt(F);for(let g=0;g<i;g++){let S=c[g]*m[g],x=s[g]**2+l[g]**2;if(S<=F){let I=Math.abs(c[g]??0)<=y,C=Math.abs(m[g]??0)<=y;p[g]=I&&C?1:0;continue}let A=x/S;p[g]=Math.min(1,Math.max(0,A))}return p}function ln(e,r,t,n){let a=new Float32Array(e.length);for(let o=0;o<a.length;o++){let i=e[o],s=r[o],l=t[o],c=n[o],m=l-c,f=Math.abs(i+s)/2,u=Math.abs(i-s)/2,h=Math.abs(Math.sin(m/2)),b=u/(f+u+1e-10);a[o]=Math.max(b,h)}return a}async function ft(e,r={}){if(e.numberOfChannels<2)throw new w("INVALID_INPUT","Stereo analysis requires audio with at least two channels");let{frameSize:t=Math.min(8192,e.length),calculatePhase:n=!1,calculateITD:a=!1,calculateILD:o=!1,provider:i="native",enableProfiling:s=!1,providerCache:l}=r,c=e.channelData[0],m=e.channelData[1];if(!c||!m)throw new w("INVALID_INPUT","L/R channel data is missing");let f=Math.min(c.length,m.length);if(f===0)return{correlation:0,width:0,balance:0,midSideRatio:0};let u=ut(c,m,!0),h={correlation:u.correlation,width:u.width,balance:u.balance,midSideRatio:u.midSideRatio};if(u.side&&u.mid&&(h.goniometer={x:u.side,y:u.mid}),n){let b=Math.max(32,Math.min(t,f)),p=Q(b),d=Math.max(1,Math.floor(b/2)),{provider:F,release:y}=await E({fftSize:p,sampleRate:e.sampleRate,provider:i,enableProfiling:s,fallbackToNative:i==="webfft",cache:l});try{let[g,S]=await Promise.all([X(c.subarray(0,b),0,p,F),X(m.subarray(0,b),0,p,F)]);h.coherence=await sn(c.subarray(0,f),m.subarray(0,f),p,d,F),h.widthFrequency=ln(g.magnitude,S.magnitude,g.phase,S.phase);let x=0,A=0,I=0;for(let C=1;C<g.phase.length;C++){let R=g.magnitude[C],P=S.magnitude[C],M=R*P;if(M<=0)continue;let v=an(g.phase[C]-S.phase[C]);x+=v*M,A+=Math.cos(v)*M,I+=M}h.phaseDifference=I>1e-10?x/I*180/Math.PI:0,h.phaseCorrelation=I>1e-10?A/I:0}finally{y()}}if(a){let b=Math.max(1,Math.round(e.sampleRate*.001)),p=on(c.subarray(0,Math.min(t,f)),m.subarray(0,Math.min(t,f)),b);h.itd=p/e.sampleRate*1e3}if(o){let b=Math.sqrt(u.energyL/f),p=Math.sqrt(u.energyR/f);h.ild=b>1e-10&&p>1e-10?k(p/b,1):0}return h}async function ht(e,r={}){if(e.numberOfChannels<2)throw new w("INVALID_INPUT","Stereo analysis requires audio with at least two channels");let t=e.channelData[0],n=e.channelData[1];if(!t||!n)throw new w("INVALID_INPUT","L/R channel data is missing");let a=Math.min(t.length,n.length);if(a===0)return{times:new Float32Array(0),correlation:new Float32Array(0),width:new Float32Array(0),balance:new Float32Array(0)};let o=Math.max(32,Math.min(r.windowSize??r.frameSize??2048,a)),i=r.hopSize??Math.max(1,Math.floor(o/2));if(i<=0)throw new w("INVALID_INPUT","hopSize must be a positive integer");let s=a<=o?1:Math.floor((a-o)/i)+1,l=new Float32Array(s),c=new Float32Array(s),m=new Float32Array(s),f=new Float32Array(s);for(let u=0;u<s;u++){let h=u*i,b=Math.min(h+o,a),p=ut(t.subarray(h,b),n.subarray(h,b),!1);l[u]=(h+(b-h)/2)/e.sampleRate,c[u]=p.correlation,m[u]=p.width,f[u]=p.balance}return{times:l,correlation:c,width:m,balance:f}}function cn(e,r=.97){let t=new Float32Array(e.length);if(e.length===0)return t;t[0]=e[0];for(let n=1;n<e.length;n++)t[n]=e[n]-r*e[n-1];return t}function mn(e,r,t,n,a=!1){let o=[],i=[];return e.length===0?{energies:new Float32Array(0),times:new Float32Array(0)}:(U({samples:e,frameSize:r,hopSize:t,sampleRate:n,padEnd:!0},({frame:s,timeSec:l})=>{let c=0;for(let m=0;m<s.length;m++){let f=s[m];c+=f*f}c/=s.length,o.push(a?B(c,1,-100):c),i.push(l)}),{energies:new Float32Array(o),times:new Float32Array(i)})}function un(e,r,t,n=!0){let a=[];return e.length===0?new Float32Array(0):(U({samples:e,frameSize:r,hopSize:t,sampleRate:1,padEnd:!0},({frame:o})=>{let i=0,s=Math.sign(o[0]??0);for(let l=1;l<o.length;l++){let c=Math.sign(o[l]??0);s!==c&&s!==0&&c!==0&&(i+=1),s=c}a.push(n?i/Math.max(1,o.length-1):i)}),new Float32Array(a))}function fn(e,r,t,n=10){let a=new Float32Array(e.length),o=0,i=Math.min(n,e.length);for(let s=0;s<i;s++)o+=e[s];o=i>0?o/i:0;for(let s=0;s<e.length;s++){let l=e[s];if(s===0)a[s]=o*t;else{let c=a[s-1];c!==void 0&&l<c&&(o=r*o+(1-r)*l),a[s]=o*t}}return a}function hn(e,r=5){let t=new Float32Array(e.length),n=Math.floor(r/2);for(let a=0;a<e.length;a++){let o=Math.max(0,a-n),i=Math.min(e.length,a+n+1),s=[];for(let l=o;l<i;l++)s.push(e[l]);if(s.sort((l,c)=>l-c),s.length>0){let l=Math.floor(s.length/2);t[a]=s[l]}else t[a]=0}return t}function pn(e,r,t=.5,n=.1,a=.3){let o=[],i=null;for(let s=0;s<e.length;s++){let l=e[s],c=r[s],m=l>=t;if(!i)i={start:c,end:c,type:m?"speech":"silence",confidence:Math.abs(l-.5)*2};else if(m&&i.type==="speech"||!m&&i.type==="silence"){i.end=c;let f=Math.abs(l-.5)*2;i.confidence=Math.max(i.confidence||0,f)}else o.push(i),i={start:c,end:c,type:m?"speech":"silence",confidence:Math.abs(l-.5)*2}}return i&&o.push(i),dn(o,n,a)}function dn(e,r,t){if(e.length===0)return[];let n=[],a=0;for(;a<e.length;){let o=e[a];if(!o){a++;continue}let i=o.end-o.start;if(o.type==="speech"&&i>=r||o.type==="silence"&&i>=t)n.push(o),a++;else{if(n.length>0&&a+1<e.length){let s=n[n.length-1],l=e[a+1];if(s&&l&&s.type===l.type){s.end=l.end,a+=2;continue}}if(n.length>0){let s=n[n.length-1];s&&(s.end=o.end)}a++}}return n}function pt(e,r={}){let{channel:t="mix",frameSizeMs:n=30,hopSizeMs:a=10,method:o="combined",energyThreshold:i=.02,zcrThresholdLow:s=.05,zcrThresholdHigh:l=.15,adaptiveAlpha:c=.99,noiseFactor:m=3,minSilenceDurationMs:f=300,minSpeechDurationMs:u=100,preEmphasis:h=!0,smoothing:b=!0}=r,p=z(e,t);h&&(p=cn(p));let d=e.sampleRate,F=Math.floor(n/1e3*d),y=Math.floor(a/1e3*d);if(F===0||y===0)return{segments:[],speechRatio:0};let{energies:g,times:S}=mn(p,F,y,d,!1),x=un(p,F,y,!0);if(g.length===0)return{segments:[],speechRatio:0};let A=new Float32Array(g