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@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@16: void AccompanimentSynchroniser::updateRecordedPosition(const double& currentAlignmentPosition){ andrew@16: recordedPositionMillis = currentAlignmentPosition; 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@16: andrew@16: double difference = recordedPositionMillis - playingPositionMillis; andrew@16: andrew@16: //suppose we project that we will align in two seconds time andrew@16: double projectedAlignmentTime = 2000; andrew@16: andrew@16: //then our current speed projects forward andrew@16: double predictedPlayingTimeIncrease = projectedAlignmentTime*speed; andrew@16: andrew@16: difference += (projectedAlignmentTime - predictedPlayingTimeIncrease); andrew@16: andrew@16: speed += difference/projectedAlignmentTime; andrew@16: andrew@16: sendSpeed(speed); andrew@16: andrew@16: } andrew@16: andrew@16: void AccompanimentSynchroniser::sendSpeed(double const& val){ andrew@16: ofxOscMessage m; andrew@16: m.setAddress( "/setSpeed" ); andrew@16: m.addFloatArg( val ); andrew@16: sender.sendMessage( m ); andrew@16: }