//This example was originally created in a seminar. It illustrates creating a new SynthDef, then a GUI which controls an active Synth based on that def. 

//As an exercise, you might want to expand this by adding labels to the GUI window to show a user what each control does! 


SynthDef(\mysound,{arg density=100, centrefreq=1000, rq=0.1, amp=0.1;

var dust, filter; 

//Dust is a stochastic source of impulse clicks, density per second

dust= Dust.ar(density);

//the filtering is twofold; shaping the clicks via attack and decay smoothing in Decay2, and then a Band Pass Filter

filter= BPF.ar(50*Decay2.ar(dust,0.01,0.05),centrefreq, rq);




//GUI code. The MIDI Controller option is commented out; it was added spontaneously to answer an inquiry about how to hook up an external control to a graphical user interface control. You may want to look back at this after later material in the course on MIDI. 


var w, slid2d, knob, numberbox; 

var sound; 

//use that SynthDef! 

sound= Synth(\mysound);

w= Window("mysound's window",Rect(100,300,300,200));

slid2d= Slider2D(w,Rect(10,10,180,180));

knob= Knob(w,Rect(210,10,80,80));

numberbox= NumberBox(w,Rect(210,110,80,80));

//slid2d.action = {stuff...} is the same as slid2d.action_({stuff...})

slid2d.action = { 




//to let any MIDI control message set the knob position, and trigger the corresponding action


MIDIIn.control={arg src,chan,num,val; 

//defer avoids complaints from the system that the GUI is being updated outside of a safe thread; it pushes the code through to the AppClock (see week 6 of course)  

{knob.value= (val/127.0); knob.action.value;}.defer; 



numberbox.action={var temp; 

temp= numberbox.value.max(0.0).min(1.0);


numberbox.value = temp;



w.onClose= {sound.free;};