[Sharing] Communicate Max/MSP to Processing with OSC

Alaghast's icon

Hi to all,

I'm starting to learn how communicate with Max/MSP and Processing. I've discovered first MaxLink libraries to do it, i've tried it and i found that it is now outdated and still unsupported so i'll go out with OSC (udpsend object) and oscP5 library in Processing to do the same things. So i've tried to learn how use Processing replicating and converting some easy examples of MaxLink libraries to oscP5, in particular the "shape_sketch" one, which send from max/msp a "bang" from /drawCircle, /drawSquare, /clear message objects to generate randomly 2D shapes in Processing, but i'm still newbie and i can't understand what is not working with oscP5, because, after i click that message objects in max/msp, in Processing nothing happens.

I'm asking if Processing can see a "bang" from max (like a string?), or i have to declare it in an other way. The idea anyway is to use something that hit an ON value to "bang" an action, without use a streams of int or float values.

I'll attached here the Processing code:

import netP5.*;
import oscP5.*;

OscP5 link;

void setup() {
size(300, 300);
background(250);
noStroke();
smooth();
OscP5 link = new OscP5(this, 8080);

// declare the variable name and the setter function name
link.plug(this, "drawCircle", "/drawCircle");
link.plug(this, "drawSquare", "/drawSquare");
link.plug(this, "clear", "/clear");
}

void draw() {
// must define draw function to
// allow accurate future drawing
}

// these methods must be public
public void drawCircle() {
fill(0, 0, 240, 200);
float r = random(50) + 5;
ellipse(random(width), random(height), r, r);
}

public void drawSquare() {
fill(50, 240, 50, 200);
float w = random(50) + 5;
rect(random(width), random(height), w, w);
}

public void clear() {
background(250);
}

Nathan Wolek's icon

Hi Alaghast. I have done some projects that connect Max and Processing in this way. However, I am not familiar with this syntax:

link.plug(this, "drawCircle", "/drawCircle");

Maybe it is newer? Instead, I have implemented an oscEvent method like this

void oscEvent(OscMessage message_in) {
 //println("message received");
}


Inside the method, you can do whatever parsing is necessary for your messages. But remember, OSC is a standard requires the identifying address at the beginning. You could NOW send just a raw bang message, but you could send something like:

/drawCircle bang

Then parse it using something like this:

if (message_in.checkAddrPattern("/drawCircle") == true) {
String temp = message_in.get(0).stringValue();
}

Hope that helps!
-Nathan

Alaghast's icon

Hi Nathan,

Thank you for your answer, i didn't know oscEvent. Anyway i've solved the problem comunicating both with this method and with plug.

I find plug anyway more simple to write, i share here an example how it works with max patch example, i hope this help someone else go in stuck with it:

import oscP5.*;
OscP5 oscP5;

void setup(){
oscP5 = new OscP5(this,12000);
/* You define there with "/test" the message that come from Max,
like an object [prepend /test].
With "result" you define a name to construct a function inside Processing */
oscP5.plug(this,"result","/test");
}
void draw() {
}
// The function must be public 
public void result(int valueA, float valueB, String valueS) {
 /*The cool thing is that you can send from [prepend /test] three message,
inside a pack object for example, that have three different "type" of values*/
 println("The three result will be: " + valueA + ", " + valueB +" ," + valueS);
}

And there a max patch example to comunicate with it:

Max Patch
Copy patch and select New From Clipboard in Max.

sonichel's icon

I get the following error in the Processing sketch :( HELP ME PLEASE!

Alaghast's icon

Hi Sonichel,

You have typed wrong "import", it isn't import"e" <--- This final "e" is wrong.

Just try to copy and paste to Processing sketch the code that i've previously attached.

sonichel's icon

Thx ALAGHAST!!! :)

Dani Pogue's icon

Is it possible to do something similar in p3 on a local network?

yaniki's icon

OSC works over local network too.