Audacity  3.0.3
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 <queue>
15 
16 #include "Effect.h"
17 
18 class wxSlider;
19 class wxStaticText;
20 class wxCheckBox;
21 class wxTextCtrl;
22 class ShuttleGui;
23 
24 #define STEPS 1024 // number of +ve or -ve steps in lookup tabe
25 #define TABLESIZE 2049 // size of lookup table (steps * 2 + 1)
26 
28 {
29 public:
30  float samplerate;
33  bool dcblock;
34  double threshold;
35  double noisefloor;
36  double param1;
37  double param2;
38  int repeats;
39 
40  // DC block filter variables
41  std::queue<float> queuesamples;
42  double queuetotal;
43 };
44 
45 class EffectDistortion final : public Effect
46 {
47 public:
49 
51  virtual ~EffectDistortion();
52 
53  struct Params
54  {
56  bool mDCBlock;
57  double mThreshold_dB;
58  double mNoiseFloor;
59  double mParam1;
60  double mParam2;
61  int mRepeats;
62  };
63 
64  // ComponentInterface implementation
65 
68  ManualPageID ManualPage() override;
69 
70  // EffectDefinitionInterface implementation
71 
72  EffectType GetType() override;
73  bool SupportsRealtime() override;
74 
75  // EffectClientInterface implementation
76 
77  unsigned GetAudioInCount() override;
78  unsigned GetAudioOutCount() override;
79  bool ProcessInitialize(sampleCount totalLen, ChannelNames chanMap = NULL) override;
80  size_t ProcessBlock(float **inBlock, float **outBlock, size_t blockLen) override;
81  bool RealtimeInitialize() override;
82  bool RealtimeAddProcessor(unsigned numChannels, float sampleRate) override;
83  bool RealtimeFinalize() override;
84  size_t RealtimeProcess(int group,
85  float **inbuf,
86  float **outbuf,
87  size_t numSamples) override;
88  bool DefineParams( ShuttleParams & S ) override;
89  bool GetAutomationParameters(CommandParameters & parms) override;
90  bool SetAutomationParameters(CommandParameters & parms) 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, TranslatableString 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 
184  wxChoice *mTypeChoiceCtrl;
185  wxTextCtrl *mThresholdT;
186  wxTextCtrl *mNoiseFloorT;
187  wxTextCtrl *mParam1T;
188  wxTextCtrl *mParam2T;
189  wxTextCtrl *mRepeatsT;
190 
191  wxSlider *mThresholdS;
192  wxSlider *mNoiseFloorS;
193  wxSlider *mParam1S;
194  wxSlider *mParam2S;
195  wxSlider *mRepeatsS;
196 
197  wxCheckBox *mDCBlockCheckBox;
198 
199  wxStaticText *mThresholdTxt;
200  wxStaticText *mNoiseFloorTxt;
201  wxStaticText *mParam1Txt;
202  wxStaticText *mParam2Txt;
203  wxStaticText *mRepeatsTxt;
204 
207  wxString mOldParam1Txt;
208  wxString mOldParam2Txt;
209  wxString mOldRepeatsTxt;
210 
212 
213  DECLARE_EVENT_TABLE()
214 };
215 
216 #endif
EffectDistortion::ProcessBlock
size_t ProcessBlock(float **inBlock, float **outBlock, size_t blockLen) override
Definition: Distortion.cpp:240
TranslatableString
Holds a msgid for the translation catalog; may also bind format arguments.
Definition: TranslatableString.h:32
EffectDistortion::SetAutomationParameters
bool SetAutomationParameters(CommandParameters &parms) override
Definition: Distortion.cpp:306
CommandParameters
CommandParameters, derived from wxFileConfig, is essentially doing the same things as the Shuttle cla...
Definition: EffectAutomationParameters.h:67
EffectDistortion::mThresholdS
wxSlider * mThresholdS
Definition: Distortion.h:191
EffectDistortion::Params::mParam1
double mParam1
Definition: Distortion.h:59
EffectDistortion::mParam2S
wxSlider * mParam2S
Definition: Distortion.h:194
EffectDistortion::mMaster
EffectDistortionState mMaster
Definition: Distortion.h:171
EffectDistortion::mParam1Txt
wxStaticText * mParam1Txt
Definition: Distortion.h:201
EffectDistortion::Params::mDCBlock
bool mDCBlock
Definition: Distortion.h:56
EffectDistortion::EffectDistortion
EffectDistortion()
Definition: Distortion.cpp:167
EffectDistortion::OnParam1Text
void OnParam1Text(wxCommandEvent &evt)
Definition: Distortion.cpp:669
EffectDistortion::CubicTable
void CubicTable()
Definition: Distortion.cpp:1120
Effect.h
EffectDistortion::mDCBlockCheckBox
wxCheckBox * mDCBlockCheckBox
Definition: Distortion.h:197
EffectDistortion::LogCurve
float LogCurve(double threshold, float value, double ratio)
Definition: Distortion.cpp:1063
EffectDistortion::UpdateUI
void UpdateUI()
Definition: Distortion.cpp:706
EffectDistortion::Rectifier
void Rectifier()
Definition: Distortion.cpp:1246
Effect
Base class for many of the effects in Audacity.
Definition: Effect.h:72
EffectDistortionState::samplerate
float samplerate
Definition: Distortion.h:30
EffectDistortion::Params::mNoiseFloor
double mNoiseFloor
Definition: Distortion.h:58
EffectDistortionState::param2
double param2
Definition: Distortion.h:37
RegistryPaths
std::vector< RegistryPath > RegistryPaths
Definition: Identifier.h:219
EffectDistortion::mThresholdTxt
wxStaticText * mThresholdTxt
Definition: Distortion.h:199
EffectDistortion::GetFactoryPresets
RegistryPaths GetFactoryPresets() override
Definition: Distortion.cpp:327
EffectDistortionState::queuetotal
double queuetotal
Definition: Distortion.h:42
EffectDistortion::mRepeatsTxt
wxStaticText * mRepeatsTxt
Definition: Distortion.h:203
EffectDistortion::mTable
double mTable[TABLESIZE]
Definition: Distortion.h:174
EffectDistortion::OnRepeatsText
void OnRepeatsText(wxCommandEvent &evt)
Definition: Distortion.cpp:693
EffectDistortion::ID_Repeats
@ ID_Repeats
Definition: Distortion.h:110
EffectDistortion::RealtimeInitialize
bool RealtimeInitialize() override
Definition: Distortion.cpp:245
EffectDistortion::ManualPage
ManualPageID ManualPage() override
Definition: Distortion.cpp:201
EffectDistortion::CopyHalfTable
void CopyHalfTable()
Definition: Distortion.cpp:1277
EffectDistortion::HalfSinTable
void HalfSinTable()
Definition: Distortion.cpp:1102
EffectDistortion::mbSavedFilterState
bool mbSavedFilterState
Definition: Distortion.h:176
ShuttleParams
Shuttle that deals with parameters. This is a base class with lots of virtual functions that do nothi...
Definition: Shuttle.h:62
EffectDistortion::InstanceInit
void InstanceInit(EffectDistortionState &data, float sampleRate)
Definition: Distortion.cpp:528
EffectDistortion::mNoiseFloorS
wxSlider * mNoiseFloorS
Definition: Distortion.h:192
EffectDistortion::ID_Type
@ ID_Type
Definition: Distortion.h:104
EffectDistortion::mThresholdT
wxTextCtrl * mThresholdT
Definition: Distortion.h:185
ComponentInterfaceSymbol
ComponentInterfaceSymbol pairs a persistent string identifier used internally with an optional,...
Definition: ComponentInterfaceSymbol.h:27
EffectDistortion::PopulateOrExchange
void PopulateOrExchange(ShuttleGui &S) override
Definition: Distortion.cpp:360
EffectDistortionState::param1
double param1
Definition: Distortion.h:36
EffectDistortion::RealtimeAddProcessor
bool RealtimeAddProcessor(unsigned numChannels, float sampleRate) override
Definition: Distortion.cpp:254
EffectDistortion::mSlaves
std::vector< EffectDistortionState > mSlaves
Definition: Distortion.h:172
EffectDistortion::GetSymbol
ComponentInterfaceSymbol GetSymbol() override
Definition: Distortion.cpp:191
EffectDistortion::control
control
Definition: Distortion.h:103
EffectDistortion::mOldRepeatsTxt
wxString mOldRepeatsTxt
Definition: Distortion.h:209
EffectDistortion::OnRepeatsSlider
void OnRepeatsSlider(wxCommandEvent &evt)
Definition: Distortion.cpp:699
EffectDistortion::mRepeatsS
wxSlider * mRepeatsS
Definition: Distortion.h:195
EffectDistortion::mThreshold
double mThreshold
Definition: Distortion.h:175
EffectDistortion::LoadFactoryPreset
bool LoadFactoryPreset(int id) override
Definition: Distortion.cpp:339
EffectDistortionState
Definition: Distortion.h:28
EffectDistortion::ID_NoiseFloor
@ ID_NoiseFloor
Definition: Distortion.h:107
EffectDistortion::GetDescription
TranslatableString GetDescription() override
Definition: Distortion.cpp:196
EffectDistortion::Cubic
double Cubic(double x)
Definition: Distortion.cpp:1146
EffectDistortion::mParams
Params mParams
Definition: Distortion.h:211
EffectDistortion::~EffectDistortion
virtual ~EffectDistortion()
Definition: Distortion.cpp:185
EffectDistortion::Params::mTableChoiceIndx
int mTableChoiceIndx
Definition: Distortion.h:55
EffectDistortion::SineTable
void SineTable()
Definition: Distortion.cpp:1171
EffectDistortion::Params
Definition: Distortion.h:54
EffectDistortion::ExponentialTable
void ExponentialTable()
Definition: Distortion.cpp:1068
EffectDistortion::mOldThresholdTxt
wxString mOldThresholdTxt
Definition: Distortion.h:205
EffectDistortion::TransferDataToWindow
bool TransferDataToWindow() override
Definition: Distortion.cpp:495
EffectDistortion::GetType
EffectType GetType() override
Definition: Distortion.cpp:208
name
const TranslatableString name
Definition: Distortion.cpp:98
EffectDistortion::MakeTable
void MakeTable()
Definition: Distortion.cpp:983
EffectDistortion::OnParam2Text
void OnParam2Text(wxCommandEvent &evt)
Definition: Distortion.cpp:681
EffectDistortion::ID_Threshold
@ ID_Threshold
Definition: Distortion.h:106
EffectDistortion::mMakeupGain
double mMakeupGain
Definition: Distortion.h:180
EffectDistortion::Params::mThreshold_dB
double mThreshold_dB
Definition: Distortion.h:57
EffectDistortion::LogarithmicTable
void LogarithmicTable()
Definition: Distortion.cpp:1081
EffectDistortion::mOldParam2Txt
wxString mOldParam2Txt
Definition: Distortion.h:208
EffectDistortion::mTypChoiceIndex
int mTypChoiceIndex
Definition: Distortion.h:182
EffectDistortion::OnNoiseFloorText
void OnNoiseFloorText(wxCommandEvent &evt)
Definition: Distortion.cpp:656
EffectDistortion::HardClip
void HardClip()
Definition: Distortion.cpp:1028
EffectDistortion::OnDCBlockCheckbox
void OnDCBlockCheckbox(wxCommandEvent &evt)
Definition: Distortion.cpp:634
EffectDistortion::RealtimeFinalize
bool RealtimeFinalize() override
Definition: Distortion.cpp:265
EffectDistortion::OnParam2Slider
void OnParam2Slider(wxCommandEvent &evt)
Definition: Distortion.cpp:687
ChannelNames
enum ChannelName * ChannelNames
EffectDistortion::mNoiseFloorT
wxTextCtrl * mNoiseFloorT
Definition: Distortion.h:186
EffectDistortionState::noisefloor
double noisefloor
Definition: Distortion.h:35
EffectDistortion::UpdateControl
void UpdateControl(control id, bool enable, TranslatableString name)
Definition: Distortion.cpp:875
EffectDistortion::InstanceProcess
size_t InstanceProcess(EffectDistortionState &data, float **inBlock, float **outBlock, size_t blockLen)
Definition: Distortion.cpp:552
EffectDistortion::OnParam1Slider
void OnParam1Slider(wxCommandEvent &evt)
Definition: Distortion.cpp:675
EffectDistortionState::tablechoiceindx
int tablechoiceindx
Definition: Distortion.h:32
EffectDistortion::DCFilter
float DCFilter(EffectDistortionState &data, float sample)
Definition: Distortion.cpp:1318
EffectDistortionState::skipcount
sampleCount skipcount
Definition: Distortion.h:31
EffectDistortion::OnThresholdText
void OnThresholdText(wxCommandEvent &evt)
Definition: Distortion.cpp:641
TABLESIZE
#define TABLESIZE
Definition: Distortion.h:25
EffectDistortion::GetAudioInCount
unsigned GetAudioInCount() override
Definition: Distortion.cpp:224
EffectDistortion::mRepeatsT
wxTextCtrl * mRepeatsT
Definition: Distortion.h:189
EffectDistortion::mOldmNoiseFloorTxt
wxString mOldmNoiseFloorTxt
Definition: Distortion.h:206
EffectDistortion::SoftClip
void SoftClip()
Definition: Distortion.cpp:1045
EffectDistortionState::queuesamples
std::queue< float > queuesamples
Definition: Distortion.h:41
EffectDistortion::GetAudioOutCount
unsigned GetAudioOutCount() override
Definition: Distortion.cpp:229
TaggedIdentifier< ManualPageIDTag >
EffectDistortion::DefineParams
bool DefineParams(ShuttleParams &S) override
Definition: Distortion.cpp:280
EffectDistortion::ProcessInitialize
bool ProcessInitialize(sampleCount totalLen, ChannelNames chanMap=NULL) override
Definition: Distortion.cpp:234
EffectDistortion::OnTypeChoice
void OnTypeChoice(wxCommandEvent &evt)
Definition: Distortion.cpp:627
sampleCount
Positions or offsets within audio files need a wide type.
Definition: SampleCount.h:18
EffectDistortion::UpdateControlText
void UpdateControlText(wxTextCtrl *textCtrl, wxString &string, bool enabled)
Definition: Distortion.cpp:968
EffectDistortion::mParam2Txt
wxStaticText * mParam2Txt
Definition: Distortion.h:202
EffectDistortion::GetAutomationParameters
bool GetAutomationParameters(CommandParameters &parms) override
Definition: Distortion.cpp:292
EffectDistortionState::dcblock
bool dcblock
Definition: Distortion.h:33
EffectDistortion::ID_DCBlock
@ ID_DCBlock
Definition: Distortion.h:105
EffectDistortion::Leveller
void Leveller()
Definition: Distortion.cpp:1189
EffectDistortion::HardLimiter
void HardLimiter()
Definition: Distortion.cpp:1266
EffectDistortion::mTypeChoiceCtrl
wxChoice * mTypeChoiceCtrl
Definition: Distortion.h:184
EffectDistortion::ID_Param2
@ ID_Param2
Definition: Distortion.h:109
EffectDistortion::mOldParam1Txt
wxString mOldParam1Txt
Definition: Distortion.h:207
EffectDistortion::mNoiseFloorTxt
wxStaticText * mNoiseFloorTxt
Definition: Distortion.h:200
EffectDistortion
A WaveShaper distortion effect.
Definition: Distortion.h:46
EffectDistortion::OnNoiseFloorSlider
void OnNoiseFloorSlider(wxCommandEvent &evt)
Definition: Distortion.cpp:662
EffectDistortionState::repeats
int repeats
Definition: Distortion.h:38
EffectType
EffectType
Definition: EffectInterface.h:55
EffectDistortion::Params::mParam2
double mParam2
Definition: Distortion.h:60
EffectDistortion::mParam1S
wxSlider * mParam1S
Definition: Distortion.h:193
EffectDistortion::Symbol
static const ComponentInterfaceSymbol Symbol
Definition: Distortion.h:48
EffectDistortion::mParam1T
wxTextCtrl * mParam1T
Definition: Distortion.h:187
EffectDistortion::mParam2T
wxTextCtrl * mParam2T
Definition: Distortion.h:188
EffectDistortion::RealtimeProcess
size_t RealtimeProcess(int group, float **inbuf, float **outbuf, size_t numSamples) override
Definition: Distortion.cpp:272
EffectDistortion::SupportsRealtime
bool SupportsRealtime() override
Definition: Distortion.cpp:213
EffectDistortion::OnThresholdSlider
void OnThresholdSlider(wxCommandEvent &evt)
Definition: Distortion.cpp:648
EffectDistortion::Params::mRepeats
int mRepeats
Definition: Distortion.h:61
EffectDistortion::WaveShaper
float WaveShaper(float sample)
Definition: Distortion.cpp:1288
EffectDistortionState::threshold
double threshold
Definition: Distortion.h:34
ShuttleGui
Derived from ShuttleGuiBase, an Audacity specific class for shuttling data to and from GUI.
Definition: ShuttleGui.h:631
EffectDistortion::ID_Param1
@ ID_Param1
Definition: Distortion.h:108
EffectDistortion::TransferDataFromWindow
bool TransferDataFromWindow() override
Definition: Distortion.cpp:516
EffectDistortion::EvenHarmonicTable
void EvenHarmonicTable()
Definition: Distortion.cpp:1155