/*RUN*/

"/home/rob/gtr/motherPatterns.scd".load;

MIDIClient.init;

~m32_1 = MIDIOut.newByName("MidiSport 4x4", "MidiSport 4x4 MIDI 1").latency_(Server.default.latency);

~m32_2 = MIDIOut.newByName("MidiSport 4x4", "MidiSport 4x4 MIDI 2").latency_(Server.default.latency);

//  setup of mother 32 noteon events

Pdef(\m_1,  Pbind( \type, \midi, \midicmd, \noteOn, \midiout, ~m32_1)).play(quant: 4);

Pdef(\m_2,  Pbind( \type, \midi, \midicmd, \noteOn, \midiout, ~m32_2)).play(quant: 4);

// CC Control Message 1 --->  TOP Mother32 ASSIGN OUT
( Pdef(\m_1_CC,  Pbind( \type, \midi, \midicmd, \control, \midiout, ~m32_1,
	\ctlNum, 1, \control, Pexprand(72,127, inf), \dur, ~r1)).play(quant:4));

// CC Control Message 1 --->  BOTTOM Mother32 ASSIGN OUT
Pdef(\m_2_CC,  Pbind( \type, \midi, \midicmd, \control, \midiout, ~m32_2, \ctlNum, 1, \control, Pexprand(77,127, inf), \dur, 1/8 )).play(quant:4);

///////////////////////////////////
//Pdef(\processing, Pfunc { ~host.sendMsg("/sc3p5", rrand(1,256).asFloat);}).play(quant: 4);

//~host.sendMsg("/sc3p5", rrand(1,256).asFloat);

//https://sc-users.bham.ac.narkive.com/75iSw2f6/sending-osc-commands-instead-of-trigger-synths-with-patterns

n = NetAddr("127.0.0.1", 47120);

//{50.do {n.sendMsg("/test",200.rand,200.rand);0.2.wait;}}.fork;

( Event.addEventType(\processingOSC, { | server |
var processingOscServer = NetAddr("192.168.178.166", 47120);
//var processingOscServer = NetAddr("127.0.0.1", 47120);
var notes = [~degree.value, ~ctranspose.value, ~velocity.value,
~sustain.value, ~lag.value, ~timingOffset.value].flop;

var bndl;
var timeNoteOn, timeNoteOff;

//processingOscServer.sendMsg("/test",200.rand,200.rand);
//processingOscServer.sendMsg("/sc3p5",200.rand, 200.rand);

notes.do {|note|
processingOscServer.sendMsg("/sc3p5", note[0].asFloat); // send OSC message to P5
processingOscServer.sendMsg("/mother2", note[1].asFloat); // send OSC message to P5

//processingOscServer.sendMsg("/mother1", 1920.rand);
//processingOscServer.sendMsg("/mother2", 1080.rand);
//processingOscServer.sendMsg("/sc3p5", rrand(1,256).asFloat);

// sustain and timingOffset are in beats, lag is in seconds
//timeNoteOn = (thisThread.clock.tempo.reciprocal*note[5])+note[4];
//timeNoteOff =
//(thisThread.clock.tempo.reciprocal*(note[3]+note[5]))+note[4];
//{processingOscServer.sendMsg("/renoise/trigger/note_on", -1, -1,
//(note[0]+note[1]).asInt, note[2].asInt)}.defer(timeNoteOn);
//{processingOscServer.sendMsg("/renoise/trigger/note_off", -1, -1,
//(note[0]+note[1]).asInt)}.defer(timeNoteOff);
}
});
)

( Pdef(\processingA, Pbind(*[ type: \processingOSC,
	dur: 1/8, degree: Pseq([2,5,12,200,600,900,1000], inf), ]); ))

( Pdef(\processingB, Pbind(*[ type: \processingOSC,
	dur: 1/8, ctranspose: Pseq([2,5,12,300,600,900], inf), ]); ))

Pdef(\processingA).play(quant: 4);    // TOP MOTHER
Pdef(\processingA).stop;
Pdef(\processingB).play(quant: 4);    // TOP MOTHER
Pdef(\processingB).stop;

