comparison data/model/ReadOnlyWaveFileModel.cpp @ 1206:659372323b45 tony-2.0-integration

Merge latest SV 3.0 branch code
author Chris Cannam
date Fri, 19 Aug 2016 15:58:57 +0100
parents d73b4bac2dc4
children 6b847a59d908
comparison
equal deleted inserted replaced
1136:e94719f941ba 1206:659372323b45
191 { 191 {
192 // Read directly from the file. This is used for e.g. audio 192 // Read directly from the file. This is used for e.g. audio
193 // playback or input to transforms. 193 // playback or input to transforms.
194 194
195 #ifdef DEBUG_WAVE_FILE_MODEL 195 #ifdef DEBUG_WAVE_FILE_MODEL
196 cout << "ReadOnlyWaveFileModel::getData[" << this << "]: " << channel << ", " << start << ", " << count << ", " << buffer << endl; 196 cout << "ReadOnlyWaveFileModel::getData[" << this << "]: " << channel << ", " << start << ", " << count << endl;
197 #endif 197 #endif
198 198
199 int channels = getChannelCount(); 199 int channels = getChannelCount();
200 200
201 if (channel >= channels) { 201 if (channel >= channels) {
250 { 250 {
251 // Read directly from the file. This is used for e.g. audio 251 // Read directly from the file. This is used for e.g. audio
252 // playback or input to transforms. 252 // playback or input to transforms.
253 253
254 #ifdef DEBUG_WAVE_FILE_MODEL 254 #ifdef DEBUG_WAVE_FILE_MODEL
255 cout << "ReadOnlyWaveFileModel::getData[" << this << "]: " << fromchannel << "," << tochannel << ", " << start << ", " << count << ", " << buffer << endl; 255 cout << "ReadOnlyWaveFileModel::getData[" << this << "]: " << fromchannel << "," << tochannel << ", " << start << ", " << count << endl;
256 #endif 256 #endif
257 257
258 int channels = getChannelCount(); 258 int channels = getChannelCount();
259 259
260 if (fromchannel > tochannel) { 260 if (fromchannel > tochannel) {
320 } 320 }
321 } 321 }
322 322
323 void 323 void
324 ReadOnlyWaveFileModel::getSummaries(int channel, sv_frame_t start, sv_frame_t count, 324 ReadOnlyWaveFileModel::getSummaries(int channel, sv_frame_t start, sv_frame_t count,
325 RangeBlock &ranges, int &blockSize) const 325 RangeBlock &ranges, int &blockSize) const
326 { 326 {
327 ranges.clear(); 327 ranges.clear();
328 if (!isOK()) return; 328 if (!isOK()) return;
329 ranges.reserve((count / blockSize) + 1); 329 ranges.reserve((count / blockSize) + 1);
330 330
401 const RangeBlock &cache = m_cache[cacheType]; 401 const RangeBlock &cache = m_cache[cacheType];
402 402
403 blockSize = roundedBlockSize; 403 blockSize = roundedBlockSize;
404 404
405 sv_frame_t cacheBlock, div; 405 sv_frame_t cacheBlock, div;
406 406
407 if (cacheType == 0) { 407 cacheBlock = (sv_frame_t(1) << m_zoomConstraint.getMinCachePower());
408 cacheBlock = (1 << m_zoomConstraint.getMinCachePower()); 408 if (cacheType == 1) {
409 div = (1 << power) / cacheBlock; 409 cacheBlock = sv_frame_t(double(cacheBlock) * sqrt(2.) + 0.01);
410 } else {
411 cacheBlock = sv_frame_t((1 << m_zoomConstraint.getMinCachePower()) * sqrt(2.) + 0.01);
412 div = sv_frame_t(((1 << power) * sqrt(2.) + 0.01) / double(cacheBlock));
413 } 410 }
411 div = blockSize / cacheBlock;
414 412
415 sv_frame_t startIndex = start / cacheBlock; 413 sv_frame_t startIndex = start / cacheBlock;
416 sv_frame_t endIndex = (start + count) / cacheBlock; 414 sv_frame_t endIndex = (start + count) / cacheBlock;
417 415
418 float max = 0.0, min = 0.0, total = 0.0; 416 float max = 0.0, min = 0.0, total = 0.0;
423 #endif 421 #endif
424 422
425 for (i = 0; i <= endIndex - startIndex; ) { 423 for (i = 0; i <= endIndex - startIndex; ) {
426 424
427 sv_frame_t index = (i + startIndex) * channels + channel; 425 sv_frame_t index = (i + startIndex) * channels + channel;
428 if (index >= (sv_frame_t)cache.size()) break; 426 if (!in_range_for(cache, index)) break;
429 427
430 const Range &range = cache[index]; 428 const Range &range = cache[index];
431 if (range.max() > max || got == 0) max = range.max(); 429 if (range.max() > max || got == 0) max = range.max();
432 if (range.min() < min || got == 0) min = range.min(); 430 if (range.min() < min || got == 0) min = range.min();
433 total += range.absmean(); 431 total += range.absmean();
446 ranges.push_back(Range(min, max, total / float(got))); 444 ranges.push_back(Range(min, max, total / float(got)));
447 } 445 }
448 } 446 }
449 447
450 #ifdef DEBUG_WAVE_FILE_MODEL 448 #ifdef DEBUG_WAVE_FILE_MODEL
451 SVDEBUG << "returning " << ranges.size() << " ranges" << endl; 449 cerr << "returning " << ranges.size() << " ranges" << endl;
452 #endif 450 #endif
453 return; 451 return;
454 } 452 }
455 453
456 ReadOnlyWaveFileModel::Range 454 ReadOnlyWaveFileModel::Range
571 cacheBlockSize[0] = (1 << m_model.m_zoomConstraint.getMinCachePower()); 569 cacheBlockSize[0] = (1 << m_model.m_zoomConstraint.getMinCachePower());
572 cacheBlockSize[1] = (int((1 << m_model.m_zoomConstraint.getMinCachePower()) * 570 cacheBlockSize[1] = (int((1 << m_model.m_zoomConstraint.getMinCachePower()) *
573 sqrt(2.) + 0.01)); 571 sqrt(2.) + 0.01));
574 572
575 sv_frame_t frame = 0; 573 sv_frame_t frame = 0;
576 const sv_frame_t readBlockSize = 16384; 574 const sv_frame_t readBlockSize = 32768;
577 vector<float> block; 575 vector<float> block;
578 576
579 if (!m_model.isOK()) return; 577 if (!m_model.isOK()) return;
580 578
581 int channels = m_model.getChannelCount(); 579 int channels = m_model.getChannelCount();
582 bool updating = m_model.m_reader->isUpdating(); 580 bool updating = m_model.m_reader->isUpdating();
583 581
584 if (updating) { 582 if (updating) {
585 while (channels == 0 && !m_model.m_exiting) { 583 while (channels == 0 && !m_model.m_exiting) {
586 // SVDEBUG << "ReadOnlyWaveFileModel::fill: Waiting for channels..." << endl; 584 #ifdef DEBUG_WAVE_FILE_MODEL
585 cerr << "ReadOnlyWaveFileModel::fill: Waiting for channels..." << endl;
586 #endif
587 sleep(1); 587 sleep(1);
588 channels = m_model.getChannelCount(); 588 channels = m_model.getChannelCount();
589 } 589 }
590 } 590 }
591 591
602 while (first || updating) { 602 while (first || updating) {
603 603
604 updating = m_model.m_reader->isUpdating(); 604 updating = m_model.m_reader->isUpdating();
605 m_frameCount = m_model.getFrameCount(); 605 m_frameCount = m_model.getFrameCount();
606 606
607 // SVDEBUG << "ReadOnlyWaveFileModel::fill: frame = " << frame << ", count = " << m_frameCount << endl; 607 m_model.m_mutex.lock();
608 608
609 while (frame < m_frameCount) { 609 while (frame < m_frameCount) {
610 610
611 // SVDEBUG << "ReadOnlyWaveFileModel::fill inner loop: frame = " << frame << ", count = " << m_frameCount << ", blocksize " << readBlockSize << endl; 611 m_model.m_mutex.unlock();
612
613 #ifdef DEBUG_WAVE_FILE_MODEL
614 cerr << "ReadOnlyWaveFileModel::fill inner loop: frame = " << frame << ", count = " << m_frameCount << ", blocksize " << readBlockSize << endl;
615 #endif
612 616
613 if (updating && (frame + readBlockSize > m_frameCount)) break; 617 if (updating && (frame + readBlockSize > m_frameCount)) break;
614 618
615 block = m_model.m_reader->getInterleavedFrames(frame, readBlockSize); 619 block = m_model.m_reader->getInterleavedFrames(frame, readBlockSize);
616 620
617 // cerr << "block is " << block.size() << endl; 621 sv_frame_t gotBlockSize = block.size() / channels;
618 622
619 for (sv_frame_t i = 0; i < readBlockSize; ++i) { 623 m_model.m_mutex.lock();
624
625 for (sv_frame_t i = 0; i < gotBlockSize; ++i) {
620 626
621 if (channels * i + channels > (int)block.size()) break;
622
623 for (int ch = 0; ch < channels; ++ch) { 627 for (int ch = 0; ch < channels; ++ch) {
624 628
625 sv_frame_t index = channels * i + ch; 629 sv_frame_t index = channels * i + ch;
626 float sample = block[index]; 630 float sample = block[index];
627 631
628 for (int cacheType = 0; cacheType < 2; ++cacheType) { // cache type 632 for (int cacheType = 0; cacheType < 2; ++cacheType) {
629
630 sv_frame_t rangeIndex = ch * 2 + cacheType; 633 sv_frame_t rangeIndex = ch * 2 + cacheType;
631 range[rangeIndex].sample(sample); 634 range[rangeIndex].sample(sample);
632 means[rangeIndex] += fabsf(sample); 635 means[rangeIndex] += fabsf(sample);
633 } 636 }
634 } 637 }
635
636 //!!! this looks like a ludicrous way to do synchronisation
637 QMutexLocker locker(&m_model.m_mutex);
638 638
639 for (int cacheType = 0; cacheType < 2; ++cacheType) { 639 for (int cacheType = 0; cacheType < 2; ++cacheType) {
640 640
641 if (++count[cacheType] == cacheBlockSize[cacheType]) { 641 if (++count[cacheType] == cacheBlockSize[cacheType]) {
642 642
653 } 653 }
654 } 654 }
655 655
656 ++frame; 656 ++frame;
657 } 657 }
658
659 if (m_model.m_exiting) break;
660 m_fillExtent = frame;
661 }
662
663 m_model.m_mutex.unlock();
658 664
659 if (m_model.m_exiting) break;
660
661 m_fillExtent = frame;
662 }
663
664 // cerr << "ReadOnlyWaveFileModel: inner loop ended" << endl;
665
666 first = false; 665 first = false;
667 if (m_model.m_exiting) break; 666 if (m_model.m_exiting) break;
668 if (updating) { 667 if (updating) {
669 // cerr << "sleeping..." << endl;
670 sleep(1); 668 sleep(1);
671 } 669 }
672 } 670 }
673 671
674 if (!m_model.m_exiting) { 672 if (!m_model.m_exiting) {