// ===========================================================
// Ark/D, amorphic (2020)
// ===========================================================
(
Server.default.waitForBoot {

	// library path
	var libPath = PathName(thisProcess.nowExecutingPath.dirname +/+ "lib");
	// samples path
	var smpPath = PathName(thisProcess.nowExecutingPath.dirname +/+ "smp");

	// load samples //
	"\n--- load samples: ...".postln;
	
	~smpBuffers.do(_.free); //- free all buffers so we don't accumulate?
	~smpBuffers = List();
	
	smpPath.filesDo({ |smpfile,i|
		// tell me what you are loading:
		postln("   " + i + smpfile.fileName );
		
		// add a sample into a buffer, store object to List
		~smpBuffers.add(Buffer.readChannel(s, smpfile.fullPath, channels:[0]));
	});


	// load libs //
	"\n--- load libs: ...".postln;
	
	// for each files in that lib folder 
	libPath.filesDo({|afile|
		// tell me what you're executing:
		postln("    ." + afile.fileName);

		// execute it:
		this.executeFile(afile.fullPath);	
	});


});

s.meter;
s.scope(2);
s.plotTree



~smpBuffers.postln;

x = Synth(\softSaw);

y = Synth(\granulator, [\bufnum, ~smpBuffers.at(0)])
y.set(\amp,4)
y.set(\spread,1)
y.set(\pan, 0)
y.set(\gate, 0)

z = Synth(\bufPlayer, [\bufnum, ~smpBuffers.at(0)])


///////////////////////////////////////////////////////////////////////////////////////////

~smpBuffers[1].plot


(
Ndef(\bosc, {
	var buffer = ~smpBuffers[2], freq = 30, oscDetune = 1.01, amp = 0.2;
	BufRd.ar(1, bufnum:buffer, phase: LFSaw.ar([freq,freq*oscDetune]) * BufFrames.ir(buffer)) * amp
	//+ SinOsc.ar(60.2) * 0.5;
} );
)

Ndef(\bosc).play
Ndef(\bosc).fadeTime=2;
Ndef(\bosc).clear

///////////////////////////////////////////////////////////////////////////////////////////////
// creating buffers and filling them with weird waveforms

a = Harmonics.new(16);    // just returns an instance of Harmonics with size
b = Buffer.alloc(s, 512, 1);
(
z = SynthDef("help-Osc",{ arg out=0,bufnum=0;
    Out.ar(out,
        LeakDC.ar(
			Osc.ar(bufnum, [50,50.22], 0, 0.5)
		)
    )
});
)

a.ramp(1.0, 2.0);   
a.ramp(0.5, -0.025)

(
b.sine2(
	freqs: a.ramp(1,1).postln,
	amps: a.ramp(0, 0.01).postln,
	amps: [0.8,0,0.1,0,0,0.1,0,0,0.01,0,0.01,0,0.02,0,0,0].postln,
	normalize:false, asWavetable:true, clearFirst:true);
)
b.plot


a.formant(6, 4);
(
b.sine2(
	freqs: a.ramp(0,0.1).postln,
	amps: a.ramp(1,0),
	normalize:true, asWavetable:true, clearFirst:true).plot;
)
b.sine2(a.ramp(1.0,0.1).postln, a.geom(1.2), true, true, true);
c = b.plot;

y = z.play(s,[\out, 0, \bufnum, b]);
y.free;

{SinOsc.ar()}.play



///////////////////////////////////////////////////////////////////////////////////////////

~reverBus = Bus.audio(s,2);

Ndef(\ba).play(~reverBus, addAction: \addToHead)

~reverbDelay = Synth(\verbDelayFX, [\inBus, ~reverBus, \outBus, 0], addAction: \addAfter);
~reverbDelay.set(\revWet, 0);
~reverbDelay.set(\dlyWet, 1);
~reverbDelay.free

Ndef(\ba).fadeTime=0.01; // for Patterns we don't want crossfade, but quantization

// set default quant
Ndef(\ba).proxyspace.quant = 2;

(
Ndef(\ba, 
	Pbindf(
		Ppar([
			Pbind(
				\octave, [4,5,7],
				\amp, [0.1,0.2,0.005], 
				\degree, Pseq([0,3,4,2,6,Prand([3,4,2,6])], inf),
				\dur, Prand([1,0.5,2], 50),
			),		
			Pbind(
				\octave, [3,6],
				\degree, Pseq([[0,-3],4,6,3,2], inf),
				\dur, Pseq([3.5,5], 10),
				\amp, 0.05,
				\legato, 0.6,
				\attackTime, 2,
				\releaseTime, 2,
			)]),
		// common parameters
		\instrument, \sinPulz,
		\oscDetune, Prand([1,3,5,7]*0.01, inf),
		\scale, Scale.minor.tuning_(\just),
		\cutoff, Prand([0.5,1,1.5], inf),
		\out, ~reverBus
	))
)

Ndef(\ba).stop(1);
Ndef(\ba).clear;

////////////////////////////////////////////////////////////////////////////////////////////






































///////////////////////////////////////////////////////////////////////////////////////

//testng patterns

(
Pbind(
	\instrument, 	\softSaw,
	\octave,		2,
	\degree, 		Pseq([2,5,4,1], inf),
	\dur,			3,
	\legato,		1,
	\amp,			0.5,
	\releaseTime,	0
).play;
)




////////////////////////////////////////////////////////////////////////////////////////


// PlayBufer:
(
SynthDef(\bufPlayer, {|out = 0, bufnum = 0 |
	Out.ar(out,
		PlayBuf.ar(1, bufnum, BufRateScale.kr(bufnum), loop: 1.0)
	)
}).play(s, [\bufnum, ~smpBuffers.at(0)]);
)

~bufplay = Synth(\bufPlayer, [ \bufnum, ~smpBuffers.at(0)])

(
SynthDef(\granulator, {
	arg out = 0, bufnum = ~smpBuffers.ar(0), dur=0.2, freq=140, amp=0.2;
	var snd;

	snd = GrainBuf.ar(numChannels: 2,
		trigger: Impulse.kr(freq * LFNoise0.kr(freq).range(0.5,1.5)),
		dur: dur,
		sndbuf: bufnum,
		rate: [0.2,0.3,0.4,0.5],
		pos:LFTri.ar(0.0001, iphase:[0.5,1,0,2]).range(0,1),
		interp: 4,
		maxGrains: 1024,
		pan: [
			LFNoise1.kr(0.1).range(-1,1),
			LFNoise1.kr(0.1).range(-1,1),
			LFNoise1.kr(0.1).range(-1,1),
			LFNoise1.kr(0.1).range(-1,1)
			]
	);

	snd = Splay.ar(snd, spread:1);
	
	Out.ar(out, snd * amp);
}).play;
)

~smpBuffers.do(_.free);
~smpBuffers.at(0).free;
s.meter

(
SynthDef("sawM", {
	arg out=0, freq=140, gate=1, fadeTime=1, amp=0.5;
	var snd, env;

	snd = Saw.ar({freq/2 * LFNoise1.ar().range(0.95,1.05)  } ! 2)
	+ Saw.ar({freq * LFNoise1.ar().range(0.95,1.05)  } ! 2, )
	//+ (Saw.ar({freq * 2 * LFNoise1.ar().range(0.95,1.05) } ! 2) * 0.4)
	//	+ (Saw.ar({freq * 4 * LFNoise1.ar().range(0.95,1.05) } ! 2) * 0.2)
	;
	//snd = Saw.ar({freq/2 * LFNoise1.ar().range(0.95,1.05)} ! 4);
	//snd = snd + Saw.ar([freq/2,freq]/2 * LFNoise1.ar().range(0.95,1.05))!3;
	snd = Splay.ar(snd, spread:1, level:1, center:0);
 	env = EnvGen.kr( Env([0, 1, 0], [fadeTime, fadeTime], \sin, 1),
        gate, doneAction: Done.freeSelf);

	snd = snd * env * amp;

	Out.ar(out, snd);
}).play;
);
s.scope

// load samples



// load busses



// load patterns

2.rand








(
x = { arg spread=1, level=1, center=0.0;
 Splay.ar(
  SinOsc.ar( { |i| LFNoise2.kr(1).exprange(200, 4000) } ! 10),
  spread,
  level,
  center
 );
}.play;
)



b = Buffer.read(s, Platform.resourceDir +/+ "sounds/a11wlk01.wav");

(
x = {
	arg out = 0, bufnum, dur=0.2, freq=140, amp=0.5;
	var snd;

	snd = GrainBuf.ar(numChannels: 2,
		trigger: Impulse.kr(freq * LFNoise0.kr(freq).range(0.5,1.5)),
		dur: dur,
		sndbuf: bufnum,
		rate: [0.2,0.3,0.4,0.5],
		pos:LFTri.ar(0.01, iphase:[0.5,1,0,2]).range(0,1),
		interp: 4
	);

	//snd = [snd.at(0),snd.at(1)] + [snd.at(3),snd.at(2)] * 0.2;
	snd = Splay.ar(snd * amp, spread:1);
	//Mix(snd * 0.25);
	
	//Out.ar(out, snd);
}.play(s,[\bufnum, b]);
)

s.scope



{ Mix.new([ PinkNoise.ar(0.1), FSinOsc.ar(801, mul:0.1), LFSaw.ar(40, mul:0.1)]) }.play



play({
    VarSaw.ar(
        LFPulse.kr(3, 0, 0.3, 200, 200),
        0,
        LFTri.kr(1.0).range(0,1), //width
        0.1)
});

(
play({
	VarSaw.ar([50,50.5], 0, SinOsc.kr(0.01,1).range(0.05,0.95), 0.5)!2
	+ VarSaw.ar([101,99.3], 0, SinOsc.kr(0.06,0).range(0.2,0.8) , 0.5)!2 * 0.2
});)


(
SynthDef(\softSaw, {
	arg out = 0, freq = 40, detune = 0.1, amp = 1, pan = 0,
	gate = 1, attackTime = 0.1, susLevel = 1.0, releaseTime = 1.0;
	var env, snd;
	detune = detune * 0.1;
	amp = amp * 0.4;

	env = Linen.kr(gate, attackTime, susLevel, releaseTime);

	snd = VarSaw.ar(
		freq: [freq, freq * (1.003+detune)],
		width: SinOsc.kr(0.01,0.2).range(0.05,0.095));
	snd = snd + VarSaw.ar(
		freq: [(freq*2 * (1.001-detune)),(freq*2 * (1+detune))],
		width: SinOsc.kr(0.06,0.8).range(0.2,0.8));

	
	snd = Balance2.ar(snd.at(0), snd.at(1), pos: pan);
	
	snd = snd * amp * env;

	Out.ar(out, snd);
	
}).play);



Linen.kr()





(
SynthDef(\granulator, {
	
	arg out = 0, bufnum = ~smpBuffers.ar(0), grainDur=0.2, grainFreq=140, amp=0.8,
	gate = 1, attackTime = 0.1, susLevel = 1.0, releaseTime = 1.0,
	spread = 1, pan = 0, seed = 1924, id=0;
	var snd, env;
	
    RandID.ir(id);
    RandSeed.ir(1, seed);
	spread = spread * abs(abs(pan)-1);

	env = Linen.kr(gate, attackTime, susLevel, releaseTime);
	snd = GrainBuf.ar(numChannels: 1,
		trigger: Impulse.kr(grainFreq * LFNoise0.kr(grainFreq).range(0.5,1.5)),
		dur: grainDur,
		sndbuf: bufnum,
		rate: [0.2,0.3,0.4,0.5],
		pos:LFTri.ar(0.0001, iphase:[IRand(0,1),IRand(0,1),IRand(0,1),IRand(0,1)]).range(0,1),
		interp: 4,
		maxGrains: 1024,
		pan: [ LFNoise1.kr(0.1).range(-1,1),
			   LFNoise1.kr(0.1).range(-1,1),
			   LFNoise1.kr(0.1).range(-1,1),
			   LFNoise1.kr(0.1).range(-1,1) ]
	);
	snd = Splay.ar(snd, spread:spread, level:amp, center: pan);
	snd = snd * env;
	
	Out.ar(out, snd);
}).play;
);












spread

(
x = { arg spread=1, level=0.2, center=0.0;
 Splay.ar(
  SinOsc.ar( { |i| LFNoise2.kr(1).exprange(200, 4000) } ! 2, mul: { LFNoise2.kr(1).exprange(0.01,1)}!2),
  spread,
  level,
  center
 );
}.play;
)

x.set(\spread, 1,   \center, 0);  // full stereo
x.set(\spread, 0.5, \center, 0);  // less wide
x.set(\spread, 0,   \center, 0);  // mono center
x.set(\spread, 0.5, \center, 0.5);
// spread from center to right
x.set(\spread, 0,   \center, -1); // all left
x.set(\spread, 1,   \center, 0);  // full stereo