NAP
fftbuffer.h
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4 
5 #pragma once
6 
7 // External Includes
8 #include <nap/resource.h>
9 #include <nap/numeric.h>
10 #include <complex>
11 #include <mutex>
12 #include <atomic>
13 
14 namespace nap
15 {
19  class NAPAPI FFTBuffer final
20  {
21  RTTI_ENABLE()
22  public:
23  using AmplitudeSpectrum = std::vector<float>;
24  using PhaseSpectrum = std::vector<float>;
25 
29  enum class EOverlap : uint
30  {
31  One = 1,
32  Three = 3,
33  Five = 5,
34  Seven = 7,
35  Nine = 9,
36  Eleven = 11
37  };
38 
43  FFTBuffer(uint dataSize, EOverlap overlap = EOverlap::One);
44 
45  // Destructor
46  ~FFTBuffer();
47 
51  void supply(const std::vector<float>& samples);
52 
56  uint getBinCount() const { return mBinCount; }
57 
61  uint getDataSize();
62 
66  const AmplitudeSpectrum& getAmplitudeSpectrum() { transform(); return mAmplitude; }
67 
71  const PhaseSpectrum& getPhaseSpectrum() { transform(); return mPhase; }
72 
73  private:
74 
75  void transform(); //< Transform sample data into FFT image (thread safe)
76  void createImage(); //< Construct FFT image
77 
78  class KissContext;
79  std::unique_ptr<KissContext> mContext;
80 
81  std::vector<std::complex<float>> mComplexOut; //< Complex
82  std::vector<std::complex<float>> mComplexOutAverage; //< Complex averaged over multiple analysis hops
83 
84  AmplitudeSpectrum mAmplitude; //< Magnitude (rho)
85  PhaseSpectrum mPhase; //< Phase angle (theta)
86 
87  std::vector<float> mForwardHammingWindow; //< Preprocessed window function coefficients
88  float mHammingWindowSum; //< The sum of all window function coefficients
89  float mNormalizationFactor; //< Inverse of the window sum (2/sum)
90 
91 
92  std::mutex mSampleBufferMutex; //< The mutex for accessing the sample buffer
93  std::vector<float> mSampleBufferA; //< Samples provided by the audio node
94  std::vector<float> mSampleBufferB; //< Thread safe copy of original samples
95  std::vector<float> mSampleBufferFormatted; //< The sample buffer before application of a window function
96  std::vector<float> mSampleBufferWindowed; //< The sample buffer after application of a window function
97 
98  uint mBinCount; //< The number of FFT bins
99  float mBinFrequency; //< The frequency each bin comprises
100 
101  EOverlap mOverlap; //< The number of audio buffer overlaps for FFT analysis (hops)
102  uint mHopSize; //< The number of bins of a single hop
103  std::atomic<bool> mSampleData = { false }; //< Amplitudes dirty checking flag, prevents redundant FFT analyses
104  };
105 }
nap::ESerialByteSize::Seven
@ Seven
nap::uint
unsigned int uint
Definition: numeric.h:23
nap::FFTBuffer::getBinCount
uint getBinCount() const
Definition: fftbuffer.h:56
nap::ERasterizationSamples::One
@ One
nap::FFTBuffer
Definition: fftbuffer.h:19
nap::FFTBuffer::getAmplitudeSpectrum
const AmplitudeSpectrum & getAmplitudeSpectrum()
Definition: fftbuffer.h:66
nap::FFTBuffer::PhaseSpectrum
std::vector< float > PhaseSpectrum
Definition: fftbuffer.h:24
nap::ESerialByteSize::Five
@ Five
nap::FFTBuffer::getPhaseSpectrum
const PhaseSpectrum & getPhaseSpectrum()
Definition: fftbuffer.h:71
nap::FFTBuffer::AmplitudeSpectrum
std::vector< float > AmplitudeSpectrum
Definition: fftbuffer.h:23
nap
Definition: templateapp.h:17
nap::FFTBuffer::EOverlap
EOverlap
Definition: fftbuffer.h:29