Marsyas  0.6.0-alpha
/usr/src/RPM/BUILD/marsyas-0.6.0/src/marsyas/marsystems/NoiseGate.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 "NoiseGate.h"
00020 #include "../common_source.h"
00021 
00022 using std::ostringstream;
00023 using namespace Marsyas;
00024 
00025 NoiseGate::NoiseGate(mrs_string name):MarSystem("NoiseGate",name)
00026 {
00027   //type_ = "NoiseGate";
00028   //name_ = name;
00029 
00030   state_ = 1.0;
00031   xdprev_ = 0.0;
00032   alpha_ = 0.0;
00033   gainsprev_ = 1.0;
00034 
00035   addControls();
00036 }
00037 
00038 
00039 NoiseGate::~NoiseGate()
00040 {
00041 }
00042 
00043 
00044 MarSystem*
00045 NoiseGate::clone() const
00046 {
00047   return new NoiseGate(*this);
00048 }
00049 
00050 void
00051 NoiseGate::addControls()
00052 {
00053   addctrl("mrs_real/thresh", 0.1);
00054   addctrl("mrs_real/release", 0.5);
00055   addctrl("mrs_real/rolloff", .130);
00056   addctrl("mrs_real/at", 0.0001);
00057   addctrl("mrs_real/rt", 0.130);
00058   addctrl("mrs_real/slope", 1.0);
00059 }
00060 
00061 void
00062 NoiseGate::myUpdate(MarControlPtr sender)
00063 {
00064   (void) sender;  //suppress warning of unused parameter(s)
00065   MRSDIAG("NoiseGate.cpp - NoiseGate:myUpdate");
00066 
00067   setctrl("mrs_natural/onSamples", getctrl("mrs_natural/inSamples"));
00068   setctrl("mrs_natural/onObservations", getctrl("mrs_natural/inObservations"));
00069   setctrl("mrs_real/osrate", getctrl("mrs_real/israte"));
00070 
00071   //defaultUpdate(); [!]
00072   inSamples_ = getctrl("mrs_natural/inSamples")->to<mrs_natural>();
00073 
00074   xd_.create(inSamples_);
00075   gains_.create(inSamples_);
00076 }
00077 
00078 
00079 void
00080 NoiseGate::myProcess(realvec& in, realvec& out)
00081 {
00082   mrs_natural t,o;
00083   //checkFlow(in,out);
00084 
00085   mrs_real thresh = getctrl("mrs_real/thresh")->to<mrs_real>();
00086   mrs_real release = getctrl("mrs_real/release")->to<mrs_real>();
00087   mrs_real rolloff = getctrl("mrs_real/rolloff")->to<mrs_real>();
00088   mrs_real at = getctrl("mrs_real/at")->to<mrs_real>();
00089   mrs_real rt = getctrl("mrs_real/rt")->to<mrs_real>();
00090   // FIXME This variable is defined but unused.
00091   // mrs_real slope = getctrl("mrs_real/slope")->to<mrs_real>();
00092 
00093   // calculate rolloff, at and rt time
00094   at = 1 - exp(-2.2/(22050*at));
00095   rt = 1 - exp(-2.2/(22050*rt));
00096 
00097   for (o = 0; o < inObservations_; o++)
00098     for (t = 0; t < inSamples_; t++)
00099     {
00100       // Calculates the current amplitude of signal and incorporates
00101       // the at and rt times into xd(o,t)
00102       alpha_ = fabs(in(o,t)) - xdprev_;
00103       if (alpha_ < 0)
00104       {
00105         alpha_ = 0;
00106       }
00107       xdprev_=xdprev_*(1-rt)+at*alpha_;
00108 
00109       if (state_ == 1.0)
00110       {
00111         if (xdprev_ < thresh)
00112         {
00113           gains_(o,t) = gainsprev_*rolloff;
00114           state_ = 0.0;
00115         }
00116         else
00117         {
00118           gains_(o,t) = 1;
00119         }
00120       }
00121       else
00122       {
00123         if (xdprev_ < release)
00124         {
00125           gains_(o,t) = gainsprev_*rolloff;
00126           // rolloff time
00127         }
00128         else if (xdprev_ > release)
00129         {
00130           gains_(o,t) = 1.0;
00131           state_ = 1.0;
00132         }
00133         else
00134         {
00135           gains_(o,t) = 0.0;
00136         }
00137       }
00138 
00139       gainsprev_ = gains_(o,t);
00140       out(o,t) =  gainsprev_ * in(o,t);
00141 
00142     }
00143 }
00144 
00145 
00146 
00147 
00148 
00149 
00150 
00151