Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import arb.soundcipher.*;
- import arb.soundcipher.constants.*;
- import oscP5.*;
- import netP5.*;
- OscP5 oscP5;
- OscP5 oscP52;
- NetAddress myRemoteLocation;
- NetAddress myRemoteLocation2;
- int old_x=0, old_y=0;
- int h;
- int x = 0;
- SoundCipher sc = new SoundCipher(this);
- // DATA INTERPRETING
- boolean lastHeartTrigger = false;
- boolean heartTrigger = false;
- float CHANGE_DECREASE = 0.8f;
- float changeVal = 0.f;
- float LAST_DATA_REDUCTION_RATE = 1f; //experiment with this, could be 0.5f
- int []lastDataVals = new int[3];
- int AVG_WINDOW_SIZE = 100;
- float AVG_WINDOW_THRES_FACTOR = 1.5f;
- float []avg_window = new float[AVG_WINDOW_SIZE];
- float window_avg = 0.f;
- int v1, v2, v3, v4, v5;
- int BPM;
- int savedTime;
- int totalTime = 15000;
- int sendBPM = 0;
- void updateLastDataVals(int val) {
- for( int i = lastDataVals.length-1 ; i >= 1 ; i-- ) {
- lastDataVals[i] = lastDataVals[i-1];
- }
- lastDataVals[0] = val;
- }
- void updateAvgWindow(float val) {
- for( int i = avg_window.length-1 ; i >= 1 ; i-- ) {
- avg_window[i] = avg_window[i-1];
- }
- avg_window[0] = val;
- float avg = 0.f;
- for( int i = 0 ; i < avg_window.length ; i++ ) {
- avg += avg_window[i];
- }
- window_avg = avg / (float)avg_window.length;
- }
- float getDataDirection() {
- float dataImportance = 1.f;
- float dir = 0.f;
- for( int i = 0 ; i < lastDataVals.length-1 ; i++ ) {
- dir += abs(lastDataVals[i] - lastDataVals[i+1]) * dataImportance;
- dataImportance *= LAST_DATA_REDUCTION_RATE;
- }
- return dir;
- }
- void setup()
- {
- background(208,24,24);
- oscP5 = new OscP5(this,1040);
- myRemoteLocation = new NetAddress("127.0.0.1",2040);
- size(displayWidth/2, displayHeight/2);
- h = displayHeight/2;
- savedTime = millis();
- }
- void draw()
- {
- int passedTime = millis() - savedTime;
- if (passedTime > totalTime) {
- print("Time " + millis()/1000);
- println(" BPM " + BPM*4);
- sendBPM = BPM*4;
- BPM = 0;
- savedTime = millis();
- }
- }
- boolean newDataVal(int val) {
- //print(val + ",");
- updateLastDataVals(val);
- float lastChangeVal = changeVal;
- changeVal *= CHANGE_DECREASE;
- changeVal += getDataDirection();
- //println("change val: "+changeVal);
- float adjustedVal = (changeVal + (lastChangeVal*0.5f))/3.f;
- //println("adj val: "+adjustedVal);
- updateAvgWindow(adjustedVal);
- lastHeartTrigger = heartTrigger;
- heartTrigger = adjustedVal >= (window_avg*AVG_WINDOW_THRES_FACTOR);
- return heartTrigger && heartTrigger != lastHeartTrigger;
- }
- int COUNT_NUMBER = 10;
- int count = 0;
- void oscEvent(OscMessage theOscMessage) {
- int v1 = theOscMessage.get(2).intValue();
- //v2 = theOscMessage.get(3).intValue();
- //v3 = theOscMessage.get(0).intValue();
- //v4 = theOscMessage.get(1).intValue();
- //v5 = theOscMessage.get(4).intValue();
- boolean trigger = newDataVal(v1);
- strokeWeight(12);//beef up our white line
- stroke(255,128,128);//make the line white
- float val = map(v1, 200, 800, 0, h);
- float avg = map(window_avg, 200, 800, 0, h);
- //println("avg: "+window_avg);
- line(old_x,old_y, x, h-int(val));
- //stroke(0,255,255);
- //line(0, avg, width, avg);
- if( trigger ) {
- BPM ++;
- //sc.playNote(100, 100, 1.0);
- stroke(255);
- line(x,0,x,displayHeight/4);
- count = COUNT_NUMBER;
- }
- old_x = x;
- old_y = h-int(val);
- x=x+10;
- if(x>(displayWidth/2)){
- background(208,24,24); //refresh the screen, erases everything
- x=0; //make the increment back to 0,
- //but used 50, so it sweeps better into the screen
- //reset the old x,y values
- old_x = x;
- old_y = 0;
- }
- sendMsg(count > 0?1:0);
- count--;
- if( count < 0 ) {
- count = 0;
- }
- }
- void sendMsg(int v1){
- OscMessage myMessage = new OscMessage("/vvvv");
- myMessage.add(v1); // add an int to the osc message
- //myMessage.add(v2); // add a float to the osc message
- //myMessage.add(v3); // add a float to the osc message
- //myMessage.add(v4); // add a float to the osc message
- //myMessage.add(v5); // add a float to the osc message
- myMessage.add(sendBPM);
- // send the message
- oscP5.send(myMessage, myRemoteLocation);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement