Buffer Operations¶
Optimized SIMD-accelerated buffer operations for real-time audio processing.
Features¶
- SIMD Optimized: SSE/AVX vectorization with scalar fallbacks
- Header-Only: No linking required
- Real-Time Safe: No allocations in processing functions
- Tested: Comprehensive correctness and performance tests
Modules¶
buffer_math.hpp¶
Mathematical operations on audio buffers:
- buffer_add() - Add buffers element-wise
- buffer_multiply() - Multiply buffers element-wise
- buffer_scale() - Scale by constant
- buffer_mix() - Mix with gain
- buffer_peak() - Find maximum absolute value
- buffer_rms() - Calculate RMS level
buffer_copy.hpp¶
Copy and format conversion:
- buffer_copy() - Optimized memory copy
- buffer_deinterleave() - LRLR... → [L...][R...]
- buffer_interleave() - [L...][R...] → LRLR...
- buffer_copy_with_gain() - Copy with gain applied
- buffer_reverse() - Reverse buffer in-place
buffer_fill.hpp¶
Fill patterns:
- buffer_clear() - Zero buffer
- buffer_fill() - Fill with constant
- buffer_ramp() - Linear ramp
- buffer_ramp_exponential() - Exponential curve
- buffer_ramp_scurve() - Smooth S-curve
Usage¶
#include "buffer_math.hpp"
#include "buffer_copy.hpp"
#include "buffer_fill.hpp"
using namespace audiolab::core::buffer;
// Process audio
float left[512], right[512];
float output[1024];
// Mix channels
buffer_mix(left, right, 0.5f, 512); // left += right * 0.5
// Interleave for output
buffer_interleave_stereo(output, left, right, 512);
// Check levels
float peak = buffer_peak(output, 1024);
float rms = buffer_rms(output, 1024);
Performance¶
All operations have SIMD-optimized paths:
| Operation | SIMD | Speedup |
|---|---|---|
| buffer_add | AVX | ~8x |
| buffer_multiply | AVX | ~8x |
| buffer_scale | AVX | ~8x |
| buffer_mix | AVX | ~6x |
| buffer_peak | AVX | ~4x |
| buffer_rms | AVX | ~6x |
| deinterleave | SSE | ~3x |
| interleave | SSE | ~3x |
Benchmarks on Intel i7 @3.6GHz, 4096 samples
Building¶
mkdir build && cd build
cmake ..
cmake --build .
# Run tests
ctest
# Run benchmarks (optimized)
./benchmark_buffer_operations
SIMD Support¶
The library automatically detects and uses available SIMD instructions:
- AVX: 8 floats per operation (most modern CPUs)
- SSE: 4 floats per operation (fallback)
- Scalar: No SIMD (universal fallback)
To force specific SIMD level:
# AVX
cmake -DCMAKE_CXX_FLAGS="-mavx" ..
# SSE only
cmake -DCMAKE_CXX_FLAGS="-msse -mno-avx" ..
# No SIMD
cmake -DCMAKE_CXX_FLAGS="-mno-sse" ..
Integration¶
Add to your CMakeLists.txt:
Design Principles¶
- Zero-cost abstraction - No performance penalty vs hand-written code
- Explicit SIMD paths - Clear code with
#ifdef __AVX__guards - Safe scalar fallbacks - Works on any platform
- Cache-friendly - Sequential memory access patterns
- Alignment-agnostic - Works with unaligned data
License¶
Part of AudioLab project.