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 CFRelease(cfNameString);
146 if (flags & kAudioUnitParameterUnit_CustomUnit)
147 CFRelease(unitName);
148 }
149 }
150 };
151
152 struct ParameterNameInfo : AudioUnitParameterNameInfo {
153 ParameterNameInfo(AudioUnitParameterID id, SInt32 desiredLength)
154 : AudioUnitParameterNameInfo{} {
155 inID = id;
156 inDesiredLength = desiredLength;
157 }
159 if (outName)
160 CFRelease(outName);
161 }
162 };
163
164 struct Property : AudioUnitProperty {
165 Property(AudioUnit audioUnit, AudioUnitPropertyID propertyID,
166 AudioUnitScope scope)
167 : AudioUnitProperty{} {
168 mAudioUnit = audioUnit;
169 mPropertyID = propertyID;
170 mScope = scope;
171 // Default element to 0
172 }
173 };
174
175 struct UserPreset : AUPreset {
176 UserPreset(CFStringRef name) : AUPreset{} {
177 presetNumber = -1;
178 presetName = name;
179 }
180 };
181
182 struct StreamBasicDescription : AudioStreamBasicDescription {
183 StreamBasicDescription(Float64 sampleRate,
184 AudioFormatID formatID, AudioFormatFlags formatFlags,
185 UInt32 bytesPerPacket, UInt32 framesPerPacket,
186 UInt32 bytesPerFrame, UInt32 channelsPerFrame,
187 UInt32 bitsPerChannel)
188 : AudioStreamBasicDescription{} {
189 mSampleRate = sampleRate;
190 mFormatID = formatID;
191 mFormatFlags = formatFlags;
192 mBytesPerPacket = bytesPerPacket;
193 mFramesPerPacket = framesPerPacket;
194 mBytesPerFrame = bytesPerFrame;
195 mChannelsPerFrame = channelsPerFrame;
196 mBitsPerChannel = bitsPerChannel;
197 // Just padding:
198 // mReserved = 0; // But AudioStreamBasicDescription{} did that
199 }
200 };
201
203}
204
209
211 struct header_type { UInt32 mNumberBuffers; };
212 // Overlay the element type with the wrapper type
214 static constexpr auto array_member = &AudioBufferList::mBuffers;
215};
216
218 struct header_type {
219 CF_ptr<CFURLRef> p1;
220
222 // Sanity checks against toolkit version change
223 static_assert(offsetof(header_type, p1) ==
224 offsetof(AudioUnitCocoaViewInfo, mCocoaAUViewBundleLocation));
225 }
226 };
227 using element_type = CF_ptr<CFStringRef>;
228 static constexpr auto array_member =
229 &AudioUnitCocoaViewInfo::mCocoaAUViewClass;
230};
231
233
234#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.
static TransactionScope::Factory::Scope scope
const TranslatableString name
Definition: Distortion.cpp:82
Smart pointer for a header contiguous with an array holding a dynamically determined number of elemen...
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)
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