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