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

#include <TResamplerLinear.hpp>

Collaboration diagram for tklb::ResamplerLinearTpl< T, Buffer >:

Public Member Functions

 ResamplerLinearTpl (uint rateIn, uint rateOut, uint maxBlock=512, uchar channels=2, uchar quality=5)
 
 ResamplerLinearTpl ()=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 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 Buffer = AudioBufferTpl<T>>
class tklb::ResamplerLinearTpl< T, Buffer >

Definition at line 8 of file TResamplerLinear.hpp.

Member Typedef Documentation

◆ Size

template<typename T , class Buffer = AudioBufferTpl<T>>
using tklb::ResamplerLinearTpl< T, Buffer >::Size = typename Buffer::Size
private

Definition at line 12 of file TResamplerLinear.hpp.

◆ uchar

template<typename T , class Buffer = AudioBufferTpl<T>>
using tklb::ResamplerLinearTpl< T, Buffer >::uchar = unsigned char
private

Definition at line 10 of file TResamplerLinear.hpp.

◆ uint

template<typename T , class Buffer = AudioBufferTpl<T>>
using tklb::ResamplerLinearTpl< T, Buffer >::uint = unsigned int
private

Definition at line 11 of file TResamplerLinear.hpp.

Constructor & Destructor Documentation

◆ ResamplerLinearTpl() [1/2]

template<typename T , class Buffer = AudioBufferTpl<T>>
tklb::ResamplerLinearTpl< T, Buffer >::ResamplerLinearTpl ( uint  rateIn,
uint  rateOut,
uint  maxBlock = 512,
uchar  channels = 2,
uchar  quality = 5 
)
inline

Definition at line 20 of file TResamplerLinear.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:

◆ ResamplerLinearTpl() [2/2]

template<typename T , class Buffer = AudioBufferTpl<T>>
tklb::ResamplerLinearTpl< T, Buffer >::ResamplerLinearTpl ( )
default

Member Function Documentation

◆ calculateBufferSize()

template<typename T , class Buffer = AudioBufferTpl<T>>
Size tklb::ResamplerLinearTpl< T, Buffer >::calculateBufferSize ( Size  in)
inline

Calculate a buffersize fit for the resampled result.

Also adds a bit of padding.

Definition at line 115 of file TResamplerLinear.hpp.

115 {
116 return estimateOut(in) + 10;
117 }
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 , class Buffer = AudioBufferTpl<T>>
Size tklb::ResamplerLinearTpl< T, Buffer >::estimateNeed ( const Size  out) const
inline

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

Definition at line 98 of file TResamplerLinear.hpp.

98 {
99 return std::round(out * mFactor);
100 }

◆ estimateOut()

template<typename T , class Buffer = AudioBufferTpl<T>>
Size tklb::ResamplerLinearTpl< T, Buffer >::estimateOut ( const Size  in) const
inline

Definition at line 102 of file TResamplerLinear.hpp.

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

◆ getLatency()

template<typename T , class Buffer = AudioBufferTpl<T>>
int tklb::ResamplerLinearTpl< T, Buffer >::getLatency ( ) const
inline

Get the latency in samples.

Definition at line 91 of file TResamplerLinear.hpp.

91 {
92 return 1; // lerp wil be one sample behind
93 };

◆ init()

template<typename T , class Buffer = AudioBufferTpl<T>>
bool tklb::ResamplerLinearTpl< T, Buffer >::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 TResamplerLinear.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 , class Buffer = AudioBufferTpl<T>>
bool tklb::ResamplerLinearTpl< T, Buffer >::isInitialized ( ) const
inline

Definition at line 107 of file TResamplerLinear.hpp.

107 {
108 return true;
109 };

◆ process()

template<typename T , class Buffer = AudioBufferTpl<T>>
Size tklb::ResamplerLinearTpl< T, Buffer >::process ( const Buffer &  in,
Buffer &  out 
)
inline

Resample function Make sure the out buffer has enough space.

Definition at line 49 of file TResamplerLinear.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 mix = position - lastPosition; // mix factor between first and second sample
73 const T next = in[c][lastIndex];
74 out[c][output] = last + mix * (next - last);
75 // out[c][o] = next * mix + last * (T(1.0) - mix);
76 last = next; // TODO tkbl this seems like bullshit
77 // ! fix this mess since this gets carried over after one sample and no interpolation happens ?
78 }
79 mLastFrame[c] = last;
80 lastMix = mix;
81 countOut = output;
82 }
83 // mOffset = lastMix;
84 out.setValidSize(countOut);
85 return countOut;
86 }
typename Buffer::Size Size
Here is the call graph for this function:
Here is the caller graph for this function:

◆ resample()

template<typename T , class Buffer = AudioBufferTpl<T>>
static void tklb::ResamplerLinearTpl< T, Buffer >::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 126 of file TResamplerLinear.hpp.

126 {
127 // TODO tklb compensate delay
128 const uint rateIn = buffer.sampleRate;
129 const Size samples = buffer.size();
130 TKLB_ASSERT(rateIn > 0)
131 // Make a copy, this could be skipped when a conversion to float is needed anyways
132 Buffer copy;
133 copy.resize(buffer);
134 copy.set(buffer);
135 copy.sampleRate = rateIn;
136 copy.setValidSize(samples);
137
138 ResamplerLinearTpl<T, Buffer> resampler;
139 resampler.init(rateIn, rateOut, copy.size(), buffer.channels(), quality);
140 buffer.resize(resampler.calculateBufferSize(samples));
141
142 resampler.process(copy, buffer);
143 }
Size process(const Buffer &in, Buffer &out)
Resample function Make sure the out buffer has enough space.
Size calculateBufferSize(Size in)
Calculate a buffersize fit for the resampled result.
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 , class Buffer = AudioBufferTpl<T>>
constexpr Size tklb::ResamplerLinearTpl< T, Buffer >::MAX_CHANNELS = 32
staticconstexprprivate

Definition at line 14 of file TResamplerLinear.hpp.

◆ mFactor

template<typename T , class Buffer = AudioBufferTpl<T>>
double tklb::ResamplerLinearTpl< T, Buffer >::mFactor = 1.0
private

Definition at line 16 of file TResamplerLinear.hpp.

◆ mLastFrame

template<typename T , class Buffer = AudioBufferTpl<T>>
T tklb::ResamplerLinearTpl< T, Buffer >::mLastFrame[MAX_CHANNELS]
private

Definition at line 18 of file TResamplerLinear.hpp.

◆ mOffset

template<typename T , class Buffer = AudioBufferTpl<T>>
T tklb::ResamplerLinearTpl< T, Buffer >::mOffset = 0
private

Definition at line 17 of file TResamplerLinear.hpp.

◆ mRateIn

template<typename T , class Buffer = AudioBufferTpl<T>>
uint tklb::ResamplerLinearTpl< T, Buffer >::mRateIn
private

Definition at line 15 of file TResamplerLinear.hpp.

◆ mRateOut

template<typename T , class Buffer = AudioBufferTpl<T>>
uint tklb::ResamplerLinearTpl< T, Buffer >::mRateOut
private

Definition at line 15 of file TResamplerLinear.hpp.


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