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 <functional>
16#include <memory>
17#include <vector>
18#include "audacity/Types.h"
19#include "RealFFTf.h"
20#include "SampleCount.h"
21
22enum eWindowFunctions : int;
23
24class WaveChannel;
25
35class SpectrumTransformer /* not final */
36{
37public:
38 // Public interface
39 using FloatVector = std::vector<float>;
40
42
45 using WindowProcessor = std::function< bool(SpectrumTransformer&) >;
46
53 bool needsOutput,
54 eWindowFunctions inWindowType,
55 eWindowFunctions outWindowType,
56 size_t windowSize,
57 unsigned stepsPerWindow,
58 bool leadingPadding,
61 bool trailingPadding
64 );
66
67 bool NeedsOutput() const { return mNeedsOutput; }
68
70
71 bool Start(size_t queueLength);
72
74
77 bool ProcessSamples(const WindowProcessor &processor,
78 const float *buffer, size_t len);
79
82
83 bool Finish(const WindowProcessor &processor);
84
86 struct Window
87 {
88 explicit Window(size_t windowSize)
89 : mRealFFTs( windowSize / 2 )
90 , mImagFFTs( windowSize / 2 )
91 {
92 }
93
94 virtual ~Window();
95
96 void Zero()
97 {
98 const auto size = mRealFFTs.size();
99 auto pFill = mRealFFTs.data();
100 std::fill(pFill, pFill + size, 0.0f);
101 pFill = mImagFFTs.data();
102 std::fill(pFill, pFill + size, 0.0f);
103 }
104
109 };
110
112
114 virtual std::unique_ptr<Window> NewWindow(size_t windowSize);
115
117
119 virtual bool DoStart();
120
122 virtual void DoOutput(const float *outBuffer, size_t mStepSize) = 0;
123
125
126 virtual bool DoFinish();
127
129
131 size_t TotalQueueSize() const { return mQueue.size(); }
132
134
135 size_t CurrentQueueSize() const;
136
139 bool QueueIsFull() const;
140
142
143 Window &Nth(int n) { return *mQueue[n]; }
144
145 Window &Newest() { return **mQueue.begin(); }
146 Window &Latest() { return **mQueue.rbegin(); }
147
148private:
149 void ResizeQueue(size_t queueLength);
150 void FillFirstWindow();
151 void RotateWindows();
152 void OutputStep();
153
154protected:
155 const size_t mWindowSize;
156 const size_t mSpectrumSize;
157
158 const unsigned mStepsPerWindow;
159 const size_t mStepSize;
160
161 const bool mLeadingPadding;
162
164
165private:
166 std::vector<std::unique_ptr<Window>> mQueue;
170 size_t mInWavePos = 0;
171
179
180 const bool mNeedsOutput;
181};
182
183class WaveTrack;
184
186class TrackSpectrumTransformer /* not final */ : public SpectrumTransformer {
187public:
194 bool needsOutput, eWindowFunctions inWindowType,
195 eWindowFunctions outWindowType, size_t windowSize,
196 unsigned stepsPerWindow, bool leadingPadding, bool trailingPadding
197 ) : SpectrumTransformer{ needsOutput, inWindowType, outWindowType,
198 windowSize, stepsPerWindow, leadingPadding, trailingPadding
199 }
200 , mOutputTrack{ pOutputTrack }
201 {
202 assert(!needsOutput || pOutputTrack != nullptr);
203 }
205
207 bool Process(const WindowProcessor &processor, const WaveChannel &channel,
208 size_t queueLength, sampleCount start, sampleCount len);
209
211 static bool PostProcess(WaveTrack &outputTrack, sampleCount len);
212
213protected:
214 bool DoStart() override;
215 void DoOutput(const float *outBuffer, size_t mStepSize) override;
216 bool DoFinish() override;
217
218private:
220 const WaveChannel *mpChannel = nullptr;
221};
222
223#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...
sampleCount mOutStepCount
sometimes negative
FloatVector mFFTBuffer
These have size mWindowSize:
std::function< bool(SpectrumTransformer &) > WindowProcessor
Type of function that transforms windows in the queue.
bool Finish(const WindowProcessor &processor)
void ResizeQueue(size_t queueLength)
virtual bool DoStart()
Called before any calls to ProcessWindow.
virtual void DoOutput(const float *outBuffer, size_t mStepSize)=0
Called within ProcessSamples if output was requested.
std::vector< float > FloatVector
const unsigned mStepsPerWindow
bool ProcessSamples(const WindowProcessor &processor, const float *buffer, size_t len)
Call multiple times.
virtual std::unique_ptr< Window > NewWindow(size_t windowSize)
Allocates a window to place in the queue.
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()
virtual bool DoFinish()
Called after the last call to ProcessWindow().
SpectrumTransformer(bool needsOutput, eWindowFunctions inWindowType, eWindowFunctions outWindowType, size_t windowSize, unsigned stepsPerWindow, bool leadingPadding, bool trailingPadding)
FloatVector mInWindow
These have size mWindowSize, or 0 for rectangular window:
size_t CurrentQueueSize() const
How many windows in the queue have been filled?
bool Start(size_t queueLength)
Call once before a sequence of calls to ProcessSamples; Invokes DoStart.
Subclass of SpectrumTransformer that rewrites a track.
~TrackSpectrumTransformer() override
void DoOutput(const float *outBuffer, size_t mStepSize) override
Called within ProcessSamples if output was requested.
WaveChannel *const mOutputTrack
const WaveChannel * mpChannel
bool DoFinish() override
Called after the last call to ProcessWindow().
static bool PostProcess(WaveTrack &outputTrack, sampleCount len)
Final flush and trimming of tail samples.
bool DoStart() override
Called before any calls to ProcessWindow.
bool Process(const WindowProcessor &processor, const WaveChannel &channel, size_t queueLength, sampleCount start, sampleCount len)
Invokes Start(), ProcessSamples(), and Finish()
TrackSpectrumTransformer(WaveChannel *pOutputTrack, bool needsOutput, eWindowFunctions inWindowType, eWindowFunctions outWindowType, size_t windowSize, unsigned stepsPerWindow, bool leadingPadding, bool trailingPadding)
A Track that contains audio waveform data.
Definition: WaveTrack.h:203
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