comparison data/fft/FFTFileCache.h @ 0:fc9323a41f5a

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