annotate layer/SpectrogramLayer.h @ 1040:25b035362c44 spectrogram-minor-refactor

Truncate edges when about to paint beyond limits of cache
author Chris Cannam
date Wed, 03 Feb 2016 16:51:37 +0000
parents bfce7940c017
children 5e5873c24142
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@71 21 #include "base/RealTime.h"
Chris@92 22 #include "base/Thread.h"
Chris@122 23 #include "base/PropertyContainer.h"
Chris@128 24 #include "data/model/PowerOfSqrtTwoZoomConstraint.h"
Chris@128 25 #include "data/model/DenseTimeValueModel.h"
Chris@130 26 #include "data/model/FFTModel.h"
Chris@0 27
Chris@1030 28 #include "ScrollableImageCache.h"
Chris@1030 29
Chris@0 30 #include <QMutex>
Chris@0 31 #include <QWaitCondition>
Chris@95 32 #include <QImage>
Chris@95 33 #include <QPixmap>
Chris@0 34
Chris@0 35 class View;
Chris@0 36 class QPainter;
Chris@0 37 class QImage;
Chris@0 38 class QPixmap;
Chris@0 39 class QTimer;
Chris@130 40 class FFTModel;
Chris@484 41 class Dense3DModelPeakCache;
Chris@114 42
Chris@0 43
Chris@0 44 /**
Chris@0 45 * SpectrogramLayer represents waveform data (obtained from a
Chris@0 46 * DenseTimeValueModel) in spectrogram form.
Chris@0 47 */
Chris@0 48
Chris@193 49 class SpectrogramLayer : public SliceableLayer,
Chris@31 50 public PowerOfSqrtTwoZoomConstraint
Chris@0 51 {
Chris@0 52 Q_OBJECT
Chris@0 53
Chris@0 54 public:
Chris@37 55 enum Configuration { FullRangeDb, MelodicRange, MelodicPeaks };
Chris@0 56
Chris@44 57 SpectrogramLayer(Configuration = FullRangeDb);
Chris@0 58 ~SpectrogramLayer();
Chris@0 59
Chris@0 60 virtual const ZoomConstraint *getZoomConstraint() const { return this; }
Chris@0 61 virtual const Model *getModel() const { return m_model; }
Chris@916 62 virtual void paint(LayerGeometryProvider *v, QPainter &paint, QRect rect) const;
Chris@389 63 virtual void setSynchronousPainting(bool synchronous);
Chris@0 64
Chris@918 65 virtual int getVerticalScaleWidth(LayerGeometryProvider *v, bool detailed, QPainter &) const;
Chris@918 66 virtual void paintVerticalScale(LayerGeometryProvider *v, bool detailed, QPainter &paint, QRect rect) const;
Chris@0 67
Chris@918 68 virtual bool getCrosshairExtents(LayerGeometryProvider *, QPainter &, QPoint cursorPos,
Chris@77 69 std::vector<QRect> &extents) const;
Chris@918 70 virtual void paintCrosshairs(LayerGeometryProvider *, QPainter &, QPoint) const;
Chris@77 71
Chris@918 72 virtual QString getFeatureDescription(LayerGeometryProvider *v, QPoint &) const;
Chris@0 73
Chris@918 74 virtual bool snapToFeatureFrame(LayerGeometryProvider *v, sv_frame_t &frame,
Chris@805 75 int &resolution,
Chris@28 76 SnapType snap) const;
Chris@13 77
Chris@918 78 virtual void measureDoubleClick(LayerGeometryProvider *, QMouseEvent *);
Chris@283 79
Chris@224 80 virtual bool hasLightBackground() const;
Chris@224 81
Chris@0 82 void setModel(const DenseTimeValueModel *model);
Chris@0 83
Chris@0 84 virtual PropertyList getProperties() const;
Chris@87 85 virtual QString getPropertyLabel(const PropertyName &) const;
Chris@335 86 virtual QString getPropertyIconName(const PropertyName &) const;
Chris@0 87 virtual PropertyType getPropertyType(const PropertyName &) const;
Chris@0 88 virtual QString getPropertyGroupName(const PropertyName &) const;
Chris@0 89 virtual int getPropertyRangeAndValue(const PropertyName &,
Chris@216 90 int *min, int *max, int *deflt) const;
Chris@0 91 virtual QString getPropertyValueLabel(const PropertyName &,
Chris@0 92 int value) const;
Chris@862 93 virtual QString getPropertyValueIconName(const PropertyName &,
Chris@862 94 int value) const;
Chris@167 95 virtual RangeMapper *getNewPropertyRangeMapper(const PropertyName &) const;
Chris@0 96 virtual void setProperty(const PropertyName &, int value);
Chris@0 97
Chris@0 98 /**
Chris@0 99 * Specify the channel to use from the source model.
Chris@0 100 * A value of -1 means to mix all available channels.
Chris@0 101 * The default is channel 0.
Chris@0 102 */
Chris@0 103 void setChannel(int);
Chris@0 104 int getChannel() const;
Chris@0 105
Chris@805 106 void setWindowSize(int);
Chris@805 107 int getWindowSize() const;
Chris@0 108
Chris@805 109 void setWindowHopLevel(int level);
Chris@805 110 int getWindowHopLevel() const;
Chris@0 111
Chris@0 112 void setWindowType(WindowType type);
Chris@0 113 WindowType getWindowType() const;
Chris@0 114
Chris@805 115 void setZeroPadLevel(int level);
Chris@805 116 int getZeroPadLevel() const;
Chris@109 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@862 178 enum Normalization {
Chris@862 179 NoNormalization,
Chris@862 180 NormalizeColumns,
Chris@862 181 NormalizeVisibleArea,
Chris@862 182 NormalizeHybrid
Chris@862 183 };
Chris@36 184
Chris@719 185 /**
Chris@862 186 * Specify the normalization mode for bin values.
Chris@719 187 */
Chris@862 188 void setNormalization(Normalization);
Chris@862 189 Normalization getNormalization() const;
Chris@120 190
Chris@719 191 /**
Chris@862 192 * Specify the colour map. See ColourMapper for the colour map
Chris@862 193 * values.
Chris@719 194 */
Chris@197 195 void setColourMap(int map);
Chris@197 196 int getColourMap() const;
Chris@0 197
Chris@9 198 /**
Chris@9 199 * Specify the colourmap rotation for the colour scale.
Chris@9 200 */
Chris@9 201 void setColourRotation(int);
Chris@9 202 int getColourRotation() const;
Chris@9 203
Chris@0 204 virtual VerticalPosition getPreferredFrameCountPosition() const {
Chris@0 205 return PositionTop;
Chris@0 206 }
Chris@0 207
Chris@15 208 virtual bool isLayerOpaque() const { return true; }
Chris@287 209
Chris@287 210 virtual ColourSignificance getLayerColourSignificance() const {
Chris@287 211 return ColourHasMeaningfulValue;
Chris@287 212 }
Chris@15 213
Chris@918 214 double getYForFrequency(const LayerGeometryProvider *v, double frequency) const;
Chris@918 215 double getFrequencyForY(const LayerGeometryProvider *v, int y) const;
Chris@42 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@862 274 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@477 308 * we don't draw directly onto the cache?)
Chris@477 309 */
Chris@95 310 mutable QImage m_drawBuffer;
Chris@0 311
Chris@0 312 bool m_exiting;
Chris@0 313
Chris@197 314 void initialisePalette();
Chris@197 315 void rotatePalette(int distance);
Chris@0 316
Chris@918 317 unsigned char getDisplayValue(LayerGeometryProvider *v, double input) const;
Chris@40 318
Chris@40 319 int getColourScaleWidth(QPainter &) const;
Chris@40 320
Chris@918 321 void illuminateLocalFeatures(LayerGeometryProvider *v, QPainter &painter) const;
Chris@121 322
Chris@905 323 double getEffectiveMinFrequency() const;
Chris@905 324 double getEffectiveMaxFrequency() const;
Chris@38 325
Chris@486 326 // Note that the getYBin... methods return the nominal bin in the
Chris@486 327 // un-smoothed spectrogram. This is not necessarily the same bin
Chris@486 328 // as is pulled from the spectrogram and drawn at the given
Chris@486 329 // position, if the spectrogram has oversampling smoothing. Use
Chris@486 330 // getSmoothedYBinRange to obtain that.
Chris@486 331
Chris@918 332 bool getXBinRange(LayerGeometryProvider *v, int x, double &windowMin, double &windowMax) const;
Chris@918 333 bool getYBinRange(LayerGeometryProvider *v, int y, double &freqBinMin, double &freqBinMax) const;
Chris@918 334 bool getSmoothedYBinRange(LayerGeometryProvider *v, int y, double &freqBinMin, double &freqBinMax) const;
Chris@0 335
Chris@918 336 bool getYBinSourceRange(LayerGeometryProvider *v, int y, double &freqMin, double &freqMax) const;
Chris@918 337 bool getAdjustedYBinSourceRange(LayerGeometryProvider *v, int x, int y,
Chris@905 338 double &freqMin, double &freqMax,
Chris@905 339 double &adjFreqMin, double &adjFreqMax) const;
Chris@918 340 bool getXBinSourceRange(LayerGeometryProvider *v, int x, RealTime &timeMin, RealTime &timeMax) const;
Chris@918 341 bool getXYBinSourceRange(LayerGeometryProvider *v, int x, int y, double &min, double &max,
Chris@905 342 double &phaseMin, double &phaseMax) const;
Chris@0 343
Chris@805 344 int getWindowIncrement() const {
Chris@97 345 if (m_windowHopLevel == 0) return m_windowSize;
Chris@97 346 else if (m_windowHopLevel == 1) return (m_windowSize * 3) / 4;
Chris@97 347 else return m_windowSize / (1 << (m_windowHopLevel - 1));
Chris@0 348 }
Chris@113 349
Chris@918 350 int getZeroPadLevel(const LayerGeometryProvider *v) const;
Chris@918 351 int getFFTSize(const LayerGeometryProvider *v) const;
Chris@918 352 FFTModel *getFFTModel(const LayerGeometryProvider *v) const;
Chris@918 353 Dense3DModelPeakCache *getPeakCache(const LayerGeometryProvider *v) const;
Chris@130 354 void invalidateFFTModels();
Chris@115 355
Chris@1030 356 typedef std::map<int, FFTModel *> ViewFFTMap; // key is view id
Chris@1030 357 typedef std::map<int, Dense3DModelPeakCache *> PeakCacheMap; // key is view id
Chris@130 358 mutable ViewFFTMap m_fftModels;
Chris@484 359 mutable PeakCacheMap m_peakCaches;
Chris@193 360 mutable Model *m_sliceableModel;
Chris@119 361
Chris@119 362 class MagnitudeRange {
Chris@119 363 public:
Chris@119 364 MagnitudeRange() : m_min(0), m_max(0) { }
Chris@119 365 bool operator==(const MagnitudeRange &r) {
Chris@119 366 return r.m_min == m_min && r.m_max == m_max;
Chris@119 367 }
Chris@905 368 bool isSet() const { return (m_min != 0.f || m_max != 0.f); }
Chris@119 369 void set(float min, float max) {
Chris@905 370 m_min = min;
Chris@905 371 m_max = max;
Chris@119 372 if (m_max < m_min) m_max = m_min;
Chris@119 373 }
Chris@119 374 bool sample(float f) {
Chris@119 375 bool changed = false;
Chris@119 376 if (isSet()) {
Chris@905 377 if (f < m_min) { m_min = f; changed = true; }
Chris@905 378 if (f > m_max) { m_max = f; changed = true; }
Chris@119 379 } else {
Chris@905 380 m_max = m_min = f;
Chris@119 381 changed = true;
Chris@119 382 }
Chris@119 383 return changed;
Chris@119 384 }
Chris@119 385 bool sample(const MagnitudeRange &r) {
Chris@119 386 bool changed = false;
Chris@119 387 if (isSet()) {
Chris@119 388 if (r.m_min < m_min) { m_min = r.m_min; changed = true; }
Chris@119 389 if (r.m_max > m_max) { m_max = r.m_max; changed = true; }
Chris@119 390 } else {
Chris@119 391 m_min = r.m_min;
Chris@119 392 m_max = r.m_max;
Chris@119 393 changed = true;
Chris@119 394 }
Chris@119 395 return changed;
Chris@119 396 }
Chris@905 397 float getMin() const { return m_min; }
Chris@905 398 float getMax() const { return m_max; }
Chris@119 399 private:
Chris@905 400 float m_min;
Chris@905 401 float m_max;
Chris@119 402 };
Chris@119 403
Chris@1030 404 typedef std::map<int, MagnitudeRange> ViewMagMap; // key is view id
Chris@119 405 mutable ViewMagMap m_viewMags;
Chris@119 406 mutable std::vector<MagnitudeRange> m_columnMags;
Chris@119 407 void invalidateMagnitudes();
Chris@918 408 bool updateViewMagnitudes(LayerGeometryProvider *v) const;
Chris@1024 409 int paintDrawBuffer(LayerGeometryProvider *v, int w, int h,
Chris@1024 410 const std::vector<int> &binforx,
Chris@1024 411 const std::vector<double> &binfory,
Chris@1024 412 bool usePeaksCache,
Chris@1024 413 MagnitudeRange &overallMag,
Chris@1026 414 bool &overallMagChanged,
Chris@1039 415 bool rightToLeft,
Chris@1039 416 double softTimeLimit) const;
Chris@1024 417 int paintDrawBufferPeakFrequencies(LayerGeometryProvider *v, int w, int h,
Chris@1024 418 const std::vector<int> &binforx,
Chris@1024 419 int minbin,
Chris@1024 420 int maxbin,
Chris@1024 421 double displayMinFreq,
Chris@1024 422 double displayMaxFreq,
Chris@1024 423 bool logarithmic,
Chris@1024 424 MagnitudeRange &overallMag,
Chris@1026 425 bool &overallMagChanged,
Chris@1039 426 bool rightToLeft,
Chris@1039 427 double softTimeLimit) const;
Chris@273 428
Chris@918 429 virtual void updateMeasureRectYCoords(LayerGeometryProvider *v, const MeasureRect &r) const;
Chris@918 430 virtual void setMeasureRectYCoord(LayerGeometryProvider *v, MeasureRect &r, bool start, int y) const;
Chris@0 431 };
Chris@0 432
Chris@0 433 #endif