VAE - Virtual Audio Engine 1
Small Data Driven Audio Engine
tklb::AudioRingBufferTpl< T, STORAGE > Class Template Reference

#include <TAudioRingBuffer.hpp>

Inheritance diagram for tklb::AudioRingBufferTpl< T, STORAGE >:
Collaboration diagram for tklb::AudioRingBufferTpl< T, STORAGE >:

Public Member Functions

 AudioRingBufferTpl ()
 
 AudioRingBufferTpl (const Size length, const uchar channels)
 
void reset ()
 
template<typename T2 , class STORAGE2 >
Size peek (AudioBufferTpl< T2, STORAGE2 > &out, Size elements, Size offsetSrc=0, Size offsetDst=0)
 Puts a number of elements in the buffer provided. More...
 
template<typename T2 , class STORAGE2 >
Size pop (AudioBufferTpl< T2, STORAGE2 > &out, const Size elements, Size offsetSrc=0, Size offsetDst=0)
 Pops a number of elements in the buffer provided. More...
 
template<typename T2 , class STORAGE2 >
Size push (const AudioBufferTpl< T2, STORAGE2 > &in, Size offsetSrc=0)
 Adds validSize() amount of frames to the buffer. More...
 
Size remaining () const
 Returns how many more elements the buffer can hold. More...
 
Size filled () const
 Returns how many elements are in the buffer. More...
 
- Public Member Functions inherited from tklb::AudioBufferTpl< T, HeapBuffer< T, DEFAULT_ALIGNMENT > >
 AudioBufferTpl ()
 Empty buffer with no memory allocated yet. More...
 
 AudioBufferTpl (const Size length, const uchar channels)
 Buffer with memory allocated. More...
 
 AudioBufferTpl (AudioBufferTpl &&source)=default
 move contructor is implicitly deleted Don't know why More...
 
 AudioBufferTpl (const AudioBufferTpl &)=delete
 
 AudioBufferTpl (const AudioBufferTpl *)=delete
 
 ~AudioBufferTpl ()
 
AudioBufferTploperator= (AudioBufferTpl &&source)=default
 
AudioBufferTploperator= (const AudioBufferTpl &)=delete
 
void set (const T2 *samples, Size length, const uchar channel=0, const Size offsetDst=0)
 Set a single channel from an array. More...
 
void set (T2 **const samples, const Size length, const uchar channels, const Size offsetSrc=0, const Size offsetDst=0)
 Set multiple channels from a 2D array. More...
 
void set (const AudioBufferTpl< T2, STORAGE2 > &buffer, Size length=0, const Size offsetSrc=0, const Size offsetDst=0)
 Set from another buffer object, will not adjust size and channel count! Use clone() instead. More...
 
void set (T value=0, Size length=0, const Size offsetDst=0)
 Set the entire buffer to a constant value. More...
 
void setFromInterleaved (const T2 *samples, Size length, const uchar channels, Size offsetSrc=0, const Size offsetDst=0)
 Set multiple channels from an interleaved array. More...
 
void clone (const AudioBufferTpl< T2, STORAGE2 > &buffer)
 Match the size of the provided buffer and copy the contents. More...
 
bool resize (const Size length, uchar channels)
 ! Will not keep the contents! Resizes the buffer to the desired length and channel count. More...
 
void resize (const Size length)
 Resize to the provided length and keep the channelcount. More...
 
void resize (const AudioBufferTpl< T2, STORAGE2 > &buffer)
 Resize to match the provided buffer. More...
 
void add (const AudioBufferTpl< T2, STORAGE2 > &buffer, Size length=0, Size offsetSrc=0, Size offsetDst=0)
 Add the provided buffer. More...
 
void add (T value)
 Adds a constant to the contents of the buffer. More...
 
void multiply (const AudioBufferTpl< T2, STORAGE2 > &buffer, Size length=0, Size offsetSrc=0, Size offsetDst=0)
 Multiply two buffers. More...
 
void multiply (T value)
 Mutliplies the content of the buffer with a constant. More...
 
bool inject (T *mem, const Size size, const uchar channels)
 Inject forgeign memory to be used by the buffer. More...
 
bool inject (const T *mem, const Size size, const uchar channels)
 Inject const forgeign memory to be used by the buffer. More...
 
uchar channels () const
 Returns the amount of channels. More...
 
Size size () const
 Returns the allocated length of the buffer. More...
 
Size validSize () const
 Returns the length of actually valid audio in the buffer. More...
 
void setValidSize (const Size v)
 Set the amount of valid samples currently in the buffer This is mostly a convenience flag since the actual size of the buffer may be larger. More...
 
T * get (const uchar channel)
 
const T * get (const uchar channel) const
 
const T * operator[] (const uchar channel) const
 
T * operator[] (const uchar channel)
 
void getRaw (T **put)
 Fills an 2d array of size maxChannels() with pointers to each channel. More...
 
void getRaw (const T **put) const
 Fills an 2d array of size maxChannels() with pointers to each channel. More...
 
Size put (T2 *target, Size length=0, const uchar channel=0, const Size offset=0) const
 Fill the provided array with the contents of this buffer. More...
 
Size put (T2 **target, const Size length=0, uchar channels=0, const Size offset=0) const
 Fill the provided 2D array with the contents of this buffer. More...
 
Size putInterleaved (T2 *buffer, Size length=0, const Size offset=0) const
 Puts the interleaved contents in the target buffer. More...
 

Private Types

using Base = AudioBufferTpl< T, STORAGE >
 
using uchar = unsigned char
 
using Size = typename Base::Size
 

Private Attributes

Size mHead = 0
 
Size mTail = 0
 

Additional Inherited Members

- Public Types inherited from tklb::AudioBufferTpl< T, HeapBuffer< T, DEFAULT_ALIGNMENT > >
using Sample = T
 
using Storage = HeapBuffer< T, DEFAULT_ALIGNMENT >
 
using uchar = unsigned char
 
using ushort = unsigned short
 
using uint = unsigned int
 
using Size = typename Storage::Size
 
- Static Public Member Functions inherited from tklb::AudioBufferTpl< T, HeapBuffer< T, DEFAULT_ALIGNMENT > >
static constexpr bool needsScaling ()
 Figure out if conversions between types like float -> int or int -> short need scaling applied. More...
 
static constexpr Ratio getConversionScale ()
 calculate the scale factor needed between the types More...
 
- Public Attributes inherited from tklb::AudioBufferTpl< T, HeapBuffer< T, DEFAULT_ALIGNMENT > >
ushort sampleRate
 Only relevant for resampling and oversampling. More...
 
- Static Public Attributes inherited from tklb::AudioBufferTpl< T, HeapBuffer< T, DEFAULT_ALIGNMENT > >
static constexpr Size Stride
 

Detailed Description

template<typename T, class STORAGE = HeapBuffer<T, DEFAULT_ALIGNMENT>>
class tklb::AudioRingBufferTpl< T, STORAGE >

Definition at line 8 of file TAudioRingBuffer.hpp.

Member Typedef Documentation

◆ Base

template<typename T , class STORAGE = HeapBuffer<T, DEFAULT_ALIGNMENT>>
using tklb::AudioRingBufferTpl< T, STORAGE >::Base = AudioBufferTpl<T, STORAGE>
private

Definition at line 9 of file TAudioRingBuffer.hpp.

◆ Size

template<typename T , class STORAGE = HeapBuffer<T, DEFAULT_ALIGNMENT>>
using tklb::AudioRingBufferTpl< T, STORAGE >::Size = typename Base::Size
private

Definition at line 11 of file TAudioRingBuffer.hpp.

◆ uchar

template<typename T , class STORAGE = HeapBuffer<T, DEFAULT_ALIGNMENT>>
using tklb::AudioRingBufferTpl< T, STORAGE >::uchar = unsigned char
private

Definition at line 10 of file TAudioRingBuffer.hpp.

Constructor & Destructor Documentation

◆ AudioRingBufferTpl() [1/2]

template<typename T , class STORAGE = HeapBuffer<T, DEFAULT_ALIGNMENT>>
tklb::AudioRingBufferTpl< T, STORAGE >::AudioRingBufferTpl ( )
inline

Definition at line 16 of file TAudioRingBuffer.hpp.

16{ }

◆ AudioRingBufferTpl() [2/2]

template<typename T , class STORAGE = HeapBuffer<T, DEFAULT_ALIGNMENT>>
tklb::AudioRingBufferTpl< T, STORAGE >::AudioRingBufferTpl ( const Size  length,
const uchar  channels 
)
inline

Definition at line 18 of file TAudioRingBuffer.hpp.

19 : AudioBufferTpl<T, STORAGE>(length, channels) { }
uchar channels() const
Returns the amount of channels.

Member Function Documentation

◆ filled()

template<typename T , class STORAGE = HeapBuffer<T, DEFAULT_ALIGNMENT>>
Size tklb::AudioRingBufferTpl< T, STORAGE >::filled ( ) const
inline

Returns how many elements are in the buffer.

Definition at line 120 of file TAudioRingBuffer.hpp.

120{ return mHead; }
Here is the caller graph for this function:

◆ peek()

template<typename T , class STORAGE = HeapBuffer<T, DEFAULT_ALIGNMENT>>
template<typename T2 , class STORAGE2 >
Size tklb::AudioRingBufferTpl< T, STORAGE >::peek ( AudioBufferTpl< T2, STORAGE2 > &  out,
Size  elements,
Size  offsetSrc = 0,
Size  offsetDst = 0 
)
inline

Puts a number of elements in the buffer provided.

Parameters
outDestination buffer to store retrieved samples in
elementsHow many elements to retrieve from the ring buffer
offsetSrcWhere to start in the ringbuffer
offsetDstWhere to start in the destination buffer
Returns
How many elements where retrieved

Definition at line 35 of file TAudioRingBuffer.hpp.

35 {
36 const Size head = mHead - offsetSrc; // Offset the head
37 if (elements > head) {
38 elements = head; // Clamp the elements to peek to the elements in the buffer
39 }
40 if (elements > 0) {
41 Size tailStart = mTail - head; // This should always be negative when the offset is 0
42 if (mTail < head) {
43 // So move it back
44 // TODO tklb this does overflow, which is a little mad
45 tailStart += Base::size();
46 }
47 const Size spaceLeft = Base::size() - tailStart;
48 if (spaceLeft < elements) {
49 // Means it wraps around and split in two moves
50 out.set(*this, spaceLeft , tailStart, offsetDst);
51 out.set(*this, (elements - spaceLeft), 0 , spaceLeft + offsetDst);
52 }
53 else {
54 // Enough buffer left and can be done in one step
55 out.set(*this, elements, tailStart, offsetDst);
56 }
57 }
58 out.setValidSize(elements);
59 return elements;
60 }
Size size() const
Returns the allocated length of the buffer.
typename Base::Size Size
Here is the call graph for this function:
Here is the caller graph for this function:

◆ pop()

template<typename T , class STORAGE = HeapBuffer<T, DEFAULT_ALIGNMENT>>
template<typename T2 , class STORAGE2 >
Size tklb::AudioRingBufferTpl< T, STORAGE >::pop ( AudioBufferTpl< T2, STORAGE2 > &  out,
const Size  elements,
Size  offsetSrc = 0,
Size  offsetDst = 0 
)
inline

Pops a number of elements in the buffer provided.

Parameters
outDestination buffer to store retrieved samples in
elementsHow many elements to retrieve from the ring buffer
offsetSrcWhere to start in the ringbuffer
offsetDstWhere to start in the destination buffer
Returns
How many elements where retrieved

Definition at line 71 of file TAudioRingBuffer.hpp.

71 {
72 const Size elementsOut = peek(out, elements, offsetSrc, offsetDst);
73 mHead -= elementsOut; // Move the head back, can't exceed bounds since it was clamped in peek
74 return elementsOut;
75 }
Size peek(AudioBufferTpl< T2, STORAGE2 > &out, Size elements, Size offsetSrc=0, Size offsetDst=0)
Puts a number of elements in the buffer provided.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ push()

template<typename T , class STORAGE = HeapBuffer<T, DEFAULT_ALIGNMENT>>
template<typename T2 , class STORAGE2 >
Size tklb::AudioRingBufferTpl< T, STORAGE >::push ( const AudioBufferTpl< T2, STORAGE2 > &  in,
Size  offsetSrc = 0 
)
inline

Adds validSize() amount of frames to the buffer.

Parameters
inSource buffer
offsetSrcWhere to start in the source buffer
Returns
How many elements where stored in the ring buffer

Clamp the elements added to the buffer to it's bounds This ring buffer will stop adding elements if that's the case instead of wrapping around

Definition at line 84 of file TAudioRingBuffer.hpp.

84 {
85 const Size spaceLeftHead = Base::size() - mHead; // Space left before exceeding upper buffer bounds
86 Size elements = in.validSize();
87 if (elements > spaceLeftHead) {
88 /**
89 * Clamp the elements added to the buffer to it's bounds
90 * This ring buffer will stop adding elements if that's the case instead of wrapping around
91 */
92 elements = spaceLeftHead;
93 }
94 if (elements > 0) {
95 const Size spaceLeftTail = Base::size() - mTail;
96 if (spaceLeftTail < elements) {
97 // We'll need to wrap around since there's not enough space left
98 Base::set(in, spaceLeftTail , 0 + offsetSrc , mTail);
99 Base::set(in, (elements - spaceLeftTail), spaceLeftTail + offsetSrc);
100 mTail = elements - spaceLeftTail;
101 }
102 else { // If there's enough space left we can just copy the buffer starting at the tail index
103 Base::set(in, elements, offsetSrc, mTail);
104 // If we end up taking all space left for the tail, move it back to the start, otherwise move it forward
105 mTail = (spaceLeftTail == elements) ? 0 : mTail + elements;
106 }
107 mHead += elements; // Move the head forward, can't exceed the bounds since we clamped it
108 }
109 return elements;
110 }
void set(const T2 *samples, Size length, const uchar channel=0, const Size offsetDst=0)
Set a single channel from an array.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ remaining()

template<typename T , class STORAGE = HeapBuffer<T, DEFAULT_ALIGNMENT>>
Size tklb::AudioRingBufferTpl< T, STORAGE >::remaining ( ) const
inline

Returns how many more elements the buffer can hold.

Definition at line 115 of file TAudioRingBuffer.hpp.

115{ return Base::size() - mHead; }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ reset()

template<typename T , class STORAGE = HeapBuffer<T, DEFAULT_ALIGNMENT>>
void tklb::AudioRingBufferTpl< T, STORAGE >::reset ( )
inline

Definition at line 21 of file TAudioRingBuffer.hpp.

21 {
22 Base::set(0.0);
23 mTail = mHead = 0;
24 }
Here is the call graph for this function:

Member Data Documentation

◆ mHead

template<typename T , class STORAGE = HeapBuffer<T, DEFAULT_ALIGNMENT>>
Size tklb::AudioRingBufferTpl< T, STORAGE >::mHead = 0
private

Definition at line 12 of file TAudioRingBuffer.hpp.

◆ mTail

template<typename T , class STORAGE = HeapBuffer<T, DEFAULT_ALIGNMENT>>
Size tklb::AudioRingBufferTpl< T, STORAGE >::mTail = 0
private

Definition at line 13 of file TAudioRingBuffer.hpp.


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