arduino-audio-tools
All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends Modules Pages
SynchronizedBuffer.h
1#pragma once
2#include "AudioToolsConfig.h"
3#include "AudioTools/CoreAudio/AudioTypes.h"
4#include "AudioTools/CoreAudio/Buffers.h"
5#include "AudioTools/CoreAudio/AudioLogger.h"
6#include "Mutex.h"
7#include "LockGuard.h"
8
9namespace audio_tools {
10
20template <typename T>
21class SynchronizedBuffer : public BaseBuffer<T> {
22public:
23 SynchronizedBuffer(BaseBuffer<T> &buffer, MutexBase &mutex, bool syncAvailable=false) {
24 p_buffer = &buffer;
25 p_mutex = &mutex;
26 is_sync_available = syncAvailable;
27 }
28
29 // reads a single value
30 bool read(T &result) override {
31 TRACED();
32 LockGuard guard(p_mutex);
33 return p_buffer->read(result);
34 }
35
36 // reads multiple values
37 int readArray(T data[], int len) {
38 TRACED();
39 LockGuard guard(p_mutex);
40 int lenResult = MIN(len, available());
41 return p_buffer->readArray(data, lenResult);
42 }
43
44 int writeArray(const T data[], int len) {
45 LOGD("%s: %d", LOG_METHOD, len);
46 LockGuard guard(p_mutex);
47 return p_buffer->writeArray(data, len);
48 }
49
50 // peeks the actual entry from the buffer
51 bool peek(T &result) override {
52 TRACED();
53 LockGuard guard(p_mutex);
54 return p_buffer->peek(result);
55 }
56
57 // checks if the buffer is full
58 bool isFull() override { return p_buffer->isFull(); }
59
60 bool isEmpty() { return available() == 0; }
61
62 // write add an entry to the buffer
63 bool write(T data) override {
64 TRACED();
65 LockGuard guard(p_mutex);
66 return p_buffer->write(data);
67 }
68
69 // clears the buffer
70 void reset() override {
71 TRACED();
72 LockGuard guard(p_mutex);
73 p_buffer->reset();
74 }
75
76 // provides the number of entries that are available to read
77 int available() override {
78 TRACED();
79 if (is_sync_available) LockGuard guard(p_mutex);
80 return p_buffer->available();
81 }
82
83 // provides the number of entries that are available to write
84 int availableForWrite() override {
85 TRACED();
86 if (is_sync_available) LockGuard guard(p_mutex);
87 return p_buffer->availableForWrite();
88 }
89
90 // returns the address of the start of the physical read buffer
91 T *address() override {
92 TRACED();
93 return p_buffer->address();
94 }
95
96 size_t size() {
97 return p_buffer->size();
98 }
99
100protected:
101 BaseBuffer<T> *p_buffer = nullptr;
102 MutexBase *p_mutex = nullptr;
103 bool is_sync_available = false;
104};
105
106
107} // namespace audio_tools
108
Shared functionality of all buffers.
Definition Buffers.h:26
RAII implementaion using a Mutex: Only a few microcontrollers provide lock guards,...
Definition LockGuard.h:17
Empty Mutex implementation which does nothing.
Definition Mutex.h:18
Wrapper class that can turn any Buffer into a thread save implementation.
Definition SynchronizedBuffer.h:21
bool peek(T &result) override
peeks the actual entry from the buffer
Definition SynchronizedBuffer.h:51
bool read(T &result) override
reads a single value
Definition SynchronizedBuffer.h:30
bool write(T data) override
write add an entry to the buffer
Definition SynchronizedBuffer.h:63
int available() override
provides the number of entries that are available to read
Definition SynchronizedBuffer.h:77
int availableForWrite() override
provides the number of entries that are available to write
Definition SynchronizedBuffer.h:84
T * address() override
returns the address of the start of the physical read buffer
Definition SynchronizedBuffer.h:91
bool isFull() override
checks if the buffer is full
Definition SynchronizedBuffer.h:58
int writeArray(const T data[], int len)
Fills the buffer data.
Definition SynchronizedBuffer.h:44
void reset() override
clears the buffer
Definition SynchronizedBuffer.h:70
int readArray(T data[], int len)
reads multiple values
Definition SynchronizedBuffer.h:37
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition AudioCodecsBase.h:10