Drawing Out The Light [source code]

import SimpleOpenNI.*;
import themidibus.*;

MidiBus myBus;

// Kinect Variables
SimpleOpenNI context;
float        zoomF = 0.6f;
float        rotX = radians(180); 
float        rotY = radians(0);

PVector      lineSubOne = new PVector (0, 0, 0);
public int[] pXYZ;

// Variables for Fades
float        counterOne = 254;
float        counterTwo = 254;
float        counterThree = 0;
float        counterFour = 0;

// Variables for Images
PImage[]     img;
int          imgCounter = 0;
float        weightMod = 1;
float        colorVal;
float        rand;

// Variables for Scene Flow
int sceneCount = 1;
int transitionOne = 0;
int transitionTwo = 0;
int transitionThree = 0;
int transitionFour = 0;
int transitionFive = 0;

// Fullscreen
boolean sketchFullScreen() {
  return true;
}

void setup() {
  size(displayWidth, displayHeight, P3D);
  background(0);
  strokeCap(ROUND);
  
  // Images  
  img = new PImage[31];
  for (int i=0; i < img.length; i++) {
    img[i] = loadImage(i + ".jpg");
  }
  
  // Midi
  MidiBus.list();
  myBus = new MidiBus(this, -1, "LoopBe Internal MIDI");
  
  // Kinect
  context = new SimpleOpenNI(this);
  context.setMirror(true);

  if(context.enableDepth() == false) {
     println("Can't open the depthMap, maybe the camera is not connected!"); 
     exit();
     return;
  }
  
  context.alternativeViewPointDepthToImage();

  perspective(radians(45), float(width)/float(height), 10, 150000);
           
  context.enableUser(SimpleOpenNI.SKEL_PROFILE_ALL);
  pXYZ = new int[6];
  context.enableScene();
  
  myBus.sendControllerChange(0, 41, 127);
  myBus.sendControllerChange(0, 41, 0);
  delay(25);
  myBus.sendControllerChange(0, 41, 127);
  
  myBus.sendControllerChange(0, 42, 0);
  myBus.sendControllerChange(0, 42, 127);
  delay(25);
  myBus.sendControllerChange(0, 42, 0);
  
  myBus.sendControllerChange(0, 43, 0);
  myBus.sendControllerChange(0, 43, 127);
  delay(25);
  myBus.sendControllerChange(0, 43, 0);
  
  myBus.sendControllerChange(0, 44, 0);
  myBus.sendControllerChange(0, 44, 127);
  delay(25);
  myBus.sendControllerChange(0, 44, 0);
  
  myBus.sendControllerChange(0, 45, 0);
  myBus.sendControllerChange(0, 45, 127);
  delay(25);
  myBus.sendControllerChange(0, 45, 0);
}

void draw() {
  context.update();
  background(0,50);
  
  translate(width/2, height/2, 0);
  rotateX(rotX);
  rotateY(rotY);
  scale(zoomF);
  translate(0, 0, -1000);
  
  for (int i=1; i<=10; i++) {
    if(context.isTrackingSkeleton(i)) {
      skeletonData(i);
    }
  }
} 
  
void sceneFlow(int rX, int rY, int rZ, int lX, int lY, int lZ, int rlX, int[] pXYZ) {
  if (sceneCount == 1) {
    sceneOne(rX,rY,rZ,lX,lY,lZ,rlX);
    sceneOneVis(rlX);
    boolean once = true;
    
    if (rlX > 110) {                                              // if arms outstretched
      transitionOne += 5;
      myBus.sendControllerChange(0, 42, transitionOne);          // SceneTwo Volume Up
      myBus.sendControllerChange(0, 41, (127 - transitionOne));  // SceneOne Volume Down
      
      println("rX: " + rX + " rY: " + rY + " rZ: " + rZ);
      println("rlX: " + rlX);  
      println("transitionOne: " + transitionOne);
      
      if (transitionOne > 127) {
        sceneCount++;
        myBus.sendNoteOn(16, 52, 127);                           // SceneTwo LooperStart | SceneTwoOsc Play
        myBus.sendNoteOff(16, 52, 127);
        
        myBus.sendNoteOn(12, 61, 127);                           // SceneOne Disarm       
        myBus.sendNoteOff(12, 61, 127);
      }
    }
  } else if (sceneCount == 2) {
    sceneTwo(rX,rY,rZ,lX,lY,lZ,rlX);
    sceneTwoVis(rlX);
    boolean once = true;
    
    if (rY > 90 && lY > 90) {                                    // if arms raised
      transitionTwo += 5;
      myBus.sendControllerChange(0, 43, transitionOne);          // SceneThree Volume Up
      myBus.sendControllerChange(0, 42, (127 - transitionTwo));  // SceneTwo Volume Down
      
      if (transitionTwo > 127) {
        sceneCount++;
        myBus.sendNoteOn(16, 53, 127);                           // SceneThree LooperStart 
                                                                 // SceneTwoOsc Stop
        myBus.sendNoteOff(16, 53, 127);
        
        myBus.sendNoteOn(12, 62, 127);                           // SceneTwo Disarm       
        myBus.sendNoteOff(12, 62, 127);
      }  
    }
  } else if (sceneCount == 3) {
    sceneThree(rX,rY,rZ,lX,lY,lZ,rlX,pXYZ);
    sceneThreeVis();
    boolean once = true;
    
    if (rZ > 85 && lZ > 85) {                                         // if arms foreward
      transitionThree += 5;
      myBus.sendControllerChange(0, 44, transitionThree);         // SceneFour Volume Up
      myBus.sendControllerChange(0, 43, (127 - transitionThree)); // SceneThree Volume Down
      
      if (transitionThree > 127) {
        sceneCount++;
        myBus.sendNoteOn(16, 54, 127);                             // SceneFour LooperStart 
                                                                   // SceneThree Looper Play
        myBus.sendNoteOff(16, 54, 127);
        
        myBus.sendNoteOn(12, 63, 127);                             // SceneThree Disarm      
        myBus.sendNoteOff(12, 63, 127);
      }          
    }
  } else if (sceneCount == 4) {      
    sceneFour(rX,rY,rZ,lX,lY,lZ,rlX,pXYZ);
    sceneFourVis(lX);
    boolean once = true;
      
    if (rlX > 60 && rY > 90 && lY > 90) {                          // arms up and out
      transitionFour += 10;
      myBus.sendControllerChange(0, 45, transitionFour);           // SceneFive Volume Up
      myBus.sendControllerChange(0, 44, (127 - transitionFour));   // SceneFour Volume Down
      
      if (transitionFour > 127) {
        sceneCount++;
        myBus.sendNoteOn(16, 55, 127);                             // SceneFour Beat Repeat On
                                                                   // SceneFour Looper Play
                                                                   // SceneFiveOsc Play
        myBus.sendNoteOff(16, 55, 127);
        
        myBus.sendNoteOn(12, 64, 127);                           // SceneFour Disarm       
        myBus.sendNoteOff(12, 64, 127);
        
      }
    }
  } else if (sceneCount == 5) {
    sceneFive(rX,rY,rZ,lX,lY,lZ,rlX,pXYZ);
    sceneFiveVis(rZ,lZ);
    
    if (rlX < 10 && rZ > 90 && lZ > 90) {                        // arms up and hands crossed
      transitionFive += 5;
      if (transitionFive > 127) {
        myBus.sendNoteOn(12, 66, 127);                           // Stop signal
        myBus.sendNoteOff(12, 66, 127);
        exit();
      }
    }
  }
}

void sceneOneVis(int rlX) {
  
  int[]   depthMap = context.depthMap();
  int     steps   = 5;
  int     index;
  PVector realWorldPoint;
  
  int userCount = context.getNumberOfUsers();
  int[] userMap = null;
  
  if(userCount > 0) {
    userMap = context.getUsersPixels(SimpleOpenNI.USERS_ALL);
  }
  
  for (int y=0; y < context.depthHeight(); y+=steps) {
    for (int x=0; x < context.depthWidth(); x += steps) {
      index = x + y * context.depthWidth();
      if (depthMap[index] > 0) {
        realWorldPoint = context.depthMapRealWorld()[index];
         if(userMap != null && userMap[index] != 0) {
          strokeWeight(random(5, (rlX/5)));
          stroke(255, random(5, 255));
          
          realWorldPoint.x = random(realWorldPoint.x-(50), realWorldPoint.x);
          realWorldPoint.y = random(realWorldPoint.y-(50), realWorldPoint.y);
          realWorldPoint.z = random(realWorldPoint.z-(50), realWorldPoint.z);
         
          point(realWorldPoint.x, realWorldPoint.y, realWorldPoint.z);
          point(realWorldPoint.x,realWorldPoint.y,realWorldPoint.z);
        }
      }
    }
  }
}

void sceneTwoVis(int rlX) {

  int[]   depthMap = context.depthMap();
  int     steps   = 4;
  int     index;
  PVector realWorldPoint;
  
  int userCount = context.getNumberOfUsers();
  int[] userMap = null;
  
  if(userCount > 0) {
    userMap = context.getUsersPixels(SimpleOpenNI.USERS_ALL);
  }
  
  for (int y=0; y < context.depthHeight(); y+=steps) {
    lineSubOne.x = 0;
    lineSubOne.y = 0;
    lineSubOne.z = 0;
    for (int x=0; x < context.depthWidth(); x += steps) {
      index = x + y * context.depthWidth();
      if (depthMap[index] > 0) {
        realWorldPoint = context.depthMapRealWorld()[index];
         if(userMap != null && userMap[index] != 0) {
          
          if (random(1) < .05) {
            strokeWeight(random(75, 300));
            stroke(255, random(50)); 
            point(random(realWorldPoint.x),random(realWorldPoint.y),random(realWorldPoint.z*.5, realWorldPoint.z*8));
          }

          realWorldPoint.x = random(realWorldPoint.x-(50), realWorldPoint.x);
          realWorldPoint.y = random(realWorldPoint.y-(50), realWorldPoint.y);
          realWorldPoint.z = random(realWorldPoint.z-(50), realWorldPoint.z);
         
          strokeWeight(random(5, 5+(rlX/3)));
          stroke(255, random(125, 255));
          point(realWorldPoint.x, realWorldPoint.y, realWorldPoint.z);
        }
      }
    }
  }
}

void sceneThreeVis() {
  int[]   depthMap = context.depthMap();
  int     steps   = 2;
  int     index;
  PVector realWorldPoint;

  int userCount = context.getNumberOfUsers();
  int[] userMap = null;
  
  if(userCount > 0) {
    userMap = context.getUsersPixels(SimpleOpenNI.USERS_ALL);
  }

  for (int y=0; y < context.depthHeight(); y+=steps) {
    lineSubOne.x = 0;
    lineSubOne.y = 0;
    lineSubOne.z = 0;
    
    for (int x=0; x < context.depthWidth(); x += steps) {
      index = x + y * context.depthWidth();
      if (depthMap[index] > 0) {
        realWorldPoint = context.depthMapRealWorld()[index];
         if(userMap != null && userMap[index] != 0) {          
            if (random(1) < .02) {
              strokeWeight(random(75, 300));
              stroke(255, random(25)); 
              point(random(realWorldPoint.x),random(realWorldPoint.y),random(realWorldPoint.z*10));
            }
            
            strokeWeight(4);
            stroke(255, 255);
              line(lineSubOne.x, lineSubOne.y, lineSubOne.z, 
                 realWorldPoint.x, realWorldPoint.y, realWorldPoint.z);
                 
              strokeWeight(15);
              stroke(255, 5);
              line(lineSubOne.x, lineSubOne.y, (lineSubOne.z + 5), 
                 realWorldPoint.x, realWorldPoint.y, (realWorldPoint.z + 5));
         }
         lineSubOne = realWorldPoint;
      }
    }
  }
}

void sceneFourVis(int lX) {
  int[]   depthMap = context.depthMap();
  int     steps   = 2;
  int     index;
  PVector realWorldPoint;
  color   c = 0;

  int userCount = context.getNumberOfUsers();
  int[] userMap = null;
  
  if(userCount > 0) {
    userMap = context.getUsersPixels(SimpleOpenNI.USERS_ALL);
  }
  
  imgCounter = int(random(0,img.length-1));
  
  for (int y=0; y < context.depthHeight(); y+=steps) {
    lineSubOne.x = 0;
    lineSubOne.y = 0;
    lineSubOne.z = 0;
            
    for (int x=0; x < context.depthWidth(); x += steps) {
      index = x + y * context.depthWidth();
      if (depthMap[index] > 0) {
        realWorldPoint = context.depthMapRealWorld()[index];
         if(userMap != null && userMap[index] != 0) {
          if (abs(realWorldPoint.x - lineSubOne.x) < 45 && abs(realWorldPoint.z - lineSubOne.z) < 45) {
            c = img[imgCounter].get(int(abs(realWorldPoint.x/2)), 
                                    int(abs(realWorldPoint.y/2)));
            if (random(1) < .1) {
              strokeWeight(random(50, 150));
              stroke(c, random(50+(lX/4))); 
              point(random(realWorldPoint.x),random(realWorldPoint.y),random(realWorldPoint.z*2));
            }
            strokeWeight(2);
            stroke(c, 255);
            line(lineSubOne.x, lineSubOne.y, lineSubOne.z, 
                 realWorldPoint.x, realWorldPoint.y, realWorldPoint.z);
  
            strokeWeight(25);
            stroke(c, 25);
            line(lineSubOne.x, lineSubOne.y, (lineSubOne.z + 15), 
                 realWorldPoint.x, realWorldPoint.y, (realWorldPoint.z + 15));
          }
        }
        lineSubOne = realWorldPoint;
      }
    }
  }
}

void sceneFiveVis(int rZ, int lZ) {
  int[]   depthMap = context.depthMap();
  int     steps   = 2;
  int     index;
  PVector realWorldPoint;
  color   c = 0;

  int userCount = context.getNumberOfUsers();
  int[] userMap = null;
  
  if(userCount > 0) {
    userMap = context.getUsersPixels(SimpleOpenNI.USERS_ALL);
  }
  
  imgCounter = int(random(0,img.length-1));
  
  for (int y=0; y < context.depthHeight(); y+=steps) {
    lineSubOne.x = 0;
    lineSubOne.y = 0;
    lineSubOne.z = 0;
    
    for (int x=0; x < context.depthWidth(); x += steps) {
      index = x + y * context.depthWidth();
      if (depthMap[index] > 0) {
        realWorldPoint = context.depthMapRealWorld()[index];
         if(userMap != null && userMap[index] != 0) {
            c = img[imgCounter].get(int(abs(realWorldPoint.x/2)), 
                                    int(abs(realWorldPoint.y/2)));
            rand = random(1);
            if (rand < .1) {
              strokeWeight(random(25,rZ*4));
              stroke(c, random(10+(lZ/2))); 
              point(random(realWorldPoint.x),random(realWorldPoint.y),random(realWorldPoint.z*4));
            }
            
            strokeWeight(5);
            stroke(c, 255);
            line(lineSubOne.x, lineSubOne.y, lineSubOne.z, 
                 realWorldPoint.x, realWorldPoint.y, realWorldPoint.z);
            
            stroke(c, random(50));
            line(lineSubOne.x, lineSubOne.y, (lineSubOne.z + 15), 
                 realWorldPoint.x, realWorldPoint.y, (realWorldPoint.z + 15));
                 
            strokeWeight(lZ);
            stroke(255, random(35));
            line(lineSubOne.x, lineSubOne.y, (lineSubOne.z + lZ), 
            realWorldPoint.x, realWorldPoint.y, (realWorldPoint.z + lZ));
        }
        lineSubOne = realWorldPoint;
      }
    }
  }
}

void sceneOne(int rX,int rY,int rZ,int lX,int lY,int lZ,int rlX) {
  // Right Hand (x,y,z)
  myBus.sendControllerChange(0, 0, rX); // Panning
  myBus.sendControllerChange(0, 1, rY); // Filter Frequency
  
  if (rZ > 110) {                       // Trigger for Resonator Gate
    myBus.sendNoteOn(0,40,127);
    myBus.sendNoteOff(0,40,127);
  } 
  
  // Left Hand (x,y,z)
  myBus.sendControllerChange(0, 2, lY);  // Frequency Shifter Frequency
  myBus.sendControllerChange(0, 20, lZ);  // Send C to Frequency Shifter
}



void sceneTwo(int rX,int rY,int rZ,int lX,int lY,int lZ,int rlX) {
  // Right Hand (x,y,z)
  myBus.sendControllerChange(0, 4, rX);   // Octave Channel Vol. (0dB to -24.dB)
  myBus.sendControllerChange(0, 5, rY);   // Reverb Channel Vol. (-9dB to -24dB)
                                          // Tremol Channel Vol. (-inf.dB to 0dB)
  
  // Left Hand (x,y,z)
  myBus.sendControllerChange(0, 6, lX);   // Fade Vocal Stereo Width 200% to 100%
  myBus.sendControllerChange(0, 2, lY);   // Oscillator Frequency
                                          // Frequency Shifter Frequency
  myBus.sendControllerChange(0, 21, lZ);   // Send C to Frequency Shifter
  
  // Left-Right Hand Distance
  myBus.sendControllerChange(0, 7, rlX); // Fade Ambient Stereo Width 0% to 125%
}

void sceneThree(int rX,int rY,int rZ,int lX,int lY,int lZ,int rlX, int [] pXYZ) {
  int speed = 15;
  
  // Triggers for Hand Movement Gates
  if (abs(pXYZ[0] - rX) > speed ) {
    myBus.sendNoteOn(1,61,(abs(pXYZ[0] - rX)));          // Rigth X Trigger;
    myBus.sendNoteOff(1,61,(abs(pXYZ[0] - rX)));
  } else if (abs(pXYZ[1] - rY) > speed) {
    myBus.sendNoteOn(1,61,(abs(pXYZ[1] - rY)));          // Rigth Y Trigger;
    myBus.sendNoteOff(1,61,(abs(pXYZ[1] - rY)));
  } else if (abs(pXYZ[2] - rZ) > speed) {
    myBus.sendNoteOn(1,61,(abs(pXYZ[2] - rZ)));          // Rigth Z Trigger;
    myBus.sendNoteOff(1,61,(abs(pXYZ[2] - rZ)));
  } else if (abs(pXYZ[3] - lX) > speed) {
    myBus.sendNoteOn(2,61,(abs(pXYZ[3] - lX)));          // Left X Trigger;
    myBus.sendNoteOff(2,61,(abs(pXYZ[3] - lX)));
  } else if (abs(pXYZ[4] - lY) > speed) {
    myBus.sendNoteOn(2,61,(abs(pXYZ[4] - lY)));          // Left Y Trigger;
    myBus.sendNoteOff(2,61,(abs(pXYZ[4] - lY)));
  } else if (abs(pXYZ[5] - lZ) > speed) {
    myBus.sendNoteOn(2,61,(abs(pXYZ[5] - lZ)));          // Left Z Trigger;
    myBus.sendNoteOff(2,61,(abs(pXYZ[5] - lZ)));
  }
  
  // Right Hand (x,y,z)
  myBus.sendControllerChange(0, 0, rX); // Panning for Vocals
                                        // Panning for Right Clicks
                                        
  myBus.sendControllerChange(0, 1, rY); // Vocal Filter Frequency
                                        // Filter for Right Clicks
                                         
  if (rZ > 110) {                       // Trigger for Resonator Gate
    myBus.sendNoteOn(0,40,127);
    myBus.sendNoteOff(0,40,127);
  } 
  
  // Left Hand (x,y,z)
  myBus.sendControllerChange(0, 8, lX);  // Panning for Left Clicks
  myBus.sendControllerChange(0, 2, lY);  // Frequency Shifter Frequency
                                         // Filter for Left Clicks
                                          
  myBus.sendControllerChange(0, 22, lZ);  // Send C to Frequency Shifter
  
  // Left-Right Hand Distance
  myBus.sendControllerChange(0, 7, rlX);  // Fade Ambient Stereo Width 0% to 125%
  
  // Assign Previous (x,y,z) Values
  pXYZ[0] = rX;
  pXYZ[1] = rY;
  pXYZ[2] = rZ;
  pXYZ[3] = lX;
  pXYZ[4] = lY;
  pXYZ[5] = lZ;
}

void sceneFour(int rX,int rY,int rZ,int lX,int lY,int lZ,int rlX, int[] pXYZ) {
  int speed = 15;
  
  // Triggers for Hand Movement Gates
  if (abs(pXYZ[0] - rX) > speed ) {
    myBus.sendNoteOn(1,61,(abs(pXYZ[0] - rX)));          // Rigth X Trigger;
    myBus.sendNoteOff(1,61,(abs(pXYZ[0] - rX)));
  } else if (abs(pXYZ[1] - rY) > speed) {
    myBus.sendNoteOn(1,61,(abs(pXYZ[1] - rY)));          // Rigth Y Trigger;
    myBus.sendNoteOff(1,61,(abs(pXYZ[1] - rY)));
  } else if (abs(pXYZ[2] - rZ) > speed) {
    myBus.sendNoteOn(1,61,(abs(pXYZ[2] - rZ)));          // Rigth Z Trigger;
    myBus.sendNoteOff(1,61,(abs(pXYZ[2] - rZ)));
  } else if (abs(pXYZ[3] - lX) > speed) {
    myBus.sendNoteOn(2,61,(abs(pXYZ[3] - lX)));          // Left X Trigger;
    myBus.sendNoteOff(2,61,(abs(pXYZ[3] - lX)));
  } else if (abs(pXYZ[4] - lY) > speed) {
    myBus.sendNoteOn(2,61,(abs(pXYZ[4] - lY)));          // Left Y Trigger;
    myBus.sendNoteOff(2,61,(abs(pXYZ[4] - lY)));
  } else if (abs(pXYZ[5] - lZ) > speed) {
    myBus.sendNoteOn(2,61,(abs(pXYZ[5] - lZ)));          // Left Z Trigger;
    myBus.sendNoteOff(2,61,(abs(pXYZ[5] - lZ)));
  }
  
  // Right Hand (x,y,z)
  myBus.sendControllerChange(0, 0, rX); // Panning
                                        // Panning for Right Clicks
                                        
  myBus.sendControllerChange(0, 1, rY); // Filter Frequency
                                        // Filter for Right Clicks
  
  if (rZ > 110) {                        // Trigger for Stutter Edit
    if (abs(pXYZ[1] - rY) < 20) {
      rY = pXYZ[1];
    }
    myBus.sendNoteOn(4,rY,127);
    
    if (rY != pXYZ[1]) { 
      myBus.sendNoteOff(4,pXYZ[1],127);
    }
  } else {
    myBus.sendNoteOff(4,rY,127);
  }
  
  // Left Hand (x,y,z)
  myBus.sendControllerChange(0, 8, lX);  // Panning for Left Clicks
  
  myBus.sendControllerChange(0, 2, lY);  // Frequency Shifter Frequency
  myBus.sendControllerChange(0, 23, lZ);  // Send C to Frequency Shifter
  
  if (lZ > 80) {                         // Trigger for Stutter Edit
    if (abs(pXYZ[4] - lY) < 20) {
      lY = pXYZ[4];
    }
    myBus.sendNoteOn(4,lY,127);
    
    if (lY != pXYZ[4]) { 
      myBus.sendNoteOff(4,pXYZ[4],127);
    }
  } else {
    myBus.sendNoteOff(4,lY,127);
  }
  
  // Left-Right Hand Distance
  myBus.sendControllerChange(0, 7, rlX);  // Fade Ambient Stereo Width 0% to 125%
  
  // Assign Previous (x,y,z) Values
  pXYZ[0] = rX;
  pXYZ[1] = rY;
  pXYZ[2] = rZ;
  pXYZ[3] = lX;
  pXYZ[4] = lY;
  pXYZ[5] = lZ;
}

void sceneFive(int rX,int rY,int rZ,int lX,int lY,int lZ,int rlX, int[] pXYZ) {
  int speed = 15;
  
  // Triggers for Hand Movement Gates
  if (abs(pXYZ[0] - rX) > speed ) {
    myBus.sendNoteOn(1,61,(abs(pXYZ[0] - rX)));          // Rigth X Trigger;
    myBus.sendNoteOff(1,61,(abs(pXYZ[0] - rX)));
  } else if (abs(pXYZ[1] - rY) > speed) {
    myBus.sendNoteOn(1,61,(abs(pXYZ[1] - rY)));          // Rigth Y Trigger;
    myBus.sendNoteOff(1,61,(abs(pXYZ[1] - rY)));
  } else if (abs(pXYZ[2] - rZ) > speed) {
    myBus.sendNoteOn(1,61,(abs(pXYZ[2] - rZ)));          // Rigth Z Trigger;
    myBus.sendNoteOff(1,61,(abs(pXYZ[2] - rZ)));
  } else if (abs(pXYZ[3] - lX) > speed) {
    myBus.sendNoteOn(2,61,(abs(pXYZ[3] - lX)));          // Left X Trigger;
    myBus.sendNoteOff(2,61,(abs(pXYZ[3] - lX)));
  } else if (abs(pXYZ[4] - lY) > speed) {
    myBus.sendNoteOn(2,61,(abs(pXYZ[4] - lY)));          // Left Y Trigger;
    myBus.sendNoteOff(2,61,(abs(pXYZ[4] - lY)));
  } else if (abs(pXYZ[5] - lZ) > speed) {
    myBus.sendNoteOn(2,61,(abs(pXYZ[5] - lZ)));          // Left Z Trigger;
    myBus.sendNoteOff(2,61,(abs(pXYZ[5] - lZ)));
  }
  
  // Right Hand (x,y,z)
  myBus.sendControllerChange(0, 0, rX); // Send D to Right Ambience
                                        // Panning for Right Clicks
                                        
  myBus.sendControllerChange(0, 1, rY); // Filter Frequency
                                        // Filter for Right Clicks
                                        // Vocal Spray Send
                                        // Filter for SceneFiveOsc Frequency Shifter
                                        
  myBus.sendControllerChange(0, 10, rZ); // Vocal Spray Feedback
  
  if (rZ > 110) {                        // Trigger for Stutter Edit
    if (abs(pXYZ[1] - rY) < 35) {
      rY = pXYZ[1];
    }
    myBus.sendNoteOn(5,rY,127);
    
    if (rY != pXYZ[1]) { 
      myBus.sendNoteOff(5,pXYZ[1],127);
    }
  } else {
    myBus.sendNoteOff(5,rY,127);
  }
  
  // Left Hand (x,y,z)
  myBus.sendControllerChange(0, 8, lX); // Send D to Left Ambience
                                        // Panning for Left Clicks
  
  myBus.sendControllerChange(0, 2, lY);  // Frequency Shifter Frequency
                                         // Filter for Left Clicks
  myBus.sendControllerChange(0, 24, lZ);  // Send C to Frequency Shifter
                                         // Send D for SceneFiveOsc

  
  if (lZ > 110) {                         // Trigger for Stutter Edit
    if (abs(pXYZ[4] - lY) < 35) {
      lY = pXYZ[4];
    }
    myBus.sendNoteOn(5,lY,127);
    
    if (lY != pXYZ[4]) { 
      myBus.sendNoteOff(5,pXYZ[4],127);
    }
  } else {
    myBus.sendNoteOff(5,lY,127);
  }
  
  // Left-Right Hand Distance
  myBus.sendControllerChange(0, 9, rlX);  // SceneFiveOsc Bit Crusher (50 to 0)
  
  // Assign Previous (x,y,z) Values
  pXYZ[0] = rX;
  pXYZ[1] = rY;
  pXYZ[2] = rZ;
  pXYZ[3] = lX;
  pXYZ[4] = lY;
  pXYZ[5] = lZ;
}

void skeletonData(int userId) {
    
    PVector comPos = new PVector();
    context.getCoM(userId, comPos);
    pushStyle();
    stroke(255, 1);
    strokeWeight(350);
    point(comPos.x, comPos.y, (comPos.z + 50));
    pointLight(255, 255, 255, comPos.x, comPos.y, (comPos.z + 50));
    context.convertRealWorldToProjective(comPos, comPos); 
  
    PVector rightHandPos = new PVector();
    context.getJointPositionSkeleton(userId, SimpleOpenNI.SKEL_RIGHT_HAND, rightHandPos);
    context.convertRealWorldToProjective(rightHandPos, rightHandPos);
    
    PVector leftHandPos = new PVector();
    context.getJointPositionSkeleton(userId, SimpleOpenNI.SKEL_LEFT_HAND, leftHandPos);
    context.convertRealWorldToProjective(leftHandPos, leftHandPos);
    
    normDistances(userId, comPos, rightHandPos, leftHandPos);
    rotX = radians(220 - (rightHandPos.y/height)*80);
}

void normDistances(int userId, PVector comPos, PVector rightHandPos, PVector leftHandPos) {
    // Method Variables
    int rX, rY, rZ, lX, lY, lZ, rlX;
    
     // Normalizing Distances to 0-127 Scale for MIDI
     
     // Right (x,y,z)
     if (rightHandPos.x > comPos.x) {
       rX = int( (rightHandPos.x - comPos.x)*127/190 );
     } else {
       rX = 0;
     }
    
    if (comPos.y > rightHandPos.y) {
      rY = int( ((comPos.y - rightHandPos.y)*127)/270 );
    } else {
      rY = 0;
    }
    
    if (comPos.z > rightHandPos.z) {
      rZ = int( ((comPos.z - rightHandPos.z)*127)/550 );
    } else {
      rZ = 0;
    }
    
     // Left (x,y,z)
     if (comPos.x > leftHandPos.x) {
       lX = int( (comPos.x - leftHandPos.x)*127/190 );
     } else {
       lX = 0;
     }
     
    if (comPos.y > leftHandPos.y) {
      lY = int( ((comPos.y - leftHandPos.y)*127)/270 );
    } else {
      lY = 0;
    }
    
    if (comPos.z > leftHandPos.z) {
      lZ = int( ((comPos.z - leftHandPos.z)*127)/550 );
    } else {
      lZ = 0;
    }
        
    // Right/Left (x)
    if (rightHandPos.x > leftHandPos.x) {
      rlX = int( ((rightHandPos.x - leftHandPos.x)*127)/420 ); 
    } else {
      rlX = 0;
    }
    
    zoomF = 1.5 + (rZ/32);
    sceneFlow(rX,rY,rZ,lX,lY,lZ,rlX,pXYZ);
}

void delay(int time) {
  int current = millis();
  while(millis() < current+time) Thread.yield();
}

// Event-based Methods
void onNewUser(int userId) {
  println("New User Detected - userId: " + userId);
  context.startPoseDetection("Psi",userId);
}

void onLostUser(int userId) {
  println("User Lost - userId: " + userId);
}
 
void onStartPose(String pose,int userId) {
  println("Start of Pose Detected  - userId: " + userId + ", pose: " + pose);
  context.stopPoseDetection(userId); 
  context.requestCalibrationSkeleton(userId, true); 
}
 
void onStartCalibration(int userId) {
  println("Beginning Calibration - userId: " + userId);
}
 
void onEndCalibration(int userId, boolean successfull) {
  println("Calibration of userId: " + userId + ", successfull: " + successfull);
 
  if (successfull) { 
    println("  User calibrated !!!");
    context.startTrackingSkeleton(userId); 
  } 
  else { 
    println("  Failed to calibrate user !!!");
    context.startPoseDetection("Psi",userId);
  }
}
  1. zaghack reblogged this from avmovement
  2. avmovement posted this