You are not recognized as the original poster of this topic.
var PI=Math.PI,TAU=PI*2,
mod=(n,m)=>(n%m+m)%m;
class Delay{
constructor(maximumDelayInSamples=0){
this.sampleRate=44100; //Default sample rate
this.totalSize=Math.max(4,maximumDelayInSamples+1);
this.bufferData=new Float32Array(this.totalSize);
this.writePos=0;
this.readPos=0;
this.delay=0;
this.delayInt=0;
this.delayFrac=0;
this.v=new Float32Array(this.totalSize);
this.feedback=0; //Feedback factor
}
setDelay(newDelayInSamples){
const upperLimit=this.getMaximumDelayInSamples();
if(newDelayInSamples<0||newDelayInSamples>upperLimit)
throw new Error("Delay out of bounds");
this.delay=newDelayInSamples;
this.delayInt=Math.floor(this.delay);
this.delayFrac=this.delay-this.delayInt;
}
getDelay(){
return this.delay;
}
setFeedback(feedback){
this.feedback=feedback; //Set feedback factor (0 to 1)
}
prepare(numChannels,sampleRate){
this.sampleRate=sampleRate;
this.bufferData=new Float32Array(numChannels*this.totalSize);
this.writePos=new Array(numChannels).fill(0);
this.readPos=new Array(numChannels).fill(0);
this.v=new Float32Array(numChannels);
this.reset();
}
setMaximumDelayInSamples(maxDelayInSamples){
this.totalSize=Math.max(4,maxDelayInSamples+1);
this.bufferData=new Float32Array(this.totalSize);
this.reset();
}
reset(){
this.writePos.fill(0);
this.readPos.fill(0);
this.v.fill(0);
this.bufferData.fill(0);
}
pushSample(channel,sample){
const index=(this.writePos[channel]+this.totalSize-1)%this.totalSize;
this.bufferData[index]=sample;
this.writePos[channel]=index;
}
popSample(channel,delayInSamples=-1,updateReadPointer=true){
if(delayInSamples>=0)
this.setDelay(delayInSamples);
const result=this.interpolateSample(channel);
if(updateReadPointer)
this.readPos[channel]=(this.readPos[channel]+this.totalSize-1)%this.totalSize;
return result;
}
interpolateSample(channel){
const index=(this.readPos[channel]+this.delayInt)%this.totalSize,
value1=this.bufferData[index],
value2=this.bufferData[(index+1)%this.totalSize];
return value1+this.delayFrac*(value2-value1);
}
processSample(channel,inputSample){
//Get the delayed sample
const delayedSample=this.popSample(channel);
//Calculate the output sample with feedback
const outputSample=inputSample+delayedSample*this.feedback;
//Push the new sample into the delay line
this.pushSample(channel,outputSample);
return outputSample;
}
getMaximumDelayInSamples(){return this.totalSize-1;}
}
let d=new Delay(32000);
d.prepare(1,32000); //Prepare the delay line
d.setFeedback(.85); //Set the feedback factor (0 to 1)
//d.setDelay(2000);
return function dsp(time){
let o=sin(time*2e3*TAU)*(1-mod(time,.5))**40;
//d.setDelay((.05+(time%.1))*8000);
d.setDelay(500+(((1+sin(time*20))/2)*100));
o=d.processSample(0,o);
return o*.5;
};