//
//
//// crucial tests
//TestCase(Crucial,
//	\patcha -> { arg test;
//		// patch
//		i = Instr("help-Patch",{ arg freq=100,amp=1.0;
//				SinOsc.ar([freq,freq + 30],0,amp)
//			});
//		p = Patch(i,[ 500,	0.3 ]);
//		p.play;
//		
//		//message,button wait for confirm
//		//test.askMonkey("stereo sin wave");
//	}
//).run
//

(
// patch
i = Instr("help-Patch",{ arg freq=100,amp=1.0;
		SinOsc.ar([freq,freq + 30],0,amp)
	});
p = Patch(i,[ 500,	0.3 ]);
p.play;
)

p.stop;

p.insp

(
// gui works
Instr(\bubbles, { arg amp=1.0;
	var f, zout;
	f = LFSaw.kr(0.4, 0, 24, LFSaw.kr([8,7.23], 0, 3, 80)).midicps;
	zout = CombN.ar(SinOsc.ar(f, 0, 0.04), 0.2, 0.2, 4);
	zout * amp
});

Instr(\rlpf,{ arg audio=0,freq=500,rq=0.1;
	RLPF.ar(audio, freq, rq)
});

p = Patch(\rlpf,[
	q = Patch(\bubbles)
]);

p.gui
)

(
// fixed arg
Instr([\jmcExamples,'moto-rev'],{ arg lfo=0.2,freq=1000,rq=0.1;
	RLPF.ar(LFPulse.ar(SinOsc.kr(lfo, 0, 10, 21), [0,0.1], 0.1), freq, rq).clip2(0.4);
});
q = Patch([\jmcExamples,'moto-rev'],[
	nil,500
]);
q.play;
)

(
// should be filtered low
Instr(\bubbles, { arg amp=1.0;
	var f, zout;
	f = LFSaw.kr(0.4, 0, 24, LFSaw.kr([8,7.23], 0, 3, 80)).midicps;
	zout = CombN.ar(SinOsc.ar(f, 0, 0.04), 0.2, 0.2, 4);
	zout * amp
});
Instr(\rlpf,{ arg audio=0,freq=200,rq=0.1;
	RLPF.ar(audio, freq, rq)
});
p = Patch(\rlpf,[
	q = Patch(\bubbles)
]);
p.play;
)

(
// tempo bus
t = TempoBus.new(s);
Tempo.bpm = 60;
SynthDef("help-TempoBus",{ arg out=0,tempoBus;
	var tempo,trig,amp;
	tempo = In.kr(tempoBus);
	trig = Impulse.kr(tempo);
	amp = Decay2.kr(trig,0.01,0.1).clip2(1.0);
	Out.ar(out,
		amp * SinOsc.ar(300)
	)
}).play(s,[0,0,1,t.index]);
)

Tempo.bpm = 40;

Tempo.bpm = 666;

Tempo.bpm = 100;

(
// TempoPlayer
Instr(\helpTempoPlayer,{ arg tempo;
	Impulse.ar( tempo )
});
Patch(\helpTempoPlayer,[
	TempoPlayer.new
]).play;
)
Tempo.bpm = 40;

Tempo.bpm = 666;

Tempo.bpm = 100;

(
// ModalFreq
m = 	ModalFreq.new(
			StreamKrDur.new(Pseq(#[ 7, 6,1,10 ], inf), 1, 0.1), 
			FloatArray[ 0, 2, 3, 4, 7 ], 
			StreamKrDur.new(Pseq(#[ 6, 5 ],inf), 4, 0.1), 
			2, 
			12
		); 	
p = Patch({ arg freq=200;
	Pulse.ar(freq,0.3,0.3)
},[
	m
]).play;
)

p.stop;

p.play;

// NOT WORKING !!
(
p = Patch({ arg freq=200;
	Pulse.ar(freq,0.3,0.3)
},[  
  Patch(
		UGenInstr(ModalFreqUGen,\kr),
		[
		StreamKrDur.new(Pseq(#[ 7, 6,1,10 ], inf), 1, 0.1), 
		ArrayBuffer( [ 0, 2, 3, 4, 7 ]  ),
		6,//StreamKrDur.new(Pseq(#[ 6, 5 ],inf), 4, 0.1), 
		4,
		12]
  )
]);
p.play;
)

p.stop;

p.play;

// NOT WORKING	  
(
p = Patch({ arg freq=200;
	Pulse.ar(freq,0.3,0.3)
},[  
  Patch(UGenInstr(ModalFreqUGen,\kr),
	[
	StreamKrDur.new(Pseq(#[ 7, 6,1,10 ], inf), 1, 0.1), 
	ArrayBuffer( [ 0, 2, 3, 4, 7 ]  ),
	StreamKrDur.new(Pseq(#[ 6, 5 ],inf), 4, 0.1), 
	2,
	12]
  )
]);
p.play;
)
thisProcess.stop;

p.play;

p.insp

(
p = Patch({ arg freq=100,amp=1.0;
		SinOsc.ar([freq,freq + 30],0,amp)
	},[	
		StreamKrDur(
			ModalFreq(Pseq([1,2,3,4],inf)), 
			Pseq([0.25,0.5,0.75],inf),
			0.1),
		0.2
	]);
p.play;
)

p.stop;

p.play;

thisProcess.stop; // command .

p.play;	

(
p = Patch({ arg freq=100,amp=1.0;
		SinOsc.ar([freq,freq + 30],0,amp)
	},[	
		StreamKrDur(
			ModalFreq(Pseq([1,2,3,4],inf)), 
			Pseq([0.25,0.5,0.75],inf),
			0.1),
		0.2
	]);
q = Patch({ arg freq=100,amp=1.0;
		SinOsc.ar([freq,freq + 30],0,amp)
	},[	
		StreamKrDur(
			ModalFreq(Pseq([1,2,3,4],inf)), 
			Pseq([0.25,0.5,0.75],inf),
			0.1),
		0.2
	]);
p.play;
q.play;
)

thisProcess.stop;

p.play;
q.play;

(
// Sample
p = Patch({ arg sample;
	PlayBuf.ar(sample.numChannels,
			sample.bufnumIr,
			sample.bufRateScaleKr,
			1.0,
			0.0,
			1.0)

},[
	Sample("a11wlk01.wav")
]);
p.play;
)

p.stop;

p.play;

p.free;

(
Instr([\help,\Sample],{ arg sample,tempo;
	PlayBuf.ar(
		sample.numChannels,
		sample.bufnumKr,
		sample.sampleRateKr / 44100 * tempo * sample.tempo.reciprocal,
		1.0,0.0,1.0);
},[
	\sample,
	\tempo
]);
p = Patch([\help,\Sample],
[
	Sample("a11wlk01.wav"),
	TempoPlayer.new
]);
p.play;
)

p.isPlaying

Tempo.bpm = 150;

Tempo.bpm = 40;

Tempo.bpm = 100;

(
//PlayerMixer
Instr(\helpPlayerMixer, { arg freq=100,amp=1.0;
	SinOsc.ar([freq,freq + 30],0,amp)
});
p= PlayerMixer([
	Patch(\helpPlayerMixer,[
		500,
		0.3
	]),
	Patch(\helpPlayerMixer,[
		450,
		0.3
	]),
	Patch(\helpPlayerMixer,[
		800,
		0.3
	])
]);
p.play;

)
//0.5.wait;
p.stop;
//0.5.wait;
//
p.play;
//

(
Tempo.bpm = 100; // times are in beats
p = Patch({ arg freq;
	Saw.ar(freq,0.2)
},[
	StreamKrDur( 
		Prand(Array.fill(rrand(4,16),{ rrand(20,80).midicps }),inf),
		 0.25,// a float
		 0.1);
]);
p.play;
)
p.stop;

p.play;

// still having problems changing tempo
(
	Instr(\beatClockPlayerTest,{ arg gate=0.0,freq=300;
		var a,t;

		t = gate;
		a = Decay2.kr(t,0.01,0.1).clip2(1.0);
		a * SinOsc.ar(freq);
	},[
		\gate
	]);
	
	PlayerMixer([
		Patch.new(\beatClockPlayerTest,
		[
			BeatClockPlayer(16), // trig / gate 
			300
		]),
		Patch.new(\beatClockPlayerTest,
		[
			Stream2Trig(Pseq([0.25],inf)),
			600
		])
	]).play;
)

(
// children are started
Patch({ arg freq=100,amp=1.0;
		SinOsc.ar([freq,freq + 3],0,amp)
},[StreamKrDur(Pseq([1000,660,470,400,500,700,600,570],inf), Pseq([1],inf),0),0.1])
.play
)

(
// children inside of patch inside of player mixer are started
PlayerMixer([
	Patch({ arg freq=100,amp=1.0;
		SinOsc.ar([freq,freq + 3],0,amp)
	},[StreamKrDur(Pseq([1000,660,470,400,500,700,600,570],inf), Pseq([1.0],inf),0),0.1])
]).play
)

(
PlayerMixer([
	Patch({ arg audio=0,freq=500,rq=0.1;
		RLPF.ar(audio, freq, rq)
	},[
		Patch({ arg freq=100,amp=1.0;
			SinOsc.ar([freq,freq + 3],0,amp)
		},[	
			StreamKrDur(Pseq([1000,660,470,400,500,700,600,570],inf), Pseq([1],inf),0),
			0.01
		]),
	1000,
	0.2]),
//	Patch(\reverb,[
		Patch({ arg freq=100,amp=1.0;
			SinOsc.ar([freq,freq + 3],0,amp)
		},[
			Stream2Trig(Pseq([1],inf),Pseq([0.75,0.25],inf))
		])
//	20])
]).play;
)	

p = PlayerSocket.new;
p.play;

q = Patch({ Saw.ar * 0.2 });
r = Patch({ Saw.ar(800) * 0.2 });

p.prepareAndSpawn(q);

p.prepareAndSpawn(r);

p.release;

(

	Instr([\oscillOrc,	\pmosc],{ arg freq=400, freq2=500,pmindex=0,phasemod=0.0,amp=1.0;
		PMOsc.ar(freq,freq2,pmindex,phasemod,amp)
	});
	
	p = Patch.new([ 'oscillOrc', 'pmosc' ],
	[
		KrNumberEditor(240.06,\freq), 
		27.7707, 
	  	1.3, 
	 	0, 
	  	0.616317
	 ]);
	 
	 q = Patch.new([ 'oscillOrc', 'pmosc' ],
		[
		KrNumberEditor(90,\freq), 
	  	27.7707, 
	  	Patch.new([ 'oscillOrc', 'pmosc' ],
		[
			KrNumberEditor(150.06,\freq), 
			27.7707, 
		  	1.3, 
		 	0, 
		  	0.616317
		 ]), 
	 	0, 
		Patch.new([ 'oscillOrc', 'pmosc' ],
		[
			KrNumberEditor(150.06,\freq), 
			27.7707, 
		  	1.3, 
		 	0, 
		  	0.616317
		 ])
	 ]);
	 
	 r =  Patch.new([ 'oscillOrc', 'pmosc' ], [
		KrNumberEditor(40,\freq), 
	  	nil, 
	  	Patch.new([ 'oscillOrc', 'pmosc' ], [
			KrNumberEditor(150.06,\freq), 
			27.7707, 
		  	1.3, 
		 	0, 
		  	0.616317
		 ]),
	 	0, 
		Patch.new([ 'oscillOrc', 'pmosc' ], [
			KrNumberEditor(150.06,\freq), 
			27.7707, 
		  	1.3, 
		 	0, 
		  	0.616317
		 ])
	 ]);

	 PlayerPool([ p,q,r ]).gui
)

//check that it releases the same busses it allocates, none twice
// the tempo bus stays
(
x = ModalFreq.new(
			3, //StreamKrDur.new(SeqSliders.new('degree',#[ 2, 2, 1 ], 0, 11, 1), 1, 0.1), 
			FloatArray[ 3, 4, 7, 8, 9 ], 
			0, 
			3, 
			12
	);

Patch.new(
	[ 'synths', 'PMOsc', 'FQenv' ], 
	[ BeatClockPlayer.new, 20, 
		x,
		
		 5, 400, 80, 0.3, 0.8, Env.new(
		[ 0, 0.441358, 0.356481, 0 ], 
		[ 0.01, 0.3, 1 ], 
		-4, 
		2, 
		nil
	), 0.0343698, 
	0.2 ]
).gui

)