27#ifndef ASYNC_AUDIO_THREAD_SOURCE_INCLUDED
28#define ASYNC_AUDIO_THREAD_SOURCE_INCLUDED
39#include <condition_variable>
164 std::lock_guard<std::mutex> lk(m_in_buf_mu);
166 m_in_buf.insert(m_in_buf.end(), samples, samples+count);
167 if (!m_pending_processing)
169 m_pending_processing =
true;
170 m_runner(&AudioThreadSource::resumeOutput,
this);
186 std::lock_guard<std::mutex> lk(m_in_buf_mu);
188 if (!m_pending_processing)
190 m_pending_processing =
true;
191 m_runner(&AudioThreadSource::resumeOutput,
this);
205 std::unique_lock<std::mutex> lk(m_all_flushed_mu);
206 m_all_flushed_cond.wait(lk, [
this]{
return m_all_flushed; });
210 std::mutex m_in_buf_mu;
211 std::vector<float> m_in_buf;
212 std::vector<float> m_out_buf;
213 std::vector<float>::iterator m_out_buf_it = m_out_buf.begin();
214 bool m_flush =
false;
215 bool m_all_flushed =
true;
216 std::mutex m_all_flushed_mu;
217 std::condition_variable_any m_all_flushed_cond;
218 bool m_pending_processing =
false;
228 void resumeOutput(
void)
233 std::lock_guard<std::mutex> lk(m_in_buf_mu);
234 m_pending_processing =
false;
235 if (m_out_buf.empty() && !m_in_buf.empty())
237 m_out_buf.swap(m_in_buf);
238 m_out_buf_it = m_out_buf.begin();
241 if (!m_out_buf.empty())
243 int write_cnt = m_out_buf.end()-m_out_buf_it;
247 m_out_buf_it += std::min(cnt, write_cnt);
248 if (m_out_buf_it == m_out_buf.end())
251 m_out_buf_it = m_out_buf.begin();
264 std::lock_guard<std::mutex> lk(m_in_buf_mu);
268 std::lock_guard<std::mutex> lk(m_all_flushed_mu);
269 m_all_flushed =
false;
285 void allSamplesFlushed(
void)
288 std::lock_guard<std::mutex> lk(m_all_flushed_mu);
289 m_all_flushed =
true;
291 m_all_flushed_cond.notify_all();
The core class for writing asyncronous applications.
This file contains the base class for an audio sink.
This file contains the base class for an audio source.
Run tasks from the Async main thread.
static Application & app(void)
Get the one and only application instance.
The base class for an audio source.
void sinkFlushSamples(void)
int sinkWriteSamples(const float *samples, int len)
A threadsafe audio source base class.
virtual ~AudioThreadSource(void)
Destructor.
AudioThreadSource & operator=(const AudioThreadSource &)=delete
AudioThreadSource(const AudioThreadSource &)=delete
No copy constructor.
AudioThreadSource(void)
Default constructor.
virtual int sinkWriteSamples(const float *samples, int count)
Queue samples for delivery to the connected sink.
virtual void sinkFlushSamples(void)
Flush samples in the connected sink when the queue is empty.
void waitForAllSamplesFlushed(void)
Block execution until all samples have been flushed.
Run tasks from the Async main thread with automatic cleanup.
Namespace for the asynchronous programming classes.