diff jnmr/midiEventHolder.cpp @ 46:43edc8abe2a7

Fixed bug in complex update due to mismatch between ms and vector units
author Andrew N Robertson <andrew.robertson@eecs.qmul.ac.uk>
date Sun, 05 Feb 2012 19:40:21 +0000
parents 6c8a048720c3
children 1726189a6317
line wrap: on
line diff
--- a/jnmr/midiEventHolder.cpp	Sun Feb 05 10:00:34 2012 +0000
+++ b/jnmr/midiEventHolder.cpp	Sun Feb 05 19:40:21 2012 +0000
@@ -10,6 +10,12 @@
 
 //Main file to look at here is newNoteEvent() - this calls everything else to update the Bayesian array
 
+//Relative speed is RECORDED / PLAYED
+//so for timedifference t
+//this is t/scalar in terms of position units of played time
+//and by relative speed r, r*t/scalar
+
+
 #include "midiEventHolder.h"
 
 #include <iostream>
@@ -19,6 +25,7 @@
 midiEventHolder::midiEventHolder(){
 	
 
+	double scalarForPositionVectors = 20;
 	
 //	recordedNoteOnIndex = 0;
 	alignmentPosition = 0;
@@ -68,9 +75,10 @@
 	
 	speedPriorValue = 1.0;
 	
-	
-	bayesStruct.resetSize(matchWindowWidth);
-	bayesStruct.setPositionDistributionScalar(1);
+	int tmpArraySize = (int)(matchWindowWidth/scalarForPositionVectors);
+	bayesStruct.resetSize(tmpArraySize);
+	bayesStruct.setPositionDistributionScalar(scalarForPositionVectors);
+	printf("ARRAY SIZE IS %i\n", tmpArraySize);
 	
 	bayesStruct.resetSpeedSize(200);
 	bayesStruct.setRelativeSpeedScalar(0.01);
@@ -129,14 +137,14 @@
 //	lastSmoothUpdateTime = getTimeNow(0);
 	
 	printf("reset speed prior is %f\n", speedPriorValue);
-	bayesStruct.resetSpeedToOne();
+//	bayesStruct.resetSpeedToOne();
 	bayesStruct.setSpeedPrior(speedPriorValue);
 	setMatchedNotesBackToFalse();
 
-	periodCounter = 0;
-	for (int i = 0;i < periodValues.size();i++){
+//	periodCounter = 0;
+//	for (int i = 0;i < periodValues.size();i++){
 	//	printf("period at %f is %f\n", periodValues[i][2], periodValues[i][1]);
-	}
+//	}
 /*	if (periodValues.size() > 0){
 		updatePeriodValue(0);// periodValues[0][2];
 		printf("Resetting period to %f , size is %i\n", period,  (int)periodValues.size());
@@ -189,7 +197,7 @@
 
 void midiEventHolder::newNoteOnEvent(int pitch, int velocity, double timePlayed){
 //	tempoSpeedString = "";
-	
+
 	//MOVE INTO BAYESSTRUCT?? XXX
 	//bayesStruct.copyPriorToPosterior();
 	//why was this here??
@@ -252,8 +260,8 @@
 	//then we recalculate the window start based on MAP being central
 	//then we do the matches on these and the likelihood on these.
 	
-	bayesStruct.setNewDistributionOffsets(max(0., bayesStruct.bestEstimate - (bayesStruct.prior.scalar*bayesStruct.prior.arraySize/2)));
-//	bayesStruct.prior.offset = max(0.,newMAPestimateTime - (bayesStruct.prior.scalar*bayesStruct.prior.arraySize/2));
+	bayesStruct.setNewDistributionOffsets(max(bayesStruct.posterior.offset, bayesStruct.bestEstimate - (bayesStruct.prior.scalar*bayesStruct.prior.arraySize/2)));
+	//bayesStruct.setNewDistributionOffsets(max(0.,newMAPestimateTime - (bayesStruct.prior.scalar*bayesStruct.prior.arraySize/2));
 	
 	timeString += " \n :  new offset " + ofToString(bayesStruct.prior.offset , 0);
 	timeString += " \n best estimate "+ofToString(bayesStruct.bestEstimate, 1);
@@ -271,7 +279,8 @@
 	timeString += " notearrayindex "+ofToString(noteArrayIndex, 0);
 	//when this is off teh screen there is a problem somehow XXX
 	
-	bayesStruct.posterior.offset = max(0., bayesStruct.bestEstimate - (bayesStruct.prior.scalar*bayesStruct.prior.arraySize/2));//	bayesStruct.prior.offset = max(0., bayesStruct.bestEstimate - (bayesStruct.prior.scalar*bayesStruct.prior.arraySize/2));
+	bayesStruct.posterior.offset = bayesStruct.prior.offset;
+	//max(0., bayesStruct.bestEstimate - (bayesStruct.prior.scalar*bayesStruct.prior.arraySize/2));//	bayesStruct.prior.offset = max(0., bayesStruct.bestEstimate - (bayesStruct.prior.scalar*bayesStruct.prior.arraySize/2));
 
 	//trying to switch to prior
 	
@@ -289,6 +298,7 @@
 	//calcuateNewInterNoteIntervals();
 	}
 	
+
 	//storedSmoothPlayPosition = smoothPlayPosition;
 	 
 }
@@ -342,7 +352,7 @@
 	for (int i = 0;i < numberOfMatches && matchesFound[i] >= 0 && matchesFound[i] < recordedEventTimes.size();i++){
 	//	printf("match times %i of %i::%f adding likelihood to %f\n", i, numberOfMatches, recordedEventTimes[matchesFound[i]], recordedEventTimes[matchesFound[i]] - bayesStruct.likelihood.offset);
 		//this is the vent time since start of file
-		if (recordedEventTimes[matchesFound[i]] - bayesStruct.likelihood.offset < bayesStruct.likelihood.arraySize){
+		if (recordedEventTimes[matchesFound[i]] - bayesStruct.likelihood.offset < bayesStruct.likelihood.arraySize*bayesStruct.likelihood.scalar){
 	//		double confidenceMeasure = 0;
 	//		if (totalConfidence > 0)
 	//			confidenceMeasure =	bayesStruct.posterior.getValueAtMillis(recordedEventTimes[matchesFound[i]])/totalConfidence;
@@ -678,12 +688,12 @@
 		if (recordedTimeDifference > minimumTimeIntervalForTempoUpdate
 			&& speedRatio < maximumMatchSpeed && speedRatio > minimumMatchSpeed){
 			
-			/*		 printf("(%i)", previousIndex);
+		/*	 printf("(%i)", previousIndex);
 			 printf("[%i] :: ", recordedPreviousIndex);
 			 //	printf(" conf %f & %f ", currentMatchConfidence, previousMatchConfidence);
 			 printf(" rec{%f} vs play(%f) ", recordedTimeDifference, playedTimeDifference);
 			 printf("update on speed ratio %f\n", speedRatio);
-			 */	 
+		*/	 	 
 			//	matchString += " speed: "+ofToString(speedRatio, 3);
 			//	commented for debug
 			
@@ -720,6 +730,7 @@
 	
 	if (needToUpdate)
 		bayesStruct.updateTempoDistribution();
+	
 	//printf("current speed is %f\n", bayesStruct.relativeSpeedPosterior.getIndexInRealTerms(bayesStruct.relativeSpeedPosterior.MAPestimate));
 }
 
@@ -878,24 +889,25 @@
 		
 }
 
-
+/*
 void midiEventHolder::updatePeriodValue(const double& millis){
 
 	double tmp = period;
-/*	
-	while (periodCounter >= 0 && periodCounter < periodValues.size()-1 && periodValues[periodCounter][2] < millis){
-		periodCounter++;
-	}
-	while (periodCounter > 0 && periodValues[periodCounter][2] > millis){
-		periodCounter--;
-	}
- */
+	
+//	while (periodCounter >= 0 && periodCounter < periodValues.size()-1 && periodValues[periodCounter][2] < millis){
+//		periodCounter++;
+//	}
+//	while (periodCounter > 0 && periodValues[periodCounter][2] > millis){
+//		periodCounter--;
+//	}
+ //
 	//period = periodValues[periodCounter][1];
 	
 	if (period != tmp){
 		printf("new period at %f of %f\n", millis, period);
 	}
 }
+*/
 
 void  midiEventHolder::updateNoteCounter(){
 	while (totalNoteCounterIndex < bestMatchIndex){
@@ -907,6 +919,7 @@
 
 
 void midiEventHolder::drawMidiFile(){
+	//ofBackground(80,80,80);
 	
 	//draws midi file on scrolling screen
 	int size = recordedNoteOnMatrix.size();
@@ -1003,8 +1016,8 @@
 	}	
 	
 	//ofDrawBitmapString(ofToString(timeOffsetForScreen, 1), 20,20);
-	
-	//ofDrawBitmapString(timeString, 20, 60);
+	ofSetColor(255,255,255);
+	ofDrawBitmapString(timeString, 20, 60);
 	
 //last played piutch
 	ofSetColor(0,200,0,50);
@@ -1141,24 +1154,24 @@
 	
 }
 
-int midiEventHolder::getLocationFromTicks(double tickPosition){
-	return 0;
+//int midiEventHolder::getLocationFromTicks(double tickPosition){
+//	return 0;
 //	return (int)((float)(tickPosition - numberOfScreensIn*ticksPerScreen)*(*screenWidth)/(float)ticksPerScreen);
 //not used
-}
+//}
 
 int midiEventHolder::getLocationFromMillis(double millisPosition){
 	//(getEventTimeTicks(windowStartTime+matchWindowWidth) - numberOfScreensIn*ticksPerScreen)*(*screenWidth) / (double)ticksPerScreen
 	return (millisPosition - timeOffsetForScreen)*(*screenWidth)/getEventTimeMillis(ticksPerScreen);
 }
 
-
+/*
 void midiEventHolder::exampleCrossUpdate(){
 	
 	bayesStruct.crossUpdateArrays(bayesStruct.posterior, bayesStruct.relativeSpeedPosterior, 200);
 	
 }
-
+*/
 
 void midiEventHolder::setStartPlayingTimes(){
 	startPlayingTime = getTimeNow(0);//ofGetElapsedTimeMillis();