// SoundTouchJS 2.1.1 — MPL-2.0: https://mozilla.org/MPL/2.0/
// Copyright (c) Olli Parviainen, Ryan Berdeen, Jakub Fiala, Steve "Cutter" Blades.
// Unmodified DSP from the accompanying source.tar.gz.
// Rebuild: node tools/build-soundtouch-core.mjs
var S=class{_inputBuffer;_outputBuffer;constructor({createBuffers:e=!1,inputBufferFactory:t,outputBufferFactory:r}={}){if(e){if(!t||!r)throw new Error("buffer factories are required when createBuffers is true");this._inputBuffer=t(),this._outputBuffer=r()}else this._inputBuffer=null,this._outputBuffer=null}get inputBuffer(){return this._inputBuffer}set inputBuffer(e){this._inputBuffer=e}get outputBuffer(){return this._outputBuffer}set outputBuffer(e){this._outputBuffer=e}clear(){this._inputBuffer?.clear(),this._outputBuffer?.clear()}};var m=class{_buffer;_capacityFrames;_readFrame;_frameCount;constructor(e=2048){let t=Math.max(1,Math.floor(e));this._capacityFrames=t,this._buffer=new Float32Array(t*2),this._readFrame=0,this._frameCount=0}get capacityFrames(){return this._capacityFrames}get frameCount(){return this._frameCount}clear(){this._readFrame=0,this._frameCount=0}ensureCapacity(e){let t=Math.max(0,Math.floor(e));if(t<=this._capacityFrames)return;let r=Math.max(t,this._capacityFrames*2,this._capacityFrames+1024),a=new Float32Array(r*2);for(let s=0;s<this._frameCount;s+=1){let u=(this._readFrame+s)%this._capacityFrames*2,o=s*2;a[o]=this._buffer[u],a[o+1]=this._buffer[u+1]}this._buffer=a,this._capacityFrames=r,this._readFrame=0}pushSamples(e,t=0,r=0){let s=Math.max(0,Math.floor(t))*2,i=Math.max(0,Math.floor((e.length-s)/2)),u=r>0?Math.floor(r):0,o=u>0?Math.min(u,i):i;if(o<=0)return;this.ensureCapacity(this._frameCount+o);let f=(this._readFrame+this._frameCount)%this._capacityFrames;for(let p=0;p<o;p+=1){let l=s+p*2,d=(f+p)%this._capacityFrames*2;this._buffer[d]=e[l],this._buffer[d+1]=e[l+1]}this._frameCount+=o}putSamples(e,t=0,r=0){this.pushSamples(e,t,r)}extract(e,t=0,r=0,a=!1){let s=Math.max(0,Math.floor(t)),i=r>0?Math.floor(r):0,u=Math.max(0,this._frameCount-s),o=i>0?Math.min(i,u):u;if(o<=0)return 0;for(let f=0;f<o;f+=1){let l=(this._readFrame+s+f)%this._capacityFrames*2,c=f*2;e[c]=this._buffer[l],e[c+1]=this._buffer[l+1]}if(a){let f=s+o;this.dropFrames(f)}return o}readSample(e){let t=Math.max(0,Math.floor(e)),r=Math.floor(t/2);if(r>=this._frameCount)return 0;let a=t%2,i=(this._readFrame+r)%this._capacityFrames*2+a;return this._buffer[i]??0}dropFrames(e){let t=Math.max(0,Math.floor(e)),r=Math.max(0,Math.min(t,this._frameCount));return r===0?0:(this._readFrame=(this._readFrame+r)%this._capacityFrames,this._frameCount-=r,this._frameCount===0&&(this._readFrame=0),r)}receive(e=this._frameCount){this.dropFrames(e)}};var h=class{_buffer;_vector;_position;_frameCount;constructor(e=131072){this._buffer=new ArrayBuffer(0,{maxByteLength:e*8}),this._vector=new Float32Array(this._buffer),this._position=0,this._frameCount=0}get vector(){return this._vector}get position(){return this._position}get startIndex(){return this._position*2}get frameCount(){return this._frameCount}get endIndex(){return(this._position+this._frameCount)*2}clear(){this._vector.fill(0),this._position=0,this._frameCount=0}put(e){this._frameCount+=e}putSamples(e,t=0,r=0){let a=t*2;(!(r>=0)||r===0)&&(r=(e.length-a)/2);let s=r*2;this.ensureCapacity(r+this._frameCount);let i=this.endIndex;this._vector.set(e.subarray(a,a+s),i),this._frameCount+=r}putBuffer(e,t=0,r=0){(!(r>=0)||r===0)&&(r=e.frameCount-t),this.putSamples(e.vector,e.position+t,r)}receive(e){(e===void 0||!(e>=0)||e>this._frameCount)&&(e=this._frameCount),this._frameCount-=e,this._position+=e}receiveSamples(e,t=0){let r=t*2,a=this.startIndex;e.set(this._vector.subarray(a,a+r)),this.receive(t)}extract(e,t=0,r=0){let a=this.startIndex+t*2,s=r*2;e.set(this._vector.subarray(a,a+s))}ensureCapacity(e=0){let t=Math.floor(e*2);if(this._vector.length<t){let r=t*4;if(r<=this._buffer.maxByteLength)this.rewind(),this._buffer.resize(r),this._vector=new Float32Array(this._buffer);else{let a=r*2,s=new ArrayBuffer(r,{maxByteLength:a}),i=new Float32Array(s);i.set(this._vector.subarray(this.startIndex,this.endIndex)),this._buffer=s,this._vector=i,this._position=0}}else this.rewind()}ensureAdditionalCapacity(e=0){this.ensureCapacity(this._frameCount+e)}rewind(){this._position>0&&(this._vector.set(this._vector.subarray(this.startIndex,this.endIndex)),this._position=0)}};var A=class{inputBuffer;constructor(){this.inputBuffer=null}get frameCount(){return this.inputBuffer?.frameCount??0}clear(){this.inputBuffer=null}syncFromInputBuffer(e){this.inputBuffer=e}extract(e,t,r){let a=this.inputBuffer;if(a===null)return 0;let s=Math.max(0,a.frameCount-t),i=Math.max(0,Math.min(r,s));return i===0?0:(a.extract(e,t,i),i)}receive(e){this.inputBuffer?.receive(e)}},R=class{circularBuffer;scratch;constructor(){this.circularBuffer=new m,this.scratch=new Float32Array(0)}get frameCount(){return this.circularBuffer.frameCount}clear(){this.circularBuffer.clear()}syncFromInputBuffer(e){if(e instanceof h){let a=e.frameCount;if(a===0)return;this.circularBuffer.pushSamples(e.vector,e.position,a),e.receive(a);return}let t=e.frameCount;if(t===0)return;let r=t*2;this.scratch.length<r&&(this.scratch=new Float32Array(r)),e.extract(this.scratch,0,t),this.circularBuffer.pushSamples(this.scratch,0,t),e.receive(t)}extract(e,t,r){return this.circularBuffer.extract(e,t,r,!1)}receive(e){this.circularBuffer.dropFrames(e)}},L=()=>new A,v=()=>new R;var O={zeroCrossings:4,normalize:!1};function Z(n,e){let t={...e,...n??{}},r=Math.max(2,Math.min(8,Math.round(Number(t.zeroCrossings??e.zeroCrossings??4)))),a=!!t.normalize;return{zeroCrossings:r,normalize:a}}function G(n,e){if(typeof n!="object"||n===null)return;let t=n;t.params={zeroCrossings:Math.max(2,Math.round(Number(e.zeroCrossings??4))),normalize:!!e.normalize}}function C(n,e,t,r,a,s){if(r<0)return a===0?s.prevSampleL:s.prevSampleR;if(r>=t){let i=e+2*(t-1)+a;return n[i]}return n[e+2*r+a]}function w(n){if(n===0)return 1;let e=Math.PI*n;return Math.sin(e)/e}function J(n,e){return Math.abs(n)>=e?0:w(n)*w(n/e)}var z=(n,e,t,r,a,s)=>{let i=s,u=i.params.zeroCrossings,o=!!i.params.normalize,f=Math.floor(r),p=f-(u-1),l=f+u,c=0,d=0;for(let B=p;B<=l;B+=1){let $=r-B,T=J($,u);c+=C(n,e,t,B,a,i)*T,d+=T}return Math.abs(d)<1e-12?C(n,e,t,Math.round(r),a,i):o?c/d:c/(d||1)};z.createState=()=>({prevSampleL:0,prevSampleR:0,params:{...O}});var K={id:"lanczos",baseStrategy:"linear",kernel:z,defaultParams:O,normalizeParams:Z,applyParams:G};var q=new Map,ee="lanczos";function te(n){return typeof n=="string"?{id:n}:n!==void 0?n:{id:ee}}function W(n){let e=q.get(n);if(e!==void 0)return e;throw new Error(`Unknown interpolation strategy id "${n}". Register it before use.`)}function re(n){let e=n.baseStrategy??"lanczos";q.set(n.id,{id:n.id,baseStrategy:e,builtIn:!0,kernel:n.kernel,defaultParams:{...n.defaultParams??{}},normalizeParams:n.normalizeParams,applyParams:n.applyParams})}function D(n,e=new Set){if(n.kernel!==void 0)return n;if(e.has(n.id))throw new Error(`Interpolation strategy resolution cycle detected at "${n.id}".`);e.add(n.id);let t=W(n.baseStrategy);return D(t,e)}function ae(n,e){let t=n.defaultParams;if(n.normalizeParams!==void 0)return n.normalizeParams(e,t);let r={...t};if(e!==void 0)for(let[a,s]of Object.entries(e))s!==void 0&&(r[a]=s);return r}function U(n){let e=te(n),t=W(e.id),r=D(t),a=r.kernel;if(a===void 0)throw new Error(`Interpolation strategy "${e.id}" did not resolve to a kernel.`);let s=ae(t,e.params);return{id:t.id,kernel:a,params:s,applyParams:t.applyParams??r.applyParams}}re({...K,baseStrategy:"lanczos"});var y=class n extends S{_rate;fractionalPosition;previousLeftSample;previousRightSample;inputScratch;outputScratch;sampleBufferAdapterFactory;sampleBufferFactory;inputAdapter;interpolationStrategy;resolvedInterpolationKernel;kernelState;interpolationStrategyParams;applyKernelParams;constructor({createBuffers:e=!1,sampleBufferAdapterFactory:t=v,sampleBufferFactory:r=()=>new m,interpolationStrategy:a}={}){super({createBuffers:e,inputBufferFactory:r,outputBufferFactory:r}),this.fractionalPosition=-1,this.previousLeftSample=0,this.previousRightSample=0,this._rate=1,this.inputScratch=new Float32Array(0),this.outputScratch=new Float32Array(0),this.sampleBufferAdapterFactory=t,this.sampleBufferFactory=r,this.inputAdapter=t(),this.interpolationStrategy="lanczos",this.resolvedInterpolationKernel=()=>0,this.kernelState=void 0,this.interpolationStrategyParams={},this.applyKernelParams=void 0,this.setInterpolationStrategy(a??"lanczos")}set rate(e){this._rate=e}get strategy(){return this.interpolationStrategy}get strategyParams(){return{...this.interpolationStrategyParams}}setInterpolationStrategy(e){let t=U(e);this.interpolationStrategy=t.id,this.resolvedInterpolationKernel=t.kernel,this.interpolationStrategyParams={...t.params},this.applyKernelParams=t.applyParams,"createState"in this.resolvedInterpolationKernel&&typeof this.resolvedInterpolationKernel.createState=="function"?this.kernelState=this.resolvedInterpolationKernel.createState():this.kernelState=void 0,this.applyKernelParams!==void 0&&this.applyKernelParams(this.kernelState,this.interpolationStrategyParams),this.reset()}setInterpolationStrategyParams(e){let t={...this.interpolationStrategyParams};for(let[r,a]of Object.entries(e))a!==void 0&&(t[r]=a);this.interpolationStrategyParams=t,this.applyKernelParams!==void 0&&this.applyKernelParams(this.kernelState,this.interpolationStrategyParams)}reset(){this.fractionalPosition=-1,this.previousLeftSample=0,this.previousRightSample=0}clear(){super.clear(),this.inputAdapter.clear(),this.reset()}clone(){let e=new n({createBuffers:!1,sampleBufferAdapterFactory:this.sampleBufferAdapterFactory,sampleBufferFactory:this.sampleBufferFactory,interpolationStrategy:{id:this.interpolationStrategy,params:this.interpolationStrategyParams}});return e.rate=this._rate,e}process(){if(this._inputBuffer===null||this._outputBuffer===null)return;this.inputAdapter.syncFromInputBuffer(this._inputBuffer);let e=this.inputAdapter.frameCount;if(e===0)return;let t=this.transpose(e);this.inputAdapter.receive(e),t>0&&this._outputBuffer.putSamples(this.outputScratch,0,t)}ensureScratchCapacity(e){let t=e*2;this.inputScratch.length<t&&(this.inputScratch=new Float32Array(t));let r=Math.ceil(e/this._rate)+2,a=Math.max(0,r)*2;this.outputScratch.length<a&&(this.outputScratch=new Float32Array(a))}transpose(e=0){if(this._inputBuffer!==null&&(this.inputAdapter.syncFromInputBuffer(this._inputBuffer),e===0&&(e=this.inputAdapter.frameCount)),e===0)return 0;this.ensureScratchCapacity(e);let t=this.inputScratch,r=this.inputAdapter.extract(t,0,e);return r===0?0:(e=r,this.transposePluginKernel(e))}transposePluginKernel(e){let t=this.inputScratch,r=this.outputScratch,a=0,s=0,i=this.resolvedInterpolationKernel,u=this.kernelState,o=this.getKernelStateRecord(u);o!==void 0&&(o.prevSampleL=this.previousLeftSample,o.prevSampleR=this.previousRightSample);let f=0,p=this.fractionalPosition,l=e-1;for(;p<=l;)r[s+2*f]=i(t,a,e,p,0,u),r[s+2*f+1]=i(t,a,e,p,1,u),f=f+1,p+=this._rate;return this.fractionalPosition=p-e,this.previousLeftSample=t[a+2*e-2],this.previousRightSample=t[a+2*e-1],o!==void 0&&(o.prevSampleL=this.previousLeftSample,o.prevSampleR=this.previousRightSample),f}getKernelStateRecord(e){if(typeof e!="object"||e===null)return;let t=e,r=t.prevSampleL,a=t.prevSampleR;if(typeof r=="number"&&typeof a=="number")return t}};var F=class{buffer;fallbackBuffer;fallbackScratch;constructor(){this.buffer=null,this.fallbackBuffer=new h,this.fallbackScratch=new Float32Array(0)}setBuffer(e){if(e instanceof h){this.buffer=e;return}let t=e.frameCount;if(t>0){let r=t*2;this.fallbackScratch.length<r&&(this.fallbackScratch=new Float32Array(r)),e.extract(this.fallbackScratch,0,t),this.fallbackBuffer.clear(),this.fallbackBuffer.putSamples(this.fallbackScratch,0,t),e.receive(t)}else this.fallbackBuffer.clear();this.buffer=this.fallbackBuffer}getBoundBuffer(){if(this.buffer===null)throw new Error("buffer is not set");return this.buffer}get frameCount(){return this.getBoundBuffer().frameCount}get startIndex(){return this.getBoundBuffer().startIndex}readSample(e){let t=this.getBoundBuffer(),r=t.startIndex,a=r+t.frameCount*2;return e<r||e>=a?0:t.vector[e]}readSubarray(e,t){return this.getBoundBuffer().vector.subarray(e,t)}receive(e){this.getBoundBuffer().receive(e)}receiveSamples(e,t){this.getBoundBuffer().receiveSamples(e,t)}},P=class{buffer;constructor(){this.buffer=null}setOutputBuffer(e){this.buffer=e}getBoundBuffer(){if(this.buffer===null)throw new Error("output buffer is not set");return this.buffer}appendSamples(e,t){this.getBoundBuffer().putSamples(e,0,t)}putFrom(e,t,r){let a=e.startIndex+t*2,s=a+r*2,i=e.readSubarray(a,s);this.getBoundBuffer().putSamples(i,0,r)}},M=class{circularBuffer;rangeScratch;constructor(){this.circularBuffer=new m,this.rangeScratch=new Float32Array(0)}setBuffer(e){if(e instanceof h){let r=e.frameCount;r>0&&(this.circularBuffer.pushSamples(e.vector,e.position,r),e.receive(r));return}let t=e.frameCount;if(t>0){let r=t*2;this.rangeScratch.length<r&&(this.rangeScratch=new Float32Array(r)),e.extract(this.rangeScratch,0,t),this.circularBuffer.pushSamples(this.rangeScratch,0,t),e.receive(t)}}get frameCount(){return this.circularBuffer.frameCount}get startIndex(){return 0}readSample(e){return this.circularBuffer.readSample(e)}readSubarray(e,t){let r=Math.max(0,Math.floor(e)),s=Math.max(r,Math.floor(t))-r,i=Math.floor(s/2);if(i<=0)return this.rangeScratch.subarray(0,0);let u=i*2;this.rangeScratch.length<u&&(this.rangeScratch=new Float32Array(u));let o=Math.floor(r/2),p=this.circularBuffer.extract(this.rangeScratch,o,i,!1)*2;return p<u&&this.rangeScratch.fill(0,p,u),this.rangeScratch.subarray(0,u)}receive(e){this.circularBuffer.dropFrames(e)}receiveSamples(e,t){this.circularBuffer.extract(e,0,t,!0)}},k=()=>new F,j=()=>new M,ne=0,se=ne,ie=0,oe=ie,ue=8,b=.25,Q=4,E=125,Y=50,H=(Y-E)/(Q-b),fe=E-H*b,x=25,X=15,V=(X-x)/(Q-b),pe=x-V*b,N=1e-12,le=256,ce=8,g=class n extends S{inputBufferAdapterFactory;sampleBufferFactory;inputBufferAdapter;outputBufferAdapter;overlapScratch;_quickSeek;midBufferDirty;midBuffer;refMidBuffer;refMidBufferEnergy;overlapLength;autoSeqSetting;autoSeekSetting;_tempo;sampleRate;_overlapMs;sequenceMs;seekWindowMs;seekWindowLength;seekLength;nominalSkip;skipFract;sampleReq;constructor({sampleRate:e=44100,createBuffers:t=!1,inputBufferAdapterFactory:r=k,sampleBufferFactory:a=()=>new h}={}){super({createBuffers:t,inputBufferFactory:a,outputBufferFactory:a}),this.inputBufferAdapterFactory=r,this.sampleBufferFactory=a,this.inputBufferAdapter=r(),this.outputBufferAdapter=new P,this.overlapScratch=new Float32Array(0),this._quickSeek=!0,this.midBufferDirty=!0,this.midBuffer=null,this.refMidBufferEnergy=0,this.overlapLength=0,this.autoSeqSetting=!0,this.autoSeekSetting=!0,this._tempo=1,this.setParameters(e,se,oe,ue)}clear(){super.clear(),this.clearMidBuffer()}clearMidBuffer(){this.midBufferDirty=!0,this.midBuffer&&this.midBuffer.fill(0),this.refMidBuffer&&this.refMidBuffer.fill(0),this.skipFract=0}setParameters(e,t,r,a){e>0&&(this.sampleRate=e),a>0&&(this._overlapMs=a),t>0?(this.sequenceMs=t,this.autoSeqSetting=!1):this.autoSeqSetting=!0,r>0?(this.seekWindowMs=r,this.autoSeekSetting=!1):this.autoSeekSetting=!0,this.calculateSequenceParameters(),this.calculateOverlapLength(this._overlapMs),this.updateTempoDerivedState()}set tempo(e){this._tempo=e,this.updateTempoDerivedState()}get tempo(){return this._tempo}get inputChunkSize(){return this.sampleReq}get outputChunkSize(){return this.overlapLength+Math.max(0,this.seekWindowLength-2*this.overlapLength)}calculateOverlapLength(e=0){let t=this.sampleRate*e/1e3;if(t=t<16?16:t,t-=t%8,t===this.overlapLength&&this.midBuffer!==null)return;this.overlapLength=t;let r=this.overlapLength*2;(!this.refMidBuffer||this.refMidBuffer.length<r)&&(this.refMidBuffer=new Float32Array(r)),(!this.midBuffer||this.midBuffer.length<r)&&(this.midBuffer=new Float32Array(r))}checkLimits(e,t,r){return e<t?t:e>r?r:e}calculateSequenceParameters(){if(this.autoSeqSetting){let e=fe+H*this._tempo;e=this.checkLimits(e,Y,E),this.sequenceMs=Math.floor(e+.5)}if(this.autoSeekSetting){let e=pe+V*this._tempo;e=this.checkLimits(e,X,x),this.seekWindowMs=Math.floor(e+.5)}this.seekWindowLength=Math.floor(this.sampleRate*this.sequenceMs/1e3),this.seekLength=Math.floor(this.sampleRate*this.seekWindowMs/1e3),this.normalizeWindowInvariants()}normalizeWindowInvariants(){this.seekLength=Math.max(1,this.seekLength),this.seekWindowLength=Math.max(this.seekWindowLength,this.overlapLength)}updateTempoDerivedState(){this.calculateSequenceParameters(),this.nominalSkip=this._tempo*(this.seekWindowLength-this.overlapLength),this.skipFract=0;let e=Math.floor(this.nominalSkip+.5);this.sampleReq=Math.max(e+this.overlapLength,this.seekWindowLength)+this.seekLength}get quickSeek(){return this._quickSeek}set quickSeek(e){this._quickSeek=e}get overlapMs(){return this._overlapMs}set overlapMs(e){e>0&&(this._overlapMs=e,this.calculateOverlapLength(this._overlapMs),this.calculateSequenceParameters(),this.updateTempoDerivedState())}setStretchParameters(e){e.quickSeek!==void 0&&(this._quickSeek=e.quickSeek);let t=!1;e.sequenceMs!==void 0&&(e.sequenceMs>0?(this.sequenceMs=e.sequenceMs,this.autoSeqSetting=!1):this.autoSeqSetting=!0,t=!0),e.seekWindowMs!==void 0&&(e.seekWindowMs>0?(this.seekWindowMs=e.seekWindowMs,this.autoSeekSetting=!1):this.autoSeekSetting=!0,t=!0),e.overlapMs!==void 0&&e.overlapMs>0&&(this._overlapMs=e.overlapMs,this.calculateOverlapLength(this._overlapMs),t=!0),t&&(this.calculateSequenceParameters(),this.updateTempoDerivedState())}clone(){let e=new n({createBuffers:!1,inputBufferAdapterFactory:this.inputBufferAdapterFactory,sampleBufferFactory:this.sampleBufferFactory});return e.tempo=this._tempo,e.setParameters(this.sampleRate,this.sequenceMs,this.seekWindowMs,this._overlapMs),e}seekBestOverlapPosition(e){let t=e??this.getInputBufferAdapter();return!this._quickSeek||this.seekLength<=le?this.seekBestOverlapPositionStereo(t):this.seekBestOverlapPositionStereoQuick(t)}seekBestOverlapPositionStereo(e){let t,r,a;this.preCalculateCorrelationReferenceStereo(),t=0,r=-1/0;for(let s=0;s<this.seekLength;s++)a=this.calculateCrossCorrelationStereo(2*s,this.refMidBuffer,e),a>r&&(r=a,t=s);return t}seekBestOverlapPositionStereoQuick(e){let t,r,a,s,i,u;this.preCalculateCorrelationReferenceStereo(),r=this.calculateCrossCorrelationStereo(0,this.refMidBuffer,e),u=1,t=0,s=0;for(let o=0;o<4;o++){let f=Number.MIN_SAFE_INTEGER,p=this.getQuickScanOffsets(o);for(let l of p)i=s+l,i!==f&&(f=i,!(i<0)&&(i>=this.seekLength||(a=this.calculateCrossCorrelationStereo(2*i,this.refMidBuffer,e),u++,a>r&&(r=a,t=i))));s=t}return u<ce?this.seekBestOverlapPositionStereo(e):t}getQuickScanOffsets(e){let t=Math.max(1,this.seekLength-1);return e===0?this.generateFractionalScanOffsets(t,2,1,14,24):e===1?this.generateSymmetricScanOffsets(t,.2):e===2?this.generateSymmetricScanOffsets(t,.06):this.generateSymmetricScanOffsets(t,.015)}generateFractionalScanOffsets(e,t,r,a,s){let i=[],u=new Set,o=Math.max(1,a),f=Math.max(1,s);for(let p=0;p<f;p++){let l=t+p*r,c=Math.round(e*l/o);c<=0||c>=this.seekLength||u.has(c)||(u.add(c),i.push(c))}return i}generateSymmetricScanOffsets(e,t){let r=Math.max(1,Math.round(e*t)),a=[1,.75,.5,.25],s=[],i=[],u=new Set;for(let o of a){let f=Math.max(1,Math.round(r*o)),p=-f,l=f;u.has(p)||(u.add(p),s.push(p)),u.has(l)||(u.add(l),i.push(l))}return s.concat(i)}preCalculateCorrelationReferenceStereo(){let e=0;for(let t=0;t<this.overlapLength;t++){let r=t*(this.overlapLength-t),a=t*2,s=this.midBuffer[a]*r,i=this.midBuffer[a+1]*r;this.refMidBuffer[a]=s,this.refMidBuffer[a+1]=i,e+=s*s+i*i}this.refMidBufferEnergy=e}calculateCrossCorrelationStereo(e,t,r){e+=r.startIndex;let a=0,s=0,i=2*this.overlapLength,u=r.readSubarray(e,e+i);for(let o=0;o<i;o+=2){let f=o<u.length?u[o]:0,p=o+1<u.length?u[o+1]:0,l=t[o],c=t[o+1];a+=f*l+p*c,s+=f*f+p*p}return s<=N||this.refMidBufferEnergy<=N?-1:a/Math.sqrt(s*this.refMidBufferEnergy)}overlapStereo(e,t,r){e+=t.startIndex;let a=this.overlapLength*2;this.overlapScratch.length<a&&(this.overlapScratch=new Float32Array(a));let s=this.overlapScratch,i=t.readSubarray(e,e+a),u=1/this.overlapLength;for(let o=0;o<this.overlapLength;o++){let f=(this.overlapLength-o)*u,p=o*u,l=2*o,c=l<i.length?i[l]:0,d=l+1<i.length?i[l+1]:0;s[l]=c*p+this.midBuffer[l]*f,s[l+1]=d*p+this.midBuffer[l+1]*f}r.appendSamples(s,this.overlapLength)}process(){let e=this.getInputBufferAdapter(),t=this.getOutputBufferAdapter();if(this.bootstrapMidBuffer(e))for(;e.frameCount>=this.sampleReq;)this.processOneWindow(e,t)}bootstrapMidBuffer(e){if(!this.midBufferDirty)return!0;if(e.frameCount<this.overlapLength)return!1;let t=this.overlapLength*2;return(!this.midBuffer||this.midBuffer.length<t)&&(this.midBuffer=new Float32Array(t)),e.receiveSamples(this.midBuffer,this.overlapLength),this.midBufferDirty=!1,!0}processOneWindow(e,t){let r=this.seekBestOverlapPosition(e);this.overlapStereo(2*Math.floor(r),e,t);let a=this.seekWindowLength-2*this.overlapLength;a>0&&t.putFrom(e,r+this.overlapLength,a),this.captureOverlapHistory(r,e),this.advanceInputByNominalSkip(e)}captureOverlapHistory(e,t){let r=t.startIndex+2*(e+this.seekWindowLength-this.overlapLength);this.midBuffer.set(t.readSubarray(r,r+2*this.overlapLength))}advanceInputByNominalSkip(e){this.skipFract+=this.nominalSkip;let t=Math.floor(this.skipFract);this.skipFract-=t,e.receive(t)}getInputBufferAdapter(){if(this._inputBuffer===null)throw new Error("inputBuffer is not set");return this.inputBufferAdapter.setBuffer(this._inputBuffer),this.inputBufferAdapter}getOutputBufferAdapter(){if(this._outputBuffer===null)throw new Error("outputBuffer is not set");return this.outputBufferAdapter.setOutputBuffer(this._outputBuffer),this.outputBufferAdapter}};function _(n,e){return(n>e?n-e:e-n)>1e-10}var I=class n{transposer;stretch;_sampleRate;_sampleBufferType;_sampleBufferFactory;_interpolationStrategy;_inputBuffer;_intermediateBuffer;_outputBuffer;_rate;_tempo;virtualPitch;constructor(e={}){this._sampleBufferType=e.sampleBufferType??"circular",this._sampleBufferFactory=e.sampleBufferFactory??(this._sampleBufferType==="fifo"?()=>new h:()=>new m),this.transposer=new y({createBuffers:!1,sampleBufferAdapterFactory:this._sampleBufferType==="circular"?v:L,sampleBufferFactory:this._sampleBufferFactory,interpolationStrategy:e.interpolationStrategy}),this._interpolationStrategy=this.transposer.strategy,this._sampleRate=e.sampleRate??44100,e.stretchFactory?this.stretch=e.stretchFactory(this._sampleRate,{sampleBufferFactory:this._sampleBufferFactory,sampleBufferType:this._sampleBufferType}):this.stretch=new g({sampleRate:this._sampleRate,createBuffers:!1,inputBufferAdapterFactory:this._sampleBufferType==="circular"?j:k,sampleBufferFactory:this._sampleBufferFactory}),this.stretch.setParameters(this._sampleRate,0,0,0),this._inputBuffer=this._sampleBufferFactory(),this._intermediateBuffer=this._sampleBufferFactory(),this._outputBuffer=this._sampleBufferFactory(),this._rate=0,this._tempo=0,this.virtualPitch=1,this.calculateEffectiveRateAndTempo()}clear(){this.transposer.clear(),this.stretch.clear()}clone(){let e=new n({sampleRate:this._sampleRate,sampleBufferType:this._sampleBufferType,sampleBufferFactory:this._sampleBufferFactory,interpolationStrategy:{id:this._interpolationStrategy,params:this.transposer.strategyParams}});return e.pitch=this.virtualPitch,e}get interpolationStrategy(){return this._interpolationStrategy}get interpolationStrategyParams(){return this.transposer.strategyParams}setInterpolationStrategy(e){this.transposer.setInterpolationStrategy(e),this._interpolationStrategy=this.transposer.strategy}setInterpolationStrategyParams(e){this.transposer.setInterpolationStrategyParams(e)}setStretchParameters(e){this.stretch.setStretchParameters(e)}set pitch(e){this.virtualPitch=e,this.calculateEffectiveRateAndTempo()}set pitchOctaves(e){this.pitch=Math.exp(.69314718056*e),this.calculateEffectiveRateAndTempo()}set pitchSemitones(e){this.pitchOctaves=e/12}get inputBuffer(){return this._inputBuffer}get outputBuffer(){return this._outputBuffer}calculateEffectiveRateAndTempo(){let e=this._tempo,t=this._rate;this._tempo=1/this.virtualPitch,this._rate=this.virtualPitch,_(this._tempo,e)&&(this.stretch.tempo=this._tempo),_(this._rate,t)&&(this.transposer.rate=this._rate),this._rate>1?this._outputBuffer!==this.transposer.outputBuffer&&(this.stretch.inputBuffer=this._inputBuffer,this.stretch.outputBuffer=this._intermediateBuffer,this.transposer.inputBuffer=this._intermediateBuffer,this.transposer.outputBuffer=this._outputBuffer):this._outputBuffer!==this.stretch.outputBuffer&&(this.transposer.inputBuffer=this._inputBuffer,this.transposer.outputBuffer=this._intermediateBuffer,this.stretch.inputBuffer=this._intermediateBuffer,this.stretch.outputBuffer=this._outputBuffer)}process(){this._rate>1?(this.stretch.process(),this.transposer.process()):(this.transposer.process(),this.stretch.process())}};export{I as SoundTouch};
