Chris@148
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
Chris@148
|
2
|
Chris@148
|
3 /*
|
Chris@148
|
4 Sonic Visualiser
|
Chris@148
|
5 An audio file viewer and annotation editor.
|
Chris@148
|
6 Centre for Digital Music, Queen Mary, University of London.
|
Chris@202
|
7 This file copyright 2006 Chris Cannam and QMUL.
|
Chris@148
|
8
|
Chris@148
|
9 This program is free software; you can redistribute it and/or
|
Chris@148
|
10 modify it under the terms of the GNU General Public License as
|
Chris@148
|
11 published by the Free Software Foundation; either version 2 of the
|
Chris@148
|
12 License, or (at your option) any later version. See the file
|
Chris@148
|
13 COPYING included with this distribution for more information.
|
Chris@148
|
14 */
|
Chris@148
|
15
|
Chris@148
|
16 #include "FFTDataServer.h"
|
Chris@148
|
17
|
Chris@148
|
18 #include "FFTFileCache.h"
|
Chris@159
|
19 #include "FFTMemoryCache.h"
|
Chris@148
|
20
|
Chris@148
|
21 #include "model/DenseTimeValueModel.h"
|
Chris@148
|
22
|
Chris@150
|
23 #include "system/System.h"
|
Chris@148
|
24
|
Chris@168
|
25 #include "base/StorageAdviser.h"
|
Chris@200
|
26 #include "base/Exceptions.h"
|
Chris@183
|
27 #include "base/Profiler.h"
|
Chris@168
|
28
|
Chris@200
|
29 #include <QMessageBox>
|
Chris@200
|
30 #include <QApplication>
|
Chris@200
|
31
|
Chris@168
|
32
|
Chris@194
|
33 //#define DEBUG_FFT_SERVER 1
|
Chris@194
|
34 //#define DEBUG_FFT_SERVER_FILL 1
|
Chris@148
|
35
|
Chris@148
|
36 #ifdef DEBUG_FFT_SERVER_FILL
|
Chris@153
|
37 #ifndef DEBUG_FFT_SERVER
|
Chris@153
|
38 #define DEBUG_FFT_SERVER 1
|
Chris@153
|
39 #endif
|
Chris@148
|
40 #endif
|
Chris@148
|
41
|
Chris@148
|
42 FFTDataServer::ServerMap FFTDataServer::m_servers;
|
Chris@215
|
43 FFTDataServer::ServerQueue FFTDataServer::m_releasedServers;
|
Chris@148
|
44 QMutex FFTDataServer::m_serverMapMutex;
|
Chris@148
|
45
|
Chris@148
|
46 FFTDataServer *
|
Chris@148
|
47 FFTDataServer::getInstance(const DenseTimeValueModel *model,
|
Chris@148
|
48 int channel,
|
Chris@148
|
49 WindowType windowType,
|
Chris@148
|
50 size_t windowSize,
|
Chris@148
|
51 size_t windowIncrement,
|
Chris@148
|
52 size_t fftSize,
|
Chris@148
|
53 bool polar,
|
Chris@148
|
54 size_t fillFromColumn)
|
Chris@148
|
55 {
|
Chris@148
|
56 QString n = generateFileBasename(model,
|
Chris@148
|
57 channel,
|
Chris@148
|
58 windowType,
|
Chris@148
|
59 windowSize,
|
Chris@148
|
60 windowIncrement,
|
Chris@148
|
61 fftSize,
|
Chris@148
|
62 polar);
|
Chris@148
|
63
|
Chris@148
|
64 FFTDataServer *server = 0;
|
Chris@148
|
65
|
Chris@148
|
66 QMutexLocker locker(&m_serverMapMutex);
|
Chris@148
|
67
|
Chris@148
|
68 if ((server = findServer(n))) {
|
Chris@148
|
69 return server;
|
Chris@148
|
70 }
|
Chris@148
|
71
|
Chris@148
|
72 QString npn = generateFileBasename(model,
|
Chris@148
|
73 channel,
|
Chris@148
|
74 windowType,
|
Chris@148
|
75 windowSize,
|
Chris@148
|
76 windowIncrement,
|
Chris@148
|
77 fftSize,
|
Chris@148
|
78 !polar);
|
Chris@148
|
79
|
Chris@148
|
80 if ((server = findServer(npn))) {
|
Chris@148
|
81 return server;
|
Chris@148
|
82 }
|
Chris@148
|
83
|
Chris@200
|
84 try {
|
Chris@200
|
85 server = new FFTDataServer(n,
|
Chris@200
|
86 model,
|
Chris@200
|
87 channel,
|
Chris@200
|
88 windowType,
|
Chris@200
|
89 windowSize,
|
Chris@200
|
90 windowIncrement,
|
Chris@200
|
91 fftSize,
|
Chris@200
|
92 polar,
|
Chris@200
|
93 fillFromColumn);
|
Chris@200
|
94 } catch (InsufficientDiscSpace) {
|
Chris@200
|
95 delete server;
|
Chris@200
|
96 server = 0;
|
Chris@200
|
97 }
|
Chris@148
|
98
|
Chris@200
|
99 if (server) {
|
Chris@200
|
100 m_servers[n] = ServerCountPair(server, 1);
|
Chris@200
|
101 }
|
Chris@200
|
102
|
Chris@200
|
103 return server;
|
Chris@148
|
104 }
|
Chris@148
|
105
|
Chris@148
|
106 FFTDataServer *
|
Chris@148
|
107 FFTDataServer::getFuzzyInstance(const DenseTimeValueModel *model,
|
Chris@148
|
108 int channel,
|
Chris@148
|
109 WindowType windowType,
|
Chris@148
|
110 size_t windowSize,
|
Chris@148
|
111 size_t windowIncrement,
|
Chris@148
|
112 size_t fftSize,
|
Chris@148
|
113 bool polar,
|
Chris@148
|
114 size_t fillFromColumn)
|
Chris@148
|
115 {
|
Chris@148
|
116 // Fuzzy matching:
|
Chris@148
|
117 //
|
Chris@148
|
118 // -- if we're asked for polar and have non-polar, use it (and
|
Chris@148
|
119 // vice versa). This one is vital, and we do it for non-fuzzy as
|
Chris@148
|
120 // well (above).
|
Chris@148
|
121 //
|
Chris@148
|
122 // -- if we're asked for an instance with a given fft size and we
|
Chris@148
|
123 // have one already with a multiple of that fft size but the same
|
Chris@148
|
124 // window size and type (and model), we can draw the results from
|
Chris@148
|
125 // it (e.g. the 1st, 2nd, 3rd etc bins of a 512-sample FFT are the
|
Chris@148
|
126 // same as the the 1st, 5th, 9th etc of a 2048-sample FFT of the
|
Chris@148
|
127 // same window plus zero padding).
|
Chris@148
|
128 //
|
Chris@148
|
129 // -- if we're asked for an instance with a given window type and
|
Chris@148
|
130 // size and fft size and we have one already the same but with a
|
Chris@148
|
131 // smaller increment, we can draw the results from it (provided
|
Chris@148
|
132 // our increment is a multiple of its)
|
Chris@148
|
133 //
|
Chris@152
|
134 // The FFTModel knows how to interpret these things. In
|
Chris@148
|
135 // both cases we require that the larger one is a power-of-two
|
Chris@148
|
136 // multiple of the smaller (e.g. even though in principle you can
|
Chris@148
|
137 // draw the results at increment 256 from those at increment 768
|
Chris@152
|
138 // or 1536, the model doesn't support this).
|
Chris@148
|
139
|
Chris@148
|
140 {
|
Chris@148
|
141 QMutexLocker locker(&m_serverMapMutex);
|
Chris@148
|
142
|
Chris@148
|
143 ServerMap::iterator best = m_servers.end();
|
Chris@148
|
144 int bestdist = -1;
|
Chris@148
|
145
|
Chris@148
|
146 for (ServerMap::iterator i = m_servers.begin(); i != m_servers.end(); ++i) {
|
Chris@148
|
147
|
Chris@148
|
148 FFTDataServer *server = i->second.first;
|
Chris@148
|
149
|
Chris@148
|
150 if (server->getModel() == model &&
|
Chris@148
|
151 (server->getChannel() == channel || model->getChannelCount() == 1) &&
|
Chris@148
|
152 server->getWindowType() == windowType &&
|
Chris@148
|
153 server->getWindowSize() == windowSize &&
|
Chris@148
|
154 server->getWindowIncrement() <= windowIncrement &&
|
Chris@148
|
155 server->getFFTSize() >= fftSize) {
|
Chris@148
|
156
|
Chris@148
|
157 if ((windowIncrement % server->getWindowIncrement()) != 0) continue;
|
Chris@148
|
158 int ratio = windowIncrement / server->getWindowIncrement();
|
Chris@148
|
159 bool poweroftwo = true;
|
Chris@148
|
160 while (ratio > 1) {
|
Chris@148
|
161 if (ratio & 0x1) {
|
Chris@148
|
162 poweroftwo = false;
|
Chris@148
|
163 break;
|
Chris@148
|
164 }
|
Chris@148
|
165 ratio >>= 1;
|
Chris@148
|
166 }
|
Chris@148
|
167 if (!poweroftwo) continue;
|
Chris@148
|
168
|
Chris@148
|
169 if ((server->getFFTSize() % fftSize) != 0) continue;
|
Chris@148
|
170 ratio = server->getFFTSize() / fftSize;
|
Chris@148
|
171 while (ratio > 1) {
|
Chris@148
|
172 if (ratio & 0x1) {
|
Chris@148
|
173 poweroftwo = false;
|
Chris@148
|
174 break;
|
Chris@148
|
175 }
|
Chris@148
|
176 ratio >>= 1;
|
Chris@148
|
177 }
|
Chris@148
|
178 if (!poweroftwo) continue;
|
Chris@148
|
179
|
Chris@148
|
180 int distance = 0;
|
Chris@148
|
181
|
Chris@148
|
182 if (server->getPolar() != polar) distance += 1;
|
Chris@148
|
183
|
Chris@148
|
184 distance += ((windowIncrement / server->getWindowIncrement()) - 1) * 15;
|
Chris@148
|
185 distance += ((server->getFFTSize() / fftSize) - 1) * 10;
|
Chris@148
|
186
|
Chris@148
|
187 if (server->getFillCompletion() < 50) distance += 100;
|
Chris@148
|
188
|
Chris@148
|
189 #ifdef DEBUG_FFT_SERVER
|
Chris@148
|
190 std::cerr << "Distance " << distance << ", best is " << bestdist << std::endl;
|
Chris@148
|
191 #endif
|
Chris@148
|
192
|
Chris@148
|
193 if (bestdist == -1 || distance < bestdist) {
|
Chris@148
|
194 bestdist = distance;
|
Chris@148
|
195 best = i;
|
Chris@148
|
196 }
|
Chris@148
|
197 }
|
Chris@148
|
198 }
|
Chris@148
|
199
|
Chris@148
|
200 if (bestdist >= 0) {
|
Chris@148
|
201 ++best->second.second;
|
Chris@148
|
202 return best->second.first;
|
Chris@148
|
203 }
|
Chris@148
|
204 }
|
Chris@148
|
205
|
Chris@148
|
206 // Nothing found, make a new one
|
Chris@148
|
207
|
Chris@148
|
208 return getInstance(model,
|
Chris@148
|
209 channel,
|
Chris@148
|
210 windowType,
|
Chris@148
|
211 windowSize,
|
Chris@148
|
212 windowIncrement,
|
Chris@148
|
213 fftSize,
|
Chris@148
|
214 polar,
|
Chris@148
|
215 fillFromColumn);
|
Chris@148
|
216 }
|
Chris@148
|
217
|
Chris@148
|
218 FFTDataServer *
|
Chris@148
|
219 FFTDataServer::findServer(QString n)
|
Chris@148
|
220 {
|
Chris@148
|
221 if (m_servers.find(n) != m_servers.end()) {
|
Chris@148
|
222 ++m_servers[n].second;
|
Chris@148
|
223 return m_servers[n].first;
|
Chris@148
|
224 }
|
Chris@148
|
225
|
Chris@148
|
226 return 0;
|
Chris@148
|
227 }
|
Chris@148
|
228
|
Chris@148
|
229 void
|
Chris@152
|
230 FFTDataServer::claimInstance(FFTDataServer *server)
|
Chris@152
|
231 {
|
Chris@152
|
232
|
Chris@152
|
233 QMutexLocker locker(&m_serverMapMutex);
|
Chris@152
|
234
|
Chris@152
|
235 for (ServerMap::iterator i = m_servers.begin(); i != m_servers.end(); ++i) {
|
Chris@152
|
236 if (i->second.first == server) {
|
Chris@215
|
237
|
Chris@215
|
238 for (ServerQueue::iterator j = m_releasedServers.begin();
|
Chris@215
|
239 j != m_releasedServers.end(); ++j) {
|
Chris@215
|
240 if (*j == server) {
|
Chris@215
|
241 m_releasedServers.erase(j);
|
Chris@215
|
242 break;
|
Chris@215
|
243 }
|
Chris@215
|
244 }
|
Chris@215
|
245
|
Chris@152
|
246 ++i->second.second;
|
Chris@152
|
247 return;
|
Chris@152
|
248 }
|
Chris@152
|
249 }
|
Chris@152
|
250
|
Chris@152
|
251 std::cerr << "ERROR: FFTDataServer::claimInstance: instance "
|
Chris@152
|
252 << server << " unknown!" << std::endl;
|
Chris@152
|
253 }
|
Chris@152
|
254
|
Chris@152
|
255 void
|
Chris@148
|
256 FFTDataServer::releaseInstance(FFTDataServer *server)
|
Chris@148
|
257 {
|
Chris@148
|
258 #ifdef DEBUG_FFT_SERVER
|
Chris@148
|
259 std::cerr << "FFTDataServer::releaseInstance(" << server << ")" << std::endl;
|
Chris@148
|
260 #endif
|
Chris@148
|
261
|
Chris@148
|
262 QMutexLocker locker(&m_serverMapMutex);
|
Chris@148
|
263
|
Chris@148
|
264 // -- if ref count > 0, decrement and return
|
Chris@148
|
265 // -- if the instance hasn't been used at all, delete it immediately
|
Chris@148
|
266 // -- if fewer than N instances (N = e.g. 3) remain with zero refcounts,
|
Chris@148
|
267 // leave them hanging around
|
Chris@148
|
268 // -- if N instances with zero refcounts remain, delete the one that
|
Chris@148
|
269 // was last released first
|
Chris@148
|
270 // -- if we run out of disk space when allocating an instance, go back
|
Chris@148
|
271 // and delete the spare N instances before trying again
|
Chris@148
|
272 // -- have an additional method to indicate that a model has been
|
Chris@148
|
273 // destroyed, so that we can delete all of its fft server instances
|
Chris@148
|
274
|
Chris@148
|
275 for (ServerMap::iterator i = m_servers.begin(); i != m_servers.end(); ++i) {
|
Chris@148
|
276 if (i->second.first == server) {
|
Chris@148
|
277 if (i->second.second == 0) {
|
Chris@148
|
278 std::cerr << "ERROR: FFTDataServer::releaseInstance("
|
Chris@148
|
279 << server << "): instance not allocated" << std::endl;
|
Chris@148
|
280 } else if (--i->second.second == 0) {
|
Chris@148
|
281 if (server->m_lastUsedCache == -1) { // never used
|
Chris@148
|
282 delete server;
|
Chris@148
|
283 m_servers.erase(i);
|
Chris@148
|
284 } else {
|
Chris@215
|
285 m_releasedServers.push_back(server);
|
Chris@148
|
286 server->suspend();
|
Chris@148
|
287 purgeLimbo();
|
Chris@148
|
288 }
|
Chris@148
|
289 }
|
Chris@148
|
290 return;
|
Chris@148
|
291 }
|
Chris@148
|
292 }
|
Chris@148
|
293
|
Chris@148
|
294 std::cerr << "ERROR: FFTDataServer::releaseInstance(" << server << "): "
|
Chris@148
|
295 << "instance not found" << std::endl;
|
Chris@148
|
296 }
|
Chris@148
|
297
|
Chris@148
|
298 void
|
Chris@148
|
299 FFTDataServer::purgeLimbo(int maxSize)
|
Chris@148
|
300 {
|
Chris@215
|
301 while (m_releasedServers.size() > maxSize) {
|
Chris@148
|
302
|
Chris@215
|
303 FFTDataServer *server = *m_releasedServers.begin();
|
Chris@148
|
304
|
Chris@215
|
305 bool found = false;
|
Chris@215
|
306
|
Chris@215
|
307 for (ServerMap::iterator i = m_servers.begin(); i != m_servers.end(); ++i) {
|
Chris@215
|
308
|
Chris@215
|
309 if (i->second.first == server) {
|
Chris@215
|
310 found = true;
|
Chris@215
|
311 if (i->second.second > 0) {
|
Chris@215
|
312 std::cerr << "ERROR: FFTDataServer::purgeLimbo: Server "
|
Chris@215
|
313 << server << " is in released queue, but still has non-zero refcount "
|
Chris@215
|
314 << i->second.second << std::endl;
|
Chris@215
|
315 // ... so don't delete it
|
Chris@215
|
316 break;
|
Chris@215
|
317 }
|
Chris@148
|
318 m_servers.erase(i);
|
Chris@215
|
319 delete server;
|
Chris@215
|
320 break;
|
Chris@148
|
321 }
|
Chris@148
|
322 }
|
Chris@215
|
323
|
Chris@215
|
324 if (!found) {
|
Chris@215
|
325 std::cerr << "ERROR: FFTDataServer::purgeLimbo: Server "
|
Chris@215
|
326 << server << " is in released queue, but not in server map!"
|
Chris@215
|
327 << std::endl;
|
Chris@215
|
328 delete server;
|
Chris@215
|
329 }
|
Chris@215
|
330
|
Chris@215
|
331 m_releasedServers.pop_front();
|
Chris@215
|
332 }
|
Chris@215
|
333 }
|
Chris@215
|
334
|
Chris@215
|
335 void
|
Chris@215
|
336 FFTDataServer::modelAboutToBeDeleted(Model *model)
|
Chris@215
|
337 {
|
Chris@215
|
338 QMutexLocker locker(&m_serverMapMutex);
|
Chris@215
|
339
|
Chris@215
|
340 for (ServerMap::iterator i = m_servers.begin(); i != m_servers.end(); ++i) {
|
Chris@215
|
341
|
Chris@215
|
342 FFTDataServer *server = i->second.first;
|
Chris@215
|
343
|
Chris@215
|
344 if (server->getModel() == model) {
|
Chris@215
|
345 if (i->second.second > 0) {
|
Chris@215
|
346 std::cerr << "ERROR: FFTDataServer::modelAboutToBeDeleted: Model " << model << " (\"" << model->objectName().toStdString() << "\") is about to be deleted, but is still being referred to by FFT server " << server << " with non-zero refcount " << i->second.second << std::endl;
|
Chris@215
|
347 }
|
Chris@215
|
348 for (ServerQueue::iterator j = m_releasedServers.begin();
|
Chris@215
|
349 j != m_releasedServers.end(); ++j) {
|
Chris@215
|
350 if (*j == server) {
|
Chris@215
|
351 m_releasedServers.erase(j);
|
Chris@215
|
352 break;
|
Chris@215
|
353 }
|
Chris@215
|
354 }
|
Chris@215
|
355 m_servers.erase(i);
|
Chris@215
|
356 delete server;
|
Chris@215
|
357 return;
|
Chris@215
|
358 }
|
Chris@148
|
359 }
|
Chris@148
|
360 }
|
Chris@148
|
361
|
Chris@148
|
362 FFTDataServer::FFTDataServer(QString fileBaseName,
|
Chris@148
|
363 const DenseTimeValueModel *model,
|
Chris@148
|
364 int channel,
|
Chris@148
|
365 WindowType windowType,
|
Chris@148
|
366 size_t windowSize,
|
Chris@148
|
367 size_t windowIncrement,
|
Chris@148
|
368 size_t fftSize,
|
Chris@148
|
369 bool polar,
|
Chris@148
|
370 size_t fillFromColumn) :
|
Chris@148
|
371 m_fileBaseName(fileBaseName),
|
Chris@148
|
372 m_model(model),
|
Chris@148
|
373 m_channel(channel),
|
Chris@148
|
374 m_windower(windowType, windowSize),
|
Chris@148
|
375 m_windowSize(windowSize),
|
Chris@148
|
376 m_windowIncrement(windowIncrement),
|
Chris@148
|
377 m_fftSize(fftSize),
|
Chris@148
|
378 m_polar(polar),
|
Chris@183
|
379 m_width(0),
|
Chris@183
|
380 m_height(0),
|
Chris@183
|
381 m_cacheWidth(0),
|
Chris@172
|
382 m_memoryCache(false),
|
Chris@172
|
383 m_compactCache(false),
|
Chris@148
|
384 m_lastUsedCache(-1),
|
Chris@148
|
385 m_fftInput(0),
|
Chris@148
|
386 m_exiting(false),
|
Chris@153
|
387 m_suspended(true), //!!! or false?
|
Chris@148
|
388 m_fillThread(0)
|
Chris@148
|
389 {
|
Chris@193
|
390 #ifdef DEBUG_FFT_SERVER
|
Chris@193
|
391 std::cerr << "FFTDataServer(" << this << " [" << (void *)QThread::currentThreadId() << "])::FFTDataServer" << std::endl;
|
Chris@193
|
392 #endif
|
Chris@193
|
393
|
Chris@148
|
394 size_t start = m_model->getStartFrame();
|
Chris@148
|
395 size_t end = m_model->getEndFrame();
|
Chris@148
|
396
|
Chris@148
|
397 m_width = (end - start) / m_windowIncrement + 1;
|
Chris@203
|
398 m_height = m_fftSize / 2 + 1; // DC == 0, Nyquist == fftsize/2
|
Chris@148
|
399
|
Chris@148
|
400 size_t maxCacheSize = 20 * 1024 * 1024;
|
Chris@148
|
401 size_t columnSize = m_height * sizeof(fftsample) * 2 + sizeof(fftsample);
|
Chris@148
|
402 if (m_width * columnSize < maxCacheSize * 2) m_cacheWidth = m_width;
|
Chris@148
|
403 else m_cacheWidth = maxCacheSize / columnSize;
|
Chris@148
|
404
|
Chris@148
|
405 int bits = 0;
|
Chris@148
|
406 while (m_cacheWidth) { m_cacheWidth >>= 1; ++bits; }
|
Chris@148
|
407 m_cacheWidth = 2;
|
Chris@148
|
408 while (bits) { m_cacheWidth <<= 1; --bits; }
|
Chris@172
|
409
|
Chris@172
|
410 //!!! Need to pass in what this server is intended for
|
Chris@172
|
411 // (e.g. playback processing, spectrogram, feature extraction),
|
Chris@172
|
412 // or pass in something akin to the storage adviser criteria.
|
Chris@172
|
413 // That probably goes alongside the polar argument.
|
Chris@172
|
414 // For now we'll assume "spectrogram" criteria for polar ffts,
|
Chris@172
|
415 // and "feature extraction" criteria for rectangular ones.
|
Chris@172
|
416
|
Chris@172
|
417 StorageAdviser::Criteria criteria;
|
Chris@172
|
418 if (m_polar) {
|
Chris@172
|
419 criteria = StorageAdviser::Criteria
|
Chris@172
|
420 (StorageAdviser::SpeedCritical | StorageAdviser::LongRetentionLikely);
|
Chris@172
|
421 } else {
|
Chris@172
|
422 criteria = StorageAdviser::Criteria(StorageAdviser::PrecisionCritical);
|
Chris@172
|
423 }
|
Chris@172
|
424
|
Chris@172
|
425 int cells = m_width * m_height;
|
Chris@172
|
426 int minimumSize = (cells / 1024) * sizeof(uint16_t); // kb
|
Chris@172
|
427 int maximumSize = (cells / 1024) * sizeof(float); // kb
|
Chris@215
|
428
|
Chris@215
|
429 StorageAdviser::Recommendation recommendation;
|
Chris@172
|
430
|
Chris@215
|
431 try {
|
Chris@215
|
432
|
Chris@215
|
433 recommendation =
|
Chris@215
|
434 StorageAdviser::recommend(criteria, minimumSize, maximumSize);
|
Chris@215
|
435
|
Chris@215
|
436 } catch (InsufficientDiscSpace s) {
|
Chris@215
|
437
|
Chris@215
|
438 // Delete any unused servers we may have been leaving around
|
Chris@215
|
439 // in case we wanted them again
|
Chris@215
|
440
|
Chris@215
|
441 purgeLimbo(0);
|
Chris@215
|
442
|
Chris@215
|
443 // This time we don't catch InsufficientDiscSpace -- we
|
Chris@215
|
444 // haven't allocated anything yet and can safely let the
|
Chris@215
|
445 // exception out to indicate to the caller that we can't
|
Chris@215
|
446 // handle it.
|
Chris@215
|
447
|
Chris@215
|
448 recommendation =
|
Chris@215
|
449 StorageAdviser::recommend(criteria, minimumSize, maximumSize);
|
Chris@215
|
450 }
|
Chris@172
|
451
|
Chris@172
|
452 std::cerr << "Recommendation was: " << recommendation << std::endl;
|
Chris@172
|
453
|
Chris@172
|
454 m_memoryCache = ((recommendation & StorageAdviser::UseMemory) ||
|
Chris@172
|
455 (recommendation & StorageAdviser::PreferMemory));
|
Chris@172
|
456
|
Chris@172
|
457 m_compactCache = (recommendation & StorageAdviser::ConserveSpace);
|
Chris@148
|
458
|
Chris@148
|
459 #ifdef DEBUG_FFT_SERVER
|
Chris@148
|
460 std::cerr << "Width " << m_width << ", cache width " << m_cacheWidth << " (size " << m_cacheWidth * columnSize << ")" << std::endl;
|
Chris@148
|
461 #endif
|
Chris@148
|
462
|
Chris@205
|
463 StorageAdviser::notifyPlannedAllocation
|
Chris@205
|
464 (m_memoryCache ? StorageAdviser::MemoryAllocation :
|
Chris@205
|
465 StorageAdviser::DiscAllocation,
|
Chris@205
|
466 m_compactCache ? minimumSize : maximumSize);
|
Chris@205
|
467
|
Chris@148
|
468 for (size_t i = 0; i <= m_width / m_cacheWidth; ++i) {
|
Chris@148
|
469 m_caches.push_back(0);
|
Chris@148
|
470 }
|
Chris@148
|
471
|
Chris@148
|
472 m_fftInput = (fftsample *)
|
Chris@148
|
473 fftwf_malloc(fftSize * sizeof(fftsample));
|
Chris@148
|
474
|
Chris@148
|
475 m_fftOutput = (fftwf_complex *)
|
Chris@203
|
476 fftwf_malloc((fftSize/2 + 1) * sizeof(fftwf_complex));
|
Chris@148
|
477
|
Chris@148
|
478 m_workbuffer = (float *)
|
Chris@203
|
479 fftwf_malloc((fftSize+2) * sizeof(float));
|
Chris@148
|
480
|
Chris@148
|
481 m_fftPlan = fftwf_plan_dft_r2c_1d(m_fftSize,
|
Chris@148
|
482 m_fftInput,
|
Chris@148
|
483 m_fftOutput,
|
Chris@148
|
484 FFTW_ESTIMATE);
|
Chris@148
|
485
|
Chris@148
|
486 if (!m_fftPlan) {
|
Chris@148
|
487 std::cerr << "ERROR: fftwf_plan_dft_r2c_1d(" << m_windowSize << ") failed!" << std::endl;
|
Chris@148
|
488 throw(0);
|
Chris@148
|
489 }
|
Chris@148
|
490
|
Chris@148
|
491 m_fillThread = new FillThread(*this, fillFromColumn);
|
Chris@148
|
492 }
|
Chris@148
|
493
|
Chris@148
|
494 FFTDataServer::~FFTDataServer()
|
Chris@148
|
495 {
|
Chris@148
|
496 #ifdef DEBUG_FFT_SERVER
|
Chris@193
|
497 std::cerr << "FFTDataServer(" << this << " [" << (void *)QThread::currentThreadId() << "])::~FFTDataServer()" << std::endl;
|
Chris@148
|
498 #endif
|
Chris@148
|
499
|
Chris@155
|
500 m_suspended = false;
|
Chris@148
|
501 m_exiting = true;
|
Chris@148
|
502 m_condition.wakeAll();
|
Chris@148
|
503 if (m_fillThread) {
|
Chris@148
|
504 m_fillThread->wait();
|
Chris@148
|
505 delete m_fillThread;
|
Chris@148
|
506 }
|
Chris@148
|
507
|
Chris@148
|
508 QMutexLocker locker(&m_writeMutex);
|
Chris@148
|
509
|
Chris@148
|
510 for (CacheVector::iterator i = m_caches.begin(); i != m_caches.end(); ++i) {
|
Chris@205
|
511 if (*i) {
|
Chris@205
|
512 delete *i;
|
Chris@205
|
513 } else {
|
Chris@205
|
514 StorageAdviser::notifyDoneAllocation
|
Chris@205
|
515 (m_memoryCache ? StorageAdviser::MemoryAllocation :
|
Chris@205
|
516 StorageAdviser::DiscAllocation,
|
Chris@205
|
517 m_cacheWidth * m_height *
|
Chris@205
|
518 (m_compactCache ? sizeof(uint16_t) : sizeof(float)) / 1024 + 1);
|
Chris@205
|
519 }
|
Chris@148
|
520 }
|
Chris@148
|
521
|
Chris@148
|
522 deleteProcessingData();
|
Chris@148
|
523 }
|
Chris@148
|
524
|
Chris@148
|
525 void
|
Chris@148
|
526 FFTDataServer::deleteProcessingData()
|
Chris@148
|
527 {
|
Chris@193
|
528 #ifdef DEBUG_FFT_SERVER
|
Chris@193
|
529 std::cerr << "FFTDataServer(" << this << " [" << (void *)QThread::currentThreadId() << "]): deleteProcessingData" << std::endl;
|
Chris@193
|
530 #endif
|
Chris@148
|
531 if (m_fftInput) {
|
Chris@148
|
532 fftwf_destroy_plan(m_fftPlan);
|
Chris@148
|
533 fftwf_free(m_fftInput);
|
Chris@148
|
534 fftwf_free(m_fftOutput);
|
Chris@148
|
535 fftwf_free(m_workbuffer);
|
Chris@148
|
536 }
|
Chris@148
|
537 m_fftInput = 0;
|
Chris@148
|
538 }
|
Chris@148
|
539
|
Chris@148
|
540 void
|
Chris@148
|
541 FFTDataServer::suspend()
|
Chris@148
|
542 {
|
Chris@148
|
543 #ifdef DEBUG_FFT_SERVER
|
Chris@193
|
544 std::cerr << "FFTDataServer(" << this << " [" << (void *)QThread::currentThreadId() << "]): suspend" << std::endl;
|
Chris@148
|
545 #endif
|
Chris@183
|
546 Profiler profiler("FFTDataServer::suspend", false);
|
Chris@183
|
547
|
Chris@148
|
548 QMutexLocker locker(&m_writeMutex);
|
Chris@148
|
549 m_suspended = true;
|
Chris@148
|
550 for (CacheVector::iterator i = m_caches.begin(); i != m_caches.end(); ++i) {
|
Chris@148
|
551 if (*i) (*i)->suspend();
|
Chris@148
|
552 }
|
Chris@148
|
553 }
|
Chris@148
|
554
|
Chris@148
|
555 void
|
Chris@155
|
556 FFTDataServer::suspendWrites()
|
Chris@155
|
557 {
|
Chris@155
|
558 #ifdef DEBUG_FFT_SERVER
|
Chris@193
|
559 std::cerr << "FFTDataServer(" << this << " [" << (void *)QThread::currentThreadId() << "]): suspendWrites" << std::endl;
|
Chris@155
|
560 #endif
|
Chris@183
|
561 Profiler profiler("FFTDataServer::suspendWrites", false);
|
Chris@183
|
562
|
Chris@155
|
563 m_suspended = true;
|
Chris@155
|
564 }
|
Chris@155
|
565
|
Chris@155
|
566 void
|
Chris@148
|
567 FFTDataServer::resume()
|
Chris@148
|
568 {
|
Chris@154
|
569 #ifdef DEBUG_FFT_SERVER
|
Chris@193
|
570 std::cerr << "FFTDataServer(" << this << " [" << (void *)QThread::currentThreadId() << "]): resume" << std::endl;
|
Chris@154
|
571 #endif
|
Chris@183
|
572 Profiler profiler("FFTDataServer::resume", false);
|
Chris@183
|
573
|
Chris@148
|
574 m_suspended = false;
|
Chris@157
|
575 if (m_fillThread) {
|
Chris@157
|
576 if (m_fillThread->isFinished()) {
|
Chris@157
|
577 delete m_fillThread;
|
Chris@157
|
578 m_fillThread = 0;
|
Chris@157
|
579 deleteProcessingData();
|
Chris@157
|
580 } else {
|
Chris@157
|
581 m_condition.wakeAll();
|
Chris@157
|
582 }
|
Chris@157
|
583 }
|
Chris@148
|
584 }
|
Chris@148
|
585
|
Chris@148
|
586 FFTCache *
|
Chris@148
|
587 FFTDataServer::getCacheAux(size_t c)
|
Chris@148
|
588 {
|
Chris@183
|
589 Profiler profiler("FFTDataServer::getCacheAux", false);
|
Chris@193
|
590 #ifdef DEBUG_FFT_SERVER
|
Chris@193
|
591 std::cerr << "FFTDataServer(" << this << " [" << (void *)QThread::currentThreadId() << "])::getCacheAux" << std::endl;
|
Chris@193
|
592 #endif
|
Chris@183
|
593
|
Chris@148
|
594 QMutexLocker locker(&m_writeMutex);
|
Chris@148
|
595
|
Chris@148
|
596 if (m_lastUsedCache == -1) {
|
Chris@148
|
597 m_fillThread->start();
|
Chris@148
|
598 }
|
Chris@148
|
599
|
Chris@148
|
600 if (int(c) != m_lastUsedCache) {
|
Chris@148
|
601
|
Chris@148
|
602 // std::cerr << "switch from " << m_lastUsedCache << " to " << c << std::endl;
|
Chris@148
|
603
|
Chris@148
|
604 for (IntQueue::iterator i = m_dormantCaches.begin();
|
Chris@148
|
605 i != m_dormantCaches.end(); ++i) {
|
Chris@148
|
606 if (*i == c) {
|
Chris@148
|
607 m_dormantCaches.erase(i);
|
Chris@148
|
608 break;
|
Chris@148
|
609 }
|
Chris@148
|
610 }
|
Chris@148
|
611
|
Chris@148
|
612 if (m_lastUsedCache >= 0) {
|
Chris@148
|
613 bool inDormant = false;
|
Chris@148
|
614 for (size_t i = 0; i < m_dormantCaches.size(); ++i) {
|
Chris@148
|
615 if (m_dormantCaches[i] == m_lastUsedCache) {
|
Chris@148
|
616 inDormant = true;
|
Chris@148
|
617 break;
|
Chris@148
|
618 }
|
Chris@148
|
619 }
|
Chris@148
|
620 if (!inDormant) {
|
Chris@148
|
621 m_dormantCaches.push_back(m_lastUsedCache);
|
Chris@148
|
622 }
|
Chris@148
|
623 while (m_dormantCaches.size() > 4) {
|
Chris@148
|
624 int dc = m_dormantCaches.front();
|
Chris@148
|
625 m_dormantCaches.pop_front();
|
Chris@148
|
626 m_caches[dc]->suspend();
|
Chris@148
|
627 }
|
Chris@148
|
628 }
|
Chris@148
|
629 }
|
Chris@148
|
630
|
Chris@148
|
631 if (m_caches[c]) {
|
Chris@148
|
632 m_lastUsedCache = c;
|
Chris@148
|
633 return m_caches[c];
|
Chris@148
|
634 }
|
Chris@148
|
635
|
Chris@148
|
636 QString name = QString("%1-%2").arg(m_fileBaseName).arg(c);
|
Chris@148
|
637
|
Chris@172
|
638 FFTCache *cache = 0;
|
Chris@172
|
639
|
Chris@213
|
640 size_t width = m_cacheWidth;
|
Chris@213
|
641 if (c * m_cacheWidth + width > m_width) {
|
Chris@213
|
642 width = m_width - c * m_cacheWidth;
|
Chris@213
|
643 }
|
Chris@213
|
644
|
Chris@200
|
645 try {
|
Chris@200
|
646
|
Chris@200
|
647 if (m_memoryCache) {
|
Chris@172
|
648
|
Chris@200
|
649 cache = new FFTMemoryCache();
|
Chris@172
|
650
|
Chris@200
|
651 } else if (m_compactCache) {
|
Chris@172
|
652
|
Chris@200
|
653 cache = new FFTFileCache(name, MatrixFile::ReadWrite,
|
Chris@200
|
654 FFTFileCache::Compact);
|
Chris@172
|
655
|
Chris@200
|
656 } else {
|
Chris@172
|
657
|
Chris@200
|
658 cache = new FFTFileCache(name, MatrixFile::ReadWrite,
|
Chris@200
|
659 m_polar ? FFTFileCache::Polar :
|
Chris@200
|
660 FFTFileCache::Rectangular);
|
Chris@200
|
661 }
|
Chris@200
|
662
|
Chris@200
|
663 cache->resize(width, m_height);
|
Chris@200
|
664 cache->reset();
|
Chris@200
|
665
|
Chris@213
|
666 } catch (std::bad_alloc) {
|
Chris@205
|
667
|
Chris@213
|
668 delete cache;
|
Chris@213
|
669 cache = 0;
|
Chris@213
|
670
|
Chris@213
|
671 if (m_memoryCache) {
|
Chris@213
|
672
|
Chris@213
|
673 std::cerr << "WARNING: Memory allocation failed when resizing"
|
Chris@213
|
674 << " FFT memory cache no. " << c << " to " << width
|
Chris@213
|
675 << "x" << m_height << " (of total width " << m_width
|
Chris@213
|
676 << "): falling back to disc cache" << std::endl;
|
Chris@213
|
677
|
Chris@213
|
678 try {
|
Chris@213
|
679
|
Chris@213
|
680 cache = new FFTFileCache(name, MatrixFile::ReadWrite,
|
Chris@213
|
681 FFTFileCache::Compact);
|
Chris@213
|
682
|
Chris@213
|
683 cache->resize(width, m_height);
|
Chris@213
|
684 cache->reset();
|
Chris@213
|
685
|
Chris@213
|
686 } catch (std::bad_alloc) {
|
Chris@213
|
687
|
Chris@213
|
688 delete cache;
|
Chris@213
|
689 cache = 0;
|
Chris@213
|
690 }
|
Chris@213
|
691 }
|
Chris@213
|
692
|
Chris@213
|
693 if (cache) {
|
Chris@213
|
694 std::cerr << "ERROR: Memory allocation failed when resizing"
|
Chris@213
|
695 << " FFT file cache no. " << c << " to " << width
|
Chris@213
|
696 << "x" << m_height << " (of total width " << m_width
|
Chris@213
|
697 << "): abandoning this cache" << std::endl;
|
Chris@213
|
698 }
|
Chris@213
|
699
|
Chris@200
|
700 //!!! Shouldn't be using QtGui here. Need a better way to report this.
|
Chris@200
|
701 QMessageBox::critical
|
Chris@200
|
702 (0, QApplication::tr("FFT cache resize failed"),
|
Chris@200
|
703 QApplication::tr
|
Chris@200
|
704 ("Failed to create or resize an FFT model slice.\n"
|
Chris@200
|
705 "There may be insufficient memory or disc space to continue."));
|
Chris@172
|
706 }
|
Chris@148
|
707
|
Chris@213
|
708 StorageAdviser::notifyDoneAllocation
|
Chris@213
|
709 (m_memoryCache ? StorageAdviser::MemoryAllocation :
|
Chris@213
|
710 StorageAdviser::DiscAllocation,
|
Chris@213
|
711 width * m_height *
|
Chris@213
|
712 (m_compactCache ? sizeof(uint16_t) : sizeof(float)) / 1024 + 1);
|
Chris@213
|
713
|
Chris@148
|
714 m_caches[c] = cache;
|
Chris@148
|
715 m_lastUsedCache = c;
|
Chris@148
|
716 return cache;
|
Chris@148
|
717 }
|
Chris@148
|
718
|
Chris@148
|
719 float
|
Chris@148
|
720 FFTDataServer::getMagnitudeAt(size_t x, size_t y)
|
Chris@148
|
721 {
|
Chris@183
|
722 Profiler profiler("FFTDataServer::getMagnitudeAt", false);
|
Chris@183
|
723
|
Chris@148
|
724 size_t col;
|
Chris@148
|
725 FFTCache *cache = getCache(x, col);
|
Chris@200
|
726 if (!cache) return 0;
|
Chris@148
|
727
|
Chris@148
|
728 if (!cache->haveSetColumnAt(col)) {
|
Chris@183
|
729 std::cerr << "FFTDataServer::getMagnitudeAt: calling fillColumn("
|
Chris@183
|
730 << x << ")" << std::endl;
|
Chris@148
|
731 fillColumn(x);
|
Chris@148
|
732 }
|
Chris@148
|
733 return cache->getMagnitudeAt(col, y);
|
Chris@148
|
734 }
|
Chris@148
|
735
|
Chris@148
|
736 float
|
Chris@148
|
737 FFTDataServer::getNormalizedMagnitudeAt(size_t x, size_t y)
|
Chris@148
|
738 {
|
Chris@183
|
739 Profiler profiler("FFTDataServer::getNormalizedMagnitudeAt", false);
|
Chris@183
|
740
|
Chris@148
|
741 size_t col;
|
Chris@148
|
742 FFTCache *cache = getCache(x, col);
|
Chris@200
|
743 if (!cache) return 0;
|
Chris@148
|
744
|
Chris@148
|
745 if (!cache->haveSetColumnAt(col)) {
|
Chris@148
|
746 fillColumn(x);
|
Chris@148
|
747 }
|
Chris@148
|
748 return cache->getNormalizedMagnitudeAt(col, y);
|
Chris@148
|
749 }
|
Chris@148
|
750
|
Chris@148
|
751 float
|
Chris@148
|
752 FFTDataServer::getMaximumMagnitudeAt(size_t x)
|
Chris@148
|
753 {
|
Chris@183
|
754 Profiler profiler("FFTDataServer::getMaximumMagnitudeAt", false);
|
Chris@183
|
755
|
Chris@148
|
756 size_t col;
|
Chris@148
|
757 FFTCache *cache = getCache(x, col);
|
Chris@200
|
758 if (!cache) return 0;
|
Chris@148
|
759
|
Chris@148
|
760 if (!cache->haveSetColumnAt(col)) {
|
Chris@148
|
761 fillColumn(x);
|
Chris@148
|
762 }
|
Chris@148
|
763 return cache->getMaximumMagnitudeAt(col);
|
Chris@148
|
764 }
|
Chris@148
|
765
|
Chris@148
|
766 float
|
Chris@148
|
767 FFTDataServer::getPhaseAt(size_t x, size_t y)
|
Chris@148
|
768 {
|
Chris@183
|
769 Profiler profiler("FFTDataServer::getPhaseAt", false);
|
Chris@183
|
770
|
Chris@148
|
771 size_t col;
|
Chris@148
|
772 FFTCache *cache = getCache(x, col);
|
Chris@200
|
773 if (!cache) return 0;
|
Chris@148
|
774
|
Chris@148
|
775 if (!cache->haveSetColumnAt(col)) {
|
Chris@148
|
776 fillColumn(x);
|
Chris@148
|
777 }
|
Chris@148
|
778 return cache->getPhaseAt(col, y);
|
Chris@148
|
779 }
|
Chris@148
|
780
|
Chris@148
|
781 void
|
Chris@148
|
782 FFTDataServer::getValuesAt(size_t x, size_t y, float &real, float &imaginary)
|
Chris@148
|
783 {
|
Chris@183
|
784 Profiler profiler("FFTDataServer::getValuesAt", false);
|
Chris@183
|
785
|
Chris@148
|
786 size_t col;
|
Chris@148
|
787 FFTCache *cache = getCache(x, col);
|
Chris@200
|
788 if (!cache) { real = 0; imaginary = 0; return; }
|
Chris@148
|
789
|
Chris@148
|
790 if (!cache->haveSetColumnAt(col)) {
|
Chris@148
|
791 #ifdef DEBUG_FFT_SERVER
|
Chris@148
|
792 std::cerr << "FFTDataServer::getValuesAt(" << x << ", " << y << "): filling" << std::endl;
|
Chris@148
|
793 #endif
|
Chris@148
|
794 fillColumn(x);
|
Chris@148
|
795 }
|
Chris@148
|
796 float magnitude = cache->getMagnitudeAt(col, y);
|
Chris@148
|
797 float phase = cache->getPhaseAt(col, y);
|
Chris@148
|
798 real = magnitude * cosf(phase);
|
Chris@148
|
799 imaginary = magnitude * sinf(phase);
|
Chris@148
|
800 }
|
Chris@148
|
801
|
Chris@148
|
802 bool
|
Chris@148
|
803 FFTDataServer::isColumnReady(size_t x)
|
Chris@148
|
804 {
|
Chris@183
|
805 Profiler profiler("FFTDataServer::isColumnReady", false);
|
Chris@183
|
806
|
Chris@148
|
807 if (!haveCache(x)) {
|
Chris@148
|
808 if (m_lastUsedCache == -1) {
|
Chris@183
|
809 if (m_suspended) {
|
Chris@183
|
810 std::cerr << "FFTDataServer::isColumnReady(" << x << "): no cache, calling resume" << std::endl;
|
Chris@183
|
811 resume();
|
Chris@183
|
812 }
|
Chris@148
|
813 m_fillThread->start();
|
Chris@148
|
814 }
|
Chris@148
|
815 return false;
|
Chris@148
|
816 }
|
Chris@148
|
817
|
Chris@148
|
818 size_t col;
|
Chris@148
|
819 FFTCache *cache = getCache(x, col);
|
Chris@200
|
820 if (!cache) return true;
|
Chris@148
|
821
|
Chris@148
|
822 return cache->haveSetColumnAt(col);
|
Chris@148
|
823 }
|
Chris@148
|
824
|
Chris@148
|
825 void
|
Chris@148
|
826 FFTDataServer::fillColumn(size_t x)
|
Chris@148
|
827 {
|
Chris@183
|
828 Profiler profiler("FFTDataServer::fillColumn", false);
|
Chris@183
|
829
|
Chris@148
|
830 size_t col;
|
Chris@148
|
831 #ifdef DEBUG_FFT_SERVER_FILL
|
Chris@148
|
832 std::cout << "FFTDataServer::fillColumn(" << x << ")" << std::endl;
|
Chris@148
|
833 #endif
|
Chris@148
|
834 FFTCache *cache = getCache(x, col);
|
Chris@200
|
835 if (!cache) return;
|
Chris@148
|
836
|
Chris@148
|
837 QMutexLocker locker(&m_writeMutex);
|
Chris@148
|
838
|
Chris@148
|
839 if (cache->haveSetColumnAt(col)) return;
|
Chris@148
|
840
|
Chris@148
|
841 int startFrame = m_windowIncrement * x;
|
Chris@148
|
842 int endFrame = startFrame + m_windowSize;
|
Chris@148
|
843
|
Chris@148
|
844 startFrame -= int(m_windowSize - m_windowIncrement) / 2;
|
Chris@148
|
845 endFrame -= int(m_windowSize - m_windowIncrement) / 2;
|
Chris@148
|
846 size_t pfx = 0;
|
Chris@148
|
847
|
Chris@148
|
848 size_t off = (m_fftSize - m_windowSize) / 2;
|
Chris@148
|
849
|
Chris@148
|
850 for (size_t i = 0; i < off; ++i) {
|
Chris@148
|
851 m_fftInput[i] = 0.0;
|
Chris@148
|
852 m_fftInput[m_fftSize - i - 1] = 0.0;
|
Chris@148
|
853 }
|
Chris@148
|
854
|
Chris@148
|
855 if (startFrame < 0) {
|
Chris@148
|
856 pfx = size_t(-startFrame);
|
Chris@148
|
857 for (size_t i = 0; i < pfx; ++i) {
|
Chris@148
|
858 m_fftInput[off + i] = 0.0;
|
Chris@148
|
859 }
|
Chris@148
|
860 }
|
Chris@148
|
861
|
Chris@195
|
862 #ifdef DEBUG_FFT_SERVER_FILL
|
Chris@193
|
863 std::cerr << "FFTDataServer::fillColumn: requesting frames "
|
Chris@193
|
864 << startFrame + pfx << " -> " << endFrame << " ( = "
|
Chris@193
|
865 << endFrame - (startFrame + pfx) << ") at index "
|
Chris@193
|
866 << off + pfx << " in buffer of size " << m_fftSize
|
Chris@193
|
867 << " with window size " << m_windowSize
|
Chris@193
|
868 << " from channel " << m_channel << std::endl;
|
Chris@195
|
869 #endif
|
Chris@193
|
870
|
Chris@148
|
871 size_t got = m_model->getValues(m_channel, startFrame + pfx,
|
Chris@148
|
872 endFrame, m_fftInput + off + pfx);
|
Chris@148
|
873
|
Chris@148
|
874 while (got + pfx < m_windowSize) {
|
Chris@148
|
875 m_fftInput[off + got + pfx] = 0.0;
|
Chris@148
|
876 ++got;
|
Chris@148
|
877 }
|
Chris@148
|
878
|
Chris@148
|
879 if (m_channel == -1) {
|
Chris@148
|
880 int channels = m_model->getChannelCount();
|
Chris@148
|
881 if (channels > 1) {
|
Chris@148
|
882 for (size_t i = 0; i < m_windowSize; ++i) {
|
Chris@148
|
883 m_fftInput[off + i] /= channels;
|
Chris@148
|
884 }
|
Chris@148
|
885 }
|
Chris@148
|
886 }
|
Chris@148
|
887
|
Chris@148
|
888 m_windower.cut(m_fftInput + off);
|
Chris@148
|
889
|
Chris@148
|
890 for (size_t i = 0; i < m_fftSize/2; ++i) {
|
Chris@148
|
891 fftsample temp = m_fftInput[i];
|
Chris@148
|
892 m_fftInput[i] = m_fftInput[i + m_fftSize/2];
|
Chris@148
|
893 m_fftInput[i + m_fftSize/2] = temp;
|
Chris@148
|
894 }
|
Chris@148
|
895
|
Chris@148
|
896 fftwf_execute(m_fftPlan);
|
Chris@148
|
897
|
Chris@148
|
898 fftsample factor = 0.0;
|
Chris@148
|
899
|
Chris@203
|
900 for (size_t i = 0; i <= m_fftSize/2; ++i) {
|
Chris@148
|
901
|
Chris@148
|
902 fftsample mag = sqrtf(m_fftOutput[i][0] * m_fftOutput[i][0] +
|
Chris@148
|
903 m_fftOutput[i][1] * m_fftOutput[i][1]);
|
Chris@148
|
904 mag /= m_windowSize / 2;
|
Chris@148
|
905
|
Chris@148
|
906 if (mag > factor) factor = mag;
|
Chris@148
|
907
|
Chris@148
|
908 fftsample phase = atan2f(m_fftOutput[i][1], m_fftOutput[i][0]);
|
Chris@148
|
909 phase = princargf(phase);
|
Chris@148
|
910
|
Chris@148
|
911 m_workbuffer[i] = mag;
|
Chris@203
|
912 m_workbuffer[i + m_fftSize/2+1] = phase;
|
Chris@148
|
913 }
|
Chris@148
|
914
|
Chris@148
|
915 cache->setColumnAt(col,
|
Chris@148
|
916 m_workbuffer,
|
Chris@203
|
917 m_workbuffer + m_fftSize/2+1,
|
Chris@148
|
918 factor);
|
Chris@154
|
919
|
Chris@183
|
920 if (m_suspended) {
|
Chris@183
|
921 // std::cerr << "FFTDataServer::fillColumn(" << x << "): calling resume" << std::endl;
|
Chris@183
|
922 // resume();
|
Chris@183
|
923 }
|
Chris@148
|
924 }
|
Chris@148
|
925
|
Chris@148
|
926 size_t
|
Chris@148
|
927 FFTDataServer::getFillCompletion() const
|
Chris@148
|
928 {
|
Chris@148
|
929 if (m_fillThread) return m_fillThread->getCompletion();
|
Chris@148
|
930 else return 100;
|
Chris@148
|
931 }
|
Chris@148
|
932
|
Chris@148
|
933 size_t
|
Chris@148
|
934 FFTDataServer::getFillExtent() const
|
Chris@148
|
935 {
|
Chris@148
|
936 if (m_fillThread) return m_fillThread->getExtent();
|
Chris@148
|
937 else return m_model->getEndFrame();
|
Chris@148
|
938 }
|
Chris@148
|
939
|
Chris@148
|
940 QString
|
Chris@148
|
941 FFTDataServer::generateFileBasename() const
|
Chris@148
|
942 {
|
Chris@148
|
943 return generateFileBasename(m_model, m_channel, m_windower.getType(),
|
Chris@148
|
944 m_windowSize, m_windowIncrement, m_fftSize,
|
Chris@148
|
945 m_polar);
|
Chris@148
|
946 }
|
Chris@148
|
947
|
Chris@148
|
948 QString
|
Chris@148
|
949 FFTDataServer::generateFileBasename(const DenseTimeValueModel *model,
|
Chris@148
|
950 int channel,
|
Chris@148
|
951 WindowType windowType,
|
Chris@148
|
952 size_t windowSize,
|
Chris@148
|
953 size_t windowIncrement,
|
Chris@148
|
954 size_t fftSize,
|
Chris@148
|
955 bool polar)
|
Chris@148
|
956 {
|
Chris@148
|
957 char buffer[200];
|
Chris@148
|
958
|
Chris@148
|
959 sprintf(buffer, "%u-%u-%u-%u-%u-%u%s",
|
Chris@148
|
960 (unsigned int)XmlExportable::getObjectExportId(model),
|
Chris@148
|
961 (unsigned int)(channel + 1),
|
Chris@148
|
962 (unsigned int)windowType,
|
Chris@148
|
963 (unsigned int)windowSize,
|
Chris@148
|
964 (unsigned int)windowIncrement,
|
Chris@148
|
965 (unsigned int)fftSize,
|
Chris@148
|
966 polar ? "-p" : "-r");
|
Chris@148
|
967
|
Chris@148
|
968 return buffer;
|
Chris@148
|
969 }
|
Chris@148
|
970
|
Chris@148
|
971 void
|
Chris@148
|
972 FFTDataServer::FillThread::run()
|
Chris@148
|
973 {
|
Chris@148
|
974 m_extent = 0;
|
Chris@148
|
975 m_completion = 0;
|
Chris@148
|
976
|
Chris@148
|
977 size_t start = m_server.m_model->getStartFrame();
|
Chris@148
|
978 size_t end = m_server.m_model->getEndFrame();
|
Chris@148
|
979 size_t remainingEnd = end;
|
Chris@148
|
980
|
Chris@148
|
981 int counter = 0;
|
Chris@148
|
982 int updateAt = (end / m_server.m_windowIncrement) / 20;
|
Chris@148
|
983 if (updateAt < 100) updateAt = 100;
|
Chris@148
|
984
|
Chris@148
|
985 if (m_fillFrom > start) {
|
Chris@148
|
986
|
Chris@148
|
987 for (size_t f = m_fillFrom; f < end; f += m_server.m_windowIncrement) {
|
Chris@148
|
988
|
Chris@148
|
989 m_server.fillColumn(int((f - start) / m_server.m_windowIncrement));
|
Chris@148
|
990
|
Chris@148
|
991 if (m_server.m_exiting) return;
|
Chris@148
|
992
|
Chris@148
|
993 while (m_server.m_suspended) {
|
Chris@148
|
994 #ifdef DEBUG_FFT_SERVER
|
Chris@193
|
995 std::cerr << "FFTDataServer(" << this << " [" << (void *)QThread::currentThreadId() << "]): suspended, waiting..." << std::endl;
|
Chris@148
|
996 #endif
|
Chris@148
|
997 m_server.m_writeMutex.lock();
|
Chris@148
|
998 m_server.m_condition.wait(&m_server.m_writeMutex, 10000);
|
Chris@148
|
999 m_server.m_writeMutex.unlock();
|
Chris@159
|
1000 #ifdef DEBUG_FFT_SERVER
|
Chris@193
|
1001 std::cerr << "FFTDataServer(" << this << " [" << (void *)QThread::currentThreadId() << "]): waited" << std::endl;
|
Chris@159
|
1002 #endif
|
Chris@148
|
1003 if (m_server.m_exiting) return;
|
Chris@148
|
1004 }
|
Chris@148
|
1005
|
Chris@148
|
1006 if (++counter == updateAt) {
|
Chris@148
|
1007 m_extent = f;
|
Chris@148
|
1008 m_completion = size_t(100 * fabsf(float(f - m_fillFrom) /
|
Chris@148
|
1009 float(end - start)));
|
Chris@148
|
1010 counter = 0;
|
Chris@148
|
1011 }
|
Chris@148
|
1012 }
|
Chris@148
|
1013
|
Chris@148
|
1014 remainingEnd = m_fillFrom;
|
Chris@148
|
1015 if (remainingEnd > start) --remainingEnd;
|
Chris@148
|
1016 else remainingEnd = start;
|
Chris@148
|
1017 }
|
Chris@148
|
1018
|
Chris@148
|
1019 size_t baseCompletion = m_completion;
|
Chris@148
|
1020
|
Chris@148
|
1021 for (size_t f = start; f < remainingEnd; f += m_server.m_windowIncrement) {
|
Chris@148
|
1022
|
Chris@148
|
1023 m_server.fillColumn(int((f - start) / m_server.m_windowIncrement));
|
Chris@148
|
1024
|
Chris@148
|
1025 if (m_server.m_exiting) return;
|
Chris@148
|
1026
|
Chris@148
|
1027 while (m_server.m_suspended) {
|
Chris@148
|
1028 #ifdef DEBUG_FFT_SERVER
|
Chris@193
|
1029 std::cerr << "FFTDataServer(" << this << " [" << (void *)QThread::currentThreadId() << "]): suspended, waiting..." << std::endl;
|
Chris@148
|
1030 #endif
|
Chris@148
|
1031 m_server.m_writeMutex.lock();
|
Chris@148
|
1032 m_server.m_condition.wait(&m_server.m_writeMutex, 10000);
|
Chris@148
|
1033 m_server.m_writeMutex.unlock();
|
Chris@148
|
1034 if (m_server.m_exiting) return;
|
Chris@148
|
1035 }
|
Chris@148
|
1036
|
Chris@148
|
1037 if (++counter == updateAt) {
|
Chris@148
|
1038 m_extent = f;
|
Chris@148
|
1039 m_completion = baseCompletion +
|
Chris@148
|
1040 size_t(100 * fabsf(float(f - start) /
|
Chris@148
|
1041 float(end - start)));
|
Chris@148
|
1042 counter = 0;
|
Chris@148
|
1043 }
|
Chris@148
|
1044 }
|
Chris@148
|
1045
|
Chris@148
|
1046 m_completion = 100;
|
Chris@148
|
1047 m_extent = end;
|
Chris@148
|
1048 }
|
Chris@148
|
1049
|