Class: AudiobufferPool
Overview
Memory allocation is a blocking operation, therefore should be avoided in a real-time audio context. AudiobufferPool can allocate audio buffers from pre-allocated memory without any blocking. Every buffer has a retain count which can be incremented by calling retain
and decremented by calling release
. Whenever the retain count value reaches zero, the buffer is destroyed and its memory will be automatically reused for another buffer.
How to implement
AudiobufferPool::initialize();...void *buffer = AudiobufferPool::getBuffer(128); // Returns a 128 byte buffer with retain count set to 1AudiobufferPool::retainBuffer(buffer); // Retains buffer (retain count = 2)AudiobufferPool::releaseBuffer(buffer); // Releases buffer (retain count = 1)AudiobufferPool::releaseBuffer(buffer); // Releases buffer
Methods
getBuffer
METHODCreates a buffer with retain count set to 1, similar to an NSObject in Objective-C. Use releaseBuffer() to release. This method never blocks, never locks, is very fast and safe to use in a realtime thread. Can be called concurrently on any thread.ParametersReturnsName Type Default Description sizeBytes unsigned int
The requested buffer's size in bytes. Maximum: 8388608 (8 MB). Type Description void *
The buffer. initialize
METHODInitializes the buffer pool.ParametersNoneReturnsNonereleaseBuffer
METHODRelease a buffer, similar to an NSObject in Objective-C (decreases the retain count by 1, the memory will be freed after retain count reaches 0). This method never blocks, never locks, is very fast and safe to use in a realtime thread. Can be called concurrently on any thread.ParametersReturnsName Type Default Description buffer void *
The buffer. NoneretainBuffer
METHODRetains a buffer, similar to Objective-C (increases the retain count by 1, similar to an NSObject in Objective-C). This method never blocks, never locks, is very fast and safe to use in a realtime thread. Can be called concurrently on any thread.ParametersReturnsName Type Default Description buffer void *
The buffer. None
is not supported here.