audio-inspect
Version:
Lightweight yet powerful audio analysis library
2 lines (1 loc) • 46.8 kB
JavaScript
"use strict";var Ge=Object.create;var $=Object.defineProperty;var He=Object.getOwnPropertyDescriptor;var Ze=Object.getOwnPropertyNames;var Qe=Object.getPrototypeOf,Ye=Object.prototype.hasOwnProperty;var Je=(e,r)=>{for(var t in r)$(e,t,{get:r[t],enumerable:!0})},ae=(e,r,t,a)=>{if(r&&typeof r=="object"||typeof r=="function")for(let n of Ze(r))!Ye.call(e,n)&&n!==t&&$(e,n,{get:()=>r[n],enumerable:!(a=He(r,n))||a.enumerable});return e};var Xe=(e,r,t)=>(t=e!=null?Ge(Qe(e)):{},ae(r||!e||!e.__esModule?$(t,"default",{value:e,enumerable:!0}):t,e)),et=e=>ae($({},"__esModule",{value:!0}),e);var Kt={};Je(Kt,{getCrestFactor:()=>qe,getEnergy:()=>Oe,getFFT:()=>L,getLUFS:()=>Ue,getLUFSRealtime:()=>ne,getMFCC:()=>ee,getMFCCWithDelta:()=>Ce,getPeak:()=>W,getPeakAmplitude:()=>W,getPeaks:()=>pe,getPeaksAnalysis:()=>we,getRMS:()=>G,getRMSAnalysis:()=>Me,getSpectralCrest:()=>Te,getSpectralEntropy:()=>ve,getSpectralFeatures:()=>X,getSpectrum:()=>Ie,getStereoAnalysis:()=>Ne,getTimeVaryingSpectralFeatures:()=>Re,getTimeVaryingStereoAnalysis:()=>Ee,getVAD:()=>_e,getWaveform:()=>be,getWaveformAnalysis:()=>Ae,getZeroCrossing:()=>de});module.exports=et(Kt);var A=class e extends Error{constructor(t,a,n,s){super(a,{cause:n});this.code=t;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}}};var oe=new Map;function tt(e){if(e<=0||!isFinite(e))throw new A("INVALID_INPUT","\u30B5\u30F3\u30D7\u30EB\u30EC\u30FC\u30C8\u306F\u6B63\u306E\u6709\u9650\u5024\u3067\u3042\u308B\u5FC5\u8981\u304C\u3042\u308A\u307E\u3059");if(e<8e3||e>384e3)throw new A("UNSUPPORTED_FORMAT",`\u30B5\u30F3\u30D7\u30EB\u30EC\u30FC\u30C8 ${e}Hz \u306F\u30B5\u30DD\u30FC\u30C8\u3055\u308C\u3066\u3044\u307E\u305B\u3093`)}function se(e){tt(e);let r=oe.get(e);if(r)return r;let t=20.598997,a=107.65265,n=737.86223,s=12194.217,i=1.9997,l=[],o=2*e*Math.tan(Math.PI*t/e),c=2*e*Math.tan(Math.PI*a/e),u=2*e*Math.tan(Math.PI*n/e),m=2*e*Math.tan(Math.PI*s/e),f=Math.pow(10,i/20);{let h=o,b=h*h,y=Math.SQRT2,d=4*e*e+2*y*h*e+b,F=2*(b-4*e*e)/d,S=(4*e*e-2*y*h*e+b)/d;l.push({b0:4*e*e*f/d,b1:-8*e*e*f/d,b2:4*e*e*f/d,a0:1,a1:F,a2:S})}{let h=c,b=2*e+h,y=(h-2*e)/b;l.push({b0:2*e/b,b1:-2*e/b,b2:0,a0:1,a1:y,a2:0})}{let h=u,b=2*e+h,y=(h-2*e)/b;l.push({b0:2*e/b,b1:-2*e/b,b2:0,a0:1,a1:y,a2:0})}{let h=m,b=h*h,y=Math.SQRT2,d=4*e*e+2*y*h*e+b,F=2*(b-4*e*e)/d,S=(4*e*e-2*y*h*e+b)/d;l.push({b0:b/d,b1:2*b/d,b2:b/d,a0:1,a1:F,a2:S})}let g=1/ie(l,1e3,e).magnitude;return l.forEach(h=>{h.b0*=g,h.b1*=g,h.b2*=g}),oe.set(e,l),l}function ie(e,r,t){let a=2*Math.PI*r/t,n=1,s=0;for(let o of e){let c=Math.cos(a),u=Math.sin(a),m=Math.cos(2*a),f=Math.sin(2*a),p=o.b0+o.b1*c+o.b2*m,g=-o.b1*u-o.b2*f,h=o.a0+o.a1*c+o.a2*m,b=-o.a1*u-o.a2*f,y=h*h+b*b;if(y===0)throw new A("INVALID_INPUT","\u5468\u6CE2\u6570\u5FDC\u7B54\u8A08\u7B97\u3067\u30BC\u30ED\u9664\u7B97\u304C\u767A\u751F\u3057\u307E\u3057\u305F");let d=(p*h+g*b)/y,F=(g*h-p*b)/y,S=n*d-s*F,w=n*F+s*d;n=S,s=w}let i=Math.sqrt(n*n+s*s),l=Math.atan2(s,n);return{magnitude:i,phase:l}}function V(){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 k(e,r=0){if(r===-1){if(e.numberOfChannels===0)throw new A("INVALID_INPUT","No channels available");for(let n=0;n<e.numberOfChannels;n++)if(!e.channelData[n])throw new A("INVALID_INPUT",`Channel ${n} data does not exist for averaging`);let a=new Float32Array(e.length);for(let n=0;n<e.length;n++){let s=0;for(let i=0;i<e.numberOfChannels;i++){let l=e.channelData[i];l&&n<l.length&&(s+=l[n]??0)}a[n]=s/e.numberOfChannels}return a}if(r<-1||r>=e.numberOfChannels)throw new A("INVALID_INPUT",`Invalid channel number: ${r}`);let t=e.channelData[r];if(!t)throw new A("INVALID_INPUT",`Channel ${r} data does not exist`);return t}function nt(e){return e>0&&(e&e-1)===0}function ce(e){if(e<=0||!isFinite(e)||isNaN(e))return 1;if(nt(e))return e;let r=1;for(;r<e&&r<Number.MAX_SAFE_INTEGER;)r*=2;return r}function ue(e,r,t){return r<0||r>=e.length?t:e[r]??t}function K(e){return e==null?!1:!isNaN(e)&&isFinite(e)}function M(e){return e==null||isNaN(e)||!isFinite(e)?0:e}function U(e,r=1){return e<=0||r<=0?-1/0:20*Math.log10(e/r)}function rt(e,r,t={x1:0,x2:0,y1:0,y2:0}){let a=new Float32Array(e.length),{x1:n,x2:s,y1:i,y2:l}=t;for(let o=0;o<e.length;o++){let c=M(e[o]??0),u=r.b0*c+r.b1*n+r.b2*s-r.a1*i-r.a2*l;a[o]=u,s=n,n=c,l=i,i=u}return t.x1=n,t.x2=s,t.y1=i,t.y2=l,a}function Z(e,r){let t=se(r),a=e;for(let n of t)a=rt(a,n);return a}function me(e,r){let t=(e.length-1)*r+1,a=new Float32Array(t);for(let n=0;n<e.length-1;n++){let s=M(e[n]??0),l=(M(e[n+1]??0)-s)/r;for(let o=0;o<r;o++)a[n*r+o]=s+l*o}return e.length>0&&(a[t-1]=M(e[e.length-1]??0)),a}function le(e,r){if(e.length<4)return me(e,r);let t=(e.length-1)*r+1,a=new Float32Array(t);for(let n=0;n<e.length-1;n++){let s=M(e[Math.max(0,n-1)]??0),i=M(e[n]??0),l=M(e[Math.min(e.length-1,n+1)]??0),o=M(e[Math.min(e.length-1,n+2)]??0);for(let c=0;c<r;c++){let u=c/r,m=u*u,f=m*u,p=.5*(2*i+(-s+l)*u+(2*s-5*i+4*l-o)*m+(-s+3*i-3*l+o)*f);a[n*r+c]=M(p)}}return e.length>0&&(a[t-1]=M(e[e.length-1]??0)),a}function at(e,r){let t=(e.length-1)*r+1,a=new Float32Array(t),n=s=>{if(Math.abs(s)<1e-10)return 1;let i=Math.PI*s;return Math.abs(s)<3?3*Math.sin(i/3)*Math.sin(i)/(i*i):0};for(let s=0;s<t;s++){let i=s/r,l=0;for(let o=-3;o<=3;o++){let c=Math.floor(i)+o;if(c>=0&&c<e.length){let u=M(e[c]??0),m=n(i-c);l+=u*m}}a[s]=M(l)}return a}function ot(e,r={}){let{factor:t=4,interpolation:a="cubic"}=r;if(t<=1)return e;if(e.length===0)return new Float32Array(0);switch(a){case"linear":return me(e,t);case"cubic":return le(e,t);case"sinc":return at(e,t);default:return le(e,t)}}function fe(e,r={}){if(e.length===0)return 0;let t=ot(e,r),a=0;for(let n=0;n<t.length;n++){let s=Math.abs(M(t[n]??0));a=Math.max(a,s)}return a}function he(e,r,t=!1){let a=[],n=e.length;if(n<3)return a;for(let s=1;s<n-1;s++){let i=Math.abs(M(e[s]??0)),l=Math.abs(M(e[s-1]??0)),o=Math.abs(M(e[s+1]??0));if(i>l&&i>o&&i>r){let c={position:s,amplitude:i};t&&(c.prominence=st(e,s,i)),a.push(c)}}return a}function st(e,r,t){let a=t;for(let s=r-1;s>=0;s--){let i=Math.abs(M(e[s]??0));if(i>t)break;a=Math.min(a,i)}let n=t;for(let s=r+1;s<e.length;s++){let i=Math.abs(M(e[s]??0));if(i>t)break;n=Math.min(n,i)}return t-Math.max(a,n)}function pe(e,r={}){let{count:t=100,threshold:a=.1,channel:n=0,minDistance:s=Math.floor(e.sampleRate/100)}=r;if(t<=0)throw new A("INVALID_INPUT","\u30D4\u30FC\u30AF\u6570\u306F\u6B63\u306E\u6574\u6570\u3067\u3042\u308B\u5FC5\u8981\u304C\u3042\u308A\u307E\u3059");if(a<0||a>1)throw new A("INVALID_INPUT","\u95BE\u5024\u306F0\u304B\u30891\u306E\u7BC4\u56F2\u3067\u3042\u308B\u5FC5\u8981\u304C\u3042\u308A\u307E\u3059");let i=k(e,n);if(i.length===0)return{peaks:[],maxAmplitude:0,averageAmplitude:0};let l=he(i,a);if(l.length===0)return{peaks:[],maxAmplitude:0,averageAmplitude:0};l.sort((f,p)=>p.amplitude-f.amplitude);let o=[],c=[];for(let f of l){if(o.length>=t)break;let p=f.position-s,g=f.position+s;c.some(([b,y])=>!(g<b||p>y))||(o.push({position:f.position,time:f.position/e.sampleRate,amplitude:f.amplitude}),c.push([p,g]))}o.sort((f,p)=>f.position-p.position);let u=l.length>0?l[0]?.amplitude??0:0,m=l.length>0?l.reduce((f,p)=>f+p.amplitude,0)/l.length:0;return{peaks:o,maxAmplitude:u,averageAmplitude:m}}var Q=-1/0;function G(e,r={}){let t=typeof r=="number"?{channel:r,asDB:!1,reference:1,truePeak:!1,oversamplingFactor:4,interpolation:"cubic"}:{channel:r.channel??0,asDB:r.asDB??!1,reference:r.reference??1,truePeak:r.truePeak??!1,oversamplingFactor:r.oversamplingFactor??4,interpolation:r.interpolation??"cubic"},a=k(e,t.channel);if(a.length===0)return t.asDB?Q:0;let n=0,s=0;for(let l=0;l<a.length;l++){let o=a[l]??0;K(o)&&(n+=o*o,s++)}if(s===0)return t.asDB?Q:0;let i=Math.sqrt(n/s);return t.asDB?U(i,t.reference):i}function W(e,r={}){let t={channel:r.channel??0,asDB:r.asDB??!1,reference:r.reference??1,truePeak:r.truePeak??!1,oversamplingFactor:r.oversamplingFactor??4,interpolation:r.interpolation??"cubic"},a=k(e,t.channel);if(a.length===0)return t.asDB?Q:0;let n;if(t.truePeak)n=fe(a,{factor:t.oversamplingFactor,interpolation:t.interpolation});else{n=0;for(let s=0;s<a.length;s++){let i=a[s]??0;K(i)&&(n=Math.max(n,Math.abs(i)))}}return t.asDB?U(n,t.reference):n}function de(e,r=0){let t=k(e,r);if(t.length<2)return 0;let a=0;for(let n=1;n<t.length;n++){let s=M(t[n-1]??0),i=M(t[n]??0);(s>=0&&i<0||s<0&&i>=0)&&a++}return a/(t.length-1)}function be(e,r={}){let{framesPerSecond:t=60,channel:a=0,method:n="rms"}=r,s=k(e,a),i=Math.ceil(e.duration*t),l=e.length>0?e.length:i>0?1:0,o=Math.min(i,l),c=o>0?Math.max(1,Math.floor(e.length/o)):0,u=[],m=0,f=0;for(let g=0;g<o;g++){let h=g*c,b=Math.min(h+c,s.length);if(b<=h){let S=u.length>0?ue(u,u.length-1,{time:0,amplitude:0}).amplitude:0;u.push({time:(h+c/2)/e.sampleRate,amplitude:S});continue}let y=s.subarray(h,b),d;switch(n){case"peak":d=ye(y);break;case"average":d=Fe(y);break;case"rms":default:d=ge(y);break}let F=(h+(b-h)/2)/e.sampleRate;u.push({time:F,amplitude:d}),m=Math.max(m,d),f+=d}let p=o>0?f/o:0;return{waveform:u,maxAmplitude:m,averageAmplitude:p,frameCount:o,samplesPerFrame:c}}function ge(e){if(e.length===0)return 0;let r=0;for(let t=0;t<e.length;t++){let a=M(e[t]??0);r+=a*a}return Math.sqrt(r/e.length)}function ye(e){let r=0;for(let t=0;t<e.length;t++){let a=Math.abs(M(e[t]??0));r=Math.max(r,a)}return r}function Fe(e){if(e.length===0)return 0;let r=0;for(let t=0;t<e.length;t++)r+=Math.abs(M(e[t]??0));return r/e.length}function Ae(e,r={}){let t=V(),{framesPerSecond:a=60,channel:n=0,method:s="rms",onProgress:i}=r;i?.(0,"Waveform\u89E3\u6790\u3092\u958B\u59CB");let l=k(e,n),o=Math.ceil(e.duration*a),c=e.length>0?e.length:o>0?1:0,u=Math.min(o,c),m=u>0?Math.max(1,Math.floor(e.length/u)):0;i?.(25,"\u30D5\u30EC\u30FC\u30E0\u8A2D\u5B9A\u5B8C\u4E86");let f=new Float32Array(u),p=new Float32Array(u),g=0,h=0;i?.(50,"\u632F\u5E45\u8A08\u7B97\u3092\u958B\u59CB");for(let d=0;d<u;d++){let F=d*m,S=Math.min(F+m,l.length);if(S<=F){let P=d>0?f[d-1]??0:0;f[d]=P,p[d]=(F+m/2)/e.sampleRate;continue}let w=l.subarray(F,S),x;switch(s){case"peak":x=ye(w);break;case"average":x=Fe(w);break;case"rms":default:x=ge(w);break}if(f[d]=x,p[d]=(F+(S-F)/2)/e.sampleRate,g=Math.max(g,x),h+=x,d%Math.max(1,Math.floor(u/20))===0){let P=50+d/u*45;i?.(Math.round(P),`\u30D5\u30EC\u30FC\u30E0 ${d+1}/${u} \u51E6\u7406\u4E2D`)}}let b=u>0?h/u:0,y=V()-t;return i?.(100,"\u51E6\u7406\u5B8C\u4E86"),{amplitudes:f,timestamps:p,frameCount:u,samplesPerFrame:m,framesPerSecond:a,maxAmplitude:g,averageAmplitude:b,sampleRate:e.sampleRate,duration:e.duration,processingTime:y}}function we(e,r={}){let t=V(),{count:a=100,threshold:n=.1,channel:s=0,minDistance:i=Math.floor(e.sampleRate/100),onProgress:l}=r;if(l?.(0,"Peaks\u89E3\u6790\u3092\u958B\u59CB"),a<=0)throw new A("INVALID_INPUT","\u30D4\u30FC\u30AF\u6570\u306F\u6B63\u306E\u6574\u6570\u3067\u3042\u308B\u5FC5\u8981\u304C\u3042\u308A\u307E\u3059");if(n<0||n>1)throw new A("INVALID_INPUT","\u95BE\u5024\u306F0\u304B\u30891\u306E\u7BC4\u56F2\u3067\u3042\u308B\u5FC5\u8981\u304C\u3042\u308A\u307E\u3059");let o=k(e,s);if(o.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:V()-t};l?.(25,"\u30D4\u30FC\u30AF\u5019\u88DC\u3092\u691C\u51FA\u4E2D");let c=he(o,n);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:V()-t};l?.(50,"\u30D4\u30FC\u30AF\u3092\u30BD\u30FC\u30C8\u4E2D"),c.sort((d,F)=>F.amplitude-d.amplitude),l?.(75,"\u30D4\u30FC\u30AF\u3092\u9078\u629E\u4E2D");let u=[],m=[];for(let d of c){if(u.length>=a)break;let F=d.position-i,S=d.position+i;m.some(([x,P])=>!(S<x||F>P))||(u.push({position:d.position,time:d.position/e.sampleRate,amplitude:d.amplitude}),m.push([F,S]))}u.sort((d,F)=>d.position-F.position);let f=new Float32Array(u.length),p=new Float32Array(u.length),g=new Float32Array(u.length);for(let d=0;d<u.length;d++){let F=u[d];F&&(f[d]=F.position,p[d]=F.amplitude,g[d]=F.time)}let h=c.length>0?c[0]?.amplitude??0:0,b=c.length>0?c.reduce((d,F)=>d+F.amplitude,0)/c.length:0,y=V()-t;return l?.(100,"\u51E6\u7406\u5B8C\u4E86"),{positions:f,amplitudes:p,times:g,maxAmplitude:h,averageAmplitude:b,count:u.length,sampleRate:e.sampleRate,duration:e.duration,processingTime:y}}function Me(e,r={}){let t=V(),{channel:a=0,asDB:n=!1,reference:s=1,onProgress:i}=r;i?.(0,"RMS\u89E3\u6790\u3092\u958B\u59CB");let l=k(e,a);if(l.length===0)return{value:n?-1/0:0,channel:a,sampleRate:e.sampleRate,duration:e.duration,processingTime:V()-t};i?.(50,"RMS\u5024\u3092\u8A08\u7B97\u4E2D");let o=0;for(let m=0;m<l.length;m++){let f=l[m]??0;if(K(f)){let p=M(f);o+=p*p}}let c=Math.sqrt(o/l.length),u=V()-t;return i?.(100,"\u51E6\u7406\u5B8C\u4E86"),n?{value:U(c,s),channel:a,sampleRate:e.sampleRate,duration:e.duration,processingTime:u}:{value:c,valueDB:U(c,s),channel:a,sampleRate:e.sampleRate,duration:e.duration,processingTime:u}}var Y=class{constructor(r,t,a=!1){this.size=r;this.sampleRate=t;this.enableProfiling=a;this.initializationPromise=this.initializeWebFFT()}fftInstance=null;initializationPromise=null;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 A("UNSUPPORTED_FORMAT",`WebFFT\u306E\u521D\u671F\u5316\u306B\u5931\u6557\u3057\u307E\u3057\u305F: ${r instanceof Error?r.message:String(r)}`)}}async fft(r){if(this.initializationPromise&&await this.initializationPromise,!this.fftInstance)throw new A("UNSUPPORTED_FORMAT","WebFFT initialization failed");if(r.length!==this.size)throw new A("INVALID_INPUT",`\u5165\u529B\u30B5\u30A4\u30BA\u304C\u4E0D\u6B63\u3067\u3059\u3002\u671F\u5F85\u5024: ${this.size}, \u5B9F\u969B: ${r.length}`);let t=new Float32Array(this.size*2);for(let l=0;l<this.size;l++)t[l*2]=r[l]||0,t[l*2+1]=0;let a=this.fftInstance.fft(t),n=new Float32Array(this.size/2+1),s=new Float32Array(this.size/2+1),i=new Float32Array(this.size/2+1);for(let l=0;l<n.length;l++){let o=a[l*2]||0,c=a[l*2+1]||0;n[l]=Math.sqrt(o*o+c*c),s[l]=Math.atan2(c,o),i[l]=l*this.sampleRate/this.size}return{complex:a,magnitude:n,phase:s,frequencies:i}}async profile(){if(this.initializationPromise&&await this.initializationPromise,!this.fftInstance||!this.fftInstance.profile)throw new A("UNSUPPORTED_FORMAT","WebFFT\u304C\u521D\u671F\u5316\u3055\u308C\u3066\u3044\u307E\u305B\u3093");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}}},J=class{constructor(r,t){this.size=r;this.sampleRate=t;if(!this.isPowerOfTwo(r))throw new A("INVALID_INPUT","FFT\u30B5\u30A4\u30BA\u306F2\u306E\u51AA\u3067\u3042\u308B\u5FC5\u8981\u304C\u3042\u308A\u307E\u3059");this.precomputeTables()}bitReversalTable;twiddleFactorsReal;twiddleFactorsImag;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 a=0;a<this.size;a++){let n=0;for(let s=0;s<r;s++)n=n<<1|a>>s&1;this.bitReversalTable[a]=n}let t=this.size/2;this.twiddleFactorsReal=new Float32Array(t),this.twiddleFactorsImag=new Float32Array(t);for(let a=0;a<t;a++){let n=-2*Math.PI*a/this.size;this.twiddleFactorsReal[a]=Math.cos(n),this.twiddleFactorsImag[a]=Math.sin(n)}}fft(r){if(r.length!==this.size)throw new A("INVALID_INPUT",`\u5165\u529B\u30B5\u30A4\u30BA\u304C\u4E0D\u6B63\u3067\u3059\u3002\u671F\u5F85\u5024: ${this.size}, \u5B9F\u969B: ${r.length}`);let t=new Float32Array(this.size),a=new Float32Array(this.size);for(let o=0;o<this.size;o++){let c=this.bitReversalTable[o];c!==void 0&&(t[c]=r[o]||0,a[c]=0)}for(let o=1;o<this.size;o*=2){let c=o*2,u=this.size/c;for(let m=0;m<this.size;m+=c)for(let f=0;f<o;f++){let p=f*u,g=this.twiddleFactorsReal[p]||0,h=this.twiddleFactorsImag[p]||0,b=m+f,y=m+f+o,d=t[b]||0,F=a[b]||0,S=t[y]||0,w=a[y]||0,x=S*g-w*h,P=S*h+w*g;t[b]=d+x,a[b]=F+P,t[y]=d-x,a[y]=F-P}}let n=new Float32Array(this.size*2),s=new Float32Array(this.size/2+1),i=new Float32Array(this.size/2+1),l=new Float32Array(this.size/2+1);for(let o=0;o<this.size;o++)if(n[o*2]=t[o]||0,n[o*2+1]=a[o]||0,o<=this.size/2){let c=t[o]||0,u=a[o]||0;s[o]=Math.sqrt(c*c+u*u),i[o]=Math.atan2(u,c),l[o]=o*this.sampleRate/this.size}return{complex:n,magnitude:s,phase:i,frequencies:l}}dispose(){}},j=class{static async createProvider(r){switch(r.type){case"webfft":{let t=new Y(r.fftSize,r.sampleRate,r.enableProfiling);return await t.waitForInitialization(),t}case"native":return new J(r.fftSize,r.sampleRate);case"custom":if(!r.customProvider)throw new A("INVALID_INPUT","\u30AB\u30B9\u30BF\u30E0\u30D7\u30ED\u30D0\u30A4\u30C0\u30FC\u304C\u6307\u5B9A\u3055\u308C\u3066\u3044\u307E\u305B\u3093");return r.customProvider;default:{let t=r.type;throw new A("UNSUPPORTED_FORMAT",`\u672A\u5BFE\u5FDC\u306EFFT\u30D7\u30ED\u30D0\u30A4\u30C0\u30FC: ${String(t)}`)}}}static getAvailableProviders(){return["webfft","native"]}};function Se(e,r){let t=new Float32Array(e.length),a=e.length;for(let n=0;n<a;n++){let s=1;switch(r){case"hann":s=.5*(1-Math.cos(2*Math.PI*n/(a-1)));break;case"hamming":s=.54-.46*Math.cos(2*Math.PI*n/(a-1));break;case"blackman":s=.42-.5*Math.cos(2*Math.PI*n/(a-1))+.08*Math.cos(4*Math.PI*n/(a-1));break;case"rectangular":default:s=1;break}t[n]=(e[n]||0)*s}return t}function xe(e,r){if(r===-1){let a=new Float32Array(e.length);for(let n=0;n<e.length;n++){let s=0;for(let i=0;i<e.numberOfChannels;i++){let l=e.channelData[i];l&&n<l.length&&(s+=l[n])}a[n]=s/e.numberOfChannels}return a}if(r<-1||r>=e.numberOfChannels)throw new A("INVALID_INPUT",`Invalid channel number: ${r}`);let t=e.channelData[r];if(!t)throw new A("INVALID_INPUT",`Channel ${r} data does not exist`);return t}async function L(e,r={}){let{fftSize:t=2048,windowFunction:a="hann",channel:n=0,provider:s="native",enableProfiling:i=!1}=r,l=xe(e,n),o;l.length<t?(o=new Float32Array(t),o.set(l)):o=l.slice(0,t);let c=Se(o,a),u=s==="webfft"?["webfft","native"]:[s],m,f=null;for(let p of u)try{f=await j.createProvider({type:p,fftSize:t,sampleRate:e.sampleRate,enableProfiling:i});break}catch(g){if(m=g,p==="webfft"&&u.includes("native"))continue}if(!f)throw m||new A("FFT_PROVIDER_ERROR","Failed to create FFT provider");try{return{...await f.fft(c),fftSize:t,windowFunction:a,providerName:f.name}}finally{f.dispose()}}async function Ie(e,r={}){let{fftSize:t=2048,minFrequency:a=0,maxFrequency:n=e.sampleRate/2,decibels:s=!0,timeFrames:i=1,overlap:l=.5,...o}=r,c=xe(e,r.channel||0);if(i===1){let u=await L(e,{...o,fftSize:t}),m=it(u,a,n),f={frequencies:m.frequencies,magnitudes:m.magnitude};return s&&(f.decibels=De(m.magnitude)),f}else{let u=await lt(c,e.sampleRate,t,i,l,{...o,minFrequency:a,maxFrequency:n,decibels:s});return{frequencies:u.frequencies,magnitudes:new Float32Array,spectrogram:u}}}function it(e,r,t){let{frequencies:a,magnitude:n,phase:s,complex:i}=e,l=a.findIndex(u=>u>=r),o=a.findIndex(u=>u>t),c=o===-1?a.length:o;return{frequencies:a.slice(l,c),magnitude:n.slice(l,c),phase:s.slice(l,c),complex:i.slice(l*2,c*2)}}function De(e){let r=new Float32Array(e.length);for(let t=0;t<e.length;t++){let a=e[t]||0;r[t]=a>0?20*Math.log10(a):-1/0}return r}async function lt(e,r,t,a,n,s){let i=Math.floor(t*(1-n)),l;e.length===0?l=0:e.length<t?l=1:l=Math.floor((e.length-t)/i)+1;let o=Math.min(a,l),c=new Float32Array(o),u=[],m=new Float32Array,f=new Float32Array,p=0,g=0,h=await j.createProvider({type:s.provider||"native",fftSize:t,sampleRate:r,enableProfiling:s.enableProfiling||!1});try{for(let b=0;b<o;b++){let y=b*i,d=new Float32Array(t);for(let v=0;v<t;v++)d[v]=y+v<e.length&&e[y+v]||0;let F=Se(d,s.windowFunction||"hann"),S=await h.fft(F);if(b===0){m=S.frequencies;let v=s.minFrequency||0,z=s.maxFrequency||r/2;p=m.findIndex(O=>O>=v),p===-1&&(p=0);let E=m.findIndex(O=>O>z);g=E===-1?m.length:E,f=m.slice(p,g)}let x=S.magnitude.slice(p,g),P=s.decibels?De(x):x;u.push(P),c[b]=(y+t/2)/r}}finally{h.dispose()}return{times:c,frequencies:f,intensities:u,timeFrames:o,frequencyBins:f.length}}function ct(e,r,t,a){let n=0,s=0;for(let i=0;i<e.length&&i<r.length;i++){let l=r[i],o=e[i];l!==void 0&&o!==void 0&&l>=t&&l<=a&&(n+=l*o,s+=o)}return s>1e-10?n/s:0}function ut(e,r,t,a,n){let s=0,i=0;for(let l=0;l<e.length&&l<r.length;l++){let o=r[l],c=e[l];if(o!==void 0&&c!==void 0&&o>=a&&o<=n){let u=o-t;s+=u*u*c,i+=c}}return i>1e-10?Math.sqrt(s/i):0}function mt(e,r,t,a,n){let s=0;for(let o=0;o<e.length&&o<r.length;o++){let c=r[o],u=e[o];c!==void 0&&u!==void 0&&c>=a&&c<=n&&(s+=u*u)}let i=s*t,l=0;for(let o=0;o<e.length&&o<r.length;o++){let c=r[o],u=e[o];if(c!==void 0&&u!==void 0&&c>=a&&c<=n&&(l+=u*u,l>=i))return c}return n}function ft(e,r,t){let a=0,n=0,s=0;for(let i=r;i<=t&&i<e.length;i++){let l=e[i];if(l!==void 0){let o=Math.max(l,1e-10);a+=Math.log(o),n+=o,s++}}return s===0?0:(a=Math.exp(a/s),n=n/s,n>1e-10?a/n:0)}function ht(e){if(e.length<2)return 0;let r=0;for(let t=1;t<e.length;t++){let a=M(e[t-1]??0),n=M(e[t]??0);(a>=0&&n<0||a<0&&n>=0)&&r++}return r/(e.length-1)}function pt(e,r,t={normalize:!0}){if(!r||r.length===0)return 0;let a=Math.min(e.length,r.length);if(a===0)return 0;let n=e,s=r;if(t.normalize!==!1){let l=0,o=0;for(let m=0;m<a;m++){let f=e[m]??0,p=r[m]??0;l+=f*f,o+=p*p}let c=Math.sqrt(l),u=Math.sqrt(o);if(c>1e-10&&u>1e-10){n=new Float32Array(a),s=new Float32Array(a);for(let m=0;m<a;m++)n[m]=(e[m]??0)/c,s[m]=(r[m]??0)/u}}let i=0;for(let l=0;l<a;l++){let o=(n[l]??0)-(s[l]??0);i+=o*o}return Math.sqrt(i)}async function X(e,r={}){let{fftSize:t=2048,windowFunction:a="hann",channel:n=0,minFrequency:s=0,maxFrequency:i=e.sampleRate/2,rolloffThreshold:l=.85}=r;if(n>=e.numberOfChannels)throw new A("INVALID_INPUT",`\u7121\u52B9\u306A\u30C1\u30E3\u30F3\u30CD\u30EB\u756A\u53F7: ${n}`);let o=await L(e,{fftSize:t,windowFunction:a,channel:n}),c=Math.max(0,Math.floor(s*t/e.sampleRate)),u=Math.min(o.frequencies.length-1,Math.floor(i*t/e.sampleRate)),m=ct(o.magnitude,o.frequencies,s,i),f=ut(o.magnitude,o.frequencies,m,s,i),p=mt(o.magnitude,o.frequencies,l,s,i),g=ft(o.magnitude,c,u),h=e.channelData[n];if(!h)throw new A("INVALID_INPUT",`\u30C1\u30E3\u30F3\u30CD\u30EB ${n} \u306E\u30C7\u30FC\u30BF\u304C\u5B58\u5728\u3057\u307E\u305B\u3093`);let b=ht(h);return{spectralCentroid:m,spectralBandwidth:f,spectralRolloff:p,spectralFlatness:g,zeroCrossingRate:b,frequencyRange:{min:s,max:i}}}async function Re(e,r={}){let{frameSize:t=2048,hopSize:a=t/2,fftSize:n=t,windowFunction:s="hann",channel:i=0,minFrequency:l=0,maxFrequency:o=e.sampleRate/2,rolloffThreshold:c=.85,numFrames:u}=r;if(i>=e.numberOfChannels)throw new A("INVALID_INPUT",`\u7121\u52B9\u306A\u30C1\u30E3\u30F3\u30CD\u30EB\u756A\u53F7: ${i}`);let m=e.channelData[i];if(!m)throw new A("INVALID_INPUT",`\u30C1\u30E3\u30F3\u30CD\u30EB ${i} \u306E\u30C7\u30FC\u30BF\u304C\u5B58\u5728\u3057\u307E\u305B\u3093`);let f=u||Math.floor((m.length-t)/a)+1;if(f<=0)throw new A("INVALID_INPUT","\u30D5\u30EC\u30FC\u30E0\u6570\u304C\u4E0D\u6B63\u3067\u3059");let p=new Float32Array(f),g=new Float32Array(f),h=new Float32Array(f),b=new Float32Array(f),y=new Float32Array(f),d=new Float32Array(f),F=new Float32Array(f),S;for(let w=0;w<f;w++){let x=w*a,P=Math.min(x+t,m.length);p[w]=x/e.sampleRate;let v=m.subarray(x,P),z=new Float32Array(n),E=Math.min(v.length,n);E>0&&z.set(v.subarray(0,E));let O={channelData:[z],sampleRate:e.sampleRate,numberOfChannels:1,length:n,duration:n/e.sampleRate},N=await X(O,{fftSize:n,windowFunction:s,channel:0,minFrequency:l,maxFrequency:o,rolloffThreshold:c});g[w]=N.spectralCentroid,h[w]=N.spectralBandwidth,b[w]=N.spectralRolloff,y[w]=N.spectralFlatness,F[w]=N.zeroCrossingRate;let I=await L(O,{fftSize:n,windowFunction:s,channel:0});d[w]=pt(I.magnitude,S),S=new Float32Array(I.magnitude)}return{times:p,spectralCentroid:g,spectralBandwidth:h,spectralRolloff:b,spectralFlatness:y,spectralFlux:d,zeroCrossingRate:F,frameInfo:{frameSize:t,hopSize:a,numFrames:f}}}function dt(e,r,t){let a=[],n=0;for(let c=r;c<=t&&c<e.length;c++){let u=e[c];if(u!==void 0){let m=u*u;a.push(m),n+=m}}if(a.length===0||n<=1e-10)return{entropy:0,entropyNorm:0};let s=a.map(c=>c/n),i=0;for(let c of s)c>1e-10&&(i-=c*Math.log2(c));let l=Math.log2(a.length),o=l>0?i/l:0;return{entropy:i,entropyNorm:o}}function bt(e,r,t){let a=0,n=0,s=0;for(let o=r;o<=t&&o<e.length;o++){let c=e[o];c!==void 0&&(a=Math.max(a,c),n+=c,s++)}if(s===0)return{crest:0,peak:0,average:0};let i=n/s;return{crest:i>1e-10?a/i:0,peak:a,average:i}}async function ve(e,r={}){let{fftSize:t=2048,windowFunction:a="hann",channel:n=0,minFrequency:s=0,maxFrequency:i=e.sampleRate/2}=r;if(n>=e.numberOfChannels)throw new A("INVALID_INPUT",`\u7121\u52B9\u306A\u30C1\u30E3\u30F3\u30CD\u30EB\u756A\u53F7: ${n}`);let l=await L(e,{fftSize:t,windowFunction:a,channel:n}),o=Math.max(0,Math.floor(s*t/e.sampleRate)),c=Math.min(l.frequencies.length-1,Math.floor(i*t/e.sampleRate)),{entropy:u,entropyNorm:m}=dt(l.magnitude,o,c);return{entropy:u,entropyNorm:m,frequencyRange:{min:s,max:i}}}async function Te(e,r={}){let{fftSize:t=2048,windowFunction:a="hann",channel:n=0,minFrequency:s=0,maxFrequency:i=e.sampleRate/2,asDB:l=!1}=r;if(n>=e.numberOfChannels)throw new A("INVALID_INPUT",`\u7121\u52B9\u306A\u30C1\u30E3\u30F3\u30CD\u30EB\u756A\u53F7: ${n}`);let o=await L(e,{fftSize:t,windowFunction:a,channel:n}),c=Math.max(0,Math.floor(s*t/e.sampleRate)),u=Math.min(o.frequencies.length-1,Math.floor(i*t/e.sampleRate)),{crest:m,peak:f,average:p}=bt(o.magnitude,c,u),g={crest:m,peak:f,average:p,frequencyRange:{min:s,max:i}};return l&&(g.crestDB=m>0?20*Math.log10(m):-1/0),g}function gt(e,r){let t=new Float32Array(r);for(let a=0;a<r;a++)switch(e){case"hann":t[a]=.5-.5*Math.cos(2*Math.PI*a/(r-1));break;case"hamming":t[a]=.54-.46*Math.cos(2*Math.PI*a/(r-1));break;case"blackman":t[a]=.42-.5*Math.cos(2*Math.PI*a/(r-1))+.08*Math.cos(4*Math.PI*a/(r-1));break;case"rectangular":default:t[a]=1;break}return t}function yt(e,r){let t=gt(r,e.length),a=new Float32Array(e.length);for(let n=0;n<e.length;n++)a[n]=M(e[n]??0)*(t[n]??0);return a}function Pe(e){return 2595*Math.log10(1+e/700)}function Ft(e){return 700*(Math.pow(10,e/2595)-1)}function At(e,r,t,a,n){let s=[],i=t/2,l=Math.floor(r/2)+1,o=Pe(a),c=Pe(n),u=new Array(e+2);for(let f=0;f<e+2;f++)u[f]=o+(c-o)*f/(e+1);let m=u.map(f=>{let p=Ft(f);return Math.floor(p/i*(l-1))});for(let f=1;f<=e;f++){let p=new Float32Array(l),g=m[f-1],h=m[f],b=m[f+1];if(!(g===void 0||h===void 0||b===void 0)){for(let y=g;y<=b;y++)y<0||y>=l||y>=p.length||(y<h?h-g>0&&(p[y]=(y-g)/(h-g)):b-h>0&&(p[y]=(b-y)/(b-h)));s.push(p)}}return s}function wt(e,r){let t=e.length,a=new Array(r);for(let n=0;n<r;n++){let s=0;for(let l=0;l<t;l++){let o=e[l];o!==void 0&&(s+=o*Math.cos(Math.PI*n*(l+.5)/t))}let i=Math.sqrt(n===0?1/t:2/t);a[n]=s*i}return a}function Mt(e,r){let t=new Float32Array(e.length);t[0]=M(e[0]??0);for(let a=1;a<e.length;a++){let n=M(e[a]??0),s=M(e[a-1]??0);t[a]=n-r*s}return t}function St(e,r){return e.map((t,a)=>{if(r===0)return t;let n=1+r/2*Math.sin(Math.PI*a/r);return t*n})}async function ee(e,r={}){let{frameSizeMs:t=25,hopSizeMs:a=10,windowFunction:n="hamming",channel:s=0,numMelFilters:i=40,numMfccCoeffs:l=13,minFrequency:o=0,maxFrequency:c=e.sampleRate/2,preEmphasis:u=.97,lifterCoeff:m=22}=r;if(s>=e.numberOfChannels)throw new A("INVALID_INPUT",`\u7121\u52B9\u306A\u30C1\u30E3\u30F3\u30CD\u30EB\u756A\u53F7: ${s}`);let f=Math.round(t/1e3*e.sampleRate),p=Math.round(a/1e3*e.sampleRate);if(f<=0||p<=0)throw new A("INVALID_INPUT","\u30D5\u30EC\u30FC\u30E0\u30B5\u30A4\u30BA\u307E\u305F\u306F\u30DB\u30C3\u30D7\u30B5\u30A4\u30BA\u304C\u5C0F\u3055\u3059\u304E\u307E\u3059");let g=r.fftSize??Math.max(1024,ce(f)),h=e.channelData[s];if(!h)throw new A("INVALID_INPUT",`\u30C1\u30E3\u30F3\u30CD\u30EB ${s} \u306E\u30C7\u30FC\u30BF\u304C\u5B58\u5728\u3057\u307E\u305B\u3093`);let b=Mt(h,u),y=Math.floor((b.length-f)/p)+1;if(y<=0)throw new A("INSUFFICIENT_DATA","\u30D5\u30EC\u30FC\u30E0\u6570\u304C\u4E0D\u6B63\u3067\u3059");let d=At(i,g,e.sampleRate,o,c),F=[],S=new Float32Array(y);for(let w=0;w<y;w++){let x=w*p;S[w]=x/e.sampleRate;let P=b.subarray(x,x+f),v;P.length>g?v=P.subarray(0,g):(v=new Float32Array(g),v.set(P));let E={channelData:[yt(v,n)],sampleRate:e.sampleRate,numberOfChannels:1,length:g,duration:g/e.sampleRate},O=await L(E,{fftSize:g,windowFunction:"none",channel:0}),N=new Array(O.magnitude.length);for(let R=0;R<O.magnitude.length;R++){let q=O.magnitude[R]??0;N[R]=q*q}let I=new Array(i);for(let R=0;R<i;R++){let q=0,B=d[R];if(B)for(let _=0;_<B.length&&_<N.length;_++)q+=(N[_]??0)*(B[_]??0);I[R]=Math.max(q,1e-10)}let D=I.map(R=>Math.log(R)),C=wt(D,l),T=St(C,m);F.push(T)}return{mfcc:F,times:S,frameInfo:{frameSizeMs:t,hopSizeMs:a,numFrames:y,numCoeffs:l},frequencyRange:{min:o,max:c}}}function xt(e,r=2){let t=e.length,a=e[0]?.length||0,n=[],s=[];for(let i=0;i<t;i++){let l=[];for(let o=0;o<a;o++){let c=0,u=0;for(let m=-r;m<=r;m++){let f=Math.max(0,Math.min(t-1,i+m)),p=e[f]?.[o]||0;c+=m*p,u+=m*m}l[o]=u>0?c/u:0}n.push(l)}for(let i=0;i<t;i++){let l=[];for(let o=0;o<a;o++){let c=0,u=0;for(let m=-r;m<=r;m++){let f=Math.max(0,Math.min(t-1,i+m)),p=n[f]?.[o]||0;c+=m*p,u+=m*m}l[o]=u>0?c/u:0}s.push(l)}return{delta:n,deltaDelta:s}}async function Ce(e,r={}){let{deltaWindowSize:t=2,computeDelta:a=!0,computeDeltaDelta:n=!0,...s}=r,i=await ee(e,s),l={...i};if(a||n){let o=xt(i.mfcc,t);a&&(l.delta=o.delta),n&&(l.deltaDelta=o.deltaDelta)}return l}function ke(e,r,t,a){let n=new Float32Array(a);for(let s=0;s<a&&t+s<e.length;s++){let i=1;switch(r){case"hann":i=.5*(1-Math.cos(2*Math.PI*s/(a-1)));break;case"hamming":i=.54-.46*Math.cos(2*Math.PI*s/(a-1));break;case"rectangular":default:i=1}let l=M(e[t+s]??0);n[s]=l*i}return n}function Oe(e,r={}){let{frameSize:t=Math.floor(e.sampleRate*.025),hopSize:a=Math.floor(e.sampleRate*.01),channel:n=0,normalized:s=!1,windowFunction:i="rectangular"}=r;if(t<=0||!Number.isInteger(t))throw new A("INVALID_INPUT","frameSize\u306F\u6B63\u306E\u6574\u6570\u3067\u3042\u308B\u5FC5\u8981\u304C\u3042\u308A\u307E\u3059");if(a<=0||!Number.isInteger(a))throw new A("INVALID_INPUT","hopSize\u306F\u6B63\u306E\u6574\u6570\u3067\u3042\u308B\u5FC5\u8981\u304C\u3042\u308A\u307E\u3059");a>t&&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 l=k(e,n),o=l.length;if(o===0)return{times:new Float32Array(0),energies:new Float32Array(0),totalEnergy:0,statistics:{mean:0,std:0,max:0,min:0}};let c=Math.max(0,Math.floor((o-t)/a)+1);if(c===0){let d=It(l,0,o,i);return{times:new Float32Array([o/2/e.sampleRate]),energies:new Float32Array([d]),totalEnergy:d,statistics:{mean:d,std:0,max:d,min:d}}}let u=new Float32Array(c),m=new Float32Array(c),f=0,p=-1/0,g=1/0;for(let d=0;d<c;d++){let F=d*a,S=ke(l,i,F,t),w=0;for(let x=0;x<S.length;x++){let P=S[x];P!==void 0&&(w+=P*P)}u[d]=(F+t/2)/e.sampleRate,m[d]=w,f+=w,p=Math.max(p,w),g=Math.min(g,w)}let h=f/c,b=0;for(let d=0;d<c;d++){let F=m[d];if(F!==void 0){let S=F-h;b+=S*S}}let y=Math.sqrt(b/c);if(s&&f>1e-10){for(let d=0;d<m.length;d++){let F=m[d];F!==void 0&&(m[d]=F/f)}return{times:u,energies:m,totalEnergy:1,statistics:{mean:h/f,std:y/f,max:p/f,min:g/f}}}return{times:u,energies:m,totalEnergy:f,statistics:{mean:h,std:y,max:p,min:g}}}function It(e,r,t,a){let n=ke(e,a,r,t),s=0;for(let i of n)s+=i*i;return s}function ze(e,r,t="simple"){if(e.length===0)return{peak:0,rms:0,cfDb:-1/0,cfLinear:0};let a=e;t==="weighted"&&(a=Z(e,r));let n=0,s=0,i=0;for(let u=0;u<a.length;u++){let m=M(a[u]??0),f=Math.abs(m);n=Math.max(n,f),s+=m*m,i++}if(i===0)return{peak:0,rms:0,cfDb:-1/0,cfLinear:0};let l=Math.sqrt(s/i);if(l<1e-10)return{peak:n,rms:l,cfDb:1/0,cfLinear:1/0};let o=n/l,c=20*Math.log10(o);return{peak:n,rms:l,cfDb:c,cfLinear:o}}function qe(e,r={}){let{channel:t=0,windowSize:a,hopSize:n,method:s="simple"}=r,i,l;if(s==="weighted"){let m=k(e,t),f=Z(m,e.sampleRate),p=0,g=0,h=0;for(let b=0;b<f.length;b++){let y=M(f[b]??0),d=Math.abs(y);p=Math.max(p,d),g+=y*y,h++}i=p,l=h>0?Math.sqrt(g/h):0}else{let m={channel:t,asDB:!1};i=W(e,m),l=G(e,m)}let o=l>1e-10?i/l:1/0,c=l>1e-10?20*Math.log10(o):1/0,u;if(typeof a=="number"&&typeof n=="number"){if(a<=0||n<=0)throw new A("INVALID_INPUT","windowSize\u3068hopSize\u306F\u6B63\u306E\u5024\u3067\u3042\u308B\u5FC5\u8981\u304C\u3042\u308A\u307E\u3059");n>a&&console.warn("[audio-inspect] hopSize\u304CwindowSize\u3088\u308A\u5927\u304D\u3044\u305F\u3081\u3001\u5206\u6790\u7A93\u9593\u306B\u30AE\u30E3\u30C3\u30D7\u304C\u751F\u3058\u307E\u3059");let m=Math.floor(a*e.sampleRate),f=Math.floor(n*e.sampleRate);if(m===0||f===0)throw new A("INVALID_INPUT","\u30B5\u30F3\u30D7\u30EB\u30EC\u30FC\u30C8\u306B\u5BFE\u3057\u3066\u7A93\u30B5\u30A4\u30BA\u304C\u5C0F\u3055\u3059\u304E\u307E\u3059");let p=k(e,t),g=p.length;if(g<m){let h=ze(p,e.sampleRate,s);u={times:new Float32Array([e.duration/2]),values:new Float32Array([h.cfDb]),valuesLinear:new Float32Array([h.cfLinear]),peaks:new Float32Array([h.peak]),rmsValues:new Float32Array([h.rms])}}else{let h=Math.floor((g-m)/f)+1,b=new Float32Array(h),y=new Float32Array(h),d=new Float32Array(h),F=new Float32Array(h),S=new Float32Array(h);for(let w=0;w<h;w++){let x=w*f,P=Math.min(x+m,g),v=p.subarray(x,P),z=ze(v,e.sampleRate,s);b[w]=(x+m/2)/e.sampleRate,y[w]=z.cfDb,d[w]=z.cfLinear,F[w]=z.peak,S[w]=z.rms}u={times:b,values:y,valuesLinear:d,peaks:F,rmsValues:S}}}return{crestFactor:c,crestFactorLinear:o,peak:i,rms:l,timeVarying:u}}function Dt(e,r,t=44){let a=Math.min(e.length,r.length),n=-1/0,s=0;for(let i=-t;i<=t;i++){let l=0,o=0;for(let c=0;c<a;c++){let u=c,m=c+i;if(m>=0&&m<a){let f=M(e[u]??0),p=M(r[m]??0);l+=f*p,o++}}o>0&&(l/=o,l>n&&(n=l,s=i))}return s}async function Pt(e,r){let t=new Float32Array(e.magnitude.length);for(let a=0;a<t.length;a++){let n=e.magnitude[a]??0,s=r.magnitude[a]??0,i=(e.phase[a]??0)-(r.phase[a]??0),l=n*s*Math.cos(i),o=n*n*s*s;t[a]=o>1e-10?l*l/o:0}return t}function Rt(e,r,t,a){let n=new Float32Array(e.length);for(let s=0;s<n.length;s++){let i=e[s]||0,l=r[s]||0,o=t[s]||0,c=a[s]||0,u=o-c,m=Math.abs(i+l)/2,f=Math.abs(i-l)/2,p=Math.abs(Math.sin(u/2)),g=f/(m+f+1e-10);n[s]=Math.max(g,p)}return n}async function Ne(e,r={}){if(e.numberOfChannels<2)throw new A("INVALID_INPUT","\u30B9\u30C6\u30EC\u30AA\u89E3\u6790\u306B\u306F2\u30C1\u30E3\u30F3\u30CD\u30EB\u4EE5\u4E0A\u306E\u97F3\u58F0\u304C\u5FC5\u8981\u3067\u3059");let{frameSize:t=Math.min(8192,e.length),calculatePhase:a=!1,calculateITD:n=!1,calculateILD:s=!1}=r,i=e.channelData[0],l=e.channelData[1];if(!i||!l)throw new A("INVALID_INPUT","L/R\u30C1\u30E3\u30F3\u30CD\u30EB\u306E\u30C7\u30FC\u30BF\u304C\u5B58\u5728\u3057\u307E\u305B\u3093");let o=Math.min(i.length,l.length);if(o===0)return{correlation:0,width:0,balance:0,midSideRatio:0};let c=0,u=0,m=0,f=0,p=0,g=0,h=0,b=0,y=0,d=new Float32Array(o),F=new Float32Array(o);for(let D=0;D<o;D++){let C=M(i[D]??0),T=M(l[D]??0);c+=C,u+=T,m+=C*T,f+=C*C,p+=T*T,g+=C*C,h+=T*T;let R=(C+T)*.5,q=(C-T)*.5;b+=R*R,y+=q*q,d[D]=R,F[D]=q}let S=c/o,w=u/o,x=m/o-S*w,P=Math.sqrt(f/o-S*S),v=Math.sqrt(p/o-w*w),z=P>1e-10&&v>1e-10?x/(P*v):0,E=b+y>1e-10?y/(b+y):0,O=g+h>1e-10?(h-g)/(g+h):0,N=y>1e-10?10*Math.log10(b/y):1/0,I={correlation:z,width:E,balance:O,midSideRatio:N};if(I.goniometer={x:F,y:d},a){let D=Math.min(t,o),C=Math.pow(2,Math.ceil(Math.log2(D))),[T,R]=await Promise.all([L({channelData:[i.subarray(0,D)],sampleRate:e.sampleRate,numberOfChannels:1,length:D,duration:D/e.sampleRate},{fftSize:C}),L({channelData:[l.subarray(0,D)],sampleRate:e.sampleRate,numberOfChannels:1,length:D,duration:D/e.sampleRate},{fftSize:C})]);I.coherence=await Pt(T,R),I.widthFrequency=Rt(T.magnitude,R.magnitude,T.phase,R.phase);let q=0,B=0;for(let _=1;_<T.phase.length;_++){let We=T.magnitude[_]||0,je=R.magnitude[_]||0,$e=T.phase[_]||0,Ke=R.phase[_]||0,re=We*je,H=$e-Ke;H=(H+Math.PI)%(2*Math.PI)-Math.PI,q+=H*re,B+=re}I.phaseDifference=B>1e-10?q/B*180/Math.PI:0}if(n){let D=Dt(i.subarray(0,Math.min(t,o)),l.subarray(0,Math.min(t,o)));I.itd=D/e.sampleRate*1e3}if(s){let D=Math.sqrt(g/o),C=Math.sqrt(h/o);I.ild=D>1e-10&&C>1e-10?20*Math.log10(C/D):0}return I}function Ee(e,r={}){return Promise.reject(new A("UNSUPPORTED_FORMAT","\u6642\u7CFB\u5217\u30B9\u30C6\u30EC\u30AA\u89E3\u6790\u306F\u5C06\u6765\u306E\u30D0\u30FC\u30B8\u30E7\u30F3\u3067\u5B9F\u88C5\u4E88\u5B9A\u3067\u3059"))}function vt(e,r=.97){let t=new Float32Array(e.length);t[0]=e[0]||0;for(let a=1;a<e.length;a++){let n=M(e[a]??0),s=M(e[a-1]??0);t[a]=n-r*s}return t}function Tt(e,r,t,a,n=!1){let s=e.length;if(s<r)return{energies:new Float32Array(0),times:new Float32Array(0)};let i=Math.floor((s-r)/t)+1,l=new Float32Array(i),o=new Float32Array(i);for(let c=0;c<i;c++){let u=c*t,m=Math.min(u+r,s),f=0,p=0;for(let g=u;g<m;g++){let h=M(e[g]??0);f+=h*h,p++}f=p>0?f/p:0,n?l[c]=f>1e-10?10*Math.log10(f):-100:l[c]=f,o[c]=(u+r/2)/a}return{energies:l,times:o}}function Ct(e,r,t,a=!0){let n=e.length;if(n<r)return new Float32Array(0);let s=Math.floor((n-r)/t)+1,i=new Float32Array(s);for(let l=0;l<s;l++){let o=l*t,c=Math.min(o+r,n),u=0,m=Math.sign(M(e[o]??0));for(let f=o+1;f<c;f++){let p=M(e[f]??0),g=Math.sign(p);m!==g&&m!==0&&g!==0&&u++,m=g}i[l]=a?u/Math.max(1,c-o-1):u}return i}function kt(e,r,t,a=10){let n=new Float32Array(e.length),s=0,i=Math.min(a,e.length);for(let l=0;l<i;l++){let o=e[l]??0;o!==void 0&&(s+=o)}s=i>0?s/i:0;for(let l=0;l<e.length;l++){let o=e[l]??0;if(o===void 0){n[l]=l>0?n[l-1]??s*t:s*t;continue}if(l===0)n[l]=s*t;else{let c=n[l-1];c!==void 0&&o<c&&(s=r*s+(1-r)*o),n[l]=s*t}}return n}function Ot(e,r=5){let t=new Float32Array(e.length),a=Math.floor(r/2);for(let n=0;n<e.length;n++){let s=Math.max(0,n-a),i=Math.min(e.length,n+a+1),l=[];for(let o=s;o<i;o++){let c=e[o]??0;c!==void 0&&l.push(c)}if(l.sort((o,c)=>o-c),l.length>0){let o=Math.floor(l.length/2),c=l[o];t[n]=c??0}else t[n]=0}return t}function zt(e,r,t=.5,a=.1,n=.3){let s=[],i=null;for(let l=0;l<e.length;l++){let o=e[l]??0,c=r[l]??0;if(o===void 0||c===void 0)continue;let u=o>=t;if(!i)i={start:c,end:c,type:u?"speech":"silence",confidence:Math.abs(o-.5)*2};else if(u&&i.type==="speech"||!u&&i.type==="silence"){i.end=c;let m=Math.abs(o-.5)*2;i.confidence=Math.max(i.confidence||0,m)}else s.push(i),i={start:c,end:c,type:u?"speech":"silence",confidence:Math.abs(o-.5)*2}}return i&&s.push(i),qt(s,a,n)}function qt(e,r,t){if(e.length===0)return[];let a=[],n=0;for(;n<e.length;){let s=e[n];if(!s){n++;continue}let i=s.end-s.start;if(s.type==="speech"&&i>=r||s.type==="silence"&&i>=t)a.push(s),n++;else{if(a.length>0&&n+1<e.length){let l=a[a.length-1],o=e[n+1];if(l&&o&&l.type===o.type){l.end=o.end,n+=2;continue}}if(a.length>0){let l=a[a.length-1];l&&(l.end=s.end)}n++}}return a}function _e(e,r={}){let{channel:t=0,frameSizeMs:a=30,hopSizeMs:n=10,method:s="combined",energyThreshold:i=.02,zcrThresholdLow:l=.05,zcrThresholdHigh:o=.15,adaptiveAlpha:c=.99,noiseFactor:u=3,minSilenceDurationMs:m=300,minSpeechDurationMs:f=100,preEmphasis:p=!0,smoothing:g=!0}=r,h=k(e,t);p&&(h=vt(h));let b=e.sampleRate,y=Math.floor(a/1e3*b),d=Math.floor(n/1e3*b);if(y===0||d===0)return{segments:[],speechRatio:0};let{energies:F,times:S}=Tt(h,y,d,b,!1),w=Ct(h,y,d,!0);if(F.length===0)return{segments:[],speechRatio:0};let x=new Float32Array(F.length);switch(s){case"energy":{for(let I=0;I<F.length;I++){let D=F[I]??0;x[I]=D>i?1:0}break}case"zcr":{for(let I=0;I<w.length;I++){let D=w[I]??0;x[I]=D>l&&D<o?1:0}break}case"combined":{for(let I=0;I<F.length;I++){let D=F[I]??0,C=w[I]??0,T=D>i?1:0,R=C>l&&C<o?1:0;x[I]=(T+R)/2}break}case"adaptive":{let I=kt(F,c,u);for(let D=0;D<F.length;D++){let C=F[D]??0,T=w[D]??0,R=I[D]??0,q=C>R?1:0,B=T>l&&T<o?.5:0;x[D]=Math.min(1,q+B)}break}}let P=g?Ot(x,5):x,v=f/1e3,z=m/1e3,E=zt(P,S,.5,v,z),O=0;for(let I of E)I.type==="speech"&&(O+=I.end-I.start);let N=e.duration>0?Math.min(1,O/e.duration):0;return{segments:E,speechRatio:N,features:{energies:F,zcrs:w,decisions:P,times:S}}}var Le=new Map;function Nt(e){if(!isFinite(e)||e<=0)throw new A("INVALID_INPUT","\u30B5\u30F3\u30D7\u30EB\u30EC\u30FC\u30C8\u306F\u6B63\u306E\u6709\u9650\u5024\u3067\u3042\u308B\u5FC5\u8981\u304C\u3042\u308A\u307E\u3059");if(e<8e3||e>384e3)throw new A("UNSUPPORTED_FORMAT",`\u30B5\u30F3\u30D7\u30EB\u30EC\u30FC\u30C8 ${e}Hz \u306F\u30B5\u30DD\u30FC\u30C8\u3055\u308C\u3066\u3044\u307E\u305B\u3093`)}function Et(e){Nt(e);let r=Le.get(e);if(r)return r;let t=[];if(e===48e3)t.push({b0:1.53512485958697,b1:-2.69169618940638,b2:1.19839281085285,a0:1,a1:-1.69065929318241,a2:.73248077421585}),t.push({b0:1,b1:-2,b2:1,a0:1,a1:-1.99004745483398,a2:.99007225036621});else{let a=1681.974450955532,n=3.99984385397,s=.7071752369554193,i=Math.tan(Math.PI*a/e),l=Math.pow(10,n/20),o=Math.pow(l,.499666774155),c=1+i/s+i*i;t.push({b0:(l+o*i/s+i*i)/c,b1:2*(i*i-l)/c,b2:(l-o*i/s+i*i)/c,a0:1,a1:2*(i*i-1)/c,a2:(1-i/s+i*i)/c});let u=38.13547087613982,m=.5003270373253953,f=Math.tan(Math.PI*u/e),p=1+f/m+f*f;t.push({b0:1,b1:-2,b2:1,a0:1,a1:2*(f*f-1)/p,a2:(1-f/m+f*f)/p});let h=1/_t(t,997,e).magnitude;t[0]&&(t[0].b0*=h,t[0].b1*=h,t[0].b2*=h)}return Le.set(e,t),t}function _t(e,r,t){let a=2*Math.PI*r/t,n=1,s=0;for(let o of e){let c=Math.cos(a),u=Math.sin(a),m=Math.cos(2*a),f=Math.sin(2*a),p=o.b0+o.b1*c+o.b2*m,g=-o.b1*u-o.b2*f,h=o.a0+o.a1*c+o.a2*m,b=-o.a1*u-o.a2*f,y=h*h+b*b;if(y===0)throw new A("INVALID_INPUT","\u5468\u6CE2\u6570\u5FDC\u7B54\u8A08\u7B97\u3067\u30BC\u30ED\u9664\u7B97\u304C\u767A\u751F\u3057\u307E\u3057\u305F");let d=(p*h+g*b)/y,F=(g*h-p*b)/y,S=n*d-s*F,w=n*F+s*d;n=S,s=w}let i=Math.sqrt(n*n+s*s),l=Math.atan2(s,n);return{magnitude:i,phase:l}}function Ve(e){return Et(e)}var Lt=-70,Vt=10,Bt=400,Ut=.75,Wt=3e3,jt=400;function Be(e,r,t={x1:0,x2:0,y1:0,y2:0}){let a=new Float32Array(e.length),{x1:n,x2:s,y1:i,y2:l}=t;for(let o=0;o<e.length;o++){let c=M(e[o]??0),u=r.b0*c+r.b1*n+r.b2*s-r.a1*i-r.a2*l;a[o]=u,s=n,n=c,l=i,i=u}return t.x1=n,t.x2=s,t.y1=i,t.y2=l,a}function $t(e){let r=0,t=e.length;if(t===0)return-1/0;for(let a=0;a<t;a++){let n=e[a];if(!n||n.length===0)continue;let s=0,i=0;for(let o=0;o<n.length;o++){let c=M(n[o]??0);s+=c*c,i++}if(i===0)continue;r+=1*(s/i)}return-.691+10*Math.log10(Math.max(1e-15,r))}function Ue(e,r={}){let{channelMode:t=e.numberOfChannels>=2?"stereo":"mono",calculateShortTerm:a=!1,calculateMomentary:n=!1,calculateLoudnessRange:s=!1,calculateTruePeak:i=!1}=r;if(e.numberOfChannels===0)throw new A("INVALID_INPUT","\u51E6\u7406\u53EF\u80FD\u306A\u30C1\u30E3\u30F3\u30CD\u30EB\u304C\u3042\u308A\u307E\u305B\u3093");let l=ne(e.sampleRate,{channelMode:t,maxDurationMs:e.duration*1e3+5e3}),o=[];if(t==="mono"){let h=e.channelData[0];h&&o.push(h)}else{let h=e.channelData[0],b=e.channelData[1];h&&o.push(h),b&&o.push(b)}if(o.length===0)throw new A("INVALID_INPUT","\u51E6\u7406\u53EF\u80FD\u306A\u30C1\u30E3\u30F3\u30CD\u30EB\u304C\u3042\u308A\u307E\u305B\u3093");let c=[],u=o[0].length;if(e.duration<5)c.push(o);else{let h=Math.floor(e.sampleRate*.5);for(let b=0;b<u;b+=h){let y=Math.min(b+h,u),d=o.map(F=>F.subarray(b,y));c.push(d)}}let m=[],f=[],p;for(let h of c)p=l.process(h),n&&isFinite(p.momentary)&&m.push(p.momentary),a&&isFinite(p.shortTerm)&&f.push(p.shortTerm);let g={integrated:p?.integrated??-1/0};if(a&&f.length>0&&(g.shortTerm=new Float32Array(f)),n&&m.length>0&&(g.momentary=new Float32Array(m)),s&&g.shortTerm){let h=Array.from(g.shortTerm).filter(b=>b>-70&&isFinite(b)).sort((b,y)=>b-y);if(h.length>0){let b=Math.floor(h.length*.1),y=Math.floor(h.length*.95),d=h[b]??-1/0,F=h[y]??-1/0;g.loudnessRange=F-d,g.statistics={percentile10:d,percentile95:F}}}return i&&(g.truePeak=o.map(h=>{let b=0;for(let y of h){let d=M(y??0);b=Math.max(b,Math.abs(d))}return b>0?20*Math.log10(b):-1/0})),g}var te=class{sampleRate;channelMode;blockSize;hopSize;blockBuffer=[];maxBlocks;currentSamples=[];sampleCount=0;biquadStates=[];totalSamplesProcessed=0;constructor(r,t="stereo",a=3e4){this.sampleRate=r,this.channelMode=t,this.blockSize=Math.floor(Bt/1e3*r),this.hopSize=Math.floor(this.blockSize*(1-Ut)),this.maxBlocks=Math.ceil(a/(this.hopSize/r*1e3));let n=t==="stereo"?2:1;for(let s=0;s<n;s++)this.currentSamples.push(new Float32Array(this.blockSize)),this.biquadStates.push([{x1:0,x2:0,y1:0,y2:0},{x1:0,x2:0,y1:0,y2:0}])}process(r){let t=this.channelMode==="stereo"?Math.min(r.length,2):1,a=Ve(this.sampleRate);for(let n=0;n<t;n++){let s=r[n];if(!s)continue;let i=Be(s,a[0],this.biquadStates[n][0]);i=Be(i,a[1],this.biquadStates[n][1]);let l=this.currentSamples[n],o=0;for(;o<i.length;){let c=this.blockSize-this.sampleCount,u=Math.min(i.length-o,c);if(l.set(i.subarray(o,o+u),this.sampleCount),this.sampleCount+=u,o+=u,this.sampleCount>=this.blockSize){let m=$t(this.currentSamples.slice(0,t));if(isFinite(m)){let f=this.totalSamplesProcessed+this.blockSize/2;this.blockBuffer.push([m,f]),this.blockBuffer.length>this.maxBlocks&&this.blockBuffer.shift()}l.copyWithin(0,this.hopSize),this.sampleCount=this.blockSize-this.hopSize,this.totalSamplesProcessed+=this.hopSize}}}return this.calculateCurrentLUFS()}calculateCurrentLUFS(){if(this.blockBuffer.length===0)return{integrated:-1/0,momentary:-1/0,shortTerm:-1/0};let r=this.totalSamplesProcessed+this.sampleCount,t=jt/1e3*this.sampleRate,a=Wt/1e3*this.sampleRate,n=this.blockBuffer.filter(([u,m])=>r-m<=t).map(([u])=>u),s=this.blockBuffer.filter(([u,m])=>r-m<=a).map(([u])=>u),i=this.blockBuffer.map(([u])=>u);i=i.filter(u=>u>=Lt);let l=-1/0;if(i.length>0){let u=i.reduce((g,h)=>g+Math.pow(10,(h+.691)/10),0),f=-.691+10*Math.log10(u/i.length)-Vt,p=i.filter(g=>g>=f);if(p.length>0){let g=p.reduce((h,b)=>h+Math.pow(10,(b+.691)/10),0);l=-.691+10*Math.log10(g/p.length)}}let o=-1/0;if(n.length>0){let u=n.reduce((m,f)=>m+Math.pow(10,(f+.691)/10),0);o=-.691+10*Math.log10(u/n.length)}let c=-1/0;if(s.length>0){let u=s.reduce((m,f)=>m+Math.pow(10,(f+.691)/10),0);c=-.691+10*Math.log10(u/s.length)}return{integrated:l,momentary:o,shortTerm:c}}reset(){this.blockBuffer=[],this.sampleCount=0,this.totalSamplesProcessed=0;for(let r=0;r<this.currentSamples.length;r++){this.currentSamples[r].fill(0);for(let t=0;t<this.biquadStates[r].length;t++)this.biquadStates[r][t]={x1:0,x2:0,y1:0,y2:0}}}getBufferSize(){return this.blockBuffer.length}};function ne(e,r={}){let{channelMode:t="stereo",maxDurationMs:a=3e4}=r;return new te(e,t,a)}