/*
If the input audio signal is out of phase, the plugin sends
a defined CC message.

You can e.g. add markers at the positions where the signal is out of phase.
Plugin sends midi data only while Reaper is playing (...or recording).

1. Insert this plugin.
2. Insert MIDItoReaControlPath.
3. Open the actions window and search for
   the action 'Insert Marker at current position'.
4. Midi learn the action by moving the 'Trigger' slider.
*/

desc: Read info...

slider1:0,Samplerate
slider2:0<0,1,1{MONO,STEREO}>Output
slider3:0<0,2,1{OUT OFF PHASE,IN PHASE,-}>Stereo Channels
slider4:300<0,1000,1>Check Interval (ms)
slider5:0,Count
slider6:0<0,1,1>Trigger (for CC learning)
slider7:15<0,15,1{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16}>MIDI Channel
slider8:20<0,127,1>CC Number

in_pin:L in
in_pin:R in
out_pin:L out
out_pin:R out

@init
slider5=0;
slider6=0;

CC_MESG_MIN = 176; // a CC message on MIDI channel 1
BYTE_SIZE = 256; // size of an 8-bit byte
offset = 0; // sample offset for midisend
send_cc = 0; // trigger
last_value = 0; // last known CC value

@slider
a = srate/1000 * slider4|0;

cc = slider8;
channel = slider7;
value = slider6;
( value != last_value ) ? send_cc = 1;
slider6 = floor(slider6);

@block
(t+=samplesblock) > a ?
(
phase > 0 ? (slider3=1; send_cc=0;) : (slider3=0; slider5+=1; send_cc=1;);
spl0==spl1 ? (slider2=0; slider3=2;) : slider2=1;
slider1=srate;
sliderchange(1+2+4);
phase=0;
t=0;
);

(play_state == 1) || (play_state == 5) ? (
send_cc ?
(
// send the message
msg1 = channel + CC_MESG_MIN;
msg23 = cc + ( value * BYTE_SIZE );
midisend( offset, msg1, msg23 );

// clean up
send_cc = 0;
last_value = value;
);
);

@sample
sign(spl0) != sign(spl1) ? phase-=1 : phase+=1;

spl0=spl0;
spl1=spl1;
