SimpleOscillator.cxx

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2001-2004 MUSIC TECHNOLOGY GROUP (MTG)
00003  *                         UNIVERSITAT POMPEU FABRA
00004  *
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  *
00020  */
00021 
00022 #include "SimpleOscillator.hxx"
00023 #include "ProcessingFactory.hxx"
00024 
00025 namespace CLAM
00026 {
00027 
00028 namespace Hidden
00029 {
00030         static const char * metadata[] = {
00031                 "key", "SimpleOscillator",
00032                 "category", "Generators",
00033                 "description", "SimpleOscillator",
00034                 0
00035         };
00036         static FactoryRegistrator<ProcessingFactory, SimpleOscillator> reg = metadata;
00037 }
00038 
00039         
00040 // OscillatorConfig method definition
00041 void SimpleOscillatorConfig::DefaultInit(void)
00042 {
00043         AddFrequency();
00044         AddAmplitude();
00045         AddPhase();
00046         AddSamplingRate();
00047         
00048         UpdateData();
00049 
00050         SetFrequency(440.0);
00051         SetAmplitude(1.0);
00052         SetPhase(0.0);
00053         SetSamplingRate( 44100 );
00054 }
00055 
00056 
00057 // Oscillator method definition
00058 SimpleOscillator::SimpleOscillator()
00059         :mOutput("Audio Output", this),
00060         mFreqUpdated( false ),
00061         mAmpUpdated( false ),
00062         mFreqCtl(0),
00063         mAmpCtl(0),
00064         mSamplesBetweenCallsCtl("SamplesBetweenCalls", this)
00065 
00066 {
00067         mFreqCtl = new SimpleOscillatorCtrl( "Pitch", this, &SimpleOscillator::UpdateFreq );
00068         mAmpCtl = new SimpleOscillatorCtrl( "Amplitude", this, &SimpleOscillator::UpdateAmp );
00069 
00070         SimpleOscillatorConfig cfg;
00071 
00072         Configure( cfg );
00073 }
00074 
00075 SimpleOscillator::SimpleOscillator( const SimpleOscillatorConfig& cfg )
00076         :mOutput("Audio Output", this),
00077         mFreqUpdated( false ),
00078         mAmpUpdated( false ),
00079         mFreqCtl(0),
00080         mAmpCtl(0),
00081         mSamplesBetweenCallsCtl("SamplesBetweenCalls", this)
00082 {
00083         mFreqCtl = new SimpleOscillatorCtrl( "Pitch", this, &SimpleOscillator::UpdateFreq );
00084         mAmpCtl = new SimpleOscillatorCtrl( "Amplitude", this, &SimpleOscillator::UpdateAmp );
00085         Configure( cfg );
00086 }
00087 
00088 SimpleOscillator::~SimpleOscillator()
00089 {
00090         delete mFreqCtl;
00091         delete mAmpCtl;
00092 }
00093 
00094 bool SimpleOscillator::ConcreteConfigure( const ProcessingConfig& c )
00095 {
00096         CopyAsConcreteConfig(mConfig, c);
00097 
00098 
00099         mAmp = mConfig.GetAmplitude();
00100         mPhase = mConfig.GetPhase(); // TEMP HACK  (See also constructor
00101         mSamplingRate = mConfig.GetSamplingRate();
00102         mDeltaPhase = TData(2.*PI*mConfig.GetFrequency()/mSamplingRate);
00103         //xamat: kludge to convert this into an LFO, eventually separate into a different class
00104         mSamplesBetweenCallsCtl.DoControl(1);
00105         return true;
00106 }
00107 
00108 bool SimpleOscillator::Do()
00109 {
00110         bool res = false;
00111         res = Do(mOutput.GetAudio());
00112         mOutput.Produce();
00113         return res;
00114 }
00115 
00116 bool SimpleOscillator::Do( Audio& out )
00117 {
00118         if( !AbleToExecute() ) return true;
00119         
00120         ApplyFreqAndAmpControls();
00121 
00122         TData* ptr = out.GetBuffer().GetPtr();
00123         for (int i=0;i<out.GetSize();i++)
00124         {
00125                 (*ptr++) = mAmp * TData(sin(mPhase));
00126                 mPhase += mDeltaPhase;
00127                 
00128                 if (mPhase>TData(2*PI)) 
00129                         mPhase-=TData(2*PI);
00130         }
00131 
00132         return true;
00133 }
00134 
00135 //xamat: kludge to convert this into an LFO, eventually separate into a different class
00136 bool SimpleOscillator::Do( TData& out )
00137 {
00138         if( !AbleToExecute() ) return true;
00139         
00140         ApplyFreqAndAmpControls();
00141 
00142         out = mAmp * TData(sin(mPhase));
00143         mPhase += mDeltaPhase*mSamplesBetweenCallsCtl.GetLastValue();
00144                 
00145         if (mPhase>TData(2*PI)) 
00146                 mPhase-=TData(2*PI);
00147 
00148         return true;
00149 }
00150 
00151 
00152 int SimpleOscillator::UpdateFreq( TControlData value )
00153 {
00154         mFreqUpdated = true;
00155 
00156         return 0;
00157 }
00158 
00159 int SimpleOscillator::UpdateAmp( TControlData value )
00160 {
00161         mAmpUpdated = true;
00162 
00163         return 0;
00164 }
00165 
00166 } // namespace CLAM
00167 

Generated on Tue Aug 12 22:33:44 2008 for CLAM by  doxygen 1.5.5