comparison base/FFTCache.h @ 90:c4e163f911dd

* Switch spectrogram layer over to using the new rudimentary disk-backed FFT cache
author Chris Cannam
date Wed, 03 May 2006 14:26:26 +0000
parents 6a1803d578e0
children 0c19e50bad7c
comparison
equal deleted inserted replaced
89:6a1803d578e0 90:c4e163f911dd
14 */ 14 */
15 15
16 #ifndef _FFT_CACHE_H_ 16 #ifndef _FFT_CACHE_H_
17 #define _FFT_CACHE_H_ 17 #define _FFT_CACHE_H_
18 18
19 #include <QColor> 19 #include <cstdlib>
20 #include <cmath>
21
20 #include <stdint.h> 22 #include <stdint.h>
21
22 #define M_PI (3.14159265358979232846)
23 23
24 class FFTCacheBase 24 class FFTCacheBase
25 { 25 {
26 public: 26 public:
27 virtual ~FFTCacheBase() { } 27 virtual ~FFTCacheBase() { }
34 34
35 virtual float getMagnitudeAt(size_t x, size_t y) const = 0; 35 virtual float getMagnitudeAt(size_t x, size_t y) const = 0;
36 virtual float getNormalizedMagnitudeAt(size_t x, size_t y) const = 0; 36 virtual float getNormalizedMagnitudeAt(size_t x, size_t y) const = 0;
37 virtual float getPhaseAt(size_t x, size_t y) const = 0; 37 virtual float getPhaseAt(size_t x, size_t y) const = 0;
38 38
39 virtual bool isLocalPeak(size_t x, size_t y) const = 0;
40 virtual bool isOverThreshold(size_t x, size_t y, float threshold) const = 0;
41
42 virtual void setNormalizationFactor(size_t x, float factor) = 0; 39 virtual void setNormalizationFactor(size_t x, float factor) = 0;
43 virtual void setMagnitudeAt(size_t x, size_t y, float mag) = 0; 40 virtual void setMagnitudeAt(size_t x, size_t y, float mag) = 0;
44 virtual void setNormalizedMagnitudeAt(size_t x, size_t y, float norm) = 0; 41 virtual void setNormalizedMagnitudeAt(size_t x, size_t y, float norm) = 0;
45 virtual void setPhaseAt(size_t x, size_t y, float phase) = 0; 42 virtual void setPhaseAt(size_t x, size_t y, float phase) = 0;
46 43
47 virtual QColor getColour(unsigned char index) const = 0; 44 virtual void setColumnAt(size_t x, float *mags, float *phases, float factor) = 0;
48 virtual void setColour(unsigned char index, QColor colour) = 0; 45
46 bool isLocalPeak(size_t x, size_t y) const {
47 float mag = getMagnitudeAt(x, y);
48 if (y > 0 && mag < getMagnitudeAt(x, y - 1)) return false;
49 if (y < getHeight()-1 && mag < getMagnitudeAt(x, y + 1)) return false;
50 return true;
51 }
52 bool isOverThreshold(size_t x, size_t y, float threshold) const {
53 return getMagnitudeAt(x, y) > threshold;
54 }
49 55
50 protected: 56 protected:
51 FFTCacheBase() { } 57 FFTCacheBase() { }
52 }; 58 };
53 59
94 virtual float getPhaseAt(size_t x, size_t y) const { 100 virtual float getPhaseAt(size_t x, size_t y) const {
95 int16_t i = (int16_t)m_phase[x][y]; 101 int16_t i = (int16_t)m_phase[x][y];
96 return (float(i) / 32767.0) * M_PI; 102 return (float(i) / 32767.0) * M_PI;
97 } 103 }
98 104
99 virtual bool isLocalPeak(size_t x, size_t y) const {
100 if (y > 0 && m_magnitude[x][y] < m_magnitude[x][y-1]) return false;
101 if (y < m_height-1 && m_magnitude[x][y] < m_magnitude[x][y+1]) return false;
102 return true;
103 }
104
105 virtual bool isOverThreshold(size_t x, size_t y, float threshold) const {
106 if (threshold == 0.0) return true;
107 return getMagnitudeAt(x, y) > threshold;
108 }
109
110 virtual void setNormalizationFactor(size_t x, float factor) { 105 virtual void setNormalizationFactor(size_t x, float factor) {
111 if (x < m_width) m_factor[x] = factor; 106 if (x < m_width) m_factor[x] = factor;
112 } 107 }
113 108
114 virtual void setMagnitudeAt(size_t x, size_t y, float mag) { 109 virtual void setMagnitudeAt(size_t x, size_t y, float mag) {
127 if (x < m_width && y < m_height) { 122 if (x < m_width && y < m_height) {
128 m_phase[x][y] = uint16_t(int16_t((phase * 32767) / M_PI)); 123 m_phase[x][y] = uint16_t(int16_t((phase * 32767) / M_PI));
129 } 124 }
130 } 125 }
131 126
132 virtual QColor getColour(unsigned char index) const { 127 virtual void setColumnAt(size_t x, float *mags, float *phases, float factor) {
133 return m_colours[index]; 128 setNormalizationFactor(x, factor);
129 for (size_t y = 0; y < m_height; ++y) {
130 setMagnitudeAt(x, y, mags[y]);
131 setPhaseAt(x, y, phases[y]);
132 }
134 } 133 }
135 134
136 virtual void setColour(unsigned char index, QColor colour) {
137 m_colours[index] = colour;
138 }
139
140 private: 135 private:
141 size_t m_width; 136 size_t m_width;
142 size_t m_height; 137 size_t m_height;
143 uint16_t **m_magnitude; 138 uint16_t **m_magnitude;
144 uint16_t **m_phase; 139 uint16_t **m_phase;
145 float *m_factor; 140 float *m_factor;
146 QColor m_colours[256]; 141
147
148 void resize(uint16_t **&, size_t, size_t); 142 void resize(uint16_t **&, size_t, size_t);
149 }; 143 };
150 144
151 #endif 145 #endif