comparison src/midiEventHolder.cpp @ 24:5a11b19906c7

hackday code is added.
author Andrew N Robertson <andrew.robertson@eecs.qmul.ac.uk>
date Sat, 03 Dec 2011 17:19:43 +0000
parents 9860abc92a30
children
comparison
equal deleted inserted replaced
23:032edf186a68 24:5a11b19906c7
128 double midiEventHolder::getEventTimeMillis(double ticks){ 128 double midiEventHolder::getEventTimeMillis(double ticks){
129 return (period * ticks / (double) pulsesPerQuarternote); 129 return (period * ticks / (double) pulsesPerQuarternote);
130 } 130 }
131 131
132 void midiEventHolder::newNoteOnEvent(int pitch, int velocity, double timePlayed){ 132 void midiEventHolder::newNoteOnEvent(int pitch, int velocity, double timePlayed){
133 tempoSpeedString = ""; 133 // tempoSpeedString = "";
134 134
135 //MOVE INTO BAYESSTRUCT?? XXX 135 //MOVE INTO BAYESSTRUCT?? XXX
136 //bayesStruct.copyPriorToPosterior(); 136 //bayesStruct.copyPriorToPosterior();
137 //why was this here?? 137 //why was this here??
138 bayesStruct.prior.copyFromDynamicVector(bayesStruct.posterior);//try the otehr way 138 bayesStruct.prior.copyFromDynamicVector(bayesStruct.posterior);//try the otehr way
160 // double timeDifference = ofGetElapsedTimeMillis() - bayesStruct.lastEventTime; 160 // double timeDifference = ofGetElapsedTimeMillis() - bayesStruct.lastEventTime;
161 double timeDifference = timePlayed - bayesStruct.lastEventTime; 161 double timeDifference = timePlayed - bayesStruct.lastEventTime;
162 162
163 163
164 164
165 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); 165 //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);
166
166 //addnoise to the tempo distribution 167 //addnoise to the tempo distribution
167 //bayesStruct.decaySpeedDistribution(timeDifference); 168 //bayesStruct.decaySpeedDistribution(timeDifference);
168 if (timeDifference > 50){ 169 if (timeDifference > 50){
169 bayesStruct.addGaussianNoiseToSpeedPosterior(timeDifference * 10 / 100.); 170 bayesStruct.addGaussianNoiseToSpeedPosterior(timeDifference * 10 / 100.);
170 // bayesStruct.addTriangularNoiseToSpeedPosterior(timeDifference * 10 / 100.); 171 // bayesStruct.addTriangularNoiseToSpeedPosterior(timeDifference * 10 / 100.);
246 //here we find the matches to the new note within appropriate range 247 //here we find the matches to the new note within appropriate range
247 248
248 matchString = ""; 249 matchString = "";
249 250
250 windowStartTime = max(0.0,(bayesStruct.bestEstimate - matchWindowWidth/2));//was playPositionInMillis 251 windowStartTime = max(0.0,(bayesStruct.bestEstimate - matchWindowWidth/2));//was playPositionInMillis
251 cout << "best estimate is " << bayesStruct.bestEstimate << endl; 252 // cout << "best estimate is " << bayesStruct.bestEstimate << endl;
252 int numberOfMatches = findMatch(notePitch, windowStartTime, windowStartTime + matchWindowWidth); 253 int numberOfMatches = findMatch(notePitch, windowStartTime, windowStartTime + matchWindowWidth);
253 254
254 255
255 matchString += " pitch: "+ofToString(notePitch)+" matches "+ofToString(numberOfMatches)+" win start "+ofToString(windowStartTime); 256 //matchString += " pitch: "+ofToString(notePitch)+" matches "+ofToString(numberOfMatches)+" win start "+ofToString(windowStartTime);
256 257
257 return numberOfMatches; 258 return numberOfMatches;
258 259
259 260
260 } 261 }
313 } 314 }
314 d.push_back(eventConfidence); 315 d.push_back(eventConfidence);
315 316
316 double confidence = eventConfidence;//bayesStruct.posterior.getValueAtMillis(mouseX); 317 double confidence = eventConfidence;//bayesStruct.posterior.getValueAtMillis(mouseX);
317 // recordedEventTimes[startIndex]); 318 // recordedEventTimes[startIndex]);
318 matchString += "["+ofToString(startIndex)+"] = "+ofToString(confidence, 3)+" ."; 319 // matchString += "["+ofToString(startIndex)+"] = "+ofToString(confidence, 3)+" .";
319 320
320 if (abs(recordedEventTimes[startIndex] - bayesStruct.bestEstimate) < tmpError){ 321 if (abs(recordedEventTimes[startIndex] - bayesStruct.bestEstimate) < tmpError){
321 //record the error between expected and observed times 322 //record the error between expected and observed times
322 tmpError = abs(recordedEventTimes[startIndex] - bayesStruct.bestEstimate); 323 tmpError = abs(recordedEventTimes[startIndex] - bayesStruct.bestEstimate);
323 minimumMatchError = tmpError;//recordedEventTimes[startIndex] - bayesStruct.bestEstimate; 324 minimumMatchError = tmpError;//recordedEventTimes[startIndex] - bayesStruct.bestEstimate;
348 matchConfidence.push_back(d); 349 matchConfidence.push_back(d);
349 350
350 //bringing in way to list only the best matches and use these in tempo process 351 //bringing in way to list only the best matches and use these in tempo process
351 bestMatchFound.push_back(bestMatchIndex); 352 bestMatchFound.push_back(bestMatchIndex);
352 353
353 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); 354 // 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);
354 355
355 return size; 356 return size;
356 } 357 }
357 358
358 bool midiEventHolder::checkIfMatchedNote(const int& tmpIndex){ 359 bool midiEventHolder::checkIfMatchedNote(const int& tmpIndex){
420 421
421 //bayesStruct.updateTempoDistribution(speedRatio, 0.1);//second paramter is confidence in the match 422 //bayesStruct.updateTempoDistribution(speedRatio, 0.1);//second paramter is confidence in the match
422 double amount = (1-bayesStruct.speedLikelihoodNoise)/10; 423 double amount = (1-bayesStruct.speedLikelihoodNoise)/10;
423 amount *= priorWeighting; 424 amount *= priorWeighting;
424 bayesStruct.updateTempoLikelihood(speedRatio, amount); 425 bayesStruct.updateTempoLikelihood(speedRatio, amount);
425 tempoSpeedString += ofToString(recordedPreviousIndex) + " "+ ofToString(speedRatio, 2) + " "+ofToString(amount, 2) += " \n"; 426 // tempoSpeedString += ofToString(recordedPreviousIndex) + " "+ ofToString(speedRatio, 2) + " "+ofToString(amount, 2) += " \n";
426 needToUpdate = true; 427 needToUpdate = true;
427 } 428 }
428 // printf("\n"); 429 // printf("\n");
429 } 430 }
430 431
439 } 440 }
440 441
441 442
442 void midiEventHolder::findLocalTempoPairsWeightedForConfidence(){ 443 void midiEventHolder::findLocalTempoPairsWeightedForConfidence(){
443 bool needToUpdate = false; 444 bool needToUpdate = false;
445
446 DoubleVector speedIntervalsFound;
444 447
445 //adapted this to just use the best match for each note 448 //adapted this to just use the best match for each note
446 449
447 int currentPlayedIndex = playedNoteOnMatrix.size()-1; 450 int currentPlayedIndex = playedNoteOnMatrix.size()-1;
448 // printf("played %i : %i, vel %i\n", currentPlayedIndex, playedNoteOnMatrix[currentPlayedIndex][0], playedNoteOnMatrix[currentPlayedIndex][1]); 451 // printf("played %i : %i, vel %i\n", currentPlayedIndex, playedNoteOnMatrix[currentPlayedIndex][0], playedNoteOnMatrix[currentPlayedIndex][1]);
455 int recordedCurrentIndex = bestMatchFound[currentPlayedIndex]; 458 int recordedCurrentIndex = bestMatchFound[currentPlayedIndex];
456 //we only look at intervals between the current best match and other recent best matched notes 459 //we only look at intervals between the current best match and other recent best matched notes
457 //that is the difference in confidence method 460 //that is the difference in confidence method
458 461
459 462
460 printf("BEST MATCH FOUND for index %i is %i ", currentPlayedIndex, bestMatchFound[currentPlayedIndex]); 463 //printf("BEST MATCH FOUND for index %i is %i ", currentPlayedIndex, bestMatchFound[currentPlayedIndex]);
461 464
462 int previousIndex = currentPlayedIndex-1; 465 int previousIndex = currentPlayedIndex-1;
463 466
464 //withing speedwindow i.e. 4 seconds 467 //withing speedwindow i.e. 4 seconds
465 while (previousIndex >= 0 && playedEventTimes[previousIndex] + speedWindowWidthMillis > playedEventTimes[currentPlayedIndex]) { 468 while (previousIndex >= 0 && playedEventTimes[previousIndex] + speedWindowWidthMillis > playedEventTimes[currentPlayedIndex]) {
474 477
475 double speedRatio = recordedTimeDifference / playedTimeDifference; 478 double speedRatio = recordedTimeDifference / playedTimeDifference;
476 if (recordedTimeDifference > minimumTimeIntervalForTempoUpdate 479 if (recordedTimeDifference > minimumTimeIntervalForTempoUpdate
477 && speedRatio < maximumMatchSpeed && speedRatio > minimumMatchSpeed){ 480 && speedRatio < maximumMatchSpeed && speedRatio > minimumMatchSpeed){
478 481
479 printf("(%i)", previousIndex); 482 /* printf("(%i)", previousIndex);
480 printf("[%i] :: ", recordedPreviousIndex); 483 printf("[%i] :: ", recordedPreviousIndex);
481 // printf(" conf %f & %f ", currentMatchConfidence, previousMatchConfidence); 484 // printf(" conf %f & %f ", currentMatchConfidence, previousMatchConfidence);
482 printf(" rec{%f} vs play(%f) ", recordedTimeDifference, playedTimeDifference); 485 printf(" rec{%f} vs play(%f) ", recordedTimeDifference, playedTimeDifference);
483 printf("update on speed ratio %f\n", speedRatio); 486 printf("update on speed ratio %f\n", speedRatio);
484 487 */
485 // matchString += " speed: "+ofToString(speedRatio, 3); 488 // matchString += " speed: "+ofToString(speedRatio, 3);
486 // commented for debug 489 // commented for debug
487 490
488 491
489 double priorWeighting = 1; 492 double priorWeighting = 1;
492 priorWeighting = sin(speedRatio * PI/2);//adding in a prior that prefers 1.0 speed 495 priorWeighting = sin(speedRatio * PI/2);//adding in a prior that prefers 1.0 speed
493 496
494 497
495 // double weighting = previousMatchConfidence * currentMatchConfidence ; 498 // double weighting = previousMatchConfidence * currentMatchConfidence ;
496 double amount = (1-bayesStruct.speedLikelihoodNoise)*priorWeighting/16;//was 9 499 double amount = (1-bayesStruct.speedLikelihoodNoise)*priorWeighting/16;//was 9
497 bayesStruct.updateTempoLikelihood(speedRatio, amount);//second paramter is confidence in the match 500
498 501 speedIntervalsFound.push_back(speedRatio);
499 tempoSpeedString += ofToString(recordedCurrentIndex) + " :: " + ofToString(recordedPreviousIndex); 502 // bayesStruct.updateTempoLikelihood(speedRatio, amount);//second paramter is confidence in the match
500 tempoSpeedString += " " + ofToString(recordedTimeDifference)+ " " + ofToString(speedRatio, 2) + " "+ofToString(amount, 2) += " \n"; 503
504 // tempoSpeedString += ofToString(recordedCurrentIndex) + " :: " + ofToString(recordedPreviousIndex);
505 // tempoSpeedString += " " + ofToString(recordedTimeDifference)+ " " + ofToString(speedRatio, 2) + " "+ofToString(amount, 2) += " \n";
501 506
502 needToUpdate = true; 507 needToUpdate = true;
503 } 508 }
504 // printf("\n"); 509 // printf("\n");
505 510
506 511
507 previousIndex--; 512 previousIndex--;
508 }//end while previousindex countdown 513 }//end while previousindex countdown
514
515 if (speedIntervalsFound.size() > 0){
516 double amount = (1 - bayesStruct.speedLikelihoodNoise) / speedIntervalsFound.size();
517 for (int i = 0;i < speedIntervalsFound.size();i++)
518 bayesStruct.updateTempoLikelihood(speedIntervalsFound[i], amount);
519 }
520
509 521
510 if (needToUpdate) 522 if (needToUpdate)
511 bayesStruct.updateTempoDistribution(); 523 bayesStruct.updateTempoDistribution();
512 //printf("current speed is %f\n", bayesStruct.relativeSpeedPosterior.getIndexInRealTerms(bayesStruct.relativeSpeedPosterior.MAPestimate)); 524 //printf("current speed is %f\n", bayesStruct.relativeSpeedPosterior.getIndexInRealTerms(bayesStruct.relativeSpeedPosterior.MAPestimate));
513 } 525 }
692 ofLine(xLocation, 0, xLocation, (*screenHeight)); 704 ofLine(xLocation, 0, xLocation, (*screenHeight));
693 705
694 706
695 int maxSize = recordedNoteOnMatrix[size-1][0]; 707 int maxSize = recordedNoteOnMatrix[size-1][0];
696 708
697 ofDrawBitmapString(tempoSpeedString, 20, 20); 709 // ofDrawBitmapString(tempoSpeedString, 20, 20);
698 /* string indexString = "num screens in "+ofToString(numberOfScreensIn)+"; min index to print "+ofToString(minNoteIndexToPrint)+", max index to print "+ofToString(maxNoteIndexToPrint); 710 /* string indexString = "num screens in "+ofToString(numberOfScreensIn)+"; min index to print "+ofToString(minNoteIndexToPrint)+", max index to print "+ofToString(maxNoteIndexToPrint);
699 indexString += " size "+ofToString(size)+" tick loc "+ofToString(tickLocation)+" max size "+ofToString(maxSize); 711 indexString += " size "+ofToString(size)+" tick loc "+ofToString(tickLocation)+" max size "+ofToString(maxSize);
700 ofDrawBitmapString(indexString, 20, 40); 712 ofDrawBitmapString(indexString, 20, 40);
701 */ 713 */
702 } 714 }
804 816
805 void midiEventHolder::reorderMatrixFromNoteTimes(IntMatrix& noteOnMatrix){ 817 void midiEventHolder::reorderMatrixFromNoteTimes(IntMatrix& noteOnMatrix){
806 double currentTime = -19999.; 818 double currentTime = -19999.;
807 for (int i = 0;i < noteOnMatrix.size();i++){ 819 for (int i = 0;i < noteOnMatrix.size();i++){
808 int nextIndex = getIndexOfMinimumAboveTime(currentTime, noteOnMatrix); 820 int nextIndex = getIndexOfMinimumAboveTime(currentTime, noteOnMatrix);
809 cout << "index of min time " << currentTime << " is " << nextIndex << " at time " << noteOnMatrix[nextIndex][0] << endl; 821 // cout << "index of min time " << currentTime << " is " << nextIndex << " at time " << noteOnMatrix[nextIndex][0] << endl;
810 // cout << "next index " << nextIndex << " time " << noteOnMatrix[nextIndex][0] << endl; 822
811 if (nextIndex >= 0 && nextIndex > i && noteOnMatrix[nextIndex][0] < noteOnMatrix[i][0] ){ 823 if (nextIndex >= 0 && nextIndex > i && noteOnMatrix[nextIndex][0] < noteOnMatrix[i][0] ){
812 //which it should be 824 //which it should be
813 cout << " index " << nextIndex << " at time " << noteOnMatrix[nextIndex][0] << " swaps with inex " << i << " at time " << noteOnMatrix[i][0] << endl; 825 // cout << " index " << nextIndex << " at time " << noteOnMatrix[nextIndex][0] << " swaps with inex " << i << " at time " << noteOnMatrix[i][0] << endl;
814 noteOnMatrix[i].swap(noteOnMatrix[nextIndex]); 826 noteOnMatrix[i].swap(noteOnMatrix[nextIndex]);
815 currentTime = noteOnMatrix[i][0]; 827 currentTime = noteOnMatrix[i][0];
816 } 828 }
817 829
818 } 830 }