// 3 steps to make a recording

// 1.  set the number of channels, the soundfile format and the word size:

/*
recChannels_(anInteger) 
	- Sets the number of channels to record. The default is two. Must be called BEFORE prepareForRecord.

recHeaderFormat_(aString) 
	- Sets the header format of the output file. The default is "aiff". Must be called BEFORE prepareForRecord.
	
recSampleFormat_(aString) 
	- Sets the sample format of the output file. The default is "float". Must be called BEFORE prepareForRecord.
*/

s.recChannels = 4;
s.recHeaderFormat = "WAV";	// other header formats:  "AIFF", "RIFF", "Sun", "IRCAM", "none"
s.recSampleFormat = "int24";	// other word sizes: "int8", "int16", "int32", "float"

// REMARK: the sampling rate of the new sound file is the sampleRate of the server!    (s.sampleRate)

// 2. prepare the recording by setting the path for the new sound file:
// s.prepareForRecord(path);

s.prepareForRecord("/Users/abart/snd/neuer_sound.aiff")

// 3. send the message record to the server:
s.record;

// 4. pause or stop the recording:
s.pauseRecording; // re-start with s.record

s.stopRecording;

/*
see also the helpfiles for these classes and methods:

	Server  (scroll down to "Recording support")
	Object.render  ( Pattern.render )
	Score.recordNRT
	DiskOut
	RecordBuf
	SoundFile

*/

// complete recording example using one of the Task examples:

// 1. define your synthdefs
(
	// Synthdef for buffer granulation:
	SynthDef("bufgrain", { arg buf=0, rate=1, start=0, amp=0.2, dur=0.3, pan=0;
		var env, sig;
	
		env = EnvGen.ar(Env([0, amp, 0], [dur*0.5, dur*0.5], \sine), doneAction: 2); 
		sig = PlayBuf.ar(1, buf, rate * BufRateScale.ir(buf), 1, start * BufSamples.ir(buf), 0);
		sig = Pan2.ar(sig * env, pan);
		OffsetOut.ar(0, sig);
	}).send(s);
	
	// simple reverb
	SynthDef("rev", {
		var in, rev;
		in = In.ar(0, 2);	
		rev = Mix(in);	
		6.do({ rev = LPF.ar(AllpassN.ar(rev, 1.0, [0.05.rand, 0.05.rand], 2.0), 7000)});
		ReplaceOut.ar(0, (in * 0.6) + (rev * 0.3))
	}).send(s);

)	

// 2. prepare your source sound files (if needed)
b = Buffer.read(s, "sounds/a11wlk01.wav");

// 3. prepare your schedulers, tasks, patterns, groups, effect synths etc. (if needed)
(
g = Group(s);
r = Synth.after(g, "rev");
t = Task({
	var next, rate, st, vol, pos, dur;
	inf.do({ arg i;
		dur 	= 0.1;
		next = dur * 0.1;
		vol 	= 0.8;
		pos 	= rrand(-1.0, 1.0);
		rate	= [-12, -5, 0, 7, 12].choose.midiratio;
		st 	= 0.9.rand;	
		Synth("bufgrain", [\buf, b.bufnum, \dur, dur, \rate, rate, \start, st, \pan, pos, \amp, vol], g); // target is group g !!
		next.wait;	
	});
})
)

// 4. set the format for the recording and set the path
(
s.recChannels = 2;
s.recHeaderFormat = "AIFF";	
s.recSampleFormat = "int16";
	
s.prepareForRecord("/Users/abart/Desktop/test.aiff")
)

// 5. start recording, your tasks, patterns, other synths etc.
(
s.record;
t.start;
)

// 6. stop everything
(
t.stop;
// delay the stop of the recording because of the tail of the reverb:
{ s.stopRecording; r.free; g.free; }.defer(2.0);  
)

// 7. test the new soundfile (if needed)
a = SoundFile.openRead("/Users/abart/Desktop/test.aiff").play;