AsyncAudioValve.h
Go to the documentation of this file.00001
00028 #ifndef ASYNC_AUDIO_VALVE_INCLUDED
00029 #define ASYNC_AUDIO_VALVE_INCLUDED
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046 #include <AsyncAudioSink.h>
00047 #include <AsyncAudioSource.h>
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073 namespace Async
00074 {
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00116 class AudioValve : public Async::AudioSink, public Async::AudioSource
00117 {
00118 public:
00122 explicit AudioValve(void)
00123 : block_when_closed(false), is_open(true),
00124 is_idle(true), is_flushing(false), input_stopped(false)
00125 {
00126 }
00127
00131 ~AudioValve(void) {}
00132
00142 void setOpen(bool do_open)
00143 {
00144 if (is_open == do_open)
00145 {
00146 return;
00147 }
00148
00149 is_open = do_open;
00150
00151 if (do_open)
00152 {
00153 if (input_stopped)
00154 {
00155 input_stopped = false;
00156 sourceResumeOutput();
00157 }
00158 }
00159 else
00160 {
00161 if (!is_idle && !is_flushing)
00162 {
00163 sinkFlushSamples();
00164 }
00165 if (!block_when_closed && input_stopped)
00166 {
00167 input_stopped = false;
00168 sourceResumeOutput();
00169 }
00170 if (is_flushing)
00171 {
00172 is_idle = true;
00173 is_flushing = false;
00174 sourceAllSamplesFlushed();
00175 }
00176 }
00177 }
00178
00188 void setBlockWhenClosed(bool block_when_closed)
00189 {
00190 if (block_when_closed == this->block_when_closed)
00191 {
00192 return;
00193 }
00194
00195 this->block_when_closed = block_when_closed;
00196
00197 if (!block_when_closed && input_stopped)
00198 {
00199 input_stopped = false;
00200 sourceResumeOutput();
00201 }
00202 }
00203
00208 bool isOpen(void) const
00209 {
00210 return is_open;
00211 }
00212
00217 bool isIdle(void) const
00218 {
00219 return is_idle;
00220 }
00221
00233 int writeSamples(const float *samples, int count)
00234 {
00235 int ret = 0;
00236 is_idle = false;
00237 is_flushing = false;
00238 if (is_open)
00239 {
00240 ret = sinkWriteSamples(samples, count);
00241 }
00242 else
00243 {
00244 ret = (block_when_closed ? 0 : count);
00245 }
00246
00247 if (ret == 0)
00248 {
00249 input_stopped = true;
00250 }
00251
00252 return ret;
00253 }
00254
00263 void flushSamples(void)
00264 {
00265 if (is_open)
00266 {
00267 is_flushing = true;
00268 sinkFlushSamples();
00269 }
00270 else
00271 {
00272 is_flushing = false;
00273 is_idle = true;
00274 sourceAllSamplesFlushed();
00275 }
00276 }
00277
00286 void resumeOutput(void)
00287 {
00288 if (is_open)
00289 {
00290 if (input_stopped)
00291 {
00292 input_stopped = false;
00293 sourceResumeOutput();
00294 }
00295 }
00296 }
00297
00305 virtual void allSamplesFlushed(void)
00306 {
00307 bool was_flushing = is_flushing;
00308 is_flushing = false;
00309 is_idle = true;
00310 if (is_open && was_flushing)
00311 {
00312 sourceAllSamplesFlushed();
00313 }
00314 }
00315
00316
00317 protected:
00318
00319 private:
00320 AudioValve(const AudioValve&);
00321 AudioValve& operator=(const AudioValve&);
00322
00323 bool block_when_closed;
00324 bool is_open;
00325 bool is_idle;
00326 bool is_flushing;
00327 bool input_stopped;
00328
00329 };
00330
00331
00332 }
00333
00334 #endif
00335
00336
00337
00338
00339
00340
00341