Hitting up against variable scope issue with OSC messages

Hello all,

I’m having a problem passing OSC into processing. Everything is working in the code below except for the fact that the ellipse does not find the variable pan. I even know why this is: variable scope. However, I have no idea how to solve this issue. I feel like putting the draw inside the oscEvent, but something feels wrong about doing so.

Can the Osc messages be turned into global variables or something similar?

Many thanks for any help.

Any help would be greatly appreciated.

/**
 * oscP5oscArgument by andreas schlegel
 * example shows how to parse incoming osc messages "by hand".
 * it is recommended to take a look at oscP5plug for an alternative way to parse messages.
 * oscP5 website at http://www.sojamo.de/oscP5
 */

import oscP5.*;
import netP5.*;

OscP5 oscP5;
NetAddress myRemoteLocation;

void setup() {
  size(600,400);
  background(255);
  frameRate(25);
  /* start oscP5, listening for incoming messages at port 12000 */
  oscP5 = new OscP5(this,12000);
  myRemoteLocation = new NetAddress("127.0.0.1",12000);
  /* send an OSC message to this sketch */
  oscP5.send("/test",new Object[] {new Float(4.3), new Float(3.2), new Float(2.2)}, myRemoteLocation);
  
}

void oscEvent(OscMessage theOscMessage) {
  /* check if theOscMessage has the address pattern we are looking for. */  
  if(theOscMessage.checkAddrPattern("/test")==true) {
    /* check if the typetag is the right one. */
    if(theOscMessage.checkTypetag("fff")) {
      /* parse theOscMessage and extract the values from the osc message arguments. */
      float freq = theOscMessage.get(0).floatValue();  // get the first osc argument
      float pan = theOscMessage.get(1).floatValue(); // get the second osc argument
      float dur = theOscMessage.get(2).floatValue(); // get the third osc argument
      print("### received an osc message /test with typetag fff.");
      println(" values: "+freq+", "+pan+", "+dur);
      return;
    }
  }
  println("### received an osc message. with address pattern "+
          theOscMessage.addrPattern()+" typetag "+ theOscMessage.typetag());
}

void draw() {
  background(0); 
  ellipse(pan, 300, 10, 10);
}

You’re right, you need to make pan a global variable

...

float freq = 0;
float pan = 0;
float dur = 0;

...

void oscEvent(OscMessage theOscMessage) {
  /* check if theOscMessage has the address pattern we are looking for. */  
  if(theOscMessage.checkAddrPattern("/test")==true) {
    /* check if the typetag is the right one. */
    if(theOscMessage.checkTypetag("fff")) {
      /* parse theOscMessage and extract the values from the osc message arguments. */
      freq = theOscMessage.get(0).floatValue();  // get the first osc argument
      pan = theOscMessage.get(1).floatValue(); // get the second osc argument
      dur = theOscMessage.get(2).floatValue(); // get the third osc argument
      print("### received an osc message /test with typetag fff.");
      println(" values: "+freq+", "+pan+", "+dur);
      return;
    }
  }
  println("### received an osc message. with address pattern "+
          theOscMessage.addrPattern()+" typetag "+ theOscMessage.typetag());
}

void draw() {
  background(0); 
  ellipse(pan, 300, 10, 10);
}

You could make OscMessage global as well, but oscEvent and draw are called with different timing and weird things might happen, so I suggest making float values as global instead.

Thanks for your reply Micuat!

Well, it got me closer, but no cigar. I see the circle flash up when the sketch is started, then it disappears.

I modified the code a bit as it didn’t run without importing oscP5 etc.

However there is a wider problem I perceive with using global variable, in that if I want more than one circle (which I do, eventually) then I can’t envision how global variables.

I’m not wondering if this has something to do with threads in Processing. I’m reading up on the subject, but it’s no really clear to me what the solution is (if it’s the problem).

Thank you for the help so far!

/**
 * oscP5oscArgument by andreas schlegel
 * example shows how to parse incoming osc messages "by hand".
 * it is recommended to take a look at oscP5plug for an alternative way to parse messages.
 * oscP5 website at http://www.sojamo.de/oscP5
 */
 
float freq = 0;
float pan = 0;
float dur = 0; 

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);
  /* send an OSC message to this sketch */
  oscP5.send("/test",new Object[] {new Float(4.3), new Float(3.2), new Float(2.2)}, myRemoteLocation);
  
}


void oscEvent(OscMessage theOscMessage) {
  /* check if theOscMessage has the address pattern we are looking for. */  
  if(theOscMessage.checkAddrPattern("/test")==true) {
    /* check if the typetag is the right one. */
    if(theOscMessage.checkTypetag("fff")) {
      /* parse theOscMessage and extract the values from the osc message arguments. */
      freq = theOscMessage.get(0).floatValue();  // get the first osc argument
      pan = theOscMessage.get(1).floatValue(); // get the second osc argument
      dur = theOscMessage.get(2).floatValue(); // get the third osc argument
      print("### received an osc message /test with typetag fff.");
      println(" values: "+freq+", "+pan+", "+dur);
      return;
    }
  }
  println("### received an osc message. with address pattern "+
          theOscMessage.addrPattern()+" typetag "+ theOscMessage.typetag());
}

void draw() {
  background(0); 
  ellipse(pan, 300, 10, 10);
}