annotate src/AudioEventMatcher.cpp @ 21:604add81822f

fixing aspects of the draw window
author Andrew N Robertson <andrew.robertson@eecs.qmul.ac.uk>
date Sun, 12 Feb 2012 01:07:45 +0000
parents 4f6006cac9de
children 24c413f0f2c5
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@0 14
andrew@0 15 AudioEventMatcher::AudioEventMatcher(){
andrew@7 16
andrew@15 17
andrew@17 18 pitchLikelihoodToNoise = 0.7;//more noise
andrew@16 19
andrew@17 20 onsetLikelihoodToNoise = 0.5;
andrew@17 21 onsetLikelihoodWidth = 10;//in ms
andrew@15 22
andrew@0 23 setArraySizes();
andrew@3 24
andrew@3 25 usingRealTime = false;
andrew@3 26 bayesianStruct.realTimeMode = &usingRealTime;
andrew@7 27 recentPitch = 0;
andrew@8 28 currentAlignmentPosition = 0;
andrew@14 29
andrew@15 30
andrew@9 31
andrew@9 32 followingLiveInput = true;
andrew@15 33 startedPlaying = false;
andrew@20 34 recordedTempoIndex = 0;
andrew@20 35 // temporal.setUpEventTimeMatrix();
andrew@20 36 // recordedTempoData.setUpEventTimeMatrix();
andrew@0 37 }
andrew@0 38
andrew@14 39
andrew@19 40
andrew@19 41
andrew@7 42 void AudioEventMatcher::setWindowDimensions(){
andrew@7 43 double startHeight = recordedTracks.numberOfAudioTracks * recordedTracks.trackScreenHeight;
andrew@7 44 double heightAvailable = 1 - startHeight;
andrew@19 45 heightAvailable /= NUMBER_OF_CHANNELS;
andrew@7 46
andrew@7 47 bayesPositionWindow.setToRelativeSize(0, startHeight, 1, heightAvailable);
andrew@7 48 bayesLikelihoodWindow.setToRelativeSize(0, startHeight + 1*heightAvailable, 1, heightAvailable);
andrew@7 49 bayesTempoWindow.setToRelativeSize(0, startHeight + 2*heightAvailable, 1, heightAvailable);
andrew@7 50
andrew@7 51
andrew@7 52 }
andrew@0 53
andrew@0 54 void AudioEventMatcher::setArraySizes(){
andrew@0 55 bayesianStruct.resetSpeedSize(200);
andrew@0 56 bayesianStruct.setRelativeSpeedScalar(0.01);
andrew@0 57 bayesianStruct.setSpeedPrior(1.0);
andrew@0 58 bayesianStruct.relativeSpeedPrior.getMaximum();
andrew@0 59
andrew@0 60 bayesianStruct.resetSize(matchWindowWidth);
andrew@0 61 bayesianStruct.setPositionDistributionScalar(1);
andrew@0 62
andrew@0 63 }
andrew@0 64
andrew@16 65 void AudioEventMatcher::loadAudioFiles(){
andrew@16 66 recordedTracks.loadTestAudio();
andrew@16 67 synchroniser.fileLengthSamples = recordedTracks.loadedAudioFiles[0].fileLoader.totalNumberOfSamples;
andrew@16 68 printf("synchroniser has %f samples\n", synchroniser.fileLengthSamples);
andrew@20 69
andrew@20 70 calculateRecordedTempoData();
andrew@20 71 printf("\n\nFIRST PASS: FINAL recorded tempo is %f\n", recordedTempoData.playingTempo);
andrew@20 72 setTempoPrior(recordedTempoData.playingTempo);
andrew@20 73 calculateRecordedTempoData();//now calculate again using better prior
andrew@20 74
andrew@20 75 printf("\n\nSECOND PASS: FINAL recorded tempo is %f\n", recordedTempoData.playingTempo);
andrew@20 76 printf("GLOBAL TEMPO of RECORDED FILES\n");
andrew@20 77 recordedTempoData.printTempoTimes();
andrew@20 78 }
andrew@20 79
andrew@20 80 void AudioEventMatcher::setTempoPrior(double tempo){
andrew@20 81 recordedTempoData.zero();
andrew@20 82 recordedTempoData.tempoPosterior.zero();
andrew@20 83 recordedTempoData.tempoPosterior.addGaussianShapeFromRealTime(tempo, 3, 1);
andrew@20 84
andrew@20 85 }
andrew@20 86
andrew@20 87 void AudioEventMatcher::calculateRecordedTempoData(){
andrew@20 88 int indexForOnsets[3];
andrew@20 89 indexForOnsets[0] = 0;
andrew@20 90 indexForOnsets[1] = 0;
andrew@20 91 indexForOnsets[2] = 0;
andrew@20 92 int kickTime, snareTime;
andrew@20 93 while (indexForOnsets[0] < recordedTracks.loadedAudioFiles[0].fileLoader.onsetDetect.chromaOnsets.size() ||
andrew@20 94 indexForOnsets[2] < recordedTracks.loadedAudioFiles[2].fileLoader.onsetDetect.chromaOnsets.size()) {
andrew@20 95
andrew@20 96 setNextOnsetTime(0, kickTime, &indexForOnsets[0]);
andrew@20 97 setNextOnsetTime(2, snareTime, &indexForOnsets[0]);
andrew@20 98
andrew@20 99 if (kickTime < snareTime){
andrew@20 100 printf("update kick at %i\n", kickTime);
andrew@20 101 recordedTempoData.updateTempo(0, kickTime);
andrew@20 102 printf("recorded tempo is %f\n", recordedTempoData.playingTempo);
andrew@20 103 indexForOnsets[0]++;
andrew@20 104 }else {
andrew@20 105 printf("update snare at %i\n", snareTime);
andrew@20 106 recordedTempoData.updateTempo(2, snareTime);
andrew@20 107 printf("recorded tempo is %f\n", recordedTempoData.playingTempo);
andrew@20 108 indexForOnsets[2]++;
andrew@20 109 }
andrew@20 110 }//end while
andrew@20 111
andrew@20 112
andrew@20 113 }
andrew@20 114
andrew@20 115 void AudioEventMatcher::setNextOnsetTime(const int& channel, int& time, int* indexForOnsets){
andrew@20 116 if (indexForOnsets[channel] < recordedTracks.loadedAudioFiles[channel].fileLoader.onsetDetect.chromaOnsets.size()){
andrew@20 117 time = recordedTracks.loadedAudioFiles[channel].fileLoader.onsetDetect.chromaOnsets[indexForOnsets[channel]].millisTime;
andrew@20 118 }
andrew@20 119 else {
andrew@20 120 time = 2147483647;//infinity
andrew@20 121 }
andrew@16 122 }
andrew@16 123
andrew@9 124 void AudioEventMatcher::startPlaying(){
andrew@3 125 bayesianStruct.setStartPlaying();
andrew@8 126 currentAlignmentPosition = 0;
andrew@8 127 startTime = ofGetElapsedTimeMillis();
andrew@11 128
andrew@11 129 projectedPrior = bayesianStruct.prior;
andrew@15 130 startedPlaying = true;
andrew@17 131 synchroniser.reset();
andrew@19 132 temporal.reset();
andrew@17 133
andrew@20 134 recordedTempoIndex = 0;
andrew@20 135 recordedTempo = recordedTempoData.globalTempo[recordedTempoIndex];
andrew@20 136
andrew@20 137 currentSpeedRatio = 1;
andrew@20 138
andrew@21 139 temporal.tempoPosterior.zero();
andrew@21 140 temporal.tempoPosterior.addGaussianShapeFromRealTime(recordedTempo, 10, 1);
andrew@21 141
andrew@20 142 //SET TEMPO PRIOR for Speed Ratio
andrew@20 143 //the update this
andrew@20 144 setSpeedRatioDistribution(currentSpeedRatio);
andrew@3 145 //bayesianStruct.posterior.printArray();
andrew@3 146 }
andrew@3 147
andrew@9 148
andrew@20 149 void AudioEventMatcher::setSpeedRatioDistribution(const double& speedRatio){
andrew@20 150 bayesianStruct.relativeSpeedPosterior.zero();
andrew@20 151 bayesianStruct.relativeSpeedPosterior.addToIndex(bayesianStruct.relativeSpeedPosterior.getRealTermsAsIndex(speedRatio), 1);
andrew@20 152 bayesianStruct.relativeSpeedPosterior.addGaussianShapeFromRealTime(1, 0.06, 0.5);
andrew@20 153 }
andrew@20 154
andrew@15 155 void AudioEventMatcher::stopPlaying(){
andrew@15 156 startedPlaying = false;
andrew@19 157 temporal.printEventTimes();
andrew@15 158 }
andrew@15 159
andrew@9 160 void AudioEventMatcher::updatePosition(){
andrew@19 161
andrew@19 162 if (startedPlaying){
andrew@9 163 if (!followingLiveInput)
andrew@9 164 recordedTracks.updatePosition();
andrew@19 165 else
andrew@9 166 recordedTracks.updatePositionToMillis(currentAlignmentPosition);
andrew@9 167
andrew@20 168 updateBestAlignmentPosition();
andrew@19 169 }
andrew@19 170
andrew@20 171 updateRecordedTempo();
andrew@20 172
andrew@19 173 temporal.tempoPosterior.addGaussianShape(temporal.tempoPosterior.MAPestimate, temporal.tempoArraySize / 4, 0.5 );
andrew@9 174 }
andrew@9 175
andrew@20 176 void AudioEventMatcher::updateRecordedTempo(){
andrew@20 177 //tempo of equivalent recorded position is updated
andrew@20 178 while(currentAlignmentPosition > recordedTempoData.globalTempoTimes[recordedTempoIndex]){
andrew@20 179 recordedTempoIndex++;
andrew@20 180 }
andrew@20 181 recordedTempo = recordedTempoData.globalTempo[recordedTempoIndex];
andrew@20 182 double tmpRatio = currentSpeedRatio;
andrew@20 183 currentSpeedRatio = temporal.playingTempo / recordedTempo;
andrew@20 184 if (currentSpeedRatio != tmpRatio)
andrew@20 185 setSpeedRatioDistribution(currentSpeedRatio);
andrew@20 186 }
andrew@20 187
andrew@8 188 void AudioEventMatcher::updateBestAlignmentPosition(){
andrew@10 189 //THIS DEALS WITH WHERE WE ARE NOW! ON THE SCREEN
andrew@10 190 //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 191
andrew@10 192 int newTime = ofGetElapsedTimeMillis() - startTime;
andrew@10 193 // double tmp = bayesianStruct.posterior.getIndexInRealTerms(bayesianStruct.posterior.MAPestimate);;
andrew@10 194 // double timetmp = (newTime - lastAlignmentTime);
andrew@10 195 // double speedtmp = bayesianStruct.relativeSpeedPosterior.getIndexInRealTerms(bayesianStruct.relativeSpeedPosterior.MAPestimate);
andrew@11 196 // currentAlignmentTime = newTime;
andrew@9 197 currentAlignmentPosition = bayesianStruct.posterior.getIndexInRealTerms(bayesianStruct.posterior.MAPestimate);
andrew@10 198 currentAlignmentPosition += (newTime - lastAlignmentTime) * bayesianStruct.relativeSpeedPosterior.getIndexInRealTerms(bayesianStruct.relativeSpeedPosterior.MAPestimate);
andrew@10 199
andrew@16 200
andrew@17 201 synchroniser.updateRecordedPosition(currentAlignmentPosition, newTime);
andrew@16 202
andrew@16 203 synchroniser.updateOutputSpeed();
andrew@16 204
andrew@11 205 bayesianStruct.projectDistribution(newTime, currentAlignmentPosition, projectedPrior);//prior gets updated to where we are now
andrew@11 206
andrew@10 207 // 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 208 }
andrew@8 209
andrew@0 210 void AudioEventMatcher::draw(){
andrew@6 211 //draw some outlines in blue
andrew@3 212 ofSetColor(20,200,200);
andrew@3 213 bayesPositionWindow.drawOutline();
andrew@3 214 bayesTempoWindow.drawOutline();
andrew@0 215
andrew@6 216 //draw the scrolling audio tracks
andrew@1 217 recordedTracks.drawTracks();
andrew@7 218
andrew@2 219 ofSetColor(255);
andrew@2 220 // bayesianStruct.relativeSpeedPrior.drawVector(0, 200, bayesTempoWindow);
andrew@9 221
andrew@9 222 setScreenDisplayTimes();
andrew@6 223 drawBayesianDistributions();
andrew@8 224
andrew@11 225 //bayesianStruct.posterior.drawVector(0, bayesianStruct.posterior.getRealTermsAsIndex(screenWidthMillis), bayesPositionWindow);
andrew@6 226 //bayesianStruct.posterior.drawVector(bayesianStruct.posterior.getRealTermsAsIndex(0), bayesianStruct.posterior.getRealTermsAsIndex(screenWidthMillis), bayesPositionWindow);
andrew@11 227 //bayesianStruct.relativeSpeedPosterior.drawVector(0, bayesianStruct.relativeSpeedPosterior.getRealTermsAsIndex(2), bayesTempoWindow);
andrew@18 228 string tmpStr = "pitch "+ofToString(recentPitch, 2);
andrew@18 229 tmpStr += " Nearest "+ofToString(pitchOfNearestMatch,2);
andrew@18 230 tmpStr += " dist "+ofToString(distanceOfNearestMatch, 2);
andrew@18 231 tmpStr += ", Time "+ofToString(recentTime, 0);
andrew@18 232 ofDrawBitmapString(tmpStr, 20, 20);
andrew@18 233
andrew@18 234
andrew@9 235
andrew@16 236 string alignString = " align "+ofToString(currentAlignmentPosition, 2);
andrew@16 237 alignString += " playing "+ofToString(synchroniser.playingPositionRatio, 5);
andrew@17 238 alignString += " pos "+ofToString(synchroniser.playingPositionMillis,0)+" ms";
andrew@17 239 alignString += " rec pos "+ofToString(synchroniser.recordedPositionMillis,0)+" ms";
andrew@16 240 ofDrawBitmapString(alignString, 20, 50);
andrew@16 241
andrew@9 242 ofDrawBitmapString("pos "+ofToString(recordedTracks.loadedAudioFiles[0].fileLoader.onsetDetect.playPosition), 200,600);
andrew@20 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@6 299
andrew@6 300 int startIndex = bayesianStruct.posterior.getRealTermsAsIndex(screenStartTimeMillis);
andrew@6 301 int endIndex = bayesianStruct.posterior.getRealTermsAsIndex(screenEndTimeMillis);
andrew@4 302
andrew@6 303 bayesianStruct.posterior.drawConstrainedVector(startIndex, endIndex, 0, ofGetWidth(), bayesPositionWindow);
andrew@6 304
andrew@6 305 string tmpString = "start "+ofToString(screenStartTimeMillis)+" (index "+ofToString(startIndex)+"), end "+ofToString(screenEndTimeMillis);
andrew@6 306 ofDrawBitmapString(tmpString, bayesPositionWindow.x+20, bayesPositionWindow.y+20);
andrew@4 307
andrew@8 308 // bayesianStruct.likelihood.drawConstrainedVector(startIndex, endIndex, 0, ofGetWidth(), bayesLikelihoodWindow);
andrew@2 309
andrew@6 310 bayesianStruct.relativeSpeedPosterior.drawConstrainedVector(0, bayesianStruct.relativeSpeedPosterior.arraySize, 0, ofGetWidth(), bayesTempoWindow);
andrew@6 311
andrew@3 312 string tmpStr = "zero is "+ofToString(bayesianStruct.posterior.getRealTermsAsIndex(0));
andrew@3 313 tmpStr += " offsetis "+ofToString(bayesianStruct.posterior.offset);
andrew@3 314 tmpStr += " screenWidth = "+ofToString(bayesianStruct.posterior.getRealTermsAsIndex(screenWidthMillis));
andrew@3 315 ofDrawBitmapString(tmpStr, 20,140);
andrew@3 316 tmpStr = "best est "+ofToString(bayesianStruct.bestEstimate);
andrew@3 317 ofDrawBitmapString(tmpStr, 20, 180);
andrew@3 318
andrew@8 319 ofDrawBitmapString("screenwidth "+ofToString(screenWidthMillis), 20, 800);
andrew@3 320
andrew@9 321 //green line at current best estimate
andrew@13 322 ofSetColor(0,255,0);//green scrolling line best position
andrew@8 323 double currentEstimateIndex = (currentAlignmentPosition - screenStartTimeMillis)*ofGetWidth()/screenWidthMillis;
andrew@8 324 ofLine(currentEstimateIndex, bayesPositionWindow.y, currentEstimateIndex, bayesPositionWindow.y + bayesPositionWindow.height);
andrew@7 325
andrew@16 326
andrew@16 327 ofSetColor(0,255,255);//synchroniser position
andrew@16 328 currentEstimateIndex = (synchroniser.playingPositionMillis - screenStartTimeMillis)*ofGetWidth()/screenWidthMillis;
andrew@16 329 ofLine(currentEstimateIndex, bayesLikelihoodWindow.y, currentEstimateIndex, bayesLikelihoodWindow.y + bayesPositionWindow.height);
andrew@16 330
andrew@16 331
andrew@16 332
andrew@7 333 //draw track by track likelihoods
andrew@7 334 for (int i = 0; i <recordedTracks.numberOfAudioTracks;i++){
andrew@13 335 ofSetColor(200,255,50);//channel likelihoods in yellow
andrew@8 336 likelihoodVisualisation[i].drawConstrainedVector(likelihoodVisualisation[i].getRealTermsAsIndex(screenStartTimeMillis), likelihoodVisualisation[i].getRealTermsAsIndex(screenEndTimeMillis), 0, ofGetWidth(), recordedTracks.loadedAudioFiles[i].fileLoader.onsetDetect.window);
andrew@11 337
andrew@13 338 ofSetColor(0,255,150);//channel priors
andrew@11 339 recentPriors[i].drawConstrainedVector(recentPriors[i].getRealTermsAsIndex(screenStartTimeMillis), recentPriors[i].getRealTermsAsIndex(screenEndTimeMillis), 0, ofGetWidth(), recordedTracks.loadedAudioFiles[i].fileLoader.onsetDetect.window);
andrew@11 340
andrew@11 341
andrew@8 342 ofSetColor(255);
andrew@8 343 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 344 }
andrew@8 345
andrew@13 346 int priorStartIndex = bayesianStruct.prior.getRealTermsAsIndex(screenStartTimeMillis);
andrew@13 347 int priorEndIndex = bayesianStruct.prior.getRealTermsAsIndex(screenEndTimeMillis);
andrew@13 348 // ofSetColor(0,200,200);//recent prior
andrew@13 349 // recentPrior.drawConstrainedVector(priorStartIndex, priorEndIndex, 0, ofGetWidth(), bayesPositionWindow);
andrew@8 350
andrew@10 351 ofSetColor(255,0,100);//purple prior
andrew@11 352 bayesianStruct.prior.drawConstrainedVector(bayesianStruct.prior.getRealTermsAsIndex(screenStartTimeMillis), bayesianStruct.prior.getRealTermsAsIndex(screenEndTimeMillis), 0, ofGetWidth(), bayesPositionWindow);
andrew@11 353
andrew@11 354 ofSetColor(255,0,0);
andrew@13 355 projectedPrior.drawConstrainedVector(bayesianStruct.prior.getRealTermsAsIndex(screenStartTimeMillis), bayesianStruct.prior.getRealTermsAsIndex(screenEndTimeMillis), 0, ofGetWidth(), bayesPositionWindow);
andrew@7 356
andrew@20 357
andrew@19 358
andrew@1 359 }
andrew@1 360
andrew@6 361 void AudioEventMatcher::newPitchEvent(const int& channel, const double& pitchIn, const double& timeIn){
andrew@7 362 if (pitchIn > 0){
andrew@1 363 liveInput.addPitchEvent(pitchIn, timeIn);
andrew@4 364
andrew@10 365 //printPosteriorMAPinfo();
andrew@11 366
andrew@7 367 matchNewPitchEvent(channel, pitchIn, timeIn);//main pitch matching fn
andrew@7 368
andrew@7 369 likelihoodVisualisation[1] = bayesianStruct.likelihood;
andrew@7 370
andrew@7 371 recentPitch = pitchIn;//for drawing
andrew@7 372 recentTime = timeIn;
andrew@7 373 }
andrew@8 374
andrew@2 375 }
andrew@2 376
andrew@6 377 void AudioEventMatcher::newKickEvent(const double& timeIn){
andrew@6 378 // liveInput.addKickEvent(timeIn);
andrew@2 379 matchNewOnsetEvent(0, timeIn);
andrew@7 380 likelihoodVisualisation[0] = bayesianStruct.likelihood;
andrew@2 381 }
andrew@2 382
andrew@6 383 void AudioEventMatcher::newKickEvent(const int& channel, const double& timeIn){
andrew@6 384 // liveInput.addKickEvent(timeIn);
andrew@6 385 matchNewOnsetEvent(channel, timeIn);
andrew@7 386 likelihoodVisualisation[0] = bayesianStruct.likelihood;
andrew@6 387 }
andrew@6 388
andrew@2 389
andrew@2 390 void AudioEventMatcher::newSnareEvent(const double& timeIn){
andrew@6 391 matchNewOnsetEvent(2, timeIn);
andrew@7 392 likelihoodVisualisation[2] = bayesianStruct.likelihood;
andrew@7 393 }
andrew@7 394
andrew@7 395
andrew@7 396 void AudioEventMatcher::newSnareEvent(const int& channel, const double& timeIn){
andrew@7 397 matchNewOnsetEvent(channel, timeIn);
andrew@7 398 likelihoodVisualisation[2] = bayesianStruct.likelihood;
andrew@2 399 }
andrew@2 400
andrew@2 401 //Needs just to set bounds for the matching process, not have TimeIn
andrew@2 402 void AudioEventMatcher::matchNewOnsetEvent(const int& channel, const double& timeIn){
andrew@3 403
andrew@6 404 bayesianStruct.updateBayesianDistributions(timeIn);//moves the posterior up into prior given the time interval and calculates new offsets
andrew@10 405
andrew@2 406 //start at beginning but OPTIMISE later
andrew@15 407
andrew@2 408
andrew@2 409 bayesianStruct.likelihood.offset = bayesianStruct.prior.offset;
andrew@2 410 bayesianStruct.likelihood.zero();//set to zero
andrew@2 411
andrew@2 412 double quantity = 1;//likelihoodToNoiseRatio / numberOfMatches;
andrew@2 413 int numberOfMatchesFound = 0;
andrew@2 414
andrew@2 415
andrew@10 416 double startMatchingTime = bayesianStruct.likelihood.offset;
andrew@10 417 double endMatchingTime = bayesianStruct.likelihood.offset + matchWindowWidth;
andrew@2 418
andrew@2 419 if (channel <= recordedTracks.numberOfAudioTracks){
andrew@2 420 for (int i = 0;i < recordedTracks.loadedAudioFiles[channel].fileLoader.onsetDetect.chromaOnsets.size();i++){
andrew@2 421 double millisTime = recordedTracks.loadedAudioFiles[channel].fileLoader.onsetDetect.chromaOnsets[i].millisTime;
andrew@10 422 if (millisTime >= startMatchingTime && millisTime <= endMatchingTime){
andrew@14 423 bayesianStruct.likelihood.addGaussianShapeFromRealTime(millisTime, onsetLikelihoodWidth, quantity);
andrew@2 424 numberOfMatchesFound++;
andrew@6 425 // printf("Adding Gaussian for onset at time %f offset %f\n", millisTime, bayesianStruct.likelihood.offset);
andrew@2 426
andrew@2 427 }
andrew@2 428 }
andrew@2 429 }
andrew@2 430
andrew@11 431 if (numberOfMatchesFound > 0){
andrew@3 432 // bayesianStruct.likelihood.addConstant((1-likelihoodToNoiseRatio)/bayesianStruct.likelihood.length);
andrew@3 433 bayesianStruct.likelihood.addConstant(numberOfMatchesFound*(1-onsetLikelihoodToNoise)/(onsetLikelihoodToNoise*bayesianStruct.likelihood.length));
andrew@2 434 bayesianStruct.likelihood.renormalise();
andrew@2 435
andrew@8 436 bayesianStruct.calculatePosterior();
andrew@10 437 lastAlignmentTime = timeIn;//use TIMESTAMP
andrew@10 438 recentEventTime[channel] = timeIn;//ofGetElapsedTimeMillis() - startTime;
andrew@11 439
andrew@11 440 recentPriors[channel] = bayesianStruct.prior;
andrew@13 441 projectedPrior = bayesianStruct.prior;
andrew@19 442
andrew@19 443
andrew@19 444 temporal.updateTempo(channel, timeIn);
andrew@11 445 }
andrew@11 446
andrew@11 447
andrew@6 448
andrew@3 449 }
andrew@3 450
andrew@3 451
andrew@3 452
andrew@3 453 void AudioEventMatcher::matchNewPitchEvent(const int& channel, const double& pitchIn, const double& timeIn){
andrew@3 454 //start at beginning but OPTIMISE later
andrew@10 455 /*printf("TIME %i\n", ofGetElapsedTimeMillis());
andrew@10 456 //tmp debug
andrew@10 457 updateBestAlignmentPosition();
andrew@10 458 printf("current alignment best estimate %f\n", currentAlignmentPosition);
andrew@10 459 */
andrew@6 460 bayesianStruct.updateBayesianDistributions(timeIn);//moves the posterior up into prior given the time interval and calculates new offsets
andrew@8 461
andrew@7 462 //set the lielihoods by matching the pitched note
andrew@7 463
andrew@15 464
andrew@3 465 int numberOfMatches = 0;
andrew@3 466 bayesianStruct.likelihood.zero();//set to zero
andrew@18 467 double newOnsetTime;
andrew@18 468 double closestDistance = INFINITY;
andrew@3 469
andrew@3 470 double quantity = 0;
andrew@3 471 if (channel <= recordedTracks.numberOfAudioTracks){
andrew@3 472 for (int i = 0;i < recordedTracks.loadedAudioFiles[channel].fileLoader.onsetDetect.chromaOnsets.size();i++){
andrew@3 473
andrew@3 474 if (checkMatch(recordedTracks.loadedAudioFiles[channel].fileLoader.onsetDetect.chromaOnsets[i].aubioPitch, pitchIn)) {
andrew@18 475 quantity = getPitchDistance(recordedTracks.loadedAudioFiles[channel].fileLoader.onsetDetect.chromaOnsets[i].aubioPitch, pitchIn, 8);
andrew@18 476
andrew@3 477 bayesianStruct.likelihood.addGaussianShapeFromRealTime(recordedTracks.loadedAudioFiles[channel].fileLoader.onsetDetect.chromaOnsets[i].millisTime, 30, quantity);
andrew@3 478 recordedTracks.loadedAudioFiles[channel].fileLoader.onsetDetect.chromaOnsets[i].matched = true;
andrew@3 479 numberOfMatches++;
andrew@3 480 }
andrew@3 481 else{
andrew@3 482 recordedTracks.loadedAudioFiles[channel].fileLoader.onsetDetect.chromaOnsets[i].matched = false;
andrew@3 483 }
andrew@18 484 //checking nearest pitch
andrew@18 485 newOnsetTime = recordedTracks.loadedAudioFiles[channel].fileLoader.onsetDetect.chromaOnsets[i].millisTime;
andrew@18 486 if (abs(newOnsetTime - currentAlignmentPosition) < closestDistance){
andrew@18 487 closestDistance = abs(newOnsetTime - currentAlignmentPosition);
andrew@18 488 pitchOfNearestMatch = recordedTracks.loadedAudioFiles[channel].fileLoader.onsetDetect.chromaOnsets[i].aubioPitch;
andrew@18 489 distanceOfNearestMatch = quantity;
andrew@18 490 }
andrew@3 491
andrew@3 492 }
andrew@3 493 }
andrew@6 494
andrew@8 495
andrew@8 496
andrew@6 497 if (numberOfMatches > 0){//no point updating unless there is a match
andrew@7 498
andrew@6 499 bayesianStruct.likelihood.addConstant(numberOfMatches*(1-pitchLikelihoodToNoise)/(pitchLikelihoodToNoise*bayesianStruct.likelihood.length));
andrew@4 500
andrew@4 501 //tmp set likelihood constant and calculate using that
andrew@6 502 //bayesianStruct.likelihood.zero();
andrew@6 503 //bayesianStruct.likelihood.addConstant(1);
andrew@7 504
andrew@6 505 bayesianStruct.calculatePosterior();
andrew@11 506 lastAlignmentTime = timeIn;//has to use the STAMPED time
andrew@11 507 recentEventTime[channel] = timeIn;
andrew@11 508
andrew@11 509 recentPriors[channel] = bayesianStruct.prior;
andrew@13 510 projectedPrior = bayesianStruct.prior;
andrew@19 511
andrew@19 512 temporal.eventTimes[channel].push_back(timeIn);
andrew@6 513 }
andrew@4 514
andrew@11 515
andrew@1 516 }
andrew@1 517
andrew@3 518 double AudioEventMatcher::getPitchDistance(const double& pitchOne, const double& pitchTwo, const double& scale){
andrew@3 519
andrew@18 520 double scaleFactor = scale * pitchOne / 110.0;
andrew@16 521
andrew@18 522 int multiplicationFactor = 1;
andrew@18 523 if (pitchTwo > 0){
andrew@18 524 int multiplicationFactor = round(pitchOne/pitchTwo);
andrew@18 525 }
andrew@16 526
andrew@18 527 double distance = abs(pitchOne - pitchTwo*multiplicationFactor);
andrew@16 528 if (distance < scaleFactor)
andrew@16 529 distance = 1 - (distance/scaleFactor);
andrew@3 530 else
andrew@3 531 distance = 0;
andrew@3 532
andrew@3 533 // printf("[pitch distance %f vs %f = %f\n", pitchOne, pitchTwo, distance);
andrew@3 534 return distance;
andrew@3 535
andrew@3 536 }
andrew@3 537
andrew@3 538
andrew@3 539 bool AudioEventMatcher::checkMatch(const double& recordedPitch, const double& livePitch){
andrew@18 540
andrew@18 541 if (livePitch > 0){
andrew@18 542 int multiplicationFactor = (int)(round(recordedPitch/livePitch));
andrew@18 543
andrew@18 544 if (abs(recordedPitch - livePitch * multiplicationFactor) < 16)
andrew@3 545 return true;
andrew@3 546 else
andrew@3 547 return false;
andrew@18 548 }else {
andrew@18 549 return false;
andrew@18 550 }
andrew@18 551
andrew@3 552 }
andrew@3 553
andrew@3 554
andrew@1 555
andrew@1 556 void AudioEventMatcher::windowResized(const int& w, const int& h){
andrew@1 557 recordedTracks.windowResized(w,h);
andrew@3 558 bayesTempoWindow.resized(w,h);
andrew@3 559 bayesPositionWindow.resized(w,h);
andrew@3 560 }
andrew@3 561
andrew@10 562 /*
andrew@10 563
andrew@10 564 void printPosteriorMAPinfo(){ //tmp print stuff
andrew@10 565 printf("New pitch MAP post estimate now %i, ", bayesianStruct.posterior.MAPestimate);
andrew@10 566 double tmp = bayesianStruct.posterior.getMAPestimate();
andrew@10 567 printf(" getting it %f and offset %f == %f ms\n", tmp, bayesianStruct.posterior.offset, bayesianStruct.posterior.getIndexInRealTerms(tmp));
andrew@10 568
andrew@10 569 }
andrew@10 570 */
andrew@3 571