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