69 const SampleType d = input - state_;
70 const SampleType lp = g_ * d + state_;
85 return SampleType{2.0f} * lp - input;
99 run_block(
buffer, num_samples, [](SampleType, SampleType lp) {
return lp; });
104 run_block(
buffer, num_samples, [](SampleType in, SampleType lp) {
return in - lp; });
109 run_block(
buffer, num_samples,
110 [](SampleType in, SampleType lp) {
return SampleType{2.0f} * lp - in; });
119 const SampleType d = input - state_;
120 const SampleType lp = g_ * d + state_;
122 SampleType hp = input - lp;
123 SampleType ap = lp - hp;
128 state_ = SampleType{0.0f};
138 SampleType state_ = SampleType{0.0f};
139 SampleType sample_rate_ = SampleType{44100.0f};
140 SampleType cutoff_ = SampleType{1000.0f};
141 SampleType g_ = SampleType{0.0f};
142 SampleType two_g_ = SampleType{0.0f};
144 void update_coefficient() {
145 constexpr SampleType
pi = SampleType{3.14159265358979323846};
146 SampleType wd = SampleType{2.0f} *
pi * cutoff_;
148 (SampleType{2.0f} * sample_rate_) * std::tan(wd / (SampleType{2.0f} * sample_rate_));
149 g_ = wa / (SampleType{2.0f} * sample_rate_ + wa);
150 two_g_ = SampleType{2.0f} * g_;
153 template <
typename Output>
154 void run_block(SampleType*
buffer, std::size_t num_samples, Output output) {
155 const SampleType
g = g_;
156 const SampleType two_g = two_g_;
157 SampleType
s = state_;
158 for (
const auto [begin, end] :
snap_chunks(num_samples)) {
159 for (std::size_t
i = begin;
i <
end; ++
i) {
160 const SampleType input =
buffer[
i];
161 const SampleType
d = input -
s;
162 const SampleType lp =
g *
d +
s;
First-order TPT (trapezoidal integration) filter.
Definition tpt_filter.hpp:30
Outputs process(SampleType input)
Definition tpt_filter.hpp:118
static constexpr float kMaxCutoffFraction
Definition tpt_filter.hpp:35
SampleType process_highpass(SampleType input)
Process and return the highpass output.
Definition tpt_filter.hpp:78
void reset()
Definition tpt_filter.hpp:127
SampleType process_allpass(SampleType input)
Process and return the allpass output.
Definition tpt_filter.hpp:83
SampleType process_lowpass(SampleType input)
Process and return the lowpass output.
Definition tpt_filter.hpp:68
SampleType cutoff() const
Definition tpt_filter.hpp:56
void process_highpass(SampleType *buffer, std::size_t num_samples)
Process a buffer in place, replacing it with the highpass output.
Definition tpt_filter.hpp:103
void prepare(SampleType sample_rate)
Definition tpt_filter.hpp:41
static constexpr float kMinCutoffHz
Definition tpt_filter.hpp:34
static constexpr float kMinSampleRate
Definition tpt_filter.hpp:39
void process_allpass(SampleType *buffer, std::size_t num_samples)
Process a buffer in place, replacing it with the allpass output.
Definition tpt_filter.hpp:108
float sample_rate() const
Definition tpt_filter.hpp:59
void process_lowpass(SampleType *buffer, std::size_t num_samples)
Process a buffer in place, replacing it with the lowpass output.
Definition tpt_filter.hpp:98
void set_cutoff(SampleType hz)
Set cutoff frequency in Hz. Safe to modulate per-sample.
Definition tpt_filter.hpp:49
@ d
Definition pitch.hpp:22
@ g
Definition pitch.hpp:27
constexpr double pi
Definition fir_design.hpp:164
Definition additive_bank.hpp:218
@ buffer
A borrowed static buffer with a traversable playhead.
Definition granular.hpp:110
TptFilterT< float > TptFilter
Definition tpt_filter.hpp:172
T snap_to_zero(T x)
Returns 0 if |x| < snap_threshold, else x.
Definition denormal.hpp:45
@ i
Definition formant_filter_bank.hpp:20
TptFilterT< double > TptFilter64
Definition tpt_filter.hpp:173
constexpr SnapChunks snap_chunks(std::size_t num_samples) noexcept
Definition denormal.hpp:173
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
@ s
Definition input_events.hpp:41
Process and return all three outputs at once.
Definition tpt_filter.hpp:114
SampleType allpass
Definition tpt_filter.hpp:115
SampleType lowpass
Definition tpt_filter.hpp:115
SampleType highpass
Definition tpt_filter.hpp:115