You are not recognized as the original poster of this topic.
let ticks=0,start=0,
seconds=3, //until calculation
sampleRate,
oscT=0; //the time for oscillator
const TAU=Math.PI*2;
function main(t){
let freq=440;
oscT=(oscT+(freq/sampleRate))%1;
return(t%2<1?Math.sin(oscT*TAU):Math.sin(t*freq*TAU))*.5;
}
return time=>{
if(time<.1){ //Reset on playback.
ticks=0;sampleRate=undefined;
}
if(sampleRate!==undefined)
return main(time); //Play main function if there's a sample rate.
if(ticks===0)
start=time; //Set start at current time.
ticks++;
if(time-start>=seconds) //Check if specified duration has elapsed.
sampleRate=Math.max(ticks/Math.max(1e-4,time-start),1); //Divide ticks by elapsed duration to get the sample rate.
return(Math.random()-.5)*.1; //Play noise if there's no calculated sample rate.
};