Pulp 1.88.0
Cross-platform audio plugin framework
Loading...
Searching...
No Matches
minblep.hpp
Go to the documentation of this file.
1#pragma once
2
5
6#include "detail/minblep_table.hpp"
7
8#include <array>
9#include <cmath>
10#include <cstddef>
11
12namespace pulp::signal::osc {
13
21
43template <std::size_t MaximumEvents = 8> class MinBlepAccumulator {
44 public:
45 static_assert(MaximumEvents > 0, "minBLEP event capacity must be positive");
46
47 static constexpr std::size_t capacity = MaximumEvents;
48 static constexpr std::size_t kernel_samples = detail::kMinBlepKernelSamples;
49 static constexpr std::size_t phases_per_sample = detail::kMinBlepPhasesPerSample;
50
51 void reset() noexcept {
52 slots_ = {};
53 active_events_ = 0;
54 }
55
56 [[nodiscard]] MinBlepInsertResult insert(double position, double height) noexcept {
57 if (!(position >= 0.0 && position <= 1.0))
59 if (!std::isfinite(height))
61 if (height == 0.0)
63
64 for (auto& slot : slots_) {
65 if (slot.active)
66 continue;
67 slot.active = true;
68 slot.table_position = (1.0 - position) * static_cast<double>(phases_per_sample);
69 slot.height = height;
70 ++active_events_;
72 }
74 }
75
76 [[nodiscard]] double next() noexcept {
77 double correction = 0.0;
78 constexpr auto last_index = detail::kMinBlepResidual.size() - 1;
79 for (auto& slot : slots_) {
80 if (!slot.active)
81 continue;
82
83 const auto index = static_cast<std::size_t>(slot.table_position);
84 const double fraction = slot.table_position - static_cast<double>(index);
85 const double first = static_cast<double>(detail::kMinBlepResidual[index]);
86 const double second = static_cast<double>(detail::kMinBlepResidual[index + 1]);
87 correction += slot.height * (first + fraction * (second - first));
88 slot.table_position += static_cast<double>(phases_per_sample);
89 if (slot.table_position >= static_cast<double>(last_index)) {
90 slot = {};
91 --active_events_;
92 }
93 }
94 return correction;
95 }
96
97 [[nodiscard]] std::size_t active_events() const noexcept {
98 return active_events_;
99 }
100
101 private:
102 struct Slot {
103 double table_position = 0.0;
104 double height = 0.0;
105 bool active = false;
106 };
107
108 std::array<Slot, MaximumEvents> slots_{};
109 std::size_t active_events_ = 0;
110};
111
112} // namespace pulp::signal::osc
Accumulates causal minBLEP corrections for discontinuities found between adjacent output samples.
Definition minblep.hpp:43
static constexpr std::size_t phases_per_sample
Definition minblep.hpp:49
void reset() noexcept
Definition minblep.hpp:51
std::size_t active_events() const noexcept
Definition minblep.hpp:97
double next() noexcept
Definition minblep.hpp:76
static constexpr std::size_t kernel_samples
Definition minblep.hpp:48
MinBlepInsertResult insert(double position, double height) noexcept
Definition minblep.hpp:56
static constexpr std::size_t capacity
Definition minblep.hpp:47
Definition blep.hpp:72
MinBlepInsertResult
Definition minblep.hpp:14
@ non_finite_height
Definition minblep.hpp:18
@ inserted
Definition minblep.hpp:15
@ zero_height
Definition minblep.hpp:16
@ invalid_position
Definition minblep.hpp:17
@ capacity_exceeded
Definition minblep.hpp:19
@ first
Definition dither.hpp:23
@ second
Definition dither.hpp:24