annotate layer/SpectrogramLayer.h @ 112:43b1ca9647e1

* Exceptions for file read etc
author Chris Cannam
date Wed, 28 Jun 2006 15:42:04 +0000
parents f262aa8973e3
children 7a23edd831cb
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@59 7 This file copyright 2006 Chris Cannam.
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@0 19 #include "base/Layer.h"
Chris@0 20 #include "base/Window.h"
Chris@71 21 #include "base/RealTime.h"
Chris@92 22 #include "base/Thread.h"
Chris@0 23 #include "model/PowerOfSqrtTwoZoomConstraint.h"
Chris@0 24 #include "model/DenseTimeValueModel.h"
Chris@0 25
Chris@0 26 #include <QMutex>
Chris@0 27 #include <QWaitCondition>
Chris@95 28 #include <QImage>
Chris@95 29 #include <QPixmap>
Chris@0 30
Chris@0 31 class View;
Chris@0 32 class QPainter;
Chris@0 33 class QImage;
Chris@0 34 class QPixmap;
Chris@0 35 class QTimer;
Chris@110 36 class FFTDataServer;
Chris@0 37
Chris@0 38 /**
Chris@0 39 * SpectrogramLayer represents waveform data (obtained from a
Chris@0 40 * DenseTimeValueModel) in spectrogram form.
Chris@0 41 */
Chris@0 42
Chris@0 43 class SpectrogramLayer : public Layer,
Chris@31 44 public PowerOfSqrtTwoZoomConstraint
Chris@0 45 {
Chris@0 46 Q_OBJECT
Chris@0 47
Chris@0 48 public:
Chris@37 49 enum Configuration { FullRangeDb, MelodicRange, MelodicPeaks };
Chris@0 50
Chris@44 51 SpectrogramLayer(Configuration = FullRangeDb);
Chris@0 52 ~SpectrogramLayer();
Chris@0 53
Chris@0 54 virtual const ZoomConstraint *getZoomConstraint() const { return this; }
Chris@0 55 virtual const Model *getModel() const { return m_model; }
Chris@44 56 virtual void paint(View *v, QPainter &paint, QRect rect) const;
Chris@0 57
Chris@44 58 virtual int getVerticalScaleWidth(View *v, QPainter &) const;
Chris@44 59 virtual void paintVerticalScale(View *v, QPainter &paint, QRect rect) const;
Chris@0 60
Chris@77 61 virtual bool getCrosshairExtents(View *, QPainter &, QPoint cursorPos,
Chris@77 62 std::vector<QRect> &extents) const;
Chris@77 63 virtual void paintCrosshairs(View *, QPainter &, QPoint) const;
Chris@77 64
Chris@44 65 virtual QString getFeatureDescription(View *v, QPoint &) const;
Chris@0 66
Chris@44 67 virtual bool snapToFeatureFrame(View *v, int &frame,
Chris@28 68 size_t &resolution,
Chris@28 69 SnapType snap) const;
Chris@13 70
Chris@0 71 void setModel(const DenseTimeValueModel *model);
Chris@0 72
Chris@0 73 virtual PropertyList getProperties() const;
Chris@87 74 virtual QString getPropertyLabel(const PropertyName &) const;
Chris@0 75 virtual PropertyType getPropertyType(const PropertyName &) const;
Chris@0 76 virtual QString getPropertyGroupName(const PropertyName &) const;
Chris@0 77 virtual int getPropertyRangeAndValue(const PropertyName &,
Chris@0 78 int *min, int *max) const;
Chris@0 79 virtual QString getPropertyValueLabel(const PropertyName &,
Chris@0 80 int value) const;
Chris@0 81 virtual void setProperty(const PropertyName &, int value);
Chris@0 82
Chris@0 83 /**
Chris@0 84 * Specify the channel to use from the source model.
Chris@0 85 * A value of -1 means to mix all available channels.
Chris@0 86 * The default is channel 0.
Chris@0 87 */
Chris@0 88 void setChannel(int);
Chris@0 89 int getChannel() const;
Chris@0 90
Chris@0 91 void setWindowSize(size_t);
Chris@0 92 size_t getWindowSize() const;
Chris@0 93
Chris@109 94 void setWindowHopLevel(size_t level);
Chris@97 95 size_t getWindowHopLevel() const;
Chris@0 96
Chris@0 97 void setWindowType(WindowType type);
Chris@0 98 WindowType getWindowType() const;
Chris@0 99
Chris@109 100 void setZeroPadLevel(size_t level);
Chris@109 101 size_t getZeroPadLevel() const;
Chris@109 102
Chris@0 103 /**
Chris@110 104 * Set the gain multiplier for sample values in this view.
Chris@0 105 * The default is 1.0.
Chris@0 106 */
Chris@0 107 void setGain(float gain);
Chris@0 108 float getGain() const;
Chris@0 109
Chris@37 110 /**
Chris@110 111 * Set the threshold for sample values to qualify for being shown
Chris@110 112 * in the FFT, in voltage units.
Chris@37 113 *
Chris@37 114 * The default is 0.0.
Chris@37 115 */
Chris@37 116 void setThreshold(float threshold);
Chris@37 117 float getThreshold() const;
Chris@37 118
Chris@37 119 void setMinFrequency(size_t);
Chris@37 120 size_t getMinFrequency() const;
Chris@37 121
Chris@0 122 void setMaxFrequency(size_t); // 0 -> no maximum
Chris@0 123 size_t getMaxFrequency() const;
Chris@0 124
Chris@37 125 enum ColourScale {
Chris@37 126 LinearColourScale,
Chris@37 127 MeterColourScale,
Chris@37 128 dBColourScale,
Chris@37 129 PhaseColourScale
Chris@37 130 };
Chris@0 131
Chris@0 132 /**
Chris@0 133 * Specify the scale for sample levels. See WaveformLayer for
Chris@0 134 * details of meter and dB scaling. The default is dBColourScale.
Chris@0 135 */
Chris@0 136 void setColourScale(ColourScale);
Chris@0 137 ColourScale getColourScale() const;
Chris@0 138
Chris@35 139 enum FrequencyScale {
Chris@35 140 LinearFrequencyScale,
Chris@35 141 LogFrequencyScale
Chris@35 142 };
Chris@0 143
Chris@0 144 /**
Chris@0 145 * Specify the scale for the y axis.
Chris@0 146 */
Chris@0 147 void setFrequencyScale(FrequencyScale);
Chris@0 148 FrequencyScale getFrequencyScale() const;
Chris@0 149
Chris@37 150 enum BinDisplay {
Chris@37 151 AllBins,
Chris@37 152 PeakBins,
Chris@37 153 PeakFrequencies
Chris@35 154 };
Chris@35 155
Chris@35 156 /**
Chris@35 157 * Specify the processing of frequency bins for the y axis.
Chris@35 158 */
Chris@37 159 void setBinDisplay(BinDisplay);
Chris@37 160 BinDisplay getBinDisplay() const;
Chris@35 161
Chris@36 162 void setNormalizeColumns(bool n);
Chris@36 163 bool getNormalizeColumns() const;
Chris@36 164
Chris@0 165 enum ColourScheme { DefaultColours, WhiteOnBlack, BlackOnWhite,
Chris@71 166 RedOnBlue, YellowOnBlack, BlueOnBlack, Rainbow };
Chris@0 167
Chris@0 168 void setColourScheme(ColourScheme scheme);
Chris@0 169 ColourScheme getColourScheme() const;
Chris@0 170
Chris@9 171 /**
Chris@9 172 * Specify the colourmap rotation for the colour scale.
Chris@9 173 */
Chris@9 174 void setColourRotation(int);
Chris@9 175 int getColourRotation() const;
Chris@9 176
Chris@0 177 virtual VerticalPosition getPreferredFrameCountPosition() const {
Chris@0 178 return PositionTop;
Chris@0 179 }
Chris@0 180
Chris@15 181 virtual bool isLayerOpaque() const { return true; }
Chris@15 182
Chris@44 183 float getYForFrequency(View *v, float frequency) const;
Chris@44 184 float getFrequencyForY(View *v, int y) const;
Chris@42 185
Chris@0 186 virtual int getCompletion() const;
Chris@0 187
Chris@101 188 virtual bool getValueExtents(float &min, float &max,
Chris@101 189 bool &logarithmic, QString &unit) const;
Chris@101 190
Chris@101 191 virtual bool getDisplayExtents(float &min, float &max) const;
Chris@79 192
Chris@6 193 virtual QString toXmlString(QString indent = "",
Chris@6 194 QString extraAttributes = "") const;
Chris@6 195
Chris@11 196 void setProperties(const QXmlAttributes &attributes);
Chris@11 197
Chris@47 198 virtual void setLayerDormant(const View *v, bool dormant);
Chris@29 199
Chris@94 200 virtual bool isLayerScrollable(const View *v) const { return false; }
Chris@94 201
Chris@0 202 protected slots:
Chris@0 203 void cacheInvalid();
Chris@0 204 void cacheInvalid(size_t startFrame, size_t endFrame);
Chris@0 205
Chris@0 206 void fillTimerTimedOut();
Chris@0 207
Chris@0 208 protected:
Chris@0 209 const DenseTimeValueModel *m_model; // I do not own this
Chris@0 210
Chris@35 211 int m_channel;
Chris@35 212 size_t m_windowSize;
Chris@35 213 WindowType m_windowType;
Chris@97 214 size_t m_windowHopLevel;
Chris@109 215 size_t m_zeroPadLevel;
Chris@107 216 size_t m_fftSize;
Chris@35 217 float m_gain;
Chris@37 218 float m_threshold;
Chris@35 219 int m_colourRotation;
Chris@37 220 size_t m_minFrequency;
Chris@35 221 size_t m_maxFrequency;
Chris@35 222 ColourScale m_colourScale;
Chris@35 223 ColourScheme m_colourScheme;
Chris@77 224 QColor m_crosshairColour;
Chris@35 225 FrequencyScale m_frequencyScale;
Chris@37 226 BinDisplay m_binDisplay;
Chris@36 227 bool m_normalizeColumns;
Chris@0 228
Chris@38 229 enum { NO_VALUE = 0 }; // colour index for unused pixels
Chris@38 230
Chris@86 231 class ColourMap
Chris@86 232 {
Chris@86 233 public:
Chris@86 234 QColor getColour(unsigned char index) const {
Chris@86 235 return m_colours[index];
Chris@86 236 }
Chris@86 237
Chris@86 238 void setColour(unsigned char index, QColor colour) {
Chris@86 239 m_colours[index] = colour;
Chris@86 240 }
Chris@86 241
Chris@86 242 private:
Chris@86 243 QColor m_colours[256];
Chris@86 244 };
Chris@86 245
Chris@86 246 ColourMap m_colourMap;
Chris@31 247
Chris@110 248 FFTDataServer *m_fftServer;
Chris@110 249 void getFFTServer();
Chris@0 250
Chris@95 251 struct PixmapCache
Chris@95 252 {
Chris@95 253 QPixmap pixmap;
Chris@95 254 QRect validArea;
Chris@95 255 long startFrame;
Chris@95 256 size_t zoomLevel;
Chris@95 257 };
Chris@95 258 typedef std::map<const View *, PixmapCache> ViewPixmapCache;
Chris@95 259 void invalidatePixmapCaches();
Chris@95 260 void invalidatePixmapCaches(size_t startFrame, size_t endFrame);
Chris@95 261 mutable ViewPixmapCache m_pixmapCaches;
Chris@95 262 mutable QImage m_drawBuffer;
Chris@0 263
Chris@0 264 QTimer *m_updateTimer;
Chris@110 265
Chris@44 266 mutable size_t m_candidateFillStartFrame;
Chris@0 267 size_t m_lastFillExtent;
Chris@0 268 bool m_exiting;
Chris@0 269
Chris@90 270 void setColourmap();
Chris@90 271 void rotateColourmap(int distance);
Chris@0 272
Chris@38 273 static float calculateFrequency(size_t bin,
Chris@38 274 size_t windowSize,
Chris@38 275 size_t windowIncrement,
Chris@38 276 size_t sampleRate,
Chris@38 277 float previousPhase,
Chris@38 278 float currentPhase,
Chris@38 279 bool &steadyState);
Chris@38 280
Chris@38 281 unsigned char getDisplayValue(float input) const;
Chris@40 282 float getInputForDisplayValue(unsigned char uc) const;
Chris@40 283
Chris@40 284 int getColourScaleWidth(QPainter &) const;
Chris@40 285
Chris@40 286 float getEffectiveMinFrequency() const;
Chris@40 287 float getEffectiveMaxFrequency() const;
Chris@38 288
Chris@0 289 struct LayerRange {
Chris@0 290 long startFrame;
Chris@0 291 int zoomLevel;
Chris@0 292 size_t modelStart;
Chris@0 293 size_t modelEnd;
Chris@0 294 };
Chris@44 295 bool getXBinRange(View *v, int x, float &windowMin, float &windowMax) const;
Chris@44 296 bool getYBinRange(View *v, int y, float &freqBinMin, float &freqBinMax) const;
Chris@0 297
Chris@44 298 bool getYBinSourceRange(View *v, int y, float &freqMin, float &freqMax) const;
Chris@44 299 bool getAdjustedYBinSourceRange(View *v, int x, int y,
Chris@35 300 float &freqMin, float &freqMax,
Chris@35 301 float &adjFreqMin, float &adjFreqMax) const;
Chris@44 302 bool getXBinSourceRange(View *v, int x, RealTime &timeMin, RealTime &timeMax) const;
Chris@44 303 bool getXYBinSourceRange(View *v, int x, int y, float &min, float &max,
Chris@38 304 float &phaseMin, float &phaseMax) const;
Chris@0 305
Chris@0 306 size_t getWindowIncrement() const {
Chris@97 307 if (m_windowHopLevel == 0) return m_windowSize;
Chris@97 308 else if (m_windowHopLevel == 1) return (m_windowSize * 3) / 4;
Chris@97 309 else return m_windowSize / (1 << (m_windowHopLevel - 1));
Chris@0 310 }
Chris@0 311 };
Chris@0 312
Chris@0 313 #endif