
// Ate Bit - "backDAW"
// Reaper JS script demo by 4mat - 1/1/2012
// Don't take it too serious, proof of concept. ;)
//
// My last demo was 23 bytes long so I guess this has a bit more substance.

desc:"backDAW" demo by Atebit
filename:0,border.png
filename:1,star_rez1.png
filename:2,star_rez2.png
filename:3,star_rez3.png
filename:4,star_rez4.png
filename:5,ballnew1.png
filename:6,ballnew2.png
filename:7,ballnew3.png
filename:8,ballnew4.png
filename:9,text1.png
filename:10,text2.png
filename:11,Logo_Atebit.png

@init

  temp = 0;
  pagewidth = 320;
  pageheight = 210;
  pagescale = 1;
  pagescalewidth = 1;
  pagescaleheight = 1;
  demopart = -1;
  demotimer = 1;
  logofade = 0;
  redstart = 1.0;
  greenstart = 1.0;
  bluestart = 1.0;
  redtarget = 0;
  greentarget = 0;
  bluetarget = 0;
  glow = 16;

    rotz = 0.0;
    roty = 0.0;
    rotx = 0.0;
    
    rotzadd = -0.03;
    rotyadd = 0.00;
    rotxadd = 0.00;
    
    py = 0.0;
    px = 0.0;
    pz = 0.0;
    pya = 0.0;
    pxa = 0.0;
    pza = 0.0;
    
  threedx = 1000;
  threedy = 1200;
  threedz = 1400;
  
  artx = 1600;
  arty = 1800;
  artz = 2000;
  artc = 2200;
  artm = 2400;
  artp = 2600;
  
  newx = 2800;
  newy = 3000;
  newz = 3200;

  transcount = 0;

  songpos = 4000;
  songpos[0] = 0;
  songpos[1] = -3;
  songpos[2] = -6;

  wavePos = 4008;
  wavePos[0] = 0;
  wavePos[1] = 0;
  wavePos[2] = 0;
  waveSpeed = 4010;
  waveSpeed[0] = 0;
  waveSpeed[1] = 0;
  waveSpeed[2] = 0;

  notedata = 5000;

notedata[0]=5;notedata[1]=24;notedata[2]=29;notedata[3]=5;notedata[4]=29;notedata[5]=24;notedata[6]=5;notedata[7]=12;
notedata[8]=17;notedata[9]=12;notedata[10]=20;notedata[11]=5;notedata[12]=17;notedata[13]=-7;notedata[14]=5;notedata[15]=32;
notedata[16]=5;notedata[17]=24;notedata[18]=29;notedata[19]=5;notedata[20]=29;notedata[21]=24;notedata[22]=5;notedata[23]=12;
notedata[24]=17;notedata[25]=12;notedata[26]=20;notedata[27]=5;notedata[28]=17;notedata[29]=-7;notedata[30]=5;notedata[31]=27;
notedata[32]=5;notedata[33]=24;notedata[34]=29;notedata[35]=5;notedata[36]=29;notedata[37]=24;notedata[38]=5;notedata[39]=12;
notedata[40]=17;notedata[41]=12;notedata[42]=20;notedata[43]=5;notedata[44]=17;notedata[45]=-7;notedata[46]=5;notedata[47]=32;
notedata[48]=5;notedata[49]=24;notedata[50]=29;notedata[51]=5;notedata[52]=29;notedata[53]=24;notedata[54]=5;notedata[55]=12;
notedata[56]=17;notedata[57]=12;notedata[58]=20;notedata[59]=5;notedata[60]=17;notedata[61]=20;notedata[62]=22;notedata[63]=24;
notedata[64]=5;notedata[65]=24;notedata[66]=29;notedata[67]=5;notedata[68]=24;notedata[69]=31;notedata[70]=24;notedata[71]=32;
notedata[72]=17;notedata[73]=34;notedata[74]=5;notedata[75]=31;notedata[76]=32;notedata[77]=20;notedata[78]=31;notedata[79]=29;
notedata[80]=5;notedata[81]=24;notedata[82]=29;notedata[83]=5;notedata[84]=24;notedata[85]=31;notedata[86]=24;notedata[87]=32;
notedata[88]=17;notedata[89]=34;notedata[90]=5;notedata[91]=31;notedata[92]=32;notedata[93]=20;notedata[94]=31;notedata[95]=29;
notedata[96]=10;notedata[97]=17;notedata[98]=25;notedata[99]=10;notedata[100]=17;notedata[101]=29;notedata[102]=10;notedata[103]=34;
notedata[104]=10;notedata[105]=32;notedata[106]=17;notedata[107]=13;notedata[108]=32;notedata[109]=10;notedata[110]=31;notedata[111]=32;
notedata[112]=10;notedata[113]=17;notedata[114]=29;notedata[115]=10;notedata[116]=17;notedata[117]=25;notedata[118]=10;notedata[119]=34;
notedata[120]=10;notedata[121]=36;notedata[122]=10;notedata[123]=17;notedata[124]=25;notedata[125]=10;notedata[126]=29;notedata[127]=31;
notedata[128]=5;notedata[129]=24;notedata[130]=29;notedata[131]=5;notedata[132]=24;notedata[133]=31;notedata[134]=24;notedata[135]=32;
notedata[136]=17;notedata[137]=34;notedata[138]=5;notedata[139]=31;notedata[140]=32;notedata[141]=20;notedata[142]=31;notedata[143]=29;
notedata[144]=5;notedata[145]=24;notedata[146]=29;notedata[147]=5;notedata[148]=24;notedata[149]=31;notedata[150]=24;notedata[151]=32;
notedata[152]=17;notedata[153]=34;notedata[154]=5;notedata[155]=31;notedata[156]=32;notedata[157]=20;notedata[158]=31;notedata[159]=29;
notedata[160]=10;notedata[161]=17;notedata[162]=25;notedata[163]=10;notedata[164]=17;notedata[165]=29;notedata[166]=10;notedata[167]=34;
notedata[168]=10;notedata[169]=32;notedata[170]=17;notedata[171]=13;notedata[172]=32;notedata[173]=10;notedata[174]=31;notedata[175]=32;
notedata[176]=10;notedata[177]=17;notedata[178]=29;notedata[179]=10;notedata[180]=17;notedata[181]=25;notedata[182]=10;notedata[183]=34;
notedata[184]=10;notedata[185]=36;notedata[186]=10;notedata[187]=17;notedata[188]=25;notedata[189]=10;notedata[190]=29;notedata[191]=31;
notedata[192]=34;notedata[193]=18;notedata[194]=6;notedata[195]=37;notedata[196]=6;notedata[197]=18;notedata[198]=39;notedata[199]=18;
notedata[200]=6;notedata[201]=41;notedata[202]=6;notedata[203]=18;notedata[204]=25;notedata[205]=18;notedata[206]=6;notedata[207]=18;
notedata[208]=36;notedata[209]=13;notedata[210]=1;notedata[211]=37;notedata[212]=1;notedata[213]=13;notedata[214]=39;notedata[215]=13;
notedata[216]=1;notedata[217]=41;notedata[218]=1;notedata[219]=13;notedata[220]=20;notedata[221]=13;notedata[222]=1;notedata[223]=3;
notedata[224]=36;notedata[225]=17;notedata[226]=5;notedata[227]=37;notedata[228]=5;notedata[229]=17;notedata[230]=39;notedata[231]=17;
notedata[232]=5;notedata[233]=41;notedata[234]=5;notedata[235]=17;notedata[236]=39;notedata[237]=5;notedata[238]=37;notedata[239]=8;
notedata[240]=37;notedata[241]=6;notedata[242]=13;notedata[243]=39;notedata[244]=18;notedata[245]=6;notedata[246]=46;notedata[247]=6;
notedata[248]=18;notedata[249]=37;notedata[250]=18;notedata[251]=6;notedata[252]=13;notedata[253]=6;notedata[254]=13;notedata[255]=18;
notedata[256]=37;notedata[257]=22;notedata[258]=10;notedata[259]=39;notedata[260]=-2;notedata[261]=10;notedata[262]=41;notedata[263]=10;
notedata[264]=22;notedata[265]=44;notedata[266]=22;notedata[267]=10;notedata[268]=43;notedata[269]=10;notedata[270]=41;notedata[271]=-2;
notedata[272]=41;notedata[273]=20;notedata[274]=17;notedata[275]=5;notedata[276]=17;notedata[277]=5;notedata[278]=-7;notedata[279]=17;
notedata[280]=32;notedata[281]=17;notedata[282]=5;notedata[283]=34;notedata[284]=17;notedata[285]=5;notedata[286]=32;notedata[287]=5;
notedata[288]=32;notedata[289]=15;notedata[290]=3;notedata[291]=31;notedata[292]=3;notedata[293]=15;notedata[294]=27;notedata[295]=15;
notedata[296]=3;notedata[297]=15;notedata[298]=27;notedata[299]=3;notedata[300]=31;notedata[301]=3;notedata[302]=29;notedata[303]=27;
notedata[304]=29;notedata[305]=13;notedata[306]=1;notedata[307]=20;notedata[308]=1;notedata[309]=13;notedata[310]=17;notedata[311]=13;
notedata[312]=1;notedata[313]=29;notedata[314]=13;notedata[315]=25;notedata[316]=32;notedata[317]=15;notedata[318]=34;notedata[319]=22;
notedata[320]=255;

  songtempo = 6000;

@gfx 320 200

  gfx_mode = 0;
  gfx_a = 1;
  gfx_r = 1;
  gfx_g = 1;
  gfx_b = 1;

  pagescale = 1.0;
  pagescalewidth = gfx_w/pagewidth;
  pagescaleheight = gfx_h/pageheight;


  gfx_w/pagewidth > gfx_h/pageheight ? 
  (
    pagescale=gfx_w/pagewidth;
  );
  gfx_w/pagewidth < gfx_h/pageheight ?
  (                 
    pagescale=gfx_h/pageheight;
  );


   gfx_x = 0;
   gfx_y = 0;
   gfx_r = redstart;
   gfx_b = bluestart;
   gfx_g = greenstart;
   gfx_a = 1.0;
   gfx_rectto(gfx_w,gfx_h);
      redstart < redtarget ?
      (
      redstart = redstart + 0.01;
      );
      redstart > redtarget ?
      (
      redstart = redstart - 0.01;
      );
      greenstart < greentarget ?
      (
      greenstart = greenstart + 0.01;
      );
      greenstart > greentarget ?
      (
      greenstart = greenstart - 0.01;
      );
      bluestart < bluetarget ?
      (
      bluestart = bluestart + 0.01;
      );
      bluestart > bluetarget ?
      (
      bluestart = bluestart - 0.01;
      );

  i = 0;
  loop(199,

        x3d = threedx[i];
        y3d = threedy[i];
        z3d = threedz[i];
        
      x2 = (x3d * Cos(rotz)) - (y3d * Sin(rotz));
      y2 = (x3d * Sin(rotz)) + (y3d * Cos(rotz));
      x3 = (x2 * Cos(roty)) - (z3d * Sin(roty));
      z2 = (x2 * Sin(roty)) + (z3d * Cos(roty));
      y3 = (y2 * Cos(rotx)) - (z2 * Sin(rotx));
      z3 = (y2 * Sin(rotx)) + (z2 * Cos(rotx));
      x2d = (200*pagescalewidth) * (x3 / (z3 + 40)) + (gfx_w/2);
      y2d = (200*pagescaleheight) * (y3 / (z3 + 40)) + (gfx_h/2);
    
   artz[i] = -z3;
   artx[i] = x2d;
   arty[i] = y2d; 
  i+=1;
  );


   i = 0;
      loop(199,
      artc[i] = artz[i];
      artm[i] = i;
   i+=1;
   );
    
   i = 1;
   loop(199,
     value = artc[i];
     temp = artm[i];
          j = i-1;
            While(
               artc[j + 1] = artc[j];
               artm[j + 1] = artm[j];
               j -=1;
               artc[j+1] = value;
               artm[j+1] = temp;
          j >= 0 && artc[j] > value;
          );
       i+=1;
      );
         
  i = 0;
  numblur = 4;
  tempz = artz[artm[i]];
  loop(199,
         gfx_x = artx[artm[i]];
         gfx_y = arty[artm[i]];
         temp = (artz[artm[i]]+64)/100;
         
         temp < 0 ? 
         (
         temp = 0;
         );
         temp > 1 ?
         (
         temp = 1;
         );

         demopart == 0 ?
         (
         gfx_mode = 1;
         gfx_a = temp*glow; 
         gfx_blit(artp[artm[i]]+1,pagescale/4,0);
           
            threedz[i] = threedz[i] - 1;
       
             threedz[i] < -32 ?
             (
             threedz[i] = 32;
             );
          );
        
         demopart == 1 ?
         (
         gfx_mode = 0;
         gfx_a = 1.0;
         gfx_blit(artp[artm[i]]+5,pagescale/4,0);

                 loop(-(artz[artm[i]]+64/64),
                 gfx_x = artx[artm[i]];
                 gfx_y = arty[artm[i]];
                 gfx_blurto(artx[artm[i]]+32*pagescale,arty[artm[i]]+32*pagescale);
                 );
         );

         demopart == 2 ?
         (      
         gfx_mode = 1;
         gfx_a = 0.8;
         gfx_blit(4,pagescale,0);      

         );

  i+=1;
  );

     glow > 1 ?
     (
     glow = glow - 1;
     );
  
  songpos[0] & 8 ?
     (
     glow = 16;
     );

  demopart == 1 ?
  (
     transcount -=1;
 
     transcount < 0 ?
        (
         i =0;
     loop(199,
            newx[i]=(Rand(32)-16);
            newy[i]=(Rand(32)-16);
            newz[i]=(Rand(32)-16);
            i+=1;
         );
            rotyadd = (Rand(10)-0.5)/100;
            rotzadd = (Rand(10)-0.5)/100; 
            rotxadd = (Rand(10)-0.5)/100;
          transcount = 128;
        );

       i = 0;
     loop(199,
          threedx[i] < newx[i] ?
          (
          threedx[i]+=1;
          );
          threedx[i] > newx[i] ?
          (
          threedx[i]-=1;
          );
          threedy[i] < newy[i] ?
          (
          threedy[i]+=1;
          );
          threedy[i] > newy[i] ?
          (
          threedy[i]-=1;
          );
          
            threedz[i] < newz[i] ?
            (
            threedz[i]+=1;
            );
            threedz[i] > newz[i] ?
            (
            threedz[i]-=1;
            );
          
      i+=1;
      );        
  );

  rotz += rotzadd;
  roty += rotyadd;
  rotx += rotxadd;
  
   logofade < 0.90 ?
   (
   logofade = logofade + 0.01
   );

   gfx_a = logofade;
   gfx_x = 0;
   gfx_y = 0;
   demopart == 0 ?
   (
   gfx_mode = 0;
   gfx_blit(11,pagescale,0);
   );
   demopart == 1 ?
   (
   gfx_mode = 0;
   gfx_blit(9,pagescale,0);
   );
   demopart == 2 ?
   (
   gfx_mode = 1;
   gfx_blit(10,pagescale,0);
   );

   gfx_x = 0;
   gfx_y = 0;
   gfx_a = 0.5; 
   gfx_mode = 1;
   gfx_blit(0,pagescale,0);

   demotimer -=1;

   demotimer < 0 ?
   (
   demopart = demopart + 1;
 
    demopart == 3 ?
    (
    demopart = 0;
    );

   demotimer = 180;
   logofade = 0; 
   redstart = 1;
   greenstart = 1;
   bluestart = 1;
    
      i = 0;
      x = 0;
      y = 0;
      loop(199,
           threedx[i] = (rand(32)-16);
           threedy[i] = (rand(32)-16);
           demopart == 0 ?
           (
           threedz[i] = (rand(64)-32);
           redtarget = 0.4;
           greentarget = 0.2;
           bluetarget = 0.1;
           rotzadd = -0.03;
           rotyadd = 0.00;
           rotxadd = 0.00;
           rotx = 0;
           roty = 0;
           rotz = 0;
           );
           demopart == 1 ?
           (
           transcount = 0;
           threedz[i] = rand(32)-16;
           redtarget = 0.1;
           greentarget = 0.1;
           bluetarget = 0.1;
           );
           demopart == 2 ?
           (
            py = 15;
              px = -7;
              pz = 24;
              pya = 4;
              pxa = 2;
              pza = -9;
                
             i=0;
             loop(199,
                threedx[i]=16*(Sin(px)+Cos(py));
                threedy[i]=16*(Sin(py)+Cos(pz));
                threedz[i]=16*(Sin(pz)+Cos(px));
                pz = pz + pza/2;
                px = px + pxa/2;
                py = py + pya/2;
                i+=1;
             );
           redtarget = 0.5;
           greentarget = 0.25;
           bluetarget = 0.0;
           rotzadd = 0.07;
           rotyadd = -0.02;
           rotxadd = 0.04;
           rotx = 0;
           roty = 0;
           rotz = 0;

           );
           artp[i] = rand(4);
           newx[i] = threedx[i];
           newy[i] = threedy[i];
           newz[i] = threedz[i];
      i+=1;
      );
 ); 

@sample

  // The frequency stuff here I took from the midisynth tutorial.

  srate = 44100;
  q = 0;
  wavemix = 0;
  rootvol = 8192;
  loop(3,
      songpos[q] > -1 ?
      (
      note = notedata[songpos[q]]+36;
      vol = ((rootvol/256)&127) / 127; 
      freq = 440 * 2^((note-69)/12);
      waveSpeed[q] = (2*$pi*freq)/srate;
      );  
   wavemix = wavemix + (sin(wavePos[q])*vol); // tri
   wavemix = wavemix + (sign(sin(wavePos[q]))*(vol/8)); // pulse
   rootvol = rootvol / 8;
 
   //wavemix = wavemix + wavePos[q]*(vol); // saw

(wavePos[q] += (waveSpeed[q])) >= 2*$pi ? wavePos[q] -= 2*$pi;
 q+=1;
 );

spl0 = wavemix;
spl1 = wavemix;

  songtempo -=1;
  songtempo < 0 ?
  (
  songtempo = 6000;    
      q = 0;
      loop(3,
      songpos[q] +=1;
        
        songpos[q] > -1 ?
        (
          notedata[songpos[q]] == 255 ?
          (
          songpos[q] = 0;
          );
        );
      q+=1;
      );
  );
