OSC (Open Sound Control) Parsing Messages : how to use class functions

edited April 2014 in Library Questions

Hi There, I am reading some data processed in a Gesture Recognition Kit, and the data is sent, and received via OSC (Open Sound Control) via Processing OSC Library. Although i've dabbled with OSC Messages I need some help to understand the code in the class file(See below) to read OSC data

The GRT class partly indicated at the end of this post is provided by the developer to send and receive message from OSC. NOTE : Since the code is too long to be included in this post i've shared it via txt.do/t6wf

My Question is When I have to use this class to parse messages receivedfrom OSC, what should I write in my main code ? (I know how to send data from processing to OSC).

I guess i have to first initialze it like

final int pipelineMode = GRT.CLASSIFICATION_MODE;
final int numInputs = 4;//NO OF INPUTS
final int numOutputs = 1;
//Create a new GRT instance, this will initalize everything for us and send the setup message to the GRT GUI
GRT grt = new GRT( pipelineMode, numInputs, numOutputs, "192.168.16.108", 5000, 5001, true );

and then

if OSCEvent.......?

Thanks in advance Genny

/**
GRT
Version: 1.0
Author: Nick Gillian

Info: This class acts as an interface to the GRT GUI. It sends data and commands to the GUI via Open Sound Control (OSC). To use this
code you should download and install the Processing OSC library from: http://www.sojamo.de/libraries/oscP5/
*/

class GRT{

  private OscP5 oscP5;
  private NetAddress grtBackend;

  private boolean initialized;
  private boolean record;
  private int pipelineMode;
  private int numDimensions;
  private int targetVectorSize;
  private int trainingClassLabel;
  private int predictedClassLabel;

  private int grtPipelineMode;
  private boolean grtPipelineTrained;
  private boolean grtRecording;
  private int grtNumTrainingSamples;
  private int grtNumClassesInTrainingData;
  private String grtInfoText;

  private float maximumLikelihood;
  private float[] preProcessedData;
  private float[] featureExtractionData;
  private float[] classLikelihoods;
  private float[] classDistances;
  private float[] regressionData;
  private float[] targetVector;
  private int[] classLabels;

  //Pipeline Modes
  public static final int CLASSIFICATION_MODE = 0;
  public static final int REGRESSION_MODE = 1;
  public static final int TIMESERIES_MODE = 2;

  //Classifier Types
  public static final int ANBC = 0;
  public static final int ADABOOST = 1;
  public static final int GMM = 2;
  public static final int KNN = 3;
  public static final int MINDIST = 4;
  public static final int SOFTMAX = 5;
  public static final int SVM = 6;

  /**
   Default constructor
  */
  GRT(){
    initialized = false;
  }

  /**
   Main constructor used to initialize the instance.

   @param int pipelineMode: sets the mode that the pipeline will run in. This should be a valid pipeline mode, either CLASSIFICATION_MODE, REGRESSION_MODE, or TIMESERIES_MODE
   @param int numInputs: sets the size of your data vector, this is the data that you will send to the GRT GUI
   @param int numOutputs: this parameter is only used for REGRESSION_MODE, in which case it sets the target vector size
   @param String grtIPAddress: the IP address of the machine running the GRT GUI. If it is running on the same machine as this Processing Sketch this should be "127.0.0.1"
   @param int grtPort: the network port that the GRT GUI is listening for connections on. This is set by the OSC Receive Port setting in the GRT GUI
   @param int listenerPort: the network port that this Processing Sketch should listen for OSC messages from the GRT GUI
   @param bool sendSetupMessage: if true then the Setup message will be sent to the GRT GUI, if false then the message will not be sent
  */
  GRT(int pipelineMode,int numInputs,int numOutputs,String grtIPAddress,int grtPort,int listenerPort, boolean sendSetupMessage){
    initialized = init( pipelineMode, numInputs, numOutputs, grtIPAddress, grtPort, listenerPort, sendSetupMessage );
  }

  /**

// <<<<Cropped due to space Limitation>>

  /**
   This function is called anytime a new OSC message is received. It will then try and parse the message.
  */
  void oscEvent(OscMessage theOscMessage) {

    if( parseMessage( theOscMessage ) ) return;

  }

  /**
   This function parses OSC messages from the GRT GUI. 

   @param OscMessage theOscMessage: the osc message to parse
   @return returns true if the message was successfully parsed, false otherwise
  */
  boolean parseMessage(OscMessage theOscMessage) {

    if( theOscMessage.checkAddrPattern("/Status")==true) {
      grtPipelineMode = theOscMessage.get(0).intValue();
      grtPipelineTrained = theOscMessage.get(1).intValue() == 1 ? true : false;
      grtRecording = theOscMessage.get(2).intValue() == 1 ? true : false;
      grtNumTrainingSamples = theOscMessage.get(3).intValue();
      grtNumClassesInTrainingData = theOscMessage.get(4).intValue();
      grtInfoText = theOscMessage.get(5).stringValue();
      return true;
    }

    if(theOscMessage.checkAddrPattern("/PreProcessedData")==true) {
      int N = theOscMessage.get(0).intValue();
      if( preProcessedData.length != N ){
        preProcessedData = new float[N]; 
      }
      for(int i=0; i<N; i++){
        preProcessedData[i] =  theOscMessage.get(i+1).floatValue();
      }
      return true;
    }

    if(theOscMessage.checkAddrPattern("/FeatureExtractionData")==true) {
      int N = theOscMessage.get(0).intValue();
      if( featureExtractionData.length != N ){
        featureExtractionData = new float[N]; 
      }
      for(int i=0; i<N; i++){
        featureExtractionData[i] =  theOscMessage.get(i+1).floatValue();
      }
      return true;
    }

    if(theOscMessage.checkAddrPattern("/Prediction")==true) {
      if(theOscMessage.checkTypetag("if")) {
        predictedClassLabel = theOscMessage.get(0).intValue();
        maximumLikelihood = theOscMessage.get(1).floatValue();
        return true;
      }   
    }

    if(theOscMessage.checkAddrPattern("/ClassLikelihoods")==true) {
        int N = theOscMessage.get(0).intValue();
        if( classLikelihoods.length != N ) classLikelihoods = new float[N];

        for(int i=0; i<N; i++){
          classLikelihoods[i] = theOscMessage.get(1+i).floatValue();
        }
        return true;
    }

    if(theOscMessage.checkAddrPattern("/ClassDistances")==true) {
        int N = theOscMessage.get(0).intValue();
        if( classDistances.length != N ) classDistances = new float[N];

        for(int i=0; i<N; i++){
          classDistances[i] = theOscMessage.get(1+i).floatValue();
        }
        return true;
    }

    if(theOscMessage.checkAddrPattern("/ClassLabels")==true) {
      int N = theOscMessage.get(0).intValue();
      if( classLabels.length != N ) classLabels = new int[N]; 

      for(int i=0; i<N; i++){
        classLabels[i] = theOscMessage.get(i+1).intValue();
      }
      return true;
    }

     if(theOscMessage.checkAddrPattern("/RegressionData")==true) {
        int N = theOscMessage.get(0).intValue();
        if( regressionData.length != N ) regressionData = new float[N];
        for(int i=0; i<N; i++){
          regressionData[i] = theOscMessage.get(1+i).floatValue();
        }
        return true;
    }

    return false;

  }

};
Tagged:
Sign In or Register to comment.