//Welcome to the magical world of Chris's Ultra-Mega-Fantastic-BallBusting 2nd Supercollider Project. Please take your time and use this fantastic patch to its full ability or i'll send the boys round to get ya!

// Below lies the first synth

// This Synth is controlled by the mouse movement

(
SynthDef("variablesined", { arg freqd;
        var f, g, osc, clockRated, centerFreqd, patchd, clockTimed, clockd, panPosd;
        
        clockRated = MouseX.kr(1, 200, 1);
        centerFreqd = MouseY.kr(100, 8000, 1);
        clockTimed = clockRated.reciprocal;
        clockd = Impulse.kr(clockRated, 0.4);
        panPosd = Latch.kr(WhiteNoise.kr, clockd);

        osc = CombN.ar(
                        Pan2.ar( 
                                SinOsc.ar(
                                        freqd, 
                                        0, 
                                        Decay2.kr(clockd, 0.1 * clockTimed, 0.9 * clockTimed)
                                ), 
                                panPosd
                        ),
                        0.3, 0.3, 2
                );
        Out.ar(0, osc);
}).writeDefFile; 
s.sendSynthDef("variablesined");
)

// Below lies the second synth

// This Synth is controlled by the mouse movement

(
SynthDef("liquid", {
        var clockRate, clockTime, clock, centerFreq, freq, panPos, patch;

        clockRate = MouseX.kr(1, 200, 1);
        clockTime = clockRate.reciprocal;
        clock = Impulse.kr(clockRate, 0.4);

        centerFreq = MouseY.kr(100, 8000, 1);
        freq = Latch.kr(WhiteNoise.kr(centerFreq * 0.5, centerFreq), clock);
        panPos = Latch.kr(WhiteNoise.kr, clock);
        patch = CombN.ar(
                        Pan2.ar( 
                                SinOsc.ar(
                                        freq, 
                                        0, 
                                        Decay2.kr(clock, 0.1 * clockTime, 0.9 * clockTime)
                                ), 
                                panPos
                        ),
                        0.3, 0.3, 2
                );
        Out.ar(0, patch);
}).writeDefFile;
s.sendSynthDef("liquid");
)

//Below lies the Third synth

// This Synth is controlled by the sliders on screen

(
SynthDef("bubbles", { arg freq;
	var f, g, osc;
	f = LFSaw.kr(freq, 0, 24, LFSaw.kr([8,7.23], 0, 3, 80)).midicps; // glissando function
	osc = CombN.ar(SinOsc.ar(f, 0, 0.04), 0.2, 0.2, 4); // echoing sine wave
	Out.ar(0, osc);
}).writeDefFile;
s.sendSynthDef("bubbles");
)

//Below lies the fourth synth

// This Synth is more like an FX unit that is added to the other synths playing. 

(
SynthDef("wah", { arg out, rate = 1.5, cfreq = 1400, mfreq = 1200, rq=0.1;
	var zin, zout;

	zin = In.ar(out, 2);
	cfreq = Lag3.kr(cfreq, 0.1);
	mfreq = Lag3.kr(mfreq, 0.1);
	rq   = Ramp.kr(rq, 0.1);
	zout = RLPF.ar(zin, LFNoise1.kr(rate, mfreq, cfreq), rq, 10).distort 
					* 0.15;

	// replace the incoming bus with the effected version
	ReplaceOut.ar( out , zout ); 
	
}).writeDefFile;
s.sendSynthDef("wah");

)

//Below lies the fifth synth

// This Synth is some kind of moto-revvy ultra synth thingy.

(
SynthDef("moto-rev", { arg out=0;
	var x;
	x = RLPF.ar(LFPulse.ar(SinOsc.kr(0.2, 0, 10, 21), [0,0.1], 0.1), 
		100, 0.1).clip2(0.4);
	Out.ar(out, x);
}).writeDefFile;
s.sendSynthDef("moto-rev");
)

// Here's the that lovely picturey-thing from'n which you control it y'hear?

(

//variables for the Window in which the GUI is displayed and the prudy buttons y'all can press

var w, b1, b2, b3, b4, b5, b7, b8, b9, sl1, sl2, sl3, sl4, sl5, sl6, num1, num2, num3, num4, num5, num6, str1, str2;    

s = Server.local;                       // instance of a local server

// Create window - 1400 x 1000 pixels
w = SCWindow("Chris's Kick Arse Super Collider Thingy Mark 2.4 ", Rect(5, 5, 1600, 1000));

w.front;                                                // Stops the darn thing from hiding

//These are the sliders for the 3rd synth

sl1 = SCSlider(w, Rect(25, 100, 50, 600));
sl1.action = {
                var val;
//              val = ((800*sl1.value)+80);
                val = ((30*sl1.value));
                s.sendMsg("/n_set", 1003, "freq", val);
                num1.value = val;               // This will set the value of number box 1 = value of slider 1
};

num1 = SCNumberBox(w, Rect(25, 730, 50, 20));

sl2 = SCSlider(w, Rect(125, 100, 50, 600));
sl2.action = {
                var val;
//              val = ((800*sl2.value)+80);
                val = ((100*sl2.value));
                s.sendMsg("/n_set", 1003, "freq", val);
                num2.value = val;               // This will set the value of number box 2 = value of slider 2
};

num2 = SCNumberBox(w, Rect(125, 730, 50, 20));

sl3 = SCSlider(w, Rect(225, 100, 50, 600));
sl3.action = {
                var val;
//              val = ((800*sl3.value)+80);
                val = ((300*sl3.value));
                s.sendMsg("/n_set", 1003, "freq", val);
                num3.value = val;               // This will set the value of number box 3 = value of slider 3
};

num3 = SCNumberBox(w, Rect(225, 730, 50, 20));

sl4 = SCSlider(w, Rect(325, 100, 50, 600));
sl4.action = {
                var val;
//              val = ((800*sl4.value)+80);
                val = ((500*sl4.value));
                s.sendMsg("/n_set", 1003, "freq", val);
                num4.value = val;               // This will set the value of number box 4 = value of slider 4
};

num4 = SCNumberBox(w, Rect(325, 730, 50, 20));

sl5 = SCSlider(w, Rect(425, 100, 50, 600));
sl5.action = {
                var val;
//              val = ((800*sl5.value)+80);
                val = ((700*sl5.value));
                s.sendMsg("/n_set", 1003, "freq", val);
                num5.value = val;               // This will set the value of number box 5 = value of slider 5
};

num5 = SCNumberBox(w, Rect(425, 730, 50, 20));

sl6 = SCSlider(w, Rect(525, 100, 50, 600));
sl6.action = {
                var val;
//              val = ((800*sl6.value)+80);
                val = ((900*sl6.value));
                s.sendMsg("/n_set", 1003, "freq", val);
                num6.value = val;               // This will set the value of number box 6 = value of slider 6
};

num6 = SCNumberBox(w, Rect(525, 730, 50, 20));

// Create our 5 prudy buttons

b1 = SCButton(w, Rect(5, 5, 110, 25));  // The first button starts and stops that there server
b1.states = [
                ["Start Server", Color.black, Color.green],
                ["Stop Server", Color.white, Color.red],
                        ];
b1.action = { arg view;
                if (b1.value == 1, {
                        	s.boot;
                },{
                              s.quit;  
                });
};

b5 = SCButton(w, Rect(250, 770, 1000, 150)); // This is the BIG stop button 
b5.states = [
                ["THIS IS THE LONG RED EMERGENCY STOP BUTTON", Color.black, Color.red],
                
                        ];
b5.action = { arg view;

                     s.sendMsg("/n_free", 1001);
			s.sendMsg("/n_free", 1002);
			s.sendMsg("/n_free", 1003);
			s.sendMsg("/n_free", 1004);
			s.sendMsg("/n_free", 1005);

};

b2 = SCButton(w, Rect(120, 5, 110, 25));        // This button sends all our Synthesiserthings
b2.states = [
                ["Load SynthDefs", Color.black, Color.green]
                        ];
b2.action = { arg view;
        
        s.sendSynthDef("liquid");
        s.sendSynthDef("variablesined");
        s.sendSynthDef("bubbles");
        s.sendSynthDef("wah");
        s.sendSynthDef("moto-rev");
};

b3 = SCButton(w, Rect(575, 5, 200, 50));        // This one starts and stops the one that sounds like worter
b3.states = [
                ["Luvly Bubbly Synth", Color.black, Color.green],
                ["Stop the Bubbles", Color.white, Color.red],
                        ];
b3.action = { arg view;
                if (b3.value == 1, {
                                
                                s.sendMsg("/s_new", "liquid", 1001, 0, 0, "freq", 80, "clock", 0.4);

                },{
                                
                                s.sendMsg("/n_free", 1001);
                });
};

b4 = SCButton(w, Rect(825, 5, 200, 50));        // This one starts and stops that gosh darn Variable Sine Bassy-percussion synth
b4.states = [
                ["Siney Synth", Color.black, Color.green],
                ["Stop the Siney synth", Color.white, Color.red],
                        ];
b4.action = { arg view;
                if (b4.value == 1, {
                                
                                s.sendMsg("/s_new", "variablesined", 1002, 0, 0, "freqd", 80, "clockd", 0.4);

                },{
                                
                                s.sendMsg("/n_free", 1002);
                });
};

b7 = SCButton(w, Rect(1075, 5, 200, 50));        // This one starts and stops the 3rd synth
b7.states = [
                ["BubFreqRock Synth", Color.black, Color.green],
                ["Stop the BubFreqRock synth", Color.white, Color.red],
                        ];
b7.action = { arg view;
                if (b7.value == 1, {
                                
                                s.sendMsg("/s_new", "bubbles", 1003, 0, 0, "freq", 80);

                },{
                                
                                s.sendMsg("/n_free", 1003);
                });
};

b8 = SCButton(w, Rect(825, 275, 200, 50));        // This one starts and stops that'n one that adds an FX like wot that ther jimi hendrix used to play.
b8.states = [
                ["wah wah", Color.black, Color.green],
                ["Stop the wah wah", Color.white, Color.red],
                        ];
b8.action = { arg view;
                if (b8.value == 1, {
                                
                                s.sendMsg("/s_new", "wah", 1004, 0, 0, "zin", 80, "zout", 80);

                },{
                                
                                s.sendMsg("/n_free", 1004);
                });
};

b9 = SCButton(w, Rect(1325, 5, 200, 50));        // this is a button. THATS ALL!!!!!

b9.states = [
                ["Engine Synth", Color.black, Color.green],
                ["Slow it down there daddy-o", Color.white, Color.red],
                        ];
b9.action = { arg view;
                if (b9.value == 1, {
                                
                                s.sendMsg("/s_new", "moto-rev", 1005, 0, 0, "x", 10);

                },{
                                
                                s.sendMsg("/n_free", 1005);
                });
};

// Lookie here. Its that writing thats there on that glowing box or screen if you prefer thats in front of you.

str1 = SCStaticText(w, Rect(750, 500, 500, 250));
str1.string = "Please Click on any of the 4 synths to activate them. If you click the WAH WAH button before you turn a synth on, it will add a WAH WAH effect to it (surprisingly enough).";
str1.font = Font("Futura-Medium", 14);

str2 = SCStaticText(w, Rect(750, 350, 500, 150));
str2.string = "The first 2 synths are controlled by moving the mouse. The 3rd synth controlled by the sliders. The 4th synth is somewhat static.";
str2.font = Font("Futura-Medium", 14);

)

//ITS FINISHED!!!!!!!!! (c) C jolley bLmRecords 2005