comparison data/fileio/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
comparison
equal deleted inserted replaced
147:3a13b0d4934e 148:1a42221a1522
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.
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 "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 protected:
60 char *m_writebuf;
61 mutable char *m_readbuf;
62 mutable size_t m_readbufCol;
63 mutable size_t m_readbufWidth;
64
65 float getFromReadBufStandard(size_t x, size_t y) const {
66 if (m_readbuf &&
67 (m_readbufCol == x || (m_readbufWidth > 1 && m_readbufCol+1 == x))) {
68 return ((float *)m_readbuf)[(x - m_readbufCol) * m_mfc->getHeight() + y];
69 } else {
70 populateReadBuf(x);
71 return getFromReadBufStandard(x, y);
72 }
73 }
74
75 float getFromReadBufCompactUnsigned(size_t x, size_t y) const {
76 if (m_readbuf &&
77 (m_readbufCol == x || (m_readbufWidth > 1 && m_readbufCol+1 == x))) {
78 return ((uint16_t *)m_readbuf)[(x - m_readbufCol) * m_mfc->getHeight() + y];
79 } else {
80 populateReadBuf(x);
81 return getFromReadBufCompactUnsigned(x, y);
82 }
83 }
84
85 float getFromReadBufCompactSigned(size_t x, size_t y) const {
86 if (m_readbuf &&
87 (m_readbufCol == x || (m_readbufWidth > 1 && m_readbufCol+1 == x))) {
88 return ((int16_t *)m_readbuf)[(x - m_readbufCol) * m_mfc->getHeight() + y];
89 } else {
90 populateReadBuf(x);
91 return getFromReadBufCompactSigned(x, y);
92 }
93 }
94
95 void populateReadBuf(size_t x) const {
96 if (!m_readbuf) {
97 m_readbuf = new char[m_mfc->getHeight() * 2 * m_mfc->getCellSize()];
98 }
99 m_mfc->getColumnAt(x, m_readbuf);
100 if (m_mfc->haveSetColumnAt(x + 1)) {
101 m_mfc->getColumnAt
102 (x + 1, m_readbuf + m_mfc->getCellSize() * m_mfc->getHeight());
103 m_readbufWidth = 2;
104 } else {
105 m_readbufWidth = 1;
106 }
107 m_readbufCol = x;
108 }
109
110 float getNormalizationFactor(size_t col) const {
111 if (m_storageType != Compact) {
112 return getFromReadBufStandard(col, m_mfc->getHeight() - 1);
113 } else {
114 float factor;
115 factor = getFromReadBufCompactUnsigned(col, m_mfc->getHeight() - 1);
116 return factor / 65535.0;
117 }
118 }
119
120 MatrixFile *m_mfc;
121 QMutex m_writeMutex;
122 StorageType m_storageType;
123 };
124
125 #endif