Anonymous commenting enabled / on the bottom of the page is a vote for next tutorials

Monday, February 21, 2011

Pure Data to Processing

Sending PureData data to Processing over local UTP port.


01| THE PUREDATA PATCH

1. We will start with the Pure Data patch on how to send data over UDP.

2. Open a new Pure Data patch

3. Add a "connect localhost 12000" Messagebox.
This message connects to yourself (localhost) and sends messages on port 12000. this port is important to specify later in Processing


4. Connect this message to the UDPSEND ( udpsend is the function that actually sends the message over port 12000 )

if udpsend is not availaible ( the box isn't made ) then you need to add a "import mrpeach" box. this is the library for udp connection



5. Add an packOSC object to pack the message.


6. Add a message with "send /first $1" and above a numberBox
the send "/first" will be linked and checked in Processing, this can be anything you like "/test" will also work.
the $1 refers to the numberBox above so u can test other values on runtime.


02| DOWNLOADING THE OSC LIBRARY FOR PROCESSING

1. You need to download the OSCP5 library

DOWNLOAD

2. place it into your sketchbook folder
( in processing go to preferences, there you will see the sketchbook location )


03| PROCESSING SKETCH

1. this is the processing sketch, explanation follows.

import oscP5.*;
import netP5.*;

OscP5 oscP5;
NetAddress myRemoteLocation;

void setup() {
size(400,400);
frameRate(25);
/* start oscP5, listening for incoming messages at port 12000 */
oscP5 = new OscP5(this,12000);

myRemoteLocation = new NetAddress("127.0.0.1",12000);
}

void draw() {
background(0);
}


void oscEvent(OscMessage theOscMessage) {

if(theOscMessage.checkAddrPattern("/first")==true) {
if(theOscMessage.checkTypetag("i")) {

int firstValue = theOscMessage.get(0).intValue();
println(" values: "+firstValue);
return;
}
}
}

04| PROCESSING SKETCH EXPLANATION

The first part defines the OSC library. and defines a NetAdress for comunnication over UPT protocol.
import oscP5.*;
import netP5.*;

OscP5 oscP5;
NetAddress myRemoteLocation;

This line declares the OSC object who will receive on port 12000
oscP5 = new OscP5(this,12000);

RemoteLocation defines the address on wich to send. 127.0.0.1 refers to local pc.
myRemoteLocation = new NetAddress("127.0.0.1",12000);

Declares the event wich will pull data out of the message
void oscEvent(OscMessage theOscMessage) {

Check if the "/first" is in the sent data ( as defined in PureData this can be anything else )
if(theOscMessage.checkAddrPattern("/first")==true) {

Checks the tag of the incoming object ."i" stands for integer , if this would be text you would make it "s" from string, a float is also possible with "f"
if(theOscMessage.checkTypetag("i")) {

Parse theOscMessage and extract the value from it. there is only one value to parse now ( the integer we are sending from PureData ) so there's only one value to declare (Firstvalue) the ".get(0)" is the first value that we receive.

If we would have more values you would also have a ".get(1)" and so on.
int firstValue = theOscMessage.get(0).intValue();

This will print the received value in the console.
println(" values: "+firstValue);


You can test it by pressing play in Processing, and changing the value of the numberbox in PureData.

7 comments:

  1. God!
    I wish this was posted two months ago, when I struggled to get this communication and succeeded after lot of headache.

    Thank you!
    ps: Why don't you make a chapter in FLOSS/Pd manual?!

    -----
    GeorgeKer
    path_to_sanity [αt] joindiaspora [dοt] com
    -----

    ReplyDelete
  2. I can't seem to get the packOSC object to work, is there different object I can use?

    ReplyDelete
  3. What would be the the command on the processing side to send data back to PD? Like you if you want to make a GUI in Processing that talks to PD (without the gui)

    ReplyDelete
  4. What about sending data from Processing to PD?
    OscP5 does not work anymore...

    ReplyDelete
  5. Vey useful, thank you very much.

    ReplyDelete
  6. Hi there, nice post and a good source of information. It really shows that you're an expert in this field. Thanks for sharing.
    Keep it up!

    data processing

    ReplyDelete
  7. Hi friends, Thanks for share information in this blog........

    Data Processing Outsourcing

    ReplyDelete