Marsyas  0.5.0-beta1
/Users/jleben/code/marsyas/src/marsyas/marsystems/PWMSource.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 "PWMSource.h"
00020 
00021 using namespace std;
00022 using namespace Marsyas;
00023 
00024 PWMSource::PWMSource(mrs_string name):MarSystem("PWMSource",name)
00025 {
00026   //type_ = "PWMSource";
00027   //name_ = name;
00028 
00029   addControls();
00030 
00031 
00032 }
00033 
00034 PWMSource::~PWMSource()
00035 {
00036 }
00037 
00038 MarSystem*
00039 PWMSource::clone() const
00040 {
00041   return new PWMSource(*this);
00042 }
00043 
00044 void
00045 PWMSource::addControls()
00046 {
00047   addctrl("mrs_real/frequency", 440.0);
00048   addctrl("mrs_real/duty_cicle", 0.5);
00049 }
00050 
00051 void
00052 PWMSource::myUpdate(MarControlPtr sender)
00053 {
00054 
00055 //   setctrl("mrs_natural/onSamples", getctrl("mrs_natural/inSamples"));
00056 //   setctrl("mrs_natural/onObservations", getctrl("mrs_natural/inObservations"));
00057 //   setctrl("mrs_real/osrate", getctrl("mrs_real/israte"));
00058   MarSystem::myUpdate(sender);
00059 }
00060 
00061 void
00062 PWMSource::myProcess(realvec &in, realvec &out)
00063 {
00064   (void) in;
00065 
00066   mrs_real frequency = (getctrl("mrs_real/frequency")->to<mrs_real>());
00067   mrs_real duty =  getctrl("mrs_real/duty_cicle")->to<mrs_real>();
00068   mrs_real irate = (getctrl("mrs_real/israte")->to<mrs_real>());
00069   mrs_real incr = frequency / irate;
00070   mrs_natural inSamples = getctrl("mrs_natural/inSamples")->to<mrs_natural>();
00071 
00072 //  cout << "f=" << frequency;
00073 //  cout << "d=" << duty;
00074 //  cout << "irate=" << irate;
00075 //  cout << "deltaphase=" << incr;
00076 //  cout << "insamples=" << inSamples;
00077 
00078 
00079   for (mrs_natural t=0; t < inSamples; t++)
00080   {
00081     out(0,t)=0;
00082     phase += incr;
00083     if (phase>duty) {
00084       out(0,t) = 0;
00085     }    else {
00086       out(0,t) = 1;
00087     }
00088     if (phase>1) {
00089       phase=0;
00090     }
00091   }
00092 }
00093 
00094 
00095 
00096 //00053 void
00097 //00054 NoiseSource::myProcess(realvec &in, realvec &out)
00098 //00055 {
00099 //00056     for (mrs_natural t=0; t < inSamples_; t++)
00100 //00057         out(t) = (mrs_real)(2.0 * rand() / (RAND_MAX + 1.0) )-1;
00101 //00058 }
00102 //00059
00103 //00060
00104 //00061