Pulp 1.88.0
Cross-platform audio plugin framework
Loading...
Searching...
No Matches
mirrored_history_buffer.hpp
Go to the documentation of this file.
1#pragma once
2
5
6#include <algorithm>
7#include <cstddef>
8#include <limits>
9#include <span>
10#include <type_traits>
11#include <utility>
12#include <vector>
13
14namespace pulp::signal {
15
16namespace detail {
17
18template <typename SampleType>
20 std::is_arithmetic_v<SampleType> &&
21 std::is_same_v<SampleType, std::remove_cv_t<SampleType>> &&
22 !std::is_same_v<SampleType, bool>;
23
24} // namespace detail
25
38template <detail::MirroredHistorySample SampleType>
40public:
44
46 : storage_(std::move(other.storage_)),
47 capacity_(std::exchange(other.capacity_, 0u)),
48 write_pos_(std::exchange(other.write_pos_, 0u)) {}
49
51 if (this == &other)
52 return *this;
53 storage_ = std::move(other.storage_);
54 capacity_ = std::exchange(other.capacity_, 0u);
55 write_pos_ = std::exchange(other.write_pos_, 0u);
56 return *this;
57 }
58
63 bool prepare(std::size_t capacity) {
64 if (capacity > std::numeric_limits<std::size_t>::max() / 2u)
65 return false;
66 std::vector<SampleType> storage(capacity * 2u, SampleType{0});
67 storage_.swap(storage);
68 capacity_ = capacity;
69 write_pos_ = 0u;
70 return true;
71 }
72
73 void reset() noexcept {
74 std::fill(storage_.begin(), storage_.end(), SampleType{0});
75 write_pos_ = 0u;
76 }
77
78 void push(SampleType sample) noexcept {
79 if (capacity_ == 0u)
80 return;
81 storage_[write_pos_] = sample;
82 storage_[write_pos_ + capacity_] = sample;
83 if (++write_pos_ == capacity_)
84 write_pos_ = 0u;
85 }
86
90 std::span<const SampleType> window() const noexcept {
91 if (capacity_ == 0u)
92 return {};
93 return {storage_.data() + write_pos_, capacity_};
94 }
95
96 std::size_t capacity() const noexcept {
97 return capacity_;
98 }
99 bool empty() const noexcept {
100 return capacity_ == 0u;
101 }
102
103private:
104 std::vector<SampleType> storage_;
105 std::size_t capacity_ = 0u;
106 std::size_t write_pos_ = 0u;
107};
108
109} // namespace pulp::signal
void push(SampleType sample) noexcept
Definition mirrored_history_buffer.hpp:78
MirroredHistoryBuffer & operator=(const MirroredHistoryBuffer &)=default
MirroredHistoryBuffer & operator=(MirroredHistoryBuffer &&other) noexcept
Definition mirrored_history_buffer.hpp:50
MirroredHistoryBuffer(MirroredHistoryBuffer &&other) noexcept
Definition mirrored_history_buffer.hpp:45
void reset() noexcept
Definition mirrored_history_buffer.hpp:73
MirroredHistoryBuffer(const MirroredHistoryBuffer &)=default
std::size_t capacity() const noexcept
Definition mirrored_history_buffer.hpp:96
bool empty() const noexcept
Definition mirrored_history_buffer.hpp:99
bool prepare(std::size_t capacity)
Allocates control-thread storage transactionally.
Definition mirrored_history_buffer.hpp:63
std::span< const SampleType > window() const noexcept
The view remains memory-valid until prepare(), but consume it before the next push() or reset() becau...
Definition mirrored_history_buffer.hpp:90
Definition mirrored_history_buffer.hpp:19
Round-2 effect-wave vocabulary.
Definition analysis_frontends.hpp:50
Definition additive_bank.hpp:218
@ u
Definition formant_filter_bank.hpp:20