comparison data/model/WaveFileModel.cpp @ 232:03a24547cf3c

* Fix crash in short spectrogram paint * Fix incorrect apparent end point for waveforms
author Chris Cannam
date Wed, 14 Feb 2007 17:52:06 +0000
parents 185454896a76
children 3e6fee4e4257
comparison
equal deleted inserted replaced
231:0031495aba07 232:03a24547cf3c
298 size_t endIndex = end / cacheBlock; 298 size_t endIndex = end / cacheBlock;
299 299
300 float max = 0.0, min = 0.0, total = 0.0; 300 float max = 0.0, min = 0.0, total = 0.0;
301 size_t i = 0, count = 0; 301 size_t i = 0, count = 0;
302 302
303 //cerr << "blockSize is " << blockSize << ", cacheBlock " << cacheBlock << ", start " << start << ", end " << end << ", power is " << power << ", div is " << div << ", startIndex " << startIndex << ", endIndex " << endIndex << endl; 303 cerr << "blockSize is " << blockSize << ", cacheBlock " << cacheBlock << ", start " << start << ", end " << end << " (frame count " << getFrameCount() << "), power is " << power << ", div is " << div << ", startIndex " << startIndex << ", endIndex " << endIndex << endl;
304 304
305 for (i = 0; i < endIndex - startIndex; ) { 305 for (i = 0; i < endIndex - startIndex; ) {
306 306
307 size_t index = (i + startIndex) * channels + channel; 307 size_t index = (i + startIndex) * channels + channel;
308 if (index >= cache.size()) break; 308 if (index >= cache.size()) break;
325 if (count > 0) { 325 if (count > 0) {
326 ranges.push_back(Range(min, max, total / count)); 326 ranges.push_back(Range(min, max, total / count));
327 } 327 }
328 } 328 }
329 329
330 //cerr << "returning " << ranges.size() << " ranges" << endl; 330 cerr << "returning " << ranges.size() << " ranges" << endl;
331 return; 331 return;
332 } 332 }
333 333
334 WaveFileModel::Range 334 WaveFileModel::Range
335 WaveFileModel::getRange(size_t channel, size_t start, size_t end) const 335 WaveFileModel::getRange(size_t channel, size_t start, size_t end) const
472 472
473 m_model.m_reader->getInterleavedFrames(frame, readBlockSize, block); 473 m_model.m_reader->getInterleavedFrames(frame, readBlockSize, block);
474 474
475 for (size_t i = 0; i < readBlockSize; ++i) { 475 for (size_t i = 0; i < readBlockSize; ++i) {
476 476
477 if (channels * i + channels > block.size()) break;
478
477 for (size_t ch = 0; ch < size_t(channels); ++ch) { 479 for (size_t ch = 0; ch < size_t(channels); ++ch) {
478 480
479 size_t index = channels * i + ch; 481 size_t index = channels * i + ch;
480 if (index >= block.size()) continue;
481 float sample = block[index]; 482 float sample = block[index];
482 483
483 for (size_t ct = 0; ct < 2; ++ct) { 484 for (size_t ct = 0; ct < 2; ++ct) { // cache type
484 485
485 size_t rangeIndex = ch * 2 + ct; 486 size_t rangeIndex = ch * 2 + ct;
486 487
487 if (sample > range[rangeIndex].max || count[ct] == 0) { 488 if (sample > range[rangeIndex].max || count[ct] == 0) {
488 range[rangeIndex].max = sample; 489 range[rangeIndex].max = sample;
493 range[rangeIndex].absmean += fabsf(sample); 494 range[rangeIndex].absmean += fabsf(sample);
494 } 495 }
495 } 496 }
496 497
497 QMutexLocker locker(&m_model.m_mutex); 498 QMutexLocker locker(&m_model.m_mutex);
499
498 for (size_t ct = 0; ct < 2; ++ct) { 500 for (size_t ct = 0; ct < 2; ++ct) {
501
499 if (++count[ct] == cacheBlockSize[ct]) { 502 if (++count[ct] == cacheBlockSize[ct]) {
503
500 for (size_t ch = 0; ch < size_t(channels); ++ch) { 504 for (size_t ch = 0; ch < size_t(channels); ++ch) {
501 size_t rangeIndex = ch * 2 + ct; 505 size_t rangeIndex = ch * 2 + ct;
502 range[rangeIndex].absmean /= count[ct]; 506 range[rangeIndex].absmean /= count[ct];
503 m_model.m_cache[ct].push_back(range[rangeIndex]); 507 m_model.m_cache[ct].push_back(range[rangeIndex]);
504 range[rangeIndex] = Range(); 508 range[rangeIndex] = Range();
505 } 509 }
510
506 count[ct] = 0; 511 count[ct] = 0;
507 } 512 }
508 } 513 }
509 514
510 ++frame; 515 ++frame;
523 } 528 }
524 529
525 if (!m_model.m_exiting) { 530 if (!m_model.m_exiting) {
526 531
527 QMutexLocker locker(&m_model.m_mutex); 532 QMutexLocker locker(&m_model.m_mutex);
533
528 for (size_t ct = 0; ct < 2; ++ct) { 534 for (size_t ct = 0; ct < 2; ++ct) {
535
529 if (count[ct] > 0) { 536 if (count[ct] > 0) {
537
530 for (size_t ch = 0; ch < size_t(channels); ++ch) { 538 for (size_t ch = 0; ch < size_t(channels); ++ch) {
531 size_t rangeIndex = ch * 2 + ct; 539 size_t rangeIndex = ch * 2 + ct;
532 range[rangeIndex].absmean /= count[ct]; 540 range[rangeIndex].absmean /= count[ct];
533 m_model.m_cache[ct].push_back(range[rangeIndex]); 541 m_model.m_cache[ct].push_back(range[rangeIndex]);
534 range[rangeIndex] = Range(); 542 range[rangeIndex] = Range();
535 } 543 }
544
536 count[ct] = 0; 545 count[ct] = 0;
537 } 546 }
538 547
539 const Range &rr = *m_model.m_cache[ct].begin(); 548 const Range &rr = *m_model.m_cache[ct].begin();
540 MUNLOCK(&rr, m_model.m_cache[ct].capacity() * sizeof(Range)); 549 MUNLOCK(&rr, m_model.m_cache[ct].capacity() * sizeof(Range));