arduino-audio-tools
SynchronizedBuffer.h
1 
2 #pragma once
3 #include "AudioConfig.h"
4 #include "AudioTools/CoreAudio/AudioTypes.h"
5 #include "AudioTools/CoreAudio/Buffers.h"
6 #include "AudioTools/CoreAudio/AudioLogger.h"
7 #include "Mutex.h"
8 #include "LockGuard.h"
9 
10 namespace audio_tools {
11 
21 template <typename T>
22 class SynchronizedBuffer : public BaseBuffer<T> {
23 public:
24  SynchronizedBuffer(BaseBuffer<T> &buffer, MutexBase &mutex, bool syncAvailable=false) {
25  p_buffer = &buffer;
26  p_mutex = &mutex;
27  is_sync_available = syncAvailable;
28  }
29 
30  // reads a single value
31  T read() override {
32  TRACED();
33  LockGuard guard(p_mutex);
34  return p_buffer->read();
35  }
36 
37  // reads multiple values
38  int readArray(T data[], int len) {
39  TRACED();
40  LockGuard guard(p_mutex);
41  int lenResult = MIN(len, available());
42  for (int j = 0; j < lenResult; j++) {
43  data[j] = p_buffer->read();
44  }
45  return lenResult;
46  }
47 
48  int writeArray(const T data[], int len) {
49  LOGD("%s: %d", LOG_METHOD, len);
50  LockGuard guard(p_mutex);
51  int result = 0;
52  for (int j = 0; j < len; j++) {
53  if (p_buffer->write(data[j]) == 0) {
54  break;
55  }
56  result = j + 1;
57  }
58  return result;
59  }
60 
61  // peeks the actual entry from the buffer
62  T peek() override {
63  TRACED();
64  LockGuard guard(p_mutex);
65  return p_buffer->peek();
66  }
67 
68  // checks if the buffer is full
69  bool isFull() override { return p_buffer->isFull(); }
70 
71  bool isEmpty() { return available() == 0; }
72 
73  // write add an entry to the buffer
74  bool write(T data) override {
75  TRACED();
76  LockGuard guard(p_mutex);
77  return p_buffer->write(data);
78  }
79 
80  // clears the buffer
81  void reset() override {
82  TRACED();
83  LockGuard guard(p_mutex);
84  p_buffer->reset();
85  }
86 
87  // provides the number of entries that are available to read
88  int available() override {
89  TRACED();
90  if (is_sync_available) LockGuard guard(p_mutex);
91  return p_buffer->available();
92  }
93 
94  // provides the number of entries that are available to write
95  int availableForWrite() override {
96  TRACED();
97  if (is_sync_available) LockGuard guard(p_mutex);
98  return p_buffer->availableForWrite();
99  }
100 
101  // returns the address of the start of the physical read buffer
102  T *address() override {
103  TRACED();
104  return p_buffer->address();
105  }
106 
107  size_t size() {
108  return p_buffer->size();
109  }
110 
111 protected:
112  BaseBuffer<T> *p_buffer = nullptr;
113  MutexBase *p_mutex = nullptr;
114  bool is_sync_available = false;
115 };
116 
117 
118 } // namespace audio_tools
119 
Shared functionality of all buffers.
Definition: Buffers.h:30
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
T * address() override
returns the address of the start of the physical read buffer
Definition: SynchronizedBuffer.h:102
T peek() override
peeks the actual entry from the buffer
Definition: SynchronizedBuffer.h:62
bool write(T data) override
write add an entry to the buffer
Definition: SynchronizedBuffer.h:74
int available() override
provides the number of entries that are available to read
Definition: SynchronizedBuffer.h:88
int availableForWrite() override
provides the number of entries that are available to write
Definition: SynchronizedBuffer.h:95
bool isFull() override
checks if the buffer is full
Definition: SynchronizedBuffer.h:69
int writeArray(const T data[], int len)
Fills the buffer data.
Definition: SynchronizedBuffer.h:48
void reset() override
clears the buffer
Definition: SynchronizedBuffer.h:81
T read() override
reads a single value
Definition: SynchronizedBuffer.h:31
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: AudioConfig.h:872