Audacity 3.2.0
AudioUnitWrapper.h
Go to the documentation of this file.
1/*!********************************************************************
2
3 Audacity: A Digital Audio Editor
4
5 @file AudioUnitWrapper.h
6
7 Dominic Mazzoni
8 Leland Lucius
9
10 Paul Licameli split from AudioUnitEffect.h
11
12**********************************************************************/
13#ifndef AUDACITY_AUDIOUNIT_WRAPPER_H
14#define AUDACITY_AUDIOUNIT_WRAPPER_H
15
16#if USE_AUDIO_UNITS
17
18#include <optional>
19#include <map>
20#include <set>
21#include <unordered_map>
22#include <wx/string.h>
23
24#include "AudioUnitUtils.h"
25#include "Identifier.h"
26
27class wxCFStringRef;
28class wxMemoryBuffer;
30class EffectSettings;
33
50 using StringSet = std::set<wxString>;
51 const std::shared_ptr<StringSet> mSharedNames{
52 std::make_shared<StringSet>() };
53
55 std::optional<SInt32> mPresetNumber;
56
59 using Pair = std::pair<const wxString &, AudioUnitParameterValue>;
60 using Map = std::map<AudioUnitParameterID, std::optional<Pair>>;
62
64 AudioUnitEffectSettings(Map map) : values{ move(map) } {}
65
68 const wxString &Intern(const wxString &name) {
69 // std::set::insert guarantees this iterator is not at the end
70 auto [iter, _] = mSharedNames->insert(name);
71 // so dereference it merrily
72 return *iter;
73 }
74
77 {
78 for (auto &[_, value] : values)
79 value.reset();
80 }
81};
82
91{
93
97
101 AudioUnitWrapper(AudioComponent component, Parameters *pParameters)
102 : mComponent{ component }
103 , mParameters{ pParameters ? *pParameters : mOwnParameters }
104 {
105 }
106
107 // Supply most often used values as defaults for scope and element
108 template<typename T>
109 OSStatus GetFixedSizeProperty(AudioUnitPropertyID inID, T &property,
110 AudioUnitScope inScope = kAudioUnitScope_Global,
111 AudioUnitElement inElement = 0) const
112 {
113 // Supply mUnit.get() to the non-member function
115 inID, property, inScope, inElement);
116 }
117
118 // Supply most often used values as defaults for scope and element
119 template<typename T>
120 OSStatus GetVariableSizeProperty(AudioUnitPropertyID inID,
121 PackedArray::Ptr<T> &pObject,
122 AudioUnitScope inScope = kAudioUnitScope_Global,
123 AudioUnitElement inElement = 0) const
124 {
126 inID, pObject, inScope, inElement);
127 }
128
129 // Supply most often used values as defaults for scope and element
130 template<typename T>
131 OSStatus SetProperty(AudioUnitPropertyID inID, const T &property,
132 AudioUnitScope inScope = kAudioUnitScope_Global,
133 AudioUnitElement inElement = 0) const
134 {
135 // Supply mUnit.get() to the non-member function
137 inID, property, inScope, inElement);
138 }
139
140 class ParameterInfo;
143 std::function< bool(const ParameterInfo &pi, AudioUnitParameterID ID) >;
144 void ForEachParameter(ParameterVisitor visitor) const;
145
146 bool LoadPreset(const EffectDefinitionInterface &effect,
147 const RegistryPath & group, EffectSettings &settings) const;
149 int id, EffectSettings *pSettings) const;
150
152
157 std::pair<CF_ptr<CFDataRef>, TranslatableString>
160 const wxCFStringRef &cfname, bool binary) const;
161
163
168 const wxString &group, const wxMemoryBuffer &buf) const;
169
172 bool fetchValues, bool fetchPreset = false) const;
173 bool StoreSettings(const EffectDefinitionInterface &effect,
174 const AudioUnitEffectSettings &settings) const;
175
177 static bool MoveSettingsContents(
178 AudioUnitEffectSettings &&src, AudioUnitEffectSettings &dst, bool merge);
179
180 bool CreateAudioUnit();
181
182 AudioUnit GetAudioUnit() const { return mUnit.get(); }
183 AudioComponent GetComponent() const { return mComponent; }
185 { return mParameters; }
186
187 // @param identifier only for logging messages
188 bool SetRateAndChannels(double sampleRate, const wxString &identifier);
189
190protected:
191 const AudioComponent mComponent;
193
196
197 // Reassinged in GetRateAndChannels()
198 unsigned mAudioIns{ 2 };
199 unsigned mAudioOuts{ 2 };
200};
201
207{
208public:
210 ParameterInfo(AudioUnit mUnit, AudioUnitParameterID parmID);
212 static std::optional<AudioUnitParameterID> ParseKey(const wxString &key);
213
214 std::optional<wxString> mName;
216
217private:
218 // constants
219 static constexpr char idBeg = wxT('<');
220 static constexpr char idSep = wxT(',');
221 static constexpr char idEnd = wxT('>');
222};
223
224#endif
225
226#endif
wxT("CloseDown"))
std::unique_ptr< T, AudioUnitCleaner< T, fn > > AudioUnitCleanup
RAII for cleaning up AU plugin state.
const TranslatableString name
Definition: Distortion.cpp:76
wxString RegistryPath
Definition: Identifier.h:218
#define _(s)
Definition: Internat.h:73
static const AudacityProject::AttachedObjects::RegisteredFactory key
static Settings & settings()
Definition: TrackInfo.cpp:47
Encapsulates parameter information for an AudioUnit.
ParameterInfo(AudioUnit mUnit, AudioUnitParameterID parmID)
Make a structure holding a key for the config file and a value.
static std::optional< AudioUnitParameterID > ParseKey(const wxString &key)
Recover the parameter ID from the key, if well formed.
std::optional< wxString > mName
AudioUnitUtils::ParameterInfo mInfo
EffectDefinitionInterface is a ComponentInterface that adds some basic read-only information about ef...
Holds a msgid for the translation catalog; may also bind format arguments.
OSStatus GetFixedSizeProperty(AudioUnit unit, AudioUnitPropertyID inID, T &property, AudioUnitScope inScope=kAudioUnitScope_Global, AudioUnitElement inElement=0)
OSStatus SetProperty(AudioUnit unit, AudioUnitPropertyID inID, const T &property, AudioUnitScope inScope=kAudioUnitScope_Global, AudioUnitElement inElement=0)
OSStatus GetVariableSizeProperty(AudioUnit unit, AudioUnitPropertyID inID, PackedArray::Ptr< T > &pObject, AudioUnitScope inScope=kAudioUnitScope_Global, AudioUnitElement inElement=0)
Represents a cached copy of the state stored in an AudioUnit, but can outlive the original AudioUnit.
AudioUnitEffectSettings()=default
const wxString & Intern(const wxString &name)
std::set< wxString > StringSet
Shared set of strings to optimize memory usage by avoiding repeated allocations.
std::pair< const wxString &, AudioUnitParameterValue > Pair
const std::shared_ptr< StringSet > mSharedNames
void ResetValues()
Associate nullopt with all keys already present in the map.
std::optional< SInt32 > mPresetNumber
Optionally store a preset.
std::map< AudioUnitParameterID, std::optional< Pair > > Map
Manages and interacts with an AudioUnit, providing operations on audio effects.
Parameters & mParameters
bool LoadPreset(const EffectDefinitionInterface &effect, const RegistryPath &group, EffectSettings &settings) const
AudioUnitWrapper(AudioComponent component, Parameters *pParameters)
bool StoreSettings(const EffectDefinitionInterface &effect, const AudioUnitEffectSettings &settings) const
bool LoadFactoryPreset(const EffectDefinitionInterface &effect, int id, EffectSettings *pSettings) const
OSStatus GetFixedSizeProperty(AudioUnitPropertyID inID, T &property, AudioUnitScope inScope=kAudioUnitScope_Global, AudioUnitElement inElement=0) const
std::function< bool(const ParameterInfo &pi, AudioUnitParameterID ID) > ParameterVisitor
Return value: if true, continue visiting.
static bool MoveSettingsContents(AudioUnitEffectSettings &&src, AudioUnitEffectSettings &dst, bool merge)
Copy, then clear the optionals in src.
OSStatus GetVariableSizeProperty(AudioUnitPropertyID inID, PackedArray::Ptr< T > &pObject, AudioUnitScope inScope=kAudioUnitScope_Global, AudioUnitElement inElement=0) const
AudioUnit GetAudioUnit() const
TranslatableString InterpretBlob(AudioUnitEffectSettings &settings, const wxString &group, const wxMemoryBuffer &buf) const
Interpret the dump made before by MakeBlob.
const Parameters & GetParameters() const
bool SetRateAndChannels(double sampleRate, const wxString &identifier)
AudioComponent GetComponent() const
AudioUnitCleanup< AudioUnit, AudioComponentInstanceDispose > mUnit
OSStatus SetProperty(AudioUnitPropertyID inID, const T &property, AudioUnitScope inScope=kAudioUnitScope_Global, AudioUnitElement inElement=0) const
static AudioUnitEffectSettings & GetSettings(EffectSettings &settings)
const AudioComponent mComponent
Parameters mOwnParameters
std::pair< CF_ptr< CFDataRef >, TranslatableString > MakeBlob(const EffectDefinitionInterface &effect, const AudioUnitEffectSettings &settings, const wxCFStringRef &cfname, bool binary) const
Obtain dump of the setting state of an AudioUnit instance.
void ForEachParameter(ParameterVisitor visitor) const
bool FetchSettings(AudioUnitEffectSettings &settings, bool fetchValues, bool fetchPreset=false) const
May allocate memory, so should be called only in the main thread.
Externalized state of a plug-in.