Oscillator.cxx

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 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 "Oscillator.hxx"
00023 #include "ProcessingFactory.hxx"
00024 
00025 
00026 namespace CLAM
00027 {
00028 
00029 namespace Hidden
00030 {
00031         static const char * metadata[] = {
00032                 "key", "Oscillator",
00033                 "category", "Generators",
00034                 "description", "Oscillator",
00035                 0
00036         };
00037         static FactoryRegistrator<ProcessingFactory, Oscillator> reg = metadata;
00038 }
00039 
00040 void OscillatorConfig::DefaultInit(void)
00041 {
00042         AddFrequency();
00043         AddAmplitude();
00044         AddModIndex();
00045         AddPhase();
00046         AddSamplingRate();
00047         
00048         UpdateData();
00049 
00050         SetFrequency(440.0);
00051         SetAmplitude(1.0);
00052         SetModIndex(1.0);
00053         SetPhase(0.0);
00054         SetSamplingRate( 44100 );
00055 }
00056 
00057 
00058 Oscillator::Oscillator()
00059         :mInputPhaseMod("Input Phase Modulation", this ),
00060         mInputFreqMod("Input Frequency Modulation", this ),
00061         mModIdxUpdated( false ),
00062         mModIdxCtl(0)
00063 {
00064         mModIdxCtl = new OscillatorCtrl( "ModIndex", this, &Oscillator::UpdateModIdx );
00065         
00066         OscillatorConfig cfg;
00067         Configure( cfg );
00068 }
00069 
00070 Oscillator::Oscillator(const OscillatorConfig& c )
00071         : mInputPhaseMod("Input Phase Modulation", this ),
00072         mInputFreqMod("Input Frequency Modulation", this ),
00073         mModIdxUpdated( false ),
00074         mModIdxCtl(0)
00075 {       
00076         mModIdxCtl = new OscillatorCtrl( "ModIndex", this, &Oscillator::UpdateModIdx );
00077 
00078         SimpleOscillatorConfig simpleCfg;
00079         simpleCfg.SetFrequency( c.GetFrequency() );
00080         simpleCfg.SetAmplitude( c.GetAmplitude() );
00081         simpleCfg.SetSamplingRate( c.GetSamplingRate() );
00082 
00083         Configure( c );
00084 }
00085 
00086 Oscillator::~Oscillator()
00087 {
00088         delete mModIdxCtl;
00089 }
00090 
00091 bool Oscillator::ConcreteConfigure( const ProcessingConfig& c )
00092 {
00093         CopyAsConcreteConfig(mConfig, c);
00094 
00095 
00096         mAmp = mConfig.GetAmplitude();
00097         mPhase = mConfig.GetPhase(); // TEMP HACK  (See also constructor
00098         mModIndex = mConfig.GetModIndex();
00099         mSamplingRate = mConfig.GetSamplingRate();
00100         mDeltaPhase = TData(2.*PI*mConfig.GetFrequency()/mSamplingRate);
00101 
00102         return true;
00103 }
00104 
00105 bool Oscillator::Do()
00106 {
00107         bool res =Do(mInputFreqMod.GetAudio(),mInputPhaseMod.GetAudio(),mOutput.GetAudio());
00108         mInputFreqMod.Consume();
00109         mInputPhaseMod.Consume();
00110         mOutput.Produce();
00111         return res;
00112 }
00113 
00114 bool Oscillator::Do( const Audio& pitchModIn, const Audio& phaseModIn, Audio& out )
00115 {
00116         if( !AbleToExecute() ) return true;
00117 
00118         ApplyControls();
00119 
00120         TData* ptr = out.GetBuffer().GetPtr();
00121         TData* pitchModptr = pitchModIn.GetBuffer().GetPtr();
00122         TData* phaseModptr = phaseModIn.GetBuffer().GetPtr();
00123         
00124         for (int i=0;i<out.GetSize();i++)
00125         {
00126                 (*ptr++) = mAmp * TData(sin(mPhase + mModIndex*(*phaseModptr++)));
00127                 mPhase += mDeltaPhase*(*pitchModptr++);
00128                 
00129                 if (mPhase>2.*PI) 
00130                         mPhase-=TData(2.*PI);
00131                 
00132                 if (mPhase<0) 
00133                         mPhase+=TData(2.*PI);
00134         }
00135 
00136         return true;
00137 }
00138 
00139 bool Oscillator::Do( const Audio& pitchModIn, const int& dum, Audio& out )
00140 {
00141         if( !AbleToExecute() ) return true;
00142 
00143         ApplyControls();
00144 
00145         TData* ptr = out.GetBuffer().GetPtr();
00146         TData* pitchModptr = pitchModIn.GetBuffer().GetPtr();
00147         
00148         for (int i=0;i<out.GetSize();i++)
00149         {
00150                 (*ptr++) = mAmp * TData(sin(mPhase));
00151                 mPhase += mDeltaPhase*(*pitchModptr++);
00152                 
00153                 if (mPhase>TData(2.*PI) )
00154                         mPhase-=TData(2.*PI);
00155                 
00156                 if (mPhase<0) 
00157                         mPhase+=TData(2.*PI);
00158         }
00159         return true;
00160 }
00161 
00162 bool Oscillator::Do( const int& dum, const Audio& phaseModIn, Audio& out )
00163 {
00164         if( !AbleToExecute() ) return true;
00165 
00166         ApplyControls();
00167 
00168         TData* ptr = out.GetBuffer().GetPtr();
00169         TData* phaseModptr = phaseModIn.GetBuffer().GetPtr();
00170         
00171         for (int i=0;i<out.GetSize();i++)
00172         {
00173                 (*ptr++) = mAmp * TData(sin(mPhase + mModIndex*(*phaseModptr++)));
00174                 mPhase += mDeltaPhase;
00175                 
00176                 if (mPhase>TData(2.*PI) )
00177                         mPhase-=TData(2.*PI);
00178                         
00179                 if (mPhase<0) 
00180                         mPhase+=TData(2.*PI);
00181         }
00182         
00183         return true;
00184 }
00185 
00186 int Oscillator::UpdateModIdx( TControlData value )
00187 {
00188         mModIdxUpdated = true;
00189 
00190         return 0;
00191 }
00192 
00193 
00194 } // namespace CLAM
00195 

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