Marsyas  0.6.0-alpha
/usr/src/RPM/BUILD/marsyas-0.6.0/src/marsyas/marsystems/WaveguideOsc.cpp
Go to the documentation of this file.
00001 /*
00002 ** Copyright (C) 1998-2011 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 "WaveguideOsc.h"
00020 #include "math.h"
00021 
00022 using namespace Marsyas;
00023 
00024 WaveguideOsc::WaveguideOsc(mrs_string name):MarSystem("WaveguideOsc", name)
00025 {
00026 
00027   addControls();
00028 }
00029 
00030 WaveguideOsc::WaveguideOsc(const WaveguideOsc& a) : MarSystem(a)
00031 {
00032   // For any MarControlPtr in a MarSystem
00033   // it is necessary to perform this getctrl
00034   // in the copy constructor in order for cloning to work
00035 }
00036 
00037 WaveguideOsc::~WaveguideOsc()
00038 {
00039 }
00040 
00041 MarSystem* WaveguideOsc::clone() const
00042 {
00043   return new WaveguideOsc(*this);
00044 }
00045 
00046 void WaveguideOsc::addControls()
00047 {
00048   addctrl("mrs_real/frequency", 440.0);
00049   addctrl("mrs_bool/noteon", false);
00050 
00051   setctrlState("mrs_real/frequency", true);
00052   setctrlState("mrs_bool/noteon", true);
00053 }
00054 
00055 
00056 void WaveguideOsc::myUpdate(MarControlPtr sender)
00057 {
00058   // x1n1_ is initialized with an impluse
00059   x1n1_ = 0.95;
00060   x2n1_ = 0;
00061 
00062   frequency_ = (getctrl("mrs_real/frequency")->to<mrs_real>());
00063   israte_ = (getctrl("mrs_real/israte")->to<mrs_real>());
00064 
00065   k_ = cos((TWOPI*frequency_)/israte_);
00066   MarSystem::myUpdate(sender);
00067 }
00068 
00069 void WaveguideOsc::myProcess(realvec& in, realvec& out)
00070 {
00071   mrs_real x1, x2;
00072 
00073   for (mrs_natural t = 0; t < inSamples_; t++)
00074   {
00075     k_ = cos((TWOPI*frequency_*(in(0,t) + 1))/israte_);
00076     x1 = (2* k_ * x1n1_) - x2n1_;
00077     x2 = x1n1_;
00078     x1n1_ = x1;
00079     x2n1_ = x2;
00080 
00081     out(0,t) = x1 - x2;
00082   }
00083 }