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

~samples.class;
~samples[39].class;

(
~sampleDir = PathName("/home/lcoogan/snd/samples/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 with amp bus and drum bus
(
SynthDef(\sampleTrigger, { |buf, ampin = 0, outbus = 0|
	var sound, amp;
	amp = In.kr(ampin, 1);
	sound = PlayBuf.ar(2, buf, BufRateScale.ir(buf), doneAction: 2);
	sound = sound * amp;
	Out.ar(outbus, sound.dup);
}).add;
)


(
s.newBusAllocators;

// Create amp buses
~bdampbus = Bus.control(s, 1); ~bdampbus.set(0.8);
~sdampbus = Bus.control(s, 1); ~sdampbus.set(0.8);
~ltampbus = Bus.control(s, 1); ~ltampbus.set(0.8);
~mtampbus = Bus.control(s, 1); ~mtampbus.set(0.8);
~htampbus = Bus.control(s, 1); ~htampbus.set(0.8);
~rsampbus = Bus.control(s, 1); ~rsampbus.set(0.8);
~cpampbus = Bus.control(s, 1); ~cpampbus.set(0.8);
~champbus = Bus.control(s, 1); ~champbus.set(0.8);
~ohampbus = Bus.control(s, 1); ~ohampbus.set(0.8);
~cyampbus = Bus.control(s, 1); ~cyampbus.set(0.8);
~cbampbus = Bus.control(s, 1); ~cbampbus.set(0.8);

~drumAmpBus = Bus.control(s, 1); ~drumAmpBus.set(0.8);
~drumBus = Bus.audio(s, 2);

// Create drum output synth
SynthDef(\drumOut, {
	var sound = In.ar(~drumBus, 2);
	var amp = In.kr(~drumAmpBus, 1);
	Out.ar(0, sound * amp);
}).add;

// s.sync;
Synth(\drumOut);

MIDIdef.noteOn('808trig', { |vel, nn|
	switch(nn)
	{36} { Synth(\sampleTrigger, [buf: ~sample.bd, ampin: ~bdampbus, outbus: ~drumBus]); }
	{37} { Synth(\sampleTrigger, [buf: ~sample.rs, ampin: ~rsampbus, outbus: ~drumBus]); }
	{38} { Synth(\sampleTrigger, [buf: ~sample.sd, ampin: ~sdampbus, outbus: ~drumBus]); }
	{39} { Synth(\sampleTrigger, [buf: ~sample.cp, ampin: ~cpampbus, outbus: ~drumBus]); }
	{42} { Synth(\sampleTrigger, [buf: ~sample.ch, ampin: ~champbus, outbus: ~drumBus]); }
	{43} { Synth(\sampleTrigger, [buf: ~sample.lt, ampin: ~ltampbus, outbus: ~drumBus]); }
	{46} { Synth(\sampleTrigger, [buf: ~sample.oh, ampin: ~ohampbus, outbus: ~drumBus]); }
	{47} { Synth(\sampleTrigger, [buf: ~sample.mt, ampin: ~mtampbus, outbus: ~drumBus]); }
	{49} { Synth(\sampleTrigger, [buf: ~sample.cy, ampin: ~cyampbus, outbus: ~drumBus]); }
	{50} { Synth(\sampleTrigger, [buf: ~sample.ht, ampin: ~htampbus, outbus: ~drumBus]); }
	{56} { Synth(\sampleTrigger, [buf: ~sample.cb, ampin: ~cbampbus, outbus: ~drumBus]); }
}, noteNum: [36, 38, 39, 42, 43, 46, 47, 49, 50, 56], chan: [1]);

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

MIDIdef.cc('drumBusAmp', { |val|
	~drumAmpBus.set(val / 127);
	["808 volume", val/127].postln;
}, ccNum: 0, chan: 1);
)
MIDIFunc.trace(true);