SpectrogramLayer.h
Go to the documentation of this file.
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2 
3 /*
4  Sonic Visualiser
5  An audio file viewer and annotation editor.
6  Centre for Digital Music, Queen Mary, University of London.
7  This file copyright 2006 Chris Cannam and QMUL.
8 
9  This program is free software; you can redistribute it and/or
10  modify it under the terms of the GNU General Public License as
11  published by the Free Software Foundation; either version 2 of the
12  License, or (at your option) any later version. See the file
13  COPYING included with this distribution for more information.
14 */
15 
16 #ifndef SPECTROGRAM_LAYER_H
17 #define SPECTROGRAM_LAYER_H
18 
19 #include "SliceableLayer.h"
20 #include "base/Window.h"
21 #include "base/MagnitudeRange.h"
22 #include "base/RealTime.h"
23 #include "base/Thread.h"
24 #include "base/PropertyContainer.h"
25 #include "data/model/PowerOfSqrtTwoZoomConstraint.h"
26 #include "data/model/DenseTimeValueModel.h"
27 #include "data/model/FFTModel.h"
28 
29 #include "VerticalBinLayer.h"
30 #include "ColourScale.h"
31 #include "Colour3DPlotRenderer.h"
32 
33 #include <QMutex>
34 #include <QWaitCondition>
35 #include <QImage>
36 #include <QPixmap>
37 
38 class View;
39 class QPainter;
40 class QImage;
41 class QPixmap;
42 class QTimer;
43 class FFTModel;
44 class Dense3DModelPeakCache;
45 
52  public PowerOfSqrtTwoZoomConstraint
53 {
54  Q_OBJECT
55 
56 public:
58 
65 
66  const ZoomConstraint *getZoomConstraint() const override { return this; }
67  ModelId getModel() const override { return m_model; }
68 
69  ModelId getExportModel(LayerGeometryProvider *) const override;
70 
71  void paint(LayerGeometryProvider *v, QPainter &paint, QRect rect) const override;
72  void setSynchronousPainting(bool synchronous) override;
73 
74  int getVerticalScaleWidth(LayerGeometryProvider *v, bool detailed, QPainter &) const override;
75  void paintVerticalScale(LayerGeometryProvider *v, bool detailed, QPainter &paint, QRect rect) const override;
76 
77  bool getCrosshairExtents(LayerGeometryProvider *, QPainter &, QPoint cursorPos,
78  std::vector<QRect> &extents) const override;
79  void paintCrosshairs(LayerGeometryProvider *, QPainter &, QPoint) const override;
80 
81  QString getFeatureDescription(LayerGeometryProvider *v, QPoint &) const override;
82 
83  bool snapToFeatureFrame(LayerGeometryProvider *v, sv_frame_t &frame,
84  int &resolution,
85  SnapType snap, int ycoord) const override;
86 
87  void measureDoubleClick(LayerGeometryProvider *, QMouseEvent *) override;
88 
89  bool hasLightBackground() const override;
90 
91  void setModel(ModelId model); // a DenseTimeValueModel
92 
93  PropertyList getProperties() const override;
94  QString getPropertyLabel(const PropertyName &) const override;
95  QString getPropertyIconName(const PropertyName &) const override;
96  PropertyType getPropertyType(const PropertyName &) const override;
97  QString getPropertyGroupName(const PropertyName &) const override;
98  int getPropertyRangeAndValue(const PropertyName &,
99  int *min, int *max, int *deflt) const override;
100  QString getPropertyValueLabel(const PropertyName &,
101  int value) const override;
102  QString getPropertyValueIconName(const PropertyName &,
103  int value) const override;
104  RangeMapper *getNewPropertyRangeMapper(const PropertyName &) const override;
105  void setProperty(const PropertyName &, int value) override;
106 
112  void setChannel(int);
113  int getChannel() const;
114 
115  void setWindowSize(int);
116  int getWindowSize() const;
117 
118  void setWindowHopLevel(int level);
119  int getWindowHopLevel() const;
120 
121  void setOversampling(int oversampling);
122  int getOversampling() const;
123 
124  void setWindowType(WindowType type);
125  WindowType getWindowType() const;
126 
131  void setGain(float gain);
132  float getGain() const;
133 
140  void setThreshold(float threshold);
141  float getThreshold() const;
142 
155  void setVerticallyFixed();
156 
157  void setMinFrequency(int);
158  int getMinFrequency() const;
159 
160  void setMaxFrequency(int); // 0 -> no maximum
161  int getMaxFrequency() const;
162 
170 
175  void setColourScaleMultiple(double);
176  double getColourScaleMultiple() const;
177 
181  void setBinScale(BinScale);
182  BinScale getBinScale() const;
183 
188  BinDisplay getBinDisplay() const;
189 
193  void setNormalization(ColumnNormalization);
194  ColumnNormalization getNormalization() const;
195 
199  void setNormalizeVisibleArea(bool);
200  bool getNormalizeVisibleArea() const;
201 
206  void setColourMap(int map);
207  int getColourMap() const;
208 
212  void setColourRotation(int);
213  int getColourRotation() const;
214 
216  return PositionTop;
217  }
218 
219  bool isLayerOpaque() const override { return true; }
220 
223  }
224 
225  double getYForFrequency(const LayerGeometryProvider *v, double frequency) const;
226  double getFrequencyForY(const LayerGeometryProvider *v, int y) const;
227 
229  double getYForBin(const LayerGeometryProvider *, double bin) const override;
230  double getBinForY(const LayerGeometryProvider *, double y) const override;
231 
232  int getCompletion(LayerGeometryProvider *v) const override;
233  QString getError(LayerGeometryProvider *v) const override;
234 
235  bool getValueExtents(double &min, double &max,
236  bool &logarithmic, QString &unit) const override;
237 
238  bool getDisplayExtents(double &min, double &max) const override;
239 
240  bool setDisplayExtents(double min, double max) override;
241 
242  bool getYScaleValue(const LayerGeometryProvider *, int, double &, QString &) const override;
243 
244  void toXml(QTextStream &stream, QString indent = "",
245  QString extraAttributes = "") const override;
246 
247  void setProperties(const QXmlAttributes &attributes) override;
248 
249  void setLayerDormant(const LayerGeometryProvider *v, bool dormant) override;
250 
251  bool isLayerScrollable(const LayerGeometryProvider *) const override;
252 
253  int getVerticalZoomSteps(int &defaultStep) const override;
254  int getCurrentVerticalZoomStep() const override;
255  void setVerticalZoomStep(int) override;
256  RangeMapper *getNewVerticalZoomRangeMapper() const override;
257 
258  ModelId getSliceableModel() const override;
259 
260 protected slots:
261  void cacheInvalid(ModelId);
262  void cacheInvalid(ModelId, sv_frame_t startFrame, sv_frame_t endFrame);
263 
264  void preferenceChanged(PropertyContainer::PropertyName name);
265 
266 protected:
267  ModelId m_model; // a DenseTimeValueModel
268 
271  WindowType m_windowType;
274  float m_gain;
276  float m_threshold;
288  mutable QColor m_crosshairColour;
291  ColumnNormalization m_normalization; // of individual columns
295 
296  mutable bool m_haveDetailedScale;
297 
298  static std::pair<ColourScaleType, double> convertToColourScale(int value);
299  static int convertFromColourScale(ColourScaleType type, double multiple);
300  static std::pair<ColumnNormalization, bool> convertToColumnNorm(int value);
301  static int convertFromColumnNorm(ColumnNormalization norm, bool visible);
302 
303  bool m_exiting;
304 
305  int getColourScaleWidth(QPainter &) const;
306 
307  void illuminateLocalFeatures(LayerGeometryProvider *v, QPainter &painter) const;
308 
309  double getEffectiveMinFrequency() const;
310  double getEffectiveMaxFrequency() const;
311 
312  bool getXBinRange(LayerGeometryProvider *v, int x, double &windowMin, double &windowMax) const;
313  bool getYBinRange(LayerGeometryProvider *v, int y, double &freqBinMin, double &freqBinMax) const;
314 
315  bool getYBinSourceRange(LayerGeometryProvider *v, int y, double &freqMin, double &freqMax) const;
317  double &freqMin, double &freqMax,
318  double &adjFreqMin, double &adjFreqMax) const;
319  bool getXBinSourceRange(LayerGeometryProvider *v, int x, RealTime &timeMin, RealTime &timeMax) const;
320  bool getXYBinSourceRange(LayerGeometryProvider *v, int x, int y, double &min, double &max,
321  double &phaseMin, double &phaseMax) const;
322 
323  int getWindowIncrement() const {
324  if (m_windowHopLevel == 0) return m_windowSize;
325  else if (m_windowHopLevel == 1) return (m_windowSize * 3) / 4;
326  else return m_windowSize / (1 << (m_windowHopLevel - 1));
327  }
328 
329  int getFFTSize() const; // m_windowSize * getOversampling()
330 
331  // We take responsibility for registering/deregistering these
332  // models and caches with ModelById
333  ModelId m_fftModel; // an FFTModel
334  ModelId m_wholeCache; // a Dense3DModelPeakCache
335  ModelId m_peakCache; // a Dense3DModelPeakCache
337 
338  mutable std::vector<ModelId> m_exporters; // used, waiting to be released
339 
340  void checkCacheSpace(int *suggestedPeakDivisor,
341  bool *createWholeCache) const;
342  void recreateFFTModel();
343 
344  typedef std::map<int, MagnitudeRange> ViewMagMap; // key is view id
345  mutable ViewMagMap m_viewMags;
346  mutable ViewMagMap m_lastRenderedMags; // when in normalizeVisibleArea mode
347  void invalidateMagnitudes();
348 
349  typedef std::map<int, Colour3DPlotRenderer *> ViewRendererMap; // key is view id
350  mutable ViewRendererMap m_renderers;
352  void invalidateRenderers();
353 
354  void deleteDerivedModels();
355 
356  void paintWithRenderer(LayerGeometryProvider *v, QPainter &paint, QRect rect) const;
357 
359  QPainter &paint, QRect rect) const;
361  QPainter &paint, QRect rect) const;
362 
364  const MeasureRect &r) const override;
366  MeasureRect &r, bool start, int y) const override;
367 };
368 
369 #endif
QString getPropertyValueLabel(const PropertyName &, int value) const override
void setChannel(int)
Specify the channel to use from the source model.
void setColourRotation(int)
Specify the colourmap rotation for the colour scale.
RangeMapper * getNewVerticalZoomRangeMapper() const override
Create and return a range mapper for vertical zoom step values.
void measureDoubleClick(LayerGeometryProvider *, QMouseEvent *) override
int getMaxFrequency() const
bool getYScaleValue(const LayerGeometryProvider *, int, double &, QString &) const override
Return the value and unit at the given y coordinate in the given view.
ModelId getModel() const override
Return the ID of the model represented in this layer.
void setNormalizeVisibleArea(bool)
Specify whether to normalize the visible area.
bool isLayerScrollable(const LayerGeometryProvider *) const override
This should return true if the layer can safely be scrolled automatically by a given view (simply cop...
void cacheInvalid(ModelId)
bool setDisplayExtents(double min, double max) override
Set the displayed minimum and maximum values for the y axis to the given range, if supported...
SpectrogramLayer(Configuration=FullRangeDb)
Construct a SpectrogramLayer with default parameters appropriate for the given configuration.
PropertyList getProperties() const override
double getYForBin(const LayerGeometryProvider *, double bin) const override
!! VerticalBinLayer methods. Note overlap with get*BinRange()
double getFrequencyForY(const LayerGeometryProvider *v, int y) const
void checkCacheSpace(int *suggestedPeakDivisor, bool *createWholeCache) const
int getWindowSize() const
SpectrogramLayer represents waveform data (obtained from a DenseTimeValueModel) in spectrogram form...
int getWindowIncrement() const
static std::pair< ColourScaleType, double > convertToColourScale(int value)
void illuminateLocalFeatures(LayerGeometryProvider *v, QPainter &painter) const
bool getDisplayExtents(double &min, double &max) const override
Return the minimum and maximum values within the visible area for the y axis of this layer...
std::map< int, MagnitudeRange > ViewMagMap
void updateMeasureRectYCoords(LayerGeometryProvider *v, const MeasureRect &r) const override
ViewMagMap m_lastRenderedMags
int getPropertyRangeAndValue(const PropertyName &, int *min, int *max, int *deflt) const override
void setBinScale(BinScale)
Specify the scale for the y axis.
bool getXBinSourceRange(LayerGeometryProvider *v, int x, RealTime &timeMin, RealTime &timeMax) const
void paintWithRenderer(LayerGeometryProvider *v, QPainter &paint, QRect rect) const
double getBinForY(const LayerGeometryProvider *, double y) const override
Return the bin number, possibly fractional, at the given y coordinate.
int getColourScaleWidth(QPainter &) const
int getColourRotation() const
int getOversampling() const
int getFFTSize() const
ViewRendererMap m_renderers
bool getYBinRange(LayerGeometryProvider *v, int y, double &freqBinMin, double &freqBinMax) const
void setColourScaleMultiple(double)
Specify multiple factor for colour scale.
bool getXYBinSourceRange(LayerGeometryProvider *v, int x, int y, double &min, double &max, double &phaseMin, double &phaseMax) const
void setNormalization(ColumnNormalization)
Specify the normalization mode for individual columns.
void setThreshold(float threshold)
Set the threshold for sample values to qualify for being shown in the FFT, in voltage units...
void setModel(ModelId model)
int getMinFrequency() const
ColourSignificance
Definition: Layer.h:348
void setProperty(const PropertyName &, int value) override
WindowType m_windowType
VerticalPosition
Definition: Layer.h:140
void setVerticallyFixed()
Mark the spectrogram layer as having a fixed range in the vertical axis.
void setWindowHopLevel(int level)
static std::pair< ColumnNormalization, bool > convertToColumnNorm(int value)
double getEffectiveMinFrequency() const
void setColourMap(int map)
Specify the colour map.
bool snapToFeatureFrame(LayerGeometryProvider *v, sv_frame_t &frame, int &resolution, SnapType snap, int ycoord) const override
Adjust the given frame to snap to the nearest feature, if possible.
Interface for classes that provide geometry information (such as size, start frame, and a large number of other properties) about the disposition of a layer.
VerticalPosition getPreferredFrameCountPosition() const override
int getVerticalScaleWidth(LayerGeometryProvider *v, bool detailed, QPainter &) const override
void setVerticalZoomStep(int) override
Set the vertical zoom step.
ColourScaleType getColourScale() const
Interface for layers in which the Y axis corresponds to bin number rather than scale value...
void setMeasureRectYCoord(LayerGeometryProvider *v, MeasureRect &r, bool start, int y) const override
void setOversampling(int oversampling)
ModelId getExportModel(LayerGeometryProvider *) const override
Return the ID of a model representing the contents of this layer in a form suitable for export to a t...
ColourSignificance getLayerColourSignificance() const override
This should return the degree of meaning associated with colour in this layer.
bool hasLightBackground() const override
float getGain() const
void paint(LayerGeometryProvider *v, QPainter &paint, QRect rect) const override
Paint the given rectangle of this layer onto the given view using the given painter, superimposing it on top of any existing material in that view.
double getYForFrequency(const LayerGeometryProvider *v, double frequency) const
int getWindowHopLevel() const
void setSynchronousPainting(bool synchronous) override
Enable or disable synchronous painting.
ColumnNormalization m_normalization
BinDisplay m_binDisplay
void paintDetailedScale(LayerGeometryProvider *v, QPainter &paint, QRect rect) const
QString getError(LayerGeometryProvider *v) const override
Return an error string if any errors have occurred while loading or processing data for the given vie...
int getVerticalZoomSteps(int &defaultStep) const override
Get the number of vertical zoom steps available for this layer.
bool getValueExtents(double &min, double &max, bool &logarithmic, QString &unit) const override
Return the minimum and maximum values for the y axis of the model in this layer, as well as whether t...
SnapType
Definition: Layer.h:195
void preferenceChanged(PropertyContainer::PropertyName name)
std::vector< ModelId > m_exporters
bool getYBinSourceRange(LayerGeometryProvider *v, int y, double &freqMin, double &freqMax) const
void paintVerticalScale(LayerGeometryProvider *v, bool detailed, QPainter &paint, QRect rect) const override
ColourScaleType m_colourScale
static int convertFromColourScale(ColourScaleType type, double multiple)
const ZoomConstraint * getZoomConstraint() const override
Return a zoom constraint object defining the supported zoom levels for this layer.
RangeMapper * getNewPropertyRangeMapper(const PropertyName &) const override
void paintDetailedScalePhase(LayerGeometryProvider *v, QPainter &paint, QRect rect) const
void toXml(QTextStream &stream, QString indent="", QString extraAttributes="") const override
QString getPropertyIconName(const PropertyName &) const override
QString getPropertyGroupName(const PropertyName &) const override
void setProperties(const QXmlAttributes &attributes) override
Set the particular properties of a layer (those specific to the subclass) from a set of XML attribute...
ColourScaleType
Definition: ColourScale.h:21
BinDisplay getBinDisplay() const
void paintCrosshairs(LayerGeometryProvider *, QPainter &, QPoint) const override
double getEffectiveMaxFrequency() const
Colour3DPlotRenderer * getRenderer(LayerGeometryProvider *) const
BinScale getBinScale() const
View is the base class of widgets that display one or more overlaid views of data against a horizonta...
Definition: View.h:55
std::map< int, Colour3DPlotRenderer * > ViewRendererMap
QString getPropertyValueIconName(const PropertyName &, int value) const override
ColumnNormalization getNormalization() const
void setLayerDormant(const LayerGeometryProvider *v, bool dormant) override
Indicate that a layer is not currently visible in the given view and is not expected to become visibl...
bool isLayerOpaque() const override
This should return true if the layer completely obscures any underlying layers.
void setGain(float gain)
Set the gain multiplier for sample values in this view.
int getColourMap() const
QString getFeatureDescription(LayerGeometryProvider *v, QPoint &) const override
void setColourScale(ColourScaleType)
Specify the scale for sample levels.
int getChannel() const
QString getPropertyLabel(const PropertyName &) const override
int getCompletion(LayerGeometryProvider *v) const override
Return the proportion of background work complete in drawing this view, as a percentage – in most ca...
ModelId getSliceableModel() const override
float getThreshold() const
double getColourScaleMultiple() const
bool getAdjustedYBinSourceRange(LayerGeometryProvider *v, int x, int y, double &freqMin, double &freqMax, double &adjFreqMin, double &adjFreqMax) const
void setWindowType(WindowType type)
bool getXBinRange(LayerGeometryProvider *v, int x, double &windowMin, double &windowMax) const
int getCurrentVerticalZoomStep() const override
Get the current vertical zoom step.
bool getCrosshairExtents(LayerGeometryProvider *, QPainter &, QPoint cursorPos, std::vector< QRect > &extents) const override
bool getNormalizeVisibleArea() const
void setBinDisplay(BinDisplay)
Specify the processing of frequency bins for the y axis.
WindowType getWindowType() const
static int convertFromColumnNorm(ColumnNormalization norm, bool visible)
PropertyType getPropertyType(const PropertyName &) const override