Here is my code for my project...
/* --------------------------------------------------------------------------
* SimpleOpenNI AlternativeViewpoint3d and loop sampler
* --------------------------------------------------------------------------
* Processing Wrapper for the OpenNI/Kinect library
* http://code.google.com/p/simple-openni
* --------------------------------------------------------------------------
* prog: Rob Weidner / http://weidscreen.com
* date: 05/08/2013 (m/d/y)
* ----------------------------------------------------------------------------
*/
// Always starts the sketch in full screen mode.
boolean sketchFullScreen() {
return true;
}
import librarytests.*;
import org.openkinect.*;
import org.openkinect.processing.*;
import SimpleOpenNI.*;
import ddf.minim.*;
import ddf.minim.analysis.*;
// Initialize global variables
Minim minim;
AudioSample kick;
AudioSample snare;
AudioPlayer drum;
AudioPlayer guitar;
AudioSample radio;
AudioPlayer strings;
AudioPlayer reggae;
FFT drumFFT;
FFT guitarFFT;
FFT reggaeFFT;
FFT stringsFFT;
FFT radioFFT;
FFT kickFFT;
FFT snareFFT;
SimpleOpenNI context;
Kinect kinect;
KinectTracker tracker;
Hotpoint snareTrigger;
Hotpoint kickTrigger;
Hotpoint drumTrigger;
Hotpoint guitarTrigger;
Hotpoint reggaeTrigger;
Hotpoint stringsTrigger;
Hotpoint radioTrigger;
Hotpoint pauseTrigger;
float zoomF =0.3f;
float rotX = radians(180); // by default rotate the hole scene 180deg around the x-axis,
// the data from openni comes upside down
float rotY = radians(0);
void setup()
{
size(displayWidth, displayHeight, P3D);
context = new SimpleOpenNI(this);
minim = new Minim(this);
// load audio files
snare = minim.loadSample("BD.mp3", displayWidth);
if ( snare == null) println("Didn't get hat!");
snareFFT = new FFT(snare.bufferSize(), snare.sampleRate());
kick = minim.loadSample("SD.wav", displayWidth);
if ( kick == null) println("Didn't get kick!");
kickFFT = new FFT(kick.bufferSize(), kick.sampleRate());
drum = minim.loadFile("modernDrum.mp3", displayWidth);
if ( drum == null) println("Didn't get drum!");
drumFFT = new FFT(drum.bufferSize(), drum.sampleRate());
radio = minim.loadSample("radio.mp3", displayWidth);
if ( radio == null) println("Didn't get radio!");
radioFFT = new FFT(radio.bufferSize(), radio.sampleRate());
guitar = minim.loadFile("guitar.mp3", displayWidth);
if ( guitar == null) println("Didn't get guitar!");
guitarFFT = new FFT(guitar.bufferSize(), guitar.sampleRate());
strings = minim.loadFile("strings.mp3", displayWidth);
if ( strings == null) println("Didn't get strings!");
stringsFFT = new FFT(strings.bufferSize(), strings.sampleRate());
reggae = minim.loadFile("reggae.mp3", displayWidth);
if ( reggae == null) println("Didn't get reggae!");
reggaeFFT = new FFT(reggae.bufferSize(), reggae.sampleRate());
snareTrigger = new Hotpoint(200, 0, 600, 300);
kickTrigger = new Hotpoint(-200, 0, 600, 300);
drumTrigger = new Hotpoint(00, 0, 3000, 300);
stringsTrigger = new Hotpoint(1000, 500, 2000, 350);
reggaeTrigger = new Hotpoint(-1000, 500, 2000, 350);
radioTrigger = new Hotpoint(00, 750, 2000, 200);
guitarTrigger = new Hotpoint(00, -1000, 3000, 300);
pauseTrigger = new Hotpoint(0,0, 2000, 200);
// enable depthMap generation
if (context.enableDepth() == false)
{
println("Can't open the depthMap, maybe the camera is not connected!");
exit();
return;
}
if (context.enableRGB() == false)
{
println("Can't open the rgbMap, maybe the camera is not connected or there is no rgbSensor!");
exit();
return;
}
// align depth data to image data
context.alternativeViewPointDepthToImage();
stroke(255, 255, 255);
smooth();
perspective(radians(45),
float(width)/float(height), 10, 150000);
}
void draw()
{
// update the cam
context.update();
background(0, 0, 0);
// int ui;
// for (ui=1; ui<=10; ui++)
// {
// // check if the skeleton is being tracked
// if (context.isTrackingSkeleton(ui))
// {
if (drum.isPlaying()) drumFFT.forward(drum.mix);
if (strings.isPlaying()) stringsFFT.forward(strings.mix);
if (guitar.isPlaying()) guitarFFT.forward(guitar.mix);
if (reggae.isPlaying()) reggaeFFT.forward(reggae.mix);
for (int i = 0; i < drumFFT.specSize(); i++)
{
line(i, height, i, height - drumFFT.getBand(i)*4);
}
for (int i = 0; i < drum.left.size() - 1; i++)
{
line(i, 50 + drum.left.get(i)*50, i+1, 50 + drum.left.get(i+1)*50);
line(i, 150 + drum.right.get(i)*50, i+1, 150 + drum.right.get(i+1)*50);
}
for (int i = 0; i < stringsFFT.specSize(); i++)
{
line(i, height, i, height - stringsFFT.getBand(i)*4);
}
for (int i = 0; i < strings.left.size() - 1; i++)
{
line(i, 50 + strings.left.get(i)*50, i+1, 50 + strings.left.get(i+1)*50);
line(i, 150 + strings.right.get(i)*50, i+1, 150 + strings.right.get(i+1)*50);
}
for (int i = 0; i < reggaeFFT.specSize(); i++)
{
line(i, height, i, height - reggaeFFT.getBand(i)*4);
}
for (int i = 0; i < reggae.left.size() - 1; i++)
{
line(i, 50 + reggae.left.get(i)*50, i+1, 50 + reggae.left.get(i+1)*50);
line(i, 150 + reggae.right.get(i)*50, i+1, 150 + reggae.right.get(i+1)*50);
}
for (int i = 0; i < guitarFFT.specSize(); i++)
{
line(i, height, i, height - guitarFFT.getBand(i)*4);
}
for (int i = 0; i < guitar.left.size() - 1; i++)
{
line(i, 50 + guitar.left.get(i)*50, i+1, 50 + guitar.left.get(i+1)*50);
line(i, 150 + guitar.right.get(i)*50, i+1, 150 + guitar.right.get(i+1)*50);
}
translate(width/2, height/2, 0);
rotateX(rotX);
rotateY(rotY);
scale(zoomF);
PImage rgbImage = context.rgbImage();
int[] depthMap = context.depthMap();
int steps = 3; // to speed up the drawing, draw every third point
int index;
PVector realWorldPoint;
color pixelColor;
strokeWeight(steps);
PVector[] depthPoints = context.depthMapRealWorld();
for (int i = 0; i < depthPoints.length; i+=10) {
PVector currentPoint = depthPoints[i];
// have each hotpoint check to see
// if it includes the currentPoint
snareTrigger.check(currentPoint);
kickTrigger.check(currentPoint);
drumTrigger.check(currentPoint);
guitarTrigger.check(currentPoint);
reggaeTrigger.check(currentPoint);
stringsTrigger.check(currentPoint);
radioTrigger.check(currentPoint);
pauseTrigger.check(currentPoint);
point(currentPoint.x, currentPoint.y, currentPoint.z);
}
translate(0, 0, -1000); // set the rotation center of the scene 1000 infront of the camera
PVector[] realWorldMap = context.depthMapRealWorld();
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)
{
// get the color of the point
pixelColor = rgbImage.pixels[index];
stroke(pixelColor);
// draw the projected point
realWorldPoint = realWorldMap[index];
point(realWorldPoint.x, realWorldPoint.y, realWorldPoint.z); // make realworld z negative, in the 3d drawing coordsystem +z points in the direction of the eye
}
}
}
if ( snareTrigger.isHit() ) snare.trigger();
if ( kickTrigger.isHit() ) kick.trigger();
if ( radioTrigger.isHit() ) radio.trigger();
if ( drumTrigger.isHit() ) {
if (drum.isPlaying() ) drum.pause();
else {
drum.play();
drum.loop();
}
}
if ( stringsTrigger.isHit() ) {
if (strings.isPlaying() ) strings.pause();
else {
strings.play();
strings.loop();
}
}
if ( reggaeTrigger.isHit() ) {
if (reggae.isPlaying() ) reggae.pause();
else {
reggae.play();
reggae.loop();
}
}
if ( guitarTrigger.isHit() ) {
if (guitar.isPlaying() ) guitar.pause();
else {
guitar.play();
guitar.loop();
}
}
if ( pauseTrigger.isHit() ) {
guitar.pause();
reggae.pause();
drum.pause();
strings.pause();
}
// display each hotpoint
// and clear its points
snareTrigger.draw();
snareTrigger.clear();
kickTrigger.draw();
kickTrigger.clear();
drumTrigger.draw();
drumTrigger.clear();
guitarTrigger.draw();
guitarTrigger.clear();
radioTrigger.draw();
radioTrigger.clear();
reggaeTrigger.draw();
reggaeTrigger.clear();
stringsTrigger.draw();
stringsTrigger.clear();
pauseTrigger.draw();
pauseTrigger.clear();
// draw the kinect cam
context.drawCamFrustum();
}
//}
//}
void keyPressed()
{
switch(key)
{
case ' ':
context.setMirror(!context.mirror());
break;
}
switch(keyCode)
{
// case ALT:
// closeAll();
// case RETURN:
// openAll();
case LEFT:
rotY += 0.1f;
break;
case RIGHT:
// zoom out
rotY -= 0.1f;
break;
case UP:
if (keyEvent.isShiftDown())
zoomF += 0.02f;
// Attempted to control the tilt of the kinect using the ALT key and the arrows
// else if(keyEvent.isAltDown())
// { kinectAngle++;
// kinectAngle = constrain(kinectAngle, 0, 30);
// kinect.tilt(kinectAngle);
// }
else
rotX += 0.1f;
break;
case DOWN:
if (keyEvent.isShiftDown())
{
zoomF -= 0.02f;
if (zoomF < 0.01)
zoomF = 0.01;
}
// Attempted to control the tilt of the kinect using the ALT key and the arrows
// else if(keyEvent.isAltDown())
// {
// kinectAngle--;
// kinectAngle = constrain(kinectAngle, 0, 30);
// kinect.tilt(kinectAngle);
// }
else
rotX -= 0.1f;
break;
}
}
void stop()
{
// make sure to close
// both AudioPlayer objects
kick.close();
snare.close();
drum.close();
guitar.close();
strings.close();
reggae.close();
radio.close();
minim.stop();
super.stop();
}
class Hotpoint
{
PVector center;
color fillColor;
color strokeColor;
color FSColor;
int size;
int pointsIncluded;
int maxPoints;
boolean wasJustHit;
int threshold;
Hotpoint(float centerX, float centerY, float centerZ, int boxSize)
{
center = new PVector(centerX, centerY, centerZ);
size = boxSize;
pointsIncluded = 0;
maxPoints = 1000;
threshold = 0;
FSColor = rColor();
fillColor = strokeColor = FSColor;
}
color rColor(){
return color (random(255), random(255), random(255));
}
void setThreshold(int newThreshold)
{
threshold = newThreshold;
}
void setMaxPoints(int newMaxPoints)
{
maxPoints = newMaxPoints;
}
void setColor(color FSColor)
{
fillColor = strokeColor = FSColor;
}
boolean check(PVector point)
{
boolean result = false;
if(point.x > center.x - size/2 && point.x < center.x + size/2)
{
if(point.y > center.y - size/2 && point.y < center.y + size/2)
{
if(point.z > center.z - size/2 && point.z < center.z + size/2)
{
result = true;
pointsIncluded++;
}
}
}
return result;
}
void draw()
{
pushMatrix();
translate(center.x, center.y, center.z);
fill(red(fillColor), blue(fillColor), green(fillColor), 255*percentIncluded());
stroke(red(strokeColor), blue(strokeColor), green(strokeColor), 255);
box(size);
popMatrix();
}
float percentIncluded()
{
return map(pointsIncluded, 0, maxPoints, 0, 1);
}
boolean currentlyHit()
{
return(pointsIncluded > threshold);
}
boolean isHit()
{
return currentlyHit() && !wasJustHit;
}
void clear()
{
wasJustHit = currentlyHit();
pointsIncluded = 0;
}
}






No comments:
Post a Comment