Audacity 3.2.0
Public Member Functions | Static Public Member Functions | Static Public Attributes | Protected Member Functions | Protected Attributes | Static Protected Attributes | Private Member Functions | List of all members
AutoDuckBase Class Reference

Implements the Auto Ducking effect. More...

#include <AutoDuckBase.h>

Inheritance diagram for AutoDuckBase:
[legend]
Collaboration diagram for AutoDuckBase:
[legend]

Public Member Functions

 AutoDuckBase ()
 
virtual ~AutoDuckBase ()
 
ComponentInterfaceSymbol GetSymbol () const override
 
TranslatableString GetDescription () const override
 
ManualPageID ManualPage () const override
 Name of a page in the Audacity alpha manual, default is empty. More...
 
EffectType GetType () const override
 Type determines how it behaves. More...
 
bool Init () override
 
bool Process (EffectInstance &instance, EffectSettings &settings) override
 
- Public Member Functions inherited from StatefulEffect
 ~StatefulEffect () override
 
std::shared_ptr< EffectInstanceMakeInstance () const override
 Make an object maintaining short-term state of an Effect. More...
 
- Public Member Functions inherited from StatefulEffectBase
virtual bool Init ()
 
virtual bool Process (EffectInstance &instance, EffectSettings &settings)=0
 
virtual bool RealtimeInitialize (EffectSettings &settings, double sampleRate)
 
virtual bool RealtimeAddProcessor (EffectSettings &settings, EffectOutputs *pOutputs, unsigned numChannels, float sampleRate)
 
virtual bool RealtimeSuspend ()
 
virtual bool RealtimeResume ()
 
virtual bool RealtimeProcessStart (MessagePackage &package)
 settings are possibly changed, since last call, by an asynchronous dialog More...
 
virtual size_t RealtimeProcess (size_t group, EffectSettings &settings, const float *const *inBuf, float *const *outBuf, size_t numSamples)
 
virtual bool RealtimeProcessEnd (EffectSettings &settings) noexcept
 settings can be updated to let a dialog change appearance at idle More...
 
virtual bool RealtimeFinalize (EffectSettings &settings) noexcept
 
virtual size_t SetBlockSize (size_t maxBlockSize)
 
virtual size_t GetBlockSize () const
 
virtual unsigned GetAudioInCount () const
 How many input buffers to allocate at once. More...
 
virtual unsigned GetAudioOutCount () const
 How many output buffers to allocate at once. More...
 
virtual sampleCount GetLatency () const
 
virtual bool NeedsDither () const
 
virtual bool ProcessInitialize (EffectSettings &settings, double sampleRate, ChannelNames chanMap=nullptr)
 
virtual bool ProcessFinalize () noexcept
 
- Public Member Functions inherited from Effect
 Effect ()
 
virtual ~Effect ()
 
PluginPath GetPath () const override
 
bool VisitSettings (SettingsVisitor &visitor, EffectSettings &settings) override
 
bool VisitSettings (ConstSettingsVisitor &visitor, const EffectSettings &settings) const override
 
ComponentInterfaceSymbol GetSymbol () const override
 
VendorSymbol GetVendor () const override
 
wxString GetVersion () const override
 
TranslatableString GetDescription () const override
 
EffectFamilySymbol GetFamily () const override
 Report identifier and user-visible name of the effect protocol. More...
 
bool IsInteractive () const override
 Whether the effect needs a dialog for entry of settings. More...
 
bool IsDefault () const override
 Whether the effect sorts "above the line" in the menus. More...
 
RealtimeSince RealtimeSupport () const override
 Since which version of Audacity has the effect supported realtime? More...
 
bool SupportsAutomation () const override
 Whether the effect has any automatable controls. More...
 
bool SaveSettings (const EffectSettings &settings, CommandParameters &parms) const override
 Store settings as keys and values. More...
 
bool LoadSettings (const CommandParameters &parms, EffectSettings &settings) const override
 Restore settings from keys and values. More...
 
OptionalMessage LoadUserPreset (const RegistryPath &name, EffectSettings &settings) const override
 
bool SaveUserPreset (const RegistryPath &name, const EffectSettings &settings) const override
 Save settings in the configuration file as a user-named preset. More...
 
RegistryPaths GetFactoryPresets () const override
 Report names of factory presets. More...
 
OptionalMessage LoadFactoryPreset (int id, EffectSettings &settings) const override
 
OptionalMessage LoadFactoryDefaults (EffectSettings &settings) const override
 
virtual const EffectParameterMethodsParameters () const
 
bool CanExportPresets () const override
 Whether the effect supports export of presets to files, and importing too. More...
 
bool HasOptions () const override
 
const EffectSettingsManagerGetDefinition () const override
 
virtual NumericFormatID GetSelectionFormat ()
 
bool SaveSettingsAsString (const EffectSettings &settings, wxString &parms) const override
 
OptionalMessage LoadSettingsFromString (const wxString &parms, EffectSettings &settings) const override
 
bool IsBatchProcessing () const override
 
void SetBatchProcessing () override
 
void UnsetBatchProcessing () override
 
unsigned TestUIFlags (unsigned mask)
 
bool Delegate (Effect &delegate, EffectSettings &settings, InstanceFinder finder={})
 Re-invoke DoEffect on another Effect object that implements the work. More...
 
- Public Member Functions inherited from EffectBase
 EffectBase ()
 
 ~EffectBase () override
 
bool IsLinearEffect () const
 
bool PreviewsFullSelection () const
 
void SetTracks (TrackList *pTracks)
 
double GetDefaultDuration ()
 
virtual std::any BeginPreview (const EffectSettings &settings)
 Called when Preview() starts, to allow temporary effect state changes. More...
 
bool DoEffect (EffectSettings &settings, const InstanceFinder &finder, double projectRate, TrackList *list, WaveTrackFactory *factory, NotifyingSelectedRegion &selectedRegion, unsigned flags, const EffectSettingsAccessPtr &pAccess) override
 
virtual double CalcPreviewInputLength (const EffectSettings &settings, double previewLength) const =0
 
void CountWaveTracks ()
 
- Public Member Functions inherited from EffectPlugin
EffectPluginoperator= (EffectPlugin &)=delete
 
virtual ~EffectPlugin ()
 
virtual bool HasOptions () const =0
 
virtual bool CanExportPresets () const =0
 Whether the effect supports export of presets to files, and importing too. More...
 
virtual const EffectSettingsManagerGetDefinition () const =0
 
virtual bool SaveSettingsAsString (const EffectSettings &settings, wxString &parms) const =0
 
virtual OptionalMessage LoadSettingsFromString (const wxString &parms, EffectSettings &settings) const =0
 
virtual bool IsBatchProcessing () const =0
 
virtual void SetBatchProcessing ()=0
 
virtual void UnsetBatchProcessing ()=0
 
virtual bool DoEffect (EffectSettings &settings, const InstanceFinder &finder, double projectRate, TrackList *list, WaveTrackFactory *factory, NotifyingSelectedRegion &selectedRegion, unsigned flags, const EffectSettingsAccessPtr &pAccess=nullptr)=0
 
- Public Member Functions inherited from EffectInstanceFactory
virtual ~EffectInstanceFactory ()
 
virtual std::shared_ptr< EffectInstanceMakeInstance () const =0
 Make an object maintaining short-term state of an Effect. More...
 
- Public Member Functions inherited from EffectSettingsManager
virtual ~EffectSettingsManager ()
 
virtual bool VisitSettings (SettingsVisitor &visitor, EffectSettings &settings)
 
virtual bool VisitSettings (ConstSettingsVisitor &visitor, const EffectSettings &settings) const
 
virtual EffectSettings MakeSettings () const
 
virtual bool CopySettingsContents (const EffectSettings &src, EffectSettings &dst) const
 Update one settings object from another. More...
 
virtual std::unique_ptr< EffectOutputsMakeOutputs () const
 Produce an object to hold values to send to effect output meters. More...
 
- Public Member Functions inherited from EffectDefinitionInterface
virtual ~EffectDefinitionInterface ()
 
virtual EffectType GetType () const =0
 Type determines how it behaves. More...
 
virtual EffectType GetClassification () const
 Determines which menu it appears in; default same as GetType(). More...
 
virtual EffectFamilySymbol GetFamily () const =0
 Report identifier and user-visible name of the effect protocol. More...
 
virtual bool IsInteractive () const =0
 Whether the effect needs a dialog for entry of settings. More...
 
virtual bool IsDefault () const =0
 Whether the effect sorts "above the line" in the menus. More...
 
virtual RealtimeSince RealtimeSupport () const =0
 Since which version of Audacity has the effect supported realtime? More...
 
bool SupportsRealtime () const
 
virtual bool SupportsAutomation () const =0
 Whether the effect has any automatable controls. More...
 
virtual bool EnablesDebug () const
 Whether the effect dialog should have a Debug button; default, always false. More...
 
virtual ManualPageID ManualPage () const
 Name of a page in the Audacity alpha manual, default is empty. More...
 
virtual FilePath HelpPage () const
 Fully qualified local help file name, default is empty. More...
 
virtual bool IsHiddenFromMenus () const
 Default is false. More...
 
- Public Member Functions inherited from ComponentInterface
virtual ~ComponentInterface ()
 
virtual PluginPath GetPath () const =0
 
virtual ComponentInterfaceSymbol GetSymbol () const =0
 
virtual VendorSymbol GetVendor () const =0
 
virtual wxString GetVersion () const =0
 
virtual TranslatableString GetDescription () const =0
 
TranslatableString GetName () const
 

Static Public Member Functions

static AutoDuckBaseFetchParameters (AutoDuckBase &e, EffectSettings &)
 
- Static Public Member Functions inherited from Effect
static EffectFetchParameters (Effect &e, EffectSettings &)
 
- Static Public Member Functions inherited from EffectBase
static std::optional< InstancePointerFindInstance (EffectPlugin &plugin)
 
static InstanceFinder DefaultInstanceFinder (EffectPlugin &plugin)
 
- Static Public Member Functions inherited from EffectDefinitionInterface
static Identifier GetSquashedName (const Identifier &ident)
 A utility that strips spaces and CamelCases a name. More...
 

Static Public Attributes

static const ComponentInterfaceSymbol Symbol { XO("Auto Duck") }
 
- Static Public Attributes inherited from EffectPlugin
static const wxString kUserPresetIdent = wxT("User Preset:")
 
static const wxString kFactoryPresetIdent = wxT("Factory Preset:")
 
static const wxString kCurrentSettingsIdent = wxT("<Current Settings>")
 
static const wxString kFactoryDefaultsIdent = wxT("<Factory Defaults>")
 

Protected Member Functions

const EffectParameterMethodsParameters () const override
 
- Protected Member Functions inherited from Effect
bool CheckWhetherSkipEffect (const EffectSettings &settings) const override
 Default implementation returns false. More...
 
double CalcPreviewInputLength (const EffectSettings &settings, double previewLength) const override
 Default implementation returns previewLength More...
 
bool TotalProgress (double frac, const TranslatableString &={}) const
 
bool TrackProgress (int whichTrack, double frac, const TranslatableString &={}) const
 
bool TrackGroupProgress (int whichGroup, double frac, const TranslatableString &={}) const
 
int GetNumWaveTracks () const
 
int GetNumWaveGroups () const
 
void GetBounds (const WaveTrack &track, sampleCount *start, sampleCount *len)
 
- Protected Member Functions inherited from EffectBase
virtual bool CheckWhetherSkipEffect (const EffectSettings &settings) const =0
 After Init(), tell whether Process() should be skipped. More...
 
void SetLinearEffectFlag (bool linearEffectFlag)
 
void SetPreviewFullSelectionFlag (bool previewDurationFlag)
 
bool IsPreviewing () const
 
const TrackListinputTracks () const
 
const AudacityProjectFindProject () const
 

Protected Attributes

double mDuckAmountDb
 
double mInnerFadeDownLen
 
double mInnerFadeUpLen
 
double mOuterFadeDownLen
 
double mOuterFadeUpLen
 
double mThresholdDb
 
double mMaximumPause
 
const WaveTrackmControlTrack {}
 
- Protected Attributes inherited from EffectBase
double mF0 {}
 
double mF1 {}
 
wxArrayString mPresetNames
 
unsigned mUIFlags { 0 }
 

Static Protected Attributes

static constexpr EffectParameter DuckAmountDb
 
static constexpr EffectParameter InnerFadeDownLen
 
static constexpr EffectParameter InnerFadeUpLen
 
static constexpr EffectParameter OuterFadeDownLen
 
static constexpr EffectParameter OuterFadeUpLen
 
static constexpr EffectParameter ThresholdDb
 
static constexpr EffectParameter MaximumPause
 

Private Member Functions

bool ApplyDuckFade (int trackNum, WaveChannel &track, double t0, double t1)
 

Additional Inherited Members

- Public Types inherited from StatefulEffectBase
using MessagePackage = EffectInstance::MessagePackage
 
- Public Types inherited from EffectPlugin
using EffectSettingsAccessPtr = std::shared_ptr< EffectSettingsAccess >
 
using InstancePointer = std::shared_ptr< EffectInstanceEx >
 
using InstanceFinder = std::function< std::optional< InstancePointer >(EffectSettings &settings) >
 
- Public Types inherited from EffectDefinitionInterface
enum class  RealtimeSince : unsigned { Never , After_3_1 , Always }
 In which versions of Audacity was an effect realtime capable? More...
 
- Public Attributes inherited from EffectBase
std::shared_ptr< TrackListmTracks {}
 
int mNumTracks {}
 
BasicUI::ProgressDialogmProgress {}
 
double mProjectRate {}
 
WaveTrackFactorymFactory {}
 
double mT0 {}
 
double mT1 {}
 
bool mIsPreview { false }
 

Detailed Description

Implements the Auto Ducking effect.

Definition at line 18 of file AutoDuckBase.h.

Constructor & Destructor Documentation

◆ AutoDuckBase()

AutoDuckBase::AutoDuckBase ( )

Definition at line 63 of file AutoDuckBase.cpp.

64{
65 Parameters().Reset(*this);
67}
const EffectParameterMethods & Parameters() const override
void SetLinearEffectFlag(bool linearEffectFlag)
Definition: EffectBase.cpp:210
virtual void Reset(Effect &effect) const =0

References Parameters(), EffectParameterMethods::Reset(), and EffectBase::SetLinearEffectFlag().

Referenced by Parameters().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ~AutoDuckBase()

AutoDuckBase::~AutoDuckBase ( )
virtual

Definition at line 69 of file AutoDuckBase.cpp.

70{
71}

Member Function Documentation

◆ ApplyDuckFade()

bool AutoDuckBase::ApplyDuckFade ( int  trackNum,
WaveChannel track,
double  t0,
double  t1 
)
private

Definition at line 337 of file AutoDuckBase.cpp.

339{
340 bool cancel = false;
341
342 auto start = track.TimeToLongSamples(t0);
343 auto end = track.TimeToLongSamples(t1);
344
345 Floats buf { kBufSize };
346 auto pos = start;
347
348 auto fadeDownSamples =
350 if (fadeDownSamples < 1)
351 fadeDownSamples = 1;
352
353 auto fadeUpSamples =
355 if (fadeUpSamples < 1)
356 fadeUpSamples = 1;
357
358 float fadeDownStep = mDuckAmountDb / fadeDownSamples.as_double();
359 float fadeUpStep = mDuckAmountDb / fadeUpSamples.as_double();
360
361 while (pos < end)
362 {
363 const auto len = limitSampleBufferSize(kBufSize, end - pos);
364 track.GetFloats(buf.get(), pos, len);
365 for (auto i = pos; i < pos + len; ++i)
366 {
367 float gainDown = fadeDownStep * (i - start).as_float();
368 float gainUp = fadeUpStep * (end - i).as_float();
369
370 float gain;
371 if (gainDown > gainUp)
372 gain = gainDown;
373 else
374 gain = gainUp;
375 if (gain < mDuckAmountDb)
376 gain = mDuckAmountDb;
377
378 // i - pos is bounded by len:
379 buf[(i - pos).as_size_t()] *= DB_TO_LINEAR(gain);
380 }
381
382 if (!track.SetFloats(buf.get(), pos, len))
383 {
384 cancel = true;
385 break;
386 }
387
388 pos += len;
389
390 float curTime = track.LongSamplesToTime(pos);
391 float fractionFinished = (curTime - mT0) / (mT1 - mT0);
392 if (TotalProgress(
393 (trackNum + 1 + fractionFinished) / (GetNumWaveTracks() + 1)))
394 {
395 cancel = true;
396 break;
397 }
398 }
399
400 return cancel;
401}
static const size_t kBufSize
#define DB_TO_LINEAR(x)
Definition: MemoryX.h:338
size_t limitSampleBufferSize(size_t bufferSize, sampleCount limit)
Definition: SampleCount.cpp:22
double mOuterFadeUpLen
Definition: AutoDuckBase.h:55
double mInnerFadeDownLen
Definition: AutoDuckBase.h:52
double mOuterFadeDownLen
Definition: AutoDuckBase.h:54
double mDuckAmountDb
Definition: AutoDuckBase.h:51
double mInnerFadeUpLen
Definition: AutoDuckBase.h:53
double mT1
Definition: EffectBase.h:123
double mT0
Definition: EffectBase.h:122
bool TotalProgress(double frac, const TranslatableString &={}) const
Definition: Effect.cpp:335
int GetNumWaveTracks() const
Definition: Effect.h:139
bool SetFloats(const float *buffer, sampleCount start, size_t len, sampleFormat effectiveFormat=widestSampleFormat)
Random-access assignment of a range of samples.
Definition: WaveTrack.h:162
bool GetFloats(float *buffer, sampleCount start, size_t len, fillFormat fill=FillFormat::fillZero, bool mayThrow=true, sampleCount *pNumWithinClips=nullptr) const
"narrow" overload fetches from the unique channel
Definition: WaveTrack.h:129
double LongSamplesToTime(sampleCount pos) const
sampleCount TimeToLongSamples(double t0) const
const char * end(const char *str) noexcept
Definition: StringUtils.h:106

References DB_TO_LINEAR, details::end(), WaveChannel::GetFloats(), Effect::GetNumWaveTracks(), kBufSize, limitSampleBufferSize(), WideSampleSequence::LongSamplesToTime(), mDuckAmountDb, mInnerFadeDownLen, mInnerFadeUpLen, mOuterFadeDownLen, mOuterFadeUpLen, EffectBase::mT0, EffectBase::mT1, WaveChannel::SetFloats(), WideSampleSequence::TimeToLongSamples(), and Effect::TotalProgress().

Referenced by Process().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ FetchParameters()

static AutoDuckBase * AutoDuckBase::FetchParameters ( AutoDuckBase e,
EffectSettings  
)
inlinestatic

Definition at line 21 of file AutoDuckBase.h.

22 {
23 return &e;
24 }

◆ GetDescription()

TranslatableString AutoDuckBase::GetDescription ( ) const
overridevirtual

Implements ComponentInterface.

Definition at line 80 of file AutoDuckBase.cpp.

81{
82 return XO(
83 "Reduces (ducks) the volume of one or more tracks whenever the volume of a specified \"control\" track reaches a particular level");
84}
XO("Cut/Copy/Paste")

References XO().

Here is the call graph for this function:

◆ GetSymbol()

ComponentInterfaceSymbol AutoDuckBase::GetSymbol ( ) const
overridevirtual

Implements ComponentInterface.

Definition at line 75 of file AutoDuckBase.cpp.

76{
77 return Symbol;
78}
static const ComponentInterfaceSymbol Symbol
Definition: AutoDuckBase.h:25

References Symbol.

◆ GetType()

EffectType AutoDuckBase::GetType ( ) const
overridevirtual

Type determines how it behaves.

Implements EffectDefinitionInterface.

Definition at line 93 of file AutoDuckBase.cpp.

94{
95 return EffectTypeProcess;
96}
@ EffectTypeProcess

References EffectTypeProcess.

Referenced by Process().

Here is the caller graph for this function:

◆ Init()

bool AutoDuckBase::Init ( )
overridevirtual

Default implementation does nothing, returns true

Reimplemented from StatefulEffectBase.

Definition at line 100 of file AutoDuckBase.cpp.

101{
102 mControlTrack = nullptr;
103
104 // Find the control track, which is the non-selected wave track immediately
105 // after the last selected wave track. Fail if there is no such track or if
106 // any selected track is not a wave track.
107 bool lastWasSelectedWaveTrack = false;
108 const WaveTrack* controlTrackCandidate = nullptr;
109 for (auto t : *inputTracks())
110 {
111 if (lastWasSelectedWaveTrack && !t->GetSelected())
112 // This could be the control track, so remember it
113 controlTrackCandidate = dynamic_cast<const WaveTrack*>(t);
114
115 lastWasSelectedWaveTrack = false;
116 if (t->GetSelected())
117 {
118 bool ok = t->TypeSwitch<bool>(
119 [&](const WaveTrack&) {
120 lastWasSelectedWaveTrack = true;
121 controlTrackCandidate = nullptr;
122 return true;
123 },
124 [&](const Track&) {
125 using namespace BasicUI;
127 /* i18n-hint: Auto duck is the name of an effect that 'ducks'
128 (reduces the volume) of the audio automatically when there is
129 sound on another track. Not as in 'Donald-Duck'!*/
130 XO("You selected a track which does not contain audio. "
131 "AutoDuck can only process audio tracks."),
132 MessageBoxOptions {}.IconStyle(Icon::Error));
133 return false;
134 });
135 if (!ok)
136 return false;
137 }
138 }
139
140 if (!controlTrackCandidate)
141 {
142 using namespace BasicUI;
144 /* i18n-hint: Auto duck is the name of an effect that 'ducks' (reduces
145 the volume) of the audio automatically when there is sound on another
146 track. Not as in 'Donald-Duck'!*/
147 XO("Auto Duck needs a control track which must be placed below the "
148 "selected track(s)."),
149 MessageBoxOptions {}.IconStyle(Icon::Error));
150 return false;
151 }
152
153 mControlTrack = controlTrackCandidate;
154 return true;
155}
const WaveTrack * mControlTrack
Definition: AutoDuckBase.h:59
const TrackList * inputTracks() const
Definition: EffectBase.h:102
Abstract base class for an object holding data associated with points on a time axis.
Definition: Track.h:110
R TypeSwitch(const Functions &...functions)
Definition: Track.h:381
A Track that contains audio waveform data.
Definition: WaveTrack.h:203
MessageBoxResult ShowMessageBox(const TranslatableString &message, MessageBoxOptions options={})
Show a modal message box with either Ok or Yes and No, and optionally Cancel.
Definition: BasicUI.h:287
MessageBoxOptions && IconStyle(Icon style) &&
Definition: BasicUI.h:104

References BasicUI::MessageBoxOptions::IconStyle(), EffectBase::inputTracks(), mControlTrack, BasicUI::ShowMessageBox(), Track::TypeSwitch(), and XO().

Here is the call graph for this function:

◆ ManualPage()

ManualPageID AutoDuckBase::ManualPage ( ) const
overridevirtual

Name of a page in the Audacity alpha manual, default is empty.

Reimplemented from EffectDefinitionInterface.

Definition at line 86 of file AutoDuckBase.cpp.

87{
88 return L"Auto_Duck";
89}

◆ Parameters()

const EffectParameterMethods & AutoDuckBase::Parameters ( ) const
overrideprotectedvirtual

Reimplemented from Effect.

Definition at line 30 of file AutoDuckBase.cpp.

31{
32 static CapturedParameters<
35 parameters;
36 return parameters;
37}
static constexpr EffectParameter OuterFadeUpLen
Definition: AutoDuckBase.h:75
static constexpr EffectParameter ThresholdDb
Definition: AutoDuckBase.h:78
static constexpr EffectParameter MaximumPause
Definition: AutoDuckBase.h:81
static constexpr EffectParameter InnerFadeDownLen
Definition: AutoDuckBase.h:66
static constexpr EffectParameter InnerFadeUpLen
Definition: AutoDuckBase.h:69
static constexpr EffectParameter DuckAmountDb
Definition: AutoDuckBase.h:63
static constexpr EffectParameter OuterFadeDownLen
Definition: AutoDuckBase.h:72
Generates EffectParameterMethods overrides from variadic template arguments.

References AutoDuckBase(), DuckAmountDb, InnerFadeDownLen, InnerFadeUpLen, MaximumPause, OuterFadeDownLen, OuterFadeUpLen, and ThresholdDb.

Referenced by AutoDuckBase().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Process()

bool AutoDuckBase::Process ( EffectInstance instance,
EffectSettings settings 
)
overridevirtual

Implements StatefulEffectBase.

Definition at line 157 of file AutoDuckBase.cpp.

158{
159 if (GetNumWaveTracks() == 0 || !mControlTrack)
160 return false;
161
162 bool cancel = false;
163
166
167 if (end <= start)
168 return false;
169
170 WaveTrack::Holder pFirstTrack;
171 auto pControlTrack = mControlTrack;
172 // If there is any stretch in the control track, substitute a temporary
173 // rendering before trying to use GetFloats
174 {
175 const auto t0 = pControlTrack->LongSamplesToTime(start);
176 const auto t1 = pControlTrack->LongSamplesToTime(end);
177 if (TimeStretching::HasPitchOrSpeed(*pControlTrack, t0, t1))
178 {
179 pFirstTrack = pControlTrack->Duplicate()->SharedPointer<WaveTrack>();
180 if (pFirstTrack)
181 {
183 [&](const ProgressReporter& reportProgress) {
184 pFirstTrack->ApplyPitchAndSpeed(
185 { { t0, t1 } }, reportProgress);
186 },
188 XO("Rendering Control-Track Time-Stretched Audio"));
189 pControlTrack = pFirstTrack.get();
190 }
191 }
192 }
193
194 // the minimum number of samples we have to wait until the maximum
195 // pause has been exceeded
196 double maxPause = mMaximumPause;
197
198 // We don't fade in until we have time enough to actually fade out again
199 if (maxPause < mOuterFadeDownLen + mOuterFadeUpLen)
201
202 auto minSamplesPause = pControlTrack->TimeToLongSamples(maxPause);
203
204 double threshold = DB_TO_LINEAR(mThresholdDb);
205
206 // adjust the threshold so we can compare it to the rmsSum value
207 threshold = threshold * threshold * kRMSWindowSize;
208
209 int rmsPos = 0;
210 double rmsSum = 0;
211 // to make the progress bar appear more natural, we first look for all
212 // duck regions and apply them all at once afterwards
213 std::vector<AutoDuckRegion> regions;
214 bool inDuckRegion = false;
215 {
216 Floats rmsWindow { kRMSWindowSize, true };
217
218 Floats buf { kBufSize };
219
220 // initialize the following two variables to prevent compiler warning
221 double duckRegionStart = 0;
222 sampleCount curSamplesPause = 0;
223
224 auto pos = start;
225
226 const auto pControlChannel = *pControlTrack->Channels().begin();
227 while (pos < end)
228 {
229 const auto len = limitSampleBufferSize(kBufSize, end - pos);
230
231 pControlChannel->GetFloats(buf.get(), pos, len);
232
233 for (auto i = pos; i < pos + len; i++)
234 {
235 rmsSum -= rmsWindow[rmsPos];
236 // i - pos is bounded by len:
237 auto index = (i - pos).as_size_t();
238 rmsWindow[rmsPos] = buf[index] * buf[index];
239 rmsSum += rmsWindow[rmsPos];
240 rmsPos = (rmsPos + 1) % kRMSWindowSize;
241
242 bool thresholdExceeded = rmsSum > threshold;
243
244 if (thresholdExceeded)
245 {
246 // everytime the threshold is exceeded, reset our count for
247 // the number of pause samples
248 curSamplesPause = 0;
249
250 if (!inDuckRegion)
251 {
252 // the threshold has been exceeded for the first time, so
253 // let the duck region begin here
254 inDuckRegion = true;
255 duckRegionStart = pControlTrack->LongSamplesToTime(i);
256 }
257 }
258
259 if (!thresholdExceeded && inDuckRegion)
260 {
261 // the threshold has not been exceeded and we are in a duck
262 // region, but only fade in if the maximum pause has been
263 // exceeded
264 curSamplesPause += 1;
265
266 if (curSamplesPause >= minSamplesPause)
267 {
268 // do the actual duck fade and reset all values
269 double duckRegionEnd =
270 pControlTrack->LongSamplesToTime(i - curSamplesPause);
271
272 regions.push_back(AutoDuckRegion(
273 duckRegionStart - mOuterFadeDownLen,
274 duckRegionEnd + mOuterFadeUpLen));
275
276 inDuckRegion = false;
277 }
278 }
279 }
280
281 pos += len;
282
283 if (TotalProgress(
284 (pos - start).as_double() / (end - start).as_double() /
285 (GetNumWaveTracks() + 1)))
286 {
287 cancel = true;
288 break;
289 }
290 }
291
292 // apply last duck fade, if any
293 if (inDuckRegion)
294 {
295 double duckRegionEnd =
296 pControlTrack->LongSamplesToTime(end - curSamplesPause);
297 regions.push_back(AutoDuckRegion(
298 duckRegionStart - mOuterFadeDownLen,
299 duckRegionEnd + mOuterFadeUpLen));
300 }
301 }
302
303 if (!cancel)
304 {
305 EffectOutputTracks outputs { *mTracks, GetType(), { { mT0, mT1 } } };
306
307 int trackNum = 0;
308
309 for (auto iterTrack : outputs.Get().Selected<WaveTrack>())
310 {
311 for (const auto pChannel : iterTrack->Channels())
312 for (size_t i = 0; i < regions.size(); ++i)
313 {
314 const AutoDuckRegion& region = regions[i];
315 if (ApplyDuckFade(trackNum++, *pChannel, region.t0, region.t1))
316 {
317 cancel = true;
318 goto done;
319 }
320 }
321
322 done:
323 if (cancel)
324 break;
325 }
326
327 if (!cancel)
328 outputs.Commit();
329 }
330
331 return !cancel;
332}
static const size_t kRMSWindowSize
double mThresholdDb
Definition: AutoDuckBase.h:56
bool ApplyDuckFade(int trackNum, WaveChannel &track, double t0, double t1)
EffectType GetType() const override
Type determines how it behaves.
double mMaximumPause
Definition: AutoDuckBase.h:57
std::shared_ptr< TrackList > mTracks
Definition: EffectBase.h:116
Use this object to copy the input tracks to tentative outputTracks.
static void WithCancellableProgress(std::function< void(const ProgressReporter &)> action, TranslatableString title, TranslatableString message)
A frequently useful convenience wraps a lambda and may throw this type.
std::shared_ptr< WaveTrack > Holder
Definition: WaveTrack.h:247
Positions or offsets within audio files need a wide type.
Definition: SampleCount.h:19
std::function< void(double)> ProgressReporter
Definition: BasicUI.h:25
WAVE_TRACK_API const TranslatableString defaultStretchRenderingTitle
WAVE_TRACK_API bool HasPitchOrSpeed(const WaveTrack &track, double t0, double t1)
a struct that holds a start and end time.

References ApplyDuckFade(), DB_TO_LINEAR, TimeStretching::defaultStretchRenderingTitle, details::end(), Effect::GetNumWaveTracks(), GetType(), TimeStretching::HasPitchOrSpeed(), kBufSize, kRMSWindowSize, limitSampleBufferSize(), WideSampleSequence::LongSamplesToTime(), mControlTrack, mMaximumPause, mOuterFadeDownLen, mOuterFadeUpLen, EffectBase::mT0, EffectBase::mT1, mThresholdDb, EffectBase::mTracks, AutoDuckRegion::t0, AutoDuckRegion::t1, WideSampleSequence::TimeToLongSamples(), Effect::TotalProgress(), UserException::WithCancellableProgress(), and XO().

Here is the call graph for this function:

Member Data Documentation

◆ DuckAmountDb

constexpr EffectParameter AutoDuckBase::DuckAmountDb
staticconstexprprotected
Initial value:
{
&AutoDuckBase::mDuckAmountDb, L"DuckAmountDb", -12.0, -24.0, 0.0, 1
}

Definition at line 63 of file AutoDuckBase.h.

Referenced by EffectAutoDuck::Panel::OnMotion(), EffectAutoDuck::Panel::OnPaint(), and Parameters().

◆ InnerFadeDownLen

constexpr EffectParameter AutoDuckBase::InnerFadeDownLen
staticconstexprprotected
Initial value:
{
&AutoDuckBase::mInnerFadeDownLen, L"InnerFadeDownLen", 0.0, 0.0, 3.0, 1
}

Definition at line 66 of file AutoDuckBase.h.

Referenced by EffectAutoDuck::Panel::OnMotion(), EffectAutoDuck::Panel::OnPaint(), and Parameters().

◆ InnerFadeUpLen

constexpr EffectParameter AutoDuckBase::InnerFadeUpLen
staticconstexprprotected
Initial value:
{
&AutoDuckBase::mInnerFadeUpLen, L"InnerFadeUpLen", 0.0, 0.0, 3.0, 1
}

Definition at line 69 of file AutoDuckBase.h.

Referenced by EffectAutoDuck::Panel::OnMotion(), EffectAutoDuck::Panel::OnPaint(), and Parameters().

◆ MaximumPause

constexpr EffectParameter AutoDuckBase::MaximumPause
staticconstexprprotected
Initial value:
{
&AutoDuckBase::mMaximumPause, L"MaximumPause", 1.0, 0.0, DBL_MAX, 1
}

Definition at line 81 of file AutoDuckBase.h.

Referenced by Parameters().

◆ mControlTrack

const WaveTrack* AutoDuckBase::mControlTrack {}
protected

Definition at line 59 of file AutoDuckBase.h.

Referenced by Init(), and Process().

◆ mDuckAmountDb

double AutoDuckBase::mDuckAmountDb
protected

Definition at line 51 of file AutoDuckBase.h.

Referenced by ApplyDuckFade().

◆ mInnerFadeDownLen

double AutoDuckBase::mInnerFadeDownLen
protected

Definition at line 52 of file AutoDuckBase.h.

Referenced by ApplyDuckFade().

◆ mInnerFadeUpLen

double AutoDuckBase::mInnerFadeUpLen
protected

Definition at line 53 of file AutoDuckBase.h.

Referenced by ApplyDuckFade().

◆ mMaximumPause

double AutoDuckBase::mMaximumPause
protected

Definition at line 57 of file AutoDuckBase.h.

Referenced by Process().

◆ mOuterFadeDownLen

double AutoDuckBase::mOuterFadeDownLen
protected

Definition at line 54 of file AutoDuckBase.h.

Referenced by ApplyDuckFade(), and Process().

◆ mOuterFadeUpLen

double AutoDuckBase::mOuterFadeUpLen
protected

Definition at line 55 of file AutoDuckBase.h.

Referenced by ApplyDuckFade(), and Process().

◆ mThresholdDb

double AutoDuckBase::mThresholdDb
protected

Definition at line 56 of file AutoDuckBase.h.

Referenced by Process().

◆ OuterFadeDownLen

constexpr EffectParameter AutoDuckBase::OuterFadeDownLen
staticconstexprprotected
Initial value:
{
&AutoDuckBase::mOuterFadeDownLen, L"OuterFadeDownLen", 0.5, 0.0, 3.0, 1
}

Definition at line 72 of file AutoDuckBase.h.

Referenced by EffectAutoDuck::Panel::OnMotion(), EffectAutoDuck::Panel::OnPaint(), and Parameters().

◆ OuterFadeUpLen

constexpr EffectParameter AutoDuckBase::OuterFadeUpLen
staticconstexprprotected
Initial value:
{
&AutoDuckBase::mOuterFadeUpLen, L"OuterFadeUpLen", 0.5, 0.0, 3.0, 1
}

Definition at line 75 of file AutoDuckBase.h.

Referenced by EffectAutoDuck::Panel::OnMotion(), EffectAutoDuck::Panel::OnPaint(), and Parameters().

◆ Symbol

const ComponentInterfaceSymbol AutoDuckBase::Symbol { XO("Auto Duck") }
static

Definition at line 25 of file AutoDuckBase.h.

Referenced by GetSymbol().

◆ ThresholdDb

constexpr EffectParameter AutoDuckBase::ThresholdDb
staticconstexprprotected
Initial value:
{
&AutoDuckBase::mThresholdDb, L"ThresholdDb", -30.0, -100.0, 0.0, 1
}

Definition at line 78 of file AutoDuckBase.h.

Referenced by Parameters().


The documentation for this class was generated from the following files: