annotate layer/SpectrogramLayer.h @ 1085:179ea8a2f650 spectrogram-minor-refactor

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