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@1091
|
24 #include "data/fft/FFTapi.h"
|
Chris@1091
|
25
|
Chris@275
|
26 #include <set>
|
Chris@275
|
27 #include <map>
|
Chris@1091
|
28 #include <vector>
|
Chris@1091
|
29 #include <complex>
|
Chris@275
|
30
|
Chris@254
|
31 /**
|
Chris@254
|
32 * An implementation of DenseThreeDimensionalModel that makes FFT data
|
Chris@387
|
33 * derived from a DenseTimeValueModel available as a generic data
|
Chris@1090
|
34 * grid.
|
Chris@254
|
35 */
|
Chris@152
|
36 class FFTModel : public DenseThreeDimensionalModel
|
Chris@152
|
37 {
|
Chris@247
|
38 Q_OBJECT
|
Chris@247
|
39
|
Chris@152
|
40 public:
|
Chris@254
|
41 /**
|
Chris@254
|
42 * Construct an FFT model derived from the given
|
Chris@254
|
43 * DenseTimeValueModel, with the given window parameters and FFT
|
Chris@254
|
44 * size (which may exceed the window size, for zero-padded FFTs).
|
Chris@254
|
45 *
|
Chris@254
|
46 * If the model has multiple channels use only the given channel,
|
Chris@254
|
47 * unless the channel is -1 in which case merge all available
|
Chris@254
|
48 * channels.
|
Chris@254
|
49 */
|
Chris@152
|
50 FFTModel(const DenseTimeValueModel *model,
|
Chris@152
|
51 int channel,
|
Chris@152
|
52 WindowType windowType,
|
Chris@929
|
53 int windowSize,
|
Chris@929
|
54 int windowIncrement,
|
Chris@1090
|
55 int fftSize);
|
Chris@152
|
56 ~FFTModel();
|
Chris@152
|
57
|
Chris@152
|
58 // DenseThreeDimensionalModel and Model methods:
|
Chris@152
|
59 //
|
Chris@1090
|
60 virtual int getWidth() const;
|
Chris@1090
|
61 virtual int getHeight() const;
|
Chris@1090
|
62 virtual float getValueAt(int x, int y) const { return getMagnitudeAt(x, y); }
|
Chris@1090
|
63 virtual bool isOK() const { return m_model && m_model->isOK(); }
|
Chris@1090
|
64 virtual sv_frame_t getStartFrame() const { return 0; }
|
Chris@1038
|
65 virtual sv_frame_t getEndFrame() const {
|
Chris@1038
|
66 return sv_frame_t(getWidth()) * getResolution() + getResolution();
|
Chris@152
|
67 }
|
Chris@1090
|
68 virtual sv_samplerate_t getSampleRate() const {
|
Chris@1090
|
69 return isOK() ? m_model->getSampleRate() : 0;
|
Chris@152
|
70 }
|
Chris@1090
|
71 virtual int getResolution() const { return m_windowIncrement; }
|
Chris@1090
|
72 virtual int getYBinCount() const { return getHeight(); }
|
Chris@1090
|
73 virtual float getMinimumLevel() const { return 0.f; } // Can't provide
|
Chris@1090
|
74 virtual float getMaximumLevel() const { return 1.f; } // Can't provide
|
Chris@1090
|
75 virtual Column getColumn(int x) const; // magnitudes
|
Chris@1090
|
76 virtual QString getBinName(int n) const;
|
Chris@1090
|
77 virtual bool shouldUseLogValueScale() const { return true; }
|
Chris@1090
|
78 virtual int getCompletion() const {
|
Chris@1090
|
79 int c = 100;
|
Chris@1090
|
80 if (m_model) (void)m_model->isReady(&c);
|
Chris@1090
|
81 return c;
|
Chris@152
|
82 }
|
Chris@1090
|
83 virtual QString getError() const { return ""; } //!!!???
|
Chris@1090
|
84 virtual sv_frame_t getFillExtent() const { return getEndFrame(); }
|
Chris@152
|
85
|
Chris@1090
|
86 // FFTModel methods:
|
Chris@1090
|
87 //
|
Chris@1090
|
88 int getChannel() const { return m_channel; }
|
Chris@1090
|
89 WindowType getWindowType() const { return m_windowType; }
|
Chris@1090
|
90 int getWindowSize() const { return m_windowSize; }
|
Chris@1090
|
91 int getWindowIncrement() const { return m_windowIncrement; }
|
Chris@1090
|
92 int getFFTSize() const { return m_fftSize; }
|
Chris@1090
|
93
|
Chris@1090
|
94 float getMagnitudeAt(int x, int y) const;
|
Chris@1090
|
95 float getMaximumMagnitudeAt(int x) const;
|
Chris@1090
|
96 float getPhaseAt(int x, int y) const;
|
Chris@1090
|
97 void getValuesAt(int x, int y, float &real, float &imaginary) const;
|
Chris@1090
|
98 bool isColumnAvailable(int x) const;
|
Chris@1090
|
99 bool getMagnitudesAt(int x, float *values, int minbin = 0, int count = 0) const;
|
Chris@1090
|
100 bool getNormalizedMagnitudesAt(int x, float *values, int minbin = 0, int count = 0) const;
|
Chris@1090
|
101 bool getPhasesAt(int x, float *values, int minbin = 0, int count = 0) const;
|
Chris@1090
|
102 bool getValuesAt(int x, float *reals, float *imaginaries, int minbin = 0, int count = 0) const;
|
Chris@478
|
103
|
Chris@275
|
104 /**
|
Chris@275
|
105 * Calculate an estimated frequency for a stable signal in this
|
Chris@275
|
106 * bin, using phase unwrapping. This will be completely wrong if
|
Chris@275
|
107 * the signal is not stable here.
|
Chris@275
|
108 */
|
Chris@1045
|
109 virtual bool estimateStableFrequency(int x, int y, double &frequency);
|
Chris@275
|
110
|
Chris@275
|
111 enum PeakPickType
|
Chris@275
|
112 {
|
Chris@275
|
113 AllPeaks, /// Any bin exceeding its immediate neighbours
|
Chris@275
|
114 MajorPeaks, /// Peaks picked using sliding median window
|
Chris@275
|
115 MajorPitchAdaptivePeaks /// Bigger window for higher frequencies
|
Chris@275
|
116 };
|
Chris@275
|
117
|
Chris@929
|
118 typedef std::set<int> PeakLocationSet; // bin
|
Chris@1045
|
119 typedef std::map<int, double> PeakSet; // bin -> freq
|
Chris@275
|
120
|
Chris@275
|
121 /**
|
Chris@275
|
122 * Return locations of peak bins in the range [ymin,ymax]. If
|
Chris@275
|
123 * ymax is zero, getHeight()-1 will be used.
|
Chris@275
|
124 */
|
Chris@929
|
125 virtual PeakLocationSet getPeaks(PeakPickType type, int x,
|
Chris@929
|
126 int ymin = 0, int ymax = 0);
|
Chris@275
|
127
|
Chris@275
|
128 /**
|
Chris@275
|
129 * Return locations and estimated stable frequencies of peak bins.
|
Chris@275
|
130 */
|
Chris@929
|
131 virtual PeakSet getPeakFrequencies(PeakPickType type, int x,
|
Chris@929
|
132 int ymin = 0, int ymax = 0);
|
Chris@273
|
133
|
Chris@345
|
134 QString getTypeName() const { return tr("FFT"); }
|
Chris@345
|
135
|
Chris@360
|
136 public slots:
|
Chris@360
|
137 void sourceModelAboutToBeDeleted();
|
Chris@360
|
138
|
Chris@152
|
139 private:
|
Chris@297
|
140 FFTModel(const FFTModel &); // not implemented
|
Chris@152
|
141 FFTModel &operator=(const FFTModel &); // not implemented
|
Chris@152
|
142
|
Chris@1090
|
143 const DenseTimeValueModel *m_model;
|
Chris@1090
|
144 int m_channel;
|
Chris@1090
|
145 WindowType m_windowType;
|
Chris@1090
|
146 int m_windowSize;
|
Chris@1090
|
147 int m_windowIncrement;
|
Chris@1090
|
148 int m_fftSize;
|
Chris@1090
|
149 Window<float> m_windower;
|
Chris@1091
|
150 FFTForward m_fft;
|
Chris@1090
|
151
|
Chris@1040
|
152 int getPeakPickWindowSize(PeakPickType type, sv_samplerate_t sampleRate,
|
Chris@1040
|
153 int bin, float &percentile) const;
|
Chris@1091
|
154
|
Chris@1091
|
155 std::pair<sv_frame_t, sv_frame_t> getSourceSampleRange(int column) const {
|
Chris@1091
|
156 sv_frame_t startFrame = m_windowIncrement * sv_frame_t(column);
|
Chris@1091
|
157 sv_frame_t endFrame = startFrame + m_windowSize;
|
Chris@1091
|
158 // Cols are centred on the audio sample (e.g. col 0 is centred at sample 0)
|
Chris@1091
|
159 startFrame -= m_windowSize / 2;
|
Chris@1091
|
160 endFrame -= m_windowSize / 2;
|
Chris@1091
|
161 return { startFrame, endFrame };
|
Chris@1091
|
162 }
|
Chris@1091
|
163
|
Chris@1091
|
164 std::vector<std::complex<float> > getFFTColumn(int column) const;
|
Chris@1091
|
165 std::vector<float> getSourceSamples(int column) const;
|
Chris@152
|
166 };
|
Chris@152
|
167
|
Chris@152
|
168 #endif
|