Marsyas  0.6.0-alpha
/usr/src/RPM/BUILD/marsyas-0.6.0/src/marsyas/marsystems/Cascade.cpp
Go to the documentation of this file.
00001 /*
00002 ** Copyright (C) 1998-2010 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 "Cascade.h"
00020 
00021 
00022 using std::ostringstream;
00023 using std::vector;
00024 
00025 using namespace Marsyas;
00026 
00027 Cascade::Cascade(mrs_string name):
00028   MarSystem("Cascade", name),
00029   m_valid_output(false)
00030 {
00031   isComposite_ = true;
00032 }
00033 
00034 Cascade::Cascade(const Cascade & other):
00035   MarSystem(other),
00036   m_valid_output(false)
00037 {
00038 }
00039 
00040 Cascade::~Cascade()
00041 {
00042 }
00043 
00044 MarSystem*
00045 Cascade::clone() const
00046 {
00047   return new Cascade(*this);
00048 }
00049 
00050 void
00051 Cascade::myUpdate(MarControlPtr sender)
00052 {
00053   child_count_t child_count = marsystems_.size();
00054 
00055   m_valid_output = true;
00056 
00057   if (!child_count)
00058   {
00059     MarSystem::myUpdate(sender);
00060     return;
00061   }
00062 
00063   m_child_info.resize(child_count);
00064 
00065   ostringstream out_obs_names;
00066   mrs_natural out_observations = 0;
00067   mrs_natural out_samples = 0;
00068   mrs_real out_sample_rate = 0.0;
00069 
00070   MarControlPtr observations = getControl("mrs_natural/inObservations");
00071   MarControlPtr samples = getControl("mrs_natural/inSamples");
00072   MarControlPtr rate = getControl("mrs_real/israte");
00073   MarControlPtr obs_names = getControl("mrs_string/inObsNames");
00074 
00075   for (child_count_t i=0; i < child_count; ++i)
00076   {
00077     MarSystem * child = marsystems_[i];
00078     system_info & child_info = m_child_info[i];
00079 
00080     child->setctrl("mrs_natural/inObservations", observations);
00081     child->setctrl("mrs_natural/inSamples", samples);
00082     child->setctrl("mrs_real/israte", rate);
00083     child->setctrl("mrs_string/inObsNames", obs_names);
00084     observations = child->getControl("mrs_natural/onObservations");
00085     samples = child->getControl("mrs_natural/onSamples");
00086     rate = child->getControl("mrs_real/osrate");
00087     obs_names = child->getControl("mrs_string/onObsNames");
00088 
00089     mrs_natural child_out_observations = observations->to<mrs_natural>();
00090     mrs_natural child_out_samples = samples->to<mrs_natural>();
00091     mrs_natural child_out_sr = rate->to<mrs_real>();
00092 
00093     child_info.buffer.create(child_out_observations, child_out_samples);
00094 
00095     if (i == 0)
00096     {
00097       out_samples = child_out_samples;
00098       out_sample_rate = child_out_sr;
00099     }
00100     else if ( (out_samples != child_out_samples) ||
00101               (out_sample_rate != child_out_sr) )
00102     {
00103       m_valid_output = false;
00104     }
00105 
00106     out_observations += child_out_observations;
00107     out_obs_names << obs_names;
00108   }
00109 
00110   if (!m_valid_output)
00111     MRSWARN("Cascade: children have incompatible output formats. Output disabled.");
00112 
00113   //forward flow propagation
00114   setctrl(ctrl_onSamples_, out_samples);
00115   setctrl(ctrl_onObservations_, out_observations);
00116   setctrl(ctrl_osrate_, out_sample_rate);
00117   setctrl(ctrl_onObsNames_, out_obs_names.str());
00118 }
00119 
00120 void
00121 Cascade::myProcess(realvec& in, realvec& out)
00122 {
00123   child_count_t child_count = marsystems_.size();
00124 
00125   if (!m_valid_output)
00126     return;
00127 
00128   if (!child_count)
00129   {
00130     out = in;
00131     return;
00132   }
00133 
00134   if (child_count == 1)
00135   {
00136     marsystems_[0]->process(in, out);
00137     return;
00138   }
00139 
00140   mrs_natural out_obs_offset = 0;
00141 
00142   {
00143     realvec & child_out = m_child_info[0].buffer;
00144 
00145     marsystems_[0]->process(in, child_out);
00146 
00147     for (mrs_natural o = 0; o < child_out.getRows(); o++)
00148       for (mrs_natural t = 0; t < onSamples_; t++)
00149         out(o, t) = child_out(o, t);
00150 
00151     out_obs_offset = child_out.getRows();
00152   }
00153 
00154   for (child_count_t i = 1; i < child_count; ++i)
00155   {
00156     realvec & child_out = m_child_info[i].buffer;
00157     realvec & prev_child_out = m_child_info[i-1].buffer;
00158 
00159     marsystems_[i]->process(prev_child_out, child_out);
00160 
00161     for (mrs_natural o = 0; o < child_out.getRows(); o++)
00162       for (mrs_natural t = 0; t < onSamples_; t++)
00163         out(out_obs_offset + o, t) = child_out(o, t);
00164 
00165     out_obs_offset += child_out.getRows();
00166   }
00167 }