Async  1.5.0
AsyncAudioSource.h
Go to the documentation of this file.
1 
28 #ifndef ASYNC_AUDIO_SOURCE_INCLUDED
29 #define ASYNC_AUDIO_SOURCE_INCLUDED
30 
31 
32 /****************************************************************************
33  *
34  * System Includes
35  *
36  ****************************************************************************/
37 
38 #include <cassert>
39 
40 
41 /****************************************************************************
42  *
43  * Project Includes
44  *
45  ****************************************************************************/
46 
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 class AudioSink;
82 
83 
84 /****************************************************************************
85  *
86  * Defines & typedefs
87  *
88  ****************************************************************************/
89 
90 
91 
92 /****************************************************************************
93  *
94  * Exported Global Variables
95  *
96  ****************************************************************************/
97 
98 
99 
100 /****************************************************************************
101  *
102  * Class definitions
103  *
104  ****************************************************************************/
105 
114 class AudioSource
115 {
116  public:
120  AudioSource(void)
121  : m_sink(0), m_sink_managed(false), m_handler(0),
122  m_auto_unreg_source(false), is_flushing(false)
123  {
124  }
125 
129  virtual ~AudioSource(void);
130 
138  bool registerSink(AudioSink *sink, bool managed=false);
139 
143  void unregisterSink(void);
144 
149  bool isRegistered(void) const { return m_sink != 0; }
150 
156  AudioSink *sink(void) const { return m_sink; }
157 
166  bool sinkManaged(void) const { return m_sink_managed; }
167 
175  void handleAllSamplesFlushed(void)
176  {
177  is_flushing = false;
179  }
180 
189  virtual void resumeOutput(void)
190  {
191  assert(m_handler != 0);
192  m_handler->resumeOutput();
193  }
194 
195 
196  protected:
206  virtual void allSamplesFlushed(void)
207  {
208  assert(m_handler != 0);
209  m_handler->handleAllSamplesFlushed();
210  }
211 
212  /*
213  * @brief Write samples to the connected sink
214  * @param samples The buffer containing the samples to write
215  * @param len The number of samples in the buffer
216  * @return Return the number of samples that was taken care of
217  *
218  * This function is used by the inheriting class to write samples to
219  * the connected sink, if any. If there is no connected sink, the samples
220  * will be thrown away. This function will return the number of samples
221  * that was taken care of. Samples that was not taken care of should
222  * normally be written again to the sink.
223  */
224  int sinkWriteSamples(const float *samples, int len);
225 
226  /*
227  * @brief Tell the sink to flush any buffered samples
228  *
229  * This function is used by the inheriting class to tell the connected
230  * sink to flush its buffered samples. When the sink have flushed all its
231  * samples it will call the allSamplesFlushed function in this class.
232  * If there is no registered sink the allSamplesFlushed function will be
233  * called right away.
234  */
235  void sinkFlushSamples(void);
236 
247 
248  /*
249  * @brief Return the handler
250  * @return Returns the handler previously set with setHandler or 0
251  * if none have been set
252  */
253  AudioSource *handler(void) const { return m_handler; }
254 
258  void clearHandler(void);
259 
260 
261  private:
262  AudioSink *m_sink;
263  bool m_sink_managed;
264  AudioSource *m_handler;
265  bool m_auto_unreg_source;
266  bool is_flushing;
267 
268  bool registerSinkInternal(AudioSink *sink, bool managed, bool reg);
269  void unregisterSinkInternal(bool is_being_destroyed);
270 
271 }; /* class AudioSource */
272 
273 
274 } /* namespace */
275 
276 #endif /* ASYNC_AUDIO_SOURCE_INCLUDED */
277 
278 
279 
280 /*
281  * This file has not been truncated
282  */
283 
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::AudioSource::clearHandler
void clearHandler(void)
Clear a handler that was previously setup with setHandler.
Async::AudioSource::handler
AudioSource * handler(void) const
Definition: AsyncAudioSource.h:285
Async::AudioSource::allSamplesFlushed
virtual void allSamplesFlushed(void)
The registered sink has flushed all samples.
Definition: AsyncAudioSource.h:238
Async::AudioSource::registerSink
bool registerSink(AudioSink *sink, bool managed=false)
Register an audio sink to provide samples to.
Async::AudioSource::AudioSource
AudioSource(void)
Default constuctor.
Definition: AsyncAudioSource.h:152
Async::AudioSource
The base class for an audio source.
Definition: AsyncAudioSource.h:134
Async::AudioSource::setHandler
bool setHandler(AudioSource *handler)
Setup another source to handle the outgoing audio.
Async::AudioSource::~AudioSource
virtual ~AudioSource(void)
Destructor.
Async::AudioSource::unregisterSink
void unregisterSink(void)
Unregister the previously registered audio sink.
Async::AudioSink
The base class for an audio sink.
Definition: AsyncAudioSink.h:135
Async::AudioSource::sinkFlushSamples
void sinkFlushSamples(void)
Async::AudioSource::resumeOutput
virtual void resumeOutput(void)
Resume audio output to the sink.
Definition: AsyncAudioSource.h:221
Async
Namespace for the asynchronous programming classes.
Definition: AsyncApplication.h:75
Async::AudioSource::handleAllSamplesFlushed
void handleAllSamplesFlushed(void)
The registered sink has flushed all samples.
Definition: AsyncAudioSource.h:207
Async::AudioSource::sinkManaged
bool sinkManaged(void) const
Check if the sink is managed or not.
Definition: AsyncAudioSource.h:198
Async::AudioSource::isRegistered
bool isRegistered(void) const
Check if an audio sink has been registered.
Definition: AsyncAudioSource.h:181