Marsyas  0.6.0-alpha
/usr/src/RPM/BUILD/marsyas-0.6.0/src/marsyas/marsystems/ADSR.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 "ADSR.h"
00020 #include "../common_source.h"
00021 
00022 
00023 using namespace Marsyas;
00024 
00025 
00026 ADSR::ADSR(mrs_string name):MarSystem("ADSR", name)
00027 {
00028   addControls();
00029 }
00030 
00031 ADSR::~ADSR()
00032 {
00033 }
00034 
00035 MarSystem*
00036 ADSR::clone() const
00037 {
00038   return new ADSR(*this);
00039 }
00040 
00041 void
00042 ADSR::addControls()
00043 {
00044   //addctrl("mrs_real/aRate",  0.001); //attack rate
00045   addctrl("mrs_real/aTime",    0.2);   //attack time
00046   addctrl("mrs_real/aTarget",  1.0);   //attack target
00047   //addctrl("mrs_real/dRate",  0.001); //decay rate
00048   addctrl("mrs_real/dTime",    0.1);   //decay time
00049   addctrl("mrs_real/susLevel", 0.85);  //sustain level
00050   //addctrl("mrs_real/rRate",  0.001); //release rate
00051   addctrl("mrs_real/rTime",    0.2);   //release time
00052 
00053   // Lee's Adjustments
00054   addctrl("mrs_real/eValue", 0.0 );     // envelope value
00055   addctrl("mrs_bool/bypass", false);
00056 
00057   addctrl("mrs_natural/state", 1);
00058 
00059   addctrl("mrs_real/nton",  0.0);
00060   addctrl("mrs_real/ntoff", 0.0);
00061 
00062   addctrl("mrs_bool/noteon",  false);
00063   addctrl("mrs_bool/noteoff", false);
00064 
00065   //setctrlState("mrs_real/aRate",  true);
00066   setctrlState("mrs_real/aTime",    true);
00067   setctrlState("mrs_real/aTarget",  true);
00068   //setctrlState("mrs_real/dRate",  true);
00069   setctrlState("mrs_real/dTime",    true);
00070   setctrlState("mrs_real/susLevel", true);
00071   //setctrlState("mrs_real/rRate",  true);
00072   setctrlState("mrs_real/rTime",    true);
00073   setctrlState("mrs_real/nton",     true);
00074   setctrlState("mrs_real/ntoff",    true);
00075   setctrlState("mrs_real/eValue",   true);
00076   setctrlState("mrs_bool/bypass",   true);
00077   setctrlState("mrs_bool/noteon",   true);
00078   setctrlState("mrs_bool/noteoff",  true);
00079 }
00080 
00081 void
00082 ADSR::myUpdate(MarControlPtr sender)
00083 {
00084   MRSDIAG("ADSR.cpp - ADSR:myUpdate");
00085 
00086   //setctrl("natural/onSamples", getctrl("natural/inSamples"));
00087   //setctrl("natural/onObservations", getctrl("natural/inObservations"));
00088   //setctrl("mrs_real/osrate", getctrl("mrs_real/israte"));
00089   //setctrl("string/onObsNames", getctrl("string/inObsNames"));
00090   MarSystem::myUpdate(sender);
00091 
00092   //aRate_ = getctrl("mrs_real/aRate")->to<mrs_real>();
00093   aTime_ = getctrl("mrs_real/aTime")->to<mrs_real>();
00094   aTarget_ = getctrl("mrs_real/aTarget")->to<mrs_real>();
00095   //dRate_ = getctrl("mrs_real/dRate")->to<mrs_real>();
00096   dTime_ = getctrl("mrs_real/dTime")->to<mrs_real>();
00097   susLevel_ = getctrl("mrs_real/susLevel")->to<mrs_real>();
00098   //rRate_ = getctrl("mrs_real/rRate")->to<mrs_real>();
00099   rTime_ = getctrl("mrs_real/rTime")->to<mrs_real>();
00100 
00101   sampleRate_= getctrl("mrs_real/israte")->to<mrs_real>();
00102 
00103   aRate_ = 1.0 / (aTime_ * sampleRate_);
00104   dRate_ = 1.0 / (dTime_ * sampleRate_);
00105   rRate_ = 1.0 / (rTime_ * sampleRate_);
00106   nton_ = getctrl("mrs_real/nton")->to<mrs_real>();
00107   ntoff_ = getctrl("mrs_real/ntoff")->to<mrs_real>();
00108 
00109   noteon_ = getctrl("mrs_bool/noteon")->to<mrs_bool>();
00110   noteoff_ = getctrl("mrs_bool/noteoff")->to<mrs_bool>();
00111 
00112   bypass_ = getctrl("mrs_bool/bypass")->to<mrs_bool>();
00113 
00114 
00115   if(noteon_ || nton_)
00116   {
00117     this->setctrl("mrs_real/nton",0.0);
00118     this->setctrl("mrs_bool/noteon", false);
00119     value_=0.0;
00120     target_ = aTarget_;
00121     state_ = 1;
00122   }
00123 
00124   if(noteoff_ || ntoff_)
00125   {
00126     this->setctrl("mrs_real/ntoff",0.0);
00127     this->setctrl("mrs_bool/noteoff", false);
00128     target_ = 0.0;
00129     state_ = 4;
00130   }
00131 }
00132 
00133 void
00134 ADSR::myProcess(realvec& in, realvec& out)
00135 {
00136   mrs_natural o,t;
00137   for (o = 0; o < inObservations_; o++)
00138   {
00139     for (t = 0; t < inSamples_; t++)
00140     {
00141       switch (state_)
00142       {
00143       case 1://attack
00144         value_ += aRate_;
00145         if (value_ >= target_)
00146         {
00147           value_ = target_;
00148           rate_ = dRate_;
00149           target_ = susLevel_;
00150           state_ = 2;
00151         }
00152         break;
00153       case 2://decay
00154         value_ -= dRate_;
00155         if (value_ <= susLevel_)
00156         {
00157           value_ = susLevel_;
00158           rate_ = 0.0;
00159           state_ = 3;
00160         }
00161         break;
00162       case 4://release
00163         value_ -= rRate_;
00164         if (value_ <= 0.0)
00165         {
00166           value_ = 0.0;
00167           state_ = 5;//done
00168         }
00169       }//switch
00170 
00171       if ( !bypass_ )
00172       {
00173         out(o,t) =  value_* in(o,t);
00174       }
00175       else
00176       {
00177         out(o,t) =  value_;
00178       }
00179     }//for
00180   }//for
00181 
00182   //used for toy_with_onsets.m (DO NOT DELETE! - COMMENT INSTEAD)
00183   //MATLAB_PUT(out, "ADSR_out");
00184   //MATLAB_EVAL("onsetAudio = [onsetAudio, ADSR_out];");
00185   //MATLAB_EVAL("toy_with_onsets");
00186 
00187 }