annotate layer/SpectrogramLayer.h @ 85:d31c4f5230d7

* Start factoring out the spectrogram's FFT cache into a separate set of classes that will permit a choice of disk or memory cache strategies
author Chris Cannam
date Tue, 02 May 2006 12:27:41 +0000
parents c683705adcbf
children 93a7efc75fb7
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@0 22 #include "model/PowerOfSqrtTwoZoomConstraint.h"
Chris@0 23 #include "model/DenseTimeValueModel.h"
Chris@0 24
Chris@0 25 #include <QThread>
Chris@0 26 #include <QMutex>
Chris@0 27 #include <QWaitCondition>
Chris@0 28
Chris@0 29 #include <fftw3.h>
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@85 36 class FFTCacheBase;
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@0 74 virtual PropertyType getPropertyType(const PropertyName &) const;
Chris@0 75 virtual QString getPropertyGroupName(const PropertyName &) const;
Chris@0 76 virtual int getPropertyRangeAndValue(const PropertyName &,
Chris@0 77 int *min, int *max) const;
Chris@0 78 virtual QString getPropertyValueLabel(const PropertyName &,
Chris@0 79 int value) const;
Chris@0 80 virtual void setProperty(const PropertyName &, int value);
Chris@0 81
Chris@0 82 /**
Chris@0 83 * Specify the channel to use from the source model.
Chris@0 84 * A value of -1 means to mix all available channels.
Chris@0 85 * The default is channel 0.
Chris@0 86 */
Chris@0 87 void setChannel(int);
Chris@0 88 int getChannel() const;
Chris@0 89
Chris@0 90 void setWindowSize(size_t);
Chris@0 91 size_t getWindowSize() const;
Chris@0 92
Chris@0 93 void setWindowOverlap(size_t percent);
Chris@0 94 size_t getWindowOverlap() const;
Chris@0 95
Chris@0 96 void setWindowType(WindowType type);
Chris@0 97 WindowType getWindowType() const;
Chris@0 98
Chris@0 99 /**
Chris@0 100 * Set the gain multiplier for sample values in this view prior to
Chris@0 101 * FFT calculation.
Chris@0 102 *
Chris@0 103 * The default is 1.0.
Chris@0 104 */
Chris@0 105 void setGain(float gain);
Chris@0 106 float getGain() const;
Chris@0 107
Chris@37 108 /**
Chris@37 109 * Set the threshold for sample values to be shown in the FFT,
Chris@37 110 * in voltage units.
Chris@37 111 *
Chris@37 112 * The default is 0.0.
Chris@37 113 */
Chris@37 114 void setThreshold(float threshold);
Chris@37 115 float getThreshold() const;
Chris@37 116
Chris@37 117 void setMinFrequency(size_t);
Chris@37 118 size_t getMinFrequency() const;
Chris@37 119
Chris@0 120 void setMaxFrequency(size_t); // 0 -> no maximum
Chris@0 121 size_t getMaxFrequency() const;
Chris@0 122
Chris@37 123 enum ColourScale {
Chris@37 124 LinearColourScale,
Chris@37 125 MeterColourScale,
Chris@37 126 dBColourScale,
Chris@37 127 PhaseColourScale
Chris@37 128 };
Chris@0 129
Chris@0 130 /**
Chris@0 131 * Specify the scale for sample levels. See WaveformLayer for
Chris@0 132 * details of meter and dB scaling. The default is dBColourScale.
Chris@0 133 */
Chris@0 134 void setColourScale(ColourScale);
Chris@0 135 ColourScale getColourScale() const;
Chris@0 136
Chris@35 137 enum FrequencyScale {
Chris@35 138 LinearFrequencyScale,
Chris@35 139 LogFrequencyScale
Chris@35 140 };
Chris@0 141
Chris@0 142 /**
Chris@0 143 * Specify the scale for the y axis.
Chris@0 144 */
Chris@0 145 void setFrequencyScale(FrequencyScale);
Chris@0 146 FrequencyScale getFrequencyScale() const;
Chris@0 147
Chris@37 148 enum BinDisplay {
Chris@37 149 AllBins,
Chris@37 150 PeakBins,
Chris@37 151 PeakFrequencies
Chris@35 152 };
Chris@35 153
Chris@35 154 /**
Chris@35 155 * Specify the processing of frequency bins for the y axis.
Chris@35 156 */
Chris@37 157 void setBinDisplay(BinDisplay);
Chris@37 158 BinDisplay getBinDisplay() const;
Chris@35 159
Chris@36 160 void setNormalizeColumns(bool n);
Chris@36 161 bool getNormalizeColumns() const;
Chris@36 162
Chris@0 163 enum ColourScheme { DefaultColours, WhiteOnBlack, BlackOnWhite,
Chris@71 164 RedOnBlue, YellowOnBlack, BlueOnBlack, Rainbow };
Chris@0 165
Chris@0 166 void setColourScheme(ColourScheme scheme);
Chris@0 167 ColourScheme getColourScheme() const;
Chris@0 168
Chris@9 169 /**
Chris@9 170 * Specify the colourmap rotation for the colour scale.
Chris@9 171 */
Chris@9 172 void setColourRotation(int);
Chris@9 173 int getColourRotation() const;
Chris@9 174
Chris@0 175 virtual VerticalPosition getPreferredFrameCountPosition() const {
Chris@0 176 return PositionTop;
Chris@0 177 }
Chris@0 178
Chris@15 179 virtual bool isLayerOpaque() const { return true; }
Chris@15 180
Chris@44 181 float getYForFrequency(View *v, float frequency) const;
Chris@44 182 float getFrequencyForY(View *v, int y) const;
Chris@42 183
Chris@0 184 virtual int getCompletion() const;
Chris@0 185
Chris@79 186 virtual bool getValueExtents(float &min, float &max, QString &unit) const;
Chris@79 187
Chris@6 188 virtual QString toXmlString(QString indent = "",
Chris@6 189 QString extraAttributes = "") const;
Chris@6 190
Chris@11 191 void setProperties(const QXmlAttributes &attributes);
Chris@11 192
Chris@47 193 virtual void setLayerDormant(const View *v, bool dormant);
Chris@29 194
Chris@0 195 protected slots:
Chris@0 196 void cacheInvalid();
Chris@0 197 void cacheInvalid(size_t startFrame, size_t endFrame);
Chris@0 198
Chris@0 199 void fillTimerTimedOut();
Chris@0 200
Chris@0 201 protected:
Chris@0 202 const DenseTimeValueModel *m_model; // I do not own this
Chris@0 203
Chris@35 204 int m_channel;
Chris@35 205 size_t m_windowSize;
Chris@35 206 WindowType m_windowType;
Chris@35 207 size_t m_windowOverlap;
Chris@35 208 float m_gain;
Chris@37 209 float m_threshold;
Chris@35 210 int m_colourRotation;
Chris@37 211 size_t m_minFrequency;
Chris@35 212 size_t m_maxFrequency;
Chris@35 213 ColourScale m_colourScale;
Chris@35 214 ColourScheme m_colourScheme;
Chris@77 215 QColor m_crosshairColour;
Chris@35 216 FrequencyScale m_frequencyScale;
Chris@37 217 BinDisplay m_binDisplay;
Chris@36 218 bool m_normalizeColumns;
Chris@0 219
Chris@38 220 enum { NO_VALUE = 0 }; // colour index for unused pixels
Chris@38 221
Chris@85 222 FFTCacheBase *m_cache;
Chris@31 223 bool m_cacheInvalid;
Chris@31 224
Chris@0 225 class CacheFillThread : public QThread
Chris@0 226 {
Chris@0 227 public:
Chris@0 228 CacheFillThread(SpectrogramLayer &layer) :
Chris@0 229 m_layer(layer), m_fillExtent(0) { }
Chris@0 230
Chris@0 231 size_t getFillExtent() const { return m_fillExtent; }
Chris@0 232 size_t getFillCompletion() const { return m_fillCompletion; }
Chris@0 233 virtual void run();
Chris@0 234
Chris@0 235 protected:
Chris@0 236 SpectrogramLayer &m_layer;
Chris@0 237 size_t m_fillExtent;
Chris@0 238 size_t m_fillCompletion;
Chris@0 239 };
Chris@0 240
Chris@0 241 void fillCache();
Chris@0 242
Chris@0 243 mutable QPixmap *m_pixmapCache;
Chris@0 244 mutable bool m_pixmapCacheInvalid;
Chris@0 245 mutable long m_pixmapCacheStartFrame;
Chris@0 246 mutable size_t m_pixmapCacheZoomLevel;
Chris@0 247
Chris@0 248 QWaitCondition m_condition;
Chris@0 249 mutable QMutex m_mutex;
Chris@0 250
Chris@0 251 CacheFillThread *m_fillThread;
Chris@0 252 QTimer *m_updateTimer;
Chris@44 253 mutable size_t m_candidateFillStartFrame;
Chris@0 254 size_t m_lastFillExtent;
Chris@0 255 bool m_exiting;
Chris@0 256
Chris@0 257 void setCacheColourmap();
Chris@9 258 void rotateCacheColourmap(int distance);
Chris@0 259
Chris@38 260 void fillCacheColumn(int column,
Chris@0 261 double *inputBuffer,
Chris@0 262 fftw_complex *outputBuffer,
Chris@0 263 fftw_plan plan,
Chris@9 264 size_t windowSize,
Chris@9 265 size_t windowIncrement,
Chris@38 266 const Window<double> &windower)
Chris@0 267 const;
Chris@0 268
Chris@38 269 static float calculateFrequency(size_t bin,
Chris@38 270 size_t windowSize,
Chris@38 271 size_t windowIncrement,
Chris@38 272 size_t sampleRate,
Chris@38 273 float previousPhase,
Chris@38 274 float currentPhase,
Chris@38 275 bool &steadyState);
Chris@38 276
Chris@38 277 unsigned char getDisplayValue(float input) const;
Chris@40 278 float getInputForDisplayValue(unsigned char uc) const;
Chris@40 279
Chris@40 280 int getColourScaleWidth(QPainter &) const;
Chris@40 281
Chris@40 282 float getEffectiveMinFrequency() const;
Chris@40 283 float getEffectiveMaxFrequency() const;
Chris@38 284
Chris@0 285 struct LayerRange {
Chris@0 286 long startFrame;
Chris@0 287 int zoomLevel;
Chris@0 288 size_t modelStart;
Chris@0 289 size_t modelEnd;
Chris@0 290 };
Chris@44 291 bool getXBinRange(View *v, int x, float &windowMin, float &windowMax) const;
Chris@44 292 bool getYBinRange(View *v, int y, float &freqBinMin, float &freqBinMax) const;
Chris@0 293
Chris@44 294 bool getYBinSourceRange(View *v, int y, float &freqMin, float &freqMax) const;
Chris@44 295 bool getAdjustedYBinSourceRange(View *v, int x, int y,
Chris@35 296 float &freqMin, float &freqMax,
Chris@35 297 float &adjFreqMin, float &adjFreqMax) const;
Chris@44 298 bool getXBinSourceRange(View *v, int x, RealTime &timeMin, RealTime &timeMax) const;
Chris@44 299 bool getXYBinSourceRange(View *v, int x, int y, float &min, float &max,
Chris@38 300 float &phaseMin, float &phaseMax) const;
Chris@0 301
Chris@0 302 size_t getWindowIncrement() const {
Chris@0 303 return m_windowSize - m_windowSize * m_windowOverlap / 100;
Chris@0 304 }
Chris@0 305 };
Chris@0 306
Chris@0 307 #endif