view of/testApp.cpp @ 50:f4c6999ecfe9 tip

added the files on my computer that aren't aiff s> these shoudl be everything for the big bang fair 2011 - heresy, and tim's file's also here
author Andrew N Robertson <andrew.robertson@eecs.qmul.ac.uk>
date Sat, 08 Oct 2011 22:12:49 +0100
parents 0eeda0223db3
children
line wrap: on
line source
#include "testApp.h"

//--------------------------------------------------------------
void testApp::setup(){
	drawIt=false;
	// load the default ofxOpenNI config settings
	// see bin/data/openni/config/ofxopenni_config.xml
	context.setupUsingXMLFile();
	
	// uncomment this to see what open ni is doing inside
	//context.enableLogging(OF_LOG_VERBOSE);
	
	// setup resources
	depth.setup(&context);
	image.setup(&context);
	user.setup(&context, &depth);
//	Davy.loadImage("images/davy.jpg");
   ofEnableSmoothing();
   ofSetBackgroundAuto(false);
   ofBackground(255,255,255);
  // 	ofEnableAlphaBlending();
   ofSetLineWidth(12);


	for (int y = 0;y < NUMBER_OF_USERS;y++){
		typeOfFaceToUse[y] = y%2;
	}
	
	anubis.loadImage("images/mask1.png");//anubis-wall-mask.gif
	secondUserImage.loadImage("images/mask2.png");//davy2.jpg"
	thirdUserImage.loadImage("images/mask3.png");//mubrarksign.jpg
	
	franklinBook.loadFont("frabk.ttf", 32);

//	sender.setup( HOST, PORT );
	stopSender.setup( STOPHOST, STOPPORT );
	heresySender.setup(HERESY_HOST, HERESY_PORT);
	mophoSender.setup(STOPHOST, MOPHO_PORT);
	
	
	maxTorsoValues[0] = 0;
	maxTorsoValues[1] = 0;
	maxTorsoValues[2] = 0;
	double timeNow = ofGetElapsedTimeMillis();
	for (int user = 0;user < NUMBER_OF_USERS;user++)
	lastRecordedTime[user] = timeNow;
	
	leftMaximum[0] = 700;
	
}

//--------------------------------------------------------------
void testApp::update(){
	
	// update the open ni context and user tracker
	context.update();
	user.update();

	
}


void testApp::getCoordinates(int userID){
	//tracked = user.getTrackedUser(i);
	if (userID < 0 && userID > 7)
	    cout << "PROBLEM IN NUMBER FOR getCoordinates";
    
	ofxTrackedUser* tracked = user.getTrackedUser(userID);

	

	double timeNow = ofGetElapsedTimeMillis();

		
		
		
		if ((fabs(torso[userID][0] - tracked->left_upper_torso.end.x) + fabs(torso[userID][1] - tracked->left_upper_torso.end.y) > 0.1 && tracked->left_upper_torso.end.x > 0 )){
			//printf("tracking time %d and last recorded %d\n", timeNow, lastRecordedTime[userID]);
		userPresent[userID] = true;
		lastRecordedTime[userID] = timeNow ;

		torso[userID][0] = tracked->left_upper_torso.end.x;
		torso[userID][1] = tracked->left_upper_torso.end.y;
		torso[userID][2] = tracked->left_upper_torso.end.z;
			
		leftHandPosition[userID][0] = tracked->left_lower_arm.end.x;	
		leftHandPosition[userID][1] = tracked->left_lower_arm.end.y;	
		leftHandPosition[userID][2] = tracked->left_lower_arm.end.z;	

		rightHandPosition[userID][0] = tracked->right_lower_arm.end.x;	
		rightHandPosition[userID][1] = tracked->right_lower_arm.end.y;	
		rightHandPosition[userID][2] = tracked->right_lower_arm.end.z;		
			
		checkTorsoMaxima(userID);
		
		//recent track, low distance but do nothing yet
	}
	else if(userPresent[userID] && 	(timeNow - lastRecordedTime[userID]) > 500 ){
		//not recent track => stopped
		userPresent[userID] = false;
		printf("user %i has STOPPED!\n", userID);
		torso[userID][0] = tracked->left_upper_torso.end.x;
		torso[userID][1] = tracked->left_upper_torso.end.y;
		torso[userID][2] = tracked->left_upper_torso.end.z;
		
		sendStopMessageForUser(userID);
		userInfoString[userID] = "stopped";
		
		//tracked->delete!!

	}
	ofSetColor(0x00FF00);
	outputString = "HeadX : ";
	outputString += ofToString(torso[0][0]);
	//franklinBook.drawString(outputString, 100,600);

}

void testApp::checkTorsoMaxima(int id){
	for (int h = 0; h < 3;h++){
	if (torso[id][h] > maxTorsoValues[h])
		maxTorsoValues[h] = torso[id][h];
	}
	
	for (int h = 0; h < 3;h++){
		if (leftHandPosition[id][h] > leftMaximum[h])
			leftMaximum[h] = leftHandPosition[id][h];
	}

	for (int h = 0; h < 3;h++){
		if (rightHandPosition[id][h] > rightMaximum[h])
			rightMaximum[h] = rightHandPosition[id][h];
	}
	
}

void testApp::sendDataViaOSC(int userID){
/*
	ofxOscMessage m;
	m.setAddress( "/person" );
	m.addIntArg( userID );
	m.addFloatArg( torso[userID][0]/maxTorsoValues[0] );
	m.addFloatArg( torso[userID][1] /maxTorsoValues[1]);
	m.addFloatArg( torso[userID][2] /maxTorsoValues[2]);
	sender.sendMessage( m );
*/
}

void testApp::sendStopMessageForUser(int userID){
	ofxOscMessage m;
	m.setAddress( "/stopperson" );
	m.addIntArg( userID );
	m.addFloatArg( torso[userID][0]/maxTorsoValues[0] );
	m.addFloatArg( torso[userID][1] /maxTorsoValues[1]);
	m.addFloatArg( torso[userID][2] /maxTorsoValues[2]);
	stopSender.sendMessage( m );
	
	/*
	ofxOscMessage newMess;
	newMess.setAddress( "/stopperson" );
	newMess.addIntArg( userID );
	sender.sendMessage( newMess );
*/
}


void testApp::sendInformationToHeresy(int userID){
	
	//map to pitches interpolation
	double zVal = torso[userID][2] /maxTorsoValues[2];
	int pitch;
	if (zVal < 1)
		pitch = (int)(16 * zVal);
	else 
		pitch = 15;

	
	
	if (userID < NUMBER_OF_USERS){
		double leftHand, rightHand,  leftHandX, rightHandX,  leftHandY, rightHandY;
		leftHand = leftHandPosition[userID][0]/leftMaximum[0];//i.e x [position
		rightHand = rightHandPosition[userID][0]/rightMaximum[0];//same for right

		
		leftHandY = leftHandPosition[userID][1]/leftMaximum[1];//i.e. x position but 0 to 1
		rightHandY = rightHandPosition[userID][1]/rightMaximum[1];//same for right
		
		//THIS IS THE ONLY ROUTINE CALLED
		//i.e. monophonic
		
		//	int controlChangeLeft[3][4];//userId, leftX, leftR, rightX, right Y
		//need to send continuous control change not just when note changes
		int controlChangeLeft = (int) min((double)127,max((double)0,(leftHand*127)));
		int controlChangeRight = (int) min((double)127,max((double)0,(rightHand*127)));
		
		int controlChangeLeftY = (int)((double)127 - min((double)127,max((double)0,(leftHandY*127))));
		int controlChangeRightY = (int)((double)127 - min((double)127,max((double)0,(rightHandY*127))));	
		
		if (userID > 0){ //hackoo
		bool checkNotSame1 = checkSameUserAs(userID, userID + 1 % NUMBER_OF_USERS);
		bool checkNotSame2 = checkSameUserAs(userID, userID + 2 % NUMBER_OF_USERS);
		
			checkingSameUser[userID] = checkSameUserAs(userID, userID + 1 % NUMBER_OF_USERS) ||  checkSameUserAs(userID, userID + 2 % NUMBER_OF_USERS);
		}
		
				
		ControlChangeData[userID][0] = controlChangeLeft ;
		ControlChangeData[userID][1] = controlChangeLeftY ;
		ControlChangeData[userID][2] = controlChangeRight ;	
		ControlChangeData[userID][3] = controlChangeRightY ;	
		
		userInfoString[userID] = "USER ";
		userInfoString[userID] += ofToString(userID+1, 0);	
		userInfoString[userID] += " Pitch ";
		userInfoString[userID] += ofToString(pitch, 0);
		
		userInfoString[userID] += " ccL ";
		userInfoString[userID] += ofToString(controlChangeLeft, 0);
		userInfoString[userID] += " , ";
		userInfoString[userID] += ofToString(controlChangeLeftY, 0);								
										
		userInfoString[userID] += " ccR ";
		userInfoString[userID] += ofToString(controlChangeRight, 0);
		userInfoString[userID] += " , ";
		userInfoString[userID] += ofToString(controlChangeRightY, 0);			
		
		if (userID == 0){
			sendControlChangeDataToAbleton(userID);
		}
		else{
		sendControlChangeDataToMax(userID);
		}
		
	if (pitch != pitchPlaying[userID]){
		//then we have a change
		//send note off
		sendNoteMessageToHeresy(pitchPlaying[userID], 0, 0, userID);
		printf("note OFF user %i pitch %i \n ", userID+1, pitchPlaying[userID]);

		//if (!checkingSameUser[userID]){
			sendNoteMessageToHeresy(pitch, 1, 127, userID);// int channel){
			pitchPlaying[userID] = pitch;
		//}
		
		//now get send control change for the hands XXX TO DO
		/*
		 leftHand *= 127;
		rightHand *= 127;
		
		*/

	//	printf("note ON user %i pitch %i \n", userID+1, pitch);
		}
	}
	else{
		//NO LONGER CALLED - WE SEND JUST MAIN PITCHES TRIGGERED BY BODY POSITION
		//i.e. for was channel for user 2 and 3
		/*
			double minimum = min(leftHand, rightHand);
			double maximum = max(leftHand, rightHand);
			int minPitch = (int) (16 * minimum);
			int maxPitch = (int) (16 * maximum);
			minMaxValues[userID][0] = minPitch;
			minMaxValues[userID][1] = maxPitch;
		
		if (maxPitch > 15)
				maxPitch = 15;
		
		userInfoString[userID] = "USER ";
		userInfoString[userID] += ofToString(userID+1, 0);
		
			printf("user %i min %i max %i\n", userID+1, minPitch, maxPitch);
		
		userInfoString[userID] += "min ";
		userInfoString[userID] += ofToString(minPitch, 0);
		userInfoString[userID] += "max ";
		userInfoString[userID] += ofToString(maxPitch, 0);
		
		for (int p = 0;p < 16;p++){
				
				if (p < minPitch || p > maxPitch){
					//sendNoteMessageToHeresy(p, 0, 0, userID);
					nowPlaying[userID][p] = false;
				}
				else{
					nowPlaying[userID][p] = true;
				}
				
				
				if (nowPlaying[userID][p] != lastPlaying[userID][p]){
					if (nowPlaying[userID][p])
					{
						//sendNoteMessageToHeresy(p, 1, 127, userID);
						lastPlaying[userID][p] = true;
					}
					else {
						//sendNoteMessageToHeresy(p, 0, 0, userID);
						lastPlaying[userID][p] = false;
					}
				}
			}//end for p
			*/
	}//end else - not called!
			
			
	

			
	//x and y get sent to max after being mapped 0 to 127
	//these are control change messages
	//cvan be sent straight to max
	int xVal, yVal;
	xVal = (int)(127 * torso[userID][0]/maxTorsoValues[0] );
	yVal = (int)(127 * torso[userID][1]/maxTorsoValues[1] );
	
	

}


bool testApp::checkSameUserAs(int userID, int checkID){
	bool check = false;
	
	if (rightHandPosition[userID][0] == rightHandPosition[checkID][0] && leftHandPosition[userID][0] == leftHandPosition[checkID][0])
		check = true;
	
	return check;

}



void testApp::sendNoteMessageToHeresy(int noteNumber, int onOrOff, int velocity, int channel){
	channel++;
	
	ofxOscMessage m;
	m.setAddress( "/test" );
	m.addStringArg("note");
	m.addIntArg( noteNumber );
	m.addIntArg( onOrOff );
	m.addIntArg( velocity );
	m.addIntArg( channel );
	heresySender.sendMessage( m );
	
	
}


void testApp::sendControlChangeDataToMax(int userID){
	int channel = userID+1;
	
	ofxOscMessage m;
	m.setAddress( "/controlChangeData" );
	m.addIntArg( channel );
	m.addIntArg( ControlChangeData[userID][0] );
	m.addIntArg( ControlChangeData[userID][1] );
	m.addIntArg( ControlChangeData[userID][2] );
	m.addIntArg( ControlChangeData[userID][3] );
	stopSender.sendMessage( m );

}

void testApp::sendControlChangeDataToAbleton(int userID){
	int channel = userID+1;
	
	ofxOscMessage m;
	m.setAddress( "/controlChangeData" );
	m.addIntArg( channel );
	m.addIntArg( ControlChangeData[userID][0] );
	m.addIntArg( ControlChangeData[userID][1] );
	m.addIntArg( ControlChangeData[userID][2] );
	m.addIntArg( ControlChangeData[userID][3] );
	mophoSender.sendMessage( m );
	
}

//--------------------------------------------------------------
void testApp::draw(){
		ofBackground(255,255,255);
	
	// draw depth and rgb images
	//depth.draw();
if (drawIt != true)	{
image.draw();
user.draw();	
}
	// draw all the users automatically?

	
	
	
	stringstream info;
	info << "number of tracked users: " << user.getNumTrackedUsers() << endl;
	
	//string numString = "num tracked users is ";
	//numString += ofToString(user.getNumTrackedUsers());
	//franklinBook.drawString(numString, 60, 60);
	
	// draw the neck positions of each user
	for(int i = 0; i < user.getNumTrackedUsers(); ++i) {//
		
		ofxTrackedUser* tracked = user.getTrackedUser(i);
		
		if(tracked != NULL) {

		//andrews code
			int userID = i;
//			printf("user %i \n", userID);
			int tmpID = userID;
			userID = userID % NUMBER_OF_USERS;//hack
			
			if (userID < NUMBER_OF_USERS){// && !checkingSameUser[userID]
			
				getCoordinates(userID);
				

				if (userPresent[userID]){
		//		sendDataViaOSC(userID);
				sendInformationToHeresy(userID);
					
				
				ofSetColor(0x000000);
				outputString = "Us ";
				outputString += ofToString(userID);
				outputString += "  ";	
				outputString += ofToString(userPresent[userID]);	
				outputString += "HeadX : ";
				outputString += ofToString(torso[userID][0], 2);
				outputString += "  HeadY : ";
				outputString += ofToString(torso[userID][1], 2);
				outputString += "  HeadZ : ";
				outputString += ofToString(torso[userID][2], 2);
					
					franklinBook.drawString(outputString, 100,600+userID*160);
				

					outputString = "LeftX : ";
					outputString += ofToString(leftHandPosition[userID][0], 2);
					outputString += "  LeftY : ";
					outputString += ofToString(leftHandPosition[userID][1], 2);
					outputString += "  LeftZ : ";
					outputString += ofToString(leftHandPosition[userID][2], 2);					
		
					franklinBook.drawString(outputString, 100,640+userID*160);

					outputString = "RightX : ";
					outputString += ofToString(rightHandPosition[userID][0], 2);
					outputString += "  LeftY : ";
					outputString += ofToString(rightHandPosition[userID][1], 2);
					outputString += "  LeftZ : ";
					outputString += ofToString(rightHandPosition[userID][2], 2);					
					
					franklinBook.drawString(outputString, 100,680+userID*160);
					
					outputString = "MaxX: ";
					outputString += ofToString(maxTorsoValues[0], 2);
					outputString += "  MaxY: ";
					outputString += ofToString(maxTorsoValues[1], 2);
					outputString += " MaxZ: ";
					outputString += ofToString(maxTorsoValues[2], 2);					
					
					
					franklinBook.drawString(outputString, 100,720+userID*160);
				
					
			//	ofSetColor(0,0,255);
			//	ofCircle(tracked->neck.begin.x, tracked->neck.begin.y , 40);
	
					
					
					ofSetColor(255,255,255);
					float scalingFactor = 0.75 * 1500 / tracked->neck.begin.z ;
					switch (userID){
							//x was - anubis.width*scalingFactor /2
							//y was - anubis.height*scalingFactor /2
						case 0:
					anubis.draw(tracked->neck.begin.x - anubis.width*scalingFactor /2,tracked->neck.begin.y - anubis.height*scalingFactor /4, anubis.width*scalingFactor, anubis.height*scalingFactor  );		
							break;
						case 1:
					secondUserImage.draw(tracked->neck.begin.x- secondUserImage.width*scalingFactor /2,tracked->neck.begin.y - secondUserImage.height*scalingFactor /4, secondUserImage.width*scalingFactor, secondUserImage.height*scalingFactor );			
							break;
						case 2:
							thirdUserImage.draw(tracked->neck.begin.x- thirdUserImage.width*scalingFactor /2,tracked->neck.begin.y - thirdUserImage.height*scalingFactor /4, thirdUserImage.width*scalingFactor, thirdUserImage.height*scalingFactor );			
							break;	
						default:
							drawAcidSmiley(userID);
							break;
							
					}//end switch user image
					
				}//end if is present
				
			}
			
			//end andrew

			info << i << " neck pos: "
				 << tracked->left_lower_arm.end.x << " " 
				 << tracked->left_lower_arm.end.y << " "
				 << tracked->neck.begin.z << endl; 

			
			if (tracked->neck.begin.x != 0)


			ofSetColor(139,0,0,100);
            
			ofNoFill();
			float w = ofGetWidth();
			float h = ofGetHeight();

			/*
			if (typeOfFaceToUse[userID] == 1)
			bezierFunctionStuff(userID);
			else 
			drawAcidSmiley(userID);
			 */
			}
		ofSetColor(0,0,0);
		for (int userNumber = 0;userNumber < 3;userNumber++){
		franklinBook.drawString(userInfoString[userNumber], 660, (userNumber*60)+60);
		}
		
	}

	
	//faint rectangle?
	ofFill();
	ofSetColor(238,238,224,20);
//	ofRect(0,0,ofGetWidth(),ofGetHeight()); blurring
	ofDrawBitmapString(info.str(), 10, 500);
		
}

//--------------------------------------------------------------
void testApp::keyPressed(int key){
//	if (key=='r')
//		ofxUserGenerator::setup(context, depth);
//	else {
		ofSetColor(238,238,224);
		ofRect(0,0,ofGetWidth(),ofGetHeight());
		//	drawIt =!drawIt;
		ofToggleFullscreen();
		
		//sendDataViaOSC(0);
//	}
}

//--------------------------------------------------------------
void testApp::keyReleased(int key){

}

//--------------------------------------------------------------
void testApp::mouseMoved(int x, int y ){

}

//--------------------------------------------------------------
void testApp::mouseDragged(int x, int y, int button){

}

//--------------------------------------------------------------
void testApp::mousePressed(int x, int y, int button){
	ofSetColor(238,238,224);
	ofFill();
	ofRect(0,0,ofGetWidth(),ofGetHeight());
	
	int pitch = (int) ofRandom(0, 15);
	//sendNoteMessageToHeresy(pitch, 1, 127, 1);
}

//--------------------------------------------------------------
void testApp::mouseReleased(int x, int y, int button){

}

//--------------------------------------------------------------
void testApp::windowResized(int w, int h){

}


void testApp::bezierFunctionStuff(int i){

	ofxTrackedUser* tracked = user.getTrackedUser(i);
	float w = ofGetWidth();
	float h = ofGetHeight();
	
	ofFill();
	
	ofBezier(tracked->neck.begin.x,
			 tracked->neck.begin.y,
			 tracked->neck.end.x,
			 tracked->neck.end.y,
			 tracked->left_lower_arm.begin.x,
			 tracked->left_lower_arm.begin.y,
			 tracked->left_lower_arm.end.x,
			 /*tracked->left_lower_arm.end.y*/ w);
	
	ofBezier(tracked->neck.begin.x,
			 tracked->neck.begin.y,
			 tracked->neck.end.x,
			 tracked->neck.end.y,
			 tracked->right_lower_arm.begin.x,
			 tracked->right_lower_arm.begin.y,
			 tracked->right_lower_arm.end.x,
			 /*tracked->right_lower_arm.end.y*/ w);
	
	ofBezier(tracked->neck.begin.x,
			 tracked->neck.begin.y,
			 tracked->neck.end.x,
			 tracked->neck.end.y,
			 tracked->left_lower_leg.begin.x,
			 tracked->left_lower_leg.begin.y,
			 tracked->left_lower_leg.end.x,
			 /*tracked->left_lower_leg.end.y*/ h);
	
	ofBezier(tracked->neck.begin.x,
			 tracked->neck.begin.y,
			 tracked->neck.end.x,
			 tracked->neck.end.y,
			 tracked->right_lower_leg.begin.x,
			 tracked->right_lower_leg.begin.y,
			 tracked->right_lower_leg.end.x,
			 /*tracked->right_lower_leg.end.y*/ h);
	
	
	ofBezier(tracked->neck.begin.x,tracked->neck.begin.y,tracked->neck.end.x,tracked->neck.end.y,tracked->left_lower_arm.begin.x,tracked->left_lower_arm.begin.y,tracked->left_lower_arm.end.x,/*tracked->left_lower_arm.end.y*/ w);
	ofBezier(tracked->neck.begin.x,tracked->neck.begin.y,tracked->neck.end.x,tracked->neck.end.y,tracked->right_lower_arm.begin.x,tracked->right_lower_arm.begin.y,tracked->right_lower_arm.end.x,/*tracked->right_lower_arm.end.y*/ w);
	ofBezier(tracked->neck.begin.x,tracked->neck.begin.y,tracked->neck.end.x,tracked->neck.end.y,tracked->left_lower_leg.begin.x,tracked->left_lower_leg.begin.y,tracked->left_lower_leg.end.x,/*tracked->left_lower_leg.end.y*/ h);
	ofBezier(tracked->neck.begin.x,tracked->neck.begin.y,tracked->neck.end.x,tracked->neck.end.y,tracked->right_lower_leg.begin.x,tracked->right_lower_leg.begin.y,tracked->right_lower_leg.end.x,/*tracked->right_lower_leg.end.y*/ h);
	
	
}

void testApp::drawAcidSmiley(int i){
	//acid smiley
	
	ofxTrackedUser* tracked = user.getTrackedUser(i);
	float w = ofGetWidth();
	float h = ofGetHeight();
	
	
	ofFill();
	
	ofSetColor(255,255,100);
	ofCircle(tracked->neck.begin.x,tracked->neck.begin.y,30);
	ofSetColor(0);
	ofCircle(tracked->neck.begin.x,tracked->neck.begin.y,35);
	ofSetColor(255,255,100);
	ofCircle(tracked->neck.begin.x,tracked->neck.begin.y,30);
	ofSetColor(0);
	ofCircle(tracked->neck.begin.x+10.0f,tracked->neck.begin.y,6);
	ofCircle(tracked->neck.begin.x-10.0f,tracked->neck.begin.y,6);
	
	
	
}