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#include "EffectOutputTracks.h"
19
20#include <algorithm>
21
22#include <wx/defs.h>
23
24#include "BasicUI.h"
25#include "ConfigInterface.h"
27#include "ShuttleAutomation.h"
28#include "SyncLock.h"
29#include "ViewInfo.h"
30#include "WaveTrack.h"
31#include "wxFileNameWrapper.h"
33
35{
36}
37
39{
40}
41
42// ComponentInterface implementation
43
45{
47}
48
50{
51 return {};
52}
53
55{
56 return XO("Audacity");
57}
58
59wxString Effect::GetVersion() const
60{
61 return AUDACITY_VERSION_STRING;
62}
63
65{
66 return {};
67}
68
70{
71 // Unusually, the internal and visible strings differ for the built-in
72 // effect family.
73 return { wxT("Audacity"), XO("Built-in") };
74}
75
77{
78 return true;
79}
80
82{
83 return true;
84}
85
87{
89}
90
92{
93 return true;
94}
95
97{
98 static const CapturedParameters<Effect> empty;
99 return empty;
100}
101
103{
104 Parameters().Visit(*this, visitor, settings);
105 return true;
106}
107
109 ConstSettingsVisitor &visitor, const EffectSettings &settings) const
110{
111 Parameters().Visit(*this, visitor, settings);
112 return true;
113}
114
116 const EffectSettings &settings, CommandParameters & parms) const
117{
118 Parameters().Get( *this, settings, parms );
119 return true;
120}
121
123 const CommandParameters & parms, EffectSettings &settings) const
124{
125 // The first argument, and with it the const_cast, will disappear when
126 // all built-in effects are stateless.
127 return Parameters().Set( *const_cast<Effect*>(this), parms, settings );
128}
129
132{
133 // Find one string in the registry and then reinterpret it
134 // as complete settings
135 wxString parms;
137 name, wxT("Parameters"), parms))
138 return {};
139
140 return LoadSettingsFromString(parms, settings);
141}
142
144 const RegistryPath & name, const EffectSettings &settings) const
145{
146 // Save all settings as a single string value in the registry
147 wxString parms;
148 if (!SaveSettingsAsString(settings, parms))
149 return false;
150
152 name, wxT("Parameters"), parms);
153}
154
156{
157 return {};
158}
159
161{
162 return { nullptr };
163}
164
166{
168}
169
171{
172 return true;
173}
174
176{
177 return false;
178}
179
180// EffectPlugin implementation
181
183{
184 return *this;
185}
186
188{
189 if( !IsBatchProcessing() && FindProject() )
193}
194
196{
197 return wxT("SavedState");
198}
199
200// Effect implementation
201
203 const EffectSettings &settings, wxString & parms) const
204{
207 S.mpEap = &eap;
208 if( VisitSettings( S, settings ) ){
209 ;// got eap value using VisitSettings.
210 }
211 // Won't be needed in future
212 else if (!SaveSettings(settings, eap))
213 {
214 return false;
215 }
216
217 return eap.GetParameters(parms);
218}
219
221 const wxString & parms, EffectSettings &settings) const
222{
223 // If the string starts with one of certain significant substrings,
224 // then the rest of the string is reinterpreted as part of a registry key,
225 // and a user or factory preset is then loaded.
226 // (Where did these prefixes come from? See EffectPresetsDialog; and
227 // ultimately the uses of it by EffectManager::GetPreset, which is used by
228 // the macro management dialog)
229 wxString preset = parms;
230 OptionalMessage result;
231 if (preset.StartsWith(kUserPresetIdent))
232 {
233 preset.Replace(kUserPresetIdent, wxEmptyString, false);
235 }
236 else if (preset.StartsWith(kFactoryPresetIdent))
237 {
238 preset.Replace(kFactoryPresetIdent, wxEmptyString, false);
239 auto presets = GetFactoryPresets();
240 result = LoadFactoryPreset(
241 make_iterator_range( presets ).index( preset ), settings );
242 }
243 else if (preset.StartsWith(kCurrentSettingsIdent))
244 {
245 preset.Replace(kCurrentSettingsIdent, wxEmptyString, false);
247 }
248 else if (preset.StartsWith(kFactoryDefaultsIdent))
249 {
250 preset.Replace(kFactoryDefaultsIdent, wxEmptyString, false);
252 }
253 else
254 {
255 // If the string did not start with any of the significant substrings,
256 // then use VisitSettings or LoadSettings to reinterpret it,
257 // or use LoadSettings.
258 // This interprets what was written by SaveSettings, above.
259 CommandParameters eap(parms);
261 S.SetForValidating( &eap );
262 // VisitSettings returns false if not defined for this effect.
263 // To do: fix const_cast in use of VisitSettings
264 if ( !const_cast<Effect*>(this)->VisitSettings(S, settings) ) {
265 // the old method...
266 if (LoadSettings(eap, settings))
267 return { nullptr };
268 }
269 else if( !S.bOK )
270 result = {};
271 else{
272 result = { nullptr };
273 S.SetForWriting( &eap );
274 const_cast<Effect*>(this)->VisitSettings(S, settings);
275 }
276 }
277
278 if (!result)
279 {
280 using namespace BasicUI;
282 XO("%s: Could not load settings below. Default settings will be used.\n\n%s")
283 .Format( GetName(), preset ),
285 // We are using default settings and we still wish to continue.
286 result = { nullptr };
287 }
288 return result;
289}
290
291unsigned Effect::TestUIFlags(unsigned mask) {
292 return mask & mUIFlags;
293}
294
296{
297 return mIsBatch;
298}
299
301{
302 mIsBatch = true;
303 // Save effect's internal state in a special registry path
304 // just for this purpose
305 // If effect is not stateful, this step doesn't really matter, and the
306 // settings object is a dummy
307 auto dummySettings = MakeSettings();
308 SaveUserPreset(GetSavedStateGroup(), dummySettings);
309}
310
312{
313 mIsBatch = false;
314 // Restore effect's internal state from registry
315 // If effect is not stateful, this call doesn't really matter, and the
316 // settings object is a dummy
317 auto dummySettings = MakeSettings();
318 // Ignore failure
319 (void ) LoadUserPreset(GetSavedStateGroup(), dummySettings);
320}
321
324{
325 if (!finder)
326 finder = DefaultInstanceFinder(delegate);
327
329 region.setTimes( mT0, mT1 );
330
331 return delegate.DoEffect(settings, finder, mProjectRate, mTracks.get(),
332 mFactory, region, mUIFlags, nullptr);
333}
334
335bool Effect::TotalProgress(double frac, const TranslatableString &msg) const
336{
337 auto updateResult = (mProgress ?
338 mProgress->Poll(frac * 1000, 1000, msg) :
340 return (updateResult != BasicUI::ProgressResult::Success);
341}
342
344 int whichTrack, double frac, const TranslatableString &msg) const
345{
346 auto updateResult = (mProgress ?
347 mProgress->Poll((whichTrack + frac) * 1000,
348 (double) mNumTracks * 1000, msg) :
350 return (updateResult != BasicUI::ProgressResult::Success);
351}
352
354 int whichGroup, double frac, const TranslatableString &msg) const
355{
356 auto updateResult = (mProgress ?
357 mProgress->Poll((whichGroup + frac) * 1000,
358 (double) mNumGroups * 1000, msg) :
360 return (updateResult != BasicUI::ProgressResult::Success);
361}
362
364 const WaveTrack &track, sampleCount *start, sampleCount *len)
365{
366 const auto t0 = std::max(mT0, track.GetStartTime());
367 const auto t1 = std::min(mT1, track.GetEndTime());
368 if (t1 > t0) {
369 *start = track.TimeToLongSamples(t0);
370 auto end = track.TimeToLongSamples(t1);
371 *len = end - *start;
372 }
373 else {
374 *start = 0;
375 *len = 0;
376 }
377}
378
380{
381 return false;
382}
383
385 const EffectSettings &, double previewLength) const
386{
387 return previewLength;
388}
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.
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: IteratorX.h:210
EffectReverbSettings preset
Definition: Reverb.cpp:44
#define S(N)
Definition: ToChars.cpp:64
static Settings & settings()
Definition: TrackInfo.cpp:47
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:108
double mT1
Definition: EffectBase.h:114
WaveTrackFactory * mFactory
Definition: EffectBase.h:112
double mProjectRate
Definition: EffectBase.h:110
BasicUI::ProgressDialog * mProgress
Definition: EffectBase.h:109
int mNumGroups
Definition: EffectBase.h:125
bool DoEffect(EffectSettings &settings, const InstanceFinder &finder, double projectRate, TrackList *list, WaveTrackFactory *factory, NotifyingSelectedRegion &selectedRegion, unsigned flags, const EffectSettingsAccessPtr &pAccess) override
Definition: EffectBase.cpp:57
std::shared_ptr< TrackList > mTracks
Definition: EffectBase.h:107
static InstanceFinder DefaultInstanceFinder(EffectPlugin &plugin)
Definition: EffectBase.cpp:249
double mT0
Definition: EffectBase.h:113
unsigned mUIFlags
Definition: EffectBase.h:98
const AudacityProject * FindProject() const
Definition: EffectBase.cpp:220
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:115
RealtimeSince RealtimeSupport() const override
Since which version of Audacity has the effect supported realtime?
Definition: Effect.cpp:86
const EffectSettingsManager & GetDefinition() const override
Definition: Effect.cpp:182
bool TrackGroupProgress(int whichGroup, double frac, const TranslatableString &={}) const
Definition: Effect.cpp:353
bool SaveUserPreset(const RegistryPath &name, const EffectSettings &settings) const override
Save settings in the configuration file as a user-named preset.
Definition: Effect.cpp:143
void SetBatchProcessing() override
Definition: Effect.cpp:300
OptionalMessage LoadFactoryDefaults(EffectSettings &settings) const override
Definition: Effect.cpp:165
bool Delegate(Effect &delegate, EffectSettings &settings, InstanceFinder finder={})
Re-invoke DoEffect on another Effect object that implements the work.
Definition: Effect.cpp:322
bool VisitSettings(SettingsVisitor &visitor, EffectSettings &settings) override
Definition: Effect.cpp:102
bool SaveSettingsAsString(const EffectSettings &settings, wxString &parms) const override
Definition: Effect.cpp:202
void GetBounds(const WaveTrack &track, sampleCount *start, sampleCount *len)
Definition: Effect.cpp:363
OptionalMessage LoadSettingsFromString(const wxString &parms, EffectSettings &settings) const override
Definition: Effect.cpp:220
Effect()
Definition: Effect.cpp:34
virtual NumericFormatID GetSelectionFormat()
Definition: Effect.cpp:187
bool SupportsAutomation() const override
Whether the effect has any automatable controls.
Definition: Effect.cpp:91
bool LoadSettings(const CommandParameters &parms, EffectSettings &settings) const override
Restore settings from keys and values.
Definition: Effect.cpp:122
virtual const EffectParameterMethods & Parameters() const
Definition: Effect.cpp:96
TranslatableString GetDescription() const override
Definition: Effect.cpp:64
bool mIsBatch
Definition: Effect.h:148
double CalcPreviewInputLength(const EffectSettings &settings, double previewLength) const override
Default implementation returns previewLength
Definition: Effect.cpp:384
OptionalMessage LoadFactoryPreset(int id, EffectSettings &settings) const override
Definition: Effect.cpp:160
bool TotalProgress(double frac, const TranslatableString &={}) const
Definition: Effect.cpp:335
bool TrackProgress(int whichTrack, double frac, const TranslatableString &={}) const
Definition: Effect.cpp:343
bool HasOptions() const override
Definition: Effect.cpp:175
wxString GetVersion() const override
Definition: Effect.cpp:59
bool CheckWhetherSkipEffect(const EffectSettings &settings) const override
Default implementation returns false.
Definition: Effect.cpp:379
PluginPath GetPath() const override
Definition: Effect.cpp:44
wxString GetSavedStateGroup()
Definition: Effect.cpp:195
bool IsBatchProcessing() const override
Definition: Effect.cpp:295
virtual ~Effect()
Definition: Effect.cpp:38
VendorSymbol GetVendor() const override
Definition: Effect.cpp:54
OptionalMessage LoadUserPreset(const RegistryPath &name, EffectSettings &settings) const override
Definition: Effect.cpp:130
bool CanExportPresets() const override
Whether the effect supports export of presets to files, and importing too.
Definition: Effect.cpp:170
ComponentInterfaceSymbol GetSymbol() const override
Definition: Effect.cpp:49
EffectFamilySymbol GetFamily() const override
Report identifier and user-visible name of the effect protocol.
Definition: Effect.cpp:69
void UnsetBatchProcessing() override
Definition: Effect.cpp:311
bool IsDefault() const override
Whether the effect sorts "above the line" in the menus.
Definition: Effect.cpp:81
unsigned TestUIFlags(unsigned mask)
Definition: Effect.cpp:291
RegistryPaths GetFactoryPresets() const override
Report names of factory presets.
Definition: Effect.cpp:155
bool IsInteractive() const override
Whether the effect needs a dialog for entry of settings.
Definition: Effect.cpp:76
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
std::function< std::optional< InstancePointer >(EffectSettings &settings) > InstanceFinder
Definition: EffectPlugin.h:72
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
NumericFormatID GetSelectionFormat() const
static ProjectNumericFormats & Get(AudacityProject &project)
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)
Holds a msgid for the translation catalog; may also bind format arguments.
A Track that contains audio waveform data.
Definition: WaveTrack.h:203
double GetStartTime() const override
Implement WideSampleSequence.
Definition: WaveTrack.cpp:2576
double GetEndTime() const override
Implement WideSampleSequence.
Definition: WaveTrack.cpp:2586
sampleCount TimeToLongSamples(double t0) const
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:279
NUMERIC_FORMATS_API NumericFormatSymbol HoursMinsSecondsFormat()
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)
const char * end(const char *str) noexcept
Definition: StringUtils.h:106
MessageBoxOptions && Caption(TranslatableString caption_) &&
Definition: BasicUI.h:101
Externalized state of a plug-in.