Chris@148: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@148: Chris@148: /* Chris@148: Sonic Visualiser Chris@148: An audio file viewer and annotation editor. Chris@148: Centre for Digital Music, Queen Mary, University of London. Chris@148: This file copyright 2006 Chris Cannam. Chris@148: Chris@148: This program is free software; you can redistribute it and/or Chris@148: modify it under the terms of the GNU General Public License as Chris@148: published by the Free Software Foundation; either version 2 of the Chris@148: License, or (at your option) any later version. See the file Chris@148: COPYING included with this distribution for more information. Chris@148: */ Chris@148: Chris@148: #ifndef _FFT_FILE_CACHE_H_ Chris@148: #define _FFT_FILE_CACHE_H_ Chris@148: Chris@152: #include "FFTCache.h" Chris@150: #include "fileio/MatrixFile.h" Chris@148: Chris@148: #include Chris@148: Chris@148: class FFTFileCache : public FFTCache Chris@148: { Chris@148: public: Chris@148: enum StorageType { Chris@148: Compact, // 16 bits normalized polar Chris@148: Rectangular, // floating point real+imag Chris@148: Polar, // floating point mag+phase Chris@148: }; Chris@148: Chris@148: FFTFileCache(QString fileBase, MatrixFile::Mode mode, Chris@148: StorageType storageType); Chris@148: virtual ~FFTFileCache(); Chris@148: Chris@148: MatrixFile::Mode getMode() const { return m_mfc->getMode(); } Chris@148: Chris@148: virtual size_t getWidth() const; Chris@148: virtual size_t getHeight() const; Chris@148: Chris@148: virtual void resize(size_t width, size_t height); Chris@148: virtual void reset(); // zero-fill or 1-fill as appropriate without changing size Chris@148: Chris@148: virtual float getMagnitudeAt(size_t x, size_t y) const; Chris@148: virtual float getNormalizedMagnitudeAt(size_t x, size_t y) const; Chris@148: virtual float getMaximumMagnitudeAt(size_t x) const; Chris@148: virtual float getPhaseAt(size_t x, size_t y) const; Chris@148: Chris@148: virtual void getValuesAt(size_t x, size_t y, float &real, float &imag) const; Chris@148: Chris@148: virtual bool haveSetColumnAt(size_t x) const; Chris@148: Chris@148: virtual void setColumnAt(size_t x, float *mags, float *phases, float factor); Chris@148: virtual void setColumnAt(size_t x, float *reals, float *imags); Chris@148: Chris@148: virtual void suspend() { m_mfc->suspend(); } Chris@148: Chris@170: static size_t getCacheSize(size_t width, size_t height, StorageType type); Chris@170: Chris@148: protected: Chris@148: char *m_writebuf; Chris@148: mutable char *m_readbuf; Chris@148: mutable size_t m_readbufCol; Chris@148: mutable size_t m_readbufWidth; Chris@148: Chris@148: float getFromReadBufStandard(size_t x, size_t y) const { Chris@148: if (m_readbuf && Chris@148: (m_readbufCol == x || (m_readbufWidth > 1 && m_readbufCol+1 == x))) { Chris@148: return ((float *)m_readbuf)[(x - m_readbufCol) * m_mfc->getHeight() + y]; Chris@148: } else { Chris@148: populateReadBuf(x); Chris@148: return getFromReadBufStandard(x, y); Chris@148: } Chris@148: } Chris@148: Chris@148: float getFromReadBufCompactUnsigned(size_t x, size_t y) const { Chris@148: if (m_readbuf && Chris@148: (m_readbufCol == x || (m_readbufWidth > 1 && m_readbufCol+1 == x))) { Chris@148: return ((uint16_t *)m_readbuf)[(x - m_readbufCol) * m_mfc->getHeight() + y]; Chris@148: } else { Chris@148: populateReadBuf(x); Chris@148: return getFromReadBufCompactUnsigned(x, y); Chris@148: } Chris@148: } Chris@148: Chris@148: float getFromReadBufCompactSigned(size_t x, size_t y) const { Chris@148: if (m_readbuf && Chris@148: (m_readbufCol == x || (m_readbufWidth > 1 && m_readbufCol+1 == x))) { Chris@148: return ((int16_t *)m_readbuf)[(x - m_readbufCol) * m_mfc->getHeight() + y]; Chris@148: } else { Chris@148: populateReadBuf(x); Chris@148: return getFromReadBufCompactSigned(x, y); Chris@148: } Chris@148: } Chris@148: Chris@148: void populateReadBuf(size_t x) const { Chris@148: if (!m_readbuf) { Chris@148: m_readbuf = new char[m_mfc->getHeight() * 2 * m_mfc->getCellSize()]; Chris@148: } Chris@148: m_mfc->getColumnAt(x, m_readbuf); Chris@148: if (m_mfc->haveSetColumnAt(x + 1)) { Chris@148: m_mfc->getColumnAt Chris@148: (x + 1, m_readbuf + m_mfc->getCellSize() * m_mfc->getHeight()); Chris@148: m_readbufWidth = 2; Chris@148: } else { Chris@148: m_readbufWidth = 1; Chris@148: } Chris@148: m_readbufCol = x; Chris@148: } Chris@148: Chris@148: float getNormalizationFactor(size_t col) const { Chris@148: if (m_storageType != Compact) { Chris@148: return getFromReadBufStandard(col, m_mfc->getHeight() - 1); Chris@148: } else { Chris@148: float factor; Chris@148: factor = getFromReadBufCompactUnsigned(col, m_mfc->getHeight() - 1); Chris@148: return factor / 65535.0; Chris@148: } Chris@148: } Chris@148: Chris@148: MatrixFile *m_mfc; Chris@148: QMutex m_writeMutex; Chris@148: StorageType m_storageType; Chris@148: }; Chris@148: Chris@148: #endif