// TODO: connect MIDI CC knobs to an array of defined pos, also connect amp, possibly rate though probably not


(
b = Buffer.read(s, "/home/lcoogan/snd/live/2025-04-26.Basic_City_Brewery/granular/Cantorial/Cantor_Samuel_Malavsky_Zechor.wav");

(
SynthDef(\cantorial_tremolo, { |out, pan = 0, dur = 0.1, atk=0.3, dec=0.4, sus=0.1, rel=0, amp=1, crf=1100|
	var sig, rate, env, filter;

	env = EnvGen.ar(
		Env.new(
			[0, 1, 1, 0],
			[atk, dec, sus, rel],
		),
		gate: 1,
		doneAction: 2
	);

	sig = GrainBuf.ar(
		numChannels: 2,
		trigger: Impulse.kr(10),
		dur: dur,
		sndbuf: b,
		rate: 1,
		pos: 0.1,
		interp: 2,
		pan: pan,
		envbufnum: -1,
		maxGrains: 512
	);

	sig = sig * env * amp;

	filter = MoogVCF.ar(sig, crf, 0.3);

	Out.ar(out, filter);
}).add;
)
)

(
Ppar([
	Pbind(
		\instrument, \cantorial_trill,
		\dur, 1,   // Adjust as needed
		// \amp, 1,     // Amplitude (adjust as needed)
		\pan, 0,     // Pan position
	),
	Pbind(
		\instrument, \cantorial_tremolo,
		\dur, 0.2,   // Adjust as needed
		// \amp, 1,     // Amplitude (adjust as needed)
		\pan, 0,     // Pan position for the second synth (can be changed)
	)
]).play;
)




(
// Becoming satisfactory granular synth

b = Buffer.read(s, "/home/lcoogan/snd/live/2025-04-26.Basic_City_Brewery/granular/Cantorial/Cantor_Samuel_Malavsky_Zechor.wav");

(
SynthDef(\cantorial_trill, { |out, pan = 0, dur = 0.5, atk=0.3, dec=0.4, sus=0.1, rel=0, amp=1, crf=1000, grainRate=5|
	var sig, rate, env, filt;

	env = EnvGen.ar(
		Env.new(
			[0, 1, 1, 0],
			[atk, dec, sus, rel],
		),
		gate: 1,
		doneAction: 2
	);

	sig = GrainBuf.ar(
		numChannels: 2,
		trigger: Impulse.kr(grainRate),
		dur: dur,
		sndbuf: b,
		rate: 1,
		pos: 0.01,
		interp: 2,
		pan: pan,
		envbufnum: -1,
		maxGrains: 512
	);

	sig = sig * env * amp;

	sig = LPF.ar(sig, crf);

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


(
// IMPORTANT!

b = Buffer.read(s, "/home/lcoogan/snd/live/2025-04-26.Basic_City_Brewery/granular/Cantorial/Acheinu_Kol_Beit_Israel.wav");

b = Buffer.readChannel(s, "/Users/eli/Sounds/scaudio/loops/5_128-170/135evrythngCHILL-8.aiff", channels:[0]);
)

(
s.newBusAllocators;
~posbus = Bus.control(s, 1).value_(0.25);
~grainbus = Bus.audio(s, 2);

SynthDef(\cantorial_reed ,
	{
		arg amp = 0.3, pan = 0, posin = 0, out = 0;
		var sig, pos;
		pos = In.kr(posin, 1);
		sig = GrainBuf.ar(
			numChannels: 2,
			trigger: Dust.ar(40),
			dur: 0.8,
			sndbuf: b,
			rate: 1,
			pos: pos, // 1.25, 1.20
			interp: 2,
			pan: pan,
			envbufnum: -1,
			maxGrains: 512
		);

		sig = sig * amp;
		Out.ar(out, sig);
}).add;

MIDIdef.cc(\pos, {
	arg val, num;
	var posval = [0.25, 0.2, 0.7], index, out;
	index = val.linlin(0, 128, 0, posval.size).floor.asInteger;
	out = posval[index].postln;
	~posbus.value_(out);
}, [95]);
)

x = Synth(\cantorial_reed, [amp: 0.1, posin: ~posbus, out: ~grainbus]);

x.set(\pos, 0.6)

x.free

(
s.newBusAllocators;
~grainbus = Bus.audio(s, 2);
~tr808bus = Bus.audio(s, 2);
~padbus = Bus.audio(s, 2);
SynthDef(\master, {
	arg grainamp = 1, tr808amp = 1, padamp = 1, out = 0;
	var grainsig, tr808sig, padsig, sig;
	grainsig = In.ar(\grainbus.ir(~grainbus), 2) * grainamp;
	tr808sig = In.ar(\tr808bus.ir(~tr808bus), 2) * tr808amp;
	padsig = In.ar(\padbus.ir(~padbus), 2) * padamp;
	sig = [grainsig, tr808sig, padsig].sum;
	Out.ar(out, sig);
}).add;
)






