Skip to content

Commit

Permalink
FFT implementation cache changes
Browse files Browse the repository at this point in the history
  • Loading branch information
kupix committed Jul 23, 2024
1 parent 30d03d7 commit bca568c
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/Basic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ void Basic::analyseGrain(const float *data, std::ptrdiff_t stride)

auto log2TransformLength = input.applyAnalysisWindow(ref);

Fourier::transforms.forward(log2TransformLength, input.windowedInput, grain.transformed);
Fourier::transforms->forward(log2TransformLength, input.windowedInput, grain.transformed);

const auto n = Fourier::binCount(grain.log2TransformLength) - 1;
grain.validBinCount = std::min<int>(std::ceil(n / grain.resampleOperations.output.ratio), n) + 1;
Expand Down Expand Up @@ -112,7 +112,7 @@ void Basic::synthesiseGrain(OutputChunk &outputChunk)
else
grain.transformed.topRows(grain.validBinCount).colwise() *= t;

Fourier::transforms.inverse(grain.log2TransformLength, output.inverseTransformed, grain.transformed);
Fourier::transforms->inverse(grain.log2TransformLength, output.inverseTransformed, grain.transformed);
}

output.applySynthesisWindow(log2SynthesisHop, grains, output.synthesisWindow);
Expand Down
2 changes: 2 additions & 0 deletions src/Basic.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#pragma once

#include "Fourier.h"
#include "Grains.h"
#include "Input.h"
#include "Output.h"
Expand All @@ -11,6 +12,7 @@
namespace Bungee {

struct Basic :
Fourier::Bootstrap,
Timing
{
Input input;
Expand Down
10 changes: 8 additions & 2 deletions src/Fourier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,13 @@ void Kiss::Kernel<isInverse>::inverse(int, float *t, std::complex<float> *f) con
kiss_fftri((kiss_fftr_cfg)implementation, (kiss_fft_cpx *)f, t);
}

static Fourier::Cache<Kiss, 16> cache;
Transforms &transforms = cache;
Transforms *transforms{};

Bootstrap::Bootstrap()
{
BUNGEE_ASSERT1(!transforms);
static Fourier::Cache<Kiss, 16> cache;
BUNGEE_ASSERT1(transforms == &cache);
}

} // namespace Bungee::Fourier
12 changes: 11 additions & 1 deletion src/Fourier.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ struct Transforms
virtual void inverse(int log2TransformLength, Eigen::Ref<Eigen::ArrayXXf> t, const Eigen::Ref<const Eigen::ArrayXXcf> &f) const = 0;
};

extern Transforms &transforms;
extern Transforms *transforms;

// General case when an FFT implementation has different states for forward and reverse transforms of same size.
template <class F, class I>
Expand Down Expand Up @@ -133,6 +133,11 @@ struct Cache :

Table table;

Cache()
{
transforms = this;
}

void prepareForward(int log2Length) override
{
std::scoped_lock lock(preparationMutex);
Expand Down Expand Up @@ -172,4 +177,9 @@ struct Cache :
}
};

struct Bootstrap
{
Bootstrap();
};

} // namespace Bungee::Fourier
2 changes: 1 addition & 1 deletion src/Input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Input::Input(int log2SynthesisHop, int channelCount) :
windowedInput{(8 << log2SynthesisHop), channelCount}
{
windowedInput.setZero();
Fourier::transforms.prepareForward(log2SynthesisHop + 3);
Fourier::transforms->prepareForward(log2SynthesisHop + 3);
}

int Input::applyAnalysisWindow(const Eigen::Ref<const Eigen::ArrayXXf> &input)
Expand Down
2 changes: 1 addition & 1 deletion src/Output.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Output::Output(int log2SynthesisHop, int channelCount, int maxOutputChunkSize, f
inverseTransformed(8 << log2SynthesisHop, channelCount),
bufferResampled(maxOutputChunkSize, channelCount)
{
Fourier::transforms.prepareInverse(log2SynthesisHop + 3);
Fourier::transforms->prepareInverse(log2SynthesisHop + 3);
}

void Output::applySynthesisWindow(int log2SynthesisHop, Grains &grains, const Eigen::Ref<const Eigen::ArrayXf> &window)
Expand Down
4 changes: 2 additions & 2 deletions src/Window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ Eigen::ArrayXf fromFrequencyDomainCoefficients(int log2Size, float gain, std::in
frequencyDomain.bottomRows(frequencyDomain.rows() - i).setZero();

Eigen::ArrayXf window(1 << log2Size);
Fourier::transforms.prepareInverse(log2Size);
Fourier::transforms.inverse(log2Size, window, frequencyDomain);
Fourier::transforms->prepareInverse(log2Size);
Fourier::transforms->inverse(log2Size, window, frequencyDomain);
return window;
}

Expand Down

0 comments on commit bca568c

Please sign in to comment.