VAE - Virtual Audio Engine 1
Small Data Driven Audio Engine
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
tklb::ResamplerCosineTpl< T > Class Template Reference

#include <TResamplerCosine.hpp>

Collaboration diagram for tklb::ResamplerCosineTpl< T >:

Public Member Functions

 ResamplerCosineTpl (uint rateIn, uint rateOut, uint maxBlock=512, uchar quality=5)
 
 ResamplerCosineTpl ()=default
 
bool init (uint rateIn, uint rateOut, uint maxBlock=512, uchar channels=2, 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
 
bool isInitialized () const
 
Size calculateBufferSize (Size in)
 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
 
mOffset = 0
 
mLastFrame [MAX_CHANNELS]
 

Static Private Attributes

static constexpr Size MAX_CHANNELS = 32
 

Detailed Description

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

Definition at line 8 of file TResamplerCosine.hpp.

Member Typedef Documentation

◆ Buffer

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

Definition at line 11 of file TResamplerCosine.hpp.

◆ Size

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

Definition at line 12 of file TResamplerCosine.hpp.

◆ uchar

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

Definition at line 9 of file TResamplerCosine.hpp.

◆ uint

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

Definition at line 10 of file TResamplerCosine.hpp.

Constructor & Destructor Documentation

◆ ResamplerCosineTpl() [1/2]

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

Definition at line 20 of file TResamplerCosine.hpp.

20 {
21 for (auto& i : mLastFrame) { i = 0.0; }
22 init(rateIn, rateOut, maxBlock, quality);
23 }
bool init(uint rateIn, uint rateOut, uint maxBlock=512, uchar channels=2, uchar quality=5)
setup the resampler
Here is the call graph for this function:

◆ ResamplerCosineTpl() [2/2]

template<typename T >
tklb::ResamplerCosineTpl< T >::ResamplerCosineTpl ( )
default

Member Function Documentation

◆ calculateBufferSize()

template<typename T >
Size tklb::ResamplerCosineTpl< T >::calculateBufferSize ( Size  in)
inline

Calculate a buffersize fit for the resampled result.

Also adds a bit of padding.

Definition at line 113 of file TResamplerCosine.hpp.

113 {
114 return estimateOut(in) + 10;
115 }
Size estimateOut(const Size in) const
Here is the call graph for this function:
Here is the caller graph for this function:

◆ estimateNeed()

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

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

Definition at line 96 of file TResamplerCosine.hpp.

96 {
97 return std::round(out * mFactor);
98 }

◆ estimateOut()

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

Definition at line 100 of file TResamplerCosine.hpp.

100 {
101 return std::round(in * (double(mRateOut) / double(mRateIn)));
102 }
Here is the caller graph for this function:

◆ getLatency()

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

Get the latency in samples.

Definition at line 89 of file TResamplerCosine.hpp.

89 {
90 return 1; // lerp wil be one sample behind
91 };

◆ init()

template<typename T >
bool tklb::ResamplerCosineTpl< T >::init ( uint  rateIn,
uint  rateOut,
uint  maxBlock = 512,
uchar  channels = 2,
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 37 of file TResamplerCosine.hpp.

37 {
38 TKLB_ASSERT(channels <= MAX_CHANNELS)
39 mRateIn = rateIn;
40 mRateOut = rateOut;
41 mFactor = double(mRateIn) / double(mRateOut);
42 return true;
43 }
#define TKLB_ASSERT(condition)
Wrap assertions.
Definition: TAssert.h:18
static constexpr Size MAX_CHANNELS
Here is the caller graph for this function:

◆ isInitialized()

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

Definition at line 105 of file TResamplerCosine.hpp.

105 {
106 return true;
107 };

◆ process()

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

Resample function Make sure the out buffer has enough space.

Definition at line 49 of file TResamplerCosine.hpp.

49 {
50 TKLB_ASSERT(in.sampleRate == mRateIn);
51 TKLB_ASSERT(out.sampleRate == mRateOut);
52 TKLB_ASSERT(in.validSize() > 0)
53 TKLB_ASSERT(estimateOut(in.validSize()) <= out.size())
54
55 const Size countIn = in.validSize();
56 Size countOut = 0;
57 const T offset = mOffset;
58 T lastMix = 0;
59
60 for (int c = 0; c < in.channels(); c++) {
61 Size output = 0; // index in output buffer
62 T last = mLastFrame[c]; // last sample
63 T mix = 0.0;
64 for (; output < out.size(); output++) {
65 const T position = output * mFactor; // index in input buffer, somewhere between two samples
66 const T lastPosition = std::floor(position); // next sample index in the input buffer
67 const Size lastIndex = lastPosition;
68 const Size nextIndex = lastPosition + 1; // next sample index in the input buffer this is the one we need to fetch
69
70 if (countIn <= nextIndex) { break; }
71
72 const T next = in[c][lastIndex];
73 T mix = 0.5 * (1.0 - cos((position - lastPosition) * 3.1416));
74 out[c][output] = last + mix * (next - last);
75 last = next;
76 }
77 mLastFrame[c] = last;
78 lastMix = mix;
79 countOut = output;
80 }
81 // mOffset = lastMix;
82 out.setValidSize(countOut);
83 return countOut;
84 }
typename Buffer::Size Size
Here is the call graph for this function:
Here is the caller graph for this function:

◆ resample()

template<typename T >
static void tklb::ResamplerCosineTpl< 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 124 of file TResamplerCosine.hpp.

124 {
125 // TODO tklb compensate delay
126 const uint rateIn = buffer.sampleRate;
127 const Size samples = buffer.size();
128 TKLB_ASSERT(rateIn > 0)
129 // Make a copy, this could be skipped when a conversion to float is needed anyways
130 Buffer copy;
131 copy.resize(buffer);
132 copy.set(buffer);
133 copy.sampleRate = rateIn;
134 copy.setValidSize(samples);
135
136 ResamplerCosineTpl<T> resampler;
137 resampler.init(rateIn, rateOut, copy.size(), quality);
138 buffer.resize(resampler.calculateBufferSize(samples));
139
140 resampler.process(copy, buffer);
141 }
AudioBufferTpl< T > Buffer
Size calculateBufferSize(Size in)
Calculate a buffersize fit for the resampled result.
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

◆ MAX_CHANNELS

template<typename T >
constexpr Size tklb::ResamplerCosineTpl< T >::MAX_CHANNELS = 32
staticconstexprprivate

Definition at line 17 of file TResamplerCosine.hpp.

◆ mFactor

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

Definition at line 15 of file TResamplerCosine.hpp.

◆ mLastFrame

template<typename T >
T tklb::ResamplerCosineTpl< T >::mLastFrame[MAX_CHANNELS]
private

Definition at line 18 of file TResamplerCosine.hpp.

◆ mOffset

template<typename T >
T tklb::ResamplerCosineTpl< T >::mOffset = 0
private

Definition at line 16 of file TResamplerCosine.hpp.

◆ mRateIn

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

Definition at line 14 of file TResamplerCosine.hpp.

◆ mRateOut

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

Definition at line 14 of file TResamplerCosine.hpp.


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