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("/home/lcoogan/snd/live/2025-04-26.Basic_City_Brewery/trax/01-borrowed flesh/seq/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)
    }
);
)


// 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;
});
)





MIDIFunc.trace;



