arduino-audio-tools
Loading...
Searching...
No Matches
Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
SynchronizedNBufferRTOST< T > Class Template Reference

NBuffer which uses FreeRTOS queues to manage the available and filled buffers, making it safe for cross-core SPSC (single producer, single consumer) use on dual-core ESP32. More...

#include <SynchronizedNBufferRTOS.h>

Inheritance diagram for SynchronizedNBufferRTOST< T >:
NBuffer< T > BaseBuffer< T >

Public Member Functions

 SynchronizedNBufferRTOST (int bufferSize, int bufferCount, TickType_t writeMaxWait=1000, TickType_t readMaxWait=1000)
 
 ~SynchronizedNBufferRTOST ()
 
Taddress ()
 returns the address of the start of the phsical read buffer
 
int available ()
 determines the available entries for the current read buffer
 
int availableForWrite ()
 determines the available entries for the write buffer
 
int bufferCountEmpty ()
 Provides the number of entries that are available to write.
 
int bufferCountFilled ()
 Provides the number of entries that are available to read.
 
void clear ()
 same as reset
 
virtual int clearArray (int len)
 Removes the next len entries.
 
void flush ()
 
bool isEmpty ()
 
bool isFull ()
 checks if the buffer is full
 
virtual float levelPercent ()
 Returns the level of the buffer in %.
 
bool peek (T &result) override
 peeks the actual entry from the buffer
 
bool read (T &result) override
 reads an entry from the buffer
 
int readArray (T data[], int len) override
 
void reset ()
 resets all buffers
 
bool resize (size_t bufferSize, int bufferCount)
 Resize the buffers by defining a new buffer size and buffer count.
 
virtual bool resize (size_t bytes)
 Resizes the buffer if supported: returns false if not supported.
 
unsigned long sampleRate ()
 provides the actual sample rate
 
void setReadMaxWait (TickType_t ticks)
 Set how long the reader waits for a filled block (0 = non-blocking)
 
void setWriteMaxWait (TickType_t ticks)
 Set how long the writer waits for a free block (0 = non-blocking)
 
size_t size ()
 Provides the total capacity (=buffer size * buffer count)
 
bool write (T data)
 write add an entry to the buffer
 
virtual int writeArray (const T data[], int len)
 Fills the buffer data.
 
virtual int writeArrayOverwrite (const T data[], int len)
 Fills the buffer data and overwrites the oldest data if the buffer is full.
 

Protected Member Functions

bool addAvailableBuffer (BaseBuffer< T > *buffer) override
 
bool addFilledBuffer (BaseBuffer< T > *buffer) override
 
void applyWaitTimes ()
 
void cleanup ()
 
void freeMemory ()
 
BaseBuffer< T > * getNextAvailableBuffer () override
 
BaseBuffer< T > * getNextFilledBuffer () override
 
void resetCurrent ()
 

Protected Attributes

BaseBuffer< T > * actual_read_buffer
 
BaseBuffer< T > * actual_write_buffer
 
QueueFromVector< BaseBuffer< T > * > available_buffers
 
QueueRTOS< BaseBuffer< T > * > available_queue_ {0, 1000 , 0}
 
uint16_t buffer_count
 
int buffer_size
 
QueueFromVector< BaseBuffer< T > * > filled_buffers
 
QueueRTOS< BaseBuffer< T > * > filled_queue_ {0, 1000 , 0}
 
TickType_t read_max_wait_ = 1000
 
unsigned long sample_count
 
unsigned long start_time
 
TickType_t write_max_wait_ = 1000
 

Detailed Description

template<typename T>
class audio_tools::SynchronizedNBufferRTOST< T >

NBuffer which uses FreeRTOS queues to manage the available and filled buffers, making it safe for cross-core SPSC (single producer, single consumer) use on dual-core ESP32.

The writer (e.g. sketch task, core 1) calls write()/writeArray()/availableForWrite(). The reader (e.g. USB task, core 0) calls read()/readArray()/available()/address(). Block ownership is transferred via FreeRTOS queues which provide the necessary memory barriers for cross-core visibility.

Note
Supported by all FreeRTOS platforms
Template Parameters
T

Constructor & Destructor Documentation

◆ SynchronizedNBufferRTOST()

template<typename T >
SynchronizedNBufferRTOST ( int  bufferSize,
int  bufferCount,
TickType_t  writeMaxWait = 1000,
TickType_t  readMaxWait = 1000 
)
inline

◆ ~SynchronizedNBufferRTOST()

template<typename T >
~SynchronizedNBufferRTOST ( )
inline

Member Function Documentation

◆ addAvailableBuffer()

template<typename T >
bool addAvailableBuffer ( BaseBuffer< T > *  buffer)
inlineoverrideprotectedvirtual

Reimplemented from NBuffer< T >.

◆ addFilledBuffer()

template<typename T >
bool addFilledBuffer ( BaseBuffer< T > *  buffer)
inlineoverrideprotectedvirtual

Reimplemented from NBuffer< T >.

◆ address()

T * address ( )
inlinevirtualinherited

returns the address of the start of the phsical read buffer

Implements BaseBuffer< T >.

◆ applyWaitTimes()

template<typename T >
void applyWaitTimes ( )
inlineprotected

Apply wait times with correct semantics: Writer needs: available_queue_ READ wait + filled_queue_ WRITE wait Reader needs: filled_queue_ READ wait + available_queue_ WRITE wait

◆ available()

int available ( )
inlinevirtualinherited

determines the available entries for the current read buffer

Implements BaseBuffer< T >.

◆ availableForWrite()

int availableForWrite ( )
inlinevirtualinherited

determines the available entries for the write buffer

Implements BaseBuffer< T >.

◆ bufferCountEmpty()

template<typename T >
int bufferCountEmpty ( )
inlinevirtual

Provides the number of entries that are available to write.

Reimplemented from NBuffer< T >.

◆ bufferCountFilled()

template<typename T >
int bufferCountFilled ( )
inlinevirtual

Provides the number of entries that are available to read.

Reimplemented from NBuffer< T >.

◆ cleanup()

template<typename T >
void cleanup ( )
inlineprotected

◆ clear()

void clear ( )
inlineinherited

same as reset

◆ clearArray()

virtual int clearArray ( int  len)
inlinevirtualinherited

Removes the next len entries.

Reimplemented in SingleBuffer< T >.

◆ flush()

void flush ( )
inlinevirtualinherited

Submit the partially-filled write buffer to the filled queue so the reader can access it immediately. Call after writeArray() when the writer won't add more data to the current block for a while.

Reimplemented from BaseBuffer< T >.

◆ freeMemory()

void freeMemory ( )
inlineprotectedinherited

◆ getNextAvailableBuffer()

template<typename T >
BaseBuffer< T > * getNextAvailableBuffer ( )
inlineoverrideprotectedvirtual

Reimplemented from NBuffer< T >.

◆ getNextFilledBuffer()

template<typename T >
BaseBuffer< T > * getNextFilledBuffer ( )
inlineoverrideprotectedvirtual

Reimplemented from NBuffer< T >.

◆ isEmpty()

bool isEmpty ( )
inlineinherited

◆ isFull()

bool isFull ( )
inlinevirtualinherited

checks if the buffer is full

Reimplemented from BaseBuffer< T >.

◆ levelPercent()

virtual float levelPercent ( )
inlinevirtualinherited

Returns the level of the buffer in %.

◆ peek()

bool peek ( T &  result)
inlineoverridevirtualinherited

peeks the actual entry from the buffer

Implements BaseBuffer< T >.

◆ read()

bool read ( T &  result)
inlineoverridevirtualinherited

reads an entry from the buffer

Implements BaseBuffer< T >.

◆ readArray()

int readArray ( data[],
int  len 
)
inlineoverridevirtualinherited

Reads up to len entries, spanning across multiple blocks. BaseBuffer::readArray stops at the first block boundary because it calls available() once. This override keeps reading across blocks.

Reimplemented from BaseBuffer< T >.

◆ reset()

void reset ( )
inlinevirtualinherited

resets all buffers

Implements BaseBuffer< T >.

◆ resetCurrent()

void resetCurrent ( )
inlineprotectedinherited

◆ resize() [1/2]

template<typename T >
bool resize ( size_t  size,
int  count 
)
inlinevirtual

Resize the buffers by defining a new buffer size and buffer count.

Reimplemented from NBuffer< T >.

◆ resize() [2/2]

virtual bool resize ( size_t  bytes)
inlinevirtualinherited

Resizes the buffer if supported: returns false if not supported.

Reimplemented from BaseBuffer< T >.

◆ sampleRate()

unsigned long sampleRate ( )
inlineinherited

provides the actual sample rate

◆ setReadMaxWait()

template<typename T >
void setReadMaxWait ( TickType_t  ticks)
inline

Set how long the reader waits for a filled block (0 = non-blocking)

◆ setWriteMaxWait()

template<typename T >
void setWriteMaxWait ( TickType_t  ticks)
inline

Set how long the writer waits for a free block (0 = non-blocking)

◆ size()

template<typename T >
size_t size ( )
inlinevirtual

Provides the total capacity (=buffer size * buffer count)

Reimplemented from NBuffer< T >.

◆ write()

bool write ( data)
inlinevirtualinherited

write add an entry to the buffer

Implements BaseBuffer< T >.

◆ writeArray()

virtual int writeArray ( const T  data[],
int  len 
)
inlinevirtualinherited

◆ writeArrayOverwrite()

virtual int writeArrayOverwrite ( const T  data[],
int  len 
)
inlinevirtualinherited

Fills the buffer data and overwrites the oldest data if the buffer is full.

Member Data Documentation

◆ actual_read_buffer

BaseBuffer<T>* actual_read_buffer
protectedinherited

◆ actual_write_buffer

BaseBuffer<T>* actual_write_buffer
protectedinherited

◆ available_buffers

QueueFromVector<BaseBuffer<T> *> available_buffers
protectedinherited

◆ available_queue_

template<typename T >
QueueRTOS<BaseBuffer<T>*> available_queue_ {0, 1000 , 0}
protected

◆ buffer_count

uint16_t buffer_count
protectedinherited

◆ buffer_size

int buffer_size
protectedinherited

◆ filled_buffers

QueueFromVector<BaseBuffer<T> *> filled_buffers
protectedinherited

◆ filled_queue_

template<typename T >
QueueRTOS<BaseBuffer<T>*> filled_queue_ {0, 1000 , 0}
protected

◆ read_max_wait_

template<typename T >
TickType_t read_max_wait_ = 1000
protected

◆ sample_count

unsigned long sample_count
protectedinherited

◆ start_time

unsigned long start_time
protectedinherited

◆ write_max_wait_

template<typename T >
TickType_t write_max_wait_ = 1000
protected

The documentation for this class was generated from the following file: