arduino-audio-tools
Loading...
Searching...
No Matches
src
AudioTools
Concurrency
SynchronizedBuffer.h
Go to the documentation of this file.
1
#pragma once
2
#include <algorithm>
3
#include "
AudioToolsConfig.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
bool
read
(
T
&result)
override
{
32
TRACED
();
33
LockGuard
guard
(
p_mutex
);
34
return
p_buffer
->
read
(result);
35
}
36
37
// reads multiple values
38
int
readArray
(
T
data[],
int
len) {
39
TRACED
();
40
LockGuard
guard
(
p_mutex
);
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);
47
LockGuard
guard
(
p_mutex
);
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
();
54
LockGuard
guard
(
p_mutex
);
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
();
66
LockGuard
guard
(
p_mutex
);
67
return
p_buffer
->
write
(data);
68
}
69
70
// clears the buffer
71
void
reset
()
override
{
72
TRACED
();
73
LockGuard
guard
(
p_mutex
);
74
p_buffer
->
reset
();
75
}
76
77
// provides the number of entries that are available to read
78
int
available
()
override
{
79
TRACED
();
80
if
(
is_sync_available
)
LockGuard
guard
(
p_mutex
);
81
return
p_buffer
->
available
();
82
}
83
84
// provides the number of entries that are available to write
85
int
availableForWrite
()
override
{
86
TRACED
();
87
if
(
is_sync_available
)
LockGuard
guard
(
p_mutex
);
88
return
p_buffer
->
availableForWrite
();
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
101
protected
:
102
BaseBuffer<T>
*
p_buffer
=
nullptr
;
103
MutexBase
*
p_mutex
=
nullptr
;
104
bool
is_sync_available
=
false
;
105
};
106
107
108
}
// namespace audio_tools
109
TRACED
#define TRACED()
Definition
AudioLoggerIDF.h:31
LOGD
#define LOGD(...)
Definition
AudioLoggerIDF.h:27
AudioLogger.h
AudioToolsConfig.h
LOG_METHOD
#define LOG_METHOD
Definition
AudioToolsConfig.h:74
AudioTypes.h
Buffers.h
LockGuard.h
Mutex.h
audio_tools::BaseBuffer
Shared functionality of all buffers.
Definition
Buffers.h:23
audio_tools::BaseBuffer::read
virtual bool read(T &result)=0
reads a single value
audio_tools::BaseBuffer::readArray
virtual int readArray(T data[], int len)
reads multiple values
Definition
Buffers.h:34
audio_tools::BaseBuffer::reset
virtual void reset()=0
clears the buffer
audio_tools::BaseBuffer::writeArray
virtual int writeArray(const T data[], int len)
Fills the buffer data.
Definition
Buffers.h:56
audio_tools::BaseBuffer::address
virtual T * address()=0
returns the address of the start of the physical read buffer
audio_tools::BaseBuffer::size
virtual size_t size()=0
audio_tools::BaseBuffer::availableForWrite
virtual int availableForWrite()=0
provides the number of entries that are available to write
audio_tools::BaseBuffer::isFull
virtual bool isFull()
checks if the buffer is full
Definition
Buffers.h:85
audio_tools::BaseBuffer::peek
virtual bool peek(T &result)=0
peeks the actual entry from the buffer
audio_tools::BaseBuffer::write
virtual bool write(T data)=0
write add an entry to the buffer
audio_tools::BaseBuffer::available
virtual int available()=0
provides the number of entries that are available to read
audio_tools::LockGuard
RAII implementaion using a Mutex: Only a few microcontrollers provide lock guards,...
Definition
LockGuard.h:17
audio_tools::MutexBase
Empty Mutex implementation which does nothing.
Definition
Mutex.h:18
audio_tools::SynchronizedBuffer
Wrapper class that can turn any Buffer into a thread save implementation.
Definition
SynchronizedBuffer.h:22
audio_tools::SynchronizedBuffer::is_sync_available
bool is_sync_available
Definition
SynchronizedBuffer.h:104
audio_tools::SynchronizedBuffer::size
size_t size()
Definition
SynchronizedBuffer.h:97
audio_tools::SynchronizedBuffer::p_buffer
BaseBuffer< T > * p_buffer
Definition
SynchronizedBuffer.h:102
audio_tools::SynchronizedBuffer::p_mutex
MutexBase * p_mutex
Definition
SynchronizedBuffer.h:103
audio_tools::SynchronizedBuffer::peek
bool peek(T &result) override
peeks the actual entry from the buffer
Definition
SynchronizedBuffer.h:52
audio_tools::SynchronizedBuffer::read
bool read(T &result) override
reads a single value
Definition
SynchronizedBuffer.h:31
audio_tools::SynchronizedBuffer::write
bool write(T data) override
write add an entry to the buffer
Definition
SynchronizedBuffer.h:64
audio_tools::SynchronizedBuffer::available
int available() override
provides the number of entries that are available to read
Definition
SynchronizedBuffer.h:78
audio_tools::SynchronizedBuffer::availableForWrite
int availableForWrite() override
provides the number of entries that are available to write
Definition
SynchronizedBuffer.h:85
audio_tools::SynchronizedBuffer::address
T * address() override
returns the address of the start of the physical read buffer
Definition
SynchronizedBuffer.h:92
audio_tools::SynchronizedBuffer::isFull
bool isFull() override
checks if the buffer is full
Definition
SynchronizedBuffer.h:59
audio_tools::SynchronizedBuffer::writeArray
int writeArray(const T data[], int len)
Fills the buffer data.
Definition
SynchronizedBuffer.h:45
audio_tools::SynchronizedBuffer::reset
void reset() override
clears the buffer
Definition
SynchronizedBuffer.h:71
audio_tools::SynchronizedBuffer::isEmpty
bool isEmpty()
Definition
SynchronizedBuffer.h:61
audio_tools::SynchronizedBuffer::SynchronizedBuffer
SynchronizedBuffer(BaseBuffer< T > &buffer, MutexBase &mutex, bool syncAvailable=false)
Definition
SynchronizedBuffer.h:24
audio_tools::SynchronizedBuffer::readArray
int readArray(T data[], int len)
reads multiple values
Definition
SynchronizedBuffer.h:38
audio_tools
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition
AudioCodecsBase.h:10
audio_tools::writeData
size_t writeData(Print *p_out, T *data, int samples, int maxSamples=512)
Definition
AudioTypes.h:508
Generated by
1.9.8