arduino-audio-tools
Loading...
Searching...
No Matches
NBuffer< T > Class Template Referenceabstract

A N buffer. If count=2 we create a DoubleBuffer, if count=3 a TripleBuffer etc. More...

#include <Buffers.h>

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

Public Member Functions

 NBuffer (int size, int count)
virtual ~NBuffer ()
T * address ()
 returns the address of the start of the actual physical read buffer
int available ()
 determines the available entries for the current read buffer
int availableForWrite ()
 determines the available entries for the write buffer
virtual int bufferCountEmpty ()
 Provides the number of entries that are available to write.
virtual 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 ()
virtual float freePercent ()
 Returns the free space of the buffer in %.
bool isEmpty ()
bool isFull ()
 checks if the buffer is full
virtual float levelPercent ()
 Returns the level of the buffer in %.
virtual bool peek (uint8_t &result)=0
 peeks the actual entry from the buffer
bool peek (T &result) override
 peeks the actual entry from the buffer
virtual bool read (uint8_t &result)=0
 reads a single value
bool read (T &result) override
 reads an entry from the buffer
virtual int readArray (uint8_t data[], int len)
 reads multiple values
int readArray (T data[], int len) override
void reset ()
 resets all buffers
virtual bool resize (size_t bytes)
 Resize the buffer to the next multiple of buffer size.
virtual bool resize (size_t size, int count)
unsigned long sampleRate ()
 provides the actual sample rate
size_t size ()
 Provides the total capacity (=buffer size * buffer count).
virtual bool write (uint8_t data)=0
 write add an entry to the buffer
bool write (T data)
 write add an entry to the buffer
virtual int writeArray (const uint8_t data[], int len)
 Fills the buffer data.
virtual int writeArrayOverwrite (const uint8_t data[], int len)
 Fills the buffer data and overwrites the oldest data if the buffer is full.

Protected Member Functions

 NBuffer ()=default
 empty constructor only allowed by subclass
virtual bool addAvailableBuffer (BaseBuffer< T > *buffer)
virtual bool addFilledBuffer (BaseBuffer< T > *buffer)
void freeMemory ()
virtual BaseBuffer< T > * getNextAvailableBuffer ()
virtual BaseBuffer< T > * getNextFilledBuffer ()
void resetCurrent ()

Protected Attributes

BaseBuffer< T > * actual_read_buffer = nullptr
BaseBuffer< T > * actual_write_buffer = nullptr
QueueFromVector< BaseBuffer< T > * > available_buffers {0, nullptr}
size_t buffer_count = 0
int buffer_size = 1024
QueueFromVector< BaseBuffer< T > * > filled_buffers {0, nullptr}
unsigned long sample_count = 0
unsigned long start_time = 0

Detailed Description

template<typename T = uint8_t>
class audio_tools::NBuffer< T >

A N buffer. If count=2 we create a DoubleBuffer, if count=3 a TripleBuffer etc.

Note
This class is not thread safe: the internal queues are plain, non-atomic structures, so concurrent producer/consumer access requires external synchronization. For cross-core/cross-thread SPSC use, see SynchronizedNBufferRTOST or SynchronizedNBufferZephyr instead.
The buffer count must be > 1!
Author
Phil Schatzmann

Constructor & Destructor Documentation

◆ NBuffer() [1/2]

template<typename T = uint8_t>
NBuffer ( int size,
int count )
inline

◆ ~NBuffer()

template<typename T = uint8_t>
virtual ~NBuffer ( )
inlinevirtual

◆ NBuffer() [2/2]

template<typename T = uint8_t>
NBuffer ( )
protecteddefault

empty constructor only allowed by subclass

Member Function Documentation

◆ addAvailableBuffer()

template<typename T = uint8_t>
virtual bool addAvailableBuffer ( BaseBuffer< T > * buffer)
inlineprotectedvirtual

◆ addFilledBuffer()

template<typename T = uint8_t>
virtual bool addFilledBuffer ( BaseBuffer< T > * buffer)
inlineprotectedvirtual

◆ address()

template<typename T = uint8_t>
T * address ( )
inlinevirtual

returns the address of the start of the actual physical read buffer

Implements BaseBuffer< uint8_t >.

◆ available()

template<typename T = uint8_t>
int available ( )
inlinevirtual

determines the available entries for the current read buffer

Implements BaseBuffer< uint8_t >.

◆ availableForWrite()

template<typename T = uint8_t>
int availableForWrite ( )
inlinevirtual

determines the available entries for the write buffer

Implements BaseBuffer< uint8_t >.

◆ bufferCountEmpty()

template<typename T = uint8_t>
virtual int bufferCountEmpty ( )
inlinevirtual

Provides the number of entries that are available to write.

Reimplemented from BaseBuffer< uint8_t >.

◆ bufferCountFilled()

template<typename T = uint8_t>
virtual int bufferCountFilled ( )
inlinevirtual

Provides the number of entries that are available to read.

Reimplemented from BaseBuffer< uint8_t >.

◆ clear()

void clear ( )
inlineinherited

same as reset

◆ clearArray()

virtual int clearArray ( int len)
inlinevirtualinherited

Removes the next len entries.

Reimplemented in SingleBuffer< T >, SingleBuffer< T >, SingleBuffer< uint8_t >, and SingleBuffer< T >.

◆ flush()

template<typename T = uint8_t>
void flush ( )
inlinevirtual

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< uint8_t >.

◆ freeMemory()

template<typename T = uint8_t>
void freeMemory ( )
inlineprotected

◆ freePercent()

virtual float freePercent ( )
inlinevirtualinherited

Returns the free space of the buffer in %.

◆ getNextAvailableBuffer()

template<typename T = uint8_t>
virtual BaseBuffer< T > * getNextAvailableBuffer ( )
inlineprotectedvirtual

◆ getNextFilledBuffer()

template<typename T = uint8_t>
virtual BaseBuffer< T > * getNextFilledBuffer ( )
inlineprotectedvirtual

◆ isEmpty()

bool isEmpty ( )
inlineinherited

◆ isFull()

template<typename T = uint8_t>
bool isFull ( )
inlinevirtual

checks if the buffer is full

Reimplemented from BaseBuffer< uint8_t >.

◆ levelPercent()

virtual float levelPercent ( )
inlinevirtualinherited

Returns the level of the buffer in %.

Reimplemented in AdaptiveResamplingBuffer.

◆ peek() [1/2]

virtual bool peek ( uint8_t & result)
pure virtualinherited

◆ peek() [2/2]

template<typename T = uint8_t>
bool peek ( T & result)
inlineoverride

peeks the actual entry from the buffer

◆ read() [1/2]

virtual bool read ( uint8_t & result)
pure virtualinherited

◆ read() [2/2]

template<typename T = uint8_t>
bool read ( T & result)
inlineoverride

reads an entry from the buffer

◆ readArray() [1/2]

virtual int readArray ( uint8_t data[],
int len )
inlinevirtualinherited

◆ readArray() [2/2]

template<typename T = uint8_t>
int readArray ( T data[],
int len )
inlineoverride

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.

◆ reset()

template<typename T = uint8_t>
void reset ( )
inlinevirtual

resets all buffers

Implements BaseBuffer< uint8_t >.

◆ resetCurrent()

template<typename T = uint8_t>
void resetCurrent ( )
inlineprotected

◆ resize() [1/2]

template<typename T = uint8_t>
virtual bool resize ( size_t bytes)
inlinevirtual

Resize the buffer to the next multiple of buffer size.

Reimplemented from BaseBuffer< uint8_t >.

◆ resize() [2/2]

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

Resize the buffers by defining a new buffer size and buffer count. If the block size changes, existing blocks can't be reused, so all buffered data is discarded (with a warning). If only the count changes, existing blocks and their data are kept: buffers are just allocated or freed to reach the new count.

◆ sampleRate()

template<typename T = uint8_t>
unsigned long sampleRate ( )
inline

provides the actual sample rate

◆ size()

template<typename T = uint8_t>
size_t size ( )
inlinevirtual

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

Implements BaseBuffer< uint8_t >.

◆ write() [1/2]

virtual bool write ( uint8_t data)
pure virtualinherited

◆ write() [2/2]

template<typename T = uint8_t>
bool write ( T data)
inline

write add an entry to the buffer

◆ writeArray()

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

◆ writeArrayOverwrite()

virtual int writeArrayOverwrite ( const uint8_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

template<typename T = uint8_t>
BaseBuffer<T>* actual_read_buffer = nullptr
protected

◆ actual_write_buffer

template<typename T = uint8_t>
BaseBuffer<T>* actual_write_buffer = nullptr
protected

◆ available_buffers

template<typename T = uint8_t>
QueueFromVector<BaseBuffer<T> *> available_buffers {0, nullptr}
protected

◆ buffer_count

template<typename T = uint8_t>
size_t buffer_count = 0
protected

◆ buffer_size

template<typename T = uint8_t>
int buffer_size = 1024
protected

◆ filled_buffers

template<typename T = uint8_t>
QueueFromVector<BaseBuffer<T> *> filled_buffers {0, nullptr}
protected

◆ sample_count

template<typename T = uint8_t>
unsigned long sample_count = 0
protected

◆ start_time

template<typename T = uint8_t>
unsigned long start_time = 0
protected

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