Marsyas  0.6.0-alpha
/usr/src/RPM/BUILD/marsyas-0.6.0/src/marsyas/marsystems/Memory.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 "Memory.h"
00020 #include "../common_source.h"
00021 
00022 
00023 using std::cout;
00024 using std::endl;
00025 
00026 using std::ostringstream;
00027 using namespace Marsyas;
00028 
00029 
00030 Memory::Memory(mrs_string name):MarSystem("Memory",name)
00031 {
00032   end_ = 0;
00033   counter_since_reset_ = 0;
00034   addControls();
00035 }
00036 
00037 Memory::~Memory()
00038 {
00039 }
00040 
00041 Memory::Memory(const Memory& a):MarSystem(a)
00042 {
00043   end_ = 0;
00044   counter_since_reset_ = 0 ;
00045   ctrl_reset_ = getctrl("mrs_bool/reset");
00046   ctrl_memSize_ = getctrl("mrs_natural/memSize");
00047 }
00048 
00049 MarSystem*
00050 Memory::clone() const
00051 {
00052   return new Memory(*this);
00053 }
00054 
00055 void
00056 Memory::addControls()
00057 {
00058   addctrl("mrs_natural/memSize", 40, ctrl_memSize_);
00059   setctrlState("mrs_natural/memSize", true);
00060   addctrl("mrs_bool/reset", false, ctrl_reset_);
00061   setctrlState("mrs_bool/reset", true);
00062 }
00063 
00064 void
00065 Memory::myUpdate(MarControlPtr sender)
00066 {
00067   (void) sender;  //suppress warning of unused parameter(s)
00068   MRSDIAG("Memory.cpp - Memory:myUpdate");
00069 
00070 
00071   mrs_natural memSize = ctrl_memSize_->to<mrs_natural>();
00072 
00073   if (memSize != 0)
00074   {
00075     ctrl_onSamples_->setValue(ctrl_inSamples_->to<mrs_natural>() * memSize,
00076                               NOUPDATE);
00077     ctrl_onObservations_->setValue(ctrl_inObservations_, NOUPDATE);
00078     ctrl_osrate_->setValue(ctrl_israte_, NOUPDATE);
00079   }
00080   else
00081   {
00082     ctrl_onSamples_->setValue(ctrl_inSamples_->to<mrs_natural>(), NOUPDATE);
00083     ctrl_onObservations_->setValue(ctrl_inObservations_, NOUPDATE);
00084     ctrl_osrate_->setValue(ctrl_israte_, NOUPDATE);
00085 
00086   }
00087 
00088   inObservations_ = ctrl_inObservations_->to<mrs_natural>();
00089   onObservations_ = ctrl_onObservations_->to<mrs_natural>();
00090   onSamples_ = ctrl_onSamples_->to<mrs_natural>();
00091   cir_out_.stretch(onObservations_, onSamples_);
00092   ostringstream oss;
00093   mrs_string inObsNames = ctrl_inObsNames_->to<mrs_string>();
00094   for (int i = 0; i < inObservations_; ++i)
00095   {
00096     mrs_string inObsName;
00097     mrs_string temp;
00098     inObsName = inObsNames.substr(0, inObsNames.find(","));
00099     temp = inObsNames.substr(inObsNames.find(",")+1, inObsNames.length());
00100     inObsNames = temp;
00101     oss << "Mem" << memSize << "_" << inObsName << ",";
00102   }
00103   ctrl_onObsNames_->setValue(oss.str(), NOUPDATE);
00104 
00105 }
00106 
00107 void
00108 Memory::myProcess(realvec& in, realvec& out)
00109 {
00110   mrs_natural t,o,j;
00111   mrs_natural memSize = ctrl_memSize_->to<mrs_natural>();
00112 
00113   if (ctrl_reset_->to<mrs_bool>())
00114   {
00115     cir_out_.setval(0.0);
00116     end_ = 0;
00117     counter_since_reset_ = 0;
00118 
00119     // fill up with copies of first incoming vector
00120     for (j=0; j < onSamples_; j++)
00121     {
00122       for (o=0; o < inObservations_; o++)
00123       {
00124         cir_out_(o, end_) = in(o,0);
00125       }
00126       end_ = (end_ + 1) % onSamples_;       // circular buffer index
00127     }
00128 
00129   }
00130 
00131   if (memSize != 0)
00132   {
00133     for (t=0; t < inSamples_; t++)
00134     {
00135       for (o=0; o < inObservations_; o++)
00136       {
00137         cir_out_(o, end_) = in(o,t);
00138       }
00139       end_ = (end_ + 1) % onSamples_;       // circular buffer index
00140     }
00141     counter_since_reset_++;
00142   }
00143   else // memSize == 0
00144   {
00145     for (t = 0; t < inSamples_; t++)
00146     {
00147       for (o=0; o < inObservations_; o++)
00148       {
00149         cir_out_(o,t) = in(o,t);
00150       }
00151     }
00152   }
00153 
00154   mrs_natural i_cir = end_;
00155   for (t = 0; t < onSamples_; t++)
00156   {
00157     for (o=0; o < inObservations_; o++)
00158     {
00159       out(o, t) = cir_out_(o, i_cir);
00160     }
00161     i_cir = (i_cir + 1) % onSamples_;
00162   }
00163 
00164 
00165   //MATLAB_PUT(out, "Memory_out");
00166   //MATLAB_EVAL("plot(Memory_out);");
00167 }