Mercurial > hg > multitrack-audio-matcher
comparison 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 |
comparison
equal
deleted
inserted
replaced
16:680ba08e9925 | 17:c96b18dd0f48 |
---|---|
9 | 9 |
10 #include "AccompanimentSynchroniser.h" | 10 #include "AccompanimentSynchroniser.h" |
11 | 11 |
12 AccompanimentSynchroniser::AccompanimentSynchroniser(){ | 12 AccompanimentSynchroniser::AccompanimentSynchroniser(){ |
13 sender.setup(HOST, SENDER_PORT); | 13 sender.setup(HOST, SENDER_PORT); |
14 counter = 0; | |
15 reset(); | |
16 } | |
17 | |
18 void AccompanimentSynchroniser::reset(){ | |
19 | |
20 | |
21 playingPositionRatio = 0; | |
22 playingPositionSamples = 0; | |
23 playingPositionMillis = 0; | |
24 playingPositionTimeSent = 0; | |
25 | |
26 recordedPositionMillis = 0; | |
27 recordedPositionTimeSent = 0; | |
28 | |
29 speed = 1; | |
30 | |
31 | |
14 } | 32 } |
15 | 33 |
16 void AccompanimentSynchroniser::setPlayingRatio(const double& ratio, const double& timePlayed){ | 34 void AccompanimentSynchroniser::setPlayingRatio(const double& ratio, const double& timePlayed){ |
17 playingPositionRatio = ratio; | 35 playingPositionRatio = ratio; |
18 playingPositionSamples = ratio * fileLengthSamples; | 36 playingPositionSamples = ratio * fileLengthSamples; |
19 playingPositionMillis = playingPositionSamples * 1000.0/44100.0; | 37 playingPositionMillis = playingPositionSamples * 1000.0/44100.0; |
20 playingPositionTimeSent = timePlayed; | 38 playingPositionTimeSent = timePlayed; |
21 } | 39 } |
22 | 40 |
23 | 41 |
24 void AccompanimentSynchroniser::updateRecordedPosition(const double& currentAlignmentPosition){ | 42 void AccompanimentSynchroniser::updateRecordedPosition(const double& currentAlignmentPosition, const double& timeSent){ |
25 recordedPositionMillis = currentAlignmentPosition; | 43 recordedPositionMillis = currentAlignmentPosition; |
44 recordedPositionTimeSent = timeSent; | |
26 } | 45 } |
27 | 46 |
28 | 47 |
29 void AccompanimentSynchroniser::updateOutputSpeed(){ | 48 void AccompanimentSynchroniser::updateOutputSpeed(){ |
30 //we want the playing position to more closely align with the recordedPosition | 49 //we want the playing position to more closely align with the recordedPosition |
50 | |
31 | 51 |
32 double difference = recordedPositionMillis - playingPositionMillis; | 52 double difference = recordedPositionMillis - playingPositionMillis; |
53 // difference -= (recordedPositionTimeSent - playingPositionTimeSent); | |
33 | 54 |
34 //suppose we project that we will align in two seconds time | 55 //suppose we project that we will align in 1 seconds time |
35 double projectedAlignmentTime = 2000; | 56 double projectedAlignmentTime = 1000; |
36 | 57 |
37 //then our current speed projects forward | 58 //then our current speed projects forward |
38 double predictedPlayingTimeIncrease = projectedAlignmentTime*speed; | 59 double predictedPlayingTimeIncrease = projectedAlignmentTime*speed; |
39 | |
40 difference += (projectedAlignmentTime - predictedPlayingTimeIncrease); | 60 difference += (projectedAlignmentTime - predictedPlayingTimeIncrease); |
41 | 61 double tmpSpeed = speed; |
42 speed += difference/projectedAlignmentTime; | 62 speed += difference/projectedAlignmentTime; |
43 | 63 |
44 sendSpeed(speed); | 64 sendSpeed(speed); |
65 | |
66 if (counter % 10 == 0){ | |
67 printf("SYNC: rec pos %f play pos %f, diff %f\n", recordedPositionMillis, playingPositionMillis, recordedPositionMillis - playingPositionMillis); | |
68 printf("projected align diff %f at speed %f\n", (projectedAlignmentTime - predictedPlayingTimeIncrease), tmpSpeed); | |
69 printf("Final difference %f and speed output %f\n\n", difference, speed); | |
70 } | |
71 counter++; | |
45 | 72 |
46 } | 73 } |
47 | 74 |
48 void AccompanimentSynchroniser::sendSpeed(double const& val){ | 75 void AccompanimentSynchroniser::sendSpeed(double const& val){ |
49 ofxOscMessage m; | 76 if (val > -3 && val < 4){ |
50 m.setAddress( "/setSpeed" ); | 77 ofxOscMessage m; |
51 m.addFloatArg( val ); | 78 m.setAddress( "/setSpeed" ); |
52 sender.sendMessage( m ); | 79 m.addFloatArg( val ); |
80 sender.sendMessage( m ); | |
81 } | |
53 } | 82 } |