Audacity 3.2.0
Public Member Functions | Static Public Member Functions | Public Attributes | List of all members
WaveClipSpectrumCache Struct Referencefinal

#include <SpectrumCache.h>

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

Public Member Functions

 WaveClipSpectrumCache (size_t nChannels)
 
 ~WaveClipSpectrumCache () override
 
void MarkChanged () override
 
void Invalidate () override
 
bool GetSpectrogram (const WaveChannelInterval &clip, const float *&spectrogram, SpectrogramSettings &spectrogramSettings, const sampleCount *&where, size_t numPixels, double t0, double pixelsPerSecond)
 
- Public Member Functions inherited from WaveClipListener
virtual ~WaveClipListener ()=0
 
virtual void MarkChanged ()=0
 
virtual void Invalidate ()=0
 

Static Public Member Functions

static WaveClipSpectrumCacheGet (const WaveClip &clip)
 

Public Attributes

std::vector< std::unique_ptr< SpecPxCache > > mSpecPxCaches
 
std::vector< std::unique_ptr< SpecCache > > mSpecCaches
 
int mDirty { 0 }
 

Detailed Description

Definition at line 104 of file SpectrumCache.h.

Constructor & Destructor Documentation

◆ WaveClipSpectrumCache()

WaveClipSpectrumCache::WaveClipSpectrumCache ( size_t  nChannels)
explicit

Definition at line 575 of file SpectrumCache.cpp.

577 : mSpecCaches(std::max<size_t>(2, nChannels))
578 , mSpecPxCaches(std::max<size_t>(2, nChannels))
579{
580 for (auto &pCache : mSpecCaches)
581 pCache = std::make_unique<SpecCache>();
582}
std::vector< std::unique_ptr< SpecPxCache > > mSpecPxCaches
std::vector< std::unique_ptr< SpecCache > > mSpecCaches

References mSpecCaches.

◆ ~WaveClipSpectrumCache()

WaveClipSpectrumCache::~WaveClipSpectrumCache ( )
override

Definition at line 584 of file SpectrumCache.cpp.

585{
586}

Member Function Documentation

◆ Get()

WaveClipSpectrumCache & WaveClipSpectrumCache::Get ( const WaveClip clip)
static

Definition at line 592 of file SpectrumCache.cpp.

593{
594 return const_cast< WaveClip& >( clip ) // Consider it mutable data
595 .Caches::Get< WaveClipSpectrumCache >( sKeyS );
596}
static WaveClip::Caches::RegisteredFactory sKeyS
This allows multiple clips to be a part of one WaveTrack.
Definition: WaveClip.h:138

References sKeyS.

Referenced by anonymous_namespace{SpectrumView.cpp}::DrawClipSpectrum().

Here is the caller graph for this function:

◆ GetSpectrogram()

bool WaveClipSpectrumCache::GetSpectrogram ( const WaveChannelInterval clip,
const float *&  spectrogram,
SpectrogramSettings spectrogramSettings,
const sampleCount *&  where,
size_t  numPixels,
double  t0,
double  pixelsPerSecond 
)

Getting high-level data for screen display

Definition at line 461 of file SpectrumCache.cpp.

467{
468 auto &mSpecCache = mSpecCaches[clip.GetChannelIndex()];
469
470 const auto sampleRate = clip.GetRate();
471 const auto stretchRatio = clip.GetStretchRatio();
472 const auto samplesPerPixel = sampleRate / pixelsPerSecond / stretchRatio;
473
474 //Trim offset comparison failure forces spectrogram cache rebuild
475 //and skip copying "unchanged" data after clip border was trimmed.
476 bool match = mSpecCache && mSpecCache->leftTrim == clip.GetTrimLeft() &&
477 mSpecCache->rightTrim == clip.GetTrimRight() &&
478 mSpecCache->len > 0 &&
479 mSpecCache->Matches(mDirty, samplesPerPixel, settings);
480
481 if (match && mSpecCache->start == t0 && mSpecCache->len >= numPixels)
482 {
483 spectrogram = &mSpecCache->freq[0];
484 where = &mSpecCache->where[0];
485
486 return false; //hit cache completely
487 }
488
489 // Caching is not implemented for reassignment, unless for
490 // a complete hit, because of the complications of time reassignment
492 match = false;
493
494 // Free the cache when it won't cause a major stutter.
495 // If the window size changed, we know there is nothing to be copied
496 // If we zoomed out, or resized, we can give up memory. But not too much -
497 // up to 2x extra is needed at the end of the clip to prevent stutter.
498 if (mSpecCache->freq.capacity() > 2.1 * mSpecCache->freq.size() ||
499 mSpecCache->windowSize*mSpecCache->zeroPaddingFactor <
500 settings.WindowSize()*settings.ZeroPaddingFactor())
501 {
502 match = false;
503 mSpecCache = std::make_unique<SpecCache>();
504 }
505
506 int oldX0 = 0;
507 double correction = 0.0;
508
509 int copyBegin = 0, copyEnd = 0;
510 if (match) {
512 mSpecCache->where, mSpecCache->len, numPixels, t0, sampleRate,
513 stretchRatio, samplesPerPixel, oldX0, correction);
514 // Remember our first pixel maps to oldX0 in the old cache,
515 // possibly out of bounds.
516 // For what range of pixels can data be copied?
517 copyBegin = std::min((int)numPixels, std::max(0, -oldX0));
518 copyEnd = std::min((int)numPixels, std::max(0,
519 (int)mSpecCache->len - oldX0
520 ));
521 }
522
523 // Resize the cache, keep the contents unchanged.
524 mSpecCache->Grow(numPixels, settings, samplesPerPixel, t0);
525 mSpecCache->leftTrim = clip.GetTrimLeft();
526 mSpecCache->rightTrim = clip.GetTrimRight();
527 auto nBins = settings.NBins();
528
529 // Optimization: if the old cache is good and overlaps
530 // with the current one, re-use as much of the cache as
531 // possible
532 if (copyEnd > copyBegin)
533 {
534 // memmove is required since dst/src overlap
535 memmove(&mSpecCache->freq[nBins * copyBegin],
536 &mSpecCache->freq[nBins * (copyBegin + oldX0)],
537 nBins * (copyEnd - copyBegin) * sizeof(float));
538 }
539
540 // Reassignment accumulates, so it needs a zeroed buffer
542 {
543 // The cache could theoretically copy from the middle, resulting
544 // in two regions to update. This won't happen in zoom, since
545 // old cache doesn't match. It won't happen in resize, since the
546 // spectrum view is pinned to left side of window.
547 wxASSERT(
548 (copyBegin >= 0 && copyEnd == (int)numPixels) || // copied the end
549 (copyBegin == 0 && copyEnd <= (int)numPixels) // copied the beginning
550 );
551
552 int zeroBegin = copyBegin > 0 ? 0 : copyEnd-copyBegin;
553 int zeroEnd = copyBegin > 0 ? copyBegin : numPixels;
554
555 memset(&mSpecCache->freq[nBins*zeroBegin], 0, nBins*(zeroEnd-zeroBegin)*sizeof(float));
556 }
557
558 // purposely offset the display 1/2 sample to the left (as compared
559 // to waveform display) to properly center response of the FFT
560 constexpr auto addBias = true;
562 mSpecCache->where, numPixels, addBias, correction, t0, sampleRate,
563 stretchRatio, samplesPerPixel);
564
565 mSpecCache->Populate(
566 settings, clip, copyBegin, copyEnd, numPixels, pixelsPerSecond);
567
568 mSpecCache->dirty = mDirty;
569 spectrogram = &mSpecCache->freq[0];
570 where = &mSpecCache->where[0];
571
572 return true;
573}
int min(int a, int b)
static Settings & settings()
Definition: TrackInfo.cpp:69
double GetStretchRatio() const override
Definition: WaveTrack.cpp:128
double GetTrimLeft() const
Definition: WaveTrack.cpp:133
double GetTrimRight() const
Definition: WaveTrack.cpp:138
int GetRate() const override
Definition: WaveTrack.cpp:108
size_t GetChannelIndex() const
Definition: WaveTrack.h:81
AUDACITY_DLL_API void findCorrection(const std::vector< sampleCount > &oldWhere, size_t oldLen, size_t newLen, double t0, double sampleRate, double stretchRatio, double samplesPerPixel, int &oldX0, double &correction)
AUDACITY_DLL_API void fillWhere(std::vector< sampleCount > &where, size_t len, bool addBias, double correction, double t0, double sampleRate, double stretchRatio, double samplesPerPixel)

References SpectrogramSettings::algReassignment, WaveClipUtilities::fillWhere(), WaveClipUtilities::findCorrection(), WaveChannelInterval::GetChannelIndex(), WaveChannelInterval::GetRate(), WaveChannelInterval::GetStretchRatio(), WaveChannelInterval::GetTrimLeft(), WaveChannelInterval::GetTrimRight(), mDirty, min(), mSpecCaches, anonymous_namespace{ClipSegmentTest.cpp}::sampleRate, and settings().

Referenced by anonymous_namespace{SpectrumView.cpp}::DrawClipSpectrum().

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

◆ Invalidate()

void WaveClipSpectrumCache::Invalidate ( )
overridevirtual

Implements WaveClipListener.

Definition at line 603 of file SpectrumCache.cpp.

604{
605 // Invalidate the spectrum display cache
606 for (auto &pCache : mSpecCaches)
607 pCache = std::make_unique<SpecCache>();
608}

References mSpecCaches.

◆ MarkChanged()

void WaveClipSpectrumCache::MarkChanged ( )
overridevirtual

Implements WaveClipListener.

Definition at line 598 of file SpectrumCache.cpp.

599{
600 ++mDirty;
601}

References mDirty.

Member Data Documentation

◆ mDirty

int WaveClipSpectrumCache::mDirty { 0 }

Definition at line 112 of file SpectrumCache.h.

Referenced by GetSpectrogram(), and MarkChanged().

◆ mSpecCaches

std::vector<std::unique_ptr<SpecCache> > WaveClipSpectrumCache::mSpecCaches

Definition at line 111 of file SpectrumCache.h.

Referenced by GetSpectrogram(), Invalidate(), and WaveClipSpectrumCache().

◆ mSpecPxCaches

std::vector<std::unique_ptr<SpecPxCache> > WaveClipSpectrumCache::mSpecPxCaches

Definition at line 110 of file SpectrumCache.h.


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