Audacity  2.2.2
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 class EffectDistortion final : public Effect
49 {
50 public:
52  virtual ~EffectDistortion();
53 
54  struct Params
55  {
57  bool mDCBlock;
58  double mThreshold_dB;
59  double mNoiseFloor;
60  double mParam1;
61  double mParam2;
62  int mRepeats;
63  };
64 
65  // IdentInterface implementation
66 
67  wxString GetSymbol() override;
68  wxString GetDescription() override;
69  wxString ManualPage() override;
70 
71  // EffectIdentInterface implementation
72 
73  EffectType GetType() override;
74  bool SupportsRealtime() override;
75 
76  // EffectClientInterface implementation
77 
78  unsigned GetAudioInCount() override;
79  unsigned GetAudioOutCount() override;
80  bool ProcessInitialize(sampleCount totalLen, ChannelNames chanMap = NULL) override;
81  size_t ProcessBlock(float **inBlock, float **outBlock, size_t blockLen) override;
82  bool RealtimeInitialize() override;
83  bool RealtimeAddProcessor(unsigned numChannels, float sampleRate) override;
84  bool RealtimeFinalize() override;
85  size_t RealtimeProcess(int group,
86  float **inbuf,
87  float **outbuf,
88  size_t numSamples) override;
89  bool GetAutomationParameters(EffectAutomationParameters & parms) override;
90  bool SetAutomationParameters(EffectAutomationParameters & parms) override;
91  wxArrayString GetFactoryPresets() override;
92  bool LoadFactoryPreset(int id) override;
93 
94  // Effect implementation
95 
96  void PopulateOrExchange(ShuttleGui & S) override;
97  bool TransferDataToWindow() override;
98  bool TransferDataFromWindow() override;
99 
100 private:
101 
102  enum control
103  {
104  ID_Type = 10000,
111  };
112  // EffectDistortion implementation
113 
114  void InstanceInit(EffectDistortionState & data, float sampleRate);
116  float **inBlock,
117  float **outBlock,
118  size_t blockLen);
119 
120  // Control Handlers
121 
122  void OnTypeChoice(wxCommandEvent & evt);
123  void OnDCBlockCheckbox(wxCommandEvent & evt);
124  void OnThresholdText(wxCommandEvent & evt);
125  void OnThresholdSlider(wxCommandEvent & evt);
126  void OnNoiseFloorText(wxCommandEvent & evt);
127  void OnNoiseFloorSlider(wxCommandEvent & evt);
128  void OnParam1Text(wxCommandEvent & evt);
129  void OnParam1Slider(wxCommandEvent & evt);
130  void OnParam2Text(wxCommandEvent & evt);
131  void OnParam2Slider(wxCommandEvent & evt);
132  void OnRepeatsText(wxCommandEvent & evt);
133  void OnRepeatsSlider(wxCommandEvent & evt);
134  void UpdateUI();
135  void UpdateControl(control id, bool enable, wxString name);
136  void UpdateControlText(wxTextCtrl *textCtrl, wxString &string, bool enabled);
137 
138  void MakeTable();
139  float WaveShaper(float sample);
140  float DCFilter(EffectDistortionState & data, float sample);
141 
142  // Preset tables for gain lookup
143 
144  void HardClip(); // hard clipping
145  void SoftClip(); // soft clipping
146  void ExponentialTable(); // exponential mapping
147  void LogarithmicTable(); // logarithmic mapping
148  void HalfSinTable();
149  void CubicTable();
150  void EvenHarmonicTable();
151  void SineTable();
152  void Leveller(); // 'Leveller' wavetable is modeled on the legacy effect of the same name.
153  void Rectifier(); // 0% = Dry, 50% = half-wave rectified, 100% = full-wave rectified (abs value).
154  void HardLimiter(); // Same effect as the LADSPA "hardLimiter 1413"
155 
156  // Wavetable helper functions
157 
158  void CopyHalfTable(); // for symmetric tables
159 
160  // Used by Soft Clipping but could be used for other tables.
161  // Log curve formula: y = T + (((e^(RT - Rx)) - 1) / -R)
162  // where R is the ratio, T is the threshold, and x is from T to 1.
163  inline float LogCurve(double threshold, float value, double ratio);
164 
165  // Used by Cubic curve but could be used for other tables
166  // Cubic formula: y = x - (x^3 / 3.0)
167  inline double Cubic(double x);
168 
169 
170 private:
172  std::vector<EffectDistortionState> mSlaves;
173 
174  double mTable[TABLESIZE];
175  double mThreshold;
177 
178  // mMakeupGain is used by some distortion types to pass the
179  // amount of gain required to bring overall effect gain to unity
180  double mMakeupGain;
181 
183  wxArrayString mTableTypes;
184 
185  wxChoice *mTypeChoiceCtrl;
186  wxTextCtrl *mThresholdT;
187  wxTextCtrl *mNoiseFloorT;
188  wxTextCtrl *mParam1T;
189  wxTextCtrl *mParam2T;
190  wxTextCtrl *mRepeatsT;
191 
192  wxSlider *mThresholdS;
193  wxSlider *mNoiseFloorS;
194  wxSlider *mParam1S;
195  wxSlider *mParam2S;
196  wxSlider *mRepeatsS;
197 
198  wxCheckBox *mDCBlockCheckBox;
199 
200  wxStaticText *mThresholdTxt;
201  wxStaticText *mNoiseFloorTxt;
202  wxStaticText *mParam1Txt;
203  wxStaticText *mParam2Txt;
204  wxStaticText *mRepeatsTxt;
205 
208  wxString mOldParam1Txt;
209  wxString mOldParam2Txt;
210  wxString mOldRepeatsTxt;
211 
213 
214  DECLARE_EVENT_TABLE()
215 };
216 
217 #endif
bool ProcessInitialize(sampleCount totalLen, ChannelNames chanMap=NULL) override
Definition: Distortion.cpp:236
wxStaticText * mParam1Txt
Definition: Distortion.h:202
unsigned GetAudioOutCount() override
Definition: Distortion.cpp:231
wxStaticText * mNoiseFloorTxt
Definition: Distortion.h:201
bool SupportsRealtime() override
Definition: Distortion.cpp:215
bool RealtimeAddProcessor(unsigned numChannels, float sampleRate) override
Definition: Distortion.cpp:256
wxSlider * mParam1S
Definition: Distortion.h:194
void UpdateControl(control id, bool enable, wxString name)
Definition: Distortion.cpp:849
wxTextCtrl * mNoiseFloorT
Definition: Distortion.h:187
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:622
wxArrayString GetFactoryPresets() override
Definition: Distortion.cpp:317
wxChoice * mTypeChoiceCtrl
Definition: Distortion.h:185
wxString mOldRepeatsTxt
Definition: Distortion.h:210
size_t InstanceProcess(EffectDistortionState &data, float **inBlock, float **outBlock, size_t blockLen)
Definition: Distortion.cpp:526
bool mbSavedFilterState
Definition: Distortion.h:176
double Cubic(double x)
void OnParam1Text(wxCommandEvent &evt)
Definition: Distortion.cpp:643
EffectType GetType() override
Definition: Distortion.cpp:210
bool RealtimeInitialize() override
Definition: Distortion.cpp:247
Base class for many of the effects in Audacity.
Definition: Effect.h:61
wxString GetDescription() override
Definition: Distortion.cpp:198
void OnRepeatsSlider(wxCommandEvent &evt)
Definition: Distortion.cpp:673
void OnTypeChoice(wxCommandEvent &evt)
Definition: Distortion.cpp:601
#define TABLESIZE
Definition: Distortion.h:28
wxTextCtrl * mParam1T
Definition: Distortion.h:188
wxSlider * mRepeatsS
Definition: Distortion.h:196
sampleCount skipcount
Definition: Distortion.h:34
void OnParam2Slider(wxCommandEvent &evt)
Definition: Distortion.cpp:661
float LogCurve(double threshold, float value, double ratio)
wxTextCtrl * mThresholdT
Definition: Distortion.h:186
bool GetAutomationParameters(EffectAutomationParameters &parms) override
Definition: Distortion.cpp:283
void OnDCBlockCheckbox(wxCommandEvent &evt)
Definition: Distortion.cpp:608
A WaveShaper distortion effect.
Definition: Distortion.h:48
void EvenHarmonicTable()
wxStaticText * mParam2Txt
Definition: Distortion.h:203
bool RealtimeFinalize() override
Definition: Distortion.cpp:267
virtual ~EffectDistortion()
Definition: Distortion.cpp:187
wxString ManualPage() override
Definition: Distortion.cpp:203
wxSlider * mThresholdS
Definition: Distortion.h:192
double mTable[TABLESIZE]
Definition: Distortion.h:174
unsigned GetAudioInCount() override
Definition: Distortion.cpp:226
wxTextCtrl * mParam2T
Definition: Distortion.h:189
wxSlider * mParam2S
Definition: Distortion.h:195
void InstanceInit(EffectDistortionState &data, float sampleRate)
Definition: Distortion.cpp:502
float DCFilter(EffectDistortionState &data, float sample)
std::vector< EffectDistortionState > mSlaves
Definition: Distortion.h:172
wxString mOldParam2Txt
Definition: Distortion.h:209
bool LoadFactoryPreset(int id) override
Definition: Distortion.cpp:329
float WaveShaper(float sample)
EffectDistortionState mMaster
Definition: Distortion.h:171
wxStaticText * mRepeatsTxt
Definition: Distortion.h:204
double mMakeupGain
Definition: Distortion.h:180
void OnNoiseFloorText(wxCommandEvent &evt)
Definition: Distortion.cpp:630
void OnNoiseFloorSlider(wxCommandEvent &evt)
Definition: Distortion.cpp:636
wxStaticText * mThresholdTxt
Definition: Distortion.h:200
void OnRepeatsText(wxCommandEvent &evt)
Definition: Distortion.cpp:667
const wxChar * name
Definition: Distortion.cpp:94
wxSlider * mNoiseFloorS
Definition: Distortion.h:193
wxString GetSymbol() override
Definition: Distortion.cpp:193
void OnParam2Text(wxCommandEvent &evt)
Definition: Distortion.cpp:655
wxArrayString mTableTypes
Definition: Distortion.h:183
bool TransferDataToWindow() override
Definition: Distortion.cpp:469
size_t RealtimeProcess(int group, float **inbuf, float **outbuf, size_t numSamples) override
Definition: Distortion.cpp:274
bool SetAutomationParameters(EffectAutomationParameters &parms) override
Definition: Distortion.cpp:296
wxCheckBox * mDCBlockCheckBox
Definition: Distortion.h:198
std::queue< float > queuesamples
Definition: Distortion.h:44
size_t ProcessBlock(float **inBlock, float **outBlock, size_t blockLen) override
Definition: Distortion.cpp:242
void PopulateOrExchange(ShuttleGui &S) override
Definition: Distortion.cpp:350
wxString mOldmNoiseFloorTxt
Definition: Distortion.h:207
wxTextCtrl * mRepeatsT
Definition: Distortion.h:190
void UpdateControlText(wxTextCtrl *textCtrl, wxString &string, bool enabled)
Definition: Distortion.cpp:929
void OnThresholdText(wxCommandEvent &evt)
Definition: Distortion.cpp:615
bool TransferDataFromWindow() override
Definition: Distortion.cpp:490
wxString mOldThresholdTxt
Definition: Distortion.h:206
wxString mOldParam1Txt
Definition: Distortion.h:208
void OnParam1Slider(wxCommandEvent &evt)
Definition: Distortion.cpp:649