MIDIClient.init;
MIDIIn.connectAll;

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


// 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("/Users/eli/Desktop/2025-04-26.Basic_City_Brewery (Copy)/trax/01-borrowed flesh/seq/808/").fullPath;

~samples = Dictionary.newFrom(
    (
        42: "ch.wav",  46: "oh.wav",  39: "cp.wav",  38: "sd.wav",  36: "bd.wav"
    ).collect { |file, key|
        key -> Buffer.read(s, ~sampleDir +/+ file)
    }
);
)

~samples.class
~samples[39].class

(
~sampleDir = PathName("/Users/eli/Desktop/2025-04-26.Basic_City_Brewery (Copy)/trax/01-borrowed flesh/seq/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).lag(0.01);
    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);  // Send to audio output
}).add;
)

(
s.newBusAllocators;
~bdampbus = Bus.control(s, 1).value_(0.5);
~sdampbus = Bus.control(s, 1).value_(0.5);
~cpampbus = Bus.control(s, 1).value_(0.5);
~champbus = Bus.control(s, 1).value_(0.5);
~ohampbus = Bus.control(s, 1).value_(0.5);
MIDIIn.connectAll;
MIDIdef.noteOn('808trig', { |vel, nn|
	nn.postln;
	switch(nn)
	{36} {
		Synth(\sampleTrigger, [buf: ~sample.bd, ampin: ~bdampbus]);
	}
	{38} {
		Synth(\sampleTrigger, [buf: ~sample.sd, ampin: ~sdampbus]);
	}
	{39} {
		Synth(\sampleTrigger, [buf: ~sample.cp, ampin: ~cpampbus]);
	}
	{42} {
		Synth(\sampleTrigger, [buf: ~sample.ch, ampin: ~champbus]);
	}
	{46} {
		Synth(\sampleTrigger, [buf: ~sample.oh, ampin: ~ohampbus]);
	}
}, noteNum: [36, 38, 39, 42, 46], chan: [0]);

MIDIdef.cc('808amp', { |val, num|
	switch(num)
	{22} {~bdampbus.value = val / 127}
	{23} {~sdampbus.value = val / 127}
	{61} {~cpampbus.value = val / 127}
	{24} {~champbus.value = val / 127}
	{26} {~ohampbus.value = val / 127};

}, ccNum: [22, 23, 61, 24, 26], chan: [0]);
)
MIDIFunc.trace(false)

s.scope;





// Testing ~samples



(
// For some reason, nn and vel are swapped on the tr-08 MIDIIn
// if vel isn't in the enclosure, then nn doesn't work
// still figuring out how to do polyphony
// set it to if == 9 when everything else is working
MIDIdef.noteOn(\noteOnTest, {|vel, nn|
	[nn].postln;

	Synth(\sampleTrigger, [\buf, ~samples[nn]]);
});
)








(
// Define the Synth for sample triggering (if not defined already)
SynthDef(\sampleTrigger, { |buf|
    var sound;
    sound = PlayBuf.ar(1, buf, doneAction: 2);  // doneAction: 2 will free the synth when it's done
    Out.ar(0, sound);  // Send to audio output
}).add;
)



// Manually trigger a sample for note number 42
(
var noteNumber = 39;  // The MIDI note number to play
if (~samples.includesKey(noteNumber), {
    ("Triggering sample for note: " + noteNumber).postln;
    Synth(\sampleTrigger, [\buf, ~samples[noteNumber]]);
}, {
    ("No sample found for note: " + noteNumber).postln;
});
)