1 /**********************************************************************
2 
3   Audacity: A Digital Audio Editor
4 
5   EffectManager.h
6 
7   Audacity(R) is copyright (c) 1999-2008 Audacity Team.
8   License: GPL v2.  See License.txt.
9 
10 **********************************************************************/
11 
12 #ifndef __AUDACITY_EFFECTMANAGER__
13 #define __AUDACITY_EFFECTMANAGER__
14 
15 #include <memory>
16 #include <vector>
17 
18 #include <unordered_map>
19 #include "EffectInterface.h"
20 #include "Identifier.h"
21 
22 class AudacityCommand;
23 class AudacityProject;
24 class CommandContext;
25 class CommandMessageTarget;
26 class ComponentInterfaceSymbol;
27 class Effect;
28 class TrackList;
29 class SelectedRegion;
30 class wxString;
31 typedef wxString PluginID;
32 
33 using EffectMap = std::unordered_map<wxString, Effect *>;
34 using AudacityCommandMap = std::unordered_map<wxString, AudacityCommand *>;
35 using EffectOwnerMap = std::unordered_map< wxString, std::shared_ptr<Effect> >;
36 
37 #if defined(EXPERIMENTAL_EFFECTS_RACK)
38 class EffectRack;
39 #endif
40 class AudacityCommand;
41 
42 
43 class NotifyingSelectedRegion;
44 
45 class AUDACITY_DLL_API EffectManager
46 {
47 public:
48 
49    enum : unsigned {
50       // No flags specified
51       kNone = 0x00,
52       // Flag used to disable prompting for configuration parameteres.
53       kConfigured = 0x01,
54       // Flag used to disable saving the state after processing.
55       kSkipState = 0x02,
56       // Flag used to disable "Repeat Last Effect"
57       kDontRepeatLast = 0x04,
58       // Flag used to disable "Select All during Repeat Generator Effect"
59       kRepeatGen = 0x08,
60       // Flag used for repeating Nyquist Prompt
61       kRepeatNyquistPrompt = 0x10,
62    };
63 
64    /** Get the singleton instance of the EffectManager. Probably not safe
65        for multi-thread use. */
66    static EffectManager & Get();
67 
68 //
69 // public methods
70 //
71 // Used by the outside program to register the list of effects and retrieve
72 // them by index number, usually when the user selects one from a menu.
73 //
74 public:
75    EffectManager();
76    virtual ~EffectManager();
77 
78    //! Here solely for the purpose of Nyquist Workbench until a better solution is devised.
79    /** Register an effect so it can be executed. */
80    const PluginID & RegisterEffect(std::unique_ptr<Effect> uEffect);
81    //! Used only by Nyquist Workbench module
82    void UnregisterEffect(const PluginID & ID);
83 
84    TranslatableString GetEffectFamilyName(const PluginID & ID);
85    TranslatableString GetVendorName(const PluginID & ID);
86 
87    /** Run a command given the plugin ID */
88    // Returns true on success.
89    bool DoAudacityCommand(const PluginID & ID,
90                          const CommandContext &,
91                          wxWindow *parent,
92                          bool shouldPrompt  = true );
93 
94    // Renamed from 'Effect' to 'Command' prior to moving out of this class.
95    ComponentInterfaceSymbol GetCommandSymbol(const PluginID & ID);
96    TranslatableString GetCommandName(const PluginID & ID);
97    CommandID GetCommandIdentifier(const PluginID & ID);
98    TranslatableString GetCommandDescription(const PluginID & ID);
99    ManualPageID GetCommandUrl(const PluginID & ID);
100    TranslatableString GetCommandTip(const PluginID & ID);
101    // flags control which commands are included.
102    void GetCommandDefinition(const PluginID & ID, const CommandContext & context, int flags);
103    bool IsHidden(const PluginID & ID);
104 
105    /** Support for batch commands */
106    bool SupportsAutomation(const PluginID & ID);
107    wxString GetEffectParameters(const PluginID & ID);
108    bool SetEffectParameters(const PluginID & ID, const wxString & params);
109    bool PromptUser( const PluginID & ID,
110       const EffectClientInterface::EffectDialogFactory &factory,
111       wxWindow &parent );
112    bool HasPresets(const PluginID & ID);
113    wxString GetPreset(const PluginID & ID, const wxString & params, wxWindow * parent);
114    wxString GetDefaultPreset(const PluginID & ID);
115 
116 private:
117    void SetBatchProcessing(const PluginID & ID, bool start);
118    struct UnsetBatchProcessing {
119       PluginID mID;
operatorUnsetBatchProcessing120       void operator () (EffectManager *p) const
121          { if(p) p->SetBatchProcessing(mID, false); }
122    };
123    using BatchProcessingScope =
124       std::unique_ptr< EffectManager, UnsetBatchProcessing >;
125 public:
126    // RAII for the function above
SetBatchProcessing(const PluginID & ID)127    BatchProcessingScope SetBatchProcessing(const PluginID &ID)
128    {
129       SetBatchProcessing(ID, true); return BatchProcessingScope{ this, {ID} };
130    }
131 
132    /** Allow effects to disable saving the state at run time */
133    void SetSkipStateFlag(bool flag);
134    bool GetSkipStateFlag();
135 
136    const PluginID & GetEffectByIdentifier(const CommandID & strTarget);
137 
138    /** Return an effect by its ID. */
139    Effect *GetEffect(const PluginID & ID);
140 
141 private:
142    AudacityCommand *GetAudacityCommand(const PluginID & ID);
143 
144    EffectMap mEffects;
145    AudacityCommandMap mCommands;
146    EffectOwnerMap mHostEffects;
147 
148    int mNumEffects;
149 
150    // Set true if we want to skip pushing state
151    // after processing at effect run time.
152    bool mSkipStateFlag;
153 
154 #if defined(EXPERIMENTAL_EFFECTS_RACK)
155    friend class EffectRack;
156 #endif
157 
158 };
159 
160 #endif
161