forked from plugdata-team/JUCE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAUScopeElement.h
435 lines (347 loc) · 13 KB
/
AUScopeElement.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
/*!
@file AudioUnitSDK/AUScopeElement.h
@copyright © 2000-2021 Apple Inc. All rights reserved.
*/
#ifndef AudioUnitSDK_AUScopeElement_h
#define AudioUnitSDK_AUScopeElement_h
// module
#include <AudioUnitSDK/AUBuffer.h>
#include <AudioUnitSDK/AUUtility.h>
#include <AudioUnitSDK/ComponentBase.h>
// OS
#include <AudioToolbox/AudioUnit.h>
// std
#include <algorithm>
#include <atomic>
#include <memory>
#include <vector>
namespace ausdk {
class AUBase;
/// Wrap an atomic in a copy-constructible/assignable object. This allows storing atomic values in a
/// vector (not directly possible since atomics are not copy-constructible/assignable).
template <typename T>
class AtomicValue {
public:
AtomicValue() = default;
explicit AtomicValue(T val) : mValue{ val } {}
~AtomicValue() = default;
AtomicValue(const AtomicValue& other) : mValue{ other.mValue.load() } {}
AtomicValue(AtomicValue&& other) noexcept : mValue{ other.mValue.load() } {}
AtomicValue& operator=(const AtomicValue& other)
{
if (&other != this) {
mValue.store(other.mValue.load());
}
return *this;
}
AtomicValue& operator=(AtomicValue&& other) noexcept
{
mValue.store(other.mValue.load());
return *this;
}
T load(std::memory_order m = std::memory_order_seq_cst) const { return mValue.load(m); }
void store(T v, std::memory_order m = std::memory_order_seq_cst) { mValue.store(v, m); }
operator T() const { return load(); } // NOLINT implicit conversions OK
AtomicValue& operator=(T value)
{
store(value);
return *this;
}
private:
std::atomic<T> mValue{};
};
/// A bare-bones reinvention of boost::flat_map, just enough to hold parameters in sorted vectors.
template <typename Key, typename Value>
class flat_map {
using KVPair = std::pair<Key, Value>;
using Impl = std::vector<std::pair<Key, Value>>;
static bool keyless(const KVPair& item, Key k) { return k > item.first; }
Impl mImpl;
public:
using iterator = typename Impl::iterator;
using const_iterator = typename Impl::const_iterator;
[[nodiscard]] bool empty() const { return mImpl.empty(); }
[[nodiscard]] size_t size() const { return mImpl.size(); }
[[nodiscard]] const_iterator begin() const { return mImpl.begin(); }
[[nodiscard]] const_iterator end() const { return mImpl.end(); }
iterator begin() { return mImpl.begin(); }
iterator end() { return mImpl.end(); }
const_iterator cbegin() { return mImpl.cbegin(); }
const_iterator cend() { return mImpl.cend(); }
[[nodiscard]] const_iterator lower_bound(Key k) const
{
return std::lower_bound(mImpl.begin(), mImpl.end(), k, keyless);
}
iterator lower_bound(Key k) { return std::lower_bound(mImpl.begin(), mImpl.end(), k, keyless); }
[[nodiscard]] const_iterator find(Key k) const
{
auto iter = lower_bound(k);
if (iter != mImpl.end()) {
if ((*iter).first != k) {
iter = mImpl.end();
}
}
return iter;
}
iterator find(Key k)
{
auto iter = lower_bound(k);
if (iter != mImpl.end()) {
if ((*iter).first != k) {
iter = mImpl.end();
}
}
return iter;
}
class ItemProxy {
public:
ItemProxy(flat_map& map, Key k) : mMap{ map }, mKey{ k } {}
operator Value() const // NOLINT implicit conversion is OK
{
const auto iter = mMap.find(mKey);
if (iter == mMap.end()) {
throw std::runtime_error("Invalid map key");
}
return (*iter).second;
}
ItemProxy& operator=(const Value& v)
{
const auto iter = mMap.lower_bound(mKey);
if (iter != mMap.end() && (*iter).first == mKey) {
(*iter).second = v;
} else {
mMap.mImpl.insert(iter, { mKey, v });
}
return *this;
}
private:
flat_map& mMap;
const Key mKey;
};
ItemProxy operator[](Key k) { return ItemProxy{ *this, k }; }
};
// ____________________________________________________________________________
//
class AUIOElement;
/// An organizational unit for parameters, with a name.
class AUElement {
using ParameterValue = AtomicValue<float>;
using ParameterMap = flat_map<AudioUnitParameterID, ParameterValue>;
public:
explicit AUElement(AUBase& audioUnit) : mAudioUnit(audioUnit), mUseIndexedParameters(false) {}
AUSDK_DEPRECATED("Construct with a reference")
explicit AUElement(AUBase* audioUnit) : AUElement(*audioUnit) {}
AUElement(const AUElement&) = delete;
AUElement(AUElement&&) = delete;
AUElement& operator=(const AUElement&) = delete;
AUElement& operator=(AUElement&&) = delete;
virtual ~AUElement() = default;
virtual UInt32 GetNumberOfParameters()
{
return mUseIndexedParameters ? static_cast<UInt32>(mIndexedParameters.size())
: static_cast<UInt32>(mParameters.size());
}
virtual void GetParameterList(AudioUnitParameterID* outList);
[[nodiscard]] bool HasParameterID(AudioUnitParameterID paramID) const;
[[nodiscard]] AudioUnitParameterValue GetParameter(AudioUnitParameterID paramID) const;
// Only set okWhenInitialized to true when you know the outside world cannot access this
// element. Otherwise the parameter map could get corrupted.
void SetParameter(AudioUnitParameterID paramID, AudioUnitParameterValue value,
bool okWhenInitialized = false);
// Only set okWhenInitialized to true when you know the outside world cannot access this
// element. Otherwise the parameter map could get corrupted. N.B. This only handles
// immediate parameters. Override to implement ramping. Called from
// AUBase::ProcessForScheduledParams.
virtual void SetScheduledEvent(AudioUnitParameterID paramID,
const AudioUnitParameterEvent& inEvent, UInt32 inSliceOffsetInBuffer,
UInt32 inSliceDurationFrames, bool okWhenInitialized = false);
[[nodiscard]] AUBase& GetAudioUnit() const noexcept { return mAudioUnit; }
void SaveState(AudioUnitScope scope, CFMutableDataRef data);
const UInt8* RestoreState(const UInt8* state);
[[nodiscard]] Owned<CFStringRef> GetName() const { return mElementName; }
void SetName(CFStringRef inName) { mElementName = inName; }
[[nodiscard]] bool HasName() const { return *mElementName != nil; }
virtual void UseIndexedParameters(UInt32 inNumberOfParameters);
virtual AUIOElement* AsIOElement() { return nullptr; }
private:
// --
AUBase& mAudioUnit;
ParameterMap mParameters;
bool mUseIndexedParameters;
std::vector<ParameterValue> mIndexedParameters;
Owned<CFStringRef> mElementName;
};
// ____________________________________________________________________________
//
/// A subclass of AUElement which represents an input or output bus, and has an associated
/// audio format and buffers.
class AUIOElement : public AUElement {
public:
explicit AUIOElement(AUBase& audioUnit);
AUIOElement(AUBase& audioUnit, const AudioStreamBasicDescription& format)
: AUIOElement{ audioUnit }
{
mStreamFormat = format;
}
AUSDK_DEPRECATED("Construct with a reference")
explicit AUIOElement(AUBase* audioUnit) : AUIOElement(*audioUnit) {}
[[nodiscard]] const AudioStreamBasicDescription& GetStreamFormat() const noexcept
{
return mStreamFormat;
}
virtual OSStatus SetStreamFormat(const AudioStreamBasicDescription& format);
virtual void AllocateBuffer(UInt32 inFramesToAllocate = 0);
void DeallocateBuffer();
/// Determines (via subclass override) whether the element's buffer list needs to be allocated.
[[nodiscard]] virtual bool NeedsBufferSpace() const = 0;
void SetWillAllocateBuffer(bool inFlag) noexcept { mWillAllocate = inFlag; }
[[nodiscard]] bool WillAllocateBuffer() const noexcept { return mWillAllocate; }
AudioBufferList& PrepareBuffer(UInt32 nFrames)
{
if (mWillAllocate) {
return mIOBuffer.PrepareBuffer(mStreamFormat, nFrames);
}
Throw(kAudioUnitErr_InvalidPropertyValue);
}
AudioBufferList& PrepareNullBuffer(UInt32 nFrames)
{
return mIOBuffer.PrepareNullBuffer(mStreamFormat, nFrames);
}
AudioBufferList& SetBufferList(AudioBufferList& abl) { return mIOBuffer.SetBufferList(abl); }
void SetBuffer(UInt32 index, AudioBuffer& ab) { mIOBuffer.SetBuffer(index, ab); }
void InvalidateBufferList() { mIOBuffer.InvalidateBufferList(); }
[[nodiscard]] AudioBufferList& GetBufferList() const { return mIOBuffer.GetBufferList(); }
[[nodiscard]] float* GetFloat32ChannelData(UInt32 ch)
{
if (IsInterleaved()) {
return static_cast<float*>(mIOBuffer.GetBufferList().mBuffers[0].mData) + ch; // NOLINT
}
return static_cast<float*>(mIOBuffer.GetBufferList().mBuffers[ch].mData); // NOLINT
}
void CopyBufferListTo(AudioBufferList& abl) const { mIOBuffer.CopyBufferListTo(abl); }
void CopyBufferContentsTo(AudioBufferList& abl) const { mIOBuffer.CopyBufferContentsTo(abl); }
[[nodiscard]] bool IsInterleaved() const noexcept { return ASBD::IsInterleaved(mStreamFormat); }
[[nodiscard]] UInt32 NumberChannels() const noexcept { return mStreamFormat.mChannelsPerFrame; }
[[nodiscard]] UInt32 NumberInterleavedChannels() const noexcept
{
return ASBD::NumberInterleavedChannels(mStreamFormat);
}
virtual std::vector<AudioChannelLayoutTag> GetChannelLayoutTags();
[[nodiscard]] const AUChannelLayout& ChannelLayout() const { return mChannelLayout; }
// Old layout methods
virtual OSStatus SetAudioChannelLayout(const AudioChannelLayout& inLayout);
virtual UInt32 GetAudioChannelLayout(AudioChannelLayout* outLayoutPtr, bool& outWritable);
virtual OSStatus RemoveAudioChannelLayout();
/*! @fn AsIOElement*/
AUIOElement* AsIOElement() override { return this; }
protected:
AUBufferList& IOBuffer() noexcept { return mIOBuffer; }
void ForceSetAudioChannelLayout(const AudioChannelLayout& inLayout)
{
mChannelLayout = inLayout;
}
private:
AudioStreamBasicDescription mStreamFormat{};
AUChannelLayout mChannelLayout{};
AUBufferList mIOBuffer; // for input: input proc buffer, only allocated when needed
// for output: output cache, usually allocated early on
bool mWillAllocate{ false };
};
// ____________________________________________________________________________
//
/*!
@class AUScopeDelegate
@brief Provides a way to customize a scope, thereby obtaining virtual scopes.
Can be used to implement scopes with variable numbers of elements.
*/
class AUScopeDelegate {
public:
AUScopeDelegate() = default;
virtual ~AUScopeDelegate() = default;
AUScopeDelegate(const AUScopeDelegate&) = delete;
AUScopeDelegate(AUScopeDelegate&&) = delete;
AUScopeDelegate& operator=(const AUScopeDelegate&) = delete;
AUScopeDelegate& operator=(AUScopeDelegate&&) = delete;
void Initialize(AUBase* creator, AudioUnitScope scope, UInt32 numElements)
{
mCreator = creator;
mScope = scope;
SetNumberOfElements(numElements);
}
virtual void SetNumberOfElements(UInt32 numElements) = 0;
virtual UInt32 GetNumberOfElements() = 0;
virtual AUElement* GetElement(UInt32 elementIndex) = 0;
[[nodiscard]] AUBase* GetCreator() const noexcept { return mCreator; }
[[nodiscard]] AudioUnitScope GetScope() const noexcept { return mScope; }
private:
AUBase* mCreator{ nullptr };
AudioUnitScope mScope{ 0 };
};
// ____________________________________________________________________________
//
/*!
@class AUScope
@brief Organizes one or more elements into an addressable group (e.g. global, input, output).
*/
class AUScope {
public:
AUScope() = default;
~AUScope() = default;
AUScope(const AUScope&) = delete;
AUScope(AUScope&&) = delete;
AUScope& operator=(const AUScope&) = delete;
AUScope& operator=(AUScope&&) = delete;
void Initialize(AUBase* creator, AudioUnitScope scope, UInt32 numElements)
{
mCreator = creator;
mScope = scope;
if (mDelegate != nullptr) {
return mDelegate->Initialize(creator, scope, numElements);
}
SetNumberOfElements(numElements);
}
void SetNumberOfElements(UInt32 numElements);
[[nodiscard]] UInt32 GetNumberOfElements() const
{
if (mDelegate != nullptr) {
return mDelegate->GetNumberOfElements();
}
return static_cast<UInt32>(mElements.size());
}
[[nodiscard]] AUElement* GetElement(UInt32 elementIndex) const
{
if (mDelegate != nullptr) {
return mDelegate->GetElement(elementIndex);
}
return elementIndex < mElements.size() ? mElements[elementIndex].get() : nullptr;
}
[[nodiscard]] AUElement* SafeGetElement(UInt32 elementIndex) const
{
AUElement* const element = GetElement(elementIndex);
ausdk::ThrowExceptionIf(element == nullptr, kAudioUnitErr_InvalidElement);
return element;
}
[[nodiscard]] AUIOElement* GetIOElement(UInt32 elementIndex) const
{
AUElement* const element = GetElement(elementIndex);
AUIOElement* const ioel = element != nullptr ? element->AsIOElement() : nullptr;
ausdk::ThrowExceptionIf(ioel == nullptr, kAudioUnitErr_InvalidElement);
return ioel;
}
[[nodiscard]] bool HasElementWithName() const;
void AddElementNamesToDict(CFMutableDictionaryRef inNameDict) const;
[[nodiscard]] std::vector<AudioUnitElement> RestoreElementNames(
CFDictionaryRef inNameDict) const;
[[nodiscard]] AudioUnitScope GetScope() const noexcept { return mScope; }
void SetDelegate(AUScopeDelegate* inDelegate) noexcept { mDelegate = inDelegate; }
void SaveState(CFMutableDataRef data) const;
const UInt8* RestoreState(const UInt8* state) const;
private:
using ElementVector = std::vector<std::unique_ptr<AUElement>>;
AUBase* mCreator{ nullptr };
AudioUnitScope mScope{ 0 };
ElementVector mElements;
AUScopeDelegate* mDelegate{ nullptr };
};
} // namespace ausdk
#endif // AudioUnitSDK_AUScopeElement_h