Audacity 3.2.0
AmplifyBase.cpp
Go to the documentation of this file.
1#include "AmplifyBase.h"
3#include "ShuttleAutomation.h"
4#include "TimeStretching.h"
6#include "WaveTrack.h"
7
9{
10 static CapturedParameters<
12 // Interactive case
14 parameters;
15
16 static CapturedParameters<AmplifyBase, Ratio> batchParameters {
17 // If invoking Amplify from a macro, mCanClip is not a parameter
18 // but is always true
19 [](AmplifyBase&, EffectSettings&, AmplifyBase& e, bool) {
20 e.mCanClip = true;
21 return true;
22 },
23 };
24
25 // Parameters differ depending on batch mode. Option to disable clipping
26 // is interactive only.
28 return batchParameters;
29 else
30 return parameters;
31}
32
33//
34// AmplifyBase
35//
36
38
40{
41 // In case the dialog is cancelled before effect processing
42 static_cast<AmplifyBase&>(GetEffect()).DestroyOutputTracks();
43}
44
46{
47 mAmp = Amp.def;
48 // Ratio.def == DB_TO_LINEAR(Amp.def)
49 Parameters().Reset(*this);
50 mRatioClip = 0.0;
51 mPeak = 0.0;
52
54}
55
57{
58}
59
61{
62 return Symbol;
63}
64
66{
67 return EffectTypeProcess;
68}
69
71{
72 return 1;
73}
74
76{
77 return 1;
78}
79
81 EffectSettings&, const float* const* inBlock, float* const* outBlock,
82 size_t blockLen)
83{
84 for (decltype(blockLen) i = 0; i < blockLen; i++)
85 {
86 outBlock[0][i] = inBlock[0][i] * mRatio;
87 }
88
89 return blockLen;
90}
91
93{
94 // To do: externalize state so const_cast isn't needed
95 return const_cast<AmplifyBase&>(*this).DoLoadFactoryDefaults(settings);
96}
97
99{
100 Init();
101
102 mRatioClip = 0.0;
103 if (mPeak > 0.0)
104 {
105 mRatio = 1.0 / mPeak;
107 }
108 else
109 {
110 mRatio = 1.0;
111 }
112 mCanClip = false;
113
114 ClampRatio();
115 return { nullptr };
116}
117
118// Effect implementation
119
121{
122 auto range = inputTracks()->Selected<const WaveTrack>();
123 bool hasPitchOrSpeed =
124 any_of(begin(range), end(range), [this](auto* pTrack) {
125 return TimeStretching::HasPitchOrSpeed(*pTrack, mT0, mT1);
126 });
127 if (hasPitchOrSpeed)
128 range = MakeOutputTracks()->Get().Selected<const WaveTrack>();
129 mPeak = 0.0;
130 for (auto t : range)
131 {
132 for (const auto pChannel : t->Channels())
133 {
134 auto pair =
135 WaveChannelUtilities::GetMinMax(*pChannel, mT0, mT1); // may throw
136 const float min = pair.first, max = pair.second;
137 const float newpeak = std::max(fabs(min), fabs(max));
138 mPeak = std::max<double>(mPeak, newpeak);
139 }
140 }
141 return true;
142}
143
145{
146 return { std::pair { CopyableValueRestorer(mRatio),
148}
149
151{
152 // limit range of gain
153 double dBInit = LINEAR_TO_DB(mRatio);
154 double dB = std::clamp<double>(dBInit, Amp.min, Amp.max);
155 if (dB != dBInit)
156 mRatio = DB_TO_LINEAR(dB);
157
160}
int min(int a, int b)
EffectType
@ EffectTypeProcess
std::optional< std::unique_ptr< EffectSettingsAccess::Message > > OptionalMessage
XO("Cut/Copy/Paste")
#define LINEAR_TO_DB(x)
Definition: MemoryX.h:339
#define DB_TO_LINEAR(x)
Definition: MemoryX.h:338
static Settings & settings()
Definition: TrackInfo.cpp:51
bool Init() override
OptionalMessage LoadFactoryDefaults(EffectSettings &settings) const override
Definition: AmplifyBase.cpp:92
double mNewPeak
Definition: AmplifyBase.h:55
unsigned GetAudioInCount() const override
How many input buffers to allocate at once.
Definition: AmplifyBase.cpp:70
const EffectParameterMethods & Parameters() const override
Definition: AmplifyBase.cpp:8
size_t ProcessBlock(EffectSettings &settings, const float *const *inBlock, float *const *outBlock, size_t blockLen) override
Called for destructive effect computation.
Definition: AmplifyBase.cpp:80
std::any BeginPreview(const EffectSettings &settings) override
Called when Preview() starts, to allow temporary effect state changes.
static const ComponentInterfaceSymbol Symbol
Definition: AmplifyBase.h:13
static constexpr EffectParameter Ratio
Definition: AmplifyBase.h:62
EffectType GetType() const override
Type determines how it behaves.
Definition: AmplifyBase.cpp:65
OptionalMessage DoLoadFactoryDefaults(EffectSettings &settings)
Definition: AmplifyBase.cpp:98
double mRatio
Definition: AmplifyBase.h:51
unsigned GetAudioOutCount() const override
How many output buffers to allocate at once.
Definition: AmplifyBase.cpp:75
double mPeak
Definition: AmplifyBase.h:49
void ClampRatio()
double mAmp
Definition: AmplifyBase.h:54
double mRatioClip
Definition: AmplifyBase.h:52
virtual ~AmplifyBase() override
Definition: AmplifyBase.cpp:56
static constexpr EffectParameter Amp
Definition: AmplifyBase.h:66
bool mCanClip
Definition: AmplifyBase.h:56
ComponentInterfaceSymbol GetSymbol() const override
Definition: AmplifyBase.cpp:60
static constexpr EffectParameter Clipping
Definition: AmplifyBase.h:69
Generates EffectParameterMethods overrides from variadic template arguments.
ComponentInterfaceSymbol pairs a persistent string identifier used internally with an optional,...
double mT1
Definition: EffectBase.h:123
void SetLinearEffectFlag(bool linearEffectFlag)
Definition: EffectBase.cpp:210
const TrackList * inputTracks() const
Definition: EffectBase.h:102
double mT0
Definition: EffectBase.h:122
bool IsBatchProcessing() const override
Definition: Effect.cpp:295
Interface for manipulations of an Effect's settings.
virtual void Reset(Effect &effect) const =0
void DestroyOutputTracks() const
std::shared_ptr< EffectOutputTracks > MakeOutputTracks()
StatefulPerTrackEffect & GetEffect() const
auto Selected() -> TrackIterRange< TrackType >
Definition: Track.h:967
A Track that contains audio waveform data.
Definition: WaveTrack.h:203
WAVE_TRACK_API bool HasPitchOrSpeed(const WaveTrack &track, double t0, double t1)
WAVE_TRACK_API std::pair< float, float > GetMinMax(const WaveChannel &channel, double t0, double t1, bool mayThrow=true)
const char * end(const char *str) noexcept
Definition: StringUtils.h:106
const char * begin(const char *str) noexcept
Definition: StringUtils.h:101
const Type def
Default value.
const Type min
Minimum value.
const Type max
Maximum value.
Externalized state of a plug-in.