annotate data/fft/FFTDataServer.cpp @ 171:ebf55cf0d34d

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