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
33class 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
79
80 bool Finish(const WindowProcessor &processor);
81
83 struct Window
84 {
85 explicit Window(size_t windowSize)
86 : mRealFFTs( windowSize / 2 )
87 , mImagFFTs( windowSize / 2 )
88 {
89 }
90
91 virtual ~Window();
92
93 void Zero()
94 {
95 const auto size = mRealFFTs.size();
96 auto pFill = mRealFFTs.data();
97 std::fill(pFill, pFill + size, 0.0f);
98 pFill = mImagFFTs.data();
99 std::fill(pFill, pFill + size, 0.0f);
100 }
101
106 };
107
109
111 virtual std::unique_ptr<Window> NewWindow(size_t windowSize);
112
114
116 virtual bool DoStart();
117
119 virtual void DoOutput(const float *outBuffer, size_t mStepSize) = 0;
120
122
123 virtual bool DoFinish();
124
126
128 size_t TotalQueueSize() const { return mQueue.size(); }
129
131
132 size_t CurrentQueueSize() const;
133
136 bool QueueIsFull() const;
137
139
140 Window &Nth(int n) { return *mQueue[n]; }
141
142 Window &Newest() { return **mQueue.begin(); }
143 Window &Latest() { return **mQueue.rbegin(); }
144
145private:
146 void ResizeQueue(size_t queueLength);
147 void FillFirstWindow();
148 void RotateWindows();
149 void OutputStep();
150
151protected:
152 const size_t mWindowSize;
153 const size_t mSpectrumSize;
154
155 const unsigned mStepsPerWindow;
156 const size_t mStepSize;
157
158 const bool mLeadingPadding;
159
161
162private:
163 std::vector<std::unique_ptr<Window>> mQueue;
167 size_t mInWavePos = 0;
168
176
177 const bool mNeedsOutput;
178};
179
180class WaveTrack;
181
183class TrackSpectrumTransformer /* not final */ : public SpectrumTransformer {
184public:
187
189 bool Process( const WindowProcessor &processor, WaveTrack *track,
190 size_t queueLength, sampleCount start, sampleCount len);
191
192protected:
193 bool DoStart() override;
194 void DoOutput(const float *outBuffer, size_t mStepSize) override;
195 bool DoFinish() override;
196
197private:
198 WaveTrack *mpTrack = nullptr;
199 std::shared_ptr<WaveTrack> mOutputTrack;
201};
202
203#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)
Call once after a sequence of calls to ProcessSamples; flushes the queue and Invokes DoFinish.
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.
std::shared_ptr< WaveTrack > mOutputTrack
~TrackSpectrumTransformer() override
bool Process(const WindowProcessor &processor, WaveTrack *track, size_t queueLength, sampleCount start, sampleCount len)
Invokes Start(), ProcessSamples(), and Finish()
void DoOutput(const float *outBuffer, size_t mStepSize) override
Called within ProcessSamples if output was requested.
bool DoFinish() override
Called after the last call to ProcessWindow().
bool DoStart() override
Called before any calls to ProcessWindow.
A Track that contains audio waveform data.
Definition: WaveTrack.h:57
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