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