annotate src/AudioEventMatcher.cpp @ 39:f5de07b4d733

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