Audacity 3.2.0
Public Types | Public Member Functions | Private Member Functions | Private Attributes | List of all members
WaveDataCache Class Referencefinal

Cache that contains the waveform data. More...

#include <WaveDataCache.h>

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

Public Types

using DataProvider = std::function< bool(int64_t requiredSample, WaveCacheSampleBlock::Type dataType, WaveCacheSampleBlock &block)>
 
- Public Types inherited from GraphicsDataCache< WaveCacheElement >
using ElementFactory = std::function< std::unique_ptr< WaveCacheElement >()>
 
using Initializer = std::function< bool(const GraphicsDataCacheKey &Key, WaveCacheElement &element)>
 

Public Member Functions

 WaveDataCache (const WaveClip &waveClip, int channelIndex)
 
- Public Member Functions inherited from GraphicsDataCache< WaveCacheElement >
 GraphicsDataCache (const GraphicsDataCache &)=delete
 
 GraphicsDataCache (double scaledSampleRate, ElementFactory elementFactory)
 
GraphicsDataCacheoperator= (const GraphicsDataCache &)=delete
 
 ~GraphicsDataCache () override
 
IteratorRange< GraphicsDataCacheIterator< WaveCacheElement > > PerformLookup (const ZoomInfo &zoomInfo, double t0, double t1)
 
const WaveCacheElementPerformLookup (GraphicsDataCacheKey key)
 
void setInitializer (Initializer initializer)
 
- Public Member Functions inherited from GraphicsDataCacheBase
virtual ~GraphicsDataCacheBase ()=default
 
void Invalidate ()
 Invalidate the cache content. More...
 
double GetScaledSampleRate () const noexcept
 Returns the sample rate associated with cache. More...
 
void UpdateViewportWidth (int64_t width) noexcept
 
int64_t GetMaxViewportWidth () const noexcept
 

Private Member Functions

bool InitializeElement (const GraphicsDataCacheKey &key, WaveCacheElement &element) override
 

Private Attributes

DataProvider mProvider
 
WaveCacheSampleBlock mCachedBlock
 
const WaveClipmWaveClip
 
Observer::Subscription mStretchChangedSubscription
 

Additional Inherited Members

- Static Public Attributes inherited from GraphicsDataCacheBase
static constexpr uint32_t CacheElementWidth = 256
 
- Protected Types inherited from GraphicsDataCacheBase
using Lookup = std::vector< LookupElement >
 Cache lookup is a vector, with items sorted using Key. More...
 
- Protected Member Functions inherited from GraphicsDataCache< WaveCacheElement >
virtual void CheckCache (const ZoomInfo &, double, double)
 
virtual bool InitializeElement (const GraphicsDataCacheKey &key, WaveCacheElement &element)
 
- Protected Member Functions inherited from GraphicsDataCacheBase
 GraphicsDataCacheBase (double scaledSampleRate)
 
void SetScaledSampleRate (double scaledSampleRate)
 
virtual GraphicsDataCacheElementBaseCreateElement (const GraphicsDataCacheKey &key)=0
 Create a new Cache element. Implementation is responsible of the lifetime control. More...
 
virtual void DisposeElement (GraphicsDataCacheElementBase *element)=0
 This method is called, when the cache element should be evicted. Implementation may not deallocate the object. More...
 
virtual bool UpdateElement (const GraphicsDataCacheKey &key, GraphicsDataCacheElementBase &element)=0
 This method is called on all elements matching the request that are not complete (i. e. IsComplete if false). More...
 
BaseLookupResult PerformBaseLookup (const ZoomInfo &zoomInfo, double t0, double t1)
 Perform a lookup inside the cache. This method modifies mLookup and invalidates any previous result. More...
 
const GraphicsDataCacheElementBasePerformBaseLookup (GraphicsDataCacheKey key)
 Perform a lookup for the given key. This method modifies mLookup and invalidates any previous result. More...
 

Detailed Description

Cache that contains the waveform data.

Definition at line 92 of file WaveDataCache.h.

Member Typedef Documentation

◆ DataProvider

using WaveDataCache::DataProvider = std::function<bool (int64_t requiredSample, WaveCacheSampleBlock::Type dataType, WaveCacheSampleBlock& block)>

Definition at line 96 of file WaveDataCache.h.

Constructor & Destructor Documentation

◆ WaveDataCache()

WaveDataCache::WaveDataCache ( const WaveClip waveClip,
int  channelIndex 
)

Definition at line 274 of file WaveDataCache.cpp.

276 waveClip.GetRate() / waveClip.GetStretchRatio(),
277 [] { return std::make_unique<WaveCacheElement>(); })
278 , mProvider { MakeDefaultDataProvider(waveClip, channelIndex) }
279 , mWaveClip { waveClip }
281 const_cast<WaveClip&>(waveClip)
283 [this](const StretchRatioChange&) {
286 })
287 }
288{
289}
void SetScaledSampleRate(double scaledSampleRate)
An object that sends messages to an open-ended list of subscribed callbacks.
Definition: Observer.h:108
This allows multiple clips to be a part of one WaveTrack.
Definition: WaveClip.h:238
double GetStretchRatio() const override
Definition: WaveClip.cpp:625
int GetRate() const override
Definition: WaveClip.h:337
Observer::Subscription mStretchChangedSubscription
DataProvider mProvider
const WaveClip & mWaveClip
WaveDataCache::DataProvider MakeDefaultDataProvider(const WaveClip &clip, int channelIndex)

Member Function Documentation

◆ InitializeElement()

bool WaveDataCache::InitializeElement ( const GraphicsDataCacheKey key,
WaveCacheElement element 
)
overrideprivatevirtual

Reimplemented from GraphicsDataCache< WaveCacheElement >.

Definition at line 291 of file WaveDataCache.cpp.

293{
296
297 element.AvailableColumns = 0;
298
299 int64_t firstSample = key.FirstSample;
300
301 const auto samplesPerColumn =
302 std::max(0.0, GetScaledSampleRate() / key.PixelsPerSecond);
303
304 const size_t elementSamplesCount =
305 samplesPerColumn * WaveDataCache::CacheElementWidth;
306 size_t processedSamples = 0;
307
308 const WaveCacheSampleBlock::Type blockType =
309 samplesPerColumn >= 64 * 1024 ?
311 (samplesPerColumn >= 256 ? WaveCacheSampleBlock::Type::MinMaxRMS256 :
313
314 if (blockType != mCachedBlock.DataType)
316
317 size_t columnIndex = 0;
318
319 for (; columnIndex < WaveDataCache::CacheElementWidth; ++columnIndex)
320 {
322
323 auto samplesLeft =
324 static_cast<size_t>(std::round(samplesPerColumn * (columnIndex + 1)) - std::round(samplesPerColumn * columnIndex));
325
326 while (samplesLeft != 0)
327 {
328 if (!mCachedBlock.ContainsSample(firstSample))
329 if (!mProvider(firstSample, blockType, mCachedBlock))
330 break;
331
332 summary = mCachedBlock.GetSummary(firstSample, samplesLeft, summary);
333 if(summary.SamplesCount == 0)
334 break;
335
336 samplesLeft -= summary.SamplesCount;
337 firstSample += summary.SamplesCount;
338 processedSamples += summary.SamplesCount;
339 }
340
341 if (summary.SamplesCount > 0)
342 {
343 auto& column = element.Data[columnIndex];
344
345 column.min = summary.Min;
346 column.max = summary.Max;
347
348 column.rms = std::sqrt(summary.SquaresSum / summary.SumItemsCount);
349 }
350
351 if (columnIndex > 0)
352 {
353 const auto prevColumn = element.Data[columnIndex - 1];
354 auto& column = element.Data[columnIndex];
355
356 bool updated = false;
357
358 if (prevColumn.min > column.max)
359 {
360 column.max = prevColumn.min;
361 updated = true;
362 }
363
364 if (prevColumn.max < column.min)
365 {
366 column.min = prevColumn.max;
367 updated = true;
368 }
369
370 if (updated)
371 column.rms = std::clamp(column.rms, column.min, column.max);
372 }
373
374 if (samplesLeft != 0)
375 {
376 ++columnIndex;
377 break;
378 }
379 }
380
381 element.AvailableColumns = columnIndex;
382 element.IsComplete = processedSamples == elementSamplesCount;
383
384 return processedSamples != 0;
385}
static const AudacityProject::AttachedObjects::RegisteredFactory key
@ WaveDataCache
Time required to access the data cache.
static Stopwatch CreateStopwatch(SectionID section) noexcept
Create a Stopwatch for the section specified.
static constexpr uint32_t CacheElementWidth
double GetScaledSampleRate() const noexcept
Returns the sample rate associated with cache.
WaveCacheSampleBlock mCachedBlock
fastfloat_really_inline void round(adjusted_mantissa &am, callback cb) noexcept
Definition: fast_float.h:2512
__finl float_x4 __vecc sqrt(const float_x4 &a)
bool IsComplete
Cache implementation is responsible to set this flag when all the data of the item is filled.
size_t AvailableColumns
Definition: WaveDataCache.h:86
Summary calculated over the requested range.
Definition: WaveDataCache.h:47
Summary GetSummary(int64_t from, size_t samplesCount, const Summary &initializer) const noexcept
void Reset() noexcept
Type
Type of the data of the request.
Definition: WaveDataCache.h:30
@ Samples
Each element of the resulting array is a sample.
bool ContainsSample(int64_t sampleIndex) const noexcept
Checks if sample is in the range represented by this block.

References WaveCacheElement::AvailableColumns, GraphicsDataCacheBase::CacheElementWidth, WaveCacheSampleBlock::ContainsSample(), FrameStatistics::CreateStopwatch(), WaveCacheElement::Data, WaveCacheSampleBlock::DataType, GraphicsDataCacheBase::GetScaledSampleRate(), WaveCacheSampleBlock::GetSummary(), GraphicsDataCacheElementBase::IsComplete, key, WaveCacheSampleBlock::Summary::Max, mCachedBlock, WaveCacheSampleBlock::Summary::Min, WaveCacheSampleBlock::MinMaxRMS256, WaveCacheSampleBlock::MinMaxRMS64k, mProvider, WaveCacheSampleBlock::Reset(), fast_float::round(), WaveCacheSampleBlock::Samples, WaveCacheSampleBlock::Summary::SamplesCount, staffpad::audio::simd::sqrt(), WaveCacheSampleBlock::Summary::SquaresSum, WaveCacheSampleBlock::Summary::SumItemsCount, and FrameStatistics::WaveDataCache.

Here is the call graph for this function:

Member Data Documentation

◆ mCachedBlock

WaveCacheSampleBlock WaveDataCache::mCachedBlock
private

Definition at line 106 of file WaveDataCache.h.

Referenced by InitializeElement().

◆ mProvider

DataProvider WaveDataCache::mProvider
private

Definition at line 104 of file WaveDataCache.h.

Referenced by InitializeElement().

◆ mStretchChangedSubscription

Observer::Subscription WaveDataCache::mStretchChangedSubscription
private

Definition at line 109 of file WaveDataCache.h.

◆ mWaveClip

const WaveClip& WaveDataCache::mWaveClip
private

Definition at line 108 of file WaveDataCache.h.


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