arduino-audio-tools
|
Optimized buffer implementation for pitch shifting with interpolation. More...
#include <PitchShift.h>
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 | |
T | getValue (int pos) |
Get buffer value with wraparound support. | |
void | handleReadWriteOverrun (T last_value) |
Handle read/write pointer collisions with phase alignment. | |
T | 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. | |
T | 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 |
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:
T | The sample data type (typically int16_t or float) |
|
inline |
Constructor.
size | Initial buffer size (0 means no allocation yet) |
increment | Reading speed multiplier for pitch shifting |
|
inlinevirtual |
returns the address of the start of the physical read buffer
Implements BaseBuffer< T >.
|
inlinevirtual |
provides the number of entries that are available to read
Implements BaseBuffer< T >.
|
inlinevirtual |
provides the number of entries that are available to write
Implements BaseBuffer< T >.
|
inlinevirtualinherited |
Removes the next len entries.
Reimplemented in SingleBuffer< T >, SingleBuffer< float >, SingleBuffer< int16_t >, SingleBuffer< stsz_sample_size_t >, SingleBuffer< uint32_t >, and SingleBuffer< uint8_t >.
|
inlineprotected |
Get buffer value with wraparound support.
pos | Position in buffer (can exceed buffer_size, will wrap around) |
|
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:
last_value | The last successfully read sample value |
|
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.
read_pos | Fractional position in buffer (e.g., 10.5 means halfway between samples 10 and 11) |
|
inlinevirtual |
checks if the buffer is full
Reimplemented from BaseBuffer< T >.
|
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.
value1 | The last read value to check |
incrementing | Whether the last read was part of an increasing trend |
v1 | First sample value |
v2 | Second sample value |
|
inlinevirtual |
peeks the actual entry from the buffer
Implements BaseBuffer< T >.
|
inlinevirtual |
reads a single value
Implements BaseBuffer< T >.
|
inlinevirtualinherited |
reads multiple values
Reimplemented in RingBufferFile< File, T >, BufferRTOS< T >, SynchronizedBuffer< T >, RedisBuffer< T >, BufferRP2040T< T >, NBufferFile< File, T >, and DynamicMultiBuffer< T, BufferType >.
|
inlinevirtual |
Reset pointer positions and clear buffer.
Implements BaseBuffer< T >.
|
inlinevirtual |
Resize buffer and set initial read position to prevent immediate overrun.
size | New buffer size in samples |
Reimplemented from BaseBuffer< T >.
|
inline |
Set the reading speed increment for pitch shifting.
increment | Speed multiplier (1.0 = normal, >1.0 = faster for higher pitch) |
|
inlinevirtual |
Implements BaseBuffer< T >.
|
inlinevirtual |
write add an entry to the buffer
Implements BaseBuffer< T >.
|
inlinevirtualinherited |
Fills the buffer data.
Reimplemented in BufferRTOS< T >, SynchronizedBuffer< T >, RedisBuffer< T >, BufferRP2040T< T >, SingleBuffer< T >, RingBufferFile< File, T >, NBufferFile< File, T >, and DynamicMultiBuffer< T, BufferType >.