annotate jnmr/midiEventHolder.cpp @ 48:803edc47e825

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