forked from juce-framework/JUCE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AUBuffer.cpp
188 lines (162 loc) · 5.64 KB
/
AUBuffer.cpp
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
/*!
@file AudioUnitSDK/AUBuffer.cpp
@copyright © 2000-2021 Apple Inc. All rights reserved.
*/
#include <AudioUnitSDK/AUBuffer.h>
#include <AudioUnitSDK/AUUtility.h>
namespace ausdk {
inline void ThrowBadAlloc()
{
AUSDK_LogError("AUBuffer throwing bad_alloc");
throw std::bad_alloc();
}
// x: number to be rounded; y: the power of 2 to which to round
constexpr uint32_t RoundUpToMultipleOfPowerOf2(uint32_t x, uint32_t y) noexcept
{
const auto mask = y - 1;
#if DEBUG
assert((mask & y) == 0u); // verifies that y is a power of 2 NOLINT
#endif
return (x + mask) & ~mask;
}
// a * b + c
static UInt32 SafeMultiplyAddUInt32(UInt32 a, UInt32 b, UInt32 c)
{
if (a == 0 || b == 0) {
return c; // prevent zero divide
}
if (a > (0xFFFFFFFF - c) / b) { // NOLINT magic
ThrowBadAlloc();
}
return a * b + c;
}
AllocatedBuffer* BufferAllocator::Allocate(
UInt32 numberBuffers, UInt32 maxBytesPerBuffer, UInt32 /*reservedFlags*/)
{
constexpr size_t kAlignment = 16;
constexpr size_t kMaxBufferListSize = 65536;
// Check for a reasonable number of buffers (obviate a more complicated check with offsetof).
if (numberBuffers > kMaxBufferListSize / sizeof(AudioBuffer)) {
throw std::out_of_range("AudioBuffers::Allocate: Too many buffers");
}
maxBytesPerBuffer = RoundUpToMultipleOfPowerOf2(maxBytesPerBuffer, kAlignment);
const auto bufferDataSize = SafeMultiplyAddUInt32(numberBuffers, maxBytesPerBuffer, 0);
void* bufferData = nullptr;
if (bufferDataSize > 0) {
bufferData = malloc(bufferDataSize);
// don't use calloc(); it might not actually touch the memory and cause a VM fault later
memset(bufferData, 0, bufferDataSize);
}
const auto implSize = static_cast<uint32_t>(
offsetof(AllocatedBuffer, mAudioBufferList.mBuffers[std::max(UInt32(1), numberBuffers)]));
auto* const implMem = malloc(implSize);
auto* const allocatedBuffer =
new (implMem) AllocatedBuffer{ .mMaximumNumberBuffers = numberBuffers,
.mMaximumBytesPerBuffer = maxBytesPerBuffer,
.mHeaderSize = implSize,
.mBufferDataSize = bufferDataSize,
.mBufferData = bufferData };
allocatedBuffer->mAudioBufferList.mNumberBuffers = numberBuffers;
return allocatedBuffer;
}
void BufferAllocator::Deallocate(AllocatedBuffer* allocatedBuffer)
{
if (allocatedBuffer->mBufferData != nullptr) {
free(allocatedBuffer->mBufferData);
}
allocatedBuffer->~AllocatedBuffer();
free(allocatedBuffer);
}
AudioBufferList& AllocatedBuffer::Prepare(UInt32 channelsPerBuffer, UInt32 bytesPerBuffer)
{
if (mAudioBufferList.mNumberBuffers > mMaximumNumberBuffers) {
throw std::out_of_range("AllocatedBuffer::Prepare(): too many buffers");
}
if (bytesPerBuffer > mMaximumBytesPerBuffer) {
throw std::out_of_range("AllocatedBuffer::Prepare(): insufficient capacity");
}
auto* ptr = static_cast<Byte*>(mBufferData);
auto* const ptrend = ptr + mBufferDataSize;
for (UInt32 bufIdx = 0, nBufs = mAudioBufferList.mNumberBuffers; bufIdx < nBufs; ++bufIdx) {
auto& buf = mAudioBufferList.mBuffers[bufIdx]; // NOLINT
buf.mNumberChannels = channelsPerBuffer;
buf.mDataByteSize = bytesPerBuffer;
buf.mData = ptr;
ptr += mMaximumBytesPerBuffer; // NOLINT ptr math
}
if (ptr > ptrend) {
throw std::out_of_range("AllocatedBuffer::Prepare(): insufficient capacity");
}
return mAudioBufferList;
}
AudioBufferList& AllocatedBuffer::PrepareNull(UInt32 channelsPerBuffer, UInt32 bytesPerBuffer)
{
if (mAudioBufferList.mNumberBuffers > mMaximumNumberBuffers) {
throw std::out_of_range("AllocatedBuffer::PrepareNull(): too many buffers");
}
for (UInt32 bufIdx = 0, nBufs = mAudioBufferList.mNumberBuffers; bufIdx < nBufs; ++bufIdx) {
auto& buf = mAudioBufferList.mBuffers[bufIdx]; // NOLINT
buf.mNumberChannels = channelsPerBuffer;
buf.mDataByteSize = bytesPerBuffer;
buf.mData = nullptr;
}
return mAudioBufferList;
}
AudioBufferList& AUBufferList::PrepareBuffer(
const AudioStreamBasicDescription& format, UInt32 nFrames)
{
ausdk::ThrowExceptionIf(nFrames > mAllocatedFrames, kAudioUnitErr_TooManyFramesToProcess);
UInt32 nStreams = 0;
UInt32 channelsPerStream = 0;
if (ASBD::IsInterleaved(format)) {
nStreams = 1;
channelsPerStream = format.mChannelsPerFrame;
} else {
nStreams = format.mChannelsPerFrame;
channelsPerStream = 1;
}
ausdk::ThrowExceptionIf(nStreams > mAllocatedStreams, kAudioUnitErr_FormatNotSupported);
auto& abl = mBuffers->Prepare(channelsPerStream, nFrames * format.mBytesPerFrame);
mPtrState = EPtrState::ToMyMemory;
return abl;
}
AudioBufferList& AUBufferList::PrepareNullBuffer(
const AudioStreamBasicDescription& format, UInt32 nFrames)
{
UInt32 nStreams = 0;
UInt32 channelsPerStream = 0;
if (ASBD::IsInterleaved(format)) {
nStreams = 1;
channelsPerStream = format.mChannelsPerFrame;
} else {
nStreams = format.mChannelsPerFrame;
channelsPerStream = 1;
}
ausdk::ThrowExceptionIf(nStreams > mAllocatedStreams, kAudioUnitErr_FormatNotSupported);
auto& abl = mBuffers->PrepareNull(channelsPerStream, nFrames * format.mBytesPerFrame);
mPtrState = EPtrState::ToExternalMemory;
return abl;
}
void AUBufferList::Allocate(const AudioStreamBasicDescription& format, UInt32 nFrames)
{
auto& alloc = BufferAllocator::instance();
if (mBuffers != nullptr) {
alloc.Deallocate(mBuffers);
}
const uint32_t nstreams = ASBD::IsInterleaved(format) ? 1 : format.mChannelsPerFrame;
mBuffers = alloc.Allocate(nstreams, nFrames * format.mBytesPerFrame, 0u);
mAllocatedFrames = nFrames;
mAllocatedStreams = nstreams;
mPtrState = EPtrState::Invalid;
}
void AUBufferList::Deallocate()
{
if (mBuffers != nullptr) {
BufferAllocator::instance().Deallocate(mBuffers);
mBuffers = nullptr;
}
mAllocatedFrames = 0;
mAllocatedStreams = 0;
mPtrState = EPtrState::Invalid;
}
} // namespace ausdk