WaveGenerator.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 "OSDefines.hxx"
00023 #include "WaveGenerator.hxx"
00024 #include "CLAM_Math.hxx"
00025 
00026 namespace CLAM {
00027 
00028         void WaveGeneratorConfig::DefaultInit()
00029         {
00030                 AddAll();
00031                 UpdateData();
00032                 SetFrequency(440.0);
00033                 SetAmplitude(1.0);
00034                 SetPhase(0.0);
00035                 SetSampleRate(44100);
00036                 SetFrameSize(512);
00037                 SetWaveType(EWaveType::eSine);
00038         }
00039 
00040 
00041         template<> 
00042         inline TData WaveFunctor<WaveGenerator::EWaveType_eSine>::operator()(TTime x,TData amplitude)
00043         {
00044                 return amplitude * sin(x);
00045         }
00046 
00047 
00048 
00049 
00050         WaveGenerator::WaveGenerator()
00051                 : Output("Output",this)
00052         {
00053                 Configure(WaveGeneratorConfig());
00054         };
00055 
00056         WaveGenerator::WaveGenerator(const WaveGeneratorConfig &c)
00057                 : Output("Output",this)
00058         { 
00059                 Configure(c);
00060         };
00061 
00062         bool WaveGenerator::ConcreteConfigure(const ProcessingConfig& c)
00063         {
00064                 CopyAsConcreteConfig(mConfig, c);
00065 
00066                 mAmplitude = mConfig.GetAmplitude();
00067 
00068                 TData samples_per_period = mConfig.GetSampleRate() / mConfig.GetFrequency();
00069 
00070                 mXDelta =  2.0 * M_PI / samples_per_period;
00071 
00072                 mXPos = fmod( TData(mConfig.GetPhase()), TData(2 * M_PI) );
00073 
00074                 mType = mConfig.GetWaveType();
00075                 
00076                 Output.SetSize(mConfig.GetFrameSize());
00077 
00078                 return true;
00079         }
00080 
00081         WaveGenerator::~WaveGenerator()
00082         {
00083         }
00084 
00085         bool WaveGenerator::Do(Audio& out)
00086         {
00087                 switch(mType)
00088                 {
00089                 case EWaveType::eSine:
00090 
00091                         FillBuffer< EWaveType_eSine > ( out.GetBuffer(), *this );
00092                         break;
00093                 default:
00094                         return false;
00095                 }
00096                 return true;
00097         }
00098 
00099 
00100         bool WaveGenerator::Do(void)
00101         {
00102                 bool res = Do(Output.GetData());
00103                 Output.Produce();
00104                 return res;
00105         }
00106 
00107 
00108 }
00109 
Generated by  doxygen 1.6.3