annotate layer/SpectrogramLayer.h @ 862:1986c9b0d9c3 normalize_hybrid_option

Experimentally add the hybrid normalisation as an option (not working well either in UI or implementation)
author Chris Cannam
date Fri, 12 Sep 2014 11:38:55 +0100
parents 4c8ca536b54f
children 98827470ada2
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@30 16 #ifndef _SPECTROGRAM_LAYER_H_
Chris@30 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@0 28 #include <QMutex>
Chris@0 29 #include <QWaitCondition>
Chris@95 30 #include <QImage>
Chris@95 31 #include <QPixmap>
Chris@0 32
Chris@0 33 class View;
Chris@0 34 class QPainter;
Chris@0 35 class QImage;
Chris@0 36 class QPixmap;
Chris@0 37 class QTimer;
Chris@130 38 class FFTModel;
Chris@484 39 class Dense3DModelPeakCache;
Chris@114 40
Chris@0 41
Chris@0 42 /**
Chris@0 43 * SpectrogramLayer represents waveform data (obtained from a
Chris@0 44 * DenseTimeValueModel) in spectrogram form.
Chris@0 45 */
Chris@0 46
Chris@193 47 class SpectrogramLayer : public SliceableLayer,
Chris@31 48 public PowerOfSqrtTwoZoomConstraint
Chris@0 49 {
Chris@0 50 Q_OBJECT
Chris@0 51
Chris@0 52 public:
Chris@37 53 enum Configuration { FullRangeDb, MelodicRange, MelodicPeaks };
Chris@0 54
Chris@44 55 SpectrogramLayer(Configuration = FullRangeDb);
Chris@0 56 ~SpectrogramLayer();
Chris@0 57
Chris@0 58 virtual const ZoomConstraint *getZoomConstraint() const { return this; }
Chris@0 59 virtual const Model *getModel() const { return m_model; }
Chris@44 60 virtual void paint(View *v, QPainter &paint, QRect rect) const;
Chris@389 61 virtual void setSynchronousPainting(bool synchronous);
Chris@0 62
Chris@607 63 virtual int getVerticalScaleWidth(View *v, bool detailed, QPainter &) const;
Chris@607 64 virtual void paintVerticalScale(View *v, bool detailed, QPainter &paint, QRect rect) const;
Chris@0 65
Chris@77 66 virtual bool getCrosshairExtents(View *, QPainter &, QPoint cursorPos,
Chris@77 67 std::vector<QRect> &extents) const;
Chris@77 68 virtual void paintCrosshairs(View *, QPainter &, QPoint) const;
Chris@77 69
Chris@44 70 virtual QString getFeatureDescription(View *v, QPoint &) const;
Chris@0 71
Chris@44 72 virtual bool snapToFeatureFrame(View *v, int &frame,
Chris@805 73 int &resolution,
Chris@28 74 SnapType snap) const;
Chris@13 75
Chris@283 76 virtual void measureDoubleClick(View *, QMouseEvent *);
Chris@283 77
Chris@224 78 virtual bool hasLightBackground() const;
Chris@224 79
Chris@0 80 void setModel(const DenseTimeValueModel *model);
Chris@0 81
Chris@0 82 virtual PropertyList getProperties() const;
Chris@87 83 virtual QString getPropertyLabel(const PropertyName &) const;
Chris@335 84 virtual QString getPropertyIconName(const PropertyName &) const;
Chris@0 85 virtual PropertyType getPropertyType(const PropertyName &) const;
Chris@0 86 virtual QString getPropertyGroupName(const PropertyName &) const;
Chris@0 87 virtual int getPropertyRangeAndValue(const PropertyName &,
Chris@216 88 int *min, int *max, int *deflt) const;
Chris@0 89 virtual QString getPropertyValueLabel(const PropertyName &,
Chris@0 90 int value) const;
Chris@862 91 virtual QString getPropertyValueIconName(const PropertyName &,
Chris@862 92 int value) const;
Chris@167 93 virtual RangeMapper *getNewPropertyRangeMapper(const PropertyName &) const;
Chris@0 94 virtual void setProperty(const PropertyName &, int value);
Chris@0 95
Chris@0 96 /**
Chris@0 97 * Specify the channel to use from the source model.
Chris@0 98 * A value of -1 means to mix all available channels.
Chris@0 99 * The default is channel 0.
Chris@0 100 */
Chris@0 101 void setChannel(int);
Chris@0 102 int getChannel() const;
Chris@0 103
Chris@805 104 void setWindowSize(int);
Chris@805 105 int getWindowSize() const;
Chris@0 106
Chris@805 107 void setWindowHopLevel(int level);
Chris@805 108 int getWindowHopLevel() const;
Chris@0 109
Chris@0 110 void setWindowType(WindowType type);
Chris@0 111 WindowType getWindowType() const;
Chris@0 112
Chris@805 113 void setZeroPadLevel(int level);
Chris@805 114 int getZeroPadLevel() const;
Chris@109 115
Chris@0 116 /**
Chris@110 117 * Set the gain multiplier for sample values in this view.
Chris@0 118 * The default is 1.0.
Chris@0 119 */
Chris@0 120 void setGain(float gain);
Chris@0 121 float getGain() const;
Chris@0 122
Chris@37 123 /**
Chris@110 124 * Set the threshold for sample values to qualify for being shown
Chris@110 125 * in the FFT, in voltage units.
Chris@37 126 *
Chris@37 127 * The default is 0.0.
Chris@37 128 */
Chris@37 129 void setThreshold(float threshold);
Chris@37 130 float getThreshold() const;
Chris@37 131
Chris@805 132 void setMinFrequency(int);
Chris@805 133 int getMinFrequency() const;
Chris@37 134
Chris@805 135 void setMaxFrequency(int); // 0 -> no maximum
Chris@805 136 int getMaxFrequency() const;
Chris@0 137
Chris@37 138 enum ColourScale {
Chris@37 139 LinearColourScale,
Chris@37 140 MeterColourScale,
Chris@215 141 dBSquaredColourScale,
Chris@37 142 dBColourScale,
Chris@37 143 PhaseColourScale
Chris@37 144 };
Chris@0 145
Chris@0 146 /**
Chris@0 147 * Specify the scale for sample levels. See WaveformLayer for
Chris@0 148 * details of meter and dB scaling. The default is dBColourScale.
Chris@0 149 */
Chris@0 150 void setColourScale(ColourScale);
Chris@0 151 ColourScale getColourScale() const;
Chris@0 152
Chris@35 153 enum FrequencyScale {
Chris@35 154 LinearFrequencyScale,
Chris@35 155 LogFrequencyScale
Chris@35 156 };
Chris@0 157
Chris@0 158 /**
Chris@0 159 * Specify the scale for the y axis.
Chris@0 160 */
Chris@0 161 void setFrequencyScale(FrequencyScale);
Chris@0 162 FrequencyScale getFrequencyScale() const;
Chris@0 163
Chris@37 164 enum BinDisplay {
Chris@37 165 AllBins,
Chris@37 166 PeakBins,
Chris@37 167 PeakFrequencies
Chris@35 168 };
Chris@35 169
Chris@35 170 /**
Chris@35 171 * Specify the processing of frequency bins for the y axis.
Chris@35 172 */
Chris@37 173 void setBinDisplay(BinDisplay);
Chris@37 174 BinDisplay getBinDisplay() const;
Chris@862 175
Chris@862 176 enum Normalization {
Chris@862 177 NoNormalization,
Chris@862 178 NormalizeColumns,
Chris@862 179 NormalizeVisibleArea,
Chris@862 180 NormalizeHybrid
Chris@862 181 };
Chris@36 182
Chris@719 183 /**
Chris@862 184 * Specify the normalization mode for bin values.
Chris@719 185 */
Chris@862 186 void setNormalization(Normalization);
Chris@862 187 Normalization getNormalization() const;
Chris@120 188
Chris@719 189 /**
Chris@862 190 * Specify the colour map. See ColourMapper for the colour map
Chris@862 191 * values.
Chris@719 192 */
Chris@197 193 void setColourMap(int map);
Chris@197 194 int getColourMap() const;
Chris@0 195
Chris@9 196 /**
Chris@9 197 * Specify the colourmap rotation for the colour scale.
Chris@9 198 */
Chris@9 199 void setColourRotation(int);
Chris@9 200 int getColourRotation() const;
Chris@9 201
Chris@0 202 virtual VerticalPosition getPreferredFrameCountPosition() const {
Chris@0 203 return PositionTop;
Chris@0 204 }
Chris@0 205
Chris@15 206 virtual bool isLayerOpaque() const { return true; }
Chris@287 207
Chris@287 208 virtual ColourSignificance getLayerColourSignificance() const {
Chris@287 209 return ColourHasMeaningfulValue;
Chris@287 210 }
Chris@15 211
Chris@267 212 float getYForFrequency(const View *v, float frequency) const;
Chris@267 213 float getFrequencyForY(const View *v, int y) const;
Chris@42 214
Chris@115 215 virtual int getCompletion(View *v) const;
Chris@583 216 virtual QString getError(View *v) const;
Chris@0 217
Chris@101 218 virtual bool getValueExtents(float &min, float &max,
Chris@101 219 bool &logarithmic, QString &unit) const;
Chris@101 220
Chris@101 221 virtual bool getDisplayExtents(float &min, float &max) const;
Chris@79 222
Chris@120 223 virtual bool setDisplayExtents(float min, float max);
Chris@120 224
Chris@267 225 virtual bool getYScaleValue(const View *, int, float &, QString &) const;
Chris@261 226
Chris@316 227 virtual void toXml(QTextStream &stream, QString indent = "",
Chris@316 228 QString extraAttributes = "") const;
Chris@6 229
Chris@11 230 void setProperties(const QXmlAttributes &attributes);
Chris@11 231
Chris@47 232 virtual void setLayerDormant(const View *v, bool dormant);
Chris@29 233
Chris@248 234 virtual bool isLayerScrollable(const View *) const { return false; }
Chris@94 235
Chris@133 236 virtual int getVerticalZoomSteps(int &defaultStep) const;
Chris@133 237 virtual int getCurrentVerticalZoomStep() const;
Chris@133 238 virtual void setVerticalZoomStep(int);
Chris@187 239 virtual RangeMapper *getNewVerticalZoomRangeMapper() const;
Chris@133 240
Chris@193 241 virtual const Model *getSliceableModel() const;
Chris@193 242
Chris@0 243 protected slots:
Chris@0 244 void cacheInvalid();
Chris@805 245 void cacheInvalid(int startFrame, int endFrame);
Chris@122 246
Chris@122 247 void preferenceChanged(PropertyContainer::PropertyName name);
Chris@0 248
Chris@0 249 void fillTimerTimedOut();
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@805 255 int m_windowSize;
Chris@35 256 WindowType m_windowType;
Chris@805 257 int m_windowHopLevel;
Chris@805 258 int m_zeroPadLevel;
Chris@805 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@805 266 int m_minFrequency;
Chris@805 267 int m_maxFrequency;
Chris@805 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 mutable int m_lastPaintBlockWidth;
Chris@215 280 mutable RealTime m_lastPaintTime;
Chris@215 281
Chris@38 282 enum { NO_VALUE = 0 }; // colour index for unused pixels
Chris@38 283
Chris@197 284 class Palette
Chris@86 285 {
Chris@86 286 public:
Chris@86 287 QColor getColour(unsigned char index) const {
Chris@86 288 return m_colours[index];
Chris@86 289 }
Chris@86 290
Chris@86 291 void setColour(unsigned char index, QColor colour) {
Chris@86 292 m_colours[index] = colour;
Chris@86 293 }
Chris@86 294
Chris@86 295 private:
Chris@86 296 QColor m_colours[256];
Chris@86 297 };
Chris@86 298
Chris@197 299 Palette m_palette;
Chris@31 300
Chris@477 301 /**
Chris@478 302 * ImageCache covers the area of the view, at view resolution.
Chris@477 303 * Not all of it is necessarily valid at once (it is refreshed
Chris@477 304 * in parts when scrolling, for example).
Chris@477 305 */
Chris@478 306 struct ImageCache
Chris@95 307 {
Chris@478 308 QImage image;
Chris@95 309 QRect validArea;
Chris@806 310 int startFrame;
Chris@805 311 int zoomLevel;
Chris@95 312 };
Chris@478 313 typedef std::map<const View *, ImageCache> ViewImageCache;
Chris@478 314 void invalidateImageCaches();
Chris@805 315 void invalidateImageCaches(int startFrame, int endFrame);
Chris@478 316 mutable ViewImageCache m_imageCaches;
Chris@477 317
Chris@477 318 /**
Chris@477 319 * When painting, we draw directly onto the draw buffer and then
Chris@478 320 * copy this to the part of the image cache that needed refreshing
Chris@478 321 * before copying the image cache onto the window. (Remind me why
Chris@477 322 * we don't draw directly onto the cache?)
Chris@477 323 */
Chris@95 324 mutable QImage m_drawBuffer;
Chris@0 325
Chris@114 326 mutable QTimer *m_updateTimer;
Chris@110 327
Chris@805 328 mutable int m_candidateFillStartFrame;
Chris@0 329 bool m_exiting;
Chris@0 330
Chris@197 331 void initialisePalette();
Chris@197 332 void rotatePalette(int distance);
Chris@0 333
Chris@119 334 unsigned char getDisplayValue(View *v, float input) const;
Chris@40 335
Chris@40 336 int getColourScaleWidth(QPainter &) const;
Chris@40 337
Chris@121 338 void illuminateLocalFeatures(View *v, QPainter &painter) const;
Chris@121 339
Chris@40 340 float getEffectiveMinFrequency() const;
Chris@40 341 float getEffectiveMaxFrequency() const;
Chris@38 342
Chris@0 343 struct LayerRange {
Chris@806 344 int startFrame;
Chris@0 345 int zoomLevel;
Chris@805 346 int modelStart;
Chris@805 347 int modelEnd;
Chris@0 348 };
Chris@486 349
Chris@486 350 // Note that the getYBin... methods return the nominal bin in the
Chris@486 351 // un-smoothed spectrogram. This is not necessarily the same bin
Chris@486 352 // as is pulled from the spectrogram and drawn at the given
Chris@486 353 // position, if the spectrogram has oversampling smoothing. Use
Chris@486 354 // getSmoothedYBinRange to obtain that.
Chris@486 355
Chris@44 356 bool getXBinRange(View *v, int x, float &windowMin, float &windowMax) const;
Chris@44 357 bool getYBinRange(View *v, int y, float &freqBinMin, float &freqBinMax) const;
Chris@486 358 bool getSmoothedYBinRange(View *v, int y, float &freqBinMin, float &freqBinMax) const;
Chris@0 359
Chris@44 360 bool getYBinSourceRange(View *v, int y, float &freqMin, float &freqMax) const;
Chris@44 361 bool getAdjustedYBinSourceRange(View *v, int x, int y,
Chris@35 362 float &freqMin, float &freqMax,
Chris@35 363 float &adjFreqMin, float &adjFreqMax) const;
Chris@44 364 bool getXBinSourceRange(View *v, int x, RealTime &timeMin, RealTime &timeMax) const;
Chris@44 365 bool getXYBinSourceRange(View *v, int x, int y, float &min, float &max,
Chris@38 366 float &phaseMin, float &phaseMax) const;
Chris@0 367
Chris@805 368 int getWindowIncrement() const {
Chris@97 369 if (m_windowHopLevel == 0) return m_windowSize;
Chris@97 370 else if (m_windowHopLevel == 1) return (m_windowSize * 3) / 4;
Chris@97 371 else return m_windowSize / (1 << (m_windowHopLevel - 1));
Chris@0 372 }
Chris@113 373
Chris@805 374 int getZeroPadLevel(const View *v) const;
Chris@805 375 int getFFTSize(const View *v) const;
Chris@130 376 FFTModel *getFFTModel(const View *v) const;
Chris@484 377 Dense3DModelPeakCache *getPeakCache(const View *v) const;
Chris@130 378 void invalidateFFTModels();
Chris@115 379
Chris@130 380 typedef std::pair<FFTModel *, int> FFTFillPair; // model, last fill
Chris@115 381 typedef std::map<const View *, FFTFillPair> ViewFFTMap;
Chris@484 382 typedef std::map<const View *, Dense3DModelPeakCache *> PeakCacheMap;
Chris@130 383 mutable ViewFFTMap m_fftModels;
Chris@484 384 mutable PeakCacheMap m_peakCaches;
Chris@193 385 mutable Model *m_sliceableModel;
Chris@119 386
Chris@119 387 class MagnitudeRange {
Chris@119 388 public:
Chris@119 389 MagnitudeRange() : m_min(0), m_max(0) { }
Chris@119 390 bool operator==(const MagnitudeRange &r) {
Chris@119 391 return r.m_min == m_min && r.m_max == m_max;
Chris@119 392 }
Chris@119 393 bool isSet() const { return (m_min != 0 || m_max != 0); }
Chris@119 394 void set(float min, float max) {
Chris@119 395 m_min = convert(min);
Chris@119 396 m_max = convert(max);
Chris@119 397 if (m_max < m_min) m_max = m_min;
Chris@119 398 }
Chris@119 399 bool sample(float f) {
Chris@119 400 unsigned int ui = convert(f);
Chris@119 401 bool changed = false;
Chris@119 402 if (isSet()) {
Chris@119 403 if (ui < m_min) { m_min = ui; changed = true; }
Chris@119 404 if (ui > m_max) { m_max = ui; changed = true; }
Chris@119 405 } else {
Chris@119 406 m_max = m_min = ui;
Chris@119 407 changed = true;
Chris@119 408 }
Chris@119 409 return changed;
Chris@119 410 }
Chris@119 411 bool sample(const MagnitudeRange &r) {
Chris@119 412 bool changed = false;
Chris@119 413 if (isSet()) {
Chris@119 414 if (r.m_min < m_min) { m_min = r.m_min; changed = true; }
Chris@119 415 if (r.m_max > m_max) { m_max = r.m_max; changed = true; }
Chris@119 416 } else {
Chris@119 417 m_min = r.m_min;
Chris@119 418 m_max = r.m_max;
Chris@119 419 changed = true;
Chris@119 420 }
Chris@119 421 return changed;
Chris@119 422 }
Chris@119 423 float getMin() const { return float(m_min) / UINT_MAX; }
Chris@119 424 float getMax() const { return float(m_max) / UINT_MAX; }
Chris@119 425 private:
Chris@119 426 unsigned int m_min;
Chris@119 427 unsigned int m_max;
Chris@119 428 unsigned int convert(float f) {
Chris@119 429 if (f < 0.f) f = 0.f;
Chris@119 430 if (f > 1.f) f = 1.f;
Chris@119 431 return (unsigned int)(f * UINT_MAX);
Chris@119 432 }
Chris@119 433 };
Chris@119 434
Chris@119 435 typedef std::map<const View *, MagnitudeRange> ViewMagMap;
Chris@119 436 mutable ViewMagMap m_viewMags;
Chris@119 437 mutable std::vector<MagnitudeRange> m_columnMags;
Chris@119 438 void invalidateMagnitudes();
Chris@119 439 bool updateViewMagnitudes(View *v) const;
Chris@484 440 bool paintDrawBuffer(View *v, int w, int h,
Chris@490 441 int *binforx, float *binfory,
Chris@491 442 bool usePeaksCache,
Chris@491 443 MagnitudeRange &overallMag,
Chris@491 444 bool &overallMagChanged) const;
Chris@488 445 bool paintDrawBufferPeakFrequencies(View *v, int w, int h,
Chris@488 446 int *binforx,
Chris@488 447 int minbin,
Chris@488 448 int maxbin,
Chris@488 449 float displayMinFreq,
Chris@488 450 float displayMaxFreq,
Chris@491 451 bool logarithmic,
Chris@491 452 MagnitudeRange &overallMag,
Chris@491 453 bool &overallMagChanged) const;
Chris@273 454
Chris@273 455 virtual void updateMeasureRectYCoords(View *v, const MeasureRect &r) const;
Chris@273 456 virtual void setMeasureRectYCoord(View *v, MeasureRect &r, bool start, int y) const;
Chris@0 457 };
Chris@0 458
Chris@0 459 #endif