Advertisement
Guest User

Untitled

a guest
Apr 1st, 2015
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.21 KB | None | 0 0
  1. import arb.soundcipher.*;
  2. import arb.soundcipher.constants.*;
  3.  
  4. import oscP5.*;
  5. import netP5.*;
  6.  
  7.  
  8. OscP5 oscP5;
  9. OscP5 oscP52;
  10. NetAddress myRemoteLocation;
  11. NetAddress myRemoteLocation2;
  12. int  old_x=0, old_y=0;  
  13. int h;
  14. int x = 0;
  15. SoundCipher sc = new SoundCipher(this);
  16.  
  17.  
  18. // DATA INTERPRETING
  19. boolean lastHeartTrigger = false;
  20. boolean heartTrigger = false;
  21. float CHANGE_DECREASE = 0.8f;
  22. float changeVal = 0.f;
  23. float LAST_DATA_REDUCTION_RATE = 1f; //experiment with this, could be 0.5f
  24. int []lastDataVals = new int[3];
  25. int AVG_WINDOW_SIZE = 100;
  26. float AVG_WINDOW_THRES_FACTOR = 1.5f;
  27. float []avg_window = new float[AVG_WINDOW_SIZE];
  28. float window_avg = 0.f;
  29. int v1, v2, v3, v4, v5;
  30. int BPM;
  31. int savedTime;
  32. int totalTime = 15000;
  33. int sendBPM = 0;
  34.  
  35.  
  36. void updateLastDataVals(int val) {
  37.   for( int i = lastDataVals.length-1 ; i >= 1 ; i-- ) {
  38.     lastDataVals[i] = lastDataVals[i-1];
  39.   }
  40.   lastDataVals[0] = val;
  41. }
  42.  
  43. void updateAvgWindow(float val) {
  44.   for( int i = avg_window.length-1 ; i >= 1 ; i-- ) {
  45.     avg_window[i] = avg_window[i-1];
  46.   }
  47.   avg_window[0] = val;
  48.   float avg = 0.f;
  49.   for( int i = 0 ; i < avg_window.length ; i++ ) {
  50.     avg += avg_window[i];
  51.   }
  52.   window_avg = avg / (float)avg_window.length;
  53. }
  54.  
  55. float getDataDirection() {
  56.   float dataImportance = 1.f;
  57.   float dir = 0.f;
  58.   for( int i = 0 ; i < lastDataVals.length-1 ; i++ ) {
  59.     dir += abs(lastDataVals[i] - lastDataVals[i+1]) * dataImportance;
  60.     dataImportance *= LAST_DATA_REDUCTION_RATE;
  61.   }
  62.   return dir;
  63. }
  64.  
  65.  
  66. void setup()
  67. {
  68.   background(208,24,24);
  69.   oscP5 = new OscP5(this,1040);
  70.   myRemoteLocation = new NetAddress("127.0.0.1",2040);
  71.   size(displayWidth/2, displayHeight/2);
  72.    h = displayHeight/2;
  73.   savedTime = millis();
  74. }
  75.  
  76.  
  77. void draw()
  78. {
  79.   int passedTime = millis() - savedTime;
  80.   if (passedTime > totalTime) {
  81.     print("Time " + millis()/1000);
  82.     println(" BPM  " + BPM*4);
  83.     sendBPM = BPM*4;
  84.     BPM = 0;
  85.     savedTime = millis();
  86.   }
  87. }
  88.  
  89.  
  90.  
  91. boolean newDataVal(int val) {
  92.   //print(val + ",");
  93.   updateLastDataVals(val);
  94.   float lastChangeVal = changeVal;
  95.   changeVal *= CHANGE_DECREASE;
  96.   changeVal += getDataDirection();
  97.   //println("change val: "+changeVal);
  98.   float adjustedVal = (changeVal + (lastChangeVal*0.5f))/3.f;
  99.   //println("adj val: "+adjustedVal);
  100.   updateAvgWindow(adjustedVal);
  101.   lastHeartTrigger = heartTrigger;
  102.   heartTrigger = adjustedVal >= (window_avg*AVG_WINDOW_THRES_FACTOR);
  103.   return heartTrigger && heartTrigger != lastHeartTrigger;
  104. }
  105.  
  106.  
  107. int COUNT_NUMBER = 10;
  108. int count = 0;
  109.  
  110.  
  111. void oscEvent(OscMessage theOscMessage) {
  112.    int v1 = theOscMessage.get(2).intValue();
  113.    //v2 = theOscMessage.get(3).intValue();
  114.    //v3 = theOscMessage.get(0).intValue();
  115.    //v4 = theOscMessage.get(1).intValue();
  116.    //v5 = theOscMessage.get(4).intValue();
  117.    
  118.    boolean trigger = newDataVal(v1);
  119.    
  120.    
  121.    
  122.    strokeWeight(12);//beef up our white line
  123.   stroke(255,128,128);//make the line white
  124.  
  125.  
  126.   float val = map(v1, 200, 800, 0, h);
  127.   float avg = map(window_avg, 200, 800, 0, h);
  128.   //println("avg: "+window_avg);
  129.   line(old_x,old_y, x, h-int(val));
  130.    //stroke(0,255,255);
  131.   //line(0, avg, width, avg);
  132.  
  133.  
  134.  
  135.  
  136.   if( trigger ) {
  137.     BPM ++;
  138.      //sc.playNote(100, 100, 1.0);
  139.      stroke(255);
  140.      line(x,0,x,displayHeight/4);
  141.      count = COUNT_NUMBER;
  142.    }
  143.  
  144.    old_x = x;
  145.    old_y = h-int(val);
  146.    
  147.     x=x+10;
  148.    
  149.    
  150.     if(x>(displayWidth/2)){
  151.     background(208,24,24); //refresh the screen, erases everything
  152.     x=0; //make the increment back to 0,
  153.     //but used 50, so it sweeps better into the screen
  154.     //reset the old x,y values
  155.   old_x = x;
  156.   old_y = 0;
  157.  
  158.   }
  159.    
  160.    sendMsg(count > 0?1:0);
  161.    count--;
  162.    if( count < 0 ) {
  163.      count = 0;
  164.    }
  165.  
  166. }
  167.  
  168. void sendMsg(int v1){
  169.   OscMessage myMessage = new OscMessage("/vvvv");
  170.  
  171.   myMessage.add(v1); // add an int to the osc message
  172.   //myMessage.add(v2); // add a float to the osc message
  173.   //myMessage.add(v3); // add a float to the osc message
  174.   //myMessage.add(v4); // add a float to the osc message
  175.   //myMessage.add(v5); // add a float to the osc message
  176.   myMessage.add(sendBPM);
  177.  
  178.  // send the message
  179.   oscP5.send(myMessage, myRemoteLocation);
  180.  
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement