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
28class Track;
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 AddTrack(const Track &track, unsigned chans, float sampleRate);
75
76 bool ProcessStart(bool running);
78
81 size_t Process(const Track &track,
82 unsigned chans, // How many channels the playback device needs
83 const float *const *inbuf,
84 float *const *outbuf,
85 float *dummybuf,
86 size_t numSamples);
88 bool ProcessEnd();
89
90 const EffectSettings &GetSettings() const { return mMainSettings.settings; }
91
93 bool IsEnabled() const noexcept;
94
96 bool IsActive() const noexcept;
97
99 void SetActive(bool active);
100
102 bool Finalize() noexcept;
103
104 static const std::string &XMLTag();
105 bool HandleXMLTag(
106 const std::string_view &tag, const AttributesList &attrs) override;
107 void HandleXMLEndTag(const std::string_view &tag) override;
108 XMLTagHandler *HandleXMLChild(const std::string_view &tag) override;
109 void WriteXML(XMLWriter &xmlFile);
110
113
116 std::shared_ptr<EffectSettingsAccess> GetAccess();
117
118private:
119
120 std::shared_ptr<EffectInstance> MakeInstance();
121 std::shared_ptr<EffectInstance> EnsureInstance(double rate);
122
123 struct Access;
124 struct AccessState;
125
126 AccessState *GetAccessState() const
127 {
128 return mpAccessState.load(std::memory_order_relaxed);
129 }
131 {
132 return mpAccessState.load(std::memory_order_acquire);
133 }
134
136
138 std::weak_ptr<EffectInstance> mwInstance;
140 const EffectInstanceFactory *mPlugin{};
141
143 using Counter = unsigned char;
144
146 Counter counter{ 0 };
147
149 settings.swap(other.settings);
150 std::swap(counter, other.counter);
151 }
152 };
153
154 struct Response {
155 using Counter = unsigned char;
156
157 Counter counter{ 0 };
158 std::unique_ptr<EffectOutputs> pOutputs;
159 };
160
163 std::unique_ptr<EffectInstance::Message> mMessage;
164 std::unique_ptr<EffectOutputs> mMovedOutputs;
165
173 std::unique_ptr<EffectInstance::Message> mMovedMessage;
174 std::unique_ptr<EffectOutputs> mOutputs;
175
177 std::optional<EffectInstance::SampleCount> mLatency;
179 bool mLastActive{};
180
182
187 std::unordered_map<const Track *, std::pair<size_t, double>> mGroups;
188
189 // This must not be reset to nullptr while a worker thread is running.
190 // In fact it is never yet reset to nullptr, before destruction.
191 // Destroy before mWorkerSettings:
192 AtomicUniquePointer<AccessState> mpAccessState{ nullptr };
193
194 wxString mParameters; // Used only during deserialization
195 size_t mCurrentProcessor{ 0 };
196 bool mInitialized{ false };
197
199};
200
201#endif // __AUDACITY_REALTIMEEFFECTSTATE_H__
Utility ClientData::Site to register hooks into a host class that attach client data.
wxString PluginID
Definition: EffectManager.h:30
static PluginID GetID(EffectPlugin &effect)
Definition: EffectUI.cpp:33
Generalized interface for discovery of plug-ins for one protocol.
RealtimeEffectStateChange
static Settings & settings()
Definition: TrackInfo.cpp:87
std::vector< Attribute > AttributesList
Definition: XMLTagHandler.h:40
Utility to register hooks into a host class that attach client data.
Definition: ClientData.h:220
Site & operator=(const Site &other)
Definition: ClientData.h:242
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::unique_ptr< EffectOutputs > mOutputs
AccessState * TestAccessState() const
RealtimeEffectState(const RealtimeEffectState &other)=delete
std::unique_ptr< EffectInstance::Message > mMessage
std::unordered_map< const Track *, std::pair< size_t, double > > mGroups
std::unique_ptr< EffectInstance::Message > mMovedMessage
std::weak_ptr< EffectInstance > mwInstance
Stateful instance made by the plug-in.
std::unique_ptr< EffectOutputs > mMovedOutputs
Abstract base class for an object holding data associated with points on a time axis.
Definition: Track.h:225
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:26
FrameStatistics & GetInstance() noexcept
void swap(std::unique_ptr< Alg_seq > &a, std::unique_ptr< Alg_seq > &b)
Definition: NoteTrack.cpp:752
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:503