Async  1.6.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  const std::string& name="AudioDebugger")
127  : name(name), sample_count(0)
128  {
129  gettimeofday(&start_time, 0);
130  if (src != 0)
131  {
132  Async::AudioSink *sink = src->sink();
133  if (sink != 0)
134  {
135  src->unregisterSink();
136  registerSink(sink);
137  }
138  registerSource(src);
139  }
140  }
141 
145  virtual ~AudioDebugger(void) {}
146 
151  void setName(std::string debug_name) { name = debug_name; }
152 
164  virtual int writeSamples(const float *samples, int count)
165  {
166  int ret = sinkWriteSamples(samples, count);
167  sample_count += ret;
168 
169  float max_samp = 0.0f;
170  for (int i=0; i<count; ++i)
171  {
172  if (samples[i] > max_samp)
173  {
174  max_samp = samples[i];
175  }
176  if (-samples[i] > max_samp)
177  {
178  max_samp = -samples[i];
179  }
180  }
181 
182  struct timeval time, diff;
183  gettimeofday(&time, 0);
184 
185  timersub(&time, &start_time, &diff);
186  uint64_t diff_ms = diff.tv_sec * 1000 + diff.tv_usec / 1000;
187 
188  std::cout << name << "::writeSamples: count=" << count
189  << " ret=" << ret << " sample_rate=";
190  if (diff_ms > 0)
191  {
192  std::cout << sample_count * 1000 / diff_ms;
193  }
194  else
195  {
196  std::cout << "inf";
197  }
198  std::cout << " max=" << max_samp;
199  std::cout << std::endl;
200  return ret;
201  }
202 
211  virtual void flushSamples(void)
212  {
213  std::cout << name << "::flushSamples\n";
215  }
216 
224  virtual void resumeOutput(void)
225  {
226  std::cout << name << "::resumeOutput\n";
228  }
229 
237  virtual void allSamplesFlushed(void)
238  {
239  std::cout << name << "::allSamplesFlushed\n";
241  }
242 
243  protected:
244 
245  private:
246  std::string name;
247  struct timeval start_time;
248  uint64_t sample_count;
249 
251  AudioDebugger& operator=(const AudioDebugger&);
252 
253 }; /* AudioDebugger */
254 
255 
256 } /* namespace */
257 
258 #endif /* AUDIO_DEBUGGER_INCLUDED */
259 
260 
261 
262 /*
263  * This file has not been truncated
264  */
265 
AudioSink * sink(void) const
Get the registered audio sink.
AudioDebugger(Async::AudioSource *src=0, const std::string &name="AudioDebugger")
Default constuctor.
void sinkFlushSamples(void)
void setName(std::string debug_name)
Set the name that is displayed before debug messages.
virtual void flushSamples(void)
Tell the sink to flush the previously written samples.
This file contains the base class for an audio source.
virtual int writeSamples(const float *samples, int count)
Write samples into this audio sink.
bool registerSink(AudioSink *sink, bool managed=false)
Register an audio sink to provide samples to.
This file contains the base class for an audio sink.
int sinkWriteSamples(const float *samples, int len)
The base class for an audio sink.
Namespace for the asynchronous programming classes.
bool registerSource(AudioSource *source)
Register an audio source to provide samples to this sink.
void sourceResumeOutput(void)
Tell the source that we are ready to accept more samples.
void sourceAllSamplesFlushed(void)
Tell the source that all samples have been flushed.
The base class for an audio source.
virtual void resumeOutput(void)
Resume audio output to the sink.
virtual void allSamplesFlushed(void)
The registered sink has flushed all samples.
This class is used to debug an audio stream.
virtual ~AudioDebugger(void)
Destructor.