Async 1.9.1
AsyncAudioStreamStateDetector.h
Go to the documentation of this file.
1
27
28
29#ifndef ASYNC_AUDIO_STREAM_STATE_DETECTOR_INCLUDED
30#define ASYNC_AUDIO_STREAM_STATE_DETECTOR_INCLUDED
31
32
33/****************************************************************************
34 *
35 * System Includes
36 *
37 ****************************************************************************/
38
39#include <sigc++/sigc++.h>
40
41
42/****************************************************************************
43 *
44 * Project Includes
45 *
46 ****************************************************************************/
47
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
73namespace 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
126 : public AudioPassthrough, virtual public sigc::trackable
127{
128 public:
132 enum class State
133 {
137 };
138
143
147 virtual ~AudioStreamStateDetector(void) override {}
148
160 virtual int writeSamples(const float *samples, int count) override
161 {
162 setState(State::ACTIVE);
163 return AudioPassthrough::writeSamples(samples, count);
164 }
165
174 virtual void flushSamples(void) override
175 {
176 setState(State::FLUSHING);
178 }
179
187 virtual void allSamplesFlushed(void) override
188 {
189 setState(State::IDLE);
191 }
192
197 bool isIdle(void) const { return (m_state == State::IDLE); }
198
204 bool isActive(void) const { return (m_state == State::ACTIVE); }
205
211 bool isFlushing(void) const { return (m_state == State::FLUSHING); }
212
217 State state(void) const { return m_state; }
218
224 sigc::signal<void(bool, bool)> sigStreamStateChanged;
225
230 sigc::signal<void(bool)> sigStreamIsActive;
231
236 sigc::signal<void(bool)> sigStreamIsIdle;
237
238 private:
241
242 struct StateMapValue
243 {
244 bool is_active;
245 bool is_idle;
246 };
247 using StateMap = std::map<State, StateMapValue>;
248
249 const StateMap STATE_MAP {
250 {State::IDLE, {false, true} },
251 {State::ACTIVE, {true, false} },
252 {State::FLUSHING, {false, false}}
253 };
254 State m_state {State::IDLE};
255
256 void setState(State new_state)
257 {
258 if (new_state == m_state)
259 {
260 return;
261 }
262
263 auto& old_mapping = STATE_MAP.at(m_state);
264 auto& new_mapping = STATE_MAP.at(new_state);
265 m_state = new_state;
266
267 if (new_mapping.is_active != old_mapping.is_active)
268 {
269 sigStreamIsActive(new_mapping.is_active);
270 }
271
272 if (new_mapping.is_idle != old_mapping.is_idle)
273 {
274 sigStreamIsIdle(new_mapping.is_idle);
275 }
276
277 sigStreamStateChanged(new_mapping.is_active, new_mapping.is_idle);
278 }
279}; /* AudioStreamStateDetector */
280
281
282} /* namespace */
283
284#endif /* ASYNC_AUDIO_STREAM_STATE_DETECTOR_INCLUDED */
285
286
287
288/*
289 * This file has not been truncated
290 */
291
This file contains a class that just pass the audio through.
virtual void allSamplesFlushed(void)
The registered sink has flushed all samples.
virtual int writeSamples(const float *samples, int count)
Write samples into this audio sink.
AudioPassthrough(void)
Default constuctor.
virtual void flushSamples(void)
Tell the sink to flush the previously written samples.
sigc::signal< void(bool)> sigStreamIsIdle
A signal that is emitted when stream idle state changes.
sigc::signal< void(bool)> sigStreamIsActive
A signal that is emitted when stream activity state changes.
virtual ~AudioStreamStateDetector(void) override
Destructor.
State state(void) const
Get the stream state.
bool isActive(void) const
Check if the steam is active or not.
virtual void allSamplesFlushed(void) override
The registered sink has flushed all samples.
bool isIdle(void) const
Check if the steam is idle or not.
sigc::signal< void(bool, bool)> sigStreamStateChanged
A signal that is emitted when the stream state changes.
virtual int writeSamples(const float *samples, int count) override
Write samples into this audio sink.
virtual void flushSamples(void) override
Tell the sink to flush the previously written samples.
State
An enum representing the stream state.
@ ACTIVE
Samples have been written to the stream.
@ FLUSHING
The stream source have requested stream flush.
@ IDLE
There are no samples left in the stream.
bool isFlushing(void) const
Check if the steam is flushing or not.
Namespace for the asynchronous programming classes.