var e=class{options;samples;length=0;phase=`buffering`;readOffset=0;constructor(e){this.options=e,this.samples=new Float32Array(e.capacitySamples)}process(e,t){switch(t.fill(0),this.phase){case`buffering`:this.append(e);break;case`live`:this.copyInput(e,t);break;case`replaying`:this.hasSignal(e)&&this.append(e);for(let n=0;n<t.length;n+=1){if(this.length===0){this.phase=`live`,this.copyInput(e,t,n);break}t[n]=this.shift()}this.length===0&&(this.phase=`live`);break}}release(){if(this.phase!==`buffering`)return;let e=this.findFirstSignalOffset();if(e==null){this.length=0,this.phase=`live`;return}let t=Math.max(0,e-this.options.preRollSamples);this.readOffset=(this.readOffset+t)%this.options.capacitySamples,this.length-=t,this.phase=`replaying`}append(e){if(e!=null)for(let t of e){this.length===this.options.capacitySamples&&(this.readOffset=(this.readOffset+1)%this.options.capacitySamples,--this.length);let e=(this.readOffset+this.length)%this.options.capacitySamples;this.samples[e]=t,this.length+=1}}copyInput(e,t,n=0){if(e!=null)for(let r=n;r<t.length&&r<e.length;r+=1)t[r]=e[r]??0}findFirstSignalOffset(){for(let e=0;e<this.length;e+=1){let t=(this.readOffset+e)%this.options.capacitySamples;if(Math.abs(this.samples[t]??0)>=this.options.leadingSilenceThreshold)return e}return null}hasSignal(e){return e==null?!1:e.some(e=>Math.abs(e)>=this.options.leadingSilenceThreshold)}shift(){let e=this.samples[this.readOffset]??0;return this.readOffset=(this.readOffset+1)%this.options.capacitySamples,--this.length,e}},t=class extends AudioWorkletProcessor{queue=new e({capacitySamples:sampleRate*30,leadingSilenceThreshold:.003,preRollSamples:sampleRate*.1});constructor(){super(),this.port.onmessage=e=>{e.data?.type===`release`&&this.queue.release()}}process(e,t){let n=t[0]?.[0];return n==null||this.queue.process(e[0]?.[0],n),!0}};registerProcessor(`codex-realtime-buffered-audio`,t);
//# sourceMappingURL=realtime-buffered-audio-worklet-lroxYXhj.js.map