view matchAnnotationSrc/testApp.cpp @ 49:3ce6dadd8167

Added src for the results calculator, comparing match output with the JNMR midi follower output
author Andrew N Robertson <andrew.robertson@eecs.qmul.ac.uk>
date Fri, 23 Mar 2012 10:53:57 +0000
parents
children 158f5f38e9d3
line wrap: on
line source
#include "testApp.h"

//--------------------------------------------------------------
void testApp::setup(){
	//fixed stuff
	annotationRoot = "/Users/andrew/Documents/work/Alignment/MuseScore/RWC/ANNOTATION/RM-C0";
	jnmrPlayerRoot = "/Users/andrew/Documents/work/programming/of_preRelease_v007_osx/apps/myOpenFrameworks007/JNMR_MidiFollower/bin/data/FilesOut/savedOutputs/rwcOutputData_RM-C0";
	
	createRWCfilenameStrings();
	
	
	std::string matchRoot = "/Users/andrew/Documents/work/programming/Shell Scripts/matchOutput/RWCmatch_RM-C0";
	std::string matchEnd = "_ob.out";
	std::string txtEnd = ".txt";
	//naming convention
	//match output must also follow the RWC ID
	//01, 02, 03 etc
	
	
	
	int fileID = 2;
	
	matchPath = makeRWCfilename(matchRoot, fileID, matchEnd);
	printf("MATCH PATH\n", matchPath.c_str());
	matchNotations.readInMatchFile(matchPath);

	
	loadAnnotation(fileID);

	printf("MATCH\n");
	calculateMatchErrors();
	
	jnmrPlayerPath = makeRWCfilename(jnmrPlayerRoot, fileID, txtEnd);//+ "rwcOutputData_RM-C003.txt";
	jnmrPlayerAnnotations.readInjnmrMidiPlayerFile(jnmrPlayerPath);
	
	printf("JNMR DIFFERENCES\n");
	sortDifferenceVector(jnmrPlayerAnnotations.differences);
	
}


std::string testApp::makeRWCfilename(std::string& root, const int& fileID, std::string& endPart){
	std::string pathName;
	if (fileID >= 0 && fileID <= 64){
		
		pathName = root + rwcFileNameStrings[fileID]+endPart;
		/*
		 if (fileID >= 10)
		 pathName = root + ofToString(fileID)+endPart;
		 else
		 pathName = root + "0"+ofToString(fileID)+endPart;
		 */
		//printf("RWC PATH:%s\n", pathName.c_str());
	}
	return pathName;
}

void testApp::createRWCfilenameStrings(){
	rwcFileNameStrings.clear();
	for (int i = 1;i< 10;i++){
		rwcFileNameStrings.push_back("0"+ofToString(i));
	}
	for (int i = 10;i< 23;i++){
		rwcFileNameStrings.push_back(ofToString(i));
	}
	rwcFileNameStrings.push_back("23A");
	rwcFileNameStrings.push_back("23B");
	rwcFileNameStrings.push_back("23C");
	rwcFileNameStrings.push_back("23D");
	rwcFileNameStrings.push_back("23E");
	
	rwcFileNameStrings.push_back("24A");
	rwcFileNameStrings.push_back("24B");
	rwcFileNameStrings.push_back("24C");
	
	rwcFileNameStrings.push_back("25A");
	rwcFileNameStrings.push_back("25B");
	rwcFileNameStrings.push_back("25C");
	rwcFileNameStrings.push_back("25D");
	
	for (int i = 26;i< 35;i++){
		rwcFileNameStrings.push_back(ofToString(i));
	}
	rwcFileNameStrings.push_back("35A");
	rwcFileNameStrings.push_back("35B");
	rwcFileNameStrings.push_back("35C");
	for (int i = 36;i< 51;i++){
		rwcFileNameStrings.push_back(ofToString(i));
	}
	
	//	for (int i = 0;i < rwcFileNameStrings.size();i++) {
	//		printf("RWC{%i}:'%s'\n", i, rwcFileNameStrings[i].c_str());
	//	}
	
}

void testApp::loadAnnotation(const int& fileID){
	
	string annotationEnding = "_annotation+WavPos.csv";
	string path = makeRWCfilename(annotationRoot, fileID, annotationEnding );
	//printf("read in annotations from\n%s\n", path.c_str());
	rwcAnnotations.readInRWCfile(path);
}


void testApp::calculateMatchErrors(){
	int matchIndex = 0;

	vector<float> matchDiffs;
	
	for (int i = 0;i < rwcAnnotations.rwcAnnotations.size();i++){
//		printf("rwc time %f midi time %f\n", rwcAnnotations.rwcAnnotations[i].eventTime, rwcAnnotations.rwcAnnotations[i].midiTime);
		
		while (matchIndex < matchNotations.matchData.size() && matchNotations.matchData[matchIndex].midiTime < rwcAnnotations.rwcAnnotations[i].midiTime)
			matchIndex++;
		
		while (matchNotations.matchData[matchIndex].midiTime > rwcAnnotations.rwcAnnotations[i].midiTime)
			matchIndex--;
		
	//	float matchFraction = (rwcAnnotations.rwcAnnotations[i].midiTime - matchNotations.matchData[matchIndex].midiTime)/(matchNotations.matchData[matchIndex+1].midiTime - matchNotations.matchData[matchIndex].midiTime);
	//	matchFraction *= (matchNotations.matchData[matchIndex+1].audioTime - matchNotations.matchData[matchIndex].audioTime);
		float matchTime = matchNotations.matchData[matchIndex].audioTime;// +matchFraction  ;
		
//		printf("matchTime %f gives event time %f from flat event time %f\n", matchNotations.matchData[matchIndex].midiTime, matchTime, matchNotations.matchData[matchIndex].audioTime);
	
		float matchDifference = 1000.0*(matchTime - rwcAnnotations.rwcAnnotations[i].eventTime);
//		printf("Match diff %f\n", matchDifference);
	
		matchDiffs.push_back(fabs(matchDifference));
	}
	
	sortDifferenceVector(matchDiffs);
	/*
//GETS MEAN AND MEDIAN
	int count = 0;
	float total = 0;
	
	for (int i = 0;i < matchDiffs.size();i++){
	//	printf("M[%i] : %f\n", i, matchDiffs[i]);
		
		count++;
		total += fabs(matchDiffs[i]);
		
	}
	total /= count;
	
	std::sort(matchDiffs.begin(), matchDiffs.end());
	
//	printf("SORTED TWO HUNDRED\n");
	
//	for (int i = 0;i < matchDiffs.size() ;i++){
//		printf("M[%i] : %f\n", i, matchDiffs[i]);
//	}
	
	printf("MATCH median %f, mean is %f\n", matchDiffs[(int)(matchDiffs.size()/2)], total);
	*/

	
	
}


void testApp::sortDifferenceVector(vector<float> diffVec){
	
	//GETS MEAN AND MEDIAN
	int count = 0;
	float total = 0;
	
	for (int i = 0;i < diffVec.size();i++){
		//	printf("M[%i] : %f\n", i, matchDiffs[i]);
		
		count++;
		total += fabs(diffVec[i]);
		diffVec[i] = fabs(diffVec[i]);//replace with +ve value
	}
	total /= count;
	
	std::sort(diffVec.begin(), diffVec.end());//sort vector
	
	/*	printf("SORTED TWO HUNDRED\n");
	
		for (int i = 0;i < diffVec.size() ;i++){
			printf("Sort[%i] : %f\n", i, diffVec[i]);
		}
	*/
	printf("Count %i Median %f, mean is %f\n", count, diffVec[(int)(diffVec.size()/2)], total);
	
	

}

//--------------------------------------------------------------
void testApp::update(){

}

//--------------------------------------------------------------
void testApp::draw(){

}

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

}

//--------------------------------------------------------------
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){

}

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

}

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

}

//--------------------------------------------------------------
void testApp::gotMessage(ofMessage msg){

}

//--------------------------------------------------------------
void testApp::dragEvent(ofDragInfo dragInfo){ 

}