Basic Newbie OSC int and float

Dear All,
I’m going crazy trying to work this out - it’s gotta be something so obvious, but I’m just not getting there.

I have a Lemur template on an ipad sending OSC to my MacBook (see attached image).

I want to jump between different banks and patterns (integers) and within these control different parameters (floats). The following code is a simplification of what I’m trying to do,

import oscP5.*;
import netP5.*;

OscP5 oscP5;

int Bank = 0;               //    /ProgramChange/nbr_1/x
float val = 0;               // various address patterns

void setup() {
  size(400,400);
  oscP5 = new OscP5(this,8000);
 
}

void draw() {
  background(0);
}

void oscEvent(OscMessage theOscMessage) {
  
  Bank = theOscMessage.get(0).intValue();  
  val = theOscMessage.get(1).floatValue();  
  println ("Bank ", Bank, "Value ", val);
  }

it results in: ERROR @ OscP5 ERROR. an error occured while forwarding an OscMessage
to a method in your program. please check your code for any possible errors that might occur in the method where incoming OscMessages are parsed e.g. check for casting errors, possible nullpointers, array overflows … . method in charge : oscEvent java.lang.reflect.InvocationTargetException

I suppose the issue is about using both int and float in the same OSCMessage - I’m just not grasping what I’m doing wrong.

The project is a live A-V performance project - the Lemur template is also sending MIDI data to an Octratrak which is handling the audio - just FYI.

If anyone can point me in the right direction, I’d be really appreciative. Best wishes, …

1 Like

hi - I think lemur is sending in various formats depending on the address. So check out oscEvent in the below adapted from the osc parsing example, first you need to know what address has what type of data (int, float, …)

/**
 * oscP5parsing 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 and more convenient way to parse messages.
 * oscP5 website at http://www.sojamo.de/oscP5
 */

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 is a NetAddress. a NetAddress takes 2 parameters,
   * an ip address and a port number. myRemoteLocation is used as parameter in
   * oscP5.send() when sending osc packets to another computer, device, 
   * application. usage see below. for testing purposes the listening port
   * and the port of the remote location address are the same, hence you will
   * send messages back to this sketch.
   */
  myRemoteLocation = new NetAddress("127.0.0.1",12000);
}

void draw() {
  background(0);  
}


void mousePressed() {
  /* create a new osc message object */
  OscMessage myMessage = new OscMessage("/test");
  
  myMessage.add(123); /* add an int to the osc message */
  myMessage.add(12.34); /* add a float to the osc message */
  myMessage.add("some text"); /* add a string to the osc message */

  /* send the message */
  oscP5.send(myMessage, myRemoteLocation); 
}


void oscEvent(OscMessage theOscMessage) {
  /* check if theOscMessage has the address pattern we are looking for. */
  print("address: " + theOscMessage.addrPattern() + " ");
  println("typetag: " + theOscMessage.typetag());
}

for the typetag, if it is ifs, it means int, float and string in that order. Once you know the pattern, you can make conditions according to the address like in the original parsing example:

void oscEvent(OscMessage theOscMessage) {
  /* check if theOscMessage has the address pattern we are looking for. */
  print("address: " + theOscMessage.addrPattern() + " ");
  println("typetag: " + theOscMessage.typetag());
  
  if(theOscMessage.checkAddrPattern("/test")==true) {
    /* check if the typetag is the right one. */
    if(theOscMessage.checkTypetag("ifs")) {
      /* parse theOscMessage and extract the values from the osc message arguments. */
      int firstValue = theOscMessage.get(0).intValue();  
      float secondValue = theOscMessage.get(1).floatValue();
      String thirdValue = theOscMessage.get(2).stringValue();
      print("### received an osc message /test with typetag ifs.");
      println(" values: "+firstValue+", "+secondValue+", "+thirdValue);
      return;
    }  
  } 
  println("### received an osc message. with address pattern "+theOscMessage.addrPattern());
}
2 Likes

Hi [micuat],

Thanks so for your help - I am now illuminated!

:-).