Audacity 3.2.0
DistortionBase.h
Go to the documentation of this file.
1/**********************************************************************
2
3 Audacity: A Digital Audio Editor
4
5 DistortionBase.h
6
7 Steve Daulton
8
9**********************************************************************/
10#pragma once
11
12#include "PerTrackEffect.h"
13#include "SampleCount.h"
14#include "SettingsVisitor.h"
15#include <queue>
16
17#define TABLESIZE 2049 // size of lookup table (steps * 2 + 1)
18
19const bool defaultDCBlock = false;
20
22{
23public:
27 bool dcblock;
28 double threshold;
29 double noisefloor;
30 double param1;
31 double param2;
33
34 // DC block filter variables
35 std::queue<float> queuesamples;
36 double queuetotal;
37
39
40 // mMakeupGain is used by some distortion types to pass the
41 // amount of gain required to bring overall effect gain to unity
42 double mMakeupGain { 1.0 };
43};
44
46{
47 static constexpr int mDefaultTableChoiceIndx = 0;
48 static constexpr bool mDefaultDCBlock = defaultDCBlock;
49 static constexpr double mDefaultThreshold_dB = -6.0;
50 static constexpr double mDefaultNoiseFloor = -70.0;
51 static constexpr double mDefaultParam1 = 50.0;
52 static constexpr double mDefaultParam2 = 50.0;
53 static constexpr int mDefaultRepeats = 1;
54
62};
63
64class BUILTIN_EFFECTS_API DistortionBase :
65 public EffectWithSettings<EffectDistortionSettings, PerTrackEffect>
66{
67public:
68 struct Params;
69
71
73 virtual ~DistortionBase();
74
75 // ComponentInterface implementation
76
77 ComponentInterfaceSymbol GetSymbol() const override;
78 TranslatableString GetDescription() const override;
79 ManualPageID ManualPage() const override;
80
81 // EffectDefinitionInterface implementation
82
83 EffectType GetType() const override;
84 RealtimeSince RealtimeSupport() const override;
85 RegistryPaths GetFactoryPresets() const override;
87 LoadFactoryPreset(int id, EffectSettings& settings) const override;
88 OptionalMessage DoLoadFactoryPreset(int id, EffectSettings& settings);
89
90 // Effect implementation
91
92 struct BUILTIN_EFFECTS_API Instance :
95 {
96 explicit Instance(const PerTrackEffect& effect)
97 : PerTrackEffect::Instance { effect }
98 {
99 }
100
101 bool ProcessInitialize(
103 ChannelNames chanMap) override;
104
105 size_t ProcessBlock(
106 EffectSettings& settings, const float* const* inBlock,
107 float* const* outBlock, size_t blockLen) override;
108
109 bool RealtimeInitialize(EffectSettings& settings, double) override;
110
111 bool RealtimeAddProcessor(
113 unsigned numChannels, float sampleRate) override;
114
115 bool RealtimeFinalize(EffectSettings& settings) noexcept override;
116
117 size_t RealtimeProcess(
118 size_t group, EffectSettings& settings, const float* const* inbuf,
119 float* const* outbuf, size_t numSamples) override;
120
121 void InstanceInit(
123 float sampleRate);
124
125 size_t InstanceProcess(
127 const float* const* inBlock, float* const* outBlock, size_t blockLen);
128
129 void MakeTable(
131
132 void HardClip(
134 const EffectDistortionSettings&); // hard clipping
135
136 void SoftClip(
138 const EffectDistortionSettings&); // soft clipping
139
140 void
141 ExponentialTable(const EffectDistortionSettings&); // exponential mapping
142 void
143 LogarithmicTable(const EffectDistortionSettings&); // logarithmic mapping
144 void HalfSinTable(const EffectDistortionSettings&);
145 void CubicTable(const EffectDistortionSettings&);
146 void EvenHarmonicTable(const EffectDistortionSettings&);
147 void SineTable(const EffectDistortionSettings&);
148 void
149 Leveller(const EffectDistortionSettings&); // 'Leveller' wavetable is
150 // modeled on the legacy effect
151 // of the same name.
152 void
153 Rectifier(const EffectDistortionSettings&); // 0% = Dry, 50% = half-wave
154 // rectified, 100% = full-wave
155 // rectified (abs value).
156 void HardLimiter(
158 const EffectDistortionSettings&); // Same effect as the LADSPA
159 // "hardLimiter 1413"
160
161 void CopyHalfTable(); // for symmetric tables
162
163 // Used by Soft Clipping but could be used for other tables.
164 // Log curve formula: y = T + (((e^(RT - Rx)) - 1) / -R)
165 // where R is the ratio, T is the threshold, and x is from T to 1.
166 inline float LogCurve(double threshold, float value, double ratio);
167
168 // Used by Cubic curve but could be used for other tables
169 // Cubic formula: y = x - (x^3 / 3.0)
170 inline double Cubic(const EffectDistortionSettings&, double x);
171
172 float WaveShaper(float sample, EffectDistortionSettings& ms);
173 float DCFilter(EffectDistortionState& data, float sample);
174
175 unsigned GetAudioInCount() const override;
176 unsigned GetAudioOutCount() const override;
177
178 double mTable[TABLESIZE];
179
181 std::vector<EffectDistortionState> mSlaves;
182 };
183
184 std::shared_ptr<EffectInstance> MakeInstance() const override;
185
186protected:
187 const EffectParameterMethods& Parameters() const override;
188
190 {
202 nTableTypes
203 };
204
205 static const EnumValueSymbol kTableTypeStrings[nTableTypes];
206
207 // (Note: 'Repeats' is the total number of times the effect is applied.)
208 static constexpr EnumParameter TableTypeIndx {
210 L"Type",
212 0,
213 nTableTypes - 1,
214 1,
215 kTableTypeStrings,
216 nTableTypes
217 };
218
219 static constexpr EffectParameter DCBlock {
221 L"DC Block",
223 false,
224 true,
225 1
226 };
227
228 static constexpr EffectParameter Threshold_dB {
230 L"Threshold dB",
232 -100.0,
233 0.0,
234 1000.0f
235 };
236
237 static constexpr EffectParameter NoiseFloor {
239 L"Noise Floor",
241 -80.0,
242 -20.0,
243 1
244 };
245
246 static constexpr EffectParameter Param1 {
248 L"Parameter 1",
250 0.0,
251 100.0,
252 1
253 };
254
255 static constexpr EffectParameter Param2 {
257 L"Parameter 2",
259 0.0,
260 100.0,
261 1
262 };
263
264 static constexpr EffectParameter Repeats {
266 L"Repeats",
268 0,
269 5,
270 1
271 };
272};
#define TABLESIZE
const bool defaultDCBlock
EffectType
ChannelName
std::optional< std::unique_ptr< EffectSettingsAccess::Message > > OptionalMessage
std::vector< RegistryPath > RegistryPaths
Definition: Identifier.h:219
static Settings & settings()
Definition: TrackInfo.cpp:51
ComponentInterfaceSymbol pairs a persistent string identifier used internally with an optional,...
A WaveShaper distortion effect.
static const ComponentInterfaceSymbol Symbol
RealtimeSince
In which versions of Audacity was an effect realtime capable?
std::queue< float > queuesamples
Inherit to add a state variable to an EffectInstance subclass.
Hold values to send to effect output meters.
Interface for manipulations of an Effect's settings.
Base class for many of the effects in Audacity.
Holds a msgid for the translation catalog; may also bind format arguments.
Positions or offsets within audio files need a wide type.
Definition: SampleCount.h:19
Instance(const PerTrackEffect &effect)
std::vector< EffectDistortionState > mSlaves
EffectDistortionState mMaster
static constexpr bool mDefaultDCBlock
static constexpr int mDefaultRepeats
static constexpr double mDefaultParam2
static constexpr double mDefaultParam1
static constexpr double mDefaultNoiseFloor
static constexpr double mDefaultThreshold_dB
static constexpr int mDefaultTableChoiceIndx
Externalized state of a plug-in.