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
VariableSpeedRingBuffer180< T > Class Template Reference

Variable speed ring buffer with 180-degree phase shifting. More...

#include <PitchShift.h>

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

Public Member Functions

 VariableSpeedRingBuffer180 (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)
 Peek operation not supported in this buffer implementation.
 
bool read (T &result)
 Read the next pitch-shifted sample.
 
virtual int readArray (T data[], int len)
 reads multiple values
 
void reset ()
 Reset pointer positions and clear buffer.
 
bool resize (int size)
 Resize the internal buffer and recalculate overlap region.
 
void setIncrement (float increment)
 Set the pitch shift factor.
 
size_t size ()
 
bool write (T sample)
 Write a sample 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

virtual T pitchRead ()
 Core pitch shifting algorithm with 180° phase offset blending.
 

Protected Attributes

Vector< T > buffer {0}
 
int buffer_size = 0
 
float cross_fade = 1.0
 
int overlap = 0
 
float pitch_shift = 0
 
float read_pos_float = 0.0
 
int write_pointer = 0
 
int write_pos = 0
 

Detailed Description

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

Variable speed ring buffer with 180-degree phase shifting.

Advanced buffer implementation that reads with 0° and 180° phase offsets and blends the results to prevent overrun artifacts. This technique reduces audio artifacts when read and write pointers collide by cross-fading between two read positions that are 180° apart (half buffer apart).

Based on the algorithm described at: https://github.com/YetAnotherElectronicsChannel/STM32_DSP_PitchShift

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

Constructor & Destructor Documentation

◆ VariableSpeedRingBuffer180()

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

Constructor.

Parameters
sizeInitial buffer size (0 means no allocation yet)
incrementPitch shift factor (1.0 = no change, >1.0 = higher pitch)

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

◆ isFull()

template<typename T >
virtual bool isFull ( )
inlinevirtual

checks if the buffer is full

Reimplemented from BaseBuffer< T >.

◆ peek()

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

Peek operation not supported in this buffer implementation.

Parameters
resultUnused parameter
Returns
Always returns false (not supported)

Implements BaseBuffer< T >.

◆ pitchRead()

template<typename T >
virtual T pitchRead ( )
inlineprotectedvirtual

Core pitch shifting algorithm with 180° phase offset blending.

This method implements the heart of the pitch shifting algorithm:

  1. Creates two read pointers: one at current position, one 180° (half-buffer) away
  2. Reads samples from both positions
  3. Detects when read pointers approach the write pointer
  4. Cross-fades between the two samples to prevent artifacts
  5. Advances the read position by the pitch_shift factor

The 180° offset ensures that when one read pointer collides with the write pointer, the other is safely away, allowing smooth transition.

Returns
Pitch-shifted sample with cross-fading applied

◆ read()

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

Read the next pitch-shifted sample.

Parameters
resultReference to store the processed sample
Returns
true if successful

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 the internal buffer and recalculate overlap region.

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 pitch shift factor.

Parameters
incrementPitch shift multiplier (1.0 = no change, >1.0 = higher pitch, <1.0 = lower pitch)

◆ size()

template<typename T >
size_t size ( )
inlinevirtual

Implements BaseBuffer< T >.

◆ write()

template<typename T >
bool write ( sample)
inlinevirtual

Write a sample to the buffer.

Parameters
sampleThe sample value to write
Returns
true if successful, false if buffer not allocated

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: