s = Server.local;

s.boot;
s.sendMsg("/b_allocRead", 0, "sounds/a11wlk01.wav");
s.sendMsg("/b_allocRead", 0, "sounds/music.aif", 44100*20, 44100*30);
s.sendMsg("/b_allocRead", 0, "sounds/laughing_chick.wav");
s.sendMsg("/b_allocRead", 1, "sounds/m_septemdecim.wav");
s.sendMsg("/b_allocRead", 2, "sounds/kookabara.au");

(
SynthDef("playbuf",{ arg out=0,bufnum=0, trig=1.0, start=0;
	Out.ar(out,
		PlayBuf.ar(2,bufnum, BufRateScale.kr(bufnum), trig, start, 1)
	)
}).load(s);
)

s.sendMsg("/s_new", "playbuf", 1001);
s.sendMsg("/n_set", 1001, \bufnum, 0);
s.sendMsg("/n_set", 1001, \trig, 0);
s.sendMsg("/n_set", 1001, \trig, 1, \start, [0, 44100].choose);

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

(
SynthDef("bufgrain",{ arg out=0,bufnum=0, start=0.0;
	var env;
	env = Env.perc(0.01, 0.9);
	Out.ar(out,
		PlayBuf.ar(1,bufnum, BufRateScale.kr(bufnum), 1, start, 1) 
		* EnvGen.kr(env, doneAction:2) * 10
	)
}).load(s);
)

s.sendMsg("/s_new", "bufgrain", -1);

s.sendMsg("/s_new", "bufgrain", -1, 0, 0, \start, 44100 * 2.3);

(
SynthDef("bufgrain2", { arg out=0, bufnum=0, start=0.0, sustain=0.3;
	var env, startFrame;
	env = Env.sine(sustain);
	startFrame = BufFrames.kr(bufnum) * start;
	Out.ar(out,
		PlayBuf.ar(1,bufnum, BufRateScale.kr(bufnum), 1, startFrame, 1) 
		* EnvGen.kr(env, doneAction:2) * 5
	)
}).load(s);
)

s.sendMsg("/s_new", "bufgrain2", -1, 0, 0, \start, 0.6.rand);
s.sendMsg("/s_new", "bufgrain2", -1, 0, 0, \start, 0, \sustain, 3);

(
Task({
	45.do({
		s.sendMsg(
			"/s_new", "bufgrain2", -1, 0, 0, 
			\start, 1.0.rand, 
			\sustain, 0.01);
		0.005.wait;
	});
}).play;
)

(
Task({
	45.do({
		s.sendMsg(
			"/s_new", "bufgrain2", -1, 0, 0, 
			\start, 1.0.rand, 
			\sustain, 0.7,
			\bufnum, #[1,0].choose
		);
		0.5.wait;
	});
}).play;
)

(
Task({
	16.do({ arg i;
		s.sendMsg("/s_new", "bufgrain2", -1, 0, 0, 
		\start, i/16, 
		\sustain, (0.125/2));
		(0.125 / 4).wait;
	});
}).play;
)

(
Task({
	32.do({ arg i;
		s.sendMsg("/s_new", "bufgrain2", -1, 0, 0, 
		\start, (32 - i) /32, 
		\sustain, 0.125 * 2);
		(0.125 + 0.1.rand2).wait;
	});
}).play;
)

(
SynthDef("bufgrain3",
	{ arg out=0,bufnum=0, start=0.0, sustain=0.3, rate=1.0;
		var env, startFrame;
		env = Env.sine(sustain);
		startFrame = BufFrames.kr(bufnum) * start;
		Out.ar(out,
			PlayBuf.ar(
				1,
				bufnum, 
				rate * BufRateScale.kr(bufnum), 
				1, 
				startFrame, 
				1
			) 
			* EnvGen.kr(env, doneAction:2)
	)
}).load(s);
)

//wirklich rückwärts
(
Task({
	32.do({ arg i;
		s.sendMsg("/s_new", "bufgrain3", -1, 0, 0, 
		\start, (32 - i) /32, 
		\sustain, 0.125 * 2,
		\rate, -1
		);
		(0.125 + 0.1.rand2).wait;
	});
}).play;
)

(
Task({
	var dt, n;
	n = 32 * 2;
	dt = 2 / n * 1.2;
	n.do({ arg i;
		s.sendMsg("/s_new", "bufgrain3", -1, 0, 0, 
		\start, i / n, 
		\sustain, 3 * dt,
		\rate, 0.8,
		\bufnum, 2
		);
		(dt * rrand(0.9, 1.1)).wait;
	});
}).play;
)

(
Task({
	var dt, n;
	n = 32 * 2;
	dt = 2 / n * 1.2;
	n.do({ arg i;
		s.sendMsg("/s_new", "bufgrain3", -1, 0, 0, 
		\start, i / n, 
		\sustain, 3 * dt,
		\rate,  i / n + 1 * 10.8,
		\bufnum, 2
		);
		(dt * rrand(0.9, 1.1)).wait;
	});
}).play;
)

(
Task({
	16.do({ arg i;
		s.sendMsg("/s_new", "bufgrain2", -1, 0, 0, \start, (i/16)*0.2, \sustain, 0.2, \bufnum, 1);
		0.1.wait;
	});
}).play;
)

s.sendMsg("/b_allocRead", 1, "sounds/vigor.aif", 0, 44100*1);
s.sendMsg("/s_new", "bufgrain2", -1, 0, 0, \start, 0, \sustain, 2, \bufnum, 2);

s.sendMsg("/b_allocRead", 2, "sounds/vigor.aif", 0, 44100*4, 0, 0);
{ DiskIn.ar( 2, 2 ) }.play;

s.sendMsg("/b_alloc", 1, 44100*8, 1);

{ RecordBuf.ar(DiskIn.ar( 2, 0 ), 1) }.play;

s.sendMsg("/s_new", "bufgrain2", -1, 0, 0, \start, 0, \sustain, 1, \bufnum, 1);

{ PlayBuf.ar(1,1, BufRateScale.kr(1), 1, 0, 1) }.play;