tracktion-engine 3.0-10-g034fdde4aa5
Tracktion Engine — High level data model for audio applications

« « « Anklang Documentation
Loading...
Searching...
No Matches
tracktion_AudioBufferStack.h
Go to the documentation of this file.
1 /*
2 ,--. ,--. ,--. ,--.
3 ,-' '-.,--.--.,--,--.,---.| |,-.,-' '-.`--' ,---. ,--,--, Copyright 2024
4 '-. .-'| .--' ,-. | .--'| /'-. .-',--.| .-. || \ Tracktion Software
5 | | | | \ '-' \ `--.| \ \ | | | |' '-' '| || | Corporation
6 `---' `--' `--`--'`---'`--'`--' `---' `--' `---' `--''--' www.tracktion.com
7
8 Tracktion Engine uses a GPL/commercial licence - see LICENCE.md for details.
9*/
10
11#include <stack>
12
13namespace tracktion { inline namespace graph
14{
15
16//==============================================================================
32{
33public:
39 AudioBufferStack() = default;
40
53 choc::buffer::ChannelArrayBuffer<float> allocate (choc::buffer::Size);
54
56 void release (choc::buffer::ChannelArrayBuffer<float>&&);
57
58 //==============================================================================
60 void reset();
61
63 void reserve (size_t numBuffers, choc::buffer::Size);
64
65 //==============================================================================
67 size_t getNumBuffers();
68
70 size_t getAllocatedSize();
71
72private:
74};
75
76
77//==============================================================================
78// _ _ _ _
79// __| | ___ | |_ __ _ (_)| | ___
80// / _` | / _ \| __| / _` || || |/ __|
81// | (_| || __/| |_ | (_| || || |\__ \ _ _ _
82// \__,_| \___| \__| \__,_||_||_||___/(_)(_)(_)
83//
84// Code beyond this point is implementation detail...
85//
86//==============================================================================
87inline choc::buffer::ChannelArrayBuffer<float> AudioBufferStack::allocate (choc::buffer::Size size)
88{
89 choc::buffer::ChannelArrayBuffer<float> buffer;
90
91 if (stack.size() > 0)
92 {
93 buffer = std::move (stack.top());
94 stack.pop();
95
96 if (auto bufferSize = buffer.getSize();
97 bufferSize.numChannels < size.numChannels
98 || bufferSize.numFrames < size.numFrames)
99 {
100 buffer.resize (size);
101 }
102 }
103 else
104 {
105 buffer.resize (size);
106 }
107
108 return buffer;
109}
110
111inline void AudioBufferStack::release (choc::buffer::ChannelArrayBuffer<float>&& buffer)
112{
113 stack.push (std::move (buffer));
114}
115
116//==============================================================================
117inline void AudioBufferStack::reserve (size_t numBuffers, choc::buffer::Size size)
118{
120
121 // Remove all the buffers
122 for (size_t i = 0; i < std::min (numBuffers, stack.size()); ++i)
123 {
124 buffers.emplace_back (std::move (stack.top()));
125 stack.pop();
126 }
127
128 // Ensure their size is as big as required
129 for (auto& b : buffers)
130 {
131 if (auto bufferSize = b.getSize();
132 bufferSize.numChannels < size.numChannels
133 || bufferSize.numFrames < size.numFrames)
134 {
135 b.resize (size);
136 }
137 }
138
139 // Reset the fifo storage to hold the new number of buffers
140 const int numToAdd = static_cast<int> (numBuffers) - static_cast<int> (buffers.size());
141 assert (numToAdd >= 0);
142
143 // Push the temp buffers back
144 for (auto& b : buffers)
145 stack.push (std::move (b));
146
147 // Push any additional buffers
148 for (int i = 0; i < numToAdd; ++i)
149 stack.push (choc::buffer::ChannelArrayBuffer<float> (size));
150}
151
153{
154 return stack.size();
155}
156
158{
159 if (stack.size() == 0)
160 return 0;
161
162 auto& b = stack.top();
163 return b.getView().data.getBytesNeeded (b.getSize());
164}
165
166}} // namespace tracktion
assert
choc::buffer::ChannelArrayBuffer< float > allocate(choc::buffer::Size)
Returns an allocated buffer for a given size from the stack.
void reset()
Releases all the internal allocated storage.
size_t getAllocatedSize()
Returns the currently allocated size of all the buffers in bytes.
AudioBufferStack()=default
Create an empty stack.
void reserve(size_t numBuffers, choc::buffer::Size)
Reserves a number of buffers of a given size, preallocating them.
size_t getNumBuffers()
Returns the current number of buffers in the stack.
void release(choc::buffer::ChannelArrayBuffer< float > &&)
Releases an allocated buffer back to the stack.
T emplace_back(T... args)
T min(T... args)
T pop(T... args)
T push(T... args)
T size(T... args)
T top(T... args)