annotate layer/SpectrogramLayer.h @ 1094:8a815776151c spectrogram-minor-refactor

Split out cache rendering functions and some fixes to calculations
author Chris Cannam
date Thu, 07 Jul 2016 19:18:31 +0100
parents cd22f74dc159
children d84a0033b305
rev   line source
Chris@58 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@0 2
Chris@0 3 /*
Chris@59 4 Sonic Visualiser
Chris@59 5 An audio file viewer and annotation editor.
Chris@59 6 Centre for Digital Music, Queen Mary, University of London.
Chris@182 7 This file copyright 2006 Chris Cannam and QMUL.
Chris@0 8
Chris@59 9 This program is free software; you can redistribute it and/or
Chris@59 10 modify it under the terms of the GNU General Public License as
Chris@59 11 published by the Free Software Foundation; either version 2 of the
Chris@59 12 License, or (at your option) any later version. See the file
Chris@59 13 COPYING included with this distribution for more information.
Chris@0 14 */
Chris@0 15
Chris@1030 16 #ifndef SPECTROGRAM_LAYER_H
Chris@1030 17 #define SPECTROGRAM_LAYER_H
Chris@0 18
Chris@193 19 #include "SliceableLayer.h"
Chris@0 20 #include "base/Window.h"
Chris@1058 21 #include "base/MagnitudeRange.h"
Chris@71 22 #include "base/RealTime.h"
Chris@92 23 #include "base/Thread.h"
Chris@122 24 #include "base/PropertyContainer.h"
Chris@128 25 #include "data/model/PowerOfSqrtTwoZoomConstraint.h"
Chris@128 26 #include "data/model/DenseTimeValueModel.h"
Chris@130 27 #include "data/model/FFTModel.h"
Chris@0 28
Chris@1030 29 #include "ScrollableImageCache.h"
Chris@1085 30 #include "VerticalBinLayer.h"
Chris@1092 31 #include "ColourScale.h"
Chris@1093 32 #include "Colour3DPlotRenderer.h"
Chris@1030 33
Chris@0 34 #include <QMutex>
Chris@0 35 #include <QWaitCondition>
Chris@95 36 #include <QImage>
Chris@95 37 #include <QPixmap>
Chris@0 38
Chris@0 39 class View;
Chris@0 40 class QPainter;
Chris@0 41 class QImage;
Chris@0 42 class QPixmap;
Chris@0 43 class QTimer;
Chris@130 44 class FFTModel;
Chris@484 45 class Dense3DModelPeakCache;
Chris@0 46
Chris@0 47 /**
Chris@0 48 * SpectrogramLayer represents waveform data (obtained from a
Chris@0 49 * DenseTimeValueModel) in spectrogram form.
Chris@0 50 */
Chris@0 51
Chris@193 52 class SpectrogramLayer : public SliceableLayer,
Chris@1085 53 public VerticalBinLayer,
Chris@31 54 public PowerOfSqrtTwoZoomConstraint
Chris@0 55 {
Chris@0 56 Q_OBJECT
Chris@0 57
Chris@0 58 public:
Chris@37 59 enum Configuration { FullRangeDb, MelodicRange, MelodicPeaks };
Chris@0 60
Chris@44 61 SpectrogramLayer(Configuration = FullRangeDb);
Chris@0 62 ~SpectrogramLayer();
Chris@0 63
Chris@0 64 virtual const ZoomConstraint *getZoomConstraint() const { return this; }
Chris@0 65 virtual const Model *getModel() const { return m_model; }
Chris@916 66 virtual void paint(LayerGeometryProvider *v, QPainter &paint, QRect rect) const;
Chris@389 67 virtual void setSynchronousPainting(bool synchronous);
Chris@0 68
Chris@918 69 virtual int getVerticalScaleWidth(LayerGeometryProvider *v, bool detailed, QPainter &) const;
Chris@918 70 virtual void paintVerticalScale(LayerGeometryProvider *v, bool detailed, QPainter &paint, QRect rect) const;
Chris@0 71
Chris@918 72 virtual bool getCrosshairExtents(LayerGeometryProvider *, QPainter &, QPoint cursorPos,
Chris@77 73 std::vector<QRect> &extents) const;
Chris@918 74 virtual void paintCrosshairs(LayerGeometryProvider *, QPainter &, QPoint) const;
Chris@77 75
Chris@918 76 virtual QString getFeatureDescription(LayerGeometryProvider *v, QPoint &) const;
Chris@0 77
Chris@918 78 virtual bool snapToFeatureFrame(LayerGeometryProvider *v, sv_frame_t &frame,
Chris@805 79 int &resolution,
Chris@28 80 SnapType snap) const;
Chris@13 81
Chris@918 82 virtual void measureDoubleClick(LayerGeometryProvider *, QMouseEvent *);
Chris@283 83
Chris@224 84 virtual bool hasLightBackground() const;
Chris@224 85
Chris@0 86 void setModel(const DenseTimeValueModel *model);
Chris@0 87
Chris@0 88 virtual PropertyList getProperties() const;
Chris@87 89 virtual QString getPropertyLabel(const PropertyName &) const;
Chris@335 90 virtual QString getPropertyIconName(const PropertyName &) const;
Chris@0 91 virtual PropertyType getPropertyType(const PropertyName &) const;
Chris@0 92 virtual QString getPropertyGroupName(const PropertyName &) const;
Chris@0 93 virtual int getPropertyRangeAndValue(const PropertyName &,
Chris@216 94 int *min, int *max, int *deflt) const;
Chris@0 95 virtual QString getPropertyValueLabel(const PropertyName &,
Chris@0 96 int value) const;
Chris@862 97 virtual QString getPropertyValueIconName(const PropertyName &,
Chris@862 98 int value) const;
Chris@167 99 virtual RangeMapper *getNewPropertyRangeMapper(const PropertyName &) const;
Chris@0 100 virtual void setProperty(const PropertyName &, int value);
Chris@0 101
Chris@0 102 /**
Chris@0 103 * Specify the channel to use from the source model.
Chris@0 104 * A value of -1 means to mix all available channels.
Chris@0 105 * The default is channel 0.
Chris@0 106 */
Chris@0 107 void setChannel(int);
Chris@0 108 int getChannel() const;
Chris@0 109
Chris@805 110 void setWindowSize(int);
Chris@805 111 int getWindowSize() const;
Chris@0 112
Chris@805 113 void setWindowHopLevel(int level);
Chris@805 114 int getWindowHopLevel() const;
Chris@0 115
Chris@0 116 void setWindowType(WindowType type);
Chris@0 117 WindowType getWindowType() const;
Chris@0 118
Chris@0 119 /**
Chris@110 120 * Set the gain multiplier for sample values in this view.
Chris@0 121 * The default is 1.0.
Chris@0 122 */
Chris@0 123 void setGain(float gain);
Chris@0 124 float getGain() const;
Chris@0 125
Chris@37 126 /**
Chris@110 127 * Set the threshold for sample values to qualify for being shown
Chris@110 128 * in the FFT, in voltage units.
Chris@37 129 *
Chris@37 130 * The default is 0.0.
Chris@37 131 */
Chris@37 132 void setThreshold(float threshold);
Chris@37 133 float getThreshold() const;
Chris@37 134
Chris@805 135 void setMinFrequency(int);
Chris@805 136 int getMinFrequency() const;
Chris@37 137
Chris@805 138 void setMaxFrequency(int); // 0 -> no maximum
Chris@805 139 int getMaxFrequency() const;
Chris@0 140
Chris@0 141 /**
Chris@1092 142 * Specify the scale for sample levels. See ColourScale and
Chris@1092 143 * WaveformLayer for comparison and details of meter and dB
Chris@1092 144 * scaling. The default is LogColourScale.
Chris@0 145 */
Chris@1092 146 void setColourScale(ColourScale::Scale);
Chris@1092 147 ColourScale::Scale getColourScale() const;
Chris@0 148
Chris@0 149 /**
Chris@0 150 * Specify the scale for the y axis.
Chris@0 151 */
Chris@1093 152 void setBinScale(Colour3DPlotRenderer::BinScale);
Chris@1093 153 Colour3DPlotRenderer::BinScale getBinScale() const;
Chris@0 154
Chris@35 155 /**
Chris@35 156 * Specify the processing of frequency bins for the y axis.
Chris@35 157 */
Chris@1093 158 void setBinDisplay(Colour3DPlotRenderer::BinDisplay);
Chris@1093 159 Colour3DPlotRenderer::BinDisplay getBinDisplay() const;
Chris@862 160
Chris@719 161 /**
Chris@862 162 * Specify the normalization mode for bin values.
Chris@719 163 */
Chris@1063 164 void setNormalization(ColumnOp::Normalization);
Chris@1063 165 ColumnOp::Normalization getNormalization() const;
Chris@120 166
Chris@719 167 /**
Chris@862 168 * Specify the colour map. See ColourMapper for the colour map
Chris@862 169 * values.
Chris@719 170 */
Chris@197 171 void setColourMap(int map);
Chris@197 172 int getColourMap() const;
Chris@0 173
Chris@9 174 /**
Chris@9 175 * Specify the colourmap rotation for the colour scale.
Chris@9 176 */
Chris@9 177 void setColourRotation(int);
Chris@9 178 int getColourRotation() const;
Chris@9 179
Chris@0 180 virtual VerticalPosition getPreferredFrameCountPosition() const {
Chris@0 181 return PositionTop;
Chris@0 182 }
Chris@0 183
Chris@15 184 virtual bool isLayerOpaque() const { return true; }
Chris@287 185
Chris@287 186 virtual ColourSignificance getLayerColourSignificance() const {
Chris@287 187 return ColourHasMeaningfulValue;
Chris@287 188 }
Chris@15 189
Chris@918 190 double getYForFrequency(const LayerGeometryProvider *v, double frequency) const;
Chris@918 191 double getFrequencyForY(const LayerGeometryProvider *v, int y) const;
Chris@42 192
Chris@1085 193 //!!! VerticalBinLayer methods. Note overlap with get*BinRange()
Chris@1085 194 double getYForBin(LayerGeometryProvider *, double bin) const;
Chris@1085 195 double getBinForY(LayerGeometryProvider *, double y) const;
Chris@1085 196
Chris@918 197 virtual int getCompletion(LayerGeometryProvider *v) const;
Chris@918 198 virtual QString getError(LayerGeometryProvider *v) const;
Chris@0 199
Chris@905 200 virtual bool getValueExtents(double &min, double &max,
Chris@101 201 bool &logarithmic, QString &unit) const;
Chris@101 202
Chris@905 203 virtual bool getDisplayExtents(double &min, double &max) const;
Chris@79 204
Chris@905 205 virtual bool setDisplayExtents(double min, double max);
Chris@120 206
Chris@918 207 virtual bool getYScaleValue(const LayerGeometryProvider *, int, double &, QString &) const;
Chris@261 208
Chris@316 209 virtual void toXml(QTextStream &stream, QString indent = "",
Chris@316 210 QString extraAttributes = "") const;
Chris@6 211
Chris@11 212 void setProperties(const QXmlAttributes &attributes);
Chris@11 213
Chris@918 214 virtual void setLayerDormant(const LayerGeometryProvider *v, bool dormant);
Chris@29 215
Chris@918 216 virtual bool isLayerScrollable(const LayerGeometryProvider *) const { return false; }
Chris@94 217
Chris@133 218 virtual int getVerticalZoomSteps(int &defaultStep) const;
Chris@133 219 virtual int getCurrentVerticalZoomStep() const;
Chris@133 220 virtual void setVerticalZoomStep(int);
Chris@187 221 virtual RangeMapper *getNewVerticalZoomRangeMapper() const;
Chris@133 222
Chris@193 223 virtual const Model *getSliceableModel() const;
Chris@193 224
Chris@0 225 protected slots:
Chris@0 226 void cacheInvalid();
Chris@906 227 void cacheInvalid(sv_frame_t startFrame, sv_frame_t endFrame);
Chris@122 228
Chris@122 229 void preferenceChanged(PropertyContainer::PropertyName name);
Chris@0 230
Chris@0 231 protected:
Chris@0 232 const DenseTimeValueModel *m_model; // I do not own this
Chris@484 233
Chris@35 234 int m_channel;
Chris@1023 235 int m_windowSize;
Chris@35 236 WindowType m_windowType;
Chris@1023 237 int m_windowHopLevel;
Chris@35 238 float m_gain;
Chris@215 239 float m_initialGain;
Chris@37 240 float m_threshold;
Chris@215 241 float m_initialThreshold;
Chris@35 242 int m_colourRotation;
Chris@215 243 int m_initialRotation;
Chris@1023 244 int m_minFrequency;
Chris@1023 245 int m_maxFrequency;
Chris@1023 246 int m_initialMaxFrequency;
Chris@1092 247 ColourScale::Scale m_colourScale;
Chris@197 248 int m_colourMap;
Chris@77 249 QColor m_crosshairColour;
Chris@1093 250 Colour3DPlotRenderer::BinScale m_binScale;
Chris@1093 251 Colour3DPlotRenderer::BinDisplay m_binDisplay;
Chris@1063 252 ColumnOp::Normalization m_normalization;
Chris@133 253 int m_lastEmittedZoomStep;
Chris@389 254 bool m_synchronous;
Chris@0 255
Chris@608 256 mutable bool m_haveDetailedScale;
Chris@215 257
Chris@38 258 enum { NO_VALUE = 0 }; // colour index for unused pixels
Chris@38 259
Chris@197 260 class Palette
Chris@86 261 {
Chris@86 262 public:
Chris@86 263 QColor getColour(unsigned char index) const {
Chris@86 264 return m_colours[index];
Chris@86 265 }
Chris@86 266
Chris@86 267 void setColour(unsigned char index, QColor colour) {
Chris@86 268 m_colours[index] = colour;
Chris@86 269 }
Chris@86 270
Chris@86 271 private:
Chris@86 272 QColor m_colours[256];
Chris@86 273 };
Chris@86 274
Chris@197 275 Palette m_palette;
Chris@31 276
Chris@1030 277 typedef std::map<int, ScrollableImageCache> ViewImageCache; // key is view id
Chris@478 278 void invalidateImageCaches();
Chris@478 279 mutable ViewImageCache m_imageCaches;
Chris@1030 280 ScrollableImageCache &getImageCacheReference(const LayerGeometryProvider *) const;
Chris@477 281
Chris@477 282 /**
Chris@477 283 * When painting, we draw directly onto the draw buffer and then
Chris@478 284 * copy this to the part of the image cache that needed refreshing
Chris@478 285 * before copying the image cache onto the window. (Remind me why
Chris@1073 286 * we don't draw directly onto the cache?) (Answer: it's because
Chris@1073 287 * we usually apply scaling and smoothing in the x axis when
Chris@1073 288 * copying from draw buffer to cache)
Chris@477 289 */
Chris@95 290 mutable QImage m_drawBuffer;
Chris@0 291
Chris@0 292 bool m_exiting;
Chris@0 293
Chris@197 294 void initialisePalette();
Chris@197 295 void rotatePalette(int distance);
Chris@0 296
Chris@918 297 unsigned char getDisplayValue(LayerGeometryProvider *v, double input) const;
Chris@40 298
Chris@40 299 int getColourScaleWidth(QPainter &) const;
Chris@40 300
Chris@918 301 void illuminateLocalFeatures(LayerGeometryProvider *v, QPainter &painter) const;
Chris@121 302
Chris@905 303 double getEffectiveMinFrequency() const;
Chris@905 304 double getEffectiveMaxFrequency() const;
Chris@38 305
Chris@918 306 bool getXBinRange(LayerGeometryProvider *v, int x, double &windowMin, double &windowMax) const;
Chris@918 307 bool getYBinRange(LayerGeometryProvider *v, int y, double &freqBinMin, double &freqBinMax) const;
Chris@0 308
Chris@918 309 bool getYBinSourceRange(LayerGeometryProvider *v, int y, double &freqMin, double &freqMax) const;
Chris@918 310 bool getAdjustedYBinSourceRange(LayerGeometryProvider *v, int x, int y,
Chris@905 311 double &freqMin, double &freqMax,
Chris@905 312 double &adjFreqMin, double &adjFreqMax) const;
Chris@918 313 bool getXBinSourceRange(LayerGeometryProvider *v, int x, RealTime &timeMin, RealTime &timeMax) const;
Chris@918 314 bool getXYBinSourceRange(LayerGeometryProvider *v, int x, int y, double &min, double &max,
Chris@905 315 double &phaseMin, double &phaseMax) const;
Chris@0 316
Chris@805 317 int getWindowIncrement() const {
Chris@97 318 if (m_windowHopLevel == 0) return m_windowSize;
Chris@97 319 else if (m_windowHopLevel == 1) return (m_windowSize * 3) / 4;
Chris@97 320 else return m_windowSize / (1 << (m_windowHopLevel - 1));
Chris@0 321 }
Chris@113 322
Chris@1086 323 int getFFTOversampling() const;
Chris@1087 324 int getFFTSize() const; // m_windowSize * getFFTOversampling()
Chris@115 325
Chris@1089 326 mutable FFTModel *m_fftModel; //!!! should not be mutable, see getFFTModel()?
Chris@1088 327 mutable Dense3DModelPeakCache *m_peakCache;
Chris@1054 328 const int m_peakCacheDivisor;
Chris@119 329
Chris@1030 330 typedef std::map<int, MagnitudeRange> ViewMagMap; // key is view id
Chris@119 331 mutable ViewMagMap m_viewMags;
Chris@119 332 mutable std::vector<MagnitudeRange> m_columnMags;
Chris@119 333 void invalidateMagnitudes();
Chris@918 334 bool updateViewMagnitudes(LayerGeometryProvider *v) const;
Chris@1089 335
Chris@1089 336 typedef std::map<int, Colour3DPlotRenderer *> ViewRendererMap; // key is view id
Chris@1089 337 mutable ViewRendererMap m_renderers;
Chris@1089 338 Colour3DPlotRenderer *getRenderer(LayerGeometryProvider *) const;
Chris@1058 339
Chris@1088 340 FFTModel *getFFTModel() const;
Chris@1088 341 Dense3DModelPeakCache *getPeakCache() const;
Chris@1088 342 void invalidateFFTModel();
Chris@1089 343
Chris@1089 344 void paintAlternative(LayerGeometryProvider *v, QPainter &paint, QRect rect) const;
Chris@1088 345
Chris@1024 346 int paintDrawBuffer(LayerGeometryProvider *v, int w, int h,
Chris@1024 347 const std::vector<int> &binforx,
Chris@1024 348 const std::vector<double> &binfory,
Chris@1024 349 bool usePeaksCache,
Chris@1024 350 MagnitudeRange &overallMag,
Chris@1026 351 bool &overallMagChanged,
Chris@1039 352 bool rightToLeft,
Chris@1039 353 double softTimeLimit) const;
Chris@1024 354 int paintDrawBufferPeakFrequencies(LayerGeometryProvider *v, int w, int h,
Chris@1024 355 const std::vector<int> &binforx,
Chris@1024 356 int minbin,
Chris@1024 357 int maxbin,
Chris@1024 358 double displayMinFreq,
Chris@1024 359 double displayMaxFreq,
Chris@1024 360 bool logarithmic,
Chris@1024 361 MagnitudeRange &overallMag,
Chris@1026 362 bool &overallMagChanged,
Chris@1039 363 bool rightToLeft,
Chris@1039 364 double softTimeLimit) const;
Chris@273 365
Chris@1058 366 std::vector<float> getColumnFromFFTModel(FFTModel *model,
Chris@1058 367 int sx,
Chris@1058 368 int minbin,
Chris@1058 369 int bincount) const;
Chris@1058 370
Chris@1058 371 std::vector<float> getColumnFromGenericModel(DenseThreeDimensionalModel *model,
Chris@1058 372 int sx,
Chris@1058 373 int minbin,
Chris@1058 374 int bincount) const;
Chris@1058 375
Chris@1059 376 void recordColumnExtents(const std::vector<float> &col,
Chris@1059 377 int sx,
Chris@1059 378 MagnitudeRange &overallMag,
Chris@1059 379 bool &overallMagChanged) const;
Chris@1058 380
Chris@1058 381 virtual void updateMeasureRectYCoords(LayerGeometryProvider *v,
Chris@1058 382 const MeasureRect &r) const;
Chris@1058 383 virtual void setMeasureRectYCoord(LayerGeometryProvider *v,
Chris@1058 384 MeasureRect &r, bool start, int y) const;
Chris@0 385 };
Chris@0 386
Chris@0 387 #endif