annotate data/fft/FFTFileCache.h @ 509:6066bde1c126

* Cut back on the locking and general workload in FFTDataServer::getMagnitudes(). This stuff is far too complicated!
author Chris Cannam
date Mon, 08 Dec 2008 11:15:13 +0000
parents f60360209e5c
children
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@202 7 This file copyright 2006 Chris Cannam and QMUL.
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@152 19 #include "FFTCache.h"
Chris@150 20 #include "fileio/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 FFTFileCache(QString fileBase, MatrixFile::Mode mode,
Chris@148 28 StorageType storageType);
Chris@148 29 virtual ~FFTFileCache();
Chris@148 30
Chris@148 31 MatrixFile::Mode getMode() const { return m_mfc->getMode(); }
Chris@148 32
Chris@148 33 virtual size_t getWidth() const;
Chris@148 34 virtual size_t getHeight() const;
Chris@148 35
Chris@148 36 virtual void resize(size_t width, size_t height);
Chris@148 37 virtual void reset(); // zero-fill or 1-fill as appropriate without changing size
Chris@148 38
Chris@148 39 virtual float getMagnitudeAt(size_t x, size_t y) const;
Chris@148 40 virtual float getNormalizedMagnitudeAt(size_t x, size_t y) const;
Chris@148 41 virtual float getMaximumMagnitudeAt(size_t x) const;
Chris@148 42 virtual float getPhaseAt(size_t x, size_t y) const;
Chris@148 43
Chris@148 44 virtual void getValuesAt(size_t x, size_t y, float &real, float &imag) const;
Chris@509 45 virtual void getMagnitudesAt(size_t x, float *values, size_t minbin, size_t count, size_t step) const;
Chris@148 46
Chris@148 47 virtual bool haveSetColumnAt(size_t x) const;
Chris@148 48
Chris@148 49 virtual void setColumnAt(size_t x, float *mags, float *phases, float factor);
Chris@148 50 virtual void setColumnAt(size_t x, float *reals, float *imags);
Chris@148 51
Chris@148 52 virtual void suspend() { m_mfc->suspend(); }
Chris@148 53
Chris@170 54 static size_t getCacheSize(size_t width, size_t height, StorageType type);
Chris@170 55
Chris@408 56 virtual StorageType getStorageType() { return m_storageType; }
Chris@359 57 virtual Type getType() { return FileCache; }
Chris@359 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@509 65 float getFromReadBufStandard(size_t x, size_t y, bool lock) const {
Chris@509 66 if (lock) m_readbufMutex.lock();
Chris@509 67 float v;
Chris@148 68 if (m_readbuf &&
Chris@148 69 (m_readbufCol == x || (m_readbufWidth > 1 && m_readbufCol+1 == x))) {
Chris@509 70 v = ((float *)m_readbuf)[(x - m_readbufCol) * m_mfc->getHeight() + y];
Chris@509 71 if (lock) m_readbufMutex.unlock();
Chris@458 72 return v;
Chris@148 73 } else {
Chris@148 74 populateReadBuf(x);
Chris@509 75 v = getFromReadBufStandard(x, y, false);
Chris@509 76 if (lock) m_readbufMutex.unlock();
Chris@509 77 return v;
Chris@148 78 }
Chris@148 79 }
Chris@148 80
Chris@509 81 float getFromReadBufCompactUnsigned(size_t x, size_t y, bool lock) const {
Chris@509 82 if (lock) m_readbufMutex.lock();
Chris@509 83 float v;
Chris@148 84 if (m_readbuf &&
Chris@148 85 (m_readbufCol == x || (m_readbufWidth > 1 && m_readbufCol+1 == x))) {
Chris@509 86 v = ((uint16_t *)m_readbuf)[(x - m_readbufCol) * m_mfc->getHeight() + y];
Chris@509 87 if (lock) m_readbufMutex.unlock();
Chris@458 88 return v;
Chris@148 89 } else {
Chris@148 90 populateReadBuf(x);
Chris@509 91 v = getFromReadBufCompactUnsigned(x, y, false);
Chris@509 92 if (lock) m_readbufMutex.unlock();
Chris@509 93 return v;
Chris@148 94 }
Chris@148 95 }
Chris@148 96
Chris@509 97 float getFromReadBufCompactSigned(size_t x, size_t y, bool lock) const {
Chris@509 98 if (lock) m_readbufMutex.lock();
Chris@509 99 float v;
Chris@148 100 if (m_readbuf &&
Chris@148 101 (m_readbufCol == x || (m_readbufWidth > 1 && m_readbufCol+1 == x))) {
Chris@509 102 v = ((int16_t *)m_readbuf)[(x - m_readbufCol) * m_mfc->getHeight() + y];
Chris@509 103 if (lock) m_readbufMutex.unlock();
Chris@458 104 return v;
Chris@148 105 } else {
Chris@148 106 populateReadBuf(x);
Chris@509 107 v = getFromReadBufCompactSigned(x, y, false);
Chris@509 108 if (lock) m_readbufMutex.unlock();
Chris@509 109 return v;
Chris@148 110 }
Chris@148 111 }
Chris@148 112
Chris@183 113 void populateReadBuf(size_t x) const;
Chris@148 114
Chris@509 115 float getNormalizationFactor(size_t col, bool lock) const {
Chris@266 116 size_t h = m_mfc->getHeight();
Chris@266 117 if (h < m_factorSize) return 0;
Chris@148 118 if (m_storageType != Compact) {
Chris@509 119 return getFromReadBufStandard(col, h - 1, lock);
Chris@148 120 } else {
Chris@509 121 if (lock) m_readbufMutex.lock();
Chris@266 122 union {
Chris@266 123 float f;
Chris@266 124 uint16_t u[2];
Chris@266 125 } factor;
Chris@266 126 if (!m_readbuf ||
Chris@266 127 !(m_readbufCol == col ||
Chris@266 128 (m_readbufWidth > 1 && m_readbufCol+1 == col))) {
Chris@266 129 populateReadBuf(col);
Chris@266 130 }
Chris@266 131 size_t ix = (col - m_readbufCol) * m_mfc->getHeight() + h;
Chris@266 132 factor.u[0] = ((uint16_t *)m_readbuf)[ix - 2];
Chris@266 133 factor.u[1] = ((uint16_t *)m_readbuf)[ix - 1];
Chris@509 134 if (lock) m_readbufMutex.unlock();
Chris@266 135 return factor.f;
Chris@148 136 }
Chris@148 137 }
Chris@148 138
Chris@266 139 void setNormalizationFactorToWritebuf(float newfactor) {
Chris@266 140 size_t h = m_mfc->getHeight();
Chris@266 141 if (h < m_factorSize) return;
Chris@266 142 if (m_storageType != Compact) {
Chris@266 143 ((float *)m_writebuf)[h - 1] = newfactor;
Chris@266 144 } else {
Chris@266 145 union {
Chris@266 146 float f;
Chris@266 147 uint16_t u[2];
Chris@266 148 } factor;
Chris@266 149 factor.f = newfactor;
Chris@266 150 ((uint16_t *)m_writebuf)[h - 2] = factor.u[0];
Chris@266 151 ((uint16_t *)m_writebuf)[h - 1] = factor.u[1];
Chris@266 152 }
Chris@266 153 }
Chris@266 154
Chris@148 155 MatrixFile *m_mfc;
Chris@148 156 QMutex m_writeMutex;
Chris@458 157 mutable QMutex m_readbufMutex;
Chris@148 158 StorageType m_storageType;
Chris@266 159 size_t m_factorSize;
Chris@148 160 };
Chris@148 161
Chris@148 162 #endif