annotate jnmr/midiEventHolder.cpp @ 50:158f5f38e9d3

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