Debian Codegraph @

libraries/lib-wave-track-paint/waveform/WaveDataCache.h

raw
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*!********************************************************************
3
4 Audacity: A Digital Audio Editor
5
6 WaveDataCache.h
7
8 Dmitry Vedenko
9
10 **********************************************************************/
11 #pragma once
12
13 #include <array>
14 #include <cstdint>
15 #include <numeric>
16 #include <vector>
17 #include <functional>
18
19 #include "GraphicsDataCache.h"
20 #include "WaveData.h"
21 #include "Observer.h"
22
23 class WaveClip;
24
25 //! Helper structure used to transfer the data between the data and graphics layers
26 struct WAVE_TRACK_PAINT_API WaveCacheSampleBlock final
27 {
28 //! Type of the data of the request
29 enum class Type
30 {
31 //! Each element of the resulting array is a sample
32 Samples,
33 /*!
34 * Each element of the resulting array is a tuple (min, max, rms)
35 * calculated over 256 samples.
36 */
37 MinMaxRMS256,
38 /*!
39 * Each element of the resulting array is a tuple (min, max, rms)
40 * calculated over 256 samples.
41 */
42 MinMaxRMS64k,
43 };
44
45 //! Summary calculated over the requested range
46 struct Summary final
47 {
48 size_t SamplesCount { 0 };
49
50 float Min { std::numeric_limits<float>::infinity() };
51 float Max { -std::numeric_limits<float>::infinity() };
52
53 double SquaresSum { 0.0f };
54
55 size_t SumItemsCount { 0 };
56 };
57
58 Type DataType { Type::Samples };
59 int64_t FirstSample { 0 };
60 size_t NumSamples { 0 };
61
62 //! Checks if sample is in the range represented by this block
63 bool ContainsSample(int64_t sampleIndex) const noexcept;
64
65 //! Gets a pointer to a data buffer enough to store floatsCount floats
66 float* GetWritePointer(size_t floatsCount);
67
68 void Reset() noexcept;
69 private:
70 Summary GetSummary(
71 int64_t from, size_t samplesCount,
72 const Summary& initializer) const noexcept;
73
74 std::vector<float> mData;
75
76 friend class WaveDataCache;
77 };
78
79 //! An element of a cache that contains the waveform data
80 struct WAVE_TRACK_PAINT_API WaveCacheElement final : GraphicsDataCacheElementBase
81 {
82 using Columns =
83 std::array<WaveDisplayColumn, GraphicsDataCacheBase::CacheElementWidth>;
84
85 Columns Data;
86 size_t AvailableColumns { 0 };
87
88 void Smooth(GraphicsDataCacheElementBase* prevElement) override;
89 };
90
91 //! Cache that contains the waveform data
92 class WAVE_TRACK_PAINT_API WaveDataCache final :
93 public GraphicsDataCache<WaveCacheElement>
94 {
95 public:
96 using DataProvider = std::function<bool (int64_t requiredSample, WaveCacheSampleBlock::Type dataType, WaveCacheSampleBlock& block)>;
97
98 WaveDataCache(const WaveClip& waveClip, int channelIndex);
99
100 private:
101 bool InitializeElement(
102 const GraphicsDataCacheKey& key, WaveCacheElement& element) override;
103
104 DataProvider mProvider;
105
106 WaveCacheSampleBlock mCachedBlock;
107
108 const WaveClip& mWaveClip;
109 Observer::Subscription mStretchChangedSubscription;
110 };