Async  1.5.0
AsyncAudioGenerator.h
Go to the documentation of this file.
1 
27 #ifndef ASYNC_AUDIO_GENERATOR_INCLUDED
28 #define ASYNC_AUDIO_GENERATOR_INCLUDED
29 
30 
31 /****************************************************************************
32  *
33  * System Includes
34  *
35  ****************************************************************************/
36 
37 #include <cmath>
38 
39 
40 /****************************************************************************
41  *
42  * Project Includes
43  *
44  ****************************************************************************/
45 
46 #include <AsyncAudioSource.h>
47 
48 
49 /****************************************************************************
50  *
51  * Local Includes
52  *
53  ****************************************************************************/
54 
55 
56 
57 /****************************************************************************
58  *
59  * Forward declarations
60  *
61  ****************************************************************************/
62 
63 
64 
65 /****************************************************************************
66  *
67  * Namespace
68  *
69  ****************************************************************************/
70 
71 namespace Async
72 {
73 
74 
75 /****************************************************************************
76  *
77  * Forward declarations of classes inside of the declared namespace
78  *
79  ****************************************************************************/
80 
81 
82 
83 /****************************************************************************
84  *
85  * Defines & typedefs
86  *
87  ****************************************************************************/
88 
89 
90 
91 /****************************************************************************
92  *
93  * Exported Global Variables
94  *
95  ****************************************************************************/
96 
97 
98 
99 /****************************************************************************
100  *
101  * Class definitions
102  *
103  ****************************************************************************/
104 
118 {
119  public:
123  typedef enum {
124  SIN,
126  } Waveform;
127 
133  : pos(0), fq(0.0), level(0.0), sample_rate(INTERNAL_SAMPLE_RATE),
134  waveform(wf), power(0.0)
135  {
136  }
137 
142  {
143  enable(false);
144  }
145 
151  {
152  waveform = wf;
153  calcLevel();
154  }
155 
160  void setFq(double tone_fq)
161  {
162  fq = tone_fq;
163  }
164 
172  void setPower(float pwr_db)
173  {
174  power = pow(10.0, pwr_db / 10.0f) / 2;
175  calcLevel();
176  }
177 
183  void enable(bool enable)
184  {
185  if (enable && (fq != 0))
186  {
187  pos = 0;
188  writeSamples();
189  }
190  }
191 
197  void resumeOutput(void)
198  {
199  writeSamples();
200  }
201 
207  void allSamplesFlushed(void)
208  {
209  }
210 
211  private:
212  static const int BLOCK_SIZE = 128;
213 
214  unsigned pos;
215  double fq;
216  double level;
217  int sample_rate;
218  Waveform waveform;
219  float power;
220 
222  AudioGenerator& operator=(const AudioGenerator&);
223 
228  void calcLevel(void)
229  {
230  switch (waveform)
231  {
232  case SIN:
233  level = sqrt(2 * power);
234  break;
235  case SQUARE:
236  level = sqrt(power);
237  break;
238  default:
239  level = 1.0;
240  break;
241  }
242  }
243 
247  void writeSamples(void)
248  {
249  int written;
250  do {
251  float buf[BLOCK_SIZE];
252  for (int i=0; i<BLOCK_SIZE; ++i)
253  {
254  switch (waveform)
255  {
256  case SIN:
257  buf[i] = level * sin(2 * M_PI * fq * (pos+i) / sample_rate);
258  break;
259  case SQUARE:
260  buf[i] = level * (sin(2 * M_PI * fq * (pos+i) / sample_rate)
261  > 0.0 ? 1 : -1);
262  break;
263  default:
264  buf[i] = 0;
265  break;
266  }
267  }
268  written = sinkWriteSamples(buf, BLOCK_SIZE);
269  pos += written;
270  } while (written != 0);
271  }
272 
273 }; /* class AudioGenerator */
274 
275 
276 } /* namespace */
277 
278 #endif /* ASYNC_AUDIO_GENERATOR_INCLUDED */
279 
280 
281 
282 /*
283  * This file has not been truncated
284  */
~AudioGenerator(void)
Destructor.
void allSamplesFlushed(void)
The registered sink has flushed all samples.
A class for generating periodic audio signals.
This file contains the base class for an audio source.
AudioGenerator(Waveform wf=SIN)
Contructor.
void setPower(float pwr_db)
Set the power of the generated signal.
void setWaveform(Waveform wf)
Set which waveform to use.
void setFq(double tone_fq)
Set the audio frequency.
void enable(bool enable)
Enable or disable the generator.
int sinkWriteSamples(const float *samples, int len)
Namespace for the asynchronous programming classes.
Waveform
The type of waveform to generate.
The base class for an audio source.
void resumeOutput(void)
Resume audio output to the sink.