annotate jnmr/midiEventHolder.cpp @ 35:6cd3e0075adf

now writing out alignment data - correct beat positions for the MIDI file so alignment can be done via iostream over RWV database
author Andrew N Robertson <andrew.robertson@eecs.qmul.ac.uk>
date Wed, 14 Dec 2011 17:28:17 +0000
parents 9d2a651a87b2
children 5a1b0c6fa1fb
rev   line source
andrew@33 1 /*
andrew@33 2 * midiEventHolder.cpp
andrew@33 3 * midiCannamReader3
andrew@33 4 *
andrew@33 5 * Created by Andrew on 19/07/2011.
andrew@33 6 * Copyright 2011 QMUL. All rights reserved.
andrew@33 7 *
andrew@33 8 */
andrew@33 9
andrew@33 10
andrew@33 11 //Main file to look at here is newNoteEvent() - this calls everything else to update the Bayesian array
andrew@33 12
andrew@33 13 #include "midiEventHolder.h"
andrew@35 14 #include <iostream>
andrew@35 15 #include <fstream>
andrew@33 16
andrew@33 17 midiEventHolder::midiEventHolder(){
andrew@33 18 // recordedNoteOnIndex = 0;
andrew@34 19 alignmentPosition = 0;
andrew@33 20
andrew@33 21 useTempoPrior = false;//puts sine wave round tempo
andrew@33 22 confidenceWeightingUsed = true;
andrew@33 23 newOptimalMethod = true;
andrew@33 24
andrew@34 25 matchWindowWidth = 16000;//window size for matching in ms
andrew@33 26 interNoteRange = 1600;//preferred duration
andrew@33 27 //so max here is really four
andrew@33 28
andrew@33 29
andrew@33 30 likelihoodWidth = 100;//using 100 is good
andrew@33 31 likelihoodToNoiseRatio = 0.20;//was 0.02 on 18/11/11, changing to give more weight to observations
andrew@33 32 //was 0.08 on 11/12/11 but need more for tempo varn in rwc database
andrew@33 33
andrew@33 34 bayesStruct.speedLikelihoodNoise = 0.1;//was 0.05
andrew@33 35 bayesStruct.speedDecayWidth = 40;
andrew@33 36 bayesStruct.speedDecayAmount = 10;
andrew@33 37
andrew@33 38
andrew@33 39 //there is option to use MAP estinate or integral in beayesianarraystricture class
andrew@33 40
andrew@33 41 runningInRealTime = true;
andrew@33 42 bayesStruct.realTimeMode = &runningInRealTime;
andrew@33 43
andrew@33 44 minimumMatchSpeed = 0.0;
andrew@33 45 maximumMatchSpeed = 2.0;
andrew@33 46 minimumTimeIntervalForTempoUpdate = 150;
andrew@33 47
andrew@33 48 width = ofGetWidth();
andrew@33 49 height = ofGetHeight();
andrew@33 50 screenWidth= &width;
andrew@33 51 screenHeight = &height;
andrew@33 52
andrew@33 53 ticksPerScreen = 4000;
andrew@33 54 tickLocation = 0;
andrew@33 55 pulsesPerQuarternote = 240;
andrew@33 56 noteArrayIndex = 0;
andrew@33 57 noteMinimum = 30;
andrew@33 58 noteMaximum = 96;
andrew@33 59
andrew@33 60
andrew@33 61
andrew@33 62
andrew@33 63
andrew@33 64 speedPriorValue = 1.0;
andrew@33 65
andrew@33 66
andrew@33 67 bayesStruct.resetSize(matchWindowWidth);
andrew@33 68 bayesStruct.setPositionDistributionScalar(1);
andrew@33 69
andrew@33 70 bayesStruct.resetSpeedSize(200);
andrew@33 71 bayesStruct.setRelativeSpeedScalar(0.01);
andrew@33 72 bayesStruct.relativeSpeedPrior.getMaximum();
andrew@33 73 //bayesStruct.simpleExample();
andrew@33 74
andrew@33 75
andrew@33 76 speedWindowWidthMillis = 1600;//4000
andrew@34 77
andrew@33 78 noteHeight = (*screenHeight) / (float)(noteMaximum - noteMinimum);
andrew@33 79
andrew@33 80
andrew@33 81 intervalsToCheck.push_back(1);
andrew@33 82 intervalsToCheck.push_back(2);
andrew@33 83 //intervalsToCheck.push_back(3);
andrew@33 84 intervalsToCheck.push_back(4);
andrew@33 85 intervalsToCheck.push_back(6);
andrew@33 86 intervalsToCheck.push_back(8);
andrew@33 87 intervalsToCheck.push_back(16);
andrew@33 88
andrew@33 89
andrew@33 90 drawPhaseMode = true;
andrew@33 91
andrew@33 92 printf("lookup index %f value %f\n", bayesStruct.prior.getLookupIndex(100, 30., 10.0), bayesStruct.prior.gaussianLookupTable[(int)bayesStruct.prior.getLookupIndex(100, 30., 10.0)]);
andrew@33 93 }
andrew@33 94
andrew@33 95
andrew@33 96
andrew@33 97 void midiEventHolder::reset(){
andrew@33 98 //called when we start playing
andrew@33 99
andrew@33 100 noteArrayIndex = 0;
andrew@33 101 tickLocation = 0;
andrew@34 102 startPlayingTime = getTimeNow(0);//ofGetElapsedTimeMillis();
andrew@33 103 bayesStruct.lastEventTime = getTimeNow(0);//ofGetElapsedTimeMillis();
andrew@33 104 numberOfScreensIn = 0;
andrew@33 105 // recordedNoteOnIndex = 0;
andrew@33 106 bayesStruct.setNewDistributionOffsets(0);
andrew@33 107 bayesStruct.posterior.offset = 0;
andrew@33 108
andrew@33 109 playedEventTimes.clear();
andrew@33 110 playedNoteOnMatrix.clear();
andrew@33 111 matchMatrix.clear();
andrew@33 112 bestMatchIndex = 0;
andrew@33 113
andrew@33 114 recordedTotalNoteCounterByPitch.clear();
andrew@33 115 recordedTotalNoteCounterByPitch.assign(127,0);
andrew@33 116 totalNoteCounterIndex = 0;
andrew@34 117
andrew@33 118 interNoteIntervals.clear();
andrew@33 119
andrew@35 120 smoothIndex = 0;
andrew@34 121 smoothPlayPosition = 0.0;
andrew@34 122 // relativeSpeedForSmooth = 1.0;
andrew@34 123 // storedSmoothPlayPosition = smoothPlayPosition;
andrew@34 124 // lastSmoothUpdateTime = getTimeNow(0);
andrew@34 125
andrew@34 126 printf("reset speed prior is %f\n", speedPriorValue);
andrew@33 127 bayesStruct.resetSpeedToOne();
andrew@33 128 bayesStruct.setSpeedPrior(speedPriorValue);
andrew@33 129 setMatchedNotesBackToFalse();
andrew@34 130
andrew@34 131 periodCounter = 0;
andrew@34 132 for (int i = 0;i < periodValues.size();i++){
andrew@34 133 // printf("period at %f is %f\n", periodValues[i][2], periodValues[i][1]);
andrew@34 134 }
andrew@34 135 /* if (periodValues.size() > 0){
andrew@34 136 updatePeriodValue(0);// periodValues[0][2];
andrew@34 137 printf("Resetting period to %f , size is %i\n", period, (int)periodValues.size());
andrew@34 138 }
andrew@34 139 */
andrew@34 140 //period = 500.0;
andrew@33 141 }
andrew@33 142
andrew@33 143 void midiEventHolder::setMatchedNotesBackToFalse(){
andrew@33 144 for (int i = 0;i < noteOnMatches.size();i++)
andrew@33 145 noteOnMatches[i] = false;
andrew@33 146 }
andrew@33 147
andrew@33 148 void midiEventHolder::clearAllEvents(){
andrew@33 149 recordedNoteOnMatrix.clear();
andrew@33 150 matchesFound.clear();
andrew@33 151 noteOnMatches.clear();
andrew@33 152 recordedEventTimes.clear();
andrew@33 153 measureVector.clear();
andrew@33 154 //played events:
andrew@33 155 playedEventTimes.clear();
andrew@33 156 playedNoteOnMatrix.clear();
andrew@33 157 matchMatrix.clear();
andrew@33 158 bestMatchFound.clear();
andrew@33 159 periodValues.clear();
andrew@33 160
andrew@35 161 beatPositions.clear();
andrew@35 162
andrew@33 163 recordedTotalNoteCounterByPitch.clear();
andrew@33 164 recordedTotalNoteCounterByPitch.assign(127, 0);
andrew@33 165 totalNoteCounterIndex = 0;
andrew@33 166 }
andrew@33 167
andrew@33 168 void midiEventHolder::printNotes(){
andrew@33 169 printf("RECORDED MATRIX\n");
andrew@33 170 for (int i = 0;i < recordedNoteOnMatrix.size();i++){
andrew@33 171 printf("ticktime %i :: pitch %i @ millis %f\n", recordedNoteOnMatrix[i][0], recordedNoteOnMatrix[i][1], recordedEventTimes[i]);
andrew@33 172 }
andrew@33 173 }
andrew@33 174
andrew@33 175
andrew@33 176 double midiEventHolder::getEventTimeTicks(double millis){
andrew@34 177 return 0.0;
andrew@34 178 //return (millis * pulsesPerQuarternote / period);
andrew@33 179 }
andrew@33 180
andrew@33 181 double midiEventHolder::getEventTimeMillis(double ticks){
andrew@33 182 return (period * ticks / (double) pulsesPerQuarternote);
andrew@33 183 }
andrew@33 184
andrew@33 185 void midiEventHolder::newNoteOnEvent(int pitch, int velocity, double timePlayed){
andrew@33 186 // tempoSpeedString = "";
andrew@33 187
andrew@33 188 //MOVE INTO BAYESSTRUCT?? XXX
andrew@33 189 //bayesStruct.copyPriorToPosterior();
andrew@33 190 //why was this here??
andrew@33 191 bayesStruct.prior.copyFromDynamicVector(bayesStruct.posterior);//try the otehr way
andrew@33 192 //bayesStruct.copyPriorToPosterior();
andrew@33 193 //need to get new MAP position and set the offset of the arrays
andrew@33 194 //currently bestEstimate is the approx for the new MAP position
andrew@33 195
andrew@33 196 lastPlayedPitch = pitch;
andrew@33 197 //add the new event to our played information matrix
andrew@33 198 IntVector v;
andrew@33 199 v.push_back(pitch);
andrew@33 200 v.push_back(velocity);
andrew@33 201 playedNoteOnMatrix.push_back(v);
andrew@33 202
andrew@33 203
andrew@33 204 //would update the arrays at this point to show where out current location (phase) and tempo is.
andrew@33 205 // double timeNow = ofGetElapsedTimeMillis() - startTime;
andrew@33 206 double timeNow = timePlayed;// - startTime;
andrew@33 207 recentNoteOnTime = timePlayed;
andrew@33 208
andrew@33 209 // printf("Max time %f OF time %f \n", timePlayed, timeNow);
andrew@33 210
andrew@33 211 playedEventTimes.push_back(timePlayed);
andrew@33 212
andrew@33 213 // double timeDifference = ofGetElapsedTimeMillis() - bayesStruct.lastEventTime;
andrew@33 214 double timeDifference = timePlayed - bayesStruct.lastEventTime;
andrew@33 215
andrew@33 216 //printf("note %i played at %f and last event %f time difference %f and current best estmate %f\n", pitch, timePlayed, bayesStruct.lastEventTime, timeDifference, bayesStruct.bestEstimate);
andrew@33 217
andrew@33 218 //addnoise to the tempo distribution
andrew@33 219 //bayesStruct.decaySpeedDistribution(timeDifference);
andrew@33 220
andrew@33 221 if (timeDifference > 50){
andrew@33 222 bayesStruct.addGaussianNoiseToSpeedPosterior(timeDifference * 10.0 / 100.);
andrew@33 223 // bayesStruct.addTriangularNoiseToSpeedPosterior(timeDifference * 10 / 100.);
andrew@33 224 }
andrew@33 225
andrew@34 226 // bayesStruct.updateTmpBestEstimate(timeDifference);// debug - didnt work bayesStruct.bestEstimate = bayesStruct.tmpBestEstimate;
andrew@33 227 bayesStruct.updateBestEstimate(timeDifference);
andrew@34 228
andrew@33 229 bayesStruct.lastBestEstimateUpdateTime = getTimeNow(timePlayed);
andrew@34 230 //updatePeriodValue(bayesStruct.lastBestEstimateUpdateTime);
andrew@34 231
andrew@33 232 // double newMAPestimateTime = bayesStruct.posterior.getIndexInRealTerms(bayesStruct.posterior.MAPestimate);
andrew@33 233 //was offset + bayesStruct.posterior.MAPestimate; but this doesnt include scalar to convert to millis
andrew@33 234
andrew@33 235 timeString = "Pitch:"+ofToString(pitch);
andrew@33 236 timeString += ", time now:"+ofToString(timeNow, 1);
andrew@33 237 timeString += " TD "+ofToString(timeDifference, 1);
andrew@33 238 timeString += " offset "+ofToString(bayesStruct.posterior.offset , 0);
andrew@33 239 timeString += " map Est: "+ofToString(bayesStruct.posterior.MAPestimate, 0);
andrew@33 240 // timeString += " Previous time" + ofToString(newMAPestimateTime,0);
andrew@33 241 timeString += " speedMap "+ofToString(bayesStruct.relativeSpeedPosterior.integratedEstimate, 2);
andrew@33 242 timeString += " :: "+ofToString(bayesStruct.relativeSpeedPosterior.getIndexInRealTerms(bayesStruct.relativeSpeedPosterior.integratedEstimate), 2);
andrew@33 243
andrew@33 244 // newMAPestimateTime += (timeDifference * bayesStruct.relativeSpeedPosterior.getIndexInRealTerms(bayesStruct.relativeSpeedPosterior.MAPestimate));
andrew@33 245 // timeString += " : Predicted MAP time" + ofToString(newMAPestimateTime,0);
andrew@33 246
andrew@33 247 //then we recalculate the window start based on MAP being central
andrew@33 248 //then we do the matches on these and the likelihood on these.
andrew@33 249
andrew@33 250 bayesStruct.setNewDistributionOffsets(max(0., bayesStruct.bestEstimate - (bayesStruct.prior.scalar*bayesStruct.prior.arraySize/2)));
andrew@33 251 // bayesStruct.prior.offset = max(0.,newMAPestimateTime - (bayesStruct.prior.scalar*bayesStruct.prior.arraySize/2));
andrew@33 252
andrew@33 253 timeString += " \n : new offset " + ofToString(bayesStruct.prior.offset , 0);
andrew@33 254 timeString += " \n best estimate "+ofToString(bayesStruct.bestEstimate, 1);
andrew@33 255 timeString += " error "+ofToString(minimumMatchError, 0);
andrew@33 256 timeString += " map "+ofToString(bayesStruct.relativeSpeedPosterior.integratedEstimate, 1);
andrew@33 257 timeString += " rel speed "+ofToString(bayesStruct.relativeSpeedPosterior.getIndexInRealTerms(bayesStruct.relativeSpeedPosterior.integratedEstimate), 2);
andrew@33 258
andrew@33 259
andrew@33 260 //be able to draw the prior in correct location relative to the midi notes
andrew@33 261 //this calculates the cross update of all possible speeds and all possible positions
andrew@33 262 bayesStruct.crossUpdateArrays(bayesStruct.posterior, bayesStruct.relativeSpeedPosterior, timeDifference);
andrew@33 263
andrew@33 264
andrew@33 265 timeString += " new OFF "+ofToString(bayesStruct.bestEstimate - (bayesStruct.prior.scalar*bayesStruct.prior.arraySize/2), 1);
andrew@33 266 timeString += " notearrayindex "+ofToString(noteArrayIndex, 0);
andrew@33 267 //when this is off teh screen there is a problem somehow XXX
andrew@33 268
andrew@33 269 bayesStruct.posterior.offset = max(0., bayesStruct.bestEstimate - (bayesStruct.prior.scalar*bayesStruct.prior.arraySize/2));// bayesStruct.prior.offset = max(0., bayesStruct.bestEstimate - (bayesStruct.prior.scalar*bayesStruct.prior.arraySize/2));
andrew@33 270
andrew@33 271 //trying to switch to prior
andrew@33 272
andrew@33 273
andrew@33 274 bayesStruct.lastEventTime = timePlayed;//bayesStruct.lastEventTime = ofGetElapsedTimeMillis();
andrew@33 275
andrew@33 276 //do the cross update to find current posterior for location
andrew@33 277 // totalConfidence= 0;
andrew@33 278 int numberOfMatchesFound = findLocalMatches(pitch);
andrew@33 279 setMatchLikelihoods(numberOfMatchesFound);
andrew@33 280 bayesStruct.calculatePosterior();
andrew@33 281
andrew@33 282 if (recordedEventTimes.size() > 0){
andrew@33 283 updateTempo();
andrew@33 284 //calcuateNewInterNoteIntervals();
andrew@33 285 }
andrew@34 286
andrew@34 287 //storedSmoothPlayPosition = smoothPlayPosition;
andrew@33 288
andrew@33 289 }
andrew@33 290
andrew@33 291 void midiEventHolder::updateTempo(){
andrew@33 292 //having found matches we have matches for new note and matches for previous notes
andrew@33 293 if (newOptimalMethod)
andrew@33 294 findOptimumTempoPairsToCurrentBestMatch();
andrew@33 295 else if (!confidenceWeightingUsed)
andrew@33 296 findLocalTempoPairs();
andrew@33 297 else
andrew@33 298 findLocalTempoPairsWeightedForConfidence();
andrew@33 299
andrew@33 300
andrew@33 301 //bayesStruct.addGaussianNoiseToSpeedPosterior(10);
andrew@33 302 }
andrew@33 303
andrew@33 304 double midiEventHolder::getTimeNow(double eventTime){
andrew@33 305 double timeNow = eventTime;
andrew@33 306 if (runningInRealTime)
andrew@33 307 timeNow = ofGetElapsedTimeMillis();
andrew@33 308 return timeNow;
andrew@33 309 }
andrew@33 310
andrew@33 311 int midiEventHolder::findLocalMatches(int notePitch){
andrew@33 312
andrew@33 313 //here we find the matches to the new note within appropriate range
andrew@33 314
andrew@33 315 matchString = "";
andrew@33 316
andrew@33 317 windowStartTime = max(0.0,(bayesStruct.bestEstimate - matchWindowWidth/2));//was playPositionInMillis
andrew@33 318 // cout << "best estimate is " << bayesStruct.bestEstimate << endl;
andrew@33 319 int numberOfMatches = findMatch(notePitch, windowStartTime, windowStartTime + matchWindowWidth);
andrew@33 320
andrew@33 321
andrew@33 322 //matchString += " pitch: "+ofToString(notePitch)+" matches "+ofToString(numberOfMatches)+" win start "+ofToString(windowStartTime);
andrew@33 323
andrew@33 324 return numberOfMatches;
andrew@33 325
andrew@33 326
andrew@33 327 }
andrew@33 328
andrew@33 329
andrew@33 330 void midiEventHolder::setMatchLikelihoods(int numberOfMatches){
andrew@33 331 //reset the offset to match the prior
andrew@33 332 bayesStruct.likelihood.offset = bayesStruct.prior.offset;
andrew@33 333 bayesStruct.likelihood.zero();//set to zero
andrew@33 334
andrew@33 335 double quantity = likelihoodToNoiseRatio / numberOfMatches;
andrew@33 336
andrew@33 337 for (int i = 0;i < numberOfMatches && matchesFound[i] >= 0 && matchesFound[i] < recordedEventTimes.size();i++){
andrew@33 338 // printf("match times %i of %i::%f adding likelihood to %f\n", i, numberOfMatches, recordedEventTimes[matchesFound[i]], recordedEventTimes[matchesFound[i]] - bayesStruct.likelihood.offset);
andrew@33 339 //this is the vent time since start of file
andrew@33 340 if (recordedEventTimes[matchesFound[i]] - bayesStruct.likelihood.offset < bayesStruct.likelihood.arraySize){
andrew@33 341 // double confidenceMeasure = 0;
andrew@33 342 // if (totalConfidence > 0)
andrew@33 343 // confidenceMeasure = bayesStruct.posterior.getValueAtMillis(recordedEventTimes[matchesFound[i]])/totalConfidence;
andrew@33 344
andrew@33 345 bayesStruct.likelihood.addGaussianShape(recordedEventTimes[matchesFound[i]] - bayesStruct.likelihood.offset, likelihoodWidth, quantity);//* confidenceMeasure
andrew@33 346 }//end if
andrew@33 347 }
andrew@33 348 bayesStruct.likelihood.addConstant((1-likelihoodToNoiseRatio)/bayesStruct.likelihood.length);
andrew@33 349 }
andrew@33 350
andrew@33 351 int midiEventHolder::findMatch(const int& notePitch, const int& startTime, const int& endTime){
andrew@33 352
andrew@33 353 matchesFound.clear();
andrew@33 354 int startIndex = 0;
andrew@33 355
andrew@33 356 if (recordedEventTimes.size() > 0){
andrew@33 357
andrew@33 358 //get to the right range of events to check in
andrew@33 359 while (startIndex < recordedEventTimes.size() && recordedEventTimes[startIndex] < startTime)
andrew@33 360 startIndex++;
andrew@33 361
andrew@33 362 }
andrew@33 363
andrew@33 364 IntVector v;
andrew@33 365 DoubleVector d;
andrew@33 366 double tmpError = 100000.;//v high error
andrew@33 367
andrew@33 368 double minimumConfidence = 0;
andrew@33 369 while (startIndex < recordedEventTimes.size() && recordedEventTimes[startIndex] < endTime){
andrew@33 370 if (recordedNoteOnMatrix[startIndex][1] == notePitch){
andrew@33 371
andrew@33 372 matchesFound.push_back(startIndex);
andrew@33 373 v.push_back(startIndex);
andrew@33 374 //so startIndex is registered as a match
andrew@33 375
andrew@33 376 double eventConfidence = bayesStruct.posterior.getValueAtMillis(recordedEventTimes[startIndex]);
andrew@33 377 if (eventConfidence > minimumConfidence){
andrew@33 378 minimumConfidence = eventConfidence;
andrew@33 379 bestMatchIndex = startIndex;
andrew@33 380 }
andrew@33 381 d.push_back(eventConfidence);
andrew@33 382
andrew@33 383 double confidence = eventConfidence;//bayesStruct.posterior.getValueAtMillis(mouseX);
andrew@33 384 // recordedEventTimes[startIndex]);
andrew@33 385 // matchString += "["+ofToString(startIndex)+"] = "+ofToString(confidence, 3)+" .";
andrew@33 386
andrew@33 387 if (abs(recordedEventTimes[startIndex] - bayesStruct.bestEstimate) < tmpError){
andrew@33 388 //record the error between expected and observed times
andrew@33 389 tmpError = abs(recordedEventTimes[startIndex] - bayesStruct.bestEstimate);
andrew@33 390 minimumMatchError = tmpError;//recordedEventTimes[startIndex] - bayesStruct.bestEstimate;
andrew@33 391 }
andrew@33 392
andrew@33 393 }
andrew@33 394 startIndex++;
andrew@33 395 }
andrew@33 396
andrew@33 397
andrew@33 398 // printf("%i MATCHES TO Note %i found\n", (int)matchesFound.size(), notePitch);
andrew@33 399 int size = matchesFound.size();
andrew@33 400 if (size > 0)
andrew@33 401 noteOnMatches[bestMatchIndex] = true;
andrew@33 402
andrew@33 403 v.insert(v.begin() , (int)size);//at beginning, we list how many matches there are that we have found
andrew@33 404 d.insert(d.begin() , (double)size);
andrew@33 405
andrew@33 406 //v.push_back(size);
andrew@33 407 //d.push_back(size);
andrew@33 408 //for (int i = 0;i < matchesFound.size()+1;i++){
andrew@33 409 // v.push_back(matchesFound[i]);
andrew@33 410 // printf("match %i,[%i] is %i\n", startIndex, i, v[i]);
andrew@33 411 //}
andrew@33 412
andrew@33 413
andrew@33 414 matchMatrix.push_back(v);
andrew@33 415 matchConfidence.push_back(d);
andrew@33 416
andrew@33 417 //bringing in way to list only the best matches and use these in tempo process
andrew@33 418 bestMatchFound.push_back(bestMatchIndex);
andrew@33 419
andrew@33 420 // printf("BEST MATCH TO note %i, start time %i, endtime %i, time %i is recorded time %i, confidence %0.2f\n", notePitch, startTime, endTime, (int) recordedEventTimes[bestMatchIndex], minimumConfidence);
andrew@33 421
andrew@33 422 return size;
andrew@33 423 }
andrew@33 424
andrew@33 425 bool midiEventHolder::checkIfMatchedNote(const int& tmpIndex){
andrew@33 426 for (int i = 0;i < matchesFound.size();i++){
andrew@33 427 if (matchesFound[i] == tmpIndex)
andrew@33 428 return true;
andrew@33 429 }
andrew@33 430 return false;
andrew@33 431 }
andrew@33 432
andrew@33 433
andrew@33 434
andrew@33 435 void midiEventHolder::findLocalTempoPairs(){
andrew@33 436
andrew@33 437 int currentPlayedIndex = playedNoteOnMatrix.size()-1;
andrew@33 438 // printf("played %i : %i, vel %i\n", currentPlayedIndex, playedNoteOnMatrix[currentPlayedIndex][0], playedNoteOnMatrix[currentPlayedIndex][1]);
andrew@33 439 // printMatchesFound();
andrew@33 440 // printMatchMatrix();
andrew@33 441 // printf("possible notes \n");
andrew@33 442 bool needToUpdate = false;
andrew@33 443 bayesStruct.setLikelihoodToConstant();
andrew@33 444
andrew@33 445
andrew@33 446 for (int i = 0;i < matchMatrix[currentPlayedIndex][0];i++){
andrew@33 447 //iterate through the recently matched events - even dodgy matches included
andrew@33 448 //size, index of match0, index of match1, ....
andrew@33 449
andrew@33 450
andrew@33 451 int recordedCurrentIndex = matchMatrix[currentPlayedIndex][i+1];
andrew@33 452
andrew@33 453 int previousIndex = currentPlayedIndex-1;
andrew@33 454
andrew@33 455
andrew@33 456 while (previousIndex >= 0 && playedEventTimes[previousIndex] + speedWindowWidthMillis > playedEventTimes[currentPlayedIndex]) {
andrew@33 457 double playedTimeDifference = playedEventTimes[currentPlayedIndex] - playedEventTimes[previousIndex];
andrew@33 458
andrew@33 459 for (int k = 0;k < matchMatrix[previousIndex][0];k++){
andrew@33 460
andrew@33 461 int recordedPreviousIndex = matchMatrix[previousIndex][k+1];
andrew@33 462
andrew@33 463 double recordedTimeDifference = recordedEventTimes[recordedCurrentIndex] - recordedEventTimes[recordedPreviousIndex];
andrew@33 464
andrew@33 465
andrew@33 466 //we want the speed of the recording relative to that of the playing live
andrew@33 467
andrew@33 468 double speedRatio = recordedTimeDifference / playedTimeDifference;
andrew@33 469 if (recordedTimeDifference > minimumTimeIntervalForTempoUpdate &&
andrew@33 470 speedRatio <= maximumMatchSpeed && speedRatio >= minimumMatchSpeed){
andrew@33 471
andrew@33 472 //adding in a prior that prefers 1
andrew@33 473 double priorWeighting = 1;
andrew@33 474 if (useTempoPrior)
andrew@33 475 priorWeighting = sin(speedRatio * PI/2);
andrew@33 476
andrew@33 477
andrew@33 478
andrew@33 479 /*
andrew@33 480 printf("(%i)", matchMatrix[currentPlayedIndex][i+1]);
andrew@33 481 printf("[%i] :: ", recordedPreviousIndex);
andrew@33 482 printf(" rec{%f} vs play(%f) ", recordedTimeDifference, playedTimeDifference);
andrew@33 483 printf("update on speed ratio %f\n", speedRatio);
andrew@33 484 */
andrew@33 485 // matchString += " speed: "+ofToString(speedRatio, 3);
andrew@33 486 // commented for debug
andrew@33 487
andrew@33 488 //bayesStruct.updateTempoDistribution(speedRatio, 0.1);//second paramter is confidence in the match
andrew@33 489 double amount = (1-bayesStruct.speedLikelihoodNoise)/10;
andrew@33 490 amount *= priorWeighting;
andrew@33 491 bayesStruct.updateTempoLikelihood(speedRatio, amount);
andrew@33 492 // tempoSpeedString += ofToString(recordedPreviousIndex) + " "+ ofToString(speedRatio, 2) + " "+ofToString(amount, 2) += " \n";
andrew@33 493 needToUpdate = true;
andrew@33 494 }
andrew@33 495 // printf("\n");
andrew@33 496 }
andrew@33 497
andrew@33 498 previousIndex--;
andrew@33 499 }//end while previousindex countdown
andrew@33 500 }//end for loop through possible current matches
andrew@33 501
andrew@33 502 if (needToUpdate)
andrew@33 503 bayesStruct.updateTempoDistribution();
andrew@33 504
andrew@33 505 //printf("current speed is %f\n", bayesStruct.relativeSpeedPosterior.getIndexInRealTerms(bayesStruct.relativeSpeedPosterior.MAPestimate));
andrew@33 506 }
andrew@33 507
andrew@33 508
andrew@33 509 void midiEventHolder::findLocalTempoPairsWeightedForConfidence(){
andrew@33 510 bool needToUpdate = false;
andrew@33 511
andrew@33 512 DoubleVector speedIntervalsFound;
andrew@33 513
andrew@33 514 //adapted this to just use the best match for each note
andrew@33 515
andrew@33 516 int currentPlayedIndex = playedNoteOnMatrix.size()-1;
andrew@33 517 // printf("played %i : %i, vel %i\n", currentPlayedIndex, playedNoteOnMatrix[currentPlayedIndex][0], playedNoteOnMatrix[currentPlayedIndex][1]);
andrew@33 518 // printMatchesFound();
andrew@33 519 // printMatchMatrix();
andrew@33 520 // printf("possible notes \n");
andrew@33 521
andrew@33 522 bayesStruct.setLikelihoodToConstant();
andrew@33 523
andrew@33 524 int recordedCurrentIndex = bestMatchFound[currentPlayedIndex];
andrew@33 525 //we only look at intervals between the current best match and other recent best matched notes
andrew@33 526 //that is the difference in confidence method
andrew@33 527
andrew@33 528
andrew@33 529 //printf("BEST MATCH FOUND for index %i is %i ", currentPlayedIndex, bestMatchFound[currentPlayedIndex]);
andrew@33 530
andrew@33 531 int previousIndex = currentPlayedIndex-1;
andrew@33 532
andrew@33 533 //withing speedwindow i.e. 4 seconds
andrew@33 534 while (previousIndex >= 0 && playedEventTimes[previousIndex] + speedWindowWidthMillis > playedEventTimes[currentPlayedIndex]) {
andrew@33 535 double playedTimeDifference = playedEventTimes[currentPlayedIndex] - playedEventTimes[previousIndex];
andrew@33 536
andrew@33 537 int recordedPreviousIndex = bestMatchFound[previousIndex];
andrew@33 538
andrew@33 539 double recordedTimeDifference = recordedEventTimes[recordedCurrentIndex] - recordedEventTimes[recordedPreviousIndex];
andrew@33 540
andrew@33 541
andrew@33 542 //we want the speed of the recording relative to that of the playing live
andrew@33 543
andrew@33 544 double speedRatio = recordedTimeDifference / playedTimeDifference;
andrew@33 545 if (recordedTimeDifference > minimumTimeIntervalForTempoUpdate
andrew@33 546 && speedRatio < maximumMatchSpeed && speedRatio > minimumMatchSpeed){
andrew@33 547
andrew@33 548 /* printf("(%i)", previousIndex);
andrew@33 549 printf("[%i] :: ", recordedPreviousIndex);
andrew@33 550 // printf(" conf %f & %f ", currentMatchConfidence, previousMatchConfidence);
andrew@33 551 printf(" rec{%f} vs play(%f) ", recordedTimeDifference, playedTimeDifference);
andrew@33 552 printf("update on speed ratio %f\n", speedRatio);
andrew@33 553 */
andrew@33 554 // matchString += " speed: "+ofToString(speedRatio, 3);
andrew@33 555 // commented for debug
andrew@33 556
andrew@33 557
andrew@33 558 double priorWeighting = 1;
andrew@33 559
andrew@33 560 if (useTempoPrior)
andrew@33 561 priorWeighting = sin(speedRatio * PI/2);//adding in a prior that prefers 1.0 speed
andrew@33 562
andrew@33 563
andrew@33 564 // double weighting = previousMatchConfidence * currentMatchConfidence ;
andrew@33 565 double amount = (1-bayesStruct.speedLikelihoodNoise)*priorWeighting/16;//was 9
andrew@33 566
andrew@33 567 speedIntervalsFound.push_back(speedRatio);
andrew@33 568 // bayesStruct.updateTempoLikelihood(speedRatio, amount);//second paramter is confidence in the match
andrew@33 569
andrew@33 570 // tempoSpeedString += ofToString(recordedCurrentIndex) + " :: " + ofToString(recordedPreviousIndex);
andrew@33 571 // tempoSpeedString += " " + ofToString(recordedTimeDifference)+ " " + ofToString(speedRatio, 2) + " "+ofToString(amount, 2) += " \n";
andrew@33 572
andrew@33 573 needToUpdate = true;
andrew@33 574 }
andrew@33 575 // printf("\n");
andrew@33 576
andrew@33 577
andrew@33 578 previousIndex--;
andrew@33 579 }//end while previousindex countdown
andrew@33 580
andrew@33 581 if (speedIntervalsFound.size() > 0){
andrew@33 582 double amount = (1 - bayesStruct.speedLikelihoodNoise) / speedIntervalsFound.size();
andrew@33 583 for (int i = 0;i < speedIntervalsFound.size();i++)
andrew@33 584 bayesStruct.updateTempoLikelihood(speedIntervalsFound[i], amount);
andrew@33 585 }
andrew@33 586
andrew@33 587
andrew@33 588 if (needToUpdate)
andrew@33 589 bayesStruct.updateTempoDistribution();
andrew@33 590 //printf("current speed is %f\n", bayesStruct.relativeSpeedPosterior.getIndexInRealTerms(bayesStruct.relativeSpeedPosterior.MAPestimate));
andrew@33 591 }
andrew@33 592
andrew@33 593 double midiEventHolder::getBestSpeedEstimate(const int& currentPlayedIndex, const int& equivalentRecordedIndex){
andrew@33 594 double estimate = 1.0;
andrew@33 595 if (bestMatchIndex > 0){
andrew@33 596 double accordingToFileLengthEstimate = recordedEventTimes[equivalentRecordedIndex] - recordedEventTimes[0];
andrew@33 597 double playedEquivalent = (playedEventTimes[currentPlayedIndex] - playedEventTimes[0]);
andrew@33 598 if (accordingToFileLengthEstimate > 0 && playedEquivalent > 0)
andrew@33 599 accordingToFileLengthEstimate /= playedEquivalent;
andrew@33 600 estimate = accordingToFileLengthEstimate;
andrew@33 601 }
andrew@33 602 return estimate;
andrew@33 603 }
andrew@33 604
andrew@33 605 void midiEventHolder::findOptimumTempoPairsToCurrentBestMatch(){
andrew@33 606 bool needToUpdate = false;
andrew@33 607
andrew@33 608 DoubleVector speedIntervalsFound;
andrew@33 609
andrew@33 610 double currentSpeedEstimate = bayesStruct.relativeSpeedPosterior.getIndexInRealTerms(bayesStruct.relativeSpeedPosterior.MAPestimate);
andrew@33 611
andrew@33 612 int currentPlayedIndex = playedNoteOnMatrix.size()-1;
andrew@33 613
andrew@33 614 bayesStruct.setLikelihoodToConstant();
andrew@33 615
andrew@33 616 int recordedCurrentIndex = bestMatchFound[currentPlayedIndex];
andrew@33 617 //we only look at intervals between the current best match and other recent best matched notes
andrew@33 618
andrew@33 619 //printf("BEST MATCH FOUND for index %i is %i ", currentPlayedIndex, bestMatchFound[currentPlayedIndex]);
andrew@33 620
andrew@33 621 int previousIndex = currentPlayedIndex-1;
andrew@33 622
andrew@33 623 //withing speedwindow i.e. 4 seconds
andrew@33 624 while (previousIndex >= 0 && playedEventTimes[previousIndex] + speedWindowWidthMillis > playedEventTimes[currentPlayedIndex]) {
andrew@33 625
andrew@33 626 double playedTimeDifference = playedEventTimes[currentPlayedIndex] - playedEventTimes[previousIndex];
andrew@33 627
andrew@33 628 int recordedPreviousIndex = bestMatchFound[previousIndex];
andrew@33 629
andrew@33 630 double recordedTimeDifference = recordedEventTimes[recordedCurrentIndex] - recordedEventTimes[recordedPreviousIndex];
andrew@33 631
andrew@33 632 //we want the speed of the recording relative to that of the playing live
andrew@33 633 double speedRatio = recordedTimeDifference / playedTimeDifference;
andrew@33 634
andrew@33 635 //now check if this can be closer the observed value
andrew@33 636 int checkRecordedCurrentIndex = recordedCurrentIndex;
andrew@33 637 int checkRecordedPreviousIndex ;//= recordedCurrentIndex;
andrew@33 638 int currentPlayedPitch = playedNoteOnMatrix[currentPlayedIndex][1];
andrew@33 639 int previousPlayedPitch = playedNoteOnMatrix[previousIndex][1];
andrew@33 640
andrew@33 641 double recordedTimeOfBestMatch = recordedEventTimes[recordedCurrentIndex];
andrew@33 642
andrew@33 643 //change this so we start first in window and go to end
andrew@33 644
andrew@33 645 while (checkRecordedCurrentIndex >= 0 && recordedEventTimes[checkRecordedCurrentIndex] > recordedTimeOfBestMatch - matchWindowWidth){
andrew@33 646
andrew@33 647 checkRecordedCurrentIndex--;
andrew@33 648 }
andrew@33 649
andrew@33 650 double bestSpeedEstimate = getBestSpeedEstimate(currentPlayedIndex, bestMatchIndex);
andrew@33 651
andrew@33 652 while (checkRecordedCurrentIndex < recordedEventTimes.size() && recordedEventTimes[checkRecordedCurrentIndex] < recordedTimeOfBestMatch + matchWindowWidth ){
andrew@33 653 if (recordedNoteOnMatrix[checkRecordedCurrentIndex][1] == currentPlayedPitch ){
andrew@33 654 checkRecordedPreviousIndex = checkRecordedCurrentIndex;
andrew@33 655 double recordedTimeCurrent = recordedEventTimes[checkRecordedCurrentIndex] ;
andrew@33 656 while (checkRecordedPreviousIndex >= 0 && recordedEventTimes[checkRecordedPreviousIndex] + maximumMatchSpeed*playedTimeDifference > recordedTimeCurrent ) {
andrew@33 657 if (recordedNoteOnMatrix[checkRecordedPreviousIndex][1] == previousPlayedPitch){
andrew@33 658 //we have a candidate
andrew@33 659 double speedToTest = recordedEventTimes[checkRecordedCurrentIndex] - recordedEventTimes[checkRecordedPreviousIndex];
andrew@33 660 speedToTest /= playedTimeDifference;
andrew@33 661 if (abs(speedToTest-currentSpeedEstimate) < abs(speedRatio - currentSpeedEstimate) ){
andrew@33 662 speedRatio = speedToTest;
andrew@33 663 }
andrew@33 664 }
andrew@33 665 checkRecordedPreviousIndex--;
andrew@33 666 }
andrew@33 667 }
andrew@33 668 checkRecordedCurrentIndex++;
andrew@33 669 }
andrew@33 670
andrew@33 671
andrew@33 672 if (recordedTimeDifference > minimumTimeIntervalForTempoUpdate
andrew@33 673 && speedRatio < maximumMatchSpeed && speedRatio > minimumMatchSpeed){
andrew@33 674
andrew@33 675 /* printf("(%i)", previousIndex);
andrew@33 676 printf("[%i] :: ", recordedPreviousIndex);
andrew@33 677 // printf(" conf %f & %f ", currentMatchConfidence, previousMatchConfidence);
andrew@33 678 printf(" rec{%f} vs play(%f) ", recordedTimeDifference, playedTimeDifference);
andrew@33 679 printf("update on speed ratio %f\n", speedRatio);
andrew@33 680 */
andrew@33 681 // matchString += " speed: "+ofToString(speedRatio, 3);
andrew@33 682 // commented for debug
andrew@33 683
andrew@33 684
andrew@33 685 double priorWeighting = 1;
andrew@33 686
andrew@33 687 if (useTempoPrior)
andrew@33 688 priorWeighting = sin(speedRatio * PI/2);//adding in a prior that prefers 1.0 speed
andrew@33 689
andrew@33 690
andrew@33 691 // double weighting = previousMatchConfidence * currentMatchConfidence ;
andrew@33 692 double amount = (1-bayesStruct.speedLikelihoodNoise)*priorWeighting/16;//was 9
andrew@33 693
andrew@33 694 speedIntervalsFound.push_back(speedRatio);
andrew@33 695 // bayesStruct.updateTempoLikelihood(speedRatio, amount);//second paramter is confidence in the match
andrew@33 696
andrew@33 697 // tempoSpeedString += ofToString(recordedCurrentIndex) + " :: " + ofToString(recordedPreviousIndex);
andrew@33 698 // tempoSpeedString += " " + ofToString(recordedTimeDifference)+ " " + ofToString(speedRatio, 2) + " "+ofToString(amount, 2) += " \n";
andrew@33 699
andrew@33 700 needToUpdate = true;
andrew@33 701 }
andrew@33 702 // printf("\n");
andrew@33 703
andrew@33 704
andrew@33 705 previousIndex--;
andrew@33 706 }//end while previousindex countdown
andrew@33 707
andrew@33 708 if (speedIntervalsFound.size() > 0){
andrew@33 709 double amount = (1 - bayesStruct.speedLikelihoodNoise) / speedIntervalsFound.size();
andrew@33 710 for (int i = 0;i < speedIntervalsFound.size();i++)
andrew@33 711 bayesStruct.updateTempoLikelihood(speedIntervalsFound[i], amount);
andrew@33 712 }
andrew@33 713
andrew@33 714
andrew@33 715 if (needToUpdate)
andrew@33 716 bayesStruct.updateTempoDistribution();
andrew@33 717 //printf("current speed is %f\n", bayesStruct.relativeSpeedPosterior.getIndexInRealTerms(bayesStruct.relativeSpeedPosterior.MAPestimate));
andrew@33 718 }
andrew@33 719
andrew@33 720
andrew@33 721 void midiEventHolder::calcuateNewInterNoteIntervals(){
andrew@33 722 DoubleVector v;
andrew@33 723
andrew@33 724 int currentPlayedIndex = playedNoteOnMatrix.size()-1;
andrew@33 725 // int recordedCurrentIndex = bestMatchFound[currentPlayedIndex];
andrew@33 726 int previousIndex = currentPlayedIndex-1;
andrew@33 727
andrew@33 728 //withing speedwindow i.e. 4 seconds
andrew@33 729 while (previousIndex >= 0 && playedEventTimes[previousIndex] + interNoteRange > playedEventTimes[currentPlayedIndex]) {
andrew@33 730
andrew@33 731 double playedTimeDifference = playedEventTimes[currentPlayedIndex] - playedEventTimes[previousIndex];
andrew@33 732
andrew@33 733 checkForCorrectInterval(playedTimeDifference, &v);
andrew@33 734
andrew@33 735 previousIndex--;
andrew@33 736 }
andrew@33 737
andrew@33 738 if (v.size() > 0)
andrew@33 739 interNoteIntervals.push_back(v);
andrew@33 740 //printf("\n");
andrew@33 741 }
andrew@33 742
andrew@33 743 void midiEventHolder::checkForCorrectInterval(const double& playedTimeDifference, DoubleVector* v){
andrew@33 744 double intervalDuration = 0.0;
andrew@33 745
andrew@33 746 for (int intervalIndex = 0;intervalIndex < 3;intervalIndex++){
andrew@33 747 //on;y check 1,2 and 4
andrew@33 748 double possibleDuration = playedTimeDifference / intervalsToCheck[intervalIndex];
andrew@33 749 if (possibleDuration >= 200 && possibleDuration < 400){
andrew@33 750 v->push_back(possibleDuration);
andrew@33 751 // printf("int %f / %i :: %f ", playedTimeDifference, intervalsToCheck[intervalIndex], possibleDuration);
andrew@33 752 }
andrew@33 753 }
andrew@33 754 }
andrew@33 755
andrew@33 756
andrew@33 757 void midiEventHolder::updatePlayPosition(){
andrew@34 758 //timeDifference = ofGetElapsedTimeMillis() - startPlayingTime;//elpased
andrew@33 759
andrew@33 760 //in actual fact if we are changing the speed of the play position
andrew@33 761 //we will need to update this via the file
andrew@33 762
andrew@33 763 //actually time since beginning of file i think
andrew@33 764
andrew@33 765 double timeDifference = 0;
andrew@34 766
andrew@34 767 if (runningInRealTime){
andrew@34 768
andrew@34 769 bayesStruct.updateBestEstimate(timeDifference);
andrew@34 770 // bayesStruct.updateTmpBestEstimate(timeDifference);
andrew@34 771 }
andrew@34 772
andrew@35 773
andrew@35 774
andrew@35 775 if (smoothPlayPosition < bayesStruct.bestEstimate){
andrew@35 776 updateSmoothPositionTo(bayesStruct.bestEstimate);
andrew@35 777 //smoothPlayPosition = bayesStruct.bestEstimate;
andrew@35 778 }
andrew@34 779
andrew@34 780 // playPositionInMillis = timeDifference;//based on updating from when we change period
andrew@34 781 //this to be added
andrew@34 782
andrew@33 783
andrew@33 784 //this is time diff in milliseconds
andrew@33 785 //then we have
andrew@33 786 double quarterNoteIntervals = (timeDifference / period);
andrew@33 787 tickLocation = quarterNoteIntervals * pulsesPerQuarternote;
andrew@33 788
andrew@33 789 updateNoteCounter();
andrew@33 790
andrew@33 791 }
andrew@33 792
andrew@35 793 void midiEventHolder::updateSmoothPositionTo(const double& newPosition){
andrew@35 794 while (smoothIndex > 0 && recordedEventTimes[smoothIndex] > smoothPlayPosition){
andrew@35 795 smoothIndex--;
andrew@35 796 }
andrew@35 797 while (smoothIndex < recordedEventTimes.size()-1 && recordedEventTimes[smoothIndex] < smoothPlayPosition){
andrew@35 798 smoothIndex++;
andrew@35 799 }
andrew@35 800
andrew@35 801
andrew@35 802 double playingTime = ofGetElapsedTimeMillis();
andrew@35 803 playingTime -= startPlayingTime;
andrew@35 804 //now at the last one
andrew@35 805
andrew@35 806 while (smoothIndex < recordedEventTimes.size() && recordedEventTimes[smoothIndex] < newPosition){
andrew@35 807 if ((*fileOutput).is_open()){
andrew@35 808 (*fileOutput) << beatPositions[smoothIndex] <<", " << recordedNoteOnMatrix[smoothIndex][1] << ", ";
andrew@35 809 (*fileOutput) << playingTime << "\n";
andrew@35 810 }
andrew@35 811
andrew@35 812 smoothIndex++;
andrew@35 813 }
andrew@35 814
andrew@35 815
andrew@35 816 smoothPlayPosition = newPosition;
andrew@35 817
andrew@35 818 }
andrew@35 819
andrew@34 820
andrew@34 821 void midiEventHolder::updatePeriodValue(const double& millis){
andrew@34 822
andrew@34 823 double tmp = period;
andrew@34 824 /*
andrew@34 825 while (periodCounter >= 0 && periodCounter < periodValues.size()-1 && periodValues[periodCounter][2] < millis){
andrew@34 826 periodCounter++;
andrew@34 827 }
andrew@34 828 while (periodCounter > 0 && periodValues[periodCounter][2] > millis){
andrew@34 829 periodCounter--;
andrew@34 830 }
andrew@34 831 */
andrew@34 832 //period = periodValues[periodCounter][1];
andrew@34 833
andrew@34 834 if (period != tmp){
andrew@34 835 printf("new period at %f of %f\n", millis, period);
andrew@34 836 }
andrew@34 837 }
andrew@34 838
andrew@33 839 void midiEventHolder::updateNoteCounter(){
andrew@33 840 while (totalNoteCounterIndex < bestMatchIndex){
andrew@33 841 int tmpPitch = recordedNoteOnMatrix[totalNoteCounterIndex][1];
andrew@33 842 recordedTotalNoteCounterByPitch[tmpPitch] += 1;
andrew@33 843 totalNoteCounterIndex++;
andrew@33 844 }
andrew@33 845 }
andrew@33 846
andrew@33 847
andrew@33 848 void midiEventHolder::drawMidiFile(){
andrew@33 849
andrew@33 850 //draws midi file on scrolling screen
andrew@33 851 int size = recordedNoteOnMatrix.size();
andrew@33 852 if (size > 0){
andrew@33 853
andrew@33 854 numberOfScreensIn = floor(bayesStruct.bestEstimate / getEventTimeMillis(ticksPerScreen));//rpounds down on no screens in
andrew@33 855
andrew@33 856 // numberOfScreensIn = tickLocation / ticksPerScreen;//rounds down
andrew@33 857 timeOffsetForScreen = getEventTimeMillis(numberOfScreensIn * ticksPerScreen);
andrew@33 858
andrew@33 859 while (noteArrayIndex < recordedNoteOnMatrix.size()-1 && tickLocation > recordedNoteOnMatrix[noteArrayIndex][0] )
andrew@33 860 noteArrayIndex++;
andrew@33 861
andrew@33 862
andrew@33 863 while (noteArrayIndex > 0 && noteArrayIndex < size && tickLocation < recordedNoteOnMatrix[noteArrayIndex][0])
andrew@33 864 noteArrayIndex--;
andrew@33 865
andrew@33 866 //need to start where we currently are in file
andrew@33 867 int maxNoteIndexToPrint = noteArrayIndex;
andrew@33 868 int minNoteIndexToPrint = min(size-1,noteArrayIndex);//not needed as changed above
andrew@33 869
andrew@33 870 while (maxNoteIndexToPrint < recordedNoteOnMatrix.size() && recordedNoteOnMatrix[maxNoteIndexToPrint][0] < (numberOfScreensIn+1)*ticksPerScreen )
andrew@33 871 maxNoteIndexToPrint++;
andrew@33 872
andrew@33 873 while (minNoteIndexToPrint > 0 && recordedNoteOnMatrix[minNoteIndexToPrint][0] > numberOfScreensIn*ticksPerScreen)//&& minNoteIndexToPrint < size
andrew@33 874 minNoteIndexToPrint--;
andrew@33 875
andrew@33 876 for (int tmpIndex = max(0,minNoteIndexToPrint);tmpIndex < min(maxNoteIndexToPrint, (int)recordedNoteOnMatrix.size());tmpIndex++){
andrew@33 877
andrew@33 878 ofSetColor(255,255,255);
andrew@33 879 if (checkIfMatchedNote(tmpIndex))
andrew@33 880 ofSetColor(100,100,100);//0,0,255);
andrew@33 881 else if(noteOnMatches[tmpIndex]){
andrew@33 882 ofSetColor(255,0,255);//dark grey
andrew@33 883 }
andrew@33 884 else{
andrew@33 885 ofSetColor(255,255,255);//255,255,255);
andrew@33 886 }
andrew@33 887
andrew@33 888 //ofSetColor(255,255,255);
andrew@33 889 if (tmpIndex == bestMatchIndex)
andrew@33 890 ofSetColor(255,0,0);//best recent match is in red
andrew@33 891
andrew@33 892 // XXX replace ofgetwidth below
andrew@33 893 //if (tmpIndex >= 0 && tmpIndex < size)
andrew@33 894 int xLocation = (float)(recordedNoteOnMatrix[tmpIndex][0] - numberOfScreensIn*ticksPerScreen)*(*screenWidth)/(float)ticksPerScreen;
andrew@33 895 int duration = (float)(recordedNoteOnMatrix[tmpIndex][3]*(*screenWidth))/(float)ticksPerScreen;
andrew@33 896
andrew@33 897
andrew@33 898 int yLocation = (*screenHeight) - ((recordedNoteOnMatrix[tmpIndex][1] - noteMinimum )*(*screenHeight)/ (float)(noteMaximum - noteMinimum));
andrew@33 899 ofRect(xLocation,yLocation, duration, noteHeight);
andrew@33 900
andrew@33 901 }
andrew@33 902
andrew@33 903
andrew@33 904 int xLocation;// = getLocationFromTicks(tickLocation);
andrew@33 905 // ofLine(xLocation, 0, xLocation, (*screenHeight));
andrew@33 906
andrew@33 907 //orange line at best estimate
andrew@33 908 xLocation = getLocationFromMillis(bayesStruct.bestEstimate);
andrew@34 909 ofSetColor(250,250,20);//250,100,0);
andrew@33 910 ofLine(xLocation, 0, xLocation, (*screenHeight));
andrew@33 911
andrew@34 912 xLocation = getLocationFromMillis(smoothPlayPosition);//bayesStruct.tmpBestEstimate
andrew@34 913 ofSetColor(0,250,0);//250,150, 250,100,0);
andrew@33 914 ofLine(xLocation, 0, xLocation, (*screenHeight));
andrew@33 915
andrew@33 916
andrew@33 917 //lines where matching window start and end are
andrew@33 918 ofSetColor(0);//0,100,255);
andrew@33 919 xLocation = getLocationFromMillis(windowStartTime);
andrew@33 920 ofLine(xLocation, 0, xLocation, (*screenHeight));
andrew@33 921 xLocation = getLocationFromMillis(windowStartTime+matchWindowWidth);
andrew@33 922 ofLine(xLocation, 0, xLocation, (*screenHeight));
andrew@33 923
andrew@33 924
andrew@33 925 int maxSize = recordedNoteOnMatrix[size-1][0];
andrew@33 926
andrew@33 927 int tmpIndex = 0;
andrew@33 928 while (tmpIndex < measureVector.size() && measureVector[tmpIndex] < (numberOfScreensIn+1)*ticksPerScreen){
andrew@33 929 int measureLocation = measureVector[tmpIndex];
andrew@33 930 int xLocation = (float)(measureLocation - numberOfScreensIn*ticksPerScreen)*(*screenWidth)/(float)ticksPerScreen;
andrew@33 931 ofSetColor(155,155,0);
andrew@33 932 ofLine(xLocation, 0, xLocation, (*screenHeight));
andrew@33 933 tmpIndex++;
andrew@33 934 }
andrew@33 935
andrew@33 936
andrew@33 937 // ofDrawBitmapString(tempoSpeedString, 20, 20);
andrew@33 938 /* string indexString = "num screens in "+ofToString(numberOfScreensIn)+"; min index to print "+ofToString(minNoteIndexToPrint)+", max index to print "+ofToString(maxNoteIndexToPrint);
andrew@33 939 indexString += " size "+ofToString(size)+" tick loc "+ofToString(tickLocation)+" max size "+ofToString(maxSize);
andrew@33 940 ofDrawBitmapString(indexString, 20, 40);
andrew@33 941 */
andrew@33 942 }
andrew@33 943
andrew@33 944 //ofDrawBitmapString(ofToString(timeOffsetForScreen, 1), 20,20);
andrew@33 945
andrew@33 946 //ofDrawBitmapString(timeString, 20, 60);
andrew@33 947
andrew@33 948 //last played piutch
andrew@33 949 ofSetColor(0,200,0,50);
andrew@33 950 int yLocation = (*screenHeight) - ((lastPlayedPitch - noteMinimum )*(*screenHeight)/ (float)(noteMaximum - noteMinimum));
andrew@33 951 ofRect(0,yLocation, 100, noteHeight);
andrew@33 952
andrew@33 953
andrew@33 954
andrew@33 955 }
andrew@33 956
andrew@33 957
andrew@33 958
andrew@33 959 void midiEventHolder::drawMidiFile(IntMatrix& midiFileToDraw){
andrew@33 960
andrew@33 961 //using this to draw the live input
andrew@33 962
andrew@33 963 //draws midi file on scrolling screen
andrew@33 964 int size = midiFileToDraw.size();
andrew@33 965 if (size > 0){
andrew@33 966
andrew@33 967 numberOfScreensIn = floor(bayesStruct.bestEstimate / getEventTimeMillis(ticksPerScreen));//rpounds down on no screens in
andrew@33 968
andrew@33 969 // numberOfScreensIn = tickLocation / ticksPerScreen;//rounds down
andrew@33 970 timeOffsetForScreen = getEventTimeMillis(numberOfScreensIn * ticksPerScreen);
andrew@33 971
andrew@33 972 while (noteArrayIndex < midiFileToDraw.size()-1 && tickLocation > midiFileToDraw[noteArrayIndex][0] )
andrew@33 973 noteArrayIndex++;
andrew@33 974
andrew@33 975
andrew@33 976 while (noteArrayIndex > 0 && noteArrayIndex < size && tickLocation < midiFileToDraw[noteArrayIndex][0])
andrew@33 977 noteArrayIndex--;
andrew@33 978
andrew@33 979 //need to start where we currently are in file
andrew@33 980 int maxNoteIndexToPrint = noteArrayIndex;
andrew@33 981 int minNoteIndexToPrint = min(size-1,noteArrayIndex);//not needed as changed above
andrew@33 982
andrew@33 983 while (maxNoteIndexToPrint < midiFileToDraw.size() && midiFileToDraw[maxNoteIndexToPrint][0] < (numberOfScreensIn+1)*ticksPerScreen )
andrew@33 984 maxNoteIndexToPrint++;
andrew@33 985
andrew@33 986 while (minNoteIndexToPrint > 0 && midiFileToDraw[minNoteIndexToPrint][0] > numberOfScreensIn*ticksPerScreen)//&& minNoteIndexToPrint < size
andrew@33 987 minNoteIndexToPrint--;
andrew@33 988
andrew@33 989 for (int tmpIndex = max(0,minNoteIndexToPrint);tmpIndex < min(maxNoteIndexToPrint, (int)midiFileToDraw.size());tmpIndex++){
andrew@33 990
andrew@33 991 ofSetColor(0,0,255, 200);
andrew@33 992
andrew@33 993 int xLocation = (float)(midiFileToDraw[tmpIndex][0] - numberOfScreensIn*ticksPerScreen)*(*screenWidth)/(float)ticksPerScreen;
andrew@33 994 int duration = (float)(midiFileToDraw[tmpIndex][3]*(*screenWidth))/(float)ticksPerScreen;
andrew@33 995
andrew@33 996
andrew@33 997 int yLocation = (*screenHeight) - ((midiFileToDraw[tmpIndex][1] - noteMinimum )*(*screenHeight)/ (float)(noteMaximum - noteMinimum));
andrew@33 998 ofRect(xLocation,yLocation, duration, noteHeight);
andrew@33 999
andrew@33 1000 }
andrew@33 1001
andrew@33 1002
andrew@33 1003
andrew@33 1004 }
andrew@33 1005
andrew@33 1006
andrew@33 1007
andrew@33 1008 }
andrew@33 1009
andrew@33 1010
andrew@33 1011
andrew@33 1012 void midiEventHolder::drawFile(){
andrew@33 1013 drawMidiFile();
andrew@33 1014
andrew@34 1015 ofSetColor(0,0,255);
andrew@34 1016 ofDrawBitmapString("period"+ofToString(period, 2), ofGetWidth() - 180, 20);
andrew@33 1017
andrew@33 1018 // bayesStruct.drawArrays();
andrew@33 1019
andrew@33 1020 // ofSetColor(200,200,0);
andrew@33 1021 // bayesStruct.prior.drawConstrainedVector(0, bayesStruct.prior.arraySize, 400, 800);
andrew@33 1022
andrew@33 1023 //need to draw arrays within correct timescope
andrew@33 1024 if (drawPhaseMode)
andrew@33 1025 bayesStruct.drawArraysRelativeToTimeframe(timeOffsetForScreen, timeOffsetForScreen + getEventTimeMillis(ticksPerScreen));
andrew@33 1026
andrew@33 1027 if (drawTempoMode)
andrew@33 1028 bayesStruct.drawTempoArrays();
andrew@33 1029
andrew@33 1030
andrew@33 1031 ofSetColor(0, 0, 0);
andrew@33 1032 //ofDrawBitmapString(matchString, 20, ofGetHeight() - 20);
andrew@33 1033
andrew@33 1034 double confidence = bayesStruct.posterior.getValueAtMillis(mouseX);
andrew@33 1035 /*
andrew@33 1036 string mouseString = "mouseX "+ofToString(confidence, 3)+" .";
andrew@33 1037 ofDrawBitmapString(mouseString, 20 , ofGetHeight() - 40);
andrew@33 1038
andrew@33 1039 string mouseString = "updateCounter "+ofToString(bayesStruct.updateCounter);
andrew@33 1040 ofDrawBitmapString(mouseString, 20 , ofGetHeight() - 40);
andrew@33 1041
andrew@33 1042 string infostring = "speed "+ofToString(bayesStruct.relativeSpeedPosterior.getIndexInRealTerms(bayesStruct.relativeSpeedPosterior.MAPestimate), 3);
andrew@33 1043 ofDrawBitmapString(infostring, 20 , ofGetHeight() - 60);
andrew@33 1044 */
andrew@33 1045
andrew@33 1046 //drawInterNoteIntervals();
andrew@33 1047
andrew@33 1048 }
andrew@33 1049
andrew@33 1050 void midiEventHolder::drawInterNoteIntervals(){
andrew@33 1051
andrew@33 1052 ofSetColor(0,0,150);
andrew@33 1053 int size = interNoteIntervals.size();
andrew@33 1054 int numberToShow = min(100, size);
andrew@33 1055 double x ;
andrew@33 1056 for (int y = 1;y < numberToShow;y++){
andrew@33 1057 for (int point = 0;point < interNoteIntervals[y].size();point++){
andrew@33 1058 double interval = interNoteIntervals[size - y][point];
andrew@33 1059 x = interval - 200;
andrew@33 1060 x *= (*screenWidth) / 200.0;
andrew@33 1061 }
andrew@33 1062 double h = (double)(y * (*screenHeight)) / numberToShow;
andrew@33 1063 ofCircle(x, h, 5);
andrew@33 1064 }
andrew@33 1065
andrew@33 1066 }
andrew@33 1067
andrew@33 1068
andrew@33 1069 void midiEventHolder::printInterNoteIntervals(){
andrew@33 1070
andrew@33 1071 int size = interNoteIntervals.size();
andrew@33 1072 int numberToShow = 20;
andrew@33 1073 double x ;
andrew@33 1074 for (int y = max(0, size - numberToShow);y < interNoteIntervals.size();y++){
andrew@33 1075 for (int point = 0;point < interNoteIntervals[y].size();point++){
andrew@33 1076 printf("[%i][%i] : %f", y, point, interNoteIntervals[y][point]);
andrew@33 1077 }
andrew@33 1078 printf("\n");
andrew@33 1079 }
andrew@33 1080
andrew@33 1081 }
andrew@33 1082
andrew@33 1083 int midiEventHolder::getLocationFromTicks(double tickPosition){
andrew@34 1084 return 0;
andrew@34 1085 // return (int)((float)(tickPosition - numberOfScreensIn*ticksPerScreen)*(*screenWidth)/(float)ticksPerScreen);
andrew@34 1086 //not used
andrew@33 1087 }
andrew@33 1088
andrew@33 1089 int midiEventHolder::getLocationFromMillis(double millisPosition){
andrew@33 1090 //(getEventTimeTicks(windowStartTime+matchWindowWidth) - numberOfScreensIn*ticksPerScreen)*(*screenWidth) / (double)ticksPerScreen
andrew@33 1091 return (millisPosition - timeOffsetForScreen)*(*screenWidth)/getEventTimeMillis(ticksPerScreen);
andrew@33 1092 }
andrew@33 1093
andrew@33 1094
andrew@33 1095 void midiEventHolder::exampleCrossUpdate(){
andrew@33 1096
andrew@33 1097 bayesStruct.crossUpdateArrays(bayesStruct.posterior, bayesStruct.relativeSpeedPosterior, 200);
andrew@33 1098
andrew@33 1099 }
andrew@33 1100
andrew@33 1101
andrew@33 1102 void midiEventHolder::setStartPlayingTimes(){
andrew@34 1103 startPlayingTime = getTimeNow(0);//ofGetElapsedTimeMillis();
andrew@34 1104 //startTime = startPlayingTime;
andrew@33 1105
andrew@33 1106 /*
andrew@33 1107 bayesStruct.lastEventTime = 0;//ofGetElapsedTimeMillis();
andrew@33 1108 bayesStruct.bestEstimate = 0;
andrew@33 1109 bayesStruct.resetArrays();
andrew@33 1110 bayesStruct.lastBestEstimateUpdateTime = ofGetElapsedTimeMillis();
andrew@33 1111 */
andrew@33 1112 bayesStruct.setStartPlaying();
andrew@33 1113 matchString = "";
andrew@33 1114 }
andrew@33 1115
andrew@33 1116
andrew@33 1117 void midiEventHolder::printMatchMatrix(){
andrew@33 1118 printf("match matrix:\n");
andrew@33 1119 for (int i = 0;i < matchMatrix.size();i++){
andrew@33 1120 for (int k = 0;k < matchMatrix[i].size();k++){
andrew@33 1121 printf("%i , ", matchMatrix[i][k]);
andrew@33 1122 }
andrew@33 1123 printf("\n");
andrew@33 1124 }
andrew@33 1125
andrew@33 1126 }
andrew@33 1127
andrew@33 1128
andrew@33 1129
andrew@33 1130 void midiEventHolder::printRecordedEvents(){
andrew@33 1131 printf("Recorded Events:\n");
andrew@33 1132 for (int i = 0;i < recordedNoteOnMatrix.size();i++){
andrew@33 1133 for (int k = 0;k < recordedNoteOnMatrix[i].size();k++){
andrew@33 1134 printf("[%i] = %i ,", i, recordedNoteOnMatrix[i][k]);
andrew@33 1135 }
andrew@33 1136 if (i < recordedEventTimes.size())
andrew@33 1137 printf("time %f \n", recordedEventTimes[i]);
andrew@33 1138 else
andrew@33 1139 printf("\n");
andrew@33 1140 }
andrew@33 1141
andrew@33 1142 }
andrew@33 1143
andrew@33 1144
andrew@33 1145
andrew@33 1146 void midiEventHolder::reorderMatrixFromNoteTimes(IntMatrix& noteOnMatrix){
andrew@33 1147 double currentTime = -19999.;
andrew@33 1148 for (int i = 0;i < noteOnMatrix.size();i++){
andrew@33 1149 int nextIndex = getIndexOfMinimumAboveTime(currentTime, noteOnMatrix);
andrew@33 1150 // cout << "index of min time " << currentTime << " is " << nextIndex << " at time " << noteOnMatrix[nextIndex][0] << endl;
andrew@33 1151
andrew@33 1152 if (nextIndex >= 0 && nextIndex > i && noteOnMatrix[nextIndex][0] < noteOnMatrix[i][0] ){
andrew@33 1153 //which it should be
andrew@33 1154 // cout << " index " << nextIndex << " at time " << noteOnMatrix[nextIndex][0] << " swaps with inex " << i << " at time " << noteOnMatrix[i][0] << endl;
andrew@33 1155 noteOnMatrix[i].swap(noteOnMatrix[nextIndex]);
andrew@35 1156 // double tmp = beatPositions[i];
andrew@35 1157 // beatPositions[i] = beatPositions[nextIndex];
andrew@35 1158 // = tmp;
andrew@35 1159
andrew@35 1160 swap (beatPositions[i], beatPositions[nextIndex]);
andrew@35 1161
andrew@35 1162
andrew@33 1163 currentTime = noteOnMatrix[i][0];
andrew@33 1164 }
andrew@33 1165
andrew@33 1166 }
andrew@33 1167 //printRecordedEvents();
andrew@33 1168
andrew@33 1169 }
andrew@33 1170
andrew@33 1171
andrew@33 1172
andrew@33 1173
andrew@33 1174 void midiEventHolder::doublecheckOrder(IntMatrix& noteOnMatrix){
andrew@33 1175
andrew@33 1176 for (int i = 0;i < noteOnMatrix.size();i++){
andrew@33 1177 int nextIndex = getIndexOfMinimumAboveIndex(i, noteOnMatrix);
andrew@33 1178 if (nextIndex > i){
andrew@33 1179 noteOnMatrix[i].swap(noteOnMatrix[nextIndex]);
andrew@35 1180 swap (beatPositions[i], beatPositions[nextIndex]);
andrew@35 1181
andrew@33 1182 }
andrew@33 1183 }
andrew@33 1184 }
andrew@33 1185
andrew@33 1186 int midiEventHolder::getIndexOfMinimumAboveIndex(const int& index, IntMatrix& noteOnMatrix){
andrew@33 1187 int returnIndex = index;
andrew@33 1188 int min = noteOnMatrix[index][0];
andrew@33 1189 for (int i = index;i < noteOnMatrix.size();i++){
andrew@33 1190 if (noteOnMatrix[i][0] < min){
andrew@33 1191 returnIndex = i;
andrew@33 1192 min = noteOnMatrix[i][0];
andrew@33 1193 }
andrew@33 1194 }
andrew@33 1195 return returnIndex;
andrew@33 1196 }
andrew@33 1197
andrew@33 1198
andrew@33 1199 int midiEventHolder::getIndexOfMinimumAboveTime(const double& time, IntMatrix& noteOnMatrix){
andrew@33 1200 int index = 0;
andrew@33 1201 double minimumTime = 100000000.;
andrew@33 1202 int bestIndex = -1;
andrew@33 1203 while (index < noteOnMatrix.size()){
andrew@33 1204
andrew@33 1205 if (noteOnMatrix[index][0] > time && noteOnMatrix[index][0] < minimumTime){
andrew@33 1206 bestIndex = index;
andrew@33 1207 minimumTime = noteOnMatrix[index][0];
andrew@33 1208 }
andrew@33 1209 index++;
andrew@33 1210 }
andrew@33 1211 return bestIndex;
andrew@33 1212 }
andrew@33 1213
andrew@33 1214
andrew@33 1215
andrew@33 1216
andrew@33 1217 void midiEventHolder::correctTiming(IntMatrix& noteOnMatrix){
andrew@33 1218
andrew@33 1219 if (noteOnMatrix.size() > 0 && noteOnMatrix[0][0] < 0) {
andrew@33 1220 int offset = noteOnMatrix[0][0];
andrew@33 1221 for (int i = 0;i < noteOnMatrix.size();i++){
andrew@33 1222 noteOnMatrix[i][0] -= offset;
andrew@33 1223 }
andrew@33 1224 }
andrew@33 1225
andrew@33 1226 }
andrew@33 1227
andrew@33 1228
andrew@33 1229 void midiEventHolder::printNoteCounter(){
andrew@33 1230 for (int i = 0;i < recordedTotalNoteCounterByPitch.size();i++){
andrew@33 1231 printf("RECORDED TOTAL[%i] := %i", i, recordedTotalNoteCounterByPitch[i]);
andrew@33 1232 }
andrew@34 1233 }
andrew@34 1234
andrew@34 1235 /* double timeDiff = 0;//timeDifference;
andrew@34 1236 if (runningInRealTime)
andrew@34 1237 timeDiff = ofGetElapsedTimeMillis() - lastSmoothUpdateTime;
andrew@34 1238 // bayesStruct.lastBestEstimateUpdateTime;
andrew@34 1239
andrew@34 1240 //smoothPlayPosition = bayesStruct.bestEstimate + 100;
andrew@34 1241 //relativeSpeedForSmooth = 0.1;
andrew@34 1242 //bayesStruct.relativeSpeedPosterior.getIndexInRealTerms(bayesStruct.relativeSpeedPosterior.MAPestimate);
andrew@34 1243 // speedEstimate*2;
andrew@34 1244
andrew@34 1245 smoothPlayPosition = storedSmoothPlayPosition + timeDiff * relativeSpeedForSmooth;// * bayesStruct.speedEstimate;
andrew@34 1246 storedSmoothPlayPosition = smoothPlayPosition;
andrew@34 1247 lastSmoothUpdateTime = getTimeNow(bayesStruct.lastBestEstimateUpdateTime);
andrew@34 1248 updateSmoothPlaySpeed();
andrew@34 1249 //bayesStruct.posterior.getIndexInRealTerms(bayesStruct.posterior.MAPestimate)
andrew@34 1250
andrew@34 1251 }
andrew@34 1252
andrew@34 1253 void midiEventHolder::updateSmoothPlaySpeed(){
andrew@34 1254 //project where current play pos will be in two seconds and aim for it.
andrew@34 1255 double timeToAimForMillis = 2000.0;
andrew@34 1256 //double timeDiff = ofGetElapsedTimeMillis() - bayesStruct.lastBestEstimateUpdateTime;//since the update need to get to where we are
andrew@34 1257 double projection = bayesStruct.bestEstimate + bayesStruct.speedEstimate*timeToAimForMillis;//
andrew@34 1258 double timeDifferenceFromSmooth = projection - smoothPlayPosition;
andrew@34 1259 relativeSpeedForSmooth = timeDifferenceFromSmooth / timeToAimForMillis;
andrew@34 1260
andrew@34 1261 }
andrew@34 1262 */
andrew@35 1263
andrew@35 1264
andrew@35 1265 /*
andrew@35 1266 void midiEventHolder::findLocalTempoPairsWeightedForConfidence(){
andrew@35 1267 bool needToUpdate = false;
andrew@35 1268
andrew@35 1269 //adapted this to just use the best match for each note
andrew@35 1270
andrew@35 1271 int currentPlayedIndex = playedNoteOnMatrix.size()-1;
andrew@35 1272 // printf("played %i : %i, vel %i\n", currentPlayedIndex, playedNoteOnMatrix[currentPlayedIndex][0], playedNoteOnMatrix[currentPlayedIndex][1]);
andrew@35 1273 // printMatchesFound();
andrew@35 1274 // printMatchMatrix();
andrew@35 1275 // printf("possible notes \n");
andrew@35 1276
andrew@35 1277 bayesStruct.setLikelihoodToConstant();
andrew@35 1278
andrew@35 1279 for (int i = 0;i < matchMatrix[currentPlayedIndex][0];i++){
andrew@35 1280
andrew@35 1281 //iterate through the recently matched events - even dodgy matches included
andrew@35 1282 //size, index of match0, index of match1, ....
andrew@35 1283 int recordedCurrentIndex = matchMatrix[currentPlayedIndex][i+1];
andrew@35 1284
andrew@35 1285 double currentMatchConfidence = matchConfidence[currentPlayedIndex][i+1];//new confidence
andrew@35 1286
andrew@35 1287 int previousIndex = currentPlayedIndex-1;
andrew@35 1288
andrew@35 1289 while (previousIndex >= 0 && playedEventTimes[previousIndex] + speedWindowWidthMillis > playedEventTimes[currentPlayedIndex]) {
andrew@35 1290 double playedTimeDifference = playedEventTimes[currentPlayedIndex] - playedEventTimes[previousIndex];
andrew@35 1291
andrew@35 1292 // for (int k = 0;k < matchMatrix[previousIndex][0];k++)
andrew@35 1293 int recordedPreviousIndex = bestMatchFound[previousIndex];//matchMatrix[previousIndex][k+1];
andrew@35 1294
andrew@35 1295 //double previousMatchConfidence = matchConfidence[previousIndex][k+1];
andrew@35 1296
andrew@35 1297
andrew@35 1298 double recordedTimeDifference = recordedEventTimes[recordedCurrentIndex] - recordedEventTimes[recordedPreviousIndex];
andrew@35 1299
andrew@35 1300
andrew@35 1301 //we want the speed of the recording relative to that of the playing live
andrew@35 1302
andrew@35 1303 double speedRatio = recordedTimeDifference / playedTimeDifference;
andrew@35 1304 if (speedRatio <= maximumMatchSpeed && speedRatio >= minimumMatchSpeed){
andrew@35 1305
andrew@35 1306 printf("(%i)", matchMatrix[currentPlayedIndex][i+1]);
andrew@35 1307 printf("[%i] :: ", recordedPreviousIndex);
andrew@35 1308 // printf(" conf %f & %f ", currentMatchConfidence, previousMatchConfidence);
andrew@35 1309 printf(" rec{%f} vs play(%f) ", recordedTimeDifference, playedTimeDifference);
andrew@35 1310 printf("update on speed ratio %f\n", speedRatio);
andrew@35 1311
andrew@35 1312 // matchString += " speed: "+ofToString(speedRatio, 3);
andrew@35 1313 // commented for debug
andrew@35 1314
andrew@35 1315
andrew@35 1316 double priorWeighting = 1;
andrew@35 1317
andrew@35 1318 if (useTempoPrior)
andrew@35 1319 priorWeighting = sin(speedRatio * PI/2);//adding in a prior that prefers 1.0 speed
andrew@35 1320
andrew@35 1321
andrew@35 1322 // double weighting = previousMatchConfidence * currentMatchConfidence ;
andrew@35 1323 double amount = (1-bayesStruct.speedLikelihoodNoise)*priorWeighting/10;
andrew@35 1324 bayesStruct.updateTempoLikelihood(speedRatio, amount);//second paramter is confidence in the match
andrew@35 1325 tempoSpeedString += ofToString(recordedPreviousIndex) + " " + ofToString(speedRatio, 2) + " "+ofToString(amount, 2) += " \n";
andrew@35 1326
andrew@35 1327 needToUpdate = true;
andrew@35 1328 }
andrew@35 1329 // printf("\n");
andrew@35 1330
andrew@35 1331
andrew@35 1332 previousIndex--;
andrew@35 1333 }//end while previousindex countdown
andrew@35 1334 }//end for loop through possible current matches
andrew@35 1335
andrew@35 1336 if (needToUpdate)
andrew@35 1337 bayesStruct.updateTempoDistribution();
andrew@35 1338 //printf("current speed is %f\n", bayesStruct.relativeSpeedPosterior.getIndexInRealTerms(bayesStruct.relativeSpeedPosterior.MAPestimate));
andrew@35 1339 }
andrew@35 1340 */
andrew@35 1341