TracktionEngine
|
A lock-free pool of audio buffers. More...
#include <tracktion_AudioBufferPool.h>
Public Member Functions | |
AudioBufferPool ()=default | |
Create an empty pool. | |
AudioBufferPool (size_t maxCapacity) | |
Creates pool with a given max capacity. | |
choc::buffer::ChannelArrayBuffer< float > | allocate (choc::buffer::Size) |
Returns an allocated buffer for a given size from the pool. | |
bool | release (choc::buffer::ChannelArrayBuffer< float > &&) |
Releases an allocated buffer back to the pool. | |
void | reset () |
Releases all the internal allocated storage. | |
void | setCapacity (size_t) |
Sets the maximum number of buffers this can store. | |
size_t | getCapacity () const |
Returns the current maximum number of buffers this can store. | |
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 pool. | |
size_t | getAllocatedSize () |
Returns the currently allocated size of all the buffers in bytes. | |
A lock-free pool of audio buffers.
If you need to quickly create and then return some audio buffers this class enables you to do that in a lock free way.
Note that the buffers can be pre-allocated but if you ask for a buffer which isn't in the pool, it will either resize an existing one or allocate a new one.
After processing a constant audio graph for a while though this should be completely allocation and lock-free.
|
default |
Create an empty pool.
You won't be able to use this pool right away, you must set a capacity and reserve some a number of buffers for it to use first.
tracktion::graph::AudioBufferPool::AudioBufferPool | ( | size_t | maxCapacity | ) |
choc::buffer::ChannelArrayBuffer< float > tracktion::graph::AudioBufferPool::allocate | ( | choc::buffer::Size | size | ) |
Returns an allocated buffer for a given size from the pool.
This will attempt to get a buffer from the pre-allocated pool that can fit the size but if one can't easily be found, it will allocate one with new and return it.
Additionally, the returned buffer may be bigger than the size asked for so be sure to only use a view of it but retain ownership of the whole buffer. You can release it back to the pool later. It will also contain junk so be sure to clear or initialise the view required before use.
[[ thread_safe ]]
bool tracktion::graph::AudioBufferPool::release | ( | choc::buffer::ChannelArrayBuffer< float > && | buffer | ) |
Releases an allocated buffer back to the pool.
[[ thread_safe ]]
Referenced by getNumBuffers(), and reserve().
void tracktion::graph::AudioBufferPool::reset | ( | ) |
Releases all the internal allocated storage.
void tracktion::graph::AudioBufferPool::setCapacity | ( | size_t | maxCapacity | ) |
Sets the maximum number of buffers this can store.
Referenced by AudioBufferPool().
size_t tracktion::graph::AudioBufferPool::getCapacity | ( | ) | const |
Returns the current maximum number of buffers this can store.
void tracktion::graph::AudioBufferPool::reserve | ( | size_t | numBuffers, |
choc::buffer::Size | size | ||
) |
Reserves a number of buffers of a given size, preallocating them.
References release().
Referenced by tracktion::graph::node_player_utils::reserveAudioBufferPool().
size_t tracktion::graph::AudioBufferPool::getNumBuffers | ( | ) |
Returns the current number of buffers in the pool.
N.B. This isn't safe to call concurrently with any other methods.
References release().
size_t tracktion::graph::AudioBufferPool::getAllocatedSize | ( | ) |
Returns the currently allocated size of all the buffers in bytes.
N.B. This isn't safe to call concurrently with any other methods as it needs to pop and then push all the elements to examine their size.