00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "TimelineLabeler.h"
00020 #include <fstream>
00021 #include <sstream>
00022
00023 using namespace std;
00024 using namespace Marsyas;
00025
00026 TimelineLabeler::TimelineLabeler(mrs_string name):MarSystem("TimelineLabeler", name)
00027 {
00028 addControls();
00029 labelFiles_ = ",";
00030 numClasses_ = 0;
00031 selectedLabel_ = "init";
00032 curRegion_ = 0;
00033 foundNextRegion_ = false;
00034 }
00035
00036 TimelineLabeler::TimelineLabeler(const TimelineLabeler& a) : MarSystem(a)
00037 {
00038 ctrl_labelFiles_ = getctrl("mrs_string/labelFiles");
00039 ctrl_currentLabelFile_ = getctrl("mrs_real/currentLabelFile");
00040 ctrl_labelNames_ = getctrl("mrs_string/labelNames");
00041 ctrl_currentLabel_ = getctrl("mrs_real/currentLabel");
00042 ctrl_previousLabel_ = getctrl("mrs_real/previousLabel");
00043 ctrl_nLabels_ = getctrl("mrs_natural/nLabels");
00044 ctrl_selectLabel_ = getctrl("mrs_string/selectLabel");
00045 ctrl_advance_ = getctrl("mrs_natural/advance");
00046 ctrl_pos_ = getctrl("mrs_natural/pos");
00047 ctrl_playRegionsOnly_ = getctrl("mrs_bool/playRegionsOnly");
00048
00049 labelFiles_ = ",";
00050 numClasses_ = 0;
00051 selectedLabel_ = "init";
00052 curRegion_ = 0;
00053 foundNextRegion_ = false;
00054 }
00055
00056 TimelineLabeler::~TimelineLabeler()
00057 {
00058 }
00059
00060 MarSystem*
00061 TimelineLabeler::clone() const
00062 {
00063 return new TimelineLabeler(*this);
00064 }
00065
00066 void
00067 TimelineLabeler::addControls()
00068 {
00069 addctrl("mrs_string/labelFiles", ",", ctrl_labelFiles_);
00070 ctrl_labelFiles_->setState(true);
00071
00072 addctrl("mrs_real/currentLabelFile", 0.0, ctrl_currentLabelFile_);
00073 ctrl_currentLabelFile_->setState(true);
00074
00075 addctrl("mrs_string/selectLabel", "", ctrl_selectLabel_);
00076 ctrl_selectLabel_->setState(true);
00077
00078 addctrl("mrs_natural/advance", 0, ctrl_advance_);
00079 addctrl("mrs_natural/pos", 0, ctrl_pos_);
00080
00081 addctrl("mrs_bool/playRegionsOnly", true, ctrl_playRegionsOnly_);
00082
00083 addctrl("mrs_string/labelNames", ",", ctrl_labelNames_);
00084 addctrl("mrs_real/currentLabel", -1.0, ctrl_currentLabel_);
00085 addctrl("mrs_real/previousLabel", -1.0, ctrl_previousLabel_);
00086 addctrl("mrs_natural/nLabels", 0, ctrl_nLabels_);
00087 }
00088
00089 void
00090 TimelineLabeler::myUpdate(MarControlPtr sender)
00091 {
00092 MRSDIAG("TimelineLabeler.cpp - TimelineLabeler:myUpdate");
00093
00094 MarSystem::myUpdate(sender);
00095
00097
00099 mrs_string newLabelFiles = ctrl_labelFiles_->to<mrs_string>();
00100 if(labelFiles_ != newLabelFiles && (newLabelFiles != "" || newLabelFiles != ","))
00101 {
00102 labelFiles_ = newLabelFiles;
00103 mrs_natural i;
00104 labelFilesVec_.clear();
00105 while(newLabelFiles.length() != 0 )
00106 {
00107 i = newLabelFiles.find(",");
00108 labelFilesVec_.push_back(newLabelFiles.substr(0, i).c_str());
00109 newLabelFiles = newLabelFiles.substr(i+1 , newLabelFiles.length()-i-1);
00110 }
00111 }
00112
00113
00115
00117 mrs_bool newTimeline = false;
00118
00119 mrs_natural curLabelFile = ctrl_currentLabelFile_->to<mrs_real>() + 0.5;
00120
00121
00122
00123 if(curLabelFile < (mrs_natural)labelFilesVec_.size())
00124 {
00125 mrs_string fname = labelFilesVec_[curLabelFile];
00127
00129 if(fname != timeline_.filename() && fname != "")
00130 {
00131
00132
00133 if(timeline_.load(fname))
00134 {
00135 timeline_.setSampleRate(israte_);
00136 newTimeline = true;
00137
00138 numClasses_ = (mrs_natural)timeline_.numClasses();
00139 ctrl_nLabels_->setValue(numClasses_, NOUPDATE);
00140
00141
00142 ostringstream sstr;
00143 vector<mrs_string> classNames = timeline_.getRegionNames();
00144 for(mrs_natural i=0; i < numClasses_; ++i)
00145 sstr << classNames[i] << ",";
00146 ctrl_labelNames_->setValue(sstr.str(), NOUPDATE);
00147
00148 curRegion_ = 0;
00149 foundNextRegion_ = true;
00150 }
00151 else
00152 {
00153 MRSWARN("TimelineLabeler::myUpdate() - error reading label file " << labelFilesVec_[(mrs_natural) (ctrl_currentLabelFile_->to<mrs_real>()+0.5)]);
00154 numClasses_ = 0;
00155 ctrl_nLabels_->setValue(numClasses_, NOUPDATE);
00156 ctrl_labelNames_->setValue(",", NOUPDATE);
00157 timeline_.clear();
00158 }
00159 }
00160 }
00161 else
00162 {
00163 numClasses_ = 0;
00164 ctrl_nLabels_->setValue(numClasses_, NOUPDATE);
00165 ctrl_labelNames_->setValue(",", NOUPDATE);
00166 timeline_.clear();
00167 }
00168
00170
00172 if(timeline_.numRegions() > 0 &&
00173 ((ctrl_selectLabel_->to<mrs_string>() != selectedLabel_) || newTimeline))
00174 {
00175 selectedLabel_ = ctrl_selectLabel_->to<mrs_string>();
00176
00177 if(ctrl_playRegionsOnly_->to<mrs_bool>())
00178 {
00179
00180
00181
00182 if(selectedLabel_ == "")
00183 {
00184
00185 ctrl_pos_->setValue(timeline_.regionStart(0)*timeline_.lineSize(), NOUPDATE);
00186 curRegion_ = 0;
00187 foundNextRegion_ = true;
00188 }
00189 else
00190 {
00191 curRegion_ = 0;
00192 foundNextRegion_ = true;
00193 for(mrs_natural i=0; i < timeline_.numRegions(); ++i)
00194 {
00195 if(timeline_.regionName(i) == selectedLabel_)
00196 {
00197
00198 ctrl_pos_->setValue(timeline_.regionStart(i), NOUPDATE);
00199 curRegion_ = i;
00200
00201 break;
00202 }
00203 }
00204 }
00205 }
00206 }
00207 }
00208
00209 void
00210 TimelineLabeler::myProcess(realvec& in, realvec& out)
00211 {
00212
00213 out = in;
00214
00215 if(timeline_.numRegions() == 0)
00216 {
00217 MRSWARN("TimelineLabeler::myProcess() - no regions/labels exist in loaded timeline: " << timeline_.filename());
00218 ctrl_currentLabel_->setValue(-1.0);
00219 return;
00220 }
00221
00222 timeline_.setSampleRate(israte_);
00223
00224
00225
00226
00227 samplePos_ = ctrl_pos_->to<mrs_natural>();
00228
00229
00230 mrs_natural regionStart = timeline_.regionStart(curRegion_)*timeline_.lineSize();
00231 mrs_natural regionEnd = timeline_.regionEnd(curRegion_)*timeline_.lineSize();
00232
00234
00235
00237 if(samplePos_ == 0)
00238 samplePos_ += inSamples_/2;
00239 else
00240 samplePos_ -= inSamples_/2;
00241
00242 if (samplePos_ >= regionStart && samplePos_<= regionEnd)
00243 {
00244 if(timeline_.regionName(curRegion_) == selectedLabel_ ||
00245 selectedLabel_ == "" ||
00246 selectedLabel_ == "init")
00247 {
00248 ctrl_currentLabel_->setValue(timeline_.regionClass(curRegion_));
00249 }
00250 else
00251 ctrl_currentLabel_->setValue(-1.0);
00252
00253 foundNextRegion_ = false;
00254 }
00255 else
00256 {
00257
00258 if(!foundNextRegion_)
00259 {
00261
00263 if(selectedLabel_ == "" || selectedLabel_ == "init")
00264 curRegion_++;
00265 else
00266 {
00267 curRegion_++;
00268 while(timeline_.regionName(curRegion_)!= selectedLabel_ && curRegion_ < timeline_.numRegions())
00269 {
00270 curRegion_++;
00271 }
00272 }
00273 foundNextRegion_ = true;
00274 }
00275
00277
00279 if(curRegion_ < timeline_.numRegions())
00280 {
00281
00282
00283 regionStart = timeline_.regionStart(curRegion_)*timeline_.lineSize();
00284 regionEnd = timeline_.regionEnd(curRegion_)*timeline_.lineSize();
00285
00286
00287 if(samplePos_ >= regionStart && samplePos_ <= regionEnd)
00288 {
00289
00290 ctrl_currentLabel_->setValue(timeline_.regionClass(curRegion_));
00291 }
00292 else
00293 {
00294
00295 if(ctrl_playRegionsOnly_->to<mrs_bool>())
00296 {
00297
00298
00299
00300
00301
00302 ctrl_pos_->setValue(regionStart);
00303 }
00304
00305 ctrl_currentLabel_->setValue(-1.0);
00306 }
00307 }
00308 else
00309 {
00310
00311 if(ctrl_playRegionsOnly_->to<mrs_bool>())
00312 {
00313
00314
00315
00316
00317
00318 ctrl_advance_->setValue(1);
00319 }
00320 ctrl_currentLabel_->setValue(-1.0);
00321 }
00322 }
00323 }