// Granular leftovers from granular_EF-edit.scd


// Transposition. Doesn't sound good, not readily controllable from an artistic perspective.

~klezmer = Buffer.readChannel(s, "/home/lcoogan/snd/live/2025-04-26.Basic_City_Brewery/granular/Cantorial/Acheinu_Kol_Beit_Israel.wav", channels:[0]);

(
x = {
	arg amp = 0.1;
	var sig;
	sig = GrainBuf.ar(
		numChannels: 2,
		trigger: Impulse.kr(1),
		dur: 1,
		sndbuf: ~klezmer,
		rate: \trnsp.kr(0).midiratio,
		pos: 0.1,
		interp: 2,
		pan: 0,
		envbufnum: -1,
		maxGrains: 512
	);
	sig = sig;
}.play;
)

x.set(\trnsp, 20)
x.free;



(
// MIDI

MIDIClient.init;
MIDIIn.connectAll;
)

(
~notes = Array.newClear(128);


// Handle MIDI note on (when a note is pressed)
MIDIdef.noteOn(\noteOnTest, {
    arg vel, nn, chan, src;
    // Post the velocity and note for debugging
    // [vel, nn].postln;

    // Create a Synth for the note with the correct argument names
    ~notes[nn] = Synth.new(
        \granular,
        [
			\note, nn,  // Use \note, not \freq
            \amp, vel.linexp(1, 127, 0.01, 0.3),  // Map velocity to amplitude
            \gate, 1,  // Trigger the envelope
        ]
    );
});

// Handle MIDI note off (when a note is released)
MIDIdef.noteOff(\noteOffTest, {
    arg vel, nn;
    // Set gate to 0 to stop the sound
    ~notes[nn].set(\gate, 0);
    // Clear the reference to the Synth object for the note
    ~notes[nn] = nil;
});
)


(
~granularSynth = Synth(\granular, [
	\note, 60,  // C4
	// \dur, 1,
    \pan, 0
]);
)


(
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=1200|
    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);
}).play;
)
)
