Skip to content

Instantly share code, notes, and snippets.

@john-d-murphy
Created July 13, 2020 10:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save john-d-murphy/45ea005836aae78185e1ea04465402b6 to your computer and use it in GitHub Desktop.
Save john-d-murphy/45ea005836aae78185e1ea04465402b6 to your computer and use it in GitHub Desktop.
APCMini {
// UID
var uid;
// Control Surface
var <faders, <pads, <buttons;
// CC = Control Change / MN = Midi Note
var ctls, midiOut, ccFaders, mnPads, mnShift;
// Colors
var off = 0;
var green = 1;
var greenBlink = 2;
var red = 3;
var redBlink = 4;
var orange = 5;
var orangeBlink = 6;
*findAPCUid { |deviceNumber|
var sources = MIDIClient.sources, uid = -1 , deviceCount = 1;
sources.do { |source|
if (source.device.matchRegexp("^APC MINI")) {
if (deviceNumber == deviceCount) {
uid=source.uid;
}
};
};
if (uid == -1) {
"APC MINI not connected".postln;
};
^uid;
}
// If no UID is specified, UID will be nil, and the messages will apply to all controllers.
// Adding this as an option instead of explicitly calling the convenience method, because it's possible to
// have more than one APC MINI, and we want to allow for that.
*new {|uid|
^super.newCopyArgs(uid).init() }
init {
ctls = ();
faders = List[];
buttons = List[];
pads = List[];
ccFaders = (48..56);
mnPads = (0..63);
mnShift = 98;
//midiOut = MIDIOut.newByName("APC MINI", "APC MINI MIDI 1");
midiOut = MIDIOut(0, MIDIClient.destinations[1].uid); // TODO: find this properly
this.assignCtls();
}
assignCtls {
ccFaders.do {|cc, i|
var key = ("fader" ++ (i+1)).asSymbol;
var fader = APCMiniFader(key, cc, uid);
faders.add(fader);
ctls.put(key, fader);
};
mnPads.do {|mn, i|
var row, column, key, pad;
row = (i / 8).asInteger;
column = i % 8;
key = ("pad" ++ (row + 1) ++ "_" ++ (column + 1)).asSymbol;
pad = APCMiniButton(key, mn, uid, midiOut);
pads.add(pad);
ctls.put(key, pad);
}
}
}
APCMiniFader {
var key, cc, uid;
var state = 0, def;
*new {|key, cc, uid|
^super.newCopyArgs(("apcmini_" ++ key).asSymbol, cc, uid);
}
onCC_ {|func|
def = MIDIdef.cc(key, func, cc, 0, uid);
}
free {
def.free;
}
}
APCMiniButton {
var key, mn, uid, midiOut;
var state = 0, noteOnDef, noteOffDef;
*new {|key, mn, uid, midiOut|
^super.newCopyArgs(("apcmini_" ++ key).asSymbol, mn, uid, midiOut);
}
onNoteOn_ {|func|
noteOnDef = MIDIdef.noteOn(key ++ "_on", func, mn, 0, uid);
}
onNoteOff_ {|func|
noteOffDef = MIDIdef.noteOff(key ++ "_off", func, mn, 0, uid);
}
setColor {|color|
("% %\n").postf(mn, color);
midiOut.noteOn(0, mn, color);
}
free {
noteOnDef.free;
noteOffDef.free;
}
}
(
MIDIClient.init;
MIDIIn.connectAll;
m = MIDIOut(0);
~id = APCMini.findAPCUid(1);
~id.postln;
a = APCMini.new(~id) ; a.pads; a.buttons; a.faders;
a.faders.do {|fader, i| fader.onCC = {|val| "This is fader % its value is %\n".postf(i+1, val); }};
a.pads.do {|pad, i|
pad.onNoteOn = {|val|
"NoteOn: This is pad % its value is %\n".postf(i+1, val);
pad.setColor(1);
};
pad.onNoteOff = {|val|
"NoteOff: This is pad % its value is %\n".postf(i+1, val);
pad.setColor(0);
};
};
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment