diff --git a/spectrogram.cpp b/spectrogram.cpp index 90255c1..36c95b6 100644 --- a/spectrogram.cpp +++ b/spectrogram.cpp @@ -97,7 +97,7 @@ void Spectrogram::paintEvent(QPaintEvent *event) float* Spectrogram::getTile(off_t tile) { - float *obj = fftCache.object(qMakePair(fftSize, tile)); + float *obj = fftCache.object(TileCacheKey(fftSize, zoomLevel, tile)); if (obj != 0) return obj; @@ -109,7 +109,7 @@ float* Spectrogram::getTile(off_t tile) sample += getStride(); ptr += fftSize; } - fftCache.insert(qMakePair(fftSize, tile), dest); + fftCache.insert(TileCacheKey(fftSize, zoomLevel, tile), dest); return dest; } @@ -222,3 +222,7 @@ QString Spectrogram::sampleToTime(off_t sample) int Spectrogram::linesPerTile() { return tileSize / fftSize; } + +uint qHash(const TileCacheKey &key, uint seed) { + return key.fftSize ^ key.zoomLevel ^ key.sample ^ seed; +} diff --git a/spectrogram.h b/spectrogram.h index cf748a2..190e4d4 100644 --- a/spectrogram.h +++ b/spectrogram.h @@ -9,6 +9,8 @@ static const double Tau = M_PI * 2.0; +class TileCacheKey; + class Spectrogram : public QWidget { Q_OBJECT @@ -39,7 +41,7 @@ private: FFT *fft = nullptr; std::unique_ptr window; fftwf_complex *lineBuffer = nullptr; - QCache, float> fftCache; + QCache fftCache; uint colormap[256]; int sampleRate; @@ -55,4 +57,24 @@ private: int sampleToLine(off_t sample); QString sampleToTime(off_t sample); int linesPerTile(); -}; \ No newline at end of file +}; + +class TileCacheKey { + +public: + TileCacheKey(int fftSize, int zoomLevel, off_t sample) { + this->fftSize = fftSize; + this->zoomLevel = zoomLevel; + this->sample = sample; + } + + bool operator==(const TileCacheKey &k2) const { + return (this->fftSize == k2.fftSize) && + (this->zoomLevel == k2.zoomLevel) && + (this->sample == k2.sample); + } + + int fftSize; + int zoomLevel; + off_t sample; +};