CLAM-Development  1.4.0
WaveGenerator.hxx
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2001-2004 MUSIC TECHNOLOGY GROUP (MTG)
3  * UNIVERSITAT POMPEU FABRA
4  *
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  */
21 
22 #ifndef _WAVE_GENERATOR_H
23 #define _WAVE_GENERATOR_H
24 
25 #include "Audio.hxx"
26 #include "Processing.hxx"
27 #include "AudioOutPort.hxx"
28 #include "Enum.hxx"
29 
30 namespace CLAM {
31 
32  class EWaveType : public Enum {
33  public:
34 
37  EWaveType(std::string s) : Enum(ValueTable(), s) {};
38 
39  typedef enum {
41  } tEnum;
43  {
44  static tEnumValue sEnumValues[] = {
45  {EWaveType::eSine,"SineWave"},
46  {0,NULL}
47  };
48  return sEnumValues;
49  }
50 
51  virtual Component* Species() const
52  {
53  return (Component*) new EWaveType(eSine);
54  };
55  };
56 
57 
58  template< typename WaveType >
59  class WaveFunctor {
60  public:
61  TData operator()(TTime x,TData amplitude);
62  };
63 
65  {
66  public:
68  DYN_ATTRIBUTE (0, public, EWaveType, WaveType);
69  DYN_ATTRIBUTE (1, public, TData, Frequency);
70  DYN_ATTRIBUTE (2, public, TData, Amplitude);
71  DYN_ATTRIBUTE (3, public, TData, Phase);
72  DYN_ATTRIBUTE (4, public, TData, SampleRate);
73  DYN_ATTRIBUTE (5, public, int, FrameSize);
74  protected:
75  void DefaultInit(void);
76  };
77 
78  class WaveGenerator: public Processing
79  {
80  protected:
82 
83  public:
84  // MRJ: Convenience type for template thingies to work
86  {
87  };
88  private:
89 
90  TData mAmplitude;
91 
92  TData mXPos; // Radians
93  TData mXDelta; // Radians
94 
95  EWaveType::tValue mType;
96 
97  const char *GetClassName() const {return "WaveGenerator";}
98 
102  bool ConcreteConfigure(const ProcessingConfig&);
103 
104  inline TData Sine(TTime pos);
105 
106  public:
107 
108 
109  inline TData GetXPos() const
110  {
111  return mXPos;
112  }
113 
114  inline void SetXPos( TData new_value )
115  {
116  mXPos = new_value;
117  }
118 
119  inline TData GetXDelta() const
120  {
121  return mXDelta;
122  }
123 
124  inline void SetXDelta( TData new_value )
125  {
126  mXDelta = new_value;
127  }
128 
129  inline TData GetAmplitude() const
130  {
131  return mAmplitude;
132  }
133 
135 
136  WaveGenerator();
137 
139 
140  virtual ~WaveGenerator();
141 
144  const ProcessingConfig &GetConfig() const { return mConfig;}
145 
148  bool Do(void);
149 
153  bool Do(Audio& in);
154 
155  private:
156 
157 
158  };
159 
160  template < typename WaveType >
161  void FillBuffer(Array<TData> &buffer, WaveGenerator& generator, WaveType* dummy = 0 )
162  {
163  TData xvalue = generator.GetXPos();
164  TData xdelta = generator.GetXDelta();
165  TData amplitude = generator.GetAmplitude();
166 
167 
168  int i;
170  int size = buffer.Size();
171  for (i=0; i<size; i++) {
172  buffer[i] = func( xvalue, amplitude);
173  xvalue += xdelta;
174  }
175  if (xvalue > 2.0 * M_PI)
176  xvalue = fmod(xvalue, TData( 2.0 * M_PI ));
177 
178  generator.SetXPos( xvalue );
179  }
180 
181 
182 }//namespace CLAM
183 
184 #endif // _WaveGenerator_
185