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@537
|
18 #include "FFTFileCacheReader.h"
|
Chris@537
|
19 #include "FFTFileCacheWriter.h"
|
Chris@159
|
20 #include "FFTMemoryCache.h"
|
Chris@148
|
21
|
Chris@148
|
22 #include "model/DenseTimeValueModel.h"
|
Chris@148
|
23
|
Chris@150
|
24 #include "system/System.h"
|
Chris@148
|
25
|
Chris@168
|
26 #include "base/StorageAdviser.h"
|
Chris@200
|
27 #include "base/Exceptions.h"
|
Chris@183
|
28 #include "base/Profiler.h"
|
Chris@244
|
29 #include "base/Thread.h" // for debug mutex locker
|
Chris@168
|
30
|
Chris@537
|
31 #include <QWriteLocker>
|
Chris@537
|
32
|
Chris@1038
|
33 #include <stdexcept>
|
Chris@1038
|
34
|
Chris@571
|
35 //#define DEBUG_FFT_SERVER 1
|
Chris@536
|
36 //#define DEBUG_FFT_SERVER_FILL 1
|
Chris@148
|
37
|
Chris@148
|
38 #ifdef DEBUG_FFT_SERVER_FILL
|
Chris@153
|
39 #ifndef DEBUG_FFT_SERVER
|
Chris@153
|
40 #define DEBUG_FFT_SERVER 1
|
Chris@153
|
41 #endif
|
Chris@148
|
42 #endif
|
Chris@148
|
43
|
Chris@244
|
44
|
Chris@148
|
45 FFTDataServer::ServerMap FFTDataServer::m_servers;
|
Chris@215
|
46 FFTDataServer::ServerQueue FFTDataServer::m_releasedServers;
|
Chris@148
|
47 QMutex FFTDataServer::m_serverMapMutex;
|
Chris@148
|
48
|
Chris@148
|
49 FFTDataServer *
|
Chris@148
|
50 FFTDataServer::getInstance(const DenseTimeValueModel *model,
|
Chris@148
|
51 int channel,
|
Chris@148
|
52 WindowType windowType,
|
Chris@929
|
53 int windowSize,
|
Chris@929
|
54 int windowIncrement,
|
Chris@929
|
55 int fftSize,
|
Chris@148
|
56 bool polar,
|
Chris@334
|
57 StorageAdviser::Criteria criteria,
|
Chris@1045
|
58 sv_frame_t fillFromFrame)
|
Chris@148
|
59 {
|
Chris@148
|
60 QString n = generateFileBasename(model,
|
Chris@148
|
61 channel,
|
Chris@148
|
62 windowType,
|
Chris@148
|
63 windowSize,
|
Chris@148
|
64 windowIncrement,
|
Chris@148
|
65 fftSize,
|
Chris@148
|
66 polar);
|
Chris@148
|
67
|
Chris@148
|
68 FFTDataServer *server = 0;
|
Chris@148
|
69
|
Chris@408
|
70 MutexLocker locker(&m_serverMapMutex, "FFTDataServer::getInstance::m_serverMapMutex");
|
Chris@148
|
71
|
Chris@148
|
72 if ((server = findServer(n))) {
|
Chris@148
|
73 return server;
|
Chris@148
|
74 }
|
Chris@148
|
75
|
Chris@148
|
76 QString npn = generateFileBasename(model,
|
Chris@148
|
77 channel,
|
Chris@148
|
78 windowType,
|
Chris@148
|
79 windowSize,
|
Chris@148
|
80 windowIncrement,
|
Chris@148
|
81 fftSize,
|
Chris@148
|
82 !polar);
|
Chris@148
|
83
|
Chris@148
|
84 if ((server = findServer(npn))) {
|
Chris@148
|
85 return server;
|
Chris@148
|
86 }
|
Chris@148
|
87
|
Chris@200
|
88 try {
|
Chris@200
|
89 server = new FFTDataServer(n,
|
Chris@200
|
90 model,
|
Chris@200
|
91 channel,
|
Chris@200
|
92 windowType,
|
Chris@200
|
93 windowSize,
|
Chris@200
|
94 windowIncrement,
|
Chris@200
|
95 fftSize,
|
Chris@200
|
96 polar,
|
Chris@334
|
97 criteria,
|
Chris@1045
|
98 fillFromFrame);
|
Chris@200
|
99 } catch (InsufficientDiscSpace) {
|
Chris@200
|
100 delete server;
|
Chris@200
|
101 server = 0;
|
Chris@200
|
102 }
|
Chris@148
|
103
|
Chris@200
|
104 if (server) {
|
Chris@200
|
105 m_servers[n] = ServerCountPair(server, 1);
|
Chris@200
|
106 }
|
Chris@200
|
107
|
Chris@200
|
108 return server;
|
Chris@148
|
109 }
|
Chris@148
|
110
|
Chris@148
|
111 FFTDataServer *
|
Chris@148
|
112 FFTDataServer::getFuzzyInstance(const DenseTimeValueModel *model,
|
Chris@148
|
113 int channel,
|
Chris@148
|
114 WindowType windowType,
|
Chris@929
|
115 int windowSize,
|
Chris@929
|
116 int windowIncrement,
|
Chris@929
|
117 int fftSize,
|
Chris@148
|
118 bool polar,
|
Chris@334
|
119 StorageAdviser::Criteria criteria,
|
Chris@1045
|
120 sv_frame_t fillFromFrame)
|
Chris@148
|
121 {
|
Chris@148
|
122 // Fuzzy matching:
|
Chris@148
|
123 //
|
Chris@148
|
124 // -- if we're asked for polar and have non-polar, use it (and
|
Chris@148
|
125 // vice versa). This one is vital, and we do it for non-fuzzy as
|
Chris@148
|
126 // well (above).
|
Chris@148
|
127 //
|
Chris@148
|
128 // -- if we're asked for an instance with a given fft size and we
|
Chris@148
|
129 // have one already with a multiple of that fft size but the same
|
Chris@148
|
130 // window size and type (and model), we can draw the results from
|
Chris@148
|
131 // it (e.g. the 1st, 2nd, 3rd etc bins of a 512-sample FFT are the
|
Chris@148
|
132 // same as the the 1st, 5th, 9th etc of a 2048-sample FFT of the
|
Chris@148
|
133 // same window plus zero padding).
|
Chris@148
|
134 //
|
Chris@148
|
135 // -- if we're asked for an instance with a given window type and
|
Chris@148
|
136 // size and fft size and we have one already the same but with a
|
Chris@148
|
137 // smaller increment, we can draw the results from it (provided
|
Chris@148
|
138 // our increment is a multiple of its)
|
Chris@148
|
139 //
|
Chris@152
|
140 // The FFTModel knows how to interpret these things. In
|
Chris@148
|
141 // both cases we require that the larger one is a power-of-two
|
Chris@148
|
142 // multiple of the smaller (e.g. even though in principle you can
|
Chris@148
|
143 // draw the results at increment 256 from those at increment 768
|
Chris@152
|
144 // or 1536, the model doesn't support this).
|
Chris@148
|
145
|
Chris@148
|
146 {
|
Chris@408
|
147 MutexLocker locker(&m_serverMapMutex, "FFTDataServer::getFuzzyInstance::m_serverMapMutex");
|
Chris@148
|
148
|
Chris@148
|
149 ServerMap::iterator best = m_servers.end();
|
Chris@148
|
150 int bestdist = -1;
|
Chris@148
|
151
|
Chris@148
|
152 for (ServerMap::iterator i = m_servers.begin(); i != m_servers.end(); ++i) {
|
Chris@148
|
153
|
Chris@148
|
154 FFTDataServer *server = i->second.first;
|
Chris@148
|
155
|
Chris@148
|
156 if (server->getModel() == model &&
|
Chris@148
|
157 (server->getChannel() == channel || model->getChannelCount() == 1) &&
|
Chris@148
|
158 server->getWindowType() == windowType &&
|
Chris@148
|
159 server->getWindowSize() == windowSize &&
|
Chris@148
|
160 server->getWindowIncrement() <= windowIncrement &&
|
Chris@148
|
161 server->getFFTSize() >= fftSize) {
|
Chris@148
|
162
|
Chris@148
|
163 if ((windowIncrement % server->getWindowIncrement()) != 0) continue;
|
Chris@148
|
164 int ratio = windowIncrement / server->getWindowIncrement();
|
Chris@148
|
165 bool poweroftwo = true;
|
Chris@148
|
166 while (ratio > 1) {
|
Chris@148
|
167 if (ratio & 0x1) {
|
Chris@148
|
168 poweroftwo = false;
|
Chris@148
|
169 break;
|
Chris@148
|
170 }
|
Chris@148
|
171 ratio >>= 1;
|
Chris@148
|
172 }
|
Chris@148
|
173 if (!poweroftwo) continue;
|
Chris@148
|
174
|
Chris@148
|
175 if ((server->getFFTSize() % fftSize) != 0) continue;
|
Chris@148
|
176 ratio = server->getFFTSize() / fftSize;
|
Chris@148
|
177 while (ratio > 1) {
|
Chris@148
|
178 if (ratio & 0x1) {
|
Chris@148
|
179 poweroftwo = false;
|
Chris@148
|
180 break;
|
Chris@148
|
181 }
|
Chris@148
|
182 ratio >>= 1;
|
Chris@148
|
183 }
|
Chris@148
|
184 if (!poweroftwo) continue;
|
Chris@148
|
185
|
Chris@148
|
186 int distance = 0;
|
Chris@148
|
187
|
Chris@148
|
188 if (server->getPolar() != polar) distance += 1;
|
Chris@148
|
189
|
Chris@148
|
190 distance += ((windowIncrement / server->getWindowIncrement()) - 1) * 15;
|
Chris@148
|
191 distance += ((server->getFFTSize() / fftSize) - 1) * 10;
|
Chris@148
|
192
|
Chris@148
|
193 if (server->getFillCompletion() < 50) distance += 100;
|
Chris@148
|
194
|
Chris@148
|
195 #ifdef DEBUG_FFT_SERVER
|
chris@828
|
196 std::cerr << "FFTDataServer::getFuzzyInstance: Distance for server " << server << " is " << distance << ", best is " << bestdist << std::endl;
|
Chris@148
|
197 #endif
|
Chris@148
|
198
|
Chris@148
|
199 if (bestdist == -1 || distance < bestdist) {
|
Chris@148
|
200 bestdist = distance;
|
Chris@148
|
201 best = i;
|
Chris@148
|
202 }
|
Chris@148
|
203 }
|
Chris@148
|
204 }
|
Chris@148
|
205
|
Chris@148
|
206 if (bestdist >= 0) {
|
Chris@216
|
207 FFTDataServer *server = best->second.first;
|
Chris@216
|
208 #ifdef DEBUG_FFT_SERVER
|
chris@828
|
209 std::cerr << "FFTDataServer::getFuzzyInstance: We like server " << server << " (with distance " << bestdist << ")" << std::endl;
|
Chris@216
|
210 #endif
|
Chris@216
|
211 claimInstance(server, false);
|
Chris@216
|
212 return server;
|
Chris@148
|
213 }
|
Chris@148
|
214 }
|
Chris@148
|
215
|
Chris@148
|
216 // Nothing found, make a new one
|
Chris@148
|
217
|
Chris@148
|
218 return getInstance(model,
|
Chris@148
|
219 channel,
|
Chris@148
|
220 windowType,
|
Chris@148
|
221 windowSize,
|
Chris@148
|
222 windowIncrement,
|
Chris@148
|
223 fftSize,
|
Chris@148
|
224 polar,
|
Chris@334
|
225 criteria,
|
Chris@1045
|
226 fillFromFrame);
|
Chris@148
|
227 }
|
Chris@148
|
228
|
Chris@148
|
229 FFTDataServer *
|
Chris@148
|
230 FFTDataServer::findServer(QString n)
|
Chris@148
|
231 {
|
Chris@216
|
232 #ifdef DEBUG_FFT_SERVER
|
chris@828
|
233 std::cerr << "FFTDataServer::findServer(\"" << n << "\")" << std::endl;
|
Chris@216
|
234 #endif
|
Chris@216
|
235
|
Chris@148
|
236 if (m_servers.find(n) != m_servers.end()) {
|
Chris@216
|
237
|
Chris@216
|
238 FFTDataServer *server = m_servers[n].first;
|
Chris@216
|
239
|
Chris@216
|
240 #ifdef DEBUG_FFT_SERVER
|
chris@828
|
241 std::cerr << "FFTDataServer::findServer(\"" << n << "\"): found " << server << std::endl;
|
Chris@216
|
242 #endif
|
Chris@216
|
243
|
Chris@216
|
244 claimInstance(server, false);
|
Chris@216
|
245
|
Chris@216
|
246 return server;
|
Chris@148
|
247 }
|
Chris@148
|
248
|
Chris@216
|
249 #ifdef DEBUG_FFT_SERVER
|
chris@828
|
250 std::cerr << "FFTDataServer::findServer(\"" << n << "\"): not found" << std::endl;
|
Chris@216
|
251 #endif
|
Chris@216
|
252
|
Chris@148
|
253 return 0;
|
Chris@148
|
254 }
|
Chris@148
|
255
|
Chris@148
|
256 void
|
Chris@152
|
257 FFTDataServer::claimInstance(FFTDataServer *server)
|
Chris@152
|
258 {
|
Chris@216
|
259 claimInstance(server, true);
|
Chris@216
|
260 }
|
Chris@216
|
261
|
Chris@216
|
262 void
|
Chris@216
|
263 FFTDataServer::claimInstance(FFTDataServer *server, bool needLock)
|
Chris@216
|
264 {
|
Chris@244
|
265 MutexLocker locker(needLock ? &m_serverMapMutex : 0,
|
Chris@408
|
266 "FFTDataServer::claimInstance::m_serverMapMutex");
|
Chris@216
|
267
|
Chris@216
|
268 #ifdef DEBUG_FFT_SERVER
|
chris@828
|
269 std::cerr << "FFTDataServer::claimInstance(" << server << ")" << std::endl;
|
Chris@216
|
270 #endif
|
Chris@152
|
271
|
Chris@152
|
272 for (ServerMap::iterator i = m_servers.begin(); i != m_servers.end(); ++i) {
|
Chris@152
|
273 if (i->second.first == server) {
|
Chris@215
|
274
|
Chris@215
|
275 for (ServerQueue::iterator j = m_releasedServers.begin();
|
Chris@215
|
276 j != m_releasedServers.end(); ++j) {
|
Chris@216
|
277
|
Chris@215
|
278 if (*j == server) {
|
Chris@216
|
279 #ifdef DEBUG_FFT_SERVER
|
chris@828
|
280 std::cerr << "FFTDataServer::claimInstance: found in released server list, removing from it" << std::endl;
|
Chris@216
|
281 #endif
|
Chris@215
|
282 m_releasedServers.erase(j);
|
Chris@215
|
283 break;
|
Chris@215
|
284 }
|
Chris@215
|
285 }
|
Chris@215
|
286
|
Chris@152
|
287 ++i->second.second;
|
Chris@216
|
288
|
Chris@216
|
289 #ifdef DEBUG_FFT_SERVER
|
chris@828
|
290 std::cerr << "FFTDataServer::claimInstance: new refcount is " << i->second.second << std::endl;
|
Chris@216
|
291 #endif
|
Chris@216
|
292
|
Chris@152
|
293 return;
|
Chris@152
|
294 }
|
Chris@152
|
295 }
|
Chris@152
|
296
|
Chris@843
|
297 cerr << "ERROR: FFTDataServer::claimInstance: instance "
|
Chris@843
|
298 << server << " unknown!" << endl;
|
Chris@152
|
299 }
|
Chris@152
|
300
|
Chris@152
|
301 void
|
Chris@148
|
302 FFTDataServer::releaseInstance(FFTDataServer *server)
|
Chris@148
|
303 {
|
Chris@216
|
304 releaseInstance(server, true);
|
Chris@216
|
305 }
|
Chris@216
|
306
|
Chris@216
|
307 void
|
Chris@216
|
308 FFTDataServer::releaseInstance(FFTDataServer *server, bool needLock)
|
Chris@216
|
309 {
|
Chris@244
|
310 MutexLocker locker(needLock ? &m_serverMapMutex : 0,
|
Chris@408
|
311 "FFTDataServer::releaseInstance::m_serverMapMutex");
|
Chris@216
|
312
|
Chris@148
|
313 #ifdef DEBUG_FFT_SERVER
|
chris@828
|
314 std::cerr << "FFTDataServer::releaseInstance(" << server << ")" << std::endl;
|
Chris@148
|
315 #endif
|
Chris@148
|
316
|
Chris@148
|
317 // -- if ref count > 0, decrement and return
|
Chris@148
|
318 // -- if the instance hasn't been used at all, delete it immediately
|
Chris@148
|
319 // -- if fewer than N instances (N = e.g. 3) remain with zero refcounts,
|
Chris@148
|
320 // leave them hanging around
|
Chris@148
|
321 // -- if N instances with zero refcounts remain, delete the one that
|
Chris@148
|
322 // was last released first
|
Chris@148
|
323 // -- if we run out of disk space when allocating an instance, go back
|
Chris@148
|
324 // and delete the spare N instances before trying again
|
Chris@148
|
325 // -- have an additional method to indicate that a model has been
|
Chris@148
|
326 // destroyed, so that we can delete all of its fft server instances
|
Chris@148
|
327
|
Chris@148
|
328 for (ServerMap::iterator i = m_servers.begin(); i != m_servers.end(); ++i) {
|
Chris@148
|
329 if (i->second.first == server) {
|
Chris@148
|
330 if (i->second.second == 0) {
|
Chris@843
|
331 cerr << "ERROR: FFTDataServer::releaseInstance("
|
Chris@843
|
332 << server << "): instance not allocated" << endl;
|
Chris@148
|
333 } else if (--i->second.second == 0) {
|
Chris@537
|
334 /*!!!
|
Chris@148
|
335 if (server->m_lastUsedCache == -1) { // never used
|
Chris@216
|
336 #ifdef DEBUG_FFT_SERVER
|
chris@828
|
337 std::cerr << "FFTDataServer::releaseInstance: instance "
|
Chris@216
|
338 << server << " has never been used, erasing"
|
chris@828
|
339 << std::endl;
|
Chris@216
|
340 #endif
|
Chris@148
|
341 delete server;
|
Chris@148
|
342 m_servers.erase(i);
|
Chris@148
|
343 } else {
|
Chris@537
|
344 */
|
Chris@216
|
345 #ifdef DEBUG_FFT_SERVER
|
chris@828
|
346 std::cerr << "FFTDataServer::releaseInstance: instance "
|
Chris@216
|
347 << server << " no longer in use, marking for possible collection"
|
chris@828
|
348 << std::endl;
|
Chris@216
|
349 #endif
|
Chris@216
|
350 bool found = false;
|
Chris@216
|
351 for (ServerQueue::iterator j = m_releasedServers.begin();
|
Chris@216
|
352 j != m_releasedServers.end(); ++j) {
|
Chris@216
|
353 if (*j == server) {
|
Chris@843
|
354 cerr << "ERROR: FFTDataServer::releaseInstance("
|
Chris@216
|
355 << server << "): server is already in "
|
Chris@843
|
356 << "released servers list" << endl;
|
Chris@216
|
357 found = true;
|
Chris@216
|
358 }
|
Chris@216
|
359 }
|
Chris@216
|
360 if (!found) m_releasedServers.push_back(server);
|
Chris@148
|
361 server->suspend();
|
Chris@148
|
362 purgeLimbo();
|
Chris@537
|
363 //!!! }
|
Chris@216
|
364 } else {
|
Chris@216
|
365 #ifdef DEBUG_FFT_SERVER
|
chris@828
|
366 std::cerr << "FFTDataServer::releaseInstance: instance "
|
Chris@216
|
367 << server << " now has refcount " << i->second.second
|
chris@828
|
368 << std::endl;
|
Chris@216
|
369 #endif
|
Chris@148
|
370 }
|
Chris@148
|
371 return;
|
Chris@148
|
372 }
|
Chris@148
|
373 }
|
Chris@148
|
374
|
Chris@843
|
375 cerr << "ERROR: FFTDataServer::releaseInstance(" << server << "): "
|
Chris@843
|
376 << "instance not found" << endl;
|
Chris@148
|
377 }
|
Chris@148
|
378
|
Chris@148
|
379 void
|
Chris@148
|
380 FFTDataServer::purgeLimbo(int maxSize)
|
Chris@148
|
381 {
|
Chris@216
|
382 #ifdef DEBUG_FFT_SERVER
|
chris@828
|
383 std::cerr << "FFTDataServer::purgeLimbo(" << maxSize << "): "
|
chris@828
|
384 << m_releasedServers.size() << " candidates" << std::endl;
|
Chris@216
|
385 #endif
|
Chris@216
|
386
|
Chris@259
|
387 while (int(m_releasedServers.size()) > maxSize) {
|
Chris@148
|
388
|
Chris@215
|
389 FFTDataServer *server = *m_releasedServers.begin();
|
Chris@148
|
390
|
Chris@215
|
391 bool found = false;
|
Chris@215
|
392
|
Chris@216
|
393 #ifdef DEBUG_FFT_SERVER
|
chris@828
|
394 std::cerr << "FFTDataServer::purgeLimbo: considering candidate "
|
chris@828
|
395 << server << std::endl;
|
Chris@216
|
396 #endif
|
Chris@216
|
397
|
Chris@215
|
398 for (ServerMap::iterator i = m_servers.begin(); i != m_servers.end(); ++i) {
|
Chris@215
|
399
|
Chris@215
|
400 if (i->second.first == server) {
|
Chris@215
|
401 found = true;
|
Chris@215
|
402 if (i->second.second > 0) {
|
Chris@843
|
403 cerr << "ERROR: FFTDataServer::purgeLimbo: Server "
|
Chris@215
|
404 << server << " is in released queue, but still has non-zero refcount "
|
Chris@843
|
405 << i->second.second << endl;
|
Chris@215
|
406 // ... so don't delete it
|
Chris@215
|
407 break;
|
Chris@215
|
408 }
|
Chris@216
|
409 #ifdef DEBUG_FFT_SERVER
|
chris@828
|
410 std::cerr << "FFTDataServer::purgeLimbo: looks OK, erasing it"
|
chris@828
|
411 << std::endl;
|
Chris@216
|
412 #endif
|
Chris@216
|
413
|
Chris@148
|
414 m_servers.erase(i);
|
Chris@215
|
415 delete server;
|
Chris@215
|
416 break;
|
Chris@148
|
417 }
|
Chris@148
|
418 }
|
Chris@215
|
419
|
Chris@215
|
420 if (!found) {
|
Chris@843
|
421 cerr << "ERROR: FFTDataServer::purgeLimbo: Server "
|
Chris@215
|
422 << server << " is in released queue, but not in server map!"
|
Chris@843
|
423 << endl;
|
Chris@215
|
424 delete server;
|
Chris@215
|
425 }
|
Chris@215
|
426
|
Chris@215
|
427 m_releasedServers.pop_front();
|
Chris@215
|
428 }
|
Chris@216
|
429
|
Chris@216
|
430 #ifdef DEBUG_FFT_SERVER
|
chris@828
|
431 std::cerr << "FFTDataServer::purgeLimbo(" << maxSize << "): "
|
chris@828
|
432 << m_releasedServers.size() << " remain" << std::endl;
|
Chris@216
|
433 #endif
|
Chris@216
|
434
|
Chris@215
|
435 }
|
Chris@215
|
436
|
Chris@215
|
437 void
|
Chris@215
|
438 FFTDataServer::modelAboutToBeDeleted(Model *model)
|
Chris@215
|
439 {
|
Chris@244
|
440 MutexLocker locker(&m_serverMapMutex,
|
Chris@408
|
441 "FFTDataServer::modelAboutToBeDeleted::m_serverMapMutex");
|
Chris@215
|
442
|
Chris@216
|
443 #ifdef DEBUG_FFT_SERVER
|
chris@828
|
444 std::cerr << "FFTDataServer::modelAboutToBeDeleted(" << model << ")"
|
chris@828
|
445 << std::endl;
|
Chris@216
|
446 #endif
|
Chris@216
|
447
|
Chris@215
|
448 for (ServerMap::iterator i = m_servers.begin(); i != m_servers.end(); ++i) {
|
Chris@215
|
449
|
Chris@215
|
450 FFTDataServer *server = i->second.first;
|
Chris@215
|
451
|
Chris@215
|
452 if (server->getModel() == model) {
|
Chris@216
|
453
|
Chris@216
|
454 #ifdef DEBUG_FFT_SERVER
|
chris@828
|
455 std::cerr << "FFTDataServer::modelAboutToBeDeleted: server is "
|
chris@828
|
456 << server << std::endl;
|
Chris@216
|
457 #endif
|
Chris@216
|
458
|
Chris@215
|
459 if (i->second.second > 0) {
|
Chris@843
|
460 cerr << "WARNING: FFTDataServer::modelAboutToBeDeleted: Model " << model << " (\"" << model->objectName() << "\") is about to be deleted, but is still being referred to by FFT server " << server << " with non-zero refcount " << i->second.second << endl;
|
Chris@497
|
461 server->suspendWrites();
|
Chris@362
|
462 return;
|
Chris@215
|
463 }
|
Chris@215
|
464 for (ServerQueue::iterator j = m_releasedServers.begin();
|
Chris@215
|
465 j != m_releasedServers.end(); ++j) {
|
Chris@215
|
466 if (*j == server) {
|
Chris@216
|
467 #ifdef DEBUG_FFT_SERVER
|
chris@828
|
468 std::cerr << "FFTDataServer::modelAboutToBeDeleted: erasing from released servers" << std::endl;
|
Chris@216
|
469 #endif
|
Chris@215
|
470 m_releasedServers.erase(j);
|
Chris@215
|
471 break;
|
Chris@215
|
472 }
|
Chris@215
|
473 }
|
Chris@216
|
474 #ifdef DEBUG_FFT_SERVER
|
chris@828
|
475 std::cerr << "FFTDataServer::modelAboutToBeDeleted: erasing server" << std::endl;
|
Chris@216
|
476 #endif
|
Chris@215
|
477 m_servers.erase(i);
|
Chris@215
|
478 delete server;
|
Chris@215
|
479 return;
|
Chris@215
|
480 }
|
Chris@148
|
481 }
|
Chris@148
|
482 }
|
Chris@148
|
483
|
Chris@148
|
484 FFTDataServer::FFTDataServer(QString fileBaseName,
|
Chris@148
|
485 const DenseTimeValueModel *model,
|
Chris@148
|
486 int channel,
|
Chris@148
|
487 WindowType windowType,
|
Chris@929
|
488 int windowSize,
|
Chris@929
|
489 int windowIncrement,
|
Chris@929
|
490 int fftSize,
|
Chris@148
|
491 bool polar,
|
Chris@334
|
492 StorageAdviser::Criteria criteria,
|
Chris@1045
|
493 sv_frame_t fillFromFrame) :
|
Chris@148
|
494 m_fileBaseName(fileBaseName),
|
Chris@148
|
495 m_model(model),
|
Chris@148
|
496 m_channel(channel),
|
Chris@148
|
497 m_windower(windowType, windowSize),
|
Chris@148
|
498 m_windowSize(windowSize),
|
Chris@148
|
499 m_windowIncrement(windowIncrement),
|
Chris@148
|
500 m_fftSize(fftSize),
|
Chris@148
|
501 m_polar(polar),
|
Chris@183
|
502 m_width(0),
|
Chris@183
|
503 m_height(0),
|
Chris@183
|
504 m_cacheWidth(0),
|
Chris@359
|
505 m_cacheWidthPower(0),
|
Chris@359
|
506 m_cacheWidthMask(0),
|
Chris@359
|
507 m_criteria(criteria),
|
Chris@148
|
508 m_fftInput(0),
|
Chris@148
|
509 m_exiting(false),
|
Chris@153
|
510 m_suspended(true), //!!! or false?
|
Chris@148
|
511 m_fillThread(0)
|
Chris@148
|
512 {
|
Chris@193
|
513 #ifdef DEBUG_FFT_SERVER
|
Chris@843
|
514 cerr << "FFTDataServer(" << this << " [" << (void *)QThread::currentThreadId() << "])::FFTDataServer" << endl;
|
Chris@193
|
515 #endif
|
Chris@193
|
516
|
Chris@272
|
517 //!!! end is not correct until model finished reading -- what to do???
|
Chris@272
|
518
|
Chris@1038
|
519 sv_frame_t start = m_model->getStartFrame();
|
Chris@1038
|
520 sv_frame_t end = m_model->getEndFrame();
|
Chris@148
|
521
|
Chris@1038
|
522 m_width = int((end - start) / m_windowIncrement) + 1;
|
Chris@203
|
523 m_height = m_fftSize / 2 + 1; // DC == 0, Nyquist == fftsize/2
|
Chris@148
|
524
|
Chris@216
|
525 #ifdef DEBUG_FFT_SERVER
|
Chris@843
|
526 cerr << "FFTDataServer(" << this << "): dimensions are "
|
Chris@843
|
527 << m_width << "x" << m_height << endl;
|
Chris@216
|
528 #endif
|
Chris@216
|
529
|
Chris@929
|
530 int maxCacheSize = 20 * 1024 * 1024;
|
Chris@1038
|
531 int columnSize = int(m_height * sizeof(fftsample) * 2 + sizeof(fftsample));
|
Chris@148
|
532 if (m_width * columnSize < maxCacheSize * 2) m_cacheWidth = m_width;
|
Chris@148
|
533 else m_cacheWidth = maxCacheSize / columnSize;
|
Chris@148
|
534
|
Chris@359
|
535 #ifdef DEBUG_FFT_SERVER
|
Chris@843
|
536 cerr << "FFTDataServer(" << this << "): cache width nominal "
|
Chris@359
|
537 << m_cacheWidth << ", actual ";
|
Chris@359
|
538 #endif
|
Chris@359
|
539
|
Chris@148
|
540 int bits = 0;
|
Chris@359
|
541 while (m_cacheWidth > 1) { m_cacheWidth >>= 1; ++bits; }
|
Chris@359
|
542 m_cacheWidthPower = bits + 1;
|
Chris@148
|
543 m_cacheWidth = 2;
|
Chris@148
|
544 while (bits) { m_cacheWidth <<= 1; --bits; }
|
Chris@359
|
545 m_cacheWidthMask = m_cacheWidth - 1;
|
Chris@172
|
546
|
Chris@359
|
547 #ifdef DEBUG_FFT_SERVER
|
Chris@843
|
548 cerr << m_cacheWidth << " (power " << m_cacheWidthPower << ", mask "
|
Chris@843
|
549 << m_cacheWidthMask << ")" << endl;
|
Chris@359
|
550 #endif
|
Chris@359
|
551
|
Chris@359
|
552 if (m_criteria == StorageAdviser::NoCriteria) {
|
Chris@172
|
553
|
Chris@334
|
554 // assume "spectrogram" criteria for polar ffts, and "feature
|
Chris@334
|
555 // extraction" criteria for rectangular ones.
|
Chris@334
|
556
|
Chris@334
|
557 if (m_polar) {
|
Chris@359
|
558 m_criteria = StorageAdviser::Criteria
|
Chris@334
|
559 (StorageAdviser::SpeedCritical |
|
Chris@334
|
560 StorageAdviser::LongRetentionLikely);
|
Chris@334
|
561 } else {
|
Chris@359
|
562 m_criteria = StorageAdviser::Criteria
|
Chris@334
|
563 (StorageAdviser::PrecisionCritical);
|
Chris@334
|
564 }
|
Chris@172
|
565 }
|
Chris@172
|
566
|
Chris@929
|
567 for (int i = 0; i <= m_width / m_cacheWidth; ++i) {
|
Chris@148
|
568 m_caches.push_back(0);
|
Chris@148
|
569 }
|
Chris@148
|
570
|
Chris@148
|
571 m_fftInput = (fftsample *)
|
Chris@226
|
572 fftf_malloc(fftSize * sizeof(fftsample));
|
Chris@148
|
573
|
Chris@226
|
574 m_fftOutput = (fftf_complex *)
|
Chris@226
|
575 fftf_malloc((fftSize/2 + 1) * sizeof(fftf_complex));
|
Chris@148
|
576
|
Chris@148
|
577 m_workbuffer = (float *)
|
Chris@226
|
578 fftf_malloc((fftSize+2) * sizeof(float));
|
Chris@148
|
579
|
Chris@226
|
580 m_fftPlan = fftf_plan_dft_r2c_1d(m_fftSize,
|
Chris@334
|
581 m_fftInput,
|
Chris@334
|
582 m_fftOutput,
|
Chris@334
|
583 FFTW_MEASURE);
|
Chris@148
|
584
|
Chris@148
|
585 if (!m_fftPlan) {
|
Chris@843
|
586 cerr << "ERROR: fftf_plan_dft_r2c_1d(" << m_windowSize << ") failed!" << endl;
|
Chris@148
|
587 throw(0);
|
Chris@148
|
588 }
|
Chris@148
|
589
|
Chris@1045
|
590 m_fillThread = new FillThread(*this, fillFromFrame);
|
Chris@148
|
591 }
|
Chris@148
|
592
|
Chris@148
|
593 FFTDataServer::~FFTDataServer()
|
Chris@148
|
594 {
|
Chris@148
|
595 #ifdef DEBUG_FFT_SERVER
|
Chris@843
|
596 cerr << "FFTDataServer(" << this << " [" << (void *)QThread::currentThreadId() << "])::~FFTDataServer()" << endl;
|
Chris@148
|
597 #endif
|
Chris@148
|
598
|
Chris@155
|
599 m_suspended = false;
|
Chris@148
|
600 m_exiting = true;
|
Chris@148
|
601 m_condition.wakeAll();
|
Chris@148
|
602 if (m_fillThread) {
|
Chris@148
|
603 m_fillThread->wait();
|
Chris@148
|
604 delete m_fillThread;
|
Chris@148
|
605 }
|
Chris@148
|
606
|
Chris@549
|
607 // MutexLocker locker(&m_writeMutex,
|
Chris@549
|
608 // "FFTDataServer::~FFTDataServer::m_writeMutex");
|
Chris@549
|
609
|
Chris@549
|
610 QMutexLocker mlocker(&m_fftBuffersLock);
|
Chris@549
|
611 QWriteLocker wlocker(&m_cacheVectorLock);
|
Chris@148
|
612
|
Chris@148
|
613 for (CacheVector::iterator i = m_caches.begin(); i != m_caches.end(); ++i) {
|
Chris@205
|
614 if (*i) {
|
Chris@205
|
615 delete *i;
|
Chris@205
|
616 }
|
Chris@148
|
617 }
|
Chris@148
|
618
|
Chris@148
|
619 deleteProcessingData();
|
Chris@148
|
620 }
|
Chris@148
|
621
|
Chris@148
|
622 void
|
Chris@148
|
623 FFTDataServer::deleteProcessingData()
|
Chris@148
|
624 {
|
Chris@193
|
625 #ifdef DEBUG_FFT_SERVER
|
Chris@843
|
626 cerr << "FFTDataServer(" << this << " [" << (void *)QThread::currentThreadId() << "]): deleteProcessingData" << endl;
|
Chris@193
|
627 #endif
|
Chris@148
|
628 if (m_fftInput) {
|
Chris@226
|
629 fftf_destroy_plan(m_fftPlan);
|
Chris@226
|
630 fftf_free(m_fftInput);
|
Chris@226
|
631 fftf_free(m_fftOutput);
|
Chris@226
|
632 fftf_free(m_workbuffer);
|
Chris@148
|
633 }
|
Chris@148
|
634 m_fftInput = 0;
|
Chris@148
|
635 }
|
Chris@148
|
636
|
Chris@148
|
637 void
|
Chris@148
|
638 FFTDataServer::suspend()
|
Chris@148
|
639 {
|
Chris@148
|
640 #ifdef DEBUG_FFT_SERVER
|
Chris@843
|
641 cerr << "FFTDataServer(" << this << " [" << (void *)QThread::currentThreadId() << "]): suspend" << endl;
|
Chris@148
|
642 #endif
|
Chris@183
|
643 Profiler profiler("FFTDataServer::suspend", false);
|
Chris@183
|
644
|
Chris@549
|
645 QMutexLocker locker(&m_fftBuffersLock);
|
Chris@148
|
646 m_suspended = true;
|
Chris@148
|
647 }
|
Chris@148
|
648
|
Chris@148
|
649 void
|
Chris@155
|
650 FFTDataServer::suspendWrites()
|
Chris@155
|
651 {
|
Chris@155
|
652 #ifdef DEBUG_FFT_SERVER
|
Chris@843
|
653 cerr << "FFTDataServer(" << this << " [" << (void *)QThread::currentThreadId() << "]): suspendWrites" << endl;
|
Chris@155
|
654 #endif
|
Chris@183
|
655 Profiler profiler("FFTDataServer::suspendWrites", false);
|
Chris@183
|
656
|
Chris@155
|
657 m_suspended = true;
|
Chris@155
|
658 }
|
Chris@155
|
659
|
Chris@155
|
660 void
|
Chris@148
|
661 FFTDataServer::resume()
|
Chris@148
|
662 {
|
Chris@154
|
663 #ifdef DEBUG_FFT_SERVER
|
Chris@843
|
664 cerr << "FFTDataServer(" << this << " [" << (void *)QThread::currentThreadId() << "]): resume" << endl;
|
Chris@154
|
665 #endif
|
Chris@183
|
666 Profiler profiler("FFTDataServer::resume", false);
|
Chris@183
|
667
|
Chris@148
|
668 m_suspended = false;
|
Chris@157
|
669 if (m_fillThread) {
|
Chris@157
|
670 if (m_fillThread->isFinished()) {
|
Chris@157
|
671 delete m_fillThread;
|
Chris@157
|
672 m_fillThread = 0;
|
Chris@157
|
673 deleteProcessingData();
|
Chris@411
|
674 } else if (!m_fillThread->isRunning()) {
|
Chris@411
|
675 m_fillThread->start();
|
Chris@157
|
676 } else {
|
Chris@157
|
677 m_condition.wakeAll();
|
Chris@157
|
678 }
|
Chris@157
|
679 }
|
Chris@148
|
680 }
|
Chris@148
|
681
|
Chris@359
|
682 void
|
Chris@929
|
683 FFTDataServer::getStorageAdvice(int w, int h,
|
Chris@359
|
684 bool &memoryCache, bool &compactCache)
|
Chris@359
|
685 {
|
Chris@1038
|
686 if (w < 0 || h < 0) throw std::domain_error("width & height must be non-negative");
|
Chris@1038
|
687 size_t cells = size_t(w) * h;
|
Chris@1038
|
688 size_t minimumSize = (cells / 1024) * sizeof(uint16_t); // kb
|
Chris@1038
|
689 size_t maximumSize = (cells / 1024) * sizeof(float); // kb
|
Chris@359
|
690
|
Chris@359
|
691 // We don't have a compact rectangular representation, and compact
|
Chris@359
|
692 // of course is never precision-critical
|
Chris@359
|
693
|
Chris@359
|
694 bool canCompact = true;
|
Chris@359
|
695 if ((m_criteria & StorageAdviser::PrecisionCritical) || !m_polar) {
|
Chris@359
|
696 canCompact = false;
|
Chris@359
|
697 minimumSize = maximumSize; // don't use compact
|
Chris@359
|
698 }
|
Chris@359
|
699
|
Chris@359
|
700 StorageAdviser::Recommendation recommendation;
|
Chris@359
|
701
|
Chris@359
|
702 try {
|
Chris@359
|
703
|
Chris@359
|
704 recommendation =
|
Chris@359
|
705 StorageAdviser::recommend(m_criteria, minimumSize, maximumSize);
|
Chris@359
|
706
|
Chris@359
|
707 } catch (InsufficientDiscSpace s) {
|
Chris@359
|
708
|
Chris@359
|
709 // Delete any unused servers we may have been leaving around
|
Chris@359
|
710 // in case we wanted them again
|
Chris@359
|
711
|
Chris@359
|
712 purgeLimbo(0);
|
Chris@359
|
713
|
Chris@359
|
714 // This time we don't catch InsufficientDiscSpace -- we
|
Chris@359
|
715 // haven't allocated anything yet and can safely let the
|
Chris@359
|
716 // exception out to indicate to the caller that we can't
|
Chris@359
|
717 // handle it.
|
Chris@359
|
718
|
Chris@359
|
719 recommendation =
|
Chris@359
|
720 StorageAdviser::recommend(m_criteria, minimumSize, maximumSize);
|
Chris@359
|
721 }
|
Chris@359
|
722
|
Chris@843
|
723 // cerr << "Recommendation was: " << recommendation << endl;
|
Chris@359
|
724
|
Chris@359
|
725 memoryCache = false;
|
Chris@359
|
726
|
Chris@359
|
727 if ((recommendation & StorageAdviser::UseMemory) ||
|
Chris@359
|
728 (recommendation & StorageAdviser::PreferMemory)) {
|
Chris@538
|
729 memoryCache = true;
|
Chris@359
|
730 }
|
Chris@359
|
731
|
Chris@359
|
732 compactCache = canCompact &&
|
Chris@359
|
733 (recommendation & StorageAdviser::ConserveSpace);
|
Chris@359
|
734
|
Chris@374
|
735 #ifdef DEBUG_FFT_SERVER
|
Chris@843
|
736 cerr << "FFTDataServer: memory cache = " << memoryCache << ", compact cache = " << compactCache << endl;
|
Chris@359
|
737
|
Chris@843
|
738 cerr << "Width " << w << " of " << m_width << ", height " << h << ", size " << w * h << endl;
|
Chris@359
|
739 #endif
|
Chris@359
|
740 }
|
Chris@359
|
741
|
Chris@537
|
742 bool
|
Chris@537
|
743 FFTDataServer::makeCache(int c)
|
Chris@148
|
744 {
|
Chris@548
|
745 // Creating the cache could take a significant amount of time. We
|
Chris@548
|
746 // don't want to block readers on m_cacheVectorLock while this is
|
Chris@548
|
747 // happening, but we do want to block any further calls to
|
Chris@548
|
748 // makeCache. So we use this lock solely to serialise this
|
Chris@548
|
749 // particular function -- it isn't used anywhere else.
|
Chris@183
|
750
|
Chris@548
|
751 QMutexLocker locker(&m_cacheCreationMutex);
|
Chris@548
|
752
|
Chris@548
|
753 m_cacheVectorLock.lockForRead();
|
Chris@537
|
754 if (m_caches[c]) {
|
Chris@537
|
755 // someone else must have created the cache between our
|
Chris@548
|
756 // testing for it and taking the mutex
|
Chris@548
|
757 m_cacheVectorLock.unlock();
|
Chris@537
|
758 return true;
|
Chris@148
|
759 }
|
Chris@548
|
760 m_cacheVectorLock.unlock();
|
Chris@548
|
761
|
Chris@548
|
762 // Now m_cacheCreationMutex is held, but m_cacheVectorLock is not
|
Chris@548
|
763 // -- readers can proceed, but callers to this function will block
|
Chris@148
|
764
|
Chris@537
|
765 CacheBlock *cb = new CacheBlock;
|
Chris@148
|
766
|
Chris@148
|
767 QString name = QString("%1-%2").arg(m_fileBaseName).arg(c);
|
Chris@148
|
768
|
Chris@929
|
769 int width = m_cacheWidth;
|
Chris@213
|
770 if (c * m_cacheWidth + width > m_width) {
|
Chris@213
|
771 width = m_width - c * m_cacheWidth;
|
Chris@213
|
772 }
|
Chris@213
|
773
|
Chris@359
|
774 bool memoryCache = false;
|
Chris@359
|
775 bool compactCache = false;
|
Chris@359
|
776
|
Chris@359
|
777 getStorageAdvice(width, m_height, memoryCache, compactCache);
|
Chris@359
|
778
|
Chris@537
|
779 bool success = false;
|
Chris@264
|
780
|
Chris@537
|
781 if (memoryCache) {
|
Chris@172
|
782
|
Chris@537
|
783 try {
|
Chris@172
|
784
|
Chris@537
|
785 cb->memoryCache = new FFTMemoryCache
|
Chris@537
|
786 (compactCache ? FFTCache::Compact :
|
Chris@537
|
787 m_polar ? FFTCache::Polar :
|
Chris@537
|
788 FFTCache::Rectangular,
|
Chris@537
|
789 width, m_height);
|
Chris@172
|
790
|
Chris@537
|
791 success = true;
|
Chris@200
|
792
|
Chris@537
|
793 } catch (std::bad_alloc) {
|
Chris@200
|
794
|
Chris@537
|
795 delete cb->memoryCache;
|
Chris@537
|
796 cb->memoryCache = 0;
|
Chris@213
|
797
|
Chris@843
|
798 cerr << "WARNING: Memory allocation failed when creating"
|
Chris@537
|
799 << " FFT memory cache no. " << c << " of " << width
|
Chris@213
|
800 << "x" << m_height << " (of total width " << m_width
|
Chris@843
|
801 << "): falling back to disc cache" << endl;
|
Chris@213
|
802
|
Chris@537
|
803 memoryCache = false;
|
Chris@213
|
804 }
|
Chris@172
|
805 }
|
Chris@148
|
806
|
Chris@537
|
807 if (!memoryCache) {
|
Chris@537
|
808
|
Chris@537
|
809 try {
|
Chris@537
|
810
|
Chris@537
|
811 cb->fileCacheWriter = new FFTFileCacheWriter
|
Chris@537
|
812 (name,
|
Chris@537
|
813 compactCache ? FFTCache::Compact :
|
Chris@537
|
814 m_polar ? FFTCache::Polar :
|
Chris@537
|
815 FFTCache::Rectangular,
|
Chris@537
|
816 width, m_height);
|
Chris@537
|
817
|
Chris@537
|
818 success = true;
|
Chris@537
|
819
|
Chris@678
|
820 } catch (std::exception &e) {
|
Chris@537
|
821
|
Chris@537
|
822 delete cb->fileCacheWriter;
|
Chris@537
|
823 cb->fileCacheWriter = 0;
|
Chris@537
|
824
|
Chris@843
|
825 cerr << "ERROR: Failed to construct disc cache for FFT data: "
|
Chris@843
|
826 << e.what() << endl;
|
Chris@678
|
827
|
Chris@678
|
828 throw;
|
Chris@537
|
829 }
|
Chris@537
|
830 }
|
Chris@537
|
831
|
Chris@548
|
832 m_cacheVectorLock.lockForWrite();
|
Chris@548
|
833
|
Chris@537
|
834 m_caches[c] = cb;
|
Chris@537
|
835
|
Chris@548
|
836 m_cacheVectorLock.unlock();
|
Chris@548
|
837
|
Chris@537
|
838 return success;
|
Chris@148
|
839 }
|
Chris@537
|
840
|
Chris@537
|
841 bool
|
Chris@537
|
842 FFTDataServer::makeCacheReader(int c)
|
Chris@537
|
843 {
|
Chris@537
|
844 // preconditions: m_caches[c] exists and contains a file writer;
|
Chris@537
|
845 // m_cacheVectorLock is not locked by this thread
|
Chris@537
|
846 #ifdef DEBUG_FFT_SERVER
|
chris@828
|
847 std::cerr << "FFTDataServer::makeCacheReader(" << c << ")" << std::endl;
|
Chris@537
|
848 #endif
|
Chris@148
|
849
|
Chris@537
|
850 QThread *me = QThread::currentThread();
|
Chris@537
|
851 QWriteLocker locker(&m_cacheVectorLock);
|
Chris@537
|
852 CacheBlock *cb(m_caches.at(c));
|
Chris@537
|
853 if (!cb || !cb->fileCacheWriter) return false;
|
Chris@537
|
854
|
Chris@537
|
855 try {
|
Chris@537
|
856
|
Chris@537
|
857 cb->fileCacheReader[me] = new FFTFileCacheReader(cb->fileCacheWriter);
|
Chris@537
|
858
|
Chris@678
|
859 } catch (std::exception &e) {
|
Chris@537
|
860
|
Chris@537
|
861 delete cb->fileCacheReader[me];
|
Chris@537
|
862 cb->fileCacheReader.erase(me);
|
Chris@537
|
863
|
Chris@843
|
864 cerr << "ERROR: Failed to construct disc cache reader for FFT data: "
|
Chris@843
|
865 << e.what() << endl;
|
Chris@537
|
866 return false;
|
Chris@537
|
867 }
|
Chris@537
|
868
|
Chris@537
|
869 // erase a reader that looks like it may no longer going to be
|
Chris@537
|
870 // used by this thread for a while (leaving alone the current
|
Chris@537
|
871 // and previous cache readers)
|
Chris@537
|
872 int deleteCandidate = c - 2;
|
Chris@537
|
873 if (deleteCandidate < 0) deleteCandidate = c + 2;
|
Chris@897
|
874 if (deleteCandidate >= (int)m_caches.size()) {
|
Chris@537
|
875 return true;
|
Chris@537
|
876 }
|
Chris@537
|
877
|
Chris@537
|
878 cb = m_caches.at(deleteCandidate);
|
Chris@537
|
879 if (cb && cb->fileCacheReader.find(me) != cb->fileCacheReader.end()) {
|
Chris@537
|
880 #ifdef DEBUG_FFT_SERVER
|
chris@828
|
881 std::cerr << "FFTDataServer::makeCacheReader: Deleting probably unpopular reader " << deleteCandidate << " for this thread (as I create reader " << c << ")" << std::endl;
|
Chris@537
|
882 #endif
|
Chris@537
|
883 delete cb->fileCacheReader[me];
|
Chris@537
|
884 cb->fileCacheReader.erase(me);
|
Chris@537
|
885 }
|
Chris@537
|
886
|
Chris@537
|
887 return true;
|
Chris@537
|
888 }
|
Chris@537
|
889
|
Chris@148
|
890 float
|
Chris@929
|
891 FFTDataServer::getMagnitudeAt(int x, int y)
|
Chris@148
|
892 {
|
Chris@183
|
893 Profiler profiler("FFTDataServer::getMagnitudeAt", false);
|
Chris@183
|
894
|
Chris@217
|
895 if (x >= m_width || y >= m_height) return 0;
|
Chris@217
|
896
|
Chris@678
|
897 float val = 0;
|
Chris@148
|
898
|
Chris@678
|
899 try {
|
Chris@929
|
900 int col;
|
Chris@678
|
901 FFTCacheReader *cache = getCacheReader(x, col);
|
Chris@678
|
902 if (!cache) return 0;
|
Chris@678
|
903
|
Chris@678
|
904 if (!cache->haveSetColumnAt(col)) {
|
Chris@678
|
905 Profiler profiler("FFTDataServer::getMagnitudeAt: filling");
|
Chris@280
|
906 #ifdef DEBUG_FFT_SERVER
|
chris@828
|
907 std::cerr << "FFTDataServer::getMagnitudeAt: calling fillColumn("
|
chris@828
|
908 << x << ")" << std::endl;
|
Chris@280
|
909 #endif
|
Chris@678
|
910 fillColumn(x);
|
Chris@678
|
911 }
|
Chris@678
|
912
|
Chris@678
|
913 val = cache->getMagnitudeAt(col, y);
|
Chris@678
|
914
|
Chris@678
|
915 } catch (std::exception &e) {
|
Chris@678
|
916 m_error = e.what();
|
Chris@148
|
917 }
|
Chris@678
|
918
|
Chris@678
|
919 return val;
|
Chris@148
|
920 }
|
Chris@148
|
921
|
Chris@408
|
922 bool
|
Chris@929
|
923 FFTDataServer::getMagnitudesAt(int x, float *values, int minbin, int count, int step)
|
Chris@408
|
924 {
|
Chris@408
|
925 Profiler profiler("FFTDataServer::getMagnitudesAt", false);
|
Chris@408
|
926
|
Chris@408
|
927 if (x >= m_width) return false;
|
Chris@408
|
928
|
Chris@408
|
929 if (minbin >= m_height) minbin = m_height - 1;
|
Chris@408
|
930 if (count == 0) count = (m_height - minbin) / step;
|
Chris@408
|
931 else if (minbin + count * step > m_height) {
|
Chris@408
|
932 count = (m_height - minbin) / step;
|
Chris@408
|
933 }
|
Chris@408
|
934
|
Chris@678
|
935 try {
|
Chris@929
|
936 int col;
|
Chris@678
|
937 FFTCacheReader *cache = getCacheReader(x, col);
|
Chris@678
|
938 if (!cache) return false;
|
Chris@408
|
939
|
Chris@678
|
940 if (!cache->haveSetColumnAt(col)) {
|
Chris@678
|
941 Profiler profiler("FFTDataServer::getMagnitudesAt: filling");
|
Chris@678
|
942 fillColumn(x);
|
Chris@678
|
943 }
|
Chris@678
|
944
|
Chris@678
|
945 cache->getMagnitudesAt(col, values, minbin, count, step);
|
Chris@678
|
946
|
Chris@678
|
947 } catch (std::exception &e) {
|
Chris@678
|
948 m_error = e.what();
|
Chris@678
|
949 return false;
|
Chris@408
|
950 }
|
Chris@408
|
951
|
Chris@408
|
952 return true;
|
Chris@408
|
953 }
|
Chris@408
|
954
|
Chris@148
|
955 float
|
Chris@929
|
956 FFTDataServer::getNormalizedMagnitudeAt(int x, int y)
|
Chris@148
|
957 {
|
Chris@183
|
958 Profiler profiler("FFTDataServer::getNormalizedMagnitudeAt", false);
|
Chris@183
|
959
|
Chris@217
|
960 if (x >= m_width || y >= m_height) return 0;
|
Chris@217
|
961
|
Chris@678
|
962 float val = 0;
|
Chris@148
|
963
|
Chris@678
|
964 try {
|
Chris@678
|
965
|
Chris@929
|
966 int col;
|
Chris@678
|
967 FFTCacheReader *cache = getCacheReader(x, col);
|
Chris@678
|
968 if (!cache) return 0;
|
Chris@678
|
969
|
Chris@678
|
970 if (!cache->haveSetColumnAt(col)) {
|
Chris@678
|
971 Profiler profiler("FFTDataServer::getNormalizedMagnitudeAt: filling");
|
Chris@678
|
972 fillColumn(x);
|
Chris@678
|
973 }
|
Chris@678
|
974 val = cache->getNormalizedMagnitudeAt(col, y);
|
Chris@678
|
975
|
Chris@678
|
976 } catch (std::exception &e) {
|
Chris@678
|
977 m_error = e.what();
|
Chris@148
|
978 }
|
Chris@678
|
979
|
Chris@678
|
980 return val;
|
Chris@148
|
981 }
|
Chris@148
|
982
|
Chris@408
|
983 bool
|
Chris@929
|
984 FFTDataServer::getNormalizedMagnitudesAt(int x, float *values, int minbin, int count, int step)
|
Chris@408
|
985 {
|
Chris@408
|
986 Profiler profiler("FFTDataServer::getNormalizedMagnitudesAt", false);
|
Chris@408
|
987
|
Chris@408
|
988 if (x >= m_width) return false;
|
Chris@408
|
989
|
Chris@408
|
990 if (minbin >= m_height) minbin = m_height - 1;
|
Chris@408
|
991 if (count == 0) count = (m_height - minbin) / step;
|
Chris@408
|
992 else if (minbin + count * step > m_height) {
|
Chris@408
|
993 count = (m_height - minbin) / step;
|
Chris@408
|
994 }
|
Chris@408
|
995
|
Chris@678
|
996 try {
|
Chris@408
|
997
|
Chris@929
|
998 int col;
|
Chris@678
|
999 FFTCacheReader *cache = getCacheReader(x, col);
|
Chris@678
|
1000 if (!cache) return false;
|
Chris@408
|
1001
|
Chris@678
|
1002 if (!cache->haveSetColumnAt(col)) {
|
Chris@678
|
1003 Profiler profiler("FFTDataServer::getNormalizedMagnitudesAt: filling");
|
Chris@678
|
1004 fillColumn(x);
|
Chris@678
|
1005 }
|
Chris@678
|
1006
|
Chris@929
|
1007 for (int i = 0; i < count; ++i) {
|
Chris@678
|
1008 values[i] = cache->getNormalizedMagnitudeAt(col, i * step + minbin);
|
Chris@678
|
1009 }
|
Chris@678
|
1010
|
Chris@678
|
1011 } catch (std::exception &e) {
|
Chris@678
|
1012 m_error = e.what();
|
Chris@678
|
1013 return false;
|
Chris@408
|
1014 }
|
Chris@408
|
1015
|
Chris@408
|
1016 return true;
|
Chris@408
|
1017 }
|
Chris@408
|
1018
|
Chris@148
|
1019 float
|
Chris@929
|
1020 FFTDataServer::getMaximumMagnitudeAt(int x)
|
Chris@148
|
1021 {
|
Chris@183
|
1022 Profiler profiler("FFTDataServer::getMaximumMagnitudeAt", false);
|
Chris@183
|
1023
|
Chris@217
|
1024 if (x >= m_width) return 0;
|
Chris@217
|
1025
|
Chris@678
|
1026 float val = 0;
|
Chris@148
|
1027
|
Chris@678
|
1028 try {
|
Chris@678
|
1029
|
Chris@929
|
1030 int col;
|
Chris@678
|
1031 FFTCacheReader *cache = getCacheReader(x, col);
|
Chris@678
|
1032 if (!cache) return 0;
|
Chris@678
|
1033
|
Chris@678
|
1034 if (!cache->haveSetColumnAt(col)) {
|
Chris@678
|
1035 Profiler profiler("FFTDataServer::getMaximumMagnitudeAt: filling");
|
Chris@678
|
1036 fillColumn(x);
|
Chris@678
|
1037 }
|
Chris@678
|
1038 val = cache->getMaximumMagnitudeAt(col);
|
Chris@678
|
1039
|
Chris@678
|
1040 } catch (std::exception &e) {
|
Chris@678
|
1041 m_error = e.what();
|
Chris@148
|
1042 }
|
Chris@678
|
1043
|
Chris@678
|
1044 return val;
|
Chris@148
|
1045 }
|
Chris@148
|
1046
|
Chris@148
|
1047 float
|
Chris@929
|
1048 FFTDataServer::getPhaseAt(int x, int y)
|
Chris@148
|
1049 {
|
Chris@183
|
1050 Profiler profiler("FFTDataServer::getPhaseAt", false);
|
Chris@183
|
1051
|
Chris@217
|
1052 if (x >= m_width || y >= m_height) return 0;
|
Chris@217
|
1053
|
Chris@678
|
1054 float val = 0;
|
Chris@148
|
1055
|
Chris@678
|
1056 try {
|
Chris@678
|
1057
|
Chris@929
|
1058 int col;
|
Chris@678
|
1059 FFTCacheReader *cache = getCacheReader(x, col);
|
Chris@678
|
1060 if (!cache) return 0;
|
Chris@678
|
1061
|
Chris@678
|
1062 if (!cache->haveSetColumnAt(col)) {
|
Chris@678
|
1063 Profiler profiler("FFTDataServer::getPhaseAt: filling");
|
Chris@678
|
1064 fillColumn(x);
|
Chris@678
|
1065 }
|
Chris@678
|
1066 val = cache->getPhaseAt(col, y);
|
Chris@678
|
1067
|
Chris@678
|
1068 } catch (std::exception &e) {
|
Chris@678
|
1069 m_error = e.what();
|
Chris@148
|
1070 }
|
Chris@678
|
1071
|
Chris@678
|
1072 return val;
|
Chris@148
|
1073 }
|
Chris@148
|
1074
|
Chris@408
|
1075 bool
|
Chris@929
|
1076 FFTDataServer::getPhasesAt(int x, float *values, int minbin, int count, int step)
|
Chris@408
|
1077 {
|
Chris@408
|
1078 Profiler profiler("FFTDataServer::getPhasesAt", false);
|
Chris@408
|
1079
|
Chris@408
|
1080 if (x >= m_width) return false;
|
Chris@408
|
1081
|
Chris@408
|
1082 if (minbin >= m_height) minbin = m_height - 1;
|
Chris@408
|
1083 if (count == 0) count = (m_height - minbin) / step;
|
Chris@408
|
1084 else if (minbin + count * step > m_height) {
|
Chris@408
|
1085 count = (m_height - minbin) / step;
|
Chris@408
|
1086 }
|
Chris@408
|
1087
|
Chris@678
|
1088 try {
|
Chris@408
|
1089
|
Chris@929
|
1090 int col;
|
Chris@678
|
1091 FFTCacheReader *cache = getCacheReader(x, col);
|
Chris@678
|
1092 if (!cache) return false;
|
Chris@408
|
1093
|
Chris@678
|
1094 if (!cache->haveSetColumnAt(col)) {
|
Chris@678
|
1095 Profiler profiler("FFTDataServer::getPhasesAt: filling");
|
Chris@678
|
1096 fillColumn(x);
|
Chris@678
|
1097 }
|
Chris@678
|
1098
|
Chris@929
|
1099 for (int i = 0; i < count; ++i) {
|
Chris@678
|
1100 values[i] = cache->getPhaseAt(col, i * step + minbin);
|
Chris@678
|
1101 }
|
Chris@678
|
1102
|
Chris@678
|
1103 } catch (std::exception &e) {
|
Chris@678
|
1104 m_error = e.what();
|
Chris@678
|
1105 return false;
|
Chris@408
|
1106 }
|
Chris@408
|
1107
|
Chris@408
|
1108 return true;
|
Chris@408
|
1109 }
|
Chris@408
|
1110
|
Chris@148
|
1111 void
|
Chris@929
|
1112 FFTDataServer::getValuesAt(int x, int y, float &real, float &imaginary)
|
Chris@148
|
1113 {
|
Chris@183
|
1114 Profiler profiler("FFTDataServer::getValuesAt", false);
|
Chris@183
|
1115
|
Chris@216
|
1116 if (x >= m_width || y >= m_height) {
|
Chris@216
|
1117 real = 0;
|
Chris@216
|
1118 imaginary = 0;
|
Chris@216
|
1119 return;
|
Chris@216
|
1120 }
|
Chris@216
|
1121
|
Chris@678
|
1122 try {
|
Chris@216
|
1123
|
Chris@929
|
1124 int col;
|
Chris@678
|
1125 FFTCacheReader *cache = getCacheReader(x, col);
|
Chris@678
|
1126
|
Chris@678
|
1127 if (!cache) {
|
Chris@678
|
1128 real = 0;
|
Chris@678
|
1129 imaginary = 0;
|
Chris@678
|
1130 return;
|
Chris@678
|
1131 }
|
Chris@678
|
1132
|
Chris@678
|
1133 if (!cache->haveSetColumnAt(col)) {
|
Chris@678
|
1134 Profiler profiler("FFTDataServer::getValuesAt: filling");
|
Chris@678
|
1135 #ifdef DEBUG_FFT_SERVER
|
chris@828
|
1136 std::cerr << "FFTDataServer::getValuesAt(" << x << ", " << y << "): filling" << std::endl;
|
Chris@678
|
1137 #endif
|
Chris@678
|
1138 fillColumn(x);
|
Chris@678
|
1139 }
|
Chris@678
|
1140
|
Chris@678
|
1141 cache->getValuesAt(col, y, real, imaginary);
|
Chris@678
|
1142
|
Chris@678
|
1143 } catch (std::exception &e) {
|
Chris@678
|
1144 m_error = e.what();
|
Chris@216
|
1145 }
|
Chris@148
|
1146 }
|
Chris@148
|
1147
|
Chris@148
|
1148 bool
|
Chris@929
|
1149 FFTDataServer::getValuesAt(int x, float *reals, float *imaginaries, int minbin, int count, int step)
|
Chris@556
|
1150 {
|
Chris@556
|
1151 Profiler profiler("FFTDataServer::getValuesAt", false);
|
Chris@556
|
1152
|
Chris@556
|
1153 if (x >= m_width) return false;
|
Chris@556
|
1154
|
Chris@556
|
1155 if (minbin >= m_height) minbin = m_height - 1;
|
Chris@556
|
1156 if (count == 0) count = (m_height - minbin) / step;
|
Chris@556
|
1157 else if (minbin + count * step > m_height) {
|
Chris@556
|
1158 count = (m_height - minbin) / step;
|
Chris@556
|
1159 }
|
Chris@556
|
1160
|
Chris@678
|
1161 try {
|
Chris@556
|
1162
|
Chris@929
|
1163 int col;
|
Chris@678
|
1164 FFTCacheReader *cache = getCacheReader(x, col);
|
Chris@678
|
1165 if (!cache) return false;
|
Chris@556
|
1166
|
Chris@678
|
1167 if (!cache->haveSetColumnAt(col)) {
|
Chris@678
|
1168 Profiler profiler("FFTDataServer::getValuesAt: filling");
|
Chris@678
|
1169 fillColumn(x);
|
Chris@678
|
1170 }
|
Chris@678
|
1171
|
Chris@929
|
1172 for (int i = 0; i < count; ++i) {
|
Chris@678
|
1173 cache->getValuesAt(col, i * step + minbin, reals[i], imaginaries[i]);
|
Chris@678
|
1174 }
|
Chris@678
|
1175
|
Chris@678
|
1176 } catch (std::exception &e) {
|
Chris@678
|
1177 m_error = e.what();
|
Chris@678
|
1178 return false;
|
Chris@556
|
1179 }
|
Chris@556
|
1180
|
Chris@556
|
1181 return true;
|
Chris@556
|
1182 }
|
Chris@556
|
1183
|
Chris@556
|
1184 bool
|
Chris@929
|
1185 FFTDataServer::isColumnReady(int x)
|
Chris@148
|
1186 {
|
Chris@183
|
1187 Profiler profiler("FFTDataServer::isColumnReady", false);
|
Chris@183
|
1188
|
Chris@217
|
1189 if (x >= m_width) return true;
|
Chris@217
|
1190
|
Chris@148
|
1191 if (!haveCache(x)) {
|
Chris@537
|
1192 /*!!!
|
Chris@148
|
1193 if (m_lastUsedCache == -1) {
|
Chris@183
|
1194 if (m_suspended) {
|
chris@828
|
1195 std::cerr << "FFTDataServer::isColumnReady(" << x << "): no cache, calling resume" << std::endl;
|
Chris@183
|
1196 resume();
|
Chris@183
|
1197 }
|
Chris@148
|
1198 m_fillThread->start();
|
Chris@148
|
1199 }
|
Chris@537
|
1200 */
|
Chris@148
|
1201 return false;
|
Chris@148
|
1202 }
|
Chris@148
|
1203
|
Chris@678
|
1204 try {
|
Chris@148
|
1205
|
Chris@929
|
1206 int col;
|
Chris@678
|
1207 FFTCacheReader *cache = getCacheReader(x, col);
|
Chris@678
|
1208 if (!cache) return true;
|
Chris@678
|
1209
|
Chris@678
|
1210 return cache->haveSetColumnAt(col);
|
Chris@678
|
1211
|
Chris@678
|
1212 } catch (std::exception &e) {
|
Chris@678
|
1213 m_error = e.what();
|
Chris@678
|
1214 return false;
|
Chris@678
|
1215 }
|
Chris@148
|
1216 }
|
Chris@148
|
1217
|
Chris@148
|
1218 void
|
Chris@929
|
1219 FFTDataServer::fillColumn(int x)
|
Chris@148
|
1220 {
|
Chris@183
|
1221 Profiler profiler("FFTDataServer::fillColumn", false);
|
Chris@183
|
1222
|
Chris@272
|
1223 if (!m_model->isReady()) {
|
Chris@843
|
1224 cerr << "WARNING: FFTDataServer::fillColumn("
|
Chris@843
|
1225 << x << "): model not yet ready" << endl;
|
Chris@272
|
1226 return;
|
Chris@272
|
1227 }
|
Chris@549
|
1228 /*
|
Chris@217
|
1229 if (!m_fftInput) {
|
Chris@843
|
1230 cerr << "WARNING: FFTDataServer::fillColumn(" << x << "): "
|
Chris@217
|
1231 << "input has already been completed and discarded?"
|
Chris@843
|
1232 << endl;
|
Chris@217
|
1233 return;
|
Chris@217
|
1234 }
|
Chris@549
|
1235 */
|
Chris@217
|
1236 if (x >= m_width) {
|
Chris@843
|
1237 cerr << "WARNING: FFTDataServer::fillColumn(" << x << "): "
|
Chris@217
|
1238 << "x > width (" << x << " > " << m_width << ")"
|
Chris@843
|
1239 << endl;
|
Chris@217
|
1240 return;
|
Chris@217
|
1241 }
|
Chris@217
|
1242
|
Chris@929
|
1243 int col;
|
Chris@148
|
1244 #ifdef DEBUG_FFT_SERVER_FILL
|
Chris@843
|
1245 cout << "FFTDataServer::fillColumn(" << x << ")" << endl;
|
Chris@148
|
1246 #endif
|
Chris@537
|
1247 FFTCacheWriter *cache = getCacheWriter(x, col);
|
Chris@200
|
1248 if (!cache) return;
|
Chris@148
|
1249
|
Chris@408
|
1250 int winsize = m_windowSize;
|
Chris@408
|
1251 int fftsize = m_fftSize;
|
Chris@408
|
1252 int hs = fftsize/2;
|
Chris@408
|
1253
|
Chris@1038
|
1254 sv_frame_t pfx = 0;
|
Chris@408
|
1255 int off = (fftsize - winsize) / 2;
|
Chris@148
|
1256
|
Chris@1038
|
1257 sv_frame_t startFrame = m_windowIncrement * sv_frame_t(x);
|
Chris@1038
|
1258 sv_frame_t endFrame = startFrame + m_windowSize;
|
Chris@148
|
1259
|
Chris@408
|
1260 startFrame -= winsize / 2;
|
Chris@408
|
1261 endFrame -= winsize / 2;
|
Chris@148
|
1262
|
Chris@549
|
1263 #ifdef DEBUG_FFT_SERVER_FILL
|
chris@828
|
1264 std::cerr << "FFTDataServer::fillColumn: requesting frames "
|
Chris@549
|
1265 << startFrame + pfx << " -> " << endFrame << " ( = "
|
Chris@549
|
1266 << endFrame - (startFrame + pfx) << ") at index "
|
Chris@549
|
1267 << off + pfx << " in buffer of size " << m_fftSize
|
Chris@549
|
1268 << " with window size " << m_windowSize
|
chris@828
|
1269 << " from channel " << m_channel << std::endl;
|
Chris@549
|
1270 #endif
|
Chris@549
|
1271
|
Chris@549
|
1272 QMutexLocker locker(&m_fftBuffersLock);
|
Chris@549
|
1273
|
Chris@550
|
1274 // We may have been called from a function that wanted to obtain a
|
Chris@550
|
1275 // column using an FFTCacheReader. Before calling us, it checked
|
Chris@550
|
1276 // whether the column was available already, and the reader
|
Chris@550
|
1277 // reported that it wasn't. Now we test again, with the mutex
|
Chris@550
|
1278 // held, to avoid a race condition in case another thread has
|
Chris@550
|
1279 // called fillColumn at the same time.
|
Chris@550
|
1280 if (cache->haveSetColumnAt(x & m_cacheWidthMask)) {
|
Chris@550
|
1281 return;
|
Chris@549
|
1282 }
|
Chris@549
|
1283
|
Chris@549
|
1284 if (!m_fftInput) {
|
Chris@843
|
1285 cerr << "WARNING: FFTDataServer::fillColumn(" << x << "): "
|
Chris@549
|
1286 << "input has already been completed and discarded?"
|
Chris@843
|
1287 << endl;
|
Chris@549
|
1288 return;
|
Chris@549
|
1289 }
|
Chris@549
|
1290
|
Chris@408
|
1291 for (int i = 0; i < off; ++i) {
|
Chris@408
|
1292 m_fftInput[i] = 0.0;
|
Chris@408
|
1293 }
|
Chris@148
|
1294
|
Chris@408
|
1295 for (int i = 0; i < off; ++i) {
|
Chris@408
|
1296 m_fftInput[fftsize - i - 1] = 0.0;
|
Chris@148
|
1297 }
|
Chris@148
|
1298
|
Chris@148
|
1299 if (startFrame < 0) {
|
Chris@408
|
1300 pfx = -startFrame;
|
Chris@408
|
1301 for (int i = 0; i < pfx; ++i) {
|
Chris@148
|
1302 m_fftInput[off + i] = 0.0;
|
Chris@148
|
1303 }
|
Chris@148
|
1304 }
|
Chris@148
|
1305
|
Chris@1038
|
1306 sv_frame_t count = 0;
|
Chris@300
|
1307 if (endFrame > startFrame + pfx) count = endFrame - (startFrame + pfx);
|
Chris@300
|
1308
|
Chris@1038
|
1309 sv_frame_t got = m_model->getData(m_channel, startFrame + pfx,
|
Chris@1038
|
1310 count, m_fftInput + off + pfx);
|
Chris@148
|
1311
|
Chris@408
|
1312 while (got + pfx < winsize) {
|
Chris@148
|
1313 m_fftInput[off + got + pfx] = 0.0;
|
Chris@148
|
1314 ++got;
|
Chris@148
|
1315 }
|
Chris@148
|
1316
|
Chris@148
|
1317 if (m_channel == -1) {
|
Chris@148
|
1318 int channels = m_model->getChannelCount();
|
Chris@148
|
1319 if (channels > 1) {
|
Chris@408
|
1320 for (int i = 0; i < winsize; ++i) {
|
Chris@1038
|
1321 m_fftInput[off + i] /= float(channels);
|
Chris@148
|
1322 }
|
Chris@148
|
1323 }
|
Chris@148
|
1324 }
|
Chris@148
|
1325
|
Chris@148
|
1326 m_windower.cut(m_fftInput + off);
|
Chris@148
|
1327
|
Chris@408
|
1328 for (int i = 0; i < hs; ++i) {
|
Chris@148
|
1329 fftsample temp = m_fftInput[i];
|
Chris@408
|
1330 m_fftInput[i] = m_fftInput[i + hs];
|
Chris@408
|
1331 m_fftInput[i + hs] = temp;
|
Chris@148
|
1332 }
|
Chris@148
|
1333
|
Chris@226
|
1334 fftf_execute(m_fftPlan);
|
Chris@148
|
1335
|
Chris@408
|
1336 float factor = 0.f;
|
Chris@148
|
1337
|
Chris@408
|
1338 if (cache->getStorageType() == FFTCache::Compact ||
|
Chris@408
|
1339 cache->getStorageType() == FFTCache::Polar) {
|
Chris@408
|
1340
|
Chris@408
|
1341 for (int i = 0; i <= hs; ++i) {
|
Chris@408
|
1342 fftsample real = m_fftOutput[i][0];
|
Chris@408
|
1343 fftsample imag = m_fftOutput[i][1];
|
Chris@408
|
1344 float mag = sqrtf(real * real + imag * imag);
|
Chris@408
|
1345 m_workbuffer[i] = mag;
|
Chris@408
|
1346 m_workbuffer[i + hs + 1] = atan2f(imag, real);
|
Chris@408
|
1347 if (mag > factor) factor = mag;
|
Chris@408
|
1348 }
|
Chris@408
|
1349
|
Chris@408
|
1350 } else {
|
Chris@408
|
1351
|
Chris@408
|
1352 for (int i = 0; i <= hs; ++i) {
|
Chris@408
|
1353 m_workbuffer[i] = m_fftOutput[i][0];
|
Chris@408
|
1354 m_workbuffer[i + hs + 1] = m_fftOutput[i][1];
|
Chris@408
|
1355 }
|
Chris@148
|
1356 }
|
Chris@148
|
1357
|
Chris@408
|
1358 Profiler subprof("FFTDataServer::fillColumn: set to cache");
|
Chris@408
|
1359
|
Chris@549
|
1360 if (cache->getStorageType() == FFTCache::Compact ||
|
Chris@549
|
1361 cache->getStorageType() == FFTCache::Polar) {
|
Chris@549
|
1362
|
Chris@549
|
1363 cache->setColumnAt(col,
|
Chris@549
|
1364 m_workbuffer,
|
Chris@549
|
1365 m_workbuffer + hs + 1,
|
Chris@549
|
1366 factor);
|
Chris@408
|
1367
|
Chris@549
|
1368 } else {
|
Chris@408
|
1369
|
Chris@549
|
1370 cache->setColumnAt(col,
|
Chris@549
|
1371 m_workbuffer,
|
Chris@549
|
1372 m_workbuffer + hs + 1);
|
Chris@408
|
1373 }
|
Chris@154
|
1374
|
Chris@183
|
1375 if (m_suspended) {
|
chris@828
|
1376 // std::cerr << "FFTDataServer::fillColumn(" << x << "): calling resume" << std::endl;
|
Chris@183
|
1377 // resume();
|
Chris@183
|
1378 }
|
Chris@148
|
1379 }
|
Chris@148
|
1380
|
Chris@537
|
1381 void
|
Chris@537
|
1382 FFTDataServer::fillComplete()
|
Chris@537
|
1383 {
|
Chris@537
|
1384 for (int i = 0; i < int(m_caches.size()); ++i) {
|
Chris@678
|
1385 if (!m_caches[i]) continue;
|
Chris@537
|
1386 if (m_caches[i]->memoryCache) {
|
Chris@537
|
1387 m_caches[i]->memoryCache->allColumnsWritten();
|
Chris@537
|
1388 }
|
Chris@537
|
1389 if (m_caches[i]->fileCacheWriter) {
|
Chris@537
|
1390 m_caches[i]->fileCacheWriter->allColumnsWritten();
|
Chris@537
|
1391 }
|
Chris@537
|
1392 }
|
Chris@537
|
1393 }
|
Chris@537
|
1394
|
Chris@678
|
1395 QString
|
Chris@678
|
1396 FFTDataServer::getError() const
|
Chris@678
|
1397 {
|
Chris@678
|
1398 if (m_error != "") return m_error;
|
Chris@678
|
1399 else if (m_fillThread) return m_fillThread->getError();
|
Chris@678
|
1400 else return "";
|
Chris@678
|
1401 }
|
Chris@678
|
1402
|
Chris@929
|
1403 int
|
Chris@148
|
1404 FFTDataServer::getFillCompletion() const
|
Chris@148
|
1405 {
|
Chris@148
|
1406 if (m_fillThread) return m_fillThread->getCompletion();
|
Chris@148
|
1407 else return 100;
|
Chris@148
|
1408 }
|
Chris@148
|
1409
|
Chris@1038
|
1410 sv_frame_t
|
Chris@148
|
1411 FFTDataServer::getFillExtent() const
|
Chris@148
|
1412 {
|
Chris@148
|
1413 if (m_fillThread) return m_fillThread->getExtent();
|
Chris@148
|
1414 else return m_model->getEndFrame();
|
Chris@148
|
1415 }
|
Chris@148
|
1416
|
Chris@148
|
1417 QString
|
Chris@148
|
1418 FFTDataServer::generateFileBasename() const
|
Chris@148
|
1419 {
|
Chris@148
|
1420 return generateFileBasename(m_model, m_channel, m_windower.getType(),
|
Chris@148
|
1421 m_windowSize, m_windowIncrement, m_fftSize,
|
Chris@148
|
1422 m_polar);
|
Chris@148
|
1423 }
|
Chris@148
|
1424
|
Chris@148
|
1425 QString
|
Chris@148
|
1426 FFTDataServer::generateFileBasename(const DenseTimeValueModel *model,
|
Chris@148
|
1427 int channel,
|
Chris@148
|
1428 WindowType windowType,
|
Chris@929
|
1429 int windowSize,
|
Chris@929
|
1430 int windowIncrement,
|
Chris@929
|
1431 int fftSize,
|
Chris@148
|
1432 bool polar)
|
Chris@148
|
1433 {
|
Chris@986
|
1434 return QString("%1-%2-%3-%4-%5-%6%7")
|
Chris@986
|
1435 .arg(XmlExportable::getObjectExportId(model))
|
Chris@986
|
1436 .arg(channel + 1)
|
Chris@986
|
1437 .arg((int)windowType)
|
Chris@986
|
1438 .arg(windowSize)
|
Chris@986
|
1439 .arg(windowIncrement)
|
Chris@986
|
1440 .arg(fftSize)
|
Chris@986
|
1441 .arg(polar ? "-p" : "-r");
|
Chris@148
|
1442 }
|
Chris@148
|
1443
|
Chris@148
|
1444 void
|
Chris@148
|
1445 FFTDataServer::FillThread::run()
|
Chris@148
|
1446 {
|
Chris@411
|
1447 #ifdef DEBUG_FFT_SERVER_FILL
|
chris@828
|
1448 std::cerr << "FFTDataServer::FillThread::run()" << std::endl;
|
Chris@411
|
1449 #endif
|
Chris@411
|
1450
|
Chris@148
|
1451 m_extent = 0;
|
Chris@148
|
1452 m_completion = 0;
|
Chris@148
|
1453
|
Chris@272
|
1454 while (!m_server.m_model->isReady() && !m_server.m_exiting) {
|
Chris@411
|
1455 #ifdef DEBUG_FFT_SERVER_FILL
|
chris@828
|
1456 std::cerr << "FFTDataServer::FillThread::run(): waiting for model " << m_server.m_model << " to be ready" << std::endl;
|
Chris@411
|
1457 #endif
|
Chris@272
|
1458 sleep(1);
|
Chris@272
|
1459 }
|
Chris@272
|
1460 if (m_server.m_exiting) return;
|
Chris@272
|
1461
|
Chris@1038
|
1462 sv_frame_t start = m_server.m_model->getStartFrame();
|
Chris@1038
|
1463 sv_frame_t end = m_server.m_model->getEndFrame();
|
Chris@1038
|
1464 sv_frame_t remainingEnd = end;
|
Chris@148
|
1465
|
Chris@148
|
1466 int counter = 0;
|
Chris@246
|
1467 int updateAt = 1;
|
Chris@1038
|
1468 int maxUpdateAt = int(end / m_server.m_windowIncrement) / 20;
|
Chris@246
|
1469 if (maxUpdateAt < 100) maxUpdateAt = 100;
|
Chris@148
|
1470
|
Chris@148
|
1471 if (m_fillFrom > start) {
|
Chris@148
|
1472
|
Chris@1038
|
1473 for (sv_frame_t f = m_fillFrom; f < end; f += m_server.m_windowIncrement) {
|
Chris@148
|
1474
|
Chris@678
|
1475 try {
|
Chris@678
|
1476 m_server.fillColumn(int((f - start) / m_server.m_windowIncrement));
|
Chris@678
|
1477 } catch (std::exception &e) {
|
chris@828
|
1478 std::cerr << "FFTDataServer::FillThread::run: exception: " << e.what() << std::endl;
|
Chris@678
|
1479 m_error = e.what();
|
Chris@678
|
1480 m_server.fillComplete();
|
Chris@678
|
1481 m_completion = 100;
|
Chris@678
|
1482 m_extent = end;
|
Chris@678
|
1483 return;
|
Chris@678
|
1484 }
|
Chris@148
|
1485
|
Chris@148
|
1486 if (m_server.m_exiting) return;
|
Chris@148
|
1487
|
Chris@148
|
1488 while (m_server.m_suspended) {
|
Chris@148
|
1489 #ifdef DEBUG_FFT_SERVER
|
Chris@843
|
1490 cerr << "FFTDataServer(" << this << " [" << (void *)QThread::currentThreadId() << "]): suspended, waiting..." << endl;
|
Chris@148
|
1491 #endif
|
Chris@549
|
1492 MutexLocker locker(&m_server.m_fftBuffersLock,
|
Chris@549
|
1493 "FFTDataServer::run::m_fftBuffersLock [1]");
|
Chris@549
|
1494 if (m_server.m_suspended && !m_server.m_exiting) {
|
Chris@549
|
1495 m_server.m_condition.wait(&m_server.m_fftBuffersLock, 10000);
|
Chris@244
|
1496 }
|
Chris@159
|
1497 #ifdef DEBUG_FFT_SERVER
|
Chris@843
|
1498 cerr << "FFTDataServer(" << this << " [" << (void *)QThread::currentThreadId() << "]): waited" << endl;
|
Chris@159
|
1499 #endif
|
Chris@148
|
1500 if (m_server.m_exiting) return;
|
Chris@148
|
1501 }
|
Chris@148
|
1502
|
Chris@148
|
1503 if (++counter == updateAt) {
|
Chris@148
|
1504 m_extent = f;
|
Chris@929
|
1505 m_completion = int(100 * fabsf(float(f - m_fillFrom) /
|
Chris@148
|
1506 float(end - start)));
|
Chris@148
|
1507 counter = 0;
|
Chris@246
|
1508 if (updateAt < maxUpdateAt) {
|
Chris@246
|
1509 updateAt *= 2;
|
Chris@246
|
1510 if (updateAt > maxUpdateAt) updateAt = maxUpdateAt;
|
Chris@246
|
1511 }
|
Chris@148
|
1512 }
|
Chris@148
|
1513 }
|
Chris@148
|
1514
|
Chris@148
|
1515 remainingEnd = m_fillFrom;
|
Chris@148
|
1516 if (remainingEnd > start) --remainingEnd;
|
Chris@148
|
1517 else remainingEnd = start;
|
Chris@148
|
1518 }
|
Chris@148
|
1519
|
Chris@929
|
1520 int baseCompletion = m_completion;
|
Chris@148
|
1521
|
Chris@1038
|
1522 for (sv_frame_t f = start; f < remainingEnd; f += m_server.m_windowIncrement) {
|
Chris@148
|
1523
|
Chris@678
|
1524 try {
|
Chris@678
|
1525 m_server.fillColumn(int((f - start) / m_server.m_windowIncrement));
|
Chris@678
|
1526 } catch (std::exception &e) {
|
chris@828
|
1527 std::cerr << "FFTDataServer::FillThread::run: exception: " << e.what() << std::endl;
|
Chris@678
|
1528 m_error = e.what();
|
Chris@678
|
1529 m_server.fillComplete();
|
Chris@678
|
1530 m_completion = 100;
|
Chris@678
|
1531 m_extent = end;
|
Chris@678
|
1532 return;
|
Chris@678
|
1533 }
|
Chris@148
|
1534
|
Chris@148
|
1535 if (m_server.m_exiting) return;
|
Chris@148
|
1536
|
Chris@148
|
1537 while (m_server.m_suspended) {
|
Chris@148
|
1538 #ifdef DEBUG_FFT_SERVER
|
Chris@843
|
1539 cerr << "FFTDataServer(" << this << " [" << (void *)QThread::currentThreadId() << "]): suspended, waiting..." << endl;
|
Chris@148
|
1540 #endif
|
Chris@244
|
1541 {
|
Chris@549
|
1542 MutexLocker locker(&m_server.m_fftBuffersLock,
|
Chris@549
|
1543 "FFTDataServer::run::m_fftBuffersLock [2]");
|
Chris@549
|
1544 if (m_server.m_suspended && !m_server.m_exiting) {
|
Chris@549
|
1545 m_server.m_condition.wait(&m_server.m_fftBuffersLock, 10000);
|
Chris@549
|
1546 }
|
Chris@244
|
1547 }
|
Chris@148
|
1548 if (m_server.m_exiting) return;
|
Chris@148
|
1549 }
|
Chris@148
|
1550
|
Chris@148
|
1551 if (++counter == updateAt) {
|
Chris@148
|
1552 m_extent = f;
|
Chris@148
|
1553 m_completion = baseCompletion +
|
Chris@929
|
1554 int(100 * fabsf(float(f - start) /
|
Chris@148
|
1555 float(end - start)));
|
Chris@148
|
1556 counter = 0;
|
Chris@246
|
1557 if (updateAt < maxUpdateAt) {
|
Chris@246
|
1558 updateAt *= 2;
|
Chris@246
|
1559 if (updateAt > maxUpdateAt) updateAt = maxUpdateAt;
|
Chris@246
|
1560 }
|
Chris@148
|
1561 }
|
Chris@148
|
1562 }
|
Chris@148
|
1563
|
Chris@537
|
1564 m_server.fillComplete();
|
Chris@148
|
1565 m_completion = 100;
|
Chris@148
|
1566 m_extent = end;
|
Chris@537
|
1567
|
Chris@537
|
1568 #ifdef DEBUG_FFT_SERVER
|
chris@828
|
1569 std::cerr << "FFTDataServer::FillThread::run exiting" << std::endl;
|
Chris@537
|
1570 #endif
|
Chris@148
|
1571 }
|
Chris@148
|
1572
|