(  // MUNGER1
SynthDef("mung1", { arg munge, bufnum=0, bufdur=50000, panpos=(0.0), panlev=1,
					rate=(1.0), ratevar=(10.0), glength=(1000.0), lvar=20.0, speed=(0.2),
					speedvar=(0.9), gain=0.3, gainvar=(0.1), dir=0, pos=(-1.0), maxvox=40,
					panspread=(1.0), smooth=1, 
					envelope,
					att=3, sus=1, rel=1, lev=1,curve=1;  

munge = Munger.ar(bufnum, //buffer number
			bufdur,   //sample.bufFramesKr,
			rate, //rate in ms must be > 0
			ratevar, //rate variation in ms
			glength, //grain lenght in ms
			lvar, //length var
			speed, //speed
			speedvar, //speed var
			gain, //gain
			gainvar, //gain var
			dir, //direction  0: random; 1: fwd; -1: bwd;
			pos, //position -1 random
			maxvox, //max voices
			panspread,  //pan spread
			smooth //smoothPitch ==1: speed var continuos; ==0: diatonic steps
			);
Out.ar(0, Pan2.ar(munge, panpos, EnvGen.kr(Env.linen(att, sus,rel, lev, curve), 1.0, doneAction:2 )))  //panlev

}).writeDefFile;
)

(  // Mung2
SynthDef("mung2", { arg out=0, munge, bufnum=1, bufdur=50000, panpos=(0.0), panlev=1,
					rate=(1.0), ratevar=(10.0), glength=(1000.0), lvar=20.0, speed=(0.2),
					speedvar=(0.9), gain=0.3, gainvar=(0.1), dir=0, pos=(-1.0), voices=40,
					panspread=(1.0), smooth=1;  

munge = Munger.ar(bufnum, //buffer number
			bufdur,   //sample.bufFramesKr,
			rate, //rate in ms must be > 0
			ratevar, //rate variation in ms
			glength, //grain lenght in ms
			lvar, //length var
			speed, //speed
			speedvar, //speed var
			gain, //gain
			gainvar, //gain var
			dir, //direction  0: random; 1: fwd; -1: bwd;
			pos, //position -1 random
			voices, //max voices
			panspread,  //pan spread
			smooth //smoothPitch ==1: speed var continuos; ==0: diatonic steps
			);
Out.ar(out, Pan2.ar(munge, panpos, panlev))  

}).writeDefFile;
);

//What's in the buffer:
(
var bufnum;
bufnum = 100;
SynthDef("grain", { arg gate=0.0, vol=1.0, attack=0.01, decay=0.3,
pos=0.0, rate=1.0;
var snd, env;
snd = Mix(PlayBuf.ar(2, bufnum, rate, startPos:pos, loop: 1)) * 0.2;
env = Linen.kr(gate, attack, vol, decay);
FreeSelfWhenDone.kr(env);
Out.ar(0, snd * env);
}).writeDefFile;
s.sendSynthDef("grain");
s.sendMsg("/b_allocRead", bufnum,
"/System/Library/Sounds/Tink.aiff");
)

s.sendMsg("/b_allocRead", 1, "/System/Library/Sounds/Basso.aiff");
s.sendMsg("/b_allocRead", 2, "/System/Library/Sounds/Blow.aiff");
s.sendMsg("/b_allocRead", 3, "/System/Library/Sounds/Bottle.aiff");
s.sendMsg("/b_allocRead", 4, "/System/Library/Sounds/Frog.aiff");
s.sendMsg("/b_allocRead", 5, "/System/Library/Sounds/Funk.aiff");
s.sendMsg("/b_allocRead", 6, "/System/Library/Sounds/Glass.aiff");
s.sendMsg("/b_allocRead", 7, "/System/Library/Sounds/Hero.aiff");
s.sendMsg("/b_allocRead", 8, "/System/Library/Sounds/Morse.aiff");
s.sendMsg("/b_allocRead", 9, "/System/Library/Sounds/Ping.aiff");
s.sendMsg("/b_allocRead", 10, "/System/Library/Sounds/Pop.aiff");
s.sendMsg("/b_allocRead", 11, "/System/Library/Sounds/Purr.aiff");
s.sendMsg("/b_allocRead", 12, "/System/Library/Sounds/Sosumi.aiff");
s.sendMsg("/b_allocRead", 13, "/System/Library/Sounds/Submarine.aiff");
s.sendMsg("/b_allocRead", 14, "/System/Library/Sounds/Tink.aiff");

Synth(\mung2, [\bufnum, 102]);

(
var w, b1, b2, b3, sl1, sl2, sl3, sl4, sl5, sl6, sl100, num1, num2, num3, num4, num5, num6;    //lots of variables!
var str1, str2, str3, str4, str5, str6, str7, str8, str9, str10, str100, str101, str102, s;

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

// Create window - 350 x 500 pixels
w = SCWindow("SC Munger", Rect(100, 800, 350, 500));
// w.view.background = Color.grey;	// the window background will be white
w.front;						// Bring the window to the front

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

b2 = SCButton(w, Rect(120, 5, 110, 25));	// This button sends both our SynthDefs
b2.states = [
		["Load SynthDefs"]
			];
b2.action = { arg view;
//	s.sendSynthDef("variablesine");
//	s.sendSynthDef("modulator");
	s.sendSynthDef("mung1");
	s.sendSynthDef("mung2");
};

b3 = SCButton(w, Rect(235, 5, 110, 25));	// This one starts and stops the synths
b3.states = [
		["Play"],
		["Stop"],
			];
//
//
// Synth(\mung1, [\bufnum, ~buffers.last.bufnum, \bufdur, ~buffers.last.numFrames,\att,~length*3, \rel, 7,\maxvox, 5,\curve,1, \speed, 2, \glength,3000]);
//
b3.action = { arg view;
		if (b3.value == 1, {
//				s.sendMsg("/s_new", "mung1", 1002, 0, 0, \speed, 1, \glength,10);
				s.sendMsg("/s_new", "mung2", 1003, 0, 0, \speed, 1, \glength,10, \voices, 5, \buffnum, 1);
				s.sendMsg("/b_allocRead", 100, "/System/Library/Sounds/Tink.aiff");

//				s.sendMsg("n_map", 1000, \freq2, 10);
				sl2.value = 1;	// These two commands set sliders 2 and 4 to values of
				sl4.value = 1/20;	// 1 and 1/20 whenever you start the synth
		},{
//				s.sendMsg("/n_free", 1000);
//				s.sendMsg("/n_free", 1001);
//				s.sendMsg("/n_free", 1002);
				s.sendMsg("/n_free", 1003);
		});
};

// Create our sliders
sl1 = SCSlider(w, Rect(20, 100, 20, 300));
sl1.action = {
		var val;
		val = (10*sl1.value);
//		s.sendMsg("/n_set", 1000, "freq", val);
		s.sendMsg("/n_set", 1003, "speed", val);
		num1.value = val;		// This will set the value of number box 1 = value of slider 1
};

sl2 = SCSlider(w, Rect(50, 100, 20, 300));
sl2.action = {
		var val;
		val = (5000*sl2.value);
		s.sendMsg("/n_set", 1003, "glength", val);
		num2.value = val;		// This will set the value of number box 2 = value of slider 2
};

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

sl4 = SCSlider(w, Rect(110, 100, 20, 300));
sl4.action = {
		var val;
		val = (15*sl4.value+1);
		s.sendMsg("/n_set", 1003, "bufnum", val);
		num4.value = val;		// I'm sure you can guess by now what this does!
};

sl5 = SCSlider(w, Rect(140, 100, 20, 300));
sl5.action = {
		var val;
		val = ((1000*sl5.value)+0.1);
		s.sendMsg("/n_set", 1003, "rate", val);
		num5.value = val;		// I'm sure you can guess by now what this does!
};

sl6 = SCSlider(w, Rect(170, 100, 20, 300));
sl6.action = {
		var val;
		val = ((1000*sl6.value)+0.1);
		s.sendMsg("/n_set", 1003, "ratevar", val);
		num6.value = val;		// I'm sure you can guess by now what this does!
};

sl100 = SCSlider(w, Rect(75, 40, 200, 30));			// This slider will control the pan
sl100.value = 0.5;								// The default position is set to 0.5 (halfway)
sl100.action = {
		var val;
		val = sl100.value;
		s.sendMsg("/n_set", 1003, "pan", val);	// The value of the slider position is sent to the
};											// pan argument of node 1000 (our variablesine synth)

// Create our text strings
str1 = SCStaticText(w, Rect(23, 70, 133, 30));		// All of our text strings are assigned a position,
str1.string = "|-------Variablesine-------|";		// content and Font type. You can send colour
str1.font = Font("Lucida Grande", 9);			// (or color!) information as well.

str2 = SCStaticText(w, Rect(189, 70, 133, 30));
str2.string = "|---------Modulator---------|";
str2.font = Font("Futura-Medium", 9);

str3 = SCStaticText(w, Rect(20, 400, 30, 25));
str3.string = "Speed";
str3.font = Font("Lucida Grande", 9);

str4 = SCStaticText(w, Rect(50, 400, 30, 25));
str4.string = "GrainLength";
str4.font = Font("Lucida Grande", 9);

str5 = SCStaticText(w, Rect(80, 400, 30, 25));
str5.string = "Voices";
str5.font = Font("Lucida Grande", 9);

str6 = SCStaticText(w, Rect(110, 400, 30, 25));
str6.string = "Buffer";
str6.font = Font("Lucida Grande", 9);

str7 = SCStaticText(w, Rect(140, 400, 30, 25));
str7.string = "Rate";
str7.font = Font("Lucida Grande", 9);

str8 = SCStaticText(w, Rect(170, 400, 30, 25));
str8.string = "RateVar";
str8.font = Font("Lucida Grande", 9);

str100 = SCStaticText(w, Rect(165, 42, 50, 25));
str100.string = "Pan";
str100.font = Font("Lucida Grande", 9);

str101 = SCStaticText(w, Rect(62, 42, 10, 25));
str101.string = "L";
str101.font = Font("Lucida Grande", 9);

str102 = SCStaticText(w, Rect(279, 42, 10, 25));
str102.string = "R";
str102.font = Font("Lucida Grande", 9);

//create our number boxes
num1 = SCNumberBox(w, Rect(20, 430, 30, 25));		// Finally, we create our number boxes.
num1.font = Font("Lucida Grande", 9);										
num2 = SCNumberBox(w, Rect(50, 430, 30, 25));		// Each one is assigned a size and position.
num2.font = Font("Lucida Grande", 9);	

num3 = SCNumberBox(w, Rect(80, 430, 30, 25));		// You can assign a default value by using
num3.font = Font("Lucida Grande", 9);	

num4 = SCNumberBox(w, Rect(110, 430, 30, 25)); 	// variablename.value = number eg num1.value = 80; 
num4.font = Font("Lucida Grande", 9);	

num5 = SCNumberBox(w, Rect(140, 430, 30, 25));
num5.font = Font("Lucida Grande", 9);	

num6 = SCNumberBox(w, Rect(170, 430, 30, 25));
num6.font = Font("Lucida Grande", 9);	
)