Python help? Export OSC data to Processing

You have done the hardest part so now the question is, do you want to handle your case logic (data grouping) in Python side or Processing side?

I will do it in Processing side and I will apply a slightly modification to your Py code. First:

  • I will send client.send_message("/filter", str(index)+","+str(data[x]) ). Adding an index/counter to your data will allow to check for data integrity in the receiving end if needed. I find this will make your life easier when it comes to debugging.
  • In Processing, you process your data as it arrives adding it to the right category.

This demo runs for about 10 seconds. You can press any key to see the content of each category.

Note: If you are not concerned with data integrity, I will drop the TreeMap and use FloatList instead.

Kf

//===========================================================================
// IMPORTS:
import java.util.Map;
import java.util.TreeMap;

//===========================================================================
// FINAL FIELDS:
final int N=7;
final char SEPARATOR=',';

//===========================================================================
// GLOBAL VARIABLES:
ArrayList<TreeMap<Integer, Float>> categories;
String indata;

//===========================================================================
// PROCESSING DEFAULT FUNCTIONS:

void settings() {
  size(400, 600);
}

void setup() {

  categories = new ArrayList<TreeMap<Integer, Float>>();
  for (int i=0; i<N; i++) {
    TreeMap<Integer, Float> map = new TreeMap<Integer, Float>();
    categories.add(map);
  }
}


void draw() {
  background(0);
  indata=str(frameCount)+SEPARATOR+str(random(MAX_INT));
  processData(indata);
  
  if(millis()>10000){
   exit(); 
  }
}

void keyReleased() {
  printData();
}

//===========================================================================
// OTHER FUNCTIONS:
void processData(String data) {
  String[] tokens=data.split(SEPARATOR);
  if (tokens.length == 2) {
    int idx=int(tokens[0]);
    float val =  float(tokens[1]);
    int whichCategory = idx%N;  //Assuming there are N categories
    categories.get(whichCategory).put(idx, val);
  }
}

void printData() {
  println("=======\nDATA DUMP\n=======");
  for (int i=0; i<N; i++) {
    println("CATEGORY #"+i);
    TreeMap<Integer, Float> hm = categories.get(i);
    for (Map.Entry me : hm.entrySet()) {
      println("\t" + me.getKey() + " => " + me.getValue());
    }
  }
}
2 Likes