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@1084
|
532 if (m_width < ((maxCacheSize * 2) / columnSize)) 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@1081
|
905 if (getError() != "") return false;
|
Chris@678
|
906 Profiler profiler("FFTDataServer::getMagnitudeAt: filling");
|
Chris@280
|
907 #ifdef DEBUG_FFT_SERVER
|
chris@828
|
908 std::cerr << "FFTDataServer::getMagnitudeAt: calling fillColumn("
|
chris@828
|
909 << x << ")" << std::endl;
|
Chris@280
|
910 #endif
|
Chris@678
|
911 fillColumn(x);
|
Chris@678
|
912 }
|
Chris@678
|
913
|
Chris@678
|
914 val = cache->getMagnitudeAt(col, y);
|
Chris@678
|
915
|
Chris@678
|
916 } catch (std::exception &e) {
|
Chris@678
|
917 m_error = e.what();
|
Chris@148
|
918 }
|
Chris@678
|
919
|
Chris@678
|
920 return val;
|
Chris@148
|
921 }
|
Chris@148
|
922
|
Chris@408
|
923 bool
|
Chris@929
|
924 FFTDataServer::getMagnitudesAt(int x, float *values, int minbin, int count, int step)
|
Chris@408
|
925 {
|
Chris@408
|
926 Profiler profiler("FFTDataServer::getMagnitudesAt", false);
|
Chris@408
|
927
|
Chris@408
|
928 if (x >= m_width) return false;
|
Chris@408
|
929
|
Chris@408
|
930 if (minbin >= m_height) minbin = m_height - 1;
|
Chris@408
|
931 if (count == 0) count = (m_height - minbin) / step;
|
Chris@408
|
932 else if (minbin + count * step > m_height) {
|
Chris@408
|
933 count = (m_height - minbin) / step;
|
Chris@408
|
934 }
|
Chris@408
|
935
|
Chris@678
|
936 try {
|
Chris@929
|
937 int col;
|
Chris@678
|
938 FFTCacheReader *cache = getCacheReader(x, col);
|
Chris@678
|
939 if (!cache) return false;
|
Chris@408
|
940
|
Chris@678
|
941 if (!cache->haveSetColumnAt(col)) {
|
Chris@1081
|
942 if (getError() != "") return false;
|
Chris@678
|
943 Profiler profiler("FFTDataServer::getMagnitudesAt: filling");
|
Chris@678
|
944 fillColumn(x);
|
Chris@678
|
945 }
|
Chris@678
|
946
|
Chris@678
|
947 cache->getMagnitudesAt(col, values, minbin, count, step);
|
Chris@678
|
948
|
Chris@678
|
949 } catch (std::exception &e) {
|
Chris@678
|
950 m_error = e.what();
|
Chris@678
|
951 return false;
|
Chris@408
|
952 }
|
Chris@408
|
953
|
Chris@408
|
954 return true;
|
Chris@408
|
955 }
|
Chris@408
|
956
|
Chris@148
|
957 float
|
Chris@929
|
958 FFTDataServer::getNormalizedMagnitudeAt(int x, int y)
|
Chris@148
|
959 {
|
Chris@183
|
960 Profiler profiler("FFTDataServer::getNormalizedMagnitudeAt", false);
|
Chris@183
|
961
|
Chris@217
|
962 if (x >= m_width || y >= m_height) return 0;
|
Chris@217
|
963
|
Chris@678
|
964 float val = 0;
|
Chris@148
|
965
|
Chris@678
|
966 try {
|
Chris@678
|
967
|
Chris@929
|
968 int col;
|
Chris@678
|
969 FFTCacheReader *cache = getCacheReader(x, col);
|
Chris@678
|
970 if (!cache) return 0;
|
Chris@678
|
971
|
Chris@678
|
972 if (!cache->haveSetColumnAt(col)) {
|
Chris@1081
|
973 if (getError() != "") return false;
|
Chris@678
|
974 Profiler profiler("FFTDataServer::getNormalizedMagnitudeAt: filling");
|
Chris@678
|
975 fillColumn(x);
|
Chris@678
|
976 }
|
Chris@678
|
977 val = cache->getNormalizedMagnitudeAt(col, y);
|
Chris@678
|
978
|
Chris@678
|
979 } catch (std::exception &e) {
|
Chris@678
|
980 m_error = e.what();
|
Chris@148
|
981 }
|
Chris@678
|
982
|
Chris@678
|
983 return val;
|
Chris@148
|
984 }
|
Chris@148
|
985
|
Chris@408
|
986 bool
|
Chris@929
|
987 FFTDataServer::getNormalizedMagnitudesAt(int x, float *values, int minbin, int count, int step)
|
Chris@408
|
988 {
|
Chris@408
|
989 Profiler profiler("FFTDataServer::getNormalizedMagnitudesAt", false);
|
Chris@408
|
990
|
Chris@408
|
991 if (x >= m_width) return false;
|
Chris@408
|
992
|
Chris@408
|
993 if (minbin >= m_height) minbin = m_height - 1;
|
Chris@408
|
994 if (count == 0) count = (m_height - minbin) / step;
|
Chris@408
|
995 else if (minbin + count * step > m_height) {
|
Chris@408
|
996 count = (m_height - minbin) / step;
|
Chris@408
|
997 }
|
Chris@408
|
998
|
Chris@678
|
999 try {
|
Chris@408
|
1000
|
Chris@929
|
1001 int col;
|
Chris@678
|
1002 FFTCacheReader *cache = getCacheReader(x, col);
|
Chris@678
|
1003 if (!cache) return false;
|
Chris@408
|
1004
|
Chris@678
|
1005 if (!cache->haveSetColumnAt(col)) {
|
Chris@1081
|
1006 if (getError() != "") return false;
|
Chris@678
|
1007 Profiler profiler("FFTDataServer::getNormalizedMagnitudesAt: filling");
|
Chris@678
|
1008 fillColumn(x);
|
Chris@678
|
1009 }
|
Chris@678
|
1010
|
Chris@929
|
1011 for (int i = 0; i < count; ++i) {
|
Chris@678
|
1012 values[i] = cache->getNormalizedMagnitudeAt(col, i * step + minbin);
|
Chris@678
|
1013 }
|
Chris@678
|
1014
|
Chris@678
|
1015 } catch (std::exception &e) {
|
Chris@678
|
1016 m_error = e.what();
|
Chris@678
|
1017 return false;
|
Chris@408
|
1018 }
|
Chris@408
|
1019
|
Chris@408
|
1020 return true;
|
Chris@408
|
1021 }
|
Chris@408
|
1022
|
Chris@148
|
1023 float
|
Chris@929
|
1024 FFTDataServer::getMaximumMagnitudeAt(int x)
|
Chris@148
|
1025 {
|
Chris@183
|
1026 Profiler profiler("FFTDataServer::getMaximumMagnitudeAt", false);
|
Chris@183
|
1027
|
Chris@217
|
1028 if (x >= m_width) return 0;
|
Chris@217
|
1029
|
Chris@678
|
1030 float val = 0;
|
Chris@148
|
1031
|
Chris@678
|
1032 try {
|
Chris@678
|
1033
|
Chris@929
|
1034 int col;
|
Chris@678
|
1035 FFTCacheReader *cache = getCacheReader(x, col);
|
Chris@678
|
1036 if (!cache) return 0;
|
Chris@678
|
1037
|
Chris@678
|
1038 if (!cache->haveSetColumnAt(col)) {
|
Chris@1081
|
1039 if (getError() != "") return false;
|
Chris@678
|
1040 Profiler profiler("FFTDataServer::getMaximumMagnitudeAt: filling");
|
Chris@678
|
1041 fillColumn(x);
|
Chris@678
|
1042 }
|
Chris@678
|
1043 val = cache->getMaximumMagnitudeAt(col);
|
Chris@678
|
1044
|
Chris@678
|
1045 } catch (std::exception &e) {
|
Chris@678
|
1046 m_error = e.what();
|
Chris@148
|
1047 }
|
Chris@678
|
1048
|
Chris@678
|
1049 return val;
|
Chris@148
|
1050 }
|
Chris@148
|
1051
|
Chris@148
|
1052 float
|
Chris@929
|
1053 FFTDataServer::getPhaseAt(int x, int y)
|
Chris@148
|
1054 {
|
Chris@183
|
1055 Profiler profiler("FFTDataServer::getPhaseAt", false);
|
Chris@183
|
1056
|
Chris@217
|
1057 if (x >= m_width || y >= m_height) return 0;
|
Chris@217
|
1058
|
Chris@678
|
1059 float val = 0;
|
Chris@148
|
1060
|
Chris@678
|
1061 try {
|
Chris@678
|
1062
|
Chris@929
|
1063 int col;
|
Chris@678
|
1064 FFTCacheReader *cache = getCacheReader(x, col);
|
Chris@678
|
1065 if (!cache) return 0;
|
Chris@678
|
1066
|
Chris@678
|
1067 if (!cache->haveSetColumnAt(col)) {
|
Chris@1081
|
1068 if (getError() != "") return false;
|
Chris@678
|
1069 Profiler profiler("FFTDataServer::getPhaseAt: filling");
|
Chris@678
|
1070 fillColumn(x);
|
Chris@678
|
1071 }
|
Chris@678
|
1072 val = cache->getPhaseAt(col, y);
|
Chris@678
|
1073
|
Chris@678
|
1074 } catch (std::exception &e) {
|
Chris@678
|
1075 m_error = e.what();
|
Chris@148
|
1076 }
|
Chris@678
|
1077
|
Chris@678
|
1078 return val;
|
Chris@148
|
1079 }
|
Chris@148
|
1080
|
Chris@408
|
1081 bool
|
Chris@929
|
1082 FFTDataServer::getPhasesAt(int x, float *values, int minbin, int count, int step)
|
Chris@408
|
1083 {
|
Chris@408
|
1084 Profiler profiler("FFTDataServer::getPhasesAt", false);
|
Chris@408
|
1085
|
Chris@408
|
1086 if (x >= m_width) return false;
|
Chris@408
|
1087
|
Chris@408
|
1088 if (minbin >= m_height) minbin = m_height - 1;
|
Chris@408
|
1089 if (count == 0) count = (m_height - minbin) / step;
|
Chris@408
|
1090 else if (minbin + count * step > m_height) {
|
Chris@408
|
1091 count = (m_height - minbin) / step;
|
Chris@408
|
1092 }
|
Chris@408
|
1093
|
Chris@678
|
1094 try {
|
Chris@408
|
1095
|
Chris@929
|
1096 int col;
|
Chris@678
|
1097 FFTCacheReader *cache = getCacheReader(x, col);
|
Chris@678
|
1098 if (!cache) return false;
|
Chris@408
|
1099
|
Chris@678
|
1100 if (!cache->haveSetColumnAt(col)) {
|
Chris@1081
|
1101 if (getError() != "") return false;
|
Chris@678
|
1102 Profiler profiler("FFTDataServer::getPhasesAt: filling");
|
Chris@678
|
1103 fillColumn(x);
|
Chris@678
|
1104 }
|
Chris@678
|
1105
|
Chris@929
|
1106 for (int i = 0; i < count; ++i) {
|
Chris@678
|
1107 values[i] = cache->getPhaseAt(col, i * step + minbin);
|
Chris@678
|
1108 }
|
Chris@678
|
1109
|
Chris@678
|
1110 } catch (std::exception &e) {
|
Chris@678
|
1111 m_error = e.what();
|
Chris@678
|
1112 return false;
|
Chris@408
|
1113 }
|
Chris@408
|
1114
|
Chris@408
|
1115 return true;
|
Chris@408
|
1116 }
|
Chris@408
|
1117
|
Chris@148
|
1118 void
|
Chris@929
|
1119 FFTDataServer::getValuesAt(int x, int y, float &real, float &imaginary)
|
Chris@148
|
1120 {
|
Chris@183
|
1121 Profiler profiler("FFTDataServer::getValuesAt", false);
|
Chris@183
|
1122
|
Chris@216
|
1123 if (x >= m_width || y >= m_height) {
|
Chris@216
|
1124 real = 0;
|
Chris@216
|
1125 imaginary = 0;
|
Chris@216
|
1126 return;
|
Chris@216
|
1127 }
|
Chris@216
|
1128
|
Chris@678
|
1129 try {
|
Chris@216
|
1130
|
Chris@929
|
1131 int col;
|
Chris@678
|
1132 FFTCacheReader *cache = getCacheReader(x, col);
|
Chris@678
|
1133
|
Chris@678
|
1134 if (!cache) {
|
Chris@678
|
1135 real = 0;
|
Chris@678
|
1136 imaginary = 0;
|
Chris@678
|
1137 return;
|
Chris@678
|
1138 }
|
Chris@678
|
1139
|
Chris@678
|
1140 if (!cache->haveSetColumnAt(col)) {
|
Chris@1081
|
1141 if (getError() != "") {
|
Chris@1081
|
1142 real = 0;
|
Chris@1081
|
1143 imaginary = 0;
|
Chris@1081
|
1144 return;
|
Chris@1081
|
1145 }
|
Chris@678
|
1146 Profiler profiler("FFTDataServer::getValuesAt: filling");
|
Chris@678
|
1147 #ifdef DEBUG_FFT_SERVER
|
chris@828
|
1148 std::cerr << "FFTDataServer::getValuesAt(" << x << ", " << y << "): filling" << std::endl;
|
Chris@678
|
1149 #endif
|
Chris@678
|
1150 fillColumn(x);
|
Chris@678
|
1151 }
|
Chris@678
|
1152
|
Chris@678
|
1153 cache->getValuesAt(col, y, real, imaginary);
|
Chris@678
|
1154
|
Chris@678
|
1155 } catch (std::exception &e) {
|
Chris@678
|
1156 m_error = e.what();
|
Chris@216
|
1157 }
|
Chris@148
|
1158 }
|
Chris@148
|
1159
|
Chris@148
|
1160 bool
|
Chris@929
|
1161 FFTDataServer::getValuesAt(int x, float *reals, float *imaginaries, int minbin, int count, int step)
|
Chris@556
|
1162 {
|
Chris@556
|
1163 Profiler profiler("FFTDataServer::getValuesAt", false);
|
Chris@556
|
1164
|
Chris@556
|
1165 if (x >= m_width) return false;
|
Chris@556
|
1166
|
Chris@556
|
1167 if (minbin >= m_height) minbin = m_height - 1;
|
Chris@556
|
1168 if (count == 0) count = (m_height - minbin) / step;
|
Chris@556
|
1169 else if (minbin + count * step > m_height) {
|
Chris@556
|
1170 count = (m_height - minbin) / step;
|
Chris@556
|
1171 }
|
Chris@556
|
1172
|
Chris@678
|
1173 try {
|
Chris@556
|
1174
|
Chris@929
|
1175 int col;
|
Chris@678
|
1176 FFTCacheReader *cache = getCacheReader(x, col);
|
Chris@678
|
1177 if (!cache) return false;
|
Chris@556
|
1178
|
Chris@678
|
1179 if (!cache->haveSetColumnAt(col)) {
|
Chris@1081
|
1180 if (getError() != "") return false;
|
Chris@678
|
1181 Profiler profiler("FFTDataServer::getValuesAt: filling");
|
Chris@678
|
1182 fillColumn(x);
|
Chris@678
|
1183 }
|
Chris@678
|
1184
|
Chris@929
|
1185 for (int i = 0; i < count; ++i) {
|
Chris@678
|
1186 cache->getValuesAt(col, i * step + minbin, reals[i], imaginaries[i]);
|
Chris@678
|
1187 }
|
Chris@678
|
1188
|
Chris@678
|
1189 } catch (std::exception &e) {
|
Chris@678
|
1190 m_error = e.what();
|
Chris@678
|
1191 return false;
|
Chris@556
|
1192 }
|
Chris@556
|
1193
|
Chris@556
|
1194 return true;
|
Chris@556
|
1195 }
|
Chris@556
|
1196
|
Chris@556
|
1197 bool
|
Chris@929
|
1198 FFTDataServer::isColumnReady(int x)
|
Chris@148
|
1199 {
|
Chris@183
|
1200 Profiler profiler("FFTDataServer::isColumnReady", false);
|
Chris@183
|
1201
|
Chris@217
|
1202 if (x >= m_width) return true;
|
Chris@217
|
1203
|
Chris@148
|
1204 if (!haveCache(x)) {
|
Chris@537
|
1205 /*!!!
|
Chris@148
|
1206 if (m_lastUsedCache == -1) {
|
Chris@183
|
1207 if (m_suspended) {
|
chris@828
|
1208 std::cerr << "FFTDataServer::isColumnReady(" << x << "): no cache, calling resume" << std::endl;
|
Chris@183
|
1209 resume();
|
Chris@183
|
1210 }
|
Chris@148
|
1211 m_fillThread->start();
|
Chris@148
|
1212 }
|
Chris@537
|
1213 */
|
Chris@148
|
1214 return false;
|
Chris@148
|
1215 }
|
Chris@148
|
1216
|
Chris@678
|
1217 try {
|
Chris@148
|
1218
|
Chris@929
|
1219 int col;
|
Chris@678
|
1220 FFTCacheReader *cache = getCacheReader(x, col);
|
Chris@678
|
1221 if (!cache) return true;
|
Chris@678
|
1222
|
Chris@678
|
1223 return cache->haveSetColumnAt(col);
|
Chris@678
|
1224
|
Chris@678
|
1225 } catch (std::exception &e) {
|
Chris@678
|
1226 m_error = e.what();
|
Chris@678
|
1227 return false;
|
Chris@678
|
1228 }
|
Chris@148
|
1229 }
|
Chris@148
|
1230
|
Chris@148
|
1231 void
|
Chris@929
|
1232 FFTDataServer::fillColumn(int x)
|
Chris@148
|
1233 {
|
Chris@183
|
1234 Profiler profiler("FFTDataServer::fillColumn", false);
|
Chris@183
|
1235
|
Chris@272
|
1236 if (!m_model->isReady()) {
|
Chris@843
|
1237 cerr << "WARNING: FFTDataServer::fillColumn("
|
Chris@843
|
1238 << x << "): model not yet ready" << endl;
|
Chris@272
|
1239 return;
|
Chris@272
|
1240 }
|
Chris@549
|
1241 /*
|
Chris@217
|
1242 if (!m_fftInput) {
|
Chris@843
|
1243 cerr << "WARNING: FFTDataServer::fillColumn(" << x << "): "
|
Chris@217
|
1244 << "input has already been completed and discarded?"
|
Chris@843
|
1245 << endl;
|
Chris@217
|
1246 return;
|
Chris@217
|
1247 }
|
Chris@549
|
1248 */
|
Chris@217
|
1249 if (x >= m_width) {
|
Chris@843
|
1250 cerr << "WARNING: FFTDataServer::fillColumn(" << x << "): "
|
Chris@217
|
1251 << "x > width (" << x << " > " << m_width << ")"
|
Chris@843
|
1252 << endl;
|
Chris@217
|
1253 return;
|
Chris@217
|
1254 }
|
Chris@217
|
1255
|
Chris@929
|
1256 int col;
|
Chris@148
|
1257 #ifdef DEBUG_FFT_SERVER_FILL
|
Chris@843
|
1258 cout << "FFTDataServer::fillColumn(" << x << ")" << endl;
|
Chris@148
|
1259 #endif
|
Chris@537
|
1260 FFTCacheWriter *cache = getCacheWriter(x, col);
|
Chris@200
|
1261 if (!cache) return;
|
Chris@148
|
1262
|
Chris@408
|
1263 int winsize = m_windowSize;
|
Chris@408
|
1264 int fftsize = m_fftSize;
|
Chris@408
|
1265 int hs = fftsize/2;
|
Chris@408
|
1266
|
Chris@1038
|
1267 sv_frame_t pfx = 0;
|
Chris@408
|
1268 int off = (fftsize - winsize) / 2;
|
Chris@148
|
1269
|
Chris@1038
|
1270 sv_frame_t startFrame = m_windowIncrement * sv_frame_t(x);
|
Chris@1038
|
1271 sv_frame_t endFrame = startFrame + m_windowSize;
|
Chris@148
|
1272
|
Chris@1088
|
1273 // FFT windows are centred at the respective audio sample frame,
|
Chris@1088
|
1274 // so the first one is centred at 0
|
Chris@1088
|
1275 startFrame -= winsize / 2;
|
Chris@1088
|
1276 endFrame -= winsize / 2;
|
Chris@148
|
1277
|
Chris@549
|
1278 #ifdef DEBUG_FFT_SERVER_FILL
|
chris@828
|
1279 std::cerr << "FFTDataServer::fillColumn: requesting frames "
|
Chris@549
|
1280 << startFrame + pfx << " -> " << endFrame << " ( = "
|
Chris@549
|
1281 << endFrame - (startFrame + pfx) << ") at index "
|
Chris@549
|
1282 << off + pfx << " in buffer of size " << m_fftSize
|
Chris@549
|
1283 << " with window size " << m_windowSize
|
chris@828
|
1284 << " from channel " << m_channel << std::endl;
|
Chris@549
|
1285 #endif
|
Chris@549
|
1286
|
Chris@549
|
1287 QMutexLocker locker(&m_fftBuffersLock);
|
Chris@549
|
1288
|
Chris@550
|
1289 // We may have been called from a function that wanted to obtain a
|
Chris@550
|
1290 // column using an FFTCacheReader. Before calling us, it checked
|
Chris@550
|
1291 // whether the column was available already, and the reader
|
Chris@550
|
1292 // reported that it wasn't. Now we test again, with the mutex
|
Chris@550
|
1293 // held, to avoid a race condition in case another thread has
|
Chris@550
|
1294 // called fillColumn at the same time.
|
Chris@550
|
1295 if (cache->haveSetColumnAt(x & m_cacheWidthMask)) {
|
Chris@550
|
1296 return;
|
Chris@549
|
1297 }
|
Chris@549
|
1298
|
Chris@549
|
1299 if (!m_fftInput) {
|
Chris@843
|
1300 cerr << "WARNING: FFTDataServer::fillColumn(" << x << "): "
|
Chris@549
|
1301 << "input has already been completed and discarded?"
|
Chris@843
|
1302 << endl;
|
Chris@549
|
1303 return;
|
Chris@549
|
1304 }
|
Chris@549
|
1305
|
Chris@408
|
1306 for (int i = 0; i < off; ++i) {
|
Chris@408
|
1307 m_fftInput[i] = 0.0;
|
Chris@408
|
1308 }
|
Chris@148
|
1309
|
Chris@408
|
1310 for (int i = 0; i < off; ++i) {
|
Chris@408
|
1311 m_fftInput[fftsize - i - 1] = 0.0;
|
Chris@148
|
1312 }
|
Chris@148
|
1313
|
Chris@148
|
1314 if (startFrame < 0) {
|
Chris@408
|
1315 pfx = -startFrame;
|
Chris@408
|
1316 for (int i = 0; i < pfx; ++i) {
|
Chris@148
|
1317 m_fftInput[off + i] = 0.0;
|
Chris@148
|
1318 }
|
Chris@148
|
1319 }
|
Chris@148
|
1320
|
Chris@1038
|
1321 sv_frame_t count = 0;
|
Chris@300
|
1322 if (endFrame > startFrame + pfx) count = endFrame - (startFrame + pfx);
|
Chris@300
|
1323
|
Chris@1038
|
1324 sv_frame_t got = m_model->getData(m_channel, startFrame + pfx,
|
Chris@1038
|
1325 count, m_fftInput + off + pfx);
|
Chris@148
|
1326
|
Chris@408
|
1327 while (got + pfx < winsize) {
|
Chris@148
|
1328 m_fftInput[off + got + pfx] = 0.0;
|
Chris@148
|
1329 ++got;
|
Chris@148
|
1330 }
|
Chris@148
|
1331
|
Chris@148
|
1332 if (m_channel == -1) {
|
Chris@148
|
1333 int channels = m_model->getChannelCount();
|
Chris@148
|
1334 if (channels > 1) {
|
Chris@408
|
1335 for (int i = 0; i < winsize; ++i) {
|
Chris@1038
|
1336 m_fftInput[off + i] /= float(channels);
|
Chris@148
|
1337 }
|
Chris@148
|
1338 }
|
Chris@148
|
1339 }
|
Chris@148
|
1340
|
Chris@148
|
1341 m_windower.cut(m_fftInput + off);
|
Chris@148
|
1342
|
Chris@408
|
1343 for (int i = 0; i < hs; ++i) {
|
Chris@148
|
1344 fftsample temp = m_fftInput[i];
|
Chris@408
|
1345 m_fftInput[i] = m_fftInput[i + hs];
|
Chris@408
|
1346 m_fftInput[i + hs] = temp;
|
Chris@148
|
1347 }
|
Chris@148
|
1348
|
Chris@226
|
1349 fftf_execute(m_fftPlan);
|
Chris@148
|
1350
|
Chris@408
|
1351 float factor = 0.f;
|
Chris@148
|
1352
|
Chris@408
|
1353 if (cache->getStorageType() == FFTCache::Compact ||
|
Chris@408
|
1354 cache->getStorageType() == FFTCache::Polar) {
|
Chris@408
|
1355
|
Chris@408
|
1356 for (int i = 0; i <= hs; ++i) {
|
Chris@408
|
1357 fftsample real = m_fftOutput[i][0];
|
Chris@408
|
1358 fftsample imag = m_fftOutput[i][1];
|
Chris@408
|
1359 float mag = sqrtf(real * real + imag * imag);
|
Chris@408
|
1360 m_workbuffer[i] = mag;
|
Chris@408
|
1361 m_workbuffer[i + hs + 1] = atan2f(imag, real);
|
Chris@408
|
1362 if (mag > factor) factor = mag;
|
Chris@408
|
1363 }
|
Chris@408
|
1364
|
Chris@408
|
1365 } else {
|
Chris@408
|
1366
|
Chris@408
|
1367 for (int i = 0; i <= hs; ++i) {
|
Chris@408
|
1368 m_workbuffer[i] = m_fftOutput[i][0];
|
Chris@408
|
1369 m_workbuffer[i + hs + 1] = m_fftOutput[i][1];
|
Chris@408
|
1370 }
|
Chris@148
|
1371 }
|
Chris@148
|
1372
|
Chris@408
|
1373 Profiler subprof("FFTDataServer::fillColumn: set to cache");
|
Chris@408
|
1374
|
Chris@549
|
1375 if (cache->getStorageType() == FFTCache::Compact ||
|
Chris@549
|
1376 cache->getStorageType() == FFTCache::Polar) {
|
Chris@549
|
1377
|
Chris@549
|
1378 cache->setColumnAt(col,
|
Chris@549
|
1379 m_workbuffer,
|
Chris@549
|
1380 m_workbuffer + hs + 1,
|
Chris@549
|
1381 factor);
|
Chris@408
|
1382
|
Chris@549
|
1383 } else {
|
Chris@408
|
1384
|
Chris@549
|
1385 cache->setColumnAt(col,
|
Chris@549
|
1386 m_workbuffer,
|
Chris@549
|
1387 m_workbuffer + hs + 1);
|
Chris@408
|
1388 }
|
Chris@154
|
1389
|
Chris@183
|
1390 if (m_suspended) {
|
chris@828
|
1391 // std::cerr << "FFTDataServer::fillColumn(" << x << "): calling resume" << std::endl;
|
Chris@183
|
1392 // resume();
|
Chris@183
|
1393 }
|
Chris@148
|
1394 }
|
Chris@148
|
1395
|
Chris@537
|
1396 void
|
Chris@537
|
1397 FFTDataServer::fillComplete()
|
Chris@537
|
1398 {
|
Chris@537
|
1399 for (int i = 0; i < int(m_caches.size()); ++i) {
|
Chris@678
|
1400 if (!m_caches[i]) continue;
|
Chris@537
|
1401 if (m_caches[i]->memoryCache) {
|
Chris@537
|
1402 m_caches[i]->memoryCache->allColumnsWritten();
|
Chris@537
|
1403 }
|
Chris@537
|
1404 if (m_caches[i]->fileCacheWriter) {
|
Chris@537
|
1405 m_caches[i]->fileCacheWriter->allColumnsWritten();
|
Chris@537
|
1406 }
|
Chris@537
|
1407 }
|
Chris@537
|
1408 }
|
Chris@537
|
1409
|
Chris@678
|
1410 QString
|
Chris@678
|
1411 FFTDataServer::getError() const
|
Chris@678
|
1412 {
|
Chris@1081
|
1413 QString err;
|
Chris@1081
|
1414 if (m_error != "") {
|
Chris@1081
|
1415 err = m_error;
|
Chris@1084
|
1416 // cerr << "FFTDataServer::getError: err (server " << this << ") = " << err << endl;
|
Chris@1081
|
1417 } else {
|
Chris@1081
|
1418 MutexLocker locker(&m_fftBuffersLock, "FFTDataServer::getError");
|
Chris@1081
|
1419 if (m_fillThread) {
|
Chris@1081
|
1420 err = m_fillThread->getError();
|
Chris@1084
|
1421 // cerr << "FFTDataServer::getError: err (server " << this << ", from thread " << m_fillThread
|
Chris@1084
|
1422 // << ") = " << err << endl;
|
Chris@1081
|
1423 }
|
Chris@1081
|
1424 }
|
Chris@1081
|
1425 return err;
|
Chris@678
|
1426 }
|
Chris@678
|
1427
|
Chris@929
|
1428 int
|
Chris@148
|
1429 FFTDataServer::getFillCompletion() const
|
Chris@148
|
1430 {
|
Chris@148
|
1431 if (m_fillThread) return m_fillThread->getCompletion();
|
Chris@148
|
1432 else return 100;
|
Chris@148
|
1433 }
|
Chris@148
|
1434
|
Chris@1038
|
1435 sv_frame_t
|
Chris@148
|
1436 FFTDataServer::getFillExtent() const
|
Chris@148
|
1437 {
|
Chris@148
|
1438 if (m_fillThread) return m_fillThread->getExtent();
|
Chris@148
|
1439 else return m_model->getEndFrame();
|
Chris@148
|
1440 }
|
Chris@148
|
1441
|
Chris@148
|
1442 QString
|
Chris@148
|
1443 FFTDataServer::generateFileBasename() const
|
Chris@148
|
1444 {
|
Chris@148
|
1445 return generateFileBasename(m_model, m_channel, m_windower.getType(),
|
Chris@148
|
1446 m_windowSize, m_windowIncrement, m_fftSize,
|
Chris@148
|
1447 m_polar);
|
Chris@148
|
1448 }
|
Chris@148
|
1449
|
Chris@148
|
1450 QString
|
Chris@148
|
1451 FFTDataServer::generateFileBasename(const DenseTimeValueModel *model,
|
Chris@148
|
1452 int channel,
|
Chris@148
|
1453 WindowType windowType,
|
Chris@929
|
1454 int windowSize,
|
Chris@929
|
1455 int windowIncrement,
|
Chris@929
|
1456 int fftSize,
|
Chris@148
|
1457 bool polar)
|
Chris@148
|
1458 {
|
Chris@986
|
1459 return QString("%1-%2-%3-%4-%5-%6%7")
|
Chris@986
|
1460 .arg(XmlExportable::getObjectExportId(model))
|
Chris@986
|
1461 .arg(channel + 1)
|
Chris@986
|
1462 .arg((int)windowType)
|
Chris@986
|
1463 .arg(windowSize)
|
Chris@986
|
1464 .arg(windowIncrement)
|
Chris@986
|
1465 .arg(fftSize)
|
Chris@986
|
1466 .arg(polar ? "-p" : "-r");
|
Chris@148
|
1467 }
|
Chris@148
|
1468
|
Chris@148
|
1469 void
|
Chris@148
|
1470 FFTDataServer::FillThread::run()
|
Chris@148
|
1471 {
|
Chris@411
|
1472 #ifdef DEBUG_FFT_SERVER_FILL
|
chris@828
|
1473 std::cerr << "FFTDataServer::FillThread::run()" << std::endl;
|
Chris@411
|
1474 #endif
|
Chris@411
|
1475
|
Chris@148
|
1476 m_extent = 0;
|
Chris@148
|
1477 m_completion = 0;
|
Chris@148
|
1478
|
Chris@272
|
1479 while (!m_server.m_model->isReady() && !m_server.m_exiting) {
|
Chris@411
|
1480 #ifdef DEBUG_FFT_SERVER_FILL
|
chris@828
|
1481 std::cerr << "FFTDataServer::FillThread::run(): waiting for model " << m_server.m_model << " to be ready" << std::endl;
|
Chris@411
|
1482 #endif
|
Chris@272
|
1483 sleep(1);
|
Chris@272
|
1484 }
|
Chris@272
|
1485 if (m_server.m_exiting) return;
|
Chris@272
|
1486
|
Chris@1038
|
1487 sv_frame_t start = m_server.m_model->getStartFrame();
|
Chris@1038
|
1488 sv_frame_t end = m_server.m_model->getEndFrame();
|
Chris@1038
|
1489 sv_frame_t remainingEnd = end;
|
Chris@148
|
1490
|
Chris@148
|
1491 int counter = 0;
|
Chris@246
|
1492 int updateAt = 1;
|
Chris@1038
|
1493 int maxUpdateAt = int(end / m_server.m_windowIncrement) / 20;
|
Chris@246
|
1494 if (maxUpdateAt < 100) maxUpdateAt = 100;
|
Chris@148
|
1495
|
Chris@148
|
1496 if (m_fillFrom > start) {
|
Chris@148
|
1497
|
Chris@1038
|
1498 for (sv_frame_t f = m_fillFrom; f < end; f += m_server.m_windowIncrement) {
|
Chris@148
|
1499
|
Chris@678
|
1500 try {
|
Chris@678
|
1501 m_server.fillColumn(int((f - start) / m_server.m_windowIncrement));
|
Chris@678
|
1502 } catch (std::exception &e) {
|
Chris@1081
|
1503 MutexLocker locker(&m_server.m_fftBuffersLock,
|
Chris@1081
|
1504 "FFTDataServer::run::m_fftBuffersLock [err]");
|
Chris@1081
|
1505 m_threadError = e.what();
|
Chris@1081
|
1506 std::cerr << "FFTDataServer::FillThread::run: exception: " << m_threadError << " (thread = " << this << " from server " << &m_server << ")" << std::endl;
|
Chris@678
|
1507 m_server.fillComplete();
|
Chris@678
|
1508 m_completion = 100;
|
Chris@678
|
1509 m_extent = end;
|
Chris@678
|
1510 return;
|
Chris@678
|
1511 }
|
Chris@148
|
1512
|
Chris@148
|
1513 if (m_server.m_exiting) return;
|
Chris@148
|
1514
|
Chris@148
|
1515 while (m_server.m_suspended) {
|
Chris@148
|
1516 #ifdef DEBUG_FFT_SERVER
|
Chris@843
|
1517 cerr << "FFTDataServer(" << this << " [" << (void *)QThread::currentThreadId() << "]): suspended, waiting..." << endl;
|
Chris@148
|
1518 #endif
|
Chris@549
|
1519 MutexLocker locker(&m_server.m_fftBuffersLock,
|
Chris@549
|
1520 "FFTDataServer::run::m_fftBuffersLock [1]");
|
Chris@549
|
1521 if (m_server.m_suspended && !m_server.m_exiting) {
|
Chris@549
|
1522 m_server.m_condition.wait(&m_server.m_fftBuffersLock, 10000);
|
Chris@244
|
1523 }
|
Chris@159
|
1524 #ifdef DEBUG_FFT_SERVER
|
Chris@843
|
1525 cerr << "FFTDataServer(" << this << " [" << (void *)QThread::currentThreadId() << "]): waited" << endl;
|
Chris@159
|
1526 #endif
|
Chris@148
|
1527 if (m_server.m_exiting) return;
|
Chris@148
|
1528 }
|
Chris@148
|
1529
|
Chris@148
|
1530 if (++counter == updateAt) {
|
Chris@148
|
1531 m_extent = f;
|
Chris@929
|
1532 m_completion = int(100 * fabsf(float(f - m_fillFrom) /
|
Chris@148
|
1533 float(end - start)));
|
Chris@148
|
1534 counter = 0;
|
Chris@246
|
1535 if (updateAt < maxUpdateAt) {
|
Chris@246
|
1536 updateAt *= 2;
|
Chris@246
|
1537 if (updateAt > maxUpdateAt) updateAt = maxUpdateAt;
|
Chris@246
|
1538 }
|
Chris@148
|
1539 }
|
Chris@148
|
1540 }
|
Chris@148
|
1541
|
Chris@148
|
1542 remainingEnd = m_fillFrom;
|
Chris@148
|
1543 if (remainingEnd > start) --remainingEnd;
|
Chris@148
|
1544 else remainingEnd = start;
|
Chris@148
|
1545 }
|
Chris@148
|
1546
|
Chris@929
|
1547 int baseCompletion = m_completion;
|
Chris@148
|
1548
|
Chris@1038
|
1549 for (sv_frame_t f = start; f < remainingEnd; f += m_server.m_windowIncrement) {
|
Chris@148
|
1550
|
Chris@678
|
1551 try {
|
Chris@678
|
1552 m_server.fillColumn(int((f - start) / m_server.m_windowIncrement));
|
Chris@678
|
1553 } catch (std::exception &e) {
|
Chris@1081
|
1554 MutexLocker locker(&m_server.m_fftBuffersLock,
|
Chris@1081
|
1555 "FFTDataServer::run::m_fftBuffersLock [err]");
|
Chris@1081
|
1556 m_threadError = e.what();
|
Chris@1081
|
1557 std::cerr << "FFTDataServer::FillThread::run: exception: " << m_threadError << " (thread = " << this << " from server " << &m_server << ")" << std::endl;
|
Chris@678
|
1558 m_server.fillComplete();
|
Chris@678
|
1559 m_completion = 100;
|
Chris@678
|
1560 m_extent = end;
|
Chris@678
|
1561 return;
|
Chris@678
|
1562 }
|
Chris@148
|
1563
|
Chris@148
|
1564 if (m_server.m_exiting) return;
|
Chris@148
|
1565
|
Chris@148
|
1566 while (m_server.m_suspended) {
|
Chris@148
|
1567 #ifdef DEBUG_FFT_SERVER
|
Chris@843
|
1568 cerr << "FFTDataServer(" << this << " [" << (void *)QThread::currentThreadId() << "]): suspended, waiting..." << endl;
|
Chris@148
|
1569 #endif
|
Chris@244
|
1570 {
|
Chris@549
|
1571 MutexLocker locker(&m_server.m_fftBuffersLock,
|
Chris@549
|
1572 "FFTDataServer::run::m_fftBuffersLock [2]");
|
Chris@549
|
1573 if (m_server.m_suspended && !m_server.m_exiting) {
|
Chris@549
|
1574 m_server.m_condition.wait(&m_server.m_fftBuffersLock, 10000);
|
Chris@549
|
1575 }
|
Chris@244
|
1576 }
|
Chris@148
|
1577 if (m_server.m_exiting) return;
|
Chris@148
|
1578 }
|
Chris@148
|
1579
|
Chris@148
|
1580 if (++counter == updateAt) {
|
Chris@148
|
1581 m_extent = f;
|
Chris@148
|
1582 m_completion = baseCompletion +
|
Chris@929
|
1583 int(100 * fabsf(float(f - start) /
|
Chris@148
|
1584 float(end - start)));
|
Chris@148
|
1585 counter = 0;
|
Chris@246
|
1586 if (updateAt < maxUpdateAt) {
|
Chris@246
|
1587 updateAt *= 2;
|
Chris@246
|
1588 if (updateAt > maxUpdateAt) updateAt = maxUpdateAt;
|
Chris@246
|
1589 }
|
Chris@148
|
1590 }
|
Chris@148
|
1591 }
|
Chris@148
|
1592
|
Chris@537
|
1593 m_server.fillComplete();
|
Chris@148
|
1594 m_completion = 100;
|
Chris@148
|
1595 m_extent = end;
|
Chris@537
|
1596
|
Chris@537
|
1597 #ifdef DEBUG_FFT_SERVER
|
chris@828
|
1598 std::cerr << "FFTDataServer::FillThread::run exiting" << std::endl;
|
Chris@537
|
1599 #endif
|
Chris@148
|
1600 }
|
Chris@148
|
1601
|