Audacity 3.2.0
Public Member Functions | Protected Member Functions | Private Attributes | List of all members
TrackSpectrumTransformer Class Reference

Subclass of SpectrumTransformer that rewrites a track. More...

#include <SpectrumTransformer.h>

Inheritance diagram for TrackSpectrumTransformer:
[legend]
Collaboration diagram for TrackSpectrumTransformer:
[legend]

Public Member Functions

 ~TrackSpectrumTransformer () override
 
bool Process (const WindowProcessor &processor, WaveTrack *track, size_t queueLength, sampleCount start, sampleCount len)
 Invokes Start(), ProcessSamples(), and Finish() More...
 
 SpectrumTransformer (bool needsOutput, eWindowFunctions inWindowType, eWindowFunctions outWindowType, size_t windowSize, unsigned stepsPerWindow, bool leadingPadding, bool trailingPadding)
 
- Public Member Functions inherited from SpectrumTransformer
 SpectrumTransformer (bool needsOutput, eWindowFunctions inWindowType, eWindowFunctions outWindowType, size_t windowSize, unsigned stepsPerWindow, bool leadingPadding, bool trailingPadding)
 
virtual ~SpectrumTransformer ()
 
bool NeedsOutput () const
 
bool Start (size_t queueLength)
 Call once before a sequence of calls to ProcessSamples; Invokes DoStart. More...
 
bool ProcessSamples (const WindowProcessor &processor, const float *buffer, size_t len)
 Call multiple times. More...
 
bool Finish (const WindowProcessor &processor)
 Call once after a sequence of calls to ProcessSamples; flushes the queue and Invokes DoFinish. More...
 
virtual std::unique_ptr< WindowNewWindow (size_t windowSize)
 Allocates a window to place in the queue. More...
 
virtual bool DoStart ()
 Called before any calls to ProcessWindow. More...
 
virtual void DoOutput (const float *outBuffer, size_t mStepSize)=0
 Called within ProcessSamples if output was requested. More...
 
virtual bool DoFinish ()
 Called after the last call to ProcessWindow(). More...
 
size_t TotalQueueSize () const
 Useful functions to implement WindowProcesser: More...
 
size_t CurrentQueueSize () const
 How many windows in the queue have been filled? More...
 
bool QueueIsFull () const
 
WindowNth (int n)
 Access the queue, so you can inspect and modify any window in it. More...
 
WindowNewest ()
 
WindowLatest ()
 

Protected Member Functions

bool DoStart () override
 Called before any calls to ProcessWindow. More...
 
void DoOutput (const float *outBuffer, size_t mStepSize) override
 Called within ProcessSamples if output was requested. More...
 
bool DoFinish () override
 Called after the last call to ProcessWindow(). More...
 

Private Attributes

WaveTrackmpTrack = nullptr
 
std::shared_ptr< WaveTrackmOutputTrack
 
sampleCount mStart = 0
 
sampleCount mLen = 0
 

Additional Inherited Members

- Public Types inherited from SpectrumTransformer
using FloatVector = std::vector< float >
 
using WindowProcessor = std::function< bool(SpectrumTransformer &) >
 Type of function that transforms windows in the queue. More...
 
- Protected Attributes inherited from SpectrumTransformer
const size_t mWindowSize
 
const size_t mSpectrumSize
 
const unsigned mStepsPerWindow
 
const size_t mStepSize
 
const bool mLeadingPadding
 
const bool mTrailingPadding
 

Detailed Description

Subclass of SpectrumTransformer that rewrites a track.

Definition at line 183 of file SpectrumTransformer.h.

Constructor & Destructor Documentation

◆ ~TrackSpectrumTransformer()

TrackSpectrumTransformer::~TrackSpectrumTransformer ( )
overridedefault

Member Function Documentation

◆ DoFinish()

bool TrackSpectrumTransformer::DoFinish ( )
overrideprotectedvirtual

Called after the last call to ProcessWindow().

Returns
false to abort processing. Default implementation just returns true.

Reimplemented from SpectrumTransformer.

Definition at line 378 of file SpectrumTransformer.cpp.

379{
380 if (mOutputTrack) {
381 // Flush the output WaveTrack (since it's buffered)
382 mOutputTrack->Flush();
383
384 // Take the output track and insert it in place of the original
385 // sample data
386 auto t0 = mOutputTrack->LongSamplesToTime(mStart);
387 auto tLen = mOutputTrack->LongSamplesToTime(mLen);
388 // Filtering effects always end up with more data than they started with.
389 // Delete this 'tail'.
390 mOutputTrack->HandleClear(tLen, mOutputTrack->GetEndTime(), false, false);
391 mpTrack->ClearAndPaste(t0, t0 + tLen, &*mOutputTrack, true, false);
392 }
393
394 mOutputTrack.reset();
395 return true;
396}
std::shared_ptr< WaveTrack > mOutputTrack
void ClearAndPaste(double t0, double t1, const Track *src, bool preserve=true, bool merge=true, const TimeWarper *effectWarper=NULL)
Definition: WaveTrack.cpp:710

References WaveTrack::ClearAndPaste(), mLen, mOutputTrack, mpTrack, and mStart.

Referenced by EffectNoiseReduction::Worker::DoFinish(), and SpectralDataManager::Worker::DoFinish().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ DoOutput()

void TrackSpectrumTransformer::DoOutput ( const float *  outBuffer,
size_t  mStepSize 
)
overrideprotectedvirtual

Called within ProcessSamples if output was requested.

Implements SpectrumTransformer.

Definition at line 104 of file SpectrumTransformer.cpp.

105{
106 mOutputTrack->Append((constSamplePtr)outBuffer, floatSample, mStepSize);
107}
const char * constSamplePtr
Definition: SampleFormat.h:56

References floatSample, mOutputTrack, and SpectrumTransformer::mStepSize.

◆ DoStart()

bool TrackSpectrumTransformer::DoStart ( )
overrideprotectedvirtual

Called before any calls to ProcessWindow.

More queue initializations can be done here.

Returns
false to abort processing. Default implementation just returns true.

Reimplemented from SpectrumTransformer.

Definition at line 404 of file SpectrumTransformer.cpp.

405{
406 mOutputTrack = NeedsOutput() && mpTrack ? mpTrack->EmptyCopy() : nullptr;
408}
virtual bool DoStart()
Called before any calls to ProcessWindow.
Holder EmptyCopy(const SampleBlockFactoryPtr &pFactory={}, bool keepLink=true) const
Definition: WaveTrack.cpp:574

References SpectrumTransformer::DoStart(), WaveTrack::EmptyCopy(), mOutputTrack, mpTrack, and SpectrumTransformer::NeedsOutput().

Referenced by EffectNoiseReduction::Worker::DoStart(), and SpectralDataManager::Worker::DoStart().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Process()

bool TrackSpectrumTransformer::Process ( const WindowProcessor processor,
WaveTrack track,
size_t  queueLength,
sampleCount  start,
sampleCount  len 
)

Invokes Start(), ProcessSamples(), and Finish()

Definition at line 341 of file SpectrumTransformer.cpp.

343{
344 if (!track)
345 return false;
346
347 mpTrack = track;
348
349 if (!Start(queueLength))
350 return false;
351
352 mStart = start;
353 mLen = len;
354 auto bufferSize = track->GetMaxBlockSize();
355 FloatVector buffer(bufferSize);
356
357 bool bLoopSuccess = true;
358 auto samplePos = start;
359 while (bLoopSuccess && samplePos < start + len) {
360 //Get a blockSize of samples (smaller than the size of the buffer)
361 const auto blockSize = limitSampleBufferSize(
362 std::min(bufferSize, track->GetBestBlockSize(samplePos)),
363 start + len - samplePos);
364
365 //Get the samples from the track and put them in the buffer
366 track->GetFloats(buffer.data(), samplePos, blockSize);
367 samplePos += blockSize;
368
369 bLoopSuccess = ProcessSamples(processor, buffer.data(), blockSize);
370 }
371
372 if (!Finish(processor))
373 return false;
374
375 return bLoopSuccess;
376}
int min(int a, int b)
std::vector< float > FloatVector
size_t limitSampleBufferSize(size_t bufferSize, sampleCount limit)
Definition: SampleCount.cpp:22
bool GetFloats(float *buffer, sampleCount start, size_t len, fillFormat fill=fillZero, bool mayThrow=true, sampleCount *pNumWithinClips=nullptr) const
Retrieve samples from a track in floating-point format, regardless of the storage format.
Definition: SampleTrack.h:82
bool Finish(const WindowProcessor &processor)
Call once after a sequence of calls to ProcessSamples; flushes the queue and Invokes DoFinish.
bool ProcessSamples(const WindowProcessor &processor, const float *buffer, size_t len)
Call multiple times.
bool Start(size_t queueLength)
Call once before a sequence of calls to ProcessSamples; Invokes DoStart.
size_t GetMaxBlockSize() const override
This returns a nonnegative number of samples meant to size a memory buffer.
Definition: WaveTrack.cpp:1611
size_t GetBestBlockSize(sampleCount t) const override
This returns a nonnegative number of samples meant to size a memory buffer.
Definition: WaveTrack.cpp:1593

References SpectrumTransformer::Finish(), WaveTrack::GetBestBlockSize(), SampleTrack::GetFloats(), WaveTrack::GetMaxBlockSize(), limitSampleBufferSize(), min(), mLen, mpTrack, mStart, SpectrumTransformer::ProcessSamples(), and SpectrumTransformer::Start().

Referenced by EffectNoiseReduction::Worker::Process(), SpectralDataManager::Worker::Process(), SpectralDataManager::Worker::ProcessOvertones(), and SpectralDataManager::Worker::ProcessSnapping().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ SpectrumTransformer()

SpectrumTransformer::SpectrumTransformer ( bool  needsOutput,
eWindowFunctions  inWindowType,
eWindowFunctions  outWindowType,
size_t  windowSize,
unsigned  stepsPerWindow,
bool  leadingPadding,
bool  trailingPadding 
)
Precondition
!(inWindowType == eWinFuncRectangular && outWindowType eWinFuncRectangular)
windowSize % stepsPerWindow == 0
windowSize is a power of 2
Parameters
needsOutputWhether to do the inverse FFT
inWindowTypeUsed in FFT transform
outWindowTypeUsed in inverse FFT transform
windowSizemust be a power of 2
stepsPerWindowdetermines the overlap
leadingPaddingWhether to start the queue with windows that partially overlap the first full window of input samples
trailingPaddingWhether to stop the procedure after the last complete window of input is added to the queue

Definition at line 50 of file SpectrumTransformer.cpp.

22: mWindowSize{ windowSize }
23, mSpectrumSize{ 1 + mWindowSize / 2 }
24, mStepsPerWindow{ stepsPerWindow }
26, mLeadingPadding{ leadingPadding }
27, mTrailingPadding{ trailingPadding }
32, mNeedsOutput{ needsOutput }
33{
34 // Check preconditions
35
36 // Powers of 2 only!
37 wxASSERT(mWindowSize > 0 &&
38 0 == (mWindowSize & (mWindowSize - 1)));
39
40 wxASSERT(mWindowSize % mStepsPerWindow == 0);
41
42 wxASSERT(!(inWindowType == eWinFuncRectangular && outWindowType == eWinFuncRectangular));
43
44 // To do: check that inWindowType, outWindowType, and mStepsPerWindow
45 // are compatible for correct overlap-add reconstruction.
46
47 // Create windows as needed
48 if (inWindowType != eWinFuncRectangular) {
49 mInWindow.resize(mWindowSize);
50 std::fill(mInWindow.begin(), mInWindow.end(), 1.0f);
51 NewWindowFunc(inWindowType, mWindowSize, false, mInWindow.data());
52 }
53 if (outWindowType != eWinFuncRectangular) {
54 mOutWindow.resize(mWindowSize);
55 std::fill(mOutWindow.begin(), mOutWindow.end(), 1.0f);
56 NewWindowFunc(outWindowType, mWindowSize, false, mOutWindow.data());
57 }
58
59 // Must scale one or the other window so overlap-add
60 // comes out right
61 double denom = 0;
62 for (size_t ii = 0; ii < mWindowSize; ii += mStepSize) {
63 denom +=
64 (mInWindow.empty() ? 1.0 : mInWindow[ii])
65 *
66 (mOutWindow.empty() ? 1.0 : mOutWindow[ii]);
67 }
68 // It is ASSUMED that you have chosen window types and
69 // steps per window, so that this sum denom would be the
70 // same, starting the march anywhere from 0 to mStepSize - 1.
71 // Else, your overlap-add won't be right, and the transformer
72 // might not be an identity even when you do nothing to the
73 // spectra.
74
75 float *pWindow = 0;
76 if (!mInWindow.empty())
77 pWindow = mInWindow.data();
78 else if (!mOutWindow.empty())
79 pWindow = mOutWindow.data();
80 else
81 // Can only happen if both window types were rectangular
82 wxASSERT(false);
83 for (size_t ii = 0; ii < mWindowSize; ++ii)
84 *pWindow++ /= denom;
85}
void NewWindowFunc(int whichFunction, size_t NumSamplesIn, bool extraSample, float *in)
Definition: FFT.cpp:368
@ eWinFuncRectangular
Definition: FFT.h:111
HFFT GetFFT(size_t fftlen)
Definition: RealFFTf.cpp:104
FloatVector mFFTBuffer
These have size mWindowSize:
const unsigned mStepsPerWindow
FloatVector mInWindow
These have size mWindowSize, or 0 for rectangular window:

Member Data Documentation

◆ mLen

sampleCount TrackSpectrumTransformer::mLen = 0
private

Definition at line 200 of file SpectrumTransformer.h.

Referenced by DoFinish(), and Process().

◆ mOutputTrack

std::shared_ptr<WaveTrack> TrackSpectrumTransformer::mOutputTrack
private

Definition at line 199 of file SpectrumTransformer.h.

Referenced by DoFinish(), DoOutput(), and DoStart().

◆ mpTrack

WaveTrack* TrackSpectrumTransformer::mpTrack = nullptr
private

Definition at line 198 of file SpectrumTransformer.h.

Referenced by DoFinish(), DoStart(), and Process().

◆ mStart

sampleCount TrackSpectrumTransformer::mStart = 0
private

Definition at line 200 of file SpectrumTransformer.h.

Referenced by DoFinish(), and Process().


The documentation for this class was generated from the following files: