Marsyas  0.6.0-alpha
/usr/src/RPM/BUILD/marsyas-0.6.0/src/marsyas/marsystems/SineSource.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 "SineSource.h"
00020 
00021 using namespace std;
00022 using namespace Marsyas;
00023 
00024 SineSource::SineSource(mrs_string name):MarSystem("SineSource",name)
00025 {
00026   //type_ = "SineSource";
00027   //name_ = name;
00028 
00029   addControls();
00030   index_ = 0;
00031 }
00032 
00033 SineSource::SineSource( const SineSource & copy ):
00034   MarSystem(copy),
00035   index_(0)
00036 {
00037   freqControl_ = getControl("mrs_real/frequency");
00038 }
00039 
00040 SineSource::~SineSource()
00041 {
00042 }
00043 
00044 MarSystem*
00045 SineSource::clone() const
00046 {
00047   return new SineSource(*this);
00048 }
00049 
00050 void
00051 SineSource::addControls()
00052 {
00053   addctrl("mrs_real/frequency", 440.0, freqControl_);
00054 }
00055 
00056 void
00057 SineSource::myUpdate(MarControlPtr sender)
00058 {
00059 //   setctrl("mrs_natural/onSamples", getctrl("mrs_natural/inSamples"));
00060 //   setctrl("mrs_natural/onObservations", getctrl("mrs_natural/inObservations"));
00061 //   setctrl("mrs_real/osrate", getctrl("mrs_real/israte"));
00062   MarSystem::myUpdate(sender);
00063 
00064   wavetableSize_ = 8192;
00065   wavetable_.create((mrs_natural)wavetableSize_);
00066 
00067   mrs_real incr = TWOPI / wavetableSize_;
00068   for (mrs_natural t=0; t < wavetableSize_; t++)
00069     wavetable_(t) = (mrs_real)(0.5 * sin(incr * t));
00070 
00071 }
00072 
00073 void
00074 SineSource::myProcess(realvec &in, realvec &out)
00075 {
00076   (void) in;
00077   //checkFlow(in,out);
00078 
00079   //lmartins: if (mute_)
00080   if(ctrl_mute_->to<mrs_bool>())
00081   {
00082     out.setval(0.0);
00083     return;
00084   }
00085 
00086   mrs_real incr = freqControl_->to<mrs_real>() * wavetableSize_ / israte_;
00087   mrs_natural inSamples = inSamples_;
00088 
00089   for (mrs_natural t=0; t < inSamples; t++)
00090   {
00091     out(0,t) = wavetable_((mrs_natural)index_);
00092     index_ += incr;
00093     while (index_ >= wavetableSize_)
00094       index_ -= wavetableSize_;
00095     while (index_ < 0)
00096       index_ += wavetableSize_;
00097   }
00098 }
00099 
00100 
00101 
00102 
00103