Async 1.8.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#include <cassert>
39#include <iostream>
40
41
42/****************************************************************************
43 *
44 * Project Includes
45 *
46 ****************************************************************************/
47
48#include <AsyncAudioSource.h>
49
50
51/****************************************************************************
52 *
53 * Local Includes
54 *
55 ****************************************************************************/
56
57
58
59/****************************************************************************
60 *
61 * Forward declarations
62 *
63 ****************************************************************************/
64
65
66
67/****************************************************************************
68 *
69 * Namespace
70 *
71 ****************************************************************************/
72
73namespace Async
74{
75
76
77/****************************************************************************
78 *
79 * Forward declarations of classes inside of the declared namespace
80 *
81 ****************************************************************************/
82
83
84
85/****************************************************************************
86 *
87 * Defines & typedefs
88 *
89 ****************************************************************************/
90
91
92
93/****************************************************************************
94 *
95 * Exported Global Variables
96 *
97 ****************************************************************************/
98
99
100
101/****************************************************************************
102 *
103 * Class definitions
104 *
105 ****************************************************************************/
106
120{
121 public:
125 typedef enum {
128 TRIANGLE
130
136 : m_arg(0.0f), m_arginc(0.0f), m_peak(0.0f),
137 m_sample_rate(INTERNAL_SAMPLE_RATE), m_waveform(wf), m_power(0.0f),
138 m_enabled(false)
139 {
140 }
141
146 {
147 enable(false);
148 }
149
155 {
156 m_waveform = wf;
157 calcLevel();
158 }
159
164 void setFq(float tone_fq)
165 {
166 m_arginc = 2.0f * M_PI * tone_fq / m_sample_rate;
167 assert(m_arginc <= M_PI);
168 }
169
177 void setPower(float pwr_db)
178 {
179 m_power = powf(10.0f, pwr_db / 10.0f) / 2.0f;
180 calcLevel();
181 }
182
188 void enable(bool enable)
189 {
190 m_enabled = enable;
191 if (enable)
192 {
193 m_arg = 0.0f;
194 writeSamples();
195 }
196 else
197 {
199 }
200 }
201
207 void resumeOutput(void)
208 {
209 if (m_enabled)
210 {
211 writeSamples();
212 }
213 }
214
221 {
222 }
223
224 private:
225 static const int BLOCK_SIZE = 128;
226
227 float m_arg;
228 float m_arginc;
229 float m_peak;
230 int m_sample_rate;
231 Waveform m_waveform;
232 float m_power;
233 bool m_enabled;
234
236 AudioGenerator& operator=(const AudioGenerator&);
237
242 void calcLevel(void)
243 {
244 switch (m_waveform)
245 {
246 case SIN:
247 m_peak = sqrt(2.0f * m_power);
248 break;
249 case SQUARE:
250 m_peak = sqrt(m_power);
251 break;
252 case TRIANGLE:
253 m_peak = sqrt(3.0f * m_power);
254 break;
255 default:
256 m_peak = 0.0f;
257 break;
258 }
259 }
260
264 void writeSamples(void)
265 {
266 int written = 0;
267 do
268 {
269 float buf[BLOCK_SIZE];
270 float arg = m_arg;
271 for (int i=0; i<BLOCK_SIZE; ++i)
272 {
273 switch (m_waveform)
274 {
275 case SIN:
276 buf[i] = m_peak * sinf(arg);
277 break;
278 case SQUARE:
279 buf[i] = (arg < M_PI) ? m_peak : -m_peak;
280 break;
281 case TRIANGLE:
282 if (arg < M_PI / 2.0f)
283 {
284 buf[i] = m_peak * arg * 2.0f / M_PI;
285 }
286 else if (arg < M_PI)
287 {
288 buf[i] = m_peak * (2.0f - 2.0 * arg / M_PI);
289 }
290 else if (arg < 3.0f * M_PI / 2.0f)
291 {
292 buf[i] = -m_peak * (2.0f * arg / M_PI - 2.0f);
293 }
294 else
295 {
296 buf[i] = -m_peak * (4.0f - 2.0f * arg / M_PI);
297 }
298 break;
299 default:
300 buf[i] = 0;
301 break;
302 }
303 arg += m_arginc;
304 if (arg >= 2.0f * M_PI)
305 {
306 arg -= 2.0f * M_PI;
307 }
308 }
309 written = sinkWriteSamples(buf, BLOCK_SIZE);
310 if (written > 0)
311 {
312 m_arg += written * m_arginc;
313 while (m_arg >= 2.0f * M_PI)
314 {
315 m_arg -= 2.0f * M_PI;
316 }
317 }
318 } while (m_enabled && (written > 0));
319 }
320}; /* class AudioGenerator */
321
322
323} /* namespace */
324
325#endif /* ASYNC_AUDIO_GENERATOR_INCLUDED */
326
327/*
328 * This file has not been truncated
329 */
This file contains the base class for an audio source.
A class for generating periodic audio signals.
void enable(bool enable)
Enable or disable the generator.
void allSamplesFlushed(void)
The registered sink has flushed all samples.
void setPower(float pwr_db)
Set the power of the generated signal.
void setFq(float tone_fq)
Set the audio frequency.
Waveform
The type of waveform to generate.
void resumeOutput(void)
Resume audio output to the sink.
~AudioGenerator(void)
Destructor.
void setWaveform(Waveform wf)
Set which waveform to use.
AudioGenerator(Waveform wf=SIN)
Contructor.
The base class for an audio source.
void sinkFlushSamples(void)
int sinkWriteSamples(const float *samples, int len)
Namespace for the asynchronous programming classes.