28#include "Interpolation.h"
88 assert(maxSamples > 0);
92 constexpr int kMaxCapacity = 1 << 30;
93 if (maxSamples > kMaxCapacity) maxSamples = kMaxCapacity;
96 while (capacity_ < maxSamples)
99 mask_ = capacity_ - 1;
107 const std::uint64_t bytes64 =
108 (
static_cast<std::uint64_t
>(capacity_) *
sizeof(T) + 31u) & ~
static_cast<std::uint64_t
>(31u);
109 if (bytes64 >
static_cast<std::uint64_t
>(std::numeric_limits<size_t>::max()))
112 capacity_ = 0; mask_ = 0; writePos_ = 0;
115 const size_t bytes =
static_cast<size_t>(bytes64);
118 buffer_.reset(
static_cast<T*
>(
122 _aligned_malloc(bytes, 32)
124 std::aligned_alloc(32, bytes)
129 if (!buffer_) { capacity_ = 0; mask_ = 0; writePos_ = 0;
return; }
141 std::fill_n(buffer_.get(), capacity_, T(0));
149 inline void push(T sample)
noexcept
151 if (capacity_ == 0) [[unlikely]]
return;
152 buffer_[writePos_] = sample;
153 writePos_ = (writePos_ + 1) & mask_;
166 void pushBlock(
const T* samples,
int numSamples)
noexcept
168 if (capacity_ == 0 || numSamples <= 0) [[unlikely]]
return;
170 if (numSamples >= capacity_)
173 samples += numSamples - capacity_;
174 std::memcpy(buffer_.get(), samples,
static_cast<size_t>(capacity_) *
sizeof(T));
179 const int firstSpan = std::min(numSamples, capacity_ - writePos_);
180 std::memcpy(buffer_.get() + writePos_, samples,
181 static_cast<size_t>(firstSpan) *
sizeof(T));
183 const int remaining = numSamples - firstSpan;
185 std::memcpy(buffer_.get(), samples + firstSpan,
186 static_cast<size_t>(remaining) *
sizeof(T));
188 writePos_ = (writePos_ + numSamples) & mask_;
199 [[nodiscard]]
inline T
read(
int delaySamples)
const noexcept
201 if (capacity_ == 0) [[unlikely]]
return T(0);
202 assert(delaySamples >= 0 && delaySamples < capacity_
203 &&
"RingBuffer::read: delay out of range");
205 int idx = (writePos_ - 1 - delaySamples) & mask_;
229 template <InterpMethod Method = InterpMethod::Cubic>
232 if (capacity_ == 0) [[unlikely]]
return T(0);
234 int intDelay =
static_cast<int>(delaySamples);
235 T frac = delaySamples -
static_cast<T
>(intDelay);
237 assert(delaySamples >= T(0) && intDelay + 2 < capacity_);
241 T s0 =
read(intDelay);
242 T s1 =
read(intDelay + 1);
243 return s0 + frac * (s1 - s0);
247 assert(delaySamples >= T(1) &&
"4-point interpolators require delay >= 1.0");
251 s[0] =
read(intDelay - 1);
252 s[1] =
read(intDelay);
253 s[2] =
read(intDelay + 1);
254 s[3] =
read(intDelay + 2);
269 [[nodiscard]]
int getCapacity() const noexcept {
return capacity_; }
270 [[nodiscard]]
const T*
data() const noexcept {
return buffer_.get(); }
274 struct AlignedDeleter {
275 void operator()(T* ptr)
const {
284 std::unique_ptr<T[], AlignedDeleter> buffer_;
Power-of-two circular buffer with compile-time interpolated read access.
T read(int delaySamples) const noexcept
Reads a sample at an integer delay.
void prepare(int maxSamples) noexcept
Allocates the ring buffer with the given capacity.
const T * data() const noexcept
void pushBlock(const T *samples, int numSamples) noexcept
Pushes a block of samples into the buffer.
void push(T sample) noexcept
Pushes a single sample into the buffer.
int getCapacity() const noexcept
RingBuffer() noexcept=default
Constructs an empty ring buffer. No allocation happens until prepare() is called, honouring the frame...
void reset() noexcept
Clears the buffer to zero without deallocating. Safe to call from the audio thread.
T readInterpolated(T delaySamples) const noexcept
Reads a sample at a fractional delay using the specified interpolation.
int getWritePosition() const noexcept
Main namespace for the DSPark framework.
T interpolateHermite(T y0, T y1, T y2, T y3, T frac) noexcept
4-point, 3rd-order Hermite interpolation (optimized x-form).
T interpolateLagrange(T y0, T y1, T y2, T y3, T frac) noexcept
4-point Lagrange interpolation from discrete samples.
InterpMethod
Interpolation method for fractional-sample reads.
@ Lagrange
4-point Lagrange (highest accuracy, static delay)
@ Linear
2-point linear (fast, lower quality)
@ Hermite
4-point Hermite (smooth transients, best for modulated delay)
@ Cubic
4-point Catmull-Rom (good balance)