Audacity  2.2.0
Distortion.h
Go to the documentation of this file.
1 /**********************************************************************
2 
3  Audacity: A Digital Audio Editor
4 
5  Distortion.h
6 
7  Steve Daulton
8 
9 **********************************************************************/
10 
11 #ifndef __AUDACITY_EFFECT_DISTORTION__
12 #define __AUDACITY_EFFECT_DISTORTION__
13 
14 #include <wx/arrstr.h>
15 #include <wx/string.h>
16 #include <wx/slider.h>
17 #include <wx/textctrl.h>
18 #include <wx/checkbox.h>
19 
20 #include <queue>
21 
22 #include "Effect.h"
23 
24 class ShuttleGui;
25 
26 #define DISTORTION_PLUGIN_SYMBOL XO("Distortion")
27 #define STEPS 1024 // number of +ve or -ve steps in lookup tabe
28 #define TABLESIZE 2049 // size of lookup table (steps * 2 + 1)
29 
31 {
32 public:
33  float samplerate;
34  sampleCount skipcount;
36  bool dcblock;
37  double threshold;
38  double noisefloor;
39  double param1;
40  double param2;
41  int repeats;
42 
43  // DC block filter variables
44  std::queue<float> queuesamples;
45  double queuetotal;
46 };
47 
48 WX_DECLARE_OBJARRAY(EffectDistortionState, EffectDistortionStateArray);
49 
50 class EffectDistortion final : public Effect
51 {
52 public:
54  virtual ~EffectDistortion();
55 
56  struct Params
57  {
59  bool mDCBlock;
60  double mThreshold_dB;
61  double mNoiseFloor;
62  double mParam1;
63  double mParam2;
64  int mRepeats;
65  };
66 
67  // IdentInterface implementation
68 
69  wxString GetSymbol() override;
70  wxString GetDescription() override;
71  wxString ManualPage() override;
72 
73  // EffectIdentInterface implementation
74 
75  EffectType GetType() override;
76  bool SupportsRealtime() override;
77 
78  // EffectClientInterface implementation
79 
80  unsigned GetAudioInCount() override;
81  unsigned GetAudioOutCount() override;
82  bool ProcessInitialize(sampleCount totalLen, ChannelNames chanMap = NULL) override;
83  size_t ProcessBlock(float **inBlock, float **outBlock, size_t blockLen) override;
84  bool RealtimeInitialize() override;
85  bool RealtimeAddProcessor(unsigned numChannels, float sampleRate) override;
86  bool RealtimeFinalize() override;
87  size_t RealtimeProcess(int group,
88  float **inbuf,
89  float **outbuf,
90  size_t numSamples) override;
91  bool GetAutomationParameters(EffectAutomationParameters & parms) override;
92  bool SetAutomationParameters(EffectAutomationParameters & parms) override;
93  wxArrayString GetFactoryPresets() override;
94  bool LoadFactoryPreset(int id) override;
95 
96  // Effect implementation
97 
98  void PopulateOrExchange(ShuttleGui & S) override;
99  bool TransferDataToWindow() override;
100  bool TransferDataFromWindow() override;
101 
102 private:
103 
104  enum control
105  {
106  ID_Type = 10000,
113  };
114  // EffectDistortion implementation
115 
116  void InstanceInit(EffectDistortionState & data, float sampleRate);
118  float **inBlock,
119  float **outBlock,
120  size_t blockLen);
121 
122  // Control Handlers
123 
124  void OnTypeChoice(wxCommandEvent & evt);
125  void OnDCBlockCheckbox(wxCommandEvent & evt);
126  void OnThresholdText(wxCommandEvent & evt);
127  void OnThresholdSlider(wxCommandEvent & evt);
128  void OnNoiseFloorText(wxCommandEvent & evt);
129  void OnNoiseFloorSlider(wxCommandEvent & evt);
130  void OnParam1Text(wxCommandEvent & evt);
131  void OnParam1Slider(wxCommandEvent & evt);
132  void OnParam2Text(wxCommandEvent & evt);
133  void OnParam2Slider(wxCommandEvent & evt);
134  void OnRepeatsText(wxCommandEvent & evt);
135  void OnRepeatsSlider(wxCommandEvent & evt);
136  void UpdateUI();
137  void UpdateControl(control id, bool enable, wxString name);
138  void UpdateControlText(wxTextCtrl *textCtrl, wxString &string, bool enabled);
139 
140  void MakeTable();
141  float WaveShaper(float sample);
142  float DCFilter(EffectDistortionState & data, float sample);
143 
144  // Preset tables for gain lookup
145 
146  void HardClip(); // hard clipping
147  void SoftClip(); // soft clipping
148  void ExponentialTable(); // exponential mapping
149  void LogarithmicTable(); // logarithmic mapping
150  void HalfSinTable();
151  void CubicTable();
152  void EvenHarmonicTable();
153  void SineTable();
154  void Leveller(); // 'Leveller' wavetable is modeled on the legacy effect of the same name.
155  void Rectifier(); // 0% = Dry, 50% = half-wave rectified, 100% = full-wave rectified (abs value).
156  void HardLimiter(); // Same effect as the LADSPA "hardLimiter 1413"
157 
158  // Wavetable helper functions
159 
160  void CopyHalfTable(); // for symmetric tables
161 
162  // Used by Soft Clipping but could be used for other tables.
163  // Log curve formula: y = T + (((e^(RT - Rx)) - 1) / -R)
164  // where R is the ratio, T is the threshold, and x is from T to 1.
165  inline float LogCurve(double threshold, float value, double ratio);
166 
167  // Used by Cubic curve but could be used for other tables
168  // Cubic formula: y = x - (x^3 / 3.0)
169  inline double Cubic(double x);
170 
171 
172 private:
174  EffectDistortionStateArray mSlaves;
175 
176  double mTable[TABLESIZE];
177  double mThreshold;
179 
180  // mMakeupGain is used by some distortion types to pass the
181  // amount of gain required to bring overall effect gain to unity
182  double mMakeupGain;
183 
185  wxArrayString mTableTypes;
186 
187  wxChoice *mTypeChoiceCtrl;
188  wxTextCtrl *mThresholdT;
189  wxTextCtrl *mNoiseFloorT;
190  wxTextCtrl *mParam1T;
191  wxTextCtrl *mParam2T;
192  wxTextCtrl *mRepeatsT;
193 
194  wxSlider *mThresholdS;
195  wxSlider *mNoiseFloorS;
196  wxSlider *mParam1S;
197  wxSlider *mParam2S;
198  wxSlider *mRepeatsS;
199 
200  wxCheckBox *mDCBlockCheckBox;
201 
202  wxStaticText *mThresholdTxt;
203  wxStaticText *mNoiseFloorTxt;
204  wxStaticText *mParam1Txt;
205  wxStaticText *mParam2Txt;
206  wxStaticText *mRepeatsTxt;
207 
210  wxString mOldParam1Txt;
211  wxString mOldParam2Txt;
212  wxString mOldRepeatsTxt;
213 
215 
216  DECLARE_EVENT_TABLE()
217 };
218 
219 #endif
void Rectifier()
Definition: Distortion.cpp:1210
bool ProcessInitialize(sampleCount totalLen, ChannelNames chanMap=NULL) override
Definition: Distortion.cpp:239
wxStaticText * mParam1Txt
Definition: Distortion.h:204
unsigned GetAudioOutCount() override
Definition: Distortion.cpp:234
wxStaticText * mNoiseFloorTxt
Definition: Distortion.h:203
bool SupportsRealtime() override
Definition: Distortion.cpp:218
bool RealtimeAddProcessor(unsigned numChannels, float sampleRate) override
Definition: Distortion.cpp:259
void CubicTable()
Definition: Distortion.cpp:1084
wxSlider * mParam1S
Definition: Distortion.h:196
void MakeTable()
Definition: Distortion.cpp:947
void UpdateControl(control id, bool enable, wxString name)
Definition: Distortion.cpp:852
wxTextCtrl * mNoiseFloorT
Definition: Distortion.h:189
Derived from ShuttleGuiBase, an Audacity specific class for shuttling data to and from GUI...
Definition: ShuttleGui.h:366
void OnThresholdSlider(wxCommandEvent &evt)
Definition: Distortion.cpp:625
wxArrayString GetFactoryPresets() override
Definition: Distortion.cpp:320
wxChoice * mTypeChoiceCtrl
Definition: Distortion.h:187
void CopyHalfTable()
Definition: Distortion.cpp:1241
wxString mOldRepeatsTxt
Definition: Distortion.h:212
double threshold
Definition: Distortion.h:37
size_t InstanceProcess(EffectDistortionState &data, float **inBlock, float **outBlock, size_t blockLen)
Definition: Distortion.cpp:529
double mParam1
Definition: Distortion.h:62
bool mbSavedFilterState
Definition: Distortion.h:178
double Cubic(double x)
Definition: Distortion.cpp:1110
void OnParam1Text(wxCommandEvent &evt)
Definition: Distortion.cpp:646
Definition: Distortion.h:107
EffectType GetType() override
Definition: Distortion.cpp:213
bool RealtimeInitialize() override
Definition: Distortion.cpp:250
void HalfSinTable()
Definition: Distortion.cpp:1066
float samplerate
Definition: Distortion.h:33
int tablechoiceindx
Definition: Distortion.h:35
Base class for many of the effects in Audacity.
Definition: Effect.h:62
wxString GetDescription() override
Definition: Distortion.cpp:201
double param1
Definition: Distortion.h:39
EffectDistortion()
Definition: Distortion.cpp:172
void OnRepeatsSlider(wxCommandEvent &evt)
Definition: Distortion.cpp:676
void OnTypeChoice(wxCommandEvent &evt)
Definition: Distortion.cpp:604
#define TABLESIZE
Definition: Distortion.h:28
int mRepeats
Definition: Distortion.h:64
void SineTable()
Definition: Distortion.cpp:1135
wxTextCtrl * mParam1T
Definition: Distortion.h:190
Definition: Distortion.h:108
wxSlider * mRepeatsS
Definition: Distortion.h:198
sampleCount skipcount
Definition: Distortion.h:34
control
Definition: Distortion.h:104
void OnParam2Slider(wxCommandEvent &evt)
Definition: Distortion.cpp:664
Definition: Distortion.h:112
float LogCurve(double threshold, float value, double ratio)
Definition: Distortion.cpp:1027
double mThreshold_dB
Definition: Distortion.h:60
wxTextCtrl * mThresholdT
Definition: Distortion.h:188
bool GetAutomationParameters(EffectAutomationParameters &parms) override
Definition: Distortion.cpp:286
void OnDCBlockCheckbox(wxCommandEvent &evt)
Definition: Distortion.cpp:611
A WaveShaper distortion effect.
Definition: Distortion.h:50
void EvenHarmonicTable()
Definition: Distortion.cpp:1119
wxStaticText * mParam2Txt
Definition: Distortion.h:205
bool RealtimeFinalize() override
Definition: Distortion.cpp:270
virtual ~EffectDistortion()
Definition: Distortion.cpp:190
wxString ManualPage() override
Definition: Distortion.cpp:206
bool dcblock
Definition: Distortion.h:36
wxSlider * mThresholdS
Definition: Distortion.h:194
double mTable[TABLESIZE]
Definition: Distortion.h:176
Definition: Distortion.h:106
unsigned GetAudioInCount() override
Definition: Distortion.cpp:229
wxTextCtrl * mParam2T
Definition: Distortion.h:191
wxSlider * mParam2S
Definition: Distortion.h:197
Definition: Distortion.h:111
Definition: Distortion.h:110
void InstanceInit(EffectDistortionState &data, float sampleRate)
Definition: Distortion.cpp:505
float DCFilter(EffectDistortionState &data, float sample)
Definition: Distortion.cpp:1282
void HardLimiter()
Definition: Distortion.cpp:1230
wxString mOldParam2Txt
Definition: Distortion.h:211
bool LoadFactoryPreset(int id) override
Definition: Distortion.cpp:332
float WaveShaper(float sample)
Definition: Distortion.cpp:1252
EffectDistortionState mMaster
Definition: Distortion.h:173
double mNoiseFloor
Definition: Distortion.h:61
void ExponentialTable()
Definition: Distortion.cpp:1032
wxStaticText * mRepeatsTxt
Definition: Distortion.h:206
double queuetotal
Definition: Distortion.h:45
double mMakeupGain
Definition: Distortion.h:182
void OnNoiseFloorText(wxCommandEvent &evt)
Definition: Distortion.cpp:633
void OnNoiseFloorSlider(wxCommandEvent &evt)
Definition: Distortion.cpp:639
void UpdateUI()
Definition: Distortion.cpp:683
wxStaticText * mThresholdTxt
Definition: Distortion.h:202
double param2
Definition: Distortion.h:40
void OnRepeatsText(wxCommandEvent &evt)
Definition: Distortion.cpp:670
const wxChar * name
Definition: Distortion.cpp:94
wxSlider * mNoiseFloorS
Definition: Distortion.h:195
int mTableChoiceIndx
Definition: Distortion.h:58
wxString GetSymbol() override
Definition: Distortion.cpp:196
void OnParam2Text(wxCommandEvent &evt)
Definition: Distortion.cpp:658
wxArrayString mTableTypes
Definition: Distortion.h:185
bool TransferDataToWindow() override
Definition: Distortion.cpp:472
int mTypChoiceIndex
Definition: Distortion.h:184
double mParam2
Definition: Distortion.h:63
size_t RealtimeProcess(int group, float **inbuf, float **outbuf, size_t numSamples) override
Definition: Distortion.cpp:277
int repeats
Definition: Distortion.h:41
bool SetAutomationParameters(EffectAutomationParameters &parms) override
Definition: Distortion.cpp:299
double noisefloor
Definition: Distortion.h:38
double mThreshold
Definition: Distortion.h:177
wxCheckBox * mDCBlockCheckBox
Definition: Distortion.h:200
Definition: Distortion.h:56
WX_DECLARE_OBJARRAY(EffectDistortionState, EffectDistortionStateArray)
std::queue< float > queuesamples
Definition: Distortion.h:44
size_t ProcessBlock(float **inBlock, float **outBlock, size_t blockLen) override
Definition: Distortion.cpp:245
void PopulateOrExchange(ShuttleGui &S) override
Definition: Distortion.cpp:353
void SoftClip()
Definition: Distortion.cpp:1009
wxString mOldmNoiseFloorTxt
Definition: Distortion.h:209
wxTextCtrl * mRepeatsT
Definition: Distortion.h:192
Definition: Distortion.h:30
void UpdateControlText(wxTextCtrl *textCtrl, wxString &string, bool enabled)
Definition: Distortion.cpp:932
void HardClip()
Definition: Distortion.cpp:992
Definition: Distortion.h:109
bool mDCBlock
Definition: Distortion.h:59
void OnThresholdText(wxCommandEvent &evt)
Definition: Distortion.cpp:618
void LogarithmicTable()
Definition: Distortion.cpp:1045
bool TransferDataFromWindow() override
Definition: Distortion.cpp:493
EffectDistortionStateArray mSlaves
Definition: Distortion.h:174
wxString mOldThresholdTxt
Definition: Distortion.h:208
wxString mOldParam1Txt
Definition: Distortion.h:210
void OnParam1Slider(wxCommandEvent &evt)
Definition: Distortion.cpp:652
Params mParams
Definition: Distortion.h:214
void Leveller()
Definition: Distortion.cpp:1153