Mercurial > hg > midi-score-follower
changeset 6:6f5836d432ca
added confidence measure to each played event and associated match, but not really working yet
author | Andrew N Robertson <andrew.robertson@eecs.qmul.ac.uk> |
---|---|
date | Fri, 19 Aug 2011 19:42:01 +0100 |
parents | 195907bb8bb7 |
children | 405a4a009e1a |
files | src/midiEventHolder.cpp src/midiEventHolder.h |
diffstat | 2 files changed, 110 insertions(+), 28 deletions(-) [+] |
line wrap: on
line diff
--- a/src/midiEventHolder.cpp Fri Aug 19 16:38:30 2011 +0100 +++ b/src/midiEventHolder.cpp Fri Aug 19 19:42:01 2011 +0100 @@ -47,9 +47,13 @@ bayesStruct.relativeSpeedPrior.getMaximum(); //bayesStruct.simpleExample(); + + speedWindowWidthMillis = 4000; speedPriorValue = 1.0; noteHeight = (*screenHeight) / (float)(noteMaximum - noteMinimum); + confidenceWeightingUsed = false; + 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)]); } @@ -201,7 +205,10 @@ bayesStruct.calculatePosterior(); //having found matches we have matches for new note and matches for previous notes + if (!confidenceWeightingUsed) findLocalTempoPairs(); + else + findLocalTempoPairsWeightedForConfidence(); //bayesStruct.addGaussianNoiseToSpeedPosterior(10); @@ -238,7 +245,7 @@ if (recordedEventTimes[matchesFound[i]] - bayesStruct.likelihood.offset < bayesStruct.likelihood.arraySize){ // double confidenceMeasure = 0; // if (totalConfidence > 0) - // confidenceMeasure = bayesStruct.posterior.getValueAtMillis(recordedEventTimes[matchesFound[i]])/totalConfidence; + // confidenceMeasure = bayesStruct.posterior.getValueAtMillis(recordedEventTimes[matchesFound[i]])/totalConfidence; bayesStruct.likelihood.addGaussianShape(recordedEventTimes[matchesFound[i]] - bayesStruct.likelihood.offset, likelihoodWidth, quantity);//* confidenceMeasure }//end if @@ -259,17 +266,23 @@ } + IntVector v; + DoubleVector d; + double minimumConfidence = 0; while (startIndex < recordedEventTimes.size() && recordedEventTimes[startIndex] < endTime){ if (recordedNoteOnMatrix[startIndex][1] == notePitch){ + matchesFound.push_back(startIndex); + v.push_back(startIndex); double eventConfidence = bayesStruct.posterior.getValueAtMillis(recordedEventTimes[startIndex]); if (eventConfidence > minimumConfidence){ minimumConfidence = eventConfidence; bestMatchIndex = startIndex; } + d.push_back(eventConfidence); - double confidence = bayesStruct.posterior.getValueAtMillis(mouseX); + double confidence = eventConfidence;//bayesStruct.posterior.getValueAtMillis(mouseX); // recordedEventTimes[startIndex]); matchString += "["+ofToString(startIndex)+"] = "+ofToString(confidence, 3)+" ."; } @@ -282,12 +295,19 @@ if (size > 0) noteOnMatches[bestMatchIndex] = true; - IntVector v; - v.push_back(size); - for (int i = 0;i < matchesFound.size();i++) - v.push_back(matchesFound[i]); + v.insert(v.begin() , (int)size); + d.insert(d.begin() , (double)size); + + //v.push_back(size); + //d.push_back(size); + //for (int i = 0;i < matchesFound.size()+1;i++){ + // v.push_back(matchesFound[i]); + // printf("match %i,[%i] is %i\n", startIndex, i, v[i]); + //} + matchMatrix.push_back(v); + matchConfidence.push_back(d); return size; } @@ -303,48 +323,48 @@ void midiEventHolder::findLocalTempoPairs(){ - + int currentPlayedIndex = playedNoteOnMatrix.size()-1; -// printf("played %i : %i, vel %i\n", currentPlayedIndex, playedNoteOnMatrix[currentPlayedIndex][0], playedNoteOnMatrix[currentPlayedIndex][1]); -// printMatchesFound(); -// printMatchMatrix(); -// printf("possible notes \n"); + // printf("played %i : %i, vel %i\n", currentPlayedIndex, playedNoteOnMatrix[currentPlayedIndex][0], playedNoteOnMatrix[currentPlayedIndex][1]); + // printMatchesFound(); + // printMatchMatrix(); + // printf("possible notes \n"); for (int i = 0;i < matchMatrix[currentPlayedIndex][0];i++){ - + //iterate through the recently matched events - even dodgy matches included + //size, index of match0, index of match1, .... int recordedCurrentIndex = matchMatrix[currentPlayedIndex][i+1]; int previousIndex = currentPlayedIndex-1; - while (previousIndex >= 0 && playedEventTimes[previousIndex] + 2000 > playedEventTimes[currentPlayedIndex]) { + while (previousIndex >= 0 && playedEventTimes[previousIndex] + speedWindowWidthMillis > playedEventTimes[currentPlayedIndex]) { double playedTimeDifference = playedEventTimes[currentPlayedIndex] - playedEventTimes[previousIndex]; - + for (int k = 0;k < matchMatrix[previousIndex][0];k++){ int recordedPreviousIndex = matchMatrix[previousIndex][k+1]; - - + double recordedTimeDifference = recordedEventTimes[recordedCurrentIndex] - recordedEventTimes[recordedPreviousIndex]; - + //we want the speed of the recording relative to that of the playing live double speedRatio = recordedTimeDifference / playedTimeDifference; if (speedRatio <= maximumMatchSpeed && speedRatio >= minimumMatchSpeed){ - /* - printf("(%i)", matchMatrix[currentPlayedIndex][i+1]); - printf("[%i] :: ", recordedPreviousIndex); - printf(" rec{%f} vs play(%f) ", recordedTimeDifference, playedTimeDifference); - printf("update on speed ratio %f\n", speedRatio); - */ + /* + printf("(%i)", matchMatrix[currentPlayedIndex][i+1]); + printf("[%i] :: ", recordedPreviousIndex); + printf(" rec{%f} vs play(%f) ", recordedTimeDifference, playedTimeDifference); + printf("update on speed ratio %f\n", speedRatio); + */ // matchString += " speed: "+ofToString(speedRatio, 3); - // commented for debug + // commented for debug - bayesStruct.updateTempoDistribution(speedRatio, 0.1);//second paramter is confidence in the match - + bayesStruct.updateTempoDistribution(speedRatio, 0.1);//second paramter is confidence in the match + } - // printf("\n"); + // printf("\n"); } previousIndex--; @@ -355,6 +375,65 @@ } +void midiEventHolder::findLocalTempoPairsWeightedForConfidence(){ + + int currentPlayedIndex = playedNoteOnMatrix.size()-1; + // printf("played %i : %i, vel %i\n", currentPlayedIndex, playedNoteOnMatrix[currentPlayedIndex][0], playedNoteOnMatrix[currentPlayedIndex][1]); + // printMatchesFound(); + // printMatchMatrix(); + // printf("possible notes \n"); + + + for (int i = 0;i < matchMatrix[currentPlayedIndex][0];i++){ + //iterate through the recently matched events - even dodgy matches included + //size, index of match0, index of match1, .... + int recordedCurrentIndex = matchMatrix[currentPlayedIndex][i+1]; + + double currentMatchConfidence = matchConfidence[currentPlayedIndex][i+1];//new confidence + + int previousIndex = currentPlayedIndex-1; + + while (previousIndex >= 0 && playedEventTimes[previousIndex] + speedWindowWidthMillis > playedEventTimes[currentPlayedIndex]) { + double playedTimeDifference = playedEventTimes[currentPlayedIndex] - playedEventTimes[previousIndex]; + + for (int k = 0;k < matchMatrix[previousIndex][0];k++){ + int recordedPreviousIndex = matchMatrix[previousIndex][k+1]; + + double previousMatchConfidence = matchConfidence[previousIndex][k+1]; + + + double recordedTimeDifference = recordedEventTimes[recordedCurrentIndex] - recordedEventTimes[recordedPreviousIndex]; + + + //we want the speed of the recording relative to that of the playing live + + double speedRatio = recordedTimeDifference / playedTimeDifference; + if (speedRatio <= maximumMatchSpeed && speedRatio >= minimumMatchSpeed){ + + printf("(%i)", matchMatrix[currentPlayedIndex][i+1]); + printf("[%i] :: ", recordedPreviousIndex); + printf(" conf %f & %f ", currentMatchConfidence, previousMatchConfidence); + printf(" rec{%f} vs play(%f) ", recordedTimeDifference, playedTimeDifference); + printf("update on speed ratio %f\n", speedRatio); + + // matchString += " speed: "+ofToString(speedRatio, 3); + // commented for debug + double weighting = previousMatchConfidence * currentMatchConfidence * 0.1; + bayesStruct.updateTempoDistribution(speedRatio, weighting);//second paramter is confidence in the match + + } + // printf("\n"); + } + + previousIndex--; + }//end while previousindex countdown + }//end for loop through possible current matches + + //printf("current speed is %f\n", bayesStruct.relativeSpeedPosterior.getIndexInRealTerms(bayesStruct.relativeSpeedPosterior.MAPestimate)); +} + + + void midiEventHolder::updatePlayPosition(){ //in actual fact if we are changing the speed of the play position
--- a/src/midiEventHolder.h Fri Aug 19 16:38:30 2011 +0100 +++ b/src/midiEventHolder.h Fri Aug 19 19:42:01 2011 +0100 @@ -68,7 +68,7 @@ void findLocalTempoPairs(); - + void findLocalTempoPairsWeightedForConfidence(); double likelihoodWidth; double likelihoodToNoiseRatio; @@ -118,6 +118,9 @@ int bestMatchIndex; string timeString; double startTime; + int speedWindowWidthMillis; + + bool confidenceWeightingUsed; }; #endif \ No newline at end of file