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@45
|
12 //recorded position millis is the current alignment position, sent from updateAlignment in event matcher
|
andrew@45
|
13 //time sent is the live time since start of play
|
andrew@45
|
14
|
andrew@16
|
15 AccompanimentSynchroniser::AccompanimentSynchroniser(){
|
andrew@16
|
16 sender.setup(HOST, SENDER_PORT);
|
andrew@17
|
17 counter = 0;
|
andrew@53
|
18 movingAverageWeighting = 0.4;
|
andrew@17
|
19 reset();
|
andrew@17
|
20 }
|
andrew@17
|
21
|
andrew@17
|
22 void AccompanimentSynchroniser::reset(){
|
andrew@17
|
23
|
andrew@55
|
24 printf("ACC SYNC RESET\n");
|
andrew@17
|
25
|
andrew@17
|
26 playingPositionRatio = 0;
|
andrew@17
|
27 playingPositionSamples = 0;
|
andrew@17
|
28 playingPositionMillis = 0;
|
andrew@17
|
29 playingPositionTimeSent = 0;
|
andrew@17
|
30
|
andrew@17
|
31 recordedPositionMillis = 0;
|
andrew@17
|
32 recordedPositionTimeSent = 0;
|
andrew@55
|
33
|
andrew@17
|
34 speed = 1;
|
andrew@53
|
35 smoothedSpeedOutput = 1;
|
andrew@53
|
36 difference = 0;
|
andrew@55
|
37
|
andrew@55
|
38 sendSpeed(smoothedSpeedOutput);
|
andrew@17
|
39
|
andrew@16
|
40 }
|
andrew@16
|
41
|
andrew@16
|
42 void AccompanimentSynchroniser::setPlayingRatio(const double& ratio, const double& timePlayed){
|
andrew@16
|
43 playingPositionRatio = ratio;
|
andrew@16
|
44 playingPositionSamples = ratio * fileLengthSamples;
|
andrew@16
|
45 playingPositionMillis = playingPositionSamples * 1000.0/44100.0;
|
andrew@16
|
46 playingPositionTimeSent = timePlayed;
|
andrew@16
|
47 }
|
andrew@16
|
48
|
andrew@16
|
49
|
andrew@17
|
50 void AccompanimentSynchroniser::updateRecordedPosition(const double& currentAlignmentPosition, const double& timeSent){
|
andrew@16
|
51 recordedPositionMillis = currentAlignmentPosition;
|
andrew@17
|
52 recordedPositionTimeSent = timeSent;
|
andrew@16
|
53 }
|
andrew@16
|
54
|
andrew@16
|
55
|
andrew@16
|
56 void AccompanimentSynchroniser::updateOutputSpeed(){
|
andrew@16
|
57 //we want the playing position to more closely align with the recordedPosition
|
andrew@17
|
58
|
andrew@53
|
59
|
andrew@55
|
60 difference += 0.5*((recordedPositionMillis - playingPositionMillis) - difference);
|
andrew@17
|
61 // difference -= (recordedPositionTimeSent - playingPositionTimeSent);
|
andrew@16
|
62
|
andrew@17
|
63 //suppose we project that we will align in 1 seconds time
|
andrew@17
|
64 double projectedAlignmentTime = 1000;
|
andrew@16
|
65
|
andrew@16
|
66 //then our current speed projects forward
|
andrew@53
|
67 double predictedPlayingTimeIncrease = projectedAlignmentTime * smoothedSpeedOutput;//speed;
|
andrew@16
|
68 difference += (projectedAlignmentTime - predictedPlayingTimeIncrease);
|
andrew@53
|
69 double tmpSpeed = smoothedSpeedOutput;
|
andrew@53
|
70 speed = smoothedSpeedOutput + (difference/projectedAlignmentTime);
|
andrew@16
|
71
|
andrew@53
|
72 smoothedSpeedOutput = movingAverageWeighting*speed + (1- movingAverageWeighting)*tmpSpeed;
|
andrew@53
|
73
|
andrew@53
|
74 sendSpeed(smoothedSpeedOutput);
|
andrew@17
|
75
|
andrew@17
|
76 if (counter % 10 == 0){
|
andrew@17
|
77 printf("SYNC: rec pos %f play pos %f, diff %f\n", recordedPositionMillis, playingPositionMillis, recordedPositionMillis - playingPositionMillis);
|
andrew@17
|
78 printf("projected align diff %f at speed %f\n", (projectedAlignmentTime - predictedPlayingTimeIncrease), tmpSpeed);
|
andrew@17
|
79 printf("Final difference %f and speed output %f\n\n", difference, speed);
|
andrew@17
|
80 }
|
andrew@17
|
81 counter++;
|
andrew@16
|
82
|
andrew@16
|
83 }
|
andrew@16
|
84
|
andrew@16
|
85 void AccompanimentSynchroniser::sendSpeed(double const& val){
|
andrew@55
|
86 if (val > -2 && val < 2.){
|
andrew@17
|
87 ofxOscMessage m;
|
andrew@17
|
88 m.setAddress( "/setSpeed" );
|
andrew@17
|
89 m.addFloatArg( val );
|
andrew@17
|
90 sender.sendMessage( m );
|
andrew@53
|
91
|
andrew@53
|
92 ofxOscMessage z;
|
andrew@53
|
93 z.setAddress( "/syncDifference" );
|
andrew@53
|
94 z.addFloatArg( difference );
|
andrew@53
|
95 sender.sendMessage( z );
|
andrew@55
|
96 } else {
|
andrew@55
|
97 printf("TRY TO SEND START rec pos %f play %f\n", recordedPositionMillis, playingPositionMillis);
|
andrew@55
|
98 ofxOscMessage z;
|
andrew@55
|
99 z.setAddress( "/setToSyncPlayingPosition" );
|
andrew@55
|
100 z.addFloatArg( recordedPositionMillis );
|
andrew@55
|
101 sender.sendMessage( z );
|
andrew@55
|
102 sendSpeed(1);
|
andrew@55
|
103 smoothedSpeedOutput = 1;
|
andrew@55
|
104 difference = 0;
|
andrew@17
|
105 }
|
andrew@16
|
106 } |