Audacity 3.2.0
AudioUnitUtils.h
Go to the documentation of this file.
1/*!********************************************************************
2
3 Audacity: A Digital Audio Editor
4
5 @file AudioUnitUtils.h
6
7 Paul Licameli
8
9***********************************************************************/
10
11#ifndef __AUDACITY_AUDIO_UNIT_UTILS__
12#define __AUDACITY_AUDIO_UNIT_UTILS__
13
14#include <algorithm>
15#include <AudioUnit/AudioUnit.h>
16#include "CFResources.h"
17#include "PackedArray.h"
18
20template<typename T, OSStatus(*fn)(T*)> struct AudioUnitCleaner {
21 // Let this have non-void return type, though ~unique_ptr() will ignore it
22 auto operator () (T *p) noexcept { return fn(p); }
23};
25template<typename Ptr, OSStatus(*fn)(Ptr),
26 typename T = std::remove_pointer_t<Ptr> /* deduced */ >
27using AudioUnitCleanup = std::unique_ptr<T, AudioUnitCleaner<T, fn>>;
28
31namespace AudioUnitUtils {
33 OSStatus GetFixedSizePropertyPtr(AudioUnit unit, AudioUnitPropertyID inID,
34 void *pProperty, UInt32 size, AudioUnitScope inScope,
35 AudioUnitElement inElement);
36
39 template<typename T>
40 OSStatus GetFixedSizeProperty(AudioUnit unit, AudioUnitPropertyID inID,
41 T &property,
42 AudioUnitScope inScope = kAudioUnitScope_Global,
43 AudioUnitElement inElement = 0)
44 {
45 return GetFixedSizePropertyPtr(unit, inID,
46 &property, sizeof(property), inScope, inElement);
47 }
48
50
54 OSStatus GetVariableSizePropertyPtr(AudioUnit unit, AudioUnitPropertyID inID,
55 size_t minSize, void *&pObject, size_t &size,
56 AudioUnitScope inScope, AudioUnitElement inElement);
57
61 template<typename T>
62 OSStatus GetVariableSizeProperty(AudioUnit unit, AudioUnitPropertyID inID,
63 PackedArray::Ptr<T> &pObject,
64 AudioUnitScope inScope = kAudioUnitScope_Global,
65 AudioUnitElement inElement = 0)
66 {
67 void *p{};
68 size_t size{};
69 auto result = GetVariableSizePropertyPtr(unit, inID,
70 sizeof(typename PackedArray::Traits<T>::header_type), p, size,
71 inScope, inElement);
72 if (!result)
73 // Construct PackedArray::Ptr
74 pObject = {
75 static_cast<T*>(p), // the pointer
76 size // the size that the deleter must remember
77 };
78 return result;
79 }
80
82 OSStatus SetPropertyPtr(AudioUnit unit, AudioUnitPropertyID inID,
83 const void *pProperty, UInt32 size, AudioUnitScope inScope,
84 AudioUnitElement inElement);
85
88 template<typename T>
89 OSStatus SetProperty(AudioUnit unit, AudioUnitPropertyID inID,
90 const T &property,
91 AudioUnitScope inScope = kAudioUnitScope_Global,
92 AudioUnitElement inElement = 0)
93 {
94 return SetPropertyPtr(unit, inID,
95 &property, sizeof(property), inScope, inElement);
96 }
97
113
114 struct Buffer : AudioBuffer {
115 Buffer(UInt32 numberChannels, UInt32 dataByteSize, void *data)
116 : AudioBuffer{} {
117 mNumberChannels = numberChannels;
118 mDataByteSize = dataByteSize;
119 mData = data;
120 }
121 };
122
123 struct RenderCallback : AURenderCallbackStruct {
124 RenderCallback(AURenderCallback inProc, void *inProcRefCon)
125 : AURenderCallbackStruct{} {
126 inputProc = inProc;
127 inputProcRefCon = inProcRefCon;
128 }
129 };
130
131 struct Parameter : AudioUnitParameter {
133 Parameter(AudioUnit audioUnit, AudioUnitScope scope)
134 : AudioUnitParameter{} {
135 mAudioUnit = audioUnit;
136 mScope = scope;
137 }
138 };
139
140 struct ParameterInfo : AudioUnitParameterInfo {
141 ParameterInfo() : AudioUnitParameterInfo{} {}
143 if (flags & kAudioUnitParameterFlag_CFNameRelease) {
144 if (flags & kAudioUnitParameterFlag_HasCFNameString)
145 if (cfNameString)
146 CFRelease(cfNameString);
147 if (flags & kAudioUnitParameterUnit_CustomUnit)
148 if (unitName)
149 CFRelease(unitName);
150 }
151 }
152 };
153
154 struct ParameterNameInfo : AudioUnitParameterNameInfo {
155 ParameterNameInfo(AudioUnitParameterID id, SInt32 desiredLength)
156 : AudioUnitParameterNameInfo{} {
157 inID = id;
158 inDesiredLength = desiredLength;
159 }
161 if (outName)
162 CFRelease(outName);
163 }
164 };
165
166 struct Property : AudioUnitProperty {
167 Property(AudioUnit audioUnit, AudioUnitPropertyID propertyID,
168 AudioUnitScope scope)
169 : AudioUnitProperty{} {
170 mAudioUnit = audioUnit;
171 mPropertyID = propertyID;
172 mScope = scope;
173 // Default element to 0
174 }
175 };
176
177 struct UserPreset : AUPreset {
178 UserPreset(CFStringRef name) : AUPreset{} {
179 presetNumber = -1;
180 presetName = name;
181 }
182 };
183
184 struct StreamBasicDescription : AudioStreamBasicDescription {
186 AudioFormatID formatID, AudioFormatFlags formatFlags,
187 UInt32 bytesPerPacket, UInt32 framesPerPacket,
188 UInt32 bytesPerFrame, UInt32 channelsPerFrame,
189 UInt32 bitsPerChannel)
190 : AudioStreamBasicDescription{} {
191 mSampleRate = sampleRate;
192 mFormatID = formatID;
193 mFormatFlags = formatFlags;
194 mBytesPerPacket = bytesPerPacket;
195 mFramesPerPacket = framesPerPacket;
196 mBytesPerFrame = bytesPerFrame;
197 mChannelsPerFrame = channelsPerFrame;
198 mBitsPerChannel = bitsPerChannel;
199 // Just padding:
200 // mReserved = 0; // But AudioStreamBasicDescription{} did that
201 }
202 };
203
205}
206
211
213 struct header_type { UInt32 mNumberBuffers; };
214 // Overlay the element type with the wrapper type
216 static constexpr auto array_member = &AudioBufferList::mBuffers;
217};
218
220 struct header_type {
221 CF_ptr<CFURLRef> p1;
222
224 // Sanity checks against toolkit version change
225 static_assert(offsetof(header_type, p1) ==
226 offsetof(AudioUnitCocoaViewInfo, mCocoaAUViewBundleLocation));
227 }
228 };
229 using element_type = CF_ptr<CFStringRef>;
230 static constexpr auto array_member =
231 &AudioUnitCocoaViewInfo::mCocoaAUViewClass;
232};
233
235
236#endif
std::unique_ptr< T, AudioUnitCleaner< T, fn > > AudioUnitCleanup
RAII for cleaning up AU plugin state.
Wrap resource pointers from Apple Core SDK for RAII.
Smart pointer for a header contiguous with an array holding a dynamically determined number of elemen...
wxString name
Definition: TagsEditor.cpp:166
int id
static const auto fn
OSStatus GetFixedSizeProperty(AudioUnit unit, AudioUnitPropertyID inID, T &property, AudioUnitScope inScope=kAudioUnitScope_Global, AudioUnitElement inElement=0)
OSStatus SetPropertyPtr(AudioUnit unit, AudioUnitPropertyID inID, const void *pProperty, UInt32 size, AudioUnitScope inScope, AudioUnitElement inElement)
Type-erased function to set an AudioUnit property.
OSStatus GetVariableSizePropertyPtr(AudioUnit unit, AudioUnitPropertyID inID, size_t minSize, void *&pObject, size_t &size, AudioUnitScope inScope, AudioUnitElement inElement)
Type-erased function to get an AudioUnit property of variable size.
OSStatus GetFixedSizePropertyPtr(AudioUnit unit, AudioUnitPropertyID inID, void *pProperty, UInt32 size, AudioUnitScope inScope, AudioUnitElement inElement)
Type-erased function to get an AudioUnit property of fixed size.
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)
static CommandContext::TargetFactory::SubstituteInUnique< InteractiveOutputTargets > scope
Generates deleters for std::unique_ptr that clean up AU plugin state.
auto operator()(T *p) noexcept
Buffer(UInt32 numberChannels, UInt32 dataByteSize, void *data)
Parameter(AudioUnit audioUnit, AudioUnitScope scope)
This constructor leaves the parameter ID and element fields as 0.
ParameterNameInfo(AudioUnitParameterID id, SInt32 desiredLength)
Property(AudioUnit audioUnit, AudioUnitPropertyID propertyID, AudioUnitScope scope)
RenderCallback(AURenderCallback inProc, void *inProcRefCon)
StreamBasicDescription(Float64 sampleRate, AudioFormatID formatID, AudioFormatFlags formatFlags, UInt32 bytesPerPacket, UInt32 framesPerPacket, UInt32 bytesPerFrame, UInt32 channelsPerFrame, UInt32 bitsPerChannel)
UserPreset(CFStringRef name)
Smart pointer type that deallocates with Deleter.
Definition: PackedArray.h:130