Marsyas  0.6.0-alpha
/usr/src/RPM/BUILD/marsyas-0.6.0/src/marsyas/marsystems/Resample.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 "Resample.h"
00020 
00021 using namespace std;
00022 using namespace Marsyas;
00023 
00033 Resample::Resample(mrs_string name):MarSystem("Resample", name)
00034 {
00035   //Add any specific controls needed by Resample
00036   //(default controls all MarSystems should have
00037   //were already added by MarSystem::addControl(),
00038   //called by :MarSystem(name) constructor).
00039   //If no specific controls are needed by a MarSystem
00040   //there is no need to implement and call this addControl()
00041   //method (see for e.g. Rms.cpp)
00042 
00043   interpolator_=new ResampleLinear("resa");
00044   addControls();
00045 }
00046 
00047 Resample::Resample(const Resample& orig) : MarSystem(orig)
00048 {
00049   // For any MarControlPtr in a MarSystem
00050   // it is necessary to perform this getctrl
00051   // in the copy constructor in order for cloning to work
00052 
00053 
00054 
00055 
00056   //private:
00057   //  //Add specific controls needed by this MarSystem.
00058   //    void addControls();
00059   //    MarControlPtr ctrl_option_;
00060   //    MarControlPtr ctrl_samplingRateAdjustmentMode_;
00061   //    MarControlPtr ctrl_offStart_;
00062   //    MarControlPtr ctrl_offEnd_;
00063   //    MarControlPtr ctrl_newSamplingRate_;
00064   //    MarControlPtr ctrl_resamplingMode_;
00065   //    MarSystem* interpolator_;
00066   //    //mrs_string resaModeOld_;
00067   //    void myUpdate(MarControlPtr sender);
00068 
00069 
00070   //public:
00071   //  Resample(std::string name);
00072   //    Resample(const Resample& a);
00073   //  ~Resample();
00074   //  MarSystem* clone() const;
00075   //  //MarSystem* Resample::getInterpolator() const;
00076 
00077   //  void myProcess(realvec& in, realvec& out);
00078 
00079 
00080 
00081   ctrl_offStart_ = getctrl("mrs_real/offStart");
00082   ctrl_offEnd_ = getctrl("mrs_real/offEnd");
00083   ctrl_option_ = getctrl("mrs_bool/option");
00084   ctrl_resamplingMode_ = getctrl("mrs_string/resamplingMode");
00085   ctrl_samplingRateAdjustmentMode_ = getctrl("mrs_bool/samplingRateAdjustmentMode");
00086   ctrl_newSamplingRate_ = getctrl("mrs_real/newSamplingRate");
00087 
00088 
00089   //resaModeOld_=orig.resaModeOld_;
00090 
00091 
00092   //interpolator_=orig.getInterpolator();
00093   interpolator_=orig.interpolator_->clone();
00094 }
00095 
00096 Resample::~Resample()
00097 {
00098 
00099   delete interpolator_;
00100 }
00101 
00102 MarSystem*
00103 Resample::clone() const
00104 {
00105   return new Resample(*this);
00106 }
00107 
00108 
00109 void
00110 Resample::addControls()
00111 {
00112   //Add specific controls needed by this MarSystem.
00113   addctrl("mrs_real/offStart", 0.0, ctrl_offStart_);
00114   addctrl("mrs_real/offEnd", 0.0, ctrl_offEnd_);
00115   addctrl("mrs_bool/samplingRateAdjustmentMode", (mrs_bool)true , ctrl_samplingRateAdjustmentMode_);
00116   addctrl("mrs_string/resamplingMode", "linear" , ctrl_resamplingMode_);
00117   addctrl("mrs_bool/option", (mrs_bool)false , ctrl_option_);
00118   addctrl("mrs_real/newSamplingRate", 22050.0 , ctrl_newSamplingRate_);
00119   
00120   setctrlState("mrs_bool/samplingRateAdjustmentMode",(mrs_bool)true);
00121   setctrlState("mrs_real/newSamplingRate",(mrs_bool)true);
00122   setctrlState("mrs_string/resamplingMode",(mrs_bool)true);
00123 }
00124 
00125 void
00126 Resample::myUpdate(MarControlPtr sender)
00127 {
00128   MarSystem::myUpdate(sender);
00129 
00130 
00131   mrs_real alpha = ctrl_newSamplingRate_->to<mrs_real>()/ctrl_israte_->to<mrs_real>();
00132 
00133   ctrl_onSamples_->setValue((mrs_natural) (alpha * ctrl_inSamples_->to<mrs_natural>()), NOUPDATE);
00134   ctrl_onObservations_->setValue(ctrl_inObservations_->to<mrs_natural>());
00135   if (!(ctrl_samplingRateAdjustmentMode_->to<mrs_bool>()))
00136   {
00137     alpha=1.0;
00138   }
00139 
00140   ctrl_osrate_->setValue(ctrl_israte_->to<mrs_real>()*alpha);
00141 
00142   mrs_string inObsNames = ctrl_inObsNames_->to<mrs_string>();
00143   // Add prefix to the observation names. -> will be done by submarsystems
00144   //ctrl_onObsNames_->setValue(obsNamesAddPrefix(inObsNames, "Resample_"), NOUPDATE);
00145 
00146 
00147   mrs_string resaMode=ctrl_resamplingMode_->to<mrs_string>();
00148   //if (resaMode!=resaModeOld_)
00149   //{
00150   //    resaModeOld_=resaMode;
00151 
00152 
00153   delete interpolator_;
00154   interpolator_ = NULL;
00155   if (resaMode==(mrs_string)"sincip")
00156     {
00157       interpolator_= new ResampleSinc("resa");
00158       interpolator_->updControl("mrs_real/offStart", ctrl_offStart_->to<mrs_real>());
00159       interpolator_->updControl("mrs_real/offEnd", ctrl_offEnd_->to<mrs_real>());
00160 
00161       interpolator_->updControl("mrs_bool/windowedMode", ctrl_option_->to<mrs_bool>());
00162     }
00163   else if (resaMode==(mrs_string)       "bezier")
00164     {
00165       interpolator_= new ResampleBezier("resa");
00166       interpolator_->updControl("mrs_real/offStart", ctrl_offStart_->to<mrs_real>());
00167       interpolator_->updControl("mrs_real/offEnd", ctrl_offEnd_->to<mrs_real>());
00168 
00169       interpolator_->updControl("mrs_bool/tangentMode", ctrl_option_);
00170     }
00171   else if (resaMode==(mrs_string)       "near")
00172     {
00173       interpolator_= new ResampleNearestNeighbour("resa");
00174     }
00175   else
00176     {
00177       interpolator_= new ResampleLinear("resa");
00178     }
00179   
00180   if (interpolator_ != NULL)
00181     {
00182       interpolator_->updControl("mrs_bool/samplingRateAdjustmentMode", ctrl_samplingRateAdjustmentMode_->to<mrs_bool>());
00183       interpolator_->updControl("mrs_real/stretch", ctrl_newSamplingRate_->to<mrs_real>()/ctrl_israte_->to<mrs_real>());
00184       interpolator_->updControl("mrs_natural/inSamples", inSamples_);
00185       interpolator_->updControl("mrs_natural/inObservations", inObservations_);
00186     }
00187 }
00188 
00189 
00190 void
00191 Resample::myProcess(realvec& in, realvec& out)
00192 {
00193   interpolator_->process(in,out);
00194 }