| 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 |
|
| 14 |
class sampleCount; |
| 15 |
class SpectrogramSettings; |
| 16 |
class WaveClipChannel; |
| 17 |
using WaveChannelInterval = WaveClipChannel; |
| 18 |
class WideSampleSequence; |
| 19 |
|
| 20 |
#include <vector> |
| 21 |
#include "MemoryX.h" |
| 22 |
#include "WaveClip.h" // to inherit WaveClipListener |
| 23 |
|
| 24 |
using Floats = ArrayOf<float>; |
| 25 |
|
| 26 |
class AUDACITY_DLL_API SpecCache { |
| 27 |
public: |
| 28 |
|
| 29 |
// Make invalid cache |
| 30 |
SpecCache() |
| 31 |
: algorithm(-1) |
| 32 |
, spp(-1.0) |
| 33 |
, start(-1.0) |
| 34 |
, windowType(-1) |
| 35 |
, frequencyGain(-1) |
| 36 |
, dirty(-1) |
| 37 |
{ |
| 38 |
} |
| 39 |
|
| 40 |
~SpecCache() |
| 41 |
{ |
| 42 |
} |
| 43 |
|
| 44 |
bool isWaveletAnalysis(const SpectrogramSettings& settings) const; |
| 45 |
|
| 46 |
bool Matches( |
| 47 |
int dirty_, double samplesPerPixel, |
| 48 |
const SpectrogramSettings& settings) const; |
| 49 |
|
| 50 |
// Grow the cache while preserving the (possibly now invalid!) contents |
| 51 |
void Grow( |
| 52 |
size_t len_, SpectrogramSettings& settings, double samplesPerPixel, |
| 53 |
double start); /*relative to clip play start time*/ |
| 54 |
|
| 55 |
// Calculate the dirty columns at the begin and end of the cache |
| 56 |
void Populate( |
| 57 |
const SpectrogramSettings& settings, const WaveChannelInterval& clip, |
| 58 |
int copyBegin, int copyEnd, size_t numPixels, double pixelsPerSecond); |
| 59 |
|
| 60 |
size_t len { 0 }; // counts pixels, not samples |
| 61 |
int algorithm; |
| 62 |
double spp; // samples per pixel |
| 63 |
double leftTrim{ .0 }; |
| 64 |
double rightTrim{ .0 }; |
| 65 |
double start; // relative to clip start |
| 66 |
int windowType; |
| 67 |
size_t windowSize { 0 }; |
| 68 |
unsigned zeroPaddingFactor { 0 }; |
| 69 |
int frequencyGain; |
| 70 |
std::vector<float> freq; |
| 71 |
std::vector<sampleCount> where; |
| 72 |
|
| 73 |
int dirty; |
| 74 |
|
| 75 |
private: |
| 76 |
// Calculate one column of the spectrum |
| 77 |
bool CalculateOneSpectrum( |
| 78 |
const SpectrogramSettings& settings, const WaveChannelInterval &clip, |
| 79 |
const int xx, double pixelsPerSecond, int lowerBoundX, int upperBoundX, |
| 80 |
const std::vector<float>& gainFactors, float* __restrict scratch, |
| 81 |
float* __restrict out) const; |
| 82 |
|
| 83 |
bool CalculateOneWaveletSpectrum( |
| 84 |
const SpectrogramSettings& settings, const WaveChannelInterval &clip, |
| 85 |
const int xx, double pixelsPerSecond, int lowerBoundX, int upperBoundX, |
| 86 |
const std::vector<float>& gainFactors, |
| 87 |
float* __restrict out) const; |
| 88 |
mutable std::optional<AudioSegmentSampleView> mSampleCacheHolder; |
| 89 |
}; |
| 90 |
|
| 91 |
class SpecPxCache { |
| 92 |
public: |
| 93 |
SpecPxCache(size_t cacheLen) |
| 94 |
: len{ cacheLen } |
| 95 |
, values{ len } |
| 96 |
{ |
| 97 |
scaleType = 0; |
| 98 |
range = gain = -1; |
| 99 |
minFreq = maxFreq = -1; |
| 100 |
} |
| 101 |
|
| 102 |
size_t len; |
| 103 |
Floats values; |
| 104 |
|
| 105 |
int scaleType; |
| 106 |
int range; |
| 107 |
int gain; |
| 108 |
int minFreq; |
| 109 |
int maxFreq; |
| 110 |
}; |
| 111 |
|
| 112 |
struct WaveClipSpectrumCache final : WaveClipListener |
| 113 |
{ |
| 114 |
explicit WaveClipSpectrumCache(size_t nChannels); |
| 115 |
~WaveClipSpectrumCache() override; |
| 116 |
|
| 117 |
std::unique_ptr<WaveClipListener> Clone() const override; |
| 118 |
|
| 119 |
// Cache of values to colour pixels of Spectrogram - used by TrackArtist |
| 120 |
std::vector<std::unique_ptr<SpecPxCache>> mSpecPxCaches; |
| 121 |
std::vector<std::unique_ptr<SpecCache>> mSpecCaches; |
| 122 |
int mDirty { 0 }; |
| 123 |
|
| 124 |
static WaveClipSpectrumCache &Get(const WaveChannelInterval &clip); |
| 125 |
|
| 126 |
void MarkChanged() noexcept override; // NOFAIL-GUARANTEE |
| 127 |
void Invalidate() override; // NOFAIL-GUARANTEE |
| 128 |
|
| 129 |
/** Getting high-level data for screen display */ |
| 130 |
// PRL: |
| 131 |
// > only the 0th channel of sequence is really used |
| 132 |
// > In the interim, this still works correctly for WideSampleSequence backed |
| 133 |
// > by a right channel track, which always ignores its partner. |
| 134 |
bool GetSpectrogram(const WaveChannelInterval &clip, |
| 135 |
const float *&spectrogram, |
| 136 |
SpectrogramSettings &spectrogramSettings, |
| 137 |
const sampleCount *&where, size_t numPixels, |
| 138 |
double t0 /*absolute time*/, double pixelsPerSecond); |
| 139 |
|
| 140 |
void MakeStereo(WaveClipListener &&other, bool aligned) override; |
| 141 |
void SwapChannels() override; |
| 142 |
void Erase(size_t index) override; |
| 143 |
}; |
| 144 |
|
| 145 |
#endif |