Pulp 1.88.0
Cross-platform audio plugin framework
Loading...
Searching...
No Matches
gain.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <algorithm>
4#include <cmath>
7#include <type_traits>
8
9namespace pulp::signal {
10
11// Simple gain processor.
12// RT contract: setters and process paths are scalar-only and allocate no
13// memory.
14template <typename SampleType = float> class GainT {
15 static_assert(std::is_floating_point_v<SampleType>,
16 "GainT requires a floating-point sample type");
17
18 public:
21 void set_ramp_time(SampleType seconds, SampleType sample_rate) {
22 gain_ramp_.set_ramp_time(seconds, sample_rate);
23 ramp_enabled_ = true;
24 }
25
26 void set_gain_db(SampleType db) {
27 set_gain_linear(std::pow(SampleType{10}, db / SampleType{20}));
28 }
29 void set_gain_linear(SampleType linear) {
30 if (ramp_enabled_)
31 gain_ramp_.set_target(linear);
32 else
33 gain_ramp_.set_immediate(linear);
34 }
35 SampleType gain_db() const {
36 return SampleType{20} * std::log10(std::max(gain_ramp_.current(), SampleType{1e-10f}));
37 }
38 SampleType gain_linear() const {
39 return gain_ramp_.current();
40 }
41
42 SampleType process(SampleType input) const {
43 return input * gain_ramp_.next();
44 }
45
50 void process(SampleType* buffer, int num_samples) const {
51 for (int i = 0; i < num_samples; ++i)
52 buffer[i] *= gain_ramp_.next();
53 }
54
59 void process(SampleType* const* channels, int num_channels, int num_samples) const {
60 if (channels == nullptr || num_channels <= 0 || num_samples <= 0)
61 return;
62 int i = 0;
63 for (; i < num_samples && gain_ramp_.is_smoothing(); ++i) {
64 const SampleType gain = gain_ramp_.next();
65 for (int c = 0; c < num_channels; ++c)
66 if (channels[c] != nullptr)
67 channels[c][i] *= gain;
68 }
69 if (i == num_samples)
70 return;
71 const SampleType gain = gain_ramp_.current();
72 for (int c = 0; c < num_channels; ++c)
73 if (channels[c] != nullptr)
74 for (int k = i; k < num_samples; ++k)
75 channels[c][k] *= gain;
76 }
77
78 private:
79 mutable SmoothedValue<SampleType> gain_ramp_{SampleType{1}};
80 bool ramp_enabled_ = false;
81};
82
83// Simple dry/wet mixer (single-sample, no latency compensation).
84// RT contract: setters and process paths are scalar-only and allocate no
85// memory. For the full multi-channel version with latency compensation, use
86// dry_wet_mixer.hpp.
87template <typename SampleType = float> class SimpleMixerT {
88 static_assert(std::is_floating_point_v<SampleType>,
89 "SimpleMixerT requires a floating-point sample type");
90
91 public:
92 void set_mix(SampleType mix) {
93 mix_ = std::clamp(mix, SampleType{0}, SampleType{1});
94 }
95 SampleType mix() const {
96 return mix_;
97 }
98
99 SampleType process(SampleType dry, SampleType wet) const {
100 return dry * (SampleType{1} - mix_) + wet * mix_;
101 }
102
103 void process(const SampleType* dry, const SampleType* wet, SampleType* output,
104 int num_samples) const {
105 const SampleType d = SampleType{1} - mix_;
106 for (int i = 0; i < num_samples; ++i)
107 output[i] = dry[i] * d + wet[i] * mix_;
108 }
109
110 private:
111 SampleType mix_ = SampleType{1}; // 1.0 = fully wet
112};
113
118
119} // namespace pulp::signal
Definition gain.hpp:14
SampleType process(SampleType input) const
Definition gain.hpp:42
void set_ramp_time(SampleType seconds, SampleType sample_rate)
Enable a linear per-sample gain ramp.
Definition gain.hpp:21
void process(SampleType *buffer, int num_samples) const
One channel.
Definition gain.hpp:50
SampleType gain_db() const
Definition gain.hpp:35
SampleType gain_linear() const
Definition gain.hpp:38
void process(SampleType *const *channels, int num_channels, int num_samples) const
Every channel of a multi-channel block with one shared ramp: sample i of every channel is multiplied ...
Definition gain.hpp:59
void set_gain_db(SampleType db)
Definition gain.hpp:26
void set_gain_linear(SampleType linear)
Definition gain.hpp:29
Definition gain.hpp:87
void process(const SampleType *dry, const SampleType *wet, SampleType *output, int num_samples) const
Definition gain.hpp:103
void set_mix(SampleType mix)
Definition gain.hpp:92
float mix() const
Definition gain.hpp:95
SampleType process(SampleType dry, SampleType wet) const
Definition gain.hpp:99
Linear smoothing for control-rate parameters.
Definition smoothed_value.hpp:24
Definition additive_bank.hpp:218
@ buffer
A borrowed static buffer with a traversable playhead.
Definition granular.hpp:110
SimpleMixerT< double > SimpleMixer64
Definition gain.hpp:117
@ c
Definition spectrum_trace.hpp:35
@ i
Definition formant_filter_bank.hpp:20
@ linear
2-point. Cheapest; audible imaging when pitching up.
Definition granular.hpp:133
GainT< double > Gain64
Definition gain.hpp:115
GainT< float > Gain
Definition gain.hpp:114
SimpleMixerT< float > SimpleMixer
Definition gain.hpp:116