annotate src/AudioEventMatcher.cpp @ 34:0d52ba6844b9

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