Pulp 1.88.0
Cross-platform audio plugin framework
Loading...
Searching...
No Matches
waveguide_reflection_filter.hpp
Go to the documentation of this file.
1#pragma once
2
5
7
8#include <algorithm>
9#include <cmath>
10#include <limits>
11#include <type_traits>
12
13namespace pulp::signal {
14
21template <typename SampleType = float> class WaveguideReflectionFilterT {
22 static_assert(std::is_floating_point_v<SampleType>);
23
24 public:
25 static constexpr double minimum_reflection_gain = -0.999;
26 static constexpr double maximum_reflection_gain = 0.999;
27 static constexpr double default_reflection_gain = -0.995;
28 static constexpr double minimum_loss_pole = 0.0;
29 // Leaves headroom for rate-mapped base-domain poles when this boundary is
30 // evaluated inside an oversampled recursive loop.
31 static constexpr double maximum_loss_pole = 0.9999;
32 static constexpr double default_loss_pole = 0.20;
33
34 void set_reflection_gain(SampleType gain) noexcept {
35 if (std::isfinite(static_cast<double>(gain)))
36 reflection_gain_ = std::clamp(static_cast<double>(gain), minimum_reflection_gain,
38 }
39
40 void set_loss_pole(SampleType pole) noexcept {
41 if (std::isfinite(static_cast<double>(pole)))
42 loss_pole_ = std::clamp(static_cast<double>(pole), minimum_loss_pole,
44 }
45
46 [[nodiscard]] SampleType reflection_gain() const noexcept {
47 return static_cast<SampleType>(reflection_gain_);
48 }
49 [[nodiscard]] SampleType loss_pole() const noexcept {
50 return static_cast<SampleType>(loss_pole_);
51 }
52
53 [[nodiscard]] SampleType process(SampleType input) noexcept {
54 const auto x = static_cast<double>(input);
55 if (!std::isfinite(x))
56 return finite_cast(last_output_);
57
58 const auto output = reflection_gain_ * (1.0 - loss_pole_) * x +
59 loss_pole_ * last_output_;
60 if (!std::isfinite(output)) {
61 reset();
62 return SampleType{};
63 }
64 last_output_ = snap_recursion_state(output);
65 return finite_cast(last_output_);
66 }
67
68 void reset() noexcept { last_output_ = 0.0; }
69
70 private:
71 [[nodiscard]] static SampleType finite_cast(double value) noexcept {
72 const auto limit = static_cast<double>(std::numeric_limits<SampleType>::max());
73 if (!std::isfinite(value) || value > limit || value < -limit)
74 return SampleType{};
75 return static_cast<SampleType>(value);
76 }
77
78 double reflection_gain_ = default_reflection_gain;
79 double loss_pole_ = default_loss_pole;
80 double last_output_ = 0.0;
81};
82
85
86} // namespace pulp::signal
A passive boundary filter with H(z) = g (1 - a) / (1 - a z^-1).
Definition waveguide_reflection_filter.hpp:21
void set_reflection_gain(SampleType gain) noexcept
Definition waveguide_reflection_filter.hpp:34
void set_loss_pole(SampleType pole) noexcept
Definition waveguide_reflection_filter.hpp:40
SampleType loss_pole() const noexcept
Definition waveguide_reflection_filter.hpp:49
static constexpr double minimum_loss_pole
Definition waveguide_reflection_filter.hpp:28
SampleType process(SampleType input) noexcept
Definition waveguide_reflection_filter.hpp:53
static constexpr double default_reflection_gain
Definition waveguide_reflection_filter.hpp:27
static constexpr double maximum_loss_pole
Definition waveguide_reflection_filter.hpp:31
static constexpr double minimum_reflection_gain
Definition waveguide_reflection_filter.hpp:25
static constexpr double maximum_reflection_gain
Definition waveguide_reflection_filter.hpp:26
static constexpr double default_loss_pole
Definition waveguide_reflection_filter.hpp:32
void reset() noexcept
Definition waveguide_reflection_filter.hpp:68
SampleType reflection_gain() const noexcept
Definition waveguide_reflection_filter.hpp:46
Definition additive_bank.hpp:218
WaveguideReflectionFilterT< float > WaveguideReflectionFilter
Definition waveguide_reflection_filter.hpp:83
WaveguideReflectionFilterT< double > WaveguideReflectionFilter64
Definition waveguide_reflection_filter.hpp:84
T snap_recursion_state(T x)
Snap a per-sample recursion's state: snap_to_zero() where subnormals are slow, the identity where kSu...
Definition denormal.hpp:84