annotate src/AudioEventMatcher.cpp @ 42:6a7982661703

checked pitch tracking and added reset to onset detection
author Andrew N Robertson <andrew.robertson@eecs.qmul.ac.uk>
date Wed, 25 Apr 2012 23:25:10 +0100
parents f5de07b4d733
children d23685b9e766
rev   line source
andrew@0 1 /*
andrew@0 2 * AudioEventMatcher.cpp
andrew@0 3 * MultipleAudioMathcher
andrew@0 4 *
andrew@0 5 * Created by Andrew on 31/01/2012.
andrew@0 6 * Copyright 2012 QMUL. All rights reserved.
andrew@0 7 *
andrew@0 8 */
andrew@0 9
andrew@0 10 #include "AudioEventMatcher.h"
andrew@0 11
andrew@0 12
andrew@39 13 const int matchWindowWidth = 8000;//ms in which to match
andrew@39 14
andrew@32 15 const float pitchCutOff = 16;//within which pitches are even considered
andrew@0 16
andrew@0 17 AudioEventMatcher::AudioEventMatcher(){
andrew@7 18
andrew@35 19 useChromaDotProduct = false;
andrew@15 20
andrew@37 21 printingData = false;
andrew@37 22
andrew@23 23 pitchLikelihoodToNoise = 0.6;//more noise
andrew@32 24 chromaLikelihoodToNoise = 0.5;//lower => more noise, higher more weight for events
andrew@32 25 chromaLikelihoodWidth = 50;//ms round onset event
andrew@16 26
andrew@36 27 onsetLikelihoodToNoise = 0.1;
andrew@17 28 onsetLikelihoodWidth = 10;//in ms
andrew@15 29
andrew@0 30 setArraySizes();
andrew@3 31
andrew@3 32 usingRealTime = false;
andrew@3 33 bayesianStruct.realTimeMode = &usingRealTime;
andrew@7 34 recentPitch = 0;
andrew@8 35 currentAlignmentPosition = 0;
andrew@14 36
andrew@9 37 followingLiveInput = true;
andrew@15 38 startedPlaying = false;
andrew@20 39 recordedTempoIndex = 0;
andrew@39 40
andrew@42 41 bayesianStruct.startingWindowWidth = 100;//matchWindowWidth / 8;
andrew@42 42 bayesianStruct.matchWindowWidth = matchWindowWidth;
andrew@20 43 // temporal.setUpEventTimeMatrix();
andrew@20 44 // recordedTempoData.setUpEventTimeMatrix();
andrew@0 45 }
andrew@0 46
andrew@14 47
andrew@19 48
andrew@19 49
andrew@7 50 void AudioEventMatcher::setWindowDimensions(){
andrew@7 51 double startHeight = recordedTracks.numberOfAudioTracks * recordedTracks.trackScreenHeight;
andrew@7 52 double heightAvailable = 1 - startHeight;
andrew@32 53 heightAvailable /= numberOfChannels;
andrew@7 54
andrew@7 55 bayesPositionWindow.setToRelativeSize(0, startHeight, 1, heightAvailable);
andrew@7 56 bayesLikelihoodWindow.setToRelativeSize(0, startHeight + 1*heightAvailable, 1, heightAvailable);
andrew@7 57 bayesTempoWindow.setToRelativeSize(0, startHeight + 2*heightAvailable, 1, heightAvailable);
andrew@7 58
andrew@7 59
andrew@7 60 }
andrew@0 61
andrew@0 62 void AudioEventMatcher::setArraySizes(){
andrew@0 63 bayesianStruct.resetSpeedSize(200);
andrew@0 64 bayesianStruct.setRelativeSpeedScalar(0.01);
andrew@0 65 bayesianStruct.setSpeedPrior(1.0);
andrew@0 66 bayesianStruct.relativeSpeedPrior.getMaximum();
andrew@0 67
andrew@36 68 float scalarForBayesianDistribution = 2;
andrew@36 69
andrew@36 70 bayesianStruct.resetSize(matchWindowWidth / scalarForBayesianDistribution);
andrew@36 71 bayesianStruct.setPositionDistributionScalar(2);
andrew@0 72
andrew@0 73 }
andrew@0 74
andrew@16 75 void AudioEventMatcher::loadAudioFiles(){
andrew@16 76 recordedTracks.loadTestAudio();
andrew@16 77 synchroniser.fileLengthSamples = recordedTracks.loadedAudioFiles[0].fileLoader.totalNumberOfSamples;
andrew@16 78 printf("synchroniser has %f samples\n", synchroniser.fileLengthSamples);
andrew@20 79
andrew@20 80 calculateRecordedTempoData();
andrew@20 81 printf("\n\nFIRST PASS: FINAL recorded tempo is %f\n", recordedTempoData.playingTempo);
andrew@20 82 setTempoPrior(recordedTempoData.playingTempo);
andrew@20 83 calculateRecordedTempoData();//now calculate again using better prior
andrew@20 84
andrew@20 85 printf("\n\nSECOND PASS: FINAL recorded tempo is %f\n", recordedTempoData.playingTempo);
andrew@20 86 printf("GLOBAL TEMPO of RECORDED FILES\n");
andrew@20 87 recordedTempoData.printTempoTimes();
andrew@20 88 }
andrew@20 89
andrew@20 90 void AudioEventMatcher::setTempoPrior(double tempo){
andrew@20 91 recordedTempoData.zero();
andrew@20 92 recordedTempoData.tempoPosterior.zero();
andrew@20 93 recordedTempoData.tempoPosterior.addGaussianShapeFromRealTime(tempo, 3, 1);
andrew@20 94
andrew@20 95 }
andrew@20 96
andrew@20 97 void AudioEventMatcher::calculateRecordedTempoData(){
andrew@20 98 int indexForOnsets[3];
andrew@20 99 indexForOnsets[0] = 0;
andrew@20 100 indexForOnsets[1] = 0;
andrew@20 101 indexForOnsets[2] = 0;
andrew@20 102 int kickTime, snareTime;
andrew@20 103 while (indexForOnsets[0] < recordedTracks.loadedAudioFiles[0].fileLoader.onsetDetect.chromaOnsets.size() ||
andrew@20 104 indexForOnsets[2] < recordedTracks.loadedAudioFiles[2].fileLoader.onsetDetect.chromaOnsets.size()) {
andrew@20 105
andrew@20 106 setNextOnsetTime(0, kickTime, &indexForOnsets[0]);
andrew@20 107 setNextOnsetTime(2, snareTime, &indexForOnsets[0]);
andrew@20 108
andrew@20 109 if (kickTime < snareTime){
andrew@20 110 printf("update kick at %i\n", kickTime);
andrew@20 111 recordedTempoData.updateTempo(0, kickTime);
andrew@20 112 printf("recorded tempo is %f\n", recordedTempoData.playingTempo);
andrew@20 113 indexForOnsets[0]++;
andrew@20 114 }else {
andrew@20 115 printf("update snare at %i\n", snareTime);
andrew@20 116 recordedTempoData.updateTempo(2, snareTime);
andrew@20 117 printf("recorded tempo is %f\n", recordedTempoData.playingTempo);
andrew@20 118 indexForOnsets[2]++;
andrew@20 119 }
andrew@20 120 }//end while
andrew@20 121
andrew@20 122
andrew@20 123 }
andrew@20 124
andrew@20 125 void AudioEventMatcher::setNextOnsetTime(const int& channel, int& time, int* indexForOnsets){
andrew@20 126 if (indexForOnsets[channel] < recordedTracks.loadedAudioFiles[channel].fileLoader.onsetDetect.chromaOnsets.size()){
andrew@20 127 time = recordedTracks.loadedAudioFiles[channel].fileLoader.onsetDetect.chromaOnsets[indexForOnsets[channel]].millisTime;
andrew@20 128 }
andrew@20 129 else {
andrew@20 130 time = 2147483647;//infinity
andrew@20 131 }
andrew@16 132 }
andrew@16 133
andrew@9 134 void AudioEventMatcher::startPlaying(){
andrew@3 135 bayesianStruct.setStartPlaying();
andrew@8 136 currentAlignmentPosition = 0;
andrew@8 137 startTime = ofGetElapsedTimeMillis();
andrew@11 138
andrew@11 139 projectedPrior = bayesianStruct.prior;
andrew@15 140 startedPlaying = true;
andrew@17 141 synchroniser.reset();
andrew@19 142 temporal.reset();
andrew@17 143
andrew@20 144 recordedTempoIndex = 0;
andrew@20 145 recordedTempo = recordedTempoData.globalTempo[recordedTempoIndex];
andrew@20 146
andrew@20 147 currentSpeedRatio = 1;
andrew@20 148
andrew@21 149 temporal.tempoPosterior.zero();
andrew@36 150 temporal.tempoPosterior.addGaussianShapeFromRealTime(recordedTempo, 2000, 1);
andrew@21 151
andrew@20 152 //SET TEMPO PRIOR for Speed Ratio
andrew@20 153 //the update this
andrew@20 154 setSpeedRatioDistribution(currentSpeedRatio);
andrew@37 155
andrew@37 156 euclideanMaximumDistance = 0;
andrew@37 157
andrew@3 158 //bayesianStruct.posterior.printArray();
andrew@3 159 }
andrew@3 160
andrew@9 161
andrew@20 162 void AudioEventMatcher::setSpeedRatioDistribution(const double& speedRatio){
andrew@39 163 //here is the speed combo actually used
andrew@20 164 bayesianStruct.relativeSpeedPosterior.zero();
andrew@39 165 // bayesianStruct.relativeSpeedPosterior.addToIndex(bayesianStruct.relativeSpeedPosterior.getRealTermsAsIndex(speedRatio), 1);
andrew@39 166 bayesianStruct.relativeSpeedPosterior.addGaussianShapeFromRealTime(1, 0.1, 3);
andrew@39 167 bayesianStruct.relativeSpeedPosterior.addGaussianShapeFromRealTime(1, 0.02, 2);
andrew@20 168 }
andrew@20 169
andrew@15 170 void AudioEventMatcher::stopPlaying(){
andrew@15 171 startedPlaying = false;
andrew@37 172 //temporal.printEventTimes();
andrew@15 173 }
andrew@15 174
andrew@22 175 void AudioEventMatcher::rescue(){
andrew@22 176 bayesianStruct.posterior.zero();
andrew@22 177 bayesianStruct.posterior.addConstant(1);
andrew@22 178 bayesianStruct.prior.zero();
andrew@22 179 bayesianStruct.prior.addConstant(1);
andrew@22 180 }
andrew@22 181
andrew@9 182 void AudioEventMatcher::updatePosition(){
andrew@19 183
andrew@19 184 if (startedPlaying){
andrew@9 185 if (!followingLiveInput)
andrew@9 186 recordedTracks.updatePosition();
andrew@19 187 else
andrew@9 188 recordedTracks.updatePositionToMillis(currentAlignmentPosition);
andrew@9 189
andrew@20 190 updateBestAlignmentPosition();
andrew@19 191 }
andrew@19 192
andrew@20 193 updateRecordedTempo();
andrew@20 194
andrew@19 195 temporal.tempoPosterior.addGaussianShape(temporal.tempoPosterior.MAPestimate, temporal.tempoArraySize / 4, 0.5 );
andrew@9 196 }
andrew@9 197
andrew@20 198 void AudioEventMatcher::updateRecordedTempo(){
andrew@20 199 //tempo of equivalent recorded position is updated
andrew@37 200 if (recordedTempoIndex < recordedTempoData.globalTempoTimes.size()){//if for debug
andrew@20 201 while(currentAlignmentPosition > recordedTempoData.globalTempoTimes[recordedTempoIndex]){
andrew@20 202 recordedTempoIndex++;
andrew@20 203 }
andrew@20 204 recordedTempo = recordedTempoData.globalTempo[recordedTempoIndex];
andrew@20 205 double tmpRatio = currentSpeedRatio;
andrew@20 206 currentSpeedRatio = temporal.playingTempo / recordedTempo;
andrew@20 207 if (currentSpeedRatio != tmpRatio)
andrew@20 208 setSpeedRatioDistribution(currentSpeedRatio);
andrew@37 209
andrew@37 210 }//end if to prevent debug crash
andrew@20 211 }
andrew@20 212
andrew@8 213 void AudioEventMatcher::updateBestAlignmentPosition(){
andrew@10 214 //THIS DEALS WITH WHERE WE ARE NOW! ON THE SCREEN
andrew@10 215 //DIFFERENT TO WHEN EVENTS COME IN AS THEY ARE TIMESTAMPED - SO EG A PITCH EVENT MAY ARRIVE 16 CHROMA FRAMES LATER - BIG DIFFERENCE
andrew@10 216
andrew@10 217 int newTime = ofGetElapsedTimeMillis() - startTime;
andrew@10 218 // double tmp = bayesianStruct.posterior.getIndexInRealTerms(bayesianStruct.posterior.MAPestimate);;
andrew@10 219 // double timetmp = (newTime - lastAlignmentTime);
andrew@10 220 // double speedtmp = bayesianStruct.relativeSpeedPosterior.getIndexInRealTerms(bayesianStruct.relativeSpeedPosterior.MAPestimate);
andrew@11 221 // currentAlignmentTime = newTime;
andrew@9 222 currentAlignmentPosition = bayesianStruct.posterior.getIndexInRealTerms(bayesianStruct.posterior.MAPestimate);
andrew@10 223 currentAlignmentPosition += (newTime - lastAlignmentTime) * bayesianStruct.relativeSpeedPosterior.getIndexInRealTerms(bayesianStruct.relativeSpeedPosterior.MAPestimate);
andrew@10 224
andrew@16 225
andrew@17 226 synchroniser.updateRecordedPosition(currentAlignmentPosition, newTime);
andrew@16 227
andrew@16 228 synchroniser.updateOutputSpeed();
andrew@16 229
andrew@11 230 bayesianStruct.projectDistribution(newTime, currentAlignmentPosition, projectedPrior);//prior gets updated to where we are now
andrew@32 231
andrew@32 232 // printf("updateBestAlignment:: alignment %i:: %i\n", newTime, (int) currentAlignmentPosition);
andrew@11 233
andrew@10 234 // printf("ALIGN pos %f time diff %f (now %f , last %f)speed %f :: ALIGN BEST %f\n", tmp, timetmp, (double)ofGetElapsedTimeMillis(), lastAlignmentTime, speedtmp, currentAlignmentPosition);
andrew@8 235 }
andrew@8 236
andrew@0 237 void AudioEventMatcher::draw(){
andrew@32 238
andrew@32 239 //MAIN DRAW FUNCTION FOR ALL
andrew@32 240
andrew@6 241 //draw some outlines in blue
andrew@3 242 ofSetColor(20,200,200);
andrew@39 243 // bayesPositionWindow.drawOutline();
andrew@39 244 // bayesTempoWindow.drawOutline();
andrew@0 245
andrew@6 246 //draw the scrolling audio tracks
andrew@1 247 recordedTracks.drawTracks();
andrew@7 248
andrew@2 249 ofSetColor(255);
andrew@2 250 // bayesianStruct.relativeSpeedPrior.drawVector(0, 200, bayesTempoWindow);
andrew@9 251
andrew@9 252 setScreenDisplayTimes();
andrew@6 253 drawBayesianDistributions();
andrew@8 254
andrew@11 255 //bayesianStruct.posterior.drawVector(0, bayesianStruct.posterior.getRealTermsAsIndex(screenWidthMillis), bayesPositionWindow);
andrew@6 256 //bayesianStruct.posterior.drawVector(bayesianStruct.posterior.getRealTermsAsIndex(0), bayesianStruct.posterior.getRealTermsAsIndex(screenWidthMillis), bayesPositionWindow);
andrew@11 257 //bayesianStruct.relativeSpeedPosterior.drawVector(0, bayesianStruct.relativeSpeedPosterior.getRealTermsAsIndex(2), bayesTempoWindow);
andrew@9 258
andrew@20 259 temporal.drawTempoArray(bayesLikelihoodWindow);
andrew@20 260
andrew@20 261 drawRecordedTempo();
andrew@20 262 drawPlayingTempo();
andrew@20 263
andrew@20 264
andrew@6 265 }
andrew@20 266
andrew@20 267 void AudioEventMatcher::drawRecordedTempo(){
andrew@6 268
andrew@21 269 int xTempoIndex = ofGetWidth() * (double)(recordedTempo - recordedTempoData.minimumTempoInterval)/(double)(recordedTempoData.maximumTempoInterval - recordedTempoData.minimumTempoInterval);
andrew@20 270 ofSetColor(0, 200, 0);
andrew@20 271 ofLine(xTempoIndex, bayesLikelihoodWindow.y, xTempoIndex, bayesLikelihoodWindow.y + bayesLikelihoodWindow.height);
andrew@20 272 ofDrawBitmapString(ofToString(recordedTempo), xTempoIndex, bayesLikelihoodWindow.y + 10);
andrew@20 273 }
andrew@20 274
andrew@20 275 void AudioEventMatcher::drawPlayingTempo(){
andrew@21 276 //purple line for MAP estimate of new intervals
andrew@21 277 int xTempoIndex = (double)(ofGetWidth() * (temporal.playingTempo - temporal.minimumTempoInterval))/(double)(temporal.maximumTempoInterval - temporal.minimumTempoInterval);
andrew@20 278 ofSetColor(200, 0, 200);
andrew@20 279 ofLine(xTempoIndex, bayesLikelihoodWindow.y, xTempoIndex, bayesLikelihoodWindow.y + bayesLikelihoodWindow.height);
andrew@21 280 ofDrawBitmapString(ofToString(temporal.playingTempo), xTempoIndex, bayesLikelihoodWindow.y + 10);
andrew@20 281
andrew@21 282 //red line where the ratio is between playing tempo and recorded one
andrew@20 283 int xSpeedRatioIndex = (double)(temporal.tempoPosterior.getIndexInRealTerms(currentSpeedRatio)*ofGetWidth())/(double)temporal.tempoPosterior.arraySize;
andrew@20 284 ofSetColor(200,0,0);
andrew@20 285 ofLine(xSpeedRatioIndex, bayesTempoWindow.y, xSpeedRatioIndex, bayesTempoWindow.y + bayesTempoWindow.height);
andrew@21 286 string tmpString = "playing "+ofToString(temporal.playingTempo);
andrew@21 287 tmpString += ", recorded "+ofToString(recordedTempo);
andrew@21 288 tmpString += " ratio "+ofToString(currentSpeedRatio);
andrew@21 289 ofSetColor(155,155,155);
andrew@21 290 ofDrawBitmapString(tmpString, 20, bayesTempoWindow.y+10);
andrew@20 291
andrew@20 292 }
andrew@20 293
andrew@20 294
andrew@9 295 void AudioEventMatcher::setScreenDisplayTimes(){
andrew@9 296 screenWidthMillis = recordedTracks.loadedAudioFiles[0].fileLoader.onsetDetect.framesToMillis(recordedTracks.loadedAudioFiles[0].fileLoader.onsetDetect.amplitudeNumber);
andrew@9 297 // if (!followingLiveInput){
andrew@9 298
andrew@9 299 screenStartTimeMillis = recordedTracks.loadedAudioFiles[0].fileLoader.onsetDetect.framesToMillis(recordedTracks.loadedAudioFiles[0].fileLoader.onsetDetect.drawParams.windowStartFrame);
andrew@9 300 screenEndTimeMillis = screenStartTimeMillis + screenWidthMillis;
andrew@9 301
andrew@9 302 //need PRECISION in this alignment
andrew@9 303
andrew@9 304
andrew@9 305 /*}else{
andrew@9 306
andrew@9 307 screenStartTimeMillis = (int)(currentAlignmentPosition/screenWidthMillis) * screenWidthMillis;
andrew@9 308 screenEndTimeMillis = screenStartTimeMillis + screenWidthMillis;
andrew@9 309 }*/
andrew@9 310 }
andrew@9 311
andrew@6 312 void AudioEventMatcher::drawBayesianDistributions(){
andrew@6 313
andrew@32 314
andrew@32 315 drawPositionWindow();
andrew@4 316
andrew@8 317 // bayesianStruct.likelihood.drawConstrainedVector(startIndex, endIndex, 0, ofGetWidth(), bayesLikelihoodWindow);
andrew@2 318
andrew@6 319 bayesianStruct.relativeSpeedPosterior.drawConstrainedVector(0, bayesianStruct.relativeSpeedPosterior.arraySize, 0, ofGetWidth(), bayesTempoWindow);
andrew@32 320
andrew@6 321
andrew@32 322 drawTrackLikelihoods();
andrew@32 323
andrew@32 324 // int priorStartIndex = bayesianStruct.prior.getRealTermsAsIndex(screenStartTimeMillis);
andrew@32 325 // int priorEndIndex = bayesianStruct.prior.getRealTermsAsIndex(screenEndTimeMillis);
andrew@32 326 // ofSetColor(0,200,200);//recent prior
andrew@32 327 // recentPrior.drawConstrainedVector(priorStartIndex, priorEndIndex, 0, ofGetWidth(), bayesPositionWindow);
andrew@32 328
andrew@32 329 drawInfo();
andrew@32 330
andrew@3 331
andrew@32 332 }
andrew@32 333
andrew@32 334 void AudioEventMatcher::drawPositionWindow(){
andrew@32 335 int startIndex = bayesianStruct.posterior.getRealTermsAsIndex(screenStartTimeMillis);
andrew@32 336 int endIndex = bayesianStruct.posterior.getRealTermsAsIndex(screenEndTimeMillis);
andrew@32 337 string tmpString = "start "+ofToString(screenStartTimeMillis)+" (index "+ofToString(startIndex)+"), end "+ofToString(screenEndTimeMillis);
andrew@32 338 ofDrawBitmapString(tmpString, bayesPositionWindow.x+20, bayesPositionWindow.y+20);
andrew@32 339
andrew@32 340 //draw posterior in the bayes position window
andrew@32 341 ofSetColor(255,0,255);
andrew@32 342 bayesianStruct.posterior.drawConstrainedVector(startIndex, endIndex, 0, ofGetWidth(), bayesPositionWindow);
andrew@3 343
andrew@9 344 //green line at current best estimate
andrew@13 345 ofSetColor(0,255,0);//green scrolling line best position
andrew@8 346 double currentEstimateIndex = (currentAlignmentPosition - screenStartTimeMillis)*ofGetWidth()/screenWidthMillis;
andrew@8 347 ofLine(currentEstimateIndex, bayesPositionWindow.y, currentEstimateIndex, bayesPositionWindow.y + bayesPositionWindow.height);
andrew@7 348
andrew@32 349
andrew@16 350 ofSetColor(0,255,255);//synchroniser position
andrew@16 351 currentEstimateIndex = (synchroniser.playingPositionMillis - screenStartTimeMillis)*ofGetWidth()/screenWidthMillis;
andrew@16 352 ofLine(currentEstimateIndex, bayesLikelihoodWindow.y, currentEstimateIndex, bayesLikelihoodWindow.y + bayesPositionWindow.height);
andrew@32 353
andrew@32 354 ofSetColor(255,0,100);//purple prior
andrew@32 355 bayesianStruct.prior.drawConstrainedVector(bayesianStruct.prior.getRealTermsAsIndex(screenStartTimeMillis), bayesianStruct.prior.getRealTermsAsIndex(screenEndTimeMillis), 0, ofGetWidth(), bayesPositionWindow);
andrew@16 356
andrew@32 357 ofSetColor(255,0,0);//projected prior in red
andrew@32 358 projectedPrior.drawConstrainedVector(bayesianStruct.prior.getRealTermsAsIndex(screenStartTimeMillis), bayesianStruct.prior.getRealTermsAsIndex(screenEndTimeMillis), 0, ofGetWidth(), bayesPositionWindow);
andrew@16 359
andrew@37 360 //draw pitch
andrew@37 361 ofSetColor(0,100,255);
andrew@37 362 int index = getScreenWidthIndexOfEventTime(recentPitchEventTime);
andrew@37 363 //this window would be used (recordedTracks.loadedAudioFiles[1].fileLoader.onsetDetect.window);
andrew@16 364
andrew@32 365
andrew@32 366 }
andrew@32 367
andrew@37 368 int AudioEventMatcher::getScreenWidthIndexOfEventTime(const double& time){
andrew@37 369 return (time - screenStartTimeMillis)*ofGetWidth()/screenWidthMillis;
andrew@37 370 }
andrew@37 371
andrew@32 372 void AudioEventMatcher::drawTrackLikelihoods(){
andrew@7 373 //draw track by track likelihoods
andrew@7 374 for (int i = 0; i <recordedTracks.numberOfAudioTracks;i++){
andrew@13 375 ofSetColor(200,255,50);//channel likelihoods in yellow
andrew@8 376 likelihoodVisualisation[i].drawConstrainedVector(likelihoodVisualisation[i].getRealTermsAsIndex(screenStartTimeMillis), likelihoodVisualisation[i].getRealTermsAsIndex(screenEndTimeMillis), 0, ofGetWidth(), recordedTracks.loadedAudioFiles[i].fileLoader.onsetDetect.window);
andrew@11 377
andrew@13 378 ofSetColor(0,255,150);//channel priors
andrew@11 379 recentPriors[i].drawConstrainedVector(recentPriors[i].getRealTermsAsIndex(screenStartTimeMillis), recentPriors[i].getRealTermsAsIndex(screenEndTimeMillis), 0, ofGetWidth(), recordedTracks.loadedAudioFiles[i].fileLoader.onsetDetect.window);
andrew@11 380
andrew@11 381
andrew@8 382 ofSetColor(255);
andrew@8 383 ofDrawBitmapString("recent event "+ofToString(recentEventTime[i]), recordedTracks.loadedAudioFiles[i].fileLoader.onsetDetect.window.x + 20, recordedTracks.loadedAudioFiles[i].fileLoader.onsetDetect.window.y + recordedTracks.loadedAudioFiles[i].fileLoader.onsetDetect.window.height - 10);
andrew@7 384 }
andrew@32 385 }
andrew@8 386
andrew@8 387
andrew@32 388 void AudioEventMatcher::drawInfo(){
andrew@32 389 string tmpStr = "zero is "+ofToString(bayesianStruct.posterior.getRealTermsAsIndex(0));
andrew@32 390 tmpStr += " offsetis "+ofToString(bayesianStruct.posterior.offset);
andrew@32 391 tmpStr += " screenWidth = "+ofToString(bayesianStruct.posterior.getRealTermsAsIndex(screenWidthMillis));
andrew@32 392 ofDrawBitmapString(tmpStr, 20,140);
andrew@32 393 tmpStr = "best est "+ofToString(bayesianStruct.bestEstimate);
andrew@32 394 ofDrawBitmapString(tmpStr, 20, 180);
andrew@32 395 ofDrawBitmapString("screenwidth "+ofToString(screenWidthMillis), 20, 800);
andrew@11 396
andrew@32 397 ofSetColor(255);
andrew@32 398 tmpStr = "pitch "+ofToString(recentPitch, 2);
andrew@32 399 tmpStr += " Nearest "+ofToString(pitchOfNearestMatch,2);
andrew@32 400 tmpStr += " dist "+ofToString(distanceOfNearestMatch, 2);
andrew@37 401 tmpStr += ", Time "+ofToString(recentPitchEventTime, 0);
andrew@32 402 ofDrawBitmapString(tmpStr, 20, 20);
andrew@7 403
andrew@39 404 string alignString = "align "+ofToString(currentAlignmentPosition, 2);//same as synchroniser-recordedposition
andrew@32 405 alignString += " playing "+ofToString(synchroniser.playingPositionRatio, 5);
andrew@39 406 alignString += " pos "+ofToString(synchroniser.playingPositionMillis,0)+" ms";//playing position in file - causal correction
andrew@39 407 alignString += " rec pos "+ofToString(synchroniser.recordedPositionMillis,0)+" ms";//currentAlignmentPosition in rehearsal
andrew@39 408 alignString += "playing time "+ofToString(synchroniser.recordedPositionTimeSent, 0)+" ms";//playing time since begining of live take
andrew@32 409 ofDrawBitmapString(alignString, 20, 50);
andrew@32 410 ofDrawBitmapString("pos "+ofToString(recordedTracks.loadedAudioFiles[0].fileLoader.onsetDetect.playPosition), 200,600);
andrew@19 411
andrew@1 412 }
andrew@1 413
andrew@6 414 void AudioEventMatcher::newPitchEvent(const int& channel, const double& pitchIn, const double& timeIn){
andrew@7 415 if (pitchIn > 0){
andrew@1 416 liveInput.addPitchEvent(pitchIn, timeIn);
andrew@4 417
andrew@10 418 //printPosteriorMAPinfo();
andrew@11 419
andrew@7 420 matchNewPitchEvent(channel, pitchIn, timeIn);//main pitch matching fn
andrew@7 421
andrew@7 422 likelihoodVisualisation[1] = bayesianStruct.likelihood;
andrew@7 423
andrew@7 424 recentPitch = pitchIn;//for drawing
andrew@37 425 recentPitchEventTime = timeIn;
andrew@7 426 }
andrew@32 427 }
andrew@32 428
andrew@32 429
andrew@32 430 void AudioEventMatcher::newChromaEvent(const int& channel, float* chromaIn, const double& timeIn){
andrew@32 431
andrew@32 432 // could add event to the liveInput list? as in pitch event
andrew@37 433 if (printingData){
andrew@37 434 printf("match chroma channel %i\n", channel);
andrew@37 435 for (int i = 0;i < 12;i++){
andrew@34 436 printf("chroma in[%i] = %f\n", i, chromaIn[i]);
andrew@37 437 }
andrew@34 438 }
andrew@34 439
andrew@32 440 matchNewChromaEvent(channel, chromaIn, timeIn);//main pitch matching fn
andrew@32 441
andrew@32 442 likelihoodVisualisation[channel] = bayesianStruct.likelihood;
andrew@32 443
andrew@8 444
andrew@2 445 }
andrew@2 446
andrew@32 447
andrew@6 448 void AudioEventMatcher::newKickEvent(const double& timeIn){
andrew@6 449 // liveInput.addKickEvent(timeIn);
andrew@2 450 matchNewOnsetEvent(0, timeIn);
andrew@7 451 likelihoodVisualisation[0] = bayesianStruct.likelihood;
andrew@2 452 }
andrew@2 453
andrew@6 454 void AudioEventMatcher::newKickEvent(const int& channel, const double& timeIn){
andrew@6 455 // liveInput.addKickEvent(timeIn);
andrew@6 456 matchNewOnsetEvent(channel, timeIn);
andrew@7 457 likelihoodVisualisation[0] = bayesianStruct.likelihood;
andrew@6 458 }
andrew@6 459
andrew@2 460
andrew@2 461 void AudioEventMatcher::newSnareEvent(const double& timeIn){
andrew@6 462 matchNewOnsetEvent(2, timeIn);
andrew@7 463 likelihoodVisualisation[2] = bayesianStruct.likelihood;
andrew@7 464 }
andrew@7 465
andrew@7 466
andrew@7 467 void AudioEventMatcher::newSnareEvent(const int& channel, const double& timeIn){
andrew@7 468 matchNewOnsetEvent(channel, timeIn);
andrew@7 469 likelihoodVisualisation[2] = bayesianStruct.likelihood;
andrew@2 470 }
andrew@2 471
andrew@2 472 //Needs just to set bounds for the matching process, not have TimeIn
andrew@2 473 void AudioEventMatcher::matchNewOnsetEvent(const int& channel, const double& timeIn){
andrew@3 474
andrew@6 475 bayesianStruct.updateBayesianDistributions(timeIn);//moves the posterior up into prior given the time interval and calculates new offsets
andrew@10 476
andrew@2 477 //start at beginning but OPTIMISE later
andrew@2 478 bayesianStruct.likelihood.offset = bayesianStruct.prior.offset;
andrew@2 479 bayesianStruct.likelihood.zero();//set to zero
andrew@36 480 //double quantity = 1;//
andrew@36 481 double quantity = 1*onsetLikelihoodToNoise;//BETTER CHANGE THIS BACK TOO..see below//likelihoodToNoiseRatio / numberOfMatches;
andrew@2 482 int numberOfMatchesFound = 0;
andrew@2 483
andrew@10 484 double startMatchingTime = bayesianStruct.likelihood.offset;
andrew@10 485 double endMatchingTime = bayesianStruct.likelihood.offset + matchWindowWidth;
andrew@32 486 double millisTime = -1*INFINITY;//or 0 is fine
andrew@32 487 int checkIndex = 0;
andrew@36 488 if (channel <= recordedTracks.numberOfAudioTracks && checkIndex < recordedTracks.loadedAudioFiles[channel].fileLoader.onsetDetect.chromaOnsets.size()){
andrew@32 489 while (millisTime < startMatchingTime) {
andrew@32 490 millisTime = recordedTracks.loadedAudioFiles[channel].fileLoader.onsetDetect.chromaOnsets[checkIndex].millisTime;
andrew@32 491 checkIndex++;
andrew@32 492 }
andrew@32 493 for (int i = checkIndex;i < recordedTracks.loadedAudioFiles[channel].fileLoader.onsetDetect.chromaOnsets.size() && millisTime <= endMatchingTime;i++){
andrew@32 494 millisTime = recordedTracks.loadedAudioFiles[channel].fileLoader.onsetDetect.chromaOnsets[i].millisTime;
andrew@10 495 if (millisTime >= startMatchingTime && millisTime <= endMatchingTime){
andrew@14 496 bayesianStruct.likelihood.addGaussianShapeFromRealTime(millisTime, onsetLikelihoodWidth, quantity);
andrew@2 497 numberOfMatchesFound++;
andrew@6 498 // printf("Adding Gaussian for onset at time %f offset %f\n", millisTime, bayesianStruct.likelihood.offset);
andrew@2 499
andrew@32 500 }//end if within limits (changed so it now is 4 sure)
andrew@2 501 }
andrew@2 502 }
andrew@2 503
andrew@11 504 if (numberOfMatchesFound > 0){
andrew@3 505 // bayesianStruct.likelihood.addConstant((1-likelihoodToNoiseRatio)/bayesianStruct.likelihood.length);
andrew@36 506 // bayesianStruct.likelihood.addConstant(numberOfMatchesFound*(1-onsetLikelihoodToNoise)/(onsetLikelihoodToNoise*bayesianStruct.likelihood.length));
andrew@36 507 bayesianStruct.likelihood.addConstant(numberOfMatchesFound*(1-onsetLikelihoodToNoise)/(bayesianStruct.likelihood.length));//BETTER CHANGE THIS BACK...
andrew@2 508 bayesianStruct.likelihood.renormalise();
andrew@2 509
andrew@8 510 bayesianStruct.calculatePosterior();
andrew@10 511 lastAlignmentTime = timeIn;//use TIMESTAMP
andrew@10 512 recentEventTime[channel] = timeIn;//ofGetElapsedTimeMillis() - startTime;
andrew@11 513
andrew@11 514 recentPriors[channel] = bayesianStruct.prior;
andrew@13 515 projectedPrior = bayesianStruct.prior;
andrew@19 516
andrew@19 517
andrew@19 518 temporal.updateTempo(channel, timeIn);
andrew@11 519 }
andrew@11 520
andrew@3 521 }
andrew@3 522
andrew@3 523
andrew@3 524
andrew@3 525 void AudioEventMatcher::matchNewPitchEvent(const int& channel, const double& pitchIn, const double& timeIn){
andrew@3 526 //start at beginning but OPTIMISE later
andrew@10 527 /*printf("TIME %i\n", ofGetElapsedTimeMillis());
andrew@10 528 //tmp debug
andrew@10 529 updateBestAlignmentPosition();
andrew@10 530 printf("current alignment best estimate %f\n", currentAlignmentPosition);
andrew@10 531 */
andrew@6 532 bayesianStruct.updateBayesianDistributions(timeIn);//moves the posterior up into prior given the time interval and calculates new offsets
andrew@8 533
andrew@7 534 //set the lielihoods by matching the pitched note
andrew@7 535
andrew@15 536
andrew@3 537 int numberOfMatches = 0;
andrew@3 538 bayesianStruct.likelihood.zero();//set to zero
andrew@18 539 double newOnsetTime;
andrew@18 540 double closestDistance = INFINITY;
andrew@3 541
andrew@3 542 double quantity = 0;
andrew@32 543 double totalLikelihoodAdded = 0;
andrew@3 544 if (channel <= recordedTracks.numberOfAudioTracks){
andrew@3 545 for (int i = 0;i < recordedTracks.loadedAudioFiles[channel].fileLoader.onsetDetect.chromaOnsets.size();i++){
andrew@3 546
andrew@3 547 if (checkMatch(recordedTracks.loadedAudioFiles[channel].fileLoader.onsetDetect.chromaOnsets[i].aubioPitch, pitchIn)) {
andrew@32 548 quantity = getPitchDistance(recordedTracks.loadedAudioFiles[channel].fileLoader.onsetDetect.chromaOnsets[i].aubioPitch, pitchIn, 12);
andrew@18 549
andrew@3 550 bayesianStruct.likelihood.addGaussianShapeFromRealTime(recordedTracks.loadedAudioFiles[channel].fileLoader.onsetDetect.chromaOnsets[i].millisTime, 30, quantity);
andrew@3 551 recordedTracks.loadedAudioFiles[channel].fileLoader.onsetDetect.chromaOnsets[i].matched = true;
andrew@3 552 numberOfMatches++;
andrew@32 553 totalLikelihoodAdded += quantity;
andrew@3 554 }
andrew@3 555 else{
andrew@3 556 recordedTracks.loadedAudioFiles[channel].fileLoader.onsetDetect.chromaOnsets[i].matched = false;
andrew@3 557 }
andrew@18 558 //checking nearest pitch
andrew@18 559 newOnsetTime = recordedTracks.loadedAudioFiles[channel].fileLoader.onsetDetect.chromaOnsets[i].millisTime;
andrew@18 560 if (abs(newOnsetTime - currentAlignmentPosition) < closestDistance){
andrew@18 561 closestDistance = abs(newOnsetTime - currentAlignmentPosition);
andrew@18 562 pitchOfNearestMatch = recordedTracks.loadedAudioFiles[channel].fileLoader.onsetDetect.chromaOnsets[i].aubioPitch;
andrew@18 563 distanceOfNearestMatch = quantity;
andrew@18 564 }
andrew@3 565
andrew@3 566 }
andrew@3 567 }
andrew@6 568
andrew@8 569
andrew@8 570
andrew@37 571 if (numberOfMatches > 0 && totalLikelihoodAdded > 0){//no point updating unless there is a match
andrew@32 572 //replacing numberOfMatches with totalLike below...
andrew@37 573 //bug here was that if totaladded = 0, we add then zero likelihood
andrew@37 574 bayesianStruct.likelihood.addConstant(totalLikelihoodAdded*(1-pitchLikelihoodToNoise)/(bayesianStruct.likelihood.length));
andrew@37 575 // bayesianStruct.likelihood.addConstant(totalLikelihoodAdded*(1-pitchLikelihoodToNoise)/(pitchLikelihoodToNoise*bayesianStruct.likelihood.length));
andrew@4 576
andrew@4 577 //tmp set likelihood constant and calculate using that
andrew@6 578 //bayesianStruct.likelihood.zero();
andrew@6 579 //bayesianStruct.likelihood.addConstant(1);
andrew@7 580
andrew@6 581 bayesianStruct.calculatePosterior();
andrew@11 582 lastAlignmentTime = timeIn;//has to use the STAMPED time
andrew@11 583 recentEventTime[channel] = timeIn;
andrew@11 584
andrew@11 585 recentPriors[channel] = bayesianStruct.prior;
andrew@13 586 projectedPrior = bayesianStruct.prior;
andrew@19 587
andrew@19 588 temporal.eventTimes[channel].push_back(timeIn);
andrew@6 589 }
andrew@4 590
andrew@11 591
andrew@1 592 }
andrew@1 593
andrew@3 594 double AudioEventMatcher::getPitchDistance(const double& pitchOne, const double& pitchTwo, const double& scale){
andrew@3 595
andrew@18 596 double scaleFactor = scale * pitchOne / 110.0;
andrew@16 597
andrew@18 598 int multiplicationFactor = 1;
andrew@18 599 if (pitchTwo > 0){
andrew@32 600 multiplicationFactor = round(pitchOne/pitchTwo);
andrew@18 601 }
andrew@16 602
andrew@18 603 double distance = abs(pitchOne - pitchTwo*multiplicationFactor);
andrew@16 604 if (distance < scaleFactor)
andrew@16 605 distance = 1 - (distance/scaleFactor);
andrew@3 606 else
andrew@3 607 distance = 0;
andrew@3 608
andrew@32 609 //printf("[pitch distance %f vs %f, factor %i = %f\n", pitchOne, pitchTwo, multiplicationFactor, distance);
andrew@3 610 return distance;
andrew@3 611
andrew@3 612 }
andrew@3 613
andrew@3 614
andrew@3 615 bool AudioEventMatcher::checkMatch(const double& recordedPitch, const double& livePitch){
andrew@18 616
andrew@18 617 if (livePitch > 0){
andrew@18 618 int multiplicationFactor = (int)(round(recordedPitch/livePitch));
andrew@18 619
andrew@32 620 if (abs(recordedPitch - livePitch * multiplicationFactor) < pitchCutOff)
andrew@3 621 return true;
andrew@3 622 else
andrew@3 623 return false;
andrew@18 624 }else {
andrew@18 625 return false;
andrew@18 626 }
andrew@18 627
andrew@3 628 }
andrew@3 629
andrew@3 630
andrew@32 631 void AudioEventMatcher::matchNewChromaEvent(const int& channel, float* chromaIn, const double& timeIn){
andrew@32 632 //start at beginning but OPTIMISE later
andrew@32 633
andrew@32 634 bayesianStruct.updateBayesianDistributions(timeIn);//moves the posterior up into prior given the time interval and calculates new offsets
andrew@32 635
andrew@32 636 //set the likelihoods by matching the pitched note
andrew@32 637
andrew@32 638 int numberOfMatches = 0;
andrew@32 639 bayesianStruct.likelihood.zero();//set to zero
andrew@32 640 double newOnsetTime;
andrew@32 641 double closestDistance = INFINITY;
andrew@32 642
andrew@32 643 double quantity = 1;
andrew@32 644 double totalLikelihoodAdded = 0;
andrew@32 645
andrew@32 646 double startMatchingTime = bayesianStruct.likelihood.offset;
andrew@32 647 double endMatchingTime = bayesianStruct.likelihood.offset + matchWindowWidth;
andrew@32 648 double millisTime = -1*INFINITY;//or 0 is fine
andrew@32 649
andrew@32 650 int checkIndex = 0;
andrew@37 651 if (channel <= recordedTracks.numberOfAudioTracks && checkIndex < recordedTracks.loadedAudioFiles[channel].fileLoader.onsetDetect.chromaOnsets.size()){
andrew@37 652
andrew@32 653 while (millisTime < startMatchingTime) {
andrew@32 654 millisTime = recordedTracks.loadedAudioFiles[channel].fileLoader.onsetDetect.chromaOnsets[checkIndex].millisTime;
andrew@32 655 checkIndex++;
andrew@32 656 }//go up to where we need to check from fast
andrew@32 657
andrew@32 658 for (int i = checkIndex;i < recordedTracks.loadedAudioFiles[channel].fileLoader.onsetDetect.chromaOnsets.size() && millisTime <= endMatchingTime;i++){
andrew@32 659 millisTime = recordedTracks.loadedAudioFiles[channel].fileLoader.onsetDetect.chromaOnsets[i].millisTime;
andrew@32 660
andrew@32 661 if (millisTime >= startMatchingTime && millisTime <= endMatchingTime){
andrew@35 662
andrew@35 663 if (useChromaDotProduct)
andrew@35 664 quantity = getChromaDotProductDistance(chromaIn, &recordedTracks.loadedAudioFiles[channel].fileLoader.onsetDetect.chromaOnsets[i].chromaValues[0]);
andrew@35 665 else
andrew@35 666 quantity = getChromaEuclideanDistance(chromaIn, &recordedTracks.loadedAudioFiles[channel].fileLoader.onsetDetect.chromaOnsets[i].chromaValues[0]);
andrew@35 667
andrew@35 668
andrew@32 669 bayesianStruct.likelihood.addGaussianShapeFromRealTime(recordedTracks.loadedAudioFiles[channel].fileLoader.onsetDetect.chromaOnsets[i].millisTime, chromaLikelihoodWidth, quantity);
andrew@32 670
andrew@32 671 // bayesianStruct.likelihood.addGaussianShapeFromRealTime(millisTime, onsetLikelihoodWidth, quantity);
andrew@32 672 numberOfMatches++;
andrew@32 673 totalLikelihoodAdded += quantity;
andrew@37 674
andrew@37 675 //printf("Adding CHROMA Gaussian for onset at time %.1f dist %.3f\n", millisTime, quantity);
andrew@32 676
andrew@32 677 }//end if within limits (changed so it now is 4 sure)
andrew@32 678 }
andrew@32 679 }
andrew@32 680
andrew@32 681
andrew@37 682 if (numberOfMatches > 0 && totalLikelihoodAdded > 0){//no point updating unless there is a match
andrew@32 683 //replacing numberOfMatches with totalLike below...
andrew@32 684
andrew@32 685 printf("CHROMA HAS %i MATCHES\n", numberOfMatches);
andrew@32 686
andrew@37 687 bayesianStruct.likelihood.addConstant(totalLikelihoodAdded*(1-chromaLikelihoodToNoise)/(bayesianStruct.likelihood.length));
andrew@37 688 //previous way
andrew@37 689 // bayesianStruct.likelihood.addConstant(totalLikelihoodAdded*(1-chromaLikelihoodToNoise)/(chromaLikelihoodToNoise*bayesianStruct.likelihood.length));
andrew@32 690
andrew@32 691 bayesianStruct.calculatePosterior();
andrew@32 692 lastAlignmentTime = timeIn;//has to use the STAMPED time
andrew@32 693 recentEventTime[channel] = timeIn;
andrew@32 694
andrew@32 695 recentPriors[channel] = bayesianStruct.prior;
andrew@32 696 projectedPrior = bayesianStruct.prior;
andrew@32 697
andrew@32 698 temporal.eventTimes[channel].push_back(timeIn);
andrew@32 699 }
andrew@32 700
andrew@32 701 }
andrew@32 702
andrew@32 703
andrew@35 704 double AudioEventMatcher::getChromaDotProductDistance(float* chromaOne, float* chromaTwo){
andrew@32 705 double distance = 0;
andrew@32 706 double total = 0;
andrew@32 707 for (int i = 0;i < 12;i++){
andrew@32 708 distance += chromaOne[i]*chromaTwo[i];
andrew@32 709 total += chromaOne[i]*chromaOne[i] + (chromaTwo[i]*chromaTwo[i]);
andrew@32 710 }
andrew@32 711
andrew@35 712 if (total > 0)
andrew@35 713 distance /= sqrt(total);
andrew@35 714
andrew@35 715 return distance;
andrew@35 716 }
andrew@35 717
andrew@35 718 double AudioEventMatcher::getChromaEuclideanDistance(float* chromaOne, float* chromaTwo){
andrew@35 719 double distance = 0;
andrew@35 720 double total = 0;
andrew@37 721
andrew@35 722 // printf("\n");
andrew@35 723 for (int i = 0;i < 12;i++){
andrew@35 724 total += (chromaOne[i] - chromaTwo[i])*(chromaOne[i] - chromaTwo[i]);
andrew@35 725 // printf("chroma1: %.2f; chroma2: %.2f\n", chromaOne[i], chromaTwo[i]);
andrew@35 726 // total += chromaOne[i]*chromaOne[i] + (chromaTwo[i]*chromaTwo[i]);
andrew@35 727 }
andrew@35 728
andrew@37 729 if (total > euclideanMaximumDistance)
andrew@37 730 euclideanMaximumDistance = total;
andrew@37 731
andrew@37 732 distance = ((euclideanMaximumDistance - total)/ euclideanMaximumDistance);//i.e. 1 is
andrew@37 733
andrew@37 734 // if (total > 0)
andrew@37 735
andrew@37 736
andrew@37 737 // distance = 1.0/sqrt(total);
andrew@35 738 // printf("DISTANCE : %.3f\n", distance);
andrew@32 739 return distance;
andrew@32 740 }
andrew@1 741
andrew@1 742 void AudioEventMatcher::windowResized(const int& w, const int& h){
andrew@1 743 recordedTracks.windowResized(w,h);
andrew@3 744 bayesTempoWindow.resized(w,h);
andrew@3 745 bayesPositionWindow.resized(w,h);
andrew@3 746 }
andrew@3 747
andrew@10 748 /*
andrew@10 749
andrew@10 750 void printPosteriorMAPinfo(){ //tmp print stuff
andrew@10 751 printf("New pitch MAP post estimate now %i, ", bayesianStruct.posterior.MAPestimate);
andrew@10 752 double tmp = bayesianStruct.posterior.getMAPestimate();
andrew@10 753 printf(" getting it %f and offset %f == %f ms\n", tmp, bayesianStruct.posterior.offset, bayesianStruct.posterior.getIndexInRealTerms(tmp));
andrew@10 754
andrew@10 755 }
andrew@10 756 */
andrew@3 757