annotate data/fft/FFTFileCache.h @ 148:1a42221a1522

* Reorganising code base. This revision will not compile.
author Chris Cannam
date Mon, 31 Jul 2006 11:49:58 +0000
parents
children 4b2ea82fd0ed
rev   line source
Chris@148 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@148 2
Chris@148 3 /*
Chris@148 4 Sonic Visualiser
Chris@148 5 An audio file viewer and annotation editor.
Chris@148 6 Centre for Digital Music, Queen Mary, University of London.
Chris@148 7 This file copyright 2006 Chris Cannam.
Chris@148 8
Chris@148 9 This program is free software; you can redistribute it and/or
Chris@148 10 modify it under the terms of the GNU General Public License as
Chris@148 11 published by the Free Software Foundation; either version 2 of the
Chris@148 12 License, or (at your option) any later version. See the file
Chris@148 13 COPYING included with this distribution for more information.
Chris@148 14 */
Chris@148 15
Chris@148 16 #ifndef _FFT_FILE_CACHE_H_
Chris@148 17 #define _FFT_FILE_CACHE_H_
Chris@148 18
Chris@148 19 #include "FFTCache.h"
Chris@148 20 #include "MatrixFile.h"
Chris@148 21
Chris@148 22 #include <QMutex>
Chris@148 23
Chris@148 24 class FFTFileCache : public FFTCache
Chris@148 25 {
Chris@148 26 public:
Chris@148 27 enum StorageType {
Chris@148 28 Compact, // 16 bits normalized polar
Chris@148 29 Rectangular, // floating point real+imag
Chris@148 30 Polar, // floating point mag+phase
Chris@148 31 };
Chris@148 32
Chris@148 33 FFTFileCache(QString fileBase, MatrixFile::Mode mode,
Chris@148 34 StorageType storageType);
Chris@148 35 virtual ~FFTFileCache();
Chris@148 36
Chris@148 37 MatrixFile::Mode getMode() const { return m_mfc->getMode(); }
Chris@148 38
Chris@148 39 virtual size_t getWidth() const;
Chris@148 40 virtual size_t getHeight() const;
Chris@148 41
Chris@148 42 virtual void resize(size_t width, size_t height);
Chris@148 43 virtual void reset(); // zero-fill or 1-fill as appropriate without changing size
Chris@148 44
Chris@148 45 virtual float getMagnitudeAt(size_t x, size_t y) const;
Chris@148 46 virtual float getNormalizedMagnitudeAt(size_t x, size_t y) const;
Chris@148 47 virtual float getMaximumMagnitudeAt(size_t x) const;
Chris@148 48 virtual float getPhaseAt(size_t x, size_t y) const;
Chris@148 49
Chris@148 50 virtual void getValuesAt(size_t x, size_t y, float &real, float &imag) const;
Chris@148 51
Chris@148 52 virtual bool haveSetColumnAt(size_t x) const;
Chris@148 53
Chris@148 54 virtual void setColumnAt(size_t x, float *mags, float *phases, float factor);
Chris@148 55 virtual void setColumnAt(size_t x, float *reals, float *imags);
Chris@148 56
Chris@148 57 virtual void suspend() { m_mfc->suspend(); }
Chris@148 58
Chris@148 59 protected:
Chris@148 60 char *m_writebuf;
Chris@148 61 mutable char *m_readbuf;
Chris@148 62 mutable size_t m_readbufCol;
Chris@148 63 mutable size_t m_readbufWidth;
Chris@148 64
Chris@148 65 float getFromReadBufStandard(size_t x, size_t y) const {
Chris@148 66 if (m_readbuf &&
Chris@148 67 (m_readbufCol == x || (m_readbufWidth > 1 && m_readbufCol+1 == x))) {
Chris@148 68 return ((float *)m_readbuf)[(x - m_readbufCol) * m_mfc->getHeight() + y];
Chris@148 69 } else {
Chris@148 70 populateReadBuf(x);
Chris@148 71 return getFromReadBufStandard(x, y);
Chris@148 72 }
Chris@148 73 }
Chris@148 74
Chris@148 75 float getFromReadBufCompactUnsigned(size_t x, size_t y) const {
Chris@148 76 if (m_readbuf &&
Chris@148 77 (m_readbufCol == x || (m_readbufWidth > 1 && m_readbufCol+1 == x))) {
Chris@148 78 return ((uint16_t *)m_readbuf)[(x - m_readbufCol) * m_mfc->getHeight() + y];
Chris@148 79 } else {
Chris@148 80 populateReadBuf(x);
Chris@148 81 return getFromReadBufCompactUnsigned(x, y);
Chris@148 82 }
Chris@148 83 }
Chris@148 84
Chris@148 85 float getFromReadBufCompactSigned(size_t x, size_t y) const {
Chris@148 86 if (m_readbuf &&
Chris@148 87 (m_readbufCol == x || (m_readbufWidth > 1 && m_readbufCol+1 == x))) {
Chris@148 88 return ((int16_t *)m_readbuf)[(x - m_readbufCol) * m_mfc->getHeight() + y];
Chris@148 89 } else {
Chris@148 90 populateReadBuf(x);
Chris@148 91 return getFromReadBufCompactSigned(x, y);
Chris@148 92 }
Chris@148 93 }
Chris@148 94
Chris@148 95 void populateReadBuf(size_t x) const {
Chris@148 96 if (!m_readbuf) {
Chris@148 97 m_readbuf = new char[m_mfc->getHeight() * 2 * m_mfc->getCellSize()];
Chris@148 98 }
Chris@148 99 m_mfc->getColumnAt(x, m_readbuf);
Chris@148 100 if (m_mfc->haveSetColumnAt(x + 1)) {
Chris@148 101 m_mfc->getColumnAt
Chris@148 102 (x + 1, m_readbuf + m_mfc->getCellSize() * m_mfc->getHeight());
Chris@148 103 m_readbufWidth = 2;
Chris@148 104 } else {
Chris@148 105 m_readbufWidth = 1;
Chris@148 106 }
Chris@148 107 m_readbufCol = x;
Chris@148 108 }
Chris@148 109
Chris@148 110 float getNormalizationFactor(size_t col) const {
Chris@148 111 if (m_storageType != Compact) {
Chris@148 112 return getFromReadBufStandard(col, m_mfc->getHeight() - 1);
Chris@148 113 } else {
Chris@148 114 float factor;
Chris@148 115 factor = getFromReadBufCompactUnsigned(col, m_mfc->getHeight() - 1);
Chris@148 116 return factor / 65535.0;
Chris@148 117 }
Chris@148 118 }
Chris@148 119
Chris@148 120 MatrixFile *m_mfc;
Chris@148 121 QMutex m_writeMutex;
Chris@148 122 StorageType m_storageType;
Chris@148 123 };
Chris@148 124
Chris@148 125 #endif