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@152
|
16 #include "FFTModel.h"
|
Chris@152
|
17 #include "DenseTimeValueModel.h"
|
Chris@152
|
18
|
Chris@183
|
19 #include "base/Profiler.h"
|
Chris@275
|
20 #include "base/Pitch.h"
|
Chris@183
|
21
|
Chris@152
|
22 #include <cassert>
|
Chris@152
|
23
|
Chris@152
|
24 FFTModel::FFTModel(const DenseTimeValueModel *model,
|
Chris@152
|
25 int channel,
|
Chris@152
|
26 WindowType windowType,
|
Chris@152
|
27 size_t windowSize,
|
Chris@152
|
28 size_t windowIncrement,
|
Chris@152
|
29 size_t fftSize,
|
Chris@152
|
30 bool polar,
|
Chris@152
|
31 size_t fillFromColumn) :
|
Chris@152
|
32 //!!! ZoomConstraint!
|
Chris@152
|
33 m_server(0),
|
Chris@152
|
34 m_xshift(0),
|
Chris@152
|
35 m_yshift(0)
|
Chris@152
|
36 {
|
Chris@152
|
37 m_server = FFTDataServer::getFuzzyInstance(model,
|
Chris@152
|
38 channel,
|
Chris@152
|
39 windowType,
|
Chris@152
|
40 windowSize,
|
Chris@152
|
41 windowIncrement,
|
Chris@152
|
42 fftSize,
|
Chris@152
|
43 polar,
|
Chris@152
|
44 fillFromColumn);
|
Chris@152
|
45
|
Chris@200
|
46 if (!m_server) return; // caller should check isOK()
|
Chris@200
|
47
|
Chris@152
|
48 size_t xratio = windowIncrement / m_server->getWindowIncrement();
|
Chris@152
|
49 size_t yratio = m_server->getFFTSize() / fftSize;
|
Chris@152
|
50
|
Chris@152
|
51 while (xratio > 1) {
|
Chris@152
|
52 if (xratio & 0x1) {
|
Chris@152
|
53 std::cerr << "ERROR: FFTModel: Window increment ratio "
|
Chris@152
|
54 << windowIncrement << " / "
|
Chris@152
|
55 << m_server->getWindowIncrement()
|
Chris@152
|
56 << " must be a power of two" << std::endl;
|
Chris@152
|
57 assert(!(xratio & 0x1));
|
Chris@152
|
58 }
|
Chris@152
|
59 ++m_xshift;
|
Chris@152
|
60 xratio >>= 1;
|
Chris@152
|
61 }
|
Chris@152
|
62
|
Chris@152
|
63 while (yratio > 1) {
|
Chris@152
|
64 if (yratio & 0x1) {
|
Chris@152
|
65 std::cerr << "ERROR: FFTModel: FFT size ratio "
|
Chris@152
|
66 << m_server->getFFTSize() << " / " << fftSize
|
Chris@152
|
67 << " must be a power of two" << std::endl;
|
Chris@152
|
68 assert(!(yratio & 0x1));
|
Chris@152
|
69 }
|
Chris@152
|
70 ++m_yshift;
|
Chris@152
|
71 yratio >>= 1;
|
Chris@152
|
72 }
|
Chris@152
|
73 }
|
Chris@152
|
74
|
Chris@152
|
75 FFTModel::~FFTModel()
|
Chris@152
|
76 {
|
Chris@200
|
77 if (m_server) FFTDataServer::releaseInstance(m_server);
|
Chris@152
|
78 }
|
Chris@152
|
79
|
Chris@152
|
80 size_t
|
Chris@152
|
81 FFTModel::getSampleRate() const
|
Chris@152
|
82 {
|
Chris@152
|
83 return isOK() ? m_server->getModel()->getSampleRate() : 0;
|
Chris@152
|
84 }
|
Chris@152
|
85
|
Chris@152
|
86 void
|
Chris@182
|
87 FFTModel::getColumn(size_t x, Column &result) const
|
Chris@152
|
88 {
|
Chris@183
|
89 Profiler profiler("FFTModel::getColumn", false);
|
Chris@183
|
90
|
Chris@152
|
91 result.clear();
|
Chris@152
|
92 size_t height(getHeight());
|
Chris@152
|
93 for (size_t y = 0; y < height; ++y) {
|
Chris@152
|
94 result.push_back(const_cast<FFTModel *>(this)->getMagnitudeAt(x, y));
|
Chris@152
|
95 }
|
Chris@152
|
96 }
|
Chris@152
|
97
|
Chris@152
|
98 QString
|
Chris@152
|
99 FFTModel::getBinName(size_t n) const
|
Chris@152
|
100 {
|
Chris@152
|
101 size_t sr = getSampleRate();
|
Chris@152
|
102 if (!sr) return "";
|
Chris@204
|
103 QString name = tr("%1 Hz").arg((n * sr) / ((getHeight()-1) * 2));
|
Chris@152
|
104 return name;
|
Chris@152
|
105 }
|
Chris@152
|
106
|
Chris@275
|
107 bool
|
Chris@275
|
108 FFTModel::estimateStableFrequency(size_t x, size_t y, float &frequency)
|
Chris@275
|
109 {
|
Chris@275
|
110 if (!isOK()) return false;
|
Chris@275
|
111
|
Chris@275
|
112 size_t sampleRate = m_server->getModel()->getSampleRate();
|
Chris@275
|
113
|
Chris@275
|
114 size_t fftSize = m_server->getFFTSize() >> m_yshift;
|
Chris@275
|
115 frequency = (float(y) * sampleRate) / fftSize;
|
Chris@275
|
116
|
Chris@275
|
117 if (x+1 >= getWidth()) return false;
|
Chris@275
|
118
|
Chris@275
|
119 // At frequency f, a phase shift of 2pi (one cycle) happens in 1/f sec.
|
Chris@275
|
120 // At hopsize h and sample rate sr, one hop happens in h/sr sec.
|
Chris@275
|
121 // At window size w, for bin b, f is b*sr/w.
|
Chris@275
|
122 // thus 2pi phase shift happens in w/(b*sr) sec.
|
Chris@275
|
123 // We need to know what phase shift we expect from h/sr sec.
|
Chris@275
|
124 // -> 2pi * ((h/sr) / (w/(b*sr)))
|
Chris@275
|
125 // = 2pi * ((h * b * sr) / (w * sr))
|
Chris@275
|
126 // = 2pi * (h * b) / w.
|
Chris@275
|
127
|
Chris@275
|
128 float oldPhase = getPhaseAt(x, y);
|
Chris@275
|
129 float newPhase = getPhaseAt(x+1, y);
|
Chris@275
|
130
|
Chris@275
|
131 size_t incr = getResolution();
|
Chris@275
|
132
|
Chris@275
|
133 float expectedPhase = oldPhase + (2.0 * M_PI * y * incr) / fftSize;
|
Chris@275
|
134
|
Chris@275
|
135 float phaseError = princargf(newPhase - expectedPhase);
|
Chris@275
|
136
|
Chris@275
|
137 // bool stable = (fabsf(phaseError) < (1.1f * (m_windowIncrement * M_PI) / m_fftSize));
|
Chris@275
|
138
|
Chris@275
|
139 // The new frequency estimate based on the phase error resulting
|
Chris@275
|
140 // from assuming the "native" frequency of this bin
|
Chris@275
|
141
|
Chris@275
|
142 frequency =
|
Chris@275
|
143 (sampleRate * (expectedPhase + phaseError - oldPhase)) /
|
Chris@275
|
144 (2 * M_PI * incr);
|
Chris@275
|
145
|
Chris@275
|
146 return true;
|
Chris@275
|
147 }
|
Chris@275
|
148
|
Chris@275
|
149 FFTModel::PeakLocationSet
|
Chris@275
|
150 FFTModel::getPeaks(PeakPickType type, size_t x, size_t ymin, size_t ymax)
|
Chris@275
|
151 {
|
Chris@275
|
152 FFTModel::PeakLocationSet peaks;
|
Chris@275
|
153 if (!isOK()) return peaks;
|
Chris@275
|
154
|
Chris@275
|
155 if (ymax == 0 || ymax > getHeight() - 1) {
|
Chris@275
|
156 ymax = getHeight() - 1;
|
Chris@275
|
157 }
|
Chris@275
|
158
|
Chris@275
|
159 Column values;
|
Chris@275
|
160
|
Chris@275
|
161 if (type == AllPeaks) {
|
Chris@275
|
162 for (size_t y = ymin; y <= ymax; ++y) {
|
Chris@275
|
163 values.push_back(getMagnitudeAt(x, y));
|
Chris@275
|
164 }
|
Chris@275
|
165 size_t i = 0;
|
Chris@275
|
166 for (size_t bin = ymin; bin <= ymax; ++bin) {
|
Chris@275
|
167 if ((i == 0 || values[i] > values[i-1]) &&
|
Chris@275
|
168 (i == values.size()-1 || values[i] >= values[i+1])) {
|
Chris@275
|
169 peaks.insert(bin);
|
Chris@275
|
170 }
|
Chris@275
|
171 ++i;
|
Chris@275
|
172 }
|
Chris@275
|
173 return peaks;
|
Chris@275
|
174 }
|
Chris@275
|
175
|
Chris@275
|
176 getColumn(x, values);
|
Chris@275
|
177
|
Chris@275
|
178 // For peak picking we use a moving median window, picking the
|
Chris@275
|
179 // highest value within each continuous region of values that
|
Chris@275
|
180 // exceed the median. For pitch adaptivity, we adjust the window
|
Chris@275
|
181 // size to a roughly constant pitch range (about four tones).
|
Chris@275
|
182
|
Chris@275
|
183 size_t sampleRate = getSampleRate();
|
Chris@275
|
184
|
Chris@275
|
185 std::deque<float> window;
|
Chris@275
|
186 std::vector<size_t> inrange;
|
Chris@275
|
187 size_t medianWinSize = getPeakPickWindowSize(type, sampleRate, ymin);
|
Chris@275
|
188 size_t halfWin = medianWinSize/2;
|
Chris@275
|
189
|
Chris@275
|
190 size_t binmin;
|
Chris@275
|
191 if (ymin > halfWin) binmin = ymin - halfWin;
|
Chris@275
|
192 else binmin = 0;
|
Chris@275
|
193
|
Chris@275
|
194 size_t binmax;
|
Chris@275
|
195 if (ymax + halfWin < values.size()) binmax = ymax + halfWin;
|
Chris@275
|
196 else binmax = values.size()-1;
|
Chris@275
|
197
|
Chris@275
|
198 for (size_t bin = binmin; bin <= binmax; ++bin) {
|
Chris@275
|
199
|
Chris@275
|
200 float value = values[bin];
|
Chris@275
|
201
|
Chris@275
|
202 window.push_back(value);
|
Chris@275
|
203
|
Chris@275
|
204 medianWinSize = getPeakPickWindowSize(type, sampleRate, bin);
|
Chris@275
|
205 halfWin = medianWinSize/2;
|
Chris@275
|
206
|
Chris@275
|
207 while (window.size() > medianWinSize) window.pop_front();
|
Chris@275
|
208
|
Chris@275
|
209 if (type == MajorPitchAdaptivePeaks) {
|
Chris@275
|
210 if (ymax + halfWin < values.size()) binmax = ymax + halfWin;
|
Chris@275
|
211 else binmax = values.size()-1;
|
Chris@275
|
212 }
|
Chris@275
|
213
|
Chris@275
|
214 std::deque<float> sorted(window);
|
Chris@275
|
215 std::sort(sorted.begin(), sorted.end());
|
Chris@275
|
216 float median = sorted[sorted.size()/2];
|
Chris@275
|
217
|
Chris@275
|
218 if (value > median) {
|
Chris@275
|
219 inrange.push_back(bin);
|
Chris@275
|
220 }
|
Chris@275
|
221
|
Chris@275
|
222 if (value <= median || bin+1 == values.size()) {
|
Chris@275
|
223 size_t peakbin = 0;
|
Chris@275
|
224 float peakval = 0.f;
|
Chris@275
|
225 if (!inrange.empty()) {
|
Chris@275
|
226 for (size_t i = 0; i < inrange.size(); ++i) {
|
Chris@275
|
227 if (i == 0 || values[inrange[i]] > peakval) {
|
Chris@275
|
228 peakval = values[inrange[i]];
|
Chris@275
|
229 peakbin = inrange[i];
|
Chris@275
|
230 }
|
Chris@275
|
231 }
|
Chris@275
|
232 inrange.clear();
|
Chris@275
|
233 if (peakbin >= ymin && peakbin <= ymax) {
|
Chris@275
|
234 peaks.insert(peakbin);
|
Chris@275
|
235 }
|
Chris@275
|
236 }
|
Chris@275
|
237 }
|
Chris@275
|
238 }
|
Chris@275
|
239
|
Chris@275
|
240 return peaks;
|
Chris@275
|
241 }
|
Chris@275
|
242
|
Chris@275
|
243 size_t
|
Chris@275
|
244 FFTModel::getPeakPickWindowSize(PeakPickType type, size_t sampleRate, size_t bin) const
|
Chris@275
|
245 {
|
Chris@275
|
246 if (type == MajorPeaks) return 10;
|
Chris@275
|
247 if (bin == 0) return 3;
|
Chris@275
|
248 size_t fftSize = m_server->getFFTSize() >> m_yshift;
|
Chris@275
|
249 float binfreq = (sampleRate * bin) / fftSize;
|
Chris@275
|
250 float hifreq = Pitch::getFrequencyForPitch(73, 0, binfreq);
|
Chris@275
|
251 int hibin = lrintf((hifreq * fftSize) / sampleRate);
|
Chris@275
|
252 int medianWinSize = hibin - bin;
|
Chris@275
|
253 if (medianWinSize < 3) medianWinSize = 3;
|
Chris@275
|
254 return medianWinSize;
|
Chris@275
|
255 }
|
Chris@275
|
256
|
Chris@275
|
257 FFTModel::PeakSet
|
Chris@275
|
258 FFTModel::getPeakFrequencies(PeakPickType type, size_t x,
|
Chris@275
|
259 size_t ymin, size_t ymax)
|
Chris@275
|
260 {
|
Chris@275
|
261 PeakSet peaks;
|
Chris@275
|
262 if (!isOK()) return peaks;
|
Chris@275
|
263 PeakLocationSet locations = getPeaks(type, x, ymin, ymax);
|
Chris@275
|
264
|
Chris@275
|
265 size_t sampleRate = getSampleRate();
|
Chris@275
|
266 size_t fftSize = m_server->getFFTSize() >> m_yshift;
|
Chris@275
|
267 size_t incr = getResolution();
|
Chris@275
|
268
|
Chris@275
|
269 // This duplicates some of the work of estimateStableFrequency to
|
Chris@275
|
270 // allow us to retrieve the phases in two separate vertical
|
Chris@275
|
271 // columns, instead of jumping back and forth between columns x and
|
Chris@275
|
272 // x+1, which may be significantly slower if re-seeking is needed
|
Chris@275
|
273
|
Chris@275
|
274 std::vector<float> phases;
|
Chris@275
|
275 for (PeakLocationSet::iterator i = locations.begin();
|
Chris@275
|
276 i != locations.end(); ++i) {
|
Chris@275
|
277 phases.push_back(getPhaseAt(x, *i));
|
Chris@275
|
278 }
|
Chris@275
|
279
|
Chris@275
|
280 size_t phaseIndex = 0;
|
Chris@275
|
281 for (PeakLocationSet::iterator i = locations.begin();
|
Chris@275
|
282 i != locations.end(); ++i) {
|
Chris@275
|
283 float oldPhase = phases[phaseIndex];
|
Chris@275
|
284 float newPhase = getPhaseAt(x+1, *i);
|
Chris@275
|
285 float expectedPhase = oldPhase + (2.0 * M_PI * *i * incr) / fftSize;
|
Chris@275
|
286 float phaseError = princargf(newPhase - expectedPhase);
|
Chris@275
|
287 float frequency =
|
Chris@275
|
288 (sampleRate * (expectedPhase + phaseError - oldPhase))
|
Chris@275
|
289 / (2 * M_PI * incr);
|
Chris@275
|
290 // bool stable = (fabsf(phaseError) < (1.1f * (incr * M_PI) / fftSize));
|
Chris@275
|
291 // if (stable)
|
Chris@275
|
292 peaks[*i] = frequency;
|
Chris@275
|
293 ++phaseIndex;
|
Chris@275
|
294 }
|
Chris@275
|
295
|
Chris@275
|
296 return peaks;
|
Chris@275
|
297 }
|
Chris@275
|
298
|
Chris@152
|
299 Model *
|
Chris@152
|
300 FFTModel::clone() const
|
Chris@152
|
301 {
|
Chris@152
|
302 return new FFTModel(*this);
|
Chris@152
|
303 }
|
Chris@152
|
304
|
Chris@152
|
305 FFTModel::FFTModel(const FFTModel &model) :
|
Chris@152
|
306 DenseThreeDimensionalModel(),
|
Chris@152
|
307 m_server(model.m_server),
|
Chris@152
|
308 m_xshift(model.m_xshift),
|
Chris@152
|
309 m_yshift(model.m_yshift)
|
Chris@152
|
310 {
|
Chris@152
|
311 FFTDataServer::claimInstance(m_server);
|
Chris@152
|
312 }
|
Chris@152
|
313
|