andrew@16: /* andrew@16: * AccompanimentSynchroniser.cpp andrew@16: * MultipleAudioMathcher andrew@16: * andrew@16: * Created by Andrew on 06/02/2012. andrew@16: * Copyright 2012 QMUL. All rights reserved. andrew@16: * andrew@16: */ andrew@16: andrew@16: #include "AccompanimentSynchroniser.h" andrew@16: andrew@16: AccompanimentSynchroniser::AccompanimentSynchroniser(){ andrew@16: sender.setup(HOST, SENDER_PORT); andrew@17: counter = 0; andrew@17: reset(); andrew@17: } andrew@17: andrew@17: void AccompanimentSynchroniser::reset(){ andrew@17: andrew@17: andrew@17: playingPositionRatio = 0; andrew@17: playingPositionSamples = 0; andrew@17: playingPositionMillis = 0; andrew@17: playingPositionTimeSent = 0; andrew@17: andrew@17: recordedPositionMillis = 0; andrew@17: recordedPositionTimeSent = 0; andrew@17: andrew@17: speed = 1; andrew@17: andrew@17: andrew@16: } andrew@16: andrew@16: void AccompanimentSynchroniser::setPlayingRatio(const double& ratio, const double& timePlayed){ andrew@16: playingPositionRatio = ratio; andrew@16: playingPositionSamples = ratio * fileLengthSamples; andrew@16: playingPositionMillis = playingPositionSamples * 1000.0/44100.0; andrew@16: playingPositionTimeSent = timePlayed; andrew@16: } andrew@16: andrew@16: andrew@17: void AccompanimentSynchroniser::updateRecordedPosition(const double& currentAlignmentPosition, const double& timeSent){ andrew@16: recordedPositionMillis = currentAlignmentPosition; andrew@17: recordedPositionTimeSent = timeSent; andrew@16: } andrew@16: andrew@16: andrew@16: void AccompanimentSynchroniser::updateOutputSpeed(){ andrew@16: //we want the playing position to more closely align with the recordedPosition andrew@17: andrew@16: andrew@16: double difference = recordedPositionMillis - playingPositionMillis; andrew@17: // difference -= (recordedPositionTimeSent - playingPositionTimeSent); andrew@16: andrew@17: //suppose we project that we will align in 1 seconds time andrew@17: double projectedAlignmentTime = 1000; andrew@16: andrew@16: //then our current speed projects forward andrew@16: double predictedPlayingTimeIncrease = projectedAlignmentTime*speed; andrew@16: difference += (projectedAlignmentTime - predictedPlayingTimeIncrease); andrew@17: double tmpSpeed = speed; andrew@16: speed += difference/projectedAlignmentTime; andrew@16: andrew@16: sendSpeed(speed); andrew@17: andrew@17: if (counter % 10 == 0){ andrew@17: printf("SYNC: rec pos %f play pos %f, diff %f\n", recordedPositionMillis, playingPositionMillis, recordedPositionMillis - playingPositionMillis); andrew@17: printf("projected align diff %f at speed %f\n", (projectedAlignmentTime - predictedPlayingTimeIncrease), tmpSpeed); andrew@17: printf("Final difference %f and speed output %f\n\n", difference, speed); andrew@17: } andrew@17: counter++; andrew@16: andrew@16: } andrew@16: andrew@16: void AccompanimentSynchroniser::sendSpeed(double const& val){ andrew@17: if (val > -3 && val < 4){ andrew@17: ofxOscMessage m; andrew@17: m.setAddress( "/setSpeed" ); andrew@17: m.addFloatArg( val ); andrew@17: sender.sendMessage( m ); andrew@17: } andrew@16: }