Audacity 3.2.0
FourierTransform_pffft.cpp
Go to the documentation of this file.
2
3#include "pffft.h"
4
5namespace staffpad::audio {
6
8 : _blockSize { newBlockSize }
9{
10 _pffft_scratch = (float*)pffft_aligned_malloc(_blockSize * sizeof(float));
11 realFftSpec = pffft_new_setup(_blockSize, PFFFT_REAL);
12}
13
15{
17 {
18 pffft_aligned_free(_pffft_scratch);
19 _pffft_scratch = nullptr;
20 }
21 if (realFftSpec)
22 {
23 pffft_destroy_setup(realFftSpec);
24 realFftSpec = nullptr;
25 }
26}
27
29{
30 assert(t.getNumSamples() == _blockSize);
31
32 for (auto ch = 0; ch < t.getNumChannels(); ++ch)
33 {
34 auto* spec = c.getPtr(ch); // interleaved complex numbers, size _blockSize + 2
35 auto* cpx_flt = (float*)spec;
36 pffft_transform_ordered(realFftSpec, t.getPtr(ch), cpx_flt, _pffft_scratch, PFFFT_FORWARD);
37 // pffft combines dc and nyq values into the first complex value,
38 // adjust to CCS format.
39 auto dc = cpx_flt[0];
40 auto nyq = cpx_flt[1];
41 spec[0] = {dc, 0.f};
42 spec[c.getNumSamples() - 1] = {nyq, 0.f};
43 }
44}
45
47{
48 assert(c.getNumSamples() == _blockSize / 2 + 1);
49
50 for (auto ch = 0; ch < c.getNumChannels(); ++ch)
51 {
52 auto* spec = c.getPtr(ch);
53 // Use t to convert in-place from CCS to pffft format
54 t.assignSamples(ch, (float*)spec);
55 auto* ts = t.getPtr(ch);
56 ts[0] = spec[0].real();
57 ts[1] = spec[c.getNumSamples() - 1].real();
58 pffft_transform_ordered(realFftSpec, ts, ts, _pffft_scratch, PFFFT_BACKWARD);
59 }
60}
61
62} // namespace staffpad::audio
int32_t getNumSamples() const
Definition: SamplesFloat.h:38
void assignSamples(int32_t channel, const T *input)
Definition: SamplesFloat.h:62
T * getPtr(int32_t channel)
Definition: SamplesFloat.h:48
int32_t getNumChannels() const
Definition: SamplesFloat.h:33
void forwardReal(const SamplesReal &t, SamplesComplex &c)
void inverseReal(const SamplesComplex &c, SamplesReal &t)