Skip to content

Instantly share code, notes, and snippets.

@ffd8
Last active October 13, 2019 19:58
Show Gist options
  • Save ffd8/f9f33cc7461f8467f62d5a792dde53ca to your computer and use it in GitHub Desktop.
Save ffd8/f9f33cc7461f8467f62d5a792dde53ca to your computer and use it in GitHub Desktop.
Processing sketch for P5LIVE osc_setup demo
/*
p5live_osc_setup // cc teddavis.org 2019
Requires offline nodejs/npm installation of P5LIVE
Requires oscP5 library
*/
int tc = 10, rate = 5;
int mx = 0, my = 0, px = 0, py = 0;
PGraphics pg;
import oscP5.*;
import netP5.*;
OscP5 oscP5;
NetAddress myRemoteLocation;
void setup() {
size(400, 400);
frameRate(25);
pg = createGraphics(width, height);
/* start oscP5, listening for incoming messages */
oscP5 = new OscP5(this, 12001);
/* start oscP5, sending outgoing messages */
myRemoteLocation = new NetAddress("127.0.0.1", 12000);
}
void draw() {
background(0);
// draw outgoing pings
if (frameCount%rate==0) {
randomPing();
}
// draw incoming msgs
image(pg, 0, 0);
}
void randomPing() {
int rval = floor(random(tc));
for (int i=0; i<tc; i++) {
if (i == rval) {
fill(255);
rect(map(i, 0, tc, 0, width), 0, width/tc, height);
}
}
OscMessage myMessage = new OscMessage("/"+rval);
myMessage.add(random(1)); /* add an int to the osc message */
/* send the message */
oscP5.send(myMessage, myRemoteLocation);
}
/* incoming osc message are forwarded to the oscEvent method. */
void oscEvent(OscMessage theOscMessage) {
/* print the address pattern and the typetag of the received OscMessage */
//print("### received an osc message.");
//print(" addrpattern: "+theOscMessage.addrPattern());
//println(" typetag: "+theOscMessage.typetag());
// scale input to fit window
if (theOscMessage.addrPattern().equals("/screenSize")) {
int w = theOscMessage.get(0).intValue()/4;
int h = theOscMessage.get(1).intValue()/4;
frame.setSize(w, h);
} else if (theOscMessage.addrPattern().equals("/p5js")) {
mx = theOscMessage.get(0).intValue()/4;
my = theOscMessage.get(1).intValue()/4;
pg.beginDraw();
pg.stroke(255);
pg.line(mx, my, px, py);
pg.endDraw();
px = mx;
py = my;
} else if (theOscMessage.addrPattern().equals("/clear")) {
pg = createGraphics(width, height);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment