Marsyas  0.6.0-alpha
/usr/src/RPM/BUILD/marsyas-0.6.0/src/marsyas/marsystems/DelaySamples.cpp
Go to the documentation of this file.
00001 /*
00002  ** Copyright (C) 2010 Stefaan Lippens
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 "../common_source.h"
00020 #include "DelaySamples.h"
00021 
00022 using std::ostringstream;
00023 using namespace Marsyas;
00024 
00025 DelaySamples::DelaySamples(mrs_string name) :
00026   MarSystem("DelaySamples", name)
00027 {
00029   addControls();
00030 }
00031 
00032 DelaySamples::DelaySamples(const DelaySamples& a) :
00033   MarSystem(a)
00034 {
00037   ctrl_delay_ = getctrl("mrs_natural/delay");
00038 }
00039 
00040 DelaySamples::~DelaySamples()
00041 {
00042 }
00043 
00044 MarSystem*
00045 DelaySamples::clone() const
00046 {
00047   return new DelaySamples(*this);
00048 }
00049 
00051 void DelaySamples::addControls()
00052 {
00053   addctrl("mrs_natural/delay", 0, ctrl_delay_);
00054   setctrlState("mrs_natural/delay", true);
00055 }
00056 
00057 void DelaySamples::myUpdate(MarControlPtr sender)
00058 {
00060   MarSystem::myUpdate(sender);
00061 
00062   // Handle/cache delay value for in myProcess
00063   delay_ = ctrl_delay_->to<mrs_natural> ();
00064   if (delay_ < 0)
00065   {
00066     setctrl("mrs_natural/delay", 0);
00067     delay_ = 0;
00068   }
00069 
00070   // Prefix observation names with "DelayX".
00071   mrs_string inObsNames = ctrl_inObsNames_->to<mrs_string>();
00072   ostringstream oss;
00073   oss << "DelaySamples" << delay_ << "_";
00074   mrs_string onObsNames = obsNamesAddPrefix(inObsNames, oss.str());
00075   ctrl_onObsNames_->setValue(onObsNames, NOUPDATE);
00076 
00077   // Allocate and initialize the buffers.
00078   this->memory_.stretch(inObservations_, delay_);
00079   this->memory_.setval(0.0);
00080 }
00081 
00082 void DelaySamples::myProcess(realvec& in, realvec& out)
00083 {
00084   mrs_natural t, o;
00085   // The number of samples we'll fetch from and store to the memory buffer.
00086   mrs_natural memory_part = delay_ < inSamples_ ? delay_ : inSamples_;
00087   for (o = 0; o < inObservations_; o++)
00088   {
00089     // Fetch initial part from memory.
00090     for (t = 0; t < memory_part; t++)
00091     {
00092       out(o, t) = memory_(o, t);
00093     }
00094     // Copy from input if needed/possible (delay_ < inSamples_).
00095     for (t = delay_; t < inSamples_; t++)
00096     {
00097       out(o, t) = in(o, t - delay_);
00098     }
00099     // Shift memory if needed. (inSamples < delay_).
00100     for (t = 0; t < delay_ - inSamples_; t++)
00101     {
00102       memory_(o, t) = memory_(o, t + inSamples_);
00103     }
00104     // Put appropriate part of input in memory.
00105     for (t = 0; t < memory_part; t++)
00106     {
00107       memory_(o, delay_ - 1 - t) = in(o, inSamples_ - 1 - t);
00108     }
00109   }
00110 }