Audacity 3.2.0
AudioUnitUtils.cpp
Go to the documentation of this file.
1/*!********************************************************************
2
3 Audacity: A Digital Audio Editor
4
5 @file AudioUnitUtils.cpp
6
7 Paul Licameli
8
9***********************************************************************/
10
11#include "AudioUnitUtils.h"
12#include "MemoryX.h"
13
15 AudioUnitPropertyID inID, void *pProperty, const UInt32 size,
16 AudioUnitScope inScope, AudioUnitElement inElement)
17{
18 auto newSize = size;
19 auto result = AudioUnitGetProperty(unit, inID, inScope, inElement,
20 pProperty, &newSize);
21 assert(newSize <= size);
22 return result;
23}
24
26 AudioUnitPropertyID inID, const size_t minSize,
27 void *&pObject, size_t &size,
28 AudioUnitScope inScope, AudioUnitElement inElement)
29{
30 size = 0;
31
32 // Query for statically unknown size of the property first
33 UInt32 dataSize;
34 auto result = AudioUnitGetPropertyInfo(unit, inID, inScope, inElement,
35 &dataSize, nullptr);
36 if (result)
37 return result;
38
39 // Allocate
40 auto newSize = std::max<size_t>(minSize, dataSize);
41 auto pObj = ::operator new(newSize);
42 auto cleanup = finally([&]{
43 // In case AudioUnitGetProperty fails
44 if (!pObject)
45 ::operator delete(pObj);
46 });
47
48 // Try to get the property
49 dataSize = newSize;
50 result =
51 AudioUnitGetProperty(unit, inID, inScope, inElement, pObj, &dataSize);
52 if (result) {
53 pObject = nullptr;
54 return result;
55 }
56
57 // Success
58 pObject = pObj;
59 size = newSize;
60 return result;
61}
62
63OSStatus AudioUnitUtils::SetPropertyPtr(AudioUnit unit,
64 AudioUnitPropertyID inID, const void *pProperty, UInt32 size,
65 AudioUnitScope inScope, AudioUnitElement inElement)
66{
67 return AudioUnitSetProperty(unit, inID, inScope, inElement,
68 pProperty, size);
69}
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.