annotate layer/SpectrogramLayer.h @ 1088:c520f90bbf2e spectrogram-minor-refactor

One FFT model per spectrogram (again!) - but we do still need a magnitude range per view, as views could be showing different regions
author Chris Cannam
date Tue, 05 Jul 2016 08:58:28 +0100
parents 6d990a24ac1b
children c8683d94442a
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@0 118 /**
Chris@110 119 * Set the gain multiplier for sample values in this view.
Chris@0 120 * The default is 1.0.
Chris@0 121 */
Chris@0 122 void setGain(float gain);
Chris@0 123 float getGain() const;
Chris@0 124
Chris@37 125 /**
Chris@110 126 * Set the threshold for sample values to qualify for being shown
Chris@110 127 * in the FFT, in voltage units.
Chris@37 128 *
Chris@37 129 * The default is 0.0.
Chris@37 130 */
Chris@37 131 void setThreshold(float threshold);
Chris@37 132 float getThreshold() const;
Chris@37 133
Chris@805 134 void setMinFrequency(int);
Chris@805 135 int getMinFrequency() const;
Chris@37 136
Chris@805 137 void setMaxFrequency(int); // 0 -> no maximum
Chris@805 138 int getMaxFrequency() const;
Chris@0 139
Chris@37 140 enum ColourScale {
Chris@37 141 LinearColourScale,
Chris@37 142 MeterColourScale,
Chris@215 143 dBSquaredColourScale,
Chris@37 144 dBColourScale,
Chris@37 145 PhaseColourScale
Chris@37 146 };
Chris@0 147
Chris@0 148 /**
Chris@0 149 * Specify the scale for sample levels. See WaveformLayer for
Chris@0 150 * details of meter and dB scaling. The default is dBColourScale.
Chris@0 151 */
Chris@0 152 void setColourScale(ColourScale);
Chris@0 153 ColourScale getColourScale() const;
Chris@0 154
Chris@35 155 enum FrequencyScale {
Chris@35 156 LinearFrequencyScale,
Chris@35 157 LogFrequencyScale
Chris@35 158 };
Chris@0 159
Chris@0 160 /**
Chris@0 161 * Specify the scale for the y axis.
Chris@0 162 */
Chris@0 163 void setFrequencyScale(FrequencyScale);
Chris@0 164 FrequencyScale getFrequencyScale() const;
Chris@0 165
Chris@37 166 enum BinDisplay {
Chris@37 167 AllBins,
Chris@37 168 PeakBins,
Chris@37 169 PeakFrequencies
Chris@35 170 };
Chris@35 171
Chris@35 172 /**
Chris@35 173 * Specify the processing of frequency bins for the y axis.
Chris@35 174 */
Chris@37 175 void setBinDisplay(BinDisplay);
Chris@37 176 BinDisplay getBinDisplay() const;
Chris@862 177
Chris@719 178 /**
Chris@862 179 * Specify the normalization mode for bin values.
Chris@719 180 */
Chris@1063 181 void setNormalization(ColumnOp::Normalization);
Chris@1063 182 ColumnOp::Normalization getNormalization() const;
Chris@120 183
Chris@719 184 /**
Chris@862 185 * Specify the colour map. See ColourMapper for the colour map
Chris@862 186 * values.
Chris@719 187 */
Chris@197 188 void setColourMap(int map);
Chris@197 189 int getColourMap() const;
Chris@0 190
Chris@9 191 /**
Chris@9 192 * Specify the colourmap rotation for the colour scale.
Chris@9 193 */
Chris@9 194 void setColourRotation(int);
Chris@9 195 int getColourRotation() const;
Chris@9 196
Chris@0 197 virtual VerticalPosition getPreferredFrameCountPosition() const {
Chris@0 198 return PositionTop;
Chris@0 199 }
Chris@0 200
Chris@15 201 virtual bool isLayerOpaque() const { return true; }
Chris@287 202
Chris@287 203 virtual ColourSignificance getLayerColourSignificance() const {
Chris@287 204 return ColourHasMeaningfulValue;
Chris@287 205 }
Chris@15 206
Chris@918 207 double getYForFrequency(const LayerGeometryProvider *v, double frequency) const;
Chris@918 208 double getFrequencyForY(const LayerGeometryProvider *v, int y) const;
Chris@42 209
Chris@1085 210 //!!! VerticalBinLayer methods. Note overlap with get*BinRange()
Chris@1085 211 double getYForBin(LayerGeometryProvider *, double bin) const;
Chris@1085 212 double getBinForY(LayerGeometryProvider *, double y) const;
Chris@1085 213
Chris@918 214 virtual int getCompletion(LayerGeometryProvider *v) const;
Chris@918 215 virtual QString getError(LayerGeometryProvider *v) const;
Chris@0 216
Chris@905 217 virtual bool getValueExtents(double &min, double &max,
Chris@101 218 bool &logarithmic, QString &unit) const;
Chris@101 219
Chris@905 220 virtual bool getDisplayExtents(double &min, double &max) const;
Chris@79 221
Chris@905 222 virtual bool setDisplayExtents(double min, double max);
Chris@120 223
Chris@918 224 virtual bool getYScaleValue(const LayerGeometryProvider *, int, double &, QString &) const;
Chris@261 225
Chris@316 226 virtual void toXml(QTextStream &stream, QString indent = "",
Chris@316 227 QString extraAttributes = "") const;
Chris@6 228
Chris@11 229 void setProperties(const QXmlAttributes &attributes);
Chris@11 230
Chris@918 231 virtual void setLayerDormant(const LayerGeometryProvider *v, bool dormant);
Chris@29 232
Chris@918 233 virtual bool isLayerScrollable(const LayerGeometryProvider *) const { return false; }
Chris@94 234
Chris@133 235 virtual int getVerticalZoomSteps(int &defaultStep) const;
Chris@133 236 virtual int getCurrentVerticalZoomStep() const;
Chris@133 237 virtual void setVerticalZoomStep(int);
Chris@187 238 virtual RangeMapper *getNewVerticalZoomRangeMapper() const;
Chris@133 239
Chris@193 240 virtual const Model *getSliceableModel() const;
Chris@193 241
Chris@0 242 protected slots:
Chris@0 243 void cacheInvalid();
Chris@906 244 void cacheInvalid(sv_frame_t startFrame, sv_frame_t endFrame);
Chris@122 245
Chris@122 246 void preferenceChanged(PropertyContainer::PropertyName name);
Chris@0 247
Chris@0 248 protected:
Chris@0 249 const DenseTimeValueModel *m_model; // I do not own this
Chris@484 250
Chris@35 251 int m_channel;
Chris@1023 252 int m_windowSize;
Chris@35 253 WindowType m_windowType;
Chris@1023 254 int m_windowHopLevel;
Chris@35 255 float m_gain;
Chris@215 256 float m_initialGain;
Chris@37 257 float m_threshold;
Chris@215 258 float m_initialThreshold;
Chris@35 259 int m_colourRotation;
Chris@215 260 int m_initialRotation;
Chris@1023 261 int m_minFrequency;
Chris@1023 262 int m_maxFrequency;
Chris@1023 263 int m_initialMaxFrequency;
Chris@35 264 ColourScale m_colourScale;
Chris@197 265 int m_colourMap;
Chris@77 266 QColor m_crosshairColour;
Chris@35 267 FrequencyScale m_frequencyScale;
Chris@37 268 BinDisplay m_binDisplay;
Chris@1063 269 ColumnOp::Normalization m_normalization;
Chris@133 270 int m_lastEmittedZoomStep;
Chris@389 271 bool m_synchronous;
Chris@0 272
Chris@608 273 mutable bool m_haveDetailedScale;
Chris@215 274
Chris@38 275 enum { NO_VALUE = 0 }; // colour index for unused pixels
Chris@38 276
Chris@197 277 class Palette
Chris@86 278 {
Chris@86 279 public:
Chris@86 280 QColor getColour(unsigned char index) const {
Chris@86 281 return m_colours[index];
Chris@86 282 }
Chris@86 283
Chris@86 284 void setColour(unsigned char index, QColor colour) {
Chris@86 285 m_colours[index] = colour;
Chris@86 286 }
Chris@86 287
Chris@86 288 private:
Chris@86 289 QColor m_colours[256];
Chris@86 290 };
Chris@86 291
Chris@197 292 Palette m_palette;
Chris@31 293
Chris@1030 294 typedef std::map<int, ScrollableImageCache> ViewImageCache; // key is view id
Chris@478 295 void invalidateImageCaches();
Chris@478 296 mutable ViewImageCache m_imageCaches;
Chris@1030 297 ScrollableImageCache &getImageCacheReference(const LayerGeometryProvider *) const;
Chris@477 298
Chris@477 299 /**
Chris@477 300 * When painting, we draw directly onto the draw buffer and then
Chris@478 301 * copy this to the part of the image cache that needed refreshing
Chris@478 302 * before copying the image cache onto the window. (Remind me why
Chris@1073 303 * we don't draw directly onto the cache?) (Answer: it's because
Chris@1073 304 * we usually apply scaling and smoothing in the x axis when
Chris@1073 305 * copying from draw buffer to cache)
Chris@477 306 */
Chris@95 307 mutable QImage m_drawBuffer;
Chris@0 308
Chris@0 309 bool m_exiting;
Chris@0 310
Chris@197 311 void initialisePalette();
Chris@197 312 void rotatePalette(int distance);
Chris@0 313
Chris@918 314 unsigned char getDisplayValue(LayerGeometryProvider *v, double input) const;
Chris@40 315
Chris@40 316 int getColourScaleWidth(QPainter &) const;
Chris@40 317
Chris@918 318 void illuminateLocalFeatures(LayerGeometryProvider *v, QPainter &painter) const;
Chris@121 319
Chris@905 320 double getEffectiveMinFrequency() const;
Chris@905 321 double getEffectiveMaxFrequency() const;
Chris@38 322
Chris@918 323 bool getXBinRange(LayerGeometryProvider *v, int x, double &windowMin, double &windowMax) const;
Chris@918 324 bool getYBinRange(LayerGeometryProvider *v, int y, double &freqBinMin, double &freqBinMax) const;
Chris@0 325
Chris@918 326 bool getYBinSourceRange(LayerGeometryProvider *v, int y, double &freqMin, double &freqMax) const;
Chris@918 327 bool getAdjustedYBinSourceRange(LayerGeometryProvider *v, int x, int y,
Chris@905 328 double &freqMin, double &freqMax,
Chris@905 329 double &adjFreqMin, double &adjFreqMax) const;
Chris@918 330 bool getXBinSourceRange(LayerGeometryProvider *v, int x, RealTime &timeMin, RealTime &timeMax) const;
Chris@918 331 bool getXYBinSourceRange(LayerGeometryProvider *v, int x, int y, double &min, double &max,
Chris@905 332 double &phaseMin, double &phaseMax) const;
Chris@0 333
Chris@805 334 int getWindowIncrement() const {
Chris@97 335 if (m_windowHopLevel == 0) return m_windowSize;
Chris@97 336 else if (m_windowHopLevel == 1) return (m_windowSize * 3) / 4;
Chris@97 337 else return m_windowSize / (1 << (m_windowHopLevel - 1));
Chris@0 338 }
Chris@113 339
Chris@1086 340 int getFFTOversampling() const;
Chris@1087 341 int getFFTSize() const; // m_windowSize * getFFTOversampling()
Chris@115 342
Chris@1088 343 mutable FFTModel *m_fftModel; //!!! should not be mutable, see getFFTModel()
Chris@1088 344 mutable Dense3DModelPeakCache *m_peakCache;
Chris@1054 345 const int m_peakCacheDivisor;
Chris@119 346
Chris@1030 347 typedef std::map<int, MagnitudeRange> ViewMagMap; // key is view id
Chris@119 348 mutable ViewMagMap m_viewMags;
Chris@119 349 mutable std::vector<MagnitudeRange> m_columnMags;
Chris@119 350 void invalidateMagnitudes();
Chris@918 351 bool updateViewMagnitudes(LayerGeometryProvider *v) const;
Chris@1058 352
Chris@1088 353 FFTModel *getFFTModel() const;
Chris@1088 354 Dense3DModelPeakCache *getPeakCache() const;
Chris@1088 355 void invalidateFFTModel();
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