Audacity 3.2.0
SpectrumCache.h
Go to the documentation of this file.
1/**********************************************************************
2
3 Audacity: A Digital Audio Editor
4
5 @file SpectrumCache.h
6
7 Paul Licameli split from WaveClip.h
8
9*******************************************************************/
10
11#ifndef __AUDACITY_WAVECLIP_SPECTRUM_CACHE__
12#define __AUDACITY_WAVECLIP_SPECTRUM_CACHE__
13
14class sampleCount;
16class WaveClipChannel;
19
20#include <vector>
21#include "MemoryX.h"
22#include "WaveClip.h" // to inherit WaveClipListener
23
25
26class AUDACITY_DLL_API SpecCache {
27public:
28
29 // Make invalid cache
31 : algorithm(-1)
32 , spp(-1.0)
33 , start(-1.0)
34 , windowType(-1)
35 , frequencyGain(-1)
36 , dirty(-1)
37 {
38 }
39
41 {
42 }
43
44 bool Matches(
45 int dirty_, double samplesPerPixel,
46 const SpectrogramSettings& settings) const;
47
48 // Grow the cache while preserving the (possibly now invalid!) contents
49 void Grow(
50 size_t len_, SpectrogramSettings& settings, double samplesPerPixel,
51 double start /*relative to clip play start time*/);
52
53 // Calculate the dirty columns at the begin and end of the cache
54 void Populate(
56 int copyBegin, int copyEnd, size_t numPixels, double pixelsPerSecond);
57
58 size_t len { 0 }; // counts pixels, not samples
60 double spp; // samples per pixel
61 double leftTrim{ .0 };
62 double rightTrim{ .0 };
63 double start; // relative to clip start
65 size_t windowSize { 0 };
66 unsigned zeroPaddingFactor { 0 };
68 std::vector<float> freq;
69 std::vector<sampleCount> where;
70
71 int dirty;
72
73private:
74 // Calculate one column of the spectrum
75 bool CalculateOneSpectrum(
77 const int xx, double pixelsPerSecond, int lowerBoundX, int upperBoundX,
78 const std::vector<float>& gainFactors, float* __restrict scratch,
79 float* __restrict out) const;
80
81 mutable std::optional<AudioSegmentSampleView> mSampleCacheHolder;
82};
83
85public:
86 SpecPxCache(size_t cacheLen)
87 : len{ cacheLen }
88 , values{ len }
89 {
90 scaleType = 0;
91 range = gain = -1;
92 minFreq = maxFreq = -1;
93 }
94
95 size_t len;
97
99 int range;
100 int gain;
103};
104
106{
107 explicit WaveClipSpectrumCache(size_t nChannels);
108 ~WaveClipSpectrumCache() override;
109
110 std::unique_ptr<WaveClipListener> Clone() const override;
111
112 // Cache of values to colour pixels of Spectrogram - used by TrackArtist
113 std::vector<std::unique_ptr<SpecPxCache>> mSpecPxCaches;
114 std::vector<std::unique_ptr<SpecCache>> mSpecCaches;
115 int mDirty { 0 };
116
117 static WaveClipSpectrumCache &Get(const WaveChannelInterval &clip);
118
119 void MarkChanged() noexcept override; // NOFAIL-GUARANTEE
120 void Invalidate() override; // NOFAIL-GUARANTEE
121
123 // PRL:
124 // > only the 0th channel of sequence is really used
125 // > In the interim, this still works correctly for WideSampleSequence backed
126 // > by a right channel track, which always ignores its partner.
127 bool GetSpectrogram(const WaveChannelInterval &clip,
128 const float *&spectrogram,
129 SpectrogramSettings &spectrogramSettings,
130 const sampleCount *&where, size_t numPixels,
131 double t0 /*absolute time*/, double pixelsPerSecond);
132
133 void MakeStereo(WaveClipListener &&other, bool aligned) override;
134 void SwapChannels() override;
135 void Erase(size_t index) override;
136};
137
138#endif
static Settings & settings()
Definition: TrackInfo.cpp:51
int windowType
Definition: SpectrumCache.h:64
double start
Definition: SpectrumCache.h:63
double spp
Definition: SpectrumCache.h:60
std::vector< float > freq
Definition: SpectrumCache.h:68
int frequencyGain
Definition: SpectrumCache.h:67
std::vector< sampleCount > where
Definition: SpectrumCache.h:69
std::optional< AudioSegmentSampleView > mSampleCacheHolder
Definition: SpectrumCache.h:81
Floats values
Definition: SpectrumCache.h:96
SpecPxCache(size_t cacheLen)
Definition: SpectrumCache.h:86
Spectrogram settings, either for one track or as defaults.
WaveClipChannel(WaveClip &clip, size_t iChannel)
Definition: WaveClip.h:90
Positions or offsets within audio files need a wide type.
Definition: SampleCount.h:19
void MakeStereo(WaveClipListener &&other, bool aligned) override
std::vector< std::unique_ptr< SpecPxCache > > mSpecPxCaches
void SwapChannels() override
Default implementation does nothing.
bool GetSpectrogram(const WaveChannelInterval &clip, const float *&spectrogram, SpectrogramSettings &spectrogramSettings, const sampleCount *&where, size_t numPixels, double t0, double pixelsPerSecond)
static WaveClipSpectrumCache & Get(const WaveChannelInterval &clip)
~WaveClipSpectrumCache() override
std::unique_ptr< WaveClipListener > Clone() const override
void MarkChanged() noexcept override
void Erase(size_t index) override
WaveClipSpectrumCache(size_t nChannels)
void Invalidate() override
std::vector< std::unique_ptr< SpecCache > > mSpecCaches