Chris@152
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
Chris@152
|
2
|
Chris@152
|
3 /*
|
Chris@152
|
4 Sonic Visualiser
|
Chris@152
|
5 An audio file viewer and annotation editor.
|
Chris@152
|
6 Centre for Digital Music, Queen Mary, University of London.
|
Chris@152
|
7 This file copyright 2006 Chris Cannam.
|
Chris@152
|
8
|
Chris@152
|
9 This program is free software; you can redistribute it and/or
|
Chris@152
|
10 modify it under the terms of the GNU General Public License as
|
Chris@152
|
11 published by the Free Software Foundation; either version 2 of the
|
Chris@152
|
12 License, or (at your option) any later version. See the file
|
Chris@152
|
13 COPYING included with this distribution for more information.
|
Chris@152
|
14 */
|
Chris@152
|
15
|
Chris@1086
|
16 #ifndef FFT_MODEL_H
|
Chris@1086
|
17 #define FFT_MODEL_H
|
Chris@152
|
18
|
Chris@152
|
19 #include "DenseThreeDimensionalModel.h"
|
Chris@1090
|
20 #include "DenseTimeValueModel.h"
|
Chris@1090
|
21
|
Chris@1090
|
22 #include "base/Window.h"
|
Chris@152
|
23
|
Chris@275
|
24 #include <set>
|
Chris@275
|
25 #include <map>
|
Chris@275
|
26
|
Chris@254
|
27 /**
|
Chris@254
|
28 * An implementation of DenseThreeDimensionalModel that makes FFT data
|
Chris@387
|
29 * derived from a DenseTimeValueModel available as a generic data
|
Chris@1090
|
30 * grid.
|
Chris@254
|
31 */
|
Chris@152
|
32 class FFTModel : public DenseThreeDimensionalModel
|
Chris@152
|
33 {
|
Chris@247
|
34 Q_OBJECT
|
Chris@247
|
35
|
Chris@152
|
36 public:
|
Chris@254
|
37 /**
|
Chris@254
|
38 * Construct an FFT model derived from the given
|
Chris@254
|
39 * DenseTimeValueModel, with the given window parameters and FFT
|
Chris@254
|
40 * size (which may exceed the window size, for zero-padded FFTs).
|
Chris@254
|
41 *
|
Chris@254
|
42 * If the model has multiple channels use only the given channel,
|
Chris@254
|
43 * unless the channel is -1 in which case merge all available
|
Chris@254
|
44 * channels.
|
Chris@254
|
45 */
|
Chris@152
|
46 FFTModel(const DenseTimeValueModel *model,
|
Chris@152
|
47 int channel,
|
Chris@152
|
48 WindowType windowType,
|
Chris@929
|
49 int windowSize,
|
Chris@929
|
50 int windowIncrement,
|
Chris@1090
|
51 int fftSize);
|
Chris@152
|
52 ~FFTModel();
|
Chris@152
|
53
|
Chris@152
|
54 // DenseThreeDimensionalModel and Model methods:
|
Chris@152
|
55 //
|
Chris@1090
|
56 virtual int getWidth() const;
|
Chris@1090
|
57 virtual int getHeight() const;
|
Chris@1090
|
58 virtual float getValueAt(int x, int y) const { return getMagnitudeAt(x, y); }
|
Chris@1090
|
59 virtual bool isOK() const { return m_model && m_model->isOK(); }
|
Chris@1090
|
60 virtual sv_frame_t getStartFrame() const { return 0; }
|
Chris@1038
|
61 virtual sv_frame_t getEndFrame() const {
|
Chris@1038
|
62 return sv_frame_t(getWidth()) * getResolution() + getResolution();
|
Chris@152
|
63 }
|
Chris@1090
|
64 virtual sv_samplerate_t getSampleRate() const {
|
Chris@1090
|
65 return isOK() ? m_model->getSampleRate() : 0;
|
Chris@152
|
66 }
|
Chris@1090
|
67 virtual int getResolution() const { return m_windowIncrement; }
|
Chris@1090
|
68 virtual int getYBinCount() const { return getHeight(); }
|
Chris@1090
|
69 virtual float getMinimumLevel() const { return 0.f; } // Can't provide
|
Chris@1090
|
70 virtual float getMaximumLevel() const { return 1.f; } // Can't provide
|
Chris@1090
|
71 virtual Column getColumn(int x) const; // magnitudes
|
Chris@1090
|
72 virtual QString getBinName(int n) const;
|
Chris@1090
|
73 virtual bool shouldUseLogValueScale() const { return true; }
|
Chris@1090
|
74 virtual int getCompletion() const {
|
Chris@1090
|
75 int c = 100;
|
Chris@1090
|
76 if (m_model) (void)m_model->isReady(&c);
|
Chris@1090
|
77 return c;
|
Chris@152
|
78 }
|
Chris@1090
|
79 virtual QString getError() const { return ""; } //!!!???
|
Chris@1090
|
80 virtual sv_frame_t getFillExtent() const { return getEndFrame(); }
|
Chris@152
|
81
|
Chris@1090
|
82 // FFTModel methods:
|
Chris@1090
|
83 //
|
Chris@1090
|
84 int getChannel() const { return m_channel; }
|
Chris@1090
|
85 WindowType getWindowType() const { return m_windowType; }
|
Chris@1090
|
86 int getWindowSize() const { return m_windowSize; }
|
Chris@1090
|
87 int getWindowIncrement() const { return m_windowIncrement; }
|
Chris@1090
|
88 int getFFTSize() const { return m_fftSize; }
|
Chris@1090
|
89
|
Chris@1090
|
90 float getMagnitudeAt(int x, int y) const;
|
Chris@1090
|
91 float getNormalizedMagnitudeAt(int x, int y) const;
|
Chris@1090
|
92 float getMaximumMagnitudeAt(int x) const;
|
Chris@1090
|
93 float getPhaseAt(int x, int y) const;
|
Chris@1090
|
94 void getValuesAt(int x, int y, float &real, float &imaginary) const;
|
Chris@1090
|
95 bool isColumnAvailable(int x) const;
|
Chris@1090
|
96 bool getMagnitudesAt(int x, float *values, int minbin = 0, int count = 0) const;
|
Chris@1090
|
97 bool getNormalizedMagnitudesAt(int x, float *values, int minbin = 0, int count = 0) const;
|
Chris@1090
|
98 bool getPhasesAt(int x, float *values, int minbin = 0, int count = 0) const;
|
Chris@1090
|
99 bool getValuesAt(int x, float *reals, float *imaginaries, int minbin = 0, int count = 0) const;
|
Chris@478
|
100
|
Chris@275
|
101 /**
|
Chris@275
|
102 * Calculate an estimated frequency for a stable signal in this
|
Chris@275
|
103 * bin, using phase unwrapping. This will be completely wrong if
|
Chris@275
|
104 * the signal is not stable here.
|
Chris@275
|
105 */
|
Chris@1045
|
106 virtual bool estimateStableFrequency(int x, int y, double &frequency);
|
Chris@275
|
107
|
Chris@275
|
108 enum PeakPickType
|
Chris@275
|
109 {
|
Chris@275
|
110 AllPeaks, /// Any bin exceeding its immediate neighbours
|
Chris@275
|
111 MajorPeaks, /// Peaks picked using sliding median window
|
Chris@275
|
112 MajorPitchAdaptivePeaks /// Bigger window for higher frequencies
|
Chris@275
|
113 };
|
Chris@275
|
114
|
Chris@929
|
115 typedef std::set<int> PeakLocationSet; // bin
|
Chris@1045
|
116 typedef std::map<int, double> PeakSet; // bin -> freq
|
Chris@275
|
117
|
Chris@275
|
118 /**
|
Chris@275
|
119 * Return locations of peak bins in the range [ymin,ymax]. If
|
Chris@275
|
120 * ymax is zero, getHeight()-1 will be used.
|
Chris@275
|
121 */
|
Chris@929
|
122 virtual PeakLocationSet getPeaks(PeakPickType type, int x,
|
Chris@929
|
123 int ymin = 0, int ymax = 0);
|
Chris@275
|
124
|
Chris@275
|
125 /**
|
Chris@275
|
126 * Return locations and estimated stable frequencies of peak bins.
|
Chris@275
|
127 */
|
Chris@929
|
128 virtual PeakSet getPeakFrequencies(PeakPickType type, int x,
|
Chris@929
|
129 int ymin = 0, int ymax = 0);
|
Chris@273
|
130
|
Chris@345
|
131 QString getTypeName() const { return tr("FFT"); }
|
Chris@345
|
132
|
Chris@360
|
133 public slots:
|
Chris@360
|
134 void sourceModelAboutToBeDeleted();
|
Chris@360
|
135
|
Chris@152
|
136 private:
|
Chris@297
|
137 FFTModel(const FFTModel &); // not implemented
|
Chris@152
|
138 FFTModel &operator=(const FFTModel &); // not implemented
|
Chris@152
|
139
|
Chris@1090
|
140 const DenseTimeValueModel *m_model;
|
Chris@1090
|
141 int m_channel;
|
Chris@1090
|
142 WindowType m_windowType;
|
Chris@1090
|
143 int m_windowSize;
|
Chris@1090
|
144 int m_windowIncrement;
|
Chris@1090
|
145 int m_fftSize;
|
Chris@1090
|
146 Window<float> m_windower;
|
Chris@1090
|
147
|
Chris@1040
|
148 int getPeakPickWindowSize(PeakPickType type, sv_samplerate_t sampleRate,
|
Chris@1040
|
149 int bin, float &percentile) const;
|
Chris@152
|
150 };
|
Chris@152
|
151
|
Chris@152
|
152 #endif
|