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