Line data Source code
1 : #pragma once
2 :
3 : // Hard-clipping distortion effect with tone shaping.
4 : // The driven sample u=drive*x[n] is clipped with y=clamp(u, -clip, clip), then
5 : // filtered by the tone stage and level-scaled; this creates odd harmonics as
6 : // the transfer function flattens near the clipping threshold.
7 :
8 : #include "audio/dsp/biquad.h"
9 : #include "audio/effects/core/effect.h"
10 :
11 : namespace Amplitron {
12 :
13 2 : class Distortion : public Effect {
14 : public:
15 : // Create a distortion effect with drive, tone, and output gain controls.
16 : Distortion();
17 : // Apply the distortion curve and filtering to a mono audio buffer.
18 : void process(float* buffer, int num_samples) override;
19 : // Clear any filter state held by the effect.
20 : void reset() override;
21 : // Return the display name for this effect.
22 131 : const char* name() const override { return "Distortion"; }
23 3 : const char* type_id() const override { return "Distortion"; }
24 : // Return editable parameters exposed by this effect.
25 114 : std::vector<EffectParam>& params() override { return params_; }
26 3 : const std::vector<EffectParam>& params() const override { return params_; }
27 :
28 : private:
29 : std::vector<EffectParam> params_;
30 : OnePole tone_lp_;
31 :
32 : // One-pole smoothing states (avoids zipper noise on parameter changes)
33 : float drive_smoothed_ = 2.0f;
34 : float tone_smoothed_ = 0.6f;
35 : float level_smoothed_ = 0.5f;
36 : };
37 :
38 : } // namespace Amplitron
|