|
CLAM-Development
1.3
|
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(const OscillatorConfig& c ) 00059 : mInputPhaseMod("Input Phase Modulation", this ) 00060 , mInputFreqMod("Input Frequency Modulation", this ) 00061 , mModIdxUpdated( false ) 00062 , mModIdxCtl("ModIndex", this, &Oscillator::UpdateModIdx ) 00063 { 00064 00065 SimpleOscillatorConfig simpleCfg; 00066 simpleCfg.SetFrequency( c.GetFrequency() ); 00067 simpleCfg.SetAmplitude( c.GetAmplitude() ); 00068 simpleCfg.SetSamplingRate( c.GetSamplingRate() ); 00069 00070 Configure( c ); 00071 } 00072 00073 Oscillator::~Oscillator() 00074 { 00075 } 00076 00077 bool Oscillator::ConcreteConfigure( const ProcessingConfig& c ) 00078 { 00079 CopyAsConcreteConfig(mConfig, c); 00080 00081 00082 mAmp = mConfig.GetAmplitude(); 00083 mPhase = mConfig.GetPhase(); // TEMP HACK (See also constructor 00084 mModIndex = mConfig.GetModIndex(); 00085 mSamplingRate = mConfig.GetSamplingRate(); 00086 mDeltaPhase = TData(2.*PI*mConfig.GetFrequency()/mSamplingRate); 00087 00088 return true; 00089 } 00090 00091 bool Oscillator::Do() 00092 { 00093 bool res =Do(mInputFreqMod.GetAudio(),mInputPhaseMod.GetAudio(),mOutput.GetAudio()); 00094 mInputFreqMod.Consume(); 00095 mInputPhaseMod.Consume(); 00096 mOutput.Produce(); 00097 return res; 00098 } 00099 00100 bool Oscillator::Do( const Audio& pitchModIn, const Audio& phaseModIn, Audio& out ) 00101 { 00102 if( !AbleToExecute() ) return true; 00103 00104 ApplyControls(); 00105 00106 TData* ptr = out.GetBuffer().GetPtr(); 00107 TData* pitchModptr = pitchModIn.GetBuffer().GetPtr(); 00108 TData* phaseModptr = phaseModIn.GetBuffer().GetPtr(); 00109 00110 for (int i=0;i<out.GetSize();i++) 00111 { 00112 (*ptr++) = mAmp * TData(sin(mPhase + mModIndex*(*phaseModptr++))); 00113 mPhase += mDeltaPhase*(*pitchModptr++); 00114 00115 if (mPhase>2.*PI) 00116 mPhase-=TData(2.*PI); 00117 00118 if (mPhase<0) 00119 mPhase+=TData(2.*PI); 00120 } 00121 00122 return true; 00123 } 00124 00125 bool Oscillator::Do( const Audio& pitchModIn, const int& dum, Audio& out ) 00126 { 00127 if( !AbleToExecute() ) return true; 00128 00129 ApplyControls(); 00130 00131 TData* ptr = out.GetBuffer().GetPtr(); 00132 TData* pitchModptr = pitchModIn.GetBuffer().GetPtr(); 00133 00134 for (int i=0;i<out.GetSize();i++) 00135 { 00136 (*ptr++) = mAmp * TData(sin(mPhase)); 00137 mPhase += mDeltaPhase*(*pitchModptr++); 00138 00139 if (mPhase>TData(2.*PI) ) 00140 mPhase-=TData(2.*PI); 00141 00142 if (mPhase<0) 00143 mPhase+=TData(2.*PI); 00144 } 00145 return true; 00146 } 00147 00148 bool Oscillator::Do( const int& dum, const Audio& phaseModIn, Audio& out ) 00149 { 00150 if( !AbleToExecute() ) return true; 00151 00152 ApplyControls(); 00153 00154 TData* ptr = out.GetBuffer().GetPtr(); 00155 TData* phaseModptr = phaseModIn.GetBuffer().GetPtr(); 00156 00157 for (int i=0;i<out.GetSize();i++) 00158 { 00159 (*ptr++) = mAmp * TData(sin(mPhase + mModIndex*(*phaseModptr++))); 00160 mPhase += mDeltaPhase; 00161 00162 if (mPhase>TData(2.*PI) ) 00163 mPhase-=TData(2.*PI); 00164 00165 if (mPhase<0) 00166 mPhase+=TData(2.*PI); 00167 } 00168 00169 return true; 00170 } 00171 00172 void Oscillator::UpdateModIdx( TControlData value ) 00173 { 00174 mModIdxUpdated = true; 00175 } 00176 00177 00178 } // namespace CLAM 00179
1.7.6.1