30 namespace cinder {
namespace audio {
namespace dsp {
45 RingBufferT() : mData( nullptr ), mAllocatedSize( 0 ), mWriteIndex( 0 ), mReadIndex( 0 ) {}
53 : mData( other.mData ), mAllocatedSize( other.mAllocatedSize ), mWriteIndex( 0 ), mReadIndex( 0 )
55 other.mData =
nullptr;
56 other.mAllocatedSize = 0;
67 size_t allocatedSize = count + 1;
70 mData = (T *)::realloc( mData, allocatedSize *
sizeof( T ) );
72 mData = (T *)::calloc( allocatedSize,
sizeof( T ) );
76 mAllocatedSize = allocatedSize;
88 return mAllocatedSize - 1;
107 const size_t writeIndex = mWriteIndex.load( std::memory_order_relaxed );
108 const size_t readIndex = mReadIndex.load( std::memory_order_acquire );
113 size_t writeIndexAfter = writeIndex +
count;
115 if( writeIndex + count > mAllocatedSize ) {
116 size_t countA = mAllocatedSize - writeIndex;
117 size_t countB = count - countA;
119 std::memcpy( mData + writeIndex, array, countA *
sizeof( T ) );
120 std::memcpy( mData, array + countA, countB *
sizeof( T ) );
121 writeIndexAfter -= mAllocatedSize;
124 std::memcpy( mData + writeIndex, array, count *
sizeof( T ) );
125 if( writeIndexAfter == mAllocatedSize )
129 mWriteIndex.store( writeIndexAfter, std::memory_order_release );
137 const size_t writeIndex = mWriteIndex.load( std::memory_order_acquire );
138 const size_t readIndex = mReadIndex.load( std::memory_order_relaxed );
143 size_t readIndexAfter = readIndex +
count;
145 if( readIndex + count > mAllocatedSize ) {
146 size_t countA = mAllocatedSize - readIndex;
147 size_t countB = count - countA;
149 std::memcpy( array, mData + readIndex, countA *
sizeof( T ) );
150 std::memcpy( array + countA, mData, countB *
sizeof( T ) );
152 readIndexAfter -= mAllocatedSize;
155 std::memcpy( array, mData + readIndex, count *
sizeof( T ) );
156 if( readIndexAfter == mAllocatedSize )
160 mReadIndex.store( readIndexAfter, std::memory_order_release );
167 size_t result = readIndex - writeIndex - 1;
168 if( writeIndex >= readIndex )
169 result += mAllocatedSize;
176 if( writeIndex >= readIndex )
177 return writeIndex - readIndex;
179 return writeIndex + mAllocatedSize - readIndex;
184 size_t mAllocatedSize;
185 std::atomic<size_t> mWriteIndex, mReadIndex;
void resize(size_t count)
Resizes the container to contain count maximum elements. Invalidates the internal buffer and resets r...
Definition: RingBuffer.h:65
bool read(T *array, size_t count)
Reads count elements from the internal buffer into array.
Definition: RingBuffer.h:135
void clear()
Invalidates the internal buffer and resets read / write indices to 0.
Definition: RingBuffer.h:80
Ringbuffer (aka circular buffer) data structure for use in concurrent audio scenarios.
Definition: RingBuffer.h:42
RingBufferT< float > RingBuffer
Definition: RingBuffer.h:188
size_t getAvailableWrite() const
Returns the number of elements available for wrtiing.
Definition: RingBuffer.h:91
~RingBufferT()
Definition: RingBuffer.h:59
GLuint GLuint GLsizei count
Definition: GLee.h:963
size_t getSize() const
Returns the maximum number of elements.
Definition: RingBuffer.h:86
RingBufferT(size_t count)
Constructs a RingBufferT with count maximum elements.
Definition: RingBuffer.h:47
RingBufferT()
Constructs a RingBufferT with size = 0.
Definition: RingBuffer.h:45
bool write(const T *array, size_t count)
Writes count elements into the internal buffer from array.
Definition: RingBuffer.h:105
RingBufferT(RingBufferT &&other)
Definition: RingBuffer.h:52
size_t getAvailableRead() const
Returns the number of elements available for wrtiing.
Definition: RingBuffer.h:96
#define CI_ASSERT(expr)
Definition: CinderAssert.h:75