diff jnmr/midiEventHolder.cpp @ 34:9d2a651a87b2

autoomatically set prior that matches the durations of the songs. Using 120 bpm as default for the recorded part.
author Andrew N Robertson <andrew.robertson@eecs.qmul.ac.uk>
date Wed, 14 Dec 2011 11:35:31 +0000
parents fa527df85c2c
children 6cd3e0075adf
line wrap: on
line diff
--- a/jnmr/midiEventHolder.cpp	Mon Dec 12 12:46:17 2011 +0000
+++ b/jnmr/midiEventHolder.cpp	Wed Dec 14 11:35:31 2011 +0000
@@ -14,12 +14,13 @@
 
 midiEventHolder::midiEventHolder(){
 //	recordedNoteOnIndex = 0;
+	alignmentPosition = 0;
 	
 	useTempoPrior = false;//puts sine wave round tempo
 	confidenceWeightingUsed = true;
 	newOptimalMethod = true;
 	
-	matchWindowWidth = 8000;//window size for matching in ms 
+	matchWindowWidth = 16000;//window size for matching in ms 
 	interNoteRange = 1600;//preferred duration
 	//so max here is really four
 	
@@ -71,7 +72,7 @@
 	
 	
 	speedWindowWidthMillis =  1600;//4000
-	speedPriorValue = 1.0;
+
 	noteHeight = (*screenHeight) / (float)(noteMaximum - noteMinimum);
 	
 
@@ -96,7 +97,7 @@
 	
 	noteArrayIndex = 0;
 	tickLocation = 0;
-	lastPeriodUpdateTime = getTimeNow(0);//ofGetElapsedTimeMillis();
+	startPlayingTime = getTimeNow(0);//ofGetElapsedTimeMillis();
 	bayesStruct.lastEventTime = getTimeNow(0);//ofGetElapsedTimeMillis();
 	numberOfScreensIn = 0;
 //	recordedNoteOnIndex = 0;
@@ -111,13 +112,29 @@
 	recordedTotalNoteCounterByPitch.clear();
 	recordedTotalNoteCounterByPitch.assign(127,0);
 	totalNoteCounterIndex = 0;
-	
+
 	interNoteIntervals.clear();
 	
+	smoothPlayPosition = 0.0;
+//	relativeSpeedForSmooth = 1.0;
+//	storedSmoothPlayPosition = smoothPlayPosition;
+//	lastSmoothUpdateTime = getTimeNow(0);
+	
+	printf("reset speed prior is %f\n", speedPriorValue);
 	bayesStruct.resetSpeedToOne();
 	bayesStruct.setSpeedPrior(speedPriorValue);
 	setMatchedNotesBackToFalse();
-	
+
+	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());
+	}
+*/
+	//period = 500.0;
 }
 
 void midiEventHolder::setMatchedNotesBackToFalse(){
@@ -152,7 +169,8 @@
 
 
 double midiEventHolder::getEventTimeTicks(double millis){
-	return (millis * pulsesPerQuarternote / period);
+	return 0.0;
+	//return (millis * pulsesPerQuarternote / period);
 }
 
 double midiEventHolder::getEventTimeMillis(double ticks){
@@ -190,8 +208,6 @@
 //	double timeDifference = ofGetElapsedTimeMillis() - bayesStruct.lastEventTime; 
 	double timeDifference = timePlayed - bayesStruct.lastEventTime; 
 	
-	
-	
 	//printf("note %i played at %f and last event %f time difference %f and current best estmate %f\n", pitch, timePlayed, bayesStruct.lastEventTime, timeDifference, bayesStruct.bestEstimate);
 
 	//addnoise to the tempo distribution
@@ -202,10 +218,12 @@
 //	bayesStruct.addTriangularNoiseToSpeedPosterior(timeDifference * 10 / 100.);
 	}
 	
-	bayesStruct.updateTmpBestEstimate(timeDifference);// debug - didnt work	bayesStruct.bestEstimate = bayesStruct.tmpBestEstimate;
+//	bayesStruct.updateTmpBestEstimate(timeDifference);// debug - didnt work	bayesStruct.bestEstimate = bayesStruct.tmpBestEstimate;
 	bayesStruct.updateBestEstimate(timeDifference);
+
 	bayesStruct.lastBestEstimateUpdateTime = getTimeNow(timePlayed);
-	
+	//updatePeriodValue(bayesStruct.lastBestEstimateUpdateTime);
+
 //	double newMAPestimateTime = bayesStruct.posterior.getIndexInRealTerms(bayesStruct.posterior.MAPestimate);
 	//was offset + bayesStruct.posterior.MAPestimate; but this doesnt include scalar to convert to millis
 
@@ -260,6 +278,8 @@
 	updateTempo();
 	//calcuateNewInterNoteIntervals();
 	}
+	
+	//storedSmoothPlayPosition = smoothPlayPosition;
 	 
 }
 
@@ -807,6 +827,7 @@
 
 
 void midiEventHolder::updatePlayPosition(){
+	//timeDifference = ofGetElapsedTimeMillis() - startPlayingTime;//elpased 
 	
 	//in actual fact if we are changing the speed of the play position 
 	//we will need to update this via the file
@@ -814,24 +835,48 @@
 	//actually time since beginning of file i think
 	
 	double timeDifference = 0;
-	if (runningInRealTime)
-		timeDifference = ofGetElapsedTimeMillis() - lastPeriodUpdateTime;//elpased - lastperiodupdatetime
+	
+	if (runningInRealTime){
+		
+		bayesStruct.updateBestEstimate(timeDifference);
+		//	bayesStruct.updateTmpBestEstimate(timeDifference);
+	}
+	
+	if (smoothPlayPosition < bayesStruct.bestEstimate)
+		smoothPlayPosition = bayesStruct.bestEstimate;
+	
+//	playPositionInMillis = timeDifference;//based on updating from when we change period
+	//this to be added
+
 	
 	//this is time diff in milliseconds
 	//then we have 
 	double quarterNoteIntervals = (timeDifference / period);
 	tickLocation = quarterNoteIntervals * pulsesPerQuarternote; 
 	
-	playPositionInMillis = timeDifference;//based on updating from when we change period
-	//this to be added
-	
-	if (runningInRealTime)
-	bayesStruct.updateBestEstimate(timeDifference);
-	
 	updateNoteCounter();
 	
 }
 
+
+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--;
+	}
+ */
+	//period = periodValues[periodCounter][1];
+	
+	if (period != tmp){
+		printf("new period at %f of %f\n", millis, period);
+	}
+}
+
 void  midiEventHolder::updateNoteCounter(){
 	while (totalNoteCounterIndex < bestMatchIndex){
 		int tmpPitch = recordedNoteOnMatrix[totalNoteCounterIndex][1];
@@ -902,11 +947,11 @@
 		
 		//orange line at best estimate
 		xLocation = getLocationFromMillis(bayesStruct.bestEstimate);
-		ofSetColor(80,80,80);//250,100,0);
+		ofSetColor(250,250,20);//250,100,0);
 		ofLine(xLocation, 0, xLocation, (*screenHeight));
 		
-		xLocation = getLocationFromMillis(bayesStruct.tmpBestEstimate);
-		ofSetColor(150,150,150);//250,100,0);
+		xLocation = getLocationFromMillis(smoothPlayPosition);//bayesStruct.tmpBestEstimate
+		ofSetColor(0,250,0);//250,150, 250,100,0);
 		ofLine(xLocation, 0, xLocation, (*screenHeight));
 		
 		
@@ -1008,6 +1053,8 @@
 void midiEventHolder::drawFile(){	
 		drawMidiFile();
 		
+	ofSetColor(0,0,255);
+	ofDrawBitmapString("period"+ofToString(period, 2), ofGetWidth() - 180, 20);
 	
 //	bayesStruct.drawArrays();
 	
@@ -1075,7 +1122,9 @@
 }
 
 int midiEventHolder::getLocationFromTicks(double tickPosition){
-	return (int)((float)(tickPosition - numberOfScreensIn*ticksPerScreen)*(*screenWidth)/(float)ticksPerScreen);
+	return 0;
+//	return (int)((float)(tickPosition - numberOfScreensIn*ticksPerScreen)*(*screenWidth)/(float)ticksPerScreen);
+//not used
 }
 
 int midiEventHolder::getLocationFromMillis(double millisPosition){
@@ -1092,8 +1141,8 @@
 
 
 void midiEventHolder::setStartPlayingTimes(){
-	lastPeriodUpdateTime = getTimeNow(0);//ofGetElapsedTimeMillis();
-	startTime = lastPeriodUpdateTime;
+	startPlayingTime = getTimeNow(0);//ofGetElapsedTimeMillis();
+	//startTime = startPlayingTime;
 	
 /*	
  	bayesStruct.lastEventTime = 0;//ofGetElapsedTimeMillis();
@@ -1213,4 +1262,33 @@
 	for (int i = 0;i < recordedTotalNoteCounterByPitch.size();i++){
 		printf("RECORDED TOTAL[%i] := %i", i, recordedTotalNoteCounterByPitch[i]);
 	}
-}
\ No newline at end of file
+}
+
+/*	double timeDiff = 0;//timeDifference;
+ if (runningInRealTime)
+ timeDiff = ofGetElapsedTimeMillis() - lastSmoothUpdateTime;
+ //		bayesStruct.lastBestEstimateUpdateTime;
+ 
+ //smoothPlayPosition = bayesStruct.bestEstimate + 100;
+ //relativeSpeedForSmooth = 0.1;
+ //bayesStruct.relativeSpeedPosterior.getIndexInRealTerms(bayesStruct.relativeSpeedPosterior.MAPestimate);
+ //   speedEstimate*2;
+ 
+ smoothPlayPosition = storedSmoothPlayPosition + timeDiff * relativeSpeedForSmooth;// * bayesStruct.speedEstimate;
+ storedSmoothPlayPosition = smoothPlayPosition;
+ lastSmoothUpdateTime = getTimeNow(bayesStruct.lastBestEstimateUpdateTime);
+ updateSmoothPlaySpeed();
+ //bayesStruct.posterior.getIndexInRealTerms(bayesStruct.posterior.MAPestimate) 
+ 
+ }
+ 
+ void  midiEventHolder::updateSmoothPlaySpeed(){
+ //project where current play pos will be in two seconds and aim for it.
+ double timeToAimForMillis = 2000.0;
+ //double timeDiff = ofGetElapsedTimeMillis() - bayesStruct.lastBestEstimateUpdateTime;//since the update need to get to where we are
+ double projection = bayesStruct.bestEstimate + bayesStruct.speedEstimate*timeToAimForMillis;//
+ double timeDifferenceFromSmooth = projection - smoothPlayPosition;
+ relativeSpeedForSmooth = timeDifferenceFromSmooth / timeToAimForMillis;
+ 
+ }
+*/