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@1270
|
24 #include <bqfft/FFT.h>
|
Chris@1326
|
25 #include <bqvec/Allocators.h>
|
Chris@1091
|
26
|
Chris@275
|
27 #include <set>
|
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@1092
|
40 //!!! threading requirements?
|
Chris@1092
|
41 //!!! doubles? since we're not caching much
|
Chris@1092
|
42
|
Chris@152
|
43 public:
|
Chris@254
|
44 /**
|
Chris@254
|
45 * Construct an FFT model derived from the given
|
Chris@254
|
46 * DenseTimeValueModel, with the given window parameters and FFT
|
Chris@254
|
47 * size (which may exceed the window size, for zero-padded FFTs).
|
Chris@254
|
48 *
|
Chris@254
|
49 * If the model has multiple channels use only the given channel,
|
Chris@254
|
50 * unless the channel is -1 in which case merge all available
|
Chris@254
|
51 * channels.
|
Chris@254
|
52 */
|
Chris@1744
|
53 FFTModel(ModelId model, // a DenseTimeValueModel
|
Chris@152
|
54 int channel,
|
Chris@152
|
55 WindowType windowType,
|
Chris@929
|
56 int windowSize,
|
Chris@929
|
57 int windowIncrement,
|
Chris@1090
|
58 int fftSize);
|
Chris@152
|
59 ~FFTModel();
|
Chris@152
|
60
|
Chris@152
|
61 // DenseThreeDimensionalModel and Model methods:
|
Chris@152
|
62 //
|
Chris@1790
|
63 bool isOK() const override;
|
Chris@1790
|
64 int getCompletion() const override;
|
Chris@1790
|
65
|
Chris@1580
|
66 int getWidth() const override;
|
Chris@1580
|
67 int getHeight() const override;
|
Chris@1790
|
68
|
Chris@1790
|
69 float getValueAt(int x, int y) const override {
|
Chris@1790
|
70 return getMagnitudeAt(x, y);
|
Chris@1790
|
71 }
|
Chris@1790
|
72 sv_frame_t getStartFrame() const override {
|
Chris@1790
|
73 return 0;
|
Chris@1790
|
74 }
|
Chris@1725
|
75 sv_frame_t getTrueEndFrame() const override {
|
Chris@1038
|
76 return sv_frame_t(getWidth()) * getResolution() + getResolution();
|
Chris@152
|
77 }
|
Chris@1790
|
78 sv_samplerate_t getSampleRate() const override {
|
Chris@1790
|
79 return m_sampleRate;
|
Chris@1790
|
80 }
|
Chris@1790
|
81 int getResolution() const override {
|
Chris@1790
|
82 return m_windowIncrement;
|
Chris@1790
|
83 }
|
Chris@1790
|
84
|
Chris@1580
|
85 float getMinimumLevel() const override { return 0.f; } // Can't provide
|
Chris@1580
|
86 float getMaximumLevel() const override { return 1.f; } // Can't provide
|
Chris@1790
|
87
|
Chris@1580
|
88 Column getColumn(int x) const override; // magnitudes
|
Chris@1790
|
89
|
Chris@1790
|
90 bool hasBinValues() const override {
|
Chris@1790
|
91 return true;
|
Chris@1790
|
92 }
|
Chris@1790
|
93 QString getBinValueUnit() const override {
|
Chris@1790
|
94 return "Hz";
|
Chris@1790
|
95 }
|
Chris@1790
|
96 bool shouldUseLogValueScale() const override {
|
Chris@1790
|
97 return true;
|
Chris@1790
|
98 }
|
Chris@1790
|
99 float getBinValue(int n) const override;
|
Chris@1580
|
100 QString getBinName(int n) const override;
|
Chris@1790
|
101
|
Chris@1815
|
102 QString getDelimitedDataHeaderLine(QString, DataExportOptions) const override {
|
Chris@1815
|
103 return "";
|
Chris@1815
|
104 }
|
Chris@1679
|
105 QString toDelimitedDataString(QString, DataExportOptions,
|
Chris@1679
|
106 sv_frame_t, sv_frame_t) const override {
|
Chris@1679
|
107 return "";
|
Chris@1679
|
108 }
|
Chris@152
|
109
|
Chris@1090
|
110 // FFTModel methods:
|
Chris@1090
|
111 //
|
Chris@1790
|
112 QString getError() const { return m_error; }
|
Chris@1790
|
113
|
Chris@1090
|
114 int getChannel() const { return m_channel; }
|
Chris@1090
|
115 WindowType getWindowType() const { return m_windowType; }
|
Chris@1090
|
116 int getWindowSize() const { return m_windowSize; }
|
Chris@1090
|
117 int getWindowIncrement() const { return m_windowIncrement; }
|
Chris@1090
|
118 int getFFTSize() const { return m_fftSize; }
|
Chris@1200
|
119
|
Chris@1780
|
120 void setMaximumFrequency(double freq);
|
Chris@1780
|
121 double getMaximumFrequency() const { return m_maximumFrequency; }
|
Chris@1780
|
122
|
Chris@1200
|
123 //!!! review which of these are ever actually called
|
Chris@1090
|
124
|
Chris@1090
|
125 float getMagnitudeAt(int x, int y) const;
|
Chris@1090
|
126 float getMaximumMagnitudeAt(int x) const;
|
Chris@1790
|
127 Column getPhases(int x) const;
|
Chris@1090
|
128 float getPhaseAt(int x, int y) const;
|
Chris@1090
|
129 void getValuesAt(int x, int y, float &real, float &imaginary) const;
|
Chris@1090
|
130 bool getMagnitudesAt(int x, float *values, int minbin = 0, int count = 0) const;
|
Chris@1090
|
131 bool getPhasesAt(int x, float *values, int minbin = 0, int count = 0) const;
|
Chris@1090
|
132 bool getValuesAt(int x, float *reals, float *imaginaries, int minbin = 0, int count = 0) const;
|
Chris@478
|
133
|
Chris@275
|
134 /**
|
Chris@275
|
135 * Calculate an estimated frequency for a stable signal in this
|
Chris@275
|
136 * bin, using phase unwrapping. This will be completely wrong if
|
Chris@275
|
137 * the signal is not stable here.
|
Chris@275
|
138 */
|
Chris@1045
|
139 virtual bool estimateStableFrequency(int x, int y, double &frequency);
|
Chris@275
|
140
|
Chris@275
|
141 enum PeakPickType
|
Chris@275
|
142 {
|
Chris@275
|
143 AllPeaks, /// Any bin exceeding its immediate neighbours
|
Chris@275
|
144 MajorPeaks, /// Peaks picked using sliding median window
|
Chris@275
|
145 MajorPitchAdaptivePeaks /// Bigger window for higher frequencies
|
Chris@275
|
146 };
|
Chris@275
|
147
|
Chris@929
|
148 typedef std::set<int> PeakLocationSet; // bin
|
Chris@1045
|
149 typedef std::map<int, double> PeakSet; // bin -> freq
|
Chris@275
|
150
|
Chris@275
|
151 /**
|
Chris@275
|
152 * Return locations of peak bins in the range [ymin,ymax]. If
|
Chris@275
|
153 * ymax is zero, getHeight()-1 will be used.
|
Chris@275
|
154 */
|
Chris@929
|
155 virtual PeakLocationSet getPeaks(PeakPickType type, int x,
|
Chris@1191
|
156 int ymin = 0, int ymax = 0) const;
|
Chris@275
|
157
|
Chris@275
|
158 /**
|
Chris@275
|
159 * Return locations and estimated stable frequencies of peak bins.
|
Chris@275
|
160 */
|
Chris@929
|
161 virtual PeakSet getPeakFrequencies(PeakPickType type, int x,
|
Chris@1191
|
162 int ymin = 0, int ymax = 0) const;
|
Chris@273
|
163
|
Chris@1580
|
164 QString getTypeName() const override { return tr("FFT"); }
|
Chris@345
|
165
|
Chris@1744
|
166 private:
|
Chris@1744
|
167 FFTModel(const FFTModel &) =delete;
|
Chris@1744
|
168 FFTModel &operator=(const FFTModel &) =delete;
|
Chris@360
|
169
|
Chris@1744
|
170 const ModelId m_model; // a DenseTimeValueModel
|
Chris@1780
|
171 sv_samplerate_t m_sampleRate;
|
Chris@1090
|
172 int m_channel;
|
Chris@1090
|
173 WindowType m_windowType;
|
Chris@1090
|
174 int m_windowSize;
|
Chris@1090
|
175 int m_windowIncrement;
|
Chris@1090
|
176 int m_fftSize;
|
Chris@1090
|
177 Window<float> m_windower;
|
Chris@1270
|
178 mutable breakfastquay::FFT m_fft;
|
Chris@1780
|
179 double m_maximumFrequency;
|
Chris@1784
|
180 mutable QString m_error;
|
Chris@1090
|
181
|
Chris@1040
|
182 int getPeakPickWindowSize(PeakPickType type, sv_samplerate_t sampleRate,
|
Chris@1576
|
183 int bin, double &dist) const;
|
Chris@1091
|
184
|
Chris@1091
|
185 std::pair<sv_frame_t, sv_frame_t> getSourceSampleRange(int column) const {
|
Chris@1091
|
186 sv_frame_t startFrame = m_windowIncrement * sv_frame_t(column);
|
Chris@1091
|
187 sv_frame_t endFrame = startFrame + m_windowSize;
|
Chris@1091
|
188 // Cols are centred on the audio sample (e.g. col 0 is centred at sample 0)
|
Chris@1091
|
189 startFrame -= m_windowSize / 2;
|
Chris@1091
|
190 endFrame -= m_windowSize / 2;
|
Chris@1091
|
191 return { startFrame, endFrame };
|
Chris@1091
|
192 }
|
Chris@1091
|
193
|
Chris@1326
|
194 typedef std::vector<float, breakfastquay::StlAllocator<float>> fvec;
|
Chris@1326
|
195 typedef std::vector<std::complex<float>,
|
Chris@1326
|
196 breakfastquay::StlAllocator<std::complex<float>>> cvec;
|
Chris@1780
|
197
|
Chris@1780
|
198 cvec getFFTColumn(int column) const;
|
Chris@1326
|
199 fvec getSourceSamples(int column) const;
|
Chris@1326
|
200 fvec getSourceData(std::pair<sv_frame_t, sv_frame_t>) const;
|
Chris@1326
|
201 fvec getSourceDataUncached(std::pair<sv_frame_t, sv_frame_t>) const;
|
Chris@1093
|
202
|
Chris@1094
|
203 struct SavedSourceData {
|
Chris@1094
|
204 std::pair<sv_frame_t, sv_frame_t> range;
|
Chris@1326
|
205 fvec data;
|
Chris@1094
|
206 };
|
Chris@1094
|
207 mutable SavedSourceData m_savedData;
|
Chris@1371
|
208
|
Chris@1093
|
209 struct SavedColumn {
|
Chris@1093
|
210 int n;
|
Chris@1326
|
211 cvec col;
|
Chris@1093
|
212 };
|
Chris@1371
|
213 mutable std::vector<SavedColumn> m_cached;
|
Chris@1371
|
214 mutable size_t m_cacheWriteIndex;
|
Chris@1093
|
215 size_t m_cacheSize;
|
Chris@152
|
216 };
|
Chris@152
|
217
|
Chris@152
|
218 #endif
|