Net connection between Processing and Max/MSP

The following Max/MSP patch uses the netsend object to send data over a TCP socket. It sends three values. The Processing sketch further down is a server that receives the string that the Max patch sends and uses it to move a ball on the screen.

Technorati Tags: ,


max v2;
#N vpatcher 35 147 635 547;
#P window setfont "Sans Serif" 9.;
#P window linecount 1;
#P newex 187 227 68 196617 prepend send;
#P newex 187 202 52 196617 pak 0 0 0;
#P number 281 170 35 9 0 255 3 3 0 0 0 221 221 221 222 222 222 0 0 0;
#P number 187 170 35 9 0 255 3 3 0 0 0 221 221 221 222 222 222 0 0 0;
#P number 233 170 35 9 0 255 3 3 0 0 0 221 221 221 222 222 222 0 0 0;
#P message 60 169 55 196617 disconnect;
#P message 60 144 114 196617 connect localhost 3000;
#P number 47 294 35 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
#P newex 47 270 43 196617 netsend;
#P comment 186 151 155 196617 three arbitrary integer values:;
#P window linecount 4;
#P comment 60 88 100 196617 click here to connect after server application is running:;
#P window linecount 2;
#P comment 60 194 75 196617 click here to disconnect;
#P window linecount 1;
#P comment 242 202 154 196617 packs three values into one list;
#P comment 260 227 202 196617 adds the command "send" before the list;
#P comment 86 294 258 196617 Whether netsend is connected to the server or not;
#P window linecount 4;
#P comment 215 55 82 196617 NetSend tester Created 29 Nov 2006 by Tom Igoe;
#P fasten 10 0 7 0 65 189 52 189;
#P fasten 9 0 7 0 65 163 52 163;
#P fasten 15 0 7 0 192 258 52 258;
#P connect 7 0 8 0;
#P fasten 12 0 14 0 192 193 192 193;
#P connect 14 0 15 0;
#P fasten 11 0 14 1 238 188 213 188;
#P fasten 13 0 14 2 286 192 234 192;
#P pop;

Here’s the Processing server sketch:

/*
  Max Server 
 
 Accepts three values from a remote client. Uses those values
 to draw and move a ball.
 
 This server expects a string containing ASCII-encoded integers
 terminated by a semicolon. This is the format that Max/MSP's netsend
 object sends data.
 
 by Tom Igoe
 Created 18 Mar 2005
 Modified 20 Aug 2007
 */

// import the net library:
import processing.net.*;

int port = 3000;    // incoming TCP port
Server myServer;    // server object

void setup()
{
  size(256, 256);
  background(0);
  myServer = new Server(this, port); // Starts a server on port 3000
  smooth();
}


void draw()

{
  // see if there's a client that's connected and sent data:
  Client thisClient = myServer.available();
  if (thisClient != null) {
    // if there are bytes in the client's incoming data buffer:
    if (thisClient.available() > 0) {
      // read the incoming string:
      String thisString = thisClient.readString();
      // split it at the semicolon to strip off the semicolon:
      String[] pieces = split(thisString, ";");
      // save only what's before the semi:
      thisString = pieces[0];
      // split the resulting string on the space characters:
      int messages[] = int(split(thisString, " "));
      // if you've got three pieces, use them to draw the ball:
      if (messages.length > 2) {
        drawBall(messages[0], messages[1], messages[2]);      
      }  
    }
  }
}

// draw the ball:
void drawBall(int x, int y, int greyscale) {
  background(255 - ); 
  fill(greyscale);
  ellipse(x, y, 20, 20);
}
This entry was posted in Max/MSP, Processing. Bookmark the permalink.

Comments are closed.