00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "PhaseLock.h"
00020 #include "FileName.h"
00021
00022
00023 #define MINIMUMREAL 0.000001 //(0.000001 minimum float recognized)
00024 #define NA -10000.0 //undefined value flag
00025
00026 using namespace std;
00027 using namespace Marsyas;
00028
00029 PhaseLock::PhaseLock(mrs_string name):MarSystem("PhaseLock", name)
00030 {
00031 addControls();
00032 timeElapsed_ = 0;
00033 lastGTBeatTime_ = -1;
00034 inductionFinished_ = false;
00035 gtAfter2ndBeat_ = false;
00036 triggerInduction_ = false;
00037 lastGTIBI_ = 0.0;
00038 }
00039
00040 PhaseLock::PhaseLock(const PhaseLock& a) : MarSystem(a)
00041 {
00042
00043
00044
00045 ctrl_beatHypotheses_ = getctrl("mrs_realvec/beatHypotheses");
00046 ctrl_inductionTime_ = getctrl("mrs_natural/inductionTime");
00047 ctrl_nrPeriodHyps_ = getctrl("mrs_natural/nrPeriodHyps");
00048 ctrl_nrPhasesPerPeriod_ = getctrl("mrs_natural/nrPhasesPerPeriod");
00049 ctrl_scoreFunc_ = getctrl("mrs_string/scoreFunc");
00050 ctrl_gtBeatsFile_ = getctrl("mrs_string/gtBeatsFile");
00051 ctrl_hopSize_ = getctrl("mrs_natural/hopSize");
00052 ctrl_srcFs_ = getctrl("mrs_real/srcFs");
00053 ctrl_mode_ = getctrl("mrs_string/mode");
00054 ctrl_backtrace_ = getctrl("mrs_bool/backtrace");
00055 ctrl_tickCount_ = getctrl("mrs_natural/tickCount");
00056 ctrl_innerMargin_ = getctrl("mrs_real/innerMargin");
00057 ctrl_lftOutterMargin_ = getctrl("mrs_real/lftOutterMargin");
00058 ctrl_rgtOutterMargin_ = getctrl("mrs_real/rgtOutterMargin");
00059 ctrl_corFactor_ = getctrl("mrs_real/corFactor");
00060 ctrl_maxPeriod_ = getctrl("mrs_natural/maxPeriod");
00061 ctrl_minPeriod_ = getctrl("mrs_natural/minPeriod");
00062 ctrl_adjustment_ = getctrl("mrs_natural/adjustment");
00063 ctrl_dumbInduction_ = getctrl("mrs_bool/dumbInduction");
00064 ctrl_inductionOut_ = getctrl("mrs_string/inductionOut");
00065 ctrl_triggerInduction_ = getctrl("mrs_bool/triggerInduction");
00066 ctrl_curBestScore_ = getctrl("mrs_real/curBestScore");
00067 ctrl_triggerBestScoreFactor_ = getctrl("mrs_real/triggerBestScoreFactor");
00068
00069 inductionFinished_ = a.inductionFinished_;
00070 gtInitPhase_ = a.gtInitPhase_;
00071 gtScore_ = a.gtScore_;
00072 gtInitPeriod_ = a.gtInitPeriod_;
00073 gtLastPeriod_ = a.gtLastPeriod_;
00074 srcFs_ = a.srcFs_;
00075 hopSize_ = a.hopSize_;
00076 adjustment_ = a.adjustment_;
00077 gtAfter2ndBeat_ = a.gtAfter2ndBeat_;
00078 triggerInduction_ = a.triggerInduction_;
00079 mode_ = a.mode_;
00080 curBestScore_ = a.curBestScore_;
00081 lastGTBeatTime_ = a.lastGTBeatTime_;
00082 triggerBestScoreFactor_ = a.triggerBestScoreFactor_;
00083 lastGTIBI_ = a.lastGTIBI_;
00084 }
00085
00086 PhaseLock::~PhaseLock()
00087 {
00088 }
00089
00090 MarSystem*
00091 PhaseLock::clone() const
00092 {
00093 return new PhaseLock(*this);
00094 }
00095
00096 void
00097 PhaseLock::addControls()
00098 {
00099
00100 addctrl("mrs_realvec/beatHypotheses", realvec(), ctrl_beatHypotheses_);
00101 addctrl("mrs_natural/inductionTime", -1, ctrl_inductionTime_);
00102 setctrlState("mrs_natural/inductionTime", true);
00103 addctrl("mrs_natural/nrPeriodHyps", 6, ctrl_nrPeriodHyps_);
00104 setctrlState("mrs_natural/nrPeriodHyps", true);
00105 addctrl("mrs_natural/nrPhasesPerPeriod", 20, ctrl_nrPhasesPerPeriod_);
00106 setctrlState("mrs_natural/nrPhasesPerPeriod", true);
00107 addctrl("mrs_string/scoreFunc", "regular", ctrl_scoreFunc_);
00108 setctrlState("mrs_string/scoreFunc", true);
00109 addctrl("mrs_natural/hopSize", -1, ctrl_hopSize_);
00110 setctrlState("mrs_natural/hopSize", true);
00111 addctrl("mrs_real/srcFs", -1.0, ctrl_srcFs_);
00112 setctrlState("mrs_real/srcFs", true);
00113 addctrl("mrs_string/gtBeatsFile", "input.txt", ctrl_gtBeatsFile_);
00114 addctrl("mrs_string/mode", "regular", ctrl_mode_);
00115 setctrlState("mrs_string/mode", true);
00116 addctrl("mrs_bool/backtrace", false, ctrl_backtrace_);
00117 setctrlState("mrs_bool/backtrace", true);
00118 addctrl("mrs_natural/tickCount", 0, ctrl_tickCount_);
00119 addctrl("mrs_real/innerMargin", 3.0, ctrl_innerMargin_);
00120 setctrlState("mrs_real/innerMargin", true);
00121 addctrl("mrs_real/lftOutterMargin", 0.2, ctrl_lftOutterMargin_);
00122 setctrlState("mrs_real/lftOutterMargin", true);
00123 addctrl("mrs_real/rgtOutterMargin", 0.4, ctrl_rgtOutterMargin_);
00124 setctrlState("mrs_real/rgtOutterMargin", true);
00125 addctrl("mrs_real/corFactor", 0.5, ctrl_corFactor_);
00126 setctrlState("mrs_real/corFactor", true);
00127 addctrl("mrs_natural/maxPeriod", -1, ctrl_maxPeriod_);
00128 setctrlState("mrs_natural/maxPeriod", true);
00129 addctrl("mrs_natural/minPeriod", -1, ctrl_minPeriod_);
00130 setctrlState("mrs_natural/minPeriod", true);
00131 addctrl("mrs_natural/adjustment", 0, ctrl_adjustment_);
00132 setctrlState("mrs_natural/adjustment", true);
00133 addctrl("mrs_bool/dumbInduction", false, ctrl_dumbInduction_);
00134 addctrl("mrs_string/inductionOut", "-1", ctrl_inductionOut_);
00135 setctrlState("mrs_string/inductionOut", true);
00136 addctrl("mrs_bool/triggerInduction", false, ctrl_triggerInduction_);
00137 setctrlState("mrs_bool/triggerInduction", true);
00138 addctrl("mrs_real/curBestScore", NA, ctrl_curBestScore_);
00139 setctrlState("mrs_real/curBestScore", true);
00140 addctrl("mrs_real/triggerBestScoreFactor", 0.95, ctrl_triggerBestScoreFactor_);
00141 }
00142
00143 void
00144 PhaseLock::myUpdate(MarControlPtr sender)
00145 {
00146 MRSDIAG("PhaseLock.cpp - PhaseLock:myUpdate");
00147
00148 inductionTime_ = ctrl_inductionTime_->to<mrs_natural>();
00149 nrPeriodHyps_ = ctrl_nrPeriodHyps_->to<mrs_natural>();
00150 nrPhasesPerPeriod_ = ctrl_nrPhasesPerPeriod_->to<mrs_natural>();
00151 scoreFunc_ = ctrl_scoreFunc_->to<mrs_string>();
00152 mode_ = ctrl_mode_->to<mrs_string>();
00153 backtrace_ = ctrl_backtrace_->to<mrs_bool>();
00154 innerMargin_ = (mrs_natural) ctrl_innerMargin_->to<mrs_real>();
00155 lftOutterMargin_ = ctrl_lftOutterMargin_->to<mrs_real>();
00156 rgtOutterMargin_ = ctrl_rgtOutterMargin_->to<mrs_real>();
00157 corFactor_ = ctrl_corFactor_->to<mrs_real>();
00158 maxPeriod_ = ctrl_maxPeriod_->to<mrs_natural>();
00159 minPeriod_ = ctrl_minPeriod_->to<mrs_natural>();
00160 hopSize_ = ctrl_hopSize_->to<mrs_natural>();
00161 srcFs_ = ctrl_srcFs_->to<mrs_real>();
00162 adjustment_ = (mrs_real) ctrl_adjustment_->to<mrs_natural>();
00163 inductionOut_ = ctrl_inductionOut_->to<mrs_string>();
00164 triggerInduction_ = ctrl_triggerInduction_->to<mrs_bool>();
00165 curBestScore_ = ctrl_curBestScore_->to<mrs_real>();
00166 triggerBestScoreFactor_ = ctrl_triggerBestScoreFactor_->to<mrs_real>();
00167
00168 ctrl_onSamples_->setValue(3, NOUPDATE);
00169 ctrl_onObservations_->setValue(nrPeriodHyps_, NOUPDATE);
00170 ctrl_osrate_->setValue(ctrl_israte_, NOUPDATE);
00171
00172 nInitHyp_ = nrPeriodHyps_ * nrPhasesPerPeriod_;
00173
00174 hypSignals_.create(nInitHyp_, inSamples_);
00175 firstBeatPoint_.create(nInitHyp_);
00176 trackingScore_.create(nInitHyp_);
00177 beatCount_.create(nInitHyp_);
00178 maxLocalTrackingScore_.create(nrPeriodHyps_);
00179 maxLocalTrackingScoreInd_.create(nrPeriodHyps_);
00180 initPhases_.create(nrPeriodHyps_);
00181 lastPhases_.create(nrPeriodHyps_);
00182 initPeriods_.create(nrPeriodHyps_);
00183 lastPeriods_.create(nrPeriodHyps_);
00184 metricalSalience_.create(nrPeriodHyps_);
00185 rawScore_.create(nrPeriodHyps_);
00186 rawScoreNorm_.create(nrPeriodHyps_);
00187 metricalRelScore_.create(nrPeriodHyps_);
00188 scoreNorm_.create(nrPeriodHyps_);
00189
00190
00191 for (int i = 0; i < nrPeriodHyps_; i++)
00192 {
00193 maxLocalTrackingScore_(i) = NA;
00194 maxLocalTrackingScoreInd_(i) = -1;
00195 }
00196 }
00197
00198 mrs_realvec
00199 PhaseLock::GTInitialization(realvec& in, realvec& out, mrs_natural gtInitPhase, mrs_natural gtInitPeriod)
00200 {
00201
00202
00203
00204
00205 mrs_realvec initHypothesis(5);
00206 for(int i = 0; i < initHypothesis.getCols(); i++)
00207 initHypothesis(i) = 0;
00208
00209
00210
00211 mrs_real localPeakAmp = in(gtInitPhase);
00212 mrs_natural localPeak = gtInitPhase;
00213
00214 for (int t = gtInitPhase-innerMargin_; t <= gtInitPhase+innerMargin_; t++)
00215 {
00216
00217 if(t >= (inSamples_-1-inductionTime_))
00218 {
00219 if(in(t) > localPeakAmp)
00220 {
00221 localPeakAmp = in(t);
00222 localPeak = t;
00223 }
00224 }
00225 }
00226
00227 initHypothesis(0) = localPeak;
00228 initHypothesis(1) = gtInitPeriod;
00229 initHypothesis(4) += (localPeakAmp * (gtInitPeriod/(mrs_real)maxPeriod_));
00230
00231 outterWinLft_ = (mrs_natural) ceil(gtInitPeriod * lftOutterMargin_);
00232 outterWinRgt_ = (mrs_natural) ceil(gtInitPeriod * rgtOutterMargin_);
00233 mrs_natural beatPoint = gtInitPhase;
00234 mrs_real scoreFraction = 0.0;
00235 mrs_natural error = 0;
00236 mrs_real errorIn = MINIMUMREAL;
00237 mrs_natural period = gtInitPeriod;
00238 mrs_natural periodTmp = 0;
00239 mrs_natural it=1;
00240 do
00241 {
00242
00243
00244 periodTmp = period + ((mrs_natural) ((errorIn*corFactor_) + ((errorIn/abs(errorIn)) * 0.5)));
00245
00246 if(periodTmp >= minPeriod_ && periodTmp <= maxPeriod_)
00247 period = periodTmp;
00248
00249
00250 if((beatPoint + (period + (mrs_natural) ((errorIn*corFactor_) + ((errorIn/abs(errorIn)) * 0.5)))) >= inSamples_)
00251 {
00252 initHypothesis(2) = beatPoint;
00253 initHypothesis(3) = period;
00254 break;
00255 }
00256
00257 beatPoint += (period + (mrs_natural) ((errorIn*corFactor_) + ((errorIn/abs(errorIn)) * 0.5)));
00258 errorIn = MINIMUMREAL;
00259
00260 initHypothesis(2) = beatPoint;
00261 initHypothesis(3) = period;
00262
00263 localPeakAmp = in(beatPoint);
00264 localPeak = beatPoint;
00265
00266 for (int t = beatPoint-outterWinLft_; t <= beatPoint+outterWinRgt_; t++)
00267 {
00268 if(t > (inSamples_-1-inductionTime_) && t < inSamples_)
00269 {
00270
00271 if(in(t) > localPeakAmp)
00272 {
00273 localPeakAmp = in(t);
00274 localPeak = t;
00275
00276
00277 }
00278 }
00279 }
00280
00281
00282
00283 error = localPeak-beatPoint;
00284 if(localPeak >= beatPoint-innerMargin_ && localPeak <= beatPoint+innerMargin_)
00285 {
00286
00287 errorIn = error;
00288 scoreFraction = (mrs_real) ((mrs_real)abs(error) / outterWinRgt_);
00289 initHypothesis(4) += (((1 - scoreFraction) * localPeakAmp) * (period/(mrs_real)maxPeriod_));
00290
00291 if(errorIn == 0)
00292 errorIn = MINIMUMREAL;
00293 }
00294
00295 else
00296 {
00297 scoreFraction = (mrs_real) ((mrs_real)abs(error)/ outterWinRgt_);
00298
00299 initHypothesis(4) += ((-1 * scoreFraction * localPeakAmp) * (period/(mrs_real)maxPeriod_));
00300 }
00301
00302
00303
00304
00305
00306
00307 }while(beatPoint < inSamples_);
00308
00309 return initHypothesis;
00310 }
00311
00312
00313 mrs_realvec
00314 PhaseLock::readGTFile(mrs_string gtFilePath)
00315 {
00316 mrs_realvec gtHypotheses(4);
00317 mrs_real gtBeatTimeInit1 = NA, gtBeatTimeInit2 = NA, gtBeatTimeLast1 = NA, gtBeatTimeLast2 = NA;
00318
00319
00320 inStream_.open(gtFilePath.c_str());
00321
00322 getline (inStream_, line_);
00323
00324
00325 mrs_natural pos0, pos1, pos2;
00326 pos0 = (mrs_natural) line_.find_first_of(" ", 0);
00327 pos1 = (mrs_natural) line_.find_first_of(" ", pos0+1);
00328 pos2 = (mrs_natural) line_.find_first_of(" ", pos1+1);
00329
00330 if(pos1 >= 0)
00331 {
00332
00333 if(lastGTBeatTime_ < 0)
00334 {
00335 std::istringstream iss(line_);
00336 char c[10];
00337 while (iss >> c)
00338 {
00339
00340 lastGTBeatTime_ = atof(c);
00341 }
00342 iss.clear();
00343
00344 }
00345
00346
00347
00348 mrs_real initBeatPoint = (((mrs_real)((timeElapsed_-inductionTime_) * hopSize_)) - adjustment_) / srcFs_;
00349
00350
00351
00352 mrs_real lastGTBeatTimeInit1 = 0.0;
00353 mrs_real lastGTBeatTimeLast1 = 0.0;
00354 do
00355 {
00356
00357
00358
00359
00360
00361 if((gtBeatTimeInit1 == lastGTBeatTime_) && (gtBeatTimeInit1 < initBeatPoint))
00362 {
00363 mrs_real initGTIBI = abs(gtBeatTimeInit1 - lastGTBeatTimeInit1);
00364 mrs_natural k = (mrs_natural) ceil((initBeatPoint+MINIMUMREAL - gtBeatTimeInit1) / initGTIBI);
00365 gtBeatTimeInit1 = gtBeatTimeInit1 + (k * initGTIBI);
00366 gtBeatTimeInit2 = gtBeatTimeInit2 + initGTIBI;
00367
00368 break;
00369 }
00370 else
00371 {
00372
00373 if(gtBeatTimeInit1 > initBeatPoint)
00374 break;
00375 else
00376 {
00377 lastGTBeatTimeInit1 = gtBeatTimeInit1;
00378
00379 gtBeatTimeInit1 = strtod(line_.substr(pos0+1, pos1).c_str(), NULL);
00380 gtBeatTimeInit2 = strtod(line_.substr(pos1+1, pos2).c_str(), NULL);
00381
00382
00383 pos0 = (mrs_natural) line_.find_first_of(" ", pos0+1);
00384 pos1 = (mrs_natural) line_.find_first_of(" ", pos0+1);
00385 }
00386 }
00387
00388 }while(gtBeatTimeInit1 <= initBeatPoint);
00389
00390
00391
00392
00393
00394 mrs_real lastBeatPoint = (((mrs_real)(timeElapsed_ * hopSize_)) - adjustment_) / srcFs_;
00395 if((gtBeatTimeInit2 < lastGTBeatTime_) && (gtBeatTimeInit2 > initBeatPoint))
00396 {
00397 do
00398 {
00399
00400
00401 if((gtBeatTimeLast2 == lastGTBeatTime_) && (gtBeatTimeLast1 < lastBeatPoint))
00402 {
00403
00404
00405
00406
00407 lastGTIBI_ = abs(gtBeatTimeLast2 - gtBeatTimeLast1);
00408 lastGTBeatTimeLast1 = gtBeatTimeLast1;
00409 gtBeatTimeLast1 = gtBeatTimeLast2;
00410 gtBeatTimeLast2 = gtBeatTimeLast1 + lastGTIBI_;
00411 }
00412
00413
00414
00415 if((gtBeatTimeLast1 == lastGTBeatTime_) && (gtBeatTimeLast1 < lastBeatPoint))
00416 {
00417
00418
00419
00420 mrs_natural k = (mrs_natural) ceil((lastBeatPoint+MINIMUMREAL - gtBeatTimeLast1) / lastGTIBI_);
00421 gtBeatTimeLast1 = gtBeatTimeLast1 + (k * lastGTIBI_);
00422 gtBeatTimeLast2 = gtBeatTimeLast1 + lastGTIBI_;
00423
00424 break;
00425 }
00426 else
00427 {
00428
00429 if(gtBeatTimeLast1 > lastBeatPoint)
00430 break;
00431 else
00432 {
00433 lastGTBeatTimeLast1 = gtBeatTimeLast1;
00434
00435
00436 pos0 = (mrs_natural) line_.find_first_of(" ", pos0+1);
00437 pos1 = (mrs_natural) line_.find_first_of(" ", pos0+1);
00438
00439 gtBeatTimeLast1 = strtod(line_.substr(pos0+1, pos1).c_str(), NULL);
00440 gtBeatTimeLast2 = strtod(line_.substr(pos1+1, pos2).c_str(), NULL);
00441
00442 if(gtBeatTimeLast2 > gtBeatTimeLast1)
00443 lastGTIBI_ = abs(gtBeatTimeLast2 - gtBeatTimeLast1);
00444
00445
00446
00447 }
00448 }
00449
00450 }while(gtBeatTimeLast1 <= lastBeatPoint);
00451 }
00452 else
00453 {
00454
00455 cerr << "Reached end of ground-truth file...Last GT values propagated from the last hypotheses given by the GT file!" << endl;
00456
00457 mrs_natural k = (mrs_natural) ceil((lastBeatPoint+MINIMUMREAL - gtBeatTimeInit1) / lastGTIBI_);
00458 gtBeatTimeLast1 = gtBeatTimeInit1 + (k * lastGTIBI_);
00459 gtBeatTimeLast2 = gtBeatTimeLast1 + lastGTIBI_;
00460 }
00461
00462
00463
00464
00465
00466
00467
00468 }
00469
00470 gtHypotheses(0) = gtBeatTimeInit1;
00471 gtHypotheses(1) = gtBeatTimeInit2;
00472 gtHypotheses(2) = gtBeatTimeLast1;
00473 gtHypotheses(3) = gtBeatTimeLast2;
00474
00475 return gtHypotheses;
00476 }
00477
00478
00479 void
00480 PhaseLock::handleGTHypotheses(realvec& in, realvec& out,mrs_string gtFilePath, mrs_realvec gtHypotheses)
00481 {
00482 mrs_real gtBeatTimeInit1 = gtHypotheses(0);
00483 mrs_real gtBeatTimeInit2 = gtHypotheses(1);
00484 mrs_real gtBeatTimeLast1 = gtHypotheses(2);
00485 mrs_real gtBeatTimeLast2 = gtHypotheses(3);
00486
00487
00488 gtInitPeriod_ = (mrs_natural) (((abs(gtBeatTimeInit2 - gtBeatTimeInit1) * srcFs_) / hopSize_) + 0.5);
00489 gtInitPhase_ = (mrs_natural) ceil(((gtBeatTimeInit1 * srcFs_) + adjustment_) / hopSize_);
00490
00491
00492
00493 gtLastPeriod_ = (mrs_natural) (((abs(gtBeatTimeLast2 - gtBeatTimeLast1) * srcFs_) / hopSize_) + 0.5);
00494 gtLastPhase_ = (mrs_natural) ceil(((gtBeatTimeLast1 * srcFs_) + adjustment_) / hopSize_);
00495
00496
00497
00498
00499
00500
00501
00502
00503 mrs_natural phaseAccAdjust = (inSamples_ - 1 - timeElapsed_) + gtInitPhase_;
00504
00505
00506
00507 mrs_natural k = (mrs_natural) (((mrs_real)((inSamples_-1-inductionTime_)-phaseAccAdjust)/(mrs_real)gtInitPeriod_)+MINIMUMREAL);
00508 k += 1;
00509 phaseAccAdjust = ((mrs_natural) phaseAccAdjust) + ((mrs_natural) (k * gtInitPeriod_));
00510
00511 if(strcmp(mode_.c_str(), "2b2") == 0 || strcmp(mode_.c_str(), "2b") == 0)
00512 {
00513 mrs_realvec gtInitHypothesis = GTInitialization(in, out, phaseAccAdjust, gtInitPeriod_);
00514
00515 if(curBestScore_ == NA)
00516 {
00517 gtScore_ = gtInitHypothesis(4);
00518
00519 }
00520 else
00521 {
00522 if(curBestScore_ > 0)
00523 gtScore_ = curBestScore_ * triggerBestScoreFactor_;
00524 else
00525 gtScore_ = curBestScore_ / triggerBestScoreFactor_;
00526
00527
00528 }
00529
00530 if(strcmp(mode_.c_str(), "2b") == 0)
00531 {
00532 gtInitPeriod_ = (mrs_natural) gtInitHypothesis(1);
00533 gtLastPeriod_ = (mrs_natural) gtInitHypothesis(3);
00534 gtInitPhase_ = (mrs_natural) gtInitHypothesis(0) - (inSamples_-1 - timeElapsed_);
00535 gtLastPhase_ = (mrs_natural) gtInitHypothesis(2) - (inSamples_-1 - timeElapsed_) + gtLastPeriod_;
00536
00537
00538
00539
00540 if(gtLastPhase_ <= timeElapsed_)
00541 gtLastPhase_ += gtLastPeriod_;
00542 }
00543
00544
00545
00546
00547
00548 cerr << "\nInduction replaced by IBI given by two beats from ground-truth file at: " << gtFilePath << endl;
00549 }
00550
00551
00552
00553
00554 if(strcmp(mode_.c_str(), "1b") == 0 || strcmp(mode_.c_str(), "1b1") == 0)
00555 {
00556
00557 beatHypotheses_ = ctrl_beatHypotheses_->to<mrs_realvec>();
00558
00559 mrs_realvec lastBeatPoint(nInitHyp_);
00560 mrs_realvec lastLocalPeriod(nInitHyp_);
00561 for(int i = 0; i < nrPeriodHyps_; i++)
00562 {
00563 mrs_natural phase = phaseAccAdjust;
00564 mrs_natural period = (mrs_natural) beatHypotheses_(i*nrPhasesPerPeriod_, 0);
00565
00566 initPeriods_(i) = period;
00567 if(period > 1)
00568 {
00569 mrs_realvec gtInitHypothesis = GTInitialization(in, out, phase, period);
00570 lastPeriods_(i) = (mrs_natural) gtInitHypothesis(3);
00571
00572 if(strcmp(mode_.c_str(), "1b") == 0)
00573 {
00574 initPhases_(i) = (mrs_natural) gtInitHypothesis(0) - (inSamples_-1 - timeElapsed_);
00575 lastPhases_(i) = (mrs_natural) gtInitHypothesis(2) - (inSamples_-1 - timeElapsed_) + lastPeriods_(i);
00576
00577
00578 if(lastPhases_(i) <= timeElapsed_)
00579 lastPhases_(i) += lastPeriods_(i);
00580 }
00581
00582 trackingScore_(i) = gtInitHypothesis(4);
00583
00584 rawScore_(i) = trackingScore_(i);
00585 maxLocalTrackingScore_(i) = trackingScore_(i);
00586
00587
00588
00589
00590 }
00591 }
00592
00593 mrs_real maxMetricalRelScore = NA;
00594 mrs_natural avgPeriod = 0;
00595 mrs_real avgLocalTrackingScore_ = 0.0;
00596 mrs_real maxGlobalTrackingScore_ = NA;
00597 mrs_natural maxMetricalRelScoreInd = -1;
00598 for(int i = 0; i < nrPeriodHyps_; i++)
00599 {
00600 if(initPeriods_(i) > MINIMUMREAL)
00601 {
00602 metricalRelScore_(i) = calcRelationalScore(i, rawScore_);
00603
00604 if(metricalRelScore_(i) > maxMetricalRelScore)
00605 {
00606 maxMetricalRelScore = metricalRelScore_(i);
00607 maxMetricalRelScoreInd = i;
00608 }
00609
00610 if(maxLocalTrackingScore_(i) > maxGlobalTrackingScore_)
00611 maxGlobalTrackingScore_ = maxLocalTrackingScore_(i);
00612
00613 avgLocalTrackingScore_ += maxLocalTrackingScore_(i);
00614 avgPeriod += (mrs_natural) initPeriods_(i);
00615
00616
00617
00618
00619
00620 }
00621 }
00622
00623 for(int i = 0; i < nrPeriodHyps_; i++)
00624 {
00625 if(strcmp(mode_.c_str(), "1b") == 0)
00626 {
00627 if(backtrace_)
00628 {
00629
00630 out(i, 0) = initPeriods_(i);
00631
00632 out(i, 1) = initPhases_(i);
00633
00634 if(i == 0)
00635 {
00636 if(gtAfter2ndBeat_)
00637 cerr << "\nInitial phase backtraced from second beat in ground-truth file at: " << gtFilePath << endl;
00638 else
00639 cerr << "\nInitial phase backtraced from first beat in ground-truth file at: " << gtFilePath << endl;
00640
00641 cerr << "Initial phase: " << (((initPhases_(i) * hopSize_) - adjustment_) / srcFs_) << "s" << endl;
00642 }
00643 }
00644 else
00645 {
00646
00647 out(i, 0) = lastPeriods_(i);
00648
00649 out(i, 1) = lastPhases_(i);
00650
00651 if(i == 0)
00652 {
00653 if(gtAfter2ndBeat_)
00654 cerr << "\nInitial phase adjusted from second beat in ground-truth file at: " << gtFilePath << endl;
00655 else
00656 cerr << "\nInitial phase adjusted from first beat in ground-truth file at: " << gtFilePath << endl;
00657
00658 cerr << "Initial phase: " << (((lastPhases_(i) * hopSize_) - adjustment_) / srcFs_) << "s" << endl;
00659 }
00660 }
00661 }
00662 else if(strcmp(mode_.c_str(), "1b1") == 0)
00663 {
00664 if(backtrace_)
00665 {
00666
00667 out(i, 0) = initPeriods_(i);
00668
00669 out(i, 1) = gtInitPhase_;
00670
00671 if(i == 0)
00672 {
00673 if(gtAfter2ndBeat_)
00674 cerr << "\nInitial phase replaced by second beat in ground-truth file at: " << gtFilePath << endl;
00675 else
00676 cerr << "\nInitial phase replaced by first beat in ground-truth file at: " << gtFilePath << endl;
00677
00678 cerr << "Initial phase: " << (((gtInitPhase_ * hopSize_) - adjustment_) / srcFs_) << "s" << endl;
00679 }
00680 }
00681 else
00682 {
00683
00684 out(i, 0) = lastPeriods_(i);
00685
00686 out(i, 1) = gtLastPhase_;
00687
00688 if(i == 0)
00689 {
00690 cerr << "\nInitial phase replaced by first beat in ground-truth file at: " << gtFilePath << endl;
00691 cerr << "Initial phase: " << (((gtLastPhase_ * hopSize_) - adjustment_) / srcFs_) << "s" << endl;
00692 }
00693 }
00694 }
00695
00696
00697 mrs_real finalScore;
00698 if(!dumbInduction_)
00699 {
00700 mrs_real metricalRelFraction = 0.0;
00701 metricalRelFraction = (metricalRelScore_(i) / maxMetricalRelScore);
00702
00703
00704 if(metricalRelScore_(i) < 0 && maxMetricalRelScore > 0 && maxGlobalTrackingScore_ > 0
00705 && abs(metricalRelScore_(i)) > maxMetricalRelScore)
00706 metricalRelFraction = -1;
00707 if(metricalRelScore_(i) < 0 && maxMetricalRelScore < 0 && maxGlobalTrackingScore_ > 0)
00708 metricalRelFraction = (maxMetricalRelScore / metricalRelScore_(i));
00709
00710 mrs_real finalScore;
00711
00712 if(curBestScore_ == NA)
00713 finalScore = metricalRelFraction * maxGlobalTrackingScore_;
00714 else
00715 {
00716 if(curBestScore_ > 0)
00717 finalScore = (metricalRelFraction * curBestScore_) * triggerBestScoreFactor_;
00718 else
00719 finalScore = (metricalRelFraction * curBestScore_) / triggerBestScoreFactor_;
00720
00721
00722 }
00723
00724
00725
00726
00727
00728
00729
00730 }
00731 else
00732 {
00733
00734
00735 if(curBestScore_ == NA)
00736 finalScore = rawScore_(i);
00737 else
00738 {
00739
00740
00741
00742
00743 mrs_real rawScoreFraction;
00744
00745 if(initPeriods_(i) > MINIMUMREAL && initPhases_(i) > MINIMUMREAL)
00746 {
00747 if(rawScore_(i) == 0.0) rawScore_(i) = MINIMUMREAL;
00748 if(maxGlobalTrackingScore_ == 0.0) maxGlobalTrackingScore_ = MINIMUMREAL;
00749 }
00750
00751
00752 if((rawScore_(i) < 0 && maxGlobalTrackingScore_ > 0) && (fabs(rawScore_(i)) < 1 || maxGlobalTrackingScore_ < 1))
00753 rawScoreFraction = rawScore_(i) * maxGlobalTrackingScore_;
00754 else
00755 {
00756 if(fabs(rawScore_(i)) < maxGlobalTrackingScore_)
00757 rawScoreFraction = rawScore_(i) / maxGlobalTrackingScore_;
00758 else
00759 rawScoreFraction = maxGlobalTrackingScore_ / rawScore_(i);
00760 }
00761
00762 if(curBestScore_ > 0)
00763 finalScore = (rawScoreFraction * curBestScore_) * triggerBestScoreFactor_;
00764 else
00765 finalScore = (rawScoreFraction * curBestScore_) / triggerBestScoreFactor_;
00766
00767
00768
00769
00770 }
00771
00772
00773 ctrl_dumbInduction_->setValue(false);
00774 }
00775
00776
00777 if(strcmp(scoreFunc_.c_str(), "correlation") == 0 || strcmp(scoreFunc_.c_str(), "squareCorr") == 0 )
00778 finalScore *= 5;
00779
00780 out(i, 2) = finalScore;
00781 }
00782 }
00783
00784
00785
00786
00787
00788 inStream_.close();
00789 }
00790
00791 mrs_natural
00792 PhaseLock::metricalRelation(mrs_real period1, mrs_real period2)
00793 {
00794 mrs_real periodA = (period1 > period2 ? period1 : period2);
00795 mrs_real periodB = (period1 > period2 ? period2 : period1);
00796 mrs_real multiple = periodA / periodB;
00797 mrs_real tolerance = 0.15;
00798
00799 mrs_natural floorMultiple = (mrs_natural) floor(multiple);
00800 mrs_natural ceilMultiple = (mrs_natural) ceil(multiple);
00801 mrs_natural rel = 0;
00802
00803
00804
00805
00806
00807
00808 if(multiple - tolerance <= floorMultiple)
00809 {
00810 if(floorMultiple == 2 || floorMultiple == 4)
00811 rel = 6 - floorMultiple;
00812 else if(floorMultiple == 3)
00813 rel = 3;
00814 else if(floorMultiple >= 5 && floorMultiple <= 8)
00815 rel = 1;
00816 else
00817 rel = 0;
00818 }
00819
00820 else if(multiple + tolerance >= ceilMultiple)
00821 {
00822 if(ceilMultiple == 2 || ceilMultiple == 4)
00823 rel = 6 - ceilMultiple;
00824 else if(ceilMultiple == 3)
00825 rel = 3;
00826 else if(ceilMultiple >= 5 && ceilMultiple <= 8)
00827 rel = 1;
00828 else rel = 0;
00829 }
00830
00831 else rel = 0;
00832
00833 return rel;
00834 }
00835
00836 mrs_real
00837 PhaseLock::calcRelationalScore(mrs_natural i, mrs_realvec rawScoreVec)
00838 {
00839 mrs_real score = 0.0;
00840 score = 2*5*rawScoreVec(i);
00841
00842 mrs_natural metricalRel;
00843 for(int j = 0; j < nrPeriodHyps_; j++)
00844 {
00845 if(j != i && initPeriods_(i) > MINIMUMREAL && initPeriods_(j) > MINIMUMREAL)
00846 {
00847 metricalRel = metricalRelation((mrs_natural)initPeriods_(i), (mrs_natural)initPeriods_(j));
00848 score += rawScoreVec(j) * metricalRel;
00849 }
00850 }
00851
00852 return score;
00853 }
00854
00855
00856 void
00857 PhaseLock::regularFunc(realvec& in, realvec& out)
00858 {
00859
00860 beatHypotheses_ = ctrl_beatHypotheses_->to<mrs_realvec>();
00861
00862
00863
00864
00865
00866 mrs_realvec lastBeatPoint(nInitHyp_);
00867 mrs_realvec lastLocalPeriod(nInitHyp_);
00868 for(int h = 0; h < nInitHyp_; h++)
00869 {
00870 mrs_natural phase = (mrs_natural) beatHypotheses_(h, 1);
00871 mrs_natural period = (mrs_natural) beatHypotheses_(h, 0);
00872 if(phase > 0 && period > 1)
00873 {
00874
00875
00876
00877
00878 mrs_natural k = (mrs_natural) (((mrs_real)((inSamples_-1-inductionTime_)-phase)/(mrs_real)period)+MINIMUMREAL);
00879
00880 if((phase + ((mrs_natural) ((k+1) * period))) < inSamples_)
00881 k += 1;
00882
00883 firstBeatPoint_(h) = phase + ((mrs_natural) (k * period));
00884
00885
00886
00887
00888 beatCount_(h) = 1;
00889 mrs_real localPeakAmp = in((mrs_natural)firstBeatPoint_(h));
00890 mrs_natural localPeak = (mrs_natural) firstBeatPoint_(h);
00891 mrs_natural errorTmp = 0;
00892 for (int t = (mrs_natural)firstBeatPoint_(h)-innerMargin_; t <= (mrs_natural)firstBeatPoint_(h)+innerMargin_; t++)
00893 {
00894
00895 if(t >= (inSamples_-1-inductionTime_))
00896 {
00897 if(in(t) > localPeakAmp)
00898 {
00899
00900 localPeakAmp = in(t);
00901 localPeak = t;
00902 }
00903 }
00904 }
00905 errorTmp = (mrs_natural) firstBeatPoint_(h) - localPeak;
00906
00907
00908
00909 trackingScore_(h) += localPeakAmp * ((mrs_real)period / (mrs_real)maxPeriod_);
00910
00911
00912
00913
00914
00915
00916
00917
00918 firstBeatPoint_(h) = localPeak;
00919
00920
00921 hypSignals_(h, (mrs_natural) firstBeatPoint_(h)) = 1.0;
00922
00923 outterWinLft_ = (mrs_natural) ceil(period * lftOutterMargin_);
00924 outterWinRgt_ = (mrs_natural) ceil(period * rgtOutterMargin_);
00925 mrs_natural beatPoint = (mrs_natural) firstBeatPoint_(h);
00926 mrs_real scoreFraction = 0.0;
00927 mrs_natural error = 0;
00928 mrs_real errorIn = MINIMUMREAL;
00929 mrs_natural periodTmp = 0;
00930
00931 do
00932 {
00933
00934
00935 periodTmp = period + ((mrs_natural) ((errorIn*corFactor_) + ((errorIn/abs(errorIn)) * 0.5)));
00936
00937
00938
00939 if(periodTmp >= minPeriod_ && periodTmp <= maxPeriod_)
00940 period = periodTmp;
00941
00942
00943 if((beatPoint + (period + (mrs_natural) ((errorIn*corFactor_) + ((errorIn/abs(errorIn)) * 0.5)))) >= inSamples_)
00944 {
00945 lastBeatPoint(h) = beatPoint;
00946 lastLocalPeriod(h) = period;
00947 break;
00948 }
00949
00950 beatPoint += (period + (mrs_natural) ((errorIn*corFactor_) + ((errorIn/abs(errorIn)) * 0.5)));
00951 errorIn = MINIMUMREAL;
00952
00953
00954
00955 lastBeatPoint(h) = beatPoint;
00956 lastLocalPeriod(h) = period;
00957
00958 localPeakAmp = in(beatPoint);
00959 localPeak = beatPoint;
00960 for (int t = beatPoint-outterWinLft_; t <= beatPoint+outterWinRgt_; t++)
00961 {
00962 if(t > (inSamples_-1-inductionTime_) && t < inSamples_)
00963 {
00964 if(in(t) > localPeakAmp)
00965 {
00966 localPeakAmp = in(t);
00967 localPeak = t;
00968 }
00969 }
00970 }
00971
00972
00973
00974 error = localPeak-beatPoint;
00975 if(localPeak >= beatPoint-innerMargin_ && localPeak <= beatPoint+innerMargin_)
00976 {
00977
00978 errorIn = error;
00979 scoreFraction = (mrs_real) ((mrs_real)abs(error) / outterWinRgt_);
00980 trackingScore_(h) += (((1 - scoreFraction) * localPeakAmp) * ((mrs_real)period / (mrs_real)maxPeriod_));
00981
00982 if(errorIn == 0)
00983 errorIn = MINIMUMREAL;
00984
00985
00986 }
00987
00988 else
00989 {
00990 scoreFraction = (mrs_real) ((mrs_real)abs(error)/ outterWinRgt_);
00991 trackingScore_(h) += ((-1 * scoreFraction * localPeakAmp) * ((mrs_real)period / (mrs_real)maxPeriod_));
00992
00993
00994 }
00995
00996 hypSignals_(h, beatPoint) = 1.0;
00997 beatCount_(h)++;
00998
00999
01000
01001
01002
01003
01004 }while(beatPoint < inSamples_);
01005 }
01006 }
01007
01008
01009
01010
01011 for(int i = 0; i < nrPeriodHyps_; i++)
01012 {
01013
01014
01015 if(beatHypotheses_(i*nrPhasesPerPeriod_, 0) > 0)
01016 {
01017 for(int j = i*nrPhasesPerPeriod_; j < (i*nrPhasesPerPeriod_)+nrPhasesPerPeriod_; j++)
01018 {
01019
01020 if(beatHypotheses_(j, 1) > 0)
01021 {
01022 if(trackingScore_(j) > maxLocalTrackingScore_(i))
01023 {
01024 maxLocalTrackingScore_(i) = trackingScore_(j);
01025 maxLocalTrackingScoreInd_(i) = j;
01026 }
01027 }
01028 }
01029
01030 initPeriods_(i) = beatHypotheses_((mrs_natural)maxLocalTrackingScoreInd_(i), 0);
01031 lastPeriods_(i) = lastLocalPeriod((mrs_natural)maxLocalTrackingScoreInd_(i));
01032
01033
01034 initPhases_(i) = firstBeatPoint_((mrs_natural)maxLocalTrackingScoreInd_(i)) - (inSamples_-1 - timeElapsed_);
01035 lastPhases_(i) = lastBeatPoint((mrs_natural)maxLocalTrackingScoreInd_(i)) - (inSamples_-1 - timeElapsed_) + lastPeriods_(i);
01036
01037 if(lastPhases_(i) <= timeElapsed_)
01038 lastPhases_(i) += lastPeriods_(i);
01039
01040
01041
01042 rawScore_(i) = maxLocalTrackingScore_(i);
01043 metricalSalience_(i) = beatHypotheses_((mrs_natural)maxLocalTrackingScoreInd_(i), 2);
01044
01045
01046
01047
01048
01049 }
01050 }
01051
01052 mrs_real maxMetricalRelScore = NA;
01053 mrs_natural avgPeriod = 0;
01054 mrs_real avgLocalTrackingScore_ = 0.0;
01055 mrs_real maxGlobalTrackingScore_ = NA;
01056 mrs_natural maxMetricalRelScoreInd = -1;
01057 for(int i = 0; i < nrPeriodHyps_; i++)
01058 {
01059
01060 if(initPeriods_(i) > MINIMUMREAL && initPhases_(i) > MINIMUMREAL)
01061 {
01062 metricalRelScore_(i) = calcRelationalScore(i, rawScore_);
01063
01064 if(metricalRelScore_(i) > maxMetricalRelScore)
01065 {
01066 maxMetricalRelScore = metricalRelScore_(i);
01067 maxMetricalRelScoreInd = i;
01068 }
01069
01070 if(maxLocalTrackingScore_(i) > maxGlobalTrackingScore_)
01071 maxGlobalTrackingScore_ = maxLocalTrackingScore_(i);
01072
01073 avgLocalTrackingScore_ += maxLocalTrackingScore_(i);
01074 avgPeriod += (mrs_natural) initPeriods_(i);
01075
01076
01077
01078
01079 }
01080 }
01081
01082
01083 if(strcmp(inductionOut_.c_str(), "-1") != 0 && !dumbInduction_)
01084 {
01085 ostringstream ossInitPeriod, ossLastPeriod;
01086 fstream outStream;
01087 mrs_natural indInitPeriod = (mrs_natural) (((60.0 / initPeriods_(maxMetricalRelScoreInd))
01088 * (srcFs_ / hopSize_)) + 0.5);
01089 mrs_natural indLastPeriod = (mrs_natural) (((60.0 / lastPeriods_(maxMetricalRelScoreInd))
01090 * (srcFs_ / hopSize_)) + 0.5);
01091
01092
01093 ossInitPeriod << inductionOut_ << "_indInitTempo.txt";
01094 outStream.open(ossInitPeriod.str().c_str(), ios::out|ios::trunc);
01095 outStream << indInitPeriod;
01096 outStream.close();
01097
01098
01099 ossLastPeriod << inductionOut_ << "_indLastTempo.txt";
01100 outStream.open(ossLastPeriod.str().c_str(), ios::out|ios::trunc);
01101 outStream << indLastPeriod;
01102 outStream.close();
01103 }
01104
01105 avgLocalTrackingScore_ /= nrPeriodHyps_;
01106 avgPeriod /= nrPeriodHyps_;
01107
01108 for(int i = 0; i < nrPeriodHyps_; i++)
01109 {
01110 if(backtrace_)
01111 {
01112
01113 out(i, 0) = initPeriods_(i);
01114
01115 out(i, 1) = initPhases_(i);
01116 }
01117 else
01118 {
01119
01120 out(i, 0) = lastPeriods_(i);
01121
01122 out(i, 1) = lastPhases_(i);
01123 }
01124
01125
01126 mrs_real finalScore;
01127 if(!dumbInduction_)
01128 {
01129 mrs_real metricalRelFraction = 0.0;
01130 metricalRelFraction = (metricalRelScore_(i) / maxMetricalRelScore);
01131
01132
01133 if(metricalRelScore_(i) < 0 && maxMetricalRelScore > 0 && maxGlobalTrackingScore_ > 0
01134 && abs(metricalRelScore_(i)) > maxMetricalRelScore)
01135 metricalRelFraction = -1;
01136 if(metricalRelScore_(i) < 0 && maxMetricalRelScore < 0 && maxGlobalTrackingScore_ > 0)
01137 metricalRelFraction = (maxMetricalRelScore / metricalRelScore_(i));
01138
01139
01140 if(curBestScore_ == NA)
01141 finalScore = metricalRelFraction * maxGlobalTrackingScore_;
01142 else
01143 {
01144 if(curBestScore_ > 0)
01145 finalScore = (metricalRelFraction * curBestScore_) * triggerBestScoreFactor_;
01146 else
01147 finalScore = (metricalRelFraction * curBestScore_) / triggerBestScoreFactor_;
01148
01149
01150
01151 }
01152
01153
01154
01155
01156
01157
01158
01159 }
01160 else
01161 {
01162
01163
01164 if(curBestScore_ == NA)
01165 finalScore = rawScore_(i);
01166 else
01167 {
01168
01169
01170
01171
01172 mrs_real rawScoreFraction;
01173
01174 if(initPeriods_(i) > MINIMUMREAL && initPhases_(i) > MINIMUMREAL)
01175 {
01176 if(rawScore_(i) == 0.0) rawScore_(i) = MINIMUMREAL;
01177 if(maxGlobalTrackingScore_ == 0.0) maxGlobalTrackingScore_ = MINIMUMREAL;
01178 }
01179
01180
01181 if((rawScore_(i) < 0 && maxGlobalTrackingScore_ > 0) && (fabs(rawScore_(i)) < 1 || maxGlobalTrackingScore_ < 1))
01182 rawScoreFraction = rawScore_(i) * maxGlobalTrackingScore_;
01183 else
01184 {
01185 if(fabs(rawScore_(i)) < maxGlobalTrackingScore_)
01186 rawScoreFraction = rawScore_(i) / maxGlobalTrackingScore_;
01187 else
01188 rawScoreFraction = maxGlobalTrackingScore_ / rawScore_(i);
01189 }
01190
01191 if(curBestScore_ > 0)
01192 finalScore = (rawScoreFraction * curBestScore_) * triggerBestScoreFactor_;
01193 else
01194 finalScore = (rawScoreFraction * curBestScore_) / triggerBestScoreFactor_;
01195
01196
01197
01198
01199 }
01200
01201
01202 ctrl_dumbInduction_->setValue(false);
01203 }
01204
01205
01206 if(strcmp(scoreFunc_.c_str(), "correlation") == 0 || strcmp(scoreFunc_.c_str(), "squareCorr") == 0 )
01207 finalScore *= 5;
01208
01209 out(i, 2) = finalScore;
01210
01211
01212 }
01213
01214
01215
01216
01217
01218 }
01219
01220 void
01221 PhaseLock::forceInitPeriods(mrs_string mode)
01222 {
01223 cerr << "\nInitial period(s) given by ground-truth file at: " << ctrl_gtBeatsFile_->to<mrs_string>() << endl;
01224
01225 beatHypotheses_ = ctrl_beatHypotheses_->to<mrs_realvec>();
01226 if(strcmp(mode_.c_str(), "p") == 0)
01227 {
01228
01229 if(backtrace_)
01230 {
01231 for(int h = 0; h < nInitHyp_; h++)
01232 beatHypotheses_(h, 0) = (mrs_real) gtInitPeriod_;
01233
01234 cerr << "Period as ibi of given first 2 beats: ";
01235 cerr << ((60.0/gtInitPeriod_)*(srcFs_ / hopSize_)) << " (BPMs)" << endl;
01236 }
01237 else
01238 {
01239 for(int h = 0; h < nInitHyp_; h++)
01240 beatHypotheses_(h, 0) = (mrs_real) gtLastPeriod_;
01241
01242 cerr << "Period as ibi of given last 2 beats: ";
01243 cerr << ((60.0/gtLastPeriod_)*(srcFs_ / hopSize_)) << " (BPMs)" << endl;
01244 }
01245
01246 nrPeriodHyps_ = 1;
01247 }
01248 else if(strcmp(mode_.c_str(), "p_mr") == 0)
01249 {
01250 nrPeriodHyps_ = 5;
01251 nInitHyp_ = nrPeriodHyps_ * nrPhasesPerPeriod_;
01252 mrs_natural p = 0, ph = 0;
01253
01254 mrs_real gtPeriod;
01255 if(backtrace_)
01256 {
01257 gtPeriod = gtInitPeriod_;
01258 cerr << "Periods as ibi of given first 2 beats + others metrical related: ";
01259 }
01260 else
01261 {
01262 gtPeriod = gtLastPeriod_;
01263 cerr << "Periods as ibi of given last 2 beats + others metrical related: ";
01264 }
01265
01266 double periods[] = {gtPeriod, (mrs_natural) (gtPeriod * 2.0), (mrs_natural) (gtPeriod * 0.5),
01267 (mrs_natural) (gtPeriod * 3.0), (mrs_natural) (gtPeriod * 0.333)};
01268
01269 for(int i = 0; i < nrPeriodHyps_; i++)
01270 {
01271 for(int h = i*nrPhasesPerPeriod_; h < (i*nrPhasesPerPeriod_)+nrPhasesPerPeriod_; h++)
01272 {
01273 beatHypotheses_(h, 0) = periods[p];
01274 beatHypotheses_(h, 1) = beatHypotheses_(ph, 1);
01275
01276 ph++;
01277 }
01278 p++;
01279 ph = 0;
01280 }
01281
01282
01283 if(periods[3] > maxPeriod_) maxPeriod_ = ((mrs_natural) (periods[3] + 0.5));
01284 if(periods[4] < minPeriod_) minPeriod_ = ((mrs_natural) (periods[4] + 0.5));
01285 updControl(ctrl_maxPeriod_, maxPeriod_);
01286 updControl(ctrl_minPeriod_, minPeriod_);
01287
01288 cerr << ((60.0/periods[0])*(srcFs_ / hopSize_)) << "; " << ((60.0/periods[1])*(srcFs_ / hopSize_)) << "; " <<
01289 ((60.0/periods[2])*(srcFs_ / hopSize_)) << "; " << ((60.0/periods[3])*(srcFs_ / hopSize_)) << "; " <<
01290 ((60.0/periods[4])*(srcFs_ / hopSize_)) << " (BPMs)" << endl;
01291 }
01292 else if(strcmp(mode_.c_str(), "p_nr") == 0)
01293 {
01294 nrPeriodHyps_ = 5;
01295 nInitHyp_ = nrPeriodHyps_ * nrPhasesPerPeriod_;
01296 mrs_natural p = 0, ph = 0;
01297
01298 mrs_real gtPeriod;
01299 if(backtrace_)
01300 {
01301 gtPeriod = gtInitPeriod_;
01302 cerr << "Periods as ibi of given first 2 beats + others non-related: ";
01303 }
01304 else
01305 {
01306 gtPeriod = gtLastPeriod_;
01307 cerr << "Periods as ibi of given last 2 beats + others non-related: ";
01308 }
01309
01310 double periods[] = {gtPeriod, (mrs_natural) (gtInitPeriod_ * 1.8), (mrs_natural) (gtPeriod * 1.2),
01311 (mrs_natural) (gtPeriod * 2.3), (mrs_natural) (gtPeriod * 0.7)};
01312
01313 for(int i = 0; i < nrPeriodHyps_; i++)
01314 {
01315 for(int h = i*nrPhasesPerPeriod_; h < (i*nrPhasesPerPeriod_)+nrPhasesPerPeriod_; h++)
01316 {
01317 beatHypotheses_(h, 0) = periods[p];
01318 beatHypotheses_(h, 1) = beatHypotheses_(ph, 1);
01319
01320 ph++;
01321 }
01322 p++;
01323 ph = 0;
01324 }
01325
01326
01327 if(periods[3] > maxPeriod_) maxPeriod_ = ((mrs_natural) (periods[3] + 0.5));
01328 if(periods[4] < minPeriod_) minPeriod_ = ((mrs_natural) (periods[4] + 0.5));
01329 updControl(ctrl_maxPeriod_, maxPeriod_);
01330 updControl(ctrl_minPeriod_, minPeriod_);
01331
01332 cerr << ((60.0/periods[0])*(srcFs_ / hopSize_)) << "; " << ((60.0/periods[1])*(srcFs_ / hopSize_)) << "; " <<
01333 ((60.0/periods[2])*(srcFs_ / hopSize_)) << "; " << ((60.0/periods[3])*(srcFs_ / hopSize_)) << "; " <<
01334 ((60.0/periods[4])*(srcFs_ / hopSize_)) << " (BPMs)" << endl;
01335 }
01336
01337 updControl(ctrl_beatHypotheses_, beatHypotheses_);
01338 }
01339
01340 void
01341 PhaseLock::myProcess(realvec& in, realvec& out)
01342 {
01343 mrs_natural o,t;
01344
01345 timeElapsed_ = ctrl_tickCount_->to<mrs_natural>();
01346
01347
01348
01349
01350
01351
01352
01353 for (o=0; o < onObservations_; o++)
01354 {
01355 for (t = 0; t < onSamples_; t++)
01356 {
01357 out(o,t) = -1.0;
01358 }
01359 }
01360
01361
01362
01363
01364 triggerInduction_ = ctrl_triggerInduction_->to<mrs_bool>();
01365 if(triggerInduction_)
01366 {
01367 dumbInduction_ = ctrl_dumbInduction_->to<mrs_bool>();
01368
01369
01370
01371 cerr << "\nRequested Induction in \"" << mode_ << "\" mode at: " << (((timeElapsed_* hopSize_)-hopSize_/2) / srcFs_) << "s" << endl;
01372
01373
01374 if(strcmp(mode_.c_str(), "2b") == 0 || strcmp(mode_.c_str(), "2b2") == 0)
01375 {
01376 mrs_realvec gtHypotheses = readGTFile(ctrl_gtBeatsFile_->to<mrs_string>());
01377 handleGTHypotheses(in, out, ctrl_gtBeatsFile_->to<mrs_string>(), gtHypotheses);
01378
01379 if(backtrace_)
01380 {
01381
01382 out(0, 0) = gtInitPeriod_;
01383
01384 out(0, 1) = gtInitPhase_;
01385
01386 if(strcmp(mode_.c_str(), "2b") == 0)
01387 {
01388 if(gtAfter2ndBeat_)
01389 cerr << "Initial phase backtraced from second beat of given ground-truth file: ";
01390 else
01391 cerr << "Initial phase backtraced from first beat of given ground-truth file: ";
01392 }
01393 else if(strcmp(mode_.c_str(), "2b2") == 0)
01394 {
01395 if(gtAfter2ndBeat_)
01396 cerr << "Initial phase as second beat of given ground-truth file: ";
01397 else
01398 cerr << "Initial phase as first beat of given ground-truth file: ";
01399 }
01400
01401 cerr << (((gtInitPhase_ * hopSize_) - adjustment_) / srcFs_) << "s" << endl;
01402 cerr << "Ground-truth period: " << ((60.0/gtInitPeriod_)*(srcFs_ / hopSize_)) << " (BPMs)" << endl;
01403 }
01404 else
01405 {
01406
01407 out(0, 0) = gtLastPeriod_;
01408
01409 out(0, 1) = gtLastPhase_;
01410
01411 if(strcmp(mode_.c_str(), "2b") == 0)
01412 {
01413 if(gtAfter2ndBeat_)
01414 cerr << "Initial phase adjusted from second beat after induction time, of given ground-truth file: ";
01415 else
01416 cerr << "Initial phase adjusted from first beat after induction time, of given ground-truth file: ";
01417
01418 cerr << (((gtLastPhase_ * hopSize_) - adjustment_) / srcFs_) << "s" << endl;
01419 cerr << "Ground-truth period (adjusted): " << ((60.0/gtLastPeriod_)*(srcFs_ / hopSize_)) << " (BPMs)" << endl;
01420 }
01421 else if(strcmp(mode_.c_str(), "2b2") == 0)
01422 {
01423 cerr << "Initial phase as first beat after induction time, of given ground-truth file: ";
01424 cerr << (((gtLastPhase_ * hopSize_) - adjustment_) / srcFs_) << "s" << "[" << gtLastPhase_ << "]" << endl;
01425 cerr << "Ground-truth period: " << ((60.0/gtLastPeriod_)*(srcFs_ / hopSize_)) << " (BPMs)" << endl;
01426 }
01427
01428 }
01429
01430
01431 out(0, 2) = gtScore_;
01432
01433
01434
01435 }
01436 else if(strcmp(mode_.c_str(), "1b") == 0
01437 || strcmp(mode_.c_str(), "1b1") == 0 )
01438 {
01439 mrs_realvec gtHypotheses = readGTFile(ctrl_gtBeatsFile_->to<mrs_string>());
01440 handleGTHypotheses(in, out, ctrl_gtBeatsFile_->to<mrs_string>(), gtHypotheses);
01441 }
01442 else if((strcmp(mode_.c_str(), "p") == 0) || (strcmp(mode_.c_str(), "p_mr") == 0)
01443 || (strcmp(mode_.c_str(), "p_nr") == 0))
01444 {
01445 mrs_realvec gtHypotheses = readGTFile(ctrl_gtBeatsFile_->to<mrs_string>());
01446 handleGTHypotheses(in, out, ctrl_gtBeatsFile_->to<mrs_string>(), gtHypotheses);
01447
01448
01449 forceInitPeriods(mode_);
01450
01451
01452 regularFunc(in, out);
01453 }
01454 else if(strcmp(mode_.c_str(), "regular") == 0)
01455 regularFunc(in , out);
01456
01457 cerr << "===================FINISH INDUCTION=====================" << endl;
01458
01459
01460
01461
01462
01463
01464
01465
01466
01467
01468
01469
01470
01471
01472
01473
01474
01475
01476
01477
01478
01479 }
01480
01481
01482
01483
01484
01485
01486 }