WaveGenerator.hxx
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 #ifndef _WAVE_GENERATOR_H
00023 #define _WAVE_GENERATOR_H
00024
00025 #include "Audio.hxx"
00026 #include "Processing.hxx"
00027 #include "AudioOutPort.hxx"
00028 #include "Enum.hxx"
00029
00030 namespace CLAM {
00031
00032 class EWaveType : public Enum {
00033 public:
00034
00035 EWaveType() : Enum(ValueTable(), eSine) {}
00036 EWaveType(tValue v) : Enum(ValueTable(), v) {};
00037 EWaveType(std::string s) : Enum(ValueTable(), s) {};
00038
00039 typedef enum {
00040 eSine
00041 } tEnum;
00042 static tEnumValue * ValueTable()
00043 {
00044 static tEnumValue sEnumValues[] = {
00045 {EWaveType::eSine,"SineWave"},
00046 {0,NULL}
00047 };
00048 return sEnumValues;
00049 }
00050
00051 virtual Component* Species() const
00052 {
00053 return (Component*) new EWaveType(eSine);
00054 };
00055 };
00056
00057
00058 template< typename WaveType >
00059 class WaveFunctor {
00060 public:
00061 TData operator()(TTime x,TData amplitude);
00062 };
00063
00064 class WaveGeneratorConfig: public ProcessingConfig
00065 {
00066 public:
00067 DYNAMIC_TYPE_USING_INTERFACE (WaveGeneratorConfig, 6, ProcessingConfig);
00068 DYN_ATTRIBUTE (0, public, EWaveType, WaveType);
00069 DYN_ATTRIBUTE (1, public, TData, Frequency);
00070 DYN_ATTRIBUTE (2, public, TData, Amplitude);
00071 DYN_ATTRIBUTE (3, public, TData, Phase);
00072 DYN_ATTRIBUTE (4, public, TData, SampleRate);
00073 DYN_ATTRIBUTE (5, public, int, FrameSize);
00074 protected:
00075 void DefaultInit(void);
00076 };
00077
00078 class WaveGenerator: public Processing
00079 {
00080 protected:
00081 WaveGeneratorConfig mConfig;
00082
00083 public:
00084
00085 class EWaveType_eSine
00086 {
00087 };
00088 private:
00089
00090 TData mAmplitude;
00091
00092 TData mXPos;
00093 TData mXDelta;
00094
00095 EWaveType::tValue mType;
00096
00097 const char *GetClassName() const {return "WaveGenerator";}
00098
00102 bool ConcreteConfigure(const ProcessingConfig&);
00103
00104 inline TData Sine(TTime pos);
00105
00106 public:
00107
00108
00109 inline TData GetXPos() const
00110 {
00111 return mXPos;
00112 }
00113
00114 inline void SetXPos( TData new_value )
00115 {
00116 mXPos = new_value;
00117 }
00118
00119 inline TData GetXDelta() const
00120 {
00121 return mXDelta;
00122 }
00123
00124 inline void SetXDelta( TData new_value )
00125 {
00126 mXDelta = new_value;
00127 }
00128
00129 inline TData GetAmplitude() const
00130 {
00131 return mAmplitude;
00132 }
00133
00134 OutPort<Audio> Output;
00135
00136 WaveGenerator();
00137
00138 WaveGenerator(const WaveGeneratorConfig &c);
00139
00140 virtual ~WaveGenerator();
00141
00144 const ProcessingConfig &GetConfig() const { return mConfig;}
00145
00148 bool Do(void);
00149
00153 bool Do(Audio& in);
00154
00155 private:
00156
00157
00158 };
00159
00160 template < typename WaveType >
00161 void FillBuffer(Array<TData> &buffer, WaveGenerator& generator, WaveType* dummy = 0 )
00162 {
00163 TData xvalue = generator.GetXPos();
00164 TData xdelta = generator.GetXDelta();
00165 TData amplitude = generator.GetAmplitude();
00166
00167
00168 int i;
00169 WaveFunctor<WaveType> func;
00170 int size = buffer.Size();
00171 for (i=0; i<size; i++) {
00172 buffer[i] = func( xvalue, amplitude);
00173 xvalue += xdelta;
00174 }
00175 if (xvalue > 2.0 * M_PI)
00176 xvalue = fmod(xvalue, TData( 2.0 * M_PI ));
00177
00178 generator.SetXPos( xvalue );
00179 }
00180
00181
00182 }
00183
00184 #endif // _WaveGenerator_
00185