arduino-audio-tools
All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends Macros Modules Pages
Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
VariableSpeedRingBuffer< T > Class Template Reference

Optimized buffer implementation for pitch shifting with interpolation. More...

#include <PitchShift.h>

Inheritance diagram for VariableSpeedRingBuffer< T >:
BaseBuffer< T >

Public Member Functions

 VariableSpeedRingBuffer (int size=0, float increment=1.0)
 Constructor.
 
virtual T * address ()
 returns the address of the start of the physical read buffer
 
virtual int available ()
 provides the number of entries that are available to read
 
virtual int availableForWrite ()
 provides the number of entries that are available to write
 
void clear ()
 same as reset
 
virtual int clearArray (int len)
 Removes the next len entries.
 
bool isEmpty ()
 
virtual bool isFull ()
 checks if the buffer is full
 
virtual float levelPercent ()
 Returns the level of the buffer in %.
 
bool peek (T &result)
 peeks the actual entry from the buffer
 
bool read (T &result)
 reads a single value
 
virtual int readArray (T data[], int len)
 reads multiple values
 
void reset ()
 Reset pointer positions and clear buffer.
 
bool resize (int size)
 Resize buffer and set initial read position to prevent immediate overrun.
 
void setIncrement (float increment)
 Set the reading speed increment for pitch shifting.
 
size_t size ()
 
bool write (T sample)
 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

getValue (int pos)
 Get buffer value with wraparound support.
 
void handleReadWriteOverrun (T last_value)
 Handle read/write pointer collisions with phase alignment.
 
interpolate (float read_pos)
 Calculate exact sample value for fractional buffer position using linear interpolation.
 
bool isMatching (T value1, bool incrementing, T v1, T v2)
 Check if a value fits between two samples considering trend direction.
 

Protected Attributes

Vector< T > buffer {0}
 
int buffer_size
 
bool incrementing
 Track if last read trend was increasing or decreasing.
 
last_value = 0
 Record last read value for phase alignment.
 
float read_increment = 0.0f
 
float read_pos_float = 0.0f
 
int write_pos = 0
 

Detailed Description

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

Optimized buffer implementation for pitch shifting with interpolation.

This is the most advanced buffer implementation that uses interpolation to calculate exact sample values for fractional read positions and attempts to restore phase continuity when read and write pointers overtake each other. This provides the highest quality pitch shifting with minimal artifacts.

Key features:

Template Parameters
TThe sample data type (typically int16_t or float)

Constructor & Destructor Documentation

◆ VariableSpeedRingBuffer()

template<typename T >
VariableSpeedRingBuffer ( int  size = 0,
float  increment = 1.0 
)
inline

Constructor.

Parameters
sizeInitial buffer size (0 means no allocation yet)
incrementReading speed multiplier for pitch shifting

Member Function Documentation

◆ address()

template<typename T >
virtual T * address ( )
inlinevirtual

returns the address of the start of the physical read buffer

Implements BaseBuffer< T >.

◆ available()

template<typename T >
virtual int available ( )
inlinevirtual

provides the number of entries that are available to read

Implements BaseBuffer< T >.

◆ availableForWrite()

template<typename T >
virtual int availableForWrite ( )
inlinevirtual

provides the number of entries that are available to write

Implements BaseBuffer< T >.

◆ clearArray()

template<typename T >
virtual int clearArray ( int  len)
inlinevirtualinherited

◆ getValue()

template<typename T >
T getValue ( int  pos)
inlineprotected

Get buffer value with wraparound support.

Parameters
posPosition in buffer (can exceed buffer_size, will wrap around)
Returns
Sample value at the wrapped position

◆ handleReadWriteOverrun()

template<typename T >
void handleReadWriteOverrun ( last_value)
inlineprotected

Handle read/write pointer collisions with phase alignment.

When read and write pointers collide, this method attempts to maintain audio continuity by finding the best matching sample in the buffer that corresponds to the last read value's phase and trend. This minimizes audible clicks and pops during pointer overruns.

The algorithm:

  1. Detects when pointers are about to collide
  2. Searches for a matching sample pair that fits the last read value
  3. Interpolates the new read position within that pair
  4. Repositions the read pointer to maintain phase continuity
Parameters
last_valueThe last successfully read sample value

◆ interpolate()

template<typename T >
T interpolate ( float  read_pos)
inlineprotected

Calculate exact sample value for fractional buffer position using linear interpolation.

This method performs linear interpolation between two adjacent samples to provide smooth audio output when reading at fractional positions. It also tracks the direction of change (increasing/decreasing) for phase alignment purposes.

Parameters
read_posFractional position in buffer (e.g., 10.5 means halfway between samples 10 and 11)
Returns
Interpolated sample value

◆ isFull()

template<typename T >
virtual bool isFull ( )
inlinevirtual

checks if the buffer is full

Reimplemented from BaseBuffer< T >.

◆ isMatching()

template<typename T >
bool isMatching ( value1,
bool  incrementing,
v1,
v2 
)
inlineprotected

Check if a value fits between two samples considering trend direction.

This method determines if the last read value can logically fit between two consecutive buffer samples, taking into account whether the audio signal was increasing or decreasing at that point.

Parameters
value1The last read value to check
incrementingWhether the last read was part of an increasing trend
v1First sample value
v2Second sample value
Returns
true if value1 fits logically between v1 and v2

◆ peek()

template<typename T >
bool peek ( T &  result)
inlinevirtual

peeks the actual entry from the buffer

Implements BaseBuffer< T >.

◆ read()

template<typename T >
bool read ( T &  result)
inlinevirtual

reads a single value

Implements BaseBuffer< T >.

◆ readArray()

template<typename T >
virtual int readArray ( data[],
int  len 
)
inlinevirtualinherited

◆ reset()

template<typename T >
void reset ( )
inlinevirtual

Reset pointer positions and clear buffer.

Implements BaseBuffer< T >.

◆ resize()

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

Resize buffer and set initial read position to prevent immediate overrun.

Parameters
sizeNew buffer size in samples
Returns
true if successful

Reimplemented from BaseBuffer< T >.

◆ setIncrement()

template<typename T >
void setIncrement ( float  increment)
inline

Set the reading speed increment for pitch shifting.

Parameters
incrementSpeed multiplier (1.0 = normal, >1.0 = faster for higher pitch)

◆ size()

template<typename T >
size_t size ( )
inlinevirtual

Implements BaseBuffer< T >.

◆ write()

template<typename T >
bool write ( data)
inlinevirtual

write add an entry to the buffer

Implements BaseBuffer< T >.

◆ writeArray()

template<typename T >
virtual int writeArray ( const T  data[],
int  len 
)
inlinevirtualinherited

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