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
75 std::shared_ptr<EffectInstance>
76 AddGroup(
77 const ChannelGroup &group, unsigned chans, float sampleRate);
79
80 bool ProcessStart(bool running);
82
85 size_t Process(const ChannelGroup &group,
86 unsigned chans, // How many channels the playback device needs
87 const float *const *inbuf,
88 float *const *outbuf,
89 float *dummybuf,
90 size_t numSamples);
92 bool ProcessEnd();
93
94 const EffectSettings &GetSettings() const { return mMainSettings.settings; }
95
97 bool IsEnabled() const noexcept;
98
100 bool IsActive() const noexcept;
101
103 void SetActive(bool active);
104
106 bool Finalize() noexcept;
107
108 static const std::string &XMLTag();
109 bool HandleXMLTag(
110 const std::string_view &tag, const AttributesList &attrs) override;
111 void HandleXMLEndTag(const std::string_view &tag) override;
112 XMLTagHandler *HandleXMLChild(const std::string_view &tag) override;
113 void WriteXML(XMLWriter &xmlFile);
114
117
120 std::shared_ptr<EffectSettingsAccess> GetAccess();
121
122private:
123
124 std::shared_ptr<EffectInstance> MakeInstance();
125 std::shared_ptr<EffectInstance> EnsureInstance(double rate);
126
127 struct Access;
128 struct AccessState;
129
130 AccessState *GetAccessState() const
131 {
132 return mpAccessState.load(std::memory_order_relaxed);
133 }
135 {
136 return mpAccessState.load(std::memory_order_acquire);
137 }
138
140
142 std::weak_ptr<EffectInstance> mwInstance;
144 const EffectInstanceFactory *mPlugin{};
145
147 using Counter = unsigned char;
148
150 Counter counter{ 0 };
151
153 settings.swap(other.settings);
154 std::swap(counter, other.counter);
155 }
156 };
157
158 struct Response {
159 using Counter = unsigned char;
160
161 Counter counter{ 0 };
162 std::unique_ptr<EffectOutputs> pOutputs;
163 };
164
167 std::unique_ptr<EffectInstance::Message> mMessage;
168 std::unique_ptr<EffectOutputs> mMovedOutputs;
169
177 std::unique_ptr<EffectInstance::Message> mMovedMessage;
178 std::unique_ptr<EffectOutputs> mOutputs;
179
181 std::optional<EffectInstance::SampleCount> mLatency;
183 bool mLastActive{};
184
186
191 std::unordered_map<const ChannelGroup *, std::pair<size_t, double>>
193
194 // This must not be reset to nullptr while a worker thread is running.
195 // In fact it is never yet reset to nullptr, before destruction.
196 // Destroy before mWorkerSettings:
197 AtomicUniquePointer<AccessState> mpAccessState{ nullptr };
198
199 wxString mParameters; // Used only during deserialization
200 size_t mCurrentProcessor{ 0 };
201 bool mInitialized{ false };
202
204};
205
206#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:38
Generalized interface for discovery of plug-ins for one protocol.
RealtimeEffectStateChange
static Settings & settings()
Definition: TrackInfo.cpp:69
std::vector< Attribute > AttributesList
Definition: XMLTagHandler.h:40
Utility to register hooks into a host class that attach client data.
Definition: ClientData.h:228
Site & operator=(const Site &other)
Definition: ClientData.h:250
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:645
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:521