Marsyas  0.6.0-alpha
/usr/src/RPM/BUILD/marsyas-0.6.0/src/marsyas/marsystems/APDelayOsc.h
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 #ifndef MARSYAS_APDELAYOSC_H
00020 #define MARSYAS_APDELAYOSC_H
00021 
00022 #include <marsyas/system/MarSystem.h>
00023 
00024 namespace Marsyas
00025 {
00061 class APDelayOsc: public MarSystem
00062 {
00063 private:
00064   // Add specific controls needed by this MarSystem.
00065   void addControls();
00066 
00067   // Reads changed controls and sets up variables if necessary.
00068   void myUpdate(MarControlPtr sender);
00069 
00070   class FirstOrderAllPass
00071   {
00072   private:
00073     mrs_real y, y1;
00074     mrs_real x, x1;
00075     mrs_real a, d;
00076 
00077   public:
00078     void delay(mrs_real din)
00079     {
00080       d = din;
00081       a = (1 - d)/(1 + d);
00082       y1 = x1 = 0;
00083     }
00084 
00085     mrs_real get_delay()
00086     {
00087       return d;
00088     }
00089 
00090     mrs_real operator()(mrs_real x)
00091     {
00092       y = (a * x) + (x1 - (a * y1));
00093       x1 = x;
00094       y1 = y;
00095       return y;
00096     }
00097   };
00098 
00099   class LeakyIntegrator
00100   {
00101   private:
00102     mrs_real y, y1;
00103     mrs_real x;
00104     mrs_real e;
00105 
00106   public:
00107     LeakyIntegrator(): e(0.003) {}
00108 
00109     void leaky(mrs_real amount)
00110     {
00111       e = amount;
00112     }
00113 
00114     mrs_real operator()(mrs_real x)
00115     {
00116       y = x + ((1 - e) * y1);
00117       y1 = y;
00118       return y;
00119     }
00120   };
00121 
00122   class DCBlocker
00123   {
00124   private:
00125     mrs_real y, y1;
00126     mrs_real x, x1;
00127     mrs_real R;
00128 
00129   public:
00130     DCBlocker(): R(0.995) {}
00131 
00132     mrs_real operator()(mrs_real x)
00133     {
00134       y = x - x1 + (R * y1);
00135       y1 = y;
00136       x1 = x;
00137       return y;
00138     }
00139   };
00140 
00141   mrs_real frequency_;
00142 
00143   mrs_natural delaylineSize_;
00144   realvec delayline_;
00145 
00146   mrs_real dc_;
00147   mrs_real frac_;
00148 
00149   mrs_real israte_; // Sample rate of the system
00150   mrs_real dcoff_;  // The precalculated DC offset
00151   mrs_real neg_;    // Used to invert the system if
00152   // only even harmonics are wanted
00153 
00154   FirstOrderAllPass ap1;
00155   FirstOrderAllPass ap2; // The tuning filter
00156   DCBlocker dcb;
00157   LeakyIntegrator le1;
00158 
00159   mrs_natural wp_;   // Write Pointer
00160   mrs_natural rp_;   // Read pointer one
00161   mrs_natural rpp_;  // Read pointer two
00162   mrs_natural N_;    // The delayline length for our current pitch
00163   mrs_natural type_; // The current type of the oscillator
00164 
00165   mrs_bool noteon_;
00166 
00167   mrs_real allPass(mrs_real x);
00168   mrs_real leakyIntegrator(mrs_real x);
00169   mrs_real dcBlocker(mrs_real x);
00170 
00171 public:
00172   // APDelayOsc constructor.
00173   APDelayOsc(std::string name);
00174 
00175   // APDelayOsc copy constructor.
00176   APDelayOsc(const APDelayOsc& a);
00177 
00178   // APDelayOsc destructor.
00179   ~APDelayOsc();
00180 
00181   // Implementation of the MarSystem::clone() method.
00182   MarSystem* clone() const;
00183 
00184   // Implementation of the MarSystem::myProcess method.
00185   void myProcess(realvec& in, realvec& out);
00186 };
00187 
00188 }
00189 //namespace Marsyas
00190 
00191 #endif
00192 //MARSYAS_MARSYSTEMTEMPLATEBASIC_H
00193