Marsyas  0.6.0-alpha
/usr/src/RPM/BUILD/marsyas-0.6.0/src/marsyas/marsystems/FlowThru.cpp
Go to the documentation of this file.
00001 /*
00002 ** Copyright (C) 1998-2006 George Tzanetakis <gtzan@cs.uvic.ca>
00003 **
00004 ** This program is free software; you can redistribute it and/or modify
00005 ** it under the terms of the GNU General Public License as published by
00006 ** the Free Software Foundation; either version 2 of the License, or
00007 ** (at your option) any later version.
00008 **
00009 ** This program is distributed in the hope that it will be useful,
00010 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 ** GNU General Public License for more details.
00013 **
00014 ** You should have received a copy of the GNU General Public License
00015 ** along with this program; if not, write to the Free Software
00016 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00017 */
00018 
00019 #include "FlowThru.h"
00020 
00021 using std::ostringstream;
00022 using namespace Marsyas;
00023 using std::cout;
00024 using std::endl;
00025 
00026 
00027 FlowThru::FlowThru(mrs_string name):MarSystem("FlowThru",name)
00028 {
00029   isComposite_ = true;
00030   addControls();
00031 }
00032 
00033 FlowThru::FlowThru(const FlowThru& a):MarSystem(a)
00034 {
00035   ctrl_innerOut_ = getctrl("mrs_realvec/innerOut");
00036 }
00037 
00038 FlowThru::~FlowThru()
00039 {
00040 
00041 }
00042 
00043 MarSystem*
00044 FlowThru::clone() const
00045 {
00046   return new FlowThru(*this);
00047 }
00048 
00049 void
00050 FlowThru::addControls()
00051 {
00052   addctrl("mrs_realvec/innerOut", realvec(), ctrl_innerOut_);
00053 }
00054 
00055 
00056 void
00057 FlowThru::myUpdate(MarControlPtr sender)
00058 {
00059   //forward flow propagation
00060   //just pass input flow to the output
00061   MarSystem::myUpdate(sender);
00062 
00063   child_count_t child_count = marsystems_.size();
00064   if (child_count)
00065   {
00066     //propagate in flow controls to first child
00067     marsystems_[0]->setctrl("mrs_natural/inObservations", inObservations_);
00068     marsystems_[0]->setctrl("mrs_natural/inSamples", inSamples_);
00069     marsystems_[0]->setctrl("mrs_real/israte", israte_);
00070     marsystems_[0]->setctrl("mrs_string/inObsNames", inObsNames_);
00071     marsystems_[0]->update();
00072 
00073     // update dataflow component MarSystems in order
00074     for(child_count_t i=1; i < child_count; ++i) {
00075       marsystems_[i]->setctrl(marsystems_[i]->ctrl_inObsNames_,
00076                               marsystems_[i-1]->ctrl_onObsNames_);
00077       marsystems_[i]->setctrl(marsystems_[i]->ctrl_inObservations_,
00078                               marsystems_[i-1]->ctrl_onObservations_);
00079       marsystems_[i]->setctrl(marsystems_[i]->ctrl_inSamples_,
00080                               marsystems_[i-1]->ctrl_onSamples_);
00081       marsystems_[i]->setctrl(marsystems_[i]->ctrl_israte_,
00082                               marsystems_[i-1]->ctrl_osrate_);
00083       marsystems_[i]->update();
00084     }
00085 
00086     //link the output of the last child MarSystem to the innerOut of FlowThru Composite
00087     //(linkTo() will automatically unlink ctrl_innerOut from any previous link target before
00088     //relinking it to the last MarSystem)
00089     ctrl_innerOut_->linkTo(marsystems_[child_count-1]->ctrl_processedData_);
00090 
00091 
00092 
00093     for(child_count_t i=0; i< child_count; ++i)
00094     {
00095       MarControlAccessor acc(marsystems_[i]->ctrl_processedData_, NOUPDATE);
00096       realvec& processedData = acc.to<mrs_realvec>();
00097 
00098       if (processedData.getRows() != marsystems_[i]->ctrl_onObservations_->to<mrs_natural>()  ||
00099           processedData.getCols() != marsystems_[i]->ctrl_onSamples_->to<mrs_natural>())
00100       {
00101 
00102         processedData.create(marsystems_[i]->ctrl_onObservations_->to<mrs_natural>(),
00103                              marsystems_[i]->ctrl_onSamples_->to<mrs_natural>());
00104       }
00105 
00106       if(i==child_count-1)
00107       {
00108         MarControlAccessor acc(ctrl_innerOut_, NOUPDATE);
00109         realvec& innerOut = acc.to<mrs_realvec>();
00110         innerOut.create(marsystems_[i]->ctrl_onObservations_->to<mrs_natural>(),marsystems_[i]->ctrl_onSamples_->to<mrs_natural>());
00111       }
00112     }
00113   }
00114 }
00115 
00116 void
00117 FlowThru::myProcess(realvec& in, realvec& out)
00118 {
00119   // Add assertions about sizes [!]
00120 
00121   //input should be passed thru the output untouched!
00122   out = in;
00123 
00124   child_count_t child_count = marsystems_.size();
00125   if(child_count >= 1)
00126   {
00127     for (child_count_t i = 0; i < child_count; ++i)
00128     {
00129       if (i==0)
00130       {
00131         MarControlAccessor acc(marsystems_[i]->ctrl_processedData_);
00132         realvec& slice = acc.to<mrs_realvec>();
00133         marsystems_[i]->process(in, slice);
00134       }
00135       else if (i == child_count-1)
00136       {
00137         MarControlAccessor accSlice(marsystems_[i-1]->ctrl_processedData_, true, true);
00138         realvec& slice = accSlice.to<mrs_realvec>();
00139         MarControlAccessor accInnerOut(ctrl_innerOut_);
00140         realvec& innerOut = accInnerOut.to<mrs_realvec>();
00141 
00142 
00143         marsystems_[i]->process(slice, innerOut);
00144       }
00145       else
00146       {
00147         MarControlAccessor acc1(marsystems_[i-1]->ctrl_processedData_, true, true);
00148         realvec& slice1 = acc1.to<mrs_realvec>();
00149         MarControlAccessor acc2(marsystems_[i]->ctrl_processedData_);
00150         realvec& slice2 = acc2.to<mrs_realvec>();
00151         marsystems_[i]->process(slice1, slice2);
00152       }
00153     }
00154   }
00155   else if(child_count == 0) //composite has no children!
00156   {
00157     MRSWARN("FlowThru::process: composite has no children MarSystems - passing input to output without changes.");
00158   }
00159 }