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 IdentInterfaceSymbol{ 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;
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 
68  wxString GetDescription() override;
69  wxString ManualPage() override;
70 
71  // EffectDefinitionInterface 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 DefineParams( ShuttleParams & S ) override;
90  bool GetAutomationParameters(CommandParameters & parms) override;
91  bool SetAutomationParameters(CommandParameters & parms) override;
92  wxArrayString GetFactoryPresets() override;
93  bool LoadFactoryPreset(int id) override;
94 
95  // Effect implementation
96 
97  void PopulateOrExchange(ShuttleGui & S) override;
98  bool TransferDataToWindow() override;
99  bool TransferDataFromWindow() override;
100 
101 private:
102 
103  enum control
104  {
105  ID_Type = 10000,
112  };
113  // EffectDistortion implementation
114 
115  void InstanceInit(EffectDistortionState & data, float sampleRate);
117  float **inBlock,
118  float **outBlock,
119  size_t blockLen);
120 
121  // Control Handlers
122 
123  void OnTypeChoice(wxCommandEvent & evt);
124  void OnDCBlockCheckbox(wxCommandEvent & evt);
125  void OnThresholdText(wxCommandEvent & evt);
126  void OnThresholdSlider(wxCommandEvent & evt);
127  void OnNoiseFloorText(wxCommandEvent & evt);
128  void OnNoiseFloorSlider(wxCommandEvent & evt);
129  void OnParam1Text(wxCommandEvent & evt);
130  void OnParam1Slider(wxCommandEvent & evt);
131  void OnParam2Text(wxCommandEvent & evt);
132  void OnParam2Slider(wxCommandEvent & evt);
133  void OnRepeatsText(wxCommandEvent & evt);
134  void OnRepeatsSlider(wxCommandEvent & evt);
135  void UpdateUI();
136  void UpdateControl(control id, bool enable, wxString name);
137  void UpdateControlText(wxTextCtrl *textCtrl, wxString &string, bool enabled);
138 
139  void MakeTable();
140  float WaveShaper(float sample);
141  float DCFilter(EffectDistortionState & data, float sample);
142 
143  // Preset tables for gain lookup
144 
145  void HardClip(); // hard clipping
146  void SoftClip(); // soft clipping
147  void ExponentialTable(); // exponential mapping
148  void LogarithmicTable(); // logarithmic mapping
149  void HalfSinTable();
150  void CubicTable();
151  void EvenHarmonicTable();
152  void SineTable();
153  void Leveller(); // 'Leveller' wavetable is modeled on the legacy effect of the same name.
154  void Rectifier(); // 0% = Dry, 50% = half-wave rectified, 100% = full-wave rectified (abs value).
155  void HardLimiter(); // Same effect as the LADSPA "hardLimiter 1413"
156 
157  // Wavetable helper functions
158 
159  void CopyHalfTable(); // for symmetric tables
160 
161  // Used by Soft Clipping but could be used for other tables.
162  // Log curve formula: y = T + (((e^(RT - Rx)) - 1) / -R)
163  // where R is the ratio, T is the threshold, and x is from T to 1.
164  inline float LogCurve(double threshold, float value, double ratio);
165 
166  // Used by Cubic curve but could be used for other tables
167  // Cubic formula: y = x - (x^3 / 3.0)
168  inline double Cubic(double x);
169 
170 
171 private:
173  std::vector<EffectDistortionState> mSlaves;
174 
175  double mTable[TABLESIZE];
176  double mThreshold;
178 
179  // mMakeupGain is used by some distortion types to pass the
180  // amount of gain required to bring overall effect gain to unity
181  double mMakeupGain;
182 
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:857
wxTextCtrl * mNoiseFloorT
Definition: Distortion.h:187
Derived from ShuttleGuiBase, an Audacity specific class for shuttling data to and from GUI...
Definition: ShuttleGui.h:409
void OnThresholdSlider(wxCommandEvent &evt)
Definition: Distortion.cpp:630
wxArrayString GetFactoryPresets() override
Definition: Distortion.cpp:329
bool GetAutomationParameters(CommandParameters &parms) override
Definition: Distortion.cpp:294
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:534
bool mbSavedFilterState
Definition: Distortion.h:177
double Cubic(double x)
void OnParam1Text(wxCommandEvent &evt)
Definition: Distortion.cpp:651
EffectType GetType() override
Definition: Distortion.cpp:210
bool RealtimeInitialize() override
Definition: Distortion.cpp:247
IdentInterfaceSymbol GetSymbol() override
Definition: Distortion.cpp:193
Base class for many of the effects in Audacity.
Definition: Effect.h:62
wxString GetDescription() override
Definition: Distortion.cpp:198
Shuttle that deals with parameters. This is a base class with lots of virtual functions that do nothi...
Definition: Shuttle.h:60
void OnRepeatsSlider(wxCommandEvent &evt)
Definition: Distortion.cpp:681
void OnTypeChoice(wxCommandEvent &evt)
Definition: Distortion.cpp:609
#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:669
float LogCurve(double threshold, float value, double ratio)
wxTextCtrl * mThresholdT
Definition: Distortion.h:186
void OnDCBlockCheckbox(wxCommandEvent &evt)
Definition: Distortion.cpp:616
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
CommandParameters, derived from wxFileConfig, is essentially doing the same things as the Shuttle cla...
wxSlider * mThresholdS
Definition: Distortion.h:192
double mTable[TABLESIZE]
Definition: Distortion.h:175
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:510
float DCFilter(EffectDistortionState &data, float sample)
IdentInterfaceSymbol pairs a persistent string identifier used internally with an optional...
std::vector< EffectDistortionState > mSlaves
Definition: Distortion.h:173
wxString mOldParam2Txt
Definition: Distortion.h:209
enum ChannelName * ChannelNames
bool LoadFactoryPreset(int id) override
Definition: Distortion.cpp:341
float WaveShaper(float sample)
EffectDistortionState mMaster
Definition: Distortion.h:172
bool DefineParams(ShuttleParams &S) override
Definition: Distortion.cpp:282
wxStaticText * mRepeatsTxt
Definition: Distortion.h:204
double mMakeupGain
Definition: Distortion.h:181
void OnNoiseFloorText(wxCommandEvent &evt)
Definition: Distortion.cpp:638
void OnNoiseFloorSlider(wxCommandEvent &evt)
Definition: Distortion.cpp:644
EffectType
wxStaticText * mThresholdTxt
Definition: Distortion.h:200
void OnRepeatsText(wxCommandEvent &evt)
Definition: Distortion.cpp:675
const wxChar * name
Definition: Distortion.cpp:94
wxSlider * mNoiseFloorS
Definition: Distortion.h:193
void OnParam2Text(wxCommandEvent &evt)
Definition: Distortion.cpp:663
bool SetAutomationParameters(CommandParameters &parms) override
Definition: Distortion.cpp:308
bool TransferDataToWindow() override
Definition: Distortion.cpp:477
size_t RealtimeProcess(int group, float **inbuf, float **outbuf, size_t numSamples) override
Definition: Distortion.cpp:274
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:362
wxString mOldmNoiseFloorTxt
Definition: Distortion.h:207
wxTextCtrl * mRepeatsT
Definition: Distortion.h:190
void UpdateControlText(wxTextCtrl *textCtrl, wxString &string, bool enabled)
Definition: Distortion.cpp:937
void OnThresholdText(wxCommandEvent &evt)
Definition: Distortion.cpp:623
bool TransferDataFromWindow() override
Definition: Distortion.cpp:498
wxString mOldThresholdTxt
Definition: Distortion.h:206
wxString mOldParam1Txt
Definition: Distortion.h:208
void OnParam1Slider(wxCommandEvent &evt)
Definition: Distortion.cpp:657