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