SimpleOscillator.cxx
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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
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 SimpleOscillator::SimpleOscillator( const SimpleOscillatorConfig& cfg )
00057 : mOutput("Audio Output", this)
00058 , mFreqUpdated( false )
00059 , mAmpUpdated( false )
00060 , mFreqCtl( "Pitch", this, &SimpleOscillator::UpdateFreq )
00061 , mAmpCtl( "Amplitude", this, &SimpleOscillator::UpdateAmp )
00062 , mSamplesBetweenCallsCtl("SamplesBetweenCalls", this)
00063 {
00064 Configure( cfg );
00065 }
00066
00067 SimpleOscillator::~SimpleOscillator()
00068 {
00069 }
00070
00071 bool SimpleOscillator::ConcreteConfigure( const ProcessingConfig& c )
00072 {
00073 CopyAsConcreteConfig(mConfig, c);
00074
00075
00076 mAmp = mConfig.GetAmplitude();
00077 mPhase = mConfig.GetPhase();
00078 mSamplingRate = mConfig.GetSamplingRate();
00079 mDeltaPhase = TData(2.*PI*mConfig.GetFrequency()/mSamplingRate);
00080
00081 mSamplesBetweenCallsCtl.DoControl(1);
00082 return true;
00083 }
00084
00085 bool SimpleOscillator::Do()
00086 {
00087 bool res = false;
00088 res = Do(mOutput.GetAudio());
00089 mOutput.Produce();
00090 return res;
00091 }
00092
00093 bool SimpleOscillator::Do( Audio& out )
00094 {
00095 if( !AbleToExecute() ) return true;
00096
00097 ApplyFreqAndAmpControls();
00098
00099 TData* ptr = out.GetBuffer().GetPtr();
00100 for (int i=0;i<out.GetSize();i++)
00101 {
00102 (*ptr++) = mAmp * TData(sin(mPhase));
00103 mPhase += mDeltaPhase;
00104
00105 if (mPhase>TData(2*PI))
00106 mPhase-=TData(2*PI);
00107 }
00108
00109 return true;
00110 }
00111
00112
00113 bool SimpleOscillator::Do( TData& out )
00114 {
00115 if( !AbleToExecute() ) return true;
00116
00117 ApplyFreqAndAmpControls();
00118
00119 out = mAmp * TData(sin(mPhase));
00120 mPhase += mDeltaPhase*mSamplesBetweenCallsCtl.GetLastValue();
00121
00122 if (mPhase>TData(2*PI))
00123 mPhase-=TData(2*PI);
00124
00125 return true;
00126 }
00127
00128
00129 void SimpleOscillator::UpdateFreq( TControlData value )
00130 {
00131 mFreqUpdated = true;
00132 }
00133
00134 void SimpleOscillator::UpdateAmp( TControlData value )
00135 {
00136 mAmpUpdated = true;
00137 }
00138
00139 }
00140