4#include <dsplib/span.h>
5#include <dsplib/math.h>
37 static_assert(!std::is_same_v<T, bool>,
"boolean buffer is not supported");
38 static_assert(std::is_trivially_copyable<T>(),
"type must be trivially copyable");
55 buf_.insert(buf_.end(), data.begin(), data.end());
62 void write(
const T& value)
noexcept {
63 buf_.push_back(value);
72 const int n = min(out.size(),
int(buf_.size()));
77 std::memcpy(out.data(), buf_.data(), n *
sizeof(T));
78 if (n == buf_.size()) {
81 std::memmove(buf_.data(), buf_.data() + n, (buf_.size() - n) *
sizeof(T));
82 buf_.resize(buf_.size() - n);
92 std::vector<T>
read(
int n)
noexcept {
93 n = min(n, buf_.size());
94 std::vector<T> out(n);
103 [[nodiscard]]
int size() const noexcept {
111 [[nodiscard]]
bool empty() const noexcept {
129 return make_span(buf_);
152 static_assert(!std::is_same_v<T, bool>,
"boolean buffer is not supported");
153 static_assert(std::is_trivially_copyable<T>(),
"type must be trivially copyable");
162 : chunk_size_{chunk_size} {
163 DSPLIB_ASSERT(chunk_size > 1,
"chunk size must be greater than 1");
164 buf_.reserve(chunk_size * 2);
186 DSPLIB_ASSERT(!in_write_,
"recursive write call detected");
192 if (buf_.empty() && (data.size() % chunk_size_ == 0)) {
193 const auto* pdata = data.data();
194 int psize = data.size();
195 while (psize >= chunk_size_) {
196 fn(make_span(pdata, chunk_size_));
197 pdata += chunk_size_;
198 psize -= chunk_size_;
201 buf_.insert(buf_.end(), data.begin(), data.end());
202 const auto* pdata = buf_.data();
203 int psize = buf_.size();
204 while (psize >= chunk_size_) {
205 fn(make_span(pdata, chunk_size_));
206 pdata += chunk_size_;
207 psize -= chunk_size_;
209 std::memmove(buf_.data(), buf_.data() + buf_.size() - psize, psize *
sizeof(T));
224 DSPLIB_ASSERT(!in_write_,
"flush during callback is unsafe");
240 DSPLIB_ASSERT(!in_write_,
"reset during callback is unsafe");
257 bool in_write_{
false};
Accumulates data samples and processes them in fixed-size chunks.
Definition buffer.h:147
void reset()
Clear buffer contents Resets buffer to empty state. Preserves allocated capacity.
Definition buffer.h:239
std::function< void(span_t< T >)> Callback
Callback type for processing full chunks.
Definition buffer.h:150
void flush(Callback fn)
Process remaining data.
Definition buffer.h:223
int size() const noexcept
Get current number of accumulated samples.
Definition buffer.h:250
ChunkBuffer(int chunk_size)
Construct a chunk buffer with specified chunk size.
Definition buffer.h:161
void write(span_t< T > data, Callback fn)
Add data to buffer and process complete chunks.
Definition buffer.h:181
A thread-unsafe FIFO buffer for trivially copyable data types.
Definition buffer.h:35
bool empty() const noexcept
Check if buffer empty.
Definition buffer.h:111
void write(span_t< T > data) noexcept
Write data to buffer.
Definition buffer.h:51
FIFOBuffer()=default
Default constructor Creates an empty buffer with default capacity.
span_t< T > view() const noexcept
Get read-only view of buffer.
Definition buffer.h:128
int size() const noexcept
Get current element count.
Definition buffer.h:103
void reset() noexcept
Clear buffer contents Removes all elements. Preserves allocated capacity.
Definition buffer.h:119
void write(const T &value) noexcept
Write single element (copy)
Definition buffer.h:62
std::vector< T > read(int n) noexcept
Read data to new vector.
Definition buffer.h:92
int read(mut_span_t< T > out) noexcept
Read data from buffer.
Definition buffer.h:71