Marsyas  0.6.0-alpha
/usr/src/RPM/BUILD/marsyas-0.6.0/src/marsyas/marsystems/Sum.cpp
Go to the documentation of this file.
00001 /*
00002 ** Copyright (C) 1998-2011 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 "Sum.h"
00020 
00021 using namespace std;
00022 using namespace Marsyas;
00023 
00024 Sum::Sum(mrs_string name):MarSystem("Sum",name)
00025 {
00026   addControls();
00027 }
00028 
00029 Sum::Sum(const Sum& a): MarSystem(a)
00030 {
00031   ctrl_weight_ = getctrl("mrs_real/weight");
00032   ctrl_stereo_ = getctrl("mrs_bool/stereo");
00033   ctrl_mode_ = getctrl("mrs_string/mode");
00034   setctrlState("mrs_string/mode", true);
00035 }
00036 
00037 
00038 
00039 Sum::~Sum()
00040 {
00041 }
00042 
00043 void
00044 Sum::addControls()
00045 {
00046   addctrl("mrs_real/weight", 1.0, ctrl_weight_);
00047   addctrl("mrs_bool/stereo", false, ctrl_stereo_);
00048   addctrl("mrs_string/mode", "orig", ctrl_mode_);
00049 }
00050 
00051 MarSystem*
00052 Sum::clone() const
00053 {
00054   return new Sum(*this);
00055 }
00056 
00057 void
00058 Sum::myUpdate(MarControlPtr sender)
00059 {
00060   (void) sender;  //suppress warning of unused parameter(s)
00061   // // Start with the default MarSystem setup with equal input/output
00062   // //stream format ...
00063   // MarSystem::myUpdate(sender);
00064   ctrl_onSamples_->setValue(ctrl_inSamples_, NOUPDATE);
00065   ctrl_onObservations_->setValue(ctrl_inObservations_, NOUPDATE);
00066   ctrl_osrate_->setValue(ctrl_israte_, NOUPDATE);
00067 
00068 
00069 
00070   // get name of first observation and use for sum
00071   mrs_string inObsName = stringSplit(ctrl_inObsNames_->to<mrs_string>(), ",")[0];
00072   ctrl_onObsNames_->setValue("Sum_" + inObsName+",", NOUPDATE);
00073 
00074 
00075   // sness - Do what the MarSystem did before we refactored it, just in case
00076   // other people are depending on the old behaviour.  In the future, probably
00077   // refactor this code out
00078 
00079   if(ctrl_mode_->to<mrs_string>() == "orig") {
00081     mrs_bool stereo = ctrl_stereo_->to<mrs_bool>();
00082     if (!stereo) {
00083       ctrl_onObservations_->setValue(1, NOUPDATE);
00084     } else {
00085       ctrl_onObservations_->setValue(2, NOUPDATE);
00086     }
00087   } else {
00088 
00089     // sness - New Sum code
00090     if (ctrl_mode_->to<mrs_string>() == "sum_observations") {
00091       ctrl_onObservations_->setValue(ctrl_inObservations_, NOUPDATE);
00092       ctrl_onSamples_->setValue(1, NOUPDATE);
00093       ctrl_osrate_->setValue(ctrl_israte_->to<mrs_real>() / ctrl_inSamples_->to<mrs_natural>(), NOUPDATE);
00094 
00095     } else if (ctrl_mode_->to<mrs_string>() == "sum_samples") {
00096       ctrl_onObservations_->setValue(1, NOUPDATE);
00097       ctrl_onSamples_->setValue(ctrl_inSamples_, NOUPDATE);
00098     } else if (ctrl_mode_->to<mrs_string>() == "sum_whole") {
00099       ctrl_onObservations_->setValue(1, NOUPDATE);
00100       ctrl_onSamples_->setValue(1, NOUPDATE);
00101     }
00102   }
00103 
00104 
00105 
00106 }
00107 
00108 
00109 void
00110 Sum::myProcess(realvec& in, realvec& out)
00111 {
00112   mrs_real weightValue = ctrl_weight_->to<mrs_real>();
00113   mrs_bool stereo = ctrl_stereo_->to<mrs_bool>();
00114   mrs_natural t,o,c;
00115 
00116   // sness - Do what the MarSystem did before we refactored it, just in case
00117   // other people are depending on the old behaviour.  In the future, probably
00118   // refactor this code out
00119   if (ctrl_mode_->to<mrs_string>() == "orig")
00120   {
00121     // Sum the observation channels per sample.
00122     if (!stereo)
00123     {
00124       for (t = 0; t < inSamples_; t++)
00125       {
00126         out(0, t) = 0;
00127         for (o = 0; o < inObservations_; o++)
00128         {
00129           out(0, t) += (weightValue * in(o, t));
00130         }
00131       }
00132     }
00133     else            // stereo
00134     {
00135       for (t = 0; t < inSamples_; t++)
00136         for (c=0; c < 2; ++c)
00137         {
00138           out(c, t) = 0;
00139           for (o = c; o < inObservations_; o+=2)
00140           {
00141             out(c, t) += (weightValue * in(o, t));
00142           }
00143         }
00144     }
00145   } else {
00146     // sness - New Sum code
00147     for (o = 0; o < onObservations_; o++) {
00148       for (t = 0; t < onSamples_; t++) {
00149         out(o,t) = 0.0;
00150       }
00151     }
00152     // AL: why not replace this with out.setval for readability?
00153 
00154     if (ctrl_mode_->to<mrs_string>() == "sum_observations") {
00155       for (o = 0; o < inObservations_; o++) {
00156         for (t = 0; t < inSamples_; t++) {
00157           out(o,0) += in(o,t);
00158         }
00159       }
00160     } else if (ctrl_mode_->to<mrs_string>() == "sum_samples")
00161     {
00162       for (o = 0; o < inObservations_; o++) {
00163         for (t = 0; t < inSamples_; t++) {
00164           out(0,t) += in(o,t);
00165         }
00166       }
00167     } else if (ctrl_mode_->to<mrs_string>() == "sum_whole") {
00168       for (o = 0; o < inObservations_; o++) {
00169         for (t = 0; t < inSamples_; t++) {
00170           out(0,0) += in(o,t);
00171         }
00172       }
00173     }
00174   }
00175 }