// 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
    );

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

	sig = sig * env * amp * filter;

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


(
// 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.02,
        interp: 2,
        pan: pan,
        envbufnum: -1,
        maxGrains: 512
    );

    sig = sig * env * amp;

	sig = LPF.ar(sig, crf);

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


(
Ppar([
    Pbind(
        \instrument, \cantorial_trill,
		\dur, 0.1,
		\amp, 0.5,
        \pan, 0,
    ),
    Pbind(
        \instrument, \cantorial_tremolo,
		\dur, 0.1,
		\amp, 1,
        \pan, 0,
    )
]).play;
)





(
// Pairs with Olson.scd

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

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

    sig = sig * amp;
	Out.ar(0, sig);
}).play;
)
)

Synth(\cantorial_reed);






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

SynthDef(\cantorial_reed,
{
    arg amp = 0.3, pan = 0, pos = 0, bufSwitch = 0;
    var sig, buf;

    // Choose buffer based on bufSwitch (0 or 1)
    buf = Select.kr(bufSwitch, [b, c]);

    sig = GrainBuf.ar(
        numChannels: 2,
        trigger: Dust.ar(40),
        dur: 0.8,
        sndbuf: buf, // Dynamically selected buffer
        rate: 1,
        pos: 0.25,
        interp: 2,
        pan: pan,
        envbufnum: -1,
        maxGrains: 512
    );

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


// Example: Play the synth and switch between buffers
x = Synth(\cantorial_reed, [\bufSwitch, 0]); // Starts with b1
x.set(\bufSwitch, 1); // Switches to b2



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

SynthDef(\cantorial_reed,
{
    arg amp = 0.3, pan = 0, pos = 0, xfade = 0; // 'xfade' controls blending (0 = only b, 1 = only c)
    var sig1, sig2, sig;

    sig1 = GrainBuf.ar(
        numChannels: 2,
        trigger: Dust.ar(40),
        dur: 0.8,
        sndbuf: b,
        rate: 1,
        pos: 0.25,
        interp: 2,
        pan: pan,
        envbufnum: -1,
        maxGrains: 512
    );

    sig2 = GrainBuf.ar(
        numChannels: 2,
        trigger: Dust.ar(40),
        dur: 0.8,
        sndbuf: c,
        rate: 1,
        pos: 0.25,
        interp: 2,
        pan: pan,
        envbufnum: -1,
        maxGrains: 512
    );

    // Crossfade between the two signals
    sig = XFade2.ar(sig1, sig2, (xfade * 2) - 1); // -1 = sig1, 1 = sig2

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

// Play and smoothly fade between buffers
x = Synth(\cantorial_reed, [\xfade, 0]); // Start with buffer b
x.set(\xfade, 0.5); // Midpoint mix
x.set(\xfade, 0); // Fully buffer c
x.free;

