comparison base/FFTCache.h @ 87:7de62a884810

* Start factoring out the spectrogram's FFT cache into a separate set of classes that will permit a choice of disk or memory cache strategies
author Chris Cannam
date Tue, 02 May 2006 12:27:41 +0000
parents
children 6a1803d578e0
comparison
equal deleted inserted replaced
86:e076e676439b 87:7de62a884810
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2
3 /*
4 Sonic Visualiser
5 An audio file viewer and annotation editor.
6 Centre for Digital Music, Queen Mary, University of London.
7 This file copyright 2006 Chris Cannam.
8
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License as
11 published by the Free Software Foundation; either version 2 of the
12 License, or (at your option) any later version. See the file
13 COPYING included with this distribution for more information.
14 */
15
16 #ifndef _FFT_CACHE_H_
17 #define _FFT_CACHE_H_
18
19 #include <QColor>
20 #include <stdint.h>
21
22 class FFTCacheBase
23 {
24 public:
25 virtual ~FFTCacheBase() { }
26
27 virtual size_t getWidth() const = 0;
28 virtual size_t getHeight() const = 0;
29
30 virtual void resize(size_t width, size_t height) = 0;
31 virtual void reset() = 0; // zero-fill or 1-fill as appropriate without changing size
32
33 virtual float getMagnitudeAt(size_t x, size_t y) const = 0;
34 virtual float getNormalizedMagnitudeAt(size_t x, size_t y) const = 0;
35 virtual float getPhaseAt(size_t x, size_t y) const = 0;
36
37 virtual bool isLocalPeak(size_t x, size_t y) const = 0;
38 virtual bool isOverThreshold(size_t x, size_t y, float threshold) const = 0;
39
40 virtual void setNormalizationFactor(size_t x, float factor) = 0;
41 virtual void setMagnitudeAt(size_t x, size_t y, float mag) = 0;
42 virtual void setNormalizedMagnitudeAt(size_t x, size_t y, float norm) = 0;
43 virtual void setPhaseAt(size_t x, size_t y, float phase) = 0;
44
45 virtual QColor getColour(unsigned char index) const = 0;
46 virtual void setColour(unsigned char index, QColor colour) = 0;
47
48 protected:
49 FFTCacheBase() { }
50 };
51
52
53 /**
54 * For the in-memory FFT cache, we would like to cache magnitude with
55 * enough resolution to have gain applied afterwards and determine
56 * whether something is a peak or not, and also cache phase rather
57 * than only phase-adjusted frequency so that we don't have to
58 * recalculate if switching between phase and magnitude displays. At
59 * the same time, we don't want to take up too much memory. It's not
60 * expected to be accurate enough to be used as input for DSP or
61 * resynthesis code.
62 *
63 * This implies probably 16 bits for a normalized magnitude and at
64 * most 16 bits for phase.
65 *
66 * Each column's magnitudes are expected to be stored normalized
67 * to [0,1] with respect to the column, so the normalization
68 * factor should be calculated before all values in a column, and
69 * set appropriately.
70 */
71
72 class FFTMemoryCache : public FFTCacheBase
73 {
74 public:
75 FFTMemoryCache(); // of size zero, call resize() before using
76 virtual ~FFTMemoryCache();
77
78 virtual size_t getWidth() const { return m_width; }
79 virtual size_t getHeight() const { return m_height; }
80
81 virtual void resize(size_t width, size_t height);
82 virtual void reset(); // zero-fill or 1-fill as appropriate without changing size
83
84 virtual float getMagnitudeAt(size_t x, size_t y) const {
85 return getNormalizedMagnitudeAt(x, y) * m_factor[x];
86 }
87
88 virtual float getNormalizedMagnitudeAt(size_t x, size_t y) const {
89 return float(m_magnitude[x][y]) / 65535.0;
90 }
91
92 virtual float getPhaseAt(size_t x, size_t y) const {
93 int16_t i = (int16_t)m_phase[x][y];
94 return (float(i) / 32767.0) * M_PI;
95 }
96
97 virtual bool isLocalPeak(size_t x, size_t y) const {
98 if (y > 0 && m_magnitude[x][y] < m_magnitude[x][y-1]) return false;
99 if (y < m_height-1 && m_magnitude[x][y] < m_magnitude[x][y+1]) return false;
100 return true;
101 }
102
103 virtual bool isOverThreshold(size_t x, size_t y, float threshold) const {
104 if (threshold == 0.0) return true;
105 return getMagnitudeAt(x, y) > threshold;
106 }
107
108 virtual void setNormalizationFactor(size_t x, float factor) {
109 if (x < m_width) m_factor[x] = factor;
110 }
111
112 virtual void setMagnitudeAt(size_t x, size_t y, float mag) {
113 // norm factor must already be set
114 setNormalizedMagnitudeAt(x, y, mag / m_factor[x]);
115 }
116
117 virtual void setNormalizedMagnitudeAt(size_t x, size_t y, float norm) {
118 if (x < m_width && y < m_height) {
119 m_magnitude[x][y] = uint16_t(norm * 65535.0);
120 }
121 }
122
123 virtual void setPhaseAt(size_t x, size_t y, float phase) {
124 // phase in range -pi -> pi
125 if (x < m_width && y < m_height) {
126 m_phase[x][y] = uint16_t(int16_t((phase * 32767) / M_PI));
127 }
128 }
129
130 virtual QColor getColour(unsigned char index) const {
131 return m_colours[index];
132 }
133
134 virtual void setColour(unsigned char index, QColor colour) {
135 m_colours[index] = colour;
136 }
137
138 private:
139 size_t m_width;
140 size_t m_height;
141 uint16_t **m_magnitude;
142 uint16_t **m_phase;
143 float *m_factor;
144 QColor m_colours[256];
145
146 void resize(uint16_t **&, size_t, size_t);
147 };
148
149 #endif