Marsyas  0.6.0-alpha
/usr/src/RPM/BUILD/marsyas-0.6.0/src/marsyas/marsystems/Envelope.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 "Envelope.h"
00021 #include "../common_source.h"
00022 
00023 
00024 using namespace Marsyas;
00025 using std::ostringstream;
00026 
00027 
00028 Envelope::Envelope(mrs_string name):MarSystem("Envelope", name)
00029 {
00030   addControls();
00031 }
00032 
00033 
00034 Envelope::~Envelope()
00035 {
00036 }
00037 
00038 
00039 MarSystem*
00040 Envelope::clone() const
00041 {
00042   return new Envelope(*this);
00043 }
00044 
00045 void
00046 Envelope::addControls()
00047 {
00048   addctrl("mrs_real/target", 0.0);
00049   //addctrl("mrs_real/rate", 0.001);
00050   addctrl("mrs_real/time",0.2);
00051   addctrl("natural/state",0);
00052   addctrl("mrs_real/nton", 0.0);
00053   addctrl("mrs_real/ntoff", 0.0);
00054   setctrlState("mrs_real/target", true);
00055   //setctrlState("mrs_real/rate", true);
00056   setctrlState("mrs_real/time", true);
00057   setctrlState("mrs_real/nton", true);
00058   setctrlState("mrs_real/ntoff", true);
00059 }
00060 
00061 
00062 void
00063 Envelope::update()
00064 {
00065   MRSDIAG("Envelope.cpp - Envelope:update");
00066   setctrl("natural/onSamples", getctrl("natural/inSamples"));
00067   setctrl("natural/onObservations", getctrl("natural/inObservations"));
00068   setctrl("mrs_real/osrate", getctrl("mrs_real/israte"));
00069   setctrl("string/onObsNames", getctrl("string/inObsNames"));
00070 
00071   sampleRate_= getctrl("mrs_real/israte")->to<mrs_real>();
00072   target_ = getctrl("mrs_real/target")->to<mrs_real>();
00073   //rate_ = getctrl("mrs_real/rate")->to<mrs_real>();
00074   time_ = getctrl("mrs_real/time")->to<mrs_real>();
00075 
00076   rate_ = 1.0 / (time_ * sampleRate_);
00077 
00078 
00079   noteon_ = getctrl("mrs_real/nton")->to<mrs_real>();
00080   noteoff_ = getctrl("mrs_real/ntoff")->to<mrs_real>();
00081 
00082   if(noteon_) {
00083     value_=0.0;
00084     this->updControl("mrs_real/nton",0.0);
00085     this->updControl("mrs_real/target",1.0);
00086     state_ = 1;
00087   }
00088 
00089   if(noteoff_) {
00090 
00091     this->updControl("mrs_real/ntoff",0.0);
00092     this->updControl("mrs_real/target",0.0);
00093     state_ = 1;
00094   }
00095 
00096 }
00097 
00098 
00099 void
00100 Envelope::myProcess(realvec& in, realvec& out)
00101 {
00102   mrs_natural o,t;
00103   //checkFlow(in,out);
00104 
00105   for (o=0; o < inObservations_; o++)
00106     for (t = 0; t < inSamples_; t++)
00107     {
00108 
00109       if (state_==1) {
00110         if (target_ > value_) {
00111           value_ =value_+ rate_;
00112           if (value_ >= target_) {
00113             value_ = target_;
00114             state_ = 0;
00115           }
00116         }//target
00117         else {
00118 
00119           value_ = value_-rate_;
00120           if (value_ <= target_) {
00121             value_ = target_;
00122             state_ = 0;
00123           }
00124         }//else
00125       }//state
00126 
00127       out(o,t) =  value_* in(o,t);
00128 
00129     }//for
00130 
00131 
00132 
00133 
00134 
00135 }