UNPKG

audio

Version:

Audio loading, editing, and rendering for JavaScript

14 lines 77.8 kB
var xr=Object.defineProperty;var br=(e,t)=>()=>(e&&(t=e(e=0)),t);var Mr=(e,t)=>{for(var n in t)xr(e,n,{get:t[n],enumerable:!0})};var Et={};Mr(Et,{default:()=>vt});async function vt(e={}){let t=e.channels||1,n=e.sampleRate||44100,r=e.bitDepth||16,l={audio:{sampleRate:{ideal:n},channelCount:{ideal:t},echoCancellation:e.echoCancellation??!1,noiseSuppression:e.noiseSuppression??!1,autoGainControl:e.autoGainControl??!1}},a=await navigator.mediaDevices.getUserMedia(l),i=!e.context,o=e.context||new AudioContext({sampleRate:n}),u=o.createMediaStreamSource(a),f=!1,s=null,h;if(o.audioWorklet){let y=` class MicProcessor extends AudioWorkletProcessor { process(inputs) { const input = inputs[0] if (input && input.length > 0) { const channels = [] for (let i = 0; i < input.length; i++) channels.push(input[i].slice()) this.port.postMessage(channels) } return true } } registerProcessor('mic-processor', MicProcessor) `,w=new Blob([y],{type:"application/javascript"}),m=URL.createObjectURL(w);await o.audioWorklet.addModule(m),URL.revokeObjectURL(m),h=new AudioWorkletNode(o,"mic-processor",{numberOfInputs:1,numberOfOutputs:0,channelCount:t}),u.connect(h),h.port.onmessage=_=>{if(f||!s)return;let A=s;s=null,A(null,p(_.data,r))}}else h=o.createScriptProcessor(2048,t,1),u.connect(h),h.connect(o.destination),h.onaudioprocess=w=>{if(f||!s)return;let m=s;s=null;let _=[];for(let A=0;A<t;A++)_.push(w.inputBuffer.getChannelData(A).slice());m(null,p(_,r))};return c.close=d,c.end=d,c.backend="mediastream",c;function c(y){if(y==null||f){d();return}o.state==="suspended"&&o.resume(),s=y}function d(){f||(f=!0,s=null,u.disconnect(),a.getTracks().forEach(y=>y.stop()),i&&o.close?.())}function p(y,w){let m=y.length,_=y[0].length,A=w/8,x=new Uint8Array(_*m*A),k=new DataView(x.buffer);for(let b=0;b<_;b++)for(let M=0;M<m;M++){let R=y[M][b],E=(b*m+M)*A;w===16?k.setInt16(E,Math.max(-32768,Math.min(32767,Math.round(R*32767))),!0):w===32?k.setFloat32(E,R,!0):w===8&&(x[E]=Math.max(0,Math.min(255,Math.round((R+1)*127.5))))}return x}}var St=br(()=>{});function re(e){if(e){if(e=new Uint8Array(e.buffer||e),Rr(e))return"wav";if(Ir(e))return"aiff";if(kr(e))return"mp3";if(Cr(e))return"aac";if(Er(e))return"flac";if(Sr(e))return"m4a";if(Fr(e))return"opus";if(vr(e))return"oga";if(Pr(e))return"qoa";if(Lr(e))return"mid";if(Or(e))return"caf";if(Dr(e))return"wma";if(Tr(e))return"amr";if(Br(e))return"webm"}}function kr(e){if(!(!e||e.length<3))return e[0]===73&&e[1]===68&&e[2]===51||e[0]===255&&(e[1]&224)===224&&(e[1]&6)!==0||e[0]===84&&e[1]===65&&e[2]===71}function Rr(e){if(!(!e||e.length<12))return e[0]===82&&e[1]===73&&e[2]===70&&e[3]===70&&e[8]===87&&e[9]===65&&e[10]===86&&e[11]===69}function vr(e){if(!(!e||e.length<4))return e[0]===79&&e[1]===103&&e[2]===103&&e[3]===83}function Er(e){if(!(!e||e.length<4))return e[0]===102&&e[1]===76&&e[2]===97&&e[3]===67}function Sr(e){if(!(!e||e.length<8))return e[4]===102&&e[5]===116&&e[6]===121&&e[7]===112||e[0]===77&&e[1]===52&&e[2]===65&&e[3]===32}function Fr(e){if(!(!e||e.length<36))return e[0]===79&&e[1]===103&&e[2]===103&&e[3]===83&&e[28]===79&&e[29]===112&&e[30]===117&&e[31]===115&&e[32]===72&&e[33]===101&&e[34]===97&&e[35]===100}function Pr(e){if(!(!e||e.length<4))return e[0]===113&&e[1]===111&&e[2]===97&&e[3]===102}function Ir(e){if(!(!e||e.length<12))return e[0]===70&&e[1]===79&&e[2]===82&&e[3]===77&&e[8]===65&&e[9]===73&&e[10]===70&&(e[11]===70||e[11]===67)}function Cr(e){if(!(!e||e.length<2))return e[0]===255&&(e[1]&240)===240&&(e[1]&6)===0}function Lr(e){if(!(!e||e.length<4))return e[0]===77&&e[1]===84&&e[2]===104&&e[3]===100}function Or(e){if(!(!e||e.length<4))return e[0]===99&&e[1]===97&&e[2]===102&&e[3]===102}function Dr(e){if(!(!e||e.length<8))return e[0]===48&&e[1]===38&&e[2]===178&&e[3]===117&&e[4]===142&&e[5]===102&&e[6]===207&&e[7]===17}function Tr(e){if(!(!e||e.length<5))return e[0]===35&&e[1]===33&&e[2]===65&&e[3]===77&&e[4]===82}function Br(e){if(!(!e||e.length<4))return e[0]===26&&e[1]===69&&e[2]===223&&e[3]===163}var ae=Object.freeze({channelData:Object.freeze([]),sampleRate:0});function V(e,t){return t?ct(e,t):Ur(e)}async function Ur(e){if(!e||typeof e=="string"||!(e.buffer||e.byteLength||e.length))throw TypeError("Expected ArrayBuffer or Uint8Array");let t=new Uint8Array(e),n=re(t);if(!n)throw Error("Unknown audio format");if(!V[n])throw Error("No decoder for "+n);let r=await V[n]();try{let l=await r(t),a=await r();return pt(l,a)}catch(l){throw r.free(),l}}async function*ct(e,t){if(!V[t])throw Error("No decoder for "+t);let n=await V[t]();try{if(e.getReader){let l=e.getReader();for(;;){let{done:a,value:i}=await l.read();if(a)break;let o=await n(i instanceof Uint8Array?i:new Uint8Array(i));o.channelData.length&&(yield o)}}else for await(let l of e){let a=await n(l instanceof Uint8Array?l:new Uint8Array(l));a.channelData.length&&(yield a)}let r=await n();r.channelData.length&&(yield r)}finally{n.free()}}function j(e,t){V[e]=jr(e,async()=>{let n=await t();if(n.decoder){let a=await n.decoder();return ht(i=>a.decode(i),a.flush?()=>a.flush():null,a.free?()=>a.free():null)}let r=n.default||n,l=typeof r=="function"?await r():r;return l.ready&&await l.ready,ht(a=>l.decode(a),l.flush?()=>l.flush():null,l.free?()=>l.free():null)})}function jr(e,t){return r=>r?r[Symbol.asyncIterator]||r.getReader?ct(r,e):(console.warn("decode."+e+"(data) is deprecated, use decode(data)"),(async()=>{let l=await t();try{let a=await l(r instanceof Uint8Array?r:new Uint8Array(r.buffer||r)),i=await l();return pt(a,i)}catch(a){throw l.free(),a}})()):t()}j("mp3",()=>import("@audio/decode-mp3"));j("flac",()=>import("@audio/decode-flac"));j("opus",()=>import("@audio/decode-opus"));j("oga",()=>import("@audio/decode-vorbis"));j("m4a",()=>import("@audio/decode-aac"));j("wav",()=>import("@audio/decode-wav"));j("qoa",()=>import("@audio/decode-qoa"));j("aac",()=>import("@audio/decode-aac"));j("aiff",()=>import("@audio/decode-aiff"));j("caf",()=>import("@audio/decode-caf"));j("webm",()=>import("@audio/decode-webm"));j("amr",()=>import("@audio/decode-amr"));j("wma",()=>import("@audio/decode-wma"));function ht(e,t,n){let r=!1,l=async a=>{if(a){if(r)throw Error("Decoder already freed");try{return De(await e(a instanceof Uint8Array?a:new Uint8Array(a)))}catch(i){throw r=!0,n?.(),i}}if(r)return ae;r=!0;try{let i=t?De(await t()):ae;return n?.(),i}catch(i){throw n?.(),i}};return l.flush=async()=>r?ae:t?De(await t()):ae,l.free=()=>{r||(r=!0,n?.())},l}function De(e){if(!e?.channelData?.length)return ae;let{channelData:t,sampleRate:n,samplesDecoded:r}=e;if(r!=null&&r<t[0].length&&(t=t.map(l=>l.subarray(0,r))),!t[0]?.length)return ae;if(t.length===2){let l=t[0],a=t[1],i=!0;for(let o=0;o<l.length;o+=37)if(l[o]!==a[o]){i=!1;break}i&&(t=[l])}return{channelData:t,sampleRate:n}}function pt(e,t){if(!t?.channelData?.length)return e;if(!e?.channelData?.length)return t;let n=e.channelData.length,r=t.channelData.length,l=Math.max(n,r);return{channelData:Array.from({length:l},(a,i)=>{let o=e.channelData[i%n],u=t.channelData[i%r],f=new Float32Array(o.length+u.length);return f.set(o),f.set(u,o.length),f}),sampleRate:e.sampleRate}}var K=new Uint8Array(0),dt={},ne=dt;function oe(e,t){dt[e]=qr(async n=>{let r=(await t()).default,l=await r(n);return zr(a=>l.encode(a),()=>l.flush(),()=>l.free())})}oe("wav",()=>import("@audio/encode-wav"));oe("aiff",()=>import("@audio/encode-aiff"));oe("mp3",()=>import("@audio/encode-mp3"));oe("ogg",()=>import("@audio/encode-ogg"));oe("flac",()=>import("@audio/encode-flac"));oe("opus",()=>import("@audio/encode-opus"));function qr(e){let t=async(n,r)=>{if(!r)return e(n);if(!r.sampleRate)throw Error("sampleRate is required");let l=mt(n);if(!l.length||!l[0].length)return K;let a=await e({channels:l.length,...r});try{let i=await a(l),o=await a();return Qr(i,o)}catch(i){throw a.free(),i}};return t.stream=e,t}function mt(e){if(!e)return[];if(Array.isArray(e))return e[0]instanceof Float32Array?e:[];if(e instanceof Float32Array)return[e];if(e.getChannelData&&e.numberOfChannels){let t=[];for(let n=0;n<e.numberOfChannels;n++)t.push(e.getChannelData(n));return t}return[]}function zr(e,t,n){let r=!1,l=async a=>{if(a){if(r)throw Error("Encoder already freed");let i=mt(a);try{return Te(await e(i))}catch(o){throw r=!0,n?.(),o}}if(r)return K;r=!0;try{let i=t?Te(await t()):K;return n?.(),i}catch(i){throw n?.(),i}};return l.encode=l,l.flush=async()=>r?K:t?Te(await t()):K,l.free=()=>{r||(r=!0,n?.())},l}function Te(e){return e?.length?e instanceof Uint8Array?e:e.buffer?new Uint8Array(e.buffer,e.byteOffset,e.byteLength):new Uint8Array(e):K}function Qr(e,t){if(!t?.length)return e||K;if(!e?.length)return t||K;let n=new Uint8Array(e.length+t.length);return n.set(e),n.set(t,e.length),n}var Vr=[8e3,11025,16e3,22050,44100,48e3,88200,96e3,176400,192e3,352800,384e3],_t=typeof AudioBuffer<"u"?AudioBuffer:null;try{_t??=(await import("audio-buffer")).default}catch{}var Zr=new Set(Vr),J={float32:{C:Float32Array,min:-1,max:1},float64:{C:Float64Array,min:-1,max:1},uint8:{C:Uint8Array,min:0,max:255},uint16:{C:Uint16Array,min:0,max:65535},uint32:{C:Uint32Array,min:0,max:4294967295},int8:{C:Int8Array,min:-128,max:127},int16:{C:Int16Array,min:-32768,max:32767},int32:{C:Int32Array,min:-2147483648,max:2147483647}},Me=e=>J[e]&&e||J[e+"32"]&&e+"32",gt={array:1,arraybuffer:1,buffer:1,audiobuffer:1},fe={mono:1,stereo:2,"2.1":3,quad:4,"5.1":6};for(let e=3;e<=32;e++)fe[e+"-channel"]||=e;var Wr={};for(let e in fe)Wr[fe[e]]||=e;var At=e=>ArrayBuffer.isView(e)&&!(e instanceof DataView),Be=e=>Array.isArray(e)&&e.length>0&&At(e[0]),yt=e=>e!=null&&typeof e!="string"&&!Be(e)&&(Array.isArray(e)||At(e)||e instanceof ArrayBuffer),xt=e=>e!=null&&typeof e.getChannelData=="function"&&typeof e.numberOfChannels=="number";function Y(e){if(!e)return{};if(typeof e!="string"){let n={},r=e.dtype||e.type;return Me(r)&&(n.dtype=Me(r)),r&&gt[r]&&(n.container=r),e.channels!=null&&(n.channels=fe[e.channels]||+e.channels),e.numberOfChannels!=null&&(n.channels??=e.numberOfChannels),e.interleaved!=null&&(n.interleaved=e.interleaved),e.endianness&&(n.endianness=e.endianness),e.sampleRate!=null&&(n.sampleRate=e.sampleRate),e.rate!=null&&(n.sampleRate??=e.rate),e.container&&(n.container=e.container),n}let t={};for(let n of e.split(/\s*[,;_]\s*|\s+/)){let r=n.toLowerCase();if(Me(r))t.dtype=Me(r);else if(gt[r])t.container=r;else if(fe[r])t.channels=fe[r];else if(r==="interleaved")t.interleaved=!0;else if(r==="planar")t.interleaved=!1;else if(r==="le")t.endianness="le";else if(r==="be")t.endianness="be";else if(/^\d+$/.test(r)&&Zr.has(+r))t.sampleRate=+r;else throw Error("Unknown format token: "+n)}return t}function ke(e){return e==null?{}:xt(e)?{dtype:"float32",channels:e.numberOfChannels,interleaved:!1,sampleRate:e.sampleRate}:typeof Buffer<"u"&&Buffer.isBuffer(e)?{dtype:"uint8",container:"buffer"}:e instanceof Float32Array?{dtype:"float32"}:e instanceof Float64Array?{dtype:"float64"}:e instanceof Int8Array?{dtype:"int8"}:e instanceof Int16Array?{dtype:"int16"}:e instanceof Int32Array?{dtype:"int32"}:e instanceof Uint8Array?{dtype:"uint8"}:e instanceof Uint8ClampedArray?{dtype:"uint8"}:e instanceof Uint16Array?{dtype:"uint16"}:e instanceof Uint32Array?{dtype:"uint32"}:e instanceof ArrayBuffer?{container:"arraybuffer"}:Array.isArray(e)?Be(e)?{...ke(e[0]),channels:e.length,interleaved:!1}:{container:"array"}:{}}function wt(e){return J[e]||{min:-1,max:1}}function me(e,t,n,r){if(!e)throw Error("Source data required");if(t==null)throw Error("Format required");let l=ke(e);n===void 0&&r===void 0?yt(t)?(r=t,n=ke(r),t=l):(n=Y(t),t=l):r===void 0?yt(n)?(r=n,n=Y(t),t=l):(t={...l,...Y(t)},n=Y(n)):(t={...l,...Y(t)},n={...r?ke(r):{},...Y(n)}),n.container==="audiobuffer"&&(n.dtype="float32"),n.dtype||(n.dtype=t.dtype),n.channels==null&&t.channels!=null&&(n.channels=t.channels),n.interleaved!=null&&t.interleaved==null&&(t.interleaved=!n.interleaved,t.channels||(t.channels=2)),t.interleaved!=null&&!t.channels&&(t.channels=2);let a=t.container==="array"?{min:-1,max:1}:wt(t.dtype),i=n.container==="array"?{min:-1,max:1}:wt(n.dtype),o;if(Be(e)){let w=e.length,m=e[0].length;o=new e[0].constructor(m*w);for(let _=0;_<w;_++)o.set(e[_],m*_)}else if(xt(e)){let w=e.numberOfChannels,m=e.length;o=new Float32Array(m*w);for(let _=0;_<w;_++)o.set(e.getChannelData(_),m*_)}else e instanceof ArrayBuffer?o=new(J[t.dtype]?.C||Uint8Array)(e):typeof Buffer<"u"&&Buffer.isBuffer(e)?o=new(J[t.dtype]?.C||Uint8Array)(e.buffer.slice(e.byteOffset,e.byteOffset+e.byteLength)):o=e;let u=o.length,f=a.min!==i.min||a.max!==i.max,s=t.interleaved!=null&&n.interleaved!=null&&t.interleaved!==n.interleaved,h=t.channels||1,c=Math.floor(u/h),d=J[n.dtype]?.C||Float32Array,p;if(!f&&!s)p=n.container==="array"?Array.from(o):new d(o);else{p=n.container==="array"?new Array(u):new d(u);let w=a.max-a.min,m=i.max>1,_=a.min===-1&&a.max===1&&m?i.max-i.min+1:i.max-i.min,A=a.min===-1&&a.max===1&&m;if(s){let x=t.interleaved;for(let k=0;k<u;k++){let b=x?k%c*h+~~(k/c):k%h*c+~~(k/h),M=o[b];f&&(M=(M-a.min)/w*_+i.min,A&&(M=Math.round(M)),M<i.min?M=i.min:M>i.max&&(M=i.max)),p[k]=M}}else for(let x=0;x<u;x++){let k=(o[x]-a.min)/w*_+i.min;A&&(k=Math.round(k)),p[x]=k<i.min?i.min:k>i.max?i.max:k}}if(r)if(Array.isArray(r)){for(let w=0;w<u;w++)r[w]=p[w];p=r}else if(r instanceof ArrayBuffer){let w=new(J[n.dtype]?.C||Uint8Array)(r);w.set(p),p=w}else r.set(p),p=r;let y=J[n.dtype];if(y&&y.C.BYTES_PER_ELEMENT>1&&t.endianness&&n.endianness&&t.endianness!==n.endianness&&p.buffer){let w=n.endianness==="le",m=new DataView(p.buffer),_=y.C.BYTES_PER_ELEMENT,A="set"+n.dtype[0].toUpperCase()+n.dtype.slice(1);for(let x=0;x<u;x++)m[A](x*_,p[x],w)}if(n.container==="audiobuffer"){let w=typeof AudioBuffer<"u"?AudioBuffer:_t;if(!w)throw Error("AudioBuffer not available. In Node.js: install audio-buffer package or set globalThis.AudioBuffer");let m=n.channels||1,_=Math.floor(p.length/m),A=n.sampleRate||t.sampleRate||44100,x=new w({length:_,numberOfChannels:m,sampleRate:A});if((s?n.interleaved:t.interleaved??!1)&&m>1)for(let b=0;b<m;b++){let M=new Float32Array(_);for(let R=0;R<_;R++)M[R]=p[R*m+b];x.copyToChannel(M,b)}else for(let b=0;b<m;b++)x.copyToChannel(p.subarray(b*_,(b+1)*_),b);return x}return(n.container==="arraybuffer"||n.container==="buffer")&&p.buffer||p}var F=Object.create(null),bt=6e4,Mt=bt*60,Ue=Mt*24,kt=Ue*365.25;F.year=F.yr=F.y=kt;F.month=F.mo=F.mth=kt/12;F.week=F.wk=F.w=Ue*7;F.day=F.d=Ue;F.hour=F.hr=F.h=Mt;F.minute=F.min=F.m=bt;F.second=F.sec=F.s=1e3;F.millisecond=F.millisec=F.ms=1;F.microsecond=F.microsec=F.us=F.\u00B5s=.001;F.nanosecond=F.nanosec=F.ns=1e-6;F.group=",";F.decimal=".";F.placeholder=" _";var Rt=F;var Gr=/((?:\d{1,16}(?:\.\d{1,16})?|\.\d{1,16})(?:[eE][-+]?\d{1,4})?)\s?([\p{L}]{0,14})/gu;z.unit=Rt;function z(e="",t="ms"){let n=null,r;return String(e).replace(new RegExp(`(\\d)[${z.unit.placeholder}${z.unit.group}](\\d)`,"g"),"$1$2").replace(z.unit.decimal,".").replace(Gr,(l,a,i)=>{if(i)i=i.toLowerCase();else if(r){for(let o in z.unit)if(z.unit[o]<r){i=o;break}}else i=t;r=i=z.unit[i]||z.unit[i.replace(/s$/,"")],i&&(n=(n||0)+a*i)}),n&&n/(z.unit[t]||1)*(e[0]==="-"?-1:1)}g.version="2.2.0";function D(e){if(e==null)return e;if(typeof e=="number"){if(!Number.isFinite(e))throw new Error(`Invalid time: ${e}`);return e}let t=e.match(/^(\d+):(\d{1,2})(?::(\d{1,2}))?(?:\.(\d+))?$/);if(t){let[,r,l,a,i]=t,o=a!=null?+r*3600+ +l*60+ +a:+r*60+ +l;return i&&(o+=+("0."+i)),o}let n=z(e,"s");if(n!=null&&isFinite(n))return n;throw new Error(`Invalid time: ${e}`)}function g(e,t={}){if(e==null){let f=t.sampleRate||44100,s=t.channels||1,h=[],c=()=>{for(let p of h.splice(0))p()},d=Re([],f,s,0,t,null);return d.decoded=!1,d.recording=!1,d._.acc=Pt({pages:d.pages,notify:c,ondata:(...p)=>O(d,"data",...p)}),d._.waiters=h,d}if(e&&typeof e=="object"&&!Array.isArray(e)&&e.edits){if(!e.source)throw new TypeError("audio: cannot restore document without source reference");let f=g(e.source,t);if(f.run)for(let s of e.edits)f.run(s);return f}if(Array.isArray(e)&&e.length&&!(e[0]instanceof Float32Array)){let f=e.map(c=>c?.pages?c:g(c,t)),s=f[0].clip?f[0].clip():g.from(f[0]);if(!s.insert)throw new Error('audio([...]): concat requires insert plugin \u2014 import "audio" instead of "audio/core.js"');for(let c=1;c<f.length;c++)s.insert(f[c]);let h=f.filter(c=>!c.decoded);return h.length&&(s.ready=Promise.all(h.map(c=>c.ready)).then(()=>(delete s.then,delete s.catch,!0)),s.ready.catch(()=>{}),je(s)),s}if(e?.getChannelData&&e?.numberOfChannels)return g.from(e,t);if(Array.isArray(e)&&e[0]instanceof Float32Array||typeof e=="number"){let f=g.from(e,t);return g.evict&&f.cache&&f.budget!==1/0&&(f.ready=g.evict(f).then(()=>(delete f.then,delete f.catch,!0)),f.ready.catch(()=>{}),je(f)),f}let n=typeof e=="string"?e:e instanceof URL?e.href:null,r=[],l=[],a=()=>{for(let f of l.splice(0))f()},i=Re(r,0,0,0,{...t,source:n},null);i._.waiters=l,i.decoded=!1;let o,u;return i._.ready=new Promise((f,s)=>{o=f,u=s}),i._.ready.catch(()=>{}),i.ready=(async()=>{try{if(t.storage==="persistent"){if(!g.opfsCache)throw new Error('Persistent storage requires cache module (import "./cache.js")');try{t={...t,cache:await g.opfsCache(),budget:t.budget??g.DEFAULT_BUDGET??1/0}}catch{throw new Error('OPFS not available (required by storage: "persistent")')}i.cache=t.cache,i.budget=t.budget}let f=await Jr(e,{pages:r,notify:a,ondata:(...h)=>O(i,"data",...h)});i.sampleRate=f.sampleRate,i._.ch=f.channels,i._.chV=-1,f.acc&&(i._.acc=f.acc),f.estDuration&&(i._.estDur=f.estDuration),O(i,"metadata",{sampleRate:f.sampleRate,channels:f.channels,estDuration:f.estDuration}),o();let s=await f.decoding;return i._.len=s.length,i._.lenV=-1,i.stats=s.stats,i.decoded=!0,a(),g.evict?.(i),delete i.then,delete i.catch,!0}catch(f){throw u(f),f}})(),i.ready.catch(()=>{}),je(i),i}function je(e){e.then=function(t,n){return e.ready.then(()=>(delete e.then,delete e.catch,e)).then(t,n)},e.catch=function(t){return e.then(null,t)}}g.from=function(e,t={}){if(Array.isArray(e)&&e[0]instanceof Float32Array)return ge(e,t);if(typeof e=="number")return Hr(e,t);if(typeof e=="function")return $r(e,t);if(e?.pages)return Re(e.pages,t.sampleRate??e.sampleRate,t.channels??e._.ch,e._.len,{source:e.source,storage:e.storage,cache:e.cache,budget:t.budget??e.budget},e.stats);if(e?.getChannelData){let n=Array.from({length:e.numberOfChannels},(r,l)=>new Float32Array(e.getChannelData(l)));return ge(n,{sampleRate:e.sampleRate,...t})}if(ArrayBuffer.isView(e)&&t.format){let n=Y(t.format),r=n.channels||t.channels||1,l=n.sampleRate||t.sampleRate||44100,a={...n,channels:r};r>1&&a.interleaved==null&&(a.interleaved=!0);let i=me(e,a,{dtype:"float32",interleaved:!1,channels:r}),o=i.length/r,u=Array.from({length:r},(f,s)=>i.subarray(s*o,(s+1)*o));return ge(u,{sampleRate:l})}throw new TypeError("audio.from: expected Float32Array[], AudioBuffer, audio instance, function, or number")};var B={};g.fn=B;g.BLOCK_SIZE=1024;g.PAGE_SIZE=1024*g.BLOCK_SIZE;var H=Symbol("load"),ve=Symbol("read");function O(e,t,...n){let r=e._.ev[t];if(r)for(let l of r)l(...n)}B.on=function(e,t){return(this._.ev[e]??=[]).push(t),this};B.off=function(e,t){if(!e)return this._.ev={},this;if(!t)return delete this._.ev[e],this;let n=this._.ev[e];if(n){let r=n.indexOf(t);r>=0&&n.splice(r,1)}return this};B.dispose=function(){this.stop(),this._.ev={},this._.pcm=null,this._.plan=null,this.pages.length=0,this.stats=null,this._.waiters=null,this._.acc=null};Symbol.dispose&&(B[Symbol.dispose]=B.dispose);g.use=function(...e){for(let t of e)t(g)};function Re(e,t,n,r,l={},a){let i=Object.create(B);return i.pages=e,i.sampleRate=t,i.source=l.source??null,i.storage=l.storage||"memory",i.cache=l.cache||null,i.budget=l.budget??1/0,i.stats=a,i.decoded=!0,i.ready=Promise.resolve(!0),i._={ch:n,len:r,waiters:null,ev:{},ct:0,ctStamp:0,vol:1,muted:!1,rate:1},i.edits=[],i.version=0,i._.pcm=null,i._.pcmV=-1,i._.plan=null,i._.planV=-1,i._.statsV=-1,i._.lenC=i._.len,i._.lenV=0,i._.chC=i._.ch,i._.chV=0,Object.defineProperties(i,{currentTime:{get(){if(this.playing&&!this.paused){let o=this._.ct+(performance.now()-this._.ctStamp)/1e3*(this._.rate||1),u=this.duration;return u>0?Math.min(o,u):o}return this._.ct},set(o){this._.ct=o,this._.ctStamp=performance.now()},enumerable:!0,configurable:!0},volume:{get(){return this._.vol},set(o){o=Math.max(0,Math.min(1,+o||0)),this._.vol!==o&&(this._.vol=o,O(this,"volumechange"))},enumerable:!0,configurable:!0},muted:{get(){return this._.muted},set(o){o=!!o,this._.muted!==o&&(this._.muted=o,O(this,"volumechange"))},enumerable:!0,configurable:!0},playbackRate:{get(){return this._.rate},set(o){o=Math.max(.0625,Math.min(16,+o||1)),this._.rate!==o&&(this._.rate=o,O(this,"ratechange"))},enumerable:!0,configurable:!0}}),i.playing=!1,i.paused=!1,i.ended=!1,i.seeking=!1,i.loop=!1,i.block=null,i._.lru=new Set,i}function ge(e,t={}){let n=t.sampleRate||44100;return Re(Ft(e),n,e.length,e[0].length,t,g.statSession?.(n).page(e).done())}function Hr(e,t={}){let n=t.sampleRate||44100,r=t.channels||1;return ge(Array.from({length:r},()=>new Float32Array(Math.round(e*n))),{...t,sampleRate:n})}function $r(e,t={}){let n=t.sampleRate||44100,r=t.channels||1,l=t.duration;if(l==null)throw new TypeError("audio.from(fn): duration required");let a=Math.round(l*n),i=Array.from({length:r},()=>new Float32Array(a));for(let o=0;o<a;o++){let u=e(o/n,o);if(typeof u=="number")for(let f=0;f<r;f++)i[f][o]=u;else for(let f=0;f<r;f++)i[f][o]=u[f]??0}return ge(i,{sampleRate:n})}Object.defineProperties(B,{length:{get(){return this._.len},configurable:!0},duration:{get(){return this.length/this.sampleRate},configurable:!0},channels:{get(){return this._.ch},configurable:!0}});B[H]=async function(){this._.ready&&await this._.ready,this._.acc?.drain()};B[ve]=function(e,t){return Ee(this,e,t)};B.push=function(e,t){let n=this._.acc;if(!n)throw new Error("push: instance is not pushable \u2014 create with audio()");let r=this._.ch,l=this.sampleRate,a;if(Array.isArray(e)&&e[0]instanceof Float32Array)a=e;else if(e instanceof Float32Array)a=[e];else if(ArrayBuffer.isView(e)){let i=t||{},o=typeof i=="string"?i:i.format||"int16",u=i.channels||r,f={dtype:o,channels:u};u>1&&(f.interleaved=!0);let s=me(e,f,{dtype:"float32",interleaved:!1,channels:u}),h=s.length/u;a=Array.from({length:u},(c,d)=>s.subarray(d*h,(d+1)*h))}else throw new TypeError("push: expected Float32Array[], Float32Array, or typed array");if(!this._.ch)this._.ch=a.length,this._.chV=-1;else if(a.length!==this._.ch)throw new TypeError(`push: expected ${this._.ch} channels, got ${a.length}`);return n.push(a,t&&t.sampleRate||l),this._.len=n.length,this._.lenV=-1,this};B.stop=function(){if(this.playing=!1,this.paused=!1,this.seeking=!1,this._._wake&&this._._wake(),this.recording&&(this.recording=!1,this._._mic&&(this._._mic(null),this._._mic=null)),this._.acc&&!this.decoded&&(this._.acc.drain(),this.decoded=!0,this._.waiters))for(let e of this._.waiters.splice(0))e();return this};B.record=function(e={}){if(!this._.acc)throw new Error("record: instance is not pushable \u2014 create with audio()");if(this.recording)return this;this.recording=!0,this.decoded=!1;let t=this,n=this.sampleRate,r=this._.ch;return(async()=>{try{let{default:a}=await Promise.resolve().then(()=>(St(),Et)),i=a({sampleRate:n,channels:r,bitDepth:16,...e});t._._mic=i,i((o,u)=>{t.recording&&(o||!u||t.push(new Int16Array(u.buffer,u.byteOffset,u.byteLength/2),"int16"))})}catch(a){if(t.recording=!1,t.decoded=!0,t._.waiters)for(let i of t._.waiters.splice(0))i();throw a.code==="ERR_MODULE_NOT_FOUND"?new Error("record: audio-mic not installed \u2014 npm i audio-mic"):a}})().catch(()=>{}),this};B.seek=function(e){if(e=Math.max(0,e),this.seeking=!0,this.currentTime=e,this.cache){let t=Math.floor(e*this.sampleRate/g.PAGE_SIZE);(async()=>{for(let n=Math.max(0,t-1);n<=Math.min(t+2,this.pages.length-1);n++)this.pages[n]===null&&await this.cache.has(n)&&(this.pages[n]=await this.cache.read(n))})()}return this.playing?(this._._seekTo=e,this._._wake&&this._._wake()):this.seeking=!1,this};B.read=async function(e){(typeof e!="object"||e===null)&&(e={});let{at:t,duration:n,format:r,channel:l,meta:a}=e;t=D(t),n=D(n),await this[H]();let i=await this[ve](t,n);if(l!=null&&(i=[i[l]]),!r)return l!=null?i[0]:i;let o=ne[r]?await ne[r](i,{sampleRate:this.sampleRate,...a}):i.map(u=>me(u,"float32",r));return l!=null&&Array.isArray(o)?o[0]:o};function Ft(e){let t=e[0].length,n=[];for(let r=0;r<t;r+=g.PAGE_SIZE)n.push(e.map(l=>l.subarray(r,Math.min(r+g.PAGE_SIZE,t))));return n}function qe(e,t,n,r,l){let a=Math.floor(n/g.PAGE_SIZE),i=a*g.PAGE_SIZE;for(let o=a;o<e.pages.length&&i<n+r;o++){let u=e.pages[o],f=u?u[0].length:g.PAGE_SIZE;if(i+f>n&&u){let s=Math.max(n-i,0),h=Math.min(n+r-i,f);e._.lru&&(e._.lru.delete(o),e._.lru.add(o)),l(u,t,s,h,Math.max(i-n,0))}i+=f}}function ye(e,t,n,r,l,a){qe(e,t,n,r,(i,o,u,f,s)=>l.set(i[o].subarray(u,f),a+s))}function Ee(e,t,n){let r=e.sampleRate,l=e._.ch,a=t!=null?Math.min(Math.max(Math.round(t*r),0),e._.len):0,i=n!=null?Math.round(n*r):e._.len-a;i=Math.min(Math.max(i,0),e._.len-a);let o=Array.from({length:l},()=>new Float32Array(i));for(let u=0;u<l;u++)ye(e,u,a,i,o[u],0);return o}async function ze(e){if(e instanceof ArrayBuffer)return e;if(e instanceof Uint8Array)return e.buffer.slice(e.byteOffset,e.byteOffset+e.byteLength);if(e instanceof URL)return ze(e.href);if(typeof e=="string"){if(/^(https?|data|blob):/.test(e)||typeof window<"u")return(await fetch(e)).arrayBuffer();if(e.startsWith("file:")){let{fileURLToPath:r}=await import("url");e=r(e)}let{readFile:t}=await import("fs/promises"),n=await t(e);return n.buffer.slice(n.byteOffset,n.byteOffset+n.byteLength)}throw new TypeError("audio: unsupported source type")}async function Kr(e){if(e instanceof ArrayBuffer||e instanceof Uint8Array){let r=e instanceof ArrayBuffer?new Uint8Array(e):e.byteOffset||e.byteLength!==e.buffer.byteLength?new Uint8Array(e.buffer.slice(e.byteOffset,e.byteOffset+e.byteLength)):new Uint8Array(e.buffer);return{format:re(r),bytes:r}}if(typeof e=="string"&&!/^(https?|data|blob):/.test(e)&&typeof window>"u"){let r=e;if(e.startsWith("file:")){let{fileURLToPath:h}=await import("url");r=h(e)}let{open:l,stat:a}=await import("fs/promises"),i=await l(r,"r"),o=new Uint8Array(12);await i.read(o,0,12,0),await i.close();let u=re(new Uint8Array(o)),f=(await a(r)).size,{createReadStream:s}=await import("fs");return{format:u,reader:s(r),fileSize:f}}let t=await ze(e),n=new Uint8Array(t);return{format:re(n),bytes:n}}function Pt(e={}){let{pages:t=[],notify:n,ondata:r}=e,l=0,a=0,i=0,o=0,u=null,f;function s(h){t.push(h),i+=h[0].length,n?.()}return{pages:t,get sampleRate(){return l},get channels(){return a},get length(){return i+o},get partial(){return o>0?u.map(h=>h.subarray(0,o)):null},get partialLen(){return o},push(h,c){u||(l=c,a=h.length,u=Array.from({length:a},()=>new Float32Array(g.PAGE_SIZE)),f=g.statSession?.(l)),f?.page(h);let d=0,p=h[0].length;for(;d<p;){let y=Math.min(p-d,g.PAGE_SIZE-o);for(let w=0;w<a;w++)u[w].set(h[w].subarray(d,d+y),o);d+=y,o+=y,o===g.PAGE_SIZE&&(s(u),u=Array.from({length:a},()=>new Float32Array(g.PAGE_SIZE)),o=0)}if(r){let y=f?.delta();y&&r({delta:y,offset:(i+o)/l,sampleRate:l,channels:a,pages:t})}n?.()},drain(){o>0&&(s(u.map(h=>h.slice(0,o))),u=Array.from({length:a},()=>new Float32Array(g.PAGE_SIZE)),o=0)},done(){if(o>0&&s(u.map(h=>h.slice(0,o))),f?.flush(),r&&f){let h=f.delta();h&&r({delta:h,offset:i/l,sampleRate:l,channels:a,pages:t})}return{stats:f?.done(),length:i}}}}function Yr(e,t,n,r){return!e||!n||!r?null:t==="wav"?Math.max(0,(e-44)/(n*r*2)):t==="flac"?e/(n*r*.7):t==="mp3"?e/(128e3/8):t==="ogg"||t==="opus"?e/(96e3/8):null}async function Jr(e,t={}){let{format:n,bytes:r,reader:l,fileSize:a}=await Kr(e);if(!n||!V[n]){r||(r=new Uint8Array(await ze(e)));let{channelData:p,sampleRate:y}=await V(r.buffer||r),w=t.pages||[],m=Ft(p);for(let A of m)w.push(A),t.notify?.();let _=g.statSession?.(y)?.page(p)?.done()??null;return{pages:w,sampleRate:y,channels:p.length,decoding:Promise.resolve({stats:_,length:p[0].length})}}let i=await V[n](),o=()=>new Promise(p=>setTimeout(p,0)),u,f=t.notify,s=new Promise(p=>{u=p}),h=Pt({pages:t.pages,ondata:t.ondata,notify:()=>{if(f?.(),u){let p=u;u=null,p()}}}),c=(async()=>{try{if(l)for await(let w of l){let m=w instanceof Uint8Array?w:new Uint8Array(w),_=await i(m);_.channelData.length&&h.push(_.channelData,_.sampleRate),await o()}else{let w=65536;for(let m=0;m<r.length;m+=w){let _=await i(r.subarray(m,Math.min(m+w,r.length)));_.channelData.length&&h.push(_.channelData,_.sampleRate),await o()}}let p=await i();return p.channelData.length&&h.push(p.channelData,p.sampleRate),h.done()}catch(p){if(u){let y=u;u=null,y()}throw p}})();if(await s,!h.sampleRate)throw new Error("audio: decoded no audio data");let d=Yr(a||r?.length,n,h.sampleRate,h.channels);return{pages:h.pages,sampleRate:h.sampleRate,channels:h.channels,decoding:c,acc:h,estDuration:d}}var W=g.fn,Z={};function I(e,t,n,r,l){let a=[e,t,n];return r!=null&&r!==1&&(a[3]=r),l!==void 0&&(a[4]=l),a}function G(e,t,n=0){let r=e??n;return r<0&&(r=t+r),Math.min(Math.max(0,r),t)}function X(e,t){let n=e.sampleRate,r=e.at!=null?Math.round(e.at*n):0;return[r,e.duration!=null?r+Math.round(e.duration*n):t]}function Xr(e){return e!=null&&typeof e=="object"&&!Array.isArray(e)&&!ArrayBuffer.isView(e)&&!e.pages&&!e.getChannelData}g.op=function(e,t,n,r){if(!arguments.length)return Z;if(arguments.length===1)return Z[e];let l;if(typeof t!="function")l=t;else{let a,i;typeof n=="function"?(a=n,i=r):i=n,l={process:t},a&&(l.plan=a),i&&Object.assign(l,i)}if(!W[e]&&!l.hidden){let a=function(...i){let o={type:e,args:i},u=i[i.length-1];if(i.length&&Xr(u)){let{at:f,duration:s,channel:h,offset:c,length:d,...p}=u;o.args=i.slice(0,-1),f!=null&&(o.at=D(f)),s!=null&&(o.duration=D(s)),c!=null&&(o.offset=c),d!=null&&(o.length=d),h!=null&&(o.channel=h),Object.assign(o,p)}return this.run(o)};W[e]=l.call?function(...i){return l.call.call(this,a,...i)}:a}Z[e]=l};function Ot(e,t){e.edits.push(t),e.version++,O(e,"change")}function Nr(e){let t=e.edits.pop();return t&&(e.version++,O(e,"change")),t}Object.defineProperties(W,{length:{get(){if(this._.lenV===this.version)return this._.lenC;let e=this.edits.length?N(this).totalLen:this._.len;return this._.lenC=e,this._.lenV=this.version,e},configurable:!0},channels:{get(){if(this._.chV===this.version)return this._.chC;let e=this._.ch;for(let t of this.edits)Z[t.type]?.ch&&(e=Z[t.type].ch(e,t.args));return this._.chC=e,this._.chV=this.version,e},configurable:!0}});async function se(e,t,n,r){if(!g.ensurePages)return;let{segs:l,sr:a}=t,i=Math.round((n||0)*a),o=r!=null?i+Math.round(r*a):t.totalLen;for(let u of l){let f=Math.max(i,u[2]),s=Math.min(o,u[2]+u[1]);if(f>=s)continue;let h=Math.abs(u[3]||1),c=u[0]+(f-u[2])*h,d=(s-f)*h+1,p=u[4]===null?null:u[4]||e;p&&await g.ensurePages(p,c/a,d/a)}}async function Dt(e){for(let{args:t}of e.edits)t?.[0]?.pages&&await t[0][H]()}W[ve]=async function(e,t){if(!this.edits.length)return g.ensurePages&&await g.ensurePages(this,e,t),Ee(this,e,t);await this[H](),await Dt(this);let n=N(this);return await se(this,n,e,t),Ve(this,n,e,t)};W[Symbol.asyncIterator]=W.stream=async function*(e){let t=D(e?.at),n=D(e?.duration);if(this._.waiters&&!this.decoded&&!this.edits.length){let a=this.sampleRate,i=this._.acc,o=g.PAGE_SIZE,u=t?Math.round(t*a):0,f=n!=null?u+Math.round(n*a):1/0,s=u;for(;s<f;){let h=this.decoded?this._.len:i?this.pages.length*o+i.partialLen:this._.len;for(;s>=h&&!this.decoded;)await new Promise(y=>this._.waiters.push(y)),h=this.decoded?this._.len:i?this.pages.length*o+i.partialLen:this._.len;if(s>=h)break;let c=Math.min(f,h),d=Math.floor(s/o),p=s%o;if(d<this.pages.length){let y=this.pages[d],w=Math.min(y[0].length-p,c-s);yield y.map(m=>m.subarray(p,p+w)),s+=w}else if(i){let y=Math.min(i.partialLen-p,c-s);y>0&&(yield i.partial.map(w=>w.subarray(p,p+y).slice()),s+=y)}}return}if(this._.waiters&&!this.decoded&&this.edits.length){let a=!0;for(let i of this.edits){let o=Z[i.type];if(!o||o.plan||o.resolve){a=!1;break}}if(a){this._.ready&&await this._.ready;let i=this.sampleRate,o=this._.acc,u=g.PAGE_SIZE,f=g.BLOCK_SIZE,s=t?Math.round(t*i):0,h=n!=null?s+Math.round(n*i):1/0,c=s,d=this.edits.map(y=>{let{type:w,args:m,at:_,duration:A,channel:x,...k}=y;return{op:Z[w].process,origAt:_,at:_!=null&&_<0?1/0:_,channel:x,ctx:{...k,args:m||[],duration:A,sampleRate:i,totalDuration:1/0,render:Qe}}}),p=!1;for(;c<h;){let y=this.decoded?this._.len:o?this.pages.length*u+o.partialLen:this._.len;if(this.decoded&&!p){p=!0;let b=this._.len/i;for(let M of d)M.ctx.totalDuration=b,M.origAt!=null&&M.origAt<0&&(M.at=b+M.origAt);h===1/0&&(h=this._.len)}for(;c>=y&&!this.decoded;)await new Promise(b=>this._.waiters.push(b)),y=this.decoded?this._.len:o?this.pages.length*u+o.partialLen:this._.len;if(c>=y)break;let w=Math.min(h,y),m=Math.floor(c/u),_=c%u,A,x;if(m<this.pages.length){let b=this.pages[m],M=Math.min(b[0].length-_,w-c,f);A=b.map(R=>R.subarray(_,_+M).slice()),x=M}else if(o){let b=Math.min(o.partialLen-_,w-c,f);if(b>0)A=o.partial.map(M=>M.subarray(_,_+b).slice()),x=b;else break}else break;let k=c/i;for(let b of d){let{op:M,at:R,channel:E,ctx:S}=b;if(M)if(S.at=R!=null?R-k:void 0,S.blockOffset=k,E!=null){let v=typeof E=="number"?[E]:E,P=v.map(L=>A[L]),C=M(P,S);if(C&&C!==!1)for(let L=0;L<v.length;L++)A[v[L]]=C[L]}else{let v=M(A,S);if(v===!1||v===null)continue;v&&(A=v)}}yield A,c+=x}return}}await this.ready,await this[H](),await Dt(this);let r=N(this),l=new Set;for(let a of r.segs)a[4]&&a[4]!==null&&!l.has(a[4])&&(l.add(a[4]),await a[4][H]());await se(this,r,t,n);for(let a of ue(this,r,t,n))yield a};W.undo=function(e=1){if(!this.edits.length)return e===1?null:[];let t=[];for(let n=0;n<e&&this.edits.length;n++)t.push(Nr(this));return e===1?t[0]:t};W.run=function(...e){let t=this.sampleRate;for(let n of e){if(!n.type)throw new TypeError("audio.run: edit must have type");let r={...n,args:n.args||[]};r.at!=null&&(r.at=D(r.at)),r.duration!=null&&(r.duration=D(r.duration)),r.offset!=null&&(r.at=r.offset/t,delete r.offset),r.length!=null&&(r.duration=r.length/t,delete r.length),Ot(this,r)}return this};W.toJSON=function(){let e=this.edits.filter(t=>!t.args?.some(n=>typeof n=="function"));return{source:this.source,edits:e,sampleRate:this.sampleRate,channels:this.channels,duration:this.duration}};W.clone=function(){let e=g.from(this);for(let t of this.edits)Ot(e,{...t});return e};var en=2**29;function Qe(e,t,n){if(Array.isArray(e)&&e[0]instanceof Float32Array)return t!=null?e.map(i=>i.subarray(t,t+n)):e;if(e?.getChannelData&&!e.pages){let i=Array.from({length:e.numberOfChannels},(o,u)=>new Float32Array(e.getChannelData(u)));return t!=null?i.map(o=>o.subarray(t,t+n)):i}if(t!=null)return Bt(e,t,n);if(e._.pcm&&e._.pcmV===e.version)return e._.pcm;if(!e.edits.length){let i=Ee(e);return e._.pcm=i,e._.pcmV=e.version,i}let r=N(e),l=we(r.segs);if(l>en)throw new Error(`Audio too large for flat render (${(l/1e6).toFixed(0)}M samples). Use streaming.`);let a=Ve(e,r);return e._.pcm=a,e._.pcmV=e.version,a}function we(e){let t=0;for(let n of e)t=Math.max(t,n[2]+n[1]);return t}function N(e){if(e._.plan&&e._.planV===e.version)return e._.plan;let t=e.sampleRate,n=e._.ch,r=[[0,e._.len,0]],l=[];for(let i of e.edits){let{type:o,args:u=[],at:f,duration:s,channel:h,...c}=i,d=Z[o];if(!d)throw new Error(`Unknown op: ${o}`);if(d.resolve){let p={...c,stats:e._.srcStats||e.stats,sampleRate:t,channelCount:n,channel:h,at:f,duration:s,totalDuration:we(r)/t},y=d.resolve(u,p);if(y===!1)continue;if(y){let w=Array.isArray(y)?y:[y];for(let m of w){h!=null&&m.channel==null&&(m.channel=h),f!=null&&m.at==null&&(m.at=f),s!=null&&m.duration==null&&(m.duration=s);let _=Z[m.type];if(_?.plan&&typeof _.plan=="function"){let A=we(r),x=m.at!=null?Math.round(m.at*t):null,k=m.duration!=null?Math.round(m.duration*t):null;r=_.plan(r,{total:A,sampleRate:t,args:m.args||[],offset:x,length:k})}else l.push(m)}continue}}if(d.plan){let p=we(r),y=f!=null?Math.round(f*t):null,w=s!=null?Math.round(s*t):null;r=d.plan(r,{total:p,sampleRate:t,args:u,offset:y,length:w})}else l.push(i)}let a={segs:r,pipeline:l,totalLen:we(r),sr:t};return e._.plan=a,e._.planV=e.version,a}var It=null,Ct=0;function Lt(e,t,n,r,l,a,i){let o=i||1,u=Math.abs(o);if(u===1)return o>0?ye(e,t,n,r,l,a):qe(e,t,n,r,(h,c,d,p,y)=>{for(let w=d;w<p;w++)l[a+(r-1-(y+w-d))]=h[c][w]});let f=Math.ceil(r*u)+1;f>Ct&&(Ct=f,It=new Float32Array(f));let s=It.subarray(0,f);s.fill(0),ye(e,t,n,f,s,0),Tt(s,l,a,r,o)}function Tt(e,t,n,r,l){let a=Math.abs(l),i=l<0;for(let o=0;o<r;o++){let u=(i?r-1-o:o)*a,f=u|0,s=u-f;t[n+o]=f+1<e.length?e[f]+(e[f+1]-e[f])*s:e[f]||0}}function Bt(e,t,n){if(!e.edits.length)return Array.from({length:e._.ch},(a,i)=>{let o=new Float32Array(n);return ye(e,i,t,n,o,0),o});let r=N(e),l=r.sr;return Ve(e,r,t/l,n/l)}function*ue(e,t,n,r){let{segs:l,pipeline:a,totalLen:i,sr:o}=t,u=Math.round((n||0)*o),f=r!=null?u+Math.round(r*o):i,s=i/o,h=a.map(p=>{let y=Z[p.type],{type:w,args:m,at:_,duration:A,channel:x,...k}=p;return{op:y.process,at:_!=null&&_<0?s+_:_,dur:A,channel:x,ctx:{...k,args:m||[],duration:A,sampleRate:o,totalDuration:s,render:Qe}}}),d=u>0&&h.length?Math.max(0,u-g.BLOCK_SIZE*8):u;for(let p=d;p<f;p+=g.BLOCK_SIZE){let y=p<u?u:f,w=Math.min(g.BLOCK_SIZE,y-p),m=Array.from({length:e._.ch},()=>new Float32Array(w));for(let A of l){let x=Math.max(p,A[2]),k=Math.min(p+w,A[2]+A[1]);if(x>=k)continue;let b=A[3]||1,M=A[4],R=Math.abs(b),E=k-x,S=x-p,v=b<0?A[0]+(A[1]-(x-A[2])-E)*R:A[0]+(x-A[2])*R;if(M!==null)if(M)if(M.edits.length===0)for(let P=0;P<e._.ch;P++)Lt(M,P%M._.ch,v,E,m[P],S,b);else{let P=Math.ceil(E*R)+1,C=Bt(M,v,P);for(let L=0;L<e._.ch;L++){let q=C[L%C.length];if(R===1)if(b<0)for(let U=0;U<E;U++)m[L][S+U]=q[E-1-U];else m[L].set(q.subarray(0,E),S);else Tt(q,m[L],S,E,b)}}else for(let P=0;P<e._.ch;P++)Lt(e,P,v,E,m[P],S,b)}let _=p/o;for(let A of h){let{op:x,at:k,channel:b,ctx:M}=A;if(x)if(M.at=k!=null?k-_:void 0,M.blockOffset=_,b!=null){let R=typeof b=="number"?[b]:b,E=R.map(v=>m[v]),S=x(E,M);if(S&&S!==!1)for(let v=0;v<R.length;v++)m[R[v]]=S[v]}else{let R=x(m,M);if(R===!1||R===null)continue;R&&(m=R)}}p>=u&&(yield m)}}function Ve(e,t,n,r){let l=[];for(let o of ue(e,t,n,r))l.push(o);if(!l.length)return Array.from({length:e.channels},()=>new Float32Array(0));let a=l[0].length,i=l.reduce((o,u)=>o+u[0].length,0);return Array.from({length:a},(o,u)=>{let f=new Float32Array(i),s=0;for(let h of l)f.set(h[u],s),s+=h[0].length;return f})}var tn=500*1024*1024;async function rn(e){if(!e.cache||e.budget===1/0)return;let t=l=>l?l.reduce((a,i)=>a+i.byteLength,0):0,n=e.pages.reduce((l,a)=>l+t(a),0);if(n<=e.budget)return;let r=e._.lru&&e._.lru.size?[...e._.lru]:e.pages.map((l,a)=>a);for(let l of r){if(n<=e.budget)break;e.pages[l]&&(await e.cache.write(l,e.pages[l]),n-=t(e.pages[l]),e.pages[l]=null,e._.lru&&e._.lru.delete(l))}}async function nn(e,t,n){if(!e.cache)return;let r=g.PAGE_SIZE,l=e.sampleRate,a=t!=null?Math.max(0,Math.round(t*l)):0,i=n!=null?Math.round(n*l):e._.len-a,o=Math.floor(a/r),u=Math.min(Math.ceil((a+i)/r),e.pages.length);for(let f=o;f<u;f++)e.pages[f]===null&&await e.cache.has(f)&&(e.pages[f]=await e.cache.read(f))}async function ln(e="audio-cache"){if(typeof navigator>"u"||!navigator.storage?.getDirectory)throw new Error("OPFS not available in this environment");let n=await(await navigator.storage.getDirectory()).getDirectoryHandle(e,{create:!0});return{async read(r){let i=await(await(await n.getFileHandle(`p${r}`)).getFile()).arrayBuffer(),o=new Float32Array(i),u=o[0]|0,f=(o.length-1)/u|0,s=[];for(let h=0;h<u;h++)s.push(o.slice(1+h*f,1+(h+1)*f));return s},async write(r,l){let i=await(await n.getFileHandle(`p${r}`,{create:!0})).createWritable(),o=1+l.reduce((s,h)=>s+h.length,0),u=new Float32Array(o);u[0]=l.length;let f=1;for(let s of l)u.set(s,f),f+=s.length;await i.write(u.buffer),await i.close()},has(r){return n.getFileHandle(`p${r}`).then(()=>!0,()=>!1)},async evict(r){try{await n.removeEntry(`p${r}`)}catch{}},async clear(){for await(let[r]of n)await n.removeEntry(r)}}}g.opfsCache=ln;g.evict=rn;g.ensurePages=nn;g.DEFAULT_BUDGET=tn;var Ze={};g.stat=function(e,t){if(!arguments.length)return Ze;if(arguments.length===1)return Ze[e];typeof t=="function"&&(t={block:t}),Ze[e]=t};function Se(e){let t,n,r,l=0,a=null,i=0;function o(f){r=f,t=Object.entries(g.stat()).filter(([s,h])=>h.block).map(([s,h])=>({name:s,fn:h.block,ctx:{sampleRate:e}})),n=Object.create(null);for(let{name:s}of t)n[s]=Array.from({length:r},()=>[])}function u(f){for(let{name:s,fn:h,ctx:c}of t){let d=h(f,c);if(typeof d=="number")for(let p=0;p<r;p++)n[s][p].push(d);else for(let p=0;p<r;p++)n[s][p].push(d[p])}}return{page(f){n||o(f.length);let s=g.BLOCK_SIZE,h=0,c=f[0].length;if(i>0){let d=s-i;if(c>=d){for(let p=0;p<r;p++)a[p].set(f[p].subarray(0,d),i);u(a),h=d,i=0}else{for(let p=0;p<r;p++)a[p].set(f[p].subarray(0,c),i);return i+=c,this}}for(;h+s<=c;)u(Array.from({length:r},(d,p)=>f[p].subarray(h,h+s))),h+=s;if(h<c){a||(a=Array.from({length:r},()=>new Float32Array(s)));for(let d=0;d<r;d++)a[d].set(f[d].subarray(h));i=c-h}return this},flush(){i>0&&(u(Array.from({length:r},(f,s)=>a[s].subarray(0,i))),i=0)},delta(){if(!n)return;let f=Object.keys(n)[0];if(!f)return;let s=n[f][0].length;if(s<=l)return;let h={fromBlock:l};for(let c in n)h[c]=n[c].map(d=>new Float32Array(d.slice(l)));return l=s,h},done(){this.flush();let f={blockSize:g.BLOCK_SIZE};if(n)for(let s in n)f[s]=n[s].map(h=>new Float32Array(h));return f}}}function an(e,t,n,r,l){if(r<=0||n<=t)return new Float32Array(Math.max(0,r));if(t=Math.max(0,t),n=Math.min(n,e.length),n<=t)return new Float32Array(r);let a=new Float32Array(r),i=(n-t)/r;for(let o=0;o<r;o++){let u=t+Math.floor(o*i),f=Math.min(t+Math.floor((o+1)*i),n);f<=u&&(f=u+1),a[o]=l(e,u,f)}return a}function on(e,t,n){let r=e.blockSize,l=t.segs,a=t.totalLen;for(let s of l){let h=s[3]||1,c=s[4];if(c!=null||Math.abs(h)!==1||s[0]%r!==0||s[2]%r!==0)return null}let i=Math.ceil(a/r),o=Object.keys(e).filter(s=>s!=="blockSize"&&Array.isArray(e[s])),u=e[o[0]]?.length||1,f={blockSize:r};for(let s of o)f[s]=Array.from({length:u},()=>new Float32Array(i));for(let s of l){let h=s[0],c=s[1],d=s[2],p=s[3]||1,y=s[4],w=Math.floor(d/r),m=Math.ceil((d+c)/r);if(y===null)continue;let _=Math.floor(h/r),A=e[o[0]][0].length,x=p<0;for(let k=w;k<m&&k<i;k++){let b=x?_+(m-1-k):_+(k-w);if(!(b<0||b>=A))for(let M of o)for(let R=0;R<u;R++)f[M][R][k]=e[M][R][b]}}return f}g.statSession=Se;async function We(e,t){await e[H]();let n=D(t?.at),r=D(t?.duration),l=n!=null||r!=null;if(e.edits?.length&&e._.statsV!==e.version){if(e._.srcStats||(e._.srcStats=e.stats),l){let d=N(e);await se(e,d,n||0,r);let p=Se(e.sampleRate);for(let _ of ue(e,d,n||0,r))p.page(_);let y=p.done(),m=Object.values(y).find(_=>_?.[0]?.length)?.[0]?.length||0;return{stats:y,ch:e.channels,sr:e.sampleRate,from:0,to:m}}let c=N(e);if(!c.pipeline.length&&e._.srcStats?.blockSize){let d=on(e._.srcStats,c,e.sampleRate);if(d)e.stats=d,e._.statsV=e.version;else{let p=Se(e.sampleRate);await se(e,c);for(let y of ue(e,c))p.page(y);e.stats=p.done(),e._.statsV=e.version}}else{let d=Se(e.sampleRate);await se(e,c);for(let p of ue(e,c))d.page(p);e.stats=d.done(),e._.statsV=e.version}}let a=e.sampleRate,i=e.stats?.blockSize;if(!i)return{stats:e.stats,ch:e.channels,sr:a,from:0,to:0};let u=Object.values(e.stats).find(c=>c?.[0]?.length)?.[0]?.length||0,f=n!=null&&n<0?e.duration+n:n,s=f!=null?Math.floor(f*a/i):0,h=r!=null?Math.ceil(((f||0)+r)*a/i):u;return s=Math.max(0,Math.min(s,u)),h=Math.max(s,Math.min(h,u)),{stats:e.stats,ch:e.channels,sr:a,from:s,to:h}}g.fn.stat=async function(e,t){if(Array.isArray(e))return Promise.all(e.map(y=>this.stat(y,t)));if(typeof this[e]=="function"&&!g.stat(e))return this[e](t);let{stats:n,ch:r,sr:l,from:a,to:i}=await We(this,t),o=t?.bins,u=t?.channel,f=Array.isArray(u),s=u!=null?f?u:[u]:Array.from({length:r},(y,w)=>w),h=g.stat(e);if(h?.query&&o==null)return h.query(n,s,a,i,l);let c=n[e],d=h?.reduce;if(!c)throw new Error(`Unknown stat: '${e}'`);if(!d)throw new Error(`No reducer for stat: '${e}'`);if(o!=null){let y=o??i-a,w=A=>an(c[A],a,i,y,d);if(f)return s.map(w);if(s.length===1)return w(s[0]);let m=new Float32Array(y),_=(i-a)/y;for(let A=0;A<y;A++){let x=a+Math.floor(A*_),k=Math.min(a+Math.floor((A+1)*_),i);k<=x&&(k=x+1);let b=0;for(let M of s)b+=d(c[M],x,k);m[A]=b/s.length}return m}if(f)return s.map(y=>d(c[y],a,i));if(s.length===1)return d(c[s[0]],a,i);let p=s.map(y=>d(c[y],a,i));return p.reduce((y,w)=>y+w,0)/p.length};function Ge(e,t,n){let r=[],l=t+n;for(let a of e){let i=Math.max(a[2],t),o=Math.min(a[2]+a[1],l);i<o&&r.push(I(a[0]+(i-a[2])*Math.abs(a[3]||1),o-i,i-t,a[3],a[4]))}return r}var fn=(e,t)=>{let{total:n,length:r}=t,l=G(t.offset,n);return Ge(e,l,Math.max(0,Math.min(r??n-l,n-l)))};g.op("crop",{plan:fn});g.op("clip",{call:function(e,t){let n=this.clone?this.clone():g.from(this),r=D(t?.at),l=D(t?.duration);return r!=null||l!=null?n.crop({at:r??0,duration:l??Math.max(0,this.duration-(r??0))}):n}});g.fn.split=function(...e){let t=(Array.isArray(e[0])?e[0]:e).map(D),n=this.duration,r=[0,...t.sort((l,a)=>l-a).filter(l=>l>0&&l<n),n];return r.slice(0,-1).map((l,a)=>this.clip({at:l,duration:r[a+1]-l}))};import sn from"audio-speaker";function He(e,t,n,r,l){let a=Math.min(l,n);if(r)for(let i=0;i<a;i++){let o=i/a;for(let u=0;u<t;u++)e[i*t+u]*=o}else{let i=n-a;for(let o=0;o<a;o++){let u=1-o/a;for(let f=0;f<t;f++)e[(i+o)*t+f]*=u}}}g.fn.play=function(e){let t=e?.at??0,n=e?.duration,r=this,l=g.BLOCK_SIZE;r.playing&&(r.playing=!1,r.paused=!1,r._._wake&&r._._wake()),r.playing=!1,r.paused=e?.paused??!1,r.currentTime=t,e?.volume!=null&&(r.volume=e.volume),e?.rate!=null&&(r.playbackRate=e.rate),r.loop=e?.loop??!1,r.block=null,r._._wake=null,r._._seekTo=null,r.ended=!1;let a,i;return r.played=new Promise((o,u)=>{a=o,i=u}),r.played.catch(()=>{}),(async()=>{try{let o=r.channels,u=r.sampleRate;r.playing=!0,r.paused||O(r,"play");let f=!1,s=async()=>{for(;r.paused&&r.playing&&r._._seekTo==null;)await new Promise(d=>{r._._wake=d});r._._wake=null},h=t,c=256;for(;r.playing;){if(r.paused){if(await s(),!r.playing)break;r._._seekTo!=null&&(h=r._._seekTo,r._._seekTo=null,r.currentTime=h,r.seeking=!1)}let d=sn({sampleRate:u*(r.playbackRate||1),channels:o,bitDepth:32}),p=!1,y=0,w=!0,m=async()=>{let _=new Uint8Array(l*o*4);await new Promise(A=>d(_,A)),await new Promise(A=>d(_,A))};for await(let _ of r.stream({at:h,duration:n})){if(!r.playing)break;let A=_[0].length;for(let x=0;x<A;x+=l){if(r._._seekTo!=null){h=r._._seekTo,r._._seekTo=null,r.currentTime=h,r.seeking=!1,p=!0;break}let k=Math.min(x+l,A),b=k-x;r.block=_[0].subarray(x,k);let M=r.muted?0:r.volume,R=new Float32Array(b*o);for(let S=0;S<b;S++)for(let v=0;v<o;v++)R[S*o+v]=(_[v]||_[0])[x+S]*M;let E=()=>new Promise(S=>d(new Uint8Array(R.buffer),S));if(w&&(He(R,o,b,!0,c),w=!1),r.paused){if(He(R,o,b,!1,c),await E(),await m(),y+=b,r.currentTime=h+y/u,await s(),r._._seekTo!=null)continue;if(!r.playing)break;w=!0;continue}if(!r.playing){He(R,o,b,!1,c),await E();break}await E(),f||(f=!0,a()),y+=b,r._._seekTo==null&&(r.currentTime=h+y/u,O(r,"timeupdate",r.currentTime))}if(p||!r.playing)break}if(!p&&!r.playing&&await m(),p&&(await m(),w=!0),d(null),!p){if(!r.playing)break;if(r.loop){h=t,r.currentTime=t,O(r,"timeupdate",t);continue}r.playing=!1,r.ended=!0,O(r,"timeupdate",r.currentTime);break}}r.playing=!1,O(r,"ended"),f||a()}catch(o){console.error("Playback error:",o),r.playing=!1,resolved?O(r,"error",o):i(o)}})(),this};var Ut=g.fn;Ut.pause=function(){!this.paused&&this.playing&&(this.paused=!0,O(this,"pause"))};Ut.resume=function(){this.paused&&(this._.ctStamp=performance.now(),this.paused=!1,O(this,"play"),this._._wake&&this._._wake())};var un={aif:"aiff",oga:"ogg"};function jt(e){return un[e]||e||"wav"}async function qt(e,t,n,r){let l=await ne[t]({sampleRate:e.sampleRate,channels:e.channels,...n.meta}),a=0,i=0;for await(let u of e.stream({at:n.at,duration:n.duration})){let f=await l(u);f.length&&await r(f),a+=u[0].length,++i%2===0&&await new Promise(s=>setTimeout(s,0)),O(e,"progress",{offset:a/e.sampleRate,total:(n.duration!=null?D(n.duration):null)??e.duration})}let o=await l();return o.length&&await r(o),r(null)}g.fn.encode=async function(e,t={}){if(typeof e=="object"&&(t=e,e=void 0),e=jt(e),!ne[e])throw new Error("Unknown format: "+e);let n=[];await qt(this,e,t,i=>{i&&n.push(i)});let r=n.reduce((i,o)=>i+o.length,0),l=new Uint8Array(r),a=0;for(let i of n)l.set(i,a),a+=i.length;return l};g.fn.save=async function(e,t={}){let n=t.format??(typeof e=="string"?e.split(".").pop():"wav");if(n=jt(n),!ne[n])throw new Error("Unknown format: "+n);let r,l;if(typeof e=="string"){let{createWriteStream:a}=await import("fs"),i=a(e);r=o=>{if(!i.write(Buffer.from(o)))return new Promise(u=>i.once("drain",u))},l=()=>new Promise((o,u)=>{i.on("finish",o),i.on("error",u),i.end()})}else if(e?.write)r=a=>e.write(a),l=()=>e.close?.();else throw new Error("Invalid save target");await qt(this,n,t,a=>a?r(a):l?.())};function hn(e,t,n){let r=[],l=t+n;for(let a of e){let i=a[2]+a[1];if(i<=t)r.push(a);else if(a[2]>=l){let o=a.slice();o[2]=a[2]-n,r.push(o)}else{let o=Math.abs(a[3]||1);a[2]<t&&r.push(I(a[0],t-a[2],a[2],a[3],a[4])),i>l&&r.push(I(a[0]+(l-a[2])*o,i-l,t,a[3],a[4]))}}return r}var cn=(e,t)=>{let{total:n}=t,r=G(t.offset,n);return hn(e,r,Math.min(t.length??n-r,n-r))};g.op("remove",{plan:cn});function pn(e,t,n,r){let l=[];for(let a of e)if(a[2]+a[1]<=t)l.push(a);else if(a[2]>=t){let i=a.slice();i[2]=a[2]+n,l.push(i)}else{let i=t-a[2],o=Math.abs(a[3]||1);l.push(I(a[0],i,a[2],a[3],a[4])),l.push(I(a[0]+i*o,a[1]-i,t+n,a[3],a[4]))}return l.push(I(0,n,t,void 0,r??null)),l.sort((a,i)=>a[2]-i[2]),l}var dn=(e,t)=>{let{total:n,sampleRate:r,args:l}=t,a=l[0],i=G(t.offset,n,n);typeof a!="number"&&!a?.pages&&(a=g.from(a,{sampleRate:r}));let o=typeof a=="number"?Math.round(a*r):a.length;return t.length!=null&&(o=Math.min(o,t.length)),pn(e,i,o,typeof a=="number"?null:a)};g.op("insert",{plan:dn});function mn(e,t,n,r,l){if(r==null){let u=[];for(let f=0;f<=t