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@152
|
53 FFTModel(const DenseTimeValueModel *model,
|
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@1580
|
63 int getWidth() const override;
|
Chris@1580
|
64 int getHeight() const override;
|
Chris@1580
|
65 float getValueAt(int x, int y) const override { return getMagnitudeAt(x, y); }
|
Chris@1580
|
66 bool isOK() const override { return m_model && m_model->isOK(); }
|
Chris@1580
|
67 sv_frame_t getStartFrame() const override { return 0; }
|
Chris@1725
|
68 sv_frame_t getTrueEndFrame() const override {
|
Chris@1038
|
69 return sv_frame_t(getWidth()) * getResolution() + getResolution();
|
Chris@152
|
70 }
|
Chris@1580
|
71 sv_samplerate_t getSampleRate() const override {
|
Chris@1090
|
72 return isOK() ? m_model->getSampleRate() : 0;
|
Chris@152
|
73 }
|
Chris@1580
|
74 int getResolution() const override { return m_windowIncrement; }
|
Chris@1090
|
75 virtual int getYBinCount() const { return getHeight(); }
|
Chris@1580
|
76 float getMinimumLevel() const override { return 0.f; } // Can't provide
|
Chris@1580
|
77 float getMaximumLevel() const override { return 1.f; } // Can't provide
|
Chris@1580
|
78 Column getColumn(int x) const override; // magnitudes
|
Chris@1200
|
79 virtual Column getPhases(int x) const;
|
Chris@1580
|
80 QString getBinName(int n) const override;
|
Chris@1580
|
81 bool shouldUseLogValueScale() const override { return true; }
|
Chris@1580
|
82 int getCompletion() const override {
|
Chris@1090
|
83 int c = 100;
|
Chris@1093
|
84 if (m_model) {
|
Chris@1093
|
85 if (m_model->isReady(&c)) return 100;
|
Chris@1093
|
86 }
|
Chris@1090
|
87 return c;
|
Chris@152
|
88 }
|
Chris@1090
|
89 virtual QString getError() const { return ""; } //!!!???
|
Chris@1090
|
90 virtual sv_frame_t getFillExtent() const { return getEndFrame(); }
|
Chris@1679
|
91 QString toDelimitedDataString(QString, DataExportOptions,
|
Chris@1679
|
92 sv_frame_t, sv_frame_t) const override {
|
Chris@1679
|
93 return "";
|
Chris@1679
|
94 }
|
Chris@152
|
95
|
Chris@1090
|
96 // FFTModel methods:
|
Chris@1090
|
97 //
|
Chris@1090
|
98 int getChannel() const { return m_channel; }
|
Chris@1090
|
99 WindowType getWindowType() const { return m_windowType; }
|
Chris@1090
|
100 int getWindowSize() const { return m_windowSize; }
|
Chris@1090
|
101 int getWindowIncrement() const { return m_windowIncrement; }
|
Chris@1090
|
102 int getFFTSize() const { return m_fftSize; }
|
Chris@1200
|
103
|
Chris@1200
|
104 //!!! review which of these are ever actually called
|
Chris@1090
|
105
|
Chris@1090
|
106 float getMagnitudeAt(int x, int y) const;
|
Chris@1090
|
107 float getMaximumMagnitudeAt(int x) const;
|
Chris@1090
|
108 float getPhaseAt(int x, int y) const;
|
Chris@1090
|
109 void getValuesAt(int x, int y, float &real, float &imaginary) const;
|
Chris@1090
|
110 bool getMagnitudesAt(int x, float *values, int minbin = 0, int count = 0) const;
|
Chris@1090
|
111 bool getPhasesAt(int x, float *values, int minbin = 0, int count = 0) const;
|
Chris@1090
|
112 bool getValuesAt(int x, float *reals, float *imaginaries, int minbin = 0, int count = 0) const;
|
Chris@478
|
113
|
Chris@275
|
114 /**
|
Chris@275
|
115 * Calculate an estimated frequency for a stable signal in this
|
Chris@275
|
116 * bin, using phase unwrapping. This will be completely wrong if
|
Chris@275
|
117 * the signal is not stable here.
|
Chris@275
|
118 */
|
Chris@1045
|
119 virtual bool estimateStableFrequency(int x, int y, double &frequency);
|
Chris@275
|
120
|
Chris@275
|
121 enum PeakPickType
|
Chris@275
|
122 {
|
Chris@275
|
123 AllPeaks, /// Any bin exceeding its immediate neighbours
|
Chris@275
|
124 MajorPeaks, /// Peaks picked using sliding median window
|
Chris@275
|
125 MajorPitchAdaptivePeaks /// Bigger window for higher frequencies
|
Chris@275
|
126 };
|
Chris@275
|
127
|
Chris@929
|
128 typedef std::set<int> PeakLocationSet; // bin
|
Chris@1045
|
129 typedef std::map<int, double> PeakSet; // bin -> freq
|
Chris@275
|
130
|
Chris@275
|
131 /**
|
Chris@275
|
132 * Return locations of peak bins in the range [ymin,ymax]. If
|
Chris@275
|
133 * ymax is zero, getHeight()-1 will be used.
|
Chris@275
|
134 */
|
Chris@929
|
135 virtual PeakLocationSet getPeaks(PeakPickType type, int x,
|
Chris@1191
|
136 int ymin = 0, int ymax = 0) const;
|
Chris@275
|
137
|
Chris@275
|
138 /**
|
Chris@275
|
139 * Return locations and estimated stable frequencies of peak bins.
|
Chris@275
|
140 */
|
Chris@929
|
141 virtual PeakSet getPeakFrequencies(PeakPickType type, int x,
|
Chris@1191
|
142 int ymin = 0, int ymax = 0) const;
|
Chris@273
|
143
|
Chris@1580
|
144 QString getTypeName() const override { return tr("FFT"); }
|
Chris@345
|
145
|
Chris@360
|
146 public slots:
|
Chris@360
|
147 void sourceModelAboutToBeDeleted();
|
Chris@360
|
148
|
Chris@152
|
149 private:
|
Chris@297
|
150 FFTModel(const FFTModel &); // not implemented
|
Chris@152
|
151 FFTModel &operator=(const FFTModel &); // not implemented
|
Chris@152
|
152
|
Chris@1090
|
153 const DenseTimeValueModel *m_model;
|
Chris@1090
|
154 int m_channel;
|
Chris@1090
|
155 WindowType m_windowType;
|
Chris@1090
|
156 int m_windowSize;
|
Chris@1090
|
157 int m_windowIncrement;
|
Chris@1090
|
158 int m_fftSize;
|
Chris@1090
|
159 Window<float> m_windower;
|
Chris@1270
|
160 mutable breakfastquay::FFT m_fft;
|
Chris@1090
|
161
|
Chris@1040
|
162 int getPeakPickWindowSize(PeakPickType type, sv_samplerate_t sampleRate,
|
Chris@1576
|
163 int bin, double &dist) const;
|
Chris@1091
|
164
|
Chris@1091
|
165 std::pair<sv_frame_t, sv_frame_t> getSourceSampleRange(int column) const {
|
Chris@1091
|
166 sv_frame_t startFrame = m_windowIncrement * sv_frame_t(column);
|
Chris@1091
|
167 sv_frame_t endFrame = startFrame + m_windowSize;
|
Chris@1091
|
168 // Cols are centred on the audio sample (e.g. col 0 is centred at sample 0)
|
Chris@1091
|
169 startFrame -= m_windowSize / 2;
|
Chris@1091
|
170 endFrame -= m_windowSize / 2;
|
Chris@1091
|
171 return { startFrame, endFrame };
|
Chris@1091
|
172 }
|
Chris@1091
|
173
|
Chris@1326
|
174 typedef std::vector<float, breakfastquay::StlAllocator<float>> fvec;
|
Chris@1326
|
175 typedef std::vector<std::complex<float>,
|
Chris@1326
|
176 breakfastquay::StlAllocator<std::complex<float>>> cvec;
|
Chris@1326
|
177
|
Chris@1371
|
178 const cvec &getFFTColumn(int column) const; // returns ref for immediate use only
|
Chris@1326
|
179 fvec getSourceSamples(int column) const;
|
Chris@1326
|
180 fvec getSourceData(std::pair<sv_frame_t, sv_frame_t>) const;
|
Chris@1326
|
181 fvec getSourceDataUncached(std::pair<sv_frame_t, sv_frame_t>) const;
|
Chris@1093
|
182
|
Chris@1094
|
183 struct SavedSourceData {
|
Chris@1094
|
184 std::pair<sv_frame_t, sv_frame_t> range;
|
Chris@1326
|
185 fvec data;
|
Chris@1094
|
186 };
|
Chris@1094
|
187 mutable SavedSourceData m_savedData;
|
Chris@1371
|
188
|
Chris@1093
|
189 struct SavedColumn {
|
Chris@1093
|
190 int n;
|
Chris@1326
|
191 cvec col;
|
Chris@1093
|
192 };
|
Chris@1371
|
193 mutable std::vector<SavedColumn> m_cached;
|
Chris@1371
|
194 mutable size_t m_cacheWriteIndex;
|
Chris@1093
|
195 size_t m_cacheSize;
|
Chris@152
|
196 };
|
Chris@152
|
197
|
Chris@152
|
198 #endif
|