Notice: Welcome to TinyChan, an account has automatically been created and assigned to you, you don't have to register or log in to use the board, but don't clear your cookies unless you have set a memorable name and password. Alternatively, you can restore your ID. The use of this site requires cookies to be enabled; please cease browsing this site if you don't consent.

TinyChan

New reply in topic: Bytebeat/Floatbeat/Funcbeat

You are not recognized as the original poster of this topic.

:

You are required to fill in a captcha for your first 5 posts. Sorry, but this is required to stop people from posting while drunk. Please be responsible and don't drink and post!
If you receive this often, consider not clearing your cookies.

Please familiarise yourself with the rules and markup syntax before posting.


Replying to dynamic delay line…

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;
};

This one can change its delay time in real-time with no lag.