comparison data/fft/FFTFileCacheReader.h @ 537:3cc4b7cd2aa5

* Merge from one-fftdataserver-per-fftmodel branch. This bit of reworking (which is not described very accurately by the title of the branch) turns the MatrixFile object into something that either reads or writes, but not both, and separates the FFT file cache reader and writer implementations separately. This allows the FFT data server to have a single thread owning writers and one reader per "customer" thread, and for all locking to be vastly simplified and concentrated in the data server alone (because none of the classes it makes use of is used in more than one thread at a time). The result is faster and more trustworthy code.
author Chris Cannam
date Tue, 27 Jan 2009 13:25:10 +0000
parents
children 8accc7969c1c
comparison
equal deleted inserted replaced
536:beb51f558e9c 537:3cc4b7cd2aa5
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-2009 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_READER_H_
17 #define _FFT_FILE_CACHE_READER_H_
18
19 #include "data/fileio/MatrixFile.h"
20 #include "FFTCacheReader.h"
21 #include "FFTCacheStorageType.h"
22
23 class FFTFileCacheWriter;
24
25 class FFTFileCacheReader : public FFTCacheReader
26 {
27 public:
28 FFTFileCacheReader(FFTFileCacheWriter *);
29 ~FFTFileCacheReader();
30
31 size_t getWidth() const;
32 size_t getHeight() const;
33
34 float getMagnitudeAt(size_t x, size_t y) const;
35 float getNormalizedMagnitudeAt(size_t x, size_t y) const;
36 float getMaximumMagnitudeAt(size_t x) const;
37 float getPhaseAt(size_t x, size_t y) const;
38
39 void getValuesAt(size_t x, size_t y, float &real, float &imag) const;
40 void getMagnitudesAt(size_t x, float *values, size_t minbin, size_t count, size_t step) const;
41
42 bool haveSetColumnAt(size_t x) const;
43
44 static size_t getCacheSize(size_t width, size_t height,
45 FFTCache::StorageType type);
46
47 FFTCache::StorageType getStorageType() const { return m_storageType; }
48
49 protected:
50 mutable char *m_readbuf;
51 mutable size_t m_readbufCol;
52 mutable size_t m_readbufWidth;
53
54 float getFromReadBufStandard(size_t x, size_t y) const {
55 float v;
56 if (m_readbuf &&
57 (m_readbufCol == x || (m_readbufWidth > 1 && m_readbufCol+1 == x))) {
58 v = ((float *)m_readbuf)[(x - m_readbufCol) * m_mfc->getHeight() + y];
59 return v;
60 } else {
61 populateReadBuf(x);
62 v = getFromReadBufStandard(x, y);
63 return v;
64 }
65 }
66
67 float getFromReadBufCompactUnsigned(size_t x, size_t y) const {
68 float v;
69 if (m_readbuf &&
70 (m_readbufCol == x || (m_readbufWidth > 1 && m_readbufCol+1 == x))) {
71 v = ((uint16_t *)m_readbuf)[(x - m_readbufCol) * m_mfc->getHeight() + y];
72 return v;
73 } else {
74 populateReadBuf(x);
75 v = getFromReadBufCompactUnsigned(x, y);
76 return v;
77 }
78 }
79
80 float getFromReadBufCompactSigned(size_t x, size_t y) const {
81 float v;
82 if (m_readbuf &&
83 (m_readbufCol == x || (m_readbufWidth > 1 && m_readbufCol+1 == x))) {
84 v = ((int16_t *)m_readbuf)[(x - m_readbufCol) * m_mfc->getHeight() + y];
85 return v;
86 } else {
87 populateReadBuf(x);
88 v = getFromReadBufCompactSigned(x, y);
89 return v;
90 }
91 }
92
93 void populateReadBuf(size_t x) const;
94
95 float getNormalizationFactor(size_t col) const {
96 size_t h = m_mfc->getHeight();
97 if (h < m_factorSize) return 0;
98 if (m_storageType != FFTCache::Compact) {
99 return getFromReadBufStandard(col, h - 1);
100 } else {
101 union {
102 float f;
103 uint16_t u[2];
104 } factor;
105 if (!m_readbuf ||
106 !(m_readbufCol == col ||
107 (m_readbufWidth > 1 && m_readbufCol+1 == col))) {
108 populateReadBuf(col);
109 }
110 size_t ix = (col - m_readbufCol) * m_mfc->getHeight() + h;
111 factor.u[0] = ((uint16_t *)m_readbuf)[ix - 2];
112 factor.u[1] = ((uint16_t *)m_readbuf)[ix - 1];
113 return factor.f;
114 }
115 }
116
117 FFTCache::StorageType m_storageType;
118 size_t m_factorSize;
119 MatrixFile *m_mfc;
120 };
121
122 #endif