Line data Source code
1 : #include "audio/effects/distortion/overdrive.h"
2 :
3 : #include "audio/effects/core/effect_factory.h"
4 :
5 : namespace Amplitron {
6 :
7 2 : static EffectRegistrar<Overdrive> reg("Overdrive");
8 :
9 496 : Overdrive::Overdrive() {
10 1116 : params_ = {
11 372 : {"Drive", 1.5f, 1.0f, 10.0f, 1.5f, "x",
12 124 : "Amount of input gain pushing into soft-clipping. Increases harmonic saturation and "
13 : "sustain."},
14 124 : {"Tone", 0.7f, 0.0f, 1.0f, 0.7f, "",
15 124 : "Adjusts high-frequency content. Lower values are darker and smoother, higher values are "
16 : "brighter and more biting."},
17 124 : {"Level", 0.7f, 0.0f, 1.0f, 0.7f, "",
18 124 : "Master output volume of the pedal to compensate for the gain added by the Drive "
19 : "control."},
20 1116 : };
21 744 : }
22 :
23 27 : void Overdrive::process(float* buffer, int num_samples) {
24 27 : if (!enabled_) return;
25 :
26 27 : const float mix = mix_.load(std::memory_order_relaxed);
27 :
28 27 : const float alpha = 1.0f - std::exp(-1.0f / (sample_rate_ * 0.010f)); // 10 ms
29 27 : smoothed_drive_ += alpha * (params_[0].value - smoothed_drive_);
30 27 : smoothed_tone_ += alpha * (params_[1].value - smoothed_tone_);
31 27 : smoothed_level_ += alpha * (params_[2].value - smoothed_level_);
32 :
33 27 : float drive = smoothed_drive_;
34 27 : float tone = smoothed_tone_;
35 27 : float level = smoothed_level_;
36 :
37 27 : float lp_coeff = 0.05f + tone * 0.9f;
38 :
39 6195 : for (int i = 0; i < num_samples; ++i) {
40 6168 : float dry = buffer[i];
41 :
42 : // Asymmetric soft clipping (tube-like)
43 6168 : float x = buffer[i] * drive;
44 6168 : if (x > 0.0f) {
45 2979 : x = 1.0f - std::exp(-x);
46 993 : } else {
47 3189 : x = -1.0f + std::exp(x);
48 3189 : x *= 0.8f; // asymmetry
49 : }
50 :
51 : // Tone: LP filter
52 6168 : x = tone_lp_.lp(x, lp_coeff);
53 :
54 : // DC blocking HP filter
55 6168 : x = dc_block_.hp(x, 0.001f);
56 :
57 6168 : x *= level;
58 6168 : buffer[i] = dry * (1.0f - mix) + x * mix;
59 2056 : }
60 9 : }
61 :
62 336 : void Overdrive::reset() {
63 336 : tone_lp_.reset();
64 336 : dc_block_.reset();
65 336 : }
66 :
67 : } // namespace Amplitron
|