audio-inspect
Version:
Lightweight yet powerful audio analysis library
2 lines • 70.5 kB
JavaScript
// audio-inspect - Lightweight yet powerful audio analysis library
"use strict";var _t=Object.create;var Y=Object.defineProperty;var qt=Object.getOwnPropertyDescriptor;var Lt=Object.getOwnPropertyNames;var Bt=Object.getPrototypeOf,Ut=Object.prototype.hasOwnProperty;var Se=(e,t)=>()=>(e&&(t=e(e=0)),t);var Me=(e,t)=>{for(var n in t)Y(e,n,{get:t[n],enumerable:!0})},Ie=(e,t,n,a)=>{if(t&&typeof t=="object"||typeof t=="function")for(let r of Lt(t))!Ut.call(e,r)&&r!==n&&Y(e,r,{get:()=>t[r],enumerable:!(a=qt(t,r))||a.enumerable});return e};var Vt=(e,t,n)=>(n=e!=null?_t(Bt(e)):{},Ie(t||!e||!e.__esModule?Y(n,"default",{value:e,enumerable:!0}):n,e)),Wt=e=>Ie(Y({},"__esModule",{value:!0}),e);function xe(e,t,n){return new y(e,t,void 0,n)}function ve(e){return e instanceof y}var y,O=Se(()=>{"use strict";y=class e extends Error{constructor(n,a,r,s){super(a,{cause:r});this.code=n;this.details=s;this.cause=r,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 tt={};Me(tt,{FFTProviderFactory:()=>$});var pe,de,$,re=Se(()=>{"use strict";O();pe=class{constructor(t,n,a=!1){this.size=t;this.sampleRate=n;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 n=(await import("webfft")).default;this.fftInstance=new n(this.size),this.enableProfiling&&this.fftInstance?.profile&&await this.fftInstance.profile()}catch(t){throw new y("UNSUPPORTED_FORMAT",`WebFFT\u306E\u521D\u671F\u5316\u306B\u5931\u6557\u3057\u307E\u3057\u305F: ${t instanceof Error?t.message:String(t)}`)}}async fft(t){if(this.initializationPromise&&await this.initializationPromise,!this.fftInstance)throw new y("UNSUPPORTED_FORMAT","WebFFT initialization failed");if(t.length!==this.size)throw new y("INVALID_INPUT",`\u5165\u529B\u30B5\u30A4\u30BA\u304C\u4E0D\u6B63\u3067\u3059\u3002\u671F\u5F85\u5024: ${this.size}, \u5B9F\u969B: ${t.length}`);let n=new Float32Array(this.size*2);for(let i=0;i<this.size;i++)n[i*2]=t[i]||0,n[i*2+1]=0;let a=this.fftInstance.fft(n),r=new Float32Array(this.size/2+1),s=new Float32Array(this.size/2+1),l=new Float32Array(this.size/2+1);for(let i=0;i<r.length;i++){let o=a[i*2]||0,c=a[i*2+1]||0;r[i]=Math.sqrt(o*o+c*c),s[i]=Math.atan2(c,o),l[i]=i*this.sampleRate/this.size}return{complex:a,magnitude:r,phase:s,frequencies:l}}async profile(){if(this.initializationPromise&&await this.initializationPromise,!this.fftInstance||!this.fftInstance.profile)throw new y("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(t){console.warn("FFT instance disposal failed:",t)}finally{this.fftInstance=null,this.initializationPromise=null}}},de=class{constructor(t,n){this.size=t;this.sampleRate=n;if(!this.isPowerOfTwo(t))throw new y("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(t){return t>0&&(t&t-1)===0}precomputeTables(){this.bitReversalTable=new Uint32Array(this.size);let t=Math.log2(this.size);for(let a=0;a<this.size;a++){let r=0;for(let s=0;s<t;s++)r=r<<1|a>>s&1;this.bitReversalTable[a]=r}let n=this.size/2;this.twiddleFactorsReal=new Float32Array(n),this.twiddleFactorsImag=new Float32Array(n);for(let a=0;a<n;a++){let r=-2*Math.PI*a/this.size;this.twiddleFactorsReal[a]=Math.cos(r),this.twiddleFactorsImag[a]=Math.sin(r)}}fft(t){if(t.length!==this.size)throw new y("INVALID_INPUT",`\u5165\u529B\u30B5\u30A4\u30BA\u304C\u4E0D\u6B63\u3067\u3059\u3002\u671F\u5F85\u5024: ${this.size}, \u5B9F\u969B: ${t.length}`);let n=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&&(n[c]=t[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,A=m+f+o,d=n[b]||0,w=a[b]||0,S=n[A]||0,I=a[A]||0,x=S*g-I*h,v=S*h+I*g;n[b]=d+x,a[b]=w+v,n[A]=d-x,a[A]=w-v}}let r=new Float32Array(this.size*2),s=new Float32Array(this.size/2+1),l=new Float32Array(this.size/2+1),i=new Float32Array(this.size/2+1);for(let o=0;o<this.size;o++)if(r[o*2]=n[o]||0,r[o*2+1]=a[o]||0,o<=this.size/2){let c=n[o]||0,u=a[o]||0;s[o]=Math.sqrt(c*c+u*u),l[o]=Math.atan2(u,c),i[o]=o*this.sampleRate/this.size}return{complex:r,magnitude:s,phase:l,frequencies:i}}dispose(){}},$=class{static async createProvider(t){switch(t.type){case"webfft":{let n=new pe(t.fftSize,t.sampleRate,t.enableProfiling);return await n.waitForInitialization(),n}case"native":return new de(t.fftSize,t.sampleRate);case"custom":if(!t.customProvider)throw new y("INVALID_INPUT","\u30AB\u30B9\u30BF\u30E0\u30D7\u30ED\u30D0\u30A4\u30C0\u30FC\u304C\u6307\u5B9A\u3055\u308C\u3066\u3044\u307E\u305B\u3093");return t.customProvider;default:{let n=t.type;throw new y("UNSUPPORTED_FORMAT",`\u672A\u5BFE\u5FDC\u306EFFT\u30D7\u30ED\u30D0\u30A4\u30C0\u30FC: ${String(n)}`)}}}static getAvailableProviders(){return["webfft","native"]}}});var qn={};Me(qn,{AudioInspectError:()=>y,AudioInspectNode:()=>W,FFTProviderFactory:()=>$,RealtimeSTFTProcessor:()=>oe,STFTProcessor:()=>H,amplitudeToDecibels:()=>j,analyze:()=>De,analyzeAll:()=>it,createAudioInspectNode:()=>ke,createAudioInspectNodeWithDefaults:()=>Ne,createError:()=>xe,decibelsToAmplitude:()=>Ve,generateWindow:()=>be,getChannelData:()=>z,getCrestFactor:()=>It,getDefaultProcessorUrl:()=>X,getEnergy:()=>ae,getFFT:()=>q,getLUFS:()=>Ot,getLUFSRealtime:()=>we,getMFCC:()=>ye,getMFCCWithDelta:()=>St,getPeak:()=>K,getPeakAmplitude:()=>K,getPeaks:()=>Ge,getPeaksAnalysis:()=>G,getRMS:()=>ne,getRMSAnalysis:()=>Q,getSpectralCrest:()=>wt,getSpectralEntropy:()=>At,getSpectralFeatures:()=>bt,getSpectrum:()=>at,getStereoAnalysis:()=>xt,getTimeVaryingSpectralFeatures:()=>yt,getTimeVaryingStereoAnalysis:()=>vt,getVAD:()=>Pt,getWaveform:()=>Ye,getWaveformAnalysis:()=>Z,getZeroCrossing:()=>Qe,isAudioInspectError:()=>ve,isPowerOfTwo:()=>me,istft:()=>ut,load:()=>Re,nextPowerOfTwo:()=>ee,normalizeAudio:()=>je,sliceAudio:()=>$e,stft:()=>ct,stream:()=>ue,streamWithFallback:()=>Ee,toMono:()=>We});module.exports=Wt(qn);O();O();async function Re(e,t={}){try{if($t(e))return await Pe(e,t);let n=jt(),a=await Ht(e,n),r=Te(a);return await Pe(r,t)}catch(n){throw new y("DECODE_ERROR",`\u97F3\u58F0\u30C7\u30FC\u30BF\u306E\u8AAD\u307F\u8FBC\u307F\u306B\u5931\u6557\u3057\u307E\u3057\u305F: ${n instanceof Error?n.message:"Unknown error"}`,n)}}function $t(e){return typeof e=="object"&&e!==null&&"sampleRate"in e&&"channelData"in e&&"duration"in e&&"numberOfChannels"in e&&"length"in e}var J=null;function jt(){if(typeof AudioContext<"u")return(!J||J.state==="closed")&&(J=new AudioContext),J;throw new y("UNSUPPORTED_FORMAT","\u3053\u306E\u74B0\u5883\u3067\u306FWeb Audio API\u304C\u5229\u7528\u3067\u304D\u307E\u305B\u3093")}async function Ht(e,t){try{if(e instanceof AudioBuffer)return e;if(e instanceof ArrayBuffer)return await t.decodeAudioData(e);if(e instanceof Blob||e instanceof File){let n=await e.arrayBuffer();return await t.decodeAudioData(n)}if(typeof e=="string"||e instanceof URL){let n=e instanceof URL?e.href:e,a=await fetch(n);if(!a.ok)throw new y("NETWORK_ERROR",`\u97F3\u58F0\u30D5\u30A1\u30A4\u30EB\u306E\u53D6\u5F97\u306B\u5931\u6557\u3057\u307E\u3057\u305F: ${a.status}`);let r=await a.arrayBuffer();return await t.decodeAudioData(r)}throw e instanceof MediaStream?new y("UNSUPPORTED_FORMAT","MediaStream\u306E\u51E6\u7406\u306F\u73FE\u5728\u30B5\u30DD\u30FC\u30C8\u3055\u308C\u3066\u3044\u307E\u305B\u3093"):new y("INVALID_INPUT","\u30B5\u30DD\u30FC\u30C8\u3055\u308C\u3066\u3044\u306A\u3044\u97F3\u58F0\u30BD\u30FC\u30B9\u3067\u3059")}finally{}}function Te(e){let t=[];for(let n=0;n<e.numberOfChannels;n++)t.push(e.getChannelData(n));return{sampleRate:e.sampleRate,channelData:t,duration:e.duration,numberOfChannels:e.numberOfChannels,length:e.length}}async function Pe(e,t){let n=e;if(t.channels==="mono"||t.channels===1?n=Zt(n):(t.channels==="stereo"||t.channels===2)&&(n=Gt(n)),t.normalize&&(n=Qt(n)),t.sampleRate&&t.sampleRate!==n.sampleRate)try{n=await Kt(n,t.sampleRate)}catch(a){throw console.warn("\u30B5\u30F3\u30D7\u30EB\u30EC\u30FC\u30C8\u5909\u63DB\u306B\u5931\u6557\u3057\u307E\u3057\u305F:",a),new y("PROCESSING_ERROR",`\u30B5\u30F3\u30D7\u30EB\u30EC\u30FC\u30C8\u5909\u63DB\u306B\u5931\u6557\u3057\u307E\u3057\u305F: ${a instanceof Error?a.message:"Unknown error"}`)}return n}async function Kt(e,t){if(typeof OfflineAudioContext>"u")throw new y("UNSUPPORTED_FORMAT","\u3053\u306E\u74B0\u5883\u3067\u306F\u30B5\u30F3\u30D7\u30EB\u30EC\u30FC\u30C8\u5909\u63DB\u304C\u30B5\u30DD\u30FC\u30C8\u3055\u308C\u3066\u3044\u307E\u305B\u3093");let n=e.sampleRate,a=e.length,r=Math.floor(a*t/n),s=new OfflineAudioContext(e.numberOfChannels,r,t),l=s.createBuffer(e.numberOfChannels,a,n);for(let c=0;c<e.numberOfChannels;c++){let u=e.channelData[c];u&&l.copyToChannel(u,c)}let i=s.createBufferSource();i.buffer=l,i.connect(s.destination),i.start(0);let o=await s.startRendering();return Te(o)}function Zt(e){if(e.numberOfChannels===1)return e;let t=new Float32Array(e.length);for(let n=0;n<e.length;n++){let a=0;for(let r=0;r<e.numberOfChannels;r++){let s=e.channelData[r],l=s?.[n];s&&l!==void 0&&(a+=l)}t[n]=a/e.numberOfChannels}return{sampleRate:e.sampleRate,channelData:[t],duration:e.duration,numberOfChannels:1,length:e.length}}function Gt(e){if(e.numberOfChannels===2)return e;if(e.numberOfChannels===1){let a=e.channelData[0];if(!a)throw new y("INVALID_INPUT","\u30C1\u30E3\u30F3\u30CD\u30EB0\u306E\u30C7\u30FC\u30BF\u304C\u5B58\u5728\u3057\u307E\u305B\u3093");let r=[new Float32Array(a),new Float32Array(a)];return{sampleRate:e.sampleRate,channelData:r,duration:e.duration,numberOfChannels:2,length:e.length}}let t=e.channelData[0];if(!t)throw new y("INVALID_INPUT","\u30C1\u30E3\u30F3\u30CD\u30EB0\u306E\u30C7\u30FC\u30BF\u304C\u5B58\u5728\u3057\u307E\u305B\u3093");let n=[t,e.channelData[1]||t];return{sampleRate:e.sampleRate,channelData:n,duration:e.duration,numberOfChannels:2,length:e.length}}function Qt(e){let t=0;for(let a of e.channelData)for(let r of a)t=Math.max(t,Math.abs(r));if(t===0)return e;let n=e.channelData.map(a=>{let r=new Float32Array(a.length);for(let s=0;s<a.length;s++){let l=a[s];r[s]=l!==void 0?l/t:0}return r});return{...e,channelData:n}}O();async function De(e,t){try{return Yt(e),await t(e)}catch(n){if(n instanceof y)throw n;let a=n instanceof Error?n.message:"Unknown error";throw new y("PROCESSING_ERROR",`Feature extraction failed: ${a}`,n)}}function Yt(e){if(!e||typeof e!="object")throw new y("INVALID_INPUT","AudioData is invalid");if(typeof e.sampleRate!="number"||e.sampleRate<=0)throw new y("INVALID_INPUT","Sample rate is invalid");if(!Array.isArray(e.channelData)||e.channelData.length===0)throw new y("INVALID_INPUT","Channel data is invalid");if(typeof e.numberOfChannels!="number"||e.numberOfChannels!==e.channelData.length)throw new y("INVALID_INPUT","Number of channels does not match");if(typeof e.length!="number"||e.length<=0)throw new y("INVALID_INPUT","Data length is invalid");if(typeof e.duration!="number"||e.duration<=0)throw new y("INVALID_INPUT","Audio duration is invalid");let t=e.channelData[0];if(!t)throw new y("INVALID_INPUT","Channel 0 data does not exist");let n=t.length;for(let a=0;a<e.channelData.length;a++){let r=e.channelData[a];if(!(r instanceof Float32Array))throw new y("INVALID_INPUT",`Channel ${a} data is not a Float32Array`);if(r.length!==n)throw new y("INVALID_INPUT",`Channel ${a} data length does not match`)}}O();O();var Jt=typeof process<"u"?process.env.NODE_ENV!=="production":!1,Ce=typeof AudioWorkletNode<"u",ie=class{numberOfInputs=1;numberOfOutputs=0;channelCount=2;channelCountMode="max";channelInterpretation="speakers";context=null;port={postMessage:()=>{},close:()=>{},onmessage:null};constructor(){}connect(){return this}disconnect(){return this}dispatchEvent(){return!0}addEventListener(){}removeEventListener(){}},W=class extends(Ce?AudioWorkletNode:ie){onresult;onerror;_featureName="getRMS";_featureOptions;_bufferSize=1024;_hopSize=512;_provider;disposed=!1;cleanupInterval;constructor(t,n){if(!Ce){super(),this._initializeMockMode(n);return}if(!t)throw new y("INITIALIZATION_FAILED","AudioContext is required in browser environment");let a={featureName:"getRMS",bufferSize:1024,hopSize:512,inputChannelCount:1,provider:"native",...n},r={featureName:a.featureName,featureOptions:a.featureOptions,bufferSize:a.bufferSize,hopSize:a.hopSize,inputChannelCount:a.inputChannelCount,provider:a.provider};super(t,"audio-inspect-processor",{processorOptions:r,numberOfInputs:1,numberOfOutputs:0,channelCount:a.inputChannelCount,channelCountMode:"explicit",channelInterpretation:"speakers"}),this._featureName=a.featureName,this._featureOptions=a.featureOptions,this._bufferSize=a.bufferSize,this._hopSize=a.hopSize,this._provider=a.provider,this.setupMessageHandler(),this.setupCleanupInterval()}_initializeMockMode(t){let n={featureName:"getRMS",bufferSize:1024,hopSize:512,provider:"native",...t};this._featureName=n.featureName,this._featureOptions=n.featureOptions,this._bufferSize=n.bufferSize,this._hopSize=n.hopSize,this._provider=n.provider,this.setupCleanupInterval()}getPort(){return this.port}setupMessageHandler(){let t=this.getPort();t&&(t.onmessage=this.handleMessage.bind(this))}setupCleanupInterval(){typeof window<"u"&&(this.cleanupInterval=window.setInterval(()=>{if(this.disposed){this.cleanupInterval&&clearInterval(this.cleanupInterval);return}this.performCleanup()},3e4))}performCleanup(){let t=this.getPort();t&&"postMessage"in t&&t.postMessage({type:"cleanup"})}get featureName(){return this._featureName}get featureOptions(){return this._featureOptions}get bufferSize(){return this._bufferSize}get hopSize(){return this._hopSize}get provider(){return this._provider}updateOptions(t){let n={};if(t.featureName!==void 0&&t.featureName!==this._featureName&&(this._featureName=t.featureName,n.featureName=t.featureName),t.featureOptions!==void 0&&(this._featureOptions=t.featureOptions,n.featureOptions=t.featureOptions),t.bufferSize!==void 0&&t.bufferSize!==this._bufferSize&&(this._bufferSize=t.bufferSize,n.bufferSize=t.bufferSize),t.hopSize!==void 0&&t.hopSize!==this._hopSize&&(this._hopSize=t.hopSize,n.hopSize=t.hopSize),t.inputChannelCount!==void 0&&(n.inputChannelCount=t.inputChannelCount),t.provider!==void 0&&t.provider!==this._provider&&(this._provider=t.provider,n.provider=t.provider),Object.keys(n).length>0){let a={type:"updateOptions",payload:n},r=this.getPort();r&&"postMessage"in r&&r.postMessage(a)}}reset(){let t=this.getPort();t&&"postMessage"in t&&t.postMessage({type:"reset"})}dispose(){if(this.disposed)return;this.disposed=!0,this.cleanupInterval&&(clearInterval(this.cleanupInterval),this.cleanupInterval=void 0),this.disconnect();let t=this.getPort();t&&"close"in t&&t.close()}handleMessage(t){let n=t.data;switch(n.type){case"analysisResult":this.handleAnalysisResult(n);break;case"error":this.handleError(n);break;default:Jt&&console.warn("Unknown message type:",n.type)}}handleAnalysisResult(t){let n={data:t.data,timestamp:t.timestamp};this.onresult&&this.onresult(n),"dispatchEvent"in this&&this.dispatchEvent(new CustomEvent("result",{detail:n}))}handleError(t){let n={message:t.message,detail:t.detail};this.onerror&&this.onerror(n),"dispatchEvent"in this&&this.dispatchEvent(new CustomEvent("error",{detail:n}))}};var Oe=new WeakSet;function ze(e){return Oe.has(e)}function Xt(e){Oe.add(e)}async function ce(e,t){try{if(e.audioWorklet&&"addModule"in e.audioWorklet)await e.audioWorklet.addModule(t,{credentials:"omit"});else throw new y("WORKLET_NOT_SUPPORTED","AudioWorklet is not supported in this environment");Xt(e)}catch(n){throw n instanceof Error&&n.message.includes("Failed to load module script")?new y("MODULE_LOAD_FAILED","Failed to load AudioWorklet module. Ensure the processor file is served with correct MIME type (application/javascript)",n):new y("INITIALIZATION_FAILED",`AudioWorklet module registration failed: ${n instanceof Error?n.message:String(n)}`,n)}}var le=class{_paused=!1;inspectNode;sourceNode;constructor(t,n,a,r,s){this.sourceNode=n,this.inspectNode=a,r&&(this.inspectNode.onresult=l=>r(l.data)),s&&(this.inspectNode.onerror=s),this.sourceNode.connect(this.inspectNode)}pause(){this._paused||(this.sourceNode.disconnect(this.inspectNode),this._paused=!0)}resume(){this._paused&&(this.sourceNode.connect(this.inspectNode),this._paused=!1)}stop(){this.sourceNode.disconnect(this.inspectNode),this.inspectNode.dispose(),this._paused=!0}get paused(){return this._paused}};async function ue(e,t,n,a,r){let s=typeof t=="string"?t:t.name;if(!s)throw new y("INVALID_INPUT","\u89E3\u6790\u6A5F\u80FD\u540D\u304C\u6307\u5B9A\u3055\u308C\u3066\u3044\u307E\u305B\u3093");let l,i;if(e instanceof MediaStream)l=new AudioContext,i=l.createMediaStreamSource(e);else if(e instanceof AudioNode)l=e.context,i=e;else if(e instanceof AudioBuffer)l=new AudioContext,i=l.createBufferSource(),i.buffer=e,i.start();else throw new y("UNSUPPORTED_FORMAT","\u30B5\u30DD\u30FC\u30C8\u3055\u308C\u3066\u3044\u306A\u3044\u97F3\u58F0\u30BD\u30FC\u30B9\u5F62\u5F0F\u3067\u3059");try{await ce(l,n.processorModuleUrl);let o={featureName:s,bufferSize:n?.bufferSize||1024,hopSize:n?.hopSize||512,inputChannelCount:1},c=new W(l,o);return new le(l,i,c,a,r)}catch(o){throw new y("INITIALIZATION_FAILED",`\u30B9\u30C8\u30EA\u30FC\u30DF\u30F3\u30B0\u89E3\u6790\u306E\u521D\u671F\u5316\u306B\u5931\u6557\u3057\u307E\u3057\u305F: ${o instanceof Error?o.message:String(o)}`)}}async function ke(e,t){try{if(!ze(e)){let n=X();await ce(e,n)}return new W(e,t)}catch(n){throw new y("INITIALIZATION_FAILED",`AudioInspectNode\u306E\u4F5C\u6210\u306B\u5931\u6557\u3057\u307E\u3057\u305F: ${n instanceof Error?n.message:String(n)}`)}}function X(){return typeof process<"u"&&process.env.NODE_ENV==="development"?"/dist/core/AudioInspectProcessor.js":"/node_modules/audio-inspect/dist/core/AudioInspectProcessor.js"}async function Ne(e,t,n){let a=X();ze(e)||await ce(e,a);let r={featureName:t,bufferSize:1024,hopSize:512,inputChannelCount:1,...n};return new W(e,r)}async function Ee(e,t,n,a,r){try{return await ue(e,t,n,a,r)}catch(s){if(n.enableFallback){if(console.warn("Falling back to non-realtime analysis:",s),n.fallbackHandler&&e instanceof AudioBuffer){let l={sampleRate:e.sampleRate,channelData:Array.from({length:e.numberOfChannels},(i,o)=>e.getChannelData(o)),duration:e.duration,numberOfChannels:e.numberOfChannels,length:e.length};n.fallbackHandler(l)}return null}throw s}}O();O();O();var _e=new Map;function en(e){if(e<=0||!isFinite(e))throw new y("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 y("UNSUPPORTED_FORMAT",`\u30B5\u30F3\u30D7\u30EB\u30EC\u30FC\u30C8 ${e}Hz \u306F\u30B5\u30DD\u30FC\u30C8\u3055\u308C\u3066\u3044\u307E\u305B\u3093`)}function qe(e){en(e);let t=_e.get(e);if(t)return t.map(h=>({...h}));let n=20.598997,a=107.65265,r=737.86223,s=12194.217,l=1.9997,i=[],o=2*e*Math.tan(Math.PI*n/e),c=2*e*Math.tan(Math.PI*a/e),u=2*e*Math.tan(Math.PI*r/e),m=2*e*Math.tan(Math.PI*s/e),f=Math.pow(10,l/20);{let h=o,b=h*h,A=Math.SQRT2,d=4*e*e+2*A*h*e+b,w=2*(b-4*e*e)/d,S=(4*e*e-2*A*h*e+b)/d;i.push({b0:4*e*e*f/d,b1:-8*e*e*f/d,b2:4*e*e*f/d,a0:1,a1:w,a2:S})}{let h=c,b=2*e+h,A=(h-2*e)/b;i.push({b0:2*e/b,b1:-2*e/b,b2:0,a0:1,a1:A,a2:0})}{let h=u,b=2*e+h,A=(h-2*e)/b;i.push({b0:2*e/b,b1:-2*e/b,b2:0,a0:1,a1:A,a2:0})}{let h=m,b=h*h,A=Math.SQRT2,d=4*e*e+2*A*h*e+b,w=2*(b-4*e*e)/d,S=(4*e*e-2*A*h*e+b)/d;i.push({b0:b/d,b1:2*b/d,b2:b/d,a0:1,a1:w,a2:S})}let g=1/Le(i,1e3,e).magnitude;return i.forEach(h=>{h.b0*=g,h.b1*=g,h.b2*=g}),_e.set(e,i),i}function Le(e,t,n){let a=2*Math.PI*t/n,r=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,A=h*h+b*b;if(A===0)throw new y("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)/A,w=(g*h-p*b)/A,S=r*d-s*w,I=r*w+s*d;r=S,s=I}let l=Math.sqrt(r*r+s*s),i=Math.atan2(s,r);return{magnitude:l,phase:i}}function _(){if(typeof performance<"u"&&performance.now)return performance.now();if(typeof process<"u"&&process.hrtime){let[e,t]=process.hrtime();return e*1e3+t/1e6}return Date.now()}function z(e,t=0){if(t===-1){if(e.numberOfChannels===0)throw new y("INVALID_INPUT","No channels available");for(let r=0;r<e.numberOfChannels;r++)if(!e.channelData[r])throw new y("INVALID_INPUT",`Channel ${r} data does not exist for averaging`);let a=new Float32Array(e.length);for(let r=0;r<e.length;r++){let s=0;for(let l=0;l<e.numberOfChannels;l++){let i=e.channelData[l];i&&r<i.length&&(s+=i[r]??0)}a[r]=s/e.numberOfChannels}return a}if(t<-1||t>=e.numberOfChannels)throw new y("INVALID_INPUT",`Invalid channel number: ${t}`);let n=e.channelData[t];if(!n)throw new y("INVALID_INPUT",`Channel ${t} data does not exist`);return n}function me(e){return e>0&&(e&e-1)===0}function ee(e){if(e<=0||!isFinite(e)||isNaN(e))return 1;if(me(e))return e;let t=1;for(;t<e&&t<Number.MAX_SAFE_INTEGER;)t*=2;return t}function Ue(e,t,n){return t<0||t>=e.length?n:e[t]??n}function te(e){return e==null?!1:!isNaN(e)&&isFinite(e)}function F(e){return e==null||isNaN(e)||!isFinite(e)?0:e}function j(e,t=1){return e<=0||t<=0?-1/0:20*Math.log10(e/t)}function Ve(e,t=1){return e===-1/0?0:t*Math.pow(10,e/20)}function We(e){if(e.numberOfChannels===1)return e;let t=new Float32Array(e.length),n=e.numberOfChannels;for(let a=0;a<e.length;a++){let r=0;for(let s=0;s<n;s++)r+=e.channelData[s]?.[a]??0;t[a]=r/n}return{...e,channelData:[t],numberOfChannels:1}}function $e(e,t,n){if(t<0||n<0||t>=n)throw new y("INVALID_INPUT","\u7121\u52B9\u306A\u6642\u9593\u7BC4\u56F2\u304C\u6307\u5B9A\u3055\u308C\u307E\u3057\u305F",{startTime:t,endTime:n});if(n>e.duration)throw new y("INVALID_INPUT","\u7D42\u4E86\u6642\u9593\u304C\u97F3\u58F0\u306E\u9577\u3055\u3092\u8D85\u3048\u3066\u3044\u307E\u3059",{endTime:n,duration:e.duration});let a=Math.floor(t*e.sampleRate),r=Math.min(Math.floor(n*e.sampleRate),e.length),s=r-a;if(s<=0)throw new y("INSUFFICIENT_DATA","\u30B9\u30E9\u30A4\u30B9\u7BC4\u56F2\u304C\u5C0F\u3055\u3059\u304E\u307E\u3059",{startSample:a,endSample:r,length:s});let l=e.channelData.map(i=>i.slice(a,r));return{...e,channelData:l,duration:n-t,length:s}}function je(e){let t=0;for(let a of e.channelData)for(let r of a)t=Math.max(t,Math.abs(r));if(t===0||t===1)return e;let n=e.channelData.map(a=>{let r=new Float32Array(a.length);for(let s=0;s<a.length;s++)r[s]=(a[s]??0)/t;return r});return{...e,channelData:n}}function tn(e,t,n={x1:0,x2:0,y1:0,y2:0}){let a=new Float32Array(e.length),{x1:r,x2:s,y1:l,y2:i}=n;for(let o=0;o<e.length;o++){let c=F(e[o]??0),u=t.b0*c+t.b1*r+t.b2*s-t.a1*l-t.a2*i;a[o]=u,s=r,r=c,i=l,l=u}return n.x1=r,n.x2=s,n.y1=l,n.y2=i,a}function fe(e,t){let n=qe(t),a=e;for(let r of n)a=tn(a,r);return a}function He(e,t){let n=(e.length-1)*t+1,a=new Float32Array(n);for(let r=0;r<e.length-1;r++){let s=F(e[r]??0),i=(F(e[r+1]??0)-s)/t;for(let o=0;o<t;o++)a[r*t+o]=s+i*o}return e.length>0&&(a[n-1]=F(e[e.length-1]??0)),a}function Be(e,t){if(e.length<4)return He(e,t);let n=(e.length-1)*t+1,a=new Float32Array(n);for(let r=0;r<e.length-1;r++){let s=F(e[Math.max(0,r-1)]??0),l=F(e[r]??0),i=F(e[Math.min(e.length-1,r+1)]??0),o=F(e[Math.min(e.length-1,r+2)]??0);for(let c=0;c<t;c++){let u=c/t,m=u*u,f=m*u,p=.5*(2*l+(-s+i)*u+(2*s-5*l+4*i-o)*m+(-s+3*l-3*i+o)*f);a[r*t+c]=F(p)}}return e.length>0&&(a[n-1]=F(e[e.length-1]??0)),a}function nn(e,t){let n=(e.length-1)*t+1,a=new Float32Array(n),r=s=>{if(Math.abs(s)<1e-10)return 1;let l=Math.PI*s;return Math.abs(s)<3?3*Math.sin(l/3)*Math.sin(l)/(l*l):0};for(let s=0;s<n;s++){let l=s/t,i=0;for(let o=-3;o<=3;o++){let c=Math.floor(l)+o;if(c>=0&&c<e.length){let u=F(e[c]??0),m=r(l-c);i+=u*m}}a[s]=F(i)}return a}function rn(e,t={}){let{factor:n=4,interpolation:a="cubic"}=t;if(![2,4,8].includes(n))throw new y("INVALID_INPUT","\u30AA\u30FC\u30D0\u30FC\u30B5\u30F3\u30D7\u30EA\u30F3\u30B0\u4FC2\u6570\u306F2\u30014\u3001\u307E\u305F\u306F8\u3067\u3042\u308B\u5FC5\u8981\u304C\u3042\u308A\u307E\u3059\u3002\u4ED6\u306E\u5024\u3067\u306Fsinc\u88DC\u9593\u54C1\u8CEA\u304C\u4FDD\u8A3C\u3055\u308C\u307E\u305B\u3093\u3002");if(n<=1)return e;if(e.length===0)return new Float32Array(0);switch(a){case"linear":return He(e,n);case"cubic":return Be(e,n);case"sinc":return nn(e,n);default:return Be(e,n)}}function Ke(e,t={}){if(e.length===0)return 0;let n=rn(e,t),a=0;for(let r=0;r<n.length;r++){let s=Math.abs(F(n[r]??0));a=Math.max(a,s)}return a}function Ze(e,t,n=!1){let a=[],r=e.length;if(r<3)return a;for(let s=1;s<r-1;s++){let l=Math.abs(F(e[s]??0)),i=Math.abs(F(e[s-1]??0)),o=Math.abs(F(e[s+1]??0));if(l>i&&l>o&&l>t){let c={position:s,amplitude:l};n&&(c.prominence=an(e,s,l)),a.push(c)}}return a}function an(e,t,n){let a=n;for(let s=t-1;s>=0;s--){let l=Math.abs(F(e[s]??0));if(l>n)break;a=Math.min(a,l)}let r=n;for(let s=t+1;s<e.length;s++){let l=Math.abs(F(e[s]??0));if(l>n)break;r=Math.min(r,l)}return n-Math.max(a,r)}function Ge(e,t={}){let{count:n=100,threshold:a=.1,channel:r=0,minDistance:s=Math.floor(e.sampleRate/100)}=t;if(n<=0)throw new y("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 y("INVALID_INPUT","\u95BE\u5024\u306F0\u304B\u30891\u306E\u7BC4\u56F2\u3067\u3042\u308B\u5FC5\u8981\u304C\u3042\u308A\u307E\u3059");let l=z(e,r);if(l.length===0)return{peaks:[],maxAmplitude:0,averageAmplitude:0};let i=Ze(l,a);if(i.length===0)return{peaks:[],maxAmplitude:0,averageAmplitude:0};i.sort((f,p)=>p.amplitude-f.amplitude);let o=[],c=[];for(let f of i){if(o.length>=n)break;let p=f.position-s,g=f.position+s;c.some(([b,A])=>!(g<b||p>A))||(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=i.length>0?i[0]?.amplitude??0:0,m=i.length>0?i.reduce((f,p)=>f+p.amplitude,0)/i.length:0;return{peaks:o,maxAmplitude:u,averageAmplitude:m}}var he=-1/0;function ne(e,t={}){let n=typeof t=="number"?{channel:t,asDB:!1,reference:1,truePeak:!1,oversamplingFactor:4,interpolation:"cubic"}:{channel:t.channel??0,asDB:t.asDB??!1,reference:t.reference??1,truePeak:t.truePeak??!1,oversamplingFactor:t.oversamplingFactor??4,interpolation:t.interpolation??"cubic"},a=z(e,n.channel);if(a.length===0)return n.asDB?he:0;let r=0,s=0;for(let i=0;i<a.length;i++){let o=a[i]??0;te(o)&&(r+=o*o,s++)}if(s===0)return n.asDB?he:0;let l=Math.sqrt(r/s);return n.asDB?j(l,n.reference):l}function K(e,t={}){let n={channel:t.channel??0,asDB:t.asDB??!1,reference:t.reference??1,truePeak:t.truePeak??!1,oversamplingFactor:t.oversamplingFactor??4,interpolation:t.interpolation??"cubic"},a=z(e,n.channel);if(a.length===0)return n.asDB?he:0;let r;if(n.truePeak)r=Ke(a,{factor:n.oversamplingFactor,interpolation:n.interpolation});else{r=0;for(let s=0;s<a.length;s++){let l=a[s]??0;te(l)&&(r=Math.max(r,Math.abs(l)))}}return n.asDB?j(r,n.reference):r}function Qe(e,t=0){let n=z(e,t);if(n.length<2)return 0;let a=0;for(let r=1;r<n.length;r++){let s=F(n[r-1]??0),l=F(n[r]??0);(s>=0&&l<0||s<0&&l>=0)&&a++}return a/(n.length-1)}function Ye(e,t={}){let{framesPerSecond:n=60,channel:a=0,method:r="rms"}=t,s=z(e,a),l=Math.ceil(e.duration*n),i=e.length>0?e.length:l>0?1:0,o=Math.min(l,i),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 A=s.subarray(h,b),d;switch(r){case"peak":d=Xe(A);break;case"average":d=et(A);break;case"rms":default:d=Je(A);break}let w=(h+(b-h)/2)/e.sampleRate;u.push({time:w,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 Je(e){if(e.length===0)return 0;let t=0;for(let n=0;n<e.length;n++){let a=F(e[n]??0);t+=a*a}return Math.sqrt(t/e.length)}function Xe(e){let t=0;for(let n=0;n<e.length;n++){let a=Math.abs(F(e[n]??0));t=Math.max(t,a)}return t}function et(e){if(e.length===0)return 0;let t=0;for(let n=0;n<e.length;n++)t+=Math.abs(F(e[n]??0));return t/e.length}function Z(e,t={}){let n=_(),{framesPerSecond:a=60,channel:r=0,method:s="rms",onProgress:l}=t;l?.(0,"Waveform\u89E3\u6790\u3092\u958B\u59CB");let i=z(e,r),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;l?.(25,"\u30D5\u30EC\u30FC\u30E0\u8A2D\u5B9A\u5B8C\u4E86");let f=new Float32Array(u),p=new Float32Array(u),g=0,h=0;l?.(50,"\u632F\u5E45\u8A08\u7B97\u3092\u958B\u59CB");for(let d=0;d<u;d++){let w=d*m,S=Math.min(w+m,i.length);if(S<=w){let v=d>0?f[d-1]??0:0;f[d]=v,p[d]=(w+m/2)/e.sampleRate;continue}let I=i.subarray(w,S),x;switch(s){case"peak":x=Xe(I);break;case"average":x=et(I);break;case"rms":default:x=Je(I);break}if(f[d]=x,p[d]=(w+(S-w)/2)/e.sampleRate,g=Math.max(g,x),h+=x,d%Math.max(1,Math.floor(u/20))===0){let v=50+d/u*45;l?.(Math.round(v),`\u30D5\u30EC\u30FC\u30E0 ${d+1}/${u} \u51E6\u7406\u4E2D`)}}let b=u>0?h/u:0,A=_()-n;return l?.(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:A}}function G(e,t={}){let n=_(),{count:a=100,threshold:r=.1,channel:s=0,minDistance:l=Math.floor(e.sampleRate/100),onProgress:i}=t;if(i?.(0,"Peaks\u89E3\u6790\u3092\u958B\u59CB"),a<=0)throw new y("INVALID_INPUT","\u30D4\u30FC\u30AF\u6570\u306F\u6B63\u306E\u6574\u6570\u3067\u3042\u308B\u5FC5\u8981\u304C\u3042\u308A\u307E\u3059");if(r<0||r>1)throw new y("INVALID_INPUT","\u95BE\u5024\u306F0\u304B\u30891\u306E\u7BC4\u56F2\u3067\u3042\u308B\u5FC5\u8981\u304C\u3042\u308A\u307E\u3059");let o=z(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:_()-n};i?.(25,"\u30D4\u30FC\u30AF\u5019\u88DC\u3092\u691C\u51FA\u4E2D");let c=Ze(o,r);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:_()-n};i?.(50,"\u30D4\u30FC\u30AF\u3092\u30BD\u30FC\u30C8\u4E2D"),c.sort((d,w)=>w.amplitude-d.amplitude),i?.(75,"\u30D4\u30FC\u30AF\u3092\u9078\u629E\u4E2D");let u=[],m=[];for(let d of c){if(u.length>=a)break;let w=d.position-l,S=d.position+l;m.some(([x,v])=>!(S<x||w>v))||(u.push({position:d.position,time:d.position/e.sampleRate,amplitude:d.amplitude}),m.push([w,S]))}u.sort((d,w)=>d.position-w.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 w=u[d];w&&(f[d]=w.position,p[d]=w.amplitude,g[d]=w.time)}let h=c.length>0?c[0]?.amplitude??0:0,b=c.length>0?c.reduce((d,w)=>d+w.amplitude,0)/c.length:0,A=_()-n;return i?.(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:A}}function Q(e,t={}){let n=_(),{channel:a=0,asDB:r=!1,reference:s=1,onProgress:l}=t;l?.(0,"RMS\u89E3\u6790\u3092\u958B\u59CB");let i=z(e,a);if(i.length===0)return{value:r?-1/0:0,channel:a,sampleRate:e.sampleRate,duration:e.duration,processingTime:_()-n};l?.(50,"RMS\u5024\u3092\u8A08\u7B97\u4E2D");let o=0;for(let m=0;m<i.length;m++){let f=i[m]??0;if(te(f)){let p=F(f);o+=p*p}}let c=Math.sqrt(o/i.length),u=_()-n;return l?.(100,"\u51E6\u7406\u5B8C\u4E86"),r?{value:j(c,s),channel:a,sampleRate:e.sampleRate,duration:e.duration,processingTime:u}:{value:c,valueDB:j(c,s),channel:a,sampleRate:e.sampleRate,duration:e.duration,processingTime:u}}O();re();function nt(e,t){let n=new Float32Array(e.length),a=e.length;for(let r=0;r<a;r++){let s=1;switch(t){case"hann":s=.5*(1-Math.cos(2*Math.PI*r/(a-1)));break;case"hamming":s=.54-.46*Math.cos(2*Math.PI*r/(a-1));break;case"blackman":s=.42-.5*Math.cos(2*Math.PI*r/(a-1))+.08*Math.cos(4*Math.PI*r/(a-1));break;case"rectangular":case"none":default:s=1;break}n[r]=(e[r]||0)*s}return n}function rt(e,t){if(t===-1){let a=new Float32Array(e.length);for(let r=0;r<e.length;r++){let s=0;for(let l=0;l<e.numberOfChannels;l++){let i=e.channelData[l];i&&r<i.length&&(s+=i[r])}a[r]=s/e.numberOfChannels}return a}if(t<-1||t>=e.numberOfChannels)throw new y("INVALID_INPUT",`Invalid channel number: ${t}`);let n=e.channelData[t];if(!n)throw new y("INVALID_INPUT",`Channel ${t} data does not exist`);return n}async function q(e,t={}){let{fftSize:n=2048,windowFunction:a="hann",channel:r=0,provider:s="native",enableProfiling:l=!1}=t,i=rt(e,r),o;i.length<n?(o=new Float32Array(n),o.set(i)):o=i.slice(0,n);let c=nt(o,a),u=s==="webfft"?["webfft","native"]:[s],m,f=null;for(let p of u)try{f=await $.createProvider({type:p,fftSize:n,sampleRate:e.sampleRate,enableProfiling:l});break}catch(g){if(m=g,p==="webfft"&&u.includes("native"))continue}if(!f)throw m||new y("FFT_PROVIDER_ERROR","Failed to create FFT provider");try{return{...await f.fft(c),fftSize:n,windowFunction:a,providerName:f.name}}finally{f.dispose()}}async function at(e,t={}){let{fftSize:n=2048,minFrequency:a=0,maxFrequency:r=e.sampleRate/2,decibels:s=!0,timeFrames:l=1,overlap:i=.5,...o}=t,c=rt(e,t.channel||0);if(l===1){let u=await q(e,{...o,fftSize:n}),m=on(u,a,r),f={frequencies:m.frequencies,magnitudes:m.magnitude};return s&&(f.decibels=ot(m.magnitude)),f}else{let u=await sn(c,e.sampleRate,n,l,i,{...o,minFrequency:a,maxFrequency:r,decibels:s});return{frequencies:u.frequencies,magnitudes:new Float32Array,spectrogram:u}}}function on(e,t,n){let{frequencies:a,magnitude:r,phase:s,complex:l}=e,i=a.findIndex(u=>u>=t);i<0&&(i=0);let o=a.findIndex(u=>u>n),c=o===-1?a.length:o;return i>=c?{frequencies:new Float32Array(0),magnitude:new Float32Array(0),phase:new Float32Array(0),complex:new Float32Array(0)}:{frequencies:a.slice(i,c),magnitude:r.slice(i,c),phase:s.slice(i,c),complex:l.slice(i*2,c*2)}}function ot(e){let t=new Float32Array(e.length);for(let n=0;n<e.length;n++){let a=e[n]||0;t[n]=a>0?20*Math.log10(a):-1/0}return t}async function sn(e,t,n,a,r,s){let l=Math.floor(n*(1-r)),i;e.length===0?i=0:e.length<n?i=1:i=Math.floor((e.length-n)/l)+1;let o=Math.min(a,i),c=new Float32Array(o),u=[],m=new Float32Array,f=new Float32Array,p=0,g=0,h=await $.createProvider({type:s.provider||"native",fftSize:n,sampleRate:t,enableProfiling:s.enableProfiling||!1});try{for(let b=0;b<o;b++){let A=b*l,d=new Float32Array(n);for(let C=0;C<n;C++)d[C]=A+C<e.length&&e[A+C]||0;let w=nt(d,s.windowFunction||"hann"),S=await h.fft(w);if(b===0){m=S.frequencies;let C=s.minFrequency||0,E=s.maxFrequency||t/2;p=m.findIndex(k=>k>=C),p===-1&&(p=0);let L=m.findIndex(k=>k>E);g=L===-1?m.length:L,f=m.slice(p,g)}let x=S.magnitude.slice(p,g),v=s.decibels?ot(x):x;u.push(v),c[b]=(A+n/2)/t}}finally{h.dispose()}return{times:c,frequencies:f,intensities:u,timeFrames:o,frequencyBins:f.length}}O();function st(e,t,n,a){let r=new Float32Array(a);for(let s=0;s<a&&n+s<e.length;s++){let l=1;switch(t){case"hann":l=.5*(1-Math.cos(2*Math.PI*s/(a-1)));break;case"hamming":l=.54-.46*Math.cos(2*Math.PI*s/(a-1));break;case"blackman":l=.42-.5*Math.cos(2*Math.PI*s/(a-1))+.08*Math.cos(4*Math.PI*s/(a-1));break;case"rectangular":case"none":default:l=1}let i=F(e[n+s]??0);r[s]=i*l}return r}function ae(e,t={}){let{frameSize:n=Math.floor(e.sampleRate*.025),hopSize:a=Math.floor(e.sampleRate*.01),channel:r=0,normalized:s=!1,windowFunction:l="rectangular"}=t;if(n<=0||!Number.isInteger(n))throw new y("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 y("INVALID_INPUT","hopSize\u306F\u6B63\u306E\u6574\u6570\u3067\u3042\u308B\u5FC5\u8981\u304C\u3042\u308A\u307E\u3059");a>n&&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 i=z(e,r),o=i.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-n)/a)+1);if(c===0){let d=ln(i,0,o,l);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 w=d*a,S=st(i,l,w,n),I=0;for(let x=0;x<S.length;x++){let v=S[x];v!==void 0&&(I+=v*v)}u[d]=(w+n/2)/e.sampleRate,m[d]=I,f+=I,p=Math.max(p,I),g=Math.min(g,I)}let h=f/c,b=0;for(let d=0;d<c;d++){let w=m[d];if(w!==void 0){let S=w-h;b+=S*S}}let A=Math.sqrt(b/c);if(s&&f>1e-10){for(let d=0;d<m.length;d++){let w=m[d];w!==void 0&&(m[d]=w/f)}return{times:u,energies:m,totalEnergy:1,statistics:{mean:h/f,std:A/f,max:p/f,min:g/f}}}return{times:u,energies:m,totalEnergy:f,statistics:{mean:h,std:A,max:p,min:g}}}function ln(e,t,n,a){let r=st(e,a,t,n),s=0;for(let l of r)s+=l*l;return s}async function it(e,t){let n=_(),a={processingTime:0},r=[];t.waveform!==void 0&&r.push({name:"waveform",weight:.15,execute:()=>Z(e,{...t.waveform,onProgress:(i,o)=>t.onProgress?.(i*.15,`Waveform: ${o}`)})}),t.peaks!==void 0&&r.push({name:"peaks",weight:.2,execute:()=>G(e,{...t.peaks,onProgress:(i,o)=>t.onProgress?.(i*.2,`Peaks: ${o}`)})}),t.rms!==void 0&&r.push({name:"rms",weight:.1,execute:()=>Q(e,{...t.rms,onProgress:(i,o)=>t.onProgress?.(i*.1,`RMS: ${o}`)})}),t.spectrum!==void 0&&r.push({name:"spectrum",weight:.35,execute:async()=>{let i={};t.spectrum?.fftSize!==void 0&&(i.fftSize=t.spectrum.fftSize),t.spectrum?.channel!==void 0&&(i.channel=t.spectrum.channel),t.spectrum?.windowFunction!==void 0&&(i.windowFunction=t.spectrum.windowFunction==="rectangular"?"none":t.spectrum.windowFunction);let o=await q(e,i);return{frequencies:o.frequencies,magnitudes:o.magnitude,phases:o.phase,fftSize:o.fftSize,windowFunction:o.windowFunction,sampleRate:e.sampleRate,duration:e.duration}}}),t.energy!==void 0&&r.push({name:"energy",weight:.2,execute:()=>{let i=ae(e,{...t.energy});return{energies:i.energies,times:i.times,totalEnergy:i.totalEnergy,meanEnergy:i.statistics.mean,maxEnergy:i.statistics.max,minEnergy:i.statistics.min,sampleRate:e.sampleRate,duration:e.duration}}});let s=r.reduce((i,o)=>i+o.weight,0);s>0&&r.forEach(i=>i.weight/=s);let l=0;return await Promise.all(r.map(async i=>{try{let o=await i.execute();switch(i.name){case"waveform":o&&(a.waveform=o);break;case"peaks":o&&(a.peaks=o);break;case"rms":o&&(a.rms=o);break;case"spectrum":o&&(a.spectrum=o);break;case"energy":o&&(a.energy=o);break}l+=i.weight,t.onProgress?.(Math.round(l*100),i.name)}catch(o){console.warn(`Task ${i.name} failed:`,o)}})),a.processingTime=_()-n,a}re();O();function be(e,t,n={}){let a=new Float32Array(t);switch(e){case"hann":for(let r=0;r<t;r++)a[r]=.5-.5*Math.cos(2*Math.PI*r/(t-1));break;case"hamming":for(let r=0;r<t;r++)a[r]=.54-.46*Math.cos(2*Math.PI*r/(t-1));break;case"blackman":for(let r=0;r<t;r++)a[r]=.42-.5*Math.cos(2*Math.PI*r/(t-1))+.08*Math.cos(4*Math.PI*r/(t-1));break;case"bartlett":for(let r=0;r<t;r++)a[r]=1-Math.abs((r-(t-1)/2)/((t-1)/2));break;case"kaiser":{let r=n.kaiserBeta??8.6,s=lt(r);for(let l=0;l<t;l++){let i=2*l/(t-1)-1;a[l]=lt(r*Math.sqrt(1-i*i))/s}}break;case"tukey":{let r=n.tukeyAlpha??.5,s=Math.floor(r*(t-1)/2);for(let l=0;l<t;l++)l<s?a[l]=.5*(1+Math.cos(Math.PI*(2*l/(r*(t-1))-1))):l>t-1-s?a[l]=.5*(1+Math.cos(Math.PI*(2*l/(r*(t-1))-2/r+1))):a[l]=1}break;case"rectangular":a.fill(1);break;default:throw new y("INVALID_INPUT",`Unknown window type: ${e}`)}return a}function lt(e){let t=1,n=1,a=e*e/4;for(let r=1;r<50&&(n*=a/(r*r),t+=n,!(n<1e-10*t));r++);return t}var ge=class{size;levels;cosTable;sinTable;constructor(t){if(t<=0||(t&t-1)!==0)throw new y("INVALID_INPUT","FFT size must be a power of 2");this.size=t,this.levels=Math.log2(t),this.cosTable=new Float32Array(t/2),this.sinTable=new Float32Array(t/2);for(let n=0;n<t/2;n++){let a=-2*Math.PI*n/t;this.cosTable[n]=Math.cos(a),this.sinTable[n]=Math.sin(a)}}forward(t,n){this.bitReverse(t),this.bitReverse(n);for(let a=1;a<=this.levels;a++){let r=1<<a,s=r>>1;for(let l=0;l<this.size;l+=r)for(let i=0;i<s;i++){let o=l+i,c=o+s,u=i*(this.size/r),m=this.cosTable[u]??0,f=this.sinTable[u]??0,p=(t[c]??0)*m-(n[c]??0)*f,g=(t[c]??0)*f+(n[c]??0)*m;t[c]=(t[o]??0)-p,n[c]=(n[o]??0)-g,t[o]=(t[o]??0)+p,n[o]=(n[o]??0)+g}}}inverse(t,n){for(let r=0;r<this.size;r++)n[r]=-(n[r]??0);this.forward(t,n);let a=1/this.size;for(let r=0;r<this.size;r++)t[r]=(t[r]??0)*a,n[r]=-(n[r]??0)*a}bitReverse(t){let n=this.size,a=0;for(let r=0;r<n-1;r++){if(r<a){let l=t[r]??0;t[r]=t[a]??0,t[a]=l}let s=n>>1;for(;s<=a;)a-=s,s>>=1;a+=s}}},H=class{fft;fftSize;windowSize;hopSize;window;normalize;constructor(t,n={}){if(this.fftSize=n.fftSize??2048,this.windowSize=n.windowSize??this.fftSize,this.hopSize=n.hopSize??Math.floor(this.windowSize/2),this.normalize=n.normalize??"backward",this.fftSize<=0||(this.fftSize&this.fftSize-1)!==0)throw new y("INVALID_INPUT","FFT size must be a power of 2");if(this.windowSize>this.fftSize)throw new y("INVALID_INPUT","Window size cannot be larger than FFT size");this.fft=new ge(this.fftSize);let a=n.windowType??"hann";this.window=be(a,this.windowSize,n)}stft(t,n){let a=t.length>=this.windowSize?Math.floor((t.length-this.windowSize)/this.hopSize)+1:0,r=Math.floor(this.fftSize/2)+1,s=[],l=[],i=[],o=new Float32Array(this.fftSize),c=new Float32Array(this.fftSize);for(let f=0;f<a;f++){let p=f*this.hopSize;o.fill(0),c.fill(0);for(let d=0;d<this.windowSize;d++){let w=p+d;w<t.length&&(o[d]=F(t[w]??0)*(this.window[d]??0))}this.fft.forward(o,c);let g=1;this.normalize==="forward"?g=1/this.fftSize:this.normalize==="ortho"&&(g=1/Math.sqrt(this.fftSize));let h=new Float32Array(r*2),b=new Float32Array(r),A=new Float32Array(r);for(let d=0;d<r;d++){let w=(o[d]??0)*g,S=(c[d]??0)*g;h[d*2]=w,h[d*2+1]=S,b[d]=Math.sqrt(w*w+S*S),A[d]=Math.atan2(S,w)}s.push(h),l.push(b),i.push(A)}let u=new Float32Array(a);for(let f=0;f<a;f++)u[f]=f*this.hopSize/n;let m=new Float32Array(r);for(let f=0;f<r;f++)m[f]=f*n/this.fftSize;return{complex:s,magnitude:l,phase:i,frameCount:a,frequencyBins:r,times:u,frequencies:m}}istft(t,n){let{complex:a,frameCount:r,frequencyBins:s}=t,l=n??(r-1)*this.hopSize+this.windowSize,i=new Float32Array(l),o=new Float32Array(l),c=new Float32Array(this.fftSize),u=new Float32Array(this.fftSize);for(let m=0;m<r;m++){let f=m*this.hopSize,p=a[m]??new Float32Array(0);c.fill(0),u.fill(0);for(let h=0;h<s;h++)c[h]=p[h*2]??0,u[h]=p[h*2+1]??0;for(let h=1;h<s-1;h++)c[this.fftSize-h]=c[h]??0,u[this.fftSize-h]=-(u[h]??0);this.fft.inverse(c,u);let g=1;this.normalize==="backward"?g=1:this.normalize==="forward"?g=this.fftSize:this.normalize==="ortho"&&(g=Math.sqrt(this.fftSize));for(let h=0;h<this.windowSize;h++){let b=f+h;if(b<l){let A=(c[h]??0)*g*(this.window[h]??0);i[b]=(i[b]??0)+A,o[b]=(o[b]??0)+(this.window[h]??0)*(this.window[h]??0)}}}for(let m=0;m<l;m++)(o[m]??0)>0&&(i[m]=(i[m]??0)/(o[m]??1));return i}processFrame(t,n){if(t.length!==this.windowSize)throw new y("INVALID_INPUT",`Frame size (${t.length}) must match window size (${this.windowSize})`);let a=new Float32Array(this.fftSize),r=new Float32Array(this.fftSize);for(let m=0;m<this.windowSize;m++)a[m]=F(t[m]??0)*(this.window[m]??0);this.fft.forward(a,r);let s=1;this.normalize==="forward"?s=1/this.fftSize:this.normalize==="ortho"&&(s=1/Math.sqrt(this.fftSize));let l=Math.floor(this.fftSize/2)+1,i=new Float32Array(l*2),o=new Float32Array(l),c=new Float32Array(l),u=new Float32Array(l);for(let m=0;m<l;m++){let f=(a[m]??0)*s,p=(r[m]??0)*s;i[m*2]=f,i[m*2+1]=p,o[m]=Math.sqrt(f*f+p*p),c[m]=Math.atan2(p,f),u[m]=m*n/this.fftSize}return{complex:i,magnitude:o,phase:c,frequencies:u}}getConfig(){return{fftSize:this.fftSize,windowSize:this.windowSize,hopSize:this.hopSize,overlapRatio:1-this.hopSize/this.windowSize}}};function ct(e,t,n={}){return new H(t,n).stft(e,t)}function ut(e,t,n={},a){return new H(t,n).istft(e,a)}var oe=class{processor;inputBuffer;bufferPosition;windowSize;hopSize;sampleRate;constructor(t,n={}){this.sampleRate=t,this.processor=new H(t,n);let a=this.processor.getConfig();this.windowSize=a.windowSize,this.hopSize=a.hopSize,this.inputBuffer=new Float32Array(this.windowSize),this.bufferPosition=0}process(t,n){let a=[],r=0;for(;r<t.length;){let s=Math.min(t.length-r,this.windowSize-this.bufferPosition);if(this.inputBuffer.set(t.subarray(r,r+s),this.bufferPosition),this.bufferPosition+=s,r+=s,this.bufferPosition>=this.windowSize){let l=this.processor.processFrame(this.inputBuffer,n);a.push({...l,time:a.length*this.hopSize/this.sampleRate}),this.inputBuffer.copyWithin(0,this.hopSize),this.bufferPosition=this.windowSize-this.hopSize}}return{frames:a}}reset(){this.inputBuffer.fill(0),this.bufferPosition=0}getBufferStatus(){return{position:this.bufferPosition,size:this.windowSize}}};O();function ft(e,t,n,a){let r=0,s=0;for(let l=0;l<e.length&&l<t.length;l++){let i=t[l],o=e[l];i!==void 0&&o!==void 0&&i>=n&&i<=a&&(r+=i*o,s+=o)}return s>1e-10?r/s:0}function ht(e,t,n,a,r){let s=0,l=0;for(let i=0;i<e.length&&i<t.length;i++){let o=t[i],c=e[i];if(o!==void 0&&c!==void 0&&o>=a&&o<=r){let u=o-n;s+=u*u*c,l+=c}}return l>1e-10?Math.sqrt(s/l):0}function pt(e,t,n,a,r){let s=0;for(let o=0;o<e.length&&o<t.length;o++){let c=t[o],u=e[o];c!==void 0&&u!==void 0&&c>=a&&c<=r&&(s+=u*u)}let l=s*n,i=0;for(let o=0;o<e.length&&o<t.length;o++){let c=t[o],u=e[o];if(c!==void 0&&u!==void 0&&c>=a&&c<=r&&(i+=u*u,i>=l))return c}return r}function dt(e,t,n){let a=0,r=0,s=0;for(let l=t;l<=n&&l<e.length;l++){let i=e[l];if(i!==void 0){let o=Math.max(i,1e-10);a+=Math.log(o),r+=o,s++}}return s===0?0:(a=Math.exp(a/s),r=r/s,r>1e-10?a/r:0)}function gt(e){if(e.length<2)return 0;let t=0;for(let n=1;n<e.length;n++){let a=F(e[n-1]??0),r=F(e[n]??0);(a>=0&&r<0||a<0&&r>=0)&&t++}return t/(e.length-1)}function cn(e,t,n={normalize:!0}){if(!t||t.length===0)return 0;let a=Math.min(e.length,t.length);if(a===0)return 0;let r=e,s=t;if(n.normalize!==!1){let i=0,o=0;for(let m=0;m<a;m++){let f=e[m]??0,p=t[m]??0;i+=f*f,o+=p*p}let c=Math.sqrt(i),u=Math.sqrt(o);if(c>1e-10&&u>1e-10){r=new Float32Array(a),s=new Float32Array(a);for(let m=0;m<a;m++)r[m]=(e[m]??0)/c,s[m]=(t[m]??0)/u}}let l=0;for(let i=0;i<a;i++){let o=(r[i]??0)-(s[i]??0);l+=o*o}return Math.sqrt(l)}async function bt(e,t={}){let{fftSize:n=2048,windowFunction:a="hann",channel:r=0,minFrequency:s=0,maxFrequency:l=e.sampleRate/2,rolloffThreshold:i=.85}=t;if(r>=e.numberOfChannels)throw new y("INVALID_INPUT",`\u7121\u52B9\u306A\u30C1\u30E3\u30F3\u30CD\u30EB\u756A\u53F7: ${r}`);let o=await q(e,{fftSize:n,windowFunction:a,channel:r}),c=Math.max(0,Math.floor(s*n/e.sampleRate)),u=Math.min(o.frequencies.length-1,Math.floor(l*n/e.sampleRate)),m=ft(o.magnitude,o.frequencies,s,l),f=ht(o.magnitude,o.frequencies,m,s,l),p=pt(o.magnitude,o.frequencies,i,s,l),g=dt(o.magnitude,c,u),h=e.channelData[r];if(!h)throw new y("INVALID_INPUT",`\u30C1\u30E3\u30F3\u30CD\u30EB ${r} \u306E\u30C7\u30FC\u30BF\u304C\u5B58\u5728\u3057\u307E\u305B\u3093`);let b=gt(h);return{spectralCentroid:m,spectralBandwidth:f,spectralRolloff:p,spectralFlatness:g,zeroCrossingRate:b,frequencyRange:{min:s,max:l}}}async function yt(e,t={}){let{frameSize:n=2048,hopSize:a=n/2,fftSize:r=n,windowFunction:s="hann",channel:l=0,minFrequency:i=0,maxFrequency:o=e.sampleRate/2,rolloffThreshold:c=.85,numFrames:u}=t;if(l>=e.numberOfChannels)throw new y("INVALID_INPUT",`\u7121\u52B9\u306A\u30C1\u30E3\u30F3\u30CD\u30EB\u756A\u53F7: ${l}`);let m=e.channelData[l];if(!m)throw new y("INVALID_INPUT",`\u30C1\u30E3\u30F3\u30CD\u30EB ${l} \u306E\u30C7\u30FC\u30BF\u304C\u5B58\u5728\u3057\u307E\u305B\u3093`);let f=u||Math.floor((m.length-n)/a)+1;if(f<=0)throw new y("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),A=new Float32Array(f),d=new Float32Array(f),w=new Float32Array(f),S,{FFTProviderFactory:I}=await Promise.resolve().then(()=>(re(),tt)),x=await I.createProvider({type:"native",fftSize:r,sampleRate:e.sampleRate,enableProfiling:!1});try{for(let v=0;v<f;v++){let C=v*a,E=Math.min(C+n,m.length);p[v]=C/e.sampleRate;let L=m.subarray(C,E),k=new Float32Array(r),U=Math.min(L.length,r);U>0&&k.set(L.subarray(0,U));let P=Ft(k,s),M=await x.fft(P),D=Math.max(0,Math.floor(i*r/e.sampleRate)),T=Math.min(M.frequencies.length-1,Math.floor(o*r/e.sampleRate)),R=ft(M.frequencies,M.magnitude,D,T);g[v]=R,h[v]=ht(M.frequencies,M.magnitude,D,T,R),b[v]=pt(M.frequencies,M.magnitude,D,T,c),A[v]=dt(M.magnitude,D,T),w[v]=gt(k),d[v]=cn(M.magnitude,S),S=new Float32Array(M.magnitude)}}finally{x.dispose()}return{times:p,spectralCentroid:g,spectralBandwidth:h,spectralRolloff:b,spectralFlatness:A,spectralFlux:d,zeroCrossingRate:w,frameInfo:{frameSize:n,hopSize:a,numFrames:f}}}function un(e,t,n){let a=[],r=0;for(let c=t;c<=n&&c<e.length;c++){let u=e[c];if(u!==void 0){let m=u*u;a.push(m),r+=m}}if(a.length===0||r<=1e-10)return{entropy:0,entropyNorm:0};let s=a.map(c=>c/r),l=0;for(let c of s)c>1e-10&&(l-=c*Math.log2(c));let i=Math.log2(a.l