Audacity 3.2.0
Effect.cpp
Go to the documentation of this file.
1/**********************************************************************
2
3 Audacity: A Digital Audio Editor
4
5 Effect.cpp
6
7 Dominic Mazzoni
8 Vaughan Johnson
9 Martyn Shaw
10
11*******************************************************************//*******************************************************************/
17#include "Effect.h"
18
19#include <algorithm>
20
21#include <wx/defs.h>
22
23#include "BasicUI.h"
24#include "ConfigInterface.h"
26#include "ShuttleAutomation.h"
27#include "SyncLock.h"
28#include "ViewInfo.h"
29#include "WaveTrack.h"
30#include "wxFileNameWrapper.h"
31#include "NumericConverter.h"
32
33#include <unordered_map>
34
36{
37}
38
40{
41}
42
43// ComponentInterface implementation
44
46{
48}
49
51{
52 return {};
53}
54
56{
57 return XO("Audacity");
58}
59
60wxString Effect::GetVersion() const
61{
62 return AUDACITY_VERSION_STRING;
63}
64
66{
67 return {};
68}
69
70// EffectDefinitionInterface implementation
71
73{
74 return EffectTypeNone;
75}
76
78{
79 // Unusually, the internal and visible strings differ for the built-in
80 // effect family.
81 return { wxT("Audacity"), XO("Built-in") };
82}
83
85{
86 return true;
87}
88
90{
91 return true;
92}
93
95{
97}
98
100{
101 return true;
102}
103
105{
106 static const CapturedParameters<Effect> empty;
107 return empty;
108}
109
111{
112 Parameters().Visit(*this, visitor, settings);
113 return true;
114}
115
117 ConstSettingsVisitor &visitor, const EffectSettings &settings) const
118{
119 Parameters().Visit(*this, visitor, settings);
120 return true;
121}
122
124 const EffectSettings &settings, CommandParameters & parms) const
125{
126 Parameters().Get( *this, settings, parms );
127 return true;
128}
129
131 const CommandParameters & parms, EffectSettings &settings) const
133 // The first argument, and with it the const_cast, will disappear when
134 // all built-in effects are stateless.
135 return Parameters().Set( *const_cast<Effect*>(this), parms, settings );
136}
137
140{
141 // Find one string in the registry and then reinterpret it
142 // as complete settings
143 wxString parms;
145 name, wxT("Parameters"), parms))
146 return {};
147
148 return LoadSettingsFromString(parms, settings);
149}
150
152 const RegistryPath & name, const EffectSettings &settings) const
153{
154 // Save all settings as a single string value in the registry
155 wxString parms;
156 if (!SaveSettingsAsString(settings, parms))
157 return false;
158
160 name, wxT("Parameters"), parms);
161}
162
164{
165 return {};
166}
167
169{
170 return { nullptr };
171}
172
174{
176}
177
179{
180 return true;
181}
182
184{
185 return false;
186}
187
188// EffectPlugin implementation
189
191{
192 return *this;
193}
194
196{
197 if( !IsBatchProcessing() && FindProject() )
200}
201
203{
204 return wxT("SavedState");
205}
206
207// Effect implementation
208
210 const EffectSettings &settings, wxString & parms) const
211{
214 S.mpEap = &eap;
215 if( VisitSettings( S, settings ) ){
216 ;// got eap value using VisitSettings.
217 }
218 // Won't be needed in future
219 else if (!SaveSettings(settings, eap))
220 {
221 return false;
222 }
223
224 return eap.GetParameters(parms);
225}
226
228 const wxString & parms, EffectSettings &settings) const
229{
230 // If the string starts with one of certain significant substrings,
231 // then the rest of the string is reinterpreted as part of a registry key,
232 // and a user or factory preset is then loaded.
233 // (Where did these prefixes come from? See EffectPresetsDialog; and
234 // ultimately the uses of it by EffectManager::GetPreset, which is used by
235 // the macro management dialog)
236 wxString preset = parms;
237 OptionalMessage result;
238 if (preset.StartsWith(kUserPresetIdent))
239 {
240 preset.Replace(kUserPresetIdent, wxEmptyString, false);
242 }
243 else if (preset.StartsWith(kFactoryPresetIdent))
244 {
245 preset.Replace(kFactoryPresetIdent, wxEmptyString, false);
246 auto presets = GetFactoryPresets();
247 result = LoadFactoryPreset(
248 make_iterator_range( presets ).index( preset ), settings );
249 }
250 else if (preset.StartsWith(kCurrentSettingsIdent))
251 {
252 preset.Replace(kCurrentSettingsIdent, wxEmptyString, false);
254 }
255 else if (preset.StartsWith(kFactoryDefaultsIdent))
256 {
257 preset.Replace(kFactoryDefaultsIdent, wxEmptyString, false);
259 }
260 else
261 {
262 // If the string did not start with any of the significant substrings,
263 // then use VisitSettings or LoadSettings to reinterpret it,
264 // or use LoadSettings.
265 // This interprets what was written by SaveSettings, above.
266 CommandParameters eap(parms);
268 S.SetForValidating( &eap );
269 // VisitSettings returns false if not defined for this effect.
270 // To do: fix const_cast in use of VisitSettings
271 if ( !const_cast<Effect*>(this)->VisitSettings(S, settings) ) {
272 // the old method...
273 if (LoadSettings(eap, settings))
274 return { nullptr };
275 }
276 else if( !S.bOK )
277 result = {};
278 else{
279 result = { nullptr };
280 S.SetForWriting( &eap );
281 const_cast<Effect*>(this)->VisitSettings(S, settings);
282 }
283 }
284
285 if (!result)
286 {
287 using namespace BasicUI;
289 XO("%s: Could not load settings below. Default settings will be used.\n\n%s")
290 .Format( GetName(), preset ),
292 // We are using default settings and we still wish to continue.
293 result = { nullptr };
294 }
295 return result;
296}
297
298unsigned Effect::TestUIFlags(unsigned mask) {
299 return mask & mUIFlags;
300}
301
303{
304 return mIsBatch;
305}
306
308{
309 mIsBatch = true;
310 // Save effect's internal state in a special registry path
311 // just for this purpose
312 // If effect is not stateful, this step doesn't really matter, and the
313 // settings object is a dummy
314 auto dummySettings = MakeSettings();
315 SaveUserPreset(GetSavedStateGroup(), dummySettings);
316}
317
319{
320 mIsBatch = false;
321 // Restore effect's internal state from registry
322 // If effect is not stateful, this call doesn't really matter, and the
323 // settings object is a dummy
324 auto dummySettings = MakeSettings();
325 // Ignore failure
326 (void ) LoadUserPreset(GetSavedStateGroup(), dummySettings);
327}
328
330{
332 region.setTimes( mT0, mT1 );
333
334 return delegate.DoEffect(settings, {}, mProjectRate, mTracks, mFactory,
335 region, mUIFlags, nullptr);
336}
337
338bool Effect::TotalProgress(double frac, const TranslatableString &msg) const
339{
340 auto updateResult = (mProgress ?
341 mProgress->Poll(frac * 1000, 1000, msg) :
343 return (updateResult != BasicUI::ProgressResult::Success);
344}
345
347 int whichTrack, double frac, const TranslatableString &msg) const
348{
349 auto updateResult = (mProgress ?
350 mProgress->Poll((whichTrack + frac) * 1000,
351 (double) mNumTracks * 1000, msg) :
353 return (updateResult != BasicUI::ProgressResult::Success);
354}
355
357 int whichGroup, double frac, const TranslatableString &msg) const
358{
359 auto updateResult = (mProgress ?
360 mProgress->Poll((whichGroup + frac) * 1000,
361 (double) mNumGroups * 1000, msg) :
363 return (updateResult != BasicUI::ProgressResult::Success);
364}
365
367 const WaveTrack &track, const WaveTrack *pRight,
368 sampleCount *start, sampleCount *len)
369{
370 auto t0 = std::max( mT0, track.GetStartTime() );
371 auto t1 = std::min( mT1, track.GetEndTime() );
372
373 if ( pRight ) {
374 t0 = std::min( t0, std::max( mT0, pRight->GetStartTime() ) );
375 t1 = std::max( t1, std::min( mT1, pRight->GetEndTime() ) );
376 }
377
378 if (t1 > t0) {
379 *start = track.TimeToLongSamples(t0);
380 auto end = track.TimeToLongSamples(t1);
381 *len = end - *start;
382 }
383 else {
384 *start = 0;
385 *len = 0;
386 }
387}
388
389//
390// private methods
391//
392// Use this method to copy the input tracks to mOutputTracks, if
393// doing the processing on them, and replacing the originals only on success (and not cancel).
394// Copy the group tracks that have tracks selected
395// If not all sync-locked selected, then only selected wave tracks.
396void Effect::CopyInputTracks(bool allSyncLockSelected)
397{
398 // Reset map
399 mIMap.clear();
400 mOMap.clear();
401
403 const_cast<AudacityProject*>( FindProject() ) // how to remove this const_cast?
404 );
405
406 auto trackRange = mTracks->Any() +
407 [&] (const Track *pTrack) {
408 return allSyncLockSelected
410 : track_cast<const WaveTrack*>( pTrack ) && pTrack->GetSelected();
411 };
412
413 for (auto aTrack : trackRange)
414 {
415 Track *o = mOutputTracks->Add(aTrack->Duplicate());
416 mIMap.push_back(aTrack);
417 mOMap.push_back(o);
418 }
419}
420
421Track *Effect::AddToOutputTracks(const std::shared_ptr<Track> &t)
422{
423 mIMap.push_back(NULL);
424 mOMap.push_back(t.get());
425 return mOutputTracks->Add(t);
426}
427
429{
430 return false;
431}
432
434 const EffectSettings &, double previewLength) const
435{
436 return previewLength;
437}
wxT("CloseDown"))
Toolkit-neutral facade for basic user interface services.
int min(int a, int b)
const TranslatableString name
Definition: Distortion.cpp:76
#define BUILTIN_EFFECT_PREFIX
Definition: Effect.h:17
RegistryPath UserPresetsGroup(const RegistryPath &name)
Compute part of a registry path, given a name which may be empty.
const RegistryPath & FactoryDefaultsGroup()
Component of a configuration key path, for default state of MakeSettings()
const RegistryPath & CurrentSettingsGroup()
Component of a configuration key path, for last-used destructive settings.
EffectType
@ EffectTypeNone
std::optional< std::unique_ptr< EffectSettingsAccess::Message > > OptionalMessage
XO("Cut/Copy/Paste")
wxString RegistryPath
Definition: Identifier.h:218
wxString PluginPath
type alias for identifying a Plugin supplied by a module, each module defining its own interpretation...
Definition: Identifier.h:214
std::vector< RegistryPath > RegistryPaths
Definition: Identifier.h:219
IteratorRange< Iterator > make_iterator_range(const Iterator &i1, const Iterator &i2)
Definition: MemoryX.h:448
EffectReverbSettings preset
Definition: Reverb.cpp:44
#define S(N)
Definition: ToChars.cpp:64
static Settings & settings()
Definition: TrackInfo.cpp:87
The top-level handle to an Audacity project. It serves as a source of events that other objects can b...
Definition: Project.h:90
virtual ProgressResult Poll(unsigned long long numerator, unsigned long long denominator, const TranslatableString &message={})=0
Update the bar and poll for clicks. Call only on the main thread.
Generates EffectParameterMethods overrides from variadic template arguments.
CommandParameters, derived from wxFileConfig, is essentially doing the same things as the SettingsVis...
bool GetParameters(wxString &parms)
TranslatableString GetName() const
ComponentInterfaceSymbol pairs a persistent string identifier used internally with an optional,...
const wxString & Internal() const
int mNumTracks
Definition: EffectBase.h:142
double mT1
Definition: EffectBase.h:113
std::shared_ptr< TrackList > mOutputTracks
Definition: EffectBase.h:111
WaveTrackFactory * mFactory
Definition: EffectBase.h:107
TrackList * mTracks
Definition: EffectBase.h:130
std::vector< Track * > mOMap
Definition: EffectBase.h:140
double mProjectRate
Definition: EffectBase.h:105
BasicUI::ProgressDialog * mProgress
Definition: EffectBase.h:104
int mNumGroups
Definition: EffectBase.h:143
bool DoEffect(EffectSettings &settings, const InstanceFinder &finder, double projectRate, TrackList *list, WaveTrackFactory *factory, NotifyingSelectedRegion &selectedRegion, unsigned flags, const EffectSettingsAccessPtr &pAccess) override
Definition: EffectBase.cpp:59
std::vector< Track * > mIMap
Definition: EffectBase.h:139
double mT0
Definition: EffectBase.h:112
unsigned mUIFlags
Definition: EffectBase.h:119
const AudacityProject * FindProject() const
Definition: EffectBase.cpp:311
RealtimeSince
In which versions of Audacity was an effect realtime capable?
Base class for many of the effects in Audacity.
Definition: Effect.h:26
bool SaveSettings(const EffectSettings &settings, CommandParameters &parms) const override
Store settings as keys and values.
Definition: Effect.cpp:123
RealtimeSince RealtimeSupport() const override
Since which version of Audacity has the effect supported realtime?
Definition: Effect.cpp:94
const EffectSettingsManager & GetDefinition() const override
Definition: Effect.cpp:190
bool TrackGroupProgress(int whichGroup, double frac, const TranslatableString &={}) const
Definition: Effect.cpp:356
bool SaveUserPreset(const RegistryPath &name, const EffectSettings &settings) const override
Save settings in the configuration file as a user-named preset.
Definition: Effect.cpp:151
void SetBatchProcessing() override
Definition: Effect.cpp:307
OptionalMessage LoadFactoryDefaults(EffectSettings &settings) const override
Definition: Effect.cpp:173
bool VisitSettings(SettingsVisitor &visitor, EffectSettings &settings) override
Definition: Effect.cpp:110
bool SaveSettingsAsString(const EffectSettings &settings, wxString &parms) const override
Definition: Effect.cpp:209
void CopyInputTracks(bool allSyncLockSelected=false)
Definition: Effect.cpp:396
OptionalMessage LoadSettingsFromString(const wxString &parms, EffectSettings &settings) const override
Definition: Effect.cpp:227
Effect()
Definition: Effect.cpp:35
bool SupportsAutomation() const override
Whether the effect has any automatable controls.
Definition: Effect.cpp:99
bool LoadSettings(const CommandParameters &parms, EffectSettings &settings) const override
Restore settings from keys and values.
Definition: Effect.cpp:130
virtual const EffectParameterMethods & Parameters() const
Definition: Effect.cpp:104
TranslatableString GetDescription() const override
Definition: Effect.cpp:65
virtual NumericFormatSymbol GetSelectionFormat()
Definition: Effect.cpp:195
bool mIsBatch
Definition: Effect.h:159
double CalcPreviewInputLength(const EffectSettings &settings, double previewLength) const override
Default implementation returns previewLength
Definition: Effect.cpp:433
OptionalMessage LoadFactoryPreset(int id, EffectSettings &settings) const override
Definition: Effect.cpp:168
bool TotalProgress(double frac, const TranslatableString &={}) const
Definition: Effect.cpp:338
Track * AddToOutputTracks(const std::shared_ptr< Track > &t)
Definition: Effect.cpp:421
bool TrackProgress(int whichTrack, double frac, const TranslatableString &={}) const
Definition: Effect.cpp:346
bool HasOptions() const override
Definition: Effect.cpp:183
wxString GetVersion() const override
Definition: Effect.cpp:60
bool CheckWhetherSkipEffect(const EffectSettings &settings) const override
Default implementation returns false.
Definition: Effect.cpp:428
PluginPath GetPath() const override
Definition: Effect.cpp:45
EffectType GetType() const override
Type determines how it behaves.
Definition: Effect.cpp:72
wxString GetSavedStateGroup()
Definition: Effect.cpp:202
bool IsBatchProcessing() const override
Definition: Effect.cpp:302
bool Delegate(Effect &delegate, EffectSettings &settings)
Re-invoke DoEffect on another Effect object that implements the work.
Definition: Effect.cpp:329
virtual ~Effect()
Definition: Effect.cpp:39
VendorSymbol GetVendor() const override
Definition: Effect.cpp:55
OptionalMessage LoadUserPreset(const RegistryPath &name, EffectSettings &settings) const override
Definition: Effect.cpp:138
bool CanExportPresets() const override
Whether the effect supports export of presets to files, and importing too.
Definition: Effect.cpp:178
ComponentInterfaceSymbol GetSymbol() const override
Definition: Effect.cpp:50
EffectFamilySymbol GetFamily() const override
Report identifier and user-visible name of the effect protocol.
Definition: Effect.cpp:77
void UnsetBatchProcessing() override
Definition: Effect.cpp:318
bool IsDefault() const override
Whether the effect sorts "above the line" in the menus.
Definition: Effect.cpp:89
unsigned TestUIFlags(unsigned mask)
Definition: Effect.cpp:298
RegistryPaths GetFactoryPresets() const override
Report names of factory presets.
Definition: Effect.cpp:163
void GetBounds(const WaveTrack &track, const WaveTrack *pRight, sampleCount *start, sampleCount *len)
Definition: Effect.cpp:366
bool IsInteractive() const override
Whether the effect needs a dialog for entry of settings.
Definition: Effect.cpp:84
Interface for manipulations of an Effect's settings.
virtual bool Set(Effect &effect, const CommandParameters &parms, EffectSettings &settings) const =0
virtual void Get(const Effect &effect, const EffectSettings &settings, CommandParameters &parms) const =0
virtual void Visit(Effect &effect, SettingsVisitor &visitor, EffectSettings &settings) const =0
static const wxString kUserPresetIdent
Definition: EffectPlugin.h:40
static const wxString kFactoryPresetIdent
Definition: EffectPlugin.h:41
static const wxString kCurrentSettingsIdent
Definition: EffectPlugin.h:42
static const wxString kFactoryDefaultsIdent
Definition: EffectPlugin.h:43
EffectSettingsManager is an EffectDefinitionInterface that adds a factory function for EffectSettings...
virtual EffectSettings MakeSettings() const
Abstract base class used in importing a file.
bool setTimes(double t0, double t1)
Definition: ViewInfo.cpp:51
static NumericFormatSymbol HoursMinsSecondsFormat()
const NumericFormatSymbol & GetSelectionFormat() const
static ProjectNumericFormats & Get(AudacityProject &project)
sampleCount TimeToLongSamples(double t0) const
Convert correctly between an (absolute) time in seconds and a number of samples.
Definition: SampleTrack.cpp:43
Visitor of effect or command parameters. This is a base class with lots of virtual functions that do ...
SettingsVisitor that gets parameter values into a string.
SettingsVisitor that sets parameters to a value (from a string)
static bool IsSelectedOrSyncLockSelected(const Track *pTrack)
Definition: SyncLock.cpp:112
Abstract base class for an object holding data associated with points on a time axis.
Definition: Track.h:226
static std::shared_ptr< TrackList > Create(AudacityProject *pOwner)
Definition: Track.cpp:503
auto Any() -> TrackIterRange< TrackType >
Definition: Track.h:1440
Holds a msgid for the translation catalog; may also bind format arguments.
A Track that contains audio waveform data.
Definition: WaveTrack.h:51
double GetStartTime() const override
Get the time at which the first clip in the track starts.
Definition: WaveTrack.cpp:1802
double GetEndTime() const override
Get the time at which the last clip in the track ends, plus recorded stuff.
Definition: WaveTrack.cpp:1822
Positions or offsets within audio files need a wide type.
Definition: SampleCount.h:19
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:274
auto end(const Ptr< Type, BaseDeleter > &p)
Enables range-for.
Definition: PackedArray.h:159
bool SetConfig(const EffectDefinitionInterface &ident, ConfigurationType type, const RegistryPath &group, const RegistryPath &key, const Value &value)
bool GetConfig(const EffectDefinitionInterface &ident, ConfigurationType type, const RegistryPath &group, const RegistryPath &key, Value &var, const Value &defval)
MessageBoxOptions && Caption(TranslatableString caption_) &&
Definition: BasicUI.h:100
Externalized state of a plug-in.