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 Anonymous B…

(Type: Funcbeat, Hz: 11025, LINK)
//Started: 19 Aug 2024 13:56:32. Finished: 14:19:48.
//Moving points connected with lines to make a waveform.
rand=Math.random;
pToLine=(a,b,x)=>a.y+((b.y-a.y)*(x-a.x)/(b.x-a.x));
function makeChordSynth(count=5){
  let p=[];
  for(let i=0;i<count;i++){
    p.push({x:rand(),y:rand(),vx:(rand()*2)-1,vy:(rand()*2)-1});
  }
  p.sort((a,b)=>a.x-b.x);
  function step(speed=1){
     for(let i=0;i<p.length;i++){
       p[i].x+=p[i].vx*speed;
            if(p[i].x>1){p[i].x=1-(p[i].vx*speed);p[i].vx*=-1;}
       else if(p[i].x<0){p[i].x=0-(p[i].vx*speed);p[i].vx*=-1;}
       p[i].y+=p[i].vy*speed;
            if(p[i].y>1){p[i].y=1-(p[i].vy*speed);p[i].vy*=-1;}
       else if(p[i].y<0){p[i].y=0-(p[i].vy*speed);p[i].vy*=-1;}
     }
     p.sort((a,b)=>a.x-b.x);
  }
  function wave(x){
    //p must be in order first
    //p positions must be within the range [0,1] (but this returns in range [-1,1])
    if(x>=0&&x<=p[0].x){return(pToLine({x:p[p.length-1].x-1,y:p[p.length-1].y},p[0],x)*2)-1;}
    if(x>=p[p.length-1].x&&x<=1){return(pToLine(p[p.length-1],{x:p[0].x+1,y:p[0].y},x)*2)-1;}
    for(let i=0;i<p.length-1;i++){
      if(x>=p[i].x&&x<=p[i+1].x){ //This could be improved? (By going backwards and only using one comparison?)
        return(pToLine(p[i],p[i+1],x)*2)-1;
      }
    }
  }
  return(t)=>{
    step(1e-6+(sin(t/20)*1e-5));
    return(wave((t*60)%1)+wave((t*90)%1)+wave((t*200)%1)+wave((t*302)%1)+wave((t*480)%1))*.3;
  };
}
a=[
  //[synth,speed,LFO]
  [makeChordSynth(4),1.61,.2],
  [makeChordSynth(6),1.2,.6],
  [makeChordSynth(12),.401,.5]
];
return(t)=>{
  let m=[0,0];
  for(let i=0;i<a.length;i++){
    m[0]+=a[i][0](t*a[i][1])*sin(t*a[i][2]);
    m[1]+=a[i][0](t*a[i][1])*cos(t*a[i][2]);
  }
  return[m[0]/2,m[1]/2];
};