Chris@133
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
Chris@133
|
2
|
Chris@133
|
3 /*
|
Chris@133
|
4 Sonic Visualiser
|
Chris@133
|
5 An audio file viewer and annotation editor.
|
Chris@133
|
6 Centre for Digital Music, Queen Mary, University of London.
|
Chris@195
|
7 This file copyright 2006-2007 QMUL.
|
Chris@133
|
8
|
Chris@133
|
9 This program is free software; you can redistribute it and/or
|
Chris@133
|
10 modify it under the terms of the GNU General Public License as
|
Chris@133
|
11 published by the Free Software Foundation; either version 2 of the
|
Chris@133
|
12 License, or (at your option) any later version. See the file
|
Chris@133
|
13 COPYING included with this distribution for more information.
|
Chris@133
|
14 */
|
Chris@133
|
15
|
Chris@133
|
16 #include "SpectrumLayer.h"
|
Chris@133
|
17
|
Chris@133
|
18 #include "data/model/FFTModel.h"
|
Chris@133
|
19 #include "view/View.h"
|
Chris@153
|
20 #include "base/AudioLevel.h"
|
Chris@153
|
21 #include "base/Preferences.h"
|
Chris@167
|
22 #include "base/RangeMapper.h"
|
Chris@277
|
23 #include "base/Pitch.h"
|
Chris@254
|
24 #include "ColourMapper.h"
|
Chris@254
|
25
|
Chris@254
|
26 #include <QPainter>
|
Chris@133
|
27
|
Chris@133
|
28 SpectrumLayer::SpectrumLayer() :
|
Chris@193
|
29 m_originModel(0),
|
Chris@153
|
30 m_channel(-1),
|
Chris@153
|
31 m_channelSet(false),
|
Chris@153
|
32 m_windowSize(1024),
|
Chris@153
|
33 m_windowType(HanningWindow),
|
Chris@275
|
34 m_windowHopLevel(2),
|
Chris@275
|
35 m_newFFTNeeded(true)
|
Chris@133
|
36 {
|
Chris@153
|
37 Preferences *prefs = Preferences::getInstance();
|
Chris@153
|
38 connect(prefs, SIGNAL(propertyChanged(PropertyContainer::PropertyName)),
|
Chris@153
|
39 this, SLOT(preferenceChanged(PropertyContainer::PropertyName)));
|
Chris@153
|
40 setWindowType(prefs->getWindowType());
|
Chris@195
|
41
|
Chris@195
|
42 setBinScale(LogBins);
|
Chris@133
|
43 }
|
Chris@133
|
44
|
Chris@133
|
45 SpectrumLayer::~SpectrumLayer()
|
Chris@133
|
46 {
|
Chris@193
|
47 //!!! delete parent's model
|
Chris@193
|
48 // for (size_t i = 0; i < m_fft.size(); ++i) delete m_fft[i];
|
Chris@133
|
49 }
|
Chris@133
|
50
|
Chris@133
|
51 void
|
Chris@133
|
52 SpectrumLayer::setModel(DenseTimeValueModel *model)
|
Chris@133
|
53 {
|
Chris@193
|
54 if (m_originModel == model) return;
|
Chris@193
|
55 m_originModel = model;
|
Chris@277
|
56
|
Chris@277
|
57 if (m_sliceableModel) {
|
Chris@277
|
58 const Model *oldModel = m_sliceableModel;
|
Chris@277
|
59 setSliceableModel(0);
|
Chris@277
|
60 // surprised I'm allowed to delete a const pointer -- may be a
|
Chris@277
|
61 // source of future compiler rejection?
|
Chris@277
|
62 delete oldModel;
|
Chris@277
|
63 }
|
Chris@275
|
64 //!!! setupFFT();
|
Chris@153
|
65 }
|
Chris@153
|
66
|
Chris@153
|
67 void
|
Chris@193
|
68 SpectrumLayer::setupFFT()
|
Chris@153
|
69 {
|
Chris@193
|
70 FFTModel *oldFFT = dynamic_cast<FFTModel *>
|
Chris@193
|
71 (const_cast<DenseThreeDimensionalModel *>(m_sliceableModel));
|
Chris@153
|
72
|
Chris@193
|
73 if (oldFFT) {
|
Chris@193
|
74 setSliceableModel(0);
|
Chris@193
|
75 delete oldFFT;
|
Chris@153
|
76 }
|
Chris@153
|
77
|
Chris@193
|
78 FFTModel *newFFT = new FFTModel(m_originModel,
|
Chris@193
|
79 m_channel,
|
Chris@193
|
80 m_windowType,
|
Chris@193
|
81 m_windowSize,
|
Chris@193
|
82 getWindowIncrement(),
|
Chris@193
|
83 m_windowSize,
|
Chris@193
|
84 true);
|
Chris@153
|
85
|
Chris@193
|
86 setSliceableModel(newFFT);
|
Chris@193
|
87
|
Chris@254
|
88 m_biasCurve.clear();
|
Chris@254
|
89 for (size_t i = 0; i < m_windowSize; ++i) {
|
Chris@254
|
90 m_biasCurve.push_back(1.f / (float(m_windowSize)/2.f));
|
Chris@254
|
91 }
|
Chris@254
|
92
|
Chris@193
|
93 newFFT->resume();
|
Chris@153
|
94 }
|
Chris@153
|
95
|
Chris@153
|
96 void
|
Chris@153
|
97 SpectrumLayer::setChannel(int channel)
|
Chris@153
|
98 {
|
Chris@153
|
99 m_channelSet = true;
|
Chris@153
|
100
|
Chris@193
|
101 FFTModel *fft = dynamic_cast<FFTModel *>
|
Chris@193
|
102 (const_cast<DenseThreeDimensionalModel *>(m_sliceableModel));
|
Chris@193
|
103
|
Chris@153
|
104 if (m_channel == channel) {
|
Chris@193
|
105 if (fft) fft->resume();
|
Chris@153
|
106 return;
|
Chris@153
|
107 }
|
Chris@153
|
108
|
Chris@153
|
109 m_channel = channel;
|
Chris@153
|
110
|
Chris@275
|
111 //!!! if (!fft) setupFFT();
|
Chris@153
|
112
|
Chris@153
|
113 emit layerParametersChanged();
|
Chris@133
|
114 }
|
Chris@133
|
115
|
Chris@153
|
116 Layer::PropertyList
|
Chris@153
|
117 SpectrumLayer::getProperties() const
|
Chris@153
|
118 {
|
Chris@193
|
119 PropertyList list = SliceLayer::getProperties();
|
Chris@153
|
120 list.push_back("Window Size");
|
Chris@153
|
121 list.push_back("Window Increment");
|
Chris@153
|
122 return list;
|
Chris@153
|
123 }
|
Chris@153
|
124
|
Chris@153
|
125 QString
|
Chris@153
|
126 SpectrumLayer::getPropertyLabel(const PropertyName &name) const
|
Chris@153
|
127 {
|
Chris@153
|
128 if (name == "Window Size") return tr("Window Size");
|
Chris@153
|
129 if (name == "Window Increment") return tr("Window Overlap");
|
Chris@193
|
130 return SliceLayer::getPropertyLabel(name);
|
Chris@153
|
131 }
|
Chris@153
|
132
|
Chris@153
|
133 Layer::PropertyType
|
Chris@153
|
134 SpectrumLayer::getPropertyType(const PropertyName &name) const
|
Chris@153
|
135 {
|
Chris@193
|
136 if (name == "Window Size") return ValueProperty;
|
Chris@193
|
137 if (name == "Window Increment") return ValueProperty;
|
Chris@193
|
138 return SliceLayer::getPropertyType(name);
|
Chris@153
|
139 }
|
Chris@153
|
140
|
Chris@153
|
141 QString
|
Chris@153
|
142 SpectrumLayer::getPropertyGroupName(const PropertyName &name) const
|
Chris@153
|
143 {
|
Chris@153
|
144 if (name == "Window Size" ||
|
Chris@153
|
145 name == "Window Increment") return tr("Window");
|
Chris@193
|
146 return SliceLayer::getPropertyGroupName(name);
|
Chris@153
|
147 }
|
Chris@153
|
148
|
Chris@153
|
149 int
|
Chris@153
|
150 SpectrumLayer::getPropertyRangeAndValue(const PropertyName &name,
|
Chris@216
|
151 int *min, int *max, int *deflt) const
|
Chris@153
|
152 {
|
Chris@216
|
153 int val = 0;
|
Chris@153
|
154
|
Chris@216
|
155 int garbage0, garbage1, garbage2;
|
Chris@153
|
156 if (!min) min = &garbage0;
|
Chris@153
|
157 if (!max) max = &garbage1;
|
Chris@216
|
158 if (!deflt) deflt = &garbage2;
|
Chris@153
|
159
|
Chris@193
|
160 if (name == "Window Size") {
|
Chris@153
|
161
|
Chris@153
|
162 *min = 0;
|
Chris@254
|
163 *max = 15;
|
Chris@216
|
164 *deflt = 5;
|
Chris@153
|
165
|
Chris@216
|
166 val = 0;
|
Chris@153
|
167 int ws = m_windowSize;
|
Chris@216
|
168 while (ws > 32) { ws >>= 1; val ++; }
|
Chris@153
|
169
|
Chris@153
|
170 } else if (name == "Window Increment") {
|
Chris@153
|
171
|
Chris@153
|
172 *min = 0;
|
Chris@153
|
173 *max = 5;
|
Chris@216
|
174 *deflt = 2;
|
Chris@153
|
175
|
Chris@216
|
176 val = m_windowHopLevel;
|
Chris@153
|
177
|
Chris@153
|
178 } else {
|
Chris@193
|
179
|
Chris@216
|
180 val = SliceLayer::getPropertyRangeAndValue(name, min, max, deflt);
|
Chris@153
|
181 }
|
Chris@153
|
182
|
Chris@216
|
183 return val;
|
Chris@153
|
184 }
|
Chris@153
|
185
|
Chris@153
|
186 QString
|
Chris@153
|
187 SpectrumLayer::getPropertyValueLabel(const PropertyName &name,
|
Chris@153
|
188 int value) const
|
Chris@153
|
189 {
|
Chris@153
|
190 if (name == "Window Size") {
|
Chris@153
|
191 return QString("%1").arg(32 << value);
|
Chris@153
|
192 }
|
Chris@153
|
193 if (name == "Window Increment") {
|
Chris@153
|
194 switch (value) {
|
Chris@153
|
195 default:
|
Chris@153
|
196 case 0: return tr("None");
|
Chris@153
|
197 case 1: return tr("25 %");
|
Chris@153
|
198 case 2: return tr("50 %");
|
Chris@153
|
199 case 3: return tr("75 %");
|
Chris@153
|
200 case 4: return tr("87.5 %");
|
Chris@153
|
201 case 5: return tr("93.75 %");
|
Chris@153
|
202 }
|
Chris@153
|
203 }
|
Chris@193
|
204 return SliceLayer::getPropertyValueLabel(name, value);
|
Chris@153
|
205 }
|
Chris@153
|
206
|
Chris@167
|
207 RangeMapper *
|
Chris@167
|
208 SpectrumLayer::getNewPropertyRangeMapper(const PropertyName &name) const
|
Chris@167
|
209 {
|
Chris@193
|
210 return SliceLayer::getNewPropertyRangeMapper(name);
|
Chris@167
|
211 }
|
Chris@167
|
212
|
Chris@133
|
213 void
|
Chris@153
|
214 SpectrumLayer::setProperty(const PropertyName &name, int value)
|
Chris@133
|
215 {
|
Chris@193
|
216 if (name == "Window Size") {
|
Chris@153
|
217 setWindowSize(32 << value);
|
Chris@153
|
218 } else if (name == "Window Increment") {
|
Chris@153
|
219 setWindowHopLevel(value);
|
Chris@193
|
220 } else {
|
Chris@193
|
221 SliceLayer::setProperty(name, value);
|
Chris@153
|
222 }
|
Chris@153
|
223 }
|
Chris@153
|
224
|
Chris@153
|
225 void
|
Chris@153
|
226 SpectrumLayer::setWindowSize(size_t ws)
|
Chris@153
|
227 {
|
Chris@153
|
228 if (m_windowSize == ws) return;
|
Chris@153
|
229 m_windowSize = ws;
|
Chris@275
|
230 //!!! setupFFT();
|
Chris@275
|
231 m_newFFTNeeded = true;
|
Chris@153
|
232 emit layerParametersChanged();
|
Chris@153
|
233 }
|
Chris@153
|
234
|
Chris@153
|
235 void
|
Chris@153
|
236 SpectrumLayer::setWindowHopLevel(size_t v)
|
Chris@153
|
237 {
|
Chris@153
|
238 if (m_windowHopLevel == v) return;
|
Chris@153
|
239 m_windowHopLevel = v;
|
Chris@275
|
240 //!!! setupFFT();
|
Chris@275
|
241 m_newFFTNeeded = true;
|
Chris@153
|
242 emit layerParametersChanged();
|
Chris@153
|
243 }
|
Chris@153
|
244
|
Chris@153
|
245 void
|
Chris@153
|
246 SpectrumLayer::setWindowType(WindowType w)
|
Chris@153
|
247 {
|
Chris@153
|
248 if (m_windowType == w) return;
|
Chris@153
|
249 m_windowType = w;
|
Chris@275
|
250 //!!! setupFFT();
|
Chris@275
|
251 m_newFFTNeeded = true;
|
Chris@153
|
252 emit layerParametersChanged();
|
Chris@153
|
253 }
|
Chris@153
|
254
|
Chris@153
|
255 void
|
Chris@153
|
256 SpectrumLayer::preferenceChanged(PropertyContainer::PropertyName name)
|
Chris@153
|
257 {
|
Chris@153
|
258 if (name == "Window Type") {
|
Chris@153
|
259 setWindowType(Preferences::getInstance()->getWindowType());
|
Chris@153
|
260 return;
|
Chris@153
|
261 }
|
Chris@153
|
262 }
|
Chris@153
|
263
|
Chris@133
|
264 bool
|
Chris@248
|
265 SpectrumLayer::getValueExtents(float &, float &, bool &, QString &) const
|
Chris@133
|
266 {
|
Chris@133
|
267 return false;
|
Chris@133
|
268 }
|
Chris@133
|
269
|
Chris@254
|
270 float
|
Chris@265
|
271 SpectrumLayer::getXForBin(int bin, int totalBins, float w) const
|
Chris@265
|
272 {
|
Chris@265
|
273 if (!m_sliceableModel) return SliceLayer::getXForBin(bin, totalBins, w);
|
Chris@265
|
274
|
Chris@265
|
275 float sampleRate = m_sliceableModel->getSampleRate();
|
Chris@265
|
276 float binfreq = (sampleRate * bin) / (totalBins * 2);
|
Chris@265
|
277
|
Chris@265
|
278 return getXForFrequency(binfreq, w);
|
Chris@265
|
279 }
|
Chris@265
|
280
|
Chris@265
|
281 int
|
Chris@265
|
282 SpectrumLayer::getBinForX(float x, int totalBins, float w) const
|
Chris@265
|
283 {
|
Chris@265
|
284 if (!m_sliceableModel) return SliceLayer::getBinForX(x, totalBins, w);
|
Chris@265
|
285
|
Chris@265
|
286 float sampleRate = m_sliceableModel->getSampleRate();
|
Chris@265
|
287 float binfreq = getFrequencyForX(x, w);
|
Chris@265
|
288
|
Chris@265
|
289 return int((binfreq * totalBins * 2) / sampleRate);
|
Chris@265
|
290 }
|
Chris@265
|
291
|
Chris@265
|
292 float
|
Chris@254
|
293 SpectrumLayer::getFrequencyForX(float x, float w) const
|
Chris@254
|
294 {
|
Chris@254
|
295 float freq = 0;
|
Chris@280
|
296 if (!m_sliceableModel) return 0;
|
Chris@254
|
297
|
Chris@254
|
298 int sampleRate = m_sliceableModel->getSampleRate();
|
Chris@254
|
299
|
Chris@254
|
300 float maxfreq = float(sampleRate) / 2;
|
Chris@254
|
301
|
Chris@254
|
302 switch (m_binScale) {
|
Chris@254
|
303
|
Chris@254
|
304 case LinearBins:
|
Chris@254
|
305 freq = ((x * maxfreq) / w);
|
Chris@254
|
306 break;
|
Chris@254
|
307
|
Chris@254
|
308 case LogBins:
|
Chris@254
|
309 freq = powf(10.f, (x * log10f(maxfreq)) / w);
|
Chris@254
|
310 break;
|
Chris@254
|
311
|
Chris@254
|
312 case InvertedLogBins:
|
Chris@254
|
313 freq = maxfreq - powf(10.f, ((w - x) * log10f(maxfreq)) / w);
|
Chris@254
|
314 break;
|
Chris@254
|
315 }
|
Chris@254
|
316
|
Chris@254
|
317 return freq;
|
Chris@254
|
318 }
|
Chris@254
|
319
|
Chris@254
|
320 float
|
Chris@254
|
321 SpectrumLayer::getXForFrequency(float freq, float w) const
|
Chris@254
|
322 {
|
Chris@254
|
323 float x = 0;
|
Chris@280
|
324 if (!m_sliceableModel) return x;
|
Chris@254
|
325
|
Chris@254
|
326 int sampleRate = m_sliceableModel->getSampleRate();
|
Chris@254
|
327
|
Chris@254
|
328 float maxfreq = float(sampleRate) / 2;
|
Chris@254
|
329
|
Chris@254
|
330 switch (m_binScale) {
|
Chris@254
|
331
|
Chris@254
|
332 case LinearBins:
|
Chris@254
|
333 x = (freq * w) / maxfreq;
|
Chris@254
|
334 break;
|
Chris@254
|
335
|
Chris@254
|
336 case LogBins:
|
Chris@254
|
337 x = (log10f(freq) * w) / log10f(maxfreq);
|
Chris@254
|
338 break;
|
Chris@254
|
339
|
Chris@254
|
340 case InvertedLogBins:
|
Chris@274
|
341 if (maxfreq == freq) x = w;
|
Chris@274
|
342 else x = w - (log10f(maxfreq - freq) * w) / log10f(maxfreq);
|
Chris@254
|
343 break;
|
Chris@254
|
344 }
|
Chris@254
|
345
|
Chris@254
|
346 return x;
|
Chris@254
|
347 }
|
Chris@254
|
348
|
Chris@260
|
349 bool
|
Chris@267
|
350 SpectrumLayer::getXScaleValue(const View *v, int x,
|
Chris@260
|
351 float &value, QString &unit) const
|
Chris@260
|
352 {
|
Chris@267
|
353 if (m_xorigins.find(v) == m_xorigins.end()) return false;
|
Chris@267
|
354 int xorigin = m_xorigins.find(v)->second;
|
Chris@267
|
355 value = getFrequencyForX(x - xorigin, v->width() - xorigin - 1);
|
Chris@260
|
356 unit = "Hz";
|
Chris@260
|
357 return true;
|
Chris@260
|
358 }
|
Chris@260
|
359
|
Chris@264
|
360 bool
|
Chris@274
|
361 SpectrumLayer::getYScaleValue(const View *v, int y,
|
Chris@274
|
362 float &value, QString &unit) const
|
Chris@274
|
363 {
|
Chris@274
|
364 value = getValueForY(y, v);
|
Chris@274
|
365
|
Chris@274
|
366 if (m_energyScale == dBScale || m_energyScale == MeterScale) {
|
Chris@274
|
367
|
Chris@274
|
368 float thresh = -80.f;
|
Chris@274
|
369
|
Chris@274
|
370 if (value > 0.f) {
|
Chris@274
|
371 value = 10.f * log10f(value);
|
Chris@274
|
372 if (value < thresh) value = thresh;
|
Chris@274
|
373 } else value = thresh;
|
Chris@274
|
374
|
Chris@274
|
375 unit = "dBV";
|
Chris@274
|
376
|
Chris@274
|
377 } else {
|
Chris@274
|
378 unit = "V";
|
Chris@274
|
379 }
|
Chris@274
|
380
|
Chris@274
|
381 return true;
|
Chris@274
|
382 }
|
Chris@274
|
383
|
Chris@274
|
384 bool
|
Chris@274
|
385 SpectrumLayer::getYScaleDifference(const View *v, int y0, int y1,
|
Chris@274
|
386 float &diff, QString &unit) const
|
Chris@274
|
387 {
|
Chris@274
|
388 bool rv = SliceLayer::getYScaleDifference(v, y0, y1, diff, unit);
|
Chris@274
|
389 if (rv && (unit == "dBV")) unit = "dB";
|
Chris@274
|
390 return rv;
|
Chris@274
|
391 }
|
Chris@274
|
392
|
Chris@274
|
393
|
Chris@274
|
394 bool
|
Chris@264
|
395 SpectrumLayer::getCrosshairExtents(View *v, QPainter &paint,
|
Chris@264
|
396 QPoint cursorPos,
|
Chris@264
|
397 std::vector<QRect> &extents) const
|
Chris@264
|
398 {
|
Chris@264
|
399 QRect vertical(cursorPos.x(), cursorPos.y(), 1, v->height() - cursorPos.y());
|
Chris@264
|
400 extents.push_back(vertical);
|
Chris@264
|
401
|
Chris@264
|
402 QRect horizontal(0, cursorPos.y(), v->width(), 12);
|
Chris@264
|
403 extents.push_back(horizontal);
|
Chris@264
|
404
|
Chris@280
|
405 int hoffset = 2;
|
Chris@280
|
406 if (m_binScale == LogBins) hoffset = 13;
|
Chris@278
|
407
|
Chris@280
|
408 int sw = getVerticalScaleWidth(v, paint);
|
Chris@280
|
409
|
Chris@280
|
410 QRect value(sw, cursorPos.y() - paint.fontMetrics().ascent() - 2,
|
Chris@280
|
411 paint.fontMetrics().width("0.0000001 V") + 2,
|
Chris@264
|
412 paint.fontMetrics().height());
|
Chris@280
|
413 extents.push_back(value);
|
Chris@280
|
414
|
Chris@280
|
415 QRect log(sw, cursorPos.y() + 2,
|
Chris@280
|
416 paint.fontMetrics().width("-80.000 dBV") + 2,
|
Chris@280
|
417 paint.fontMetrics().height());
|
Chris@280
|
418 extents.push_back(log);
|
Chris@280
|
419
|
Chris@280
|
420 QRect freq(cursorPos.x(),
|
Chris@280
|
421 v->height() - paint.fontMetrics().height() - hoffset,
|
Chris@280
|
422 paint.fontMetrics().width("123456 Hz") + 2,
|
Chris@280
|
423 paint.fontMetrics().height());
|
Chris@280
|
424 extents.push_back(freq);
|
Chris@264
|
425
|
Chris@278
|
426 int w(paint.fontMetrics().width("C#10+50c") + 2);
|
Chris@278
|
427 QRect pitch(cursorPos.x() - w,
|
Chris@278
|
428 v->height() - paint.fontMetrics().height() - hoffset,
|
Chris@278
|
429 w,
|
Chris@278
|
430 paint.fontMetrics().height());
|
Chris@278
|
431 extents.push_back(pitch);
|
Chris@278
|
432
|
Chris@264
|
433 return true;
|
Chris@264
|
434 }
|
Chris@264
|
435
|
Chris@254
|
436 void
|
Chris@254
|
437 SpectrumLayer::paintCrosshairs(View *v, QPainter &paint,
|
Chris@254
|
438 QPoint cursorPos) const
|
Chris@254
|
439 {
|
Chris@280
|
440 if (!m_sliceableModel) return;
|
Chris@280
|
441
|
Chris@254
|
442 paint.save();
|
Chris@282
|
443 QFont fn = paint.font();
|
Chris@282
|
444 if (fn.pointSize() > 8) {
|
Chris@282
|
445 fn.setPointSize(fn.pointSize() - 1);
|
Chris@282
|
446 paint.setFont(fn);
|
Chris@282
|
447 }
|
Chris@254
|
448
|
Chris@254
|
449 ColourMapper mapper(m_colourMap, 0, 1);
|
Chris@254
|
450 paint.setPen(mapper.getContrastingColour());
|
Chris@254
|
451
|
Chris@254
|
452 int xorigin = m_xorigins[v];
|
Chris@254
|
453 int w = v->width() - xorigin - 1;
|
Chris@254
|
454
|
Chris@254
|
455 paint.drawLine(xorigin, cursorPos.y(), v->width(), cursorPos.y());
|
Chris@254
|
456 paint.drawLine(cursorPos.x(), cursorPos.y(), cursorPos.x(), v->height());
|
Chris@254
|
457
|
Chris@254
|
458 float fundamental = getFrequencyForX(cursorPos.x() - xorigin, w);
|
Chris@254
|
459
|
Chris@280
|
460 int hoffset = 2;
|
Chris@280
|
461 if (m_binScale == LogBins) hoffset = 13;
|
Chris@278
|
462
|
Chris@278
|
463 v->drawVisibleText(paint,
|
Chris@278
|
464 cursorPos.x() + 2,
|
Chris@278
|
465 v->height() - 2 - hoffset,
|
Chris@278
|
466 QString("%1 Hz").arg(fundamental),
|
Chris@278
|
467 View::OutlinedText);
|
Chris@278
|
468
|
Chris@278
|
469 if (Pitch::isFrequencyInMidiRange(fundamental)) {
|
Chris@278
|
470 QString pitchLabel = Pitch::getPitchLabelForFrequency(fundamental);
|
Chris@278
|
471 v->drawVisibleText(paint,
|
Chris@278
|
472 cursorPos.x() - paint.fontMetrics().width(pitchLabel) - 2,
|
Chris@278
|
473 v->height() - 2 - hoffset,
|
Chris@278
|
474 pitchLabel,
|
Chris@278
|
475 View::OutlinedText);
|
Chris@278
|
476 }
|
Chris@264
|
477
|
Chris@280
|
478 float value = getValueForY(cursorPos.y(), v);
|
Chris@280
|
479 float thresh = -80.f;
|
Chris@280
|
480 float db = thresh;
|
Chris@280
|
481 if (value > 0.f) db = 10.f * log10f(value);
|
Chris@280
|
482 if (db < thresh) db = thresh;
|
Chris@280
|
483
|
Chris@280
|
484 v->drawVisibleText(paint,
|
Chris@280
|
485 xorigin + 2,
|
Chris@280
|
486 cursorPos.y() - 2,
|
Chris@280
|
487 QString("%1 V").arg(value),
|
Chris@280
|
488 View::OutlinedText);
|
Chris@280
|
489
|
Chris@280
|
490 v->drawVisibleText(paint,
|
Chris@280
|
491 xorigin + 2,
|
Chris@280
|
492 cursorPos.y() + 2 + paint.fontMetrics().ascent(),
|
Chris@280
|
493 QString("%1 dBV").arg(db),
|
Chris@280
|
494 View::OutlinedText);
|
Chris@280
|
495
|
Chris@254
|
496 int harmonic = 2;
|
Chris@254
|
497
|
Chris@254
|
498 while (harmonic < 100) {
|
Chris@254
|
499
|
Chris@254
|
500 float hx = lrintf(getXForFrequency(fundamental * harmonic, w));
|
Chris@254
|
501 hx += xorigin;
|
Chris@254
|
502
|
Chris@254
|
503 if (hx < xorigin || hx > v->width()) break;
|
Chris@254
|
504
|
Chris@254
|
505 int len = 7;
|
Chris@254
|
506
|
Chris@254
|
507 if (harmonic % 2 == 0) {
|
Chris@254
|
508 if (harmonic % 4 == 0) {
|
Chris@254
|
509 len = 12;
|
Chris@254
|
510 } else {
|
Chris@254
|
511 len = 10;
|
Chris@254
|
512 }
|
Chris@254
|
513 }
|
Chris@254
|
514
|
Chris@254
|
515 paint.drawLine(int(hx),
|
Chris@254
|
516 cursorPos.y(),
|
Chris@254
|
517 int(hx),
|
Chris@254
|
518 cursorPos.y() + len);
|
Chris@254
|
519
|
Chris@254
|
520 ++harmonic;
|
Chris@254
|
521 }
|
Chris@254
|
522
|
Chris@254
|
523 paint.restore();
|
Chris@254
|
524 }
|
Chris@254
|
525
|
Chris@199
|
526 QString
|
Chris@199
|
527 SpectrumLayer::getFeatureDescription(View *v, QPoint &p) const
|
Chris@199
|
528 {
|
Chris@199
|
529 if (!m_sliceableModel) return "";
|
Chris@199
|
530
|
Chris@199
|
531 int minbin = 0, maxbin = 0, range = 0;
|
Chris@199
|
532 QString genericDesc = SliceLayer::getFeatureDescription
|
Chris@199
|
533 (v, p, false, minbin, maxbin, range);
|
Chris@199
|
534
|
Chris@199
|
535 if (genericDesc == "") return "";
|
Chris@199
|
536
|
Chris@199
|
537 float minvalue = 0.f;
|
Chris@248
|
538 if (minbin < int(m_values.size())) minvalue = m_values[minbin];
|
Chris@199
|
539
|
Chris@199
|
540 float maxvalue = minvalue;
|
Chris@248
|
541 if (maxbin < int(m_values.size())) maxvalue = m_values[maxbin];
|
Chris@199
|
542
|
Chris@199
|
543 if (minvalue > maxvalue) std::swap(minvalue, maxvalue);
|
Chris@199
|
544
|
Chris@199
|
545 QString binstr;
|
Chris@199
|
546 QString hzstr;
|
Chris@199
|
547 int minfreq = lrintf((minbin * m_sliceableModel->getSampleRate()) /
|
Chris@199
|
548 m_windowSize);
|
Chris@199
|
549 int maxfreq = lrintf((std::max(maxbin, minbin+1)
|
Chris@199
|
550 * m_sliceableModel->getSampleRate()) /
|
Chris@199
|
551 m_windowSize);
|
Chris@199
|
552
|
Chris@199
|
553 if (maxbin != minbin) {
|
Chris@199
|
554 binstr = tr("%1 - %2").arg(minbin+1).arg(maxbin+1);
|
Chris@199
|
555 } else {
|
Chris@199
|
556 binstr = QString("%1").arg(minbin+1);
|
Chris@199
|
557 }
|
Chris@199
|
558 if (minfreq != maxfreq) {
|
Chris@199
|
559 hzstr = tr("%1 - %2 Hz").arg(minfreq).arg(maxfreq);
|
Chris@199
|
560 } else {
|
Chris@199
|
561 hzstr = tr("%1 Hz").arg(minfreq);
|
Chris@199
|
562 }
|
Chris@199
|
563
|
Chris@199
|
564 QString valuestr;
|
Chris@199
|
565 if (maxvalue != minvalue) {
|
Chris@199
|
566 valuestr = tr("%1 - %2").arg(minvalue).arg(maxvalue);
|
Chris@199
|
567 } else {
|
Chris@199
|
568 valuestr = QString("%1").arg(minvalue);
|
Chris@199
|
569 }
|
Chris@199
|
570
|
Chris@199
|
571 QString dbstr;
|
Chris@199
|
572 float mindb = AudioLevel::multiplier_to_dB(minvalue);
|
Chris@199
|
573 float maxdb = AudioLevel::multiplier_to_dB(maxvalue);
|
Chris@199
|
574 QString mindbstr;
|
Chris@199
|
575 QString maxdbstr;
|
Chris@199
|
576 if (mindb == AudioLevel::DB_FLOOR) {
|
Chris@199
|
577 mindbstr = tr("-Inf");
|
Chris@199
|
578 } else {
|
Chris@199
|
579 mindbstr = QString("%1").arg(lrintf(mindb));
|
Chris@199
|
580 }
|
Chris@199
|
581 if (maxdb == AudioLevel::DB_FLOOR) {
|
Chris@199
|
582 maxdbstr = tr("-Inf");
|
Chris@199
|
583 } else {
|
Chris@199
|
584 maxdbstr = QString("%1").arg(lrintf(maxdb));
|
Chris@199
|
585 }
|
Chris@199
|
586 if (lrintf(mindb) != lrintf(maxdb)) {
|
Chris@199
|
587 dbstr = tr("%1 - %2").arg(mindbstr).arg(maxdbstr);
|
Chris@199
|
588 } else {
|
Chris@199
|
589 dbstr = tr("%1").arg(mindbstr);
|
Chris@199
|
590 }
|
Chris@199
|
591
|
Chris@199
|
592 QString description;
|
Chris@199
|
593
|
Chris@248
|
594 if (range > int(m_sliceableModel->getResolution())) {
|
Chris@199
|
595 description = tr("%1\nBin:\t%2 (%3)\n%4 value:\t%5\ndB:\t%6")
|
Chris@199
|
596 .arg(genericDesc)
|
Chris@199
|
597 .arg(binstr)
|
Chris@199
|
598 .arg(hzstr)
|
Chris@199
|
599 .arg(m_samplingMode == NearestSample ? tr("First") :
|
Chris@199
|
600 m_samplingMode == SampleMean ? tr("Mean") : tr("Peak"))
|
Chris@199
|
601 .arg(valuestr)
|
Chris@199
|
602 .arg(dbstr);
|
Chris@199
|
603 } else {
|
Chris@199
|
604 description = tr("%1\nBin:\t%2 (%3)\nValue:\t%4\ndB:\t%5")
|
Chris@199
|
605 .arg(genericDesc)
|
Chris@199
|
606 .arg(binstr)
|
Chris@199
|
607 .arg(hzstr)
|
Chris@199
|
608 .arg(valuestr)
|
Chris@199
|
609 .arg(dbstr);
|
Chris@199
|
610 }
|
Chris@199
|
611
|
Chris@199
|
612 return description;
|
Chris@199
|
613 }
|
Chris@199
|
614
|
Chris@254
|
615 void
|
Chris@275
|
616 SpectrumLayer::paint(View *v, QPainter &paint, QRect rect) const
|
Chris@275
|
617 {
|
Chris@275
|
618 if (!m_originModel || !m_originModel->isOK() ||
|
Chris@275
|
619 !m_originModel->isReady()) return;
|
Chris@275
|
620
|
Chris@275
|
621 if (m_newFFTNeeded) {
|
Chris@275
|
622 const_cast<SpectrumLayer *>(this)->setupFFT(); //ugh
|
Chris@275
|
623 m_newFFTNeeded = false;
|
Chris@275
|
624 }
|
Chris@277
|
625
|
Chris@277
|
626 FFTModel *fft = dynamic_cast<FFTModel *>
|
Chris@277
|
627 (const_cast<DenseThreeDimensionalModel *>(m_sliceableModel));
|
Chris@277
|
628
|
Chris@280
|
629 float thresh = (powf(10, -6) / m_gain) * (m_windowSize / 2.f); // -60dB adj
|
Chris@277
|
630
|
Chris@277
|
631 int xorigin = getVerticalScaleWidth(v, paint) + 1;
|
Chris@277
|
632 int w = v->width() - xorigin - 1;
|
Chris@277
|
633
|
Chris@278
|
634 int pkh = 0;
|
Chris@278
|
635 if (m_binScale == LogBins) pkh = 10;
|
Chris@278
|
636
|
Chris@277
|
637 if (fft) {
|
Chris@277
|
638
|
Chris@277
|
639 // draw peak lines
|
Chris@277
|
640 //!!! should be optional
|
Chris@277
|
641
|
Chris@277
|
642 size_t col = v->getCentreFrame() / fft->getResolution();
|
Chris@277
|
643
|
Chris@277
|
644 paint.save();
|
Chris@277
|
645 paint.setRenderHint(QPainter::Antialiasing, false);
|
Chris@277
|
646 paint.setPen(QColor(160, 160, 160)); //!!!
|
Chris@277
|
647
|
Chris@280
|
648 FFTModel::PeakSet peaks = fft->getPeakFrequencies
|
Chris@280
|
649 (FFTModel::MajorPitchAdaptivePeaks, col);
|
Chris@280
|
650
|
Chris@280
|
651 ColourMapper mapper(ColourMapper::BlackOnWhite, 0, 1);
|
Chris@277
|
652
|
Chris@277
|
653 BiasCurve curve;
|
Chris@277
|
654 getBiasCurve(curve);
|
Chris@277
|
655 size_t cs = curve.size();
|
Chris@280
|
656
|
Chris@280
|
657 std::vector<float> values;
|
Chris@277
|
658
|
Chris@277
|
659 for (size_t bin = 0; bin < fft->getHeight(); ++bin) {
|
Chris@280
|
660 float value = m_sliceableModel->getValueAt(col, bin);
|
Chris@280
|
661 if (bin < cs) value *= curve[bin];
|
Chris@280
|
662 values.push_back(value);
|
Chris@280
|
663 }
|
Chris@280
|
664
|
Chris@280
|
665 for (FFTModel::PeakSet::iterator i = peaks.begin();
|
Chris@280
|
666 i != peaks.end(); ++i) {
|
Chris@280
|
667
|
Chris@280
|
668 size_t bin = i->first;
|
Chris@277
|
669
|
Chris@280
|
670 // std::cerr << "bin = " << bin << ", thresh = " << thresh << ", value = " << fft->getMagnitudeAt(col, bin) << std::endl;
|
Chris@280
|
671
|
Chris@277
|
672 if (!fft->isOverThreshold(col, bin, thresh)) continue;
|
Chris@277
|
673
|
Chris@280
|
674 float freq = i->second;
|
Chris@280
|
675
|
Chris@277
|
676 int x = lrintf(getXForFrequency(freq, w));
|
Chris@277
|
677
|
Chris@277
|
678 float norm = 0.f;
|
Chris@280
|
679 float y = getYForValue(values[bin], v, norm); // don't need y, need norm
|
Chris@277
|
680
|
Chris@277
|
681 paint.setPen(mapper.map(norm));
|
Chris@278
|
682 paint.drawLine(xorigin + x, 0, xorigin + x, v->height() - pkh - 1);
|
Chris@277
|
683 }
|
Chris@277
|
684
|
Chris@277
|
685 paint.restore();
|
Chris@277
|
686 }
|
Chris@275
|
687
|
Chris@275
|
688 SliceLayer::paint(v, paint, rect);
|
Chris@277
|
689
|
Chris@278
|
690 //!!! All of this stuff relating to depicting frequencies
|
Chris@278
|
691 //(keyboard, crosshairs etc) should be applicable to any slice
|
Chris@278
|
692 //layer whose model has a vertical scale unit of Hz. However, the
|
Chris@278
|
693 //dense 3d model at the moment doesn't record its vertical scale
|
Chris@278
|
694 //unit -- we need to fix that and hoist this code as appropriate.
|
Chris@278
|
695 //Same really goes for any code in SpectrogramLayer that could be
|
Chris@278
|
696 //relevant to Colour3DPlotLayer with unit Hz, but that's a bigger
|
Chris@278
|
697 //proposition.
|
Chris@278
|
698
|
Chris@277
|
699 if (m_binScale == LogBins) {
|
Chris@277
|
700
|
Chris@277
|
701 int pkh = 10;
|
Chris@277
|
702 int h = v->height();
|
Chris@277
|
703
|
Chris@277
|
704 // piano keyboard
|
Chris@277
|
705 //!!! should be in a new paintHorizontalScale()?
|
Chris@278
|
706 // nice to have a piano keyboard class, of course
|
Chris@277
|
707
|
Chris@278
|
708 paint.drawLine(xorigin, h - pkh - 1, w + xorigin, h - pkh - 1);
|
Chris@277
|
709
|
Chris@277
|
710 int px = xorigin, ppx = xorigin;
|
Chris@278
|
711 paint.setBrush(paint.pen().color());
|
Chris@277
|
712
|
Chris@277
|
713 for (int i = 0; i < 128; ++i) {
|
Chris@277
|
714
|
Chris@277
|
715 float f = Pitch::getFrequencyForPitch(i);
|
Chris@277
|
716 int x = lrintf(getXForFrequency(f, w));
|
Chris@278
|
717
|
Chris@278
|
718 x += xorigin;
|
Chris@277
|
719
|
Chris@278
|
720 if (i == 0) {
|
Chris@278
|
721 px = ppx = x;
|
Chris@278
|
722 }
|
Chris@278
|
723 if (i == 1) {
|
Chris@278
|
724 ppx = px - (x - px);
|
Chris@278
|
725 }
|
Chris@278
|
726
|
Chris@278
|
727 if (x < xorigin) {
|
Chris@278
|
728 ppx = px;
|
Chris@278
|
729 px = x;
|
Chris@277
|
730 continue;
|
Chris@277
|
731 }
|
Chris@278
|
732
|
Chris@278
|
733 if (x > w) {
|
Chris@278
|
734 break;
|
Chris@278
|
735 }
|
Chris@277
|
736
|
Chris@277
|
737 int n = (i % 12);
|
Chris@277
|
738
|
Chris@277
|
739 if (n == 1) {
|
Chris@277
|
740 // C# -- fill the C from here
|
Chris@277
|
741 if (x - ppx > 2) {
|
Chris@278
|
742 paint.fillRect((px + ppx) / 2 + 1,
|
Chris@277
|
743 h - pkh,
|
Chris@278
|
744 x - (px + ppx) / 2 - 1,
|
Chris@277
|
745 pkh,
|
Chris@277
|
746 Qt::gray);
|
Chris@277
|
747 }
|
Chris@277
|
748 }
|
Chris@277
|
749
|
Chris@277
|
750 if (n == 1 || n == 3 || n == 6 || n == 8 || n == 10) {
|
Chris@277
|
751 // black notes
|
Chris@277
|
752 paint.drawLine(x, h - pkh, x, h);
|
Chris@278
|
753 int rw = lrintf(float(x - px) / 4) * 2;
|
Chris@277
|
754 if (rw < 2) rw = 2;
|
Chris@278
|
755 paint.drawRect(x - rw/2, h - pkh, rw, pkh/2);
|
Chris@277
|
756 } else if (n == 0 || n == 5) {
|
Chris@277
|
757 // C, F
|
Chris@277
|
758 if (px < w) {
|
Chris@277
|
759 paint.drawLine((x + px) / 2, h - pkh, (x + px) / 2, h);
|
Chris@277
|
760 }
|
Chris@277
|
761 }
|
Chris@277
|
762
|
Chris@277
|
763 ppx = px;
|
Chris@277
|
764 px = x;
|
Chris@277
|
765 }
|
Chris@277
|
766 }
|
Chris@275
|
767 }
|
Chris@275
|
768
|
Chris@275
|
769 void
|
Chris@254
|
770 SpectrumLayer::getBiasCurve(BiasCurve &curve) const
|
Chris@254
|
771 {
|
Chris@254
|
772 curve = m_biasCurve;
|
Chris@254
|
773 }
|
Chris@199
|
774
|
Chris@220
|
775 QString
|
Chris@220
|
776 SpectrumLayer::toXmlString(QString indent, QString extraAttributes) const
|
Chris@220
|
777 {
|
Chris@220
|
778 QString s;
|
Chris@199
|
779
|
Chris@220
|
780 s += QString("windowSize=\"%1\" "
|
Chris@220
|
781 "windowHopLevel=\"%2\"")
|
Chris@220
|
782 .arg(m_windowSize)
|
Chris@220
|
783 .arg(m_windowHopLevel);
|
Chris@220
|
784
|
Chris@220
|
785 return SliceLayer::toXmlString(indent, extraAttributes + " " + s);
|
Chris@220
|
786 }
|
Chris@220
|
787
|
Chris@220
|
788 void
|
Chris@220
|
789 SpectrumLayer::setProperties(const QXmlAttributes &attributes)
|
Chris@220
|
790 {
|
Chris@220
|
791 SliceLayer::setProperties(attributes);
|
Chris@220
|
792
|
Chris@220
|
793 bool ok = false;
|
Chris@220
|
794
|
Chris@220
|
795 size_t windowSize = attributes.value("windowSize").toUInt(&ok);
|
Chris@220
|
796 if (ok) setWindowSize(windowSize);
|
Chris@220
|
797
|
Chris@220
|
798 size_t windowHopLevel = attributes.value("windowHopLevel").toUInt(&ok);
|
Chris@220
|
799 if (ok) setWindowHopLevel(windowHopLevel);
|
Chris@220
|
800 }
|
Chris@220
|
801
|
Chris@220
|
802
|