Audacity 3.2.0
SpectrumTransformer.h
Go to the documentation of this file.
1/*!********************************************************************
2
3Audacity: A Digital Audio Editor
4
5SpectrumTransformer.h
6@brief Transformer of sample sequences by FFT, coefficient changes, inverse FFT, overlap-add
7
8Paul Licameli
9
10**********************************************************************/
11
12#ifndef __AUDACITY_SPECTRUM_TRANSFORMER__
13#define __AUDACITY_SPECTRUM_TRANSFORMER__
14
15#include <cassert>
16#include <functional>
17#include <memory>
18#include <vector>
19#include "RealFFTf.h"
20#include "SampleCount.h"
21
22enum eWindowFunctions : int;
23
33class FFT_API SpectrumTransformer /* not final */
34{
35public:
36 // Public interface
37 using FloatVector = std::vector<float>;
38
40
43 using WindowProcessor = std::function< bool(SpectrumTransformer&) >;
44
51 bool needsOutput,
52 eWindowFunctions inWindowType,
53 eWindowFunctions outWindowType,
54 size_t windowSize,
55 unsigned stepsPerWindow,
56 bool leadingPadding,
59 bool trailingPadding
62 );
64
65 bool NeedsOutput() const { return mNeedsOutput; }
66
68
69 bool Start(size_t queueLength);
70
72
75 bool ProcessSamples(const WindowProcessor &processor,
76 const float *buffer, size_t len);
77
80
81 bool Finish(const WindowProcessor &processor);
82
84 struct FFT_API Window
85 {
86 explicit Window(size_t windowSize)
87 : mRealFFTs( windowSize / 2 )
88 , mImagFFTs( windowSize / 2 )
89 {
90 }
91
92 virtual ~Window();
93
94 void Zero()
95 {
96 const auto size = mRealFFTs.size();
97 auto pFill = mRealFFTs.data();
98 std::fill(pFill, pFill + size, 0.0f);
99 pFill = mImagFFTs.data();
100 std::fill(pFill, pFill + size, 0.0f);
101 }
102
107 };
108
110
112 virtual std::unique_ptr<Window> NewWindow(size_t windowSize);
113
115
117 virtual bool DoStart();
118
120 virtual void DoOutput(const float *outBuffer, size_t mStepSize) = 0;
121
123
124 virtual bool DoFinish();
125
127
129 size_t TotalQueueSize() const { return mQueue.size(); }
130
132
133 size_t CurrentQueueSize() const;
134
137 bool QueueIsFull() const;
138
140
141 Window &Nth(int n) { return *mQueue[n]; }
142
143 Window &Newest() { return **mQueue.begin(); }
144 Window &Latest() { return **mQueue.rbegin(); }
145
146private:
147 void ResizeQueue(size_t queueLength);
148 void FillFirstWindow();
149 void RotateWindows();
150 void OutputStep();
151
152protected:
153 const size_t mWindowSize;
154 const size_t mSpectrumSize;
155
156 const unsigned mStepsPerWindow;
157 const size_t mStepSize;
158
159 const bool mLeadingPadding;
160
162
163private:
164 std::vector<std::unique_ptr<Window>> mQueue;
166 sampleCount mInSampleCount = 0;
167 sampleCount mOutStepCount = 0;
168 size_t mInWavePos = 0;
169
177
178 const bool mNeedsOutput;
179};
180
181#endif
eWindowFunctions
Definition: FFT.h:110
std::unique_ptr< FFTParam, FFTDeleter > HFFT
Definition: RealFFTf.h:22
A class that transforms a portion of a wave track (preserving duration) by applying Fourier transform...
FloatVector mFFTBuffer
These have size mWindowSize:
std::function< bool(SpectrumTransformer &) > WindowProcessor
Type of function that transforms windows in the queue.
virtual void DoOutput(const float *outBuffer, size_t mStepSize)=0
Called within ProcessSamples if output was requested.
std::vector< float > FloatVector
const unsigned mStepsPerWindow
Window & Nth(int n)
Access the queue, so you can inspect and modify any window in it.
std::vector< std::unique_ptr< Window > > mQueue
size_t TotalQueueSize() const
Useful functions to implement WindowProcesser:
virtual ~SpectrumTransformer()
FloatVector mInWindow
These have size mWindowSize, or 0 for rectangular window:
Positions or offsets within audio files need a wide type.
Definition: SampleCount.h:19
Derive this class to add information to the queue.
FloatVector mRealFFTs
index zero holds the dc coefficient, which has no imaginary part
FloatVector mImagFFTs
index zero holds the nyquist frequency coefficient, actually real