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