diff src/AccompanimentSynchroniser.cpp @ 16:680ba08e9925

Auto synchroniser added using the elastique~ object in max
author Andrew N Robertson <andrew.robertson@eecs.qmul.ac.uk>
date Mon, 06 Feb 2012 12:18:40 +0000
parents
children c96b18dd0f48
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/AccompanimentSynchroniser.cpp	Mon Feb 06 12:18:40 2012 +0000
@@ -0,0 +1,53 @@
+/*
+ *  AccompanimentSynchroniser.cpp
+ *  MultipleAudioMathcher
+ *
+ *  Created by Andrew on 06/02/2012.
+ *  Copyright 2012 QMUL. All rights reserved.
+ *
+ */
+
+#include "AccompanimentSynchroniser.h"
+
+AccompanimentSynchroniser::AccompanimentSynchroniser(){
+	sender.setup(HOST,  SENDER_PORT);
+}
+
+void AccompanimentSynchroniser::setPlayingRatio(const double& ratio, const double& timePlayed){
+	playingPositionRatio = ratio;
+	playingPositionSamples = ratio * fileLengthSamples;
+	playingPositionMillis = playingPositionSamples * 1000.0/44100.0;
+	playingPositionTimeSent = timePlayed;
+}
+
+
+void AccompanimentSynchroniser::updateRecordedPosition(const double& currentAlignmentPosition){
+	recordedPositionMillis = currentAlignmentPosition;
+}
+
+
+void AccompanimentSynchroniser::updateOutputSpeed(){
+	//we want the playing position to more closely align with the recordedPosition
+	
+	double difference = recordedPositionMillis - playingPositionMillis;
+	
+	//suppose we project that we will align in two seconds time
+	double projectedAlignmentTime = 2000;
+	
+	//then our current speed projects forward
+	double predictedPlayingTimeIncrease = projectedAlignmentTime*speed;
+	
+	difference += (projectedAlignmentTime - predictedPlayingTimeIncrease);
+	
+	speed += difference/projectedAlignmentTime;
+	
+	sendSpeed(speed);
+
+}
+
+void AccompanimentSynchroniser::sendSpeed(double const& val){
+	ofxOscMessage m;
+	m.setAddress( "/setSpeed" );
+	m.addFloatArg( val );
+	sender.sendMessage( m );
+}
\ No newline at end of file