Marsyas  0.6.0-alpha
/usr/src/RPM/BUILD/marsyas-0.6.0/src/marsyas/marsystems/ResampleLinear.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 "ResampleLinear.h"
00020 #include "../common_source.h"
00021 
00022 using namespace std;
00023 using namespace Marsyas;
00024 
00025 ResampleLinear::ResampleLinear(mrs_string name):MarSystem("ResampleLinear", name)
00026 {
00027   addControls();
00028 }
00029 
00030 ResampleLinear::ResampleLinear(const ResampleLinear& a) : MarSystem(a)
00031 {
00032   ctrl_stretch_ = getctrl("mrs_real/stretch");
00033   ctrl_samplingRateAdjustmentMode_ = getctrl("mrs_bool/samplingRateAdjustmentMode");
00034 }
00035 
00036 
00037 ResampleLinear::~ResampleLinear()
00038 {
00039 }
00040 
00041 MarSystem*
00042 ResampleLinear::clone() const
00043 {
00044   return new ResampleLinear(*this);
00045 }
00046 
00047 void
00048 ResampleLinear::addControls()
00049 {
00050   addctrl("mrs_real/stretch", 1.0, ctrl_stretch_);
00051   addctrl("mrs_bool/samplingRateAdjustmentMode", (mrs_bool)true , ctrl_samplingRateAdjustmentMode_);
00052 
00053   setctrlState("mrs_real/stretch", true);
00054   setctrlState("mrs_bool/samplingRateAdjustmentMode",(mrs_bool)true);
00055 }
00056 
00057 void
00058 ResampleLinear::myUpdate(MarControlPtr sender)
00059 {
00060   MarSystem::myUpdate(sender);
00061   MRSDIAG("ResampleLinear.cpp - ResampleLinear:myUpdate");
00062   mrs_real alpha = ctrl_stretch_->to<mrs_real>();
00063   ctrl_onSamples_->setValue((mrs_natural) (alpha * ctrl_inSamples_->to<mrs_natural>()), NOUPDATE);
00064   ctrl_onObservations_->setValue(ctrl_inObservations_->to<mrs_natural>());
00065 
00066   if (!(ctrl_samplingRateAdjustmentMode_->to<mrs_bool>()))
00067   {
00068     alpha=1.0;
00069   }
00070 
00071   ctrl_osrate_->setValue(ctrl_israte_->to<mrs_real>()*alpha);
00072 
00073   mrs_string inObsNames = ctrl_inObsNames_->to<mrs_string>();
00074   // Add prefix to the observation names.
00075   ctrl_onObsNames_->setValue(obsNamesAddPrefix(inObsNames, "ResampleLinear_"), NOUPDATE);
00076 
00077 }
00078 
00079 void
00080 ResampleLinear::myProcess(realvec& in, realvec& out)
00081 {
00082   mrs_real tp;
00083   mrs_natural tl, tr;
00084   mrs_real alpha = ctrl_stretch_->to<mrs_real>();
00085   mrs_natural o,t;
00086 
00087   for (o=0; o < onObservations_; o++)
00088   {
00089     for (t = 0; t < onSamples_; t++)
00090     {
00091       tp = t / alpha;
00092       tl= (mrs_natural)tp;
00093       tr = tl + 1;
00094       if (tr<inSamples_)  // check tr because tr == inSamples_ might happen
00095       {
00096         out(o,t) = (tr-tp) * in(o,tl) + (tp-tl) * in(o,tr);
00097       }
00098       else // reflect on boundary
00099       {
00100         out(o,t) = in(o,inSamples_-1); //  alternative possibilities would include:
00101 
00102         //periodic on boundary:
00103         //out(o,t) = in(o,0);
00104 
00105         //zero pad on boundary:
00106         //out(o,t) == 0;
00107       }
00108     }
00109   }
00110 }