(()=>{"use strict";var e={1021:function(e,t,o){var r,i=this&&this.__createBinding||(Object.create?function(e,t,o,r){void 0===r&&(r=o);var i=Object.getOwnPropertyDescriptor(t,o);i&&!("get"in i?!t.__esModule:i.writable||i.configurable)||(i={enumerable:!0,get:function(){return t[o]}}),Object.defineProperty(e,r,i)}:function(e,t,o,r){void 0===r&&(r=o),e[r]=t[o]}),n=this&&this.__setModuleDefault||(Object.create?function(e,t){Object.defineProperty(e,"default",{enumerable:!0,value:t})}:function(e,t){e.default=t}),a=this&&this.__importStar||(r=function(e){return r=Object.getOwnPropertyNames||function(e){var t=[];for(var o in e)Object.prototype.hasOwnProperty.call(e,o)&&(t[t.length]=o);return t},r(e)},function(e){if(e&&e.__esModule)return e;var t={};if(null!=e)for(var o=r(e),a=0;a<o.length;a++)"default"!==o[a]&&i(t,e,o[a]);return n(t,e),t});Object.defineProperty(t,"__esModule",{value:!0}),t.AudioEngine=void 0;const l=a(o(9896)),s=a(o(6928));t.AudioEngine=class{constructor(){this.module=null}async init(){if(this.module)return;const e=s.join(__dirname,"..","src","wasm"),t=s.join(e,"audio_engine.js");if(!l.existsSync(t))throw new Error(`WASM module not found: ${t}`);const o=require(t);this.module=await o({locateFile:t=>s.join(e,t)})}async analyze(e,t=32e3){this.module||await this.init();const o=await l.promises.readFile(e),r=new Uint8Array(o),i=s.extname(e).toLowerCase();console.log(`[AudioEngine] Analyzing: ${s.basename(e)} (${(r.length/1024/1024).toFixed(1)}MB, ext=${i})`);const n=this.module._malloc(r.length);if(!n)throw new Error(`WASM malloc failed for input buffer (${(r.length/1024/1024).toFixed(1)}MB)`);this.module.HEAPU8.set(r,n);const a=this.module._decode_audio(n,r.length);if(this.module._free(n),!a){const e=[".wav",".mp3",".flac",".ogg"];if(!e.includes(i))throw new Error(`Unsupported audio format: ${i}. WASM engine supports: ${e.join(", ")}`);throw new Error(`Failed to decode audio file (${i}, ${(r.length/1024/1024).toFixed(1)}MB). Possible memory limit exceeded.`)}try{const e=this.module._audio_get_channels(a),o=this.module._audio_get_sample_rate(a),r=this.module._audio_get_total_frames(a)+4294967296*this.module._audio_get_total_frames_high(a),i=r/o,n=this.module._generate_peaks(a,t);if(!n)throw new Error("Failed to generate peaks");const l=new Float32Array(this.module.HEAPF32.buffer,n,t).slice();this.module._free_buffer(n);const s=Math.max(512,Math.ceil(r/3500)),u=this.module._malloc(4),d=this.module._malloc(4),c=this.module._generate_spectrogram(a,2048,s,u,d),f=this.module.getValue(u,"i32"),h=this.module.getValue(d,"i32");this.module._free(u),this.module._free(d);let m=[];if(c&&f>0&&h>0){const e=Math.min(h,1024),t=Math.max(1,Math.floor(h/e));for(let o=0;o<f;o++){const r=new Array(e);if(1===t)for(let t=0;t<e;t++)r[t]=this.module.HEAPU8[c+o*h+t];else for(let i=0;i<e;i++){const e=i*t,n=Math.min(e+t,h);let a=0;for(let t=e;t<n;t++)a+=this.module.HEAPU8[c+o*h+t];r[i]=Math.round(a/(n-e))}m.push(r)}this.module._free_buffer(c)}return{peaks:[Array.from(l)],duration:i,sampleRate:o,channels:e,spectrogram:m}}finally{this.module._free_audio(a)}}}},6928:e=>{e.exports=require("path")},9896:e=>{e.exports=require("fs")}},t={},o=function o(r){var i=t[r];if(void 0!==i)return i.exports;var n=t[r]={exports:{}};return e[r].call(n.exports,n,n.exports,o),n.exports}(1021);module.exports=o})();
//# sourceMappingURL=audioEngine.js.map