Async  0.18.0
AsyncAudioValve.h
Go to the documentation of this file.
1 
28 #ifndef ASYNC_AUDIO_VALVE_INCLUDED
29 #define ASYNC_AUDIO_VALVE_INCLUDED
30 
31 
32 /****************************************************************************
33  *
34  * System Includes
35  *
36  ****************************************************************************/
37 
38 
39 
40 /****************************************************************************
41  *
42  * Project Includes
43  *
44  ****************************************************************************/
45 
46 #include <AsyncAudioSink.h>
47 #include <AsyncAudioSource.h>
48 
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 
73 namespace 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 
117 {
118  public:
122  explicit AudioValve(void)
123  : block_when_closed(false), is_open(true),
124  is_idle(true), is_flushing(false), input_stopped(false)
125  {
126  }
127 
131  ~AudioValve(void) {}
132 
142  void setOpen(bool do_open)
143  {
144  if (is_open == do_open)
145  {
146  return;
147  }
148 
149  is_open = do_open;
150 
151  if (do_open)
152  {
153  if (input_stopped)
154  {
155  input_stopped = false;
157  }
158  }
159  else
160  {
161  if (!is_idle && !is_flushing)
162  {
164  }
165  if (!block_when_closed && input_stopped)
166  {
167  input_stopped = false;
169  }
170  if (is_flushing)
171  {
172  is_idle = true;
173  is_flushing = false;
175  }
176  }
177  }
178 
188  void setBlockWhenClosed(bool block_when_closed)
189  {
190  if (block_when_closed == this->block_when_closed)
191  {
192  return;
193  }
194 
195  this->block_when_closed = block_when_closed;
196 
197  if (!block_when_closed && input_stopped)
198  {
199  input_stopped = false;
201  }
202  }
203 
208  bool isOpen(void) const
209  {
210  return is_open;
211  }
212 
217  bool isIdle(void) const
218  {
219  return is_idle;
220  }
221 
233  int writeSamples(const float *samples, int count)
234  {
235  int ret = 0;
236  is_idle = false;
237  is_flushing = false;
238  if (is_open)
239  {
240  ret = sinkWriteSamples(samples, count);
241  }
242  else
243  {
244  ret = (block_when_closed ? 0 : count);
245  }
246 
247  if (ret == 0)
248  {
249  input_stopped = true;
250  }
251 
252  return ret;
253  }
254 
263  void flushSamples(void)
264  {
265  if (is_open)
266  {
267  is_flushing = true;
269  }
270  else
271  {
272  is_flushing = false;
273  is_idle = true;
275  }
276  }
277 
286  void resumeOutput(void)
287  {
288  if (is_open)
289  {
290  if (input_stopped)
291  {
292  input_stopped = false;
294  }
295  }
296  }
297 
305  virtual void allSamplesFlushed(void)
306  {
307  bool was_flushing = is_flushing;
308  is_flushing = false;
309  is_idle = true;
310  if (is_open && was_flushing)
311  {
313  }
314  }
315 
316 
317  protected:
318 
319  private:
320  AudioValve(const AudioValve&);
321  AudioValve& operator=(const AudioValve&);
322 
323  bool block_when_closed;
324  bool is_open;
325  bool is_idle;
326  bool is_flushing;
327  bool input_stopped;
328 
329 }; /* class AudioValve */
330 
331 
332 } /* namespace */
333 
334 #endif /* ASYNC_AUDIO_VALVE_INCLUDED */
335 
336 
337 
338 /*
339  * This file has not been truncated
340  */
341