Audacity 3.2.0
RealtimeEffectState.h
Go to the documentation of this file.
1/**********************************************************************
2
3 Audacity: A Digital Audio Editor
4
5 @file RealtimeEffectState.h
6
7 Paul Licameli split from RealtimeEffectManager.cpp
8
9 *********************************************************************/
10
11#ifndef __AUDACITY_REALTIMEEFFECTSTATE_H__
12#define __AUDACITY_REALTIMEEFFECTSTATE_H__
13
14#include <atomic>
15#include <cstddef>
16#include <optional>
17#include <unordered_map>
18#include <vector>
19#include "ClientData.h"
20#include "EffectInterface.h"
21#include "GlobalVariable.h"
22#include "MemoryX.h"
23#include "Observer.h"
24#include "PluginProvider.h" // for PluginID
25#include "XMLTagHandler.h"
26
27class ChannelGroup;
29
31
32class REALTIME_EFFECTS_API RealtimeEffectState
33 : public XMLTagHandler
34 , public std::enable_shared_from_this<RealtimeEffectState>
35 , public SharedNonInterfering<RealtimeEffectState>
36 , public ClientData::Site<RealtimeEffectState>
37 , public Observer::Publisher<RealtimeEffectStateChange>
38{
39public:
40 struct REALTIME_EFFECTS_API EffectFactory : GlobalHook<EffectFactory,
41 const EffectInstanceFactory *(const PluginID &)
42 >{};
43
44 explicit RealtimeEffectState(const PluginID & id);
48
50
53 void SetID(const PluginID & id);
54 const PluginID& GetID() const noexcept;
56 const EffectInstanceFactory *GetEffect();
58 const EffectInstanceFactory *GetEffect() const { return mPlugin; }
59
61
64 std::shared_ptr<EffectInstance> GetInstance();
65
67 const EffectOutputs *GetOutputs() const { return mMovedOutputs.get(); }
68
70 std::shared_ptr<EffectInstance> Initialize(double rate);
72 std::shared_ptr<EffectInstance>
73 AddGroup(
74 const ChannelGroup *group, unsigned chans, float sampleRate);
76
77 bool ProcessStart(bool running);
79
82 size_t Process(const ChannelGroup *group,
83 unsigned chans, // How many channels the playback device needs
84 const float *const *inbuf,
85 float *const *outbuf,
86 float *dummybuf,
87 size_t numSamples);
89 bool ProcessEnd();
90
91 const EffectSettings &GetSettings() const { return mMainSettings.settings; }
92
94 bool IsEnabled() const noexcept;
95
97 bool IsActive() const noexcept;
98
100 void SetActive(bool active);
101
103 bool Finalize() noexcept;
104
105 static const std::string &XMLTag();
106 bool HandleXMLTag(
107 const std::string_view &tag, const AttributesList &attrs) override;
108 void HandleXMLEndTag(const std::string_view &tag) override;
109 XMLTagHandler *HandleXMLChild(const std::string_view &tag) override;
110 void WriteXML(XMLWriter &xmlFile);
111
114
117 std::shared_ptr<EffectSettingsAccess> GetAccess();
118
119private:
120
121 std::shared_ptr<EffectInstance> MakeInstance();
122 std::shared_ptr<EffectInstance> EnsureInstance(double rate);
123
124 struct Access;
125 struct AccessState;
126
127 AccessState *GetAccessState() const
128 {
129 return mpAccessState.load(std::memory_order_relaxed);
130 }
132 {
133 return mpAccessState.load(std::memory_order_acquire);
134 }
135
137
139 std::weak_ptr<EffectInstance> mwInstance;
141 const EffectInstanceFactory *mPlugin{};
142
144 using Counter = unsigned char;
145
147 Counter counter{ 0 };
148
150 settings.swap(other.settings);
151 std::swap(counter, other.counter);
152 }
153 };
154
155 struct Response {
156 using Counter = unsigned char;
157
158 Counter counter{ 0 };
159 std::unique_ptr<EffectOutputs> pOutputs;
160 };
161
164 std::unique_ptr<EffectInstance::Message> mMessage;
165 std::unique_ptr<EffectOutputs> mMovedOutputs;
166
174 std::unique_ptr<EffectInstance::Message> mMovedMessage;
175 std::unique_ptr<EffectOutputs> mOutputs;
176
178 std::optional<EffectInstance::SampleCount> mLatency;
180 bool mLastActive{};
181
183
188 std::unordered_map<const ChannelGroup *, std::pair<size_t, double>>
190
191 // This must not be reset to nullptr while a worker thread is running.
192 // In fact it is never yet reset to nullptr, before destruction.
193 // Destroy before mWorkerSettings:
194 AtomicUniquePointer<AccessState> mpAccessState{ nullptr };
195
196 wxString mParameters; // Used only during deserialization
197 size_t mCurrentProcessor{ 0 };
198 bool mInitialized{ false };
199
201};
202
203#endif // __AUDACITY_REALTIMEEFFECTSTATE_H__
Utility ClientData::Site to register hooks into a host class that attach client data.
wxString PluginID
static PluginID GetID(EffectPlugin &effect)
Definition: EffectUI.cpp:39
Generalized interface for discovery of plug-ins for one protocol.
RealtimeEffectStateChange
static Settings & settings()
Definition: TrackInfo.cpp:51
std::vector< Attribute > AttributesList
Definition: XMLTagHandler.h:40
Utility to register hooks into a host class that attach client data.
Definition: ClientData.h:229
Site & operator=(const Site &other)
Definition: ClientData.h:251
Performs effect computation.
Hold values to send to effect output meters.
Global function-valued variable, adding a convenient Call()
An object that sends messages to an open-ended list of subscribed callbacks.
Definition: Observer.h:108
Mediator of two-way inter-thread communication of changes of settings.
const EffectSettings & GetSettings() const
NonInterfering< SettingsAndCounter > mWorkerSettings
NonInterfering< SettingsAndCounter > mMainSettings
Updated immediately by Access::Set in the main thread.
const EffectOutputs * GetOutputs() const
Get locations that a GUI can connect meters to.
std::optional< EffectInstance::SampleCount > mLatency
How many samples must be discarded.
std::unordered_map< const ChannelGroup *, std::pair< size_t, double > > mGroups
std::unique_ptr< EffectOutputs > mOutputs
AccessState * TestAccessState() const
RealtimeEffectState(const RealtimeEffectState &other)=delete
std::unique_ptr< EffectInstance::Message > mMessage
std::unique_ptr< EffectInstance::Message > mMovedMessage
std::weak_ptr< EffectInstance > mwInstance
Stateful instance made by the plug-in.
std::unique_ptr< EffectOutputs > mMovedOutputs
This class is an interface which should be implemented by classes which wish to be able to load and s...
Definition: XMLTagHandler.h:42
Base class for XMLFileWriter and XMLStringWriter that provides the general functionality for creating...
Definition: XMLWriter.h:25
FrameStatistics & GetInstance() noexcept
void swap(std::unique_ptr< Alg_seq > &a, std::unique_ptr< Alg_seq > &b)
Definition: NoteTrack.cpp:634
STL namespace.
Externalized state of a plug-in.
std::unique_ptr< EffectOutputs > pOutputs
void swap(SettingsAndCounter &other)
Workaround for std::make_shared not working on macOs with over-alignment.
Definition: MemoryX.h:298