annotate src/AccompanimentSynchroniser.cpp @ 53:5274e3b5479d

Smoothed output, added tempo distribution variation to match the output
author Andrew N Robertson <andrew.robertson@eecs.qmul.ac.uk>
date Tue, 14 Aug 2012 21:45:12 +0100
parents d23685b9e766
children 2eca10a31ae2
rev   line source
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@17 24
andrew@17 25 playingPositionRatio = 0;
andrew@17 26 playingPositionSamples = 0;
andrew@17 27 playingPositionMillis = 0;
andrew@17 28 playingPositionTimeSent = 0;
andrew@17 29
andrew@17 30 recordedPositionMillis = 0;
andrew@17 31 recordedPositionTimeSent = 0;
andrew@17 32
andrew@17 33 speed = 1;
andrew@53 34 smoothedSpeedOutput = 1;
andrew@53 35 difference = 0;
andrew@17 36
andrew@16 37 }
andrew@16 38
andrew@16 39 void AccompanimentSynchroniser::setPlayingRatio(const double& ratio, const double& timePlayed){
andrew@16 40 playingPositionRatio = ratio;
andrew@16 41 playingPositionSamples = ratio * fileLengthSamples;
andrew@16 42 playingPositionMillis = playingPositionSamples * 1000.0/44100.0;
andrew@16 43 playingPositionTimeSent = timePlayed;
andrew@16 44 }
andrew@16 45
andrew@16 46
andrew@17 47 void AccompanimentSynchroniser::updateRecordedPosition(const double& currentAlignmentPosition, const double& timeSent){
andrew@16 48 recordedPositionMillis = currentAlignmentPosition;
andrew@17 49 recordedPositionTimeSent = timeSent;
andrew@16 50 }
andrew@16 51
andrew@16 52
andrew@16 53 void AccompanimentSynchroniser::updateOutputSpeed(){
andrew@16 54 //we want the playing position to more closely align with the recordedPosition
andrew@17 55
andrew@53 56
andrew@53 57 difference += 0.5*((recordedPositionMillis - playingPositionMillis)-difference);
andrew@17 58 // difference -= (recordedPositionTimeSent - playingPositionTimeSent);
andrew@16 59
andrew@17 60 //suppose we project that we will align in 1 seconds time
andrew@17 61 double projectedAlignmentTime = 1000;
andrew@16 62
andrew@16 63 //then our current speed projects forward
andrew@53 64 double predictedPlayingTimeIncrease = projectedAlignmentTime * smoothedSpeedOutput;//speed;
andrew@16 65 difference += (projectedAlignmentTime - predictedPlayingTimeIncrease);
andrew@53 66 double tmpSpeed = smoothedSpeedOutput;
andrew@53 67 speed = smoothedSpeedOutput + (difference/projectedAlignmentTime);
andrew@16 68
andrew@53 69 smoothedSpeedOutput = movingAverageWeighting*speed + (1- movingAverageWeighting)*tmpSpeed;
andrew@53 70
andrew@53 71 sendSpeed(smoothedSpeedOutput);
andrew@17 72
andrew@17 73 if (counter % 10 == 0){
andrew@17 74 printf("SYNC: rec pos %f play pos %f, diff %f\n", recordedPositionMillis, playingPositionMillis, recordedPositionMillis - playingPositionMillis);
andrew@17 75 printf("projected align diff %f at speed %f\n", (projectedAlignmentTime - predictedPlayingTimeIncrease), tmpSpeed);
andrew@17 76 printf("Final difference %f and speed output %f\n\n", difference, speed);
andrew@17 77 }
andrew@17 78 counter++;
andrew@16 79
andrew@16 80 }
andrew@16 81
andrew@16 82 void AccompanimentSynchroniser::sendSpeed(double const& val){
andrew@17 83 if (val > -3 && val < 4){
andrew@17 84 ofxOscMessage m;
andrew@17 85 m.setAddress( "/setSpeed" );
andrew@17 86 m.addFloatArg( val );
andrew@17 87 sender.sendMessage( m );
andrew@53 88
andrew@53 89 ofxOscMessage z;
andrew@53 90 z.setAddress( "/syncDifference" );
andrew@53 91 z.addFloatArg( difference );
andrew@53 92 sender.sendMessage( z );
andrew@17 93 }
andrew@16 94 }