annotate layer/SpectrogramLayer.h @ 1092:9b826dd23c84 spectrogram-minor-refactor

Colour scales (partly)
author Chris Cannam
date Thu, 07 Jul 2016 12:39:56 +0100
parents c8683d94442a
children cd22f74dc159
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@1030 32
Chris@0 33 #include <QMutex>
Chris@0 34 #include <QWaitCondition>
Chris@95 35 #include <QImage>
Chris@95 36 #include <QPixmap>
Chris@0 37
Chris@0 38 class View;
Chris@0 39 class QPainter;
Chris@0 40 class QImage;
Chris@0 41 class QPixmap;
Chris@0 42 class QTimer;
Chris@130 43 class FFTModel;
Chris@484 44 class Dense3DModelPeakCache;
Chris@1089 45 class Colour3DPlotRenderer;
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@35 149 enum FrequencyScale {
Chris@35 150 LinearFrequencyScale,
Chris@35 151 LogFrequencyScale
Chris@35 152 };
Chris@0 153
Chris@0 154 /**
Chris@0 155 * Specify the scale for the y axis.
Chris@0 156 */
Chris@0 157 void setFrequencyScale(FrequencyScale);
Chris@0 158 FrequencyScale getFrequencyScale() const;
Chris@0 159
Chris@37 160 enum BinDisplay {
Chris@37 161 AllBins,
Chris@37 162 PeakBins,
Chris@37 163 PeakFrequencies
Chris@35 164 };
Chris@35 165
Chris@35 166 /**
Chris@35 167 * Specify the processing of frequency bins for the y axis.
Chris@35 168 */
Chris@37 169 void setBinDisplay(BinDisplay);
Chris@37 170 BinDisplay getBinDisplay() const;
Chris@862 171
Chris@719 172 /**
Chris@862 173 * Specify the normalization mode for bin values.
Chris@719 174 */
Chris@1063 175 void setNormalization(ColumnOp::Normalization);
Chris@1063 176 ColumnOp::Normalization getNormalization() const;
Chris@120 177
Chris@719 178 /**
Chris@862 179 * Specify the colour map. See ColourMapper for the colour map
Chris@862 180 * values.
Chris@719 181 */
Chris@197 182 void setColourMap(int map);
Chris@197 183 int getColourMap() const;
Chris@0 184
Chris@9 185 /**
Chris@9 186 * Specify the colourmap rotation for the colour scale.
Chris@9 187 */
Chris@9 188 void setColourRotation(int);
Chris@9 189 int getColourRotation() const;
Chris@9 190
Chris@0 191 virtual VerticalPosition getPreferredFrameCountPosition() const {
Chris@0 192 return PositionTop;
Chris@0 193 }
Chris@0 194
Chris@15 195 virtual bool isLayerOpaque() const { return true; }
Chris@287 196
Chris@287 197 virtual ColourSignificance getLayerColourSignificance() const {
Chris@287 198 return ColourHasMeaningfulValue;
Chris@287 199 }
Chris@15 200
Chris@918 201 double getYForFrequency(const LayerGeometryProvider *v, double frequency) const;
Chris@918 202 double getFrequencyForY(const LayerGeometryProvider *v, int y) const;
Chris@42 203
Chris@1085 204 //!!! VerticalBinLayer methods. Note overlap with get*BinRange()
Chris@1085 205 double getYForBin(LayerGeometryProvider *, double bin) const;
Chris@1085 206 double getBinForY(LayerGeometryProvider *, double y) const;
Chris@1085 207
Chris@918 208 virtual int getCompletion(LayerGeometryProvider *v) const;
Chris@918 209 virtual QString getError(LayerGeometryProvider *v) const;
Chris@0 210
Chris@905 211 virtual bool getValueExtents(double &min, double &max,
Chris@101 212 bool &logarithmic, QString &unit) const;
Chris@101 213
Chris@905 214 virtual bool getDisplayExtents(double &min, double &max) const;
Chris@79 215
Chris@905 216 virtual bool setDisplayExtents(double min, double max);
Chris@120 217
Chris@918 218 virtual bool getYScaleValue(const LayerGeometryProvider *, int, double &, QString &) const;
Chris@261 219
Chris@316 220 virtual void toXml(QTextStream &stream, QString indent = "",
Chris@316 221 QString extraAttributes = "") const;
Chris@6 222
Chris@11 223 void setProperties(const QXmlAttributes &attributes);
Chris@11 224
Chris@918 225 virtual void setLayerDormant(const LayerGeometryProvider *v, bool dormant);
Chris@29 226
Chris@918 227 virtual bool isLayerScrollable(const LayerGeometryProvider *) const { return false; }
Chris@94 228
Chris@133 229 virtual int getVerticalZoomSteps(int &defaultStep) const;
Chris@133 230 virtual int getCurrentVerticalZoomStep() const;
Chris@133 231 virtual void setVerticalZoomStep(int);
Chris@187 232 virtual RangeMapper *getNewVerticalZoomRangeMapper() const;
Chris@133 233
Chris@193 234 virtual const Model *getSliceableModel() const;
Chris@193 235
Chris@0 236 protected slots:
Chris@0 237 void cacheInvalid();
Chris@906 238 void cacheInvalid(sv_frame_t startFrame, sv_frame_t endFrame);
Chris@122 239
Chris@122 240 void preferenceChanged(PropertyContainer::PropertyName name);
Chris@0 241
Chris@0 242 protected:
Chris@0 243 const DenseTimeValueModel *m_model; // I do not own this
Chris@484 244
Chris@35 245 int m_channel;
Chris@1023 246 int m_windowSize;
Chris@35 247 WindowType m_windowType;
Chris@1023 248 int m_windowHopLevel;
Chris@35 249 float m_gain;
Chris@215 250 float m_initialGain;
Chris@37 251 float m_threshold;
Chris@215 252 float m_initialThreshold;
Chris@35 253 int m_colourRotation;
Chris@215 254 int m_initialRotation;
Chris@1023 255 int m_minFrequency;
Chris@1023 256 int m_maxFrequency;
Chris@1023 257 int m_initialMaxFrequency;
Chris@1092 258 ColourScale::Scale m_colourScale;
Chris@197 259 int m_colourMap;
Chris@77 260 QColor m_crosshairColour;
Chris@35 261 FrequencyScale m_frequencyScale;
Chris@37 262 BinDisplay m_binDisplay;
Chris@1063 263 ColumnOp::Normalization m_normalization;
Chris@133 264 int m_lastEmittedZoomStep;
Chris@389 265 bool m_synchronous;
Chris@0 266
Chris@608 267 mutable bool m_haveDetailedScale;
Chris@215 268
Chris@38 269 enum { NO_VALUE = 0 }; // colour index for unused pixels
Chris@38 270
Chris@197 271 class Palette
Chris@86 272 {
Chris@86 273 public:
Chris@86 274 QColor getColour(unsigned char index) const {
Chris@86 275 return m_colours[index];
Chris@86 276 }
Chris@86 277
Chris@86 278 void setColour(unsigned char index, QColor colour) {
Chris@86 279 m_colours[index] = colour;
Chris@86 280 }
Chris@86 281
Chris@86 282 private:
Chris@86 283 QColor m_colours[256];
Chris@86 284 };
Chris@86 285
Chris@197 286 Palette m_palette;
Chris@31 287
Chris@1030 288 typedef std::map<int, ScrollableImageCache> ViewImageCache; // key is view id
Chris@478 289 void invalidateImageCaches();
Chris@478 290 mutable ViewImageCache m_imageCaches;
Chris@1030 291 ScrollableImageCache &getImageCacheReference(const LayerGeometryProvider *) const;
Chris@477 292
Chris@477 293 /**
Chris@477 294 * When painting, we draw directly onto the draw buffer and then
Chris@478 295 * copy this to the part of the image cache that needed refreshing
Chris@478 296 * before copying the image cache onto the window. (Remind me why
Chris@1073 297 * we don't draw directly onto the cache?) (Answer: it's because
Chris@1073 298 * we usually apply scaling and smoothing in the x axis when
Chris@1073 299 * copying from draw buffer to cache)
Chris@477 300 */
Chris@95 301 mutable QImage m_drawBuffer;
Chris@0 302
Chris@0 303 bool m_exiting;
Chris@0 304
Chris@197 305 void initialisePalette();
Chris@197 306 void rotatePalette(int distance);
Chris@0 307
Chris@918 308 unsigned char getDisplayValue(LayerGeometryProvider *v, double input) const;
Chris@40 309
Chris@40 310 int getColourScaleWidth(QPainter &) const;
Chris@40 311
Chris@918 312 void illuminateLocalFeatures(LayerGeometryProvider *v, QPainter &painter) const;
Chris@121 313
Chris@905 314 double getEffectiveMinFrequency() const;
Chris@905 315 double getEffectiveMaxFrequency() const;
Chris@38 316
Chris@918 317 bool getXBinRange(LayerGeometryProvider *v, int x, double &windowMin, double &windowMax) const;
Chris@918 318 bool getYBinRange(LayerGeometryProvider *v, int y, double &freqBinMin, double &freqBinMax) const;
Chris@0 319
Chris@918 320 bool getYBinSourceRange(LayerGeometryProvider *v, int y, double &freqMin, double &freqMax) const;
Chris@918 321 bool getAdjustedYBinSourceRange(LayerGeometryProvider *v, int x, int y,
Chris@905 322 double &freqMin, double &freqMax,
Chris@905 323 double &adjFreqMin, double &adjFreqMax) const;
Chris@918 324 bool getXBinSourceRange(LayerGeometryProvider *v, int x, RealTime &timeMin, RealTime &timeMax) const;
Chris@918 325 bool getXYBinSourceRange(LayerGeometryProvider *v, int x, int y, double &min, double &max,
Chris@905 326 double &phaseMin, double &phaseMax) const;
Chris@0 327
Chris@805 328 int getWindowIncrement() const {
Chris@97 329 if (m_windowHopLevel == 0) return m_windowSize;
Chris@97 330 else if (m_windowHopLevel == 1) return (m_windowSize * 3) / 4;
Chris@97 331 else return m_windowSize / (1 << (m_windowHopLevel - 1));
Chris@0 332 }
Chris@113 333
Chris@1086 334 int getFFTOversampling() const;
Chris@1087 335 int getFFTSize() const; // m_windowSize * getFFTOversampling()
Chris@115 336
Chris@1089 337 mutable FFTModel *m_fftModel; //!!! should not be mutable, see getFFTModel()?
Chris@1088 338 mutable Dense3DModelPeakCache *m_peakCache;
Chris@1054 339 const int m_peakCacheDivisor;
Chris@119 340
Chris@1030 341 typedef std::map<int, MagnitudeRange> ViewMagMap; // key is view id
Chris@119 342 mutable ViewMagMap m_viewMags;
Chris@119 343 mutable std::vector<MagnitudeRange> m_columnMags;
Chris@119 344 void invalidateMagnitudes();
Chris@918 345 bool updateViewMagnitudes(LayerGeometryProvider *v) const;
Chris@1089 346
Chris@1089 347 typedef std::map<int, Colour3DPlotRenderer *> ViewRendererMap; // key is view id
Chris@1089 348 mutable ViewRendererMap m_renderers;
Chris@1089 349 Colour3DPlotRenderer *getRenderer(LayerGeometryProvider *) const;
Chris@1058 350
Chris@1088 351 FFTModel *getFFTModel() const;
Chris@1088 352 Dense3DModelPeakCache *getPeakCache() const;
Chris@1088 353 void invalidateFFTModel();
Chris@1089 354
Chris@1089 355 void paintAlternative(LayerGeometryProvider *v, QPainter &paint, QRect rect) const;
Chris@1088 356
Chris@1024 357 int paintDrawBuffer(LayerGeometryProvider *v, int w, int h,
Chris@1024 358 const std::vector<int> &binforx,
Chris@1024 359 const std::vector<double> &binfory,
Chris@1024 360 bool usePeaksCache,
Chris@1024 361 MagnitudeRange &overallMag,
Chris@1026 362 bool &overallMagChanged,
Chris@1039 363 bool rightToLeft,
Chris@1039 364 double softTimeLimit) const;
Chris@1024 365 int paintDrawBufferPeakFrequencies(LayerGeometryProvider *v, int w, int h,
Chris@1024 366 const std::vector<int> &binforx,
Chris@1024 367 int minbin,
Chris@1024 368 int maxbin,
Chris@1024 369 double displayMinFreq,
Chris@1024 370 double displayMaxFreq,
Chris@1024 371 bool logarithmic,
Chris@1024 372 MagnitudeRange &overallMag,
Chris@1026 373 bool &overallMagChanged,
Chris@1039 374 bool rightToLeft,
Chris@1039 375 double softTimeLimit) const;
Chris@273 376
Chris@1058 377 std::vector<float> getColumnFromFFTModel(FFTModel *model,
Chris@1058 378 int sx,
Chris@1058 379 int minbin,
Chris@1058 380 int bincount) const;
Chris@1058 381
Chris@1058 382 std::vector<float> getColumnFromGenericModel(DenseThreeDimensionalModel *model,
Chris@1058 383 int sx,
Chris@1058 384 int minbin,
Chris@1058 385 int bincount) const;
Chris@1058 386
Chris@1059 387 void recordColumnExtents(const std::vector<float> &col,
Chris@1059 388 int sx,
Chris@1059 389 MagnitudeRange &overallMag,
Chris@1059 390 bool &overallMagChanged) const;
Chris@1058 391
Chris@1058 392 virtual void updateMeasureRectYCoords(LayerGeometryProvider *v,
Chris@1058 393 const MeasureRect &r) const;
Chris@1058 394 virtual void setMeasureRectYCoord(LayerGeometryProvider *v,
Chris@1058 395 MeasureRect &r, bool start, int y) const;
Chris@0 396 };
Chris@0 397
Chris@0 398 #endif