Chris@58: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@0: Chris@0: /* Chris@59: Sonic Visualiser Chris@59: An audio file viewer and annotation editor. Chris@59: Centre for Digital Music, Queen Mary, University of London. Chris@484: This file copyright 2006-2009 Chris Cannam and QMUL. Chris@0: Chris@59: This program is free software; you can redistribute it and/or Chris@59: modify it under the terms of the GNU General Public License as Chris@59: published by the Free Software Foundation; either version 2 of the Chris@59: License, or (at your option) any later version. See the file Chris@59: COPYING included with this distribution for more information. Chris@0: */ Chris@0: Chris@0: #include "SpectrogramLayer.h" Chris@0: Chris@128: #include "view/View.h" Chris@0: #include "base/Profiler.h" Chris@0: #include "base/AudioLevel.h" Chris@0: #include "base/Window.h" Chris@24: #include "base/Pitch.h" Chris@118: #include "base/Preferences.h" Chris@167: #include "base/RangeMapper.h" Chris@253: #include "base/LogRange.h" Chris@376: #include "widgets/CommandHistory.h" Chris@376: #include "ColourMapper.h" Chris@283: #include "ImageRegionFinder.h" Chris@484: #include "data/model/Dense3DModelPeakCache.h" Chris@690: #include "PianoScale.h" Chris@0: Chris@0: #include Chris@0: #include Chris@0: #include Chris@0: #include Chris@92: #include Chris@178: #include Chris@283: #include Chris@316: #include Chris@1017: #include Chris@0: Chris@0: #include Chris@0: Chris@0: #include Chris@0: #include Chris@0: Chris@545: #ifndef __GNUC__ Chris@545: #include Chris@545: #endif Chris@545: Chris@1044: #define DEBUG_SPECTROGRAM_REPAINT 1 Chris@1025: Chris@1025: using namespace std; Chris@907: Chris@44: SpectrogramLayer::SpectrogramLayer(Configuration config) : Chris@0: m_model(0), Chris@0: m_channel(0), Chris@0: m_windowSize(1024), Chris@0: m_windowType(HanningWindow), Chris@97: m_windowHopLevel(2), Chris@109: m_zeroPadLevel(0), Chris@107: m_fftSize(1024), Chris@0: m_gain(1.0), Chris@215: m_initialGain(1.0), Chris@37: m_threshold(0.0), Chris@215: m_initialThreshold(0.0), Chris@9: m_colourRotation(0), Chris@215: m_initialRotation(0), Chris@119: m_minFrequency(10), Chris@0: m_maxFrequency(8000), Chris@135: m_initialMaxFrequency(8000), Chris@0: m_colourScale(dBColourScale), Chris@197: m_colourMap(0), Chris@0: m_frequencyScale(LinearFrequencyScale), Chris@37: m_binDisplay(AllBins), Chris@862: m_normalization(NoNormalization), Chris@133: m_lastEmittedZoomStep(-1), Chris@390: m_synchronous(false), Chris@608: m_haveDetailedScale(false), Chris@193: m_exiting(false), Chris@1054: m_peakCacheDivisor(8), Chris@193: m_sliceableModel(0) Chris@0: { Chris@1017: QString colourConfigName = "spectrogram-colour"; Chris@1017: int colourConfigDefault = int(ColourMapper::Green); Chris@1017: Chris@215: if (config == FullRangeDb) { Chris@215: m_initialMaxFrequency = 0; Chris@215: setMaxFrequency(0); Chris@215: } else if (config == MelodicRange) { Chris@0: setWindowSize(8192); Chris@97: setWindowHopLevel(4); Chris@215: m_initialMaxFrequency = 1500; Chris@215: setMaxFrequency(1500); Chris@215: setMinFrequency(40); Chris@0: setColourScale(LinearColourScale); Chris@215: setColourMap(ColourMapper::Sunset); Chris@215: setFrequencyScale(LogFrequencyScale); Chris@1017: colourConfigName = "spectrogram-melodic-colour"; Chris@1017: colourConfigDefault = int(ColourMapper::Sunset); Chris@224: // setGain(20); Chris@37: } else if (config == MelodicPeaks) { Chris@37: setWindowSize(4096); Chris@97: setWindowHopLevel(5); Chris@135: m_initialMaxFrequency = 2000; Chris@40: setMaxFrequency(2000); Chris@37: setMinFrequency(40); Chris@37: setFrequencyScale(LogFrequencyScale); Chris@215: setColourScale(LinearColourScale); Chris@37: setBinDisplay(PeakFrequencies); Chris@862: setNormalization(NormalizeColumns); Chris@1017: colourConfigName = "spectrogram-melodic-colour"; Chris@1017: colourConfigDefault = int(ColourMapper::Sunset); Chris@0: } Chris@110: Chris@1017: QSettings settings; Chris@1017: settings.beginGroup("Preferences"); Chris@1017: setColourMap(settings.value(colourConfigName, colourConfigDefault).toInt()); Chris@1017: settings.endGroup(); Chris@1017: Chris@122: Preferences *prefs = Preferences::getInstance(); Chris@122: connect(prefs, SIGNAL(propertyChanged(PropertyContainer::PropertyName)), Chris@122: this, SLOT(preferenceChanged(PropertyContainer::PropertyName))); Chris@122: setWindowType(prefs->getWindowType()); Chris@122: Chris@197: initialisePalette(); Chris@0: } Chris@0: Chris@0: SpectrogramLayer::~SpectrogramLayer() Chris@0: { Chris@130: invalidateFFTModels(); Chris@0: } Chris@0: Chris@0: void Chris@0: SpectrogramLayer::setModel(const DenseTimeValueModel *model) Chris@0: { Chris@682: // cerr << "SpectrogramLayer(" << this << "): setModel(" << model << ")" << endl; Chris@34: Chris@110: if (model == m_model) return; Chris@110: Chris@0: m_model = model; Chris@130: invalidateFFTModels(); Chris@0: Chris@0: if (!m_model || !m_model->isOK()) return; Chris@0: Chris@320: connectSignals(m_model); Chris@0: Chris@0: connect(m_model, SIGNAL(modelChanged()), this, SLOT(cacheInvalid())); Chris@906: connect(m_model, SIGNAL(modelChangedWithin(sv_frame_t, sv_frame_t)), Chris@906: this, SLOT(cacheInvalid(sv_frame_t, sv_frame_t))); Chris@0: Chris@0: emit modelReplaced(); Chris@110: } Chris@115: Chris@0: Layer::PropertyList Chris@0: SpectrogramLayer::getProperties() const Chris@0: { Chris@0: PropertyList list; Chris@87: list.push_back("Colour"); Chris@87: list.push_back("Colour Scale"); Chris@87: list.push_back("Window Size"); Chris@97: list.push_back("Window Increment"); Chris@862: list.push_back("Normalization"); Chris@87: list.push_back("Bin Display"); Chris@87: list.push_back("Threshold"); Chris@87: list.push_back("Gain"); Chris@87: list.push_back("Colour Rotation"); Chris@153: // list.push_back("Min Frequency"); Chris@153: // list.push_back("Max Frequency"); Chris@87: list.push_back("Frequency Scale"); Chris@153: //// list.push_back("Zero Padding"); Chris@0: return list; Chris@0: } Chris@0: Chris@87: QString Chris@87: SpectrogramLayer::getPropertyLabel(const PropertyName &name) const Chris@87: { Chris@87: if (name == "Colour") return tr("Colour"); Chris@87: if (name == "Colour Scale") return tr("Colour Scale"); Chris@87: if (name == "Window Size") return tr("Window Size"); Chris@112: if (name == "Window Increment") return tr("Window Overlap"); Chris@862: if (name == "Normalization") return tr("Normalization"); Chris@87: if (name == "Bin Display") return tr("Bin Display"); Chris@87: if (name == "Threshold") return tr("Threshold"); Chris@87: if (name == "Gain") return tr("Gain"); Chris@87: if (name == "Colour Rotation") return tr("Colour Rotation"); Chris@87: if (name == "Min Frequency") return tr("Min Frequency"); Chris@87: if (name == "Max Frequency") return tr("Max Frequency"); Chris@87: if (name == "Frequency Scale") return tr("Frequency Scale"); Chris@109: if (name == "Zero Padding") return tr("Smoothing"); Chris@87: return ""; Chris@87: } Chris@87: Chris@335: QString Chris@862: SpectrogramLayer::getPropertyIconName(const PropertyName &) const Chris@335: { Chris@335: return ""; Chris@335: } Chris@335: Chris@0: Layer::PropertyType Chris@0: SpectrogramLayer::getPropertyType(const PropertyName &name) const Chris@0: { Chris@87: if (name == "Gain") return RangeProperty; Chris@87: if (name == "Colour Rotation") return RangeProperty; Chris@87: if (name == "Threshold") return RangeProperty; Chris@109: if (name == "Zero Padding") return ToggleProperty; Chris@0: return ValueProperty; Chris@0: } Chris@0: Chris@0: QString Chris@0: SpectrogramLayer::getPropertyGroupName(const PropertyName &name) const Chris@0: { Chris@153: if (name == "Bin Display" || Chris@153: name == "Frequency Scale") return tr("Bins"); Chris@87: if (name == "Window Size" || Chris@109: name == "Window Increment" || Chris@109: name == "Zero Padding") return tr("Window"); Chris@87: if (name == "Colour" || Chris@87: name == "Threshold" || Chris@87: name == "Colour Rotation") return tr("Colour"); Chris@862: if (name == "Normalization" || Chris@153: name == "Gain" || Chris@87: name == "Colour Scale") return tr("Scale"); Chris@0: return QString(); Chris@0: } Chris@0: Chris@0: int Chris@0: SpectrogramLayer::getPropertyRangeAndValue(const PropertyName &name, Chris@216: int *min, int *max, int *deflt) const Chris@0: { Chris@216: int val = 0; Chris@216: Chris@216: int garbage0, garbage1, garbage2; Chris@55: if (!min) min = &garbage0; Chris@55: if (!max) max = &garbage1; Chris@216: if (!deflt) deflt = &garbage2; Chris@10: Chris@87: if (name == "Gain") { Chris@0: Chris@0: *min = -50; Chris@0: *max = 50; Chris@0: Chris@906: *deflt = int(lrint(log10(m_initialGain) * 20.0)); Chris@216: if (*deflt < *min) *deflt = *min; Chris@216: if (*deflt > *max) *deflt = *max; Chris@216: Chris@906: val = int(lrint(log10(m_gain) * 20.0)); Chris@216: if (val < *min) val = *min; Chris@216: if (val > *max) val = *max; Chris@0: Chris@87: } else if (name == "Threshold") { Chris@37: Chris@37: *min = -50; Chris@37: *max = 0; Chris@37: Chris@906: *deflt = int(lrint(AudioLevel::multiplier_to_dB(m_initialThreshold))); Chris@216: if (*deflt < *min) *deflt = *min; Chris@216: if (*deflt > *max) *deflt = *max; Chris@216: Chris@906: val = int(lrint(AudioLevel::multiplier_to_dB(m_threshold))); Chris@216: if (val < *min) val = *min; Chris@216: if (val > *max) val = *max; Chris@37: Chris@87: } else if (name == "Colour Rotation") { Chris@9: Chris@9: *min = 0; Chris@9: *max = 256; Chris@216: *deflt = m_initialRotation; Chris@216: Chris@216: val = m_colourRotation; Chris@9: Chris@87: } else if (name == "Colour Scale") { Chris@0: Chris@0: *min = 0; Chris@176: *max = 4; Chris@216: *deflt = int(dBColourScale); Chris@216: Chris@216: val = (int)m_colourScale; Chris@0: Chris@87: } else if (name == "Colour") { Chris@0: Chris@0: *min = 0; Chris@196: *max = ColourMapper::getColourMapCount() - 1; Chris@216: *deflt = 0; Chris@216: Chris@216: val = m_colourMap; Chris@0: Chris@87: } else if (name == "Window Size") { Chris@0: Chris@0: *min = 0; Chris@0: *max = 10; Chris@216: *deflt = 5; Chris@0: Chris@216: val = 0; Chris@0: int ws = m_windowSize; Chris@216: while (ws > 32) { ws >>= 1; val ++; } Chris@0: Chris@97: } else if (name == "Window Increment") { Chris@0: Chris@0: *min = 0; Chris@97: *max = 5; Chris@216: *deflt = 2; Chris@216: Chris@216: val = m_windowHopLevel; Chris@0: Chris@109: } else if (name == "Zero Padding") { Chris@109: Chris@109: *min = 0; Chris@109: *max = 1; Chris@216: *deflt = 0; Chris@109: Chris@216: val = m_zeroPadLevel > 0 ? 1 : 0; Chris@109: Chris@87: } else if (name == "Min Frequency") { Chris@37: Chris@37: *min = 0; Chris@37: *max = 9; Chris@216: *deflt = 1; Chris@37: Chris@37: switch (m_minFrequency) { Chris@216: case 0: default: val = 0; break; Chris@216: case 10: val = 1; break; Chris@216: case 20: val = 2; break; Chris@216: case 40: val = 3; break; Chris@216: case 100: val = 4; break; Chris@216: case 250: val = 5; break; Chris@216: case 500: val = 6; break; Chris@216: case 1000: val = 7; break; Chris@216: case 4000: val = 8; break; Chris@216: case 10000: val = 9; break; Chris@37: } Chris@37: Chris@87: } else if (name == "Max Frequency") { Chris@0: Chris@0: *min = 0; Chris@0: *max = 9; Chris@216: *deflt = 6; Chris@0: Chris@0: switch (m_maxFrequency) { Chris@216: case 500: val = 0; break; Chris@216: case 1000: val = 1; break; Chris@216: case 1500: val = 2; break; Chris@216: case 2000: val = 3; break; Chris@216: case 4000: val = 4; break; Chris@216: case 6000: val = 5; break; Chris@216: case 8000: val = 6; break; Chris@216: case 12000: val = 7; break; Chris@216: case 16000: val = 8; break; Chris@216: default: val = 9; break; Chris@0: } Chris@0: Chris@87: } else if (name == "Frequency Scale") { Chris@0: Chris@0: *min = 0; Chris@0: *max = 1; Chris@216: *deflt = int(LinearFrequencyScale); Chris@216: val = (int)m_frequencyScale; Chris@0: Chris@87: } else if (name == "Bin Display") { Chris@35: Chris@35: *min = 0; Chris@35: *max = 2; Chris@216: *deflt = int(AllBins); Chris@216: val = (int)m_binDisplay; Chris@35: Chris@862: } else if (name == "Normalization") { Chris@36: Chris@862: *min = 0; Chris@862: *max = 3; Chris@862: *deflt = int(NoNormalization); Chris@862: val = (int)m_normalization; Chris@120: Chris@0: } else { Chris@216: val = Layer::getPropertyRangeAndValue(name, min, max, deflt); Chris@0: } Chris@0: Chris@216: return val; Chris@0: } Chris@0: Chris@0: QString Chris@0: SpectrogramLayer::getPropertyValueLabel(const PropertyName &name, Chris@9: int value) const Chris@0: { Chris@87: if (name == "Colour") { Chris@196: return ColourMapper::getColourMapName(value); Chris@0: } Chris@87: if (name == "Colour Scale") { Chris@0: switch (value) { Chris@0: default: Chris@37: case 0: return tr("Linear"); Chris@37: case 1: return tr("Meter"); Chris@215: case 2: return tr("dBV^2"); Chris@215: case 3: return tr("dBV"); Chris@119: case 4: return tr("Phase"); Chris@0: } Chris@0: } Chris@862: if (name == "Normalization") { Chris@862: return ""; // icon only Chris@862: } Chris@87: if (name == "Window Size") { Chris@0: return QString("%1").arg(32 << value); Chris@0: } Chris@97: if (name == "Window Increment") { Chris@0: switch (value) { Chris@0: default: Chris@112: case 0: return tr("None"); Chris@112: case 1: return tr("25 %"); Chris@112: case 2: return tr("50 %"); Chris@112: case 3: return tr("75 %"); Chris@112: case 4: return tr("87.5 %"); Chris@112: case 5: return tr("93.75 %"); Chris@0: } Chris@0: } Chris@109: if (name == "Zero Padding") { Chris@109: if (value == 0) return tr("None"); Chris@109: return QString("%1x").arg(value + 1); Chris@109: } Chris@87: if (name == "Min Frequency") { Chris@37: switch (value) { Chris@37: default: Chris@38: case 0: return tr("No min"); Chris@37: case 1: return tr("10 Hz"); Chris@37: case 2: return tr("20 Hz"); Chris@37: case 3: return tr("40 Hz"); Chris@37: case 4: return tr("100 Hz"); Chris@37: case 5: return tr("250 Hz"); Chris@37: case 6: return tr("500 Hz"); Chris@37: case 7: return tr("1 KHz"); Chris@37: case 8: return tr("4 KHz"); Chris@37: case 9: return tr("10 KHz"); Chris@37: } Chris@37: } Chris@87: if (name == "Max Frequency") { Chris@0: switch (value) { Chris@0: default: Chris@0: case 0: return tr("500 Hz"); Chris@0: case 1: return tr("1 KHz"); Chris@0: case 2: return tr("1.5 KHz"); Chris@0: case 3: return tr("2 KHz"); Chris@0: case 4: return tr("4 KHz"); Chris@0: case 5: return tr("6 KHz"); Chris@0: case 6: return tr("8 KHz"); Chris@0: case 7: return tr("12 KHz"); Chris@0: case 8: return tr("16 KHz"); Chris@38: case 9: return tr("No max"); Chris@0: } Chris@0: } Chris@87: if (name == "Frequency Scale") { Chris@0: switch (value) { Chris@0: default: Chris@0: case 0: return tr("Linear"); Chris@0: case 1: return tr("Log"); Chris@0: } Chris@0: } Chris@87: if (name == "Bin Display") { Chris@35: switch (value) { Chris@35: default: Chris@37: case 0: return tr("All Bins"); Chris@37: case 1: return tr("Peak Bins"); Chris@37: case 2: return tr("Frequencies"); Chris@35: } Chris@35: } Chris@0: return tr(""); Chris@0: } Chris@0: Chris@862: QString Chris@862: SpectrogramLayer::getPropertyValueIconName(const PropertyName &name, Chris@862: int value) const Chris@862: { Chris@862: if (name == "Normalization") { Chris@862: switch(value) { Chris@862: default: Chris@862: case 0: return "normalise-none"; Chris@862: case 1: return "normalise-columns"; Chris@862: case 2: return "normalise"; Chris@862: case 3: return "normalise-hybrid"; Chris@862: } Chris@862: } Chris@862: return ""; Chris@862: } Chris@862: Chris@167: RangeMapper * Chris@167: SpectrogramLayer::getNewPropertyRangeMapper(const PropertyName &name) const Chris@167: { Chris@167: if (name == "Gain") { Chris@167: return new LinearRangeMapper(-50, 50, -25, 25, tr("dB")); Chris@167: } Chris@167: if (name == "Threshold") { Chris@167: return new LinearRangeMapper(-50, 0, -50, 0, tr("dB")); Chris@167: } Chris@167: return 0; Chris@167: } Chris@167: Chris@0: void Chris@0: SpectrogramLayer::setProperty(const PropertyName &name, int value) Chris@0: { Chris@87: if (name == "Gain") { Chris@906: setGain(float(pow(10, float(value)/20.0))); Chris@87: } else if (name == "Threshold") { Chris@37: if (value == -50) setThreshold(0.0); Chris@906: else setThreshold(float(AudioLevel::dB_to_multiplier(value))); Chris@87: } else if (name == "Colour Rotation") { Chris@9: setColourRotation(value); Chris@87: } else if (name == "Colour") { Chris@197: setColourMap(value); Chris@87: } else if (name == "Window Size") { Chris@0: setWindowSize(32 << value); Chris@97: } else if (name == "Window Increment") { Chris@97: setWindowHopLevel(value); Chris@109: } else if (name == "Zero Padding") { Chris@109: setZeroPadLevel(value > 0.1 ? 3 : 0); Chris@87: } else if (name == "Min Frequency") { Chris@37: switch (value) { Chris@37: default: Chris@37: case 0: setMinFrequency(0); break; Chris@37: case 1: setMinFrequency(10); break; Chris@37: case 2: setMinFrequency(20); break; Chris@37: case 3: setMinFrequency(40); break; Chris@37: case 4: setMinFrequency(100); break; Chris@37: case 5: setMinFrequency(250); break; Chris@37: case 6: setMinFrequency(500); break; Chris@37: case 7: setMinFrequency(1000); break; Chris@37: case 8: setMinFrequency(4000); break; Chris@37: case 9: setMinFrequency(10000); break; Chris@37: } Chris@133: int vs = getCurrentVerticalZoomStep(); Chris@133: if (vs != m_lastEmittedZoomStep) { Chris@133: emit verticalZoomChanged(); Chris@133: m_lastEmittedZoomStep = vs; Chris@133: } Chris@87: } else if (name == "Max Frequency") { Chris@0: switch (value) { Chris@0: case 0: setMaxFrequency(500); break; Chris@0: case 1: setMaxFrequency(1000); break; Chris@0: case 2: setMaxFrequency(1500); break; Chris@0: case 3: setMaxFrequency(2000); break; Chris@0: case 4: setMaxFrequency(4000); break; Chris@0: case 5: setMaxFrequency(6000); break; Chris@0: case 6: setMaxFrequency(8000); break; Chris@0: case 7: setMaxFrequency(12000); break; Chris@0: case 8: setMaxFrequency(16000); break; Chris@0: default: Chris@0: case 9: setMaxFrequency(0); break; Chris@0: } Chris@133: int vs = getCurrentVerticalZoomStep(); Chris@133: if (vs != m_lastEmittedZoomStep) { Chris@133: emit verticalZoomChanged(); Chris@133: m_lastEmittedZoomStep = vs; Chris@133: } Chris@87: } else if (name == "Colour Scale") { Chris@0: switch (value) { Chris@0: default: Chris@0: case 0: setColourScale(LinearColourScale); break; Chris@0: case 1: setColourScale(MeterColourScale); break; Chris@215: case 2: setColourScale(dBSquaredColourScale); break; Chris@215: case 3: setColourScale(dBColourScale); break; Chris@119: case 4: setColourScale(PhaseColourScale); break; Chris@0: } Chris@87: } else if (name == "Frequency Scale") { Chris@0: switch (value) { Chris@0: default: Chris@0: case 0: setFrequencyScale(LinearFrequencyScale); break; Chris@0: case 1: setFrequencyScale(LogFrequencyScale); break; Chris@0: } Chris@87: } else if (name == "Bin Display") { Chris@35: switch (value) { Chris@35: default: Chris@37: case 0: setBinDisplay(AllBins); break; Chris@37: case 1: setBinDisplay(PeakBins); break; Chris@37: case 2: setBinDisplay(PeakFrequencies); break; Chris@35: } Chris@862: } else if (name == "Normalization") { Chris@862: switch (value) { Chris@862: default: Chris@862: case 0: setNormalization(NoNormalization); break; Chris@862: case 1: setNormalization(NormalizeColumns); break; Chris@862: case 2: setNormalization(NormalizeVisibleArea); break; Chris@862: case 3: setNormalization(NormalizeHybrid); break; Chris@862: } Chris@0: } Chris@0: } Chris@0: Chris@0: void Chris@478: SpectrogramLayer::invalidateImageCaches() Chris@95: { Chris@1044: #ifdef DEBUG_SPECTROGRAM Chris@1044: cerr << "SpectrogramLayer::invalidateImageCaches called" << endl; Chris@1044: #endif Chris@478: for (ViewImageCache::iterator i = m_imageCaches.begin(); Chris@478: i != m_imageCaches.end(); ++i) { Chris@1030: i->second.invalidate(); Chris@95: } Chris@95: } Chris@95: Chris@95: void Chris@122: SpectrogramLayer::preferenceChanged(PropertyContainer::PropertyName name) Chris@122: { Chris@587: SVDEBUG << "SpectrogramLayer::preferenceChanged(" << name << ")" << endl; Chris@122: Chris@122: if (name == "Window Type") { Chris@122: setWindowType(Preferences::getInstance()->getWindowType()); Chris@122: return; Chris@122: } Chris@490: if (name == "Spectrogram Y Smoothing") { Chris@490: invalidateImageCaches(); Chris@490: invalidateMagnitudes(); Chris@490: emit layerParametersChanged(); Chris@490: } Chris@490: if (name == "Spectrogram X Smoothing") { Chris@478: invalidateImageCaches(); Chris@122: invalidateMagnitudes(); Chris@122: emit layerParametersChanged(); Chris@122: } Chris@122: if (name == "Tuning Frequency") { Chris@122: emit layerParametersChanged(); Chris@122: } Chris@122: } Chris@122: Chris@122: void Chris@0: SpectrogramLayer::setChannel(int ch) Chris@0: { Chris@0: if (m_channel == ch) return; Chris@0: Chris@478: invalidateImageCaches(); Chris@0: m_channel = ch; Chris@130: invalidateFFTModels(); Chris@9: Chris@0: emit layerParametersChanged(); Chris@0: } Chris@0: Chris@0: int Chris@0: SpectrogramLayer::getChannel() const Chris@0: { Chris@0: return m_channel; Chris@0: } Chris@0: Chris@0: void Chris@805: SpectrogramLayer::setWindowSize(int ws) Chris@0: { Chris@0: if (m_windowSize == ws) return; Chris@0: Chris@478: invalidateImageCaches(); Chris@0: Chris@0: m_windowSize = ws; Chris@109: m_fftSize = ws * (m_zeroPadLevel + 1); Chris@0: Chris@130: invalidateFFTModels(); Chris@9: Chris@9: emit layerParametersChanged(); Chris@0: } Chris@0: Chris@805: int Chris@0: SpectrogramLayer::getWindowSize() const Chris@0: { Chris@0: return m_windowSize; Chris@0: } Chris@0: Chris@0: void Chris@805: SpectrogramLayer::setWindowHopLevel(int v) Chris@0: { Chris@97: if (m_windowHopLevel == v) return; Chris@0: Chris@478: invalidateImageCaches(); Chris@0: Chris@97: m_windowHopLevel = v; Chris@0: Chris@130: invalidateFFTModels(); Chris@9: Chris@9: emit layerParametersChanged(); Chris@9: Chris@110: // fillCache(); Chris@0: } Chris@0: Chris@805: int Chris@97: SpectrogramLayer::getWindowHopLevel() const Chris@0: { Chris@97: return m_windowHopLevel; Chris@0: } Chris@0: Chris@0: void Chris@805: SpectrogramLayer::setZeroPadLevel(int v) Chris@109: { Chris@109: if (m_zeroPadLevel == v) return; Chris@109: Chris@478: invalidateImageCaches(); Chris@109: Chris@109: m_zeroPadLevel = v; Chris@109: m_fftSize = m_windowSize * (v + 1); Chris@110: Chris@130: invalidateFFTModels(); Chris@109: Chris@109: emit layerParametersChanged(); Chris@109: } Chris@109: Chris@805: int Chris@109: SpectrogramLayer::getZeroPadLevel() const Chris@109: { Chris@109: return m_zeroPadLevel; Chris@109: } Chris@109: Chris@109: void Chris@0: SpectrogramLayer::setWindowType(WindowType w) Chris@0: { Chris@0: if (m_windowType == w) return; Chris@0: Chris@478: invalidateImageCaches(); Chris@0: Chris@0: m_windowType = w; Chris@110: Chris@130: invalidateFFTModels(); Chris@9: Chris@9: emit layerParametersChanged(); Chris@0: } Chris@0: Chris@0: WindowType Chris@0: SpectrogramLayer::getWindowType() const Chris@0: { Chris@0: return m_windowType; Chris@0: } Chris@0: Chris@0: void Chris@0: SpectrogramLayer::setGain(float gain) Chris@0: { Chris@587: // SVDEBUG << "SpectrogramLayer::setGain(" << gain << ") (my gain is now " Chris@585: // << m_gain << ")" << endl; Chris@55: Chris@40: if (m_gain == gain) return; Chris@0: Chris@478: invalidateImageCaches(); Chris@0: Chris@0: m_gain = gain; Chris@0: Chris@9: emit layerParametersChanged(); Chris@0: } Chris@0: Chris@0: float Chris@0: SpectrogramLayer::getGain() const Chris@0: { Chris@0: return m_gain; Chris@0: } Chris@0: Chris@0: void Chris@37: SpectrogramLayer::setThreshold(float threshold) Chris@37: { Chris@40: if (m_threshold == threshold) return; Chris@37: Chris@478: invalidateImageCaches(); Chris@37: Chris@37: m_threshold = threshold; Chris@37: Chris@37: emit layerParametersChanged(); Chris@37: } Chris@37: Chris@37: float Chris@37: SpectrogramLayer::getThreshold() const Chris@37: { Chris@37: return m_threshold; Chris@37: } Chris@37: Chris@37: void Chris@805: SpectrogramLayer::setMinFrequency(int mf) Chris@37: { Chris@37: if (m_minFrequency == mf) return; Chris@37: Chris@587: // SVDEBUG << "SpectrogramLayer::setMinFrequency: " << mf << endl; Chris@187: Chris@478: invalidateImageCaches(); Chris@119: invalidateMagnitudes(); Chris@37: Chris@37: m_minFrequency = mf; Chris@37: Chris@37: emit layerParametersChanged(); Chris@37: } Chris@37: Chris@805: int Chris@37: SpectrogramLayer::getMinFrequency() const Chris@37: { Chris@37: return m_minFrequency; Chris@37: } Chris@37: Chris@37: void Chris@805: SpectrogramLayer::setMaxFrequency(int mf) Chris@0: { Chris@0: if (m_maxFrequency == mf) return; Chris@0: Chris@587: // SVDEBUG << "SpectrogramLayer::setMaxFrequency: " << mf << endl; Chris@187: Chris@478: invalidateImageCaches(); Chris@119: invalidateMagnitudes(); Chris@0: Chris@0: m_maxFrequency = mf; Chris@0: Chris@9: emit layerParametersChanged(); Chris@0: } Chris@0: Chris@805: int Chris@0: SpectrogramLayer::getMaxFrequency() const Chris@0: { Chris@0: return m_maxFrequency; Chris@0: } Chris@0: Chris@0: void Chris@9: SpectrogramLayer::setColourRotation(int r) Chris@9: { Chris@478: invalidateImageCaches(); Chris@9: Chris@9: if (r < 0) r = 0; Chris@9: if (r > 256) r = 256; Chris@9: int distance = r - m_colourRotation; Chris@9: Chris@9: if (distance != 0) { Chris@197: rotatePalette(-distance); Chris@9: m_colourRotation = r; Chris@9: } Chris@9: Chris@9: emit layerParametersChanged(); Chris@9: } Chris@9: Chris@9: void Chris@0: SpectrogramLayer::setColourScale(ColourScale colourScale) Chris@0: { Chris@0: if (m_colourScale == colourScale) return; Chris@0: Chris@478: invalidateImageCaches(); Chris@0: Chris@0: m_colourScale = colourScale; Chris@0: Chris@9: emit layerParametersChanged(); Chris@0: } Chris@0: Chris@0: SpectrogramLayer::ColourScale Chris@0: SpectrogramLayer::getColourScale() const Chris@0: { Chris@0: return m_colourScale; Chris@0: } Chris@0: Chris@0: void Chris@197: SpectrogramLayer::setColourMap(int map) Chris@0: { Chris@197: if (m_colourMap == map) return; Chris@0: Chris@478: invalidateImageCaches(); Chris@0: Chris@197: m_colourMap = map; Chris@197: initialisePalette(); Chris@9: Chris@0: emit layerParametersChanged(); Chris@0: } Chris@0: Chris@196: int Chris@197: SpectrogramLayer::getColourMap() const Chris@0: { Chris@197: return m_colourMap; Chris@0: } Chris@0: Chris@0: void Chris@0: SpectrogramLayer::setFrequencyScale(FrequencyScale frequencyScale) Chris@0: { Chris@0: if (m_frequencyScale == frequencyScale) return; Chris@0: Chris@478: invalidateImageCaches(); Chris@0: m_frequencyScale = frequencyScale; Chris@9: Chris@9: emit layerParametersChanged(); Chris@0: } Chris@0: Chris@0: SpectrogramLayer::FrequencyScale Chris@0: SpectrogramLayer::getFrequencyScale() const Chris@0: { Chris@0: return m_frequencyScale; Chris@0: } Chris@0: Chris@0: void Chris@37: SpectrogramLayer::setBinDisplay(BinDisplay binDisplay) Chris@35: { Chris@37: if (m_binDisplay == binDisplay) return; Chris@35: Chris@478: invalidateImageCaches(); Chris@37: m_binDisplay = binDisplay; Chris@35: Chris@35: emit layerParametersChanged(); Chris@35: } Chris@35: Chris@37: SpectrogramLayer::BinDisplay Chris@37: SpectrogramLayer::getBinDisplay() const Chris@35: { Chris@37: return m_binDisplay; Chris@35: } Chris@35: Chris@35: void Chris@862: SpectrogramLayer::setNormalization(Normalization n) Chris@36: { Chris@862: if (m_normalization == n) return; Chris@36: Chris@478: invalidateImageCaches(); Chris@119: invalidateMagnitudes(); Chris@862: m_normalization = n; Chris@36: Chris@36: emit layerParametersChanged(); Chris@36: } Chris@36: Chris@862: SpectrogramLayer::Normalization Chris@862: SpectrogramLayer::getNormalization() const Chris@36: { Chris@862: return m_normalization; Chris@36: } Chris@36: Chris@36: void Chris@918: SpectrogramLayer::setLayerDormant(const LayerGeometryProvider *v, bool dormant) Chris@29: { Chris@33: if (dormant) { Chris@33: Chris@331: #ifdef DEBUG_SPECTROGRAM_REPAINT Chris@985: cerr << "SpectrogramLayer::setLayerDormant(" << dormant << ")" Chris@585: << endl; Chris@331: #endif Chris@331: Chris@131: if (isLayerDormant(v)) { Chris@131: return; Chris@131: } Chris@131: Chris@131: Layer::setLayerDormant(v, true); Chris@33: Chris@920: const View *view = v->getView(); Chris@920: Chris@478: invalidateImageCaches(); Chris@988: Chris@1030: m_imageCaches.erase(view->getId()); Chris@1030: Chris@1030: if (m_fftModels.find(view->getId()) != m_fftModels.end()) { Chris@1030: Chris@1030: if (m_sliceableModel == m_fftModels[view->getId()]) { Chris@193: bool replaced = false; Chris@193: for (ViewFFTMap::iterator i = m_fftModels.begin(); Chris@193: i != m_fftModels.end(); ++i) { Chris@985: if (i->second != m_sliceableModel) { Chris@985: emit sliceableModelReplaced(m_sliceableModel, i->second); Chris@193: replaced = true; Chris@193: break; Chris@193: } Chris@193: } Chris@193: if (!replaced) emit sliceableModelReplaced(m_sliceableModel, 0); Chris@193: } Chris@193: Chris@1030: delete m_fftModels[view->getId()]; Chris@1030: m_fftModels.erase(view->getId()); Chris@1030: Chris@1030: delete m_peakCaches[view->getId()]; Chris@1030: m_peakCaches.erase(view->getId()); Chris@114: } Chris@33: Chris@33: } else { Chris@33: Chris@131: Layer::setLayerDormant(v, false); Chris@33: } Chris@29: } Chris@29: Chris@29: void Chris@0: SpectrogramLayer::cacheInvalid() Chris@0: { Chris@391: #ifdef DEBUG_SPECTROGRAM_REPAINT Chris@985: cerr << "SpectrogramLayer::cacheInvalid()" << endl; Chris@391: #endif Chris@391: Chris@478: invalidateImageCaches(); Chris@119: invalidateMagnitudes(); Chris@0: } Chris@0: Chris@0: void Chris@1037: SpectrogramLayer::cacheInvalid( Chris@1037: #ifdef DEBUG_SPECTROGRAM_REPAINT Chris@1037: sv_frame_t from, sv_frame_t to Chris@1037: #else Chris@1037: sv_frame_t , sv_frame_t Chris@1037: #endif Chris@1037: ) Chris@0: { Chris@391: #ifdef DEBUG_SPECTROGRAM_REPAINT Chris@985: cerr << "SpectrogramLayer::cacheInvalid(" << from << ", " << to << ")" << endl; Chris@391: #endif Chris@391: Chris@1030: // We used to call invalidateMagnitudes(from, to) to invalidate Chris@1030: // only those caches whose views contained some of the (from, to) Chris@1030: // range. That's the right thing to do; it has been lost in Chris@1030: // pulling out the image cache code, but it might not matter very Chris@1030: // much, since the underlying models for spectrogram layers don't Chris@1030: // change very often. Let's see. Chris@1030: invalidateImageCaches(); Chris@391: invalidateMagnitudes(); Chris@0: } Chris@0: Chris@224: bool Chris@224: SpectrogramLayer::hasLightBackground() const Chris@224: { Chris@287: return ColourMapper(m_colourMap, 1.f, 255.f).hasLightBackground(); Chris@224: } Chris@224: Chris@0: void Chris@197: SpectrogramLayer::initialisePalette() Chris@0: { Chris@10: int formerRotation = m_colourRotation; Chris@10: Chris@197: if (m_colourMap == (int)ColourMapper::BlackOnWhite) { Chris@197: m_palette.setColour(NO_VALUE, Qt::white); Chris@38: } else { Chris@197: m_palette.setColour(NO_VALUE, Qt::black); Chris@38: } Chris@0: Chris@197: ColourMapper mapper(m_colourMap, 1.f, 255.f); Chris@196: Chris@0: for (int pixel = 1; pixel < 256; ++pixel) { Chris@907: m_palette.setColour((unsigned char)pixel, mapper.map(pixel)); Chris@0: } Chris@9: Chris@196: m_crosshairColour = mapper.getContrastingColour(); Chris@196: Chris@9: m_colourRotation = 0; Chris@197: rotatePalette(m_colourRotation - formerRotation); Chris@10: m_colourRotation = formerRotation; Chris@478: Chris@478: m_drawBuffer = QImage(); Chris@9: } Chris@9: Chris@9: void Chris@197: SpectrogramLayer::rotatePalette(int distance) Chris@9: { Chris@31: QColor newPixels[256]; Chris@9: Chris@197: newPixels[NO_VALUE] = m_palette.getColour(NO_VALUE); Chris@9: Chris@9: for (int pixel = 1; pixel < 256; ++pixel) { Chris@9: int target = pixel + distance; Chris@9: while (target < 1) target += 255; Chris@9: while (target > 255) target -= 255; Chris@907: newPixels[target] = m_palette.getColour((unsigned char)pixel); Chris@9: } Chris@9: Chris@9: for (int pixel = 0; pixel < 256; ++pixel) { Chris@907: m_palette.setColour((unsigned char)pixel, newPixels[pixel]); Chris@9: } Chris@478: Chris@478: m_drawBuffer = QImage(); Chris@0: } Chris@0: Chris@38: unsigned char Chris@918: SpectrogramLayer::getDisplayValue(LayerGeometryProvider *v, double input) const Chris@38: { Chris@38: int value; Chris@37: Chris@907: double min = 0.0; Chris@907: double max = 1.0; Chris@120: Chris@862: if (m_normalization == NormalizeVisibleArea) { Chris@1030: min = m_viewMags[v->getId()].getMin(); Chris@1030: max = m_viewMags[v->getId()].getMax(); Chris@862: } else if (m_normalization != NormalizeColumns) { Chris@224: if (m_colourScale == LinearColourScale //|| Chris@224: // m_colourScale == MeterColourScale) { Chris@224: ) { Chris@907: max = 0.1; Chris@120: } Chris@120: } Chris@120: Chris@907: double thresh = -80.0; Chris@907: Chris@907: if (max == 0.0) max = 1.0; Chris@907: if (max == min) min = max - 0.0001; Chris@119: Chris@40: switch (m_colourScale) { Chris@40: Chris@40: default: Chris@40: case LinearColourScale: Chris@907: value = int(((input - min) / (max - min)) * 255.0) + 1; Chris@40: break; Chris@40: Chris@40: case MeterColourScale: Chris@210: value = AudioLevel::multiplier_to_preview Chris@210: ((input - min) / (max - min), 254) + 1; Chris@40: break; Chris@119: Chris@210: case dBSquaredColourScale: Chris@215: input = ((input - min) * (input - min)) / ((max - min) * (max - min)); Chris@907: if (input > 0.0) { Chris@907: input = 10.0 * log10(input); Chris@133: } else { Chris@133: input = thresh; Chris@133: } Chris@907: if (min > 0.0) { Chris@907: thresh = 10.0 * log10(min * min); Chris@907: if (thresh < -80.0) thresh = -80.0; Chris@119: } Chris@119: input = (input - thresh) / (-thresh); Chris@907: if (input < 0.0) input = 0.0; Chris@907: if (input > 1.0) input = 1.0; Chris@907: value = int(input * 255.0) + 1; Chris@119: break; Chris@40: Chris@215: case dBColourScale: Chris@215: //!!! experiment with normalizing the visible area this way. Chris@215: //In any case, we need to have some indication of what the dB Chris@215: //scale is relative to. Chris@215: input = (input - min) / (max - min); Chris@907: if (input > 0.0) { Chris@907: input = 10.0 * log10(input); Chris@215: } else { Chris@215: input = thresh; Chris@215: } Chris@907: if (min > 0.0) { Chris@907: thresh = 10.0 * log10(min); Chris@907: if (thresh < -80.0) thresh = -80.0; Chris@215: } Chris@215: input = (input - thresh) / (-thresh); Chris@907: if (input < 0.0) input = 0.0; Chris@907: if (input > 1.0) input = 1.0; Chris@907: value = int(input * 255.0) + 1; Chris@215: break; Chris@215: Chris@40: case PhaseColourScale: Chris@40: value = int((input * 127.0 / M_PI) + 128); Chris@40: break; Chris@0: } Chris@210: Chris@38: if (value > UCHAR_MAX) value = UCHAR_MAX; Chris@38: if (value < 0) value = 0; Chris@907: return (unsigned char)value; Chris@0: } Chris@0: Chris@905: double Chris@40: SpectrogramLayer::getEffectiveMinFrequency() const Chris@40: { Chris@907: sv_samplerate_t sr = m_model->getSampleRate(); Chris@905: double minf = double(sr) / m_fftSize; Chris@40: Chris@40: if (m_minFrequency > 0.0) { Chris@805: int minbin = int((double(m_minFrequency) * m_fftSize) / sr + 0.01); Chris@40: if (minbin < 1) minbin = 1; Chris@107: minf = minbin * sr / m_fftSize; Chris@40: } Chris@40: Chris@40: return minf; Chris@40: } Chris@40: Chris@905: double Chris@40: SpectrogramLayer::getEffectiveMaxFrequency() const Chris@40: { Chris@907: sv_samplerate_t sr = m_model->getSampleRate(); Chris@905: double maxf = double(sr) / 2; Chris@40: Chris@40: if (m_maxFrequency > 0.0) { Chris@805: int maxbin = int((double(m_maxFrequency) * m_fftSize) / sr + 0.1); Chris@107: if (maxbin > m_fftSize / 2) maxbin = m_fftSize / 2; Chris@107: maxf = maxbin * sr / m_fftSize; Chris@40: } Chris@40: Chris@40: return maxf; Chris@40: } Chris@40: Chris@0: bool Chris@918: SpectrogramLayer::getYBinRange(LayerGeometryProvider *v, int y, double &q0, double &q1) const Chris@0: { Chris@382: Profiler profiler("SpectrogramLayer::getYBinRange"); Chris@382: Chris@918: int h = v->getPaintHeight(); Chris@0: if (y < 0 || y >= h) return false; Chris@0: Chris@907: sv_samplerate_t sr = m_model->getSampleRate(); Chris@905: double minf = getEffectiveMinFrequency(); Chris@905: double maxf = getEffectiveMaxFrequency(); Chris@0: Chris@38: bool logarithmic = (m_frequencyScale == LogFrequencyScale); Chris@38: Chris@44: q0 = v->getFrequencyForY(y, minf, maxf, logarithmic); Chris@44: q1 = v->getFrequencyForY(y - 1, minf, maxf, logarithmic); Chris@38: Chris@490: // Now map these on to ("proportions of") actual bins, using raw Chris@490: // FFT size (unsmoothed) Chris@490: Chris@490: q0 = (q0 * m_fftSize) / sr; Chris@490: q1 = (q1 * m_fftSize) / sr; Chris@0: Chris@0: return true; Chris@0: } Chris@486: Chris@486: bool Chris@918: SpectrogramLayer::getSmoothedYBinRange(LayerGeometryProvider *v, int y, double &q0, double &q1) const Chris@486: { Chris@486: Profiler profiler("SpectrogramLayer::getSmoothedYBinRange"); Chris@486: Chris@918: int h = v->getPaintHeight(); Chris@486: if (y < 0 || y >= h) return false; Chris@486: Chris@907: sv_samplerate_t sr = m_model->getSampleRate(); Chris@905: double minf = getEffectiveMinFrequency(); Chris@905: double maxf = getEffectiveMaxFrequency(); Chris@486: Chris@486: bool logarithmic = (m_frequencyScale == LogFrequencyScale); Chris@486: Chris@486: q0 = v->getFrequencyForY(y, minf, maxf, logarithmic); Chris@486: q1 = v->getFrequencyForY(y - 1, minf, maxf, logarithmic); Chris@486: Chris@490: // Now map these on to ("proportions of") actual bins, using raw Chris@490: // FFT size (unsmoothed) Chris@490: Chris@490: q0 = (q0 * getFFTSize(v)) / sr; Chris@490: q1 = (q1 * getFFTSize(v)) / sr; Chris@486: Chris@486: return true; Chris@486: } Chris@38: Chris@0: bool Chris@918: SpectrogramLayer::getXBinRange(LayerGeometryProvider *v, int x, double &s0, double &s1) const Chris@0: { Chris@907: sv_frame_t modelStart = m_model->getStartFrame(); Chris@907: sv_frame_t modelEnd = m_model->getEndFrame(); Chris@0: Chris@0: // Each pixel column covers an exact range of sample frames: Chris@907: sv_frame_t f0 = v->getFrameForX(x) - modelStart; Chris@907: sv_frame_t f1 = v->getFrameForX(x + 1) - modelStart - 1; Chris@20: Chris@41: if (f1 < int(modelStart) || f0 > int(modelEnd)) { Chris@41: return false; Chris@41: } Chris@20: Chris@0: // And that range may be drawn from a possibly non-integral Chris@0: // range of spectrogram windows: Chris@0: Chris@805: int windowIncrement = getWindowIncrement(); Chris@905: s0 = double(f0) / windowIncrement; Chris@905: s1 = double(f1) / windowIncrement; Chris@0: Chris@0: return true; Chris@0: } Chris@0: Chris@0: bool Chris@918: SpectrogramLayer::getXBinSourceRange(LayerGeometryProvider *v, int x, RealTime &min, RealTime &max) const Chris@0: { Chris@905: double s0 = 0, s1 = 0; Chris@44: if (!getXBinRange(v, x, s0, s1)) return false; Chris@0: Chris@0: int s0i = int(s0 + 0.001); Chris@0: int s1i = int(s1); Chris@0: Chris@0: int windowIncrement = getWindowIncrement(); Chris@0: int w0 = s0i * windowIncrement - (m_windowSize - windowIncrement)/2; Chris@0: int w1 = s1i * windowIncrement + windowIncrement + Chris@0: (m_windowSize - windowIncrement)/2 - 1; Chris@0: Chris@0: min = RealTime::frame2RealTime(w0, m_model->getSampleRate()); Chris@0: max = RealTime::frame2RealTime(w1, m_model->getSampleRate()); Chris@0: return true; Chris@0: } Chris@0: Chris@0: bool Chris@918: SpectrogramLayer::getYBinSourceRange(LayerGeometryProvider *v, int y, double &freqMin, double &freqMax) Chris@0: const Chris@0: { Chris@905: double q0 = 0, q1 = 0; Chris@44: if (!getYBinRange(v, y, q0, q1)) return false; Chris@0: Chris@0: int q0i = int(q0 + 0.001); Chris@0: int q1i = int(q1); Chris@0: Chris@907: sv_samplerate_t sr = m_model->getSampleRate(); Chris@0: Chris@0: for (int q = q0i; q <= q1i; ++q) { Chris@121: if (q == q0i) freqMin = (sr * q) / m_fftSize; Chris@121: if (q == q1i) freqMax = (sr * (q+1)) / m_fftSize; Chris@0: } Chris@0: return true; Chris@0: } Chris@35: Chris@35: bool Chris@918: SpectrogramLayer::getAdjustedYBinSourceRange(LayerGeometryProvider *v, int x, int y, Chris@905: double &freqMin, double &freqMax, Chris@905: double &adjFreqMin, double &adjFreqMax) Chris@35: const Chris@35: { Chris@277: if (!m_model || !m_model->isOK() || !m_model->isReady()) { Chris@277: return false; Chris@277: } Chris@277: Chris@130: FFTModel *fft = getFFTModel(v); Chris@114: if (!fft) return false; Chris@110: Chris@905: double s0 = 0, s1 = 0; Chris@44: if (!getXBinRange(v, x, s0, s1)) return false; Chris@35: Chris@905: double q0 = 0, q1 = 0; Chris@44: if (!getYBinRange(v, y, q0, q1)) return false; Chris@35: Chris@35: int s0i = int(s0 + 0.001); Chris@35: int s1i = int(s1); Chris@35: Chris@35: int q0i = int(q0 + 0.001); Chris@35: int q1i = int(q1); Chris@35: Chris@907: sv_samplerate_t sr = m_model->getSampleRate(); Chris@35: Chris@35: bool haveAdj = false; Chris@35: Chris@37: bool peaksOnly = (m_binDisplay == PeakBins || Chris@37: m_binDisplay == PeakFrequencies); Chris@37: Chris@35: for (int q = q0i; q <= q1i; ++q) { Chris@35: Chris@35: for (int s = s0i; s <= s1i; ++s) { Chris@35: Chris@905: double binfreq = (double(sr) * q) / m_windowSize; Chris@35: if (q == q0i) freqMin = binfreq; Chris@35: if (q == q1i) freqMax = binfreq; Chris@37: Chris@114: if (peaksOnly && !fft->isLocalPeak(s, q)) continue; Chris@38: Chris@907: if (!fft->isOverThreshold(s, q, float(m_threshold * double(m_fftSize)/2.0))) continue; Chris@907: Chris@907: double freq = binfreq; Chris@40: Chris@114: if (s < int(fft->getWidth()) - 1) { Chris@38: Chris@277: fft->estimateStableFrequency(s, q, freq); Chris@35: Chris@38: if (!haveAdj || freq < adjFreqMin) adjFreqMin = freq; Chris@38: if (!haveAdj || freq > adjFreqMax) adjFreqMax = freq; Chris@35: Chris@35: haveAdj = true; Chris@35: } Chris@35: } Chris@35: } Chris@35: Chris@35: if (!haveAdj) { Chris@40: adjFreqMin = adjFreqMax = 0.0; Chris@35: } Chris@35: Chris@35: return haveAdj; Chris@35: } Chris@0: Chris@0: bool Chris@918: SpectrogramLayer::getXYBinSourceRange(LayerGeometryProvider *v, int x, int y, Chris@905: double &min, double &max, Chris@905: double &phaseMin, double &phaseMax) const Chris@0: { Chris@277: if (!m_model || !m_model->isOK() || !m_model->isReady()) { Chris@277: return false; Chris@277: } Chris@277: Chris@905: double q0 = 0, q1 = 0; Chris@44: if (!getYBinRange(v, y, q0, q1)) return false; Chris@0: Chris@905: double s0 = 0, s1 = 0; Chris@44: if (!getXBinRange(v, x, s0, s1)) return false; Chris@0: Chris@0: int q0i = int(q0 + 0.001); Chris@0: int q1i = int(q1); Chris@0: Chris@0: int s0i = int(s0 + 0.001); Chris@0: int s1i = int(s1); Chris@0: Chris@37: bool rv = false; Chris@37: Chris@805: int zp = getZeroPadLevel(v); Chris@122: q0i *= zp + 1; Chris@122: q1i *= zp + 1; Chris@122: Chris@130: FFTModel *fft = getFFTModel(v); Chris@0: Chris@114: if (fft) { Chris@114: Chris@114: int cw = fft->getWidth(); Chris@114: int ch = fft->getHeight(); Chris@0: Chris@110: min = 0.0; Chris@110: max = 0.0; Chris@110: phaseMin = 0.0; Chris@110: phaseMax = 0.0; Chris@110: bool have = false; Chris@0: Chris@110: for (int q = q0i; q <= q1i; ++q) { Chris@110: for (int s = s0i; s <= s1i; ++s) { Chris@110: if (s >= 0 && q >= 0 && s < cw && q < ch) { Chris@117: Chris@905: double value; Chris@38: Chris@114: value = fft->getPhaseAt(s, q); Chris@110: if (!have || value < phaseMin) { phaseMin = value; } Chris@110: if (!have || value > phaseMax) { phaseMax = value; } Chris@91: Chris@907: value = fft->getMagnitudeAt(s, q) / (m_fftSize/2.0); Chris@110: if (!have || value < min) { min = value; } Chris@110: if (!have || value > max) { max = value; } Chris@110: Chris@110: have = true; Chris@110: } Chris@110: } Chris@110: } Chris@110: Chris@110: if (have) { Chris@110: rv = true; Chris@110: } Chris@0: } Chris@0: Chris@37: return rv; Chris@0: } Chris@0: Chris@805: int Chris@918: SpectrogramLayer::getZeroPadLevel(const LayerGeometryProvider *v) const Chris@114: { Chris@114: //!!! tidy all this stuff Chris@114: Chris@114: if (m_binDisplay != AllBins) return 0; Chris@221: Chris@221: Preferences::SpectrogramSmoothing smoothing = Chris@221: Preferences::getInstance()->getSpectrogramSmoothing(); Chris@221: Chris@221: if (smoothing == Preferences::NoSpectrogramSmoothing || Chris@221: smoothing == Preferences::SpectrogramInterpolated) return 0; Chris@221: Chris@114: if (m_frequencyScale == LogFrequencyScale) return 3; Chris@114: Chris@907: sv_samplerate_t sr = m_model->getSampleRate(); Chris@114: Chris@805: int maxbin = m_fftSize / 2; Chris@114: if (m_maxFrequency > 0) { Chris@184: maxbin = int((double(m_maxFrequency) * m_fftSize) / sr + 0.1); Chris@184: if (maxbin > m_fftSize / 2) maxbin = m_fftSize / 2; Chris@114: } Chris@114: Chris@805: int minbin = 1; Chris@114: if (m_minFrequency > 0) { Chris@114: minbin = int((double(m_minFrequency) * m_fftSize) / sr + 0.1); Chris@114: if (minbin < 1) minbin = 1; Chris@184: if (minbin >= maxbin) minbin = maxbin - 1; Chris@114: } Chris@114: Chris@905: double perPixel = Chris@918: double(v->getPaintHeight()) / Chris@905: double((maxbin - minbin) / (m_zeroPadLevel + 1)); Chris@118: Chris@118: if (perPixel > 2.8) { Chris@118: return 3; // 4x oversampling Chris@118: } else if (perPixel > 1.5) { Chris@118: return 1; // 2x Chris@114: } else { Chris@118: return 0; // 1x Chris@114: } Chris@114: } Chris@114: Chris@805: int Chris@918: SpectrogramLayer::getFFTSize(const LayerGeometryProvider *v) const Chris@114: { Chris@114: return m_fftSize * (getZeroPadLevel(v) + 1); Chris@114: } Chris@114: Chris@130: FFTModel * Chris@918: SpectrogramLayer::getFFTModel(const LayerGeometryProvider *v) const Chris@114: { Chris@114: if (!m_model) return 0; Chris@114: Chris@805: int fftSize = getFFTSize(v); Chris@114: Chris@920: const View *view = v->getView(); Chris@920: Chris@1030: if (m_fftModels.find(view->getId()) != m_fftModels.end()) { Chris@1030: if (m_fftModels[view->getId()] == 0) { Chris@184: #ifdef DEBUG_SPECTROGRAM_REPAINT Chris@985: cerr << "SpectrogramLayer::getFFTModel(" << v << "): Found null model" << endl; Chris@184: #endif Chris@184: return 0; Chris@184: } Chris@1030: if (m_fftModels[view->getId()]->getHeight() != fftSize / 2 + 1) { Chris@184: #ifdef DEBUG_SPECTROGRAM_REPAINT Chris@1030: cerr << "SpectrogramLayer::getFFTModel(" << v << "): Found a model with the wrong height (" << m_fftModels[view->getId()]->getHeight() << ", wanted " << (fftSize / 2 + 1) << ")" << endl; Chris@184: #endif Chris@1030: delete m_fftModels[view->getId()]; Chris@1030: m_fftModels.erase(view->getId()); Chris@1030: delete m_peakCaches[view->getId()]; Chris@1030: m_peakCaches.erase(view->getId()); Chris@184: } else { Chris@184: #ifdef DEBUG_SPECTROGRAM_REPAINT Chris@1030: cerr << "SpectrogramLayer::getFFTModel(" << v << "): Found a good model of height " << m_fftModels[view->getId()]->getHeight() << endl; Chris@184: #endif Chris@1030: return m_fftModels[view->getId()]; Chris@114: } Chris@114: } Chris@114: Chris@1030: if (m_fftModels.find(view->getId()) == m_fftModels.end()) { Chris@169: Chris@169: FFTModel *model = new FFTModel(m_model, Chris@169: m_channel, Chris@169: m_windowType, Chris@169: m_windowSize, Chris@169: getWindowIncrement(), Chris@975: fftSize); Chris@169: Chris@178: if (!model->isOK()) { Chris@178: QMessageBox::critical Chris@178: (0, tr("FFT cache failed"), Chris@178: tr("Failed to create the FFT model for this spectrogram.\n" Chris@178: "There may be insufficient memory or disc space to continue.")); Chris@178: delete model; Chris@1030: m_fftModels[view->getId()] = 0; Chris@178: return 0; Chris@178: } Chris@178: Chris@193: if (!m_sliceableModel) { Chris@248: #ifdef DEBUG_SPECTROGRAM Chris@682: cerr << "SpectrogramLayer: emitting sliceableModelReplaced(0, " << model << ")" << endl; Chris@248: #endif Chris@193: ((SpectrogramLayer *)this)->sliceableModelReplaced(0, model); Chris@193: m_sliceableModel = model; Chris@193: } Chris@193: Chris@1030: m_fftModels[view->getId()] = model; Chris@114: } Chris@114: Chris@1030: return m_fftModels[view->getId()]; Chris@114: } Chris@114: Chris@484: Dense3DModelPeakCache * Chris@918: SpectrogramLayer::getPeakCache(const LayerGeometryProvider *v) const Chris@484: { Chris@920: const View *view = v->getView(); Chris@1030: if (!m_peakCaches[view->getId()]) { Chris@484: FFTModel *f = getFFTModel(v); Chris@484: if (!f) return 0; Chris@1054: m_peakCaches[view->getId()] = Chris@1054: new Dense3DModelPeakCache(f, m_peakCacheDivisor); Chris@484: } Chris@1030: return m_peakCaches[view->getId()]; Chris@484: } Chris@484: Chris@193: const Model * Chris@193: SpectrogramLayer::getSliceableModel() const Chris@193: { Chris@193: if (m_sliceableModel) return m_sliceableModel; Chris@193: if (m_fftModels.empty()) return 0; Chris@985: m_sliceableModel = m_fftModels.begin()->second; Chris@193: return m_sliceableModel; Chris@193: } Chris@193: Chris@114: void Chris@130: SpectrogramLayer::invalidateFFTModels() Chris@114: { Chris@1044: #ifdef DEBUG_SPECTROGRAM Chris@1044: cerr << "SpectrogramLayer::invalidateFFTModels called" << endl; Chris@1044: #endif Chris@130: for (ViewFFTMap::iterator i = m_fftModels.begin(); Chris@130: i != m_fftModels.end(); ++i) { Chris@985: delete i->second; Chris@114: } Chris@486: for (PeakCacheMap::iterator i = m_peakCaches.begin(); Chris@486: i != m_peakCaches.end(); ++i) { Chris@486: delete i->second; Chris@486: } Chris@114: Chris@130: m_fftModels.clear(); Chris@486: m_peakCaches.clear(); Chris@193: Chris@193: if (m_sliceableModel) { Chris@682: cerr << "SpectrogramLayer: emitting sliceableModelReplaced(" << m_sliceableModel << ", 0)" << endl; Chris@193: emit sliceableModelReplaced(m_sliceableModel, 0); Chris@193: m_sliceableModel = 0; Chris@193: } Chris@114: } Chris@114: Chris@0: void Chris@119: SpectrogramLayer::invalidateMagnitudes() Chris@119: { Chris@1044: #ifdef DEBUG_SPECTROGRAM Chris@1044: cerr << "SpectrogramLayer::invalidateMagnitudes called" << endl; Chris@1044: #endif Chris@119: m_viewMags.clear(); Chris@1025: for (vector::iterator i = m_columnMags.begin(); Chris@119: i != m_columnMags.end(); ++i) { Chris@119: *i = MagnitudeRange(); Chris@119: } Chris@119: } Chris@119: Chris@119: bool Chris@918: SpectrogramLayer::updateViewMagnitudes(LayerGeometryProvider *v) const Chris@119: { Chris@119: MagnitudeRange mag; Chris@119: Chris@918: int x0 = 0, x1 = v->getPaintWidth(); Chris@905: double s00 = 0, s01 = 0, s10 = 0, s11 = 0; Chris@119: Chris@203: if (!getXBinRange(v, x0, s00, s01)) { Chris@907: s00 = s01 = double(m_model->getStartFrame()) / getWindowIncrement(); Chris@203: } Chris@203: Chris@203: if (!getXBinRange(v, x1, s10, s11)) { Chris@907: s10 = s11 = double(m_model->getEndFrame()) / getWindowIncrement(); Chris@203: } Chris@119: Chris@1025: int s0 = int(min(s00, s10) + 0.0001); Chris@1025: int s1 = int(max(s01, s11) + 0.0001); Chris@203: Chris@587: // SVDEBUG << "SpectrogramLayer::updateViewMagnitudes: x0 = " << x0 << ", x1 = " << x1 << ", s00 = " << s00 << ", s11 = " << s11 << " s0 = " << s0 << ", s1 = " << s1 << endl; Chris@119: Chris@248: if (int(m_columnMags.size()) <= s1) { Chris@119: m_columnMags.resize(s1 + 1); Chris@119: } Chris@119: Chris@119: for (int s = s0; s <= s1; ++s) { Chris@119: if (m_columnMags[s].isSet()) { Chris@119: mag.sample(m_columnMags[s]); Chris@119: } Chris@119: } Chris@119: Chris@184: #ifdef DEBUG_SPECTROGRAM_REPAINT Chris@985: cerr << "SpectrogramLayer::updateViewMagnitudes returning from cols " Chris@1044: << s0 << " -> " << s1 << " inclusive" << endl; Chris@1044: cerr << "SpectrogramLayer::updateViewMagnitudes: for view id " << v->getId() Chris@1044: << ": min is " << mag.getMin() << ", max is " << mag.getMax() << endl; Chris@184: #endif Chris@119: Chris@119: if (!mag.isSet()) return false; Chris@1030: if (mag == m_viewMags[v->getId()]) return false; Chris@1030: m_viewMags[v->getId()] = mag; Chris@119: return true; Chris@119: } Chris@119: Chris@119: void Chris@389: SpectrogramLayer::setSynchronousPainting(bool synchronous) Chris@389: { Chris@389: m_synchronous = synchronous; Chris@389: } Chris@389: Chris@1030: ScrollableImageCache & Chris@1030: SpectrogramLayer::getImageCacheReference(const LayerGeometryProvider *view) const Chris@1030: { Chris@1030: if (m_imageCaches.find(view->getId()) == m_imageCaches.end()) { Chris@1030: m_imageCaches[view->getId()] = ScrollableImageCache(view); Chris@1030: } Chris@1030: return m_imageCaches.at(view->getId()); Chris@1030: } Chris@1030: Chris@389: void Chris@916: SpectrogramLayer::paint(LayerGeometryProvider *v, QPainter &paint, QRect rect) const Chris@0: { Chris@334: Profiler profiler("SpectrogramLayer::paint", false); Chris@334: Chris@0: #ifdef DEBUG_SPECTROGRAM_REPAINT Chris@1026: cerr << "SpectrogramLayer::paint() entering: m_model is " << m_model << ", zoom level is " << v->getZoomLevel() << endl; Chris@95: Chris@1026: cerr << "SpectrogramLayer::paint(): rect is " << rect.x() << "," << rect.y() << " " << rect.width() << "x" << rect.height() << endl; Chris@0: #endif Chris@95: Chris@907: sv_frame_t startFrame = v->getStartFrame(); Chris@44: Chris@0: if (!m_model || !m_model->isOK() || !m_model->isReady()) { Chris@0: return; Chris@0: } Chris@0: Chris@47: if (isLayerDormant(v)) { Chris@587: SVDEBUG << "SpectrogramLayer::paint(): Layer is dormant, making it undormant again" << endl; Chris@29: } Chris@29: Chris@48: // Need to do this even if !isLayerDormant, as that could mean v Chris@48: // is not in the dormancy map at all -- we need it to be present Chris@48: // and accountable for when determining whether we need the cache Chris@48: // in the cache-fill thread above. Chris@806: //!!! no inter use cache-fill thread Chris@131: const_cast(this)->Layer::setLayerDormant(v, false); Chris@48: Chris@805: int fftSize = getFFTSize(v); Chris@920: Chris@920: const View *view = v->getView(); Chris@1030: ScrollableImageCache &cache = getImageCacheReference(view); Chris@95: Chris@95: #ifdef DEBUG_SPECTROGRAM_REPAINT Chris@1030: cerr << "SpectrogramLayer::paint(): image cache valid area from " << cache.getValidLeft() << " width " << cache.getValidWidth() << ", height " << cache.getSize().height() << endl; Chris@1030: if (rect.x() + rect.width() + 1 < cache.getValidLeft() || Chris@1030: rect.x() > cache.getValidRight()) { Chris@1030: cerr << "SpectrogramLayer: NOTE: requested rect is not contiguous with cache valid area" << endl; Chris@1030: } Chris@95: #endif Chris@95: Chris@44: int zoomLevel = v->getZoomLevel(); Chris@0: Chris@1030: int x0 = v->getXForViewX(rect.x()); Chris@1030: int x1 = v->getXForViewX(rect.x() + rect.width()); Chris@1030: if (x0 < 0) x0 = 0; Chris@1030: if (x1 > v->getPaintWidth()) x1 = v->getPaintWidth(); Chris@1022: Chris@1022: if (updateViewMagnitudes(v)) { Chris@1022: #ifdef DEBUG_SPECTROGRAM_REPAINT Chris@1030: cerr << "SpectrogramLayer: magnitude range changed to [" << m_viewMags[v->getId()].getMin() << "->" << m_viewMags[v->getId()].getMax() << "]" << endl; Chris@1022: #endif Chris@1022: if (m_normalization == NormalizeVisibleArea) { Chris@1030: cache.invalidate(); Chris@1022: } Chris@1022: } Chris@1030: Chris@1030: if (cache.getZoomLevel() != zoomLevel || Chris@1030: cache.getSize() != v->getPaintSize()) { Chris@1031: #ifdef DEBUG_SPECTROGRAM_REPAINT Chris@1031: cerr << "SpectrogramLayer: resizing image cache from " Chris@1031: << cache.getSize().width() << "x" << cache.getSize().height() Chris@1031: << " to " Chris@1031: << v->getPaintSize().width() << "x" << v->getPaintSize().height() Chris@1031: << " and updating zoom level from " << cache.getZoomLevel() Chris@1031: << " to " << zoomLevel Chris@1031: << endl; Chris@1031: #endif Chris@1030: cache.resize(v->getPaintSize()); Chris@1031: cache.setZoomLevel(zoomLevel); Chris@1031: cache.setStartFrame(startFrame); Chris@1030: } Chris@1023: Chris@1030: if (cache.isValid()) { Chris@482: Chris@1030: if (v->getXForFrame(cache.getStartFrame()) == Chris@1030: v->getXForFrame(startFrame) && Chris@1030: cache.getValidLeft() <= x0 && Chris@1030: cache.getValidRight() >= x1) { Chris@1022: Chris@0: #ifdef DEBUG_SPECTROGRAM_REPAINT Chris@1030: cerr << "SpectrogramLayer: image cache hit!" << endl; Chris@0: #endif Chris@0: Chris@1030: paint.drawImage(rect, cache.getImage(), rect); Chris@1030: Chris@1030: illuminateLocalFeatures(v, paint); Chris@1030: return; Chris@1030: Chris@1030: } else { Chris@1030: Chris@1030: // cache doesn't begin at the right frame or doesn't Chris@1030: // contain the complete view, but might be scrollable or Chris@1030: // partially usable Chris@1022: Chris@0: #ifdef DEBUG_SPECTROGRAM_REPAINT Chris@1030: cerr << "SpectrogramLayer: scrolling the image cache if applicable" << endl; Chris@0: #endif Chris@0: Chris@1030: cache.scrollTo(startFrame); Chris@1030: Chris@0: #ifdef DEBUG_SPECTROGRAM_REPAINT Chris@1031: cerr << "SpectrogramLayer: after scrolling, cache valid from " Chris@1030: << cache.getValidLeft() << " width " << cache.getValidWidth() Chris@1030: << endl; Chris@0: #endif Chris@1030: } Chris@0: } Chris@95: Chris@1030: bool rightToLeft = false; Chris@1030: Chris@1030: if (!cache.isValid()) { Chris@1028: if (!m_synchronous) { Chris@1028: // When rendering the whole thing, start from somewhere near Chris@1028: // the middle so that the region of interest appears first Chris@1038: Chris@1038: //!!! (perhaps we should have some cunning test to avoid Chris@1038: //!!! doing this if past repaints have appeared fast Chris@1038: //!!! enough to do the whole width in one shot) Chris@1030: if (x0 == 0 && x1 == v->getPaintWidth()) { Chris@1037: x0 = int(x1 * 0.3); Chris@1030: } Chris@1028: } Chris@1030: } else { Chris@1030: // When rendering only a part of the cache, we need to make Chris@1030: // sure that the part we're rendering is adjacent to (or Chris@1030: // overlapping) a valid area of cache, if we have one. The Chris@1030: // alternative is to ditch the valid area of cache and render Chris@1030: // only the requested area, but that's risky because this can Chris@1030: // happen when just waving the pointer over a small part of Chris@1030: // the view -- if we lose the partly-built cache every time Chris@1030: // the user does that, we'll never finish building it. Chris@1030: int left = x0; Chris@1030: int width = x1 - x0; Chris@1030: bool isLeftOfValidArea = false; Chris@1031: cache.adjustToTouchValidArea(left, width, isLeftOfValidArea); Chris@1030: x0 = left; Chris@1030: x1 = x0 + width; Chris@1030: Chris@1030: // That call also told us whether we should be painting Chris@1030: // sub-regions of our target region in right-to-left order in Chris@1030: // order to ensure contiguity Chris@1030: rightToLeft = isLeftOfValidArea; Chris@95: } Chris@1030: Chris@224: // We always paint the full height when refreshing the cache. Chris@224: // Smaller heights can be used when painting direct from cache Chris@224: // (further up in this function), but we want to ensure the cache Chris@224: // is coherent without having to worry about vertical matching of Chris@224: // required and valid areas as well as horizontal. Chris@918: int h = v->getPaintHeight(); Chris@1025: Chris@1024: int repaintWidth = x1 - x0; Chris@0: Chris@95: #ifdef DEBUG_SPECTROGRAM_REPAINT Chris@1026: cerr << "SpectrogramLayer: x0 " << x0 << ", x1 " << x1 Chris@1026: << ", repaintWidth " << repaintWidth << ", h " << h Chris@1030: << ", rightToLeft " << rightToLeft << endl; Chris@95: #endif Chris@95: Chris@907: sv_samplerate_t sr = m_model->getSampleRate(); Chris@122: Chris@122: // Set minFreq and maxFreq to the frequency extents of the possibly Chris@122: // zero-padded visible bin range, and displayMinFreq and displayMaxFreq Chris@122: // to the actual scale frequency extents (presumably not zero padded). Chris@253: Chris@253: // If we are zero padding, we want to use the zero-padded Chris@253: // equivalents of the bins that we would be using if not zero Chris@253: // padded, to avoid spaces at the top and bottom of the display. Chris@253: Chris@253: // Note fftSize is the actual zero-padded fft size, m_fftSize the Chris@253: // nominal fft size. Chris@35: Chris@805: int maxbin = m_fftSize / 2; Chris@35: if (m_maxFrequency > 0) { Chris@253: maxbin = int((double(m_maxFrequency) * m_fftSize) / sr + 0.001); Chris@253: if (maxbin > m_fftSize / 2) maxbin = m_fftSize / 2; Chris@35: } Chris@111: Chris@805: int minbin = 1; Chris@37: if (m_minFrequency > 0) { Chris@253: minbin = int((double(m_minFrequency) * m_fftSize) / sr + 0.001); Chris@682: // cerr << "m_minFrequency = " << m_minFrequency << " -> minbin = " << minbin << endl; Chris@40: if (minbin < 1) minbin = 1; Chris@184: if (minbin >= maxbin) minbin = maxbin - 1; Chris@37: } Chris@37: Chris@253: int zpl = getZeroPadLevel(v) + 1; Chris@253: minbin = minbin * zpl; Chris@253: maxbin = (maxbin + 1) * zpl - 1; Chris@253: Chris@905: double minFreq = (double(minbin) * sr) / fftSize; Chris@905: double maxFreq = (double(maxbin) * sr) / fftSize; Chris@905: Chris@905: double displayMinFreq = minFreq; Chris@905: double displayMaxFreq = maxFreq; Chris@122: Chris@122: if (fftSize != m_fftSize) { Chris@122: displayMinFreq = getEffectiveMinFrequency(); Chris@122: displayMaxFreq = getEffectiveMaxFrequency(); Chris@122: } Chris@122: Chris@682: // cerr << "(giving actual minFreq " << minFreq << " and display minFreq " << displayMinFreq << ")" << endl; Chris@253: Chris@518: int increment = getWindowIncrement(); Chris@40: Chris@40: bool logarithmic = (m_frequencyScale == LogFrequencyScale); Chris@1026: Chris@1030: MagnitudeRange overallMag = m_viewMags[v->getId()]; Chris@119: bool overallMagChanged = false; Chris@119: Chris@137: #ifdef DEBUG_SPECTROGRAM_REPAINT Chris@1026: cerr << "SpectrogramLayer: " << ((double(v->getFrameForX(1) - v->getFrameForX(0))) / increment) << " bin(s) per pixel" << endl; Chris@137: #endif Chris@137: Chris@1024: if (repaintWidth == 0) { Chris@1024: SVDEBUG << "*** NOTE: repaintWidth == 0" << endl; Chris@331: } Chris@331: Chris@382: Profiler outerprof("SpectrogramLayer::paint: all cols"); Chris@382: Chris@481: // The draw buffer contains a fragment at either our pixel Chris@481: // resolution (if there is more than one time-bin per pixel) or Chris@481: // time-bin resolution (if a time-bin spans more than one pixel). Chris@481: // We need to ensure that it starts and ends at points where a Chris@481: // time-bin boundary occurs at an exact pixel boundary, and with a Chris@481: // certain amount of overlap across existing pixels so that we can Chris@481: // scale and draw from it without smoothing errors at the edges. Chris@481: Chris@481: // If (getFrameForX(x) / increment) * increment == Chris@481: // getFrameForX(x), then x is a time-bin boundary. We want two Chris@481: // such boundaries at either side of the draw buffer -- one which Chris@481: // we draw up to, and one which we subsequently crop at. Chris@481: Chris@1039: bool bufferIsBinResolution = false; Chris@1039: if (increment > zoomLevel) bufferIsBinResolution = true; Chris@481: Chris@907: sv_frame_t leftBoundaryFrame = -1, leftCropFrame = -1; Chris@907: sv_frame_t rightBoundaryFrame = -1, rightCropFrame = -1; Chris@481: Chris@481: int bufwid; Chris@481: Chris@1039: if (bufferIsBinResolution) { Chris@481: Chris@482: for (int x = x0; ; --x) { Chris@907: sv_frame_t f = v->getFrameForX(x); Chris@481: if ((f / increment) * increment == f) { Chris@481: if (leftCropFrame == -1) leftCropFrame = f; Chris@1024: else if (x < x0 - 2) { Chris@1024: leftBoundaryFrame = f; Chris@1024: break; Chris@1024: } Chris@481: } Chris@481: } Chris@1024: for (int x = x0 + repaintWidth; ; ++x) { Chris@907: sv_frame_t f = v->getFrameForX(x); Chris@481: if ((f / increment) * increment == f) { Chris@481: if (rightCropFrame == -1) rightCropFrame = f; Chris@1024: else if (x > x0 + repaintWidth + 2) { Chris@1024: rightBoundaryFrame = f; Chris@1024: break; Chris@1024: } Chris@481: } Chris@481: } Chris@485: #ifdef DEBUG_SPECTROGRAM_REPAINT Chris@481: cerr << "Left: crop: " << leftCropFrame << " (bin " << leftCropFrame/increment << "); boundary: " << leftBoundaryFrame << " (bin " << leftBoundaryFrame/increment << ")" << endl; Chris@481: cerr << "Right: crop: " << rightCropFrame << " (bin " << rightCropFrame/increment << "); boundary: " << rightBoundaryFrame << " (bin " << rightBoundaryFrame/increment << ")" << endl; Chris@485: #endif Chris@481: Chris@907: bufwid = int((rightBoundaryFrame - leftBoundaryFrame) / increment); Chris@481: Chris@481: } else { Chris@481: Chris@1024: bufwid = repaintWidth; Chris@481: } Chris@481: Chris@907: vector binforx(bufwid); Chris@907: vector binfory(h); Chris@907: Chris@484: bool usePeaksCache = false; Chris@484: Chris@1039: if (bufferIsBinResolution) { Chris@481: for (int x = 0; x < bufwid; ++x) { Chris@907: binforx[x] = int(leftBoundaryFrame / increment) + x; Chris@481: } Chris@481: m_drawBuffer = QImage(bufwid, h, QImage::Format_Indexed8); Chris@481: } else { Chris@481: for (int x = 0; x < bufwid; ++x) { Chris@905: double s0 = 0, s1 = 0; Chris@481: if (getXBinRange(v, x + x0, s0, s1)) { Chris@481: binforx[x] = int(s0 + 0.0001); Chris@481: } else { Chris@487: binforx[x] = -1; //??? Chris@481: } Chris@481: } Chris@1031: if (m_drawBuffer.width() < bufwid || m_drawBuffer.height() != h) { Chris@481: m_drawBuffer = QImage(bufwid, h, QImage::Format_Indexed8); Chris@480: } Chris@1054: usePeaksCache = (increment * m_peakCacheDivisor) < zoomLevel; Chris@487: if (m_colourScale == PhaseColourScale) usePeaksCache = false; Chris@480: } Chris@481: Chris@481: for (int pixel = 0; pixel < 256; ++pixel) { Chris@907: m_drawBuffer.setColor((unsigned char)pixel, Chris@907: m_palette.getColour((unsigned char)pixel).rgb()); Chris@481: } Chris@481: Chris@481: m_drawBuffer.fill(0); Chris@1024: int attainedBufwid = bufwid; Chris@1039: Chris@1039: double softTimeLimit; Chris@1039: Chris@1039: if (m_synchronous) { Chris@1039: Chris@1039: // must paint the whole thing for synchronous mode, so give Chris@1039: // "no timeout" Chris@1039: softTimeLimit = 0.0; Chris@1039: Chris@1039: } else if (bufferIsBinResolution) { Chris@1039: Chris@1039: // calculating boundaries later will be too fiddly for partial Chris@1039: // paints, and painting should be fast anyway when this is the Chris@1039: // case because it means we're well zoomed in Chris@1039: softTimeLimit = 0.0; Chris@1039: Chris@1039: } else { Chris@1039: Chris@1039: // neither limitation applies, so use a short soft limit Chris@1039: Chris@1039: if (m_binDisplay == PeakFrequencies) { Chris@1039: softTimeLimit = 0.15; Chris@1039: } else { Chris@1039: softTimeLimit = 0.1; Chris@1039: } Chris@1039: } Chris@1039: Chris@488: if (m_binDisplay != PeakFrequencies) { Chris@488: Chris@488: for (int y = 0; y < h; ++y) { Chris@905: double q0 = 0, q1 = 0; Chris@488: if (!getSmoothedYBinRange(v, h-y-1, q0, q1)) { Chris@488: binfory[y] = -1; Chris@488: } else { Chris@490: binfory[y] = q0; Chris@488: } Chris@480: } Chris@488: Chris@1024: attainedBufwid = Chris@1026: paintDrawBuffer(v, bufwid, h, binforx, binfory, Chris@1026: usePeaksCache, Chris@1026: overallMag, overallMagChanged, Chris@1039: rightToLeft, Chris@1039: softTimeLimit); Chris@488: Chris@488: } else { Chris@488: Chris@1024: attainedBufwid = Chris@1024: paintDrawBufferPeakFrequencies(v, bufwid, h, binforx, Chris@1024: minbin, maxbin, Chris@1024: displayMinFreq, displayMaxFreq, Chris@1024: logarithmic, Chris@1026: overallMag, overallMagChanged, Chris@1039: rightToLeft, Chris@1039: softTimeLimit); Chris@480: } Chris@481: Chris@1024: int failedToRepaint = bufwid - attainedBufwid; Chris@1031: Chris@1031: int paintedLeft = x0; Chris@1031: int paintedWidth = x1 - x0; Chris@1031: Chris@1025: if (failedToRepaint > 0) { Chris@1031: Chris@1025: #ifdef DEBUG_SPECTROGRAM_REPAINT Chris@1026: cerr << "SpectrogramLayer::paint(): Failed to repaint " << failedToRepaint << " of " << bufwid Chris@1029: << " columns in time (so managed to repaint " << bufwid - failedToRepaint << ")" << endl; Chris@1025: #endif Chris@1031: Chris@1031: if (rightToLeft) { Chris@1031: paintedLeft += failedToRepaint; Chris@1031: } Chris@1031: Chris@1031: paintedWidth -= failedToRepaint; Chris@1031: Chris@1031: if (paintedWidth < 0) { Chris@1031: paintedWidth = 0; Chris@1031: } Chris@1031: Chris@1025: } else if (failedToRepaint < 0) { Chris@1024: cerr << "WARNING: failedToRepaint < 0 (= " << failedToRepaint << ")" Chris@1024: << endl; Chris@1024: failedToRepaint = 0; Chris@1024: } Chris@1031: Chris@119: if (overallMagChanged) { Chris@1030: m_viewMags[v->getId()] = overallMag; Chris@209: #ifdef DEBUG_SPECTROGRAM_REPAINT Chris@1030: cerr << "SpectrogramLayer: Overall mag is now [" << m_viewMags[v->getId()].getMin() << "->" << m_viewMags[v->getId()].getMax() << "] - will be updating" << endl; Chris@209: #endif Chris@119: } Chris@119: Chris@382: outerprof.end(); Chris@382: Chris@382: Profiler profiler2("SpectrogramLayer::paint: draw image"); Chris@137: Chris@1031: if (paintedWidth > 0) { Chris@1024: Chris@224: #ifdef DEBUG_SPECTROGRAM_REPAINT Chris@1031: cerr << "SpectrogramLayer: Copying " << paintedWidth << "x" << h Chris@1031: << " from draw buffer at " << paintedLeft - x0 << "," << 0 Chris@1031: << " to " << paintedWidth << "x" << h << " on cache at " Chris@585: << x0 << "," << 0 << endl; Chris@224: #endif Chris@224: Chris@1039: if (bufferIsBinResolution) { Chris@1031: Chris@481: int scaledLeft = v->getXForFrame(leftBoundaryFrame); Chris@481: int scaledRight = v->getXForFrame(rightBoundaryFrame); Chris@1031: Chris@485: #ifdef DEBUG_SPECTROGRAM_REPAINT Chris@1026: cerr << "SpectrogramLayer: Rescaling image from " << bufwid Chris@481: << "x" << h << " to " Chris@481: << scaledRight-scaledLeft << "x" << h << endl; Chris@485: #endif Chris@1031: Chris@490: Preferences::SpectrogramXSmoothing xsmoothing = Chris@490: Preferences::getInstance()->getSpectrogramXSmoothing(); Chris@1026: Chris@481: QImage scaled = m_drawBuffer.scaled Chris@481: (scaledRight - scaledLeft, h, Chris@490: Qt::IgnoreAspectRatio, Chris@490: ((xsmoothing == Preferences::SpectrogramXInterpolated) ? Chris@490: Qt::SmoothTransformation : Qt::FastTransformation)); Chris@1026: Chris@481: int scaledLeftCrop = v->getXForFrame(leftCropFrame); Chris@481: int scaledRightCrop = v->getXForFrame(rightCropFrame); Chris@1031: Chris@485: #ifdef DEBUG_SPECTROGRAM_REPAINT Chris@1026: cerr << "SpectrogramLayer: Drawing image region of width " << scaledRightCrop - scaledLeftCrop << " to " Chris@481: << scaledLeftCrop << " from " << scaledLeftCrop - scaledLeft << endl; Chris@485: #endif Chris@1030: Chris@1040: int targetLeft = scaledLeftCrop; Chris@1040: if (targetLeft < 0) { Chris@1040: targetLeft = 0; Chris@1040: } Chris@1040: Chris@1040: int targetWidth = scaledRightCrop - targetLeft; Chris@1040: if (targetLeft + targetWidth > cache.getSize().width()) { Chris@1040: targetWidth = cache.getSize().width() - targetLeft; Chris@1040: } Chris@1031: Chris@1040: int sourceLeft = targetLeft - scaledLeft; Chris@1040: if (sourceLeft < 0) { Chris@1040: sourceLeft = 0; Chris@1040: } Chris@1040: Chris@1040: int sourceWidth = targetWidth; Chris@1040: Chris@1040: if (targetWidth > 0) { Chris@1040: cache.drawImage Chris@1040: (targetLeft, Chris@1040: targetWidth, Chris@1040: scaled, Chris@1040: sourceLeft, Chris@1040: sourceWidth); Chris@1040: } Chris@1024: Chris@481: } else { Chris@1024: Chris@1031: cache.drawImage(paintedLeft, paintedWidth, Chris@1030: m_drawBuffer, Chris@1031: paintedLeft - x0, paintedWidth); Chris@481: } Chris@331: } Chris@331: Chris@1026: #ifdef DEBUG_SPECTROGRAM_REPAINT Chris@1030: cerr << "SpectrogramLayer: Cache valid area now from " << cache.getValidLeft() Chris@1030: << " width " << cache.getValidWidth() << ", height " Chris@1030: << cache.getSize().height() << endl; Chris@1026: #endif Chris@1030: Chris@1030: QRect pr = rect & cache.getValidArea(); Chris@337: Chris@337: #ifdef DEBUG_SPECTROGRAM_REPAINT Chris@1026: cerr << "SpectrogramLayer: Copying " << pr.width() << "x" << pr.height() Chris@337: << " from cache at " << pr.x() << "," << pr.y() Chris@585: << " to window" << endl; Chris@337: #endif Chris@337: Chris@1030: paint.drawImage(pr.x(), pr.y(), cache.getImage(), Chris@479: pr.x(), pr.y(), pr.width(), pr.height()); Chris@337: Chris@389: if (!m_synchronous) { Chris@389: Chris@862: if ((m_normalization != NormalizeVisibleArea) || !overallMagChanged) { Chris@1031: Chris@1031: QRect areaLeft(0, 0, cache.getValidLeft(), h); Chris@1031: QRect areaRight(cache.getValidRight(), 0, Chris@1031: cache.getSize().width() - cache.getValidRight(), h); Chris@1031: Chris@1031: bool haveSpaceLeft = (areaLeft.width() > 0); Chris@1031: bool haveSpaceRight = (areaRight.width() > 0); Chris@1031: Chris@1031: bool updateLeft = haveSpaceLeft; Chris@1031: bool updateRight = haveSpaceRight; Chris@1031: Chris@1031: if (updateLeft && updateRight) { Chris@1031: if (rightToLeft) { Chris@1031: // we just did something adjoining the cache on Chris@1031: // its left side, so now do something on its right Chris@1031: updateLeft = false; Chris@1031: } else { Chris@1031: updateRight = false; Chris@1031: } Chris@389: } Chris@389: Chris@1031: if (updateLeft) { Chris@1031: #ifdef DEBUG_SPECTROGRAM_REPAINT Chris@1031: cerr << "SpectrogramLayer::paint() updating left (" Chris@1031: << areaLeft.x() << ", " Chris@1031: << areaLeft.width() << ")" << endl; Chris@1031: #endif Chris@1031: v->updatePaintRect(areaLeft); Chris@1031: } Chris@1031: Chris@1031: if (updateRight) { Chris@389: #ifdef DEBUG_SPECTROGRAM_REPAINT Chris@985: cerr << "SpectrogramLayer::paint() updating right (" Chris@1031: << areaRight.x() << ", " Chris@1031: << areaRight.width() << ")" << endl; Chris@389: #endif Chris@1031: v->updatePaintRect(areaRight); Chris@389: } Chris@1031: Chris@389: } else { Chris@389: // overallMagChanged Chris@682: cerr << "\noverallMagChanged - updating all\n" << endl; Chris@1030: cache.invalidate(); Chris@1030: v->updatePaintRect(v->getPaintRect()); Chris@119: } Chris@95: } Chris@0: Chris@121: illuminateLocalFeatures(v, paint); Chris@120: Chris@0: #ifdef DEBUG_SPECTROGRAM_REPAINT Chris@985: cerr << "SpectrogramLayer::paint() returning" << endl; Chris@0: #endif Chris@0: } Chris@0: Chris@1024: int Chris@918: SpectrogramLayer::paintDrawBufferPeakFrequencies(LayerGeometryProvider *v, Chris@488: int w, Chris@488: int h, Chris@907: const vector &binforx, Chris@488: int minbin, Chris@488: int maxbin, Chris@905: double displayMinFreq, Chris@905: double displayMaxFreq, Chris@491: bool logarithmic, Chris@491: MagnitudeRange &overallMag, Chris@1026: bool &overallMagChanged, Chris@1039: bool rightToLeft, Chris@1039: double softTimeLimit) const Chris@488: { Chris@488: Profiler profiler("SpectrogramLayer::paintDrawBufferPeakFrequencies"); Chris@488: Chris@488: #ifdef DEBUG_SPECTROGRAM_REPAINT Chris@1026: cerr << "SpectrogramLayer::paintDrawBufferPeakFrequencies: minbin " << minbin << ", maxbin " << maxbin << "; w " << w << ", h " << h << endl; Chris@488: #endif Chris@488: if (minbin < 0) minbin = 0; Chris@488: if (maxbin < 0) maxbin = minbin+1; Chris@488: Chris@488: FFTModel *fft = getFFTModel(v); Chris@1024: if (!fft) return 0; Chris@488: Chris@488: FFTModel::PeakSet peakfreqs; Chris@488: Chris@848: int psx = -1; Chris@545: Chris@545: #ifdef __GNUC__ Chris@488: float values[maxbin - minbin + 1]; Chris@545: #else Chris@545: float *values = (float *)alloca((maxbin - minbin + 1) * sizeof(float)); Chris@545: #endif Chris@488: Chris@1027: int minColumns = 4; Chris@1039: bool haveTimeLimits = (softTimeLimit > 0.0); Chris@1037: double hardTimeLimit = softTimeLimit * 2.0; Chris@1037: bool overridingSoftLimit = false; Chris@1027: auto startTime = chrono::steady_clock::now(); Chris@1027: Chris@1026: int start = 0; Chris@1026: int finish = w; Chris@1026: int step = 1; Chris@1026: Chris@1026: if (rightToLeft) { Chris@1026: start = w-1; Chris@1026: finish = -1; Chris@1026: step = -1; Chris@1026: } Chris@1026: Chris@1027: int columnCount = 0; Chris@1027: Chris@1026: for (int x = start; x != finish; x += step) { Chris@488: Chris@1027: ++columnCount; Chris@1027: Chris@488: if (binforx[x] < 0) continue; Chris@488: Chris@488: int sx0 = binforx[x]; Chris@488: int sx1 = sx0; Chris@488: if (x+1 < w) sx1 = binforx[x+1]; Chris@488: if (sx0 < 0) sx0 = sx1 - 1; Chris@488: if (sx0 < 0) continue; Chris@488: if (sx1 <= sx0) sx1 = sx0 + 1; Chris@488: Chris@488: for (int sx = sx0; sx < sx1; ++sx) { Chris@488: Chris@488: if (sx < 0 || sx >= int(fft->getWidth())) continue; Chris@488: Chris@488: MagnitudeRange mag; Chris@488: Chris@488: if (sx != psx) { Chris@488: peakfreqs = fft->getPeakFrequencies(FFTModel::AllPeaks, sx, Chris@488: minbin, maxbin - 1); Chris@488: if (m_colourScale == PhaseColourScale) { Chris@488: fft->getPhasesAt(sx, values, minbin, maxbin - minbin + 1); Chris@1060: /*!!! Chris@862: } else if (m_normalization == NormalizeColumns) { Chris@488: fft->getNormalizedMagnitudesAt(sx, values, minbin, maxbin - minbin + 1); Chris@862: } else if (m_normalization == NormalizeHybrid) { Chris@1055: float max = fft->getNormalizedMagnitudesAt Chris@1055: (sx, values, minbin, maxbin - minbin + 1); Chris@1055: float scale = log10f(max + 1.f); Chris@1055: for (int i = minbin; i <= maxbin; ++i) { Chris@1055: values[i - minbin] *= scale; Chris@1060: }*/ Chris@488: } else { Chris@488: fft->getMagnitudesAt(sx, values, minbin, maxbin - minbin + 1); Chris@488: } Chris@488: psx = sx; Chris@488: } Chris@488: Chris@488: for (FFTModel::PeakSet::const_iterator pi = peakfreqs.begin(); Chris@488: pi != peakfreqs.end(); ++pi) { Chris@488: Chris@488: int bin = pi->first; Chris@907: double freq = pi->second; Chris@488: Chris@488: if (bin < minbin) continue; Chris@488: if (bin > maxbin) break; Chris@488: Chris@907: double value = values[bin - minbin]; Chris@488: Chris@488: if (m_colourScale != PhaseColourScale) { Chris@862: if (m_normalization != NormalizeColumns) { Chris@907: value /= (m_fftSize/2.0); Chris@488: } Chris@907: mag.sample(float(value)); Chris@488: value *= m_gain; Chris@488: } Chris@488: Chris@905: double y = v->getYForFrequency Chris@488: (freq, displayMinFreq, displayMaxFreq, logarithmic); Chris@488: Chris@558: int iy = int(y + 0.5); Chris@558: if (iy < 0 || iy >= h) continue; Chris@558: Chris@558: m_drawBuffer.setPixel(x, iy, getDisplayValue(v, value)); Chris@488: } Chris@488: Chris@488: if (mag.isSet()) { Chris@488: if (sx >= int(m_columnMags.size())) { Chris@540: #ifdef DEBUG_SPECTROGRAM Chris@682: cerr << "INTERNAL ERROR: " << sx << " >= " Chris@488: << m_columnMags.size() Chris@488: << " at SpectrogramLayer.cpp::paintDrawBuffer" Chris@682: << endl; Chris@540: #endif Chris@490: } else { Chris@490: m_columnMags[sx].sample(mag); Chris@491: if (overallMag.sample(mag)) overallMagChanged = true; Chris@488: } Chris@488: } Chris@488: } Chris@1027: Chris@1039: if (haveTimeLimits) { Chris@1027: if (columnCount >= minColumns) { Chris@1027: auto t = chrono::steady_clock::now(); Chris@1027: double diff = chrono::duration(t - startTime).count(); Chris@1037: if (diff > hardTimeLimit) { Chris@1027: #ifdef DEBUG_SPECTROGRAM_REPAINT Chris@1037: cerr << "SpectrogramLayer::paintDrawBufferPeakFrequencies: hard limit " << hardTimeLimit << " sec exceeded after " Chris@1031: << columnCount << " columns with time " << diff << endl; Chris@1027: #endif Chris@1027: return columnCount; Chris@1037: } else if (diff > softTimeLimit && !overridingSoftLimit) { Chris@1037: // If we're more than half way through by the time Chris@1037: // we reach the soft limit, ignore it (though Chris@1037: // still respect the hard limit, above). Otherwise Chris@1037: // respect the soft limit and return now. Chris@1037: if (columnCount > w/2) { Chris@1037: overridingSoftLimit = true; Chris@1037: } else { Chris@1037: #ifdef DEBUG_SPECTROGRAM_REPAINT Chris@1037: cerr << "SpectrogramLayer::paintDrawBufferPeakFrequencies: soft limit " << softTimeLimit << " sec exceeded after " Chris@1037: << columnCount << " columns with time " << diff << endl; Chris@1037: #endif Chris@1037: return columnCount; Chris@1037: } Chris@1037: } Chris@1027: } Chris@1027: } Chris@488: } Chris@1024: Chris@1027: return columnCount; Chris@488: } Chris@488: Chris@1058: vector Chris@1058: SpectrogramLayer::getColumnFromFFTModel(FFTModel *fft, Chris@1058: int sx, // column number in model Chris@1058: int minbin, Chris@1058: int bincount) const Chris@1058: { Chris@1058: vector values(bincount, 0.f); Chris@1058: Chris@1058: if (m_colourScale == PhaseColourScale) { Chris@1058: fft->getPhasesAt(sx, values.data(), minbin, bincount); Chris@1058: } else { Chris@1058: fft->getMagnitudesAt(sx, values.data(), minbin, bincount); Chris@1058: } Chris@1058: Chris@1058: return move(values); Chris@1058: } Chris@1058: Chris@1058: vector Chris@1058: SpectrogramLayer::getColumnFromGenericModel(DenseThreeDimensionalModel *model, Chris@1058: int sx, // column number in model Chris@1058: int minbin, Chris@1058: int bincount) const Chris@1058: { Chris@1058: if (m_colourScale == PhaseColourScale) { Chris@1058: throw std::logic_error("can't use phase scale with generic 3d model"); Chris@1058: } Chris@1058: Chris@1058: auto col = model->getColumn(sx); Chris@1058: Chris@1058: return move(vector(col.data() + minbin, Chris@1058: col.data() + minbin + bincount)); Chris@1058: } Chris@1058: Chris@1060: vector Chris@1060: SpectrogramLayer::scaleColumn(const vector &in) const Chris@1058: { Chris@1060: if (m_normalization == NormalizeColumns || Chris@1060: m_normalization == NormalizeHybrid) { Chris@1060: return in; Chris@1060: } Chris@1060: vector out; Chris@1060: out.reserve(in.size()); Chris@1060: float scale = 2.f / float(m_fftSize); Chris@1060: for (auto v: in) { Chris@1060: out.push_back(v * scale); Chris@1060: } Chris@1060: return move(out); Chris@1058: } Chris@1058: Chris@1058: static bool Chris@1058: is_peak(const vector &values, int ix) Chris@1058: { Chris@1060: if (!in_range_for(values, ix-1)) return false; Chris@1060: if (!in_range_for(values, ix+1)) return false; Chris@1058: if (values[ix] < values[ix+1]) return false; Chris@1058: if (values[ix] < values[ix-1]) return false; Chris@1058: return true; Chris@1058: } Chris@1058: Chris@1058: vector Chris@1058: SpectrogramLayer::distributeColumn(const vector &in, Chris@1058: int h, Chris@1058: const vector &binfory, Chris@1058: int minbin, Chris@1059: bool interpolate) const Chris@1058: { Chris@1058: vector out(h, 0.f); Chris@1058: int bins = int(in.size()); Chris@1058: Chris@1058: for (int y = 0; y < h; ++y) { Chris@1058: Chris@1058: double sy0 = binfory[y] - minbin; Chris@1058: double sy1 = sy0 + 1; Chris@1058: if (y+1 < h) { Chris@1058: sy1 = binfory[y+1] - minbin; Chris@1058: } Chris@1058: Chris@1058: if (interpolate && fabs(sy1 - sy0) < 1.0) { Chris@1058: Chris@1058: double centre = (sy0 + sy1) / 2; Chris@1058: double dist = (centre - 0.5) - rint(centre - 0.5); Chris@1058: int bin = int(centre); Chris@1058: Chris@1058: int other = (dist < 0 ? (bin-1) : (bin+1)); Chris@1058: Chris@1058: if (bin < 0) bin = 0; Chris@1058: if (bin >= bins) bin = bins-1; Chris@1058: Chris@1058: if (other < 0 || other >= bins) { Chris@1058: other = bin; Chris@1058: } Chris@1058: Chris@1059: double prop = 1.0 - fabs(dist); Chris@1059: Chris@1059: double v0 = in[bin]; Chris@1059: double v1 = in[other]; Chris@1058: Chris@1059: out[y] = float(prop * v0 + (1.0 - prop) * v1); Chris@1058: Chris@1058: } else { // not interpolating this one Chris@1058: Chris@1058: int by0 = int(sy0 + 0.0001); Chris@1058: int by1 = int(sy1 + 0.0001); Chris@1058: if (by1 < by0 + 1) by1 = by0 + 1; Chris@1058: Chris@1058: for (int bin = by0; bin < by1; ++bin) { Chris@1058: Chris@1058: float value = in[bin]; Chris@1058: Chris@1058: if (value > out[y] || m_colourScale == PhaseColourScale) { Chris@1058: out[y] = value; Chris@1058: } Chris@1058: } Chris@1058: } Chris@1058: } Chris@1058: Chris@1058: return out; Chris@1058: } Chris@1058: Chris@1058: void Chris@1058: SpectrogramLayer::recordColumnExtents(const vector &col, Chris@1058: int sx, // column index, for m_columnMags Chris@1058: MagnitudeRange &overallMag, Chris@1059: bool &overallMagChanged) const Chris@1058: { Chris@1060: if (!in_range_for(m_columnMags, sx)) { Chris@1058: throw logic_error("sx out of range for m_columnMags"); Chris@1058: } Chris@1058: MagnitudeRange mr; Chris@1058: for (auto v: col) { Chris@1058: mr.sample(v); Chris@1058: } Chris@1058: m_columnMags[sx] = mr; Chris@1058: if (overallMag.sample(mr)) { Chris@1058: overallMagChanged = true; Chris@1058: } Chris@1058: } Chris@1058: Chris@1059: vector Chris@1060: SpectrogramLayer::normalizeColumn(const vector &in) const Chris@1060: { Chris@1060: if (m_normalization == NoNormalization || Chris@1060: m_normalization == NormalizeVisibleArea) { Chris@1060: // NormalizeVisibleArea is handled through adjustment to m_gain Chris@1060: return in; Chris@1060: } Chris@1060: Chris@1060: float max = *max_element(in.begin(), in.end()); Chris@1060: Chris@1060: if (m_normalization == NormalizeColumns && max == 0.f) { Chris@1060: return in; Chris@1060: } Chris@1060: Chris@1060: if (m_normalization == NormalizeHybrid && max <= 0.f) { Chris@1060: return in; Chris@1060: } Chris@1060: Chris@1060: vector out; Chris@1060: out.reserve(in.size()); Chris@1060: Chris@1060: float scale; Chris@1060: if (m_normalization == NormalizeHybrid) { Chris@1060: scale = log10f(max + 1.f) / max; Chris@1060: } else { Chris@1060: scale = 1.f / max; Chris@1060: } Chris@1060: Chris@1060: for (auto v: in) { Chris@1060: out.push_back(v * scale); Chris@1060: } Chris@1060: return move(out); Chris@1060: } Chris@1060: Chris@1060: vector Chris@1059: SpectrogramLayer::peakPickColumn(const vector &in) const Chris@1059: { Chris@1059: if (m_binDisplay != PeakBins) return in; Chris@1059: Chris@1059: vector out(in.size(), 0.f); Chris@1059: Chris@1060: for (int i = 0; in_range_for(in, i); ++i) { Chris@1059: if (is_peak(in, i)) { Chris@1059: out[i] = in[i]; Chris@1059: } Chris@1059: } Chris@1059: Chris@1059: return move(out); Chris@1059: } Chris@1059: Chris@1059: vector Chris@1059: SpectrogramLayer::applyDisplayGain(const vector &in) const Chris@1059: { Chris@1059: if (m_gain == 1.0) return in; Chris@1059: Chris@1059: vector out; Chris@1059: out.reserve(in.size()); Chris@1059: for (auto v: in) { Chris@1059: out.push_back(v * m_gain); Chris@1059: } Chris@1059: return move(out); Chris@1059: } Chris@1059: Chris@1058: // order: Chris@1059: // get column -> scale -> distribute/interpolate -> record extents -> normalise -> peak pick -> apply display gain Chris@1058: Chris@1024: int Chris@918: SpectrogramLayer::paintDrawBuffer(LayerGeometryProvider *v, Chris@481: int w, Chris@481: int h, Chris@907: const vector &binforx, Chris@907: const vector &binfory, Chris@491: bool usePeaksCache, Chris@491: MagnitudeRange &overallMag, Chris@1026: bool &overallMagChanged, Chris@1039: bool rightToLeft, Chris@1039: double softTimeLimit) const Chris@480: { Chris@481: Profiler profiler("SpectrogramLayer::paintDrawBuffer"); Chris@480: Chris@490: int minbin = int(binfory[0] + 0.0001); Chris@907: int maxbin = int(binfory[h-1]); Chris@480: Chris@485: #ifdef DEBUG_SPECTROGRAM_REPAINT Chris@1026: cerr << "SpectrogramLayer::paintDrawBuffer: minbin " << minbin << ", maxbin " << maxbin << "; w " << w << ", h " << h << endl; Chris@485: #endif Chris@480: if (minbin < 0) minbin = 0; Chris@480: if (maxbin < 0) maxbin = minbin+1; Chris@480: Chris@1060: DenseThreeDimensionalModel *peakCacheModel = 0; Chris@1060: FFTModel *fftModel = 0; Chris@484: DenseThreeDimensionalModel *sourceModel = 0; Chris@1060: Chris@485: #ifdef DEBUG_SPECTROGRAM_REPAINT Chris@1026: cerr << "SpectrogramLayer::paintDrawBuffer: Note: bin display = " << m_binDisplay << ", w = " << w << ", binforx[" << w-1 << "] = " << binforx[w-1] << ", binforx[0] = " << binforx[0] << endl; Chris@485: #endif Chris@1060: Chris@1060: int divisor = 1; Chris@1054: if (usePeaksCache) { Chris@1060: peakCacheModel = getPeakCache(v); Chris@1054: divisor = m_peakCacheDivisor; Chris@1060: sourceModel = peakCacheModel; Chris@484: } else { Chris@1060: fftModel = getFFTModel(v); Chris@1060: sourceModel = fftModel; Chris@484: } Chris@484: Chris@1024: if (!sourceModel) return 0; Chris@484: Chris@490: bool interpolate = false; Chris@490: Preferences::SpectrogramSmoothing smoothing = Chris@490: Preferences::getInstance()->getSpectrogramSmoothing(); Chris@490: if (smoothing == Preferences::SpectrogramInterpolated || Chris@490: smoothing == Preferences::SpectrogramZeroPaddedAndInterpolated) { Chris@490: if (m_binDisplay != PeakBins && Chris@490: m_binDisplay != PeakFrequencies) { Chris@490: interpolate = true; Chris@490: } Chris@490: } Chris@490: Chris@480: int psx = -1; Chris@545: Chris@1027: int minColumns = 4; Chris@1039: bool haveTimeLimits = (softTimeLimit > 0.0); Chris@1037: double hardTimeLimit = softTimeLimit * 2.0; Chris@1037: bool overridingSoftLimit = false; Chris@1027: auto startTime = chrono::steady_clock::now(); Chris@1027: Chris@1026: int start = 0; Chris@1026: int finish = w; Chris@1026: int step = 1; Chris@1026: Chris@1026: if (rightToLeft) { Chris@1026: start = w-1; Chris@1026: finish = -1; Chris@1026: step = -1; Chris@1026: } Chris@1027: Chris@1027: int columnCount = 0; Chris@1026: Chris@1060: vector preparedColumn; Chris@1060: Chris@1026: for (int x = start; x != finish; x += step) { Chris@1027: Chris@1056: // x is the on-canvas pixel coord; sx (later) will be the Chris@1056: // source column index Chris@1056: Chris@1027: ++columnCount; Chris@480: Chris@482: if (binforx[x] < 0) continue; Chris@482: Chris@487: float columnMax = 0.f; Chris@487: Chris@484: int sx0 = binforx[x] / divisor; Chris@483: int sx1 = sx0; Chris@484: if (x+1 < w) sx1 = binforx[x+1] / divisor; Chris@483: if (sx0 < 0) sx0 = sx1 - 1; Chris@483: if (sx0 < 0) continue; Chris@483: if (sx1 <= sx0) sx1 = sx0 + 1; Chris@483: Chris@483: for (int sx = sx0; sx < sx1; ++sx) { Chris@483: Chris@518: #ifdef DEBUG_SPECTROGRAM_REPAINT Chris@682: // cerr << "sx = " << sx << endl; Chris@518: #endif Chris@518: Chris@1060: if (sx < 0 || sx >= sourceModel->getWidth()) continue; Chris@483: Chris@488: MagnitudeRange mag; Chris@488: Chris@483: if (sx != psx) { Chris@1060: Chris@1060: vector column; Chris@1060: if (peakCacheModel) { Chris@1060: column = getColumnFromGenericModel(peakCacheModel, Chris@1060: sx, Chris@1060: minbin, Chris@1060: maxbin - minbin + 1); Chris@484: } else { Chris@1060: column = getColumnFromFFTModel(fftModel, Chris@1060: sx, Chris@1060: minbin, Chris@1060: maxbin - minbin + 1); Chris@484: } Chris@1060: Chris@1060: vector distributed = Chris@1060: distributeColumn(scaleColumn(column), Chris@1060: h, Chris@1060: binfory, Chris@1060: minbin, Chris@1060: interpolate); Chris@1060: Chris@1060: recordColumnExtents(distributed, Chris@1060: sx, Chris@1060: overallMag, Chris@1060: overallMagChanged); Chris@1060: Chris@1060: preparedColumn = Chris@1060: applyDisplayGain(peakPickColumn Chris@1060: (normalizeColumn(distributed))); Chris@1060: Chris@483: psx = sx; Chris@483: } Chris@483: Chris@1060: //!!! now peak of all preparedColumns for this pixel Chris@483: } Chris@483: Chris@483: for (int y = 0; y < h; ++y) { Chris@1060: unsigned char pixel = getDisplayValue(v, preparedColumn[y]); Chris@1060: m_drawBuffer.setPixel(x, h-y-1, pixel); Chris@480: } Chris@1025: Chris@1039: if (haveTimeLimits) { Chris@1027: if (columnCount >= minColumns) { Chris@1025: auto t = chrono::steady_clock::now(); Chris@1025: double diff = chrono::duration(t - startTime).count(); Chris@1037: if (diff > hardTimeLimit) { Chris@1025: #ifdef DEBUG_SPECTROGRAM_REPAINT Chris@1037: cerr << "SpectrogramLayer::paintDrawBuffer: hard limit " << hardTimeLimit << " sec exceeded after " Chris@1031: << columnCount << " columns with time " << diff << endl; Chris@1025: #endif Chris@1027: return columnCount; Chris@1037: } else if (diff > softTimeLimit && !overridingSoftLimit) { Chris@1037: // If we're more than half way through by the time Chris@1037: // we reach the soft limit, ignore it (though Chris@1037: // still respect the hard limit, above). Otherwise Chris@1037: // respect the soft limit and return now. Chris@1037: if (columnCount > w/2) { Chris@1037: overridingSoftLimit = true; Chris@1037: } else { Chris@1037: #ifdef DEBUG_SPECTROGRAM_REPAINT Chris@1037: cerr << "SpectrogramLayer::paintDrawBuffer: soft limit " << softTimeLimit << " sec exceeded after " Chris@1037: << columnCount << " columns with time " << diff << endl; Chris@1037: #endif Chris@1037: return columnCount; Chris@1037: } Chris@1037: } Chris@1025: } Chris@1025: } Chris@480: } Chris@1024: Chris@1027: return columnCount; Chris@480: } Chris@477: Chris@121: void Chris@918: SpectrogramLayer::illuminateLocalFeatures(LayerGeometryProvider *v, QPainter &paint) const Chris@121: { Chris@382: Profiler profiler("SpectrogramLayer::illuminateLocalFeatures"); Chris@382: Chris@121: QPoint localPos; Chris@121: if (!v->shouldIlluminateLocalFeatures(this, localPos) || !m_model) { Chris@121: return; Chris@121: } Chris@121: Chris@682: // cerr << "SpectrogramLayer: illuminateLocalFeatures(" Chris@682: // << localPos.x() << "," << localPos.y() << ")" << endl; Chris@121: Chris@905: double s0, s1; Chris@905: double f0, f1; Chris@121: Chris@121: if (getXBinRange(v, localPos.x(), s0, s1) && Chris@121: getYBinSourceRange(v, localPos.y(), f0, f1)) { Chris@121: Chris@121: int s0i = int(s0 + 0.001); Chris@121: int s1i = int(s1); Chris@121: Chris@121: int x0 = v->getXForFrame(s0i * getWindowIncrement()); Chris@121: int x1 = v->getXForFrame((s1i + 1) * getWindowIncrement()); Chris@121: Chris@248: int y1 = int(getYForFrequency(v, f1)); Chris@248: int y0 = int(getYForFrequency(v, f0)); Chris@121: Chris@682: // cerr << "SpectrogramLayer: illuminate " Chris@682: // << x0 << "," << y1 << " -> " << x1 << "," << y0 << endl; Chris@121: Chris@287: paint.setPen(v->getForeground()); Chris@133: Chris@133: //!!! should we be using paintCrosshairs for this? Chris@133: Chris@121: paint.drawRect(x0, y1, x1 - x0 + 1, y0 - y1 + 1); Chris@121: } Chris@121: } Chris@121: Chris@905: double Chris@918: SpectrogramLayer::getYForFrequency(const LayerGeometryProvider *v, double frequency) const Chris@42: { Chris@44: return v->getYForFrequency(frequency, Chris@44: getEffectiveMinFrequency(), Chris@44: getEffectiveMaxFrequency(), Chris@44: m_frequencyScale == LogFrequencyScale); Chris@42: } Chris@42: Chris@905: double Chris@918: SpectrogramLayer::getFrequencyForY(const LayerGeometryProvider *v, int y) const Chris@42: { Chris@44: return v->getFrequencyForY(y, Chris@44: getEffectiveMinFrequency(), Chris@44: getEffectiveMaxFrequency(), Chris@44: m_frequencyScale == LogFrequencyScale); Chris@42: } Chris@42: Chris@0: int Chris@918: SpectrogramLayer::getCompletion(LayerGeometryProvider *v) const Chris@0: { Chris@920: const View *view = v->getView(); Chris@920: Chris@1030: if (m_fftModels.find(view->getId()) == m_fftModels.end()) return 100; Chris@1030: Chris@1030: int completion = m_fftModels[view->getId()]->getCompletion(); Chris@224: #ifdef DEBUG_SPECTROGRAM_REPAINT Chris@985: cerr << "SpectrogramLayer::getCompletion: completion = " << completion << endl; Chris@224: #endif Chris@0: return completion; Chris@0: } Chris@0: Chris@583: QString Chris@918: SpectrogramLayer::getError(LayerGeometryProvider *v) const Chris@583: { Chris@920: const View *view = v->getView(); Chris@1030: if (m_fftModels.find(view->getId()) == m_fftModels.end()) return ""; Chris@1030: return m_fftModels[view->getId()]->getError(); Chris@583: } Chris@583: Chris@28: bool Chris@905: SpectrogramLayer::getValueExtents(double &min, double &max, Chris@101: bool &logarithmic, QString &unit) const Chris@79: { Chris@133: if (!m_model) return false; Chris@133: Chris@907: sv_samplerate_t sr = m_model->getSampleRate(); Chris@905: min = double(sr) / m_fftSize; Chris@905: max = double(sr) / 2; Chris@133: Chris@101: logarithmic = (m_frequencyScale == LogFrequencyScale); Chris@79: unit = "Hz"; Chris@79: return true; Chris@79: } Chris@79: Chris@79: bool Chris@905: SpectrogramLayer::getDisplayExtents(double &min, double &max) const Chris@101: { Chris@101: min = getEffectiveMinFrequency(); Chris@101: max = getEffectiveMaxFrequency(); Chris@253: Chris@587: // SVDEBUG << "SpectrogramLayer::getDisplayExtents: " << min << "->" << max << endl; Chris@101: return true; Chris@101: } Chris@101: Chris@101: bool Chris@905: SpectrogramLayer::setDisplayExtents(double min, double max) Chris@120: { Chris@120: if (!m_model) return false; Chris@187: Chris@587: // SVDEBUG << "SpectrogramLayer::setDisplayExtents: " << min << "->" << max << endl; Chris@187: Chris@120: if (min < 0) min = 0; Chris@907: if (max > m_model->getSampleRate()/2.0) max = m_model->getSampleRate()/2.0; Chris@120: Chris@907: int minf = int(lrint(min)); Chris@907: int maxf = int(lrint(max)); Chris@120: Chris@120: if (m_minFrequency == minf && m_maxFrequency == maxf) return true; Chris@120: Chris@478: invalidateImageCaches(); Chris@120: invalidateMagnitudes(); Chris@120: Chris@120: m_minFrequency = minf; Chris@120: m_maxFrequency = maxf; Chris@120: Chris@120: emit layerParametersChanged(); Chris@120: Chris@133: int vs = getCurrentVerticalZoomStep(); Chris@133: if (vs != m_lastEmittedZoomStep) { Chris@133: emit verticalZoomChanged(); Chris@133: m_lastEmittedZoomStep = vs; Chris@133: } Chris@133: Chris@120: return true; Chris@120: } Chris@120: Chris@120: bool Chris@918: SpectrogramLayer::getYScaleValue(const LayerGeometryProvider *v, int y, Chris@905: double &value, QString &unit) const Chris@261: { Chris@261: value = getFrequencyForY(v, y); Chris@261: unit = "Hz"; Chris@261: return true; Chris@261: } Chris@261: Chris@261: bool Chris@918: SpectrogramLayer::snapToFeatureFrame(LayerGeometryProvider *, Chris@907: sv_frame_t &frame, Chris@805: int &resolution, Chris@28: SnapType snap) const Chris@13: { Chris@13: resolution = getWindowIncrement(); Chris@907: sv_frame_t left = (frame / resolution) * resolution; Chris@907: sv_frame_t right = left + resolution; Chris@28: Chris@28: switch (snap) { Chris@28: case SnapLeft: frame = left; break; Chris@28: case SnapRight: frame = right; break; Chris@28: case SnapNearest: Chris@28: case SnapNeighbouring: Chris@28: if (frame - left > right - frame) frame = right; Chris@28: else frame = left; Chris@28: break; Chris@28: } Chris@28: Chris@28: return true; Chris@28: } Chris@13: Chris@283: void Chris@918: SpectrogramLayer::measureDoubleClick(LayerGeometryProvider *v, QMouseEvent *e) Chris@283: { Chris@920: const View *view = v->getView(); Chris@1030: ScrollableImageCache &cache = getImageCacheReference(view); Chris@1030: Chris@1030: cerr << "cache width: " << cache.getSize().width() << ", height: " Chris@1030: << cache.getSize().height() << endl; Chris@1030: Chris@1030: QImage image = cache.getImage(); Chris@283: Chris@283: ImageRegionFinder finder; Chris@283: QRect rect = finder.findRegionExtents(&image, e->pos()); Chris@283: if (rect.isValid()) { Chris@283: MeasureRect mr; Chris@283: setMeasureRectFromPixrect(v, mr, rect); Chris@283: CommandHistory::getInstance()->addCommand Chris@283: (new AddMeasurementRectCommand(this, mr)); Chris@283: } Chris@283: } Chris@283: Chris@77: bool Chris@918: SpectrogramLayer::getCrosshairExtents(LayerGeometryProvider *v, QPainter &paint, Chris@77: QPoint cursorPos, Chris@1025: vector &extents) const Chris@77: { Chris@918: QRect vertical(cursorPos.x() - 12, 0, 12, v->getPaintHeight()); Chris@77: extents.push_back(vertical); Chris@77: Chris@77: QRect horizontal(0, cursorPos.y(), cursorPos.x(), 1); Chris@77: extents.push_back(horizontal); Chris@77: Chris@608: int sw = getVerticalScaleWidth(v, m_haveDetailedScale, paint); Chris@264: Chris@280: QRect freq(sw, cursorPos.y() - paint.fontMetrics().ascent() - 2, Chris@280: paint.fontMetrics().width("123456 Hz") + 2, Chris@280: paint.fontMetrics().height()); Chris@280: extents.push_back(freq); Chris@264: Chris@279: QRect pitch(sw, cursorPos.y() + 2, Chris@279: paint.fontMetrics().width("C#10+50c") + 2, Chris@279: paint.fontMetrics().height()); Chris@279: extents.push_back(pitch); Chris@279: Chris@280: QRect rt(cursorPos.x(), Chris@918: v->getPaintHeight() - paint.fontMetrics().height() - 2, Chris@280: paint.fontMetrics().width("1234.567 s"), Chris@280: paint.fontMetrics().height()); Chris@280: extents.push_back(rt); Chris@280: Chris@280: int w(paint.fontMetrics().width("1234567890") + 2); Chris@280: QRect frame(cursorPos.x() - w - 2, Chris@918: v->getPaintHeight() - paint.fontMetrics().height() - 2, Chris@280: w, Chris@280: paint.fontMetrics().height()); Chris@280: extents.push_back(frame); Chris@280: Chris@77: return true; Chris@77: } Chris@77: Chris@77: void Chris@918: SpectrogramLayer::paintCrosshairs(LayerGeometryProvider *v, QPainter &paint, Chris@77: QPoint cursorPos) const Chris@77: { Chris@77: paint.save(); Chris@283: Chris@608: int sw = getVerticalScaleWidth(v, m_haveDetailedScale, paint); Chris@283: Chris@282: QFont fn = paint.font(); Chris@282: if (fn.pointSize() > 8) { Chris@282: fn.setPointSize(fn.pointSize() - 1); Chris@282: paint.setFont(fn); Chris@282: } Chris@77: paint.setPen(m_crosshairColour); Chris@77: Chris@77: paint.drawLine(0, cursorPos.y(), cursorPos.x() - 1, cursorPos.y()); Chris@918: paint.drawLine(cursorPos.x(), 0, cursorPos.x(), v->getPaintHeight()); Chris@77: Chris@905: double fundamental = getFrequencyForY(v, cursorPos.y()); Chris@77: Chris@278: v->drawVisibleText(paint, Chris@278: sw + 2, Chris@278: cursorPos.y() - 2, Chris@278: QString("%1 Hz").arg(fundamental), Chris@278: View::OutlinedText); Chris@278: Chris@279: if (Pitch::isFrequencyInMidiRange(fundamental)) { Chris@279: QString pitchLabel = Pitch::getPitchLabelForFrequency(fundamental); Chris@279: v->drawVisibleText(paint, Chris@279: sw + 2, Chris@279: cursorPos.y() + paint.fontMetrics().ascent() + 2, Chris@279: pitchLabel, Chris@279: View::OutlinedText); Chris@279: } Chris@279: Chris@907: sv_frame_t frame = v->getFrameForX(cursorPos.x()); Chris@279: RealTime rt = RealTime::frame2RealTime(frame, m_model->getSampleRate()); Chris@280: QString rtLabel = QString("%1 s").arg(rt.toText(true).c_str()); Chris@280: QString frameLabel = QString("%1").arg(frame); Chris@280: v->drawVisibleText(paint, Chris@280: cursorPos.x() - paint.fontMetrics().width(frameLabel) - 2, Chris@918: v->getPaintHeight() - 2, Chris@280: frameLabel, Chris@280: View::OutlinedText); Chris@280: v->drawVisibleText(paint, Chris@280: cursorPos.x() + 2, Chris@918: v->getPaintHeight() - 2, Chris@280: rtLabel, Chris@280: View::OutlinedText); Chris@264: Chris@77: int harmonic = 2; Chris@77: Chris@77: while (harmonic < 100) { Chris@77: Chris@907: int hy = int(lrint(getYForFrequency(v, fundamental * harmonic))); Chris@918: if (hy < 0 || hy > v->getPaintHeight()) break; Chris@77: Chris@77: int len = 7; Chris@77: Chris@77: if (harmonic % 2 == 0) { Chris@77: if (harmonic % 4 == 0) { Chris@77: len = 12; Chris@77: } else { Chris@77: len = 10; Chris@77: } Chris@77: } Chris@77: Chris@77: paint.drawLine(cursorPos.x() - len, Chris@907: hy, Chris@77: cursorPos.x(), Chris@907: hy); Chris@77: Chris@77: ++harmonic; Chris@77: } Chris@77: Chris@77: paint.restore(); Chris@77: } Chris@77: Chris@25: QString Chris@918: SpectrogramLayer::getFeatureDescription(LayerGeometryProvider *v, QPoint &pos) const Chris@25: { Chris@25: int x = pos.x(); Chris@25: int y = pos.y(); Chris@0: Chris@25: if (!m_model || !m_model->isOK()) return ""; Chris@0: Chris@905: double magMin = 0, magMax = 0; Chris@905: double phaseMin = 0, phaseMax = 0; Chris@905: double freqMin = 0, freqMax = 0; Chris@905: double adjFreqMin = 0, adjFreqMax = 0; Chris@25: QString pitchMin, pitchMax; Chris@0: RealTime rtMin, rtMax; Chris@0: Chris@38: bool haveValues = false; Chris@0: Chris@44: if (!getXBinSourceRange(v, x, rtMin, rtMax)) { Chris@38: return ""; Chris@38: } Chris@44: if (getXYBinSourceRange(v, x, y, magMin, magMax, phaseMin, phaseMax)) { Chris@38: haveValues = true; Chris@38: } Chris@0: Chris@35: QString adjFreqText = "", adjPitchText = ""; Chris@35: Chris@38: if (m_binDisplay == PeakFrequencies) { Chris@35: Chris@44: if (!getAdjustedYBinSourceRange(v, x, y, freqMin, freqMax, Chris@38: adjFreqMin, adjFreqMax)) { Chris@38: return ""; Chris@38: } Chris@35: Chris@35: if (adjFreqMin != adjFreqMax) { Chris@65: adjFreqText = tr("Peak Frequency:\t%1 - %2 Hz\n") Chris@35: .arg(adjFreqMin).arg(adjFreqMax); Chris@35: } else { Chris@65: adjFreqText = tr("Peak Frequency:\t%1 Hz\n") Chris@35: .arg(adjFreqMin); Chris@38: } Chris@38: Chris@38: QString pmin = Pitch::getPitchLabelForFrequency(adjFreqMin); Chris@38: QString pmax = Pitch::getPitchLabelForFrequency(adjFreqMax); Chris@38: Chris@38: if (pmin != pmax) { Chris@65: adjPitchText = tr("Peak Pitch:\t%3 - %4\n").arg(pmin).arg(pmax); Chris@38: } else { Chris@65: adjPitchText = tr("Peak Pitch:\t%2\n").arg(pmin); Chris@35: } Chris@35: Chris@35: } else { Chris@35: Chris@44: if (!getYBinSourceRange(v, y, freqMin, freqMax)) return ""; Chris@35: } Chris@35: Chris@25: QString text; Chris@25: Chris@25: if (rtMin != rtMax) { Chris@25: text += tr("Time:\t%1 - %2\n") Chris@25: .arg(rtMin.toText(true).c_str()) Chris@25: .arg(rtMax.toText(true).c_str()); Chris@25: } else { Chris@25: text += tr("Time:\t%1\n") Chris@25: .arg(rtMin.toText(true).c_str()); Chris@0: } Chris@0: Chris@25: if (freqMin != freqMax) { Chris@65: text += tr("%1Bin Frequency:\t%2 - %3 Hz\n%4Bin Pitch:\t%5 - %6\n") Chris@65: .arg(adjFreqText) Chris@25: .arg(freqMin) Chris@25: .arg(freqMax) Chris@65: .arg(adjPitchText) Chris@65: .arg(Pitch::getPitchLabelForFrequency(freqMin)) Chris@65: .arg(Pitch::getPitchLabelForFrequency(freqMax)); Chris@65: } else { Chris@65: text += tr("%1Bin Frequency:\t%2 Hz\n%3Bin Pitch:\t%4\n") Chris@35: .arg(adjFreqText) Chris@25: .arg(freqMin) Chris@65: .arg(adjPitchText) Chris@65: .arg(Pitch::getPitchLabelForFrequency(freqMin)); Chris@25: } Chris@25: Chris@38: if (haveValues) { Chris@905: double dbMin = AudioLevel::multiplier_to_dB(magMin); Chris@905: double dbMax = AudioLevel::multiplier_to_dB(magMax); Chris@43: QString dbMinString; Chris@43: QString dbMaxString; Chris@43: if (dbMin == AudioLevel::DB_FLOOR) { Chris@43: dbMinString = tr("-Inf"); Chris@43: } else { Chris@907: dbMinString = QString("%1").arg(lrint(dbMin)); Chris@43: } Chris@43: if (dbMax == AudioLevel::DB_FLOOR) { Chris@43: dbMaxString = tr("-Inf"); Chris@43: } else { Chris@907: dbMaxString = QString("%1").arg(lrint(dbMax)); Chris@43: } Chris@907: if (lrint(dbMin) != lrint(dbMax)) { Chris@199: text += tr("dB:\t%1 - %2").arg(dbMinString).arg(dbMaxString); Chris@25: } else { Chris@199: text += tr("dB:\t%1").arg(dbMinString); Chris@25: } Chris@38: if (phaseMin != phaseMax) { Chris@38: text += tr("\nPhase:\t%1 - %2").arg(phaseMin).arg(phaseMax); Chris@38: } else { Chris@38: text += tr("\nPhase:\t%1").arg(phaseMin); Chris@38: } Chris@25: } Chris@25: Chris@25: return text; Chris@0: } Chris@25: Chris@0: int Chris@40: SpectrogramLayer::getColourScaleWidth(QPainter &paint) const Chris@40: { Chris@40: int cw; Chris@40: Chris@119: cw = paint.fontMetrics().width("-80dB"); Chris@119: Chris@40: return cw; Chris@40: } Chris@40: Chris@40: int Chris@918: SpectrogramLayer::getVerticalScaleWidth(LayerGeometryProvider *, bool detailed, QPainter &paint) const Chris@0: { Chris@0: if (!m_model || !m_model->isOK()) return 0; Chris@0: Chris@607: int cw = 0; Chris@607: if (detailed) cw = getColourScaleWidth(paint); Chris@40: Chris@0: int tw = paint.fontMetrics().width(QString("%1") Chris@0: .arg(m_maxFrequency > 0 ? Chris@0: m_maxFrequency - 1 : Chris@0: m_model->getSampleRate() / 2)); Chris@0: Chris@234: int fw = paint.fontMetrics().width(tr("43Hz")); Chris@0: if (tw < fw) tw = fw; Chris@40: Chris@40: int tickw = (m_frequencyScale == LogFrequencyScale ? 10 : 4); Chris@0: Chris@40: return cw + tickw + tw + 13; Chris@0: } Chris@0: Chris@0: void Chris@918: SpectrogramLayer::paintVerticalScale(LayerGeometryProvider *v, bool detailed, QPainter &paint, QRect rect) const Chris@0: { Chris@0: if (!m_model || !m_model->isOK()) { Chris@0: return; Chris@0: } Chris@0: Chris@382: Profiler profiler("SpectrogramLayer::paintVerticalScale"); Chris@122: Chris@120: //!!! cache this? Chris@120: Chris@0: int h = rect.height(), w = rect.width(); Chris@0: Chris@40: int tickw = (m_frequencyScale == LogFrequencyScale ? 10 : 4); Chris@40: int pkw = (m_frequencyScale == LogFrequencyScale ? 10 : 0); Chris@40: Chris@805: int bins = m_fftSize / 2; Chris@907: sv_samplerate_t sr = m_model->getSampleRate(); Chris@0: Chris@0: if (m_maxFrequency > 0) { Chris@107: bins = int((double(m_maxFrequency) * m_fftSize) / sr + 0.1); Chris@107: if (bins > m_fftSize / 2) bins = m_fftSize / 2; Chris@0: } Chris@0: Chris@607: int cw = 0; Chris@607: Chris@607: if (detailed) cw = getColourScaleWidth(paint); Chris@119: int cbw = paint.fontMetrics().width("dB"); Chris@40: Chris@0: int py = -1; Chris@0: int textHeight = paint.fontMetrics().height(); Chris@0: int toff = -textHeight + paint.fontMetrics().ascent() + 2; Chris@0: Chris@607: if (detailed && (h > textHeight * 3 + 10)) { Chris@119: Chris@119: int topLines = 2; Chris@119: if (m_colourScale == PhaseColourScale) topLines = 1; Chris@119: Chris@119: int ch = h - textHeight * (topLines + 1) - 8; Chris@119: // paint.drawRect(4, textHeight + 4, cw - 1, ch + 1); Chris@119: paint.drawRect(4 + cw - cbw, textHeight * topLines + 4, cbw - 1, ch + 1); Chris@40: Chris@40: QString top, bottom; Chris@1030: double min = m_viewMags[v->getId()].getMin(); Chris@1030: double max = m_viewMags[v->getId()].getMax(); Chris@905: Chris@905: double dBmin = AudioLevel::multiplier_to_dB(min); Chris@905: double dBmax = AudioLevel::multiplier_to_dB(max); Chris@119: Chris@1044: #ifdef DEBUG_SPECTROGRAM_REPAINT Chris@1044: cerr << "paintVerticalScale: for view id " << v->getId() Chris@1044: << ": min = " << min << ", max = " << max Chris@1044: << ", dBmin = " << dBmin << ", dBmax = " << dBmax << endl; Chris@1044: #endif Chris@1044: Chris@120: if (dBmax < -60.f) dBmax = -60.f; Chris@907: else top = QString("%1").arg(lrint(dBmax)); Chris@120: Chris@120: if (dBmin < dBmax - 60.f) dBmin = dBmax - 60.f; Chris@907: bottom = QString("%1").arg(lrint(dBmin)); Chris@119: Chris@119: //!!! & phase etc Chris@119: Chris@119: if (m_colourScale != PhaseColourScale) { Chris@119: paint.drawText((cw + 6 - paint.fontMetrics().width("dBFS")) / 2, Chris@119: 2 + textHeight + toff, "dBFS"); Chris@119: } Chris@119: Chris@119: // paint.drawText((cw + 6 - paint.fontMetrics().width(top)) / 2, Chris@119: paint.drawText(3 + cw - cbw - paint.fontMetrics().width(top), Chris@119: 2 + textHeight * topLines + toff + textHeight/2, top); Chris@119: Chris@119: paint.drawText(3 + cw - cbw - paint.fontMetrics().width(bottom), Chris@119: h + toff - 3 - textHeight/2, bottom); Chris@40: Chris@40: paint.save(); Chris@40: paint.setBrush(Qt::NoBrush); Chris@119: Chris@119: int lasty = 0; Chris@119: int lastdb = 0; Chris@119: Chris@40: for (int i = 0; i < ch; ++i) { Chris@119: Chris@905: double dBval = dBmin + (((dBmax - dBmin) * i) / (ch - 1)); Chris@119: int idb = int(dBval); Chris@119: Chris@905: double value = AudioLevel::dB_to_multiplier(dBval); Chris@119: int colour = getDisplayValue(v, value * m_gain); Chris@210: Chris@907: paint.setPen(m_palette.getColour((unsigned char)colour)); Chris@119: Chris@119: int y = textHeight * topLines + 4 + ch - i; Chris@119: Chris@119: paint.drawLine(5 + cw - cbw, y, cw + 2, y); Chris@119: Chris@119: if (i == 0) { Chris@119: lasty = y; Chris@119: lastdb = idb; Chris@119: } else if (i < ch - paint.fontMetrics().ascent() && Chris@120: idb != lastdb && Chris@119: ((abs(y - lasty) > textHeight && Chris@119: idb % 10 == 0) || Chris@119: (abs(y - lasty) > paint.fontMetrics().ascent() && Chris@119: idb % 5 == 0))) { Chris@287: paint.setPen(v->getBackground()); Chris@119: QString text = QString("%1").arg(idb); Chris@119: paint.drawText(3 + cw - cbw - paint.fontMetrics().width(text), Chris@119: y + toff + textHeight/2, text); Chris@287: paint.setPen(v->getForeground()); Chris@119: paint.drawLine(5 + cw - cbw, y, 8 + cw - cbw, y); Chris@119: lasty = y; Chris@119: lastdb = idb; Chris@119: } Chris@40: } Chris@40: paint.restore(); Chris@40: } Chris@40: Chris@40: paint.drawLine(cw + 7, 0, cw + 7, h); Chris@40: Chris@0: int bin = -1; Chris@0: Chris@918: for (int y = 0; y < v->getPaintHeight(); ++y) { Chris@0: Chris@905: double q0, q1; Chris@918: if (!getYBinRange(v, v->getPaintHeight() - y, q0, q1)) continue; Chris@0: Chris@0: int vy; Chris@0: Chris@0: if (int(q0) > bin) { Chris@0: vy = y; Chris@0: bin = int(q0); Chris@0: } else { Chris@0: continue; Chris@0: } Chris@0: Chris@907: int freq = int((sr * bin) / m_fftSize); Chris@0: Chris@0: if (py >= 0 && (vy - py) < textHeight - 1) { Chris@40: if (m_frequencyScale == LinearFrequencyScale) { Chris@40: paint.drawLine(w - tickw, h - vy, w, h - vy); Chris@40: } Chris@0: continue; Chris@0: } Chris@0: Chris@0: QString text = QString("%1").arg(freq); Chris@234: if (bin == 1) text = tr("%1Hz").arg(freq); // bin 0 is DC Chris@40: paint.drawLine(cw + 7, h - vy, w - pkw - 1, h - vy); Chris@0: Chris@0: if (h - vy - textHeight >= -2) { Chris@1025: int tx = w - 3 - paint.fontMetrics().width(text) - max(tickw, pkw); Chris@0: paint.drawText(tx, h - vy + toff, text); Chris@0: } Chris@0: Chris@0: py = vy; Chris@0: } Chris@40: Chris@40: if (m_frequencyScale == LogFrequencyScale) { Chris@40: Chris@277: // piano keyboard Chris@277: Chris@690: PianoScale().paintPianoVertical Chris@690: (v, paint, QRect(w - pkw - 1, 0, pkw, h), Chris@690: getEffectiveMinFrequency(), getEffectiveMaxFrequency()); Chris@40: } Chris@608: Chris@608: m_haveDetailedScale = detailed; Chris@0: } Chris@0: Chris@187: class SpectrogramRangeMapper : public RangeMapper Chris@187: { Chris@187: public: Chris@901: SpectrogramRangeMapper(sv_samplerate_t sr, int /* fftsize */) : Chris@901: m_dist(sr / 2), Chris@901: m_s2(sqrt(sqrt(2))) { } Chris@187: ~SpectrogramRangeMapper() { } Chris@187: Chris@901: virtual int getPositionForValue(double value) const { Chris@901: Chris@901: double dist = m_dist; Chris@187: Chris@187: int n = 0; Chris@187: Chris@901: while (dist > (value + 0.00001) && dist > 0.1) { Chris@187: dist /= m_s2; Chris@187: ++n; Chris@187: } Chris@187: Chris@187: return n; Chris@187: } Chris@724: Chris@901: virtual int getPositionForValueUnclamped(double value) const { Chris@724: // We don't really support this Chris@724: return getPositionForValue(value); Chris@724: } Chris@187: Chris@901: virtual double getValueForPosition(int position) const { Chris@187: Chris@187: // Vertical zoom step 0 shows the entire range from DC -> Chris@187: // Nyquist frequency. Step 1 shows 2^(1/4) of the range of Chris@187: // step 0, and so on until the visible range is smaller than Chris@187: // the frequency step between bins at the current fft size. Chris@187: Chris@901: double dist = m_dist; Chris@187: Chris@187: int n = 0; Chris@187: while (n < position) { Chris@187: dist /= m_s2; Chris@187: ++n; Chris@187: } Chris@187: Chris@187: return dist; Chris@187: } Chris@187: Chris@901: virtual double getValueForPositionUnclamped(int position) const { Chris@724: // We don't really support this Chris@724: return getValueForPosition(position); Chris@724: } Chris@724: Chris@187: virtual QString getUnit() const { return "Hz"; } Chris@187: Chris@187: protected: Chris@901: double m_dist; Chris@901: double m_s2; Chris@187: }; Chris@187: Chris@133: int Chris@133: SpectrogramLayer::getVerticalZoomSteps(int &defaultStep) const Chris@133: { Chris@135: if (!m_model) return 0; Chris@187: Chris@907: sv_samplerate_t sr = m_model->getSampleRate(); Chris@187: Chris@187: SpectrogramRangeMapper mapper(sr, m_fftSize); Chris@187: Chris@905: // int maxStep = mapper.getPositionForValue((double(sr) / m_fftSize) + 0.001); Chris@187: int maxStep = mapper.getPositionForValue(0); Chris@905: int minStep = mapper.getPositionForValue(double(sr) / 2); Chris@250: Chris@805: int initialMax = m_initialMaxFrequency; Chris@907: if (initialMax == 0) initialMax = int(sr / 2); Chris@250: Chris@250: defaultStep = mapper.getPositionForValue(initialMax) - minStep; Chris@250: Chris@587: // SVDEBUG << "SpectrogramLayer::getVerticalZoomSteps: " << maxStep - minStep << " (" << maxStep <<"-" << minStep << "), default is " << defaultStep << " (from initial max freq " << initialMax << ")" << endl; Chris@187: Chris@187: return maxStep - minStep; Chris@133: } Chris@133: Chris@133: int Chris@133: SpectrogramLayer::getCurrentVerticalZoomStep() const Chris@133: { Chris@133: if (!m_model) return 0; Chris@133: Chris@905: double dmin, dmax; Chris@133: getDisplayExtents(dmin, dmax); Chris@133: Chris@187: SpectrogramRangeMapper mapper(m_model->getSampleRate(), m_fftSize); Chris@187: int n = mapper.getPositionForValue(dmax - dmin); Chris@587: // SVDEBUG << "SpectrogramLayer::getCurrentVerticalZoomStep: " << n << endl; Chris@133: return n; Chris@133: } Chris@133: Chris@133: void Chris@133: SpectrogramLayer::setVerticalZoomStep(int step) Chris@133: { Chris@187: if (!m_model) return; Chris@187: Chris@905: double dmin = m_minFrequency, dmax = m_maxFrequency; Chris@253: // getDisplayExtents(dmin, dmax); Chris@253: Chris@682: // cerr << "current range " << dmin << " -> " << dmax << ", range " << dmax-dmin << ", mid " << (dmax + dmin)/2 << endl; Chris@133: Chris@907: sv_samplerate_t sr = m_model->getSampleRate(); Chris@187: SpectrogramRangeMapper mapper(sr, m_fftSize); Chris@905: double newdist = mapper.getValueForPosition(step); Chris@905: Chris@905: double newmin, newmax; Chris@253: Chris@253: if (m_frequencyScale == LogFrequencyScale) { Chris@253: Chris@253: // need to pick newmin and newmax such that Chris@253: // Chris@253: // (log(newmin) + log(newmax)) / 2 == logmid Chris@253: // and Chris@253: // newmax - newmin = newdist Chris@253: // Chris@253: // so log(newmax - newdist) + log(newmax) == 2logmid Chris@253: // log(newmax(newmax - newdist)) == 2logmid Chris@253: // newmax.newmax - newmax.newdist == exp(2logmid) Chris@253: // newmax^2 + (-newdist)newmax + -exp(2logmid) == 0 Chris@253: // quadratic with a = 1, b = -newdist, c = -exp(2logmid), all known Chris@253: // Chris@253: // positive root Chris@253: // newmax = (newdist + sqrt(newdist^2 + 4exp(2logmid))) / 2 Chris@253: // Chris@253: // but logmid = (log(dmin) + log(dmax)) / 2 Chris@253: // so exp(2logmid) = exp(log(dmin) + log(dmax)) Chris@253: // = exp(log(dmin.dmax)) Chris@253: // = dmin.dmax Chris@253: // so newmax = (newdist + sqrtf(newdist^2 + 4dmin.dmax)) / 2 Chris@253: Chris@907: newmax = (newdist + sqrt(newdist*newdist + 4*dmin*dmax)) / 2; Chris@253: newmin = newmax - newdist; Chris@253: Chris@682: // cerr << "newmin = " << newmin << ", newmax = " << newmax << endl; Chris@253: Chris@253: } else { Chris@905: double dmid = (dmax + dmin) / 2; Chris@253: newmin = dmid - newdist / 2; Chris@253: newmax = dmid + newdist / 2; Chris@253: } Chris@187: Chris@905: double mmin, mmax; Chris@187: mmin = 0; Chris@905: mmax = double(sr) / 2; Chris@133: Chris@187: if (newmin < mmin) { Chris@187: newmax += (mmin - newmin); Chris@187: newmin = mmin; Chris@187: } Chris@187: if (newmax > mmax) { Chris@187: newmax = mmax; Chris@187: } Chris@133: Chris@587: // SVDEBUG << "SpectrogramLayer::setVerticalZoomStep: " << step << ": " << newmin << " -> " << newmax << " (range " << newdist << ")" << endl; Chris@253: Chris@907: setMinFrequency(int(lrint(newmin))); Chris@907: setMaxFrequency(int(lrint(newmax))); Chris@187: } Chris@187: Chris@187: RangeMapper * Chris@187: SpectrogramLayer::getNewVerticalZoomRangeMapper() const Chris@187: { Chris@187: if (!m_model) return 0; Chris@187: return new SpectrogramRangeMapper(m_model->getSampleRate(), m_fftSize); Chris@133: } Chris@133: Chris@273: void Chris@918: SpectrogramLayer::updateMeasureRectYCoords(LayerGeometryProvider *v, const MeasureRect &r) const Chris@273: { Chris@273: int y0 = 0; Chris@907: if (r.startY > 0.0) y0 = int(getYForFrequency(v, r.startY)); Chris@273: Chris@273: int y1 = y0; Chris@907: if (r.endY > 0.0) y1 = int(getYForFrequency(v, r.endY)); Chris@273: Chris@587: // SVDEBUG << "SpectrogramLayer::updateMeasureRectYCoords: start " << r.startY << " -> " << y0 << ", end " << r.endY << " -> " << y1 << endl; Chris@273: Chris@273: r.pixrect = QRect(r.pixrect.x(), y0, r.pixrect.width(), y1 - y0); Chris@273: } Chris@273: Chris@273: void Chris@918: SpectrogramLayer::setMeasureRectYCoord(LayerGeometryProvider *v, MeasureRect &r, bool start, int y) const Chris@273: { Chris@273: if (start) { Chris@273: r.startY = getFrequencyForY(v, y); Chris@273: r.endY = r.startY; Chris@273: } else { Chris@273: r.endY = getFrequencyForY(v, y); Chris@273: } Chris@587: // SVDEBUG << "SpectrogramLayer::setMeasureRectYCoord: start " << r.startY << " <- " << y << ", end " << r.endY << " <- " << y << endl; Chris@273: Chris@273: } Chris@273: Chris@316: void Chris@316: SpectrogramLayer::toXml(QTextStream &stream, Chris@316: QString indent, QString extraAttributes) const Chris@6: { Chris@6: QString s; Chris@6: Chris@6: s += QString("channel=\"%1\" " Chris@6: "windowSize=\"%2\" " Chris@153: "windowHopLevel=\"%3\" " Chris@153: "gain=\"%4\" " Chris@153: "threshold=\"%5\" ") Chris@6: .arg(m_channel) Chris@6: .arg(m_windowSize) Chris@97: .arg(m_windowHopLevel) Chris@37: .arg(m_gain) Chris@37: .arg(m_threshold); Chris@37: Chris@37: s += QString("minFrequency=\"%1\" " Chris@37: "maxFrequency=\"%2\" " Chris@37: "colourScale=\"%3\" " Chris@37: "colourScheme=\"%4\" " Chris@37: "colourRotation=\"%5\" " Chris@37: "frequencyScale=\"%6\" " Chris@761: "binDisplay=\"%7\" ") Chris@37: .arg(m_minFrequency) Chris@6: .arg(m_maxFrequency) Chris@6: .arg(m_colourScale) Chris@197: .arg(m_colourMap) Chris@37: .arg(m_colourRotation) Chris@35: .arg(m_frequencyScale) Chris@761: .arg(m_binDisplay); Chris@761: Chris@1009: // New-style normalization attributes, allowing for more types of Chris@1009: // normalization in future: write out the column normalization Chris@1009: // type separately, and then whether we are normalizing visible Chris@1009: // area as well afterwards Chris@1009: Chris@1009: s += QString("columnNormalization=\"%1\" ") Chris@1009: .arg(m_normalization == NormalizeColumns ? "peak" : Chris@1009: m_normalization == NormalizeHybrid ? "hybrid" : "none"); Chris@1009: Chris@1009: // Old-style normalization attribute. We *don't* write out Chris@1009: // normalizeHybrid here because the only release that would accept Chris@1009: // it (Tony v1.0) has a totally different scale factor for Chris@1009: // it. We'll just have to accept that session files from Tony Chris@1009: // v2.0+ will look odd in Tony v1.0 Chris@1009: Chris@1009: s += QString("normalizeColumns=\"%1\" ") Chris@1009: .arg(m_normalization == NormalizeColumns ? "true" : "false"); Chris@1009: Chris@1009: // And this applies to both old- and new-style attributes Chris@1009: Chris@1009: s += QString("normalizeVisibleArea=\"%1\" ") Chris@1009: .arg(m_normalization == NormalizeVisibleArea ? "true" : "false"); Chris@1009: Chris@316: Layer::toXml(stream, indent, extraAttributes + " " + s); Chris@6: } Chris@6: Chris@11: void Chris@11: SpectrogramLayer::setProperties(const QXmlAttributes &attributes) Chris@11: { Chris@11: bool ok = false; Chris@11: Chris@11: int channel = attributes.value("channel").toInt(&ok); Chris@11: if (ok) setChannel(channel); Chris@11: Chris@805: int windowSize = attributes.value("windowSize").toUInt(&ok); Chris@11: if (ok) setWindowSize(windowSize); Chris@11: Chris@805: int windowHopLevel = attributes.value("windowHopLevel").toUInt(&ok); Chris@97: if (ok) setWindowHopLevel(windowHopLevel); Chris@97: else { Chris@805: int windowOverlap = attributes.value("windowOverlap").toUInt(&ok); Chris@97: // a percentage value Chris@97: if (ok) { Chris@97: if (windowOverlap == 0) setWindowHopLevel(0); Chris@97: else if (windowOverlap == 25) setWindowHopLevel(1); Chris@97: else if (windowOverlap == 50) setWindowHopLevel(2); Chris@97: else if (windowOverlap == 75) setWindowHopLevel(3); Chris@97: else if (windowOverlap == 90) setWindowHopLevel(4); Chris@97: } Chris@97: } Chris@11: Chris@11: float gain = attributes.value("gain").toFloat(&ok); Chris@11: if (ok) setGain(gain); Chris@11: Chris@37: float threshold = attributes.value("threshold").toFloat(&ok); Chris@37: if (ok) setThreshold(threshold); Chris@37: Chris@805: int minFrequency = attributes.value("minFrequency").toUInt(&ok); Chris@187: if (ok) { Chris@587: SVDEBUG << "SpectrogramLayer::setProperties: setting min freq to " << minFrequency << endl; Chris@187: setMinFrequency(minFrequency); Chris@187: } Chris@37: Chris@805: int maxFrequency = attributes.value("maxFrequency").toUInt(&ok); Chris@187: if (ok) { Chris@587: SVDEBUG << "SpectrogramLayer::setProperties: setting max freq to " << maxFrequency << endl; Chris@187: setMaxFrequency(maxFrequency); Chris@187: } Chris@11: Chris@11: ColourScale colourScale = (ColourScale) Chris@11: attributes.value("colourScale").toInt(&ok); Chris@11: if (ok) setColourScale(colourScale); Chris@11: Chris@197: int colourMap = attributes.value("colourScheme").toInt(&ok); Chris@197: if (ok) setColourMap(colourMap); Chris@11: Chris@37: int colourRotation = attributes.value("colourRotation").toInt(&ok); Chris@37: if (ok) setColourRotation(colourRotation); Chris@37: Chris@11: FrequencyScale frequencyScale = (FrequencyScale) Chris@11: attributes.value("frequencyScale").toInt(&ok); Chris@11: if (ok) setFrequencyScale(frequencyScale); Chris@35: Chris@37: BinDisplay binDisplay = (BinDisplay) Chris@37: attributes.value("binDisplay").toInt(&ok); Chris@37: if (ok) setBinDisplay(binDisplay); Chris@36: Chris@1009: bool haveNewStyleNormalization = false; Chris@1009: Chris@1009: QString columnNormalization = attributes.value("columnNormalization"); Chris@1009: Chris@1009: if (columnNormalization != "") { Chris@1009: Chris@1009: haveNewStyleNormalization = true; Chris@1009: Chris@1009: if (columnNormalization == "peak") { Chris@1009: setNormalization(NormalizeColumns); Chris@1009: } else if (columnNormalization == "hybrid") { Chris@1009: setNormalization(NormalizeHybrid); Chris@1009: } else if (columnNormalization == "none") { Chris@1009: // do nothing Chris@1009: } else { Chris@1009: cerr << "NOTE: Unknown or unsupported columnNormalization attribute \"" Chris@1009: << columnNormalization << "\"" << endl; Chris@1009: } Chris@1009: } Chris@1009: Chris@1009: if (!haveNewStyleNormalization) { Chris@1009: Chris@1009: bool normalizeColumns = Chris@1009: (attributes.value("normalizeColumns").trimmed() == "true"); Chris@1009: if (normalizeColumns) { Chris@1009: setNormalization(NormalizeColumns); Chris@1009: } Chris@1009: Chris@1009: bool normalizeHybrid = Chris@1009: (attributes.value("normalizeHybrid").trimmed() == "true"); Chris@1009: if (normalizeHybrid) { Chris@1009: setNormalization(NormalizeHybrid); Chris@1009: } Chris@862: } Chris@153: Chris@153: bool normalizeVisibleArea = Chris@153: (attributes.value("normalizeVisibleArea").trimmed() == "true"); Chris@862: if (normalizeVisibleArea) { Chris@862: setNormalization(NormalizeVisibleArea); Chris@862: } Chris@761: Chris@1009: if (!haveNewStyleNormalization && m_normalization == NormalizeHybrid) { Chris@1009: // Tony v1.0 is (and hopefully will remain!) the only released Chris@1009: // SV-a-like to use old-style attributes when saving sessions Chris@1009: // that ask for hybrid normalization. It saves them with the Chris@1009: // wrong gain factor, so hack in a fix for that here -- this Chris@1009: // gives us backward but not forward compatibility. Chris@1009: setGain(m_gain / float(m_fftSize / 2)); Chris@862: } Chris@11: } Chris@11: