-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
convolve output with sampled IR of Tesla Coil + reverb
- Loading branch information
1 parent
3af6c20
commit ee485c3
Showing
10 changed files
with
179 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
#include "Convolution.h" | ||
|
||
#include <cstring> | ||
#include <cmath> | ||
|
||
Convolution::Convolution(const float *ir, size_t irLen, size_t bufLen): ir(ir), irLen(irLen), bufLen(bufLen), scale(1.0f/sqrtf(irLen)) { | ||
inputBuffer = (float*)fftwf_malloc(irLen * sizeof(float)); | ||
memset(inputBuffer, 0, irLen * sizeof(float)); | ||
inputBufferInd = 0; | ||
|
||
intermediate = (fftwf_complex*)fftwf_malloc((irLen/2+1) * sizeof(fftwf_complex)); | ||
|
||
outputBuffers.resize(irLen/bufLen); | ||
for(float *&buffer:outputBuffers) { | ||
buffer = (float*)fftwf_malloc(irLen * sizeof(float)); | ||
memset(buffer, 0, irLen * sizeof(float)); | ||
} | ||
outputBufferInd = 0; | ||
|
||
finalBuffer = (float*)fftwf_malloc(bufLen * sizeof(float)); | ||
|
||
forwardPlan = fftwf_plan_dft_r2c_1d(irLen, inputBuffer, intermediate, 0); | ||
reversePlan = fftwf_plan_dft_c2r_1d(irLen, intermediate, outputBuffers.at(0), 0); | ||
} | ||
|
||
Convolution::~Convolution() { | ||
fftwf_free(inputBuffer); | ||
fftwf_free(intermediate); | ||
for(float *buffer:outputBuffers) | ||
fftwf_free(buffer); | ||
fftwf_free(finalBuffer); | ||
fftwf_destroy_plan(forwardPlan); | ||
fftwf_destroy_plan(reversePlan); | ||
} | ||
|
||
void Convolution::feedSample(float sample) { | ||
if(inputBufferInd >= bufLen) | ||
return; | ||
|
||
// Pre-scale input to keep energy normalized | ||
inputBuffer[inputBufferInd++] = sample * scale; | ||
} | ||
|
||
const float *Convolution::getOutput() { | ||
inputBufferInd = 0; | ||
|
||
// Compute FFT of incoming data | ||
fftwf_execute(forwardPlan); | ||
|
||
// Apply impulse response | ||
for(size_t i = 0; i < irLen/2+1; i++) { | ||
const float * const a = ir + 2*i; | ||
const fftwf_complex &b = intermediate[i]; | ||
fftwf_complex result; | ||
result[0] = a[0]*b[0] - a[1]*b[1]; | ||
result[1] = a[0]*b[1] + a[1]*b[0]; | ||
intermediate[i][0] = result[0]; | ||
intermediate[i][1] = result[1]; | ||
} | ||
|
||
// Compute inverse FFT | ||
fftwf_execute_dft_c2r(reversePlan, intermediate, outputBuffers[outputBufferInd]); | ||
|
||
// Compute output | ||
memset(finalBuffer, 0, bufLen * sizeof(float)); | ||
ssize_t ind = outputBufferInd; | ||
for(size_t i = 0; i < outputBuffers.size(); i++) { | ||
for(size_t j = 0; j < bufLen; j++) | ||
finalBuffer[j] += outputBuffers[ind][j + i*bufLen]; | ||
|
||
ind--; | ||
if(ind < 0) | ||
ind = outputBuffers.size()-1; | ||
} | ||
|
||
// Rotate output buffers | ||
outputBufferInd++; | ||
if(outputBufferInd >= outputBuffers.size()) | ||
outputBufferInd = 0; | ||
|
||
return finalBuffer; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#pragma once | ||
|
||
#include <vector> | ||
#include <stddef.h> | ||
|
||
#include <fftw3.h> | ||
|
||
class Convolution { | ||
public: | ||
Convolution(const float *ir, size_t irLen, size_t bufLen); | ||
~Convolution(); | ||
|
||
// Feed an input sample; must call bufferLen times between each call to getOutput | ||
void feedSample(float sample); | ||
|
||
// Get output buffer | ||
const float *getOutput(); | ||
|
||
protected: | ||
const float * const ir; | ||
const size_t irLen, bufLen; | ||
|
||
const float scale; | ||
|
||
// Contiguous input data | ||
float *inputBuffer; | ||
size_t inputBufferInd; | ||
|
||
// Intermediate FFT results for convolving | ||
fftwf_complex *intermediate; | ||
|
||
// Output buffers to be summed to create continuous impulse response | ||
std::vector<float*> outputBuffers; | ||
size_t outputBufferInd; | ||
|
||
// Final summed output buffer | ||
float *finalBuffer; | ||
|
||
fftwf_plan forwardPlan, reversePlan; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
.global ir | ||
.global _ir | ||
ir: | ||
_ir: | ||
.incbin "ir.bin" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
#pragma once | ||
|
||
#define VERSION "v1.1" | ||
#define VERSION "v1.2" |