Skip to content

Instantly share code, notes, and snippets.

@benlmyers
Created March 3, 2020 14:35
Show Gist options
  • Save benlmyers/940596b351b7a64d4363e064c4a009fd to your computer and use it in GitHub Desktop.
Save benlmyers/940596b351b7a64d4363e064c4a009fd to your computer and use it in GitHub Desktop.
PadKit for Processing. Use Processing with your Novation Launchpad MKII.
import themidibus.*;
import javax.sound.midi.MidiMessage;
MidiBus myBus;
PKColor padColor;
PKManager pm;
// PadKit, PKManager and all subclasses in this project are owned by Ben Myers.
// Project initated 11/19/2018
// Latest update 11/21/2018
// Current version 1.2
// PKManager creates points and shapes on the Launchpad.
// Make sure to run definePadArrays() in setup().
// midiMessage(message, timestamp, bus_name) is called whenever a button is pressed on the Launchpad. You can put handlers inside that function.
String launchpadName;
Pad[] padList = new Pad[99];
Pad[][] padGrid;
int tick = 0;
int tick2 = 5;
private void definePadArrays() {
int counter = 11;
for(int i = 0; i < 94; i++) {
padList[i] = new Pad(i);
while(counter % 10 != 0) counter++;
}
padList[91] = new Pad(104);
padList[92] = new Pad(105);
padList[93] = new Pad(106);
padList[94] = new Pad(107);
padList[95] = new Pad(108);
padList[96] = new Pad(109);
padList[97] = new Pad(110);
padList[98] = new Pad(111);
}
PKCoordinate touchCoordinate;
void midiMessage(MidiMessage message, long timestamp, String bus_name) {
int note = (int)(message.getMessage()[1] & 0xFF);
int vel = (int)(message.getMessage()[2] & 0xFF);
if(note <= 89) {
touchCoordinate = new PKCoordinate(note % 10 - 1, floor(note / 10) - 1);
} else {
touchCoordinate = new PKCoordinate(note - 104, 8);
}
print("Pad touched");
for(int i = 0; i < padList.length; i++) {
if(padList[i].noteAssignment == note) {
if(vel == 0) { padList[i].isPressed = false; padList[i].padTouchUp(); }
else { padList[i].isPressed = true; padList[i].padTouchDown(); }
}
}
//if(vel == 0) padReleased(note);
//else padTouched(note, vel);
}
void testLights() {
padList[tick].on(tick2, (floor(random(0,2)) == 0));
padList[tick].on(tick2);
tick += 1;
if(tick >= 99) { tick = 0; tick2 += 4; }
if(tick2 > 57) {
tick2 = 5;
}
}
class PKColor {
int colorNum = 3;
PKColor(String colorName) {
int toAdd = 0;
/*if(colorName.substring(0,4).equalsIgnoreCase("DARK")) toAdd = 1;
if(colorName.substring(0,6).equalsIgnoreCase("DARKER")) toAdd = 2;
if(colorName.substring(0,5).equalsIgnoreCase("LIGHT")) toAdd = -1;*/
if(colorName.equalsIgnoreCase("DARKGRAY")) colorNum = 1;
if(colorName.equalsIgnoreCase("GRAY")) colorNum = 2;
if(colorName.equalsIgnoreCase("WHITE")) colorNum = 3;
if(colorName.equalsIgnoreCase("RED")) colorNum = 5 + toAdd;
if(colorName.equalsIgnoreCase("ORANGE")) colorNum = 9 + toAdd;
if(colorName.equalsIgnoreCase("YELLOW")) colorNum = 13 + toAdd;
if(colorName.equalsIgnoreCase("CHARTREUSE")) colorNum = 17 + toAdd;
if(colorName.equalsIgnoreCase("LIME")) colorNum = 21 + toAdd;
if(colorName.equalsIgnoreCase("GREEN")) colorNum = 25 + toAdd;
if(colorName.equalsIgnoreCase("AQUA")) colorNum = 29 + toAdd;
if(colorName.equalsIgnoreCase("TEAL")) colorNum = 33 + toAdd;
if(colorName.equalsIgnoreCase("SKY")) colorNum = 37 + toAdd;
if(colorName.equalsIgnoreCase("BLUE")) colorNum = 41 + toAdd;
if(colorName.equalsIgnoreCase("INDIGO")) colorNum = 45 + toAdd;
if(colorName.equalsIgnoreCase("PURPLE")) colorNum = 49 + toAdd;
if(colorName.equalsIgnoreCase("VIOLET")) colorNum = 53 + toAdd;
if(colorName.equalsIgnoreCase("MAGENTA")) colorNum = 57 + toAdd;
if(colorName.equalsIgnoreCase("PINK")) colorNum = 56;
}
PKColor(int _colorNum) {
colorNum = _colorNum;
}
}
class PKCoordinate {
int toNote = 0;
int x;
int y;
PKCoordinate(int _x, int _y) {
toNote = (_y+1)*10 + _x + 1;
x = _x;
y = _y;
}
boolean equals(PKCoordinate a) {
if(a.x == this.x && a.y == this.y) {
return true;
} else return false;
}
}
class PKManager {
PKManager() {}
int fillColor = 1;
void fill(PKColor c) {
fillColor = c.colorNum;
}
boolean doShimmer = false;
void shimmer() {
doShimmer = true;
}
void noShimmer() {
doShimmer = false;
}
int deltaColor = 0;
void tint(int amount) {
deltaColor = amount;
fillColor += amount;
}
void noTint() {
fillColor -= deltaColor;
deltaColor = 0;
}
void point(PKCoordinate p) {
padList[p.toNote].on(fillColor, doShimmer);
}
void setPressedColorOf(PKCoordinate p, PKColor c) {
padList[p.toNote].setPressedColor(c.colorNum);
}
void background(PKColor c) {
for(int i = 0; i < padList.length; i++) {
padList[i].on(c.colorNum);
}
}
}
class Pad {
int noteAssignment;
boolean isPressed;
boolean isOn;
int currentVel;
int pressedColor;
Pad(int assignToNote) {
noteAssignment = assignToNote;
isPressed = false;
isOn = false;
currentVel = 0;
}
void on(int c) {
isOn = true;
currentVel = c;
if(noteAssignment < 104) {
myBus.sendNoteOn(0, noteAssignment, c);
} else {
myBus.sendMessage(176, noteAssignment, c);
}
}
public void on(int c, boolean shimmer) {
isOn = true;
currentVel = c;
if(shimmer && noteAssignment < 104) {
myBus.sendNoteOn(2, noteAssignment, c);
} else {
if(noteAssignment < 91) {
myBus.sendNoteOn(0, noteAssignment, c);
} else {
if(!shimmer) {
myBus.sendMessage(176, noteAssignment, c);
} else if(floor(millis() / 50) % 20 < 10) {
myBus.sendMessage(176, noteAssignment, c);
} else {
myBus.sendMessage(176, noteAssignment, 0);
}
}
}
}
public void off() {
isOn = false;
if(noteAssignment < 91) {
myBus.sendNoteOff(0, noteAssignment, 0);
} else {
myBus.sendMessage(176, noteAssignment, 0);
}
}
void setPressedColor(int c) {
pressedColor = c;
}
void padTouchDown() {
on(pressedColor);
}
void padTouchUp() {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment