title: Midi Gate tags: reaper jsfx midi The idea here is that you play a held chord on channel 1, and then channel 2 you play a rhythm, and that controls when the notes are played. Generally this is for generating chord stab patterns. Still buggy. Debugging in Reaper JSFX is a nightmare. ``` desc: jda midi gate 1 //tags: MIDI processing in_pin:none out_pin:none @init gate = 0; gatecount = 0; notes = 100; r = 0; loop(128,(notes[r] = 0; r+=1;)); function heldon(ts) local(r) ( r = 0; loop(128,( (notes[r] > 0) ? ( midisend(ts,0x90,r,notes[r]); ); r += 1; )); ); function heldoff(ts) local(r) ( r = 0; loop(128,( (notes[r] > 0) ? ( midisend(ts,0x80,r,0); ); r += 1; )); ); nheld = 0; ax = 0; bx = 0; cx = 0; dx = 0; @block while ( midirecv(ts,msg1,msg2,msg3) ? ( c = msg1 & 0xF; s = msg1 >> 4; p = msg2; v = msg3; ( c == 1 ) ? ( ( s == 8 || ( s == 9 && v == 0 ) ) ? ( gate = 0; heldoff(); ) : ( s == 9 ) ? ( gate = 1; heldon(); ); // no pass thru ) : ( c == 0 && ( s == 8 || s == 9 ) ) ? ( ( s == 8 || ( s == 9 && v == 0 ) ) ? ( notes[p] = 0; ( gate == 0 ) ? ( ax += 1; ) : ( bx += 1; midisend(ts,0x80,p,0); ); ) : ( s == 9 ) ? ( notes[p] = v; ( gate == 0 ) ? ( cx += 1; ) : ( dx += 1; midisend(ts,0x90,p,v); ); ); ) : ( // pass thru midisend(ts,msg1,msg2,msg3); ); )); @sample ```