Pulp 1.88.0
Cross-platform audio plugin framework
Loading...
Searching...
No Matches
lowpass_gate.hpp
Go to the documentation of this file.
1#pragma once
2
5
6#include <algorithm>
7#include <cmath>
8
9namespace pulp::signal {
10
42template <typename SampleType = float>
44public:
48 LowpassGateT() { update(); }
49
50 void set_sample_rate(double sr) {
51 sample_rate_ = sr > 0.0 ? sr : sample_rate_;
52 vactrol_.prepare(sample_rate_);
53 update();
54 }
55
58 void set_rise_ms(double ms) { vactrol_.set_rise_ms(ms); }
59
62 void set_fall_ms(double ms) { vactrol_.set_fall_ms(ms); }
63
65 void set_colour(double amount) { colour_ = std::clamp(amount, 0.0, 1.0); }
66
68 void set_closed_cutoff_hz(double hz) {
69 closed_hz_ = std::clamp(hz, 10.0, 20000.0);
70 update();
71 }
72
74 void set_open_cutoff_hz(double hz) {
75 open_hz_ = std::clamp(hz, 10.0, 20000.0);
76 update();
77 }
78
82 void set_gain_exponent(double e) { gain_exponent_ = std::clamp(e, 0.25, 4.0); }
83
84 void reset() {
85 vactrol_.reset();
86 gate_filter_.reset();
87 }
88
91 double control() const { return vactrol_.control(); }
92
96 SampleType process(SampleType input, double target) {
97 const double control_ = vactrol_.process(target);
98
99 const double vca = std::pow(control_, gain_exponent_);
100 // The two paths are complementary: the amplitude term carries the whole
101 // gate at colour 0 and is bypassed at colour 1, while the filter blend
102 // below does the opposite.
103 double x = static_cast<double>(input) * (1.0 - (1.0 - colour_) * (1.0 - vca));
104
105 // The corner sweeps geometrically with the control, because pitch and
106 // brightness are heard geometrically: a linear sweep spends nearly all
107 // its travel in the top octave and sounds like it does nothing.
108 gate_filter_.set_cutoff(static_cast<SampleType>(
109 closed_limit_ * std::pow(open_limit_ / closed_limit_, control_)));
110 const double filtered = gate_filter_.process_lowpass(static_cast<SampleType>(x));
111
112 // Mirror of the amplitude blend: colour 0 leaves the signal unfiltered.
113 x = x + colour_ * (filtered - x);
114 return static_cast<SampleType>(x);
115 }
116
117private:
118 void update() {
119 gate_filter_.prepare(static_cast<SampleType>(sample_rate_));
120 const double nyquist = 0.49 * sample_rate_;
121 open_limit_ = std::min(open_hz_, nyquist);
122 closed_limit_ = std::min(closed_hz_, nyquist);
123 }
124
125 double sample_rate_ = 44100.0;
126 double colour_ = 0.5;
127 double closed_hz_ = 60.0;
128 double open_hz_ = 12000.0;
129 double gain_exponent_ = 1.5;
130
131 double open_limit_ = 12000.0;
132 double closed_limit_ = 60.0;
133
134 VactrolConditionerT<SampleType> vactrol_{};
135 TptFilterT<SampleType> gate_filter_;
136};
137
140
141} // namespace pulp::signal
A lowpass gate: an amplitude gate and a lowpass filter driven by one control signal with deliberately...
Definition lowpass_gate.hpp:43
SampleType process(SampleType input, double target)
Advances the vactrol by one sample toward target and applies the resulting gate to input.
Definition lowpass_gate.hpp:96
void set_open_cutoff_hz(double hz)
Filter corner when the gate is fully open, in Hz.
Definition lowpass_gate.hpp:74
double control() const
Current smoothed control value in [0, 1].
Definition lowpass_gate.hpp:91
void set_rise_ms(double ms)
Time constant of the vactrol turning on, in milliseconds.
Definition lowpass_gate.hpp:58
void set_gain_exponent(double e)
Exponent of the control-to-gain law.
Definition lowpass_gate.hpp:82
void set_colour(double amount)
Blend between amplitude gating (0) and filtering (1).
Definition lowpass_gate.hpp:65
void reset()
Definition lowpass_gate.hpp:84
void set_sample_rate(double sr)
Definition lowpass_gate.hpp:50
LowpassGateT()
A default-constructed gate is usable at 44.1 kHz without a setup call, so a caller that only override...
Definition lowpass_gate.hpp:48
void set_closed_cutoff_hz(double hz)
Filter corner when the gate is fully closed, in Hz.
Definition lowpass_gate.hpp:68
void set_fall_ms(double ms)
Time constant of the vactrol turning off, in milliseconds.
Definition lowpass_gate.hpp:62
Definition additive_bank.hpp:218
LowpassGateT< double > LowpassGate64
Definition lowpass_gate.hpp:139
@ e
Definition formant_filter_bank.hpp:20
LowpassGateT< float > LowpassGate
Definition lowpass_gate.hpp:138
First-order topology-preserving transform (TPT) filter.
The vactrol control-conditioning law: an asymmetric one-pole whose rise and fall are deliberately dif...