annotate data/fft/FFTFileCacheReader.h @ 544:65d955c4d671

* throw (but do not yet catch!) exception when running out of disc space in coded audio file * simpler (faster?) cache-to-cache move operation in spectrogram, and some other minor fixes
author Chris Cannam
date Wed, 04 Feb 2009 12:13:16 +0000
parents 3cc4b7cd2aa5
children 8accc7969c1c
rev   line source
Chris@537 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@537 2
Chris@537 3 /*
Chris@537 4 Sonic Visualiser
Chris@537 5 An audio file viewer and annotation editor.
Chris@537 6 Centre for Digital Music, Queen Mary, University of London.
Chris@537 7 This file copyright 2006-2009 Chris Cannam and QMUL.
Chris@537 8
Chris@537 9 This program is free software; you can redistribute it and/or
Chris@537 10 modify it under the terms of the GNU General Public License as
Chris@537 11 published by the Free Software Foundation; either version 2 of the
Chris@537 12 License, or (at your option) any later version. See the file
Chris@537 13 COPYING included with this distribution for more information.
Chris@537 14 */
Chris@537 15
Chris@537 16 #ifndef _FFT_FILE_CACHE_READER_H_
Chris@537 17 #define _FFT_FILE_CACHE_READER_H_
Chris@537 18
Chris@537 19 #include "data/fileio/MatrixFile.h"
Chris@537 20 #include "FFTCacheReader.h"
Chris@537 21 #include "FFTCacheStorageType.h"
Chris@537 22
Chris@537 23 class FFTFileCacheWriter;
Chris@537 24
Chris@537 25 class FFTFileCacheReader : public FFTCacheReader
Chris@537 26 {
Chris@537 27 public:
Chris@537 28 FFTFileCacheReader(FFTFileCacheWriter *);
Chris@537 29 ~FFTFileCacheReader();
Chris@537 30
Chris@537 31 size_t getWidth() const;
Chris@537 32 size_t getHeight() const;
Chris@537 33
Chris@537 34 float getMagnitudeAt(size_t x, size_t y) const;
Chris@537 35 float getNormalizedMagnitudeAt(size_t x, size_t y) const;
Chris@537 36 float getMaximumMagnitudeAt(size_t x) const;
Chris@537 37 float getPhaseAt(size_t x, size_t y) const;
Chris@537 38
Chris@537 39 void getValuesAt(size_t x, size_t y, float &real, float &imag) const;
Chris@537 40 void getMagnitudesAt(size_t x, float *values, size_t minbin, size_t count, size_t step) const;
Chris@537 41
Chris@537 42 bool haveSetColumnAt(size_t x) const;
Chris@537 43
Chris@537 44 static size_t getCacheSize(size_t width, size_t height,
Chris@537 45 FFTCache::StorageType type);
Chris@537 46
Chris@537 47 FFTCache::StorageType getStorageType() const { return m_storageType; }
Chris@537 48
Chris@537 49 protected:
Chris@537 50 mutable char *m_readbuf;
Chris@537 51 mutable size_t m_readbufCol;
Chris@537 52 mutable size_t m_readbufWidth;
Chris@537 53
Chris@537 54 float getFromReadBufStandard(size_t x, size_t y) const {
Chris@537 55 float v;
Chris@537 56 if (m_readbuf &&
Chris@537 57 (m_readbufCol == x || (m_readbufWidth > 1 && m_readbufCol+1 == x))) {
Chris@537 58 v = ((float *)m_readbuf)[(x - m_readbufCol) * m_mfc->getHeight() + y];
Chris@537 59 return v;
Chris@537 60 } else {
Chris@537 61 populateReadBuf(x);
Chris@537 62 v = getFromReadBufStandard(x, y);
Chris@537 63 return v;
Chris@537 64 }
Chris@537 65 }
Chris@537 66
Chris@537 67 float getFromReadBufCompactUnsigned(size_t x, size_t y) const {
Chris@537 68 float v;
Chris@537 69 if (m_readbuf &&
Chris@537 70 (m_readbufCol == x || (m_readbufWidth > 1 && m_readbufCol+1 == x))) {
Chris@537 71 v = ((uint16_t *)m_readbuf)[(x - m_readbufCol) * m_mfc->getHeight() + y];
Chris@537 72 return v;
Chris@537 73 } else {
Chris@537 74 populateReadBuf(x);
Chris@537 75 v = getFromReadBufCompactUnsigned(x, y);
Chris@537 76 return v;
Chris@537 77 }
Chris@537 78 }
Chris@537 79
Chris@537 80 float getFromReadBufCompactSigned(size_t x, size_t y) const {
Chris@537 81 float v;
Chris@537 82 if (m_readbuf &&
Chris@537 83 (m_readbufCol == x || (m_readbufWidth > 1 && m_readbufCol+1 == x))) {
Chris@537 84 v = ((int16_t *)m_readbuf)[(x - m_readbufCol) * m_mfc->getHeight() + y];
Chris@537 85 return v;
Chris@537 86 } else {
Chris@537 87 populateReadBuf(x);
Chris@537 88 v = getFromReadBufCompactSigned(x, y);
Chris@537 89 return v;
Chris@537 90 }
Chris@537 91 }
Chris@537 92
Chris@537 93 void populateReadBuf(size_t x) const;
Chris@537 94
Chris@537 95 float getNormalizationFactor(size_t col) const {
Chris@537 96 size_t h = m_mfc->getHeight();
Chris@537 97 if (h < m_factorSize) return 0;
Chris@537 98 if (m_storageType != FFTCache::Compact) {
Chris@537 99 return getFromReadBufStandard(col, h - 1);
Chris@537 100 } else {
Chris@537 101 union {
Chris@537 102 float f;
Chris@537 103 uint16_t u[2];
Chris@537 104 } factor;
Chris@537 105 if (!m_readbuf ||
Chris@537 106 !(m_readbufCol == col ||
Chris@537 107 (m_readbufWidth > 1 && m_readbufCol+1 == col))) {
Chris@537 108 populateReadBuf(col);
Chris@537 109 }
Chris@537 110 size_t ix = (col - m_readbufCol) * m_mfc->getHeight() + h;
Chris@537 111 factor.u[0] = ((uint16_t *)m_readbuf)[ix - 2];
Chris@537 112 factor.u[1] = ((uint16_t *)m_readbuf)[ix - 1];
Chris@537 113 return factor.f;
Chris@537 114 }
Chris@537 115 }
Chris@537 116
Chris@537 117 FFTCache::StorageType m_storageType;
Chris@537 118 size_t m_factorSize;
Chris@537 119 MatrixFile *m_mfc;
Chris@537 120 };
Chris@537 121
Chris@537 122 #endif