annotate data/fft/FFTCache.h @ 158:74abef65711b

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