Class anira::Buffer¶
-
template<typename T>
class Buffer¶ Template class for managing multi-channel audio buffers with efficient memory layout.
The Buffer class provides a high-performance, multi-channel audio buffer implementation optimized for real-time audio processing. It manages memory in a contiguous block with channel pointers for efficient access patterns, making it suitable for low-latency audio applications and neural network inference preprocessing.
Key features:
Contiguous memory layout for cache-friendly access patterns
Channel-based indexing with both read and write pointer access
Move semantics for efficient buffer transfers
Memory swapping capabilities for zero-copy operations
Template-based design supporting various numeric types
Real-time safe operations (no allocations in critical paths)
Memory layout: [Channel0_Sample0, Channel0_Sample1, …, Channel1_Sample0, Channel1_Sample1, …]
See also
Note
This class is designed for real-time audio processing and avoids memory allocations in performance-critical operations where possible.
- Template Parameters:
T – The data type for audio samples (typically float or double)
Public Functions
-
Buffer() = default¶
Default constructor creates an empty buffer with no allocated memory.
Creates a buffer with zero channels and zero size. Memory allocation is deferred until the buffer is resized or initialized with specific dimensions.
-
inline Buffer(size_t num_channels, size_t size)¶
Constructor that creates a buffer with specified dimensions.
Allocates memory for the specified number of channels and samples, initializes channel pointers, and clears all data to zero.
- Parameters:
num_channels – Number of audio channels to allocate
size – Number of samples per channel
-
inline Buffer(const Buffer &other)¶
Copy constructor that creates a deep copy of another buffer.
Creates a new buffer with the same dimensions as the source buffer and copies all audio data. The new buffer has its own independent memory allocation.
- Parameters:
other – The source buffer to copy from
-
inline Buffer(Buffer &&other) noexcept¶
Move constructor that transfers ownership from another buffer.
Efficiently transfers all resources from the source buffer to this buffer, leaving the source buffer in a valid but empty state. This operation is optimized for performance and does not involve data copying.
Note
Marked noexcept to guarantee no exceptions are thrown, which is essential for move semantics and container operations
- Parameters:
other – The source buffer to move from (will be left empty)
-
inline ~Buffer()¶
Destructor that cleans up allocated channel pointer memory.
Automatically frees the memory allocated for channel pointers. The underlying audio data memory is managed by the MemoryBlock and cleaned up automatically.
-
inline Buffer &operator=(const Buffer &other)¶
Copy assignment operator that replaces this buffer’s content with another buffer’s data.
Performs a deep copy of the source buffer’s data and dimensions. Any existing data in this buffer is replaced. Self-assignment is safely handled.
- Parameters:
other – The source buffer to copy from
- Returns:
Reference to this buffer after the copy operation
-
inline Buffer &operator=(Buffer &&other) noexcept¶
Move assignment operator that transfers ownership from another buffer.
Efficiently transfers all resources from the source buffer to this buffer, replacing any existing content. The source buffer is left in a valid but empty state.
Note
Marked noexcept to guarantee no exceptions, essential for move semantics
- Parameters:
other – The source buffer to move from (will be left empty)
- Returns:
Reference to this buffer after the move operation
-
inline void resize(size_t num_channels, size_t size)¶
Resizes the buffer to new dimensions.
Changes the buffer’s channel count and sample count, reallocating memory as needed. All existing data is lost during the resize operation. The buffer is not automatically cleared after resizing.
Note
This operation involves memory allocation and should not be called in real-time contexts
- Parameters:
num_channels – New number of audio channels
size – New number of samples per channel
-
inline size_t get_num_channels() const¶
Gets the number of channels in the buffer.
- Returns:
The number of audio channels currently allocated in the buffer
-
inline size_t get_num_samples() const¶
Gets the number of samples per channel in the buffer.
- Returns:
The number of samples per channel currently allocated in the buffer
-
inline const T *get_read_pointer(size_t channel) const¶
Gets a read-only pointer to the start of a specific channel’s data.
Returns a const pointer to the beginning of the specified channel’s sample data. This pointer can be used for read-only access to the channel’s samples.
- Parameters:
channel – The channel index (0-based)
- Returns:
Const pointer to the first sample of the specified channel
-
inline const T *get_read_pointer(size_t channel, size_t sample_index) const¶
Gets a read-only pointer to a specific sample within a channel.
Returns a const pointer to a specific sample position within the specified channel. This is useful for accessing data starting from a particular sample offset.
- Parameters:
channel – The channel index (0-based)
sample_index – The sample index within the channel (0-based)
- Returns:
Const pointer to the specified sample position
-
inline T *get_write_pointer(size_t channel)¶
Gets a writable pointer to the start of a specific channel’s data.
Returns a mutable pointer to the beginning of the specified channel’s sample data. This pointer can be used for both reading and writing to the channel’s samples.
- Parameters:
channel – The channel index (0-based)
- Returns:
Mutable pointer to the first sample of the specified channel
-
inline T *get_write_pointer(size_t channel, size_t sample_index)¶
Gets a writable pointer to a specific sample within a channel.
Returns a mutable pointer to a specific sample position within the specified channel. This is useful for writing data starting from a particular sample offset.
- Parameters:
channel – The channel index (0-based)
sample_index – The sample index within the channel (0-based)
- Returns:
Mutable pointer to the specified sample position
-
inline const T *const *get_array_of_read_pointers() const¶
Gets an array of read-only pointers for all channels.
Returns an array of const pointers, where each pointer points to the start of a channel’s data. This is useful for interfacing with audio processing functions that expect a channel pointer array format.
- Returns:
Array of const pointers to all channel data
-
inline T *const *get_array_of_write_pointers()¶
Gets an array of writable pointers for all channels.
Returns an array of mutable pointers, where each pointer points to the start of a channel’s data. This is useful for interfacing with audio processing functions that need to modify channel data.
- Returns:
Array of mutable pointers to all channel data
-
inline T *data()¶
Gets a pointer to the raw contiguous data block.
Returns a pointer to the underlying contiguous memory block containing all audio data. Data is organized as: [Channel0_Samples…, Channel1_Samples…, etc.]
- Returns:
Pointer to the raw data block
-
inline MemoryBlock<T> &get_memory_block()¶
Gets a reference to the underlying memory block.
Provides direct access to the MemoryBlock object that manages the buffer’s contiguous memory allocation. Useful for advanced memory management operations.
- Returns:
Reference to the internal MemoryBlock
-
inline void swap_data(Buffer &other)¶
Swaps data with another buffer without copying.
Efficiently exchanges the data content between this buffer and another buffer of the same dimensions. This is a zero-copy operation that only swaps pointers. Both buffers must have identical channel count and sample count.
Note
Buffers must have identical dimensions for the swap to succeed
- Parameters:
other – The buffer to swap data with
-
inline void swap_data(MemoryBlock<T> &other)¶
Swaps data with a MemoryBlock without copying.
Efficiently exchanges the data content between this buffer and a MemoryBlock. The MemoryBlock must have the same total size as this buffer’s allocated memory. Channel pointers are automatically updated after the swap.
Note
The MemoryBlock size must match this buffer’s total allocated size
- Parameters:
other – The MemoryBlock to swap data with
-
inline void swap_data(T *&data, size_t size)¶
Swaps data with a raw memory pointer without copying.
Efficiently exchanges the data content between this buffer and raw memory. The provided memory must have the same total size as this buffer’s allocated memory. Channel pointers are automatically updated after the swap.
Note
The memory size must match this buffer’s total allocated size
- Parameters:
data – Pointer to the raw memory to swap with
size – Size of the raw memory in number of elements
-
inline void reset_channel_ptr()¶
Resets channel pointers to point to the correct memory locations.
Updates all channel pointers to point to their correct positions within the contiguous data block. This method is called automatically after operations that might change the underlying memory layout.
-
inline T get_sample(size_t channel, size_t sample_index) const¶
Gets a single sample value from the buffer.
Retrieves the sample value at the specified channel and sample position. This method provides bounds-safe access to individual samples.
- Parameters:
channel – The channel index (0-based)
sample_index – The sample index within the channel (0-based)
- Returns:
The sample value at the specified position
-
inline void set_sample(size_t channel, size_t sample_index, T value)¶
Sets a single sample value in the buffer.
Writes a sample value to the specified channel and sample position. This method provides bounds-safe access for modifying individual samples.
- Parameters:
channel – The channel index (0-based)
sample_index – The sample index within the channel (0-based)
value – The sample value to write
-
inline void clear()¶
Clears the buffer by setting all samples to zero.
Efficiently zeros out all audio data in the buffer across all channels. This operation is optimized for performance and is safe to call in real-time contexts.