annotate layer/SpectrogramLayer.h @ 95:1b3996a86cfa

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