Mercurial > hg > multitrack-audio-matcher
diff src/AudioEventMatcher.cpp @ 6:746a5af43c02
windowed bayesian distributions - drawn within constrained portion of the screen
author | Andrew N Robertson <andrew.robertson@eecs.qmul.ac.uk> |
---|---|
date | Thu, 02 Feb 2012 17:52:08 +0000 |
parents | 5ef00d1dfe68 |
children | 33dedfe32893 |
line wrap: on
line diff
--- a/src/AudioEventMatcher.cpp Thu Feb 02 12:45:36 2012 +0000 +++ b/src/AudioEventMatcher.cpp Thu Feb 02 17:52:08 2012 +0000 @@ -41,27 +41,51 @@ } void AudioEventMatcher::draw(){ + //draw some outlines in blue ofSetColor(20,200,200); bayesPositionWindow.drawOutline(); bayesTempoWindow.drawOutline(); + //draw the scrolling audio tracks recordedTracks.drawTracks(); ofSetColor(255); // bayesianStruct.relativeSpeedPrior.drawVector(0, 200, bayesTempoWindow); + drawBayesianDistributions(); + // bayesianStruct.posterior.drawVector(0, bayesianStruct.posterior.getRealTermsAsIndex(screenWidthMillis), bayesPositionWindow); + + //bayesianStruct.posterior.drawVector(bayesianStruct.posterior.getRealTermsAsIndex(0), bayesianStruct.posterior.getRealTermsAsIndex(screenWidthMillis), bayesPositionWindow); + + // bayesianStruct.relativeSpeedPosterior.drawVector(0, bayesianStruct.relativeSpeedPosterior.getRealTermsAsIndex(2), bayesTempoWindow); + +} + +void AudioEventMatcher::drawBayesianDistributions(){ + + double screenWidthMillis = recordedTracks.loadedAudioFiles[0].fileLoader.onsetDetect.framesToMillis(recordedTracks.loadedAudioFiles[0].fileLoader.onsetDetect.amplitudeNumber); - bayesianStruct.posterior.drawVector(0, bayesianStruct.posterior.getRealTermsAsIndex(screenWidthMillis), bayesPositionWindow); + + double screenStartTimeMillis = recordedTracks.loadedAudioFiles[0].fileLoader.onsetDetect.framesToMillis(recordedTracks.loadedAudioFiles[0].fileLoader.onsetDetect.drawParams.windowStartFrame); + double screenEndTimeMillis = screenStartTimeMillis + screenWidthMillis; + int startIndex = bayesianStruct.posterior.getRealTermsAsIndex(screenStartTimeMillis); + int endIndex = bayesianStruct.posterior.getRealTermsAsIndex(screenEndTimeMillis); -// bayesianStruct.posterior.drawVector(bayesianStruct.posterior.getRealTermsAsIndex(0), bayesianStruct.posterior.getRealTermsAsIndex(screenWidthMillis), bayesPositionWindow); + bayesianStruct.posterior.drawConstrainedVector(startIndex, endIndex, 0, ofGetWidth(), bayesPositionWindow); + + string tmpString = "start "+ofToString(screenStartTimeMillis)+" (index "+ofToString(startIndex)+"), end "+ofToString(screenEndTimeMillis); + ofDrawBitmapString(tmpString, bayesPositionWindow.x+20, bayesPositionWindow.y+20); - bayesianStruct.likelihood.drawVector(bayesianStruct.likelihood.getRealTermsAsIndex(0), bayesianStruct.likelihood.getRealTermsAsIndex(screenWidthMillis), bayesLikelihoodWindow); - bayesianStruct.relativeSpeedPosterior.drawVector(0, bayesianStruct.relativeSpeedPosterior.getRealTermsAsIndex(2), bayesTempoWindow); + bayesianStruct.likelihood.drawConstrainedVector(startIndex, endIndex, 0, ofGetWidth(), bayesLikelihoodWindow); - + + //bayesianStruct.likelihood.drawVector(bayesianStruct.likelihood.getRealTermsAsIndex(0), bayesianStruct.likelihood.getRealTermsAsIndex(screenWidthMillis), bayesLikelihoodWindow); + + bayesianStruct.relativeSpeedPosterior.drawConstrainedVector(0, bayesianStruct.relativeSpeedPosterior.arraySize, 0, ofGetWidth(), bayesTempoWindow); + string tmpStr = "zero is "+ofToString(bayesianStruct.posterior.getRealTermsAsIndex(0)); tmpStr += " offsetis "+ofToString(bayesianStruct.posterior.offset); tmpStr += " screenWidth = "+ofToString(bayesianStruct.posterior.getRealTermsAsIndex(screenWidthMillis)); @@ -74,8 +98,7 @@ } - -void AudioEventMatcher::newPitchEvent(const double& pitchIn, const double& timeIn){ +void AudioEventMatcher::newPitchEvent(const int& channel, const double& pitchIn, const double& timeIn){ liveInput.addPitchEvent(pitchIn, timeIn); //tmp print stuff @@ -83,25 +106,32 @@ double tmp = bayesianStruct.posterior.getMAPestimate(); printf(" getting it %f and offset %f == %f ms\n", tmp, bayesianStruct.posterior.offset, bayesianStruct.posterior.getIndexInRealTerms(tmp)); - matchNewPitchEvent(0, pitchIn, timeIn); + matchNewPitchEvent(channel, pitchIn, timeIn); } -void AudioEventMatcher::newKickEvent(const double& timeIn){ -// liveInput.addKickEvent(time); +void AudioEventMatcher::newKickEvent(const double& timeIn){ +// liveInput.addKickEvent(timeIn); matchNewOnsetEvent(0, timeIn); } +void AudioEventMatcher::newKickEvent(const int& channel, const double& timeIn){ +// liveInput.addKickEvent(timeIn); + matchNewOnsetEvent(channel, timeIn); +} + void AudioEventMatcher::newSnareEvent(const double& timeIn){ - matchNewOnsetEvent(0, timeIn); + matchNewOnsetEvent(2, timeIn); } //Needs just to set bounds for the matching process, not have TimeIn void AudioEventMatcher::matchNewOnsetEvent(const int& channel, const double& timeIn){ + bayesianStruct.updateBayesianDistributions(timeIn);//moves the posterior up into prior given the time interval and calculates new offsets + //start at beginning but OPTIMISE later - double onsetLikelihoodToNoise = 0.5; + double onsetLikelihoodToNoise = 0.3; double likelihoodWidth = 40; @@ -121,7 +151,7 @@ if (millisTime >= startTime && millisTime <= endTime){ bayesianStruct.likelihood.addGaussianShapeFromRealTime(millisTime, likelihoodWidth, quantity); numberOfMatchesFound++; - printf("Adding Gaussian for onset at time %f offset %f\n", millisTime, bayesianStruct.likelihood.offset); + // printf("Adding Gaussian for onset at time %f offset %f\n", millisTime, bayesianStruct.likelihood.offset); } } @@ -133,6 +163,7 @@ bayesianStruct.likelihood.renormalise(); + } @@ -141,11 +172,11 @@ //start at beginning but OPTIMISE later - updateBayesianDistributions(timeIn); + bayesianStruct.updateBayesianDistributions(timeIn);//moves the posterior up into prior given the time interval and calculates new offsets ///set offsets // bayesianStruct.likelihood.offset = bayesianStruct.prior.offset; - double pitchLikelihoodToNoise = 0.5; + double pitchLikelihoodToNoise = 0.2;//more noise int numberOfMatches = 0; bayesianStruct.likelihood.zero();//set to zero @@ -154,7 +185,7 @@ for (int i = 0;i < recordedTracks.loadedAudioFiles[channel].fileLoader.onsetDetect.chromaOnsets.size();i++){ if (checkMatch(recordedTracks.loadedAudioFiles[channel].fileLoader.onsetDetect.chromaOnsets[i].aubioPitch, pitchIn)) { - quantity = getPitchDistance(recordedTracks.loadedAudioFiles[channel].fileLoader.onsetDetect.chromaOnsets[i].aubioPitch, pitchIn, 40); + quantity = getPitchDistance(recordedTracks.loadedAudioFiles[channel].fileLoader.onsetDetect.chromaOnsets[i].aubioPitch, pitchIn, 20); bayesianStruct.likelihood.addGaussianShapeFromRealTime(recordedTracks.loadedAudioFiles[channel].fileLoader.onsetDetect.chromaOnsets[i].millisTime, 30, quantity); recordedTracks.loadedAudioFiles[channel].fileLoader.onsetDetect.chromaOnsets[i].matched = true; numberOfMatches++; @@ -165,21 +196,16 @@ } } - - bayesianStruct.likelihood.addConstant(numberOfMatches*(1-pitchLikelihoodToNoise)/(pitchLikelihoodToNoise*bayesianStruct.likelihood.length)); - - recordedTracks.recentPitch = pitchIn; - + + if (numberOfMatches > 0){//no point updating unless there is a match + bayesianStruct.likelihood.addConstant(numberOfMatches*(1-pitchLikelihoodToNoise)/(pitchLikelihoodToNoise*bayesianStruct.likelihood.length)); //tmp set likelihood constant and calculate using that - bayesianStruct.likelihood.zero(); - bayesianStruct.likelihood.addConstant(1); + //bayesianStruct.likelihood.zero(); + //bayesianStruct.likelihood.addConstant(1); - bayesianStruct.calculatePosterior(); - - //tmp print stuff - printf("After CALC"); - printPostOffset(); + bayesianStruct.calculatePosterior(); + } } @@ -214,51 +240,3 @@ -void AudioEventMatcher::updateBayesianDistributions(const double& newEventTime){ - //MOVE INTO bayesianStruct?? XX - - //NEED TO CHECK HERE THAT THEY HAVE THE SAME OFFSETS - bayesianStruct.prior.copyFromDynamicVector(bayesianStruct.posterior);//try the otehr way - - //bayesianStruct.copyPriorToPosterior(); - //need to get new MAP position and set the offset of the arrays - //currently bestEstimate is the approx for the new MAP position - int tmpMap = bayesianStruct.posterior.getMAPestimate(); - - double timeDifference = newEventTime - bayesianStruct.lastEventTime; - printf("updating distributions at time %f diff %f offset %f tmpmap est %i\n", newEventTime, timeDifference, bayesianStruct.posterior.offset, tmpMap); - - //addnoise to the tempo distribution - //bayesianStruct.decaySpeedDistribution(timeDifference); - - if (timeDifference > 50){ - bayesianStruct.addGaussianNoiseToSpeedPosterior(timeDifference * 10.0 / 100.); - } - - bayesianStruct.updateBestEstimate(timeDifference); - bayesianStruct.lastBestEstimateUpdateTime = newEventTime;//getTimeNow(timePlayed); - -// printf("set new distrb max 0 or %f\n", bayesianStruct.bestEstimate - (bayesianStruct.prior.scalar*bayesianStruct.prior.arraySize/2)); - - //tmp print stuff - - printf("HALFWAY BEST "); - printPostOffset(); - - bayesianStruct.setNewDistributionOffsets(max(0., bayesianStruct.bestEstimate - (bayesianStruct.prior.scalar*bayesianStruct.prior.arraySize/2))); - bayesianStruct.crossUpdateArrays(bayesianStruct.posterior, bayesianStruct.relativeSpeedPosterior, timeDifference); - - //i.e. using the same offset as prior - bayesianStruct.posterior.offset = bayesianStruct.prior.offset;// - -// float tmpPrior = max(0., bayesianStruct.bestEstimate - (bayesianStruct.prior.scalar*bayesianStruct.prior.arraySize/2));// bayesianStruct.prior.offset = max(0., bayesianStruct.bestEstimate - (bayesianStruct.prior.scalar*bayesianStruct.prior.arraySize/2)); -// printf("Using prior offset of %f not %f\n", tmpPrior, bayesianStruct.prior.offset); - - bayesianStruct.lastEventTime = newEventTime;//bayesianStruct.lastEventTime = ofGetElapsedTimeMillis(); - -} - -void AudioEventMatcher::printPostOffset(){ - double tmp = bayesianStruct.posterior.getMAPestimate(); - printf(" MAP index %i post offset %f == %f ms\n", bayesianStruct.posterior.MAPestimate, bayesianStruct.posterior.offset, bayesianStruct.posterior.getIndexInRealTerms(bayesianStruct.posterior.MAPestimate)); -} \ No newline at end of file