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

MIDIClient.init;
MIDIIn.connectAll;




(
// PV_BrickWall (CC 0-3)
SynthDef(\pv_brickwall, { |amp = 0.5, mod1 = 10, mod2 = 0.1, mod3 = 0|
    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));
	sig = IFFT(chain.dup);
	sig = Splay.ar(sig, mod3);
	// sig = FreeVerb.ar(sig, mod3, mod3);
	sig = CombL.ar(sig, mod3, mod3, mod3);
    Out.ar(0, sig * amp);
}).add;
)

(
~midiControlMap = (
	00: \amp,
	01: \mod1,
	02: \mod2,
	03: \mod3
);

// Your synth
~synth = Synth(\pv_brickwall, [\bufnum, b]);

// Connect MIDI CCs on channel 2 (0-based index, so channel 2 = 1)
~midiControlMap.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.set(param, mappedVal);
			("CC" ++ cc ++ " (" ++ param ++ ") → " ++ mappedVal).postln;
		}
	}, cc);
};
)

(

(
// PV_MagSmear (CC 16-19)
SynthDef(\pv_pv_magsmear, { |amp = 0.5, mod1 = 0, mod2 = 0, mod3 = 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;
)

(
~midiControlMap = (
	04: \amp,
	05: \mod1,
);

// Your synth
~synth = Synth(\pv_pv_magsmear, [\bufnum, b]);

// Connect MIDI CCs on channel 2 (0-based index, so channel 2 = 1)
~midiControlMap.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.set(param, mappedVal);
			("CC" ++ cc ++ " (" ++ param ++ ") → " ++ mappedVal).postln;
		}
	}, cc);
};
)





(
// PV_Freeze (CC 4-7)
SynthDef(\pv_freeze, { |amp = 0.5, mod1 = 10, mod2 = 0, mod3 = 0|
    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_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);
    Out.ar(0, IFFT(chain).dup * amp);
}).add;
)

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







(
// PV_RectComb (CC 20-23)
SynthDef(\pv_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;
)

