DSPark 1.6.1
Header-only audio DSP framework in pure C++20 — zero dependencies
Loading...
Searching...
No Matches
dspark::RingBuffer< T > Class Template Reference

Power-of-two circular buffer with compile-time interpolated read access. More...

#include <RingBuffer.h>

Public Member Functions

 RingBuffer () noexcept=default
 Constructs an empty ring buffer. No allocation happens until prepare() is called, honouring the framework's "allocate only in prepare()" contract. All methods are safe no-ops before prepare().
 
void prepare (int maxSamples) noexcept
 Allocates the ring buffer with the given capacity.
 
void reset () noexcept
 Clears the buffer to zero without deallocating. Safe to call from the audio thread.
 
void push (T sample) noexcept
 Pushes a single sample into the buffer.
 
void pushBlock (const T *samples, int numSamples) noexcept
 Pushes a block of samples into the buffer.
 
read (int delaySamples) const noexcept
 Reads a sample at an integer delay.
 
template<InterpMethod Method = InterpMethod::Cubic>
readInterpolated (T delaySamples) const noexcept
 Reads a sample at a fractional delay using the specified interpolation.
 
int getCapacity () const noexcept
 
const T * data () const noexcept
 
int getWritePosition () const noexcept
 

Detailed Description

template<FloatType T>
class dspark::RingBuffer< T >

Power-of-two circular buffer with compile-time interpolated read access.

Template Parameters
TSample type (float or double). Requires FloatType concept.

Definition at line 57 of file RingBuffer.h.

Constructor & Destructor Documentation

◆ RingBuffer()

template<FloatType T>
dspark::RingBuffer< T >::RingBuffer ( )
defaultnoexcept

Constructs an empty ring buffer. No allocation happens until prepare() is called, honouring the framework's "allocate only in prepare()" contract. All methods are safe no-ops before prepare().

Member Function Documentation

◆ data()

template<FloatType T>
const T * dspark::RingBuffer< T >::data ( ) const
inlinenoexcept

Definition at line 250 of file RingBuffer.h.

◆ getCapacity()

template<FloatType T>
int dspark::RingBuffer< T >::getCapacity ( ) const
inlinenoexcept

Definition at line 249 of file RingBuffer.h.

◆ getWritePosition()

template<FloatType T>
int dspark::RingBuffer< T >::getWritePosition ( ) const
inlinenoexcept

Definition at line 251 of file RingBuffer.h.

◆ prepare()

template<FloatType T>
void dspark::RingBuffer< T >::prepare ( int  maxSamples)
inlinenoexcept

Allocates the ring buffer with the given capacity.

Capacity is rounded up to the next power of two. Existing content is cleared. Memory is aligned to 32 bytes. Not thread-safe against concurrent processing calls.

Parameters
maxSamplesMaximum number of samples to store. Clamped release-safe to [1, 2^30] (non-positive requests allocate a single sample; larger ones would overflow the power-of-two round-up).

Definition at line 78 of file RingBuffer.h.

◆ push()

template<FloatType T>
void dspark::RingBuffer< T >::push ( sample)
inlinenoexcept

Pushes a single sample into the buffer.

Parameters
sampleThe sample to write.

Definition at line 129 of file RingBuffer.h.

◆ pushBlock()

template<FloatType T>
void dspark::RingBuffer< T >::pushBlock ( const T *  samples,
int  numSamples 
)
inlinenoexcept

Pushes a block of samples into the buffer.

Copies in at most two contiguous memcpy spans (pre-wrap / post-wrap) with a single write-position update, several times faster than a per-sample push loop for block-based delays.

Parameters
samplesSource buffer.
numSamplesNumber of samples to push.

Definition at line 146 of file RingBuffer.h.

◆ read()

template<FloatType T>
T dspark::RingBuffer< T >::read ( int  delaySamples) const
inlinenoexcept

Reads a sample at an integer delay.

A delay of 0 returns the most recently pushed sample.

Parameters
delaySamplesDelay in samples (0 to capacity-1).
Returns
The delayed sample.

Definition at line 179 of file RingBuffer.h.

◆ readInterpolated()

template<FloatType T>
template<InterpMethod Method = InterpMethod::Cubic>
T dspark::RingBuffer< T >::readInterpolated ( delaySamples) const
inlinenoexcept

Reads a sample at a fractional delay using the specified interpolation.

The interpolation method is selected at compile time (no per-sample dispatch in the audio thread).

Template Parameters
MethodInterpolation method to use.
Parameters
delaySamplesFractional delay in samples.
Returns
The interpolated sample.
Precondition
delaySamples must be finite and within [0, capacity - 3) (Linear) or [1, capacity - 3) (4-point methods, which read one sample earlier and two later). Violations assert in debug builds; in release the power-of-two index mask keeps every access in bounds, so out-of-range delays return wrapped (wrong-position) samples but never corrupt memory.
Note
4-point interpolators require delaySamples >= 1.0 to avoid reading future/unwritten samples. Modulating below 1.0 will cause audio artifacts.

Definition at line 210 of file RingBuffer.h.

◆ reset()

template<FloatType T>
void dspark::RingBuffer< T >::reset ( )
inlinenoexcept

Clears the buffer to zero without deallocating. Safe to call from the audio thread.

Definition at line 118 of file RingBuffer.h.


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