Chris@159: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
Chris@159: 
Chris@159: /*
Chris@159:     Sonic Visualiser
Chris@159:     An audio file viewer and annotation editor.
Chris@159:     Centre for Digital Music, Queen Mary, University of London.
Chris@159:     This file copyright 2006 Chris Cannam.
Chris@159:     
Chris@159:     This program is free software; you can redistribute it and/or
Chris@159:     modify it under the terms of the GNU General Public License as
Chris@159:     published by the Free Software Foundation; either version 2 of the
Chris@159:     License, or (at your option) any later version.  See the file
Chris@159:     COPYING included with this distribution for more information.
Chris@159: */
Chris@159: 
Chris@159: #ifndef _FFT_MEMORY_CACHE_H_
Chris@159: #define _FFT_MEMORY_CACHE_H_
Chris@159: 
Chris@537: #include "FFTCacheReader.h"
Chris@537: #include "FFTCacheWriter.h"
Chris@537: #include "FFTCacheStorageType.h"
Chris@159: #include "base/ResizeableBitset.h"
Chris@408: #include "base/Profiler.h"
Chris@159: 
Chris@548: #include <QReadWriteLock>
Chris@537: 
Chris@159: /**
Chris@253:  * In-memory FFT cache.  For this we want to cache magnitude with
Chris@159:  * enough resolution to have gain applied afterwards and determine
Chris@159:  * whether something is a peak or not, and also cache phase rather
Chris@159:  * than only phase-adjusted frequency so that we don't have to
Chris@159:  * recalculate if switching between phase and magnitude displays.  At
Chris@159:  * the same time, we don't want to take up too much memory.  It's not
Chris@159:  * expected to be accurate enough to be used as input for DSP or
Chris@159:  * resynthesis code.
Chris@159:  *
Chris@159:  * This implies probably 16 bits for a normalized magnitude and at
Chris@159:  * most 16 bits for phase.
Chris@159:  *
Chris@159:  * Each column's magnitudes are expected to be stored normalized
Chris@159:  * to [0,1] with respect to the column, so the normalization
Chris@159:  * factor should be calculated before all values in a column, and
Chris@159:  * set appropriately.
Chris@159:  */
Chris@159: 
Chris@537: class FFTMemoryCache : public FFTCacheReader, public FFTCacheWriter
Chris@159: {
Chris@159: public:
Chris@537:     FFTMemoryCache(FFTCache::StorageType storageType,
Chris@537:                    size_t width, size_t height);
Chris@537:     ~FFTMemoryCache();
Chris@159: 	
Chris@537:     size_t getWidth() const { return m_width; }
Chris@537:     size_t getHeight() const { return m_height; }
Chris@159: 	
Chris@537:     float getMagnitudeAt(size_t x, size_t y) const {
Chris@537:         if (m_storageType == FFTCache::Rectangular) {
Chris@408:             Profiler profiler("FFTMemoryCache::getMagnitudeAt: cart to polar");
Chris@509:             return sqrtf(m_freal[x][y] * m_freal[x][y] +
Chris@509:                          m_fimag[x][y] * m_fimag[x][y]);
Chris@334:         } else {
Chris@334:             return getNormalizedMagnitudeAt(x, y) * m_factor[x];
Chris@334:         }
Chris@159:     }
Chris@159:     
Chris@537:     float getNormalizedMagnitudeAt(size_t x, size_t y) const {
Chris@537:         if (m_storageType == FFTCache::Rectangular) return getMagnitudeAt(x, y) / m_factor[x];
Chris@537:         else if (m_storageType == FFTCache::Polar) return m_fmagnitude[x][y];
Chris@264:         else return float(m_magnitude[x][y]) / 65535.0;
Chris@159:     }
Chris@159:     
Chris@537:     float getMaximumMagnitudeAt(size_t x) const {
Chris@159:         return m_factor[x];
Chris@159:     }
Chris@159:     
Chris@537:     float getPhaseAt(size_t x, size_t y) const {
Chris@537:         if (m_storageType == FFTCache::Rectangular) {
Chris@408:             Profiler profiler("FFTMemoryCache::getValuesAt: cart to polar");
Chris@334:             return atan2f(m_fimag[x][y], m_freal[x][y]);
Chris@537:         } else if (m_storageType == FFTCache::Polar) {
Chris@334:             return m_fphase[x][y];
Chris@334:         } else {
Chris@334:             int16_t i = (int16_t)m_phase[x][y];
Chris@334:             return (float(i) / 32767.0) * M_PI;
Chris@334:         }
Chris@159:     }
Chris@159:     
Chris@537:     void getValuesAt(size_t x, size_t y, float &real, float &imag) const {
Chris@537:         if (m_storageType == FFTCache::Rectangular) {
Chris@334:             real = m_freal[x][y];
Chris@334:             imag = m_fimag[x][y];
Chris@334:         } else {
Chris@408:             Profiler profiler("FFTMemoryCache::getValuesAt: polar to cart");
Chris@334:             float mag = getMagnitudeAt(x, y);
Chris@334:             float phase = getPhaseAt(x, y);
Chris@334:             real = mag * cosf(phase);
Chris@334:             imag = mag * sinf(phase);
Chris@334:         }
Chris@159:     }
Chris@159: 
Chris@537:     void getMagnitudesAt(size_t x, float *values, size_t minbin, size_t count, size_t step) const
Chris@509:     {
Chris@537:         if (m_storageType == FFTCache::Rectangular) {
Chris@509:             for (size_t i = 0; i < count; ++i) {
Chris@509:                 size_t y = i * step + minbin;
Chris@509:                 values[i] = sqrtf(m_freal[x][y] * m_freal[x][y] +
Chris@509:                                   m_fimag[x][y] * m_fimag[x][y]);
Chris@509:             }
Chris@537:         } else if (m_storageType == FFTCache::Polar) {
Chris@509:             for (size_t i = 0; i < count; ++i) {
Chris@509:                 size_t y = i * step + minbin;
Chris@509:                 values[i] = m_fmagnitude[x][y] * m_factor[x];
Chris@509:             }
Chris@509:         } else {
Chris@509:             for (size_t i = 0; i < count; ++i) {
Chris@509:                 size_t y = i * step + minbin;
Chris@509:                 values[i] = (float(m_magnitude[x][y]) * m_factor[x]) / 65535.0;
Chris@509:             }
Chris@509:         }
Chris@509:     }
Chris@509: 
Chris@537:     bool haveSetColumnAt(size_t x) const {
Chris@548:         m_colsetLock.lockForRead();
Chris@537:         bool have = m_colset.get(x);
Chris@548:         m_colsetLock.unlock();
Chris@537:         return have;
Chris@334:     }
Chris@334: 
Chris@537:     void setColumnAt(size_t x, float *mags, float *phases, float factor);
Chris@334: 
Chris@537:     void setColumnAt(size_t x, float *reals, float *imags);
Chris@334: 
Chris@537:     void allColumnsWritten() { } 
Chris@334: 
Chris@537:     static size_t getCacheSize(size_t width, size_t height,
Chris@537:                                FFTCache::StorageType type);
Chris@537: 
Chris@537:     FFTCache::StorageType getStorageType() const { return m_storageType; }
Chris@359: 
Chris@334: private:
Chris@334:     size_t m_width;
Chris@334:     size_t m_height;
Chris@334:     uint16_t **m_magnitude;
Chris@334:     uint16_t **m_phase;
Chris@334:     float **m_fmagnitude;
Chris@334:     float **m_fphase;
Chris@334:     float **m_freal;
Chris@334:     float **m_fimag;
Chris@334:     float *m_factor;
Chris@537:     FFTCache::StorageType m_storageType;
Chris@334:     ResizeableBitset m_colset;
Chris@548:     mutable QReadWriteLock m_colsetLock;
Chris@334: 
Chris@537:     void initialise();
Chris@537: 
Chris@537:     void setNormalizationFactor(size_t x, float factor) {
Chris@159:         if (x < m_width) m_factor[x] = factor;
Chris@159:     }
Chris@159:     
Chris@537:     void setMagnitudeAt(size_t x, size_t y, float mag) {
Chris@159:          // norm factor must already be set
Chris@159:         setNormalizedMagnitudeAt(x, y, mag / m_factor[x]);
Chris@159:     }
Chris@159:     
Chris@537:     void setNormalizedMagnitudeAt(size_t x, size_t y, float norm) {
Chris@159:         if (x < m_width && y < m_height) {
Chris@537:             if (m_storageType == FFTCache::Polar) m_fmagnitude[x][y] = norm;
Chris@264:             else m_magnitude[x][y] = uint16_t(norm * 65535.0);
Chris@159:         }
Chris@159:     }
Chris@159:     
Chris@537:     void setPhaseAt(size_t x, size_t y, float phase) {
Chris@159:         // phase in range -pi -> pi
Chris@159:         if (x < m_width && y < m_height) {
Chris@537:             if (m_storageType == FFTCache::Polar) m_fphase[x][y] = phase;
Chris@264:             else m_phase[x][y] = uint16_t(int16_t((phase * 32767) / M_PI));
Chris@159:         }
Chris@159:     }
Chris@159: 
Chris@537:     void initialise(uint16_t **&);
Chris@537:     void initialise(float **&);
Chris@159: };
Chris@159: 
Chris@159: 
Chris@159: #endif
Chris@159: