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

Single stage mono convolver based on HiFi-Lofis convolver The FFT and buffers use the tklb types and the simd was replaced with xsimd TODO tklb all ! FIX THIS MESS ! FIX THIS MESS ! FIX THIS MESS ! FIX THIS MESS. More...

#include <TConvolverFFT.hpp>

Collaboration diagram for tklb::ConvolverMonoTpl< T >:

Public Types

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

Public Member Functions

 ConvolverMonoTpl ()=default
 
template<typename T2 >
void load (const AudioBufferTpl< T2 > &buffer, const uint blockSize, const uchar channel)
 Load a impulse response and prepare the convolution. More...
 
template<typename T2 >
void process (const AudioBufferTpl< T2 > &inBuf, AudioBufferTpl< T2 > &outBuf, const uchar channel)
 Do the convolution. More...
 

Private Types

using Segments = HeapBuffer< Buffer >
 

Static Private Member Functions

static void complexMultiply (Buffer &bufferOut, const Buffer &bufferA, const Buffer &bufferB)
 

Private Attributes

Size mBlockSize
 
Size mSegmentSize
 
Size mSegmentCount
 
Size mFFTComplexSize
 
Size mInputBufferFill
 
Size mCurrentPosition
 
Buffer mComplexIr
 
Buffer mFFTBuffer
 
Segments mSegmentsIR
 
Segments mSegments
 
FFT mFFT
 
Buffer mPremultipliedBuffer
 
Buffer mInputBuffer
 
Buffer mConvolutionBuffer
 
Buffer mOverlapBuffer
 

Detailed Description

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

Single stage mono convolver based on HiFi-Lofis convolver The FFT and buffers use the tklb types and the simd was replaced with xsimd TODO tklb all ! FIX THIS MESS ! FIX THIS MESS ! FIX THIS MESS ! FIX THIS MESS.

Definition at line 27 of file TConvolverFFT.hpp.

Member Typedef Documentation

◆ Buffer

template<typename T >
using tklb::ConvolverMonoTpl< T >::Buffer = AudioBufferTpl<T>

Definition at line 31 of file TConvolverFFT.hpp.

◆ Segments

template<typename T >
using tklb::ConvolverMonoTpl< T >::Segments = HeapBuffer<Buffer>
private

Definition at line 44 of file TConvolverFFT.hpp.

◆ Size

template<typename T >
using tklb::ConvolverMonoTpl< T >::Size = typename Buffer::Size

Definition at line 32 of file TConvolverFFT.hpp.

◆ uchar

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

Definition at line 29 of file TConvolverFFT.hpp.

◆ uint

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

Definition at line 30 of file TConvolverFFT.hpp.

Constructor & Destructor Documentation

◆ ConvolverMonoTpl()

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

Member Function Documentation

◆ complexMultiply()

template<typename T >
static void tklb::ConvolverMonoTpl< T >::complexMultiply ( Buffer bufferOut,
const Buffer bufferA,
const Buffer bufferB 
)
inlinestaticprivate

Definition at line 170 of file TConvolverFFT.hpp.

172 {
173 const uint size = bufferOut.size();
174 const T* aReal = bufferA[0];
175 const T* aImag = bufferA[1];
176 const T* bReal = bufferB[0];
177 const T* bImag = bufferB[1];
178 T* outReal = bufferOut[0];
179 T* outImag = bufferOut[1];
180 #ifdef TKLB_NO_SIMD
181 for (uint i = 0; i < size; i++) {
182 outReal[i] += aReal[i] * bReal[i] - aImag[i] * bImag[i];
183 outImag[i] += aReal[i] * bImag[i] + aImag[i] * bReal[i];
184 }
185 #else
186 const uint vectorize = size - size % Buffer::stride;
187 for(uint i = 0; i < vectorize; i += Buffer::stride) {
188 xsimd::simd_type<T> aR = xsimd::load_aligned(aReal + i);
189 xsimd::simd_type<T> bR = xsimd::load_aligned(bReal + i);
190 xsimd::simd_type<T> aI = xsimd::load_aligned(aImag + i);
191 xsimd::simd_type<T> bI = xsimd::load_aligned(bImag + i);
192 xsimd::store_aligned(outReal + i, (aR * bR) - (aI * bI));
193 xsimd::store_aligned(outImag + i, (aR * bI) + (aI * bR));
194 }
195 for(uint i = vectorize; i < size; i++) {
196 outReal[i] += aReal[i] * bReal[i] - aImag[i] * bImag[i];
197 outImag[i] += aReal[i] * bImag[i] + aImag[i] * bReal[i];
198 }
199 #endif
200 }
Here is the caller graph for this function:

◆ load()

template<typename T >
template<typename T2 >
void tklb::ConvolverMonoTpl< T >::load ( const AudioBufferTpl< T2 > &  buffer,
const uint  blockSize,
const uchar  channel 
)
inline

Load a impulse response and prepare the convolution.

Parameters
bufferThe ir buffer. Only the first channel is used
blockSizeSize of blocks ir will be divided in

Definition at line 63 of file TConvolverFFT.hpp.

63 {
64 uint irLength = buffer.validSize();
65 const T2* ir = buffer[channel];
66 // trim silence, since longer IRs increase CPU usage considerably
67 const T2 silence = 0.000001;
68 while (irLength && silence > fabs(ir[irLength])) { irLength--; }
69 if (irLength == 0) { return; }
70 irLength++; // index to length
71
72 // Figure out how many segments a block is
73 mBlockSize = powerOf2(blockSize);
75 mSegmentCount = uint(std::ceil(irLength / double(mBlockSize)));
77
78 // create segment buffers for the input signal
81
82 // create segment buffers for the impulse response
84 mSegments.construct();
86 mSegmentsIR.construct();
87
88 for (uint i = 0; i < mSegmentCount; i++) {
89 const uint remaining = std::min(irLength - (i * mBlockSize), mBlockSize);
90 mFFTBuffer.set(0);
91 // Put the segment in the fft buffer, might do type conversion
92 mFFTBuffer.set(ir + (i * mBlockSize), remaining);
93 mSegmentsIR[i].resize(mFFTComplexSize, 2); // make space in the result buffer
94 mSegments[i].resize(mFFTComplexSize, 2); // and the input signal buffer
95 mFFT.forward(mFFTBuffer, mSegmentsIR[i]); // save the fft result for each segment
96 }
97
101 mInputBuffer.resize(mBlockSize);
102
105 }
void forward(const AudioBufferTpl< T > &input, AudioBufferTpl< T > &result)
timedomain to frequency domain
Definition: Tpffft.hpp:56
void resize(uint size)
Definition: Tpffft.hpp:40
bool resize(const Size size, const bool downsize=true)
Resize the buffer If the memory is borrowed it will become unique and owned by this instance as soon ...
T min(const T &v1, const T &v2)
Definition: TMath.hpp:16
T powerOf2(const T &&v)
Definition: TMath.hpp:9
Here is the call graph for this function:

◆ process()

template<typename T >
template<typename T2 >
void tklb::ConvolverMonoTpl< T >::process ( const AudioBufferTpl< T2 > &  inBuf,
AudioBufferTpl< T2 > &  outBuf,
const uchar  channel 
)
inline

Do the convolution.

Definition at line 111 of file TConvolverFFT.hpp.

111 {
112 if (mSegmentCount == 0) {
113 outBuf.set(inBuf);
114 return;
115 }
116
117 const uint length = inBuf.validSize();
118 const T2* in = inBuf[channel];
119 T2* out = outBuf[channel];
120
121 uint processed = 0;
122 uint iterations = 0;
123 while (processed < length) {
124 const bool inputBufferWasEmpty = (mInputBufferFill == 0);
125 const uint processing = std::min(length - processed, mBlockSize - mInputBufferFill);
126 const uint inputBufferPos = mInputBufferFill;
127
128 mInputBuffer.set(in + processed, processing, 0, inputBufferPos);
129
131 mFFTBuffer.set(0, 0, mBlockSize); // pad the rest with 0
133
134 if (inputBufferWasEmpty) {
136 for (uint i = 1; i < mSegmentCount; i++) {
137 const size_t indexIr = i;
138 const size_t indexAudio = (mCurrentPosition + i) % mSegmentCount;
140 }
141 }
142
145
147
148 // outBuf.set(mFFTBuffer[0] + inputBufferPos, processing, 0, processed);
149 // outBuf.add(mOverlapBuffer, processing, processed, inputBufferPos);
150 for (int i = 0; i < processing; i++) {
151 out[i + processed] =
152 (mFFTBuffer[0][i + inputBufferPos] +
153 mOverlapBuffer[0][i + inputBufferPos]);
154 }
155
156 mInputBufferFill += processing;
158 mInputBuffer.set(0);
162 }
163
164 processed += processing;
165 iterations++;
166 }
167 }
static void complexMultiply(Buffer &bufferOut, const Buffer &bufferA, const Buffer &bufferB)
void back(const AudioBufferTpl< T > &input, AudioBufferTpl< T > &result)
Frequency domain back to time domain.
Definition: Tpffft.hpp:89
Here is the call graph for this function:

Member Data Documentation

◆ mBlockSize

template<typename T >
Size tklb::ConvolverMonoTpl< T >::mBlockSize
private

Definition at line 35 of file TConvolverFFT.hpp.

◆ mComplexIr

template<typename T >
Buffer tklb::ConvolverMonoTpl< T >::mComplexIr
private

Definition at line 41 of file TConvolverFFT.hpp.

◆ mConvolutionBuffer

template<typename T >
Buffer tklb::ConvolverMonoTpl< T >::mConvolutionBuffer
private

Definition at line 50 of file TConvolverFFT.hpp.

◆ mCurrentPosition

template<typename T >
Size tklb::ConvolverMonoTpl< T >::mCurrentPosition
private

Definition at line 40 of file TConvolverFFT.hpp.

◆ mFFT

template<typename T >
FFT tklb::ConvolverMonoTpl< T >::mFFT
private

Definition at line 47 of file TConvolverFFT.hpp.

◆ mFFTBuffer

template<typename T >
Buffer tklb::ConvolverMonoTpl< T >::mFFTBuffer
private

Definition at line 42 of file TConvolverFFT.hpp.

◆ mFFTComplexSize

template<typename T >
Size tklb::ConvolverMonoTpl< T >::mFFTComplexSize
private

Definition at line 38 of file TConvolverFFT.hpp.

◆ mInputBuffer

template<typename T >
Buffer tklb::ConvolverMonoTpl< T >::mInputBuffer
private

Definition at line 49 of file TConvolverFFT.hpp.

◆ mInputBufferFill

template<typename T >
Size tklb::ConvolverMonoTpl< T >::mInputBufferFill
private

Definition at line 39 of file TConvolverFFT.hpp.

◆ mOverlapBuffer

template<typename T >
Buffer tklb::ConvolverMonoTpl< T >::mOverlapBuffer
private

Definition at line 51 of file TConvolverFFT.hpp.

◆ mPremultipliedBuffer

template<typename T >
Buffer tklb::ConvolverMonoTpl< T >::mPremultipliedBuffer
private

Definition at line 48 of file TConvolverFFT.hpp.

◆ mSegmentCount

template<typename T >
Size tklb::ConvolverMonoTpl< T >::mSegmentCount
private

Definition at line 37 of file TConvolverFFT.hpp.

◆ mSegments

template<typename T >
Segments tklb::ConvolverMonoTpl< T >::mSegments
private

Definition at line 46 of file TConvolverFFT.hpp.

◆ mSegmentsIR

template<typename T >
Segments tklb::ConvolverMonoTpl< T >::mSegmentsIR
private

Definition at line 45 of file TConvolverFFT.hpp.

◆ mSegmentSize

template<typename T >
Size tklb::ConvolverMonoTpl< T >::mSegmentSize
private

Definition at line 36 of file TConvolverFFT.hpp.


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