Browse Source

Clean up RealTimeConvolver

tags/v0.6.2
Andrew Belt 6 years ago
parent
commit
b264bc6af6
2 changed files with 26 additions and 28 deletions
  1. +24
    -26
      include/dsp/fir.hpp
  2. +2
    -2
      src/engine.cpp

+ 24
- 26
include/dsp/fir.hpp View File

@@ -53,18 +53,21 @@ struct RealTimeConvolver {
RealTimeConvolver(size_t blockSize) {
this->blockSize = blockSize;
pffft = pffft_new_setup(blockSize*2, PFFFT_REAL);
outputTail = new float[blockSize]();
tmpBlock = new float[blockSize*2]();
outputTail = new float[blockSize];
memset(outputTail, 0, blockSize * sizeof(float));
tmpBlock = new float[blockSize*2];
memset(tmpBlock, 0, blockSize*2 * sizeof(float));
}

~RealTimeConvolver() {
clear();
setKernel(NULL, 0);
delete[] outputTail;
delete[] tmpBlock;
pffft_destroy_setup(pffft);
}

void clear() {
void setKernel(const float *kernel, size_t length) {
// Clear existing kernel
if (kernelFfts) {
pffft_aligned_free(kernelFfts);
kernelFfts = NULL;
@@ -75,29 +78,24 @@ struct RealTimeConvolver {
}
kernelBlocks = 0;
inputPos = 0;
}

void setKernel(const float *kernel, size_t length) {
clear();

assert(kernel);
assert(length > 0);

// Round up to the nearest factor of `blockSize`
kernelBlocks = (length - 1) / blockSize + 1;

// Allocate blocks
kernelFfts = (float*) pffft_aligned_malloc(sizeof(float) * blockSize*2 * kernelBlocks);
inputFfts = (float*) pffft_aligned_malloc(sizeof(float) * blockSize*2 * kernelBlocks);
memset(inputFfts, 0, sizeof(float) * blockSize*2 * kernelBlocks);

for (size_t i = 0; i < kernelBlocks; i++) {
// Pad each block with zeros
memset(tmpBlock, 0, sizeof(float) * blockSize*2);
size_t len = min((int) blockSize, (int) (length - i*blockSize));
memcpy(tmpBlock, &kernel[i*blockSize], sizeof(float)*len);
// Compute fft
pffft_transform(pffft, tmpBlock, &kernelFfts[blockSize*2 * i], NULL, PFFFT_FORWARD);
if (kernel && length > 0) {
// Round up to the nearest factor of `blockSize`
kernelBlocks = (length - 1) / blockSize + 1;

// Allocate blocks
kernelFfts = (float*) pffft_aligned_malloc(sizeof(float) * blockSize*2 * kernelBlocks);
inputFfts = (float*) pffft_aligned_malloc(sizeof(float) * blockSize*2 * kernelBlocks);
memset(inputFfts, 0, sizeof(float) * blockSize*2 * kernelBlocks);

for (size_t i = 0; i < kernelBlocks; i++) {
// Pad each block with zeros
memset(tmpBlock, 0, sizeof(float) * blockSize*2);
size_t len = min((int) blockSize, (int) (length - i*blockSize));
memcpy(tmpBlock, &kernel[i*blockSize], sizeof(float)*len);
// Compute fft
pffft_transform(pffft, tmpBlock, &kernelFfts[blockSize*2 * i], NULL, PFFFT_FORWARD);
}
}
}



+ 2
- 2
src/engine.cpp View File

@@ -24,8 +24,8 @@ static float sampleRate = 44100.f;
static float sampleTime = 1.f / sampleRate;
static float sampleRateRequested = sampleRate;

static Module *resetModule;
static Module *randomizeModule;
static Module *resetModule = NULL;
static Module *randomizeModule = NULL;

static std::mutex mutex;
static std::thread thread;


Loading…
Cancel
Save