annotate layer/SpectrogramLayer.h @ 1058:9a13bc339fa9 spectrogram-minor-refactor

Mid-refactor to pull out the bulk of paintDrawBuffer into chunks
author Chris Cannam
date Mon, 13 Jun 2016 16:17:44 +0100
parents 5e5873c24142
children e1c2dcc7790e
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@1030 30
Chris@0 31 #include <QMutex>
Chris@0 32 #include <QWaitCondition>
Chris@95 33 #include <QImage>
Chris@95 34 #include <QPixmap>
Chris@0 35
Chris@0 36 class View;
Chris@0 37 class QPainter;
Chris@0 38 class QImage;
Chris@0 39 class QPixmap;
Chris@0 40 class QTimer;
Chris@130 41 class FFTModel;
Chris@484 42 class Dense3DModelPeakCache;
Chris@114 43
Chris@0 44
Chris@0 45 /**
Chris@0 46 * SpectrogramLayer represents waveform data (obtained from a
Chris@0 47 * DenseTimeValueModel) in spectrogram form.
Chris@0 48 */
Chris@0 49
Chris@193 50 class SpectrogramLayer : public SliceableLayer,
Chris@31 51 public PowerOfSqrtTwoZoomConstraint
Chris@0 52 {
Chris@0 53 Q_OBJECT
Chris@0 54
Chris@0 55 public:
Chris@37 56 enum Configuration { FullRangeDb, MelodicRange, MelodicPeaks };
Chris@0 57
Chris@44 58 SpectrogramLayer(Configuration = FullRangeDb);
Chris@0 59 ~SpectrogramLayer();
Chris@0 60
Chris@0 61 virtual const ZoomConstraint *getZoomConstraint() const { return this; }
Chris@0 62 virtual const Model *getModel() const { return m_model; }
Chris@916 63 virtual void paint(LayerGeometryProvider *v, QPainter &paint, QRect rect) const;
Chris@389 64 virtual void setSynchronousPainting(bool synchronous);
Chris@0 65
Chris@918 66 virtual int getVerticalScaleWidth(LayerGeometryProvider *v, bool detailed, QPainter &) const;
Chris@918 67 virtual void paintVerticalScale(LayerGeometryProvider *v, bool detailed, QPainter &paint, QRect rect) const;
Chris@0 68
Chris@918 69 virtual bool getCrosshairExtents(LayerGeometryProvider *, QPainter &, QPoint cursorPos,
Chris@77 70 std::vector<QRect> &extents) const;
Chris@918 71 virtual void paintCrosshairs(LayerGeometryProvider *, QPainter &, QPoint) const;
Chris@77 72
Chris@918 73 virtual QString getFeatureDescription(LayerGeometryProvider *v, QPoint &) const;
Chris@0 74
Chris@918 75 virtual bool snapToFeatureFrame(LayerGeometryProvider *v, sv_frame_t &frame,
Chris@805 76 int &resolution,
Chris@28 77 SnapType snap) const;
Chris@13 78
Chris@918 79 virtual void measureDoubleClick(LayerGeometryProvider *, QMouseEvent *);
Chris@283 80
Chris@224 81 virtual bool hasLightBackground() const;
Chris@224 82
Chris@0 83 void setModel(const DenseTimeValueModel *model);
Chris@0 84
Chris@0 85 virtual PropertyList getProperties() const;
Chris@87 86 virtual QString getPropertyLabel(const PropertyName &) const;
Chris@335 87 virtual QString getPropertyIconName(const PropertyName &) const;
Chris@0 88 virtual PropertyType getPropertyType(const PropertyName &) const;
Chris@0 89 virtual QString getPropertyGroupName(const PropertyName &) const;
Chris@0 90 virtual int getPropertyRangeAndValue(const PropertyName &,
Chris@216 91 int *min, int *max, int *deflt) const;
Chris@0 92 virtual QString getPropertyValueLabel(const PropertyName &,
Chris@0 93 int value) const;
Chris@862 94 virtual QString getPropertyValueIconName(const PropertyName &,
Chris@862 95 int value) const;
Chris@167 96 virtual RangeMapper *getNewPropertyRangeMapper(const PropertyName &) const;
Chris@0 97 virtual void setProperty(const PropertyName &, int value);
Chris@0 98
Chris@0 99 /**
Chris@0 100 * Specify the channel to use from the source model.
Chris@0 101 * A value of -1 means to mix all available channels.
Chris@0 102 * The default is channel 0.
Chris@0 103 */
Chris@0 104 void setChannel(int);
Chris@0 105 int getChannel() const;
Chris@0 106
Chris@805 107 void setWindowSize(int);
Chris@805 108 int getWindowSize() const;
Chris@0 109
Chris@805 110 void setWindowHopLevel(int level);
Chris@805 111 int getWindowHopLevel() const;
Chris@0 112
Chris@0 113 void setWindowType(WindowType type);
Chris@0 114 WindowType getWindowType() const;
Chris@0 115
Chris@805 116 void setZeroPadLevel(int level);
Chris@805 117 int getZeroPadLevel() const;
Chris@109 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@37 141 enum ColourScale {
Chris@37 142 LinearColourScale,
Chris@37 143 MeterColourScale,
Chris@215 144 dBSquaredColourScale,
Chris@37 145 dBColourScale,
Chris@37 146 PhaseColourScale
Chris@37 147 };
Chris@0 148
Chris@0 149 /**
Chris@0 150 * Specify the scale for sample levels. See WaveformLayer for
Chris@0 151 * details of meter and dB scaling. The default is dBColourScale.
Chris@0 152 */
Chris@0 153 void setColourScale(ColourScale);
Chris@0 154 ColourScale getColourScale() const;
Chris@0 155
Chris@35 156 enum FrequencyScale {
Chris@35 157 LinearFrequencyScale,
Chris@35 158 LogFrequencyScale
Chris@35 159 };
Chris@0 160
Chris@0 161 /**
Chris@0 162 * Specify the scale for the y axis.
Chris@0 163 */
Chris@0 164 void setFrequencyScale(FrequencyScale);
Chris@0 165 FrequencyScale getFrequencyScale() const;
Chris@0 166
Chris@37 167 enum BinDisplay {
Chris@37 168 AllBins,
Chris@37 169 PeakBins,
Chris@37 170 PeakFrequencies
Chris@35 171 };
Chris@35 172
Chris@35 173 /**
Chris@35 174 * Specify the processing of frequency bins for the y axis.
Chris@35 175 */
Chris@37 176 void setBinDisplay(BinDisplay);
Chris@37 177 BinDisplay getBinDisplay() const;
Chris@862 178
Chris@862 179 enum Normalization {
Chris@862 180 NoNormalization,
Chris@862 181 NormalizeColumns,
Chris@862 182 NormalizeVisibleArea,
Chris@862 183 NormalizeHybrid
Chris@862 184 };
Chris@36 185
Chris@719 186 /**
Chris@862 187 * Specify the normalization mode for bin values.
Chris@719 188 */
Chris@862 189 void setNormalization(Normalization);
Chris@862 190 Normalization getNormalization() const;
Chris@120 191
Chris@719 192 /**
Chris@862 193 * Specify the colour map. See ColourMapper for the colour map
Chris@862 194 * values.
Chris@719 195 */
Chris@197 196 void setColourMap(int map);
Chris@197 197 int getColourMap() const;
Chris@0 198
Chris@9 199 /**
Chris@9 200 * Specify the colourmap rotation for the colour scale.
Chris@9 201 */
Chris@9 202 void setColourRotation(int);
Chris@9 203 int getColourRotation() const;
Chris@9 204
Chris@0 205 virtual VerticalPosition getPreferredFrameCountPosition() const {
Chris@0 206 return PositionTop;
Chris@0 207 }
Chris@0 208
Chris@15 209 virtual bool isLayerOpaque() const { return true; }
Chris@287 210
Chris@287 211 virtual ColourSignificance getLayerColourSignificance() const {
Chris@287 212 return ColourHasMeaningfulValue;
Chris@287 213 }
Chris@15 214
Chris@918 215 double getYForFrequency(const LayerGeometryProvider *v, double frequency) const;
Chris@918 216 double getFrequencyForY(const LayerGeometryProvider *v, int y) const;
Chris@42 217
Chris@918 218 virtual int getCompletion(LayerGeometryProvider *v) const;
Chris@918 219 virtual QString getError(LayerGeometryProvider *v) const;
Chris@0 220
Chris@905 221 virtual bool getValueExtents(double &min, double &max,
Chris@101 222 bool &logarithmic, QString &unit) const;
Chris@101 223
Chris@905 224 virtual bool getDisplayExtents(double &min, double &max) const;
Chris@79 225
Chris@905 226 virtual bool setDisplayExtents(double min, double max);
Chris@120 227
Chris@918 228 virtual bool getYScaleValue(const LayerGeometryProvider *, int, double &, QString &) const;
Chris@261 229
Chris@316 230 virtual void toXml(QTextStream &stream, QString indent = "",
Chris@316 231 QString extraAttributes = "") const;
Chris@6 232
Chris@11 233 void setProperties(const QXmlAttributes &attributes);
Chris@11 234
Chris@918 235 virtual void setLayerDormant(const LayerGeometryProvider *v, bool dormant);
Chris@29 236
Chris@918 237 virtual bool isLayerScrollable(const LayerGeometryProvider *) const { return false; }
Chris@94 238
Chris@133 239 virtual int getVerticalZoomSteps(int &defaultStep) const;
Chris@133 240 virtual int getCurrentVerticalZoomStep() const;
Chris@133 241 virtual void setVerticalZoomStep(int);
Chris@187 242 virtual RangeMapper *getNewVerticalZoomRangeMapper() const;
Chris@133 243
Chris@193 244 virtual const Model *getSliceableModel() const;
Chris@193 245
Chris@0 246 protected slots:
Chris@0 247 void cacheInvalid();
Chris@906 248 void cacheInvalid(sv_frame_t startFrame, sv_frame_t endFrame);
Chris@122 249
Chris@122 250 void preferenceChanged(PropertyContainer::PropertyName name);
Chris@0 251
Chris@0 252 protected:
Chris@0 253 const DenseTimeValueModel *m_model; // I do not own this
Chris@484 254
Chris@35 255 int m_channel;
Chris@1023 256 int m_windowSize;
Chris@35 257 WindowType m_windowType;
Chris@1023 258 int m_windowHopLevel;
Chris@1023 259 int m_zeroPadLevel;
Chris@1023 260 int m_fftSize;
Chris@35 261 float m_gain;
Chris@215 262 float m_initialGain;
Chris@37 263 float m_threshold;
Chris@215 264 float m_initialThreshold;
Chris@35 265 int m_colourRotation;
Chris@215 266 int m_initialRotation;
Chris@1023 267 int m_minFrequency;
Chris@1023 268 int m_maxFrequency;
Chris@1023 269 int m_initialMaxFrequency;
Chris@35 270 ColourScale m_colourScale;
Chris@197 271 int m_colourMap;
Chris@77 272 QColor m_crosshairColour;
Chris@35 273 FrequencyScale m_frequencyScale;
Chris@37 274 BinDisplay m_binDisplay;
Chris@862 275 Normalization m_normalization;
Chris@133 276 int m_lastEmittedZoomStep;
Chris@389 277 bool m_synchronous;
Chris@0 278
Chris@608 279 mutable bool m_haveDetailedScale;
Chris@215 280
Chris@38 281 enum { NO_VALUE = 0 }; // colour index for unused pixels
Chris@38 282
Chris@197 283 class Palette
Chris@86 284 {
Chris@86 285 public:
Chris@86 286 QColor getColour(unsigned char index) const {
Chris@86 287 return m_colours[index];
Chris@86 288 }
Chris@86 289
Chris@86 290 void setColour(unsigned char index, QColor colour) {
Chris@86 291 m_colours[index] = colour;
Chris@86 292 }
Chris@86 293
Chris@86 294 private:
Chris@86 295 QColor m_colours[256];
Chris@86 296 };
Chris@86 297
Chris@197 298 Palette m_palette;
Chris@31 299
Chris@1030 300 typedef std::map<int, ScrollableImageCache> ViewImageCache; // key is view id
Chris@478 301 void invalidateImageCaches();
Chris@478 302 mutable ViewImageCache m_imageCaches;
Chris@1030 303 ScrollableImageCache &getImageCacheReference(const LayerGeometryProvider *) const;
Chris@477 304
Chris@477 305 /**
Chris@477 306 * When painting, we draw directly onto the draw buffer and then
Chris@478 307 * copy this to the part of the image cache that needed refreshing
Chris@478 308 * before copying the image cache onto the window. (Remind me why
Chris@477 309 * we don't draw directly onto the cache?)
Chris@477 310 */
Chris@95 311 mutable QImage m_drawBuffer;
Chris@0 312
Chris@0 313 bool m_exiting;
Chris@0 314
Chris@197 315 void initialisePalette();
Chris@197 316 void rotatePalette(int distance);
Chris@0 317
Chris@918 318 unsigned char getDisplayValue(LayerGeometryProvider *v, double input) const;
Chris@40 319
Chris@40 320 int getColourScaleWidth(QPainter &) const;
Chris@40 321
Chris@918 322 void illuminateLocalFeatures(LayerGeometryProvider *v, QPainter &painter) const;
Chris@121 323
Chris@905 324 double getEffectiveMinFrequency() const;
Chris@905 325 double getEffectiveMaxFrequency() const;
Chris@38 326
Chris@486 327 // Note that the getYBin... methods return the nominal bin in the
Chris@486 328 // un-smoothed spectrogram. This is not necessarily the same bin
Chris@486 329 // as is pulled from the spectrogram and drawn at the given
Chris@486 330 // position, if the spectrogram has oversampling smoothing. Use
Chris@486 331 // getSmoothedYBinRange to obtain that.
Chris@486 332
Chris@918 333 bool getXBinRange(LayerGeometryProvider *v, int x, double &windowMin, double &windowMax) const;
Chris@918 334 bool getYBinRange(LayerGeometryProvider *v, int y, double &freqBinMin, double &freqBinMax) const;
Chris@918 335 bool getSmoothedYBinRange(LayerGeometryProvider *v, int y, double &freqBinMin, double &freqBinMax) const;
Chris@0 336
Chris@918 337 bool getYBinSourceRange(LayerGeometryProvider *v, int y, double &freqMin, double &freqMax) const;
Chris@918 338 bool getAdjustedYBinSourceRange(LayerGeometryProvider *v, int x, int y,
Chris@905 339 double &freqMin, double &freqMax,
Chris@905 340 double &adjFreqMin, double &adjFreqMax) const;
Chris@918 341 bool getXBinSourceRange(LayerGeometryProvider *v, int x, RealTime &timeMin, RealTime &timeMax) const;
Chris@918 342 bool getXYBinSourceRange(LayerGeometryProvider *v, int x, int y, double &min, double &max,
Chris@905 343 double &phaseMin, double &phaseMax) const;
Chris@0 344
Chris@805 345 int getWindowIncrement() const {
Chris@97 346 if (m_windowHopLevel == 0) return m_windowSize;
Chris@97 347 else if (m_windowHopLevel == 1) return (m_windowSize * 3) / 4;
Chris@97 348 else return m_windowSize / (1 << (m_windowHopLevel - 1));
Chris@0 349 }
Chris@113 350
Chris@918 351 int getZeroPadLevel(const LayerGeometryProvider *v) const;
Chris@918 352 int getFFTSize(const LayerGeometryProvider *v) const;
Chris@918 353 FFTModel *getFFTModel(const LayerGeometryProvider *v) const;
Chris@918 354 Dense3DModelPeakCache *getPeakCache(const LayerGeometryProvider *v) const;
Chris@130 355 void invalidateFFTModels();
Chris@115 356
Chris@1030 357 typedef std::map<int, FFTModel *> ViewFFTMap; // key is view id
Chris@1030 358 typedef std::map<int, Dense3DModelPeakCache *> PeakCacheMap; // key is view id
Chris@130 359 mutable ViewFFTMap m_fftModels;
Chris@484 360 mutable PeakCacheMap m_peakCaches;
Chris@1054 361 const int m_peakCacheDivisor;
Chris@193 362 mutable Model *m_sliceableModel;
Chris@119 363
Chris@1030 364 typedef std::map<int, MagnitudeRange> ViewMagMap; // key is view id
Chris@119 365 mutable ViewMagMap m_viewMags;
Chris@119 366 mutable std::vector<MagnitudeRange> m_columnMags;
Chris@119 367 void invalidateMagnitudes();
Chris@918 368 bool updateViewMagnitudes(LayerGeometryProvider *v) const;
Chris@1058 369
Chris@1024 370 int paintDrawBuffer(LayerGeometryProvider *v, int w, int h,
Chris@1024 371 const std::vector<int> &binforx,
Chris@1024 372 const std::vector<double> &binfory,
Chris@1024 373 bool usePeaksCache,
Chris@1024 374 MagnitudeRange &overallMag,
Chris@1026 375 bool &overallMagChanged,
Chris@1039 376 bool rightToLeft,
Chris@1039 377 double softTimeLimit) const;
Chris@1024 378 int paintDrawBufferPeakFrequencies(LayerGeometryProvider *v, int w, int h,
Chris@1024 379 const std::vector<int> &binforx,
Chris@1024 380 int minbin,
Chris@1024 381 int maxbin,
Chris@1024 382 double displayMinFreq,
Chris@1024 383 double displayMaxFreq,
Chris@1024 384 bool logarithmic,
Chris@1024 385 MagnitudeRange &overallMag,
Chris@1026 386 bool &overallMagChanged,
Chris@1039 387 bool rightToLeft,
Chris@1039 388 double softTimeLimit) const;
Chris@273 389
Chris@1058 390 void normalise(std::vector<float> &, Normalization norm) const;
Chris@1058 391
Chris@1058 392 std::vector<float> getColumnFromFFTModel(FFTModel *model,
Chris@1058 393 int sx,
Chris@1058 394 int minbin,
Chris@1058 395 int bincount) const;
Chris@1058 396
Chris@1058 397 std::vector<float> getColumnFromGenericModel(DenseThreeDimensionalModel *model,
Chris@1058 398 int sx,
Chris@1058 399 int minbin,
Chris@1058 400 int bincount) const;
Chris@1058 401
Chris@1058 402 void scaleColumn(std::vector<float> &col) const;
Chris@1058 403
Chris@1058 404 virtual void updateMeasureRectYCoords(LayerGeometryProvider *v,
Chris@1058 405 const MeasureRect &r) const;
Chris@1058 406 virtual void setMeasureRectYCoord(LayerGeometryProvider *v,
Chris@1058 407 MeasureRect &r, bool start, int y) const;
Chris@0 408 };
Chris@0 409
Chris@0 410 #endif