comparison data/model/WaveFileModel.cpp @ 176:570794f6f6a7

* Some fixes to updating of writable wave file models
author Chris Cannam
date Tue, 03 Oct 2006 15:01:50 +0000
parents b0f4555b625e
children aff66ec5aea4
comparison
equal deleted inserted replaced
175:b0f4555b625e 176:570794f6f6a7
58 { 58 {
59 m_reader = reader; 59 m_reader = reader;
60 connect(m_reader, SIGNAL(frameCountChanged()), 60 connect(m_reader, SIGNAL(frameCountChanged()),
61 this, SLOT(frameCountChanged())); 61 this, SLOT(frameCountChanged()));
62 setObjectName(QFileInfo(path).fileName()); 62 setObjectName(QFileInfo(path).fileName());
63 if (isOK()) fillCache(); 63 if (isOK()) {
64 std::cerr << "OK; filling cache" << std::endl;
65 fillCache();
66 }
64 } 67 }
65 68
66 WaveFileModel::~WaveFileModel() 69 WaveFileModel::~WaveFileModel()
67 { 70 {
68 m_exiting = true; 71 m_exiting = true;
429 432
430 void 433 void
431 WaveFileModel::RangeCacheFillThread::frameCountChanged() 434 WaveFileModel::RangeCacheFillThread::frameCountChanged()
432 { 435 {
433 m_frameCount = m_model.getFrameCount(); 436 m_frameCount = m_model.getFrameCount();
437 std::cerr << "WaveFileModel::RangeCacheFillThread::frameCountChanged: now "
438 << m_frameCount << std::endl;
434 } 439 }
435 440
436 void 441 void
437 WaveFileModel::RangeCacheFillThread::run() 442 WaveFileModel::RangeCacheFillThread::run()
438 { 443 {
450 size_t channels = m_model.getChannelCount(); 455 size_t channels = m_model.getChannelCount();
451 456
452 Range *range = new Range[2 * channels]; 457 Range *range = new Range[2 * channels];
453 size_t count[2]; 458 size_t count[2];
454 count[0] = count[1] = 0; 459 count[0] = count[1] = 0;
455 460
456 while (frame < m_frameCount) { 461 bool first = true;
462 bool updating = m_model.m_reader->isUpdating();
463
464 while (first || updating) {
465
466 updating = m_model.m_reader->isUpdating();
457 467
458 std::cerr << "WaveFileModel::fill: frame = " << frame << ", count = " << m_frameCount << std::endl; 468 std::cerr << "WaveFileModel::fill: frame = " << frame << ", count = " << m_frameCount << std::endl;
459 469
460 m_model.m_reader->getInterleavedFrames(frame, readBlockSize, block); 470 while (frame < m_frameCount) {
461 471
462 for (size_t i = 0; i < readBlockSize; ++i) { 472 if (updating && (frame + readBlockSize > m_frameCount)) break;
473
474 m_model.m_reader->getInterleavedFrames(frame, readBlockSize, block);
475
476 for (size_t i = 0; i < readBlockSize; ++i) {
463 477
464 for (size_t ch = 0; ch < size_t(channels); ++ch) { 478 for (size_t ch = 0; ch < size_t(channels); ++ch) {
465 479
466 size_t index = channels * i + ch; 480 size_t index = channels * i + ch;
467 if (index >= block.size()) continue; 481 if (index >= block.size()) continue;
468 float sample = block[index]; 482 float sample = block[index];
483
484 for (size_t ct = 0; ct < 2; ++ct) {
485
486 size_t rangeIndex = ch * 2 + ct;
487
488 if (sample > range[rangeIndex].max || count[ct] == 0) {
489 range[rangeIndex].max = sample;
490 }
491 if (sample < range[rangeIndex].min || count[ct] == 0) {
492 range[rangeIndex].min = sample;
493 }
494 range[rangeIndex].absmean += fabsf(sample);
495 }
496 }
469 497
498 QMutexLocker locker(&m_model.m_mutex);
470 for (size_t ct = 0; ct < 2; ++ct) { 499 for (size_t ct = 0; ct < 2; ++ct) {
500 if (++count[ct] == cacheBlockSize[ct]) {
501 for (size_t ch = 0; ch < size_t(channels); ++ch) {
502 size_t rangeIndex = ch * 2 + ct;
503 range[rangeIndex].absmean /= count[ct];
504 m_model.m_cache[ct].push_back(range[rangeIndex]);
505 range[rangeIndex] = Range();
506 }
507 count[ct] = 0;
508 }
509 }
471 510
472 size_t rangeIndex = ch * 2 + ct; 511 ++frame;
473
474 if (sample > range[rangeIndex].max || count[ct] == 0) {
475 range[rangeIndex].max = sample;
476 }
477 if (sample < range[rangeIndex].min || count[ct] == 0) {
478 range[rangeIndex].min = sample;
479 }
480 range[rangeIndex].absmean += fabsf(sample);
481 }
482 }
483
484 QMutexLocker locker(&m_model.m_mutex);
485 for (size_t ct = 0; ct < 2; ++ct) {
486 if (++count[ct] == cacheBlockSize[ct]) {
487 for (size_t ch = 0; ch < size_t(channels); ++ch) {
488 size_t rangeIndex = ch * 2 + ct;
489 range[rangeIndex].absmean /= count[ct];
490 m_model.m_cache[ct].push_back(range[rangeIndex]);
491 range[rangeIndex] = Range();
492 }
493 count[ct] = 0;
494 }
495 } 512 }
496 513
497 ++frame; 514 if (m_model.m_exiting) break;
515
516 m_fillExtent = frame;
498 } 517 }
499 518
500 if (m_model.m_exiting) break; 519 first = false;
501 520 if (updating) sleep(1);
502 m_fillExtent = frame;
503 } 521 }
504 522
505 QMutexLocker locker(&m_model.m_mutex); 523 QMutexLocker locker(&m_model.m_mutex);
506 for (size_t ct = 0; ct < 2; ++ct) { 524 for (size_t ct = 0; ct < 2; ++ct) {
507 if (count[ct] > 0) { 525 if (count[ct] > 0) {