Everything should be made as simple as possible, but not simpler. (Albert Einstein)

Friday, December 27, 2013

Basic Processing and Chuck Communication

A very basic example on how to establish communication between Processing and ChucK.
  • run oscProc.pde in Processing
  • run oscChuck.ck in ChucK
  • in the sketch, press "s" key to send message



Refs:
 http://www.sojamo.de/libraries/oscP5/
 http://electro-music.com/forum/post-322758.html
 http://chuck.cs.princeton.edu/doc/examples/osc/OSC_send.ck
 http://chuck.cs.princeton.edu/doc/examples/osc/OSC_recv.ck
 http://chuck.cs.princeton.edu/doc/examples/osc/OSC_xmit.ck
 http://chuck.cs.princeton.edu/doc/examples/osc/s.ck
 http://chuck.cs.princeton.edu/doc/examples/osc/r.ck
 etc..

// oscProc.pde
// a very basic example of communication from/to ChucK

import oscP5.*;
import netP5.*;

OscP5 oscP5;
NetAddress myBroadcastLocation;

String oscServer = "127.0.0.1";
int oscServerPortSend = 6449;
int oscServerPortReceive = 6450;

String pattern = "";
String tagType = "";
float val = 0;

void setup() {
  size(400, 400);
  frameRate(24);
  smooth();
  
  textAlign(LEFT, TOP);
  background(0);
  
  // Instantiation of a new oscP5 object
  oscP5 = new OscP5(this, oscServerPortReceive);

  // Address of the osc receiver
  myBroadcastLocation = new NetAddress(oscServer, oscServerPortSend);
  
}

void draw() {
  background(0);
  // any drawing ..
  
  // display message
  fill(255);
  text("### received an osc message.", 10, 10);
  text(" addrpattern: " + pattern, 10, 30);
  text(" typetag: " + tagType, 10, 50);
  text(" value: " + val, 10, 70);
}


void oscEvent(OscMessage theOscMessage) {
  pattern = theOscMessage.addrPattern();
  tagType = theOscMessage.typetag();
  val = theOscMessage.get(0).floatValue();
} 

void keyPressed() {
  switch(key) {
    case('s'): case('S'):
      OscMessage myOscMessage = new OscMessage("/hello");
      // add a value to the OscMessage
      myOscMessage.add("Hello world! " + val);
      // send the OscMessage to a remote location specified in myNetAddress
      oscP5.send(myOscMessage, myBroadcastLocation);
      break;
    
    default:
      break;
  }
}

// oscChuck.ck
// a very basic example of communication from/to Processing

// OSC sender
OscSend xmit;
"localhost" => string hostname;
6450 => int sendport;
xmit.setHost( hostname, sendport );

// OSC receiver
OscRecv recv;
6449 => recv.port;

// listening to receiver port
recv.listen();
// expected event "/hello", tag type string
recv.event("/hello, s") @=> OscEvent @ evtHello;
// concurrent shred
spork ~ listenHello();

0 => int cnt;  // a counter just for fun

// main loop
while( true )
{
    // send counter as float every 1 second
    xmit.startMsg( "/count", "f" );
    cnt++ => xmit.addFloat; 
    1::second => now;
}

// "/hello" listener
fun void listenHello()
{
    while(true) {
        evtHello => now;

        // get message if any
        if ( evtHello.nextMsg() != 0 )  
        { 
            evtHello.getString() => string mesg;
            <<< "Mesg:", mesg >>>;
        }
    }
}