Class anira::RingBuffer

class RingBuffer : public anira::Buffer<float>

Inheritance diagram for anira::RingBuffer:

digraph {
    graph [bgcolor="#00000000"]
    node [shape=rectangle style=filled fillcolor="#FFFFFF" font=Helvetica padding=2]
    edge [color="#1414CE"]
    "2" [label="anira::Buffer< float >" tooltip="anira::Buffer< float >"]
    "1" [label="anira::RingBuffer" tooltip="anira::RingBuffer" fillcolor="#BFBFBF"]
    "1" -> "2" [dir=forward tooltip="public-inheritance"]
}

Collaboration diagram for anira::RingBuffer:

digraph {
    graph [bgcolor="#00000000"]
    node [shape=rectangle style=filled fillcolor="#FFFFFF" font=Helvetica padding=2]
    edge [color="#1414CE"]
    "2" [label="anira::Buffer< float >" tooltip="anira::Buffer< float >"]
    "3" [label="anira::MemoryBlock< float >" tooltip="anira::MemoryBlock< float >"]
    "1" [label="anira::RingBuffer" tooltip="anira::RingBuffer" fillcolor="#BFBFBF"]
    "2" -> "3" [dir=forward tooltip="usage"]
    "1" -> "2" [dir=forward tooltip="public-inheritance"]
}

Circular buffer implementation for real-time audio processing with multi-channel support.

The RingBuffer class extends the Buffer class to provide circular buffer functionality optimized for real-time audio applications. It maintains separate read and write positions for each channel, enabling efficient streaming audio processing with lookahead and lookbehind capabilities.

Key features:

  • Multi-channel circular buffer with independent channel positions

  • Real-time safe push/pop operations for streaming audio

  • Lookahead access to future samples in the buffer

  • Lookbehind access to previously processed samples

  • Automatic wrapping and overflow handling

  • Full/empty state tracking for each channel

  • Zero-copy access patterns for performance-critical code

Note

This class inherits from Buffer<float> and adds circular buffer semantics while maintaining real-time safety guarantees.

Public Functions

RingBuffer()

Default constructor that creates an empty ring buffer.

Creates an uninitialized ring buffer with no allocated memory. The buffer must be initialized using initialize_with_positions() before it can be used for audio processing.

void initialize_with_positions(size_t num_channels, size_t num_samples)

Initializes the ring buffer with specified dimensions and position tracking.

Allocates memory for the ring buffer and initializes read/write position vectors for each channel. All positions are set to zero and the buffer is cleared. This method must be called before using any other ring buffer operations.

Note

This method involves memory allocation and should not be called in real-time contexts

Parameters:
  • num_channels – Number of audio channels to allocate

  • num_samples – Number of samples per channel (buffer size)

void clear_with_positions()

Clears the buffer content and resets all position counters.

Sets all audio data to zero and resets read/write positions for all channels to their initial state. The buffer size and channel count remain unchanged. This operation is real-time safe.

void push_sample(size_t channel, float sample)

Pushes a single sample into the specified channel’s ring buffer.

Writes a sample to the current write position of the specified channel and advances the write position with automatic wrapping. This operation is optimized for real-time audio processing.

Note

This method is real-time safe and performs automatic buffer wrapping

Parameters:
  • channel – The channel index to write to (0-based)

  • sample – The audio sample value to write

float pop_sample(size_t channel)

Pops a single sample from the specified channel’s ring buffer.

Reads a sample from the current read position of the specified channel and advances the read position with automatic wrapping. Returns the sample value that was read.

Note

This method is real-time safe and performs automatic buffer wrapping

Parameters:

channel – The channel index to read from (0-based)

Returns:

The audio sample value that was read

float get_future_sample(size_t channel, size_t offset)

Gets a future sample from the ring buffer without advancing positions.

Reads a sample from a position ahead of the current read position without modifying any position counters. This enables lookahead processing for algorithms that need access to upcoming samples.

Note

The offset should not exceed the number of available samples in the buffer

Parameters:
  • channel – The channel index to read from (0-based)

  • offset – Number of samples ahead of the read position to access

Returns:

The audio sample value at the future position

float get_past_sample(size_t channel, size_t offset)

Gets a past sample from the ring buffer without advancing positions.

Reads a sample from a position behind the current read position without modifying any position counters. This enables lookbehind processing for algorithms that need access to previously processed samples.

Note

The offset should not exceed the number of available past samples

Parameters:
  • channel – The channel index to read from (0-based)

  • offset – Number of samples behind the read position to access

Returns:

The audio sample value at the past position

size_t get_available_samples(size_t channel)

Gets the number of samples available for reading from a channel.

Calculates the number of samples that have been written to the channel but not yet read. This is useful for determining how much data is available for processing.

Parameters:

channel – The channel index to query (0-based)

Returns:

Number of samples available for reading

size_t get_available_past_samples(size_t channel)

Gets the number of past samples available for lookbehind access.

Calculates the number of samples that have been read and are available for past sample access. This is useful for algorithms that need to access historical data.

Parameters:

channel – The channel index to query (0-based)

Returns:

Number of past samples available for lookbehind access