MIDIClient.init;
MIDIIn.connectAll;

MIDIdef.noteOn(\noteOnTest, {"key down".postln});P


// Print MIDI debug info

(
MIDIdef.noteOn(\noteOnTest, {
	arg nn, chan, src;
	[nn, chan, src].postln;
});


MIDIdef.cc(\ccTest, {
	arg val, num, chan, src;
	["CC:", num, "Value:", val, "Channel:", chan, "Source:", src].postln;
});
)


// Load samples

(
~sampleDir = PathName("/home/lcoogan/snd/live/2025-04-26.Basic_City_Brewery/seq/borrowed_flesh/808").fullPath;

~samples = Dictionary.newFrom(
    (
        42: "ch.wav",  46: "oh.wav",  49: "cy.wav",
        56: "cb.wav",  39: "cp.wav",  37: "rs.wav",
        50: "ht.wav",  47: "mt.wav",  43: "lt.wav",
        38: "sd.wav",  36: "bd.wav",  70: "ma.wav",
        75: "cl.wav",  62: "hc.wav",  63: "mc.wav",
        64: "lc.wav"

    ).collect { |file, key|
        key -> Buffer.read(s, ~sampleDir +/+ file)
    }
);
)



~samples.class
~samples[39].class

(
~sampleDir = PathName("/home/lcoogan/snd/live/2025-04-26.Basic_City_Brewery/seq/borrowed_flesh/808");
~sample = ();
~sampleDir.entries.do({ |pn|
	var sym;
	sym = pn.fileNameWithoutExtension.asSymbol;
	~sample[sym] = Buffer.read(s, pn.fullPath);
});
)

(
// Define the Synth for sample triggering (if not defined already)
SynthDef(\sampleTrigger, { |buf, ampin = 0|
    var sound, amp;
	amp = In.kr(ampin, 1);
    sound = PlayBuf.ar(1, buf, BufRateScale.ir(buf), doneAction: 2);  // doneAction: 2 will free the synth when it's done
	sound = sound * amp;
    Out.ar(0, sound.dup);
}).add;
)


 /*
        75: "cl.wav",  62: "hc.wav",  63: "mc.wav",
        64: "lc.wav"*/

(
s.newBusAllocators;
~bdampbus = Bus.control(s, 1); // adding .value(0.5) sets an initial of 0.5
~sdampbus = Bus.control(s, 1);
~ltampbus = Bus.control(s, 1);
~mtampbus = Bus.control(s, 1);
~htampbus = Bus.control(s, 1);
~rsampbus = Bus.control(s, 1);
~cpampbus = Bus.control(s, 1);
~champbus = Bus.control(s, 1);
~ohampbus = Bus.control(s, 1);
~cyampbus = Bus.control(s, 1);
~cbampbus = Bus.control(s, 1);

MIDIIn.connectAll;
MIDIdef.noteOn('808trig', { |vel, nn|
	// nn.postln;
	switch(nn)
	{36} {
		Synth(\sampleTrigger, [buf: ~sample.bd, ampin: ~bdampbus]);
	}
	{37} {
		Synth(\sampleTrigger, [buf: ~sample.rs, ampin: ~rsampbus]);
	}
	{38} {
		Synth(\sampleTrigger, [buf: ~sample.sd, ampin: ~sdampbus]);
	}
	{39} {
		Synth(\sampleTrigger, [buf: ~sample.cp, ampin: ~cpampbus]);
	}
	{42} {
		Synth(\sampleTrigger, [buf: ~sample.ch, ampin: ~champbus]);
	}
	{43} {
		Synth(\sampleTrigger, [buf: ~sample.lt, ampin: ~ltampbus]);
	}
	{46} {
		Synth(\sampleTrigger, [buf: ~sample.oh, ampin: ~ohampbus]);
	}
	{47} {
		Synth(\sampleTrigger, [buf: ~sample.mt, ampin: ~mtampbus]);
	}
	{49} {
		Synth(\sampleTrigger, [buf: ~sample.cy, ampin: ~cyampbus]);
	}
	{50} {
		Synth(\sampleTrigger, [buf: ~sample.ht, ampin: ~htampbus]);
	}
	{56} {
		Synth(\sampleTrigger, [buf: ~sample.cb, ampin: ~cbampbus]);
	}
	{62} {
		Synth(\sampleTrigger, [buf: ~sample.hc, ampin: ~hcampbus]);
	}
	{63} {
		Synth(\sampleTrigger, [buf: ~sample.mc, ampin: ~mcampbus]);
	}
	{64} {
		Synth(\sampleTrigger, [buf: ~sample.lc, ampin: ~lcampbus]);
	}
	{70} {
		Synth(\sampleTrigger, [buf: ~sample.ma, ampin: ~maampbus]);
	}
	{75} {
		Synth(\sampleTrigger, [buf: ~sample.cl, ampin: ~clampbus]);
	}
}, noteNum: [36, 38, 39, 42, 43, 46, 47, 49, 50, 56, 62, 63, 64, 70, 75], chan: [1]);

MIDIdef.cc('808amp', { |val, num|
	switch(num)
	{24} {~bdampbus.value = val / 127}
	{29} {~sdampbus.value = val / 127}
	{48} {~ltampbus.value = val / 127}
	{51} {~mtampbus.value = val / 127}
	{54} {~htampbus.value = val / 127}
	{57} {~rsampbus.value = val / 127}
	{60} {~cpampbus.value = val / 127}
	{63} {~champbus.value = val / 127}
	{82} {~ohampbus.value = val / 127}
	{85} {~cyampbus.value = val / 127}
	{88} {~cbampbus.value = val / 127};

}, ccNum: [24, 29, 48, 51, 54, 57, 60, 63, 82, 85, 88], chan: [1]);
)
MIDIFunc.trace(true)