00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "Accumulator.h"
00020
00021 using std::ostringstream;
00022 using namespace Marsyas;
00023
00024 Accumulator::Accumulator(mrs_string name): MarSystem("Accumulator", name)
00025 {
00026 isComposite_ = true;
00027 addControls();
00028
00029 keptOnSamples_ = 0;
00030 }
00031
00032 Accumulator::Accumulator(const Accumulator& a): MarSystem(a)
00033 {
00034 ctrl_nTimes_ = getctrl("mrs_natural/nTimes");
00035 ctrl_maxTimes_ = getctrl("mrs_natural/maxTimes");
00036 ctrl_minTimes_ = getctrl("mrs_natural/minTimes");
00037 ctrl_mode_ = getctrl("mrs_string/mode");
00038 ctrl_flush_ = getctrl("mrs_bool/flush");
00039 ctrl_timesToKeep_ = getctrl("mrs_natural/timesToKeep");
00040 keptOnSamples_ = a.keptOnSamples_;
00041 }
00042
00043 Accumulator::~Accumulator()
00044 {
00045 }
00046
00047 MarSystem*
00048 Accumulator::clone() const
00049 {
00050 return new Accumulator(*this);
00051 }
00052
00053 void
00054 Accumulator::addControls()
00055 {
00056 addctrl("mrs_string/mode", "countTicks", ctrl_mode_);
00057 ctrl_mode_->setState(true);
00058
00059 addctrl("mrs_natural/nTimes", 5, ctrl_nTimes_);
00060 ctrl_nTimes_->setState(true);
00061
00062 addctrl("mrs_natural/timesToKeep", 0, ctrl_timesToKeep_);
00063 ctrl_timesToKeep_->setState(true);
00064
00065 addctrl("mrs_bool/flush", false, ctrl_flush_);
00066
00067 addctrl("mrs_natural/maxTimes", 5, ctrl_maxTimes_);
00068 ctrl_maxTimes_->setState(true);
00069
00070 addctrl("mrs_natural/minTimes", 5, ctrl_minTimes_);
00071 ctrl_minTimes_->setState(true);
00072 }
00073
00074
00075 bool
00076 Accumulator::addMarSystem(MarSystem *marsystem)
00077 {
00078 if(marsystemsSize_ > 0) {
00079 MarSystem* mySystem = marsystems_[0];
00080
00081 MRSWARN("Accumulator::addMarSystem: already added '"
00082 << mySystem->getAbsPath()
00083 << "' to the Accumulator NOT ADDING '"
00084 << marsystem->getName()
00085 << "'.");
00086 return false;
00087 }
00088 return MarSystem::addMarSystem(marsystem);
00089
00090
00091 }
00092
00093 void
00094 Accumulator::myUpdate(MarControlPtr sender)
00095 {
00096 MRSDIAG("Accumulator.cpp - Accumulator:myUpdate");
00097
00098 mrs_string onObsNames;
00099
00100 childOnSamples_ = 0;
00101 nTimes_ = ctrl_nTimes_->to<mrs_natural>();
00102
00103 MarSystem::myUpdate(sender);
00104
00105 if (marsystemsSize_ > 0)
00106 {
00107
00108 marsystems_[0]->setctrl("mrs_natural/inObservations", inObservations_);
00109 marsystems_[0]->setctrl("mrs_natural/inSamples", inSamples_);
00110 marsystems_[0]->setctrl("mrs_real/israte", israte_);
00111 marsystems_[0]->setctrl("mrs_string/inObsNames", inObsNames_);
00112 marsystems_[0]->update();
00113
00114 childOnSamples_ = marsystems_[0]->getctrl("mrs_natural/onSamples")->to<mrs_natural>();
00115
00116
00117
00118 setctrl(ctrl_onSamples_, nTimes_ * childOnSamples_);
00119 setctrl(ctrl_onObservations_,
00120 marsystems_[0]->getctrl("mrs_natural/onObservations")->to<mrs_natural>());
00121 setctrl(ctrl_osrate_,
00122 marsystems_[0]->getctrl("mrs_real/osrate"));
00123
00124 onObsNames = marsystems_[0]->getctrl("mrs_string/onObsNames")->to<mrs_string>();
00125 }
00126
00127 onObservations_ = ctrl_onObservations_->to<mrs_natural>();
00128
00129
00130 ostringstream prefix_oss;
00131 prefix_oss << "Acc" << nTimes_ << "_";
00132 ctrl_onObsNames_->setValue(obsNamesAddPrefix(onObsNames, prefix_oss.str()), NOUPDATE);
00133
00134
00135 onSamples_ = ctrl_onSamples_->to<mrs_natural>();
00136
00137
00138 childOut_.stretch(onObservations_, childOnSamples_);
00139
00140
00141
00142 if (ctrl_mode_->to<mrs_string>() == "explicitFlush")
00143 {
00144 tout_.stretch(onObservations_,
00145 (ctrl_timesToKeep_->to<mrs_natural>() + ctrl_maxTimes_->to<mrs_natural>()) * childOnSamples_);
00146 }
00147 else
00148 {
00149 tout_.create(0,0);
00150 }
00151 }
00152
00153 void
00154 Accumulator::myProcess(realvec& in, realvec& out)
00155 {
00156 mrs_natural o,c,t;
00157
00158 if (marsystemsSize_ == 0)
00159 {
00160 out = in;
00161 return;
00162 }
00163
00164 if (ctrl_mode_->to<mrs_string>() == "explicitFlush")
00165 {
00166 mrs_natural timesCount = keptOnSamples_/childOnSamples_;
00167 while ((!ctrl_flush_->to<mrs_bool>() && timesCount < ctrl_maxTimes_->to<mrs_natural>())
00168 || timesCount < ctrl_minTimes_->to<mrs_natural>()
00169 || timesCount <= ctrl_timesToKeep_->to<mrs_natural>())
00170 {
00171
00172
00173 marsystems_[0]->process(in, childOut_);
00174
00175
00176 for (o=0; o < onObservations_; o++)
00177 {
00178 for (t = 0; t < childOnSamples_; t++)
00179 {
00180 tout_(o, t + timesCount * childOnSamples_) = childOut_(o,t);
00181 }
00182 }
00183 timesCount++;
00184 }
00185 #ifdef MARSYAS_LOG_DIAGNOSTICS
00186 if (!ctrl_flush_->to<mrs_bool>())
00187 {
00188 MRSDIAG("Accumulator::myProcess() - maxTimes reached without a flush event!");
00189 }
00190 #endif
00191
00192
00193
00194 ctrl_nTimes_->setValue(timesCount - ctrl_timesToKeep_->to<mrs_natural>());
00195
00196 keptOnSamples_ = ctrl_timesToKeep_->to<mrs_natural>() * childOnSamples_;
00197
00198
00199 for (o=0; o < onObservations_; ++o)
00200 {
00201 for (t = 0; t < ctrl_onSamples_->to<mrs_natural>(); ++t)
00202 {
00203 out(o,t) = tout_(o,t);
00204 }
00205 }
00206
00207
00208
00209 for (t = 0; t < keptOnSamples_; ++t)
00210 {
00211 for (o=0; o < onObservations_; ++o)
00212 {
00213 tout_(o,t) = tout_(o, t + ctrl_onSamples_->to<mrs_natural>());
00214 }
00215 }
00216
00217
00218 ctrl_flush_->setValue(false);
00219 }
00220 else if (ctrl_mode_->to<mrs_string>() == "countTicks")
00221 {
00222
00223 ctrl_flush_->setValue(false);
00224 for (c = 0; c < nTimes_; ++c)
00225 {
00226 marsystems_[0]->process(in, childOut_);
00227 for (o=0; o < onObservations_; o++)
00228 {
00229 for (t = 0; t < childOnSamples_; t++)
00230 {
00231 out(o, t + c * childOnSamples_) = childOut_(o,t);
00232 }
00233 }
00234 }
00235 ctrl_flush_->setValue(true);
00236 }
00237
00238
00239
00240
00241 }