// Advanced controls
//
// We can change the value of arguments in real time using the /n_set command.
// You need to send the node ID, argument name and value.
// Lets use the variablesine synthdef from before:
s.sendSynthDef("variablesine");

// Play A440
s.sendMsg("/s_new", "variablesine", 1000, 1, 0, "freq", 440);

// Now lets go up an octave by changing the freq value to 880
s.sendMsg("/n_set", 1000, "freq", 880);
// and a few more messages, just to prove a point!
s.sendMsg("/n_set", 1000, "freq", 800);
s.sendMsg("/n_set", 1000, "freq", 1000);
s.sendMsg("/n_set", 1000, "freq", 670);
s.sendMsg("/n_set", 1000, "freq", 780);
s.sendMsg("/n_free", 1000);

// You can pass other ugens as arguments - for example, there is a ugen called Line which generates 
// a line between two points you specify. E.g Line.kr(start, end, duration);
//
// To use this to control an argument, we need to create a control bus and then map an argument 
// to it. Let's create a control using the Line ugen to control the frequency of our variablesine
// synthdef. We will use control bus 10.
(
SynthDef("line", { arg i_bus=10, i_start=880, i_end=440, i_time=1;
	ReplaceOut.kr(i_bus, Line.kr(i_start, i_end, i_time, doneAction: 2));
}).writeDefFile;
s.sendSynthDef("line");
)

// Start playing a sine wave
s.sendMsg("/s_new", "variablesine", 1000, 1, 0, "freq", 440);

// Send a frequency value to the control bus
s.sendMsg("/c_set", 10, 440);

// Now tell node 1000 to read its freq argument from control bus 10
s.sendMsg("n_map", 1000, \freq, 10);

// Now start a control process which writes to control bus 10
s.sendMsg("/s_new", "line", 1001, 0, 0);

// Free the node
s.sendMsg("/n_free", 1000);

// You can also pass arguments to the control - remember we specified arguments for start, 
// end and time
s.sendMsg("/s_new", "line", 1001, 0, 0, "i_start", 440, "i_end", 880, "i_time", 5);

// notes: ReplaceOut.ar or kr overwrites the contents of a bus. Its arguments are bus number and
// array of channels i.e. ReplaceOut.ar(busnum, Collection of ugens);
//
// In the Line.kr ugen, I specified the argument "doneAction: 2". This tells the node to free itself
// when the line is finished