comparison layer/SpectrumLayer.cpp @ 1392:900bed394f5a spectrogramparam

Highlight the current peak line in the spectrum (when shown); also a fix to the crosshair labelling
author Chris Cannam
date Wed, 14 Nov 2018 11:01:18 +0000
parents c39f2d439d59
children 4a36f6130056
comparison
equal deleted inserted replaced
1391:c39f2d439d59 1392:900bed394f5a
527 paint.drawLine(xorigin, cursorPos.y(), v->getPaintWidth(), cursorPos.y()); 527 paint.drawLine(xorigin, cursorPos.y(), v->getPaintWidth(), cursorPos.y());
528 paint.drawLine(cursorPos.x(), cursorPos.y(), cursorPos.x(), v->getPaintHeight()); 528 paint.drawLine(cursorPos.x(), cursorPos.y(), cursorPos.x(), v->getPaintHeight());
529 529
530 double fundamental = getFrequencyForX(v, cursorPos.x()); 530 double fundamental = getFrequencyForX(v, cursorPos.x());
531 531
532 int hoffset = 2; 532 int hoffset = getHorizontalScaleHeight(v, paint) +
533 if (m_binScale == LogBins) hoffset = 13; 533 2 * paint.fontMetrics().height();
534 534
535 PaintAssistant::drawVisibleText(v, paint, 535 PaintAssistant::drawVisibleText(v, paint,
536 cursorPos.x() + 2, 536 cursorPos.x() + 2,
537 v->getPaintHeight() - 2 - hoffset, 537 v->getPaintHeight() - 2 - hoffset,
538 QString("%1 Hz").arg(fundamental), 538 tr("%1 Hz").arg(fundamental),
539 PaintAssistant::OutlinedText); 539 PaintAssistant::OutlinedText);
540 540
541 if (Pitch::isFrequencyInMidiRange(fundamental)) { 541 if (Pitch::isFrequencyInMidiRange(fundamental)) {
542 QString pitchLabel = Pitch::getPitchLabelForFrequency(fundamental); 542 QString pitchLabel = Pitch::getPitchLabelForFrequency(fundamental);
543 PaintAssistant::drawVisibleText(v, paint, 543 PaintAssistant::drawVisibleText(v, paint,
547 pitchLabel, 547 pitchLabel,
548 PaintAssistant::OutlinedText); 548 PaintAssistant::OutlinedText);
549 } 549 }
550 550
551 double value = getValueForY(v, cursorPos.y()); 551 double value = getValueForY(v, cursorPos.y());
552 double thresh = m_threshold;
553 double db = thresh;
554 if (value > 0.0) db = 10.0 * log10(value);
555 if (db < thresh) db = thresh;
556 552
557 PaintAssistant::drawVisibleText(v, paint, 553 PaintAssistant::drawVisibleText(v, paint,
558 xorigin + 2, 554 xorigin + 2,
559 cursorPos.y() - 2, 555 cursorPos.y() - 2,
560 QString("%1 V").arg(value), 556 QString("%1 V").arg(value),
561 PaintAssistant::OutlinedText); 557 PaintAssistant::OutlinedText);
562 558
563 PaintAssistant::drawVisibleText(v, paint, 559 if (value > m_threshold) {
564 xorigin + 2, 560 double db = 10.0 * log10(value);
565 cursorPos.y() + 2 + paint.fontMetrics().ascent(), 561 PaintAssistant::drawVisibleText(v, paint,
566 QString("%1 dBV").arg(db), 562 xorigin + 2,
567 PaintAssistant::OutlinedText); 563 cursorPos.y() + 2 +
564 paint.fontMetrics().ascent(),
565 QString("%1 dBV").arg(db),
566 PaintAssistant::OutlinedText);
567 }
568 568
569 int harmonic = 2; 569 int harmonic = 2;
570 570
571 while (harmonic < 100) { 571 while (harmonic < 100) {
572 572
710 int scaleHeight = getHorizontalScaleHeight(v, paint); 710 int scaleHeight = getHorizontalScaleHeight(v, paint);
711 711
712 QPoint localPos; 712 QPoint localPos;
713 bool shouldIlluminate = v->shouldIlluminateLocalFeatures(this, localPos); 713 bool shouldIlluminate = v->shouldIlluminateLocalFeatures(this, localPos);
714 714
715 cerr << "shouldIlluminate = " << shouldIlluminate << ", localPos = " << localPos.x() << "," << localPos.y() << endl; 715 // cerr << "shouldIlluminate = " << shouldIlluminate << ", localPos = " << localPos.x() << "," << localPos.y() << endl;
716 716
717 if (fft && m_showPeaks) { 717 if (fft && m_showPeaks) {
718 718
719 // draw peak lines 719 // draw peak lines
720 720
721 int col = int(v->getCentreFrame() / fft->getResolution()); 721 int col = int(v->getCentreFrame() / fft->getResolution());
741 getBiasCurve(curve); 741 getBiasCurve(curve);
742 int cs = int(curve.size()); 742 int cs = int(curve.size());
743 743
744 int px = -1; 744 int px = -1;
745 745
746 int fuzz = ViewManager::scalePixelSize(2); 746 int fuzz = ViewManager::scalePixelSize(3);
747 bool illuminatedSomething = false;
747 748
748 for (FFTModel::PeakSet::iterator i = peaks.begin(); 749 for (FFTModel::PeakSet::iterator i = peaks.begin();
749 i != peaks.end(); ++i) { 750 i != peaks.end(); ++i) {
750 751
751 double freq = i->second; 752 double freq = i->second;
759 // cerr << "bin = " << bin << ", thresh = " << thresh << ", value = " << fft->getMagnitudeAt(col, bin) << endl; 760 // cerr << "bin = " << bin << ", thresh = " << thresh << ", value = " << fft->getMagnitudeAt(col, bin) << endl;
760 761
761 double value = fft->getValueAt(col, bin); 762 double value = fft->getValueAt(col, bin);
762 if (value < thresh) continue; 763 if (value < thresh) continue;
763 if (bin < cs) value *= curve[bin]; 764 if (bin < cs) value *= curve[bin];
764 765
765 bool highlightThis = false; 766 double norm = 0.f;
766 if (std::abs(localPos.x() - x) <= fuzz) { 767 // we need the norm here for colour map; the y coord is
767 highlightThis = true; 768 // only used to pick a label height if illuminating the
768 cerr << "should highlight this one (at " << x << ")" << endl; 769 // local point
770 double y = getYForValue(v, value, norm);
771
772 QColor colour = mapper.map(norm);
773
774 paint.setPen(QPen(colour, 1));
775 paint.drawLine(x, 0, x, v->getPaintHeight() - scaleHeight - 1);
776
777 bool illuminateThis = false;
778 if (shouldIlluminate && !illuminatedSomething &&
779 std::abs(localPos.x() - x) <= fuzz) {
780 illuminateThis = true;
781 }
782
783 if (illuminateThis) {
784 int labelY = v->getPaintHeight() -
785 getHorizontalScaleHeight(v, paint) -
786 paint.fontMetrics().height() * 3;
787 QString text = tr("%1 Hz").arg(freq);
788 int lw = paint.fontMetrics().width(text);
789 int gap = ViewManager::scalePixelSize(3);
790 double half = double(gap)/2.0;
791 int labelX = x - lw - gap;
792 if (labelX < getVerticalScaleWidth(v, false, paint)) {
793 labelX = x + gap;
794 }
795 PaintAssistant::drawVisibleText
796 (v, paint, labelX, labelY,
797 text, PaintAssistant::OutlinedText);
798 if (Pitch::isFrequencyInMidiRange(freq)) {
799 QString pitchLabel = Pitch::getPitchLabelForFrequency(freq);
800 PaintAssistant::drawVisibleText
801 (v, paint,
802 labelX, labelY + paint.fontMetrics().ascent() + gap,
803 pitchLabel, PaintAssistant::OutlinedText);
804 }
805 paint.fillRect(QRectF(x - half, labelY + gap, gap, gap),
806 colour);
807 illuminatedSomething = true;
769 } 808 }
770 809
771 double norm = 0.f;
772 // don't need return value, need norm:
773 (void)getYForValue(v, value, norm);
774
775 paint.setPen(QPen(mapper.map(norm), 1));
776 paint.drawLine(x, 0, x, v->getPaintHeight() - scaleHeight - 1);
777
778 px = x; 810 px = x;
779 } 811 }
780 812
781 paint.restore(); 813 paint.restore();
782 } 814 }