annotate src/midiEventHolder.cpp @ 11:df9c838d0b8f

fixed bug where the full midi file was sometimes not drawn (since minNoteIndexToPrint) was at the max and did not decrease properly in draw routine
author Andrew N Robertson <andrew.robertson@eecs.qmul.ac.uk>
date Tue, 25 Oct 2011 15:01:52 +0100
parents 2ab6f4670cf5
children 5897a5626f1f
rev   line source
andrew@0 1 /*
andrew@0 2 * midiEventHolder.cpp
andrew@0 3 * midiCannamReader3
andrew@0 4 *
andrew@0 5 * Created by Andrew on 19/07/2011.
andrew@0 6 * Copyright 2011 QMUL. All rights reserved.
andrew@0 7 *
andrew@0 8 */
andrew@0 9 //hello
andrew@0 10
andrew@0 11 #include "midiEventHolder.h"
andrew@0 12
andrew@0 13 midiEventHolder::midiEventHolder(){
andrew@0 14 // recordedNoteOnIndex = 0;
andrew@0 15
andrew@0 16 width = ofGetWidth();
andrew@0 17 height = ofGetHeight();
andrew@0 18 screenWidth= &width;
andrew@0 19 screenHeight = &height;
andrew@0 20
andrew@0 21 ticksPerScreen = 4000;
andrew@0 22 tickLocation = 0;
andrew@0 23 pulsesPerQuarternote = 240;
andrew@0 24 noteArrayIndex = 0;
andrew@0 25 noteMinimum = 30;
andrew@0 26 noteMaximum = 96;
andrew@0 27
andrew@4 28 minimumMatchSpeed = 0.0;
andrew@4 29 maximumMatchSpeed = 2.0;
andrew@2 30
andrew@0 31 likelihoodWidth = 100;
andrew@2 32 likelihoodToNoiseRatio = 0.02;
andrew@0 33
andrew@9 34 bayesStruct.speedLikelihoodNoise = 0.1;//was 0.05
andrew@2 35 bayesStruct.speedDecayWidth = 20;
andrew@2 36 bayesStruct.speedDecayAmount = 10;
andrew@2 37
andrew@3 38 speedPriorValue = 1.0;
andrew@2 39
andrew@2 40 matchWindowWidth = 8000;//window size for matching in ms
andrew@0 41
andrew@1 42 bayesStruct.resetSize(matchWindowWidth);
andrew@2 43 bayesStruct.setPositionDistributionScalar(1);
andrew@2 44
andrew@0 45 bayesStruct.resetSpeedSize(200);
andrew@0 46 bayesStruct.setRelativeSpeedScalar(0.01);
andrew@0 47 bayesStruct.relativeSpeedPrior.getMaximum();
andrew@2 48 //bayesStruct.simpleExample();
andrew@2 49
andrew@6 50
andrew@6 51 speedWindowWidthMillis = 4000;
andrew@3 52 speedPriorValue = 1.0;
andrew@0 53 noteHeight = (*screenHeight) / (float)(noteMaximum - noteMinimum);
andrew@4 54
andrew@6 55 confidenceWeightingUsed = false;
andrew@6 56
andrew@9 57 drawPhaseMode = true;
andrew@9 58
andrew@4 59 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@0 60 }
andrew@0 61
andrew@0 62
andrew@0 63
andrew@0 64 void midiEventHolder::reset(){
andrew@2 65 //called when we start playing
andrew@2 66
andrew@0 67 noteArrayIndex = 0;
andrew@0 68 tickLocation = 0;
andrew@0 69 lastPeriodUpdateTime = ofGetElapsedTimeMillis();
andrew@0 70 bayesStruct.lastEventTime = ofGetElapsedTimeMillis();
andrew@0 71 numberOfScreensIn = 0;
andrew@0 72 // recordedNoteOnIndex = 0;
andrew@0 73 bayesStruct.setNewDistributionOffsets(0);
andrew@0 74 bayesStruct.posterior.offset = 0;
andrew@0 75
andrew@0 76 playedEventTimes.clear();
andrew@0 77 playedNoteOnMatrix.clear();
andrew@0 78 matchMatrix.clear();
andrew@5 79 bestMatchIndex = 0;
andrew@0 80
andrew@0 81 bayesStruct.resetSpeedToOne();
andrew@3 82 bayesStruct.setSpeedPrior(speedPriorValue);
andrew@5 83 setMatchedNotesBackToFalse();
andrew@5 84 }
andrew@5 85
andrew@5 86 void midiEventHolder::setMatchedNotesBackToFalse(){
andrew@5 87 for (int i = 0;i < noteOnMatches.size();i++)
andrew@5 88 noteOnMatches[i] = false;
andrew@0 89 }
andrew@0 90
andrew@1 91 void midiEventHolder::clearAllEvents(){
andrew@1 92 recordedNoteOnMatrix.clear();
andrew@1 93 matchesFound.clear();
andrew@1 94 noteOnMatches.clear();
andrew@1 95 recordedEventTimes.clear();
andrew@1 96
andrew@1 97 //played events:
andrew@1 98 playedEventTimes.clear();
andrew@1 99 playedNoteOnMatrix.clear();
andrew@1 100 matchMatrix.clear();
andrew@1 101 }
andrew@1 102
andrew@0 103 void midiEventHolder::printNotes(){
andrew@0 104 printf("RECORDED MATRIX");
andrew@0 105 for (int i = 0;i < recordedNoteOnMatrix.size();i++){
andrew@0 106 printf("%i :: %i @ %f\n", recordedNoteOnMatrix[i][0], recordedNoteOnMatrix[i][1], recordedEventTimes[i]);
andrew@0 107 }
andrew@0 108 }
andrew@0 109
andrew@0 110
andrew@0 111 double midiEventHolder::getEventTimeTicks(double millis){
andrew@0 112 return (millis * pulsesPerQuarternote / period);
andrew@0 113 }
andrew@0 114
andrew@0 115 double midiEventHolder::getEventTimeMillis(double ticks){
andrew@0 116 return (period * ticks / (double) pulsesPerQuarternote);
andrew@0 117 }
andrew@0 118
andrew@0 119 void midiEventHolder::newNoteOnEvent(int pitch, int velocity, double timePlayed){
andrew@0 120
andrew@0 121 //MOVE INTO BAYESSTRUCT?? XXX
andrew@0 122 //bayesStruct.copyPriorToPosterior();
andrew@0 123 //why was this here??
andrew@0 124 bayesStruct.prior.copyFromDynamicVector(bayesStruct.posterior);//try the otehr way
andrew@0 125 //bayesStruct.copyPriorToPosterior();
andrew@0 126 //need to get new MAP position and set the offset of the arrays
andrew@0 127 //currently bestEstimate is the approx for the new MAP position
andrew@0 128
andrew@0 129
andrew@0 130 //add the new event to our played information matrix
andrew@0 131 IntVector v;
andrew@0 132 v.push_back(pitch);
andrew@0 133 v.push_back(velocity);
andrew@0 134 playedNoteOnMatrix.push_back(v);
andrew@0 135
andrew@0 136
andrew@0 137 //would update the arrays at this point to show where out current location (phase) and tempo is.
andrew@2 138 // double timeNow = ofGetElapsedTimeMillis() - startTime;
andrew@2 139 double timeNow = timePlayed;// - startTime;
andrew@2 140 recentNoteOnTime = timePlayed;
andrew@0 141
andrew@2 142 // printf("Max time %f OF time %f \n", timePlayed, timeNow);
andrew@0 143
andrew@0 144 playedEventTimes.push_back(timePlayed);
andrew@0 145
andrew@2 146 // double timeDifference = ofGetElapsedTimeMillis() - bayesStruct.lastEventTime;
andrew@2 147 double timeDifference = timePlayed - bayesStruct.lastEventTime;
andrew@0 148
andrew@9 149 // printf("note %i played at %f and last event %f\n", pitch, timePlayed, bayesStruct.lastEventTime);
andrew@0 150 //addnoise to the tempo distribution
andrew@2 151 //bayesStruct.decaySpeedDistribution(timeDifference);
andrew@2 152 if (timeDifference > 50){
andrew@2 153 bayesStruct.addGaussianNoiseToSpeedPosterior(timeDifference * 10 / 100.);
andrew@2 154 // bayesStruct.addTriangularNoiseToSpeedPosterior(timeDifference * 10 / 100.);
andrew@2 155 }
andrew@2 156
andrew@2 157 bayesStruct.updateTmpBestEstimate(timeDifference);// debug - didnt work bayesStruct.bestEstimate = bayesStruct.tmpBestEstimate;
andrew@2 158 bayesStruct.updateBestEstimate();
andrew@2 159 bayesStruct.lastBestEstimateUpdateTime = ofGetElapsedTimeMillis();
andrew@0 160
andrew@0 161 // double newMAPestimateTime = bayesStruct.posterior.getIndexInRealTerms(bayesStruct.posterior.MAPestimate);
andrew@0 162 //was offset + bayesStruct.posterior.MAPestimate; but this doesnt include scalar to convert to millis
andrew@0 163
andrew@0 164 timeString = "Pitch:"+ofToString(pitch);
andrew@0 165 timeString += ", time now:"+ofToString(timeNow, 1);
andrew@0 166 timeString += " TD "+ofToString(timeDifference, 1);
andrew@0 167 timeString += " offset "+ofToString(bayesStruct.posterior.offset , 0);
andrew@0 168 timeString += " map Est: "+ofToString(bayesStruct.posterior.MAPestimate, 0);
andrew@0 169 // timeString += " Previous time" + ofToString(newMAPestimateTime,0);
andrew@9 170 timeString += " speedMap "+ofToString(bayesStruct.relativeSpeedPosterior.integratedEstimate, 2);
andrew@9 171 timeString += " :: "+ofToString(bayesStruct.relativeSpeedPosterior.getIndexInRealTerms(bayesStruct.relativeSpeedPosterior.integratedEstimate), 2);
andrew@0 172
andrew@0 173 // newMAPestimateTime += (timeDifference * bayesStruct.relativeSpeedPosterior.getIndexInRealTerms(bayesStruct.relativeSpeedPosterior.MAPestimate));
andrew@0 174 // timeString += " : Predicted MAP time" + ofToString(newMAPestimateTime,0);
andrew@0 175
andrew@0 176 //then we recalculate the window start based on MAP being central
andrew@0 177 //then we do the matches on these and the likelihood on these.
andrew@0 178
andrew@0 179 bayesStruct.setNewDistributionOffsets(max(0., bayesStruct.bestEstimate - (bayesStruct.prior.scalar*bayesStruct.prior.arraySize/2)));
andrew@0 180 // bayesStruct.prior.offset = max(0.,newMAPestimateTime - (bayesStruct.prior.scalar*bayesStruct.prior.arraySize/2));
andrew@0 181
andrew@0 182 timeString += " \n : new offset " + ofToString(bayesStruct.prior.offset , 0);
andrew@0 183 timeString += " \n best estimate "+ofToString(bayesStruct.bestEstimate, 1);
andrew@10 184 timeString += " error "+ofToString(minimumMatchError, 0);
andrew@9 185 timeString += " map "+ofToString(bayesStruct.relativeSpeedPosterior.integratedEstimate, 1);
andrew@11 186 timeString += " rel speed "+ofToString(bayesStruct.relativeSpeedPosterior.getIndexInRealTerms(bayesStruct.relativeSpeedPosterior.integratedEstimate), 2);
andrew@0 187
andrew@0 188
andrew@0 189 //be able to draw the prior in correct location relative to the midi notes
andrew@2 190 //this calculates the cross update of all possible speeds and all possible positions
andrew@0 191 bayesStruct.crossUpdateArrays(bayesStruct.posterior, bayesStruct.relativeSpeedPosterior, timeDifference);
andrew@0 192
andrew@0 193
andrew@0 194 timeString += " new OFF "+ofToString(bayesStruct.bestEstimate - (bayesStruct.prior.scalar*bayesStruct.prior.arraySize/2), 1);
andrew@0 195 timeString += " notearrayindex "+ofToString(noteArrayIndex, 0);
andrew@0 196 //when this is off teh screen there is a problem somehow XXX
andrew@0 197 bayesStruct.posterior.offset = max(0., bayesStruct.bestEstimate - (bayesStruct.prior.scalar*bayesStruct.prior.arraySize/2));
andrew@0 198 // bayesStruct.prior.offset = max(0., bayesStruct.bestEstimate - (bayesStruct.prior.scalar*bayesStruct.prior.arraySize/2));
andrew@0 199 //trying to switch to prior
andrew@0 200
andrew@2 201 //bayesStruct.lastEventTime = ofGetElapsedTimeMillis();
andrew@2 202 bayesStruct.lastEventTime = timePlayed;
andrew@0 203
andrew@0 204 //do the cross update to find current posterior for location
andrew@1 205 // totalConfidence= 0;
andrew@0 206 int numberOfMatchesFound = findLocalMatches(pitch);
andrew@0 207 setMatchLikelihoods(numberOfMatchesFound);
andrew@0 208 bayesStruct.calculatePosterior();
andrew@0 209
andrew@0 210 //having found matches we have matches for new note and matches for previous notes
andrew@6 211 if (!confidenceWeightingUsed)
andrew@0 212 findLocalTempoPairs();
andrew@6 213 else
andrew@6 214 findLocalTempoPairsWeightedForConfidence();
andrew@0 215
andrew@2 216 //bayesStruct.addGaussianNoiseToSpeedPosterior(10);
andrew@0 217
andrew@0 218 }
andrew@0 219
andrew@0 220 int midiEventHolder::findLocalMatches(int notePitch){
andrew@0 221
andrew@0 222 //here we find the matches to the new note within appropriate range
andrew@0 223
andrew@1 224 matchString = "";
andrew@0 225
andrew@0 226 windowStartTime = max(0.0,(bayesStruct.bestEstimate - matchWindowWidth/2));//was playPositionInMillis
andrew@0 227 int numberOfMatches = findMatch(notePitch, windowStartTime, windowStartTime + matchWindowWidth);
andrew@5 228
andrew@0 229
andrew@1 230 matchString += " pitch: "+ofToString(notePitch)+" matches "+ofToString(numberOfMatches)+" win start "+ofToString(windowStartTime);
andrew@1 231
andrew@0 232 return numberOfMatches;
andrew@0 233
andrew@0 234
andrew@0 235 }
andrew@0 236
andrew@0 237
andrew@0 238 void midiEventHolder::setMatchLikelihoods(int numberOfMatches){
andrew@0 239 //reset the offset to match the prior
andrew@0 240 bayesStruct.likelihood.offset = bayesStruct.prior.offset;
andrew@0 241 bayesStruct.likelihood.zero();//set to zero
andrew@0 242
andrew@2 243 double quantity = likelihoodToNoiseRatio / numberOfMatches;
andrew@0 244
andrew@0 245 for (int i = 0;i < numberOfMatches && matchesFound[i] >= 0 && matchesFound[i] < recordedEventTimes.size();i++){
andrew@0 246 // printf("match times %i of %i::%f adding likelihood to %f\n", i, numberOfMatches, recordedEventTimes[matchesFound[i]], recordedEventTimes[matchesFound[i]] - bayesStruct.likelihood.offset);
andrew@0 247 //this is the vent time since start of file
andrew@0 248 if (recordedEventTimes[matchesFound[i]] - bayesStruct.likelihood.offset < bayesStruct.likelihood.arraySize){
andrew@1 249 // double confidenceMeasure = 0;
andrew@1 250 // if (totalConfidence > 0)
andrew@6 251 // confidenceMeasure = bayesStruct.posterior.getValueAtMillis(recordedEventTimes[matchesFound[i]])/totalConfidence;
andrew@2 252
andrew@2 253 bayesStruct.likelihood.addGaussianShape(recordedEventTimes[matchesFound[i]] - bayesStruct.likelihood.offset, likelihoodWidth, quantity);//* confidenceMeasure
andrew@0 254 }//end if
andrew@0 255 }
andrew@2 256 bayesStruct.likelihood.addConstant((1-likelihoodToNoiseRatio)/bayesStruct.likelihood.length);
andrew@0 257 }
andrew@0 258
andrew@0 259 int midiEventHolder::findMatch(const int& notePitch, const int& startTime, const int& endTime){
andrew@0 260
andrew@0 261 matchesFound.clear();
andrew@0 262 int startIndex = 0;
andrew@10 263
andrew@0 264 if (recordedEventTimes.size() > 0){
andrew@0 265
andrew@0 266 //get to the right range of events to check in
andrew@0 267 while (startIndex < recordedEventTimes.size() && recordedEventTimes[startIndex] < startTime)
andrew@0 268 startIndex++;
andrew@0 269
andrew@0 270 }
andrew@0 271
andrew@6 272 IntVector v;
andrew@6 273 DoubleVector d;
andrew@10 274 double tmpError = 100000.;//v high error
andrew@6 275
andrew@5 276 double minimumConfidence = 0;
andrew@0 277 while (startIndex < recordedEventTimes.size() && recordedEventTimes[startIndex] < endTime){
andrew@0 278 if (recordedNoteOnMatrix[startIndex][1] == notePitch){
andrew@6 279
andrew@0 280 matchesFound.push_back(startIndex);
andrew@6 281 v.push_back(startIndex);
andrew@5 282 double eventConfidence = bayesStruct.posterior.getValueAtMillis(recordedEventTimes[startIndex]);
andrew@5 283 if (eventConfidence > minimumConfidence){
andrew@5 284 minimumConfidence = eventConfidence;
andrew@5 285 bestMatchIndex = startIndex;
andrew@5 286 }
andrew@6 287 d.push_back(eventConfidence);
andrew@5 288
andrew@6 289 double confidence = eventConfidence;//bayesStruct.posterior.getValueAtMillis(mouseX);
andrew@1 290 // recordedEventTimes[startIndex]);
andrew@1 291 matchString += "["+ofToString(startIndex)+"] = "+ofToString(confidence, 3)+" .";
andrew@10 292
andrew@10 293 if (abs(recordedEventTimes[startIndex] - bayesStruct.bestEstimate) < tmpError){
andrew@10 294 //record the error between expected and observed times
andrew@10 295 tmpError = abs(recordedEventTimes[startIndex] - bayesStruct.bestEstimate);
andrew@10 296 minimumMatchError = recordedEventTimes[startIndex] - bayesStruct.bestEstimate;
andrew@10 297 }
andrew@10 298
andrew@0 299 }
andrew@0 300 startIndex++;
andrew@0 301 }
andrew@5 302
andrew@5 303
andrew@0 304 // printf("%i MATCHES TO Note %i found\n", (int)matchesFound.size(), notePitch);
andrew@0 305 int size = matchesFound.size();
andrew@5 306 if (size > 0)
andrew@5 307 noteOnMatches[bestMatchIndex] = true;
andrew@5 308
andrew@6 309 v.insert(v.begin() , (int)size);
andrew@6 310 d.insert(d.begin() , (double)size);
andrew@6 311
andrew@6 312 //v.push_back(size);
andrew@6 313 //d.push_back(size);
andrew@6 314 //for (int i = 0;i < matchesFound.size()+1;i++){
andrew@6 315 // v.push_back(matchesFound[i]);
andrew@6 316 // printf("match %i,[%i] is %i\n", startIndex, i, v[i]);
andrew@6 317 //}
andrew@6 318
andrew@0 319
andrew@0 320 matchMatrix.push_back(v);
andrew@6 321 matchConfidence.push_back(d);
andrew@0 322
andrew@0 323 return size;
andrew@0 324 }
andrew@0 325
andrew@0 326 bool midiEventHolder::checkIfMatchedNote(const int& tmpIndex){
andrew@0 327 for (int i = 0;i < matchesFound.size();i++){
andrew@0 328 if (matchesFound[i] == tmpIndex)
andrew@0 329 return true;
andrew@0 330 }
andrew@0 331 return false;
andrew@0 332 }
andrew@0 333
andrew@0 334
andrew@0 335
andrew@0 336 void midiEventHolder::findLocalTempoPairs(){
andrew@6 337
andrew@0 338 int currentPlayedIndex = playedNoteOnMatrix.size()-1;
andrew@6 339 // printf("played %i : %i, vel %i\n", currentPlayedIndex, playedNoteOnMatrix[currentPlayedIndex][0], playedNoteOnMatrix[currentPlayedIndex][1]);
andrew@6 340 // printMatchesFound();
andrew@6 341 // printMatchMatrix();
andrew@6 342 // printf("possible notes \n");
andrew@9 343 bool needToUpdate = false;
andrew@9 344 bayesStruct.setLikelihoodToConstant();
andrew@0 345
andrew@0 346
andrew@0 347 for (int i = 0;i < matchMatrix[currentPlayedIndex][0];i++){
andrew@6 348 //iterate through the recently matched events - even dodgy matches included
andrew@6 349 //size, index of match0, index of match1, ....
andrew@9 350
andrew@9 351
andrew@0 352 int recordedCurrentIndex = matchMatrix[currentPlayedIndex][i+1];
andrew@0 353
andrew@0 354 int previousIndex = currentPlayedIndex-1;
andrew@9 355
andrew@0 356
andrew@6 357 while (previousIndex >= 0 && playedEventTimes[previousIndex] + speedWindowWidthMillis > playedEventTimes[currentPlayedIndex]) {
andrew@0 358 double playedTimeDifference = playedEventTimes[currentPlayedIndex] - playedEventTimes[previousIndex];
andrew@6 359
andrew@0 360 for (int k = 0;k < matchMatrix[previousIndex][0];k++){
andrew@0 361 int recordedPreviousIndex = matchMatrix[previousIndex][k+1];
andrew@0 362
andrew@6 363
andrew@0 364 double recordedTimeDifference = recordedEventTimes[recordedCurrentIndex] - recordedEventTimes[recordedPreviousIndex];
andrew@6 365
andrew@0 366
andrew@0 367 //we want the speed of the recording relative to that of the playing live
andrew@0 368
andrew@0 369 double speedRatio = recordedTimeDifference / playedTimeDifference;
andrew@0 370 if (speedRatio <= maximumMatchSpeed && speedRatio >= minimumMatchSpeed){
andrew@6 371 /*
andrew@6 372 printf("(%i)", matchMatrix[currentPlayedIndex][i+1]);
andrew@6 373 printf("[%i] :: ", recordedPreviousIndex);
andrew@6 374 printf(" rec{%f} vs play(%f) ", recordedTimeDifference, playedTimeDifference);
andrew@6 375 printf("update on speed ratio %f\n", speedRatio);
andrew@6 376 */
andrew@1 377 // matchString += " speed: "+ofToString(speedRatio, 3);
andrew@6 378 // commented for debug
andrew@2 379
andrew@9 380 //bayesStruct.updateTempoDistribution(speedRatio, 0.1);//second paramter is confidence in the match
andrew@9 381 double amount = (1-bayesStruct.speedLikelihoodNoise)/10;
andrew@9 382 bayesStruct.updateTempoLikelihood(speedRatio, amount);
andrew@9 383 needToUpdate = true;
andrew@0 384 }
andrew@6 385 // printf("\n");
andrew@0 386 }
andrew@0 387
andrew@0 388 previousIndex--;
andrew@0 389 }//end while previousindex countdown
andrew@0 390 }//end for loop through possible current matches
andrew@0 391
andrew@9 392 if (needToUpdate)
andrew@9 393 bayesStruct.updateTempoDistribution();
andrew@9 394
andrew@2 395 //printf("current speed is %f\n", bayesStruct.relativeSpeedPosterior.getIndexInRealTerms(bayesStruct.relativeSpeedPosterior.MAPestimate));
andrew@0 396 }
andrew@0 397
andrew@0 398
andrew@6 399 void midiEventHolder::findLocalTempoPairsWeightedForConfidence(){
andrew@9 400 bool needToUpdate = false;
andrew@6 401
andrew@6 402 int currentPlayedIndex = playedNoteOnMatrix.size()-1;
andrew@6 403 // printf("played %i : %i, vel %i\n", currentPlayedIndex, playedNoteOnMatrix[currentPlayedIndex][0], playedNoteOnMatrix[currentPlayedIndex][1]);
andrew@6 404 // printMatchesFound();
andrew@6 405 // printMatchMatrix();
andrew@6 406 // printf("possible notes \n");
andrew@6 407
andrew@9 408 bayesStruct.setLikelihoodToConstant();
andrew@6 409
andrew@6 410 for (int i = 0;i < matchMatrix[currentPlayedIndex][0];i++){
andrew@6 411 //iterate through the recently matched events - even dodgy matches included
andrew@6 412 //size, index of match0, index of match1, ....
andrew@6 413 int recordedCurrentIndex = matchMatrix[currentPlayedIndex][i+1];
andrew@6 414
andrew@6 415 double currentMatchConfidence = matchConfidence[currentPlayedIndex][i+1];//new confidence
andrew@6 416
andrew@6 417 int previousIndex = currentPlayedIndex-1;
andrew@6 418
andrew@6 419 while (previousIndex >= 0 && playedEventTimes[previousIndex] + speedWindowWidthMillis > playedEventTimes[currentPlayedIndex]) {
andrew@6 420 double playedTimeDifference = playedEventTimes[currentPlayedIndex] - playedEventTimes[previousIndex];
andrew@6 421
andrew@6 422 for (int k = 0;k < matchMatrix[previousIndex][0];k++){
andrew@6 423 int recordedPreviousIndex = matchMatrix[previousIndex][k+1];
andrew@6 424
andrew@6 425 double previousMatchConfidence = matchConfidence[previousIndex][k+1];
andrew@6 426
andrew@6 427
andrew@6 428 double recordedTimeDifference = recordedEventTimes[recordedCurrentIndex] - recordedEventTimes[recordedPreviousIndex];
andrew@6 429
andrew@6 430
andrew@6 431 //we want the speed of the recording relative to that of the playing live
andrew@6 432
andrew@6 433 double speedRatio = recordedTimeDifference / playedTimeDifference;
andrew@6 434 if (speedRatio <= maximumMatchSpeed && speedRatio >= minimumMatchSpeed){
andrew@6 435
andrew@6 436 printf("(%i)", matchMatrix[currentPlayedIndex][i+1]);
andrew@6 437 printf("[%i] :: ", recordedPreviousIndex);
andrew@6 438 printf(" conf %f & %f ", currentMatchConfidence, previousMatchConfidence);
andrew@6 439 printf(" rec{%f} vs play(%f) ", recordedTimeDifference, playedTimeDifference);
andrew@6 440 printf("update on speed ratio %f\n", speedRatio);
andrew@6 441
andrew@6 442 // matchString += " speed: "+ofToString(speedRatio, 3);
andrew@6 443 // commented for debug
andrew@9 444 double weighting = previousMatchConfidence * currentMatchConfidence ;
andrew@9 445 double amount = (1-bayesStruct.speedLikelihoodNoise)*weighting/10;
andrew@9 446 bayesStruct.updateTempoLikelihood(speedRatio, amount);//second paramter is confidence in the match
andrew@9 447 needToUpdate = true;
andrew@6 448 }
andrew@6 449 // printf("\n");
andrew@6 450 }
andrew@6 451
andrew@6 452 previousIndex--;
andrew@6 453 }//end while previousindex countdown
andrew@6 454 }//end for loop through possible current matches
andrew@6 455
andrew@9 456 if (needToUpdate)
andrew@9 457 bayesStruct.updateTempoDistribution();
andrew@6 458 //printf("current speed is %f\n", bayesStruct.relativeSpeedPosterior.getIndexInRealTerms(bayesStruct.relativeSpeedPosterior.MAPestimate));
andrew@6 459 }
andrew@6 460
andrew@6 461
andrew@6 462
andrew@0 463 void midiEventHolder::updatePlayPosition(){
andrew@0 464
andrew@0 465 //in actual fact if we are changing the speed of the play position
andrew@0 466 //we will need to update this via the file
andrew@0 467
andrew@0 468 double timeDifference = ofGetElapsedTimeMillis() - lastPeriodUpdateTime;
andrew@0 469 //this is time diff in milliseconds
andrew@0 470 //then we have
andrew@0 471 double quarterNoteIntervals = (timeDifference / period);
andrew@0 472 tickLocation = quarterNoteIntervals * pulsesPerQuarternote;
andrew@0 473
andrew@0 474 playPositionInMillis = timeDifference;//based on updating from when we change period
andrew@0 475 //this to be added
andrew@0 476
andrew@0 477 bayesStruct.updateBestEstimate();
andrew@0 478
andrew@0 479 }
andrew@0 480
andrew@0 481
andrew@9 482 void midiEventHolder::drawMidiFile(){
andrew@9 483
andrew@0 484 //draws midi file on scrolling screen
andrew@0 485 int size = recordedNoteOnMatrix.size();
andrew@0 486 if (size > 0){
andrew@0 487
andrew@0 488 numberOfScreensIn = floor(bayesStruct.bestEstimate / getEventTimeMillis(ticksPerScreen));//rpounds down on no screens in
andrew@0 489
andrew@9 490 // numberOfScreensIn = tickLocation / ticksPerScreen;//rounds down
andrew@0 491 timeOffsetForScreen = getEventTimeMillis(numberOfScreensIn * ticksPerScreen);
andrew@0 492
andrew@11 493 while (noteArrayIndex < recordedNoteOnMatrix.size()-1 && tickLocation > recordedNoteOnMatrix[noteArrayIndex][0] )
andrew@0 494 noteArrayIndex++;
andrew@0 495
andrew@0 496
andrew@0 497 while (noteArrayIndex > 0 && noteArrayIndex < size && tickLocation < recordedNoteOnMatrix[noteArrayIndex][0])
andrew@0 498 noteArrayIndex--;
andrew@0 499
andrew@0 500 //need to start where we currently are in file
andrew@0 501 int maxNoteIndexToPrint = noteArrayIndex;
andrew@11 502 int minNoteIndexToPrint = min(size-1,noteArrayIndex);//not needed as changed above
andrew@0 503
andrew@0 504 while (maxNoteIndexToPrint < recordedNoteOnMatrix.size() && recordedNoteOnMatrix[maxNoteIndexToPrint][0] < (numberOfScreensIn+1)*ticksPerScreen )
andrew@0 505 maxNoteIndexToPrint++;
andrew@0 506
andrew@11 507 while (minNoteIndexToPrint > 0 && recordedNoteOnMatrix[minNoteIndexToPrint][0] > numberOfScreensIn*ticksPerScreen)//&& minNoteIndexToPrint < size
andrew@0 508 minNoteIndexToPrint--;
andrew@0 509
andrew@0 510 for (int tmpIndex = max(0,minNoteIndexToPrint);tmpIndex < min(maxNoteIndexToPrint, (int)recordedNoteOnMatrix.size());tmpIndex++){
andrew@0 511
andrew@0 512 if (checkIfMatchedNote(tmpIndex))
andrew@0 513 ofSetColor(0,0,255);
andrew@5 514 else if(noteOnMatches[tmpIndex]){
andrew@9 515 ofSetColor(255,0,255);
andrew@5 516 }else{
andrew@0 517 ofSetColor(255,255,255);
andrew@5 518 }
andrew@5 519
andrew@9 520
andrew@9 521
andrew@9 522 // XXX replace ofgetwidth below
andrew@0 523 //if (tmpIndex >= 0 && tmpIndex < size)
andrew@0 524 int xLocation = (float)(recordedNoteOnMatrix[tmpIndex][0] - numberOfScreensIn*ticksPerScreen)*(*screenWidth)/(float)ticksPerScreen;
andrew@0 525 int duration = (float)(recordedNoteOnMatrix[tmpIndex][3]*(*screenWidth))/(float)ticksPerScreen;
andrew@0 526
andrew@0 527
andrew@0 528 int yLocation = (*screenHeight) - ((recordedNoteOnMatrix[tmpIndex][1] - noteMinimum )*(*screenHeight)/ (float)(noteMaximum - noteMinimum));
andrew@0 529 ofRect(xLocation,yLocation, duration, noteHeight);
andrew@0 530
andrew@0 531 }
andrew@0 532
andrew@0 533
andrew@0 534 int xLocation;// = getLocationFromTicks(tickLocation);
andrew@9 535 // ofLine(xLocation, 0, xLocation, (*screenHeight));
andrew@0 536
andrew@0 537 //orange line at best estimate
andrew@0 538 xLocation = getLocationFromMillis(bayesStruct.bestEstimate);
andrew@0 539 ofSetColor(250,100,0);
andrew@0 540 ofLine(xLocation, 0, xLocation, (*screenHeight));
andrew@0 541
andrew@2 542 xLocation = getLocationFromMillis(bayesStruct.tmpBestEstimate);
andrew@2 543 ofSetColor(250,100,0);
andrew@2 544 ofLine(xLocation, 0, xLocation, (*screenHeight));
andrew@2 545
andrew@0 546
andrew@0 547 //lines where matching window start and end are
andrew@0 548 ofSetColor(0,100,255);
andrew@0 549 xLocation = getLocationFromMillis(windowStartTime);
andrew@0 550 ofLine(xLocation, 0, xLocation, (*screenHeight));
andrew@0 551 xLocation = getLocationFromMillis(windowStartTime+matchWindowWidth);
andrew@0 552 ofLine(xLocation, 0, xLocation, (*screenHeight));
andrew@9 553
andrew@0 554
andrew@11 555 int maxSize = recordedNoteOnMatrix[size-1][0];
andrew@11 556
andrew@11 557 string indexString = "num screens in "+ofToString(numberOfScreensIn)+"; min index to print "+ofToString(minNoteIndexToPrint)+", max index to print "+ofToString(maxNoteIndexToPrint);
andrew@11 558 indexString += " size "+ofToString(size)+" tick loc "+ofToString(tickLocation)+" max size "+ofToString(maxSize);
andrew@11 559 ofDrawBitmapString(indexString, 20, 40);
andrew@11 560
andrew@0 561 }
andrew@0 562
andrew@0 563 ofDrawBitmapString(ofToString(timeOffsetForScreen, 1), 20,20);
andrew@0 564
andrew@0 565 ofDrawBitmapString(timeString, 20, 60);
andrew@0 566
andrew@11 567
andrew@9 568 }
andrew@9 569
andrew@9 570 void midiEventHolder::drawFile(){
andrew@9 571 drawMidiFile();
andrew@9 572
andrew@9 573
andrew@0 574 // bayesStruct.drawArrays();
andrew@0 575
andrew@0 576 // ofSetColor(200,200,0);
andrew@0 577 // bayesStruct.prior.drawConstrainedVector(0, bayesStruct.prior.arraySize, 400, 800);
andrew@0 578
andrew@0 579 //need to draw arrays within correct timescope
andrew@9 580 if (drawPhaseMode)
andrew@0 581 bayesStruct.drawArraysRelativeToTimeframe(timeOffsetForScreen, timeOffsetForScreen + getEventTimeMillis(ticksPerScreen));
andrew@0 582
andrew@1 583 if (drawTempoMode)
andrew@1 584 bayesStruct.drawTempoArrays();
andrew@0 585
andrew@1 586
andrew@1 587 ofSetColor(0, 0, 0);
andrew@0 588 ofDrawBitmapString(matchString, 20, ofGetHeight() - 20);
andrew@0 589
andrew@1 590 double confidence = bayesStruct.posterior.getValueAtMillis(mouseX);
andrew@2 591 /*
andrew@2 592 string mouseString = "mouseX "+ofToString(confidence, 3)+" .";
andrew@1 593 ofDrawBitmapString(mouseString, 20 , ofGetHeight() - 40);
andrew@2 594 */
andrew@2 595 string mouseString = "updateCounter "+ofToString(bayesStruct.updateCounter);
andrew@2 596 ofDrawBitmapString(mouseString, 20 , ofGetHeight() - 40);
andrew@2 597
andrew@2 598 string infostring = "speed "+ofToString(bayesStruct.relativeSpeedPosterior.getIndexInRealTerms(bayesStruct.relativeSpeedPosterior.MAPestimate), 3);
andrew@2 599 ofDrawBitmapString(infostring, 20 , ofGetHeight() - 60);
andrew@0 600 }
andrew@0 601
andrew@0 602 int midiEventHolder::getLocationFromTicks(double tickPosition){
andrew@0 603 return (int)((float)(tickPosition - numberOfScreensIn*ticksPerScreen)*(*screenWidth)/(float)ticksPerScreen);
andrew@0 604 }
andrew@0 605
andrew@0 606 int midiEventHolder::getLocationFromMillis(double millisPosition){
andrew@0 607 //(getEventTimeTicks(windowStartTime+matchWindowWidth) - numberOfScreensIn*ticksPerScreen)*(*screenWidth) / (double)ticksPerScreen
andrew@0 608 return (millisPosition - timeOffsetForScreen)*(*screenWidth)/getEventTimeMillis(ticksPerScreen);
andrew@0 609 }
andrew@0 610
andrew@0 611
andrew@0 612 void midiEventHolder::exampleCrossUpdate(){
andrew@0 613
andrew@0 614 bayesStruct.crossUpdateArrays(bayesStruct.posterior, bayesStruct.relativeSpeedPosterior, 200);
andrew@0 615
andrew@0 616 }
andrew@0 617
andrew@0 618
andrew@0 619 void midiEventHolder::setStartPlayingTimes(){
andrew@0 620 lastPeriodUpdateTime = ofGetElapsedTimeMillis();
andrew@0 621 startTime = lastPeriodUpdateTime;
andrew@0 622
andrew@2 623 /*
andrew@2 624 bayesStruct.lastEventTime = 0;//ofGetElapsedTimeMillis();
andrew@2 625 bayesStruct.bestEstimate = 0;
andrew@0 626 bayesStruct.resetArrays();
andrew@2 627 bayesStruct.lastBestEstimateUpdateTime = ofGetElapsedTimeMillis();
andrew@2 628 */
andrew@2 629 bayesStruct.setStartPlaying();
andrew@0 630 matchString = "";
andrew@0 631 }
andrew@0 632
andrew@0 633
andrew@0 634 void midiEventHolder::printMatchMatrix(){
andrew@0 635 printf("match matrix:\n");
andrew@0 636 for (int i = 0;i < matchMatrix.size();i++){
andrew@0 637 for (int k = 0;k < matchMatrix[i].size();k++){
andrew@0 638 printf("%i , ", matchMatrix[i][k]);
andrew@0 639 }
andrew@0 640 printf("\n");
andrew@0 641 }
andrew@0 642
andrew@0 643
andrew@0 644 }