annotate layer/SpectrogramLayer.h @ 109:12340cb6e6cb

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