diff src/AccompanimentSynchroniser.cpp @ 17:c96b18dd0f48

adjusting sync parameters
author Andrew N Robertson <andrew.robertson@eecs.qmul.ac.uk>
date Mon, 06 Feb 2012 13:38:19 +0000
parents 680ba08e9925
children d23685b9e766
line wrap: on
line diff
--- a/src/AccompanimentSynchroniser.cpp	Mon Feb 06 12:18:40 2012 +0000
+++ b/src/AccompanimentSynchroniser.cpp	Mon Feb 06 13:38:19 2012 +0000
@@ -11,6 +11,24 @@
 
 AccompanimentSynchroniser::AccompanimentSynchroniser(){
 	sender.setup(HOST,  SENDER_PORT);
+	counter = 0;
+	reset();
+}
+
+void AccompanimentSynchroniser::reset(){
+	
+	
+	playingPositionRatio = 0;
+	playingPositionSamples = 0;
+	playingPositionMillis = 0;
+	playingPositionTimeSent = 0;
+	
+	recordedPositionMillis = 0;
+	recordedPositionTimeSent = 0;
+	
+	speed = 1;
+	
+	
 }
 
 void AccompanimentSynchroniser::setPlayingRatio(const double& ratio, const double& timePlayed){
@@ -21,33 +39,44 @@
 }
 
 
-void AccompanimentSynchroniser::updateRecordedPosition(const double& currentAlignmentPosition){
+void AccompanimentSynchroniser::updateRecordedPosition(const double& currentAlignmentPosition, const double& timeSent){
 	recordedPositionMillis = currentAlignmentPosition;
+	recordedPositionTimeSent = timeSent;
 }
 
 
 void AccompanimentSynchroniser::updateOutputSpeed(){
 	//we want the playing position to more closely align with the recordedPosition
+
 	
 	double difference = recordedPositionMillis - playingPositionMillis;
+//	difference -=  (recordedPositionTimeSent - playingPositionTimeSent);
 	
-	//suppose we project that we will align in two seconds time
-	double projectedAlignmentTime = 2000;
+	//suppose we project that we will align in 1 seconds time
+	double projectedAlignmentTime = 1000;
 	
 	//then our current speed projects forward
 	double predictedPlayingTimeIncrease = projectedAlignmentTime*speed;
-	
 	difference += (projectedAlignmentTime - predictedPlayingTimeIncrease);
-	
+	double tmpSpeed = speed;
 	speed += difference/projectedAlignmentTime;
 	
 	sendSpeed(speed);
+	
+	if (counter % 10 == 0){
+		printf("SYNC: rec pos %f play pos %f, diff %f\n", recordedPositionMillis, playingPositionMillis, recordedPositionMillis - playingPositionMillis);
+		printf("projected align diff %f at speed %f\n", (projectedAlignmentTime - predictedPlayingTimeIncrease), tmpSpeed);
+		printf("Final difference %f and speed output %f\n\n", difference, speed);
+	}
+		counter++;
 
 }
 
 void AccompanimentSynchroniser::sendSpeed(double const& val){
-	ofxOscMessage m;
-	m.setAddress( "/setSpeed" );
-	m.addFloatArg( val );
-	sender.sendMessage( m );
+	if (val > -3 && val < 4){
+		ofxOscMessage m;
+		m.setAddress( "/setSpeed" );
+		m.addFloatArg( val );
+		sender.sendMessage( m );
+	}
 }
\ No newline at end of file