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

#include <TResamplerHold.hpp>

Collaboration diagram for tklb::ResamplerHoldTpl< T >:

Public Member Functions

 ResamplerHoldTpl (uint rateIn, uint rateOut, uint maxBlock=512, uchar quality=5)
 
bool init (uint rateIn, uint rateOut, uint maxBlock=512, uchar quality=5)
 setup the resampler More...
 
Size process (const Buffer &in, Buffer &out)
 Resample function Make sure the out buffer has enough space. More...
 
int getLatency () const
 Get the latency in samples. More...
 
Size estimateNeed (const Size out) const
 Estimate how many samples need to be put in to get n samples out. More...
 
Size estimateOut (const Size in) const
 Estimate how many sample will be emitted in the next step. More...
 
bool isInitialized () const
 
Size calculateBufferSize (Size initialSize)
 Calculate a buffersize fit for the resampled result. More...
 

Static Public Member Functions

static void resample (Buffer &buffer, const uint rateOut, const uchar quality=5)
 Resamples the provided buffer from its sampleRate to the target rate. More...
 

Private Types

using uchar = unsigned char
 
using uint = unsigned int
 
using Buffer = AudioBufferTpl< T >
 
using Size = typename Buffer::Size
 

Private Attributes

uint mRateIn
 
uint mRateOut
 
double mFactor = 1.0
 

Detailed Description

template<typename T>
class tklb::ResamplerHoldTpl< T >

Definition at line 8 of file TResamplerHold.hpp.

Member Typedef Documentation

◆ Buffer

template<typename T >
using tklb::ResamplerHoldTpl< T >::Buffer = AudioBufferTpl<T>
private

Definition at line 11 of file TResamplerHold.hpp.

◆ Size

template<typename T >
using tklb::ResamplerHoldTpl< T >::Size = typename Buffer::Size
private

Definition at line 12 of file TResamplerHold.hpp.

◆ uchar

template<typename T >
using tklb::ResamplerHoldTpl< T >::uchar = unsigned char
private

Definition at line 9 of file TResamplerHold.hpp.

◆ uint

template<typename T >
using tklb::ResamplerHoldTpl< T >::uint = unsigned int
private

Definition at line 10 of file TResamplerHold.hpp.

Constructor & Destructor Documentation

◆ ResamplerHoldTpl()

template<typename T >
tklb::ResamplerHoldTpl< T >::ResamplerHoldTpl ( uint  rateIn,
uint  rateOut,
uint  maxBlock = 512,
uchar  quality = 5 
)
inline

Definition at line 17 of file TResamplerHold.hpp.

17 {
18 init(rateIn, rateOut, maxBlock, quality);
19 }
bool init(uint rateIn, uint rateOut, uint maxBlock=512, uchar quality=5)
setup the resampler
Here is the call graph for this function:

Member Function Documentation

◆ calculateBufferSize()

template<typename T >
Size tklb::ResamplerHoldTpl< T >::calculateBufferSize ( Size  initialSize)
inline

Calculate a buffersize fit for the resampled result.

Also adds a bit of padding.

Definition at line 86 of file TResamplerHold.hpp.

86 {
87 return estimateOut(initialSize) + 10;
88 }
Size estimateOut(const Size in) const
Estimate how many sample will be emitted in the next step.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ estimateNeed()

template<typename T >
Size tklb::ResamplerHoldTpl< T >::estimateNeed ( const Size  out) const
inline

Estimate how many samples need to be put in to get n samples out.

Definition at line 67 of file TResamplerHold.hpp.

67 {
68 return std::round(out * mFactor);
69 }

◆ estimateOut()

template<typename T >
Size tklb::ResamplerHoldTpl< T >::estimateOut ( const Size  in) const
inline

Estimate how many sample will be emitted in the next step.

Definition at line 74 of file TResamplerHold.hpp.

74 {
75 return std::round(in * (double(mRateOut) / double(mRateIn)));
76 }
Here is the caller graph for this function:

◆ getLatency()

template<typename T >
int tklb::ResamplerHoldTpl< T >::getLatency ( ) const
inline

Get the latency in samples.

Definition at line 62 of file TResamplerHold.hpp.

62{ return 0; };

◆ init()

template<typename T >
bool tklb::ResamplerHoldTpl< T >::init ( uint  rateIn,
uint  rateOut,
uint  maxBlock = 512,
uchar  quality = 5 
)
inline

setup the resampler

Parameters
rateInInput sample rate
rateOutDesired output samplerate
maxBlockThe maximum blocksize beeing passed into process(). Only relevant when doing non float resampling to allocate space for the conversion buffers
qualityQuality factor from 1-10. Higher results in better quality and higher CPU usage. Depending on implementataion may not do anything.
Returns
True on success

Definition at line 31 of file TResamplerHold.hpp.

31 {
32 mRateIn = rateIn;
33 mRateOut = rateOut;
34 mFactor = double(mRateIn) / double(mRateOut);
35 return true;
36 }
Here is the caller graph for this function:

◆ isInitialized()

template<typename T >
bool tklb::ResamplerHoldTpl< T >::isInitialized ( ) const
inline

Definition at line 78 of file TResamplerHold.hpp.

78 {
79 return true;
80 };

◆ process()

template<typename T >
Size tklb::ResamplerHoldTpl< T >::process ( const Buffer in,
Buffer out 
)
inline

Resample function Make sure the out buffer has enough space.

Definition at line 42 of file TResamplerHold.hpp.

42 {
43 const Size countIn = in.validSize();
44 Size countOut = 0;
45
46 for (int c = 0; c < in.channels(); c++) {
47 Size output = 0; // index in output buffer
48 for (; output < out.size(); output++) {
49 const Size index = std::round(output * mFactor); // closest sample
50 if (countIn <= index) { break; }
51 out[c][output] = in[c][index];
52 }
53 countOut = output;
54 }
55 out.setValidSize(countOut);
56 return countOut;
57 }
typename Buffer::Size Size
Here is the caller graph for this function:

◆ resample()

template<typename T >
static void tklb::ResamplerHoldTpl< T >::resample ( Buffer buffer,
const uint  rateOut,
const uchar  quality = 5 
)
inlinestatic

Resamples the provided buffer from its sampleRate to the target rate.

Parameters
bufferAudiobuffer to resample, set the rate of the buffer object
rateOutDesired output samplerate in Hz
qualityQuality from 1-10

Definition at line 97 of file TResamplerHold.hpp.

97 {
98 // TODO tklb compensate delay
99 const uint rateIn = buffer.sampleRate;
100 const Size samples = buffer.size();
101 TKLB_ASSERT(rateIn > 0)
102 // Make a copy, this could be skipped when a conversion to float is needed anyways
103 Buffer copy;
104 copy.resize(buffer);
105 copy.set(buffer);
106 copy.sampleRate = rateIn;
107 copy.setValidSize(samples);
108
109 ResamplerHoldTpl<T> resampler;
110 resampler.init(rateIn, rateOut, copy.size(), quality);
111 buffer.resize(resampler.calculateBufferSize(samples));
112
113 resampler.process(copy, buffer);
114 }
#define TKLB_ASSERT(condition)
Wrap assertions.
Definition: TAssert.h:18
ResamplerHoldTpl(uint rateIn, uint rateOut, uint maxBlock=512, uchar quality=5)
Size calculateBufferSize(Size initialSize)
Calculate a buffersize fit for the resampled result.
AudioBufferTpl< T > Buffer
Size process(const Buffer &in, Buffer &out)
Resample function Make sure the out buffer has enough space.
static void set(void *dst, const unsigned char val, size_t size)
memset wrapper
Definition: TMemoryUtil.hpp:40
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:

Member Data Documentation

◆ mFactor

template<typename T >
double tklb::ResamplerHoldTpl< T >::mFactor = 1.0
private

Definition at line 14 of file TResamplerHold.hpp.

◆ mRateIn

template<typename T >
uint tklb::ResamplerHoldTpl< T >::mRateIn
private

Definition at line 13 of file TResamplerHold.hpp.

◆ mRateOut

template<typename T >
uint tklb::ResamplerHoldTpl< T >::mRateOut
private

Definition at line 13 of file TResamplerHold.hpp.


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