Chris@87: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@87: Chris@87: /* Chris@87: Sonic Visualiser Chris@87: An audio file viewer and annotation editor. Chris@87: Centre for Digital Music, Queen Mary, University of London. Chris@87: This file copyright 2006 Chris Cannam. Chris@87: Chris@87: This program is free software; you can redistribute it and/or Chris@87: modify it under the terms of the GNU General Public License as Chris@87: published by the Free Software Foundation; either version 2 of the Chris@87: License, or (at your option) any later version. See the file Chris@87: COPYING included with this distribution for more information. Chris@87: */ Chris@87: Chris@87: #ifndef _FFT_CACHE_H_ Chris@87: #define _FFT_CACHE_H_ Chris@87: Chris@90: #include Chris@90: #include Chris@90: Chris@87: #include Chris@87: Chris@129: class FFTCache Chris@87: { Chris@87: public: Chris@129: virtual ~FFTCache() { } Chris@87: Chris@87: virtual size_t getWidth() const = 0; Chris@87: virtual size_t getHeight() const = 0; Chris@87: Chris@87: virtual void resize(size_t width, size_t height) = 0; Chris@87: virtual void reset() = 0; // zero-fill or 1-fill as appropriate without changing size Chris@87: Chris@87: virtual float getMagnitudeAt(size_t x, size_t y) const = 0; Chris@87: virtual float getNormalizedMagnitudeAt(size_t x, size_t y) const = 0; Chris@87: virtual float getPhaseAt(size_t x, size_t y) const = 0; Chris@87: Chris@128: virtual void getValuesAt(size_t x, size_t y, float &real, float &imaginary) const = 0; Chris@128: Chris@113: virtual bool haveSetColumnAt(size_t x) const = 0; Chris@128: Chris@128: // may modify argument arrays Chris@90: virtual void setColumnAt(size_t x, float *mags, float *phases, float factor) = 0; Chris@90: Chris@128: // may modify argument arrays Chris@128: virtual void setColumnAt(size_t x, float *reals, float *imags) = 0; Chris@128: Chris@129: virtual void suspend() { } Chris@129: Chris@87: protected: Chris@129: FFTCache() { } Chris@87: }; Chris@87: Chris@87: Chris@87: /** Chris@87: * For the in-memory FFT cache, we would like to cache magnitude with Chris@87: * enough resolution to have gain applied afterwards and determine Chris@87: * whether something is a peak or not, and also cache phase rather Chris@87: * than only phase-adjusted frequency so that we don't have to Chris@87: * recalculate if switching between phase and magnitude displays. At Chris@87: * the same time, we don't want to take up too much memory. It's not Chris@87: * expected to be accurate enough to be used as input for DSP or Chris@87: * resynthesis code. Chris@87: * Chris@87: * This implies probably 16 bits for a normalized magnitude and at Chris@87: * most 16 bits for phase. Chris@87: * Chris@87: * Each column's magnitudes are expected to be stored normalized Chris@87: * to [0,1] with respect to the column, so the normalization Chris@87: * factor should be calculated before all values in a column, and Chris@87: * set appropriately. Chris@87: */ Chris@87: Chris@129: class FFTMemoryCache : public FFTCache Chris@87: { Chris@87: public: Chris@87: FFTMemoryCache(); // of size zero, call resize() before using Chris@87: virtual ~FFTMemoryCache(); Chris@87: Chris@87: virtual size_t getWidth() const { return m_width; } Chris@87: virtual size_t getHeight() const { return m_height; } Chris@87: Chris@87: virtual void resize(size_t width, size_t height); Chris@87: virtual void reset(); // zero-fill or 1-fill as appropriate without changing size Chris@87: Chris@87: virtual float getMagnitudeAt(size_t x, size_t y) const { Chris@87: return getNormalizedMagnitudeAt(x, y) * m_factor[x]; Chris@87: } Chris@87: Chris@87: virtual float getNormalizedMagnitudeAt(size_t x, size_t y) const { Chris@87: return float(m_magnitude[x][y]) / 65535.0; Chris@87: } Chris@87: Chris@87: virtual float getPhaseAt(size_t x, size_t y) const { Chris@87: int16_t i = (int16_t)m_phase[x][y]; Chris@87: return (float(i) / 32767.0) * M_PI; Chris@87: } Chris@87: Chris@128: virtual void getValuesAt(size_t x, size_t y, float &real, float &imag) const { Chris@128: float mag = getMagnitudeAt(x, y); Chris@128: float phase = getPhaseAt(x, y); Chris@128: real = mag * cosf(phase); Chris@128: imag = mag * sinf(phase); Chris@128: } Chris@128: Chris@87: virtual void setNormalizationFactor(size_t x, float factor) { Chris@87: if (x < m_width) m_factor[x] = factor; Chris@87: } Chris@87: Chris@87: virtual void setMagnitudeAt(size_t x, size_t y, float mag) { Chris@87: // norm factor must already be set Chris@87: setNormalizedMagnitudeAt(x, y, mag / m_factor[x]); Chris@87: } Chris@87: Chris@87: virtual void setNormalizedMagnitudeAt(size_t x, size_t y, float norm) { Chris@87: if (x < m_width && y < m_height) { Chris@87: m_magnitude[x][y] = uint16_t(norm * 65535.0); Chris@87: } Chris@87: } Chris@87: Chris@87: virtual void setPhaseAt(size_t x, size_t y, float phase) { Chris@87: // phase in range -pi -> pi Chris@87: if (x < m_width && y < m_height) { Chris@87: m_phase[x][y] = uint16_t(int16_t((phase * 32767) / M_PI)); Chris@87: } Chris@87: } Chris@87: Chris@113: virtual bool haveSetColumnAt(size_t x) const { Chris@108: return true; Chris@108: } Chris@108: Chris@90: virtual void setColumnAt(size_t x, float *mags, float *phases, float factor) { Chris@90: setNormalizationFactor(x, factor); Chris@90: for (size_t y = 0; y < m_height; ++y) { Chris@90: setMagnitudeAt(x, y, mags[y]); Chris@90: setPhaseAt(x, y, phases[y]); Chris@90: } Chris@87: } Chris@90: Chris@128: virtual void setColumnAt(size_t x, float *reals, float *imags); Chris@128: Chris@87: private: Chris@87: size_t m_width; Chris@87: size_t m_height; Chris@87: uint16_t **m_magnitude; Chris@87: uint16_t **m_phase; Chris@87: float *m_factor; Chris@90: Chris@87: void resize(uint16_t **&, size_t, size_t); Chris@87: }; Chris@87: Chris@87: #endif