TracktionEngine
Public Member Functions | List of all members
tracktion::graph::AudioBufferPool Class Reference

A lock-free pool of audio buffers. More...

Public Member Functions

 AudioBufferPool ()=default
 Create an empty pool. More...
 
 AudioBufferPool (size_t maxCapacity)
 Creates pool with a given max capacity. More...
 
choc::buffer::ChannelArrayBuffer< float > allocate (choc::buffer::Size)
 Returns an allocated buffer for a given size from the pool. More...
 
bool release (choc::buffer::ChannelArrayBuffer< float > &&)
 Releases an allocated buffer back to the pool. More...
 
void reset ()
 Releases all the internal allocated storage. More...
 
void setCapacity (size_t)
 Sets the maximum number of buffers this can store. More...
 
size_t getCapacity () const
 Returns the current maximum number of buffers this can store. More...
 
void reserve (size_t numBuffers, choc::buffer::Size)
 Reserves a number of buffers of a given size, preallocating them. More...
 
size_t getNumBuffers ()
 Returns the current number of buffers in the pool. More...
 
size_t getAllocatedSize ()
 Returns the currently allocated size of all the buffers in bytes. More...
 

Detailed Description

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.

Constructor & Destructor Documentation

◆ AudioBufferPool() [1/2]

tracktion::graph::AudioBufferPool::AudioBufferPool ( )
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.

See also
setCapacity, reserve

◆ AudioBufferPool() [2/2]

tracktion::graph::AudioBufferPool::AudioBufferPool ( size_t  maxCapacity)

Creates pool with a given max capacity.

See also
reserve

References setCapacity().

Member Function Documentation

◆ allocate()

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.

See also
release

[[ thread_safe ]]

◆ release()

bool tracktion::graph::AudioBufferPool::release ( choc::buffer::ChannelArrayBuffer< float > &&  buffer)

Releases an allocated buffer back to the pool.

Returns
true if the buffer was able to fit in the pool, false if it couldn't and had to be deallocated in place

[[ thread_safe ]]

Referenced by getNumBuffers(), and reserve().

◆ reset()

void tracktion::graph::AudioBufferPool::reset ( )

Releases all the internal allocated storage.

◆ setCapacity()

void tracktion::graph::AudioBufferPool::setCapacity ( size_t  maxCapacity)

Sets the maximum number of buffers this can store.

Referenced by AudioBufferPool().

◆ getCapacity()

size_t tracktion::graph::AudioBufferPool::getCapacity ( ) const

Returns the current maximum number of buffers this can store.

◆ reserve()

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().

◆ getNumBuffers()

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().

◆ getAllocatedSize()

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.


The documentation for this class was generated from the following file: