Marsyas  0.6.0-alpha
/usr/src/RPM/BUILD/marsyas-0.6.0/src/marsyas/marsystems/DownSampler.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 
00020 #include "DownSampler.h"
00021 #include "../common_source.h"
00022 
00023 using std::ostringstream;
00024 using namespace Marsyas;
00025 
00026 
00027 DownSampler::DownSampler(mrs_string name):MarSystem("DownSampler",name)
00028 {
00029   addControls();
00030 }
00031 
00032 DownSampler::~DownSampler()
00033 {
00034 }
00035 
00036 DownSampler::DownSampler(const DownSampler& a) : MarSystem(a)
00037 {
00040   ctrl_factor_ = getControl("mrs_natural/factor");
00041 }
00042 
00043 MarSystem*
00044 DownSampler::clone() const
00045 {
00046   return new DownSampler(*this);
00047 }
00048 
00049 void
00050 DownSampler::addControls()
00051 {
00052   addControl("mrs_natural/factor", 2, ctrl_factor_);
00053   ctrl_factor_->setState(true);
00054 }
00055 
00056 void
00057 DownSampler::myUpdate(MarControlPtr sender)
00058 {
00059   MRSDIAG("DownSampler.cpp - DownSampler:myUpdate");
00060 
00061   // Use the default MarSystem setup with equal input/output stream format ...
00062   MarSystem::myUpdate(sender);
00063 
00064   // ... but override the onSamples and osrate settings based on the
00065   // downsample factor.
00066   mrs_natural factor = ctrl_factor_->to<mrs_natural>();
00067   ctrl_osrate_->setValue(ctrl_israte_->to<mrs_real>() / factor, NOUPDATE);
00068   // When inSamples is divisible by factor, the number of output samples
00069   // onSamples is just the integer division inSamples / factor.
00070   // Otherwise, onSamples is the biggest integer obeying
00071   //   factor * (onSamples - 1) < inSamples
00072   // which is ceil(inSamples / factor).
00073   mrs_natural onSamples = (mrs_natural) ceil((mrs_real)(ctrl_inSamples_->to<mrs_natural>()) / factor);
00074   ctrl_onSamples_->setValue(onSamples, NOUPDATE);
00075 }
00076 
00077 void
00078 DownSampler::myProcess(realvec& in, realvec& out)
00079 {
00080   mrs_natural t,o;
00081   mrs_natural factor = ctrl_factor_->to<mrs_natural>();
00082 
00083   // Only copy samples at multiples of factor.
00084   for (o=0; o < inObservations_; o++)
00085   {
00086     for (t = 0; t < onSamples_; t++)
00087     {
00088       out(o, t) = in(o, t * factor);
00089     }
00090   }
00091 }