comparison data/fft/FFTFileCacheReader.cpp @ 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 60482f13e627
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 #include "FFTFileCacheReader.h"
17 #include "FFTFileCacheWriter.h"
18
19 #include "fileio/MatrixFile.h"
20
21 #include "base/Profiler.h"
22 #include "base/Thread.h"
23 #include "base/Exceptions.h"
24
25 #include <iostream>
26
27
28 // The underlying matrix has height (m_height * 2 + 1). In each
29 // column we store magnitude at [0], [2] etc and phase at [1], [3]
30 // etc, and then store the normalization factor (maximum magnitude) at
31 // [m_height * 2]. In compact mode, the factor takes two cells.
32
33 FFTFileCacheReader::FFTFileCacheReader(FFTFileCacheWriter *writer) :
34 m_readbuf(0),
35 m_readbufCol(0),
36 m_readbufWidth(0),
37 m_storageType(writer->getStorageType()),
38 m_factorSize(m_storageType == FFTCache::Compact ? 2 : 1),
39 m_mfc(new MatrixFile
40 (writer->getFileBase(),
41 MatrixFile::ReadOnly,
42 m_storageType == FFTCache::Compact ? sizeof(uint16_t) : sizeof(float),
43 writer->getWidth(),
44 writer->getHeight() * 2 + m_factorSize))
45 {
46 // std::cerr << "FFTFileCacheReader: storage type is " << (storageType == FFTCache::Compact ? "Compact" : storageType == Polar ? "Polar" : "Rectangular") << std::endl;
47 }
48
49 FFTFileCacheReader::~FFTFileCacheReader()
50 {
51 if (m_readbuf) delete[] m_readbuf;
52 delete m_mfc;
53 }
54
55 size_t
56 FFTFileCacheReader::getWidth() const
57 {
58 return m_mfc->getWidth();
59 }
60
61 size_t
62 FFTFileCacheReader::getHeight() const
63 {
64 size_t mh = m_mfc->getHeight();
65 if (mh > m_factorSize) return (mh - m_factorSize) / 2;
66 else return 0;
67 }
68
69 float
70 FFTFileCacheReader::getMagnitudeAt(size_t x, size_t y) const
71 {
72 Profiler profiler("FFTFileCacheReader::getMagnitudeAt", false);
73
74 float value = 0.f;
75
76 switch (m_storageType) {
77
78 case FFTCache::Compact:
79 value = (getFromReadBufCompactUnsigned(x, y * 2) / 65535.0)
80 * getNormalizationFactor(x);
81 break;
82
83 case FFTCache::Rectangular:
84 {
85 float real, imag;
86 getValuesAt(x, y, real, imag);
87 value = sqrtf(real * real + imag * imag);
88 break;
89 }
90
91 case FFTCache::Polar:
92 value = getFromReadBufStandard(x, y * 2);
93 break;
94 }
95
96 return value;
97 }
98
99 float
100 FFTFileCacheReader::getNormalizedMagnitudeAt(size_t x, size_t y) const
101 {
102 float value = 0.f;
103
104 switch (m_storageType) {
105
106 case FFTCache::Compact:
107 value = getFromReadBufCompactUnsigned(x, y * 2) / 65535.0;
108 break;
109
110 default:
111 {
112 float mag = getMagnitudeAt(x, y);
113 float factor = getNormalizationFactor(x);
114 if (factor != 0) value = mag / factor;
115 else value = 0.f;
116 break;
117 }
118 }
119
120 return value;
121 }
122
123 float
124 FFTFileCacheReader::getMaximumMagnitudeAt(size_t x) const
125 {
126 return getNormalizationFactor(x);
127 }
128
129 float
130 FFTFileCacheReader::getPhaseAt(size_t x, size_t y) const
131 {
132 float value = 0.f;
133
134 switch (m_storageType) {
135
136 case FFTCache::Compact:
137 value = (getFromReadBufCompactSigned(x, y * 2 + 1) / 32767.0) * M_PI;
138 break;
139
140 case FFTCache::Rectangular:
141 {
142 float real, imag;
143 getValuesAt(x, y, real, imag);
144 value = atan2f(imag, real);
145 break;
146 }
147
148 case FFTCache::Polar:
149 value = getFromReadBufStandard(x, y * 2 + 1);
150 break;
151 }
152
153 return value;
154 }
155
156 void
157 FFTFileCacheReader::getValuesAt(size_t x, size_t y, float &real, float &imag) const
158 {
159 switch (m_storageType) {
160
161 case FFTCache::Rectangular:
162 real = getFromReadBufStandard(x, y * 2);
163 imag = getFromReadBufStandard(x, y * 2 + 1);
164 return;
165
166 default:
167 float mag = getMagnitudeAt(x, y);
168 float phase = getPhaseAt(x, y);
169 real = mag * cosf(phase);
170 imag = mag * sinf(phase);
171 return;
172 }
173 }
174
175 void
176 FFTFileCacheReader::getMagnitudesAt(size_t x, float *values, size_t minbin, size_t count, size_t step) const
177 {
178 Profiler profiler("FFTFileCacheReader::getMagnitudesAt");
179
180 switch (m_storageType) {
181
182 case FFTCache::Compact:
183 for (size_t i = 0; i < count; ++i) {
184 size_t y = minbin + i * step;
185 values[i] = (getFromReadBufCompactUnsigned(x, y * 2) / 65535.0)
186 * getNormalizationFactor(x);
187 }
188 break;
189
190 case FFTCache::Rectangular:
191 {
192 float real, imag;
193 for (size_t i = 0; i < count; ++i) {
194 size_t y = minbin + i * step;
195 real = getFromReadBufStandard(x, y * 2);
196 imag = getFromReadBufStandard(x, y * 2 + 1);
197 values[i] = sqrtf(real * real + imag * imag);
198 }
199 break;
200 }
201
202 case FFTCache::Polar:
203 for (size_t i = 0; i < count; ++i) {
204 size_t y = minbin + i * step;
205 values[i] = getFromReadBufStandard(x, y * 2);
206 }
207 break;
208 }
209 }
210
211 bool
212 FFTFileCacheReader::haveSetColumnAt(size_t x) const
213 {
214 return m_mfc->haveSetColumnAt(x);
215 }
216
217 size_t
218 FFTFileCacheReader::getCacheSize(size_t width, size_t height,
219 FFTCache::StorageType type)
220 {
221 return (height * 2 + (type == FFTCache::Compact ? 2 : 1)) * width *
222 (type == FFTCache::Compact ? sizeof(uint16_t) : sizeof(float)) +
223 2 * sizeof(size_t); // matrix file header size
224 }
225
226 void
227 FFTFileCacheReader::populateReadBuf(size_t x) const
228 {
229 Profiler profiler("FFTFileCacheReader::populateReadBuf", false);
230
231 if (!m_readbuf) {
232 m_readbuf = new char[m_mfc->getHeight() * 2 * m_mfc->getCellSize()];
233 }
234
235 try {
236 m_mfc->getColumnAt(x, m_readbuf);
237 if (m_mfc->haveSetColumnAt(x + 1)) {
238 m_mfc->getColumnAt
239 (x + 1, m_readbuf + m_mfc->getCellSize() * m_mfc->getHeight());
240 m_readbufWidth = 2;
241 } else {
242 m_readbufWidth = 1;
243 }
244 } catch (FileReadFailed f) {
245 std::cerr << "ERROR: FFTFileCacheReader::populateReadBuf: File read failed: "
246 << f.what() << std::endl;
247 memset(m_readbuf, 0, m_mfc->getHeight() * 2 * m_mfc->getCellSize());
248 }
249 m_readbufCol = x;
250 }
251