Created
April 16, 2013 22:16
-
-
Save dan-mckay/5400121 to your computer and use it in GitHub Desktop.
Processing code for gesture based system
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SimpleOpenNI.*; | |
import ddf.minim.*; | |
SimpleOpenNI kinect; | |
Minim minim; | |
AudioSnippet player1; | |
AudioSnippet player2; | |
AudioSnippet player3; | |
AudioSnippet player4; | |
AudioSnippet player5; | |
AudioSnippet player6; | |
//This is where the sounds are loaded | |
String wav1 = "cat purr.wav"; | |
String wav2 = "lion3.wav"; | |
String wav3 = "laugh-8.wav"; | |
String wav4 = "cat purr.wav"; | |
String wav5 = "lion3.wav"; | |
String wav6 = "laugh-8.wav"; | |
PVector currentHand; | |
Boolean depthImg = false; //flag to display depthImg | |
//this is where the squares are drawn | |
PVector target1 = new PVector(100,120); | |
PVector target2 = new PVector(100,300); | |
PVector target3 = new PVector(280,120); | |
PVector target4 = new PVector(280,300); | |
PVector target5 = new PVector(460,120); | |
PVector target6 = new PVector(460,300); | |
int targetW = 80; //width of target | |
int targetH = 80; //height of target | |
//the colour of targets | |
color c1 = color(100, 0, 0); //Red | |
color c2 = color(175, 0, 0); //Green | |
color c3 = color(175, 0, 0); //Blue | |
color c4 = color(100, 0, 0); //Blue | |
color c5 = color(100, 0, 0); //Blue | |
color c6 = color(175, 0, 0); //Blue | |
Boolean target1Active = false; //stores if these targets are active | |
Boolean target2Active= false; | |
Boolean target3Active = false; | |
Boolean target4Active = false; | |
Boolean target5Active = false; | |
Boolean target6Active = false; | |
void setup() { | |
size(640, 500); | |
minim = new Minim(this); | |
player1 = minim.loadSnippet(wav1); | |
player2 = minim.loadSnippet(wav2); | |
player3 = minim.loadSnippet(wav3); | |
player4 = minim.loadSnippet(wav4); | |
player5 = minim.loadSnippet(wav5); | |
player6 = minim.loadSnippet(wav6); | |
currentHand = new PVector(0,0); | |
kinect = new SimpleOpenNI(this); | |
kinect.setMirror(true); | |
kinect.enableDepth(); | |
kinect.enableGesture(); | |
kinect.enableHands(); | |
kinect.addGesture("RaiseHand"); | |
//stroke(255,0,0); | |
//strokeWeight(2); | |
} | |
void draw() { | |
//draw background; | |
// | |
if (depthImg) { | |
image(kinect.depthImage(), 0,0); | |
} else { | |
background(0); | |
} | |
createTarget(target1, c1); | |
createTarget(target2, c2); | |
createTarget(target3, c3); | |
createTarget(target4, c4); | |
createTarget(target5, c5); | |
createTarget(target6, c6); | |
kinect.update(); | |
stroke(255,0,0); | |
strokeWeight(1); | |
ellipse (currentHand.x, currentHand.y, 25,25); | |
target1Active = checkInsideTarget(target1); | |
target2Active = checkInsideTarget(target2); | |
target3Active = checkInsideTarget(target3); | |
target4Active = checkInsideTarget(target4); | |
target5Active = checkInsideTarget(target5); | |
target6Active = checkInsideTarget(target6); | |
//set up the wav files; | |
playSounds(); | |
} | |
//stop method for minim | |
void stop() { | |
player1.close(); | |
player2.close(); | |
player3.close(); | |
player4.close(); | |
player5.close(); | |
player6.close(); | |
minim.stop(); | |
super.stop(); | |
} | |
//creates a Target based on the arguments of a Vector for targetPosition | |
// and color | |
//NEEDS WORK | |
void createTarget(PVector tPos, color c) { | |
//draw a hit region | |
//set the strokeWeight to 2 | |
strokeWeight(2); | |
//set the stroke to argument color c | |
stroke(c); | |
fill(c); | |
//create a rectangle at positon tPos.x, tPos.y with the widht and height of argument targetH and targetW | |
rect(tPos.x,tPos.y,targetW,targetW); | |
} | |
//checks if the hand is inside a Target returns true or false | |
Boolean checkInsideTarget(PVector tPos) { | |
if (currentHand.x > tPos.x && currentHand.x < tPos.x + targetW && currentHand.y > tPos.y && currentHand.y < tPos.y + targetH) { | |
println("inside target"); | |
return true; | |
} else { | |
return false; | |
} | |
} | |
//sets the currentWav based on the target1Active values and plays them using Minim | |
void playSounds() { | |
//create a if then statement that sets curWav to | |
//wav1, wav2 or wav3 depending on the state of target1Active, target2Active, target3Active | |
if (target1Active) { | |
player1.play(); | |
//rotateVector(target1); | |
} | |
if(!target1Active) { | |
player1.pause(); | |
player1.rewind(); | |
} | |
if (target2Active) { | |
player2.play(); | |
} | |
if(!target2Active) { | |
player2.pause(); | |
player2.rewind(); | |
} | |
if (target3Active) { | |
player3.play(); | |
} | |
if(!target3Active) { | |
player3.pause(); | |
player3.rewind(); | |
} | |
if (target4Active) { | |
player4.play(); | |
} | |
if(!target4Active) { | |
player4.pause(); | |
player4.rewind(); | |
} | |
if (target5Active) { | |
player5.play(); | |
} | |
if(!target5Active) { | |
player5.pause(); | |
player5.rewind(); | |
} | |
if (target6Active) { | |
player6.play(); | |
} | |
if(!target6Active) { | |
player6.pause(); | |
player6.rewind(); | |
} | |
} | |
//event required for hand detection in OpenNI | |
void onCreateHands(int handId, PVector position, float time) { | |
kinect.convertRealWorldToProjective(position, position); | |
currentHand = position; | |
} | |
void onUpdateHands(int handId, PVector position, float time) { | |
kinect.convertRealWorldToProjective(position, position); | |
currentHand = position; | |
} | |
void onDestroyHands(int handId, float time) { | |
kinect.addGesture("RaiseHands"); | |
} | |
void onRecognizeGesture (String strGesture, PVector idPosition, PVector endPosition) { | |
kinect.startTrackingHands(endPosition); | |
kinect.removeGesture("RaiseHands"); | |
} | |
//void rotateVector(PVector target) { | |
//target.rotate(1); | |
//} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment