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

Class for handling the most basic audio needs. More...

#include <TAudioBuffer.hpp>

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

Public Types

using Sample = T
 
using Storage = STORAGE
 
using uchar = unsigned char
 
using ushort = unsigned short
 
using uint = unsigned int
 
using Size = typename Storage::Size
 

Public Member Functions

 AudioBufferTpl ()
 Empty buffer with no memory allocated yet. More...
 
 AudioBufferTpl (const Size length, const uchar channels)
 Buffer with memory allocated. More...
 
 ~AudioBufferTpl ()
 
 AudioBufferTpl (AudioBufferTpl &&source)=default
 move contructor is implicitly deleted Don't know why More...
 
AudioBufferTploperator= (AudioBufferTpl &&source)=default
 
AudioBufferTploperator= (const AudioBufferTpl &)=delete
 
 AudioBufferTpl (const AudioBufferTpl &)=delete
 
 AudioBufferTpl (const AudioBufferTpl *)=delete
 
template<typename T2 >
void set (const T2 *samples, Size length, const uchar channel=0, const Size offsetDst=0)
 Set a single channel from an array. More...
 
template<typename T2 >
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...
 
template<typename T2 , class STORAGE2 >
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...
 
template<typename T2 >
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...
 
template<typename T2 , class STORAGE2 >
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...
 
template<typename T2 , class STORAGE2 >
void resize (const AudioBufferTpl< T2, STORAGE2 > &buffer)
 Resize to match the provided buffer. More...
 
template<typename T2 , class STORAGE2 >
void add (const AudioBufferTpl< T2, STORAGE2 > &buffer, Size length=0, Size offsetSrc=0, Size offsetDst=0)
 Add the provided buffer. More...
 
template<typename T2 , class STORAGE2 >
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...
 
void add (T value)
 Adds a constant to the contents of the buffer. 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...
 
template<typename T2 >
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...
 
template<typename T2 >
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...
 
template<typename T2 >
Size putInterleaved (T2 *buffer, Size length=0, const Size offset=0) const
 Puts the interleaved contents in the target buffer. More...
 

Static Public Member Functions

template<typename T2 >
static constexpr bool needsScaling ()
 Figure out if conversions between types like float -> int or int -> short need scaling applied. More...
 
template<typename T1 , typename T2 , typename Ratio = float>
static constexpr Ratio getConversionScale ()
 calculate the scale factor needed between the types More...
 

Public Attributes

ushort sampleRate = 0
 Only relevant for resampling and oversampling. More...
 

Static Public Attributes

static constexpr Size Stride = xsimd::simd_type<T>::size
 

Private Member Functions

void assertOnConstMem ()
 

Private Attributes

Storage mBuffer
 
Size mValidSize = 0
 
uchar mChannels = 0
 

Detailed Description

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

Class for handling the most basic audio needs.

Does convenient type conversions TODO check if using a single buffer instead of one for each channel improves performance

Template Parameters
TSample type. Can be anything std::is_arithmetic
STORAGEStorage type, tklb::HeapBuffer for now since there are a few things missing in a std::vector

Definition at line 36 of file TAudioBuffer.hpp.

Member Typedef Documentation

◆ Sample

template<typename T , class STORAGE = HeapBuffer<T, DEFAULT_ALIGNMENT>>
using tklb::AudioBufferTpl< T, STORAGE >::Sample = T

Definition at line 39 of file TAudioBuffer.hpp.

◆ Size

template<typename T , class STORAGE = HeapBuffer<T, DEFAULT_ALIGNMENT>>
using tklb::AudioBufferTpl< T, STORAGE >::Size = typename Storage::Size

Definition at line 44 of file TAudioBuffer.hpp.

◆ Storage

template<typename T , class STORAGE = HeapBuffer<T, DEFAULT_ALIGNMENT>>
using tklb::AudioBufferTpl< T, STORAGE >::Storage = STORAGE

Definition at line 40 of file TAudioBuffer.hpp.

◆ uchar

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

Definition at line 41 of file TAudioBuffer.hpp.

◆ uint

template<typename T , class STORAGE = HeapBuffer<T, DEFAULT_ALIGNMENT>>
using tklb::AudioBufferTpl< T, STORAGE >::uint = unsigned int

Definition at line 43 of file TAudioBuffer.hpp.

◆ ushort

template<typename T , class STORAGE = HeapBuffer<T, DEFAULT_ALIGNMENT>>
using tklb::AudioBufferTpl< T, STORAGE >::ushort = unsigned short

Definition at line 42 of file TAudioBuffer.hpp.

Constructor & Destructor Documentation

◆ AudioBufferTpl() [1/5]

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

Empty buffer with no memory allocated yet.

Definition at line 65 of file TAudioBuffer.hpp.

65{ }

◆ AudioBufferTpl() [2/5]

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

Buffer with memory allocated.

Definition at line 70 of file TAudioBuffer.hpp.

70 {
71 resize(length, channels);
72 }
bool resize(const Size length, uchar channels)
! Will not keep the contents! Resizes the buffer to the desired length and channel count.
uchar channels() const
Returns the amount of channels.
Here is the call graph for this function:

◆ ~AudioBufferTpl()

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

Definition at line 74 of file TAudioBuffer.hpp.

74 {
77 }
#define TKLB_ASSERT_STATE(condition)
This can be used to write some additional code only needed when doing assertions.
Definition: TAssert.h:32

◆ AudioBufferTpl() [3/5]

template<typename T , class STORAGE = HeapBuffer<T, DEFAULT_ALIGNMENT>>
tklb::AudioBufferTpl< T, STORAGE >::AudioBufferTpl ( AudioBufferTpl< T, STORAGE > &&  source)
default

move contructor is implicitly deleted Don't know why

◆ AudioBufferTpl() [4/5]

template<typename T , class STORAGE = HeapBuffer<T, DEFAULT_ALIGNMENT>>
tklb::AudioBufferTpl< T, STORAGE >::AudioBufferTpl ( const AudioBufferTpl< T, STORAGE > &  )
delete

◆ AudioBufferTpl() [5/5]

template<typename T , class STORAGE = HeapBuffer<T, DEFAULT_ALIGNMENT>>
tklb::AudioBufferTpl< T, STORAGE >::AudioBufferTpl ( const AudioBufferTpl< T, STORAGE > *  )
delete

Member Function Documentation

◆ add() [1/2]

template<typename T , class STORAGE = HeapBuffer<T, DEFAULT_ALIGNMENT>>
template<typename T2 , class STORAGE2 >
void tklb::AudioBufferTpl< T, STORAGE >::add ( const AudioBufferTpl< T2, STORAGE2 > &  buffer,
Size  length = 0,
Size  offsetSrc = 0,
Size  offsetDst = 0 
)
inline

Add the provided buffer.

Parameters
bufferSource buffer object
lengthSamples to add from the source buffer
offsetSrcStart offset in the source buffer
offsetDstStart offset in the target buffer

Definition at line 318 of file TAudioBuffer.hpp.

323 {
324 TKLB_ASSERT(validSize() >= offsetDst)
325 TKLB_ASSERT(buffer.validSize() >= offsetSrc)
326 length = length == 0 ? buffer.validSize() - offsetSrc : length;
327 length = std::min(buffer.validSize() - offsetSrc, validSize() - offsetDst);
328 const uchar channelCount = std::min(buffer.channels(), channels());
329
330 #ifndef TKLB_NO_SIMD
331 if (std::is_same<T2, T>::value) {
332 const Size vectorize = length - (length % Stride);
333 for (uchar c = 0; c < channelCount; c++) {
334 T* out = get(c) + offsetDst;
335 const T* in = reinterpret_cast<const T*>(buffer[c]) + offsetSrc;
336 for(Size i = 0; i < vectorize; i += Stride) {
337 xsimd::simd_type<T> a = xsimd::load_aligned(in + i);
338 xsimd::simd_type<T> b = xsimd::load_aligned(out + i);
339 xsimd::store_aligned(out + i, (a + b));
340 }
341 for(Size i = vectorize; i < length; i++) {
342 out[i] += in[i];
343 }
344 }
345 return;
346 }
347 #endif
348
349 // If the type doen't match or simd is diabled
350 for (uchar c = 0; c < channelCount; c++) {
351 T* out = get(c) + offsetDst;
352 const T2* in = buffer[c] + offsetSrc;
353 for(Size i = 0; i < length; i++) {
354 out[i] += in[i];
355 }
356 }
357 }
#define TKLB_ASSERT(condition)
Wrap assertions.
Definition: TAssert.h:18
typename Storage::Size Size
static constexpr Size Stride
Size validSize() const
Returns the length of actually valid audio in the buffer.
unsigned char uchar
T * get(const uchar channel)
T min(const T &v1, const T &v2)
Definition: TMath.hpp:16
Here is the call graph for this function:
Here is the caller graph for this function:

◆ add() [2/2]

template<typename T , class STORAGE = HeapBuffer<T, DEFAULT_ALIGNMENT>>
void tklb::AudioBufferTpl< T, STORAGE >::add ( value)
inline

Adds a constant to the contents of the buffer.

Parameters
value

Definition at line 441 of file TAudioBuffer.hpp.

441 {
442 const Size length = validSize();
443
444 #ifndef TKLB_NO_SIMD
445 const Size vectorize = length - (length % Stride);
446 for (uchar c = 0; c < channels(); c++) {
447 T* out = get(c);
448 for(Size i = 0; i < vectorize; i += Stride) {
449 xsimd::simd_type<T> b = xsimd::load_aligned(out + i);
450 xsimd::store_aligned(out + i, (b + value));
451 }
452 for(Size i = vectorize; i < length; i++) {
453 out[i] += value;
454 }
455 }
456 #else
457 for (uchar c = 0; c < channels(); c++) {
458 T* out = get(c);
459 for(Size i = 0; i < length; i++) {
460 out[i] += value;
461 }
462 }
463 #endif
464 }
Here is the call graph for this function:

◆ assertOnConstMem()

template<typename T , class STORAGE = HeapBuffer<T, DEFAULT_ALIGNMENT>>
void tklb::AudioBufferTpl< T, STORAGE >::assertOnConstMem ( )
inlineprivate

Definition at line 667 of file TAudioBuffer.hpp.

667{ T* data = mBuffer.data(); }
Here is the caller graph for this function:

◆ channels()

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

Returns the amount of channels.

Definition at line 505 of file TAudioBuffer.hpp.

505{ return mChannels; }
Here is the caller graph for this function:

◆ clone()

template<typename T , class STORAGE = HeapBuffer<T, DEFAULT_ALIGNMENT>>
template<typename T2 , class STORAGE2 >
void tklb::AudioBufferTpl< T, STORAGE >::clone ( const AudioBufferTpl< T2, STORAGE2 > &  buffer)
inline

Match the size of the provided buffer and copy the contents.

Definition at line 264 of file TAudioBuffer.hpp.

264 {
265 resize(buffer);
266 set(buffer);
267 }
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:

◆ get() [1/2]

template<typename T , class STORAGE = HeapBuffer<T, DEFAULT_ALIGNMENT>>
T * tklb::AudioBufferTpl< T, STORAGE >::get ( const uchar  channel)
inline

Definition at line 529 of file TAudioBuffer.hpp.

529 {
530 TKLB_ASSERT(channel < mChannels)
531 // We use the size of the buffer itself since this will result in aligned addresses
532 return mBuffer.data() + (channel * (mBuffer.size() / mChannels));
533 };
Here is the caller graph for this function:

◆ get() [2/2]

template<typename T , class STORAGE = HeapBuffer<T, DEFAULT_ALIGNMENT>>
const T * tklb::AudioBufferTpl< T, STORAGE >::get ( const uchar  channel) const
inline

Definition at line 535 of file TAudioBuffer.hpp.

535 {
536 TKLB_ASSERT(channel < mChannels)
537 return mBuffer.data() + (channel * (mBuffer.size() / mChannels));
538 };

◆ getConversionScale()

template<typename T , class STORAGE = HeapBuffer<T, DEFAULT_ALIGNMENT>>
template<typename T1 , typename T2 , typename Ratio = float>
static constexpr Ratio tklb::AudioBufferTpl< T, STORAGE >::getConversionScale ( )
inlinestaticconstexpr

calculate the scale factor needed between the types

Template Parameters
T1Source type
T2target type
RatioFloating point type since the value can be smaller than 1
Returns
constexpr Ratio

Definition at line 114 of file TAudioBuffer.hpp.

114 {
115 const Ratio maxt = std::is_floating_point<T2>::value ?
116 Ratio(1.0) : Ratio(std::numeric_limits<T2>::max()) - Ratio(1);
117 const Ratio maxt2 = std::is_floating_point<T1>::value ?
118 Ratio(1.0) : Ratio(std::numeric_limits<T1>::max()) - Ratio(1);
119 return maxt / maxt2;
120 }
T max(const T &v1, const T &v2)
Definition: TMath.hpp:21
Here is the call graph for this function:

◆ getRaw() [1/2]

template<typename T , class STORAGE = HeapBuffer<T, DEFAULT_ALIGNMENT>>
void tklb::AudioBufferTpl< T, STORAGE >::getRaw ( const T **  put) const
inline

Fills an 2d array of size maxChannels() with pointers to each channel.

Parameters
putPointers go here

Definition at line 559 of file TAudioBuffer.hpp.

559 {
560 for (uchar c = 0; c < mChannels; c++) {
561 put[c] = get(c);
562 }
563 }
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.
Here is the call graph for this function:

◆ getRaw() [2/2]

template<typename T , class STORAGE = HeapBuffer<T, DEFAULT_ALIGNMENT>>
void tklb::AudioBufferTpl< T, STORAGE >::getRaw ( T **  put)
inline

Fills an 2d array of size maxChannels() with pointers to each channel.

Parameters
putPointers go here

Definition at line 548 of file TAudioBuffer.hpp.

548 {
550 for (uchar c = 0; c < mChannels; c++) {
551 put[c] = get(c);
552 }
553 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ inject() [1/2]

template<typename T , class STORAGE = HeapBuffer<T, DEFAULT_ALIGNMENT>>
bool tklb::AudioBufferTpl< T, STORAGE >::inject ( const T *  mem,
const Size  size,
const uchar  channels 
)
inline

Inject const forgeign memory to be used by the buffer.

Potentially dangerous but useful when splitting up channels for processing

Parameters
memConst memory (No type conversions here)
sizeSize of the entire buffer
channelsHow many channels are contained in mem

Definition at line 491 of file TAudioBuffer.hpp.

491 {
492 if (!mBuffer.isAligned(mem)) {
493 TKLB_ASSERT(false)
494 return false;
495 }
497 mBuffer.inject(mem, size);
500 }
bool inject(T *mem, const Size size, const uchar channels)
Inject forgeign memory to be used by the buffer.
Size size() const
Returns the allocated length of the buffer.
Here is the call graph for this function:

◆ inject() [2/2]

template<typename T , class STORAGE = HeapBuffer<T, DEFAULT_ALIGNMENT>>
bool tklb::AudioBufferTpl< T, STORAGE >::inject ( T *  mem,
const Size  size,
const uchar  channels 
)
inline

Inject forgeign memory to be used by the buffer.

Potentially dangerous but useful when splitting up channels for processing

Parameters
memModifiable memory (No type conversions here)
sizeSize of the entire buffer
channelsHow many channels are contained in mem

Definition at line 473 of file TAudioBuffer.hpp.

473 {
474 if (!mBuffer.isAligned(mem)) {
475 TKLB_ASSERT(false)
476 return false;
477 }
479 mBuffer.inject(mem, size);
482 }
Here is the call graph for this function:

◆ multiply() [1/2]

template<typename T , class STORAGE = HeapBuffer<T, DEFAULT_ALIGNMENT>>
template<typename T2 , class STORAGE2 >
void tklb::AudioBufferTpl< T, STORAGE >::multiply ( const AudioBufferTpl< T2, STORAGE2 > &  buffer,
Size  length = 0,
Size  offsetSrc = 0,
Size  offsetDst = 0 
)
inline

Multiply two buffers.

Parameters
bufferSource buffer object
lengthSamples to multiply from the source buffer
offsetInStart offset in the source buffer
offsetStart offset in the target buffer

Definition at line 367 of file TAudioBuffer.hpp.

372 {
373 TKLB_ASSERT(validSize() >= offsetDst)
374 TKLB_ASSERT(buffer.validSize() >= offsetSrc)
375 length = length == 0 ? buffer.validSize() - offsetSrc : length;
376 length = std::min(buffer.validSize() - offsetSrc, validSize() - offsetDst);
377 const uchar channelsCount = std::min(buffer.channels(), channels());
378
379 #ifndef TKLB_NO_SIMD
380 if (std::is_same<T2, T>::value) {
381 const Size vectorize = length - (length % Stride);
382 for (uchar c = 0; c < channelsCount; c++) {
383 T* out = get(c) + offsetDst;
384 const T* in = reinterpret_cast<const T*>(buffer[c]) + offsetSrc;
385 for(Size i = 0; i < vectorize; i += Stride) {
386 xsimd::simd_type<T> a = xsimd::load_aligned(in + i);
387 xsimd::simd_type<T> b = xsimd::load_aligned(out + i);
388 xsimd::store_aligned(out + i, (a * b));
389 }
390 for(Size i = vectorize; i < length; i++) {
391 out[i] *= in[i];
392 }
393 }
394 return;
395 }
396 #endif
397
398 // If the type doen't match or simd is diabled
399 for (uchar c = 0; c < channelsCount; c++) {
400 T* out = get(c) + offsetDst;
401 const T2* in = buffer[c] + offsetSrc;
402 for(Size i = 0; i < length; i++) {
403 out[i] *= in[i];
404 }
405 }
406 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ multiply() [2/2]

template<typename T , class STORAGE = HeapBuffer<T, DEFAULT_ALIGNMENT>>
void tklb::AudioBufferTpl< T, STORAGE >::multiply ( value)
inline

Mutliplies the content of the buffer with a constant.

Parameters
valueConstant to multiply the buffer with

Definition at line 412 of file TAudioBuffer.hpp.

412 {
413 const Size length = validSize();
414
415 #ifndef TKLB_NO_SIMD
416 const Size vectorize = length - (length % Stride);
417 for (uchar c = 0; c < channels(); c++) {
418 T* out = get(c);
419 for(Size i = 0; i < vectorize; i += Stride) {
420 xsimd::simd_type<T> b = xsimd::load_aligned(out + i);
421 xsimd::store_aligned(out + i, (b * value));
422 }
423 for(Size i = vectorize; i < length; i++) {
424 out[i] *= value;
425 }
426 }
427 #else
428 for (uchar c = 0; c < channels(); c++) {
429 T* out = get(c);
430 for(Size i = 0; i < length; i++) {
431 out[i] *= value;
432 }
433 }
434 #endif
435 }
Here is the call graph for this function:

◆ needsScaling()

template<typename T , class STORAGE = HeapBuffer<T, DEFAULT_ALIGNMENT>>
template<typename T2 >
static constexpr bool tklb::AudioBufferTpl< T, STORAGE >::needsScaling ( )
inlinestaticconstexpr

Figure out if conversions between types like float -> int or int -> short need scaling applied.

Template Parameters
T2Other type compared to own type
Returns
true Scaling needs to be applied
false No scaling needs to be applied
See also
getConversionScale

Definition at line 99 of file TAudioBuffer.hpp.

99 {
100 if (std::is_floating_point<T>::value && std::is_floating_point<T2>::value) {
101 return false;
102 }
104 }
Here is the call graph for this function:

◆ operator=() [1/2]

template<typename T , class STORAGE = HeapBuffer<T, DEFAULT_ALIGNMENT>>
AudioBufferTpl & tklb::AudioBufferTpl< T, STORAGE >::operator= ( AudioBufferTpl< T, STORAGE > &&  source)
default

◆ operator=() [2/2]

template<typename T , class STORAGE = HeapBuffer<T, DEFAULT_ALIGNMENT>>
AudioBufferTpl & tklb::AudioBufferTpl< T, STORAGE >::operator= ( const AudioBufferTpl< T, STORAGE > &  )
delete

◆ operator[]() [1/2]

template<typename T , class STORAGE = HeapBuffer<T, DEFAULT_ALIGNMENT>>
T * tklb::AudioBufferTpl< T, STORAGE >::operator[] ( const uchar  channel)
inline

Definition at line 542 of file TAudioBuffer.hpp.

542{ return get(channel); }
Here is the call graph for this function:

◆ operator[]() [2/2]

template<typename T , class STORAGE = HeapBuffer<T, DEFAULT_ALIGNMENT>>
const T * tklb::AudioBufferTpl< T, STORAGE >::operator[] ( const uchar  channel) const
inline

Definition at line 540 of file TAudioBuffer.hpp.

540{ return get(channel); }
Here is the call graph for this function:

◆ put() [1/2]

template<typename T , class STORAGE = HeapBuffer<T, DEFAULT_ALIGNMENT>>
template<typename T2 >
Size tklb::AudioBufferTpl< T, STORAGE >::put ( T2 **  target,
const Size  length = 0,
uchar  channels = 0,
const Size  offset = 0 
) const
inline

Fill the provided 2D array with the contents of this buffer.

Parameters
targetThe array to fill
channelsHow many channels there are in the target buffer
lengthThe length of the output
Returns
The amount of samples written, might be less than requested

Definition at line 612 of file TAudioBuffer.hpp.

616 {
617 static_assert(std::is_arithmetic<T2>::value, "Need arithmetic type.");
618 Size res = 0;
619 channels = (channels == 0) ? mChannels : channels;
620 for (uchar c = 0; c < channels; c++) {
621 res = put(target[c], length, c, offset);
622 }
623 return res;
624 }
Here is the call graph for this function:

◆ put() [2/2]

template<typename T , class STORAGE = HeapBuffer<T, DEFAULT_ALIGNMENT>>
template<typename T2 >
Size tklb::AudioBufferTpl< T, STORAGE >::put ( T2 *  target,
Size  length = 0,
const uchar  channel = 0,
const Size  offset = 0 
) const
inline

Fill the provided array with the contents of this buffer.

Parameters
targetThe arry to fill
channelWhich source channel to use
lengthThe length of the output
offsetStart offset in the source (this) buffer
Returns
The amount of samples written, might be less than requested

Definition at line 574 of file TAudioBuffer.hpp.

579 {
580 static_assert(std::is_arithmetic<T2>::value, "Need arithmetic type.");
581 if (mChannels <= channel) { return 0; }
582 const Size valid = validSize();
583 TKLB_ASSERT(offset <= valid)
584 length = length == 0 ? valid : length;
585 length = std::min(length, valid - offset);
586 const T2* source = get(channel) + offset;
587 if (std::is_same<T2, T>::value) {
588 memory::copy(target, source, sizeof(T) * length);
589 } else {
590 if (!needsScaling<T2>()) {
591 for (Size i = 0; i < length; i++) {
592 target[i] = T2(source[i]);
593 }
594 } else {
595 constexpr auto scale = getConversionScale<T, T2>();
596 for (Size i = 0; i < length; i++) {
597 target[i] = T2(source[i] * scale);
598 }
599 }
600 }
601 return length;
602 }
static void copy(void *dst, const void *src, const size_t size)
memcpy wrapper
Definition: TMemoryUtil.hpp:14
Here is the call graph for this function:
Here is the caller graph for this function:

◆ putInterleaved()

template<typename T , class STORAGE = HeapBuffer<T, DEFAULT_ALIGNMENT>>
template<typename T2 >
Size tklb::AudioBufferTpl< T, STORAGE >::putInterleaved ( T2 *  buffer,
Size  length = 0,
const Size  offset = 0 
) const
inline

Puts the interleaved contents in the target buffer.

Parameters
bufferThe array to fill with interleaved audio
lengthNumber of frames to interleave (not the length of the interleaved buffer)
offsetOffset for the sourcebuffer (this)
Returns
Number of frames emitted

Definition at line 634 of file TAudioBuffer.hpp.

638 {
639 static_assert(std::is_arithmetic<T2>::value, "Need arithmetic type.");
640 const Size valid = validSize();
641 TKLB_ASSERT(offset <= valid)
642 const uchar chan = channels();
643 length = (length == 0) ? valid : length;
644 length = std::min(valid - offset, length);
645 Size out = 0;
646 for (uchar c = 0; c < chan; c++) {
647 Size j = c;
648 const T* data = get(c);
649 if (!needsScaling<T2>()) {
650 for (Size i = 0; i < length; i++) {
651 buffer[j] = T2(data[i + offset]);
652 j += chan;
653 }
654 } else {
655 constexpr auto scale = getConversionScale<T, T2>();
656 for (Size i = 0; i < length; i++) {
657 buffer[j] = T2(data[i + offset] * scale);
658 j += chan;
659 }
660 }
661 out += length;
662 }
663 return out / Size(channels());
664 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ resize() [1/3]

template<typename T , class STORAGE = HeapBuffer<T, DEFAULT_ALIGNMENT>>
template<typename T2 , class STORAGE2 >
void tklb::AudioBufferTpl< T, STORAGE >::resize ( const AudioBufferTpl< T2, STORAGE2 > &  buffer)
inline

Resize to match the provided buffer.

Definition at line 306 of file TAudioBuffer.hpp.

306 {
307 resize(buffer.size(), buffer.channels());
308 }
Here is the call graph for this function:

◆ resize() [2/3]

template<typename T , class STORAGE = HeapBuffer<T, DEFAULT_ALIGNMENT>>
void tklb::AudioBufferTpl< T, STORAGE >::resize ( const Size  length)
inline

Resize to the provided length and keep the channelcount.

If the channel count was 0 it will assume 1 channel instead.

Parameters
lengthThe desired length in samples. 0 will deallocate.

Definition at line 298 of file TAudioBuffer.hpp.

298 {
299 resize(length, std::max(uchar(1), mChannels));
300 }
Here is the call graph for this function:

◆ resize() [3/3]

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

! Will not keep the contents! Resizes the buffer to the desired length and channel count.

If the validSize = 0 it will be set to the new size for convenience. The size reported back when calling size is can be larger if the data is aligned

Parameters
lengthThe desired length in Samples. 0 will deallocate.
channelsDesired channel count. 0 will deallocate.

Definition at line 276 of file TAudioBuffer.hpp.

276 {
277 if (channels == mChannels && size() == length) { return true; }
278 // We need to ensure each channel is aligned so we add some padding after each channel
279 const auto elementAlign = mBuffer.closestChunkSize(length, mBuffer.Alignment / sizeof(T));
280 mBuffer.resize(0); // deallocate so we don't copy old misaligned signal over
281 mBuffer.resize(channels * elementAlign);
282
284
285 if (mValidSize == 0) {
286 mValidSize = length;
287 } else {
288 mValidSize = std::min(mValidSize, length);
289 }
290 return true;
291 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ set() [1/4]

template<typename T , class STORAGE = HeapBuffer<T, DEFAULT_ALIGNMENT>>
template<typename T2 , class STORAGE2 >
void tklb::AudioBufferTpl< T, STORAGE >::set ( const AudioBufferTpl< T2, STORAGE2 > &  buffer,
Size  length = 0,
const Size  offsetSrc = 0,
const Size  offsetDst = 0 
)
inline

Set from another buffer object, will not adjust size and channel count! Use clone() instead.

e.g. offset=10 and length=15 will copy 15 samples into the buffer starting at the 10th sample

Parameters
bufferSource buffer object
lengthSamples to copy in
offsetSrcStart offset in the source buffer
offsetDstStart offset in the target buffer

Definition at line 193 of file TAudioBuffer.hpp.

198 {
199 length = length == 0 ? buffer.validSize() : length;
200 for (uchar c = 0; c < buffer.channels(); c++) {
201 set(buffer[c] + offsetSrc, length, c, offsetDst);
202 }
203 }
Here is the call graph for this function:

◆ set() [2/4]

template<typename T , class STORAGE = HeapBuffer<T, DEFAULT_ALIGNMENT>>
template<typename T2 >
void tklb::AudioBufferTpl< T, STORAGE >::set ( const T2 *  samples,
Size  length,
const uchar  channel = 0,
const Size  offsetDst = 0 
)
inline

Set a single channel from an array.

Parameters
samplesAn Array containing the audio samples
lengthAmount of sample to copy
channelChannel index
offsetDstThe offset for the destination buffer (this)

Definition at line 130 of file TAudioBuffer.hpp.

135 {
136 static_assert(std::is_arithmetic<T2>::value, "Need arithmetic type.");
137 if (mChannels <= channel) { return; }
138 TKLB_ASSERT(size() >= offsetDst)
139 length = std::min(length, size() - offsetDst);
140 T* out = get(channel);
141 if (std::is_same<T2, T>::value) {
142 // Types are identical
143 memory::copy(out + offsetDst, samples, sizeof(T) * length);
144 } else {
145 if (!needsScaling<T2>()) {
146 // Both floats means they need to be converted but not scaled
147 for (Size i = 0; i < length; i++) {
148 out[i + offsetDst] = static_cast<T>(samples[i]);
149 }
150 } else {
151 // We also need to scale
152 // float -> int, int -> float, short -> int
153 constexpr auto scale = getConversionScale<T2, T>();
154 for (Size i = 0; i < length; i++) {
155 out[i + offsetDst] = static_cast<T>(samples[i] * scale);
156 }
157 }
158 }
159 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ set() [3/4]

template<typename T , class STORAGE = HeapBuffer<T, DEFAULT_ALIGNMENT>>
void tklb::AudioBufferTpl< T, STORAGE >::set ( value = 0,
Size  length = 0,
const Size  offsetDst = 0 
)
inline

Set the entire buffer to a constant value.

Parameters
valueValue to fill the buffer with
lengthSamples to set. 0 Sets all
offsetDstStart offset in the target buffer

Definition at line 211 of file TAudioBuffer.hpp.

215 {
216 TKLB_ASSERT(size() >= offsetDst)
217 length = std::min(size() - offsetDst, length ? length : size());
218 for (uchar c = 0; c < channels(); c++) {
219 std::fill_n(get(c) + offsetDst, length, value);
220 }
221 }
Here is the call graph for this function:

◆ set() [4/4]

template<typename T , class STORAGE = HeapBuffer<T, DEFAULT_ALIGNMENT>>
template<typename T2 >
void tklb::AudioBufferTpl< T, STORAGE >::set ( T2 **const  samples,
const Size  length,
const uchar  channels,
const Size  offsetSrc = 0,
const Size  offsetDst = 0 
)
inline

Set multiple channels from a 2D array.

Parameters
samplesA 2D Array containing the audio samples (float or double)
lengthSamples to copy in (single channel)
channelsChannel count
offsetSrcOffset in the source buffer
offsetDstOffset in the destination buffer

Definition at line 170 of file TAudioBuffer.hpp.

176 {
177 static_assert(std::is_arithmetic<T2>::value, "Need arithmetic type.");
178 for (uchar c = 0; c < channels; c++) {
179 set(samples[c] + offsetSrc, length, c, offsetDst);
180 }
181 }
Here is the call graph for this function:

◆ setFromInterleaved()

template<typename T , class STORAGE = HeapBuffer<T, DEFAULT_ALIGNMENT>>
template<typename T2 >
void tklb::AudioBufferTpl< T, STORAGE >::setFromInterleaved ( const T2 *  samples,
Size  length,
const uchar  channels,
Size  offsetSrc = 0,
const Size  offsetDst = 0 
)
inline

Set multiple channels from an interleaved array.

Parameters
samplesA 1D Array containing the interleaved audio samples (float or double)
lengthThe length of a single channel
channelsChannel count
offsetSrcStart offset in the source buffer
offsetDstStart offset in the target buffer

Definition at line 232 of file TAudioBuffer.hpp.

238 {
239 static_assert(std::is_arithmetic<T2>::value, "Need arithmetic type.");
240 TKLB_ASSERT(size() >= offsetDst)
241 length = std::min(size() - offsetDst, length);
242 offsetSrc *= channels;
243 for (uchar c = 0; c < std::min(channels, mChannels); c++) {
244 T* out = get(c);
245 if (!needsScaling<T2>()) {
246 for(Size i = 0, j = c + offsetSrc; i < length; i++, j+= channels) {
247 out[i + offsetDst] = static_cast<T>(samples[j]);
248 }
249 } else {
250 constexpr auto scale = getConversionScale<T2, T>();
251 for(Size i = 0, j = c + offsetSrc; i < length; i++, j+= channels) {
252 out[i + offsetDst] = static_cast<T>(samples[j] * scale);
253 }
254 }
255 }
256 mValidSize = length;
257 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ setValidSize()

template<typename T , class STORAGE = HeapBuffer<T, DEFAULT_ALIGNMENT>>
void tklb::AudioBufferTpl< T, STORAGE >::setValidSize ( const Size  v)
inline

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.

Definition at line 524 of file TAudioBuffer.hpp.

524 {
525 TKLB_ASSERT(v <= size());
526 mValidSize = std::min(size(), v);
527 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ size()

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

Returns the allocated length of the buffer.

Definition at line 510 of file TAudioBuffer.hpp.

510 {
511 return mBuffer.empty() ? 0 : mBuffer.size() / mChannels;
512 }
Here is the caller graph for this function:

◆ validSize()

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

Returns the length of actually valid audio in the buffer.

TODO tklb make sure this is used consistently

Definition at line 518 of file TAudioBuffer.hpp.

518{ return mValidSize; }
Here is the caller graph for this function:

Member Data Documentation

◆ mBuffer

template<typename T , class STORAGE = HeapBuffer<T, DEFAULT_ALIGNMENT>>
Storage tklb::AudioBufferTpl< T, STORAGE >::mBuffer
private

Definition at line 51 of file TAudioBuffer.hpp.

◆ mChannels

template<typename T , class STORAGE = HeapBuffer<T, DEFAULT_ALIGNMENT>>
uchar tklb::AudioBufferTpl< T, STORAGE >::mChannels = 0
private

Definition at line 53 of file TAudioBuffer.hpp.

◆ mValidSize

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

Definition at line 52 of file TAudioBuffer.hpp.

◆ sampleRate

template<typename T , class STORAGE = HeapBuffer<T, DEFAULT_ALIGNMENT>>
ushort tklb::AudioBufferTpl< T, STORAGE >::sampleRate = 0

Only relevant for resampling and oversampling.

TODO higher sample rates wont work, maybe use enum

Definition at line 60 of file TAudioBuffer.hpp.

◆ Stride

template<typename T , class STORAGE = HeapBuffer<T, DEFAULT_ALIGNMENT>>
constexpr Size tklb::AudioBufferTpl< T, STORAGE >::Stride = xsimd::simd_type<T>::size
staticconstexpr

Definition at line 47 of file TAudioBuffer.hpp.


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