Audacity 3.2.0
BassTreble.cpp
Go to the documentation of this file.
1/**********************************************************************
2
3 Audacity: A Digital Audio Editor
4 Audacity(R) is copyright (c) 1999-2016 Audacity Team.
5 License: GPL v2 or later. See License.txt.
6
7 BassTreble.cpp
8 Steve Daulton
9
10******************************************************************//*******************************************************************/
16#include "BassTreble.h"
17#include "EffectEditor.h"
18#include "LoadEffects.h"
19
20#include <math.h>
21#include <algorithm>
22
23#include <wx/checkbox.h>
24#include <wx/panel.h>
25#include <wx/slider.h>
26#include <wx/weakref.h>
27
28#include "Prefs.h"
29#include "ShuttleGui.h"
30#include "WaveTrack.h"
31#include "../widgets/valnum.h"
32
34
37{
38 Editor(const EffectUIServices& services,
40 ) : EffectEditor{ services, access }
42 {}
43 virtual ~Editor() = default;
44
45 bool ValidateUI() override;
46 bool UpdateUI() override;
47
49
50 wxWeakRef<wxWindow> mUIParent{};
52
53 wxSlider* mBassS;
54 wxSlider* mTrebleS;
55 wxSlider* mGainS;
56
57 wxTextCtrl* mBassT;
58 wxTextCtrl* mTrebleT;
59 wxTextCtrl* mGainT;
60
61 wxCheckBox* mLinkCheckBox;
62
63 void OnBassText(wxCommandEvent& evt);
64 void OnTrebleText(wxCommandEvent& evt);
65 void OnGainText(wxCommandEvent& evt);
66 void OnBassSlider(wxCommandEvent& evt);
67 void OnTrebleSlider(wxCommandEvent& evt);
68 void OnGainSlider(wxCommandEvent& evt);
69 void OnLinkCheckbox(wxCommandEvent& evt);
70
71 // Auto-adjust gain to reduce variation in peak level
72 void UpdateGain(double oldVal, int control);
73
75 {
76 EnableApply(mUIParent, mUIParent->Validate());
77 }
78
80 {
81 return EnableApply(
82 mUIParent, mUIParent->TransferDataFromWindow());
83 }
84};
85
86std::shared_ptr<EffectInstance>
88{
89 return std::make_shared<BassTrebleBase::Instance>(*this);
90}
91
92// Effect implementation
93
94std::unique_ptr<EffectEditor> EffectBassTreble::MakeEditor(
96 const EffectOutputs *) const
97{
98 auto& settings = access.Get();
99 auto& myEffSettings = GetSettings(settings);
100
101 auto result = std::make_unique<Editor>(*this, access, myEffSettings);
102 result->PopulateOrExchange(S);
103 return result;
104 }
105
107{
108 mUIParent = S.GetParent();
109 auto& ms = mSettings;
110
111 S.SetBorder(5);
112 S.AddSpace(0, 5);
113
114 S.StartStatic(XO("Tone controls"));
115 {
116 S.StartMultiColumn(3, wxEXPAND);
117 {
118 S.SetStretchyCol(2);
119
120 // Bass control
121 mBassT = S
122 .Name(XO("Bass (dB):"))
123 .Validator<FloatingPointValidator<double>>(
124 1, &ms.mBass, NumValidatorStyle::DEFAULT, Bass.min, Bass.max)
125 .AddTextBox(XXO("Ba&ss (dB):"), L"", 10);
126 BindTo(*mBassT, wxEVT_TEXT, &Editor::OnBassText);
127
128 mBassS = S
129 .Name(XO("Bass"))
130 .Style(wxSL_HORIZONTAL)
131 .AddSlider( {}, 0, Bass.max * Bass.scale, Bass.min * Bass.scale);
132 BindTo(*mBassS, wxEVT_SLIDER, &Editor::OnBassSlider);
133
134 // Treble control
135 mTrebleT = S
136 .Validator<FloatingPointValidator<double>>(
137 1, &ms.mTreble, NumValidatorStyle::DEFAULT, Treble.min, Treble.max)
138 .AddTextBox(XXO("&Treble (dB):"), L"", 10);
139 BindTo(*mTrebleT, wxEVT_TEXT, &Editor::OnTrebleText);
140
141 mTrebleS = S
142 .Name(XO("Treble"))
143 .Style(wxSL_HORIZONTAL)
144 .AddSlider( {}, 0, Treble.max * Treble.scale, Treble.min * Treble.scale);
145 BindTo(*mTrebleS, wxEVT_SLIDER, &Editor::OnTrebleSlider);
146 }
147 S.EndMultiColumn();
148 }
149 S.EndStatic();
150
151 S.StartStatic(XO("Output"));
152 {
153 S.StartMultiColumn(3, wxEXPAND);
154 {
155 S.SetStretchyCol(2);
156
157 // Gain control
158 mGainT = S
159 .Validator<FloatingPointValidator<double>>(
160 1, &ms.mGain, NumValidatorStyle::DEFAULT, Gain.min, Gain.max)
161 .AddTextBox(XXO("&Volume (dB):"), L"", 10);
162 BindTo(*mGainT, wxEVT_TEXT, &Editor::OnGainText);
163
164 mGainS = S
165 .Name(XO("Level"))
166 .Style(wxSL_HORIZONTAL)
167 .AddSlider( {}, 0, Gain.max * Gain.scale, Gain.min * Gain.scale);
168 BindTo(*mGainS, wxEVT_SLIDER, &Editor::OnGainSlider);
169 }
170 S.EndMultiColumn();
171
172 S.StartMultiColumn(2, wxCENTER);
173 {
174 // Link checkbox
176 S
177 .AddCheckBox(XXO("&Link Volume control to Tone controls"),
178 Link.def);
179 BindTo(*mLinkCheckBox, wxEVT_CHECKBOX, &Editor::OnLinkCheckbox);
180 }
181 S.EndMultiColumn();
182 }
183 S.EndStatic();
184}
185
187{
188 // get the settings from the MessageBuffer and write them to our local copy
189 const auto& settings = mAccess.Get();
190
191 mSettings = GetSettings(settings);
192
193 if (! mUIParent->TransferDataToWindow())
194 {
195 return false;
196 }
197
198 mBassS-> SetValue((int)(mSettings.mBass * Bass.scale));
199 mTrebleS-> SetValue((int)(mSettings.mTreble *Treble.scale));
200 mGainS-> SetValue((int)(mSettings.mGain * Gain.scale));
201 mLinkCheckBox->SetValue(mSettings.mLink);
202
203 return true;
204}
205
206void EffectBassTreble::Editor::OnBassText(wxCommandEvent & WXUNUSED(evt))
207{
208 auto& ms = mSettings;
209
210 double oldBass = ms.mBass;
211
212 if (!EnableApplyFromTransferDataFromWindow())
213 {
214 return;
215 }
216
217 if (ms.mLink)
218 UpdateGain(oldBass, kBass);
219
220 mBassS->SetValue((int) (ms.mBass * Bass.scale));
221
222 ValidateUI();
223 Publish(EffectSettingChanged{});
224}
225
226void EffectBassTreble::Editor::OnTrebleText(wxCommandEvent & WXUNUSED(evt))
227{
228 auto& ms = mSettings;
229
230 double oldTreble = ms.mTreble;
231
232 if (!EnableApplyFromTransferDataFromWindow())
233 {
234 return;
235 }
236
237 if (ms.mLink)
238 UpdateGain(oldTreble, kTreble);
239
240 mTrebleS->SetValue((int) (ms.mTreble * Treble.scale));
241
242 ValidateUI();
243 Publish(EffectSettingChanged{});
244}
245
246void EffectBassTreble::Editor::OnGainText(wxCommandEvent & WXUNUSED(evt))
247{
248 auto& ms = mSettings;
249
250 if (!EnableApplyFromTransferDataFromWindow())
251 {
252 return;
253 }
254
255 mGainS->SetValue((int) (ms.mGain * Gain.scale));
256
257 ValidateUI();
258 Publish(EffectSettingChanged{});
259}
260
262{
263 auto& ms = mSettings;
264
265 double oldBass = ms.mBass;
266 ms.mBass = (double) evt.GetInt() / Bass.scale;
267 mBassT->GetValidator()->TransferToWindow();
268
269 if (ms.mLink)
270 UpdateGain(oldBass, kBass);
271
272 EnableApplyFromValidate();
273
274 ValidateUI();
275 Publish(EffectSettingChanged{});
276}
277
279{
280 auto& ms = mSettings;
281
282 double oldTreble = ms.mTreble;
283 ms.mTreble = (double) evt.GetInt() / Treble.scale;
284 mTrebleT->GetValidator()->TransferToWindow();
285
286 if (ms.mLink)
287 UpdateGain(oldTreble, kTreble);
288
289 EnableApplyFromValidate();
290
291 ValidateUI();
292 Publish(EffectSettingChanged{});
293}
294
296{
297 auto& ms = mSettings;
298
299 ms.mGain = (double) evt.GetInt() / Gain.scale;
300 mGainT->GetValidator()->TransferToWindow();
301
302 EnableApplyFromValidate();
303
304 ValidateUI();
305 Publish(EffectSettingChanged{});
306}
307
308void EffectBassTreble::Editor::OnLinkCheckbox(wxCommandEvent& /*evt*/)
309{
310 auto& ms = mSettings;
311
312 ms.mLink = mLinkCheckBox->GetValue();
313
314 ValidateUI();
315 Publish(EffectSettingChanged{});
316}
317
318void EffectBassTreble::Editor::UpdateGain(double oldVal, int control)
319{
320 auto& ms = mSettings;
321
322 double newVal;
323 oldVal = (oldVal > 0)? oldVal / 2.0 : oldVal / 4.0;
324
325 if (control == kBass)
326 newVal = (ms.mBass > 0)? ms.mBass / 2.0 : ms.mBass / 4.0;
327 else
328 newVal = (ms.mTreble > 0)? ms.mTreble / 2.0 : ms.mTreble / 4.0;
329
330 ms.mGain -= newVal - oldVal;
331 ms.mGain = std::min(Gain.max, std::max(Gain.min, ms.mGain));
332
333 mGainS->SetValue(ms.mGain);
334 mGainT->GetValidator()->TransferToWindow();
335
336}
337
339{
340 // This bit was copied from the original override of the effect's TransferDataFromWindow
341 if (! mUIParent->Validate() || !mUIParent->TransferDataFromWindow())
342 {
343 return false;
344 }
345
346
347 mAccess.ModifySettings
348 (
349 [this](EffectSettings& settings)
350 {
351 // pass back the modified settings to the MessageBuffer
352 //
354
355 return nullptr;
356 }
357 );
358
359 return true;
360}
361
@ kBass
@ kTreble
int min(int a, int b)
XO("Cut/Copy/Paste")
XXO("&Cut/Copy/Paste Toolbar")
#define S(N)
Definition: ToChars.cpp:64
static Settings & settings()
Definition: TrackInfo.cpp:51
bool ValidateUI(const EffectPlugin &context, EffectSettings &) const override
static constexpr EffectParameter Treble
static constexpr EffectParameter Bass
static constexpr EffectParameter Link
static constexpr EffectParameter Gain
std::unique_ptr< EffectEditor > MakeEditor(ShuttleGui &S, EffectInstance &instance, EffectSettingsAccess &access, const EffectOutputs *pOutputs) const override
Called only from PopulateUI, to add controls to effect panel.
Definition: BassTreble.cpp:94
std::shared_ptr< EffectInstance > MakeInstance() const override
Make an object maintaining short-term state of an Effect.
Definition: BassTreble.cpp:87
static bool EnableApply(wxWindow *parent, bool enable=true)
Enable or disable the Apply button of the dialog that contains parent.
void BindTo(wxEvtHandler &src, const EventTag &eventType, void(Class::*pmf)(Event &))
Definition: EffectEditor.h:85
Performs effect computation.
Hold values to send to effect output meters.
virtual const EffectSettings & Get()=0
static BassTrebleSettings & GetSettings(EffectSettings &settings)
Assume settings originated from MakeSettings() and copies thereof.
Definition: Effect.h:166
Derived from ShuttleGuiBase, an Audacity specific class for shuttling data to and from GUI.
Definition: ShuttleGui.h:640
BuiltinEffectsModule::Registration< EffectBassTreble > reg
Definition: BassTreble.cpp:33
void OnTrebleText(wxCommandEvent &evt)
Definition: BassTreble.cpp:226
void PopulateOrExchange(ShuttleGui &S)
Definition: BassTreble.cpp:106
void OnLinkCheckbox(wxCommandEvent &evt)
Definition: BassTreble.cpp:308
wxWeakRef< wxWindow > mUIParent
Definition: BassTreble.cpp:50
BassTrebleSettings mSettings
Definition: BassTreble.cpp:51
void OnGainText(wxCommandEvent &evt)
Definition: BassTreble.cpp:246
void OnBassSlider(wxCommandEvent &evt)
Definition: BassTreble.cpp:261
Editor(const EffectUIServices &services, EffectSettingsAccess &access, const BassTrebleSettings &settings)
Definition: BassTreble.cpp:38
wxCheckBox * mLinkCheckBox
Definition: BassTreble.cpp:61
bool EnableApplyFromTransferDataFromWindow()
Definition: BassTreble.cpp:79
void UpdateGain(double oldVal, int control)
Definition: BassTreble.cpp:318
void OnBassText(wxCommandEvent &evt)
Definition: BassTreble.cpp:206
bool ValidateUI() override
Get settings data from the panel; may make error dialogs and return false.
Definition: BassTreble.cpp:338
void OnTrebleSlider(wxCommandEvent &evt)
Definition: BassTreble.cpp:278
bool UpdateUI() override
Update appearance of the panel for changes in settings.
Definition: BassTreble.cpp:186
void OnGainSlider(wxCommandEvent &evt)
Definition: BassTreble.cpp:295
virtual ~Editor()=default
const Type scale
Scaling factor, for slider control.
const Type def
Default value.
const Type min
Minimum value.
const Type max
Maximum value.
Message sent by EffectEditor when a setting is changed by the user.
Definition: EffectEditor.h:26
Externalized state of a plug-in.