00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "TimeLine.h"
00020
00021 using namespace std;
00022 using namespace Marsyas;
00023
00024 TimeLine::TimeLine()
00025 {
00026 srate_ = 22050.0;
00027 psrate_ = 0.0;
00028 lineSize_ = 0;
00029 size_ = 0;
00030 filename_ = "";
00031 numRegions_ = 0;
00032 }
00033
00034 TimeLine::~TimeLine()
00035 {
00036 }
00037
00038 void
00039 TimeLine::clear()
00040 {
00041 filename_ = "";
00042 srate_ = 22050.0;
00043 psrate_ = 0.0;
00044
00045 lineSize_ = 0;
00046 size_ = 0;
00047 regions_.clear();
00048 numRegions_ = 0;
00049 }
00050
00051 void
00052 TimeLine::setSampleRate(mrs_real srate)
00053 {
00054 srate_ = srate;
00055
00056 if ((srate_ != 22050.0)&&(srate_ != psrate_))
00057 {
00058
00059 for (mrs_natural i=0; i < numRegions_; ++i)
00060 {
00061 regions_[i].start *= (srate_ / 22050.0);
00062 regions_[i].end *= (srate_ / 22050.0);
00063 }
00064 }
00065
00066 psrate_ = srate;
00067
00068 }
00069
00070
00071 void
00072 TimeLine::regular(mrs_natural spacing, mrs_natural size, mrs_natural lineSize)
00073 {
00074 if (size_ != 0)
00075 {
00076 MRSERR("TimeLine::regular() - TimeLine has data already!");
00077 return;
00078 }
00079
00080 size_ = size;
00081 mrs_natural reg_index = 0;
00082 mrs_natural i;
00083 lineSize_ = lineSize;
00084 if ((size_ % spacing) != 0)
00085 numRegions_ = (size_ / spacing) + 1;
00086 else
00087 numRegions_ = (size_ / spacing);
00088
00089 for (i=0; i < numRegions_; ++i)
00090 {
00091 TimeRegion region;
00092 regions_.push_back(region);
00093 }
00094
00095 for (i=0; i<size_; ++i)
00096 {
00097 if ((i % spacing) == 0)
00098 {
00099 if (reg_index > 0)
00100 regions_[reg_index-1].end = i-1;
00101 regions_[reg_index].start = i;
00102 regions_[reg_index].classId = 0;
00103 reg_index++;
00104 }
00105 }
00106 regions_[numRegions_-1].end = size_;
00107 regions_[reg_index-1].end = size_;
00108 }
00109
00110 void
00111 TimeLine::segment(realvec segmentation, mrs_natural lineSize)
00112 {
00113 mrs_natural i;
00114 mrs_natural peakCount=0;
00115
00116 if (size_ != 0)
00117 {
00118 MRSERR("TimeLine::scan() - TimeLine has data already!");
00119 return;
00120 }
00121
00122 size_ = segmentation.getSize();
00123 for (i=0; i<size_; ++i)
00124 {
00125 if (segmentation(i) == 1)
00126 peakCount++;
00127 }
00128
00129 numRegions_ = peakCount-1;
00130 lineSize_ = lineSize;
00131
00132 for (i=0; i < numRegions_; ++i)
00133 {
00134 TimeRegion region;
00135 regions_.push_back(region);
00136 }
00137
00138 mrs_natural reg_index = 0;
00139 for (i=0; i<size_; ++i)
00140 {
00141 if (segmentation(i) == 1)
00142 {
00143 if (reg_index > 0)
00144 regions_[reg_index-1].end = i;
00145 if (reg_index == peakCount -1)
00146 break;
00147 regions_[reg_index].start = i;
00148 regions_[reg_index].classId = 0;
00149 reg_index++;
00150 }
00151 }
00152 }
00153
00154 mrs_natural
00155 TimeLine::numClasses() const
00156 {
00157 vector<mrs_natural> classes;
00158 bool found = false;
00159
00160 for(mrs_natural i = 0; i < numRegions_; ++i)
00161 {
00162 found = false;
00163
00164 for(mrs_natural c = 0; c < (mrs_natural)classes.size(); ++c)
00165 {
00166 if(classes[c] == regions_[i].classId)
00167 {
00168 found = true;
00169 break;
00170 }
00171 }
00172
00173 if(!found)
00174 classes.push_back(regions_[i].classId);
00175 }
00176
00177 return (mrs_natural)classes.size();
00178 }
00179
00180 vector<mrs_string>
00181 TimeLine::getRegionNames() const
00182 {
00183 vector<mrs_string> classNames;
00184 bool found = false;
00185
00186 for(mrs_natural i = 0; i < numRegions_; ++i)
00187 {
00188 found = false;
00189
00190 for(mrs_natural c = 0; c < (mrs_natural)classNames.size(); ++c)
00191 {
00192 if(classNames[c] == regions_[i].name)
00193 {
00194 found = true;
00195 break;
00196 }
00197 }
00198
00199 if(!found)
00200 classNames.push_back(regions_[i].name);
00201 }
00202
00203 sort(classNames.begin(), classNames.end());
00204
00205
00206 return classNames;
00207 }
00208
00209 mrs_natural
00210 TimeLine::regionStart(mrs_natural regionNum) const
00211 {
00212 if (regionNum < numRegions_)
00213 return regions_[regionNum].start;
00214 else return -1;
00215 }
00216
00217 mrs_string
00218 TimeLine::regionName(mrs_natural regionNum) const
00219 {
00220 if (regionNum < numRegions_)
00221 return regions_[regionNum].name;
00222 else return "";
00223 }
00224
00225 void
00226 TimeLine::setRegionName(mrs_natural regionNum, mrs_string name)
00227 {
00228 if (regionNum < numRegions_)
00229 regions_[regionNum].name = name;
00230 }
00231
00232 void
00233 TimeLine::setRegionClass(mrs_natural regionNum, mrs_natural classId)
00234 {
00235 if (regionNum < numRegions_)
00236 {
00237 regions_[regionNum].classId = classId;
00238 }
00239 }
00240
00241 mrs_natural
00242 TimeLine::regionEnd(mrs_natural regionNum) const
00243 {
00244 if (regionNum < numRegions_)
00245 return regions_[regionNum].end;
00246 else return -1;
00247 }
00248
00249 void
00250 TimeLine::smooth(mrs_natural smoothSize)
00251 {
00252 TimeRegion region;
00253 TimeRegion pregion;
00254 TimeRegion nregion;
00255
00256 for (int i=1; i < numRegions_-1; ++i)
00257 {
00258 region = regions_[i];
00259 pregion = regions_[i-1];
00260 nregion = regions_[i+1];
00261
00262 if ((region.end - region.start < smoothSize) && (region.classId == 1))
00263 {
00264
00265
00266 removeRegion(i);
00267 i = i-1;
00268
00269
00270
00271 }
00272 }
00273
00274 for (mrs_natural i=1; i < numRegions_; ++i)
00275 {
00276 region = regions_[i];
00277 pregion = regions_[i-1];
00278
00279 if (region.classId == pregion.classId)
00280 {
00281 removeRegion(i);
00282 i = i-1;
00283 }
00284 }
00285 }
00286
00287 void
00288 TimeLine::removeRegion(mrs_natural regionNum)
00289 {
00290 if (regionNum >= 1)
00291 {
00292 regions_[regionNum-1].end = regions_[regionNum].end;
00293 regions_.erase(regions_.begin() + regionNum);
00294 numRegions_--;
00295 }
00296 }
00297
00298 mrs_real
00299 TimeLine::regionClass(mrs_natural regionNum) const
00300 {
00301 if (regionNum < numRegions_)
00302 return regions_[regionNum].classId;
00303 else
00304 return 0;
00305 }
00306
00307 mrs_natural
00308 TimeLine::sampleClass(mrs_natural index) const
00309 {
00310 TimeRegion region;
00311 for (mrs_natural i=0; i < numRegions_; ++i)
00312 {
00313 region = regions_[i];
00314 if ((region.start <= index) && (index < region.end))
00315 {
00316 return region.classId;
00317 }
00318 }
00319 return 0;
00320 }
00321
00322 bool
00323 TimeLine::load(mrs_string filename)
00324 {
00325 cout << "filename = " << filename << endl;
00326
00327 ifstream in;
00328 filename_ = filename;
00329
00330 if(filename == "")
00331 return false;
00332
00333 in.open(filename.c_str());
00334 if(!in.is_open())
00335 {
00336 MRSERR("TimeLine::load() - Problem opening file " << filename_);
00337 return false;
00338 }
00339
00340 FileName f(filename);
00341 vector<mrs_string> labels;
00342
00343 if (f.ext() == "txt")
00344 {
00345 numRegions_ = 0;
00346 mrs_real start, end;
00347 mrs_string label;
00348 regions_.clear();
00349 while (!in.eof())
00350 {
00351 in >> start >> end >> label;
00352
00353 TimeRegion region;
00354 region.start = start * srate_;
00355 region.end = end * srate_;
00356 region.classId = 1;
00357 region.name = label;
00358 mrs_bool label_found = false;
00359
00360 for (unsigned int i=0; i < labels.size(); i++)
00361 {
00362 if (label == labels[i])
00363 {
00364 label_found = true;
00365 region.classId = i;
00366 }
00367
00368 }
00369 if (!label_found)
00370 {
00371 labels.push_back(label);
00372 sort(labels.begin(), labels.end());
00373 }
00374 regions_.push_back(region);
00375 numRegions_ ++;
00376 }
00377
00378
00379 for (mrs_natural i=0; i < numRegions_; ++i)
00380 {
00381 mrs_string label = regions_[i].name;
00382 vector<mrs_string>::iterator it = find(labels.begin(), labels.end(), label);
00383 if (it == labels.end())
00384 regions_[i].classId = -1;
00385 mrs_natural l = distance(labels.begin(), it);
00386 regions_[i].classId = l;
00387 }
00388
00389
00390
00391
00392 numRegions_ --;
00393 regions_.pop_back();
00394
00395
00396 lineSize_ = 1;
00397 size_ = end * srate_;
00398
00399 in.close();
00400 return true;
00401 }
00402 else
00403 {
00404 in >> numRegions_;
00405 MRSDIAG("TimeLine::load() - Number of regions is " << numRegions_);
00406
00407 in >> lineSize_;
00408 MRSDIAG("TimeLine::load() - lineSize size is " << lineSize_);
00409
00410 in >> size_;
00411 MRSDIAG("TimeLine::load() - Size is " << size_);
00412
00413 regions_.clear();
00414 for (mrs_natural i=0; i < numRegions_; ++i)
00415 {
00416 TimeRegion region;
00417 regions_.push_back(region);
00418 }
00419
00420 for (mrs_natural i=0; i<numRegions_; ++i)
00421 {
00422 mrs_natural token;
00423 mrs_string stoken1, stoken2;
00424 in >> token;
00425 regions_[i].start = token;
00426 in >> token;
00427 regions_[i].classId = token;
00428 in >> token;
00429 regions_[i].end = token;
00430 in >> stoken1;
00431
00432 regions_[i].name = stoken1;
00433 }
00434
00435 in.close();
00436
00437 return true;
00438
00439 }
00440
00441 }
00442
00443 void
00444 TimeLine::info() const
00445 {
00446 mrs_natural i;
00447 MRSMSG("Number of regions = " << numRegions_ << endl);
00448 MRSMSG("Line size = " << lineSize_ << endl);
00449 MRSMSG("TimeLine size (# line size blocks ) = " << size_ << endl);
00450
00451 for (i=0; i < numRegions_; ++i)
00452 {
00453 MRSMSG("--------------------------------------------" << endl);
00454 MRSMSG("Region " << i << " start = " << regions_[i].start << endl);
00455 MRSMSG("Region " << i << " class id = " << regions_[i].classId << endl);
00456 MRSMSG("Region " << i << " name = " << regions_[i].name << endl);
00457 MRSMSG("Region " << i << " end = " << regions_[i].end << endl);
00458 }
00459 }
00460
00461 void
00462 TimeLine::printnew(FILE *fp)
00463 {
00464 mrs_natural i;
00465 fprintf(fp, "%d\n", (int)numRegions_);
00466 fprintf(fp, "%d\n", (int)lineSize_);
00467 fprintf(fp, "%d\n", (int)size_);
00468
00469 for (i=0; i<numRegions_; ++i)
00470 {
00471
00472 float smsec;
00473 float emsec;
00474 smsec = ((regions_[i].start * lineSize_ * 1.0f) / srate_)*10000;
00475
00476 fprintf(fp, "%6.0f ", smsec);
00477
00478 fprintf(fp, "%d ", (int)regions_[i].classId);
00479 emsec = ((regions_[i].end * lineSize_ * 1.0f) / srate_) * 1000;
00480
00481 fprintf(fp, "%6.0f\n", emsec);
00482
00483 fprintf(fp, "%s\n", regions_[i].name.c_str());
00484 }
00485 }
00486
00487 void
00488 TimeLine::write(mrs_string filename)
00489 {
00490 ofstream os(filename.c_str());
00491 os << (*this) << endl;
00492 }
00493
00494 ostream&
00495 Marsyas::operator<<(ostream& o, const TimeLine& tline)
00496 {
00497 o << tline.numRegions_ << endl;
00498 o << tline.lineSize_ << endl;
00499 o << tline.size_ << endl;
00500
00501 for (mrs_natural i=0; i<tline.numRegions_; ++i)
00502 {
00503 o << tline.regions_[i].start ;
00504 o << " " << tline.regions_[i].classId;
00505 o << " " << tline.regions_[i].end << endl;
00506 o << "Region " << i+1 << endl;
00507 }
00508 return o;
00509 }
00510
00511 void
00512 TimeLine::print(FILE *fp)
00513 {
00514 mrs_natural i;
00515 fprintf(fp, "%d\n", (int) numRegions_);
00516 fprintf(fp, "%d\n", (int)lineSize_);
00517 fprintf(fp, "%d\n", (int)size_);
00518
00519 for (i=0; i<numRegions_; ++i)
00520 {
00521 fprintf(fp, "%d ", (int)regions_[i].start);
00522 fprintf(fp, "%d ", (int)regions_[i].classId);
00523 fprintf(fp, "%d\n", (int)regions_[i].end);
00524 fprintf(fp, "Region %d\n", (int)i+1);
00525 }
00526 }
00527
00528
00529
00530
00531
00532
00533
00534
00535
00536
00537
00538
00539
00540
00541
00542
00543
00544
00545
00546
00547
00548
00549
00550
00551
00552
00553
00554
00555
00556
00557
00558
00559
00560
00561
00562
00563
00564
00565
00566
00567
00568
00569
00570
00571
00572
00573
00574
00575
00576
00577
00578
00579
00580
00581
00582
00583 void
00584 TimeLine::receive(Communicator* com)
00585 {
00586 static char *buf = new char[256];
00587 mrs_natural i;
00588 mrs_string message;
00589
00590 com->receive_message(buf);
00591
00592 numRegions_ = atoi(buf);
00593
00594 com->receive_message(buf);
00595
00596 lineSize_ = atoi(buf);
00597
00598 com->receive_message(buf);
00599 size_ = atoi(buf);
00600
00601 for (i=0; i < numRegions_; ++i)
00602 {
00603 com->receive_message(buf);
00604 }
00605 }
00606
00607 void
00608 TimeLine::send(Communicator* com)
00609 {
00610 static char *buf = new char[256];
00611 mrs_natural i;
00612 mrs_string message;
00613
00614 sprintf(buf, "%d\n", (int)numRegions_);
00615 message = buf;
00616 com->send_message(message);
00617
00618 sprintf(buf, "%d\n", (int)lineSize_);
00619 message = buf;
00620 com->send_message(message);
00621
00622 sprintf(buf, "%d\n", (int)size_);
00623 message = buf;
00624 com->send_message(message);
00625
00626 for (i=0; i<numRegions_; ++i)
00627 {
00628 sprintf(buf, "%d ", (int)regions_[i].start);
00629 message = buf;
00630 com->send_message(message);
00631
00632 sprintf(buf, "%d ", (int)regions_[i].classId);
00633 message = buf;
00634 com->send_message(message);
00635
00636
00637
00638
00639
00640
00641
00642
00643
00644
00645
00646
00647
00648
00649
00650 sprintf(buf, "%d\n", (int)regions_[i].end);
00651 message = buf;
00652 com->send_message(message);
00653
00654 sprintf(buf, "Region %d\n", (int)i);
00655 message = buf;
00656 com->send_message(message);
00657 }
00658 }
00659
00660
00661
00662
00663
00664
00665
00666
00667
00668
00669
00670
00671
00672
00673
00674
00675
00676
00677
00678
00679
00680
00681
00682
00683
00684
00685
00686
00687
00688
00689
00690
00691
00692
00693
00694
00695
00696
00697
00698
00699
00700
00701
00702
00703
00704
00705
00706
00707
00708
00709
00710
00711