b = Buffer.read(s, "/home/lcoogan/snd/live/2025-04-26.Basic_City_Brewery/trax/01-Cantor_Samual_Malavsky_Shomea_Kol_Bichios.wav");
b.play;



(
// PV_BrickWall (CC 0-3)
SynthDef(\pv_brickwall, { |amp = 0.5, mod1 = 10, mod2 = 0.1, mod3, mod4, mod5|
    var sig = PlayBuf.ar(1, b, BufRateScale.kr(b), loop: 1);
    var chain = FFT(LocalBuf(2048), sig);
    chain = PV_BrickWall(chain, SinOsc.ar(mod1) + SinOsc.kr(mod2));
	chain = PV_RectComb(chain, mod3, mod4, mod5);
    sig = IFFT(chain.dup);
    /*sig = Splay.ar(sig, mod3);
    sig = CombL.ar(sig, mod3, mod3, mod3);*/
    Out.ar(0, sig * amp);
}).add;



// PV_RectComb (CC 20-23)
SynthDef(\pv_rectcomb, { |amp = 0.5, mod1 = 0, mod2 = 0, mod3 = 0.2|
    var sig = PlayBuf.ar(1, b, BufRateScale.kr(b), loop: 1);
    var chain = FFT(LocalBuf(2048), sig);
    chain = PV_RectComb(chain, mod1, mod2, mod3);
    Out.ar(0, IFFT(chain).dup * amp);
}).add;



// PV_MagSmear (CC 16-19)
SynthDef(\pv_magsmear, { |amp = 0.5, mod1 = 0|
    var sig = PlayBuf.ar(1, b, BufRateScale.kr(b), loop: 1);
    var chain = FFT(LocalBuf(1024), sig);
    chain = PV_MagSmear(chain, mod1);
    Out.ar(0, IFFT(chain).dup * amp);
}).add;



// PV_Freeze (CC 4-7)
SynthDef(\pv_freeze, { |amp = 0.5, mod1 = 10|
    var sig = PlayBuf.ar(1, b, BufRateScale.kr(b), loop: 1);
    var chain = FFT(LocalBuf(1024), sig);
	chain = PV_Freeze(chain, SinOsc.kr(mod1) > 0.5);
    Out.ar(0, IFFT(chain).dup * amp);
}).add;



// PV_MagGate (CC 8-11)
SynthDef(\pv_maggate, { |amp = 0.5, mod1 = 50, mod2 = 0.5, mod3 = 0|
    var sig = PlayBuf.ar(1, b, BufRateScale.kr(b), loop: 1);
    var chain = FFT(LocalBuf(1024), sig);
    chain = PV_MagGate(chain, mod1, mod2);
	chain = PV_MagShift(chain, 1, mod3);
    Out.ar(0, IFFT(chain).dup * amp);
}).add;



// PV_MagShift (CC 12-15)
SynthDef(\pv_magshift, { |amp = 0.5, mod1 = 0|
    var sig = PlayBuf.ar(1, b, BufRateScale.kr(b), loop: 1);
    var chain = FFT(LocalBuf(1024), sig);
    Out.ar(0, IFFT(chain).dup * amp);
}).add;
)









(
~midiControlMap_BrickWall = (
    00: \amp,
    01: \mod1,
    02: \mod2,
	03: \mod3,
	04: \mod4,
	05: \mod5
);

~midiControlMap_RectComb = (
	04: \amp,
	05: \mod1,
	06: \mod2,
	07: \mod3
);
~midiControlMap_MagSmear = (
    08: \amp,
    09: \mod1,
);
~midiControlMap_Freeze = (
	10: \amp,
	11: \mod1
);
~midiControlMap_MagGate = (
	12: \amp,
	13: \mod1,
	14: \mod2,
	15: \mod3
);
)

// // Your pv_brickwall synth
// (11
// ~synth_BrickWall = Synth(\pv_brickwall, [\bufnum, b]);
// ~synth_MagSmear = Synth(\pv_magsmear, [\bufnum, b]);
// ~synth_RectComb = Synth(\pv_rectcomb, [\bufnum, b]);
// ~synth_MagGate = Synth(\pv_maggate, [\bufnum, b]);
// )
//
// ~synth_BrickWall = Synth(\pv_brickwall, [\bufnum, b]);


(
s.bind {
    ~synth_BrickWall = Synth(\pv_brickwall, [\bufnum, b]);
    ~synth_MagSmear = Synth(\pv_magsmear, [\bufnum, b]);  // Check name
    ~synth_RectComb = Synth(\pv_rectcomb, [\bufnum, b]);     // Make sure this SynthDef exists
	~synth_MagGate = Synth(\pv_maggate, [\bufnum, b]);
};
)



(
// Connect MIDI CCs for pv_brickwall
~midiControlMap_BrickWall.keysValuesDo { |cc, param|
    MIDIFunc.cc({ |val, num, chan, src|
        if (chan == 2) {  // MIDI channel 2
            var mappedVal;
            mappedVal = case
            { param == \amp } { val.linlin(0, 127, 0.0, 1.0) }
            { param == \mod1 } { val.linlin(0, 127, 1.0, 20.0) }
            { param == \mod2 } { val.linlin(0, 127, 1.0, 20.0) }
            { param == \mod3} { val.linlin(0, 127, 0.0, 1.0) }
		/*	{ param == \mod4} { val.linlin(0, 127, 0.0, 1.0) }
			{ param == \mod5} { val.linlin(0, 127, 0.0, 1.0) }*/
            { val / 127 };  // default fallback
            ~synth_BrickWall.set(param, mappedVal);
            ("CC" ++ cc ++ " (" ++ param ++ ") → " ++ mappedVal).postln;
        }
    }, cc);
};


// // Connect MIDI CCs for pv_brickwall
~midiControlMap_RectComb.keysValuesDo { |cc, param|
	MIDIFunc.cc({ |val, num, chan, src|
		if (chan == 2) {  // MIDI channel 2
			var mappedVal;
			mappedVal = case
			{ param == \amp } { val.linlin(0, 127, 0.0, 1.0) }
			{ param == \mod1 } { val.linlin(0, 127, 1.0, 20.0) }
			{ param == \mod2 } { val.linlin(0, 127, 1.0, 20.0) }
			{ param == \mod3} { val.linlin(0, 127, 0.0, 1.0) }
			{ val / 127 };  // default fallback
			~synth_RectComb.set(param, mappedVal);
			("CC" ++ cc ++ " (" ++ param ++ ") → " ++ mappedVal).postln;
		}
	}, cc);
};

// Connect MIDI CCs for pv_pv_magsmear
~midiControlMap_MagSmear.keysValuesDo { |cc, param|
	MIDIFunc.cc({ |val, num, chan, src|
		if (chan == 2) {  // MIDI channel 2
			var mappedVal;
			mappedVal = case
			{ param == \amp } { val.linlin(0, 127, 0.0, 1.0) }
			{ param == \mod1 } { val.linlin(0, 127, 0.0, 40.0) }
			{ val / 127 };  // default fallback
			~synth_MagSmear.set(param, mappedVal);
			("CC" ++ cc ++ " (" ++ param ++ ") → " ++ mappedVal).postln;
		}
	}, cc);
};


~midiControlMap_MagGate.keysValuesDo { |cc, param|
	MIDIFunc.cc({ |val, num, chan, src|
		if (chan == 2) {  // MIDI channel 2
			var mappedVal;
			mappedVal = case
			{ param == \amp } { val.linlin(0, 127, 0.0, 1.0) }
			{ param == \mod1 } { val.linlin(0, 127, 0.0, 40.0) }
			{ val / 127 };  // default fallback
			~synth_MagGate.set(param, mappedVal);
			("CC" ++ cc ++ " (" ++ param ++ ") → " ++ mappedVal).postln;
		}
	}, cc);
};
)


MIDIClient.init;
MIDIIn.connectAll;











