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@59: This file copyright 2006 Chris Cannam. 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@0: #include "base/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@110: #include "fileio/FFTDataServer.h" Chris@0: Chris@0: #include Chris@0: #include Chris@0: #include Chris@0: #include Chris@0: #include Chris@92: #include Chris@0: Chris@0: #include Chris@0: Chris@0: #include Chris@0: #include Chris@0: Chris@119: //#define DEBUG_SPECTROGRAM_REPAINT 1 Chris@0: Chris@44: SpectrogramLayer::SpectrogramLayer(Configuration config) : Chris@44: Layer(), 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@37: m_threshold(0.0), Chris@9: m_colourRotation(0), Chris@119: m_minFrequency(10), Chris@0: m_maxFrequency(8000), Chris@0: m_colourScale(dBColourScale), Chris@0: m_colourScheme(DefaultColours), Chris@0: m_frequencyScale(LinearFrequencyScale), Chris@37: m_binDisplay(AllBins), Chris@36: m_normalizeColumns(false), Chris@120: m_normalizeVisibleArea(false), Chris@0: m_updateTimer(0), Chris@44: m_candidateFillStartFrame(0), Chris@0: m_exiting(false) Chris@0: { Chris@0: if (config == MelodicRange) { Chris@0: setWindowSize(8192); Chris@97: setWindowHopLevel(4); Chris@109: // setWindowType(ParzenWindow); Chris@0: setMaxFrequency(1000); Chris@0: setColourScale(LinearColourScale); Chris@37: } else if (config == MelodicPeaks) { Chris@37: setWindowSize(4096); Chris@97: setWindowHopLevel(5); Chris@109: // setWindowType(BlackmanWindow); Chris@40: setMaxFrequency(2000); Chris@37: setMinFrequency(40); Chris@37: setFrequencyScale(LogFrequencyScale); Chris@41: setColourScale(MeterColourScale); Chris@37: setBinDisplay(PeakFrequencies); Chris@37: setNormalizeColumns(true); Chris@0: } Chris@110: 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@110: setColourmap(); Chris@0: } Chris@0: Chris@0: SpectrogramLayer::~SpectrogramLayer() Chris@0: { Chris@0: delete m_updateTimer; Chris@0: m_updateTimer = 0; Chris@0: Chris@114: invalidateFFTAdapters(); Chris@0: } Chris@0: Chris@0: void Chris@0: SpectrogramLayer::setModel(const DenseTimeValueModel *model) Chris@0: { Chris@101: // std::cerr << "SpectrogramLayer(" << this << "): setModel(" << model << ")" << std::endl; Chris@34: Chris@110: if (model == m_model) return; Chris@110: Chris@0: m_model = model; Chris@114: invalidateFFTAdapters(); Chris@0: Chris@0: if (!m_model || !m_model->isOK()) return; Chris@0: Chris@0: connect(m_model, SIGNAL(modelChanged()), this, SIGNAL(modelChanged())); Chris@0: connect(m_model, SIGNAL(modelChanged(size_t, size_t)), Chris@0: this, SIGNAL(modelChanged(size_t, size_t))); Chris@0: Chris@0: connect(m_model, SIGNAL(completionChanged()), Chris@0: this, SIGNAL(modelCompletionChanged())); Chris@0: Chris@0: connect(m_model, SIGNAL(modelChanged()), this, SLOT(cacheInvalid())); Chris@0: connect(m_model, SIGNAL(modelChanged(size_t, size_t)), Chris@0: this, SLOT(cacheInvalid(size_t, size_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@109: // list.push_back("Window Type"); Chris@87: list.push_back("Window Size"); Chris@97: list.push_back("Window Increment"); Chris@87: list.push_back("Normalize Columns"); Chris@120: list.push_back("Normalize Visible Area"); 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@87: list.push_back("Min Frequency"); Chris@87: list.push_back("Max Frequency"); Chris@87: list.push_back("Frequency Scale"); Chris@111: // 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 Type") return tr("Window Type"); Chris@87: if (name == "Window Size") return tr("Window Size"); Chris@112: if (name == "Window Increment") return tr("Window Overlap"); Chris@87: if (name == "Normalize Columns") return tr("Normalize Columns"); Chris@120: if (name == "Normalize Visible Area") return tr("Normalize Visible Area"); 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@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 == "Normalize Columns") return ToggleProperty; Chris@120: if (name == "Normalize Visible Area") return ToggleProperty; 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@87: if (name == "Window Size" || Chris@87: name == "Window Type" || Chris@109: name == "Window Increment" || Chris@109: name == "Zero Padding") return tr("Window"); Chris@87: if (name == "Colour" || Chris@87: name == "Gain" || Chris@87: name == "Threshold" || Chris@87: name == "Colour Rotation") return tr("Colour"); Chris@87: if (name == "Normalize Columns" || Chris@120: name == "Normalize Visible Area" || Chris@87: name == "Bin Display" || Chris@87: name == "Colour Scale") return tr("Scale"); Chris@87: if (name == "Max Frequency" || Chris@87: name == "Min Frequency" || Chris@87: name == "Frequency Scale" || Chris@87: name == "Frequency Adjustment") return tr("Range"); Chris@0: return QString(); Chris@0: } Chris@0: Chris@0: int Chris@0: SpectrogramLayer::getPropertyRangeAndValue(const PropertyName &name, Chris@55: int *min, int *max) const Chris@0: { Chris@0: int deft = 0; Chris@0: Chris@55: int garbage0, garbage1; Chris@55: if (!min) min = &garbage0; Chris@55: if (!max) max = &garbage1; Chris@10: Chris@87: if (name == "Gain") { Chris@0: Chris@0: *min = -50; Chris@0: *max = 50; Chris@0: Chris@0: deft = lrint(log10(m_gain) * 20.0); Chris@0: if (deft < *min) deft = *min; Chris@0: if (deft > *max) deft = *max; Chris@0: Chris@87: } else if (name == "Threshold") { Chris@37: Chris@37: *min = -50; Chris@37: *max = 0; Chris@37: Chris@37: deft = lrintf(AudioLevel::multiplier_to_dB(m_threshold)); Chris@37: if (deft < *min) deft = *min; Chris@37: if (deft > *max) deft = *max; Chris@37: Chris@87: } else if (name == "Colour Rotation") { Chris@9: Chris@9: *min = 0; Chris@9: *max = 256; Chris@9: Chris@9: deft = m_colourRotation; Chris@9: Chris@87: } else if (name == "Colour Scale") { Chris@0: Chris@0: *min = 0; Chris@0: *max = 3; Chris@0: Chris@0: deft = (int)m_colourScale; Chris@0: Chris@87: } else if (name == "Colour") { Chris@0: Chris@0: *min = 0; Chris@71: *max = 6; Chris@0: Chris@0: deft = (int)m_colourScheme; Chris@0: Chris@87: } else if (name == "Window Type") { Chris@0: Chris@0: *min = 0; Chris@0: *max = 6; Chris@0: Chris@0: deft = (int)m_windowType; Chris@0: Chris@87: } else if (name == "Window Size") { Chris@0: Chris@0: *min = 0; Chris@0: *max = 10; Chris@0: Chris@0: deft = 0; Chris@0: int ws = m_windowSize; Chris@0: while (ws > 32) { ws >>= 1; deft ++; } Chris@0: Chris@97: } else if (name == "Window Increment") { Chris@0: Chris@0: *min = 0; Chris@97: *max = 5; Chris@0: Chris@97: deft = m_windowHopLevel; Chris@0: Chris@109: } else if (name == "Zero Padding") { Chris@109: Chris@109: *min = 0; Chris@109: *max = 1; Chris@109: Chris@109: deft = 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@37: Chris@37: switch (m_minFrequency) { Chris@37: case 0: default: deft = 0; break; Chris@37: case 10: deft = 1; break; Chris@37: case 20: deft = 2; break; Chris@37: case 40: deft = 3; break; Chris@37: case 100: deft = 4; break; Chris@37: case 250: deft = 5; break; Chris@37: case 500: deft = 6; break; Chris@37: case 1000: deft = 7; break; Chris@37: case 4000: deft = 8; break; Chris@37: case 10000: deft = 9; break; Chris@37: } Chris@37: Chris@87: } else if (name == "Max Frequency") { Chris@0: Chris@0: *min = 0; Chris@0: *max = 9; Chris@0: Chris@0: switch (m_maxFrequency) { Chris@0: case 500: deft = 0; break; Chris@0: case 1000: deft = 1; break; Chris@0: case 1500: deft = 2; break; Chris@0: case 2000: deft = 3; break; Chris@0: case 4000: deft = 4; break; Chris@0: case 6000: deft = 5; break; Chris@0: case 8000: deft = 6; break; Chris@0: case 12000: deft = 7; break; Chris@0: case 16000: deft = 8; break; Chris@0: default: deft = 9; break; Chris@0: } Chris@0: Chris@87: } else if (name == "Frequency Scale") { Chris@0: Chris@0: *min = 0; Chris@0: *max = 1; Chris@0: deft = (int)m_frequencyScale; Chris@0: Chris@87: } else if (name == "Bin Display") { Chris@35: Chris@35: *min = 0; Chris@35: *max = 2; Chris@37: deft = (int)m_binDisplay; Chris@35: Chris@87: } else if (name == "Normalize Columns") { Chris@36: Chris@36: deft = (m_normalizeColumns ? 1 : 0); Chris@36: Chris@120: } else if (name == "Normalize Visible Area") { Chris@120: Chris@120: deft = (m_normalizeVisibleArea ? 1 : 0); Chris@120: Chris@0: } else { Chris@0: deft = Layer::getPropertyRangeAndValue(name, min, max); Chris@0: } Chris@0: Chris@0: return deft; 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@0: switch (value) { Chris@0: default: Chris@0: case 0: return tr("Default"); Chris@0: case 1: return tr("White on Black"); Chris@0: case 2: return tr("Black on White"); Chris@0: case 3: return tr("Red on Blue"); Chris@0: case 4: return tr("Yellow on Black"); Chris@71: case 5: return tr("Blue on Black"); Chris@71: case 6: return tr("Fruit Salad"); Chris@0: } 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@37: case 2: return tr("dB"); Chris@119: case 3: return tr("Other"); Chris@119: case 4: return tr("Phase"); Chris@0: } Chris@0: } Chris@87: if (name == "Window Type") { Chris@0: switch ((WindowType)value) { Chris@0: default: Chris@35: case RectangularWindow: return tr("Rectangle"); Chris@0: case BartlettWindow: return tr("Bartlett"); Chris@0: case HammingWindow: return tr("Hamming"); Chris@0: case HanningWindow: return tr("Hanning"); Chris@0: case BlackmanWindow: return tr("Blackman"); Chris@0: case GaussianWindow: return tr("Gaussian"); Chris@0: case ParzenWindow: return tr("Parzen"); Chris@0: } Chris@0: } 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@0: void Chris@0: SpectrogramLayer::setProperty(const PropertyName &name, int value) Chris@0: { Chris@87: if (name == "Gain") { Chris@0: setGain(pow(10, float(value)/20.0)); Chris@87: } else if (name == "Threshold") { Chris@37: if (value == -50) setThreshold(0.0); Chris@37: else setThreshold(AudioLevel::dB_to_multiplier(value)); Chris@87: } else if (name == "Colour Rotation") { Chris@9: setColourRotation(value); Chris@87: } else if (name == "Colour") { Chris@0: switch (value) { Chris@0: default: Chris@0: case 0: setColourScheme(DefaultColours); break; Chris@0: case 1: setColourScheme(WhiteOnBlack); break; Chris@0: case 2: setColourScheme(BlackOnWhite); break; Chris@0: case 3: setColourScheme(RedOnBlue); break; Chris@0: case 4: setColourScheme(YellowOnBlack); break; Chris@71: case 5: setColourScheme(BlueOnBlack); break; Chris@71: case 6: setColourScheme(Rainbow); break; Chris@0: } Chris@87: } else if (name == "Window Type") { Chris@0: setWindowType(WindowType(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@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@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@0: case 2: setColourScale(dBColourScale); break; Chris@119: case 3: setColourScale(OtherColourScale); 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@82: } else if (name == "Normalize Columns") { Chris@36: setNormalizeColumns(value ? true : false); Chris@120: } else if (name == "Normalize Visible Area") { Chris@120: setNormalizeVisibleArea(value ? true : false); Chris@0: } Chris@0: } Chris@0: Chris@0: void Chris@95: SpectrogramLayer::invalidatePixmapCaches() Chris@95: { Chris@95: for (ViewPixmapCache::iterator i = m_pixmapCaches.begin(); Chris@95: i != m_pixmapCaches.end(); ++i) { Chris@95: i->second.validArea = QRect(); Chris@95: } Chris@95: } Chris@95: Chris@95: void Chris@95: SpectrogramLayer::invalidatePixmapCaches(size_t startFrame, size_t endFrame) Chris@95: { Chris@95: for (ViewPixmapCache::iterator i = m_pixmapCaches.begin(); Chris@95: i != m_pixmapCaches.end(); ++i) { Chris@95: //!!! when are views removed from the map? on setLayerDormant? Chris@95: const View *v = i->first; Chris@95: Chris@115: if (startFrame < v->getEndFrame() && int(endFrame) >= v->getStartFrame()) { Chris@95: i->second.validArea = QRect(); Chris@95: } Chris@95: } Chris@95: } Chris@95: Chris@95: void Chris@122: SpectrogramLayer::preferenceChanged(PropertyContainer::PropertyName name) Chris@122: { Chris@122: std::cerr << "SpectrogramLayer::preferenceChanged(" << name.toStdString() << ")" << std::endl; Chris@122: Chris@122: if (name == "Window Type") { Chris@122: setWindowType(Preferences::getInstance()->getWindowType()); Chris@122: return; Chris@122: } Chris@122: if (name == "Smooth Spectrogram") { Chris@122: invalidatePixmapCaches(); 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@95: invalidatePixmapCaches(); Chris@0: m_channel = ch; Chris@114: invalidateFFTAdapters(); 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@0: SpectrogramLayer::setWindowSize(size_t ws) Chris@0: { Chris@0: if (m_windowSize == ws) return; Chris@0: Chris@95: invalidatePixmapCaches(); Chris@0: Chris@0: m_windowSize = ws; Chris@109: m_fftSize = ws * (m_zeroPadLevel + 1); Chris@0: Chris@114: invalidateFFTAdapters(); Chris@9: Chris@9: emit layerParametersChanged(); Chris@0: } Chris@0: Chris@0: size_t Chris@0: SpectrogramLayer::getWindowSize() const Chris@0: { Chris@0: return m_windowSize; Chris@0: } Chris@0: Chris@0: void Chris@97: SpectrogramLayer::setWindowHopLevel(size_t v) Chris@0: { Chris@97: if (m_windowHopLevel == v) return; Chris@0: Chris@95: invalidatePixmapCaches(); Chris@0: Chris@97: m_windowHopLevel = v; Chris@0: Chris@114: invalidateFFTAdapters(); Chris@9: Chris@9: emit layerParametersChanged(); Chris@9: Chris@110: // fillCache(); Chris@0: } Chris@0: Chris@0: size_t Chris@97: SpectrogramLayer::getWindowHopLevel() const Chris@0: { Chris@97: return m_windowHopLevel; Chris@0: } Chris@0: Chris@0: void Chris@109: SpectrogramLayer::setZeroPadLevel(size_t v) Chris@109: { Chris@109: if (m_zeroPadLevel == v) return; Chris@109: Chris@109: invalidatePixmapCaches(); Chris@109: Chris@109: m_zeroPadLevel = v; Chris@109: m_fftSize = m_windowSize * (v + 1); Chris@110: Chris@114: invalidateFFTAdapters(); Chris@109: Chris@109: emit layerParametersChanged(); Chris@109: } Chris@109: Chris@109: size_t 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@95: invalidatePixmapCaches(); Chris@0: Chris@0: m_windowType = w; Chris@110: Chris@114: invalidateFFTAdapters(); 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@101: // std::cerr << "SpectrogramLayer::setGain(" << gain << ") (my gain is now " Chris@101: // << m_gain << ")" << std::endl; Chris@55: Chris@40: if (m_gain == gain) return; Chris@0: Chris@95: invalidatePixmapCaches(); 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@95: invalidatePixmapCaches(); 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@37: SpectrogramLayer::setMinFrequency(size_t mf) Chris@37: { Chris@37: if (m_minFrequency == mf) return; Chris@37: Chris@95: invalidatePixmapCaches(); Chris@119: invalidateMagnitudes(); Chris@37: Chris@37: m_minFrequency = mf; Chris@37: Chris@37: emit layerParametersChanged(); Chris@37: } Chris@37: Chris@37: size_t Chris@37: SpectrogramLayer::getMinFrequency() const Chris@37: { Chris@37: return m_minFrequency; Chris@37: } Chris@37: Chris@37: void Chris@0: SpectrogramLayer::setMaxFrequency(size_t mf) Chris@0: { Chris@0: if (m_maxFrequency == mf) return; Chris@0: Chris@95: invalidatePixmapCaches(); Chris@119: invalidateMagnitudes(); Chris@0: Chris@0: m_maxFrequency = mf; Chris@0: Chris@9: emit layerParametersChanged(); Chris@0: } Chris@0: Chris@0: size_t 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@95: invalidatePixmapCaches(); 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@90: rotateColourmap(-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@95: invalidatePixmapCaches(); 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@0: SpectrogramLayer::setColourScheme(ColourScheme scheme) Chris@0: { Chris@0: if (m_colourScheme == scheme) return; Chris@0: Chris@95: invalidatePixmapCaches(); Chris@0: Chris@0: m_colourScheme = scheme; Chris@90: setColourmap(); Chris@9: Chris@0: emit layerParametersChanged(); Chris@0: } Chris@0: Chris@0: SpectrogramLayer::ColourScheme Chris@0: SpectrogramLayer::getColourScheme() const Chris@0: { Chris@0: return m_colourScheme; 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@95: invalidatePixmapCaches(); 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@95: invalidatePixmapCaches(); 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@36: SpectrogramLayer::setNormalizeColumns(bool n) Chris@36: { Chris@36: if (m_normalizeColumns == n) return; Chris@36: Chris@95: invalidatePixmapCaches(); Chris@119: invalidateMagnitudes(); Chris@36: m_normalizeColumns = n; Chris@36: Chris@36: emit layerParametersChanged(); Chris@36: } Chris@36: Chris@36: bool Chris@36: SpectrogramLayer::getNormalizeColumns() const Chris@36: { Chris@36: return m_normalizeColumns; Chris@36: } Chris@36: Chris@36: void Chris@120: SpectrogramLayer::setNormalizeVisibleArea(bool n) Chris@120: { Chris@120: if (m_normalizeVisibleArea == n) return; Chris@120: Chris@120: invalidatePixmapCaches(); Chris@120: invalidateMagnitudes(); Chris@120: m_normalizeVisibleArea = n; Chris@120: Chris@120: emit layerParametersChanged(); Chris@120: } Chris@120: Chris@120: bool Chris@120: SpectrogramLayer::getNormalizeVisibleArea() const Chris@120: { Chris@120: return m_normalizeVisibleArea; Chris@120: } Chris@120: Chris@120: void Chris@47: SpectrogramLayer::setLayerDormant(const View *v, bool dormant) Chris@29: { Chris@47: if (dormant == m_dormancy[v]) return; Chris@33: Chris@33: if (dormant) { Chris@33: Chris@47: m_dormancy[v] = true; Chris@33: Chris@95: invalidatePixmapCaches(); Chris@95: m_pixmapCaches.erase(v); Chris@114: Chris@114: if (m_fftAdapters.find(v) != m_fftAdapters.end()) { Chris@115: delete m_fftAdapters[v].first; Chris@114: m_fftAdapters.erase(v); Chris@114: } Chris@33: Chris@33: } else { Chris@33: Chris@47: m_dormancy[v] = false; Chris@33: } Chris@29: } Chris@29: Chris@29: void Chris@0: SpectrogramLayer::cacheInvalid() Chris@0: { Chris@95: invalidatePixmapCaches(); Chris@119: invalidateMagnitudes(); Chris@0: } Chris@0: Chris@0: void Chris@0: SpectrogramLayer::cacheInvalid(size_t, size_t) Chris@0: { Chris@0: // for now (or forever?) Chris@0: cacheInvalid(); Chris@0: } Chris@0: Chris@0: void Chris@0: SpectrogramLayer::fillTimerTimedOut() Chris@0: { Chris@115: if (!m_model) return; Chris@115: Chris@115: bool allDone = true; Chris@115: Chris@115: for (ViewFFTMap::iterator i = m_fftAdapters.begin(); Chris@115: i != m_fftAdapters.end(); ++i) { Chris@115: Chris@115: const View *v = i->first; Chris@115: const FFTFuzzyAdapter *adapter = i->second.first; Chris@115: size_t lastFill = i->second.second; Chris@115: Chris@115: if (adapter) { Chris@115: Chris@115: size_t fill = adapter->getFillExtent(); Chris@115: Chris@0: #ifdef DEBUG_SPECTROGRAM_REPAINT Chris@115: std::cerr << "SpectrogramLayer::fillTimerTimedOut: extent for " << adapter << ": " << fill << ", last " << lastFill << ", total " << m_model->getEndFrame() << std::endl; Chris@0: #endif Chris@115: Chris@115: if (fill >= lastFill) { Chris@115: if (fill >= m_model->getEndFrame() && lastFill > 0) { Chris@0: #ifdef DEBUG_SPECTROGRAM_REPAINT Chris@115: std::cerr << "complete!" << std::endl; Chris@0: #endif Chris@115: invalidatePixmapCaches(); Chris@115: emit modelChanged(); Chris@115: i->second.second = -1; Chris@115: Chris@115: } else if (fill > lastFill) { Chris@0: #ifdef DEBUG_SPECTROGRAM_REPAINT Chris@115: std::cerr << "SpectrogramLayer: emitting modelChanged(" Chris@115: << lastFill << "," << fill << ")" << std::endl; Chris@0: #endif Chris@115: invalidatePixmapCaches(lastFill, fill); Chris@115: emit modelChanged(lastFill, fill); Chris@115: i->second.second = fill; Chris@115: } Chris@115: } else { Chris@0: #ifdef DEBUG_SPECTROGRAM_REPAINT Chris@115: std::cerr << "SpectrogramLayer: going backwards, emitting modelChanged(" Chris@115: << m_model->getStartFrame() << "," << m_model->getEndFrame() << ")" << std::endl; Chris@0: #endif Chris@115: invalidatePixmapCaches(); Chris@115: emit modelChanged(m_model->getStartFrame(), m_model->getEndFrame()); Chris@115: i->second.second = fill; Chris@115: } Chris@115: Chris@115: if (i->second.second >= 0) { Chris@115: allDone = false; Chris@115: } Chris@115: } Chris@0: } Chris@115: Chris@115: if (allDone) { Chris@115: #ifdef DEBUG_SPECTROGRAM_REPAINT Chris@115: std::cerr << "SpectrogramLayer: all complete!" << std::endl; Chris@115: #endif Chris@115: delete m_updateTimer; Chris@115: m_updateTimer = 0; Chris@115: } Chris@0: } Chris@0: Chris@0: void Chris@90: SpectrogramLayer::setColourmap() Chris@0: { Chris@10: int formerRotation = m_colourRotation; Chris@10: Chris@38: if (m_colourScheme == BlackOnWhite) { Chris@86: m_colourMap.setColour(NO_VALUE, Qt::white); Chris@38: } else { Chris@86: m_colourMap.setColour(NO_VALUE, Qt::black); Chris@38: } Chris@0: Chris@0: for (int pixel = 1; pixel < 256; ++pixel) { Chris@0: Chris@0: QColor colour; Chris@0: int hue, px; Chris@0: Chris@0: switch (m_colourScheme) { Chris@0: Chris@0: default: Chris@0: case DefaultColours: Chris@0: hue = 256 - pixel; Chris@0: colour = QColor::fromHsv(hue, pixel/2 + 128, pixel); Chris@77: m_crosshairColour = QColor(255, 150, 50); Chris@77: // m_crosshairColour = QColor::fromHsv(240, 160, 255); Chris@0: break; Chris@0: Chris@0: case WhiteOnBlack: Chris@0: colour = QColor(pixel, pixel, pixel); Chris@77: m_crosshairColour = Qt::red; Chris@0: break; Chris@0: Chris@0: case BlackOnWhite: Chris@0: colour = QColor(256-pixel, 256-pixel, 256-pixel); Chris@77: m_crosshairColour = Qt::darkGreen; Chris@0: break; Chris@0: Chris@0: case RedOnBlue: Chris@0: colour = QColor(pixel > 128 ? (pixel - 128) * 2 : 0, 0, Chris@0: pixel < 128 ? pixel : (256 - pixel)); Chris@77: m_crosshairColour = Qt::green; Chris@0: break; Chris@0: Chris@0: case YellowOnBlack: Chris@0: px = 256 - pixel; Chris@0: colour = QColor(px < 64 ? 255 - px/2 : Chris@0: px < 128 ? 224 - (px - 64) : Chris@0: px < 192 ? 160 - (px - 128) * 3 / 2 : Chris@0: 256 - px, Chris@0: pixel, Chris@0: pixel / 4); Chris@77: m_crosshairColour = QColor::fromHsv(240, 255, 255); Chris@0: break; Chris@0: Chris@71: case BlueOnBlack: Chris@71: colour = QColor::fromHsv Chris@71: (240, pixel > 226 ? 256 - (pixel - 226) * 8 : 255, Chris@71: (pixel * pixel) / 255); Chris@77: m_crosshairColour = Qt::red; Chris@71: break; Chris@71: Chris@40: case Rainbow: Chris@40: hue = 250 - pixel; Chris@40: if (hue < 0) hue += 256; Chris@40: colour = QColor::fromHsv(pixel, 255, 255); Chris@77: m_crosshairColour = Qt::white; Chris@0: break; Chris@0: } Chris@0: Chris@86: m_colourMap.setColour(pixel, colour); Chris@0: } Chris@9: Chris@9: m_colourRotation = 0; Chris@90: rotateColourmap(m_colourRotation - formerRotation); Chris@10: m_colourRotation = formerRotation; Chris@9: } Chris@9: Chris@9: void Chris@90: SpectrogramLayer::rotateColourmap(int distance) Chris@9: { Chris@31: QColor newPixels[256]; Chris@9: Chris@86: newPixels[NO_VALUE] = m_colourMap.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@86: newPixels[target] = m_colourMap.getColour(pixel); Chris@9: } Chris@9: Chris@9: for (int pixel = 0; pixel < 256; ++pixel) { Chris@86: m_colourMap.setColour(pixel, newPixels[pixel]); Chris@9: } Chris@0: } Chris@0: Chris@38: float Chris@38: SpectrogramLayer::calculateFrequency(size_t bin, Chris@38: size_t windowSize, Chris@38: size_t windowIncrement, Chris@38: size_t sampleRate, Chris@38: float oldPhase, Chris@38: float newPhase, Chris@38: bool &steadyState) Chris@38: { Chris@38: // At frequency f, phase shift of 2pi (one cycle) happens in 1/f sec. Chris@38: // At hopsize h and sample rate sr, one hop happens in h/sr sec. Chris@38: // At window size w, for bin b, f is b*sr/w. Chris@38: // thus 2pi phase shift happens in w/(b*sr) sec. Chris@38: // We need to know what phase shift we expect from h/sr sec. Chris@38: // -> 2pi * ((h/sr) / (w/(b*sr))) Chris@38: // = 2pi * ((h * b * sr) / (w * sr)) Chris@38: // = 2pi * (h * b) / w. Chris@38: Chris@38: float frequency = (float(bin) * sampleRate) / windowSize; Chris@38: Chris@38: float expectedPhase = Chris@38: oldPhase + (2.0 * M_PI * bin * windowIncrement) / windowSize; Chris@38: Chris@104: float phaseError = princargf(newPhase - expectedPhase); Chris@38: Chris@38: if (fabs(phaseError) < (1.1 * (windowIncrement * M_PI) / windowSize)) { Chris@38: Chris@38: // The new frequency estimate based on the phase error Chris@38: // resulting from assuming the "native" frequency of this bin Chris@38: Chris@38: float newFrequency = Chris@38: (sampleRate * (expectedPhase + phaseError - oldPhase)) / Chris@38: (2 * M_PI * windowIncrement); Chris@38: Chris@38: steadyState = true; Chris@38: return newFrequency; Chris@38: } Chris@38: Chris@38: steadyState = false; Chris@38: return frequency; Chris@38: } Chris@38: Chris@38: unsigned char Chris@119: SpectrogramLayer::getDisplayValue(View *v, float input) const Chris@38: { Chris@38: int value; Chris@37: Chris@120: float min = 0.f; Chris@120: float max = 1.f; Chris@120: Chris@120: if (m_normalizeVisibleArea) { Chris@120: min = m_viewMags[v].getMin(); Chris@120: max = m_viewMags[v].getMax(); Chris@120: } else if (!m_normalizeColumns) { Chris@120: if (m_colourScale == LinearColourScale || Chris@120: m_colourScale == MeterColourScale) { Chris@120: max = 0.1f; Chris@120: } Chris@120: } Chris@120: Chris@119: float thresh = -80.f; Chris@119: Chris@119: if (max == 0.f) max = 1.f; Chris@119: if (max == min) min = max - 0.0001f; Chris@119: Chris@40: switch (m_colourScale) { Chris@40: Chris@40: default: Chris@40: case LinearColourScale: Chris@119: // value = int Chris@119: // (input * (m_normalizeColumns ? 1.0 : 50.0) * 255.0) + 1; Chris@119: value = int(((input - min) / (max - min)) * 255.f) + 1; Chris@40: break; Chris@40: Chris@40: case MeterColourScale: Chris@119: // value = AudioLevel::multiplier_to_preview Chris@119: // (input * (m_normalizeColumns ? 1.0 : 50.0), 255) + 1; Chris@119: value = AudioLevel::multiplier_to_preview((input - min) / (max - min), 255) + 1; Chris@40: break; Chris@40: Chris@40: case dBColourScale: Chris@119: //!!! experiment with normalizing the visible area this way. Chris@119: //In any case, we need to have some indication of what the dB Chris@119: //scale is relative to. Chris@119: input = 10.f * log10f(input / max); Chris@119: if (min > 0.f) { Chris@119: thresh = 10.f * log10f(min); Chris@119: if (thresh < -80.f) thresh = -80.f; Chris@119: } Chris@119: input = (input - thresh) / (-thresh); Chris@119: if (input < 0.f) input = 0.f; Chris@119: if (input > 1.f) input = 1.f; Chris@119: value = int(input * 255.f) + 1; Chris@119: break; Chris@119: Chris@119: case OtherColourScale: Chris@119: //!!! the "Other" scale is just where our current experiments go Chris@119: //!!! power rather than v Chris@119: input = 10.f * log10f((input * input) / (max * max)); Chris@119: if (min > 0.f) { Chris@119: thresh = 10.f * log10f(min * min); Chris@119: if (thresh < -80.f) thresh = -80.f; Chris@119: } Chris@119: input = (input - thresh) / (-thresh); Chris@119: if (input < 0.f) input = 0.f; Chris@119: if (input > 1.f) input = 1.f; Chris@119: value = int(input * 255.f) + 1; Chris@119: break; Chris@119: Chris@119: /*!!! Chris@119: input = 10.f * log10f(input * input); Chris@119: input = 1.f / (1.f + expf(- (input + 20.f) / 10.f)); Chris@119: Chris@119: if (input < 0.f) input = 0.f; Chris@119: if (input > 1.f) input = 1.f; Chris@119: value = int(input * 255.f) + 1; Chris@119: */ Chris@40: break; Chris@40: Chris@40: case PhaseColourScale: Chris@40: value = int((input * 127.0 / M_PI) + 128); Chris@40: break; Chris@0: } Chris@38: Chris@38: if (value > UCHAR_MAX) value = UCHAR_MAX; Chris@38: if (value < 0) value = 0; Chris@38: return value; Chris@0: } Chris@0: Chris@40: float Chris@40: SpectrogramLayer::getInputForDisplayValue(unsigned char uc) const Chris@40: { Chris@40: int value = uc; Chris@40: float input; Chris@40: Chris@120: //!!! incorrect for normalizing visible area (and also out of date) Chris@120: Chris@40: switch (m_colourScale) { Chris@40: Chris@40: default: Chris@40: case LinearColourScale: Chris@40: input = float(value - 1) / 255.0 / (m_normalizeColumns ? 1 : 50); Chris@40: break; Chris@40: Chris@40: case MeterColourScale: Chris@40: input = AudioLevel::preview_to_multiplier(value - 1, 255) Chris@40: / (m_normalizeColumns ? 1.0 : 50.0); Chris@40: break; Chris@40: Chris@40: case dBColourScale: Chris@40: input = float(value - 1) / 255.0; Chris@40: input = (input * 80.0) - 80.0; Chris@40: input = powf(10.0, input) / 20.0; Chris@40: value = int(input); Chris@40: break; Chris@40: Chris@119: case OtherColourScale: Chris@119: input = float(value - 1) / 255.0; Chris@119: input = (input * 80.0) - 80.0; Chris@119: input = powf(10.0, input) / 20.0; Chris@119: value = int(input); Chris@119: break; Chris@119: Chris@40: case PhaseColourScale: Chris@40: input = float(value - 128) * M_PI / 127.0; Chris@40: break; Chris@40: } Chris@40: Chris@40: return input; Chris@40: } Chris@40: Chris@40: float Chris@40: SpectrogramLayer::getEffectiveMinFrequency() const Chris@40: { Chris@40: int sr = m_model->getSampleRate(); Chris@107: float minf = float(sr) / m_fftSize; Chris@40: Chris@40: if (m_minFrequency > 0.0) { Chris@107: size_t minbin = size_t((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@40: float Chris@40: SpectrogramLayer::getEffectiveMaxFrequency() const Chris@40: { Chris@40: int sr = m_model->getSampleRate(); Chris@40: float maxf = float(sr) / 2; Chris@40: Chris@40: if (m_maxFrequency > 0.0) { Chris@107: size_t maxbin = size_t((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@44: SpectrogramLayer::getYBinRange(View *v, int y, float &q0, float &q1) const Chris@0: { Chris@44: int h = v->height(); Chris@0: if (y < 0 || y >= h) return false; Chris@0: Chris@38: int sr = m_model->getSampleRate(); Chris@40: float minf = getEffectiveMinFrequency(); Chris@40: float maxf = getEffectiveMaxFrequency(); Chris@0: Chris@38: bool logarithmic = (m_frequencyScale == LogFrequencyScale); Chris@38: Chris@114: //!!! wrong for smoothing -- wrong fft size for fft adapter Chris@114: Chris@44: q0 = v->getFrequencyForY(y, minf, maxf, logarithmic); Chris@44: q1 = v->getFrequencyForY(y - 1, minf, maxf, logarithmic); Chris@38: Chris@38: // Now map these on to actual bins Chris@38: Chris@107: int b0 = int((q0 * m_fftSize) / sr); Chris@107: int b1 = int((q1 * m_fftSize) / sr); Chris@0: Chris@40: //!!! this is supposed to return fractions-of-bins, as it were, hence the floats Chris@38: q0 = b0; Chris@38: q1 = b1; Chris@38: Chris@107: // q0 = (b0 * sr) / m_fftSize; Chris@107: // q1 = (b1 * sr) / m_fftSize; Chris@0: Chris@0: return true; Chris@0: } Chris@38: Chris@0: bool Chris@44: SpectrogramLayer::getXBinRange(View *v, int x, float &s0, float &s1) const Chris@0: { Chris@21: size_t modelStart = m_model->getStartFrame(); Chris@21: size_t modelEnd = m_model->getEndFrame(); Chris@0: Chris@0: // Each pixel column covers an exact range of sample frames: Chris@44: int f0 = v->getFrameForX(x) - modelStart; Chris@44: int 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@0: size_t windowIncrement = getWindowIncrement(); Chris@0: s0 = float(f0) / windowIncrement; Chris@0: s1 = float(f1) / windowIncrement; Chris@0: Chris@0: return true; Chris@0: } Chris@0: Chris@0: bool Chris@44: SpectrogramLayer::getXBinSourceRange(View *v, int x, RealTime &min, RealTime &max) const Chris@0: { Chris@0: float 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@44: SpectrogramLayer::getYBinSourceRange(View *v, int y, float &freqMin, float &freqMax) Chris@0: const Chris@0: { Chris@0: float 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@0: int 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@44: SpectrogramLayer::getAdjustedYBinSourceRange(View *v, int x, int y, Chris@35: float &freqMin, float &freqMax, Chris@35: float &adjFreqMin, float &adjFreqMax) Chris@35: const Chris@35: { Chris@114: FFTFuzzyAdapter *fft = getFFTAdapter(v); Chris@114: if (!fft) return false; Chris@110: Chris@35: float s0 = 0, s1 = 0; Chris@44: if (!getXBinRange(v, x, s0, s1)) return false; Chris@35: Chris@35: float 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@35: int sr = m_model->getSampleRate(); Chris@35: Chris@38: size_t windowSize = m_windowSize; Chris@38: size_t windowIncrement = getWindowIncrement(); Chris@38: 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@117: if (!fft->isColumnReady(s)) continue; Chris@117: Chris@35: float binfreq = (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@114: if (!fft->isOverThreshold(s, q, m_threshold)) continue; Chris@38: Chris@38: float freq = binfreq; Chris@38: bool steady = false; Chris@40: Chris@114: if (s < int(fft->getWidth()) - 1) { Chris@38: Chris@38: freq = calculateFrequency(q, Chris@38: windowSize, Chris@38: windowIncrement, Chris@38: sr, Chris@114: fft->getPhaseAt(s, q), Chris@114: fft->getPhaseAt(s+1, q), Chris@38: steady); 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@44: SpectrogramLayer::getXYBinSourceRange(View *v, int x, int y, Chris@38: float &min, float &max, Chris@38: float &phaseMin, float &phaseMax) const Chris@0: { Chris@0: float q0 = 0, q1 = 0; Chris@44: if (!getYBinRange(v, y, q0, q1)) return false; Chris@0: Chris@0: float 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@122: size_t zp = getZeroPadLevel(v); Chris@122: q0i *= zp + 1; Chris@122: q1i *= zp + 1; Chris@122: Chris@114: FFTFuzzyAdapter *fft = getFFTAdapter(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@117: if (!fft->isColumnReady(s)) continue; Chris@110: Chris@110: float 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@114: value = fft->getMagnitudeAt(s, q); 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@114: size_t Chris@114: SpectrogramLayer::getZeroPadLevel(const View *v) const Chris@114: { Chris@114: //!!! tidy all this stuff Chris@114: Chris@114: if (m_binDisplay != AllBins) return 0; Chris@118: if (!Preferences::getInstance()->getSmoothSpectrogram()) return 0; Chris@114: if (m_frequencyScale == LogFrequencyScale) return 3; Chris@114: Chris@114: int sr = m_model->getSampleRate(); Chris@114: Chris@114: size_t bins = m_fftSize / 2; Chris@114: if (m_maxFrequency > 0) { Chris@114: bins = int((double(m_maxFrequency) * m_fftSize) / sr + 0.1); Chris@114: if (bins > m_fftSize / 2) bins = m_fftSize / 2; Chris@114: } Chris@114: Chris@114: size_t 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@114: if (minbin >= bins) minbin = bins - 1; Chris@114: } Chris@114: Chris@118: float perPixel = Chris@118: float(v->height()) / Chris@118: float((bins - 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@114: size_t Chris@114: SpectrogramLayer::getFFTSize(const View *v) const Chris@114: { Chris@114: return m_fftSize * (getZeroPadLevel(v) + 1); Chris@114: } Chris@114: Chris@114: FFTFuzzyAdapter * Chris@114: SpectrogramLayer::getFFTAdapter(const View *v) const Chris@114: { Chris@114: if (!m_model) return 0; Chris@114: Chris@114: size_t fftSize = getFFTSize(v); Chris@114: Chris@114: if (m_fftAdapters.find(v) != m_fftAdapters.end()) { Chris@115: if (m_fftAdapters[v].first->getHeight() != fftSize / 2) { Chris@115: delete m_fftAdapters[v].first; Chris@114: m_fftAdapters.erase(v); Chris@114: } Chris@114: } Chris@114: Chris@114: if (m_fftAdapters.find(v) == m_fftAdapters.end()) { Chris@115: m_fftAdapters[v] = FFTFillPair Chris@115: (new FFTFuzzyAdapter(m_model, Chris@115: m_channel, Chris@115: m_windowType, Chris@115: m_windowSize, Chris@115: getWindowIncrement(), Chris@121: fftSize, Chris@115: true, Chris@115: m_candidateFillStartFrame), Chris@115: 0); Chris@114: Chris@114: delete m_updateTimer; Chris@114: m_updateTimer = new QTimer((SpectrogramLayer *)this); Chris@114: connect(m_updateTimer, SIGNAL(timeout()), Chris@114: this, SLOT(fillTimerTimedOut())); Chris@114: m_updateTimer->start(200); Chris@114: } Chris@114: Chris@115: return m_fftAdapters[v].first; Chris@114: } Chris@114: Chris@114: void Chris@114: SpectrogramLayer::invalidateFFTAdapters() Chris@114: { Chris@114: for (ViewFFTMap::iterator i = m_fftAdapters.begin(); Chris@114: i != m_fftAdapters.end(); ++i) { Chris@115: delete i->second.first; Chris@114: } Chris@114: Chris@114: m_fftAdapters.clear(); Chris@114: } Chris@114: Chris@0: void Chris@119: SpectrogramLayer::invalidateMagnitudes() Chris@119: { Chris@119: m_viewMags.clear(); Chris@119: for (std::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@119: SpectrogramLayer::updateViewMagnitudes(View *v) const Chris@119: { Chris@119: MagnitudeRange mag; Chris@119: Chris@119: int x0 = 0, x1 = v->width(); Chris@119: float s00 = 0, s01 = 0, s10 = 0, s11 = 0; Chris@119: Chris@119: getXBinRange(v, x0, s00, s01); Chris@119: getXBinRange(v, x1, s10, s11); Chris@119: Chris@119: int s0 = int(std::min(s00, s10) + 0.0001); Chris@119: int s1 = int(std::max(s01, s11)); Chris@119: Chris@119: if (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@119: std::cerr << "SpectrogramLayer::updateViewMagnitudes returning from cols " Chris@119: << s0 << " -> " << s1 << " inclusive" << std::endl; Chris@119: Chris@119: if (!mag.isSet()) return false; Chris@119: if (mag == m_viewMags[v]) return false; Chris@119: m_viewMags[v] = mag; Chris@119: return true; Chris@119: } Chris@119: Chris@119: void Chris@44: SpectrogramLayer::paint(View *v, QPainter &paint, QRect rect) const Chris@0: { Chris@55: if (m_colourScheme == BlackOnWhite) { Chris@55: v->setLightBackground(true); Chris@55: } else { Chris@55: v->setLightBackground(false); Chris@55: } Chris@55: Chris@124: // Profiler profiler("SpectrogramLayer::paint", true); Chris@0: #ifdef DEBUG_SPECTROGRAM_REPAINT Chris@95: std::cerr << "SpectrogramLayer::paint(): m_model is " << m_model << ", zoom level is " << v->getZoomLevel() << ", m_updateTimer " << m_updateTimer << std::endl; Chris@95: Chris@95: std::cerr << "rect is " << rect.x() << "," << rect.y() << " " << rect.width() << "x" << rect.height() << std::endl; Chris@0: #endif Chris@95: Chris@45: long sf = v->getStartFrame(); Chris@45: if (sf < 0) m_candidateFillStartFrame = 0; Chris@45: else m_candidateFillStartFrame = sf; 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@48: std::cerr << "SpectrogramLayer::paint(): Layer is dormant, making it undormant again" << std::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@48: m_dormancy[v] = false; Chris@48: Chris@114: size_t fftSize = getFFTSize(v); Chris@114: FFTFuzzyAdapter *fft = getFFTAdapter(v); Chris@114: if (!fft) { Chris@114: std::cerr << "ERROR: SpectrogramLayer::paint(): No FFT adapter, returning" << std::endl; Chris@0: return; Chris@0: } Chris@0: Chris@95: PixmapCache &cache = m_pixmapCaches[v]; Chris@95: Chris@95: #ifdef DEBUG_SPECTROGRAM_REPAINT Chris@95: std::cerr << "SpectrogramLayer::paint(): pixmap cache valid area " << cache.validArea.x() << ", " << cache.validArea.y() << ", " << cache.validArea.width() << "x" << cache.validArea.height() << std::endl; Chris@95: #endif Chris@95: Chris@0: bool stillCacheing = (m_updateTimer != 0); Chris@0: Chris@0: #ifdef DEBUG_SPECTROGRAM_REPAINT Chris@0: std::cerr << "SpectrogramLayer::paint(): Still cacheing = " << stillCacheing << std::endl; Chris@0: #endif Chris@0: Chris@44: long startFrame = v->getStartFrame(); Chris@44: int zoomLevel = v->getZoomLevel(); Chris@0: Chris@0: int x0 = 0; Chris@44: int x1 = v->width(); Chris@0: int y0 = 0; Chris@44: int y1 = v->height(); Chris@0: Chris@0: bool recreateWholePixmapCache = true; Chris@0: Chris@95: x0 = rect.left(); Chris@95: x1 = rect.right() + 1; Chris@95: y0 = rect.top(); Chris@95: y1 = rect.bottom() + 1; Chris@95: Chris@95: if (cache.validArea.width() > 0) { Chris@95: Chris@95: if (int(cache.zoomLevel) == zoomLevel && Chris@95: cache.pixmap.width() == v->width() && Chris@95: cache.pixmap.height() == v->height()) { Chris@95: Chris@95: if (v->getXForFrame(cache.startFrame) == Chris@95: v->getXForFrame(startFrame) && Chris@95: cache.validArea.x() <= x0 && Chris@95: cache.validArea.x() + cache.validArea.width() >= x1) { Chris@0: Chris@0: #ifdef DEBUG_SPECTROGRAM_REPAINT Chris@0: std::cerr << "SpectrogramLayer: pixmap cache good" << std::endl; Chris@0: #endif Chris@0: Chris@95: paint.drawPixmap(rect, cache.pixmap, rect); Chris@121: illuminateLocalFeatures(v, paint); Chris@0: return; Chris@0: Chris@0: } else { Chris@0: Chris@0: #ifdef DEBUG_SPECTROGRAM_REPAINT Chris@0: std::cerr << "SpectrogramLayer: pixmap cache partially OK" << std::endl; Chris@0: #endif Chris@0: Chris@0: recreateWholePixmapCache = false; Chris@0: Chris@95: int dx = v->getXForFrame(cache.startFrame) - Chris@44: v->getXForFrame(startFrame); Chris@0: Chris@0: #ifdef DEBUG_SPECTROGRAM_REPAINT Chris@95: std::cerr << "SpectrogramLayer: dx = " << dx << " (pixmap cache " << cache.pixmap.width() << "x" << cache.pixmap.height() << ")" << std::endl; Chris@0: #endif Chris@0: Chris@95: if (dx != 0 && Chris@95: dx > -cache.pixmap.width() && Chris@95: dx < cache.pixmap.width()) { Chris@0: Chris@0: #if defined(Q_WS_WIN32) || defined(Q_WS_MAC) Chris@0: // Copying a pixmap to itself doesn't work Chris@0: // properly on Windows or Mac (it only works when Chris@0: // moving in one direction). Chris@0: Chris@0: //!!! Need a utility function for this Chris@0: Chris@0: static QPixmap *tmpPixmap = 0; Chris@0: if (!tmpPixmap || Chris@95: tmpPixmap->width() != cache.pixmap.width() || Chris@95: tmpPixmap->height() != cache.pixmap.height()) { Chris@0: delete tmpPixmap; Chris@95: tmpPixmap = new QPixmap(cache.pixmap.width(), Chris@95: cache.pixmap.height()); Chris@0: } Chris@0: QPainter cachePainter; Chris@0: cachePainter.begin(tmpPixmap); Chris@95: cachePainter.drawPixmap(0, 0, cache.pixmap); Chris@0: cachePainter.end(); Chris@95: cachePainter.begin(&cache.pixmap); Chris@0: cachePainter.drawPixmap(dx, 0, *tmpPixmap); Chris@0: cachePainter.end(); Chris@0: #else Chris@95: QPainter cachePainter(&cache.pixmap); Chris@95: cachePainter.drawPixmap(dx, 0, cache.pixmap); Chris@0: cachePainter.end(); Chris@0: #endif Chris@0: Chris@95: int px = cache.validArea.x(); Chris@95: int pw = cache.validArea.width(); Chris@0: Chris@0: if (dx < 0) { Chris@95: x0 = cache.pixmap.width() + dx; Chris@95: x1 = cache.pixmap.width(); Chris@95: px += dx; Chris@95: if (px < 0) { Chris@95: pw += px; Chris@95: px = 0; Chris@95: if (pw < 0) pw = 0; Chris@95: } Chris@0: } else { Chris@0: x0 = 0; Chris@0: x1 = dx; Chris@95: px += dx; Chris@95: if (px + pw > cache.pixmap.width()) { Chris@95: pw = int(cache.pixmap.width()) - px; Chris@95: if (pw < 0) pw = 0; Chris@95: } Chris@0: } Chris@95: Chris@95: cache.validArea = Chris@95: QRect(px, cache.validArea.y(), Chris@95: pw, cache.validArea.height()); Chris@95: Chris@95: paint.drawPixmap(rect & cache.validArea, Chris@95: cache.pixmap, Chris@95: rect & cache.validArea); Chris@0: } Chris@0: } Chris@0: } else { Chris@0: #ifdef DEBUG_SPECTROGRAM_REPAINT Chris@0: std::cerr << "SpectrogramLayer: pixmap cache useless" << std::endl; Chris@0: #endif Chris@95: cache.validArea = QRect(); Chris@0: } Chris@0: } Chris@95: Chris@92: /* Chris@0: if (stillCacheing) { Chris@0: x0 = rect.left(); Chris@0: x1 = rect.right() + 1; Chris@0: y0 = rect.top(); Chris@0: y1 = rect.bottom() + 1; Chris@0: } Chris@92: */ Chris@95: Chris@95: if (recreateWholePixmapCache) { Chris@95: x0 = 0; Chris@95: x1 = v->width(); Chris@95: } Chris@95: Chris@119: if (updateViewMagnitudes(v)) { Chris@119: std::cerr << "SpectrogramLayer: magnitude range changed to [" << m_viewMags[v].getMin() << "->" << m_viewMags[v].getMax() << "]" << std::endl; Chris@119: } else { Chris@119: std::cerr << "No change in magnitude range [" << m_viewMags[v].getMin() << "->" << m_viewMags[v].getMax() << "]" << std::endl; Chris@119: } Chris@119: Chris@96: int paintBlockWidth = (300000 / zoomLevel); Chris@96: if (paintBlockWidth < 20) paintBlockWidth = 20; Chris@96: Chris@96: if (cache.validArea.width() > 0) { Chris@96: Chris@96: int vx0 = 0, vx1 = 0; Chris@96: vx0 = cache.validArea.x(); Chris@96: vx1 = cache.validArea.x() + cache.validArea.width(); Chris@96: Chris@96: #ifdef DEBUG_SPECTROGRAM_REPAINT Chris@96: std::cerr << "x0 " << x0 << ", x1 " << x1 << ", vx0 " << vx0 << ", vx1 " << vx1 << ", paintBlockWidth " << paintBlockWidth << std::endl; Chris@96: #endif Chris@96: if (x0 < vx0) { Chris@96: if (x0 + paintBlockWidth < vx0) { Chris@96: x0 = vx0 - paintBlockWidth; Chris@96: } else { Chris@96: x0 = 0; Chris@96: } Chris@96: } else if (x0 > vx1) { Chris@96: x0 = vx1; Chris@96: } Chris@95: Chris@96: if (x1 < vx0) { Chris@96: x1 = vx0; Chris@96: } else if (x1 > vx1) { Chris@96: if (vx1 + paintBlockWidth < x1) { Chris@96: x1 = vx1 + paintBlockWidth; Chris@96: } else { Chris@96: x1 = v->width(); Chris@95: } Chris@96: } Chris@95: Chris@96: cache.validArea = QRect Chris@96: (std::min(vx0, x0), cache.validArea.y(), Chris@96: std::max(vx1 - std::min(vx0, x0), Chris@96: x1 - std::min(vx0, x0)), Chris@96: cache.validArea.height()); Chris@95: Chris@96: } else { Chris@96: if (x1 > x0 + paintBlockWidth) { Chris@96: x1 = x0 + paintBlockWidth; Chris@95: } Chris@96: cache.validArea = QRect(x0, 0, x1 - x0, v->height()); Chris@95: } Chris@95: Chris@0: int w = x1 - x0; Chris@0: int h = y1 - y0; Chris@0: Chris@95: #ifdef DEBUG_SPECTROGRAM_REPAINT Chris@95: std::cerr << "x0 " << x0 << ", x1 " << x1 << ", w " << w << ", h " << h << std::endl; Chris@95: #endif Chris@95: Chris@95: if (m_drawBuffer.width() < w || m_drawBuffer.height() < h) { Chris@95: m_drawBuffer = QImage(w, h, QImage::Format_RGB32); Chris@95: } Chris@95: Chris@97: m_drawBuffer.fill(m_colourMap.getColour(0).rgb()); Chris@35: Chris@37: int 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@35: Chris@114: size_t bins = fftSize / 2; Chris@35: if (m_maxFrequency > 0) { Chris@114: bins = int((double(m_maxFrequency) * fftSize) / sr + 0.1); Chris@114: if (bins > fftSize / 2) bins = fftSize / 2; Chris@35: } Chris@111: Chris@40: size_t minbin = 1; Chris@37: if (m_minFrequency > 0) { Chris@114: minbin = int((double(m_minFrequency) * fftSize) / sr + 0.1); Chris@40: if (minbin < 1) minbin = 1; Chris@37: if (minbin >= bins) minbin = bins - 1; Chris@37: } Chris@37: Chris@114: float minFreq = (float(minbin) * sr) / fftSize; Chris@114: float maxFreq = (float(bins) * sr) / fftSize; Chris@0: Chris@122: float displayMinFreq = minFreq; Chris@122: float displayMaxFreq = maxFreq; Chris@122: Chris@122: if (fftSize != m_fftSize) { Chris@122: displayMinFreq = getEffectiveMinFrequency(); Chris@122: displayMaxFreq = getEffectiveMaxFrequency(); Chris@122: } Chris@122: Chris@92: float ymag[h]; Chris@92: float ydiv[h]; Chris@122: float yval[bins + 1]; //!!! cache this? Chris@92: Chris@38: size_t increment = getWindowIncrement(); Chris@40: Chris@40: bool logarithmic = (m_frequencyScale == LogFrequencyScale); Chris@38: Chris@92: for (size_t q = minbin; q <= bins; ++q) { Chris@114: float f0 = (float(q) * sr) / fftSize; Chris@122: yval[q] = v->getYForFrequency(f0, displayMinFreq, displayMaxFreq, Chris@122: logarithmic); Chris@122: // std::cerr << "min: " << minFreq << ", max: " << maxFreq << ", yval[" << q << "]: " << yval[q] << std::endl; Chris@92: } Chris@92: Chris@119: MagnitudeRange overallMag = m_viewMags[v]; Chris@119: bool overallMagChanged = false; Chris@119: Chris@35: for (int x = 0; x < w; ++x) { Chris@35: Chris@35: for (int y = 0; y < h; ++y) { Chris@40: ymag[y] = 0.0; Chris@40: ydiv[y] = 0.0; Chris@35: } Chris@35: Chris@35: float s0 = 0, s1 = 0; Chris@35: Chris@44: if (!getXBinRange(v, x0 + x, s0, s1)) { Chris@95: assert(x <= m_drawBuffer.width()); Chris@35: continue; Chris@35: } Chris@35: Chris@35: int s0i = int(s0 + 0.001); Chris@35: int s1i = int(s1); Chris@35: Chris@114: if (s1i >= fft->getWidth()) { Chris@114: if (s0i >= fft->getWidth()) { Chris@45: continue; Chris@45: } else { Chris@45: s1i = s0i; Chris@45: } Chris@45: } Chris@92: Chris@92: for (int s = s0i; s <= s1i; ++s) { Chris@92: Chris@114: if (!fft->isColumnReady(s)) continue; Chris@119: MagnitudeRange mag; Chris@92: Chris@92: for (size_t q = minbin; q < bins; ++q) { Chris@92: Chris@92: float y0 = yval[q + 1]; Chris@92: float y1 = yval[q]; Chris@92: Chris@40: if (m_binDisplay == PeakBins || Chris@40: m_binDisplay == PeakFrequencies) { Chris@114: if (!fft->isLocalPeak(s, q)) continue; Chris@40: } Chris@114: Chris@114: if (m_threshold != 0.f && Chris@114: !fft->isOverThreshold(s, q, m_threshold)) { Chris@114: continue; Chris@114: } Chris@40: Chris@35: float sprop = 1.0; Chris@35: if (s == s0i) sprop *= (s + 1) - s0; Chris@35: if (s == s1i) sprop *= s1 - s; Chris@35: Chris@38: if (m_binDisplay == PeakFrequencies && Chris@114: s < int(fft->getWidth()) - 1) { Chris@35: Chris@38: bool steady = false; Chris@92: float f = calculateFrequency(q, Chris@38: m_windowSize, Chris@38: increment, Chris@38: sr, Chris@114: fft->getPhaseAt(s, q), Chris@114: fft->getPhaseAt(s+1, q), Chris@38: steady); Chris@40: Chris@44: y0 = y1 = v->getYForFrequency Chris@122: (f, displayMinFreq, displayMaxFreq, logarithmic); Chris@35: } Chris@38: Chris@35: int y0i = int(y0 + 0.001); Chris@35: int y1i = int(y1); Chris@35: Chris@92: float value; Chris@92: Chris@92: if (m_colourScale == PhaseColourScale) { Chris@114: value = fft->getPhaseAt(s, q); Chris@92: } else if (m_normalizeColumns) { Chris@119: value = fft->getNormalizedMagnitudeAt(s, q); Chris@119: mag.sample(value); Chris@119: value *= m_gain; Chris@92: } else { Chris@119: value = fft->getMagnitudeAt(s, q); Chris@119: mag.sample(value); Chris@119: value *= m_gain; Chris@92: } Chris@92: Chris@35: for (int y = y0i; y <= y1i; ++y) { Chris@35: Chris@35: if (y < 0 || y >= h) continue; Chris@35: Chris@35: float yprop = sprop; Chris@35: if (y == y0i) yprop *= (y + 1) - y0; Chris@35: if (y == y1i) yprop *= y1 - y; Chris@37: ymag[y] += yprop * value; Chris@35: ydiv[y] += yprop; Chris@35: } Chris@35: } Chris@119: Chris@119: if (mag.isSet()) { Chris@119: Chris@119: m_columnMags[s].sample(mag); Chris@119: Chris@119: if (overallMag.sample(mag)) { Chris@119: //!!! scaling would change here Chris@119: overallMagChanged = true; Chris@119: std::cerr << "Overall mag changed (again?) at column " << s << ", to [" << overallMag.getMin() << "->" << overallMag.getMax() << "]" << std::endl; Chris@119: } Chris@119: } Chris@35: } Chris@35: Chris@35: for (int y = 0; y < h; ++y) { Chris@35: Chris@35: if (ydiv[y] > 0.0) { Chris@40: Chris@40: unsigned char pixel = 0; Chris@40: Chris@38: float avg = ymag[y] / ydiv[y]; Chris@119: pixel = getDisplayValue(v, avg); Chris@40: Chris@95: assert(x <= m_drawBuffer.width()); Chris@86: QColor c = m_colourMap.getColour(pixel); Chris@95: m_drawBuffer.setPixel(x, y, Chris@95: qRgb(c.red(), c.green(), c.blue())); Chris@35: } Chris@35: } Chris@35: } Chris@35: Chris@119: if (overallMagChanged) { Chris@119: m_viewMags[v] = overallMag; Chris@119: std::cerr << "Overall mag is now [" << m_viewMags[v].getMin() << "->" << m_viewMags[v].getMax() << "] - will be updating" << std::endl; Chris@119: } else { Chris@119: std::cerr << "Overall mag unchanged at [" << m_viewMags[v].getMin() << "->" << m_viewMags[v].getMax() << "]" << std::endl; Chris@119: } Chris@119: Chris@95: paint.drawImage(x0, y0, m_drawBuffer, 0, 0, w, h); Chris@0: Chris@0: if (recreateWholePixmapCache) { Chris@95: cache.pixmap = QPixmap(v->width(), v->height()); Chris@0: } Chris@0: Chris@95: QPainter cachePainter(&cache.pixmap); Chris@95: cachePainter.drawImage(x0, y0, m_drawBuffer, 0, 0, w, h); Chris@0: cachePainter.end(); Chris@119: Chris@120: if (!m_normalizeVisibleArea || !overallMagChanged) { Chris@0: Chris@119: cache.startFrame = startFrame; Chris@119: cache.zoomLevel = zoomLevel; Chris@119: Chris@119: if (cache.validArea.x() > 0) { Chris@95: #ifdef DEBUG_SPECTROGRAM_REPAINT Chris@119: std::cerr << "SpectrogramLayer::paint() updating left" << std::endl; Chris@95: #endif Chris@119: v->update(0, 0, cache.validArea.x(), v->height()); Chris@119: } Chris@119: Chris@119: if (cache.validArea.x() + cache.validArea.width() < Chris@119: cache.pixmap.width()) { Chris@95: #ifdef DEBUG_SPECTROGRAM_REPAINT Chris@119: std::cerr << "SpectrogramLayer::paint() updating right (" Chris@119: << cache.validArea.x() + cache.validArea.width() Chris@119: << ", " Chris@119: << cache.pixmap.width() - (cache.validArea.x() + Chris@119: cache.validArea.width()) Chris@119: << ")" << std::endl; Chris@95: #endif Chris@119: v->update(cache.validArea.x() + cache.validArea.width(), Chris@119: 0, Chris@119: cache.pixmap.width() - (cache.validArea.x() + Chris@119: cache.validArea.width()), Chris@119: v->height()); Chris@119: } Chris@119: } else { Chris@119: // overallMagChanged Chris@119: cache.validArea = QRect(); Chris@119: v->update(); Chris@95: } Chris@0: Chris@121: illuminateLocalFeatures(v, paint); Chris@120: Chris@0: #ifdef DEBUG_SPECTROGRAM_REPAINT Chris@0: std::cerr << "SpectrogramLayer::paint() returning" << std::endl; Chris@0: #endif Chris@0: } Chris@0: Chris@121: void Chris@121: SpectrogramLayer::illuminateLocalFeatures(View *v, QPainter &paint) const Chris@121: { Chris@121: QPoint localPos; Chris@121: if (!v->shouldIlluminateLocalFeatures(this, localPos) || !m_model) { Chris@121: return; Chris@121: } Chris@121: Chris@121: std::cerr << "SpectrogramLayer: illuminateLocalFeatures(" Chris@121: << localPos.x() << "," << localPos.y() << ")" << std::endl; Chris@121: Chris@121: float s0, s1; Chris@121: float 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@121: int y1 = getYForFrequency(v, f1); Chris@121: int y0 = getYForFrequency(v, f0); Chris@121: Chris@121: std::cerr << "SpectrogramLayer: illuminate " Chris@121: << x0 << "," << y1 << " -> " << x1 << "," << y0 << std::endl; Chris@121: Chris@121: paint.setPen(Qt::white); Chris@121: paint.drawRect(x0, y1, x1 - x0 + 1, y0 - y1 + 1); Chris@121: } Chris@121: } Chris@121: Chris@42: float Chris@44: SpectrogramLayer::getYForFrequency(View *v, float 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@42: float Chris@44: SpectrogramLayer::getFrequencyForY(View *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@115: SpectrogramLayer::getCompletion(View *v) const Chris@0: { Chris@115: if (m_updateTimer == 0) return 100; Chris@115: if (m_fftAdapters.find(v) == m_fftAdapters.end()) return 100; Chris@115: Chris@115: size_t completion = m_fftAdapters[v].first->getFillCompletion(); Chris@115: std::cerr << "SpectrogramLayer::getCompletion: completion = " << completion << std::endl; Chris@0: return completion; Chris@0: } Chris@0: Chris@28: bool Chris@101: SpectrogramLayer::getValueExtents(float &min, float &max, Chris@101: bool &logarithmic, QString &unit) const Chris@79: { Chris@79: min = getEffectiveMinFrequency(); Chris@79: max = getEffectiveMaxFrequency(); Chris@101: logarithmic = (m_frequencyScale == LogFrequencyScale); Chris@79: unit = "Hz"; Chris@79: return true; Chris@79: } Chris@79: Chris@79: bool Chris@101: SpectrogramLayer::getDisplayExtents(float &min, float &max) const Chris@101: { Chris@101: min = getEffectiveMinFrequency(); Chris@101: max = getEffectiveMaxFrequency(); Chris@101: return true; Chris@101: } Chris@101: Chris@101: bool Chris@120: SpectrogramLayer::setDisplayExtents(float min, float max) Chris@120: { Chris@120: if (!m_model) return false; Chris@120: if (min < 0) min = 0; Chris@120: if (max > m_model->getSampleRate()/2) max = m_model->getSampleRate()/2; Chris@120: Chris@120: size_t minf = lrintf(min); Chris@120: size_t maxf = lrintf(max); Chris@120: Chris@120: if (m_minFrequency == minf && m_maxFrequency == maxf) return true; Chris@120: Chris@120: invalidatePixmapCaches(); Chris@120: invalidateMagnitudes(); Chris@120: Chris@120: m_minFrequency = minf; Chris@120: m_maxFrequency = maxf; Chris@120: Chris@120: emit layerParametersChanged(); Chris@120: Chris@120: return true; Chris@120: } Chris@120: Chris@120: bool Chris@44: SpectrogramLayer::snapToFeatureFrame(View *v, int &frame, Chris@28: size_t &resolution, Chris@28: SnapType snap) const Chris@13: { Chris@13: resolution = getWindowIncrement(); Chris@28: int left = (frame / resolution) * resolution; Chris@28: int 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@77: bool Chris@77: SpectrogramLayer::getCrosshairExtents(View *v, QPainter &paint, Chris@77: QPoint cursorPos, Chris@77: std::vector &extents) const Chris@77: { Chris@77: QRect vertical(cursorPos.x() - 12, 0, 12, v->height()); 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@77: return true; Chris@77: } Chris@77: Chris@77: void Chris@77: SpectrogramLayer::paintCrosshairs(View *v, QPainter &paint, Chris@77: QPoint cursorPos) const Chris@77: { Chris@77: paint.save(); Chris@77: paint.setPen(m_crosshairColour); Chris@77: Chris@77: paint.drawLine(0, cursorPos.y(), cursorPos.x() - 1, cursorPos.y()); Chris@77: paint.drawLine(cursorPos.x(), 0, cursorPos.x(), v->height()); Chris@77: Chris@77: float fundamental = getFrequencyForY(v, cursorPos.y()); Chris@77: Chris@77: int harmonic = 2; Chris@77: Chris@77: while (harmonic < 100) { Chris@77: Chris@77: float hy = lrintf(getYForFrequency(v, fundamental * harmonic)); Chris@77: if (hy < 0 || hy > v->height()) 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@77: hy, Chris@77: cursorPos.x(), Chris@77: hy); Chris@77: Chris@77: ++harmonic; Chris@77: } Chris@77: Chris@77: paint.restore(); Chris@77: } Chris@77: Chris@25: QString Chris@44: SpectrogramLayer::getFeatureDescription(View *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@38: float magMin = 0, magMax = 0; Chris@38: float phaseMin = 0, phaseMax = 0; Chris@0: float freqMin = 0, freqMax = 0; Chris@35: float 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@38: float dbMin = AudioLevel::multiplier_to_dB(magMin); Chris@38: float 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@43: dbMinString = QString("%1").arg(lrintf(dbMin)); Chris@43: } Chris@43: if (dbMax == AudioLevel::DB_FLOOR) { Chris@43: dbMaxString = tr("-Inf"); Chris@43: } else { Chris@43: dbMaxString = QString("%1").arg(lrintf(dbMax)); Chris@43: } Chris@25: if (lrintf(dbMin) != lrintf(dbMax)) { Chris@25: text += tr("dB:\t%1 - %2").arg(lrintf(dbMin)).arg(lrintf(dbMax)); Chris@25: } else { Chris@25: text += tr("dB:\t%1").arg(lrintf(dbMin)); 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@119: /*!!! Chris@40: switch (m_colourScale) { Chris@40: default: Chris@40: case LinearColourScale: Chris@40: cw = paint.fontMetrics().width(QString("0.00")); Chris@40: break; Chris@40: Chris@40: case MeterColourScale: Chris@40: case dBColourScale: Chris@119: case OtherColourScale: Chris@40: cw = std::max(paint.fontMetrics().width(tr("-Inf")), Chris@40: paint.fontMetrics().width(tr("-90"))); Chris@40: break; Chris@40: Chris@40: case PhaseColourScale: Chris@40: cw = paint.fontMetrics().width(QString("-") + QChar(0x3c0)); Chris@40: break; Chris@40: } Chris@119: */ Chris@119: Chris@40: Chris@40: return cw; Chris@40: } Chris@40: Chris@40: int Chris@44: SpectrogramLayer::getVerticalScaleWidth(View *v, QPainter &paint) const Chris@0: { Chris@0: if (!m_model || !m_model->isOK()) return 0; Chris@0: Chris@40: int 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@0: int fw = paint.fontMetrics().width(QString("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@44: SpectrogramLayer::paintVerticalScale(View *v, QPainter &paint, QRect rect) const Chris@0: { Chris@0: if (!m_model || !m_model->isOK()) { Chris@0: return; Chris@0: } Chris@0: Chris@122: Profiler profiler("SpectrogramLayer::paintVerticalScale", true); 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@107: size_t bins = m_fftSize / 2; Chris@0: int 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@40: int 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@119: if (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@119: /*!!! Chris@40: switch (m_colourScale) { Chris@40: default: Chris@40: case LinearColourScale: Chris@40: top = (m_normalizeColumns ? "1.0" : "0.02"); Chris@40: bottom = (m_normalizeColumns ? "0.0" : "0.00"); Chris@40: break; Chris@40: Chris@40: case MeterColourScale: Chris@40: top = (m_normalizeColumns ? QString("0") : Chris@40: QString("%1").arg(int(AudioLevel::multiplier_to_dB(0.02)))); Chris@40: bottom = QString("%1"). Chris@40: arg(int(AudioLevel::multiplier_to_dB Chris@40: (AudioLevel::preview_to_multiplier(0, 255)))); Chris@40: break; Chris@40: Chris@40: case dBColourScale: Chris@119: case OtherColourScale: Chris@40: top = "0"; Chris@40: bottom = "-80"; Chris@40: break; Chris@40: Chris@40: case PhaseColourScale: Chris@40: top = QChar(0x3c0); Chris@40: bottom = "-" + top; Chris@40: break; Chris@40: } Chris@119: */ Chris@119: float min = m_viewMags[v].getMin(); Chris@119: float max = m_viewMags[v].getMax(); Chris@119: Chris@119: float dBmin = AudioLevel::multiplier_to_dB(min); Chris@119: float dBmax = AudioLevel::multiplier_to_dB(max); Chris@119: Chris@120: if (dBmax < -60.f) dBmax = -60.f; Chris@120: else top = QString("%1").arg(lrintf(dBmax)); Chris@120: Chris@120: if (dBmin < dBmax - 60.f) dBmin = dBmax - 60.f; Chris@119: bottom = QString("%1").arg(lrintf(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@119: float dBval = dBmin + (((dBmax - dBmin) * i) / (ch - 1)); Chris@119: int idb = int(dBval); Chris@119: Chris@119: float value = AudioLevel::dB_to_multiplier(dBval); Chris@119: int colour = getDisplayValue(v, value * m_gain); Chris@119: /* Chris@119: float value = min + (((max - min) * i) / (ch - 1)); Chris@119: if (value < m_threshold) value = 0.f; Chris@119: int colour = getDisplayValue(v, value * m_gain); Chris@119: */ Chris@119: /* Chris@119: int colour = (i * 255) / ch + 1; Chris@119: */ Chris@119: paint.setPen(m_colourMap.getColour(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: // paint.drawLine(5, 4 + textHeight + ch - i, Chris@119: // cw + 2, 4 + textHeight + ch - i); Chris@119: 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@119: paint.setPen(Qt::black); 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@119: paint.setPen(Qt::white); 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@44: for (int y = 0; y < v->height(); ++y) { Chris@0: Chris@0: float q0, q1; Chris@44: if (!getYBinRange(v, v->height() - 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@107: int freq = (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@40: if (bin == 1) text = QString("%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@40: int tx = w - 3 - paint.fontMetrics().width(text) - std::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@40: paint.drawLine(w - pkw - 1, 0, w - pkw - 1, h); Chris@40: Chris@110: int sr = m_model->getSampleRate(); Chris@40: float minf = getEffectiveMinFrequency(); Chris@40: float maxf = getEffectiveMaxFrequency(); Chris@40: Chris@122: int py = h, ppy = h; Chris@40: paint.setBrush(paint.pen().color()); Chris@40: Chris@40: for (int i = 0; i < 128; ++i) { Chris@40: Chris@40: float f = Pitch::getFrequencyForPitch(i); Chris@44: int y = lrintf(v->getYForFrequency(f, minf, maxf, true)); Chris@122: Chris@122: if (y < -2) break; Chris@122: if (y > h + 2) { Chris@122: continue; Chris@122: } Chris@122: Chris@40: int n = (i % 12); Chris@122: Chris@122: if (n == 1) { Chris@122: // C# -- fill the C from here Chris@122: if (ppy - y > 2) { Chris@122: paint.fillRect(w - pkw, Chris@122: // y - (py - y) / 2 - (py - y) / 4, Chris@122: y, Chris@122: pkw, Chris@122: (py + ppy) / 2 - y, Chris@122: // py - y + 1, Chris@122: Qt::gray); Chris@122: } Chris@122: } Chris@122: Chris@40: if (n == 1 || n == 3 || n == 6 || n == 8 || n == 10) { Chris@40: // black notes Chris@40: paint.drawLine(w - pkw, y, w, y); Chris@41: int rh = ((py - y) / 4) * 2; Chris@41: if (rh < 2) rh = 2; Chris@41: paint.drawRect(w - pkw, y - (py-y)/4, pkw/2, rh); Chris@40: } else if (n == 0 || n == 5) { Chris@122: // C, F Chris@40: if (py < h) { Chris@40: paint.drawLine(w - pkw, (y + py) / 2, w, (y + py) / 2); Chris@40: } Chris@40: } Chris@40: Chris@122: ppy = py; Chris@40: py = y; Chris@40: } Chris@40: } Chris@0: } Chris@0: Chris@6: QString Chris@6: SpectrogramLayer::toXmlString(QString indent, QString extraAttributes) const Chris@6: { Chris@6: QString s; Chris@6: Chris@6: s += QString("channel=\"%1\" " Chris@6: "windowSize=\"%2\" " Chris@6: "windowType=\"%3\" " Chris@97: "windowHopLevel=\"%4\" " Chris@37: "gain=\"%5\" " Chris@37: "threshold=\"%6\" ") Chris@6: .arg(m_channel) Chris@6: .arg(m_windowSize) Chris@6: .arg(m_windowType) 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@37: "binDisplay=\"%7\" " Chris@37: "normalizeColumns=\"%8\"") Chris@37: .arg(m_minFrequency) Chris@6: .arg(m_maxFrequency) Chris@6: .arg(m_colourScale) Chris@6: .arg(m_colourScheme) Chris@37: .arg(m_colourRotation) Chris@35: .arg(m_frequencyScale) Chris@37: .arg(m_binDisplay) Chris@36: .arg(m_normalizeColumns ? "true" : "false"); Chris@6: Chris@6: return Layer::toXmlString(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@11: size_t windowSize = attributes.value("windowSize").toUInt(&ok); Chris@11: if (ok) setWindowSize(windowSize); Chris@11: Chris@11: WindowType windowType = (WindowType) Chris@11: attributes.value("windowType").toInt(&ok); Chris@11: if (ok) setWindowType(windowType); Chris@11: Chris@97: size_t windowHopLevel = attributes.value("windowHopLevel").toUInt(&ok); Chris@97: if (ok) setWindowHopLevel(windowHopLevel); Chris@97: else { Chris@97: size_t 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@37: size_t minFrequency = attributes.value("minFrequency").toUInt(&ok); Chris@37: if (ok) setMinFrequency(minFrequency); Chris@37: Chris@11: size_t maxFrequency = attributes.value("maxFrequency").toUInt(&ok); Chris@11: if (ok) setMaxFrequency(maxFrequency); Chris@11: Chris@11: ColourScale colourScale = (ColourScale) Chris@11: attributes.value("colourScale").toInt(&ok); Chris@11: if (ok) setColourScale(colourScale); Chris@11: Chris@11: ColourScheme colourScheme = (ColourScheme) Chris@11: attributes.value("colourScheme").toInt(&ok); Chris@11: if (ok) setColourScheme(colourScheme); 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@36: bool normalizeColumns = Chris@36: (attributes.value("normalizeColumns").trimmed() == "true"); Chris@36: setNormalizeColumns(normalizeColumns); Chris@11: } Chris@11: Chris@11: Chris@0: #ifdef INCLUDE_MOCFILES Chris@0: #include "SpectrogramLayer.moc.cpp" Chris@0: #endif Chris@0: