//// MINIMAL BAROQUE GROOVE
(
TempoClock.default.tempo = 60/60;
//TempoClock.default.tempo = 30/60;

// scales / pitch structure ////////

~s0 = Scale.harmonicMinor;
~s1 = Scale.melodicMinor;
~s2 = Scale.gong;
~s3 = Scale.whole;
~s4 = Scale.chromatic;

// rhythmic patterns /////////////

~r0 = Pn(1/8, 16);
~rx0 = Pwrand([1/8, 1/4, 1/2, 4],[80,50,20,20].normalizeSum, 1);
~rx1 = Pwrand([1/8, 1/4, 1/2, 1],[80,50,20,20].normalizeSum, 1);
~rx2 = Pwrand([1/8, 1/4, 1/2],[80,50,20].normalizeSum, 1);
~rx3 = Pwrand([1/8, 1/4],[90,30].normalizeSum, 1);


// shuffled durs
(
~r0s = Pshuf([
	1/8,
	1/8,
	1/8,
	1/8,
	Rest(1/8),
	1/8,
	1/8,
	1/8,
],1);

~r1s = Pshuf([
	1/8,
	1/8,
	1/8,
	1/8,
	Rest(1/8),
	Rest(1/8),
	1/8,
	1/8,
],1);

//Place

~r2s = Pshuf([
	1/8,
	1/8,
	1/8,
	1/8,
	Rest(1/8),
	Rest(1/8),
	Rest(1/8),
	1/8,
],1);

~r3s = Pshuf([
	1/8,
	1/8,
	1/8,
	1/8,
	Rest(1/8),
	Rest(1/8),
	Rest(1/8),
	Rest(1/8),
],1);


~unfolding = Pseq([~r0s, ~r1s, ~r2s, ~r3s, ~r4s, ~r5s, ~r6s, ~r7s], 1);

);

// patterns
~rs = Pseq([
	Pconst(32, Pseq([~r0], inf)),
],inf);

~rb = Pseq([
	Pconst(32, Pseq([~r0], inf)),
],inf);

// harmonic structure and bassline

// arpeggios + patterns

~d0 = Pxrand([0,2,4,6], 32);
~d1 =  Pxrand([1,3,5,7], 32); // 1 bar
~d2 = Pseq([2,4,6,8], 8);
~d3 = Pseq([3,5,7,9],8);
~d4 = Pseq([4,6,8,10],8);
~d5 = Pxrand([0,2,4,6,8,10,12, 14], 8);

// bassline

~bassroot8  = Pdup(16, Pseq([0, 4, 2, 3, 1, 4, 4, 0],1));
~bassroot16 = Pdup(32, Pseq([0, 4, 2, 3, 1, 4, 4, 0],1));

// harmonic structure 0,4,2,3,1,4,0

~dseq =  Pseq([
	Pn( ~d0, 1),
	Pn( ~d4, 1),
	Pn( ~d2, 1),
	Pn( ~d3, 1),
	Pn( ~d1, 1),
	Pn( ~d4, 1),
	Pn( ~d4, 1),
	Pn( ~d0, 1),

], 1);

// octavisation patterns

~o0 = Prand([5], inf);
~o1 = Prand([2,3], inf);
~o2 = Prand([3,4], inf);
~o3 = Prand([3,4,5], inf);
~o4 = Prand([5,6,7], inf);
~o5 = Pwrand([4,5,6,7],[20,3,4,5].normalizeSum, inf);

/////////////////////////////////////////////////
// e harmonic minor (\root 4) ///////////////////
//var ~t1, ~t2, ~t3, ~t4, b1, b2, b3, b4, a, b, r ,t, q , w;

// =========================
// treble m31
~synth1_0000 = Pbind( \type, \midi, \midicmd, \noteOn, \midiout, ~m32_1,
	\root, 4, \scale,~s2, \degree, ~bassroot16, \octave, ~o5, \dur,~rb
);

~synth1_0001 = Pbind( \type, \midi, \midicmd, \noteOn, \midiout, ~m32_1,
	\root, 4, \scale,~s2, \degree, ~bassroot16, \octave, ~o4, \dur,~rb,
	\p5, Pfunc { |e| n.sendMsg("/mother1",  (e[\degree]).asFloat );  },
	\rotation, Pfunc { |e| n.sendMsg("/rotation1",  (e[\octave]).asFloat );
	},
);

~synth1_0002 = Pbind(\type, \midi, \midicmd, \noteOn, \midiout, ~m32_1,
	\root, 4, \scale,~s2, \degree, ~bassroot16, \octave, ~o3, \dur,~rb,
	\p5, Pfunc { |e| n.sendMsg("/mother1",  (e[\degree]).asFloat );  },
	\rotation, Pfunc { |e| n.sendMsg("/rotation1",  (e[\octave]).asFloat );
	},
);

~synth1_0003 = Pbind(\type, \midi, \midicmd, \noteOn, \midiout, ~m32_1,
	\root, 4, \scale,~s2, \degree, ~dseq, \octave, ~o4, \dur,~rb,
	\p5, Pfunc { |e| n.sendMsg("/mother1",  (e[\degree]).asFloat );  },
	\rotation, Pfunc { |e| n.sendMsg("/rotation1",  (e[\octave]).asFloat );
	},
);

~synth1_0004 = Pbind(\type, \midi, \midicmd, \noteOn, \midiout, ~m32_1,
	\root, 4, \scale,~s2, \degree, ~dseq, \octave, ~o4, \dur,~rb,
	\p5, Pfunc { |e| n.sendMsg("/mother1",  (e[\degree]).asFloat );  },
	\rotation, Pfunc { |e| n.sendMsg("/rotation1",  (e[\octave]).asFloat );
	},
);

~synth1_0005 = Pbind(\type, \midi, \midicmd, \noteOn, \midiout, ~m32_1,
	\root, 4, \scale,~s2, \degree, ~bassroot16, \octave, ~o4, \dur,~rb,
	\p5, Pfunc { |e| n.sendMsg("/mother1",  (e[\degree]).asFloat );  },
	\rotation, Pfunc { |e| n.sendMsg("/rotation1",  (e[\octave]).asFloat );
	},
);

~synth1_0006 = Pbind(\type, \midi, \midicmd, \noteOn, \midiout, ~m32_1,
	\root, 4, \scale,~s2, \degree, ~dseq, \octave, ~o3, \dur,~rb,
	\p5, Pfunc { |e| n.sendMsg("/mother1",  (e[\degree]).asFloat );  },
	\rotation, Pfunc { |e| n.sendMsg("/rotation1",  (e[\octave]).asFloat );
	},
);


// =========================
// bass m32

~synth2_0000 = Pbind( \type, \midi, \midicmd, \noteOn, \midiout, ~m32_2,
	\root, 4, \scale,~s2, \degree, ~bassroot8, \octave, 3, \dur, 1/4,
	\p5, Pfunc { |e| n.sendMsg("/mother1",  (e[\degree]).asFloat );  },
	\rotation, Pfunc { |e| n.sendMsg("/rotation1",  (e[\octave]).asFloat ); },
);

~synth2_0001 = Pbind( \type, \midi, \midicmd, \noteOn, \midiout, ~m32_2,
	\root, 4, \scale,~s2, \degree, ~bassroot16, \octave, 3, \dur,~rb,
	\p5, Pfunc { |e| n.sendMsg("/mother1",  (e[\degree]).asFloat );  },
	\rotation, Pfunc { |e| n.sendMsg("/rotation1",  (e[\octave]).asFloat ); },
);

~synth2_0002 = Pbind(\type, \midi, \midicmd, \noteOn, \midiout, ~m32_2,
	\root, 4, \scale,~s2, \degree, ~bassroot16, \octave, ~o1, \dur,~rb,
	\p5, Pfunc { |e| n.sendMsg("/mother2",  (e[\degree]).asFloat );  },
	\rotation, Pfunc { |e| n.sendMsg("/rotation2",  (e[\octave]).asFloat );
	},
);

~synth2_0003 = Pbind(\type, \midi, \midicmd, \noteOn, \midiout, ~m32_2,
	\root, 4, \scale,~s2, \degree, ~bassroot16, \octave, ~o2, \dur,~rb,
	\p5, Pfunc { |e| n.sendMsg("/mother2",  (e[\degree]).asFloat );  },
	\rotation, Pfunc { |e| n.sendMsg("/rotation2",  (e[\octave]).asFloat );
	},
);

~synth2_0004 = Pbind(\type, \midi, \midicmd, \noteOn, \midiout, ~m32_2,
	\root, 4, \scale,~s2, \degree, ~dseq, \octave, 3, \dur,~rb,
	\p5, Pfunc { |e| n.sendMsg("/mother2",  (e[\degree]).asFloat );  },
	\rotation, Pfunc { |e| n.sendMsg("/rotation2",  (e[\octave]).asFloat );
	},
);

~synth2_0005 = Pbind(\type, \midi, \midicmd, \noteOn, \midiout, ~m32_2,
	\root, 4, \scale,~s2, \degree, ~dseq, \octave, ~o2, \dur,~rb,
	\p5, Pfunc { |e| n.sendMsg("/mother2",  (e[\degree]).asFloat );  },
	\rotation, Pfunc { |e| n.sendMsg("/rotation2",  (e[\octave]).asFloat );
	},
);

~synth2_0006 = Pbind(\type, \midi, \midicmd, \noteOn, \midiout, ~m32_2,
	\root, 4, \scale,~s2, \degree, ~bassroot16, \octave, ~o2, \dur,~rb,
	\p5, Pfunc { |e| n.sendMsg("/mother2",  (e[\degree]).asFloat );  },
	\rotation, Pfunc { |e| n.sendMsg("/rotation2",  (e[\octave]).asFloat );
	},
);

// drum patterns ///////////////////////////////////////////////////////////

~h = Pbind(\type, \midi, \midicmd, \noteOn, \midiout, ~hydro_0, \chan, 10,
	\midinote, Prand([45], inf),
	\dur, Pconst(4, Pseq([ Rest(1/4), 1/4], inf)),
	//\amp, Pexprand(2,20, inf) / 127,
);

~hh = Pbind(\type, \midi, \midicmd, \noteOn, \midiout, ~hydro_0, \chan, 10,
	\midinote, Prand([42, 43,44,45], inf),
	\dur, Pconst(4, Prand([1/8], inf)),
	\amp, Pexprand(2,20, inf) / 127,
);

~d00 = Pbind(\type, \midi, \midicmd, \noteOn, \midiout, ~hydro_0, \chan, 10,
	\midinote, Prand([42, 43, 44,45], 32),
	\dur, Pconst(4, Pseq( [ Prand([1/8, 1/2, Rest(1/8)], 8), Rest(1/2)], inf)),
);

~d01 = Pbind(\type, \midi, \midicmd, \noteOn, \midiout, ~hydro_0, \chan, 10,
	\midinote, Prand([42, 43, 44], 32),
	\dur,  Pconst(4,  Pwrand([1/8, 1/4, Rest(1/8)], [5,3,2].normalizeSum,  inf)),
);

~d02 = Pbind(\type, \midi, \midicmd, \noteOn, \midiout, ~hydro_0, \chan, 10,
	\midinote, Prand([42,  43, 44, 45], 32),
	\dur,  Pconst(4, Pwrand([1/8, 1/4], [5,3,2].normalizeSum,  inf)),
);

~d03 = Pbind(\type, \midi, \midicmd, \noteOn, \midiout, ~hydro_0, \chan, 10,
	\midinote, Prand([42,43, 44, 45, 46], 32),
	\dur, Pconst(4, Prand([1/8, 1/4], inf)),
);

~d04 = Pbind(\type, \midi, \midicmd, \noteOn, \midiout, ~hydro_0, \chan, 10,
	\midinote, Prand([42,43, 44, 45, 46, 47], 32),
	\dur, Pconst(4, Prand([1/8, 1/4], inf)),
);

~d05 = Pbind(\type, \midi, \midicmd, \noteOn, \midiout, ~hydro_0, \chan, 10,
	\midinote, Prand([42,43], inf),
	\dur, Pconst(4, Pseq([
		1/8, 1/8, 1/4, Rest(1/2),
		//Prand([1/16],3), Rest(13/16),
		Pseq([ Rest(1/16), 1/16], 16)], 1)),
);

// -----Prand([1/16], 8)

~snare00 = Pbind(\type, \midi, \midicmd, \noteOn, \midiout, ~hydro_0, \chan, 10,
	\midinote, Prand([39,40, 41], inf),
	\dur, Pconst(4, Prand([1/8, 1/4, Rest(1)], inf)),
);

~snare01 = Pbind(\type, \midi, \midicmd, \noteOn, \midiout, ~hydro_0, \chan, 10,
	\midinote, Prand([39, 40 ,41], inf),
	\dur, Pconst(4, Prand([1/8, 1/4, Rest(1/2)], inf)),
);

~snare02 = Pbind(\type, \midi, \midicmd, \noteOn, \midiout, ~hydro_0, \chan, 10,
	\midinote, Prand([39,40,41], inf),
	\dur, Pconst(4, Prand([1/8, 1/4, Rest(1/4)], inf)),
);

~snare03 = Pbind(\type, \midi, \midicmd, \noteOn, \midiout, ~hydro_0, \chan, 10,
	\midinote, Prand([39,40, 41], inf),
	\dur, Pconst(4, Prand([1/8, 1/4, Rest(1/8)], inf)) ,
);

~snare04 = Pbind(\type, \midi, \midicmd, \noteOn, \midiout, ~hydro_0, \chan, 10,
	\midinote, Prand([39,40, 41, 49], inf),
	\dur, Pconst(4, Prand([1/8, 1/4, Rest(1/8)], inf)) ,
);

~snare05 = Pbind(\type, \midi, \midicmd, \noteOn, \midiout, ~hydro_0, \chan, 10,
	\midinote, Prand([39,40, 41, 49, 50], inf),
	\dur, Pconst(4, Prand([1/8, 1/4], inf))
);

// -----

~kick00 = Pbind(\type, \midi, \midicmd, \noteOn, \midiout, ~hydro_0, \chan, 10,
	\midinote, Prand([36], inf),
	\dur, Pconst(4, Pseq([1, Rest(3)], inf))
);

~kick01 = Pbind(\type, \midi, \midicmd, \noteOn, \midiout, ~hydro_0, \chan, 10,
	\midinote, Prand([36, 37,], inf),
	\dur, Pconst(4, Prand([1/8, 1/4, Rest(1/2)], inf))
);

~kick02 = Pbind(\type, \midi, \midicmd, \noteOn, \midiout, ~hydro_0, \chan, 10,
	\midinote, Prand([36,37,38], inf),
	\dur, Pconst(4, Prand([1/8, 1/4, Rest(1/4)], inf))
);

~kick03 = Pbind(\type, \midi, \midicmd, \noteOn, \midiout, ~hydro_0, \chan, 10,
	\midinote, Prand([36,37, 38], inf),
	\dur, Pconst(4, Prand([1/8, 1/4, Rest(1/4)], inf))
);

~kick04 = Pbind(\type, \midi, \midicmd, \noteOn, \midiout, ~hydro_0, \chan, 10,
	\midinote, Prand([36,37,38], inf),
	\dur, Pconst(4, Prand([1/8, 1/4, Rest(1/8)], inf))
);

~kick05 = Pbind(\type, \midi, \midicmd, \noteOn, \midiout, ~hydro_0, \chan, 10,
	\midinote, Prand([38, 36,37], inf),
	\dur, Pconst(4, Prand([1/2, 1/4, 1/8], inf))
);

// =========================


~synth1_intr = Pseq( [~t0], 1); // top part
~synth2_intr = Pseq( [~b1], 1); // bottom part

~synth1_main = Pseq( [~t2, ~t1, ~t3, ~t1, ~t4, ~t4, ~t5], 1); // top part
~synth2_main = Pseq( [~b1, ~b2, ~b2, ~b3, ~b4, ~b4, ~b5], 1); // bottom part

~kickrr = Rest(4);
~snarerr = Rest(4);

~kick_one_ =  Pseq( [ ~kick00, ~kick00, ~kick00, ~kick00, ~kick00, ~kick00, ~kick00, ~kick00 ], 1);
~kick_intr =  Pseq( [ ~kick01, ~kick01, ~kick01, ~kick01, ~kick01, ~kick02, ~kick03, ~kick04 ], 1);
~kick_main =  Pseq( [ ~kick04, ~kick05, ~kick04, ~kick05, ~kick03, ~kick04, ~kick03, ~kick04 ], 1);

~snare_intr = Pseq( [ ~snarerr, ~snare00, ~snarerr, ~snare00, ~snare04, ~snare95, ~snare05, ~snare05 ], 1);
~snare_main = Pseq( [ ~snare00, ~snare01, ~snare02, ~snare03, ~snare04, ~snare95, ~snare05, ~snare05 ], 1);

~hh1 =  Pseq( [~hh, ~hh, ~hh, ~hh, ~hh, ~hh, ~hh, ~hh], 1);

~d_intr = Pseq([~d00, ~d01, ~d02, ~d03, ~d04, ~d04, ~d04, ~d04], 1); // hydrogen part
~d_main = Pseq([~d04, ~d04, ~d04, ~d04, ~d04, ~d04, ~d04, ~d04], 1); // hydrogen part


);

//z = Ppar( [r, t, d, ~kick, ~snare, ~hh1], 1 ).play;  // in parallel
z = Ppar( [r, t, d, ~hh], 1 ).play;  // in parallel

(
z =
Pseq([
	Ppar( [~synth1_0000, ~synth2_0001], 1 ),  // in parallel
	Ppar( [~synth1_0001, ~synth2_0001, ~hh1, ~kick_one_], 1 ),
	Ppar( [~synth1_0002, ~synth2_0002, ~d_intr, ~hh1, ~kick_one_], 1 ),
	Ppar( [~synth1_0003, ~synth2_0003, ~d_main, ~kick_intr, ~kick_one_], 1 ),
	Ppar( [~synth1_0004, ~synth2_0004, ~d_main, ~kick_main, ~snare_intr], 1 ),
	Ppar( [~synth1_0005, ~synth2_0005, ~d_main, ~kick_main, ~snare_main], 1 ),
	Ppar( [~synth1_0006, ~synth2_0006, ~d_main, ~kick_main, ~snare_main], 1 ),
	Ppar( [~synth1_0001, ~synth2_0006, ~d_main, ~kick_main, ~snare_main], 1 )

], 1
).play();
)

~hh.play;

~b1.play;
// controls

t.pause;

z.stop;
z.start;
z.pause;
z.resume;

