arduino-audio-tools
Loading...
Searching...
No Matches
src
AudioTools
Concurrency
SynchronizedBuffer.h
Go to the documentation of this file.
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
9
namespace
audio_tools
{
10
20
template
<
typename
T>
21
class
SynchronizedBuffer
:
public
BaseBuffer
<T> {
22
public
:
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
100
protected
:
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
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:64
AudioTypes.h
MIN
#define MIN(A, B)
Definition
AudioTypes.h:17
Buffers.h
LockGuard.h
Mutex.h
audio_tools::BaseBuffer
Shared functionality of all buffers.
Definition
Buffers.h:22
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:33
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:55
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:84
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:21
audio_tools::SynchronizedBuffer::is_sync_available
bool is_sync_available
Definition
SynchronizedBuffer.h:103
audio_tools::SynchronizedBuffer::size
size_t size()
Definition
SynchronizedBuffer.h:96
audio_tools::SynchronizedBuffer::p_buffer
BaseBuffer< T > * p_buffer
Definition
SynchronizedBuffer.h:101
audio_tools::SynchronizedBuffer::p_mutex
MutexBase * p_mutex
Definition
SynchronizedBuffer.h:102
audio_tools::SynchronizedBuffer::peek
bool peek(T &result) override
peeks the actual entry from the buffer
Definition
SynchronizedBuffer.h:51
audio_tools::SynchronizedBuffer::read
bool read(T &result) override
reads a single value
Definition
SynchronizedBuffer.h:30
audio_tools::SynchronizedBuffer::write
bool write(T data) override
write add an entry to the buffer
Definition
SynchronizedBuffer.h:63
audio_tools::SynchronizedBuffer::available
int available() override
provides the number of entries that are available to read
Definition
SynchronizedBuffer.h:77
audio_tools::SynchronizedBuffer::availableForWrite
int availableForWrite() override
provides the number of entries that are available to write
Definition
SynchronizedBuffer.h:84
audio_tools::SynchronizedBuffer::address
T * address() override
returns the address of the start of the physical read buffer
Definition
SynchronizedBuffer.h:91
audio_tools::SynchronizedBuffer::isFull
bool isFull() override
checks if the buffer is full
Definition
SynchronizedBuffer.h:58
audio_tools::SynchronizedBuffer::writeArray
int writeArray(const T data[], int len)
Fills the buffer data.
Definition
SynchronizedBuffer.h:44
audio_tools::SynchronizedBuffer::reset
void reset() override
clears the buffer
Definition
SynchronizedBuffer.h:70
audio_tools::SynchronizedBuffer::isEmpty
bool isEmpty()
Definition
SynchronizedBuffer.h:60
audio_tools::SynchronizedBuffer::SynchronizedBuffer
SynchronizedBuffer(BaseBuffer< T > &buffer, MutexBase &mutex, bool syncAvailable=false)
Definition
SynchronizedBuffer.h:23
audio_tools::SynchronizedBuffer::readArray
int readArray(T data[], int len)
reads multiple values
Definition
SynchronizedBuffer.h:37
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:512
Generated by
1.9.8