Skip to content

Instantly share code, notes, and snippets.

@hizkifw
Created June 10, 2020 18:24
Show Gist options
  • Save hizkifw/badc63d7578cdb565a770b82b5010e53 to your computer and use it in GitHub Desktop.
Save hizkifw/badc63d7578cdb565a770b82b5010e53 to your computer and use it in GitHub Desktop.
Draw an 88-key piano that responds to UDP messages
import hypermedia.net.*;
int PORT_RX = 5000;
String HOST_IP = "127.0.0.1"; //IP Address of the PC in which this App is running
UDP udp; //Create UDP object for recieving
final int K_WIDTH = 22;
final int K_HEIGHT = 127;
final int K_B_WIDTH = 11;
final int K_B_HEIGHT = 80;
final int START_NOTE = 21;
ArrayList<Boolean> noteStates;
void setup() {
udp = new UDP(this, PORT_RX, HOST_IP);
udp.log(true);
udp.listen(true);
noteStates = new ArrayList<Boolean>();
for(int i = 0; i < 88; i++) {
noteStates.add(false);
}
surface.setTitle("Piano");
size(1280, 720, P2D);
background(0);
}
void draw() {
background(0);
stroke(0);
int currentKey = START_NOTE - 1;
for(int i = 0; i < 52; i++) {
if(i % 7 != 5 && i % 7 != 2) currentKey++;
int x = i * K_WIDTH;
fill(255);
if(noteStates.get(currentKey - START_NOTE))
fill(249, 44, 79);
rect(x, 0, K_WIDTH, K_HEIGHT);
fill(0);
currentKey++;
}
currentKey = START_NOTE - 1;
for(int i = 0; i < 51; i++) {
currentKey += 2;
if(i % 7 == 4 || i % 7 == 1) {
currentKey--;
continue;
}
int x = (i * K_WIDTH) + (K_WIDTH / 2) + (K_B_WIDTH / 2);
fill(0);
if(noteStates.get(currentKey - START_NOTE))
fill(249, 44, 79);
rect(x, 0, K_B_WIDTH, K_B_HEIGHT);
text(currentKey, x, 100);
}
}
// Messages in the format of
// <midi key> <1|0>
// e.g. "60 1" for middle C on
void receive(byte[] data, String ip, int port) {
System.out.println(new String(data).trim());
String[] msg = new String(data).trim().split(" ");
noteStates.set(parseInt(msg[0]) - START_NOTE, "1".equals(msg[1]));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment