arduino-audio-tools
Loading...
Searching...
No Matches
SynchronizedBuffer.h
Go to the documentation of this file.
1#pragma once
2#include <algorithm>
3#include "AudioToolsConfig.h"
7#include "Mutex.h"
8#include "LockGuard.h"
9
10namespace audio_tools {
11
21template <typename T>
22class SynchronizedBuffer : public BaseBuffer<T> {
23public:
25 p_buffer = &buffer;
26 p_mutex = &mutex;
28 }
29
30 // reads a single value
31 bool read(T &result) override {
32 TRACED();
34 return p_buffer->read(result);
35 }
36
37 // reads multiple values
38 int readArray(T data[], int len) {
39 TRACED();
41 int lenResult = std::min(len, available());
42 return p_buffer->readArray(data, lenResult);
43 }
44
45 int writeArray(const T data[], int len) {
46 LOGD("%s: %d", LOG_METHOD, len);
48 return p_buffer->writeArray(data, len);
49 }
50
51 // peeks the actual entry from the buffer
52 bool peek(T &result) override {
53 TRACED();
55 return p_buffer->peek(result);
56 }
57
58 // checks if the buffer is full
59 bool isFull() override { return p_buffer->isFull(); }
60
61 bool isEmpty() { return available() == 0; }
62
63 // write add an entry to the buffer
64 bool write(T data) override {
65 TRACED();
67 return p_buffer->write(data);
68 }
69
70 // clears the buffer
71 void reset() override {
72 TRACED();
74 p_buffer->reset();
75 }
76
77 // provides the number of entries that are available to read
78 int available() override {
79 TRACED();
81 return p_buffer->available();
82 }
83
84 // provides the number of entries that are available to write
85 int availableForWrite() override {
86 TRACED();
89 }
90
91 // returns the address of the start of the physical read buffer
92 T *address() override {
93 TRACED();
94 return p_buffer->address();
95 }
96
97 size_t size() {
98 return p_buffer->size();
99 }
100
101protected:
103 MutexBase *p_mutex = nullptr;
104 bool is_sync_available = false;
105};
106
107
108} // namespace audio_tools
109
#define TRACED()
Definition AudioLoggerIDF.h:31
#define LOGD(...)
Definition AudioLoggerIDF.h:27
#define LOG_METHOD
Definition AudioToolsConfig.h:74
Shared functionality of all buffers.
Definition Buffers.h:23
virtual bool read(T &result)=0
reads a single value
virtual int readArray(T data[], int len)
reads multiple values
Definition Buffers.h:34
virtual void reset()=0
clears the buffer
virtual int writeArray(const T data[], int len)
Fills the buffer data.
Definition Buffers.h:56
virtual T * address()=0
returns the address of the start of the physical read buffer
virtual size_t size()=0
virtual int availableForWrite()=0
provides the number of entries that are available to write
virtual bool isFull()
checks if the buffer is full
Definition Buffers.h:85
virtual bool peek(T &result)=0
peeks the actual entry from the buffer
virtual bool write(T data)=0
write add an entry to the buffer
virtual int available()=0
provides the number of entries that are available to read
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:22
bool is_sync_available
Definition SynchronizedBuffer.h:104
size_t size()
Definition SynchronizedBuffer.h:97
BaseBuffer< T > * p_buffer
Definition SynchronizedBuffer.h:102
MutexBase * p_mutex
Definition SynchronizedBuffer.h:103
bool peek(T &result) override
peeks the actual entry from the buffer
Definition SynchronizedBuffer.h:52
bool read(T &result) override
reads a single value
Definition SynchronizedBuffer.h:31
bool write(T data) override
write add an entry to the buffer
Definition SynchronizedBuffer.h:64
int available() override
provides the number of entries that are available to read
Definition SynchronizedBuffer.h:78
int availableForWrite() override
provides the number of entries that are available to write
Definition SynchronizedBuffer.h:85
T * address() override
returns the address of the start of the physical read buffer
Definition SynchronizedBuffer.h:92
bool isFull() override
checks if the buffer is full
Definition SynchronizedBuffer.h:59
int writeArray(const T data[], int len)
Fills the buffer data.
Definition SynchronizedBuffer.h:45
void reset() override
clears the buffer
Definition SynchronizedBuffer.h:71
bool isEmpty()
Definition SynchronizedBuffer.h:61
SynchronizedBuffer(BaseBuffer< T > &buffer, MutexBase &mutex, bool syncAvailable=false)
Definition SynchronizedBuffer.h:24
int readArray(T data[], int len)
reads multiple values
Definition SynchronizedBuffer.h:38
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition AudioCodecsBase.h:10
size_t writeData(Print *p_out, T *data, int samples, int maxSamples=512)
Definition AudioTypes.h:508