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@0
|
21 #include "model/PowerOfSqrtTwoZoomConstraint.h"
|
Chris@0
|
22 #include "model/DenseTimeValueModel.h"
|
Chris@0
|
23
|
Chris@0
|
24 #include <QThread>
|
Chris@0
|
25 #include <QMutex>
|
Chris@0
|
26 #include <QWaitCondition>
|
Chris@0
|
27
|
Chris@0
|
28 #include <fftw3.h>
|
Chris@0
|
29
|
Chris@38
|
30 #include <stdint.h>
|
Chris@38
|
31
|
Chris@0
|
32 class View;
|
Chris@0
|
33 class QPainter;
|
Chris@0
|
34 class QImage;
|
Chris@0
|
35 class QPixmap;
|
Chris@0
|
36 class QTimer;
|
Chris@0
|
37 class RealTime;
|
Chris@0
|
38
|
Chris@0
|
39 /**
|
Chris@0
|
40 * SpectrogramLayer represents waveform data (obtained from a
|
Chris@0
|
41 * DenseTimeValueModel) in spectrogram form.
|
Chris@0
|
42 */
|
Chris@0
|
43
|
Chris@0
|
44 class SpectrogramLayer : public Layer,
|
Chris@31
|
45 public PowerOfSqrtTwoZoomConstraint
|
Chris@0
|
46 {
|
Chris@0
|
47 Q_OBJECT
|
Chris@0
|
48
|
Chris@0
|
49 public:
|
Chris@37
|
50 enum Configuration { FullRangeDb, MelodicRange, MelodicPeaks };
|
Chris@0
|
51
|
Chris@44
|
52 SpectrogramLayer(Configuration = FullRangeDb);
|
Chris@0
|
53 ~SpectrogramLayer();
|
Chris@0
|
54
|
Chris@0
|
55 virtual const ZoomConstraint *getZoomConstraint() const { return this; }
|
Chris@0
|
56 virtual const Model *getModel() const { return m_model; }
|
Chris@44
|
57 virtual void paint(View *v, QPainter &paint, QRect rect) const;
|
Chris@0
|
58
|
Chris@44
|
59 virtual int getVerticalScaleWidth(View *v, QPainter &) const;
|
Chris@44
|
60 virtual void paintVerticalScale(View *v, QPainter &paint, QRect rect) const;
|
Chris@0
|
61
|
Chris@44
|
62 virtual QString getFeatureDescription(View *v, QPoint &) const;
|
Chris@0
|
63
|
Chris@44
|
64 virtual bool snapToFeatureFrame(View *v, int &frame,
|
Chris@28
|
65 size_t &resolution,
|
Chris@28
|
66 SnapType snap) const;
|
Chris@13
|
67
|
Chris@0
|
68 void setModel(const DenseTimeValueModel *model);
|
Chris@0
|
69
|
Chris@0
|
70 virtual PropertyList getProperties() const;
|
Chris@0
|
71 virtual PropertyType getPropertyType(const PropertyName &) const;
|
Chris@0
|
72 virtual QString getPropertyGroupName(const PropertyName &) const;
|
Chris@0
|
73 virtual int getPropertyRangeAndValue(const PropertyName &,
|
Chris@0
|
74 int *min, int *max) const;
|
Chris@0
|
75 virtual QString getPropertyValueLabel(const PropertyName &,
|
Chris@0
|
76 int value) const;
|
Chris@0
|
77 virtual void setProperty(const PropertyName &, int value);
|
Chris@0
|
78
|
Chris@0
|
79 /**
|
Chris@0
|
80 * Specify the channel to use from the source model.
|
Chris@0
|
81 * A value of -1 means to mix all available channels.
|
Chris@0
|
82 * The default is channel 0.
|
Chris@0
|
83 */
|
Chris@0
|
84 void setChannel(int);
|
Chris@0
|
85 int getChannel() const;
|
Chris@0
|
86
|
Chris@0
|
87 void setWindowSize(size_t);
|
Chris@0
|
88 size_t getWindowSize() const;
|
Chris@0
|
89
|
Chris@0
|
90 void setWindowOverlap(size_t percent);
|
Chris@0
|
91 size_t getWindowOverlap() const;
|
Chris@0
|
92
|
Chris@0
|
93 void setWindowType(WindowType type);
|
Chris@0
|
94 WindowType getWindowType() const;
|
Chris@0
|
95
|
Chris@0
|
96 /**
|
Chris@0
|
97 * Set the gain multiplier for sample values in this view prior to
|
Chris@0
|
98 * FFT calculation.
|
Chris@0
|
99 *
|
Chris@0
|
100 * The default is 1.0.
|
Chris@0
|
101 */
|
Chris@0
|
102 void setGain(float gain);
|
Chris@0
|
103 float getGain() const;
|
Chris@0
|
104
|
Chris@37
|
105 /**
|
Chris@37
|
106 * Set the threshold for sample values to be shown in the FFT,
|
Chris@37
|
107 * in voltage units.
|
Chris@37
|
108 *
|
Chris@37
|
109 * The default is 0.0.
|
Chris@37
|
110 */
|
Chris@37
|
111 void setThreshold(float threshold);
|
Chris@37
|
112 float getThreshold() const;
|
Chris@37
|
113
|
Chris@37
|
114 void setMinFrequency(size_t);
|
Chris@37
|
115 size_t getMinFrequency() const;
|
Chris@37
|
116
|
Chris@0
|
117 void setMaxFrequency(size_t); // 0 -> no maximum
|
Chris@0
|
118 size_t getMaxFrequency() const;
|
Chris@0
|
119
|
Chris@37
|
120 enum ColourScale {
|
Chris@37
|
121 LinearColourScale,
|
Chris@37
|
122 MeterColourScale,
|
Chris@37
|
123 dBColourScale,
|
Chris@37
|
124 PhaseColourScale
|
Chris@37
|
125 };
|
Chris@0
|
126
|
Chris@0
|
127 /**
|
Chris@0
|
128 * Specify the scale for sample levels. See WaveformLayer for
|
Chris@0
|
129 * details of meter and dB scaling. The default is dBColourScale.
|
Chris@0
|
130 */
|
Chris@0
|
131 void setColourScale(ColourScale);
|
Chris@0
|
132 ColourScale getColourScale() const;
|
Chris@0
|
133
|
Chris@35
|
134 enum FrequencyScale {
|
Chris@35
|
135 LinearFrequencyScale,
|
Chris@35
|
136 LogFrequencyScale
|
Chris@35
|
137 };
|
Chris@0
|
138
|
Chris@0
|
139 /**
|
Chris@0
|
140 * Specify the scale for the y axis.
|
Chris@0
|
141 */
|
Chris@0
|
142 void setFrequencyScale(FrequencyScale);
|
Chris@0
|
143 FrequencyScale getFrequencyScale() const;
|
Chris@0
|
144
|
Chris@37
|
145 enum BinDisplay {
|
Chris@37
|
146 AllBins,
|
Chris@37
|
147 PeakBins,
|
Chris@37
|
148 PeakFrequencies
|
Chris@35
|
149 };
|
Chris@35
|
150
|
Chris@35
|
151 /**
|
Chris@35
|
152 * Specify the processing of frequency bins for the y axis.
|
Chris@35
|
153 */
|
Chris@37
|
154 void setBinDisplay(BinDisplay);
|
Chris@37
|
155 BinDisplay getBinDisplay() const;
|
Chris@35
|
156
|
Chris@36
|
157 void setNormalizeColumns(bool n);
|
Chris@36
|
158 bool getNormalizeColumns() const;
|
Chris@36
|
159
|
Chris@0
|
160 enum ColourScheme { DefaultColours, WhiteOnBlack, BlackOnWhite,
|
Chris@40
|
161 RedOnBlue, YellowOnBlack, Rainbow };
|
Chris@0
|
162
|
Chris@0
|
163 void setColourScheme(ColourScheme scheme);
|
Chris@0
|
164 ColourScheme getColourScheme() const;
|
Chris@0
|
165
|
Chris@9
|
166 /**
|
Chris@9
|
167 * Specify the colourmap rotation for the colour scale.
|
Chris@9
|
168 */
|
Chris@9
|
169 void setColourRotation(int);
|
Chris@9
|
170 int getColourRotation() const;
|
Chris@9
|
171
|
Chris@0
|
172 virtual VerticalPosition getPreferredFrameCountPosition() const {
|
Chris@0
|
173 return PositionTop;
|
Chris@0
|
174 }
|
Chris@0
|
175
|
Chris@15
|
176 virtual bool isLayerOpaque() const { return true; }
|
Chris@15
|
177
|
Chris@44
|
178 float getYForFrequency(View *v, float frequency) const;
|
Chris@44
|
179 float getFrequencyForY(View *v, int y) const;
|
Chris@42
|
180
|
Chris@0
|
181 virtual int getCompletion() const;
|
Chris@0
|
182
|
Chris@6
|
183 virtual QString toXmlString(QString indent = "",
|
Chris@6
|
184 QString extraAttributes = "") const;
|
Chris@6
|
185
|
Chris@11
|
186 void setProperties(const QXmlAttributes &attributes);
|
Chris@11
|
187
|
Chris@47
|
188 virtual void setLayerDormant(const View *v, bool dormant);
|
Chris@29
|
189
|
Chris@0
|
190 protected slots:
|
Chris@0
|
191 void cacheInvalid();
|
Chris@0
|
192 void cacheInvalid(size_t startFrame, size_t endFrame);
|
Chris@0
|
193
|
Chris@0
|
194 void fillTimerTimedOut();
|
Chris@0
|
195
|
Chris@0
|
196 protected:
|
Chris@0
|
197 const DenseTimeValueModel *m_model; // I do not own this
|
Chris@0
|
198
|
Chris@35
|
199 int m_channel;
|
Chris@35
|
200 size_t m_windowSize;
|
Chris@35
|
201 WindowType m_windowType;
|
Chris@35
|
202 size_t m_windowOverlap;
|
Chris@35
|
203 float m_gain;
|
Chris@37
|
204 float m_threshold;
|
Chris@35
|
205 int m_colourRotation;
|
Chris@37
|
206 size_t m_minFrequency;
|
Chris@35
|
207 size_t m_maxFrequency;
|
Chris@35
|
208 ColourScale m_colourScale;
|
Chris@35
|
209 ColourScheme m_colourScheme;
|
Chris@35
|
210 FrequencyScale m_frequencyScale;
|
Chris@37
|
211 BinDisplay m_binDisplay;
|
Chris@36
|
212 bool m_normalizeColumns;
|
Chris@0
|
213
|
Chris@38
|
214 // At the moment we cache one unsigned char per bin for the
|
Chris@38
|
215 // magnitude -- which is nothing like precise enough to allow us
|
Chris@38
|
216 // to subsequently adjust gain etc without recalculating the
|
Chris@38
|
217 // cached values -- plus optionally one unsigned char per bin for
|
Chris@38
|
218 // phase-adjusted frequency.
|
Chris@37
|
219
|
Chris@38
|
220 // To speed up redrawing after parameter changes, we would like to
|
Chris@38
|
221 // cache magnitude in a way that can have gain applied afterwards
|
Chris@38
|
222 // and can determine whether something is a peak or not, and also
|
Chris@38
|
223 // cache phase rather than only phase-adjusted frequency so that
|
Chris@38
|
224 // we don't have to recalculate if switching between phase and
|
Chris@38
|
225 // magnitude displays.
|
Chris@38
|
226
|
Chris@38
|
227 // This implies probably 16 bits for a normalized magnitude (in
|
Chris@38
|
228 // dB?) and at most 16 bits for phase. 16 or 32 bits per bin
|
Chris@38
|
229 // instead of 8 or 16.
|
Chris@38
|
230
|
Chris@38
|
231 // Each column's magnitudes are expected to be stored normalized
|
Chris@38
|
232 // to [0,1] with respect to the column, so the normalization
|
Chris@38
|
233 // factor should be calculated before all values in a column, and
|
Chris@38
|
234 // set appropriately.
|
Chris@38
|
235
|
Chris@31
|
236 class Cache {
|
Chris@31
|
237 public:
|
Chris@38
|
238 Cache(); // of size zero, call resize() before using
|
Chris@31
|
239 ~Cache();
|
Chris@31
|
240
|
Chris@38
|
241 size_t getWidth() const { return m_width; }
|
Chris@38
|
242 size_t getHeight() const { return m_height; }
|
Chris@38
|
243
|
Chris@38
|
244 void resize(size_t width, size_t height);
|
Chris@38
|
245 void reset(); // zero-fill or 1-fill as appropriate without changing size
|
Chris@38
|
246
|
Chris@38
|
247 float getMagnitudeAt(size_t x, size_t y) const {
|
Chris@38
|
248 return getNormalizedMagnitudeAt(x, y) * m_factor[x];
|
Chris@38
|
249 }
|
Chris@35
|
250
|
Chris@38
|
251 float getNormalizedMagnitudeAt(size_t x, size_t y) const {
|
Chris@44
|
252 return float(m_magnitude[x][y]) / 65535.0;
|
Chris@38
|
253 }
|
Chris@31
|
254
|
Chris@38
|
255 float getPhaseAt(size_t x, size_t y) const {
|
Chris@44
|
256 int16_t i = (int16_t)m_phase[x][y];
|
Chris@39
|
257 return (float(i) / 32767.0) * M_PI;
|
Chris@38
|
258 }
|
Chris@31
|
259
|
Chris@38
|
260 bool isLocalPeak(size_t x, size_t y) const {
|
Chris@44
|
261 if (y > 0 && m_magnitude[x][y] < m_magnitude[x][y-1]) return false;
|
Chris@44
|
262 if (y < m_height-1 && m_magnitude[x][y] < m_magnitude[x][y+1]) return false;
|
Chris@38
|
263 return true;
|
Chris@38
|
264 }
|
Chris@31
|
265
|
Chris@38
|
266 bool isOverThreshold(size_t x, size_t y, float threshold) const {
|
Chris@38
|
267 if (threshold == 0.0) return true;
|
Chris@38
|
268 return getMagnitudeAt(x, y) > threshold;
|
Chris@38
|
269 }
|
Chris@38
|
270
|
Chris@38
|
271 void setNormalizationFactor(size_t x, float factor) {
|
Chris@41
|
272 if (x < m_width) m_factor[x] = factor;
|
Chris@38
|
273 }
|
Chris@38
|
274
|
Chris@38
|
275 void setMagnitudeAt(size_t x, size_t y, float mag) {
|
Chris@38
|
276 // norm factor must already be set
|
Chris@38
|
277 setNormalizedMagnitudeAt(x, y, mag / m_factor[x]);
|
Chris@38
|
278 }
|
Chris@38
|
279
|
Chris@38
|
280 void setNormalizedMagnitudeAt(size_t x, size_t y, float norm) {
|
Chris@41
|
281 if (x < m_width && y < m_height) {
|
Chris@44
|
282 m_magnitude[x][y] = uint16_t(norm * 65535.0);
|
Chris@41
|
283 }
|
Chris@38
|
284 }
|
Chris@38
|
285
|
Chris@38
|
286 void setPhaseAt(size_t x, size_t y, float phase) {
|
Chris@38
|
287 // phase in range -pi -> pi
|
Chris@41
|
288 if (x < m_width && y < m_height) {
|
Chris@44
|
289 m_phase[x][y] = uint16_t(int16_t((phase * 32767) / M_PI));
|
Chris@41
|
290 }
|
Chris@38
|
291 }
|
Chris@38
|
292
|
Chris@38
|
293 QColor getColour(unsigned char index) const {
|
Chris@38
|
294 return m_colours[index];
|
Chris@38
|
295 }
|
Chris@38
|
296
|
Chris@38
|
297 void setColour(unsigned char index, QColor colour) {
|
Chris@38
|
298 m_colours[index] = colour;
|
Chris@38
|
299 }
|
Chris@38
|
300
|
Chris@38
|
301 private:
|
Chris@31
|
302 size_t m_width;
|
Chris@31
|
303 size_t m_height;
|
Chris@38
|
304 uint16_t **m_magnitude;
|
Chris@38
|
305 uint16_t **m_phase;
|
Chris@38
|
306 float *m_factor;
|
Chris@31
|
307 QColor m_colours[256];
|
Chris@38
|
308
|
Chris@38
|
309 void resize(uint16_t **&, size_t, size_t);
|
Chris@31
|
310 };
|
Chris@38
|
311
|
Chris@38
|
312 enum { NO_VALUE = 0 }; // colour index for unused pixels
|
Chris@38
|
313
|
Chris@31
|
314 Cache *m_cache;
|
Chris@31
|
315 bool m_cacheInvalid;
|
Chris@31
|
316
|
Chris@0
|
317 class CacheFillThread : public QThread
|
Chris@0
|
318 {
|
Chris@0
|
319 public:
|
Chris@0
|
320 CacheFillThread(SpectrogramLayer &layer) :
|
Chris@0
|
321 m_layer(layer), m_fillExtent(0) { }
|
Chris@0
|
322
|
Chris@0
|
323 size_t getFillExtent() const { return m_fillExtent; }
|
Chris@0
|
324 size_t getFillCompletion() const { return m_fillCompletion; }
|
Chris@0
|
325 virtual void run();
|
Chris@0
|
326
|
Chris@0
|
327 protected:
|
Chris@0
|
328 SpectrogramLayer &m_layer;
|
Chris@0
|
329 size_t m_fillExtent;
|
Chris@0
|
330 size_t m_fillCompletion;
|
Chris@0
|
331 };
|
Chris@0
|
332
|
Chris@0
|
333 void fillCache();
|
Chris@0
|
334
|
Chris@0
|
335 mutable QPixmap *m_pixmapCache;
|
Chris@0
|
336 mutable bool m_pixmapCacheInvalid;
|
Chris@0
|
337 mutable long m_pixmapCacheStartFrame;
|
Chris@0
|
338 mutable size_t m_pixmapCacheZoomLevel;
|
Chris@0
|
339
|
Chris@0
|
340 QWaitCondition m_condition;
|
Chris@0
|
341 mutable QMutex m_mutex;
|
Chris@0
|
342
|
Chris@0
|
343 CacheFillThread *m_fillThread;
|
Chris@0
|
344 QTimer *m_updateTimer;
|
Chris@44
|
345 mutable size_t m_candidateFillStartFrame;
|
Chris@0
|
346 size_t m_lastFillExtent;
|
Chris@0
|
347 bool m_exiting;
|
Chris@0
|
348
|
Chris@0
|
349 void setCacheColourmap();
|
Chris@9
|
350 void rotateCacheColourmap(int distance);
|
Chris@0
|
351
|
Chris@38
|
352 void fillCacheColumn(int column,
|
Chris@0
|
353 double *inputBuffer,
|
Chris@0
|
354 fftw_complex *outputBuffer,
|
Chris@0
|
355 fftw_plan plan,
|
Chris@9
|
356 size_t windowSize,
|
Chris@9
|
357 size_t windowIncrement,
|
Chris@38
|
358 const Window<double> &windower)
|
Chris@0
|
359 const;
|
Chris@0
|
360
|
Chris@38
|
361 static float calculateFrequency(size_t bin,
|
Chris@38
|
362 size_t windowSize,
|
Chris@38
|
363 size_t windowIncrement,
|
Chris@38
|
364 size_t sampleRate,
|
Chris@38
|
365 float previousPhase,
|
Chris@38
|
366 float currentPhase,
|
Chris@38
|
367 bool &steadyState);
|
Chris@38
|
368
|
Chris@38
|
369 unsigned char getDisplayValue(float input) const;
|
Chris@40
|
370 float getInputForDisplayValue(unsigned char uc) const;
|
Chris@40
|
371
|
Chris@40
|
372 int getColourScaleWidth(QPainter &) const;
|
Chris@40
|
373
|
Chris@40
|
374 float getEffectiveMinFrequency() const;
|
Chris@40
|
375 float getEffectiveMaxFrequency() const;
|
Chris@38
|
376
|
Chris@0
|
377 struct LayerRange {
|
Chris@0
|
378 long startFrame;
|
Chris@0
|
379 int zoomLevel;
|
Chris@0
|
380 size_t modelStart;
|
Chris@0
|
381 size_t modelEnd;
|
Chris@0
|
382 };
|
Chris@44
|
383 bool getXBinRange(View *v, int x, float &windowMin, float &windowMax) const;
|
Chris@44
|
384 bool getYBinRange(View *v, int y, float &freqBinMin, float &freqBinMax) const;
|
Chris@0
|
385
|
Chris@44
|
386 bool getYBinSourceRange(View *v, int y, float &freqMin, float &freqMax) const;
|
Chris@44
|
387 bool getAdjustedYBinSourceRange(View *v, int x, int y,
|
Chris@35
|
388 float &freqMin, float &freqMax,
|
Chris@35
|
389 float &adjFreqMin, float &adjFreqMax) const;
|
Chris@44
|
390 bool getXBinSourceRange(View *v, int x, RealTime &timeMin, RealTime &timeMax) const;
|
Chris@44
|
391 bool getXYBinSourceRange(View *v, int x, int y, float &min, float &max,
|
Chris@38
|
392 float &phaseMin, float &phaseMax) const;
|
Chris@0
|
393
|
Chris@0
|
394 size_t getWindowIncrement() const {
|
Chris@0
|
395 return m_windowSize - m_windowSize * m_windowOverlap / 100;
|
Chris@0
|
396 }
|
Chris@0
|
397 };
|
Chris@0
|
398
|
Chris@0
|
399 #endif
|