Async  1.5.0
AsyncAudioDebugger.h
Go to the documentation of this file.
1 
28 #ifndef AUDIO_DEBUGGER_INCLUDED
29 #define AUDIO_DEBUGGER_INCLUDED
30 
31 
32 /****************************************************************************
33  *
34  * System Includes
35  *
36  ****************************************************************************/
37 
38 #include <sys/time.h>
39 #include <iostream>
40 #include <string>
41 #include <stdint.h>
42 
43 
44 /****************************************************************************
45  *
46  * Project Includes
47  *
48  ****************************************************************************/
49 
50 #include <AsyncAudioSink.h>
51 #include <AsyncAudioSource.h>
52 
53 
54 /****************************************************************************
55  *
56  * Local Includes
57  *
58  ****************************************************************************/
59 
60 
61 
62 /****************************************************************************
63  *
64  * Forward declarations
65  *
66  ****************************************************************************/
67 
68 
69 
70 /****************************************************************************
71  *
72  * Namespace
73  *
74  ****************************************************************************/
75 
76 namespace Async
77 {
78 
79 
80 /****************************************************************************
81  *
82  * Forward declarations of classes inside of the declared namespace
83  *
84  ****************************************************************************/
85 
86 
87 
88 /****************************************************************************
89  *
90  * Defines & typedefs
91  *
92  ****************************************************************************/
93 
94 
95 
96 /****************************************************************************
97  *
98  * Exported Global Variables
99  *
100  ****************************************************************************/
101 
102 
103 
104 /****************************************************************************
105  *
106  * Class definitions
107  *
108  ****************************************************************************/
109 
119 class AudioDebugger : public AudioSink, public AudioSource
120 {
121  public:
126  : name("AudioDebugger"), sample_count(0)
127  {
128  gettimeofday(&start_time, 0);
129  if (src != 0)
130  {
131  Async::AudioSink *sink = src->sink();
132  if (sink != 0)
133  {
134  src->unregisterSink();
136  }
137  registerSource(src);
138  }
139  }
140 
144  virtual ~AudioDebugger(void) {}
145 
150  void setName(std::string debug_name) { name = debug_name; }
151 
163  virtual int writeSamples(const float *samples, int count)
164  {
165  int ret = sinkWriteSamples(samples, count);
166  sample_count += ret;
167 
168  float max_samp = 0.0f;
169  for (int i=0; i<count; ++i)
170  {
171  if (samples[i] > max_samp)
172  {
173  max_samp = samples[i];
174  }
175  if (-samples[i] > max_samp)
176  {
177  max_samp = -samples[i];
178  }
179  }
180 
181  struct timeval time, diff;
182  gettimeofday(&time, 0);
183 
184  timersub(&time, &start_time, &diff);
185  uint64_t diff_ms = diff.tv_sec * 1000 + diff.tv_usec / 1000;
186 
187  std::cout << name << "::writeSamples: count=" << count
188  << " ret=" << ret << " sample_rate=";
189  if (diff_ms > 0)
190  {
191  std::cout << sample_count * 1000 / diff_ms;
192  }
193  else
194  {
195  std::cout << "inf";
196  }
197  std::cout << " max=" << max_samp;
198  std::cout << std::endl;
199  return ret;
200  }
201 
210  virtual void flushSamples(void)
211  {
212  std::cout << name << "::flushSamples\n";
214  }
215 
223  virtual void resumeOutput(void)
224  {
225  std::cout << name << "::resumeOutput\n";
227  }
228 
236  virtual void allSamplesFlushed(void)
237  {
238  std::cout << name << "::allSamplesFlushed\n";
240  }
241 
242  protected:
243 
244  private:
245  std::string name;
246  struct timeval start_time;
247  uint64_t sample_count;
248 
250  AudioDebugger& operator=(const AudioDebugger&);
251 
252 }; /* AudioDebugger */
253 
254 
255 } /* namespace */
256 
257 #endif /* AUDIO_DEBUGGER_INCLUDED */
258 
259 
260 
261 /*
262  * This file has not been truncated
263  */
264 
Async::AudioSource::sink
AudioSink * sink(void) const
Get the registered audio sink.
Definition: AsyncAudioSource.h:188
Async::AudioSource::sinkWriteSamples
int sinkWriteSamples(const float *samples, int len)
Async::AudioDebugger::AudioDebugger
AudioDebugger(Async::AudioSource *src=0)
Default constuctor.
Definition: AsyncAudioDebugger.h:181
Async::AudioSource::registerSink
bool registerSink(AudioSink *sink, bool managed=false)
Register an audio sink to provide samples to.
Async::AudioDebugger::resumeOutput
virtual void resumeOutput(void)
Resume audio output to the sink.
Definition: AsyncAudioDebugger.h:279
Async::AudioDebugger::allSamplesFlushed
virtual void allSamplesFlushed(void)
The registered sink has flushed all samples.
Definition: AsyncAudioDebugger.h:292
Async::AudioDebugger::~AudioDebugger
virtual ~AudioDebugger(void)
Destructor.
Definition: AsyncAudioDebugger.h:200
Async::AudioSource
The base class for an audio source.
Definition: AsyncAudioSource.h:134
Async::AudioSink::registerSource
bool registerSource(AudioSource *source)
Register an audio source to provide samples to this sink.
Async::AudioSink
The base class for an audio sink.
Definition: AsyncAudioSink.h:135
Async::AudioSource::sinkFlushSamples
void sinkFlushSamples(void)
Async::AudioDebugger::flushSamples
virtual void flushSamples(void)
Tell the sink to flush the previously written samples.
Definition: AsyncAudioDebugger.h:266
Async
Namespace for the asynchronous programming classes.
Definition: AsyncApplication.h:75
Async::AudioDebugger::setName
void setName(std::string debug_name)
Set the name that is displayed before debug messages.
Definition: AsyncAudioDebugger.h:206
AsyncAudioSink.h
This file contains the base class for an audio sink.
AsyncAudioSource.h
This file contains the base class for an audio source.
Async::AudioDebugger::writeSamples
virtual int writeSamples(const float *samples, int count)
Write samples into this audio sink.
Definition: AsyncAudioDebugger.h:219
Async::AudioSink::sourceAllSamplesFlushed
void sourceAllSamplesFlushed(void)
Tell the source that all samples have been flushed.
Async::AudioSink::sourceResumeOutput
void sourceResumeOutput(void)
Tell the source that we are ready to accept more samples.