desc: basic midi chopper

slider1:0<0,15,1{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16>input chord channel
slider2:1<0,15,1{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16>input rythm channel
slider3:0<0,15,1{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16>output channel
slider4:0<0,2,1{Always constant,Always rythm,Always chord,Combine}>velocity mode
slider5:127<0,127,1>velocity on constant mode

@init
noteOff = 0;
noteOn  = 1;
CC_MSG = 11;

note_array=128;

tmp=0;
loop(128,
  note_array[tmp] = 0;
  tmp += 1;
);


@slider
CH_N_IN = slider1;
CH_R_IN = slider2;
CH_OUT = slider3;
VEL_MODE = slider4;
VEL_CONSTANT = slider5;

@block
while (
  midirecv(offs,msg1_in,msg23_in) ?  (

      // moja premenna, 1 = note on, 0 = note off
      eventType = 1;

      // Extract message type and channel
      status = msg1_in & $xF0;
      statusHi = (msg1_in/16)|0;
      statusLo = msg1_in-(statusHi*16);
      msg3_in = (msg23_in/256)|0;
      msg2_in = msg23_in-(msg3_in*256);
      ch = msg1_in & $x0F;

      // Extract note number
      note = msg23_in & $x7F;

      // Extract velocity
      velocity = msg23_in >> 8;

      // neviem preco, ale namiesto note off v msg1 mi pride "note + velocity = 0", takze musim hackovat
      velocity == 0 ? eventType = 0;

      // channel - rythm
      ch == CH_R_IN ? (

            note = 0;
            while
            (

              // get velo of note
              v = note_array[note];

              // if current iterated note is on 
              v > 0 ? (

                VEL_MODE == 0 ? (
                  velocity = VEL_CONSTANT;
                );

                VEL_MODE == 1 ? (
                  velocity = velocity;
                );

                // TODO
                VEL_MODE == 2 ? (
                  velocity = v;
                );

                VEL_MODE == 3 ? (
                  // combine note velocity with rythm velocity
                  velocity =  v * velocity / 127;
                );

                // on note off ( rythm ) turn chord note off too
                eventType == noteOff ? velocity = 0;

                // compose output
                msg1_out = status + CH_OUT;
                msg23_out = note | (velocity << 8);
                midisend(0,msg1_out,msg23_out);   

              );

              // cycle until
              note=note+1;
              note < 128;

            );
      );



      // channel  - notes
      ch == CH_N_IN ? (

        //  statusHi == CC_MSG ? ( );

        // do pola not ulozim velocity.
        note > 0 && note < 128 ? (

          note_array[note] = velocity;
        
          // if note off, send it
          velocity == 0 ? (
                msg1_out = status + CH_OUT;
                msg23_out = note | (velocity << 8);
                midisend(0,msg1_out,msg23_out);   
          );


         );
 
      );  // end ch == 0


  1;    
  );
 
);

@sample


