Chris@58
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
Chris@0
|
2
|
Chris@0
|
3 /*
|
Chris@59
|
4 Sonic Visualiser
|
Chris@59
|
5 An audio file viewer and annotation editor.
|
Chris@59
|
6 Centre for Digital Music, Queen Mary, University of London.
|
Chris@484
|
7 This file copyright 2006-2009 Chris Cannam and QMUL.
|
Chris@0
|
8
|
Chris@59
|
9 This program is free software; you can redistribute it and/or
|
Chris@59
|
10 modify it under the terms of the GNU General Public License as
|
Chris@59
|
11 published by the Free Software Foundation; either version 2 of the
|
Chris@59
|
12 License, or (at your option) any later version. See the file
|
Chris@59
|
13 COPYING included with this distribution for more information.
|
Chris@0
|
14 */
|
Chris@0
|
15
|
Chris@0
|
16 #include "SpectrogramLayer.h"
|
Chris@0
|
17
|
Chris@128
|
18 #include "view/View.h"
|
Chris@0
|
19 #include "base/Profiler.h"
|
Chris@0
|
20 #include "base/AudioLevel.h"
|
Chris@0
|
21 #include "base/Window.h"
|
Chris@24
|
22 #include "base/Pitch.h"
|
Chris@118
|
23 #include "base/Preferences.h"
|
Chris@167
|
24 #include "base/RangeMapper.h"
|
Chris@253
|
25 #include "base/LogRange.h"
|
Chris@1063
|
26 #include "base/ColumnOp.h"
|
Chris@376
|
27 #include "widgets/CommandHistory.h"
|
Chris@1078
|
28 #include "data/model/Dense3DModelPeakCache.h"
|
Chris@1078
|
29
|
Chris@376
|
30 #include "ColourMapper.h"
|
Chris@283
|
31 #include "ImageRegionFinder.h"
|
Chris@690
|
32 #include "PianoScale.h"
|
Chris@1078
|
33 #include "PaintAssistant.h"
|
Chris@0
|
34
|
Chris@0
|
35 #include <QPainter>
|
Chris@0
|
36 #include <QImage>
|
Chris@0
|
37 #include <QPixmap>
|
Chris@0
|
38 #include <QRect>
|
Chris@92
|
39 #include <QApplication>
|
Chris@178
|
40 #include <QMessageBox>
|
Chris@283
|
41 #include <QMouseEvent>
|
Chris@316
|
42 #include <QTextStream>
|
Chris@1017
|
43 #include <QSettings>
|
Chris@0
|
44
|
Chris@0
|
45 #include <iostream>
|
Chris@0
|
46
|
Chris@0
|
47 #include <cassert>
|
Chris@0
|
48 #include <cmath>
|
Chris@0
|
49
|
Chris@545
|
50 #ifndef __GNUC__
|
Chris@545
|
51 #include <alloca.h>
|
Chris@545
|
52 #endif
|
Chris@545
|
53
|
Chris@1044
|
54 #define DEBUG_SPECTROGRAM_REPAINT 1
|
Chris@1025
|
55
|
Chris@1025
|
56 using namespace std;
|
Chris@907
|
57
|
Chris@44
|
58 SpectrogramLayer::SpectrogramLayer(Configuration config) :
|
Chris@0
|
59 m_model(0),
|
Chris@0
|
60 m_channel(0),
|
Chris@0
|
61 m_windowSize(1024),
|
Chris@0
|
62 m_windowType(HanningWindow),
|
Chris@97
|
63 m_windowHopLevel(2),
|
Chris@0
|
64 m_gain(1.0),
|
Chris@215
|
65 m_initialGain(1.0),
|
Chris@37
|
66 m_threshold(0.0),
|
Chris@215
|
67 m_initialThreshold(0.0),
|
Chris@9
|
68 m_colourRotation(0),
|
Chris@215
|
69 m_initialRotation(0),
|
Chris@119
|
70 m_minFrequency(10),
|
Chris@0
|
71 m_maxFrequency(8000),
|
Chris@135
|
72 m_initialMaxFrequency(8000),
|
Chris@0
|
73 m_colourScale(dBColourScale),
|
Chris@197
|
74 m_colourMap(0),
|
Chris@0
|
75 m_frequencyScale(LinearFrequencyScale),
|
Chris@37
|
76 m_binDisplay(AllBins),
|
Chris@1063
|
77 m_normalization(ColumnOp::NoNormalization),
|
Chris@133
|
78 m_lastEmittedZoomStep(-1),
|
Chris@390
|
79 m_synchronous(false),
|
Chris@608
|
80 m_haveDetailedScale(false),
|
Chris@193
|
81 m_exiting(false),
|
Chris@1088
|
82 m_fftModel(0),
|
Chris@1088
|
83 m_peakCache(0),
|
Chris@1088
|
84 m_peakCacheDivisor(8)
|
Chris@0
|
85 {
|
Chris@1017
|
86 QString colourConfigName = "spectrogram-colour";
|
Chris@1017
|
87 int colourConfigDefault = int(ColourMapper::Green);
|
Chris@1017
|
88
|
Chris@215
|
89 if (config == FullRangeDb) {
|
Chris@215
|
90 m_initialMaxFrequency = 0;
|
Chris@215
|
91 setMaxFrequency(0);
|
Chris@215
|
92 } else if (config == MelodicRange) {
|
Chris@0
|
93 setWindowSize(8192);
|
Chris@97
|
94 setWindowHopLevel(4);
|
Chris@215
|
95 m_initialMaxFrequency = 1500;
|
Chris@215
|
96 setMaxFrequency(1500);
|
Chris@215
|
97 setMinFrequency(40);
|
Chris@0
|
98 setColourScale(LinearColourScale);
|
Chris@215
|
99 setColourMap(ColourMapper::Sunset);
|
Chris@215
|
100 setFrequencyScale(LogFrequencyScale);
|
Chris@1017
|
101 colourConfigName = "spectrogram-melodic-colour";
|
Chris@1017
|
102 colourConfigDefault = int(ColourMapper::Sunset);
|
Chris@224
|
103 // setGain(20);
|
Chris@37
|
104 } else if (config == MelodicPeaks) {
|
Chris@37
|
105 setWindowSize(4096);
|
Chris@97
|
106 setWindowHopLevel(5);
|
Chris@135
|
107 m_initialMaxFrequency = 2000;
|
Chris@40
|
108 setMaxFrequency(2000);
|
Chris@37
|
109 setMinFrequency(40);
|
Chris@37
|
110 setFrequencyScale(LogFrequencyScale);
|
Chris@215
|
111 setColourScale(LinearColourScale);
|
Chris@37
|
112 setBinDisplay(PeakFrequencies);
|
Chris@1063
|
113 setNormalization(ColumnOp::NormalizeColumns);
|
Chris@1017
|
114 colourConfigName = "spectrogram-melodic-colour";
|
Chris@1017
|
115 colourConfigDefault = int(ColourMapper::Sunset);
|
Chris@0
|
116 }
|
Chris@110
|
117
|
Chris@1017
|
118 QSettings settings;
|
Chris@1017
|
119 settings.beginGroup("Preferences");
|
Chris@1017
|
120 setColourMap(settings.value(colourConfigName, colourConfigDefault).toInt());
|
Chris@1017
|
121 settings.endGroup();
|
Chris@1017
|
122
|
Chris@122
|
123 Preferences *prefs = Preferences::getInstance();
|
Chris@122
|
124 connect(prefs, SIGNAL(propertyChanged(PropertyContainer::PropertyName)),
|
Chris@122
|
125 this, SLOT(preferenceChanged(PropertyContainer::PropertyName)));
|
Chris@122
|
126 setWindowType(prefs->getWindowType());
|
Chris@122
|
127
|
Chris@197
|
128 initialisePalette();
|
Chris@0
|
129 }
|
Chris@0
|
130
|
Chris@0
|
131 SpectrogramLayer::~SpectrogramLayer()
|
Chris@0
|
132 {
|
Chris@1088
|
133 invalidateFFTModel();
|
Chris@0
|
134 }
|
Chris@0
|
135
|
Chris@0
|
136 void
|
Chris@0
|
137 SpectrogramLayer::setModel(const DenseTimeValueModel *model)
|
Chris@0
|
138 {
|
Chris@682
|
139 // cerr << "SpectrogramLayer(" << this << "): setModel(" << model << ")" << endl;
|
Chris@34
|
140
|
Chris@110
|
141 if (model == m_model) return;
|
Chris@110
|
142
|
Chris@0
|
143 m_model = model;
|
Chris@1088
|
144 invalidateFFTModel();
|
Chris@0
|
145
|
Chris@0
|
146 if (!m_model || !m_model->isOK()) return;
|
Chris@0
|
147
|
Chris@320
|
148 connectSignals(m_model);
|
Chris@0
|
149
|
Chris@0
|
150 connect(m_model, SIGNAL(modelChanged()), this, SLOT(cacheInvalid()));
|
Chris@906
|
151 connect(m_model, SIGNAL(modelChangedWithin(sv_frame_t, sv_frame_t)),
|
Chris@906
|
152 this, SLOT(cacheInvalid(sv_frame_t, sv_frame_t)));
|
Chris@0
|
153
|
Chris@0
|
154 emit modelReplaced();
|
Chris@110
|
155 }
|
Chris@115
|
156
|
Chris@0
|
157 Layer::PropertyList
|
Chris@0
|
158 SpectrogramLayer::getProperties() const
|
Chris@0
|
159 {
|
Chris@0
|
160 PropertyList list;
|
Chris@87
|
161 list.push_back("Colour");
|
Chris@87
|
162 list.push_back("Colour Scale");
|
Chris@87
|
163 list.push_back("Window Size");
|
Chris@97
|
164 list.push_back("Window Increment");
|
Chris@862
|
165 list.push_back("Normalization");
|
Chris@87
|
166 list.push_back("Bin Display");
|
Chris@87
|
167 list.push_back("Threshold");
|
Chris@87
|
168 list.push_back("Gain");
|
Chris@87
|
169 list.push_back("Colour Rotation");
|
Chris@153
|
170 // list.push_back("Min Frequency");
|
Chris@153
|
171 // list.push_back("Max Frequency");
|
Chris@87
|
172 list.push_back("Frequency Scale");
|
Chris@0
|
173 return list;
|
Chris@0
|
174 }
|
Chris@0
|
175
|
Chris@87
|
176 QString
|
Chris@87
|
177 SpectrogramLayer::getPropertyLabel(const PropertyName &name) const
|
Chris@87
|
178 {
|
Chris@87
|
179 if (name == "Colour") return tr("Colour");
|
Chris@87
|
180 if (name == "Colour Scale") return tr("Colour Scale");
|
Chris@87
|
181 if (name == "Window Size") return tr("Window Size");
|
Chris@112
|
182 if (name == "Window Increment") return tr("Window Overlap");
|
Chris@862
|
183 if (name == "Normalization") return tr("Normalization");
|
Chris@87
|
184 if (name == "Bin Display") return tr("Bin Display");
|
Chris@87
|
185 if (name == "Threshold") return tr("Threshold");
|
Chris@87
|
186 if (name == "Gain") return tr("Gain");
|
Chris@87
|
187 if (name == "Colour Rotation") return tr("Colour Rotation");
|
Chris@87
|
188 if (name == "Min Frequency") return tr("Min Frequency");
|
Chris@87
|
189 if (name == "Max Frequency") return tr("Max Frequency");
|
Chris@87
|
190 if (name == "Frequency Scale") return tr("Frequency Scale");
|
Chris@87
|
191 return "";
|
Chris@87
|
192 }
|
Chris@87
|
193
|
Chris@335
|
194 QString
|
Chris@862
|
195 SpectrogramLayer::getPropertyIconName(const PropertyName &) const
|
Chris@335
|
196 {
|
Chris@335
|
197 return "";
|
Chris@335
|
198 }
|
Chris@335
|
199
|
Chris@0
|
200 Layer::PropertyType
|
Chris@0
|
201 SpectrogramLayer::getPropertyType(const PropertyName &name) const
|
Chris@0
|
202 {
|
Chris@87
|
203 if (name == "Gain") return RangeProperty;
|
Chris@87
|
204 if (name == "Colour Rotation") return RangeProperty;
|
Chris@87
|
205 if (name == "Threshold") return RangeProperty;
|
Chris@0
|
206 return ValueProperty;
|
Chris@0
|
207 }
|
Chris@0
|
208
|
Chris@0
|
209 QString
|
Chris@0
|
210 SpectrogramLayer::getPropertyGroupName(const PropertyName &name) const
|
Chris@0
|
211 {
|
Chris@153
|
212 if (name == "Bin Display" ||
|
Chris@153
|
213 name == "Frequency Scale") return tr("Bins");
|
Chris@87
|
214 if (name == "Window Size" ||
|
Chris@1086
|
215 name == "Window Increment") return tr("Window");
|
Chris@87
|
216 if (name == "Colour" ||
|
Chris@87
|
217 name == "Threshold" ||
|
Chris@87
|
218 name == "Colour Rotation") return tr("Colour");
|
Chris@862
|
219 if (name == "Normalization" ||
|
Chris@153
|
220 name == "Gain" ||
|
Chris@87
|
221 name == "Colour Scale") return tr("Scale");
|
Chris@0
|
222 return QString();
|
Chris@0
|
223 }
|
Chris@0
|
224
|
Chris@0
|
225 int
|
Chris@0
|
226 SpectrogramLayer::getPropertyRangeAndValue(const PropertyName &name,
|
Chris@216
|
227 int *min, int *max, int *deflt) const
|
Chris@0
|
228 {
|
Chris@216
|
229 int val = 0;
|
Chris@216
|
230
|
Chris@216
|
231 int garbage0, garbage1, garbage2;
|
Chris@55
|
232 if (!min) min = &garbage0;
|
Chris@55
|
233 if (!max) max = &garbage1;
|
Chris@216
|
234 if (!deflt) deflt = &garbage2;
|
Chris@10
|
235
|
Chris@87
|
236 if (name == "Gain") {
|
Chris@0
|
237
|
Chris@0
|
238 *min = -50;
|
Chris@0
|
239 *max = 50;
|
Chris@0
|
240
|
Chris@906
|
241 *deflt = int(lrint(log10(m_initialGain) * 20.0));
|
Chris@216
|
242 if (*deflt < *min) *deflt = *min;
|
Chris@216
|
243 if (*deflt > *max) *deflt = *max;
|
Chris@216
|
244
|
Chris@906
|
245 val = int(lrint(log10(m_gain) * 20.0));
|
Chris@216
|
246 if (val < *min) val = *min;
|
Chris@216
|
247 if (val > *max) val = *max;
|
Chris@0
|
248
|
Chris@87
|
249 } else if (name == "Threshold") {
|
Chris@37
|
250
|
Chris@37
|
251 *min = -50;
|
Chris@37
|
252 *max = 0;
|
Chris@37
|
253
|
Chris@906
|
254 *deflt = int(lrint(AudioLevel::multiplier_to_dB(m_initialThreshold)));
|
Chris@216
|
255 if (*deflt < *min) *deflt = *min;
|
Chris@216
|
256 if (*deflt > *max) *deflt = *max;
|
Chris@216
|
257
|
Chris@906
|
258 val = int(lrint(AudioLevel::multiplier_to_dB(m_threshold)));
|
Chris@216
|
259 if (val < *min) val = *min;
|
Chris@216
|
260 if (val > *max) val = *max;
|
Chris@37
|
261
|
Chris@87
|
262 } else if (name == "Colour Rotation") {
|
Chris@9
|
263
|
Chris@9
|
264 *min = 0;
|
Chris@9
|
265 *max = 256;
|
Chris@216
|
266 *deflt = m_initialRotation;
|
Chris@216
|
267
|
Chris@216
|
268 val = m_colourRotation;
|
Chris@9
|
269
|
Chris@87
|
270 } else if (name == "Colour Scale") {
|
Chris@0
|
271
|
Chris@0
|
272 *min = 0;
|
Chris@176
|
273 *max = 4;
|
Chris@216
|
274 *deflt = int(dBColourScale);
|
Chris@216
|
275
|
Chris@216
|
276 val = (int)m_colourScale;
|
Chris@0
|
277
|
Chris@87
|
278 } else if (name == "Colour") {
|
Chris@0
|
279
|
Chris@0
|
280 *min = 0;
|
Chris@196
|
281 *max = ColourMapper::getColourMapCount() - 1;
|
Chris@216
|
282 *deflt = 0;
|
Chris@216
|
283
|
Chris@216
|
284 val = m_colourMap;
|
Chris@0
|
285
|
Chris@87
|
286 } else if (name == "Window Size") {
|
Chris@0
|
287
|
Chris@0
|
288 *min = 0;
|
Chris@0
|
289 *max = 10;
|
Chris@216
|
290 *deflt = 5;
|
Chris@0
|
291
|
Chris@216
|
292 val = 0;
|
Chris@0
|
293 int ws = m_windowSize;
|
Chris@216
|
294 while (ws > 32) { ws >>= 1; val ++; }
|
Chris@0
|
295
|
Chris@97
|
296 } else if (name == "Window Increment") {
|
Chris@0
|
297
|
Chris@0
|
298 *min = 0;
|
Chris@97
|
299 *max = 5;
|
Chris@216
|
300 *deflt = 2;
|
Chris@216
|
301
|
Chris@216
|
302 val = m_windowHopLevel;
|
Chris@0
|
303
|
Chris@87
|
304 } else if (name == "Min Frequency") {
|
Chris@37
|
305
|
Chris@37
|
306 *min = 0;
|
Chris@37
|
307 *max = 9;
|
Chris@216
|
308 *deflt = 1;
|
Chris@37
|
309
|
Chris@37
|
310 switch (m_minFrequency) {
|
Chris@216
|
311 case 0: default: val = 0; break;
|
Chris@216
|
312 case 10: val = 1; break;
|
Chris@216
|
313 case 20: val = 2; break;
|
Chris@216
|
314 case 40: val = 3; break;
|
Chris@216
|
315 case 100: val = 4; break;
|
Chris@216
|
316 case 250: val = 5; break;
|
Chris@216
|
317 case 500: val = 6; break;
|
Chris@216
|
318 case 1000: val = 7; break;
|
Chris@216
|
319 case 4000: val = 8; break;
|
Chris@216
|
320 case 10000: val = 9; break;
|
Chris@37
|
321 }
|
Chris@37
|
322
|
Chris@87
|
323 } else if (name == "Max Frequency") {
|
Chris@0
|
324
|
Chris@0
|
325 *min = 0;
|
Chris@0
|
326 *max = 9;
|
Chris@216
|
327 *deflt = 6;
|
Chris@0
|
328
|
Chris@0
|
329 switch (m_maxFrequency) {
|
Chris@216
|
330 case 500: val = 0; break;
|
Chris@216
|
331 case 1000: val = 1; break;
|
Chris@216
|
332 case 1500: val = 2; break;
|
Chris@216
|
333 case 2000: val = 3; break;
|
Chris@216
|
334 case 4000: val = 4; break;
|
Chris@216
|
335 case 6000: val = 5; break;
|
Chris@216
|
336 case 8000: val = 6; break;
|
Chris@216
|
337 case 12000: val = 7; break;
|
Chris@216
|
338 case 16000: val = 8; break;
|
Chris@216
|
339 default: val = 9; break;
|
Chris@0
|
340 }
|
Chris@0
|
341
|
Chris@87
|
342 } else if (name == "Frequency Scale") {
|
Chris@0
|
343
|
Chris@0
|
344 *min = 0;
|
Chris@0
|
345 *max = 1;
|
Chris@216
|
346 *deflt = int(LinearFrequencyScale);
|
Chris@216
|
347 val = (int)m_frequencyScale;
|
Chris@0
|
348
|
Chris@87
|
349 } else if (name == "Bin Display") {
|
Chris@35
|
350
|
Chris@35
|
351 *min = 0;
|
Chris@35
|
352 *max = 2;
|
Chris@216
|
353 *deflt = int(AllBins);
|
Chris@216
|
354 val = (int)m_binDisplay;
|
Chris@35
|
355
|
Chris@862
|
356 } else if (name == "Normalization") {
|
Chris@36
|
357
|
Chris@862
|
358 *min = 0;
|
Chris@862
|
359 *max = 3;
|
Chris@1063
|
360 *deflt = int(ColumnOp::NoNormalization);
|
Chris@862
|
361 val = (int)m_normalization;
|
Chris@120
|
362
|
Chris@0
|
363 } else {
|
Chris@216
|
364 val = Layer::getPropertyRangeAndValue(name, min, max, deflt);
|
Chris@0
|
365 }
|
Chris@0
|
366
|
Chris@216
|
367 return val;
|
Chris@0
|
368 }
|
Chris@0
|
369
|
Chris@0
|
370 QString
|
Chris@0
|
371 SpectrogramLayer::getPropertyValueLabel(const PropertyName &name,
|
Chris@9
|
372 int value) const
|
Chris@0
|
373 {
|
Chris@87
|
374 if (name == "Colour") {
|
Chris@196
|
375 return ColourMapper::getColourMapName(value);
|
Chris@0
|
376 }
|
Chris@87
|
377 if (name == "Colour Scale") {
|
Chris@0
|
378 switch (value) {
|
Chris@0
|
379 default:
|
Chris@37
|
380 case 0: return tr("Linear");
|
Chris@37
|
381 case 1: return tr("Meter");
|
Chris@215
|
382 case 2: return tr("dBV^2");
|
Chris@215
|
383 case 3: return tr("dBV");
|
Chris@119
|
384 case 4: return tr("Phase");
|
Chris@0
|
385 }
|
Chris@0
|
386 }
|
Chris@862
|
387 if (name == "Normalization") {
|
Chris@862
|
388 return ""; // icon only
|
Chris@862
|
389 }
|
Chris@87
|
390 if (name == "Window Size") {
|
Chris@0
|
391 return QString("%1").arg(32 << value);
|
Chris@0
|
392 }
|
Chris@97
|
393 if (name == "Window Increment") {
|
Chris@0
|
394 switch (value) {
|
Chris@0
|
395 default:
|
Chris@112
|
396 case 0: return tr("None");
|
Chris@112
|
397 case 1: return tr("25 %");
|
Chris@112
|
398 case 2: return tr("50 %");
|
Chris@112
|
399 case 3: return tr("75 %");
|
Chris@112
|
400 case 4: return tr("87.5 %");
|
Chris@112
|
401 case 5: return tr("93.75 %");
|
Chris@0
|
402 }
|
Chris@0
|
403 }
|
Chris@87
|
404 if (name == "Min Frequency") {
|
Chris@37
|
405 switch (value) {
|
Chris@37
|
406 default:
|
Chris@38
|
407 case 0: return tr("No min");
|
Chris@37
|
408 case 1: return tr("10 Hz");
|
Chris@37
|
409 case 2: return tr("20 Hz");
|
Chris@37
|
410 case 3: return tr("40 Hz");
|
Chris@37
|
411 case 4: return tr("100 Hz");
|
Chris@37
|
412 case 5: return tr("250 Hz");
|
Chris@37
|
413 case 6: return tr("500 Hz");
|
Chris@37
|
414 case 7: return tr("1 KHz");
|
Chris@37
|
415 case 8: return tr("4 KHz");
|
Chris@37
|
416 case 9: return tr("10 KHz");
|
Chris@37
|
417 }
|
Chris@37
|
418 }
|
Chris@87
|
419 if (name == "Max Frequency") {
|
Chris@0
|
420 switch (value) {
|
Chris@0
|
421 default:
|
Chris@0
|
422 case 0: return tr("500 Hz");
|
Chris@0
|
423 case 1: return tr("1 KHz");
|
Chris@0
|
424 case 2: return tr("1.5 KHz");
|
Chris@0
|
425 case 3: return tr("2 KHz");
|
Chris@0
|
426 case 4: return tr("4 KHz");
|
Chris@0
|
427 case 5: return tr("6 KHz");
|
Chris@0
|
428 case 6: return tr("8 KHz");
|
Chris@0
|
429 case 7: return tr("12 KHz");
|
Chris@0
|
430 case 8: return tr("16 KHz");
|
Chris@38
|
431 case 9: return tr("No max");
|
Chris@0
|
432 }
|
Chris@0
|
433 }
|
Chris@87
|
434 if (name == "Frequency Scale") {
|
Chris@0
|
435 switch (value) {
|
Chris@0
|
436 default:
|
Chris@0
|
437 case 0: return tr("Linear");
|
Chris@0
|
438 case 1: return tr("Log");
|
Chris@0
|
439 }
|
Chris@0
|
440 }
|
Chris@87
|
441 if (name == "Bin Display") {
|
Chris@35
|
442 switch (value) {
|
Chris@35
|
443 default:
|
Chris@37
|
444 case 0: return tr("All Bins");
|
Chris@37
|
445 case 1: return tr("Peak Bins");
|
Chris@37
|
446 case 2: return tr("Frequencies");
|
Chris@35
|
447 }
|
Chris@35
|
448 }
|
Chris@0
|
449 return tr("<unknown>");
|
Chris@0
|
450 }
|
Chris@0
|
451
|
Chris@862
|
452 QString
|
Chris@862
|
453 SpectrogramLayer::getPropertyValueIconName(const PropertyName &name,
|
Chris@862
|
454 int value) const
|
Chris@862
|
455 {
|
Chris@862
|
456 if (name == "Normalization") {
|
Chris@862
|
457 switch(value) {
|
Chris@862
|
458 default:
|
Chris@862
|
459 case 0: return "normalise-none";
|
Chris@862
|
460 case 1: return "normalise-columns";
|
Chris@862
|
461 case 2: return "normalise";
|
Chris@862
|
462 case 3: return "normalise-hybrid";
|
Chris@862
|
463 }
|
Chris@862
|
464 }
|
Chris@862
|
465 return "";
|
Chris@862
|
466 }
|
Chris@862
|
467
|
Chris@167
|
468 RangeMapper *
|
Chris@167
|
469 SpectrogramLayer::getNewPropertyRangeMapper(const PropertyName &name) const
|
Chris@167
|
470 {
|
Chris@167
|
471 if (name == "Gain") {
|
Chris@167
|
472 return new LinearRangeMapper(-50, 50, -25, 25, tr("dB"));
|
Chris@167
|
473 }
|
Chris@167
|
474 if (name == "Threshold") {
|
Chris@167
|
475 return new LinearRangeMapper(-50, 0, -50, 0, tr("dB"));
|
Chris@167
|
476 }
|
Chris@167
|
477 return 0;
|
Chris@167
|
478 }
|
Chris@167
|
479
|
Chris@0
|
480 void
|
Chris@0
|
481 SpectrogramLayer::setProperty(const PropertyName &name, int value)
|
Chris@0
|
482 {
|
Chris@87
|
483 if (name == "Gain") {
|
Chris@906
|
484 setGain(float(pow(10, float(value)/20.0)));
|
Chris@87
|
485 } else if (name == "Threshold") {
|
Chris@37
|
486 if (value == -50) setThreshold(0.0);
|
Chris@906
|
487 else setThreshold(float(AudioLevel::dB_to_multiplier(value)));
|
Chris@87
|
488 } else if (name == "Colour Rotation") {
|
Chris@9
|
489 setColourRotation(value);
|
Chris@87
|
490 } else if (name == "Colour") {
|
Chris@197
|
491 setColourMap(value);
|
Chris@87
|
492 } else if (name == "Window Size") {
|
Chris@0
|
493 setWindowSize(32 << value);
|
Chris@97
|
494 } else if (name == "Window Increment") {
|
Chris@97
|
495 setWindowHopLevel(value);
|
Chris@87
|
496 } else if (name == "Min Frequency") {
|
Chris@37
|
497 switch (value) {
|
Chris@37
|
498 default:
|
Chris@37
|
499 case 0: setMinFrequency(0); break;
|
Chris@37
|
500 case 1: setMinFrequency(10); break;
|
Chris@37
|
501 case 2: setMinFrequency(20); break;
|
Chris@37
|
502 case 3: setMinFrequency(40); break;
|
Chris@37
|
503 case 4: setMinFrequency(100); break;
|
Chris@37
|
504 case 5: setMinFrequency(250); break;
|
Chris@37
|
505 case 6: setMinFrequency(500); break;
|
Chris@37
|
506 case 7: setMinFrequency(1000); break;
|
Chris@37
|
507 case 8: setMinFrequency(4000); break;
|
Chris@37
|
508 case 9: setMinFrequency(10000); break;
|
Chris@37
|
509 }
|
Chris@133
|
510 int vs = getCurrentVerticalZoomStep();
|
Chris@133
|
511 if (vs != m_lastEmittedZoomStep) {
|
Chris@133
|
512 emit verticalZoomChanged();
|
Chris@133
|
513 m_lastEmittedZoomStep = vs;
|
Chris@133
|
514 }
|
Chris@87
|
515 } else if (name == "Max Frequency") {
|
Chris@0
|
516 switch (value) {
|
Chris@0
|
517 case 0: setMaxFrequency(500); break;
|
Chris@0
|
518 case 1: setMaxFrequency(1000); break;
|
Chris@0
|
519 case 2: setMaxFrequency(1500); break;
|
Chris@0
|
520 case 3: setMaxFrequency(2000); break;
|
Chris@0
|
521 case 4: setMaxFrequency(4000); break;
|
Chris@0
|
522 case 5: setMaxFrequency(6000); break;
|
Chris@0
|
523 case 6: setMaxFrequency(8000); break;
|
Chris@0
|
524 case 7: setMaxFrequency(12000); break;
|
Chris@0
|
525 case 8: setMaxFrequency(16000); break;
|
Chris@0
|
526 default:
|
Chris@0
|
527 case 9: setMaxFrequency(0); break;
|
Chris@0
|
528 }
|
Chris@133
|
529 int vs = getCurrentVerticalZoomStep();
|
Chris@133
|
530 if (vs != m_lastEmittedZoomStep) {
|
Chris@133
|
531 emit verticalZoomChanged();
|
Chris@133
|
532 m_lastEmittedZoomStep = vs;
|
Chris@133
|
533 }
|
Chris@87
|
534 } else if (name == "Colour Scale") {
|
Chris@0
|
535 switch (value) {
|
Chris@0
|
536 default:
|
Chris@0
|
537 case 0: setColourScale(LinearColourScale); break;
|
Chris@0
|
538 case 1: setColourScale(MeterColourScale); break;
|
Chris@215
|
539 case 2: setColourScale(dBSquaredColourScale); break;
|
Chris@215
|
540 case 3: setColourScale(dBColourScale); break;
|
Chris@119
|
541 case 4: setColourScale(PhaseColourScale); break;
|
Chris@0
|
542 }
|
Chris@87
|
543 } else if (name == "Frequency Scale") {
|
Chris@0
|
544 switch (value) {
|
Chris@0
|
545 default:
|
Chris@0
|
546 case 0: setFrequencyScale(LinearFrequencyScale); break;
|
Chris@0
|
547 case 1: setFrequencyScale(LogFrequencyScale); break;
|
Chris@0
|
548 }
|
Chris@87
|
549 } else if (name == "Bin Display") {
|
Chris@35
|
550 switch (value) {
|
Chris@35
|
551 default:
|
Chris@37
|
552 case 0: setBinDisplay(AllBins); break;
|
Chris@37
|
553 case 1: setBinDisplay(PeakBins); break;
|
Chris@37
|
554 case 2: setBinDisplay(PeakFrequencies); break;
|
Chris@35
|
555 }
|
Chris@862
|
556 } else if (name == "Normalization") {
|
Chris@862
|
557 switch (value) {
|
Chris@862
|
558 default:
|
Chris@1063
|
559 case 0: setNormalization(ColumnOp::NoNormalization); break;
|
Chris@1063
|
560 case 1: setNormalization(ColumnOp::NormalizeColumns); break;
|
Chris@1063
|
561 case 2: setNormalization(ColumnOp::NormalizeVisibleArea); break;
|
Chris@1063
|
562 case 3: setNormalization(ColumnOp::NormalizeHybrid); break;
|
Chris@862
|
563 }
|
Chris@0
|
564 }
|
Chris@0
|
565 }
|
Chris@0
|
566
|
Chris@0
|
567 void
|
Chris@478
|
568 SpectrogramLayer::invalidateImageCaches()
|
Chris@95
|
569 {
|
Chris@1044
|
570 #ifdef DEBUG_SPECTROGRAM
|
Chris@1044
|
571 cerr << "SpectrogramLayer::invalidateImageCaches called" << endl;
|
Chris@1044
|
572 #endif
|
Chris@478
|
573 for (ViewImageCache::iterator i = m_imageCaches.begin();
|
Chris@478
|
574 i != m_imageCaches.end(); ++i) {
|
Chris@1030
|
575 i->second.invalidate();
|
Chris@95
|
576 }
|
Chris@95
|
577 }
|
Chris@95
|
578
|
Chris@95
|
579 void
|
Chris@122
|
580 SpectrogramLayer::preferenceChanged(PropertyContainer::PropertyName name)
|
Chris@122
|
581 {
|
Chris@587
|
582 SVDEBUG << "SpectrogramLayer::preferenceChanged(" << name << ")" << endl;
|
Chris@122
|
583
|
Chris@122
|
584 if (name == "Window Type") {
|
Chris@122
|
585 setWindowType(Preferences::getInstance()->getWindowType());
|
Chris@122
|
586 return;
|
Chris@122
|
587 }
|
Chris@490
|
588 if (name == "Spectrogram Y Smoothing") {
|
Chris@1086
|
589 setWindowSize(m_windowSize);
|
Chris@490
|
590 invalidateImageCaches();
|
Chris@490
|
591 invalidateMagnitudes();
|
Chris@490
|
592 emit layerParametersChanged();
|
Chris@490
|
593 }
|
Chris@490
|
594 if (name == "Spectrogram X Smoothing") {
|
Chris@478
|
595 invalidateImageCaches();
|
Chris@122
|
596 invalidateMagnitudes();
|
Chris@122
|
597 emit layerParametersChanged();
|
Chris@122
|
598 }
|
Chris@122
|
599 if (name == "Tuning Frequency") {
|
Chris@122
|
600 emit layerParametersChanged();
|
Chris@122
|
601 }
|
Chris@122
|
602 }
|
Chris@122
|
603
|
Chris@122
|
604 void
|
Chris@0
|
605 SpectrogramLayer::setChannel(int ch)
|
Chris@0
|
606 {
|
Chris@0
|
607 if (m_channel == ch) return;
|
Chris@0
|
608
|
Chris@478
|
609 invalidateImageCaches();
|
Chris@0
|
610 m_channel = ch;
|
Chris@1088
|
611 invalidateFFTModel();
|
Chris@9
|
612
|
Chris@0
|
613 emit layerParametersChanged();
|
Chris@0
|
614 }
|
Chris@0
|
615
|
Chris@0
|
616 int
|
Chris@0
|
617 SpectrogramLayer::getChannel() const
|
Chris@0
|
618 {
|
Chris@0
|
619 return m_channel;
|
Chris@0
|
620 }
|
Chris@0
|
621
|
Chris@1086
|
622 int
|
Chris@1086
|
623 SpectrogramLayer::getFFTOversampling() const
|
Chris@1086
|
624 {
|
Chris@1086
|
625 if (m_binDisplay != AllBins) {
|
Chris@1086
|
626 return 1;
|
Chris@1086
|
627 }
|
Chris@1086
|
628
|
Chris@1086
|
629 Preferences::SpectrogramSmoothing smoothing =
|
Chris@1086
|
630 Preferences::getInstance()->getSpectrogramSmoothing();
|
Chris@1086
|
631
|
Chris@1086
|
632 if (smoothing == Preferences::NoSpectrogramSmoothing ||
|
Chris@1086
|
633 smoothing == Preferences::SpectrogramInterpolated) {
|
Chris@1086
|
634 return 1;
|
Chris@1086
|
635 }
|
Chris@1086
|
636
|
Chris@1086
|
637 return 4;
|
Chris@1086
|
638 }
|
Chris@1086
|
639
|
Chris@1087
|
640 int
|
Chris@1087
|
641 SpectrogramLayer::getFFTSize() const
|
Chris@1087
|
642 {
|
Chris@1087
|
643 return m_windowSize * getFFTOversampling();
|
Chris@1087
|
644 }
|
Chris@1087
|
645
|
Chris@0
|
646 void
|
Chris@805
|
647 SpectrogramLayer::setWindowSize(int ws)
|
Chris@0
|
648 {
|
Chris@1087
|
649 if (m_windowSize == ws) return;
|
Chris@0
|
650
|
Chris@478
|
651 invalidateImageCaches();
|
Chris@0
|
652
|
Chris@0
|
653 m_windowSize = ws;
|
Chris@0
|
654
|
Chris@1088
|
655 invalidateFFTModel();
|
Chris@9
|
656
|
Chris@9
|
657 emit layerParametersChanged();
|
Chris@0
|
658 }
|
Chris@0
|
659
|
Chris@805
|
660 int
|
Chris@0
|
661 SpectrogramLayer::getWindowSize() const
|
Chris@0
|
662 {
|
Chris@0
|
663 return m_windowSize;
|
Chris@0
|
664 }
|
Chris@0
|
665
|
Chris@0
|
666 void
|
Chris@805
|
667 SpectrogramLayer::setWindowHopLevel(int v)
|
Chris@0
|
668 {
|
Chris@97
|
669 if (m_windowHopLevel == v) return;
|
Chris@0
|
670
|
Chris@478
|
671 invalidateImageCaches();
|
Chris@0
|
672
|
Chris@97
|
673 m_windowHopLevel = v;
|
Chris@0
|
674
|
Chris@1088
|
675 invalidateFFTModel();
|
Chris@9
|
676
|
Chris@9
|
677 emit layerParametersChanged();
|
Chris@9
|
678
|
Chris@110
|
679 // fillCache();
|
Chris@0
|
680 }
|
Chris@0
|
681
|
Chris@805
|
682 int
|
Chris@97
|
683 SpectrogramLayer::getWindowHopLevel() const
|
Chris@0
|
684 {
|
Chris@97
|
685 return m_windowHopLevel;
|
Chris@0
|
686 }
|
Chris@0
|
687
|
Chris@0
|
688 void
|
Chris@0
|
689 SpectrogramLayer::setWindowType(WindowType w)
|
Chris@0
|
690 {
|
Chris@0
|
691 if (m_windowType == w) return;
|
Chris@0
|
692
|
Chris@478
|
693 invalidateImageCaches();
|
Chris@0
|
694
|
Chris@0
|
695 m_windowType = w;
|
Chris@110
|
696
|
Chris@1088
|
697 invalidateFFTModel();
|
Chris@9
|
698
|
Chris@9
|
699 emit layerParametersChanged();
|
Chris@0
|
700 }
|
Chris@0
|
701
|
Chris@0
|
702 WindowType
|
Chris@0
|
703 SpectrogramLayer::getWindowType() const
|
Chris@0
|
704 {
|
Chris@0
|
705 return m_windowType;
|
Chris@0
|
706 }
|
Chris@0
|
707
|
Chris@0
|
708 void
|
Chris@0
|
709 SpectrogramLayer::setGain(float gain)
|
Chris@0
|
710 {
|
Chris@587
|
711 // SVDEBUG << "SpectrogramLayer::setGain(" << gain << ") (my gain is now "
|
Chris@585
|
712 // << m_gain << ")" << endl;
|
Chris@55
|
713
|
Chris@40
|
714 if (m_gain == gain) return;
|
Chris@0
|
715
|
Chris@478
|
716 invalidateImageCaches();
|
Chris@0
|
717
|
Chris@0
|
718 m_gain = gain;
|
Chris@0
|
719
|
Chris@9
|
720 emit layerParametersChanged();
|
Chris@0
|
721 }
|
Chris@0
|
722
|
Chris@0
|
723 float
|
Chris@0
|
724 SpectrogramLayer::getGain() const
|
Chris@0
|
725 {
|
Chris@0
|
726 return m_gain;
|
Chris@0
|
727 }
|
Chris@0
|
728
|
Chris@0
|
729 void
|
Chris@37
|
730 SpectrogramLayer::setThreshold(float threshold)
|
Chris@37
|
731 {
|
Chris@40
|
732 if (m_threshold == threshold) return;
|
Chris@37
|
733
|
Chris@478
|
734 invalidateImageCaches();
|
Chris@37
|
735
|
Chris@37
|
736 m_threshold = threshold;
|
Chris@37
|
737
|
Chris@37
|
738 emit layerParametersChanged();
|
Chris@37
|
739 }
|
Chris@37
|
740
|
Chris@37
|
741 float
|
Chris@37
|
742 SpectrogramLayer::getThreshold() const
|
Chris@37
|
743 {
|
Chris@37
|
744 return m_threshold;
|
Chris@37
|
745 }
|
Chris@37
|
746
|
Chris@37
|
747 void
|
Chris@805
|
748 SpectrogramLayer::setMinFrequency(int mf)
|
Chris@37
|
749 {
|
Chris@37
|
750 if (m_minFrequency == mf) return;
|
Chris@37
|
751
|
Chris@587
|
752 // SVDEBUG << "SpectrogramLayer::setMinFrequency: " << mf << endl;
|
Chris@187
|
753
|
Chris@478
|
754 invalidateImageCaches();
|
Chris@119
|
755 invalidateMagnitudes();
|
Chris@37
|
756
|
Chris@37
|
757 m_minFrequency = mf;
|
Chris@37
|
758
|
Chris@37
|
759 emit layerParametersChanged();
|
Chris@37
|
760 }
|
Chris@37
|
761
|
Chris@805
|
762 int
|
Chris@37
|
763 SpectrogramLayer::getMinFrequency() const
|
Chris@37
|
764 {
|
Chris@37
|
765 return m_minFrequency;
|
Chris@37
|
766 }
|
Chris@37
|
767
|
Chris@37
|
768 void
|
Chris@805
|
769 SpectrogramLayer::setMaxFrequency(int mf)
|
Chris@0
|
770 {
|
Chris@0
|
771 if (m_maxFrequency == mf) return;
|
Chris@0
|
772
|
Chris@587
|
773 // SVDEBUG << "SpectrogramLayer::setMaxFrequency: " << mf << endl;
|
Chris@187
|
774
|
Chris@478
|
775 invalidateImageCaches();
|
Chris@119
|
776 invalidateMagnitudes();
|
Chris@0
|
777
|
Chris@0
|
778 m_maxFrequency = mf;
|
Chris@0
|
779
|
Chris@9
|
780 emit layerParametersChanged();
|
Chris@0
|
781 }
|
Chris@0
|
782
|
Chris@805
|
783 int
|
Chris@0
|
784 SpectrogramLayer::getMaxFrequency() const
|
Chris@0
|
785 {
|
Chris@0
|
786 return m_maxFrequency;
|
Chris@0
|
787 }
|
Chris@0
|
788
|
Chris@0
|
789 void
|
Chris@9
|
790 SpectrogramLayer::setColourRotation(int r)
|
Chris@9
|
791 {
|
Chris@478
|
792 invalidateImageCaches();
|
Chris@9
|
793
|
Chris@9
|
794 if (r < 0) r = 0;
|
Chris@9
|
795 if (r > 256) r = 256;
|
Chris@9
|
796 int distance = r - m_colourRotation;
|
Chris@9
|
797
|
Chris@9
|
798 if (distance != 0) {
|
Chris@197
|
799 rotatePalette(-distance);
|
Chris@9
|
800 m_colourRotation = r;
|
Chris@9
|
801 }
|
Chris@9
|
802
|
Chris@9
|
803 emit layerParametersChanged();
|
Chris@9
|
804 }
|
Chris@9
|
805
|
Chris@9
|
806 void
|
Chris@0
|
807 SpectrogramLayer::setColourScale(ColourScale colourScale)
|
Chris@0
|
808 {
|
Chris@0
|
809 if (m_colourScale == colourScale) return;
|
Chris@0
|
810
|
Chris@478
|
811 invalidateImageCaches();
|
Chris@0
|
812
|
Chris@0
|
813 m_colourScale = colourScale;
|
Chris@0
|
814
|
Chris@9
|
815 emit layerParametersChanged();
|
Chris@0
|
816 }
|
Chris@0
|
817
|
Chris@0
|
818 SpectrogramLayer::ColourScale
|
Chris@0
|
819 SpectrogramLayer::getColourScale() const
|
Chris@0
|
820 {
|
Chris@0
|
821 return m_colourScale;
|
Chris@0
|
822 }
|
Chris@0
|
823
|
Chris@0
|
824 void
|
Chris@197
|
825 SpectrogramLayer::setColourMap(int map)
|
Chris@0
|
826 {
|
Chris@197
|
827 if (m_colourMap == map) return;
|
Chris@0
|
828
|
Chris@478
|
829 invalidateImageCaches();
|
Chris@0
|
830
|
Chris@197
|
831 m_colourMap = map;
|
Chris@197
|
832 initialisePalette();
|
Chris@9
|
833
|
Chris@0
|
834 emit layerParametersChanged();
|
Chris@0
|
835 }
|
Chris@0
|
836
|
Chris@196
|
837 int
|
Chris@197
|
838 SpectrogramLayer::getColourMap() const
|
Chris@0
|
839 {
|
Chris@197
|
840 return m_colourMap;
|
Chris@0
|
841 }
|
Chris@0
|
842
|
Chris@0
|
843 void
|
Chris@0
|
844 SpectrogramLayer::setFrequencyScale(FrequencyScale frequencyScale)
|
Chris@0
|
845 {
|
Chris@0
|
846 if (m_frequencyScale == frequencyScale) return;
|
Chris@0
|
847
|
Chris@478
|
848 invalidateImageCaches();
|
Chris@0
|
849 m_frequencyScale = frequencyScale;
|
Chris@9
|
850
|
Chris@9
|
851 emit layerParametersChanged();
|
Chris@0
|
852 }
|
Chris@0
|
853
|
Chris@0
|
854 SpectrogramLayer::FrequencyScale
|
Chris@0
|
855 SpectrogramLayer::getFrequencyScale() const
|
Chris@0
|
856 {
|
Chris@0
|
857 return m_frequencyScale;
|
Chris@0
|
858 }
|
Chris@0
|
859
|
Chris@0
|
860 void
|
Chris@37
|
861 SpectrogramLayer::setBinDisplay(BinDisplay binDisplay)
|
Chris@35
|
862 {
|
Chris@37
|
863 if (m_binDisplay == binDisplay) return;
|
Chris@35
|
864
|
Chris@478
|
865 invalidateImageCaches();
|
Chris@37
|
866 m_binDisplay = binDisplay;
|
Chris@35
|
867
|
Chris@35
|
868 emit layerParametersChanged();
|
Chris@35
|
869 }
|
Chris@35
|
870
|
Chris@37
|
871 SpectrogramLayer::BinDisplay
|
Chris@37
|
872 SpectrogramLayer::getBinDisplay() const
|
Chris@35
|
873 {
|
Chris@37
|
874 return m_binDisplay;
|
Chris@35
|
875 }
|
Chris@35
|
876
|
Chris@35
|
877 void
|
Chris@1063
|
878 SpectrogramLayer::setNormalization(ColumnOp::Normalization n)
|
Chris@36
|
879 {
|
Chris@862
|
880 if (m_normalization == n) return;
|
Chris@36
|
881
|
Chris@478
|
882 invalidateImageCaches();
|
Chris@119
|
883 invalidateMagnitudes();
|
Chris@862
|
884 m_normalization = n;
|
Chris@36
|
885
|
Chris@36
|
886 emit layerParametersChanged();
|
Chris@36
|
887 }
|
Chris@36
|
888
|
Chris@1063
|
889 ColumnOp::Normalization
|
Chris@862
|
890 SpectrogramLayer::getNormalization() const
|
Chris@36
|
891 {
|
Chris@862
|
892 return m_normalization;
|
Chris@36
|
893 }
|
Chris@36
|
894
|
Chris@36
|
895 void
|
Chris@918
|
896 SpectrogramLayer::setLayerDormant(const LayerGeometryProvider *v, bool dormant)
|
Chris@29
|
897 {
|
Chris@33
|
898 if (dormant) {
|
Chris@33
|
899
|
Chris@331
|
900 #ifdef DEBUG_SPECTROGRAM_REPAINT
|
Chris@985
|
901 cerr << "SpectrogramLayer::setLayerDormant(" << dormant << ")"
|
Chris@585
|
902 << endl;
|
Chris@331
|
903 #endif
|
Chris@331
|
904
|
Chris@131
|
905 if (isLayerDormant(v)) {
|
Chris@131
|
906 return;
|
Chris@131
|
907 }
|
Chris@131
|
908
|
Chris@131
|
909 Layer::setLayerDormant(v, true);
|
Chris@33
|
910
|
Chris@920
|
911 const View *view = v->getView();
|
Chris@920
|
912
|
Chris@478
|
913 invalidateImageCaches();
|
Chris@988
|
914
|
Chris@1030
|
915 m_imageCaches.erase(view->getId());
|
Chris@1030
|
916
|
Chris@1088
|
917 //!!! in theory we should call invalidateFFTModel() if and
|
Chris@1088
|
918 //!!! only if there are no remaining views in which we are not
|
Chris@1088
|
919 //!!! dormant
|
Chris@33
|
920
|
Chris@33
|
921 } else {
|
Chris@33
|
922
|
Chris@131
|
923 Layer::setLayerDormant(v, false);
|
Chris@33
|
924 }
|
Chris@29
|
925 }
|
Chris@29
|
926
|
Chris@29
|
927 void
|
Chris@0
|
928 SpectrogramLayer::cacheInvalid()
|
Chris@0
|
929 {
|
Chris@391
|
930 #ifdef DEBUG_SPECTROGRAM_REPAINT
|
Chris@985
|
931 cerr << "SpectrogramLayer::cacheInvalid()" << endl;
|
Chris@391
|
932 #endif
|
Chris@391
|
933
|
Chris@478
|
934 invalidateImageCaches();
|
Chris@119
|
935 invalidateMagnitudes();
|
Chris@0
|
936 }
|
Chris@0
|
937
|
Chris@0
|
938 void
|
Chris@1037
|
939 SpectrogramLayer::cacheInvalid(
|
Chris@1037
|
940 #ifdef DEBUG_SPECTROGRAM_REPAINT
|
Chris@1037
|
941 sv_frame_t from, sv_frame_t to
|
Chris@1037
|
942 #else
|
Chris@1037
|
943 sv_frame_t , sv_frame_t
|
Chris@1037
|
944 #endif
|
Chris@1037
|
945 )
|
Chris@0
|
946 {
|
Chris@391
|
947 #ifdef DEBUG_SPECTROGRAM_REPAINT
|
Chris@985
|
948 cerr << "SpectrogramLayer::cacheInvalid(" << from << ", " << to << ")" << endl;
|
Chris@391
|
949 #endif
|
Chris@391
|
950
|
Chris@1030
|
951 // We used to call invalidateMagnitudes(from, to) to invalidate
|
Chris@1030
|
952 // only those caches whose views contained some of the (from, to)
|
Chris@1030
|
953 // range. That's the right thing to do; it has been lost in
|
Chris@1030
|
954 // pulling out the image cache code, but it might not matter very
|
Chris@1030
|
955 // much, since the underlying models for spectrogram layers don't
|
Chris@1030
|
956 // change very often. Let's see.
|
Chris@1030
|
957 invalidateImageCaches();
|
Chris@391
|
958 invalidateMagnitudes();
|
Chris@0
|
959 }
|
Chris@0
|
960
|
Chris@224
|
961 bool
|
Chris@224
|
962 SpectrogramLayer::hasLightBackground() const
|
Chris@224
|
963 {
|
Chris@287
|
964 return ColourMapper(m_colourMap, 1.f, 255.f).hasLightBackground();
|
Chris@224
|
965 }
|
Chris@224
|
966
|
Chris@0
|
967 void
|
Chris@197
|
968 SpectrogramLayer::initialisePalette()
|
Chris@0
|
969 {
|
Chris@10
|
970 int formerRotation = m_colourRotation;
|
Chris@10
|
971
|
Chris@197
|
972 if (m_colourMap == (int)ColourMapper::BlackOnWhite) {
|
Chris@197
|
973 m_palette.setColour(NO_VALUE, Qt::white);
|
Chris@38
|
974 } else {
|
Chris@197
|
975 m_palette.setColour(NO_VALUE, Qt::black);
|
Chris@38
|
976 }
|
Chris@0
|
977
|
Chris@197
|
978 ColourMapper mapper(m_colourMap, 1.f, 255.f);
|
Chris@196
|
979
|
Chris@0
|
980 for (int pixel = 1; pixel < 256; ++pixel) {
|
Chris@907
|
981 m_palette.setColour((unsigned char)pixel, mapper.map(pixel));
|
Chris@0
|
982 }
|
Chris@9
|
983
|
Chris@196
|
984 m_crosshairColour = mapper.getContrastingColour();
|
Chris@196
|
985
|
Chris@9
|
986 m_colourRotation = 0;
|
Chris@197
|
987 rotatePalette(m_colourRotation - formerRotation);
|
Chris@10
|
988 m_colourRotation = formerRotation;
|
Chris@478
|
989
|
Chris@478
|
990 m_drawBuffer = QImage();
|
Chris@9
|
991 }
|
Chris@9
|
992
|
Chris@9
|
993 void
|
Chris@197
|
994 SpectrogramLayer::rotatePalette(int distance)
|
Chris@9
|
995 {
|
Chris@31
|
996 QColor newPixels[256];
|
Chris@9
|
997
|
Chris@197
|
998 newPixels[NO_VALUE] = m_palette.getColour(NO_VALUE);
|
Chris@9
|
999
|
Chris@9
|
1000 for (int pixel = 1; pixel < 256; ++pixel) {
|
Chris@9
|
1001 int target = pixel + distance;
|
Chris@9
|
1002 while (target < 1) target += 255;
|
Chris@9
|
1003 while (target > 255) target -= 255;
|
Chris@907
|
1004 newPixels[target] = m_palette.getColour((unsigned char)pixel);
|
Chris@9
|
1005 }
|
Chris@9
|
1006
|
Chris@9
|
1007 for (int pixel = 0; pixel < 256; ++pixel) {
|
Chris@907
|
1008 m_palette.setColour((unsigned char)pixel, newPixels[pixel]);
|
Chris@9
|
1009 }
|
Chris@478
|
1010
|
Chris@478
|
1011 m_drawBuffer = QImage();
|
Chris@0
|
1012 }
|
Chris@0
|
1013
|
Chris@38
|
1014 unsigned char
|
Chris@918
|
1015 SpectrogramLayer::getDisplayValue(LayerGeometryProvider *v, double input) const
|
Chris@38
|
1016 {
|
Chris@38
|
1017 int value;
|
Chris@37
|
1018
|
Chris@907
|
1019 double min = 0.0;
|
Chris@907
|
1020 double max = 1.0;
|
Chris@120
|
1021
|
Chris@1063
|
1022 if (m_normalization == ColumnOp::NormalizeVisibleArea) {
|
Chris@1030
|
1023 min = m_viewMags[v->getId()].getMin();
|
Chris@1030
|
1024 max = m_viewMags[v->getId()].getMax();
|
Chris@1063
|
1025 } else if (m_normalization != ColumnOp::NormalizeColumns) {
|
Chris@224
|
1026 if (m_colourScale == LinearColourScale //||
|
Chris@224
|
1027 // m_colourScale == MeterColourScale) {
|
Chris@224
|
1028 ) {
|
Chris@907
|
1029 max = 0.1;
|
Chris@120
|
1030 }
|
Chris@120
|
1031 }
|
Chris@120
|
1032
|
Chris@907
|
1033 double thresh = -80.0;
|
Chris@907
|
1034
|
Chris@907
|
1035 if (max == 0.0) max = 1.0;
|
Chris@907
|
1036 if (max == min) min = max - 0.0001;
|
Chris@119
|
1037
|
Chris@40
|
1038 switch (m_colourScale) {
|
Chris@40
|
1039
|
Chris@40
|
1040 default:
|
Chris@40
|
1041 case LinearColourScale:
|
Chris@907
|
1042 value = int(((input - min) / (max - min)) * 255.0) + 1;
|
Chris@40
|
1043 break;
|
Chris@40
|
1044
|
Chris@40
|
1045 case MeterColourScale:
|
Chris@210
|
1046 value = AudioLevel::multiplier_to_preview
|
Chris@210
|
1047 ((input - min) / (max - min), 254) + 1;
|
Chris@40
|
1048 break;
|
Chris@119
|
1049
|
Chris@210
|
1050 case dBSquaredColourScale:
|
Chris@215
|
1051 input = ((input - min) * (input - min)) / ((max - min) * (max - min));
|
Chris@907
|
1052 if (input > 0.0) {
|
Chris@907
|
1053 input = 10.0 * log10(input);
|
Chris@133
|
1054 } else {
|
Chris@133
|
1055 input = thresh;
|
Chris@133
|
1056 }
|
Chris@907
|
1057 if (min > 0.0) {
|
Chris@907
|
1058 thresh = 10.0 * log10(min * min);
|
Chris@907
|
1059 if (thresh < -80.0) thresh = -80.0;
|
Chris@119
|
1060 }
|
Chris@119
|
1061 input = (input - thresh) / (-thresh);
|
Chris@907
|
1062 if (input < 0.0) input = 0.0;
|
Chris@907
|
1063 if (input > 1.0) input = 1.0;
|
Chris@907
|
1064 value = int(input * 255.0) + 1;
|
Chris@119
|
1065 break;
|
Chris@40
|
1066
|
Chris@215
|
1067 case dBColourScale:
|
Chris@215
|
1068 //!!! experiment with normalizing the visible area this way.
|
Chris@215
|
1069 //In any case, we need to have some indication of what the dB
|
Chris@215
|
1070 //scale is relative to.
|
Chris@215
|
1071 input = (input - min) / (max - min);
|
Chris@907
|
1072 if (input > 0.0) {
|
Chris@907
|
1073 input = 10.0 * log10(input);
|
Chris@215
|
1074 } else {
|
Chris@215
|
1075 input = thresh;
|
Chris@215
|
1076 }
|
Chris@907
|
1077 if (min > 0.0) {
|
Chris@907
|
1078 thresh = 10.0 * log10(min);
|
Chris@907
|
1079 if (thresh < -80.0) thresh = -80.0;
|
Chris@215
|
1080 }
|
Chris@215
|
1081 input = (input - thresh) / (-thresh);
|
Chris@907
|
1082 if (input < 0.0) input = 0.0;
|
Chris@907
|
1083 if (input > 1.0) input = 1.0;
|
Chris@907
|
1084 value = int(input * 255.0) + 1;
|
Chris@215
|
1085 break;
|
Chris@215
|
1086
|
Chris@40
|
1087 case PhaseColourScale:
|
Chris@40
|
1088 value = int((input * 127.0 / M_PI) + 128);
|
Chris@40
|
1089 break;
|
Chris@0
|
1090 }
|
Chris@210
|
1091
|
Chris@38
|
1092 if (value > UCHAR_MAX) value = UCHAR_MAX;
|
Chris@38
|
1093 if (value < 0) value = 0;
|
Chris@907
|
1094 return (unsigned char)value;
|
Chris@0
|
1095 }
|
Chris@0
|
1096
|
Chris@905
|
1097 double
|
Chris@40
|
1098 SpectrogramLayer::getEffectiveMinFrequency() const
|
Chris@40
|
1099 {
|
Chris@907
|
1100 sv_samplerate_t sr = m_model->getSampleRate();
|
Chris@1087
|
1101 double minf = double(sr) / getFFTSize();
|
Chris@40
|
1102
|
Chris@40
|
1103 if (m_minFrequency > 0.0) {
|
Chris@1087
|
1104 int minbin = int((double(m_minFrequency) * getFFTSize()) / sr + 0.01);
|
Chris@40
|
1105 if (minbin < 1) minbin = 1;
|
Chris@1087
|
1106 minf = minbin * sr / getFFTSize();
|
Chris@40
|
1107 }
|
Chris@40
|
1108
|
Chris@40
|
1109 return minf;
|
Chris@40
|
1110 }
|
Chris@40
|
1111
|
Chris@905
|
1112 double
|
Chris@40
|
1113 SpectrogramLayer::getEffectiveMaxFrequency() const
|
Chris@40
|
1114 {
|
Chris@907
|
1115 sv_samplerate_t sr = m_model->getSampleRate();
|
Chris@905
|
1116 double maxf = double(sr) / 2;
|
Chris@40
|
1117
|
Chris@40
|
1118 if (m_maxFrequency > 0.0) {
|
Chris@1087
|
1119 int maxbin = int((double(m_maxFrequency) * getFFTSize()) / sr + 0.1);
|
Chris@1087
|
1120 if (maxbin > getFFTSize() / 2) maxbin = getFFTSize() / 2;
|
Chris@1087
|
1121 maxf = maxbin * sr / getFFTSize();
|
Chris@40
|
1122 }
|
Chris@40
|
1123
|
Chris@40
|
1124 return maxf;
|
Chris@40
|
1125 }
|
Chris@40
|
1126
|
Chris@0
|
1127 bool
|
Chris@918
|
1128 SpectrogramLayer::getYBinRange(LayerGeometryProvider *v, int y, double &q0, double &q1) const
|
Chris@0
|
1129 {
|
Chris@382
|
1130 Profiler profiler("SpectrogramLayer::getYBinRange");
|
Chris@382
|
1131
|
Chris@918
|
1132 int h = v->getPaintHeight();
|
Chris@0
|
1133 if (y < 0 || y >= h) return false;
|
Chris@0
|
1134
|
Chris@907
|
1135 sv_samplerate_t sr = m_model->getSampleRate();
|
Chris@905
|
1136 double minf = getEffectiveMinFrequency();
|
Chris@905
|
1137 double maxf = getEffectiveMaxFrequency();
|
Chris@0
|
1138
|
Chris@38
|
1139 bool logarithmic = (m_frequencyScale == LogFrequencyScale);
|
Chris@38
|
1140
|
Chris@44
|
1141 q0 = v->getFrequencyForY(y, minf, maxf, logarithmic);
|
Chris@44
|
1142 q1 = v->getFrequencyForY(y - 1, minf, maxf, logarithmic);
|
Chris@38
|
1143
|
Chris@1086
|
1144 // Now map these on to ("proportions of") actual bins
|
Chris@490
|
1145
|
Chris@1087
|
1146 q0 = (q0 * getFFTSize()) / sr;
|
Chris@1087
|
1147 q1 = (q1 * getFFTSize()) / sr;
|
Chris@0
|
1148
|
Chris@0
|
1149 return true;
|
Chris@0
|
1150 }
|
Chris@486
|
1151
|
Chris@1085
|
1152 double
|
Chris@1085
|
1153 SpectrogramLayer::getYForBin(LayerGeometryProvider *, double bin) const {
|
Chris@1085
|
1154 //!!! not implemented
|
Chris@1085
|
1155 throw std::logic_error("not implemented");
|
Chris@1085
|
1156 }
|
Chris@1085
|
1157
|
Chris@1085
|
1158 double
|
Chris@1085
|
1159 SpectrogramLayer::getBinForY(LayerGeometryProvider *v, double y) const
|
Chris@1085
|
1160 {
|
Chris@1085
|
1161 //!!! overlap with range methods above (but using double arg)
|
Chris@1085
|
1162 //!!! tidy this
|
Chris@38
|
1163
|
Chris@1085
|
1164 int h = v->getPaintHeight();
|
Chris@1085
|
1165 if (y < 0 || y >= h) return false;
|
Chris@1085
|
1166
|
Chris@1085
|
1167 sv_samplerate_t sr = m_model->getSampleRate();
|
Chris@1085
|
1168 double minf = getEffectiveMinFrequency();
|
Chris@1085
|
1169 double maxf = getEffectiveMaxFrequency();
|
Chris@1085
|
1170
|
Chris@1085
|
1171 bool logarithmic = (m_frequencyScale == LogFrequencyScale);
|
Chris@1085
|
1172
|
Chris@1085
|
1173 double q = v->getFrequencyForY(y, minf, maxf, logarithmic);
|
Chris@1085
|
1174
|
Chris@1086
|
1175 // Now map on to ("proportions of") actual bins
|
Chris@1085
|
1176
|
Chris@1087
|
1177 q = (q * getFFTSize()) / sr;
|
Chris@1085
|
1178
|
Chris@1085
|
1179 return q;
|
Chris@1085
|
1180 }
|
Chris@1085
|
1181
|
Chris@0
|
1182 bool
|
Chris@918
|
1183 SpectrogramLayer::getXBinRange(LayerGeometryProvider *v, int x, double &s0, double &s1) const
|
Chris@0
|
1184 {
|
Chris@907
|
1185 sv_frame_t modelStart = m_model->getStartFrame();
|
Chris@907
|
1186 sv_frame_t modelEnd = m_model->getEndFrame();
|
Chris@0
|
1187
|
Chris@0
|
1188 // Each pixel column covers an exact range of sample frames:
|
Chris@907
|
1189 sv_frame_t f0 = v->getFrameForX(x) - modelStart;
|
Chris@907
|
1190 sv_frame_t f1 = v->getFrameForX(x + 1) - modelStart - 1;
|
Chris@20
|
1191
|
Chris@41
|
1192 if (f1 < int(modelStart) || f0 > int(modelEnd)) {
|
Chris@41
|
1193 return false;
|
Chris@41
|
1194 }
|
Chris@20
|
1195
|
Chris@0
|
1196 // And that range may be drawn from a possibly non-integral
|
Chris@0
|
1197 // range of spectrogram windows:
|
Chris@0
|
1198
|
Chris@805
|
1199 int windowIncrement = getWindowIncrement();
|
Chris@905
|
1200 s0 = double(f0) / windowIncrement;
|
Chris@905
|
1201 s1 = double(f1) / windowIncrement;
|
Chris@0
|
1202
|
Chris@0
|
1203 return true;
|
Chris@0
|
1204 }
|
Chris@0
|
1205
|
Chris@0
|
1206 bool
|
Chris@918
|
1207 SpectrogramLayer::getXBinSourceRange(LayerGeometryProvider *v, int x, RealTime &min, RealTime &max) const
|
Chris@0
|
1208 {
|
Chris@905
|
1209 double s0 = 0, s1 = 0;
|
Chris@44
|
1210 if (!getXBinRange(v, x, s0, s1)) return false;
|
Chris@0
|
1211
|
Chris@0
|
1212 int s0i = int(s0 + 0.001);
|
Chris@0
|
1213 int s1i = int(s1);
|
Chris@0
|
1214
|
Chris@0
|
1215 int windowIncrement = getWindowIncrement();
|
Chris@0
|
1216 int w0 = s0i * windowIncrement - (m_windowSize - windowIncrement)/2;
|
Chris@0
|
1217 int w1 = s1i * windowIncrement + windowIncrement +
|
Chris@0
|
1218 (m_windowSize - windowIncrement)/2 - 1;
|
Chris@0
|
1219
|
Chris@0
|
1220 min = RealTime::frame2RealTime(w0, m_model->getSampleRate());
|
Chris@0
|
1221 max = RealTime::frame2RealTime(w1, m_model->getSampleRate());
|
Chris@0
|
1222 return true;
|
Chris@0
|
1223 }
|
Chris@0
|
1224
|
Chris@0
|
1225 bool
|
Chris@918
|
1226 SpectrogramLayer::getYBinSourceRange(LayerGeometryProvider *v, int y, double &freqMin, double &freqMax)
|
Chris@0
|
1227 const
|
Chris@0
|
1228 {
|
Chris@905
|
1229 double q0 = 0, q1 = 0;
|
Chris@44
|
1230 if (!getYBinRange(v, y, q0, q1)) return false;
|
Chris@0
|
1231
|
Chris@0
|
1232 int q0i = int(q0 + 0.001);
|
Chris@0
|
1233 int q1i = int(q1);
|
Chris@0
|
1234
|
Chris@907
|
1235 sv_samplerate_t sr = m_model->getSampleRate();
|
Chris@0
|
1236
|
Chris@0
|
1237 for (int q = q0i; q <= q1i; ++q) {
|
Chris@1087
|
1238 if (q == q0i) freqMin = (sr * q) / getFFTSize();
|
Chris@1087
|
1239 if (q == q1i) freqMax = (sr * (q+1)) / getFFTSize();
|
Chris@0
|
1240 }
|
Chris@0
|
1241 return true;
|
Chris@0
|
1242 }
|
Chris@35
|
1243
|
Chris@35
|
1244 bool
|
Chris@918
|
1245 SpectrogramLayer::getAdjustedYBinSourceRange(LayerGeometryProvider *v, int x, int y,
|
Chris@905
|
1246 double &freqMin, double &freqMax,
|
Chris@905
|
1247 double &adjFreqMin, double &adjFreqMax)
|
Chris@35
|
1248 const
|
Chris@35
|
1249 {
|
Chris@277
|
1250 if (!m_model || !m_model->isOK() || !m_model->isReady()) {
|
Chris@277
|
1251 return false;
|
Chris@277
|
1252 }
|
Chris@277
|
1253
|
Chris@1088
|
1254 FFTModel *fft = getFFTModel();
|
Chris@114
|
1255 if (!fft) return false;
|
Chris@110
|
1256
|
Chris@905
|
1257 double s0 = 0, s1 = 0;
|
Chris@44
|
1258 if (!getXBinRange(v, x, s0, s1)) return false;
|
Chris@35
|
1259
|
Chris@905
|
1260 double q0 = 0, q1 = 0;
|
Chris@44
|
1261 if (!getYBinRange(v, y, q0, q1)) return false;
|
Chris@35
|
1262
|
Chris@35
|
1263 int s0i = int(s0 + 0.001);
|
Chris@35
|
1264 int s1i = int(s1);
|
Chris@35
|
1265
|
Chris@35
|
1266 int q0i = int(q0 + 0.001);
|
Chris@35
|
1267 int q1i = int(q1);
|
Chris@35
|
1268
|
Chris@907
|
1269 sv_samplerate_t sr = m_model->getSampleRate();
|
Chris@35
|
1270
|
Chris@35
|
1271 bool haveAdj = false;
|
Chris@35
|
1272
|
Chris@37
|
1273 bool peaksOnly = (m_binDisplay == PeakBins ||
|
Chris@37
|
1274 m_binDisplay == PeakFrequencies);
|
Chris@37
|
1275
|
Chris@35
|
1276 for (int q = q0i; q <= q1i; ++q) {
|
Chris@35
|
1277
|
Chris@35
|
1278 for (int s = s0i; s <= s1i; ++s) {
|
Chris@35
|
1279
|
Chris@905
|
1280 double binfreq = (double(sr) * q) / m_windowSize;
|
Chris@35
|
1281 if (q == q0i) freqMin = binfreq;
|
Chris@35
|
1282 if (q == q1i) freqMax = binfreq;
|
Chris@37
|
1283
|
Chris@114
|
1284 if (peaksOnly && !fft->isLocalPeak(s, q)) continue;
|
Chris@38
|
1285
|
Chris@1086
|
1286 if (!fft->isOverThreshold
|
Chris@1087
|
1287 (s, q, float(m_threshold * double(getFFTSize())/2.0))) {
|
Chris@1086
|
1288 continue;
|
Chris@1086
|
1289 }
|
Chris@907
|
1290
|
Chris@907
|
1291 double freq = binfreq;
|
Chris@40
|
1292
|
Chris@114
|
1293 if (s < int(fft->getWidth()) - 1) {
|
Chris@38
|
1294
|
Chris@277
|
1295 fft->estimateStableFrequency(s, q, freq);
|
Chris@35
|
1296
|
Chris@38
|
1297 if (!haveAdj || freq < adjFreqMin) adjFreqMin = freq;
|
Chris@38
|
1298 if (!haveAdj || freq > adjFreqMax) adjFreqMax = freq;
|
Chris@35
|
1299
|
Chris@35
|
1300 haveAdj = true;
|
Chris@35
|
1301 }
|
Chris@35
|
1302 }
|
Chris@35
|
1303 }
|
Chris@35
|
1304
|
Chris@35
|
1305 if (!haveAdj) {
|
Chris@40
|
1306 adjFreqMin = adjFreqMax = 0.0;
|
Chris@35
|
1307 }
|
Chris@35
|
1308
|
Chris@35
|
1309 return haveAdj;
|
Chris@35
|
1310 }
|
Chris@0
|
1311
|
Chris@0
|
1312 bool
|
Chris@918
|
1313 SpectrogramLayer::getXYBinSourceRange(LayerGeometryProvider *v, int x, int y,
|
Chris@905
|
1314 double &min, double &max,
|
Chris@905
|
1315 double &phaseMin, double &phaseMax) const
|
Chris@0
|
1316 {
|
Chris@277
|
1317 if (!m_model || !m_model->isOK() || !m_model->isReady()) {
|
Chris@277
|
1318 return false;
|
Chris@277
|
1319 }
|
Chris@277
|
1320
|
Chris@905
|
1321 double q0 = 0, q1 = 0;
|
Chris@44
|
1322 if (!getYBinRange(v, y, q0, q1)) return false;
|
Chris@0
|
1323
|
Chris@905
|
1324 double s0 = 0, s1 = 0;
|
Chris@44
|
1325 if (!getXBinRange(v, x, s0, s1)) return false;
|
Chris@0
|
1326
|
Chris@0
|
1327 int q0i = int(q0 + 0.001);
|
Chris@0
|
1328 int q1i = int(q1);
|
Chris@0
|
1329
|
Chris@0
|
1330 int s0i = int(s0 + 0.001);
|
Chris@0
|
1331 int s1i = int(s1);
|
Chris@0
|
1332
|
Chris@37
|
1333 bool rv = false;
|
Chris@37
|
1334
|
Chris@1088
|
1335 FFTModel *fft = getFFTModel();
|
Chris@0
|
1336
|
Chris@114
|
1337 if (fft) {
|
Chris@114
|
1338
|
Chris@114
|
1339 int cw = fft->getWidth();
|
Chris@114
|
1340 int ch = fft->getHeight();
|
Chris@0
|
1341
|
Chris@110
|
1342 min = 0.0;
|
Chris@110
|
1343 max = 0.0;
|
Chris@110
|
1344 phaseMin = 0.0;
|
Chris@110
|
1345 phaseMax = 0.0;
|
Chris@110
|
1346 bool have = false;
|
Chris@0
|
1347
|
Chris@110
|
1348 for (int q = q0i; q <= q1i; ++q) {
|
Chris@110
|
1349 for (int s = s0i; s <= s1i; ++s) {
|
Chris@110
|
1350 if (s >= 0 && q >= 0 && s < cw && q < ch) {
|
Chris@117
|
1351
|
Chris@905
|
1352 double value;
|
Chris@38
|
1353
|
Chris@114
|
1354 value = fft->getPhaseAt(s, q);
|
Chris@110
|
1355 if (!have || value < phaseMin) { phaseMin = value; }
|
Chris@110
|
1356 if (!have || value > phaseMax) { phaseMax = value; }
|
Chris@91
|
1357
|
Chris@1087
|
1358 value = fft->getMagnitudeAt(s, q) / (getFFTSize()/2.0);
|
Chris@110
|
1359 if (!have || value < min) { min = value; }
|
Chris@110
|
1360 if (!have || value > max) { max = value; }
|
Chris@110
|
1361
|
Chris@110
|
1362 have = true;
|
Chris@110
|
1363 }
|
Chris@110
|
1364 }
|
Chris@110
|
1365 }
|
Chris@110
|
1366
|
Chris@110
|
1367 if (have) {
|
Chris@110
|
1368 rv = true;
|
Chris@110
|
1369 }
|
Chris@0
|
1370 }
|
Chris@0
|
1371
|
Chris@37
|
1372 return rv;
|
Chris@0
|
1373 }
|
Chris@114
|
1374
|
Chris@130
|
1375 FFTModel *
|
Chris@1088
|
1376 SpectrogramLayer::getFFTModel() const
|
Chris@114
|
1377 {
|
Chris@114
|
1378 if (!m_model) return 0;
|
Chris@114
|
1379
|
Chris@1087
|
1380 int fftSize = getFFTSize();
|
Chris@114
|
1381
|
Chris@1088
|
1382 //!!! it is now surely slower to do this on every getFFTModel()
|
Chris@1088
|
1383 //!!! request than it would be to recreate the model immediately
|
Chris@1088
|
1384 //!!! when something changes instead of just invalidating it
|
Chris@920
|
1385
|
Chris@1088
|
1386 if (m_fftModel &&
|
Chris@1088
|
1387 m_fftModel->getHeight() == fftSize / 2 + 1 &&
|
Chris@1088
|
1388 m_fftModel->getWindowIncrement() == getWindowIncrement()) {
|
Chris@1088
|
1389 return m_fftModel;
|
Chris@114
|
1390 }
|
Chris@1088
|
1391
|
Chris@1088
|
1392 delete m_peakCache;
|
Chris@1088
|
1393 m_peakCache = 0;
|
Chris@1088
|
1394
|
Chris@1088
|
1395 delete m_fftModel;
|
Chris@1088
|
1396 m_fftModel = new FFTModel(m_model,
|
Chris@1088
|
1397 m_channel,
|
Chris@1088
|
1398 m_windowType,
|
Chris@1088
|
1399 m_windowSize,
|
Chris@1088
|
1400 getWindowIncrement(),
|
Chris@1088
|
1401 fftSize);
|
Chris@1088
|
1402
|
Chris@1088
|
1403 if (!m_fftModel->isOK()) {
|
Chris@1088
|
1404 QMessageBox::critical
|
Chris@1088
|
1405 (0, tr("FFT cache failed"),
|
Chris@1088
|
1406 tr("Failed to create the FFT model for this spectrogram.\n"
|
Chris@1088
|
1407 "There may be insufficient memory or disc space to continue."));
|
Chris@1088
|
1408 delete m_fftModel;
|
Chris@1088
|
1409 m_fftModel = 0;
|
Chris@1088
|
1410 return 0;
|
Chris@114
|
1411 }
|
Chris@114
|
1412
|
Chris@1088
|
1413 ((SpectrogramLayer *)this)->sliceableModelReplaced(0, m_fftModel);
|
Chris@1088
|
1414
|
Chris@1088
|
1415 return m_fftModel;
|
Chris@114
|
1416 }
|
Chris@114
|
1417
|
Chris@484
|
1418 Dense3DModelPeakCache *
|
Chris@1088
|
1419 SpectrogramLayer::getPeakCache() const
|
Chris@484
|
1420 {
|
Chris@1088
|
1421 //!!! see comment in getFFTModel
|
Chris@1088
|
1422
|
Chris@1088
|
1423 if (!m_peakCache) {
|
Chris@1088
|
1424 FFTModel *f = getFFTModel();
|
Chris@484
|
1425 if (!f) return 0;
|
Chris@1088
|
1426 m_peakCache = new Dense3DModelPeakCache(f, m_peakCacheDivisor);
|
Chris@484
|
1427 }
|
Chris@1088
|
1428 return m_peakCache;
|
Chris@484
|
1429 }
|
Chris@484
|
1430
|
Chris@193
|
1431 const Model *
|
Chris@193
|
1432 SpectrogramLayer::getSliceableModel() const
|
Chris@193
|
1433 {
|
Chris@1088
|
1434 return m_fftModel;
|
Chris@193
|
1435 }
|
Chris@193
|
1436
|
Chris@114
|
1437 void
|
Chris@1088
|
1438 SpectrogramLayer::invalidateFFTModel()
|
Chris@114
|
1439 {
|
Chris@1044
|
1440 #ifdef DEBUG_SPECTROGRAM
|
Chris@1088
|
1441 cerr << "SpectrogramLayer::invalidateFFTModel called" << endl;
|
Chris@1044
|
1442 #endif
|
Chris@1088
|
1443
|
Chris@1088
|
1444 emit sliceableModelReplaced(m_fftModel, 0);
|
Chris@1088
|
1445
|
Chris@1088
|
1446 delete m_fftModel;
|
Chris@1088
|
1447 delete m_peakCache;
|
Chris@1088
|
1448
|
Chris@1088
|
1449 m_fftModel = 0;
|
Chris@1088
|
1450 m_peakCache = 0;
|
Chris@114
|
1451 }
|
Chris@114
|
1452
|
Chris@0
|
1453 void
|
Chris@119
|
1454 SpectrogramLayer::invalidateMagnitudes()
|
Chris@119
|
1455 {
|
Chris@1044
|
1456 #ifdef DEBUG_SPECTROGRAM
|
Chris@1044
|
1457 cerr << "SpectrogramLayer::invalidateMagnitudes called" << endl;
|
Chris@1044
|
1458 #endif
|
Chris@119
|
1459 m_viewMags.clear();
|
Chris@1025
|
1460 for (vector<MagnitudeRange>::iterator i = m_columnMags.begin();
|
Chris@119
|
1461 i != m_columnMags.end(); ++i) {
|
Chris@119
|
1462 *i = MagnitudeRange();
|
Chris@119
|
1463 }
|
Chris@119
|
1464 }
|
Chris@119
|
1465
|
Chris@119
|
1466 bool
|
Chris@918
|
1467 SpectrogramLayer::updateViewMagnitudes(LayerGeometryProvider *v) const
|
Chris@119
|
1468 {
|
Chris@119
|
1469 MagnitudeRange mag;
|
Chris@119
|
1470
|
Chris@918
|
1471 int x0 = 0, x1 = v->getPaintWidth();
|
Chris@905
|
1472 double s00 = 0, s01 = 0, s10 = 0, s11 = 0;
|
Chris@119
|
1473
|
Chris@203
|
1474 if (!getXBinRange(v, x0, s00, s01)) {
|
Chris@907
|
1475 s00 = s01 = double(m_model->getStartFrame()) / getWindowIncrement();
|
Chris@203
|
1476 }
|
Chris@203
|
1477
|
Chris@203
|
1478 if (!getXBinRange(v, x1, s10, s11)) {
|
Chris@907
|
1479 s10 = s11 = double(m_model->getEndFrame()) / getWindowIncrement();
|
Chris@203
|
1480 }
|
Chris@119
|
1481
|
Chris@1025
|
1482 int s0 = int(min(s00, s10) + 0.0001);
|
Chris@1025
|
1483 int s1 = int(max(s01, s11) + 0.0001);
|
Chris@203
|
1484
|
Chris@587
|
1485 // SVDEBUG << "SpectrogramLayer::updateViewMagnitudes: x0 = " << x0 << ", x1 = " << x1 << ", s00 = " << s00 << ", s11 = " << s11 << " s0 = " << s0 << ", s1 = " << s1 << endl;
|
Chris@119
|
1486
|
Chris@248
|
1487 if (int(m_columnMags.size()) <= s1) {
|
Chris@119
|
1488 m_columnMags.resize(s1 + 1);
|
Chris@119
|
1489 }
|
Chris@119
|
1490
|
Chris@119
|
1491 for (int s = s0; s <= s1; ++s) {
|
Chris@119
|
1492 if (m_columnMags[s].isSet()) {
|
Chris@119
|
1493 mag.sample(m_columnMags[s]);
|
Chris@119
|
1494 }
|
Chris@119
|
1495 }
|
Chris@119
|
1496
|
Chris@184
|
1497 #ifdef DEBUG_SPECTROGRAM_REPAINT
|
Chris@985
|
1498 cerr << "SpectrogramLayer::updateViewMagnitudes returning from cols "
|
Chris@1044
|
1499 << s0 << " -> " << s1 << " inclusive" << endl;
|
Chris@1044
|
1500 cerr << "SpectrogramLayer::updateViewMagnitudes: for view id " << v->getId()
|
Chris@1044
|
1501 << ": min is " << mag.getMin() << ", max is " << mag.getMax() << endl;
|
Chris@184
|
1502 #endif
|
Chris@119
|
1503
|
Chris@119
|
1504 if (!mag.isSet()) return false;
|
Chris@1030
|
1505 if (mag == m_viewMags[v->getId()]) return false;
|
Chris@1030
|
1506 m_viewMags[v->getId()] = mag;
|
Chris@119
|
1507 return true;
|
Chris@119
|
1508 }
|
Chris@119
|
1509
|
Chris@119
|
1510 void
|
Chris@389
|
1511 SpectrogramLayer::setSynchronousPainting(bool synchronous)
|
Chris@389
|
1512 {
|
Chris@389
|
1513 m_synchronous = synchronous;
|
Chris@389
|
1514 }
|
Chris@389
|
1515
|
Chris@1030
|
1516 ScrollableImageCache &
|
Chris@1030
|
1517 SpectrogramLayer::getImageCacheReference(const LayerGeometryProvider *view) const
|
Chris@1030
|
1518 {
|
Chris@1030
|
1519 if (m_imageCaches.find(view->getId()) == m_imageCaches.end()) {
|
Chris@1030
|
1520 m_imageCaches[view->getId()] = ScrollableImageCache(view);
|
Chris@1030
|
1521 }
|
Chris@1030
|
1522 return m_imageCaches.at(view->getId());
|
Chris@1030
|
1523 }
|
Chris@1030
|
1524
|
Chris@389
|
1525 void
|
Chris@916
|
1526 SpectrogramLayer::paint(LayerGeometryProvider *v, QPainter &paint, QRect rect) const
|
Chris@0
|
1527 {
|
Chris@334
|
1528 Profiler profiler("SpectrogramLayer::paint", false);
|
Chris@334
|
1529
|
Chris@0
|
1530 #ifdef DEBUG_SPECTROGRAM_REPAINT
|
Chris@1026
|
1531 cerr << "SpectrogramLayer::paint() entering: m_model is " << m_model << ", zoom level is " << v->getZoomLevel() << endl;
|
Chris@95
|
1532
|
Chris@1026
|
1533 cerr << "SpectrogramLayer::paint(): rect is " << rect.x() << "," << rect.y() << " " << rect.width() << "x" << rect.height() << endl;
|
Chris@0
|
1534 #endif
|
Chris@95
|
1535
|
Chris@907
|
1536 sv_frame_t startFrame = v->getStartFrame();
|
Chris@44
|
1537
|
Chris@0
|
1538 if (!m_model || !m_model->isOK() || !m_model->isReady()) {
|
Chris@0
|
1539 return;
|
Chris@0
|
1540 }
|
Chris@0
|
1541
|
Chris@47
|
1542 if (isLayerDormant(v)) {
|
Chris@587
|
1543 SVDEBUG << "SpectrogramLayer::paint(): Layer is dormant, making it undormant again" << endl;
|
Chris@29
|
1544 }
|
Chris@29
|
1545
|
Chris@48
|
1546 // Need to do this even if !isLayerDormant, as that could mean v
|
Chris@48
|
1547 // is not in the dormancy map at all -- we need it to be present
|
Chris@48
|
1548 // and accountable for when determining whether we need the cache
|
Chris@48
|
1549 // in the cache-fill thread above.
|
Chris@806
|
1550 //!!! no inter use cache-fill thread
|
Chris@131
|
1551 const_cast<SpectrogramLayer *>(this)->Layer::setLayerDormant(v, false);
|
Chris@48
|
1552
|
Chris@1087
|
1553 int fftSize = getFFTSize();
|
Chris@920
|
1554
|
Chris@920
|
1555 const View *view = v->getView();
|
Chris@1030
|
1556 ScrollableImageCache &cache = getImageCacheReference(view);
|
Chris@95
|
1557
|
Chris@95
|
1558 #ifdef DEBUG_SPECTROGRAM_REPAINT
|
Chris@1030
|
1559 cerr << "SpectrogramLayer::paint(): image cache valid area from " << cache.getValidLeft() << " width " << cache.getValidWidth() << ", height " << cache.getSize().height() << endl;
|
Chris@1030
|
1560 if (rect.x() + rect.width() + 1 < cache.getValidLeft() ||
|
Chris@1030
|
1561 rect.x() > cache.getValidRight()) {
|
Chris@1030
|
1562 cerr << "SpectrogramLayer: NOTE: requested rect is not contiguous with cache valid area" << endl;
|
Chris@1030
|
1563 }
|
Chris@95
|
1564 #endif
|
Chris@95
|
1565
|
Chris@44
|
1566 int zoomLevel = v->getZoomLevel();
|
Chris@0
|
1567
|
Chris@1030
|
1568 int x0 = v->getXForViewX(rect.x());
|
Chris@1030
|
1569 int x1 = v->getXForViewX(rect.x() + rect.width());
|
Chris@1030
|
1570 if (x0 < 0) x0 = 0;
|
Chris@1030
|
1571 if (x1 > v->getPaintWidth()) x1 = v->getPaintWidth();
|
Chris@1022
|
1572
|
Chris@1022
|
1573 if (updateViewMagnitudes(v)) {
|
Chris@1022
|
1574 #ifdef DEBUG_SPECTROGRAM_REPAINT
|
Chris@1030
|
1575 cerr << "SpectrogramLayer: magnitude range changed to [" << m_viewMags[v->getId()].getMin() << "->" << m_viewMags[v->getId()].getMax() << "]" << endl;
|
Chris@1022
|
1576 #endif
|
Chris@1063
|
1577 if (m_normalization == ColumnOp::NormalizeVisibleArea) {
|
Chris@1030
|
1578 cache.invalidate();
|
Chris@1022
|
1579 }
|
Chris@1022
|
1580 }
|
Chris@1030
|
1581
|
Chris@1030
|
1582 if (cache.getZoomLevel() != zoomLevel ||
|
Chris@1030
|
1583 cache.getSize() != v->getPaintSize()) {
|
Chris@1031
|
1584 #ifdef DEBUG_SPECTROGRAM_REPAINT
|
Chris@1031
|
1585 cerr << "SpectrogramLayer: resizing image cache from "
|
Chris@1031
|
1586 << cache.getSize().width() << "x" << cache.getSize().height()
|
Chris@1031
|
1587 << " to "
|
Chris@1031
|
1588 << v->getPaintSize().width() << "x" << v->getPaintSize().height()
|
Chris@1031
|
1589 << " and updating zoom level from " << cache.getZoomLevel()
|
Chris@1031
|
1590 << " to " << zoomLevel
|
Chris@1031
|
1591 << endl;
|
Chris@1031
|
1592 #endif
|
Chris@1030
|
1593 cache.resize(v->getPaintSize());
|
Chris@1031
|
1594 cache.setZoomLevel(zoomLevel);
|
Chris@1031
|
1595 cache.setStartFrame(startFrame);
|
Chris@1030
|
1596 }
|
Chris@1023
|
1597
|
Chris@1030
|
1598 if (cache.isValid()) {
|
Chris@482
|
1599
|
Chris@1030
|
1600 if (v->getXForFrame(cache.getStartFrame()) ==
|
Chris@1030
|
1601 v->getXForFrame(startFrame) &&
|
Chris@1030
|
1602 cache.getValidLeft() <= x0 &&
|
Chris@1030
|
1603 cache.getValidRight() >= x1) {
|
Chris@1022
|
1604
|
Chris@0
|
1605 #ifdef DEBUG_SPECTROGRAM_REPAINT
|
Chris@1030
|
1606 cerr << "SpectrogramLayer: image cache hit!" << endl;
|
Chris@0
|
1607 #endif
|
Chris@0
|
1608
|
Chris@1030
|
1609 paint.drawImage(rect, cache.getImage(), rect);
|
Chris@1030
|
1610
|
Chris@1030
|
1611 illuminateLocalFeatures(v, paint);
|
Chris@1030
|
1612 return;
|
Chris@1030
|
1613
|
Chris@1030
|
1614 } else {
|
Chris@1030
|
1615
|
Chris@1030
|
1616 // cache doesn't begin at the right frame or doesn't
|
Chris@1030
|
1617 // contain the complete view, but might be scrollable or
|
Chris@1030
|
1618 // partially usable
|
Chris@1022
|
1619
|
Chris@0
|
1620 #ifdef DEBUG_SPECTROGRAM_REPAINT
|
Chris@1030
|
1621 cerr << "SpectrogramLayer: scrolling the image cache if applicable" << endl;
|
Chris@0
|
1622 #endif
|
Chris@0
|
1623
|
Chris@1030
|
1624 cache.scrollTo(startFrame);
|
Chris@1030
|
1625
|
Chris@0
|
1626 #ifdef DEBUG_SPECTROGRAM_REPAINT
|
Chris@1031
|
1627 cerr << "SpectrogramLayer: after scrolling, cache valid from "
|
Chris@1030
|
1628 << cache.getValidLeft() << " width " << cache.getValidWidth()
|
Chris@1030
|
1629 << endl;
|
Chris@0
|
1630 #endif
|
Chris@1030
|
1631 }
|
Chris@0
|
1632 }
|
Chris@95
|
1633
|
Chris@1030
|
1634 bool rightToLeft = false;
|
Chris@1030
|
1635
|
Chris@1030
|
1636 if (!cache.isValid()) {
|
Chris@1028
|
1637 if (!m_synchronous) {
|
Chris@1028
|
1638 // When rendering the whole thing, start from somewhere near
|
Chris@1028
|
1639 // the middle so that the region of interest appears first
|
Chris@1038
|
1640
|
Chris@1038
|
1641 //!!! (perhaps we should have some cunning test to avoid
|
Chris@1038
|
1642 //!!! doing this if past repaints have appeared fast
|
Chris@1038
|
1643 //!!! enough to do the whole width in one shot)
|
Chris@1030
|
1644 if (x0 == 0 && x1 == v->getPaintWidth()) {
|
Chris@1037
|
1645 x0 = int(x1 * 0.3);
|
Chris@1030
|
1646 }
|
Chris@1028
|
1647 }
|
Chris@1030
|
1648 } else {
|
Chris@1030
|
1649 // When rendering only a part of the cache, we need to make
|
Chris@1030
|
1650 // sure that the part we're rendering is adjacent to (or
|
Chris@1030
|
1651 // overlapping) a valid area of cache, if we have one. The
|
Chris@1030
|
1652 // alternative is to ditch the valid area of cache and render
|
Chris@1030
|
1653 // only the requested area, but that's risky because this can
|
Chris@1030
|
1654 // happen when just waving the pointer over a small part of
|
Chris@1030
|
1655 // the view -- if we lose the partly-built cache every time
|
Chris@1030
|
1656 // the user does that, we'll never finish building it.
|
Chris@1030
|
1657 int left = x0;
|
Chris@1030
|
1658 int width = x1 - x0;
|
Chris@1030
|
1659 bool isLeftOfValidArea = false;
|
Chris@1031
|
1660 cache.adjustToTouchValidArea(left, width, isLeftOfValidArea);
|
Chris@1030
|
1661 x0 = left;
|
Chris@1030
|
1662 x1 = x0 + width;
|
Chris@1030
|
1663
|
Chris@1030
|
1664 // That call also told us whether we should be painting
|
Chris@1030
|
1665 // sub-regions of our target region in right-to-left order in
|
Chris@1030
|
1666 // order to ensure contiguity
|
Chris@1030
|
1667 rightToLeft = isLeftOfValidArea;
|
Chris@95
|
1668 }
|
Chris@1030
|
1669
|
Chris@224
|
1670 // We always paint the full height when refreshing the cache.
|
Chris@224
|
1671 // Smaller heights can be used when painting direct from cache
|
Chris@224
|
1672 // (further up in this function), but we want to ensure the cache
|
Chris@224
|
1673 // is coherent without having to worry about vertical matching of
|
Chris@224
|
1674 // required and valid areas as well as horizontal.
|
Chris@918
|
1675 int h = v->getPaintHeight();
|
Chris@1025
|
1676
|
Chris@1024
|
1677 int repaintWidth = x1 - x0;
|
Chris@0
|
1678
|
Chris@95
|
1679 #ifdef DEBUG_SPECTROGRAM_REPAINT
|
Chris@1026
|
1680 cerr << "SpectrogramLayer: x0 " << x0 << ", x1 " << x1
|
Chris@1026
|
1681 << ", repaintWidth " << repaintWidth << ", h " << h
|
Chris@1030
|
1682 << ", rightToLeft " << rightToLeft << endl;
|
Chris@95
|
1683 #endif
|
Chris@95
|
1684
|
Chris@907
|
1685 sv_samplerate_t sr = m_model->getSampleRate();
|
Chris@122
|
1686
|
Chris@122
|
1687 // Set minFreq and maxFreq to the frequency extents of the possibly
|
Chris@122
|
1688 // zero-padded visible bin range, and displayMinFreq and displayMaxFreq
|
Chris@122
|
1689 // to the actual scale frequency extents (presumably not zero padded).
|
Chris@253
|
1690
|
Chris@1086
|
1691 // If we are zero padding (i.e. oversampling) we want to use the
|
Chris@1086
|
1692 // zero-padded equivalents of the bins that we would be using if
|
Chris@1086
|
1693 // not zero padded, to avoid spaces at the top and bottom of the
|
Chris@1086
|
1694 // display.
|
Chris@35
|
1695
|
Chris@1087
|
1696 int maxbin = fftSize / 2;
|
Chris@35
|
1697 if (m_maxFrequency > 0) {
|
Chris@1087
|
1698 maxbin = int((double(m_maxFrequency) * fftSize) / sr + 0.001);
|
Chris@1087
|
1699 if (maxbin > fftSize / 2) maxbin = fftSize / 2;
|
Chris@35
|
1700 }
|
Chris@111
|
1701
|
Chris@805
|
1702 int minbin = 1;
|
Chris@37
|
1703 if (m_minFrequency > 0) {
|
Chris@1087
|
1704 minbin = int((double(m_minFrequency) * fftSize) / sr + 0.001);
|
Chris@682
|
1705 // cerr << "m_minFrequency = " << m_minFrequency << " -> minbin = " << minbin << endl;
|
Chris@40
|
1706 if (minbin < 1) minbin = 1;
|
Chris@184
|
1707 if (minbin >= maxbin) minbin = maxbin - 1;
|
Chris@37
|
1708 }
|
Chris@37
|
1709
|
Chris@1086
|
1710 int over = getFFTOversampling();
|
Chris@1086
|
1711 minbin = minbin * over;
|
Chris@1086
|
1712 maxbin = (maxbin + 1) * over - 1;
|
Chris@253
|
1713
|
Chris@905
|
1714 double minFreq = (double(minbin) * sr) / fftSize;
|
Chris@905
|
1715 double maxFreq = (double(maxbin) * sr) / fftSize;
|
Chris@905
|
1716
|
Chris@905
|
1717 double displayMinFreq = minFreq;
|
Chris@905
|
1718 double displayMaxFreq = maxFreq;
|
Chris@122
|
1719
|
Chris@1087
|
1720 //!!! if (fftSize != getFFTSize()) {
|
Chris@1087
|
1721 // displayMinFreq = getEffectiveMinFrequency();
|
Chris@1087
|
1722 // displayMaxFreq = getEffectiveMaxFrequency();
|
Chris@1087
|
1723 // }
|
Chris@122
|
1724
|
Chris@682
|
1725 // cerr << "(giving actual minFreq " << minFreq << " and display minFreq " << displayMinFreq << ")" << endl;
|
Chris@253
|
1726
|
Chris@518
|
1727 int increment = getWindowIncrement();
|
Chris@40
|
1728
|
Chris@40
|
1729 bool logarithmic = (m_frequencyScale == LogFrequencyScale);
|
Chris@1026
|
1730
|
Chris@1030
|
1731 MagnitudeRange overallMag = m_viewMags[v->getId()];
|
Chris@119
|
1732 bool overallMagChanged = false;
|
Chris@119
|
1733
|
Chris@137
|
1734 #ifdef DEBUG_SPECTROGRAM_REPAINT
|
Chris@1026
|
1735 cerr << "SpectrogramLayer: " << ((double(v->getFrameForX(1) - v->getFrameForX(0))) / increment) << " bin(s) per pixel" << endl;
|
Chris@137
|
1736 #endif
|
Chris@137
|
1737
|
Chris@1024
|
1738 if (repaintWidth == 0) {
|
Chris@1024
|
1739 SVDEBUG << "*** NOTE: repaintWidth == 0" << endl;
|
Chris@331
|
1740 }
|
Chris@331
|
1741
|
Chris@382
|
1742 Profiler outerprof("SpectrogramLayer::paint: all cols");
|
Chris@382
|
1743
|
Chris@481
|
1744 // The draw buffer contains a fragment at either our pixel
|
Chris@481
|
1745 // resolution (if there is more than one time-bin per pixel) or
|
Chris@481
|
1746 // time-bin resolution (if a time-bin spans more than one pixel).
|
Chris@481
|
1747 // We need to ensure that it starts and ends at points where a
|
Chris@481
|
1748 // time-bin boundary occurs at an exact pixel boundary, and with a
|
Chris@481
|
1749 // certain amount of overlap across existing pixels so that we can
|
Chris@481
|
1750 // scale and draw from it without smoothing errors at the edges.
|
Chris@481
|
1751
|
Chris@481
|
1752 // If (getFrameForX(x) / increment) * increment ==
|
Chris@481
|
1753 // getFrameForX(x), then x is a time-bin boundary. We want two
|
Chris@481
|
1754 // such boundaries at either side of the draw buffer -- one which
|
Chris@481
|
1755 // we draw up to, and one which we subsequently crop at.
|
Chris@481
|
1756
|
Chris@1039
|
1757 bool bufferIsBinResolution = false;
|
Chris@1039
|
1758 if (increment > zoomLevel) bufferIsBinResolution = true;
|
Chris@481
|
1759
|
Chris@907
|
1760 sv_frame_t leftBoundaryFrame = -1, leftCropFrame = -1;
|
Chris@907
|
1761 sv_frame_t rightBoundaryFrame = -1, rightCropFrame = -1;
|
Chris@481
|
1762
|
Chris@481
|
1763 int bufwid;
|
Chris@481
|
1764
|
Chris@1039
|
1765 if (bufferIsBinResolution) {
|
Chris@481
|
1766
|
Chris@482
|
1767 for (int x = x0; ; --x) {
|
Chris@907
|
1768 sv_frame_t f = v->getFrameForX(x);
|
Chris@481
|
1769 if ((f / increment) * increment == f) {
|
Chris@481
|
1770 if (leftCropFrame == -1) leftCropFrame = f;
|
Chris@1024
|
1771 else if (x < x0 - 2) {
|
Chris@1024
|
1772 leftBoundaryFrame = f;
|
Chris@1024
|
1773 break;
|
Chris@1024
|
1774 }
|
Chris@481
|
1775 }
|
Chris@481
|
1776 }
|
Chris@1024
|
1777 for (int x = x0 + repaintWidth; ; ++x) {
|
Chris@907
|
1778 sv_frame_t f = v->getFrameForX(x);
|
Chris@481
|
1779 if ((f / increment) * increment == f) {
|
Chris@481
|
1780 if (rightCropFrame == -1) rightCropFrame = f;
|
Chris@1024
|
1781 else if (x > x0 + repaintWidth + 2) {
|
Chris@1024
|
1782 rightBoundaryFrame = f;
|
Chris@1024
|
1783 break;
|
Chris@1024
|
1784 }
|
Chris@481
|
1785 }
|
Chris@481
|
1786 }
|
Chris@485
|
1787 #ifdef DEBUG_SPECTROGRAM_REPAINT
|
Chris@481
|
1788 cerr << "Left: crop: " << leftCropFrame << " (bin " << leftCropFrame/increment << "); boundary: " << leftBoundaryFrame << " (bin " << leftBoundaryFrame/increment << ")" << endl;
|
Chris@481
|
1789 cerr << "Right: crop: " << rightCropFrame << " (bin " << rightCropFrame/increment << "); boundary: " << rightBoundaryFrame << " (bin " << rightBoundaryFrame/increment << ")" << endl;
|
Chris@485
|
1790 #endif
|
Chris@481
|
1791
|
Chris@907
|
1792 bufwid = int((rightBoundaryFrame - leftBoundaryFrame) / increment);
|
Chris@481
|
1793
|
Chris@481
|
1794 } else {
|
Chris@481
|
1795
|
Chris@1024
|
1796 bufwid = repaintWidth;
|
Chris@481
|
1797 }
|
Chris@481
|
1798
|
Chris@907
|
1799 vector<int> binforx(bufwid);
|
Chris@907
|
1800 vector<double> binfory(h);
|
Chris@907
|
1801
|
Chris@484
|
1802 bool usePeaksCache = false;
|
Chris@484
|
1803
|
Chris@1039
|
1804 if (bufferIsBinResolution) {
|
Chris@481
|
1805 for (int x = 0; x < bufwid; ++x) {
|
Chris@907
|
1806 binforx[x] = int(leftBoundaryFrame / increment) + x;
|
Chris@481
|
1807 }
|
Chris@481
|
1808 m_drawBuffer = QImage(bufwid, h, QImage::Format_Indexed8);
|
Chris@481
|
1809 } else {
|
Chris@481
|
1810 for (int x = 0; x < bufwid; ++x) {
|
Chris@905
|
1811 double s0 = 0, s1 = 0;
|
Chris@481
|
1812 if (getXBinRange(v, x + x0, s0, s1)) {
|
Chris@481
|
1813 binforx[x] = int(s0 + 0.0001);
|
Chris@481
|
1814 } else {
|
Chris@487
|
1815 binforx[x] = -1; //???
|
Chris@481
|
1816 }
|
Chris@481
|
1817 }
|
Chris@1031
|
1818 if (m_drawBuffer.width() < bufwid || m_drawBuffer.height() != h) {
|
Chris@481
|
1819 m_drawBuffer = QImage(bufwid, h, QImage::Format_Indexed8);
|
Chris@480
|
1820 }
|
Chris@1054
|
1821 usePeaksCache = (increment * m_peakCacheDivisor) < zoomLevel;
|
Chris@487
|
1822 if (m_colourScale == PhaseColourScale) usePeaksCache = false;
|
Chris@480
|
1823 }
|
Chris@481
|
1824
|
Chris@481
|
1825 for (int pixel = 0; pixel < 256; ++pixel) {
|
Chris@907
|
1826 m_drawBuffer.setColor((unsigned char)pixel,
|
Chris@907
|
1827 m_palette.getColour((unsigned char)pixel).rgb());
|
Chris@481
|
1828 }
|
Chris@481
|
1829
|
Chris@481
|
1830 m_drawBuffer.fill(0);
|
Chris@1024
|
1831 int attainedBufwid = bufwid;
|
Chris@1039
|
1832
|
Chris@1039
|
1833 double softTimeLimit;
|
Chris@1039
|
1834
|
Chris@1039
|
1835 if (m_synchronous) {
|
Chris@1039
|
1836
|
Chris@1039
|
1837 // must paint the whole thing for synchronous mode, so give
|
Chris@1039
|
1838 // "no timeout"
|
Chris@1039
|
1839 softTimeLimit = 0.0;
|
Chris@1039
|
1840
|
Chris@1039
|
1841 } else if (bufferIsBinResolution) {
|
Chris@1039
|
1842
|
Chris@1039
|
1843 // calculating boundaries later will be too fiddly for partial
|
Chris@1039
|
1844 // paints, and painting should be fast anyway when this is the
|
Chris@1039
|
1845 // case because it means we're well zoomed in
|
Chris@1039
|
1846 softTimeLimit = 0.0;
|
Chris@1039
|
1847
|
Chris@1039
|
1848 } else {
|
Chris@1039
|
1849
|
Chris@1039
|
1850 // neither limitation applies, so use a short soft limit
|
Chris@1039
|
1851
|
Chris@1039
|
1852 if (m_binDisplay == PeakFrequencies) {
|
Chris@1039
|
1853 softTimeLimit = 0.15;
|
Chris@1039
|
1854 } else {
|
Chris@1039
|
1855 softTimeLimit = 0.1;
|
Chris@1039
|
1856 }
|
Chris@1039
|
1857 }
|
Chris@1039
|
1858
|
Chris@488
|
1859 if (m_binDisplay != PeakFrequencies) {
|
Chris@488
|
1860
|
Chris@488
|
1861 for (int y = 0; y < h; ++y) {
|
Chris@905
|
1862 double q0 = 0, q1 = 0;
|
Chris@1086
|
1863 if (!getYBinRange(v, h-y-1, q0, q1)) {
|
Chris@488
|
1864 binfory[y] = -1;
|
Chris@488
|
1865 } else {
|
Chris@490
|
1866 binfory[y] = q0;
|
Chris@488
|
1867 }
|
Chris@480
|
1868 }
|
Chris@488
|
1869
|
Chris@1024
|
1870 attainedBufwid =
|
Chris@1026
|
1871 paintDrawBuffer(v, bufwid, h, binforx, binfory,
|
Chris@1026
|
1872 usePeaksCache,
|
Chris@1026
|
1873 overallMag, overallMagChanged,
|
Chris@1039
|
1874 rightToLeft,
|
Chris@1039
|
1875 softTimeLimit);
|
Chris@488
|
1876
|
Chris@488
|
1877 } else {
|
Chris@488
|
1878
|
Chris@1024
|
1879 attainedBufwid =
|
Chris@1024
|
1880 paintDrawBufferPeakFrequencies(v, bufwid, h, binforx,
|
Chris@1024
|
1881 minbin, maxbin,
|
Chris@1024
|
1882 displayMinFreq, displayMaxFreq,
|
Chris@1024
|
1883 logarithmic,
|
Chris@1026
|
1884 overallMag, overallMagChanged,
|
Chris@1039
|
1885 rightToLeft,
|
Chris@1039
|
1886 softTimeLimit);
|
Chris@480
|
1887 }
|
Chris@481
|
1888
|
Chris@1024
|
1889 int failedToRepaint = bufwid - attainedBufwid;
|
Chris@1031
|
1890
|
Chris@1031
|
1891 int paintedLeft = x0;
|
Chris@1031
|
1892 int paintedWidth = x1 - x0;
|
Chris@1031
|
1893
|
Chris@1025
|
1894 if (failedToRepaint > 0) {
|
Chris@1031
|
1895
|
Chris@1025
|
1896 #ifdef DEBUG_SPECTROGRAM_REPAINT
|
Chris@1026
|
1897 cerr << "SpectrogramLayer::paint(): Failed to repaint " << failedToRepaint << " of " << bufwid
|
Chris@1029
|
1898 << " columns in time (so managed to repaint " << bufwid - failedToRepaint << ")" << endl;
|
Chris@1025
|
1899 #endif
|
Chris@1031
|
1900
|
Chris@1031
|
1901 if (rightToLeft) {
|
Chris@1031
|
1902 paintedLeft += failedToRepaint;
|
Chris@1031
|
1903 }
|
Chris@1031
|
1904
|
Chris@1031
|
1905 paintedWidth -= failedToRepaint;
|
Chris@1031
|
1906
|
Chris@1031
|
1907 if (paintedWidth < 0) {
|
Chris@1031
|
1908 paintedWidth = 0;
|
Chris@1031
|
1909 }
|
Chris@1031
|
1910
|
Chris@1025
|
1911 } else if (failedToRepaint < 0) {
|
Chris@1024
|
1912 cerr << "WARNING: failedToRepaint < 0 (= " << failedToRepaint << ")"
|
Chris@1024
|
1913 << endl;
|
Chris@1024
|
1914 failedToRepaint = 0;
|
Chris@1024
|
1915 }
|
Chris@1031
|
1916
|
Chris@119
|
1917 if (overallMagChanged) {
|
Chris@1030
|
1918 m_viewMags[v->getId()] = overallMag;
|
Chris@209
|
1919 #ifdef DEBUG_SPECTROGRAM_REPAINT
|
Chris@1030
|
1920 cerr << "SpectrogramLayer: Overall mag is now [" << m_viewMags[v->getId()].getMin() << "->" << m_viewMags[v->getId()].getMax() << "] - will be updating" << endl;
|
Chris@209
|
1921 #endif
|
Chris@119
|
1922 }
|
Chris@119
|
1923
|
Chris@382
|
1924 outerprof.end();
|
Chris@382
|
1925
|
Chris@382
|
1926 Profiler profiler2("SpectrogramLayer::paint: draw image");
|
Chris@137
|
1927
|
Chris@1031
|
1928 if (paintedWidth > 0) {
|
Chris@1024
|
1929
|
Chris@224
|
1930 #ifdef DEBUG_SPECTROGRAM_REPAINT
|
Chris@1031
|
1931 cerr << "SpectrogramLayer: Copying " << paintedWidth << "x" << h
|
Chris@1031
|
1932 << " from draw buffer at " << paintedLeft - x0 << "," << 0
|
Chris@1031
|
1933 << " to " << paintedWidth << "x" << h << " on cache at "
|
Chris@585
|
1934 << x0 << "," << 0 << endl;
|
Chris@224
|
1935 #endif
|
Chris@224
|
1936
|
Chris@1039
|
1937 if (bufferIsBinResolution) {
|
Chris@1031
|
1938
|
Chris@481
|
1939 int scaledLeft = v->getXForFrame(leftBoundaryFrame);
|
Chris@481
|
1940 int scaledRight = v->getXForFrame(rightBoundaryFrame);
|
Chris@1031
|
1941
|
Chris@485
|
1942 #ifdef DEBUG_SPECTROGRAM_REPAINT
|
Chris@1026
|
1943 cerr << "SpectrogramLayer: Rescaling image from " << bufwid
|
Chris@481
|
1944 << "x" << h << " to "
|
Chris@481
|
1945 << scaledRight-scaledLeft << "x" << h << endl;
|
Chris@485
|
1946 #endif
|
Chris@1031
|
1947
|
Chris@490
|
1948 Preferences::SpectrogramXSmoothing xsmoothing =
|
Chris@490
|
1949 Preferences::getInstance()->getSpectrogramXSmoothing();
|
Chris@1026
|
1950
|
Chris@481
|
1951 QImage scaled = m_drawBuffer.scaled
|
Chris@481
|
1952 (scaledRight - scaledLeft, h,
|
Chris@490
|
1953 Qt::IgnoreAspectRatio,
|
Chris@490
|
1954 ((xsmoothing == Preferences::SpectrogramXInterpolated) ?
|
Chris@490
|
1955 Qt::SmoothTransformation : Qt::FastTransformation));
|
Chris@1026
|
1956
|
Chris@481
|
1957 int scaledLeftCrop = v->getXForFrame(leftCropFrame);
|
Chris@481
|
1958 int scaledRightCrop = v->getXForFrame(rightCropFrame);
|
Chris@1031
|
1959
|
Chris@485
|
1960 #ifdef DEBUG_SPECTROGRAM_REPAINT
|
Chris@1026
|
1961 cerr << "SpectrogramLayer: Drawing image region of width " << scaledRightCrop - scaledLeftCrop << " to "
|
Chris@481
|
1962 << scaledLeftCrop << " from " << scaledLeftCrop - scaledLeft << endl;
|
Chris@485
|
1963 #endif
|
Chris@1030
|
1964
|
Chris@1040
|
1965 int targetLeft = scaledLeftCrop;
|
Chris@1040
|
1966 if (targetLeft < 0) {
|
Chris@1040
|
1967 targetLeft = 0;
|
Chris@1040
|
1968 }
|
Chris@1040
|
1969
|
Chris@1040
|
1970 int targetWidth = scaledRightCrop - targetLeft;
|
Chris@1040
|
1971 if (targetLeft + targetWidth > cache.getSize().width()) {
|
Chris@1040
|
1972 targetWidth = cache.getSize().width() - targetLeft;
|
Chris@1040
|
1973 }
|
Chris@1031
|
1974
|
Chris@1040
|
1975 int sourceLeft = targetLeft - scaledLeft;
|
Chris@1040
|
1976 if (sourceLeft < 0) {
|
Chris@1040
|
1977 sourceLeft = 0;
|
Chris@1040
|
1978 }
|
Chris@1040
|
1979
|
Chris@1040
|
1980 int sourceWidth = targetWidth;
|
Chris@1040
|
1981
|
Chris@1040
|
1982 if (targetWidth > 0) {
|
Chris@1040
|
1983 cache.drawImage
|
Chris@1040
|
1984 (targetLeft,
|
Chris@1040
|
1985 targetWidth,
|
Chris@1040
|
1986 scaled,
|
Chris@1040
|
1987 sourceLeft,
|
Chris@1040
|
1988 sourceWidth);
|
Chris@1040
|
1989 }
|
Chris@1024
|
1990
|
Chris@481
|
1991 } else {
|
Chris@1024
|
1992
|
Chris@1031
|
1993 cache.drawImage(paintedLeft, paintedWidth,
|
Chris@1030
|
1994 m_drawBuffer,
|
Chris@1031
|
1995 paintedLeft - x0, paintedWidth);
|
Chris@481
|
1996 }
|
Chris@331
|
1997 }
|
Chris@331
|
1998
|
Chris@1026
|
1999 #ifdef DEBUG_SPECTROGRAM_REPAINT
|
Chris@1030
|
2000 cerr << "SpectrogramLayer: Cache valid area now from " << cache.getValidLeft()
|
Chris@1030
|
2001 << " width " << cache.getValidWidth() << ", height "
|
Chris@1030
|
2002 << cache.getSize().height() << endl;
|
Chris@1026
|
2003 #endif
|
Chris@1030
|
2004
|
Chris@1030
|
2005 QRect pr = rect & cache.getValidArea();
|
Chris@337
|
2006
|
Chris@337
|
2007 #ifdef DEBUG_SPECTROGRAM_REPAINT
|
Chris@1026
|
2008 cerr << "SpectrogramLayer: Copying " << pr.width() << "x" << pr.height()
|
Chris@337
|
2009 << " from cache at " << pr.x() << "," << pr.y()
|
Chris@585
|
2010 << " to window" << endl;
|
Chris@337
|
2011 #endif
|
Chris@337
|
2012
|
Chris@1030
|
2013 paint.drawImage(pr.x(), pr.y(), cache.getImage(),
|
Chris@479
|
2014 pr.x(), pr.y(), pr.width(), pr.height());
|
Chris@337
|
2015
|
Chris@389
|
2016 if (!m_synchronous) {
|
Chris@389
|
2017
|
Chris@1063
|
2018 if ((m_normalization != ColumnOp::NormalizeVisibleArea) || !overallMagChanged) {
|
Chris@1031
|
2019
|
Chris@1031
|
2020 QRect areaLeft(0, 0, cache.getValidLeft(), h);
|
Chris@1031
|
2021 QRect areaRight(cache.getValidRight(), 0,
|
Chris@1031
|
2022 cache.getSize().width() - cache.getValidRight(), h);
|
Chris@1031
|
2023
|
Chris@1031
|
2024 bool haveSpaceLeft = (areaLeft.width() > 0);
|
Chris@1031
|
2025 bool haveSpaceRight = (areaRight.width() > 0);
|
Chris@1031
|
2026
|
Chris@1031
|
2027 bool updateLeft = haveSpaceLeft;
|
Chris@1031
|
2028 bool updateRight = haveSpaceRight;
|
Chris@1031
|
2029
|
Chris@1031
|
2030 if (updateLeft && updateRight) {
|
Chris@1031
|
2031 if (rightToLeft) {
|
Chris@1031
|
2032 // we just did something adjoining the cache on
|
Chris@1031
|
2033 // its left side, so now do something on its right
|
Chris@1031
|
2034 updateLeft = false;
|
Chris@1031
|
2035 } else {
|
Chris@1031
|
2036 updateRight = false;
|
Chris@1031
|
2037 }
|
Chris@389
|
2038 }
|
Chris@389
|
2039
|
Chris@1031
|
2040 if (updateLeft) {
|
Chris@1031
|
2041 #ifdef DEBUG_SPECTROGRAM_REPAINT
|
Chris@1031
|
2042 cerr << "SpectrogramLayer::paint() updating left ("
|
Chris@1031
|
2043 << areaLeft.x() << ", "
|
Chris@1031
|
2044 << areaLeft.width() << ")" << endl;
|
Chris@1031
|
2045 #endif
|
Chris@1031
|
2046 v->updatePaintRect(areaLeft);
|
Chris@1031
|
2047 }
|
Chris@1031
|
2048
|
Chris@1031
|
2049 if (updateRight) {
|
Chris@389
|
2050 #ifdef DEBUG_SPECTROGRAM_REPAINT
|
Chris@985
|
2051 cerr << "SpectrogramLayer::paint() updating right ("
|
Chris@1031
|
2052 << areaRight.x() << ", "
|
Chris@1031
|
2053 << areaRight.width() << ")" << endl;
|
Chris@389
|
2054 #endif
|
Chris@1031
|
2055 v->updatePaintRect(areaRight);
|
Chris@389
|
2056 }
|
Chris@1031
|
2057
|
Chris@389
|
2058 } else {
|
Chris@389
|
2059 // overallMagChanged
|
Chris@682
|
2060 cerr << "\noverallMagChanged - updating all\n" << endl;
|
Chris@1030
|
2061 cache.invalidate();
|
Chris@1030
|
2062 v->updatePaintRect(v->getPaintRect());
|
Chris@119
|
2063 }
|
Chris@95
|
2064 }
|
Chris@0
|
2065
|
Chris@121
|
2066 illuminateLocalFeatures(v, paint);
|
Chris@120
|
2067
|
Chris@0
|
2068 #ifdef DEBUG_SPECTROGRAM_REPAINT
|
Chris@985
|
2069 cerr << "SpectrogramLayer::paint() returning" << endl;
|
Chris@0
|
2070 #endif
|
Chris@0
|
2071 }
|
Chris@0
|
2072
|
Chris@1024
|
2073 int
|
Chris@918
|
2074 SpectrogramLayer::paintDrawBufferPeakFrequencies(LayerGeometryProvider *v,
|
Chris@488
|
2075 int w,
|
Chris@488
|
2076 int h,
|
Chris@907
|
2077 const vector<int> &binforx,
|
Chris@488
|
2078 int minbin,
|
Chris@488
|
2079 int maxbin,
|
Chris@905
|
2080 double displayMinFreq,
|
Chris@905
|
2081 double displayMaxFreq,
|
Chris@491
|
2082 bool logarithmic,
|
Chris@491
|
2083 MagnitudeRange &overallMag,
|
Chris@1026
|
2084 bool &overallMagChanged,
|
Chris@1039
|
2085 bool rightToLeft,
|
Chris@1039
|
2086 double softTimeLimit) const
|
Chris@488
|
2087 {
|
Chris@488
|
2088 Profiler profiler("SpectrogramLayer::paintDrawBufferPeakFrequencies");
|
Chris@488
|
2089
|
Chris@488
|
2090 #ifdef DEBUG_SPECTROGRAM_REPAINT
|
Chris@1026
|
2091 cerr << "SpectrogramLayer::paintDrawBufferPeakFrequencies: minbin " << minbin << ", maxbin " << maxbin << "; w " << w << ", h " << h << endl;
|
Chris@488
|
2092 #endif
|
Chris@488
|
2093 if (minbin < 0) minbin = 0;
|
Chris@488
|
2094 if (maxbin < 0) maxbin = minbin+1;
|
Chris@488
|
2095
|
Chris@1088
|
2096 FFTModel *fft = getFFTModel();
|
Chris@1024
|
2097 if (!fft) return 0;
|
Chris@488
|
2098
|
Chris@488
|
2099 FFTModel::PeakSet peakfreqs;
|
Chris@1064
|
2100 vector<float> preparedColumn;
|
Chris@1064
|
2101
|
Chris@848
|
2102 int psx = -1;
|
Chris@545
|
2103
|
Chris@1027
|
2104 int minColumns = 4;
|
Chris@1039
|
2105 bool haveTimeLimits = (softTimeLimit > 0.0);
|
Chris@1037
|
2106 double hardTimeLimit = softTimeLimit * 2.0;
|
Chris@1037
|
2107 bool overridingSoftLimit = false;
|
Chris@1027
|
2108 auto startTime = chrono::steady_clock::now();
|
Chris@1027
|
2109
|
Chris@1026
|
2110 int start = 0;
|
Chris@1026
|
2111 int finish = w;
|
Chris@1026
|
2112 int step = 1;
|
Chris@1026
|
2113
|
Chris@1026
|
2114 if (rightToLeft) {
|
Chris@1026
|
2115 start = w-1;
|
Chris@1026
|
2116 finish = -1;
|
Chris@1026
|
2117 step = -1;
|
Chris@1026
|
2118 }
|
Chris@1026
|
2119
|
Chris@1027
|
2120 int columnCount = 0;
|
Chris@1027
|
2121
|
Chris@1026
|
2122 for (int x = start; x != finish; x += step) {
|
Chris@488
|
2123
|
Chris@1027
|
2124 ++columnCount;
|
Chris@1027
|
2125
|
Chris@488
|
2126 if (binforx[x] < 0) continue;
|
Chris@488
|
2127
|
Chris@488
|
2128 int sx0 = binforx[x];
|
Chris@488
|
2129 int sx1 = sx0;
|
Chris@488
|
2130 if (x+1 < w) sx1 = binforx[x+1];
|
Chris@488
|
2131 if (sx0 < 0) sx0 = sx1 - 1;
|
Chris@488
|
2132 if (sx0 < 0) continue;
|
Chris@488
|
2133 if (sx1 <= sx0) sx1 = sx0 + 1;
|
Chris@488
|
2134
|
Chris@1064
|
2135 vector<float> pixelPeakColumn;
|
Chris@1064
|
2136
|
Chris@488
|
2137 for (int sx = sx0; sx < sx1; ++sx) {
|
Chris@488
|
2138
|
Chris@1064
|
2139 if (sx < 0 || sx >= int(fft->getWidth())) {
|
Chris@1064
|
2140 continue;
|
Chris@1064
|
2141 }
|
Chris@488
|
2142
|
Chris@488
|
2143 if (sx != psx) {
|
Chris@1064
|
2144
|
Chris@1064
|
2145 ColumnOp::Column column;
|
Chris@1064
|
2146
|
Chris@1064
|
2147 column = getColumnFromFFTModel(fft,
|
Chris@1064
|
2148 sx,
|
Chris@1064
|
2149 minbin,
|
Chris@1064
|
2150 maxbin - minbin + 1);
|
Chris@1064
|
2151
|
Chris@1064
|
2152 if (m_colourScale != PhaseColourScale) {
|
Chris@1087
|
2153 column = ColumnOp::fftScale(column, getFFTSize());
|
Chris@1064
|
2154 }
|
Chris@1064
|
2155
|
Chris@1064
|
2156 recordColumnExtents(column,
|
Chris@1064
|
2157 sx,
|
Chris@1064
|
2158 overallMag,
|
Chris@1064
|
2159 overallMagChanged);
|
Chris@1064
|
2160
|
Chris@1064
|
2161 if (m_colourScale != PhaseColourScale) {
|
Chris@1064
|
2162 column = ColumnOp::normalize(column, m_normalization);
|
Chris@1064
|
2163 }
|
Chris@1064
|
2164
|
Chris@1064
|
2165 preparedColumn = ColumnOp::applyGain(column, m_gain);
|
Chris@1064
|
2166
|
Chris@1064
|
2167 psx = sx;
|
Chris@1064
|
2168 }
|
Chris@1064
|
2169
|
Chris@1064
|
2170 if (sx == sx0) {
|
Chris@1064
|
2171 pixelPeakColumn = preparedColumn;
|
Chris@488
|
2172 peakfreqs = fft->getPeakFrequencies(FFTModel::AllPeaks, sx,
|
Chris@488
|
2173 minbin, maxbin - 1);
|
Chris@1064
|
2174 } else {
|
Chris@1064
|
2175 for (int i = 0; in_range_for(pixelPeakColumn, i); ++i) {
|
Chris@1064
|
2176 pixelPeakColumn[i] = std::max(pixelPeakColumn[i],
|
Chris@1064
|
2177 preparedColumn[i]);
|
Chris@488
|
2178 }
|
Chris@488
|
2179 }
|
Chris@488
|
2180 }
|
Chris@1065
|
2181
|
Chris@1065
|
2182 if (!pixelPeakColumn.empty()) {
|
Chris@1065
|
2183 for (FFTModel::PeakSet::const_iterator pi = peakfreqs.begin();
|
Chris@1065
|
2184 pi != peakfreqs.end(); ++pi) {
|
Chris@1065
|
2185
|
Chris@1065
|
2186 int bin = pi->first;
|
Chris@1065
|
2187 double freq = pi->second;
|
Chris@1065
|
2188
|
Chris@1065
|
2189 if (bin < minbin) continue;
|
Chris@1065
|
2190 if (bin > maxbin) break;
|
Chris@1065
|
2191
|
Chris@1065
|
2192 double value = pixelPeakColumn[bin - minbin];
|
Chris@1065
|
2193
|
Chris@1065
|
2194 double y = v->getYForFrequency
|
Chris@1065
|
2195 (freq, displayMinFreq, displayMaxFreq, logarithmic);
|
Chris@1065
|
2196
|
Chris@1065
|
2197 int iy = int(y + 0.5);
|
Chris@1065
|
2198 if (iy < 0 || iy >= h) continue;
|
Chris@1065
|
2199
|
Chris@1065
|
2200 m_drawBuffer.setPixel(x, iy, getDisplayValue(v, value));
|
Chris@1065
|
2201 }
|
Chris@1065
|
2202 }
|
Chris@1064
|
2203
|
Chris@1039
|
2204 if (haveTimeLimits) {
|
Chris@1027
|
2205 if (columnCount >= minColumns) {
|
Chris@1027
|
2206 auto t = chrono::steady_clock::now();
|
Chris@1027
|
2207 double diff = chrono::duration<double>(t - startTime).count();
|
Chris@1037
|
2208 if (diff > hardTimeLimit) {
|
Chris@1027
|
2209 #ifdef DEBUG_SPECTROGRAM_REPAINT
|
Chris@1037
|
2210 cerr << "SpectrogramLayer::paintDrawBufferPeakFrequencies: hard limit " << hardTimeLimit << " sec exceeded after "
|
Chris@1031
|
2211 << columnCount << " columns with time " << diff << endl;
|
Chris@1027
|
2212 #endif
|
Chris@1027
|
2213 return columnCount;
|
Chris@1037
|
2214 } else if (diff > softTimeLimit && !overridingSoftLimit) {
|
Chris@1037
|
2215 // If we're more than half way through by the time
|
Chris@1037
|
2216 // we reach the soft limit, ignore it (though
|
Chris@1037
|
2217 // still respect the hard limit, above). Otherwise
|
Chris@1037
|
2218 // respect the soft limit and return now.
|
Chris@1037
|
2219 if (columnCount > w/2) {
|
Chris@1037
|
2220 overridingSoftLimit = true;
|
Chris@1037
|
2221 } else {
|
Chris@1037
|
2222 #ifdef DEBUG_SPECTROGRAM_REPAINT
|
Chris@1037
|
2223 cerr << "SpectrogramLayer::paintDrawBufferPeakFrequencies: soft limit " << softTimeLimit << " sec exceeded after "
|
Chris@1037
|
2224 << columnCount << " columns with time " << diff << endl;
|
Chris@1037
|
2225 #endif
|
Chris@1037
|
2226 return columnCount;
|
Chris@1037
|
2227 }
|
Chris@1037
|
2228 }
|
Chris@1027
|
2229 }
|
Chris@1027
|
2230 }
|
Chris@488
|
2231 }
|
Chris@1024
|
2232
|
Chris@1027
|
2233 return columnCount;
|
Chris@488
|
2234 }
|
Chris@488
|
2235
|
Chris@1058
|
2236 vector<float>
|
Chris@1058
|
2237 SpectrogramLayer::getColumnFromFFTModel(FFTModel *fft,
|
Chris@1058
|
2238 int sx, // column number in model
|
Chris@1058
|
2239 int minbin,
|
Chris@1058
|
2240 int bincount) const
|
Chris@1058
|
2241 {
|
Chris@1058
|
2242 vector<float> values(bincount, 0.f);
|
Chris@1058
|
2243
|
Chris@1058
|
2244 if (m_colourScale == PhaseColourScale) {
|
Chris@1058
|
2245 fft->getPhasesAt(sx, values.data(), minbin, bincount);
|
Chris@1058
|
2246 } else {
|
Chris@1058
|
2247 fft->getMagnitudesAt(sx, values.data(), minbin, bincount);
|
Chris@1058
|
2248 }
|
Chris@1058
|
2249
|
Chris@1061
|
2250 return values;
|
Chris@1058
|
2251 }
|
Chris@1058
|
2252
|
Chris@1058
|
2253 vector<float>
|
Chris@1058
|
2254 SpectrogramLayer::getColumnFromGenericModel(DenseThreeDimensionalModel *model,
|
Chris@1058
|
2255 int sx, // column number in model
|
Chris@1058
|
2256 int minbin,
|
Chris@1058
|
2257 int bincount) const
|
Chris@1058
|
2258 {
|
Chris@1058
|
2259 if (m_colourScale == PhaseColourScale) {
|
Chris@1058
|
2260 throw std::logic_error("can't use phase scale with generic 3d model");
|
Chris@1058
|
2261 }
|
Chris@1058
|
2262
|
Chris@1058
|
2263 auto col = model->getColumn(sx);
|
Chris@1058
|
2264
|
Chris@1061
|
2265 return vector<float>(col.data() + minbin,
|
Chris@1061
|
2266 col.data() + minbin + bincount);
|
Chris@1058
|
2267 }
|
Chris@1058
|
2268
|
Chris@1058
|
2269 void
|
Chris@1058
|
2270 SpectrogramLayer::recordColumnExtents(const vector<float> &col,
|
Chris@1058
|
2271 int sx, // column index, for m_columnMags
|
Chris@1058
|
2272 MagnitudeRange &overallMag,
|
Chris@1059
|
2273 bool &overallMagChanged) const
|
Chris@1058
|
2274 {
|
Chris@1060
|
2275 if (!in_range_for(m_columnMags, sx)) {
|
Chris@1063
|
2276 m_columnMags.resize(sx + 1);
|
Chris@1058
|
2277 }
|
Chris@1058
|
2278 MagnitudeRange mr;
|
Chris@1058
|
2279 for (auto v: col) {
|
Chris@1058
|
2280 mr.sample(v);
|
Chris@1058
|
2281 }
|
Chris@1058
|
2282 m_columnMags[sx] = mr;
|
Chris@1058
|
2283 if (overallMag.sample(mr)) {
|
Chris@1058
|
2284 overallMagChanged = true;
|
Chris@1058
|
2285 }
|
Chris@1058
|
2286 }
|
Chris@1058
|
2287
|
Chris@1024
|
2288 int
|
Chris@918
|
2289 SpectrogramLayer::paintDrawBuffer(LayerGeometryProvider *v,
|
Chris@481
|
2290 int w,
|
Chris@481
|
2291 int h,
|
Chris@907
|
2292 const vector<int> &binforx,
|
Chris@907
|
2293 const vector<double> &binfory,
|
Chris@491
|
2294 bool usePeaksCache,
|
Chris@491
|
2295 MagnitudeRange &overallMag,
|
Chris@1026
|
2296 bool &overallMagChanged,
|
Chris@1039
|
2297 bool rightToLeft,
|
Chris@1039
|
2298 double softTimeLimit) const
|
Chris@480
|
2299 {
|
Chris@481
|
2300 Profiler profiler("SpectrogramLayer::paintDrawBuffer");
|
Chris@480
|
2301
|
Chris@490
|
2302 int minbin = int(binfory[0] + 0.0001);
|
Chris@907
|
2303 int maxbin = int(binfory[h-1]);
|
Chris@480
|
2304
|
Chris@485
|
2305 #ifdef DEBUG_SPECTROGRAM_REPAINT
|
Chris@1026
|
2306 cerr << "SpectrogramLayer::paintDrawBuffer: minbin " << minbin << ", maxbin " << maxbin << "; w " << w << ", h " << h << endl;
|
Chris@485
|
2307 #endif
|
Chris@480
|
2308 if (minbin < 0) minbin = 0;
|
Chris@480
|
2309 if (maxbin < 0) maxbin = minbin+1;
|
Chris@480
|
2310
|
Chris@1060
|
2311 DenseThreeDimensionalModel *peakCacheModel = 0;
|
Chris@1060
|
2312 FFTModel *fftModel = 0;
|
Chris@484
|
2313 DenseThreeDimensionalModel *sourceModel = 0;
|
Chris@1060
|
2314
|
Chris@485
|
2315 #ifdef DEBUG_SPECTROGRAM_REPAINT
|
Chris@1026
|
2316 cerr << "SpectrogramLayer::paintDrawBuffer: Note: bin display = " << m_binDisplay << ", w = " << w << ", binforx[" << w-1 << "] = " << binforx[w-1] << ", binforx[0] = " << binforx[0] << endl;
|
Chris@485
|
2317 #endif
|
Chris@1060
|
2318
|
Chris@1060
|
2319 int divisor = 1;
|
Chris@1054
|
2320 if (usePeaksCache) {
|
Chris@1088
|
2321 peakCacheModel = getPeakCache();
|
Chris@1054
|
2322 divisor = m_peakCacheDivisor;
|
Chris@1060
|
2323 sourceModel = peakCacheModel;
|
Chris@484
|
2324 } else {
|
Chris@1088
|
2325 fftModel = getFFTModel();
|
Chris@1060
|
2326 sourceModel = fftModel;
|
Chris@484
|
2327 }
|
Chris@484
|
2328
|
Chris@1024
|
2329 if (!sourceModel) return 0;
|
Chris@484
|
2330
|
Chris@490
|
2331 bool interpolate = false;
|
Chris@490
|
2332 Preferences::SpectrogramSmoothing smoothing =
|
Chris@490
|
2333 Preferences::getInstance()->getSpectrogramSmoothing();
|
Chris@490
|
2334 if (smoothing == Preferences::SpectrogramInterpolated ||
|
Chris@490
|
2335 smoothing == Preferences::SpectrogramZeroPaddedAndInterpolated) {
|
Chris@490
|
2336 if (m_binDisplay != PeakBins &&
|
Chris@490
|
2337 m_binDisplay != PeakFrequencies) {
|
Chris@490
|
2338 interpolate = true;
|
Chris@490
|
2339 }
|
Chris@490
|
2340 }
|
Chris@490
|
2341
|
Chris@480
|
2342 int psx = -1;
|
Chris@545
|
2343
|
Chris@1027
|
2344 int minColumns = 4;
|
Chris@1039
|
2345 bool haveTimeLimits = (softTimeLimit > 0.0);
|
Chris@1037
|
2346 double hardTimeLimit = softTimeLimit * 2.0;
|
Chris@1037
|
2347 bool overridingSoftLimit = false;
|
Chris@1027
|
2348 auto startTime = chrono::steady_clock::now();
|
Chris@1027
|
2349
|
Chris@1026
|
2350 int start = 0;
|
Chris@1026
|
2351 int finish = w;
|
Chris@1026
|
2352 int step = 1;
|
Chris@1026
|
2353
|
Chris@1026
|
2354 if (rightToLeft) {
|
Chris@1026
|
2355 start = w-1;
|
Chris@1026
|
2356 finish = -1;
|
Chris@1026
|
2357 step = -1;
|
Chris@1026
|
2358 }
|
Chris@1027
|
2359
|
Chris@1027
|
2360 int columnCount = 0;
|
Chris@1026
|
2361
|
Chris@1060
|
2362 vector<float> preparedColumn;
|
Chris@1060
|
2363
|
Chris@1026
|
2364 for (int x = start; x != finish; x += step) {
|
Chris@1027
|
2365
|
Chris@1056
|
2366 // x is the on-canvas pixel coord; sx (later) will be the
|
Chris@1056
|
2367 // source column index
|
Chris@1056
|
2368
|
Chris@1027
|
2369 ++columnCount;
|
Chris@480
|
2370
|
Chris@482
|
2371 if (binforx[x] < 0) continue;
|
Chris@482
|
2372
|
Chris@484
|
2373 int sx0 = binforx[x] / divisor;
|
Chris@483
|
2374 int sx1 = sx0;
|
Chris@484
|
2375 if (x+1 < w) sx1 = binforx[x+1] / divisor;
|
Chris@483
|
2376 if (sx0 < 0) sx0 = sx1 - 1;
|
Chris@483
|
2377 if (sx0 < 0) continue;
|
Chris@483
|
2378 if (sx1 <= sx0) sx1 = sx0 + 1;
|
Chris@483
|
2379
|
Chris@1064
|
2380 vector<float> pixelPeakColumn;
|
Chris@1064
|
2381
|
Chris@483
|
2382 for (int sx = sx0; sx < sx1; ++sx) {
|
Chris@483
|
2383
|
Chris@518
|
2384 #ifdef DEBUG_SPECTROGRAM_REPAINT
|
Chris@682
|
2385 // cerr << "sx = " << sx << endl;
|
Chris@518
|
2386 #endif
|
Chris@518
|
2387
|
Chris@1064
|
2388 if (sx < 0 || sx >= sourceModel->getWidth()) {
|
Chris@1064
|
2389 continue;
|
Chris@1064
|
2390 }
|
Chris@488
|
2391
|
Chris@483
|
2392 if (sx != psx) {
|
Chris@1060
|
2393
|
Chris@1064
|
2394 // order:
|
Chris@1064
|
2395 // get column -> scale -> record extents ->
|
Chris@1063
|
2396 // normalise -> peak pick -> apply display gain ->
|
Chris@1063
|
2397 // distribute/interpolate
|
Chris@1063
|
2398
|
Chris@1063
|
2399 ColumnOp::Column column;
|
Chris@1063
|
2400
|
Chris@1060
|
2401 if (peakCacheModel) {
|
Chris@1060
|
2402 column = getColumnFromGenericModel(peakCacheModel,
|
Chris@1060
|
2403 sx,
|
Chris@1060
|
2404 minbin,
|
Chris@1060
|
2405 maxbin - minbin + 1);
|
Chris@484
|
2406 } else {
|
Chris@1060
|
2407 column = getColumnFromFFTModel(fftModel,
|
Chris@1060
|
2408 sx,
|
Chris@1060
|
2409 minbin,
|
Chris@1060
|
2410 maxbin - minbin + 1);
|
Chris@484
|
2411 }
|
Chris@1060
|
2412
|
Chris@1064
|
2413 if (m_colourScale != PhaseColourScale) {
|
Chris@1087
|
2414 column = ColumnOp::fftScale(column, getFFTSize());
|
Chris@1064
|
2415 }
|
Chris@1063
|
2416
|
Chris@1062
|
2417 recordColumnExtents(column,
|
Chris@1062
|
2418 sx,
|
Chris@1062
|
2419 overallMag,
|
Chris@1062
|
2420 overallMagChanged);
|
Chris@1062
|
2421
|
Chris@1064
|
2422 if (m_colourScale != PhaseColourScale) {
|
Chris@1064
|
2423 column = ColumnOp::normalize(column, m_normalization);
|
Chris@1064
|
2424 }
|
Chris@1063
|
2425
|
Chris@1063
|
2426 if (m_binDisplay == PeakBins) {
|
Chris@1063
|
2427 column = ColumnOp::peakPick(column);
|
Chris@1063
|
2428 }
|
Chris@1063
|
2429
|
Chris@1062
|
2430 preparedColumn =
|
Chris@1064
|
2431 ColumnOp::distribute(ColumnOp::applyGain(column, m_gain),
|
Chris@1064
|
2432 h,
|
Chris@1064
|
2433 binfory,
|
Chris@1064
|
2434 minbin,
|
Chris@1064
|
2435 interpolate);
|
Chris@1060
|
2436
|
Chris@483
|
2437 psx = sx;
|
Chris@483
|
2438 }
|
Chris@483
|
2439
|
Chris@1064
|
2440 if (sx == sx0) {
|
Chris@1064
|
2441 pixelPeakColumn = preparedColumn;
|
Chris@1064
|
2442 } else {
|
Chris@1064
|
2443 for (int i = 0; in_range_for(pixelPeakColumn, i); ++i) {
|
Chris@1064
|
2444 pixelPeakColumn[i] = std::max(pixelPeakColumn[i],
|
Chris@1064
|
2445 preparedColumn[i]);
|
Chris@1064
|
2446 }
|
Chris@1064
|
2447 }
|
Chris@483
|
2448 }
|
Chris@483
|
2449
|
Chris@1065
|
2450 if (!pixelPeakColumn.empty()) {
|
Chris@1065
|
2451 for (int y = 0; y < h; ++y) {
|
Chris@1065
|
2452 m_drawBuffer.setPixel(x,
|
Chris@1065
|
2453 h-y-1,
|
Chris@1065
|
2454 getDisplayValue(v, pixelPeakColumn[y]));
|
Chris@1065
|
2455 }
|
Chris@480
|
2456 }
|
Chris@1025
|
2457
|
Chris@1039
|
2458 if (haveTimeLimits) {
|
Chris@1027
|
2459 if (columnCount >= minColumns) {
|
Chris@1025
|
2460 auto t = chrono::steady_clock::now();
|
Chris@1025
|
2461 double diff = chrono::duration<double>(t - startTime).count();
|
Chris@1037
|
2462 if (diff > hardTimeLimit) {
|
Chris@1025
|
2463 #ifdef DEBUG_SPECTROGRAM_REPAINT
|
Chris@1037
|
2464 cerr << "SpectrogramLayer::paintDrawBuffer: hard limit " << hardTimeLimit << " sec exceeded after "
|
Chris@1031
|
2465 << columnCount << " columns with time " << diff << endl;
|
Chris@1025
|
2466 #endif
|
Chris@1027
|
2467 return columnCount;
|
Chris@1037
|
2468 } else if (diff > softTimeLimit && !overridingSoftLimit) {
|
Chris@1037
|
2469 // If we're more than half way through by the time
|
Chris@1037
|
2470 // we reach the soft limit, ignore it (though
|
Chris@1037
|
2471 // still respect the hard limit, above). Otherwise
|
Chris@1037
|
2472 // respect the soft limit and return now.
|
Chris@1037
|
2473 if (columnCount > w/2) {
|
Chris@1037
|
2474 overridingSoftLimit = true;
|
Chris@1037
|
2475 } else {
|
Chris@1037
|
2476 #ifdef DEBUG_SPECTROGRAM_REPAINT
|
Chris@1037
|
2477 cerr << "SpectrogramLayer::paintDrawBuffer: soft limit " << softTimeLimit << " sec exceeded after "
|
Chris@1037
|
2478 << columnCount << " columns with time " << diff << endl;
|
Chris@1037
|
2479 #endif
|
Chris@1037
|
2480 return columnCount;
|
Chris@1037
|
2481 }
|
Chris@1037
|
2482 }
|
Chris@1025
|
2483 }
|
Chris@1025
|
2484 }
|
Chris@480
|
2485 }
|
Chris@1024
|
2486
|
Chris@1027
|
2487 return columnCount;
|
Chris@480
|
2488 }
|
Chris@477
|
2489
|
Chris@121
|
2490 void
|
Chris@918
|
2491 SpectrogramLayer::illuminateLocalFeatures(LayerGeometryProvider *v, QPainter &paint) const
|
Chris@121
|
2492 {
|
Chris@382
|
2493 Profiler profiler("SpectrogramLayer::illuminateLocalFeatures");
|
Chris@382
|
2494
|
Chris@121
|
2495 QPoint localPos;
|
Chris@121
|
2496 if (!v->shouldIlluminateLocalFeatures(this, localPos) || !m_model) {
|
Chris@121
|
2497 return;
|
Chris@121
|
2498 }
|
Chris@121
|
2499
|
Chris@682
|
2500 // cerr << "SpectrogramLayer: illuminateLocalFeatures("
|
Chris@682
|
2501 // << localPos.x() << "," << localPos.y() << ")" << endl;
|
Chris@121
|
2502
|
Chris@905
|
2503 double s0, s1;
|
Chris@905
|
2504 double f0, f1;
|
Chris@121
|
2505
|
Chris@121
|
2506 if (getXBinRange(v, localPos.x(), s0, s1) &&
|
Chris@121
|
2507 getYBinSourceRange(v, localPos.y(), f0, f1)) {
|
Chris@121
|
2508
|
Chris@121
|
2509 int s0i = int(s0 + 0.001);
|
Chris@121
|
2510 int s1i = int(s1);
|
Chris@121
|
2511
|
Chris@121
|
2512 int x0 = v->getXForFrame(s0i * getWindowIncrement());
|
Chris@121
|
2513 int x1 = v->getXForFrame((s1i + 1) * getWindowIncrement());
|
Chris@121
|
2514
|
Chris@248
|
2515 int y1 = int(getYForFrequency(v, f1));
|
Chris@248
|
2516 int y0 = int(getYForFrequency(v, f0));
|
Chris@121
|
2517
|
Chris@682
|
2518 // cerr << "SpectrogramLayer: illuminate "
|
Chris@682
|
2519 // << x0 << "," << y1 << " -> " << x1 << "," << y0 << endl;
|
Chris@121
|
2520
|
Chris@287
|
2521 paint.setPen(v->getForeground());
|
Chris@133
|
2522
|
Chris@133
|
2523 //!!! should we be using paintCrosshairs for this?
|
Chris@133
|
2524
|
Chris@121
|
2525 paint.drawRect(x0, y1, x1 - x0 + 1, y0 - y1 + 1);
|
Chris@121
|
2526 }
|
Chris@121
|
2527 }
|
Chris@121
|
2528
|
Chris@905
|
2529 double
|
Chris@918
|
2530 SpectrogramLayer::getYForFrequency(const LayerGeometryProvider *v, double frequency) const
|
Chris@42
|
2531 {
|
Chris@44
|
2532 return v->getYForFrequency(frequency,
|
Chris@44
|
2533 getEffectiveMinFrequency(),
|
Chris@44
|
2534 getEffectiveMaxFrequency(),
|
Chris@44
|
2535 m_frequencyScale == LogFrequencyScale);
|
Chris@42
|
2536 }
|
Chris@42
|
2537
|
Chris@905
|
2538 double
|
Chris@918
|
2539 SpectrogramLayer::getFrequencyForY(const LayerGeometryProvider *v, int y) const
|
Chris@42
|
2540 {
|
Chris@44
|
2541 return v->getFrequencyForY(y,
|
Chris@44
|
2542 getEffectiveMinFrequency(),
|
Chris@44
|
2543 getEffectiveMaxFrequency(),
|
Chris@44
|
2544 m_frequencyScale == LogFrequencyScale);
|
Chris@42
|
2545 }
|
Chris@42
|
2546
|
Chris@0
|
2547 int
|
Chris@918
|
2548 SpectrogramLayer::getCompletion(LayerGeometryProvider *v) const
|
Chris@0
|
2549 {
|
Chris@1088
|
2550 if (!m_fftModel) return 100;
|
Chris@1088
|
2551 int completion = m_fftModel->getCompletion();
|
Chris@224
|
2552 #ifdef DEBUG_SPECTROGRAM_REPAINT
|
Chris@985
|
2553 cerr << "SpectrogramLayer::getCompletion: completion = " << completion << endl;
|
Chris@224
|
2554 #endif
|
Chris@0
|
2555 return completion;
|
Chris@0
|
2556 }
|
Chris@0
|
2557
|
Chris@583
|
2558 QString
|
Chris@918
|
2559 SpectrogramLayer::getError(LayerGeometryProvider *v) const
|
Chris@583
|
2560 {
|
Chris@920
|
2561 const View *view = v->getView();
|
Chris@1088
|
2562 if (!m_fftModel) return "";
|
Chris@1088
|
2563 return m_fftModel->getError();
|
Chris@583
|
2564 }
|
Chris@583
|
2565
|
Chris@28
|
2566 bool
|
Chris@905
|
2567 SpectrogramLayer::getValueExtents(double &min, double &max,
|
Chris@101
|
2568 bool &logarithmic, QString &unit) const
|
Chris@79
|
2569 {
|
Chris@133
|
2570 if (!m_model) return false;
|
Chris@133
|
2571
|
Chris@907
|
2572 sv_samplerate_t sr = m_model->getSampleRate();
|
Chris@1087
|
2573 min = double(sr) / getFFTSize();
|
Chris@905
|
2574 max = double(sr) / 2;
|
Chris@133
|
2575
|
Chris@101
|
2576 logarithmic = (m_frequencyScale == LogFrequencyScale);
|
Chris@79
|
2577 unit = "Hz";
|
Chris@79
|
2578 return true;
|
Chris@79
|
2579 }
|
Chris@79
|
2580
|
Chris@79
|
2581 bool
|
Chris@905
|
2582 SpectrogramLayer::getDisplayExtents(double &min, double &max) const
|
Chris@101
|
2583 {
|
Chris@101
|
2584 min = getEffectiveMinFrequency();
|
Chris@101
|
2585 max = getEffectiveMaxFrequency();
|
Chris@253
|
2586
|
Chris@587
|
2587 // SVDEBUG << "SpectrogramLayer::getDisplayExtents: " << min << "->" << max << endl;
|
Chris@101
|
2588 return true;
|
Chris@101
|
2589 }
|
Chris@101
|
2590
|
Chris@101
|
2591 bool
|
Chris@905
|
2592 SpectrogramLayer::setDisplayExtents(double min, double max)
|
Chris@120
|
2593 {
|
Chris@120
|
2594 if (!m_model) return false;
|
Chris@187
|
2595
|
Chris@587
|
2596 // SVDEBUG << "SpectrogramLayer::setDisplayExtents: " << min << "->" << max << endl;
|
Chris@187
|
2597
|
Chris@120
|
2598 if (min < 0) min = 0;
|
Chris@907
|
2599 if (max > m_model->getSampleRate()/2.0) max = m_model->getSampleRate()/2.0;
|
Chris@120
|
2600
|
Chris@907
|
2601 int minf = int(lrint(min));
|
Chris@907
|
2602 int maxf = int(lrint(max));
|
Chris@120
|
2603
|
Chris@120
|
2604 if (m_minFrequency == minf && m_maxFrequency == maxf) return true;
|
Chris@120
|
2605
|
Chris@478
|
2606 invalidateImageCaches();
|
Chris@120
|
2607 invalidateMagnitudes();
|
Chris@120
|
2608
|
Chris@120
|
2609 m_minFrequency = minf;
|
Chris@120
|
2610 m_maxFrequency = maxf;
|
Chris@120
|
2611
|
Chris@120
|
2612 emit layerParametersChanged();
|
Chris@120
|
2613
|
Chris@133
|
2614 int vs = getCurrentVerticalZoomStep();
|
Chris@133
|
2615 if (vs != m_lastEmittedZoomStep) {
|
Chris@133
|
2616 emit verticalZoomChanged();
|
Chris@133
|
2617 m_lastEmittedZoomStep = vs;
|
Chris@133
|
2618 }
|
Chris@133
|
2619
|
Chris@120
|
2620 return true;
|
Chris@120
|
2621 }
|
Chris@120
|
2622
|
Chris@120
|
2623 bool
|
Chris@918
|
2624 SpectrogramLayer::getYScaleValue(const LayerGeometryProvider *v, int y,
|
Chris@905
|
2625 double &value, QString &unit) const
|
Chris@261
|
2626 {
|
Chris@261
|
2627 value = getFrequencyForY(v, y);
|
Chris@261
|
2628 unit = "Hz";
|
Chris@261
|
2629 return true;
|
Chris@261
|
2630 }
|
Chris@261
|
2631
|
Chris@261
|
2632 bool
|
Chris@918
|
2633 SpectrogramLayer::snapToFeatureFrame(LayerGeometryProvider *,
|
Chris@907
|
2634 sv_frame_t &frame,
|
Chris@805
|
2635 int &resolution,
|
Chris@28
|
2636 SnapType snap) const
|
Chris@13
|
2637 {
|
Chris@13
|
2638 resolution = getWindowIncrement();
|
Chris@907
|
2639 sv_frame_t left = (frame / resolution) * resolution;
|
Chris@907
|
2640 sv_frame_t right = left + resolution;
|
Chris@28
|
2641
|
Chris@28
|
2642 switch (snap) {
|
Chris@28
|
2643 case SnapLeft: frame = left; break;
|
Chris@28
|
2644 case SnapRight: frame = right; break;
|
Chris@28
|
2645 case SnapNearest:
|
Chris@28
|
2646 case SnapNeighbouring:
|
Chris@28
|
2647 if (frame - left > right - frame) frame = right;
|
Chris@28
|
2648 else frame = left;
|
Chris@28
|
2649 break;
|
Chris@28
|
2650 }
|
Chris@28
|
2651
|
Chris@28
|
2652 return true;
|
Chris@28
|
2653 }
|
Chris@13
|
2654
|
Chris@283
|
2655 void
|
Chris@918
|
2656 SpectrogramLayer::measureDoubleClick(LayerGeometryProvider *v, QMouseEvent *e)
|
Chris@283
|
2657 {
|
Chris@920
|
2658 const View *view = v->getView();
|
Chris@1030
|
2659 ScrollableImageCache &cache = getImageCacheReference(view);
|
Chris@1030
|
2660
|
Chris@1030
|
2661 cerr << "cache width: " << cache.getSize().width() << ", height: "
|
Chris@1030
|
2662 << cache.getSize().height() << endl;
|
Chris@1030
|
2663
|
Chris@1030
|
2664 QImage image = cache.getImage();
|
Chris@283
|
2665
|
Chris@283
|
2666 ImageRegionFinder finder;
|
Chris@283
|
2667 QRect rect = finder.findRegionExtents(&image, e->pos());
|
Chris@283
|
2668 if (rect.isValid()) {
|
Chris@283
|
2669 MeasureRect mr;
|
Chris@283
|
2670 setMeasureRectFromPixrect(v, mr, rect);
|
Chris@283
|
2671 CommandHistory::getInstance()->addCommand
|
Chris@283
|
2672 (new AddMeasurementRectCommand(this, mr));
|
Chris@283
|
2673 }
|
Chris@283
|
2674 }
|
Chris@283
|
2675
|
Chris@77
|
2676 bool
|
Chris@918
|
2677 SpectrogramLayer::getCrosshairExtents(LayerGeometryProvider *v, QPainter &paint,
|
Chris@77
|
2678 QPoint cursorPos,
|
Chris@1025
|
2679 vector<QRect> &extents) const
|
Chris@77
|
2680 {
|
Chris@918
|
2681 QRect vertical(cursorPos.x() - 12, 0, 12, v->getPaintHeight());
|
Chris@77
|
2682 extents.push_back(vertical);
|
Chris@77
|
2683
|
Chris@77
|
2684 QRect horizontal(0, cursorPos.y(), cursorPos.x(), 1);
|
Chris@77
|
2685 extents.push_back(horizontal);
|
Chris@77
|
2686
|
Chris@608
|
2687 int sw = getVerticalScaleWidth(v, m_haveDetailedScale, paint);
|
Chris@264
|
2688
|
Chris@280
|
2689 QRect freq(sw, cursorPos.y() - paint.fontMetrics().ascent() - 2,
|
Chris@280
|
2690 paint.fontMetrics().width("123456 Hz") + 2,
|
Chris@280
|
2691 paint.fontMetrics().height());
|
Chris@280
|
2692 extents.push_back(freq);
|
Chris@264
|
2693
|
Chris@279
|
2694 QRect pitch(sw, cursorPos.y() + 2,
|
Chris@279
|
2695 paint.fontMetrics().width("C#10+50c") + 2,
|
Chris@279
|
2696 paint.fontMetrics().height());
|
Chris@279
|
2697 extents.push_back(pitch);
|
Chris@279
|
2698
|
Chris@280
|
2699 QRect rt(cursorPos.x(),
|
Chris@918
|
2700 v->getPaintHeight() - paint.fontMetrics().height() - 2,
|
Chris@280
|
2701 paint.fontMetrics().width("1234.567 s"),
|
Chris@280
|
2702 paint.fontMetrics().height());
|
Chris@280
|
2703 extents.push_back(rt);
|
Chris@280
|
2704
|
Chris@280
|
2705 int w(paint.fontMetrics().width("1234567890") + 2);
|
Chris@280
|
2706 QRect frame(cursorPos.x() - w - 2,
|
Chris@918
|
2707 v->getPaintHeight() - paint.fontMetrics().height() - 2,
|
Chris@280
|
2708 w,
|
Chris@280
|
2709 paint.fontMetrics().height());
|
Chris@280
|
2710 extents.push_back(frame);
|
Chris@280
|
2711
|
Chris@77
|
2712 return true;
|
Chris@77
|
2713 }
|
Chris@77
|
2714
|
Chris@77
|
2715 void
|
Chris@918
|
2716 SpectrogramLayer::paintCrosshairs(LayerGeometryProvider *v, QPainter &paint,
|
Chris@77
|
2717 QPoint cursorPos) const
|
Chris@77
|
2718 {
|
Chris@77
|
2719 paint.save();
|
Chris@283
|
2720
|
Chris@608
|
2721 int sw = getVerticalScaleWidth(v, m_haveDetailedScale, paint);
|
Chris@283
|
2722
|
Chris@282
|
2723 QFont fn = paint.font();
|
Chris@282
|
2724 if (fn.pointSize() > 8) {
|
Chris@282
|
2725 fn.setPointSize(fn.pointSize() - 1);
|
Chris@282
|
2726 paint.setFont(fn);
|
Chris@282
|
2727 }
|
Chris@77
|
2728 paint.setPen(m_crosshairColour);
|
Chris@77
|
2729
|
Chris@77
|
2730 paint.drawLine(0, cursorPos.y(), cursorPos.x() - 1, cursorPos.y());
|
Chris@918
|
2731 paint.drawLine(cursorPos.x(), 0, cursorPos.x(), v->getPaintHeight());
|
Chris@77
|
2732
|
Chris@905
|
2733 double fundamental = getFrequencyForY(v, cursorPos.y());
|
Chris@77
|
2734
|
Chris@1078
|
2735 PaintAssistant::drawVisibleText(v, paint,
|
Chris@278
|
2736 sw + 2,
|
Chris@278
|
2737 cursorPos.y() - 2,
|
Chris@278
|
2738 QString("%1 Hz").arg(fundamental),
|
Chris@1078
|
2739 PaintAssistant::OutlinedText);
|
Chris@278
|
2740
|
Chris@279
|
2741 if (Pitch::isFrequencyInMidiRange(fundamental)) {
|
Chris@279
|
2742 QString pitchLabel = Pitch::getPitchLabelForFrequency(fundamental);
|
Chris@1078
|
2743 PaintAssistant::drawVisibleText(v, paint,
|
Chris@279
|
2744 sw + 2,
|
Chris@279
|
2745 cursorPos.y() + paint.fontMetrics().ascent() + 2,
|
Chris@279
|
2746 pitchLabel,
|
Chris@1078
|
2747 PaintAssistant::OutlinedText);
|
Chris@279
|
2748 }
|
Chris@279
|
2749
|
Chris@907
|
2750 sv_frame_t frame = v->getFrameForX(cursorPos.x());
|
Chris@279
|
2751 RealTime rt = RealTime::frame2RealTime(frame, m_model->getSampleRate());
|
Chris@280
|
2752 QString rtLabel = QString("%1 s").arg(rt.toText(true).c_str());
|
Chris@280
|
2753 QString frameLabel = QString("%1").arg(frame);
|
Chris@1078
|
2754 PaintAssistant::drawVisibleText(v, paint,
|
Chris@280
|
2755 cursorPos.x() - paint.fontMetrics().width(frameLabel) - 2,
|
Chris@918
|
2756 v->getPaintHeight() - 2,
|
Chris@280
|
2757 frameLabel,
|
Chris@1078
|
2758 PaintAssistant::OutlinedText);
|
Chris@1078
|
2759 PaintAssistant::drawVisibleText(v, paint,
|
Chris@280
|
2760 cursorPos.x() + 2,
|
Chris@918
|
2761 v->getPaintHeight() - 2,
|
Chris@280
|
2762 rtLabel,
|
Chris@1078
|
2763 PaintAssistant::OutlinedText);
|
Chris@264
|
2764
|
Chris@77
|
2765 int harmonic = 2;
|
Chris@77
|
2766
|
Chris@77
|
2767 while (harmonic < 100) {
|
Chris@77
|
2768
|
Chris@907
|
2769 int hy = int(lrint(getYForFrequency(v, fundamental * harmonic)));
|
Chris@918
|
2770 if (hy < 0 || hy > v->getPaintHeight()) break;
|
Chris@77
|
2771
|
Chris@77
|
2772 int len = 7;
|
Chris@77
|
2773
|
Chris@77
|
2774 if (harmonic % 2 == 0) {
|
Chris@77
|
2775 if (harmonic % 4 == 0) {
|
Chris@77
|
2776 len = 12;
|
Chris@77
|
2777 } else {
|
Chris@77
|
2778 len = 10;
|
Chris@77
|
2779 }
|
Chris@77
|
2780 }
|
Chris@77
|
2781
|
Chris@77
|
2782 paint.drawLine(cursorPos.x() - len,
|
Chris@907
|
2783 hy,
|
Chris@77
|
2784 cursorPos.x(),
|
Chris@907
|
2785 hy);
|
Chris@77
|
2786
|
Chris@77
|
2787 ++harmonic;
|
Chris@77
|
2788 }
|
Chris@77
|
2789
|
Chris@77
|
2790 paint.restore();
|
Chris@77
|
2791 }
|
Chris@77
|
2792
|
Chris@25
|
2793 QString
|
Chris@918
|
2794 SpectrogramLayer::getFeatureDescription(LayerGeometryProvider *v, QPoint &pos) const
|
Chris@25
|
2795 {
|
Chris@25
|
2796 int x = pos.x();
|
Chris@25
|
2797 int y = pos.y();
|
Chris@0
|
2798
|
Chris@25
|
2799 if (!m_model || !m_model->isOK()) return "";
|
Chris@0
|
2800
|
Chris@905
|
2801 double magMin = 0, magMax = 0;
|
Chris@905
|
2802 double phaseMin = 0, phaseMax = 0;
|
Chris@905
|
2803 double freqMin = 0, freqMax = 0;
|
Chris@905
|
2804 double adjFreqMin = 0, adjFreqMax = 0;
|
Chris@25
|
2805 QString pitchMin, pitchMax;
|
Chris@0
|
2806 RealTime rtMin, rtMax;
|
Chris@0
|
2807
|
Chris@38
|
2808 bool haveValues = false;
|
Chris@0
|
2809
|
Chris@44
|
2810 if (!getXBinSourceRange(v, x, rtMin, rtMax)) {
|
Chris@38
|
2811 return "";
|
Chris@38
|
2812 }
|
Chris@44
|
2813 if (getXYBinSourceRange(v, x, y, magMin, magMax, phaseMin, phaseMax)) {
|
Chris@38
|
2814 haveValues = true;
|
Chris@38
|
2815 }
|
Chris@0
|
2816
|
Chris@35
|
2817 QString adjFreqText = "", adjPitchText = "";
|
Chris@35
|
2818
|
Chris@38
|
2819 if (m_binDisplay == PeakFrequencies) {
|
Chris@35
|
2820
|
Chris@44
|
2821 if (!getAdjustedYBinSourceRange(v, x, y, freqMin, freqMax,
|
Chris@38
|
2822 adjFreqMin, adjFreqMax)) {
|
Chris@38
|
2823 return "";
|
Chris@38
|
2824 }
|
Chris@35
|
2825
|
Chris@35
|
2826 if (adjFreqMin != adjFreqMax) {
|
Chris@65
|
2827 adjFreqText = tr("Peak Frequency:\t%1 - %2 Hz\n")
|
Chris@35
|
2828 .arg(adjFreqMin).arg(adjFreqMax);
|
Chris@35
|
2829 } else {
|
Chris@65
|
2830 adjFreqText = tr("Peak Frequency:\t%1 Hz\n")
|
Chris@35
|
2831 .arg(adjFreqMin);
|
Chris@38
|
2832 }
|
Chris@38
|
2833
|
Chris@38
|
2834 QString pmin = Pitch::getPitchLabelForFrequency(adjFreqMin);
|
Chris@38
|
2835 QString pmax = Pitch::getPitchLabelForFrequency(adjFreqMax);
|
Chris@38
|
2836
|
Chris@38
|
2837 if (pmin != pmax) {
|
Chris@65
|
2838 adjPitchText = tr("Peak Pitch:\t%3 - %4\n").arg(pmin).arg(pmax);
|
Chris@38
|
2839 } else {
|
Chris@65
|
2840 adjPitchText = tr("Peak Pitch:\t%2\n").arg(pmin);
|
Chris@35
|
2841 }
|
Chris@35
|
2842
|
Chris@35
|
2843 } else {
|
Chris@35
|
2844
|
Chris@44
|
2845 if (!getYBinSourceRange(v, y, freqMin, freqMax)) return "";
|
Chris@35
|
2846 }
|
Chris@35
|
2847
|
Chris@25
|
2848 QString text;
|
Chris@25
|
2849
|
Chris@25
|
2850 if (rtMin != rtMax) {
|
Chris@25
|
2851 text += tr("Time:\t%1 - %2\n")
|
Chris@25
|
2852 .arg(rtMin.toText(true).c_str())
|
Chris@25
|
2853 .arg(rtMax.toText(true).c_str());
|
Chris@25
|
2854 } else {
|
Chris@25
|
2855 text += tr("Time:\t%1\n")
|
Chris@25
|
2856 .arg(rtMin.toText(true).c_str());
|
Chris@0
|
2857 }
|
Chris@0
|
2858
|
Chris@25
|
2859 if (freqMin != freqMax) {
|
Chris@65
|
2860 text += tr("%1Bin Frequency:\t%2 - %3 Hz\n%4Bin Pitch:\t%5 - %6\n")
|
Chris@65
|
2861 .arg(adjFreqText)
|
Chris@25
|
2862 .arg(freqMin)
|
Chris@25
|
2863 .arg(freqMax)
|
Chris@65
|
2864 .arg(adjPitchText)
|
Chris@65
|
2865 .arg(Pitch::getPitchLabelForFrequency(freqMin))
|
Chris@65
|
2866 .arg(Pitch::getPitchLabelForFrequency(freqMax));
|
Chris@65
|
2867 } else {
|
Chris@65
|
2868 text += tr("%1Bin Frequency:\t%2 Hz\n%3Bin Pitch:\t%4\n")
|
Chris@35
|
2869 .arg(adjFreqText)
|
Chris@25
|
2870 .arg(freqMin)
|
Chris@65
|
2871 .arg(adjPitchText)
|
Chris@65
|
2872 .arg(Pitch::getPitchLabelForFrequency(freqMin));
|
Chris@25
|
2873 }
|
Chris@25
|
2874
|
Chris@38
|
2875 if (haveValues) {
|
Chris@905
|
2876 double dbMin = AudioLevel::multiplier_to_dB(magMin);
|
Chris@905
|
2877 double dbMax = AudioLevel::multiplier_to_dB(magMax);
|
Chris@43
|
2878 QString dbMinString;
|
Chris@43
|
2879 QString dbMaxString;
|
Chris@43
|
2880 if (dbMin == AudioLevel::DB_FLOOR) {
|
Chris@43
|
2881 dbMinString = tr("-Inf");
|
Chris@43
|
2882 } else {
|
Chris@907
|
2883 dbMinString = QString("%1").arg(lrint(dbMin));
|
Chris@43
|
2884 }
|
Chris@43
|
2885 if (dbMax == AudioLevel::DB_FLOOR) {
|
Chris@43
|
2886 dbMaxString = tr("-Inf");
|
Chris@43
|
2887 } else {
|
Chris@907
|
2888 dbMaxString = QString("%1").arg(lrint(dbMax));
|
Chris@43
|
2889 }
|
Chris@907
|
2890 if (lrint(dbMin) != lrint(dbMax)) {
|
Chris@199
|
2891 text += tr("dB:\t%1 - %2").arg(dbMinString).arg(dbMaxString);
|
Chris@25
|
2892 } else {
|
Chris@199
|
2893 text += tr("dB:\t%1").arg(dbMinString);
|
Chris@25
|
2894 }
|
Chris@38
|
2895 if (phaseMin != phaseMax) {
|
Chris@38
|
2896 text += tr("\nPhase:\t%1 - %2").arg(phaseMin).arg(phaseMax);
|
Chris@38
|
2897 } else {
|
Chris@38
|
2898 text += tr("\nPhase:\t%1").arg(phaseMin);
|
Chris@38
|
2899 }
|
Chris@25
|
2900 }
|
Chris@25
|
2901
|
Chris@25
|
2902 return text;
|
Chris@0
|
2903 }
|
Chris@25
|
2904
|
Chris@0
|
2905 int
|
Chris@40
|
2906 SpectrogramLayer::getColourScaleWidth(QPainter &paint) const
|
Chris@40
|
2907 {
|
Chris@40
|
2908 int cw;
|
Chris@40
|
2909
|
Chris@119
|
2910 cw = paint.fontMetrics().width("-80dB");
|
Chris@119
|
2911
|
Chris@40
|
2912 return cw;
|
Chris@40
|
2913 }
|
Chris@40
|
2914
|
Chris@40
|
2915 int
|
Chris@918
|
2916 SpectrogramLayer::getVerticalScaleWidth(LayerGeometryProvider *, bool detailed, QPainter &paint) const
|
Chris@0
|
2917 {
|
Chris@0
|
2918 if (!m_model || !m_model->isOK()) return 0;
|
Chris@0
|
2919
|
Chris@607
|
2920 int cw = 0;
|
Chris@607
|
2921 if (detailed) cw = getColourScaleWidth(paint);
|
Chris@40
|
2922
|
Chris@0
|
2923 int tw = paint.fontMetrics().width(QString("%1")
|
Chris@0
|
2924 .arg(m_maxFrequency > 0 ?
|
Chris@0
|
2925 m_maxFrequency - 1 :
|
Chris@0
|
2926 m_model->getSampleRate() / 2));
|
Chris@0
|
2927
|
Chris@234
|
2928 int fw = paint.fontMetrics().width(tr("43Hz"));
|
Chris@0
|
2929 if (tw < fw) tw = fw;
|
Chris@40
|
2930
|
Chris@40
|
2931 int tickw = (m_frequencyScale == LogFrequencyScale ? 10 : 4);
|
Chris@0
|
2932
|
Chris@40
|
2933 return cw + tickw + tw + 13;
|
Chris@0
|
2934 }
|
Chris@0
|
2935
|
Chris@0
|
2936 void
|
Chris@918
|
2937 SpectrogramLayer::paintVerticalScale(LayerGeometryProvider *v, bool detailed, QPainter &paint, QRect rect) const
|
Chris@0
|
2938 {
|
Chris@0
|
2939 if (!m_model || !m_model->isOK()) {
|
Chris@0
|
2940 return;
|
Chris@0
|
2941 }
|
Chris@0
|
2942
|
Chris@382
|
2943 Profiler profiler("SpectrogramLayer::paintVerticalScale");
|
Chris@122
|
2944
|
Chris@120
|
2945 //!!! cache this?
|
Chris@120
|
2946
|
Chris@0
|
2947 int h = rect.height(), w = rect.width();
|
Chris@0
|
2948
|
Chris@40
|
2949 int tickw = (m_frequencyScale == LogFrequencyScale ? 10 : 4);
|
Chris@40
|
2950 int pkw = (m_frequencyScale == LogFrequencyScale ? 10 : 0);
|
Chris@40
|
2951
|
Chris@1087
|
2952 int bins = getFFTSize() / 2;
|
Chris@907
|
2953 sv_samplerate_t sr = m_model->getSampleRate();
|
Chris@0
|
2954
|
Chris@0
|
2955 if (m_maxFrequency > 0) {
|
Chris@1087
|
2956 bins = int((double(m_maxFrequency) * getFFTSize()) / sr + 0.1);
|
Chris@1087
|
2957 if (bins > getFFTSize() / 2) bins = getFFTSize() / 2;
|
Chris@0
|
2958 }
|
Chris@0
|
2959
|
Chris@607
|
2960 int cw = 0;
|
Chris@607
|
2961
|
Chris@607
|
2962 if (detailed) cw = getColourScaleWidth(paint);
|
Chris@119
|
2963 int cbw = paint.fontMetrics().width("dB");
|
Chris@40
|
2964
|
Chris@0
|
2965 int py = -1;
|
Chris@0
|
2966 int textHeight = paint.fontMetrics().height();
|
Chris@0
|
2967 int toff = -textHeight + paint.fontMetrics().ascent() + 2;
|
Chris@0
|
2968
|
Chris@607
|
2969 if (detailed && (h > textHeight * 3 + 10)) {
|
Chris@119
|
2970
|
Chris@119
|
2971 int topLines = 2;
|
Chris@119
|
2972 if (m_colourScale == PhaseColourScale) topLines = 1;
|
Chris@119
|
2973
|
Chris@119
|
2974 int ch = h - textHeight * (topLines + 1) - 8;
|
Chris@119
|
2975 // paint.drawRect(4, textHeight + 4, cw - 1, ch + 1);
|
Chris@119
|
2976 paint.drawRect(4 + cw - cbw, textHeight * topLines + 4, cbw - 1, ch + 1);
|
Chris@40
|
2977
|
Chris@40
|
2978 QString top, bottom;
|
Chris@1030
|
2979 double min = m_viewMags[v->getId()].getMin();
|
Chris@1030
|
2980 double max = m_viewMags[v->getId()].getMax();
|
Chris@905
|
2981
|
Chris@905
|
2982 double dBmin = AudioLevel::multiplier_to_dB(min);
|
Chris@905
|
2983 double dBmax = AudioLevel::multiplier_to_dB(max);
|
Chris@119
|
2984
|
Chris@1044
|
2985 #ifdef DEBUG_SPECTROGRAM_REPAINT
|
Chris@1044
|
2986 cerr << "paintVerticalScale: for view id " << v->getId()
|
Chris@1044
|
2987 << ": min = " << min << ", max = " << max
|
Chris@1044
|
2988 << ", dBmin = " << dBmin << ", dBmax = " << dBmax << endl;
|
Chris@1044
|
2989 #endif
|
Chris@1044
|
2990
|
Chris@120
|
2991 if (dBmax < -60.f) dBmax = -60.f;
|
Chris@907
|
2992 else top = QString("%1").arg(lrint(dBmax));
|
Chris@120
|
2993
|
Chris@120
|
2994 if (dBmin < dBmax - 60.f) dBmin = dBmax - 60.f;
|
Chris@907
|
2995 bottom = QString("%1").arg(lrint(dBmin));
|
Chris@119
|
2996
|
Chris@119
|
2997 //!!! & phase etc
|
Chris@119
|
2998
|
Chris@119
|
2999 if (m_colourScale != PhaseColourScale) {
|
Chris@119
|
3000 paint.drawText((cw + 6 - paint.fontMetrics().width("dBFS")) / 2,
|
Chris@119
|
3001 2 + textHeight + toff, "dBFS");
|
Chris@119
|
3002 }
|
Chris@119
|
3003
|
Chris@119
|
3004 // paint.drawText((cw + 6 - paint.fontMetrics().width(top)) / 2,
|
Chris@119
|
3005 paint.drawText(3 + cw - cbw - paint.fontMetrics().width(top),
|
Chris@119
|
3006 2 + textHeight * topLines + toff + textHeight/2, top);
|
Chris@119
|
3007
|
Chris@119
|
3008 paint.drawText(3 + cw - cbw - paint.fontMetrics().width(bottom),
|
Chris@119
|
3009 h + toff - 3 - textHeight/2, bottom);
|
Chris@40
|
3010
|
Chris@40
|
3011 paint.save();
|
Chris@40
|
3012 paint.setBrush(Qt::NoBrush);
|
Chris@119
|
3013
|
Chris@119
|
3014 int lasty = 0;
|
Chris@119
|
3015 int lastdb = 0;
|
Chris@119
|
3016
|
Chris@40
|
3017 for (int i = 0; i < ch; ++i) {
|
Chris@119
|
3018
|
Chris@905
|
3019 double dBval = dBmin + (((dBmax - dBmin) * i) / (ch - 1));
|
Chris@119
|
3020 int idb = int(dBval);
|
Chris@119
|
3021
|
Chris@905
|
3022 double value = AudioLevel::dB_to_multiplier(dBval);
|
Chris@119
|
3023 int colour = getDisplayValue(v, value * m_gain);
|
Chris@210
|
3024
|
Chris@907
|
3025 paint.setPen(m_palette.getColour((unsigned char)colour));
|
Chris@119
|
3026
|
Chris@119
|
3027 int y = textHeight * topLines + 4 + ch - i;
|
Chris@119
|
3028
|
Chris@119
|
3029 paint.drawLine(5 + cw - cbw, y, cw + 2, y);
|
Chris@119
|
3030
|
Chris@119
|
3031 if (i == 0) {
|
Chris@119
|
3032 lasty = y;
|
Chris@119
|
3033 lastdb = idb;
|
Chris@119
|
3034 } else if (i < ch - paint.fontMetrics().ascent() &&
|
Chris@120
|
3035 idb != lastdb &&
|
Chris@119
|
3036 ((abs(y - lasty) > textHeight &&
|
Chris@119
|
3037 idb % 10 == 0) ||
|
Chris@119
|
3038 (abs(y - lasty) > paint.fontMetrics().ascent() &&
|
Chris@119
|
3039 idb % 5 == 0))) {
|
Chris@287
|
3040 paint.setPen(v->getBackground());
|
Chris@119
|
3041 QString text = QString("%1").arg(idb);
|
Chris@119
|
3042 paint.drawText(3 + cw - cbw - paint.fontMetrics().width(text),
|
Chris@119
|
3043 y + toff + textHeight/2, text);
|
Chris@287
|
3044 paint.setPen(v->getForeground());
|
Chris@119
|
3045 paint.drawLine(5 + cw - cbw, y, 8 + cw - cbw, y);
|
Chris@119
|
3046 lasty = y;
|
Chris@119
|
3047 lastdb = idb;
|
Chris@119
|
3048 }
|
Chris@40
|
3049 }
|
Chris@40
|
3050 paint.restore();
|
Chris@40
|
3051 }
|
Chris@40
|
3052
|
Chris@40
|
3053 paint.drawLine(cw + 7, 0, cw + 7, h);
|
Chris@40
|
3054
|
Chris@0
|
3055 int bin = -1;
|
Chris@0
|
3056
|
Chris@918
|
3057 for (int y = 0; y < v->getPaintHeight(); ++y) {
|
Chris@0
|
3058
|
Chris@905
|
3059 double q0, q1;
|
Chris@918
|
3060 if (!getYBinRange(v, v->getPaintHeight() - y, q0, q1)) continue;
|
Chris@0
|
3061
|
Chris@0
|
3062 int vy;
|
Chris@0
|
3063
|
Chris@0
|
3064 if (int(q0) > bin) {
|
Chris@0
|
3065 vy = y;
|
Chris@0
|
3066 bin = int(q0);
|
Chris@0
|
3067 } else {
|
Chris@0
|
3068 continue;
|
Chris@0
|
3069 }
|
Chris@0
|
3070
|
Chris@1087
|
3071 int freq = int((sr * bin) / getFFTSize());
|
Chris@0
|
3072
|
Chris@0
|
3073 if (py >= 0 && (vy - py) < textHeight - 1) {
|
Chris@40
|
3074 if (m_frequencyScale == LinearFrequencyScale) {
|
Chris@40
|
3075 paint.drawLine(w - tickw, h - vy, w, h - vy);
|
Chris@40
|
3076 }
|
Chris@0
|
3077 continue;
|
Chris@0
|
3078 }
|
Chris@0
|
3079
|
Chris@0
|
3080 QString text = QString("%1").arg(freq);
|
Chris@234
|
3081 if (bin == 1) text = tr("%1Hz").arg(freq); // bin 0 is DC
|
Chris@40
|
3082 paint.drawLine(cw + 7, h - vy, w - pkw - 1, h - vy);
|
Chris@0
|
3083
|
Chris@0
|
3084 if (h - vy - textHeight >= -2) {
|
Chris@1025
|
3085 int tx = w - 3 - paint.fontMetrics().width(text) - max(tickw, pkw);
|
Chris@0
|
3086 paint.drawText(tx, h - vy + toff, text);
|
Chris@0
|
3087 }
|
Chris@0
|
3088
|
Chris@0
|
3089 py = vy;
|
Chris@0
|
3090 }
|
Chris@40
|
3091
|
Chris@40
|
3092 if (m_frequencyScale == LogFrequencyScale) {
|
Chris@40
|
3093
|
Chris@277
|
3094 // piano keyboard
|
Chris@277
|
3095
|
Chris@690
|
3096 PianoScale().paintPianoVertical
|
Chris@690
|
3097 (v, paint, QRect(w - pkw - 1, 0, pkw, h),
|
Chris@690
|
3098 getEffectiveMinFrequency(), getEffectiveMaxFrequency());
|
Chris@40
|
3099 }
|
Chris@608
|
3100
|
Chris@608
|
3101 m_haveDetailedScale = detailed;
|
Chris@0
|
3102 }
|
Chris@0
|
3103
|
Chris@187
|
3104 class SpectrogramRangeMapper : public RangeMapper
|
Chris@187
|
3105 {
|
Chris@187
|
3106 public:
|
Chris@901
|
3107 SpectrogramRangeMapper(sv_samplerate_t sr, int /* fftsize */) :
|
Chris@901
|
3108 m_dist(sr / 2),
|
Chris@901
|
3109 m_s2(sqrt(sqrt(2))) { }
|
Chris@187
|
3110 ~SpectrogramRangeMapper() { }
|
Chris@187
|
3111
|
Chris@901
|
3112 virtual int getPositionForValue(double value) const {
|
Chris@901
|
3113
|
Chris@901
|
3114 double dist = m_dist;
|
Chris@187
|
3115
|
Chris@187
|
3116 int n = 0;
|
Chris@187
|
3117
|
Chris@901
|
3118 while (dist > (value + 0.00001) && dist > 0.1) {
|
Chris@187
|
3119 dist /= m_s2;
|
Chris@187
|
3120 ++n;
|
Chris@187
|
3121 }
|
Chris@187
|
3122
|
Chris@187
|
3123 return n;
|
Chris@187
|
3124 }
|
Chris@724
|
3125
|
Chris@901
|
3126 virtual int getPositionForValueUnclamped(double value) const {
|
Chris@724
|
3127 // We don't really support this
|
Chris@724
|
3128 return getPositionForValue(value);
|
Chris@724
|
3129 }
|
Chris@187
|
3130
|
Chris@901
|
3131 virtual double getValueForPosition(int position) const {
|
Chris@187
|
3132
|
Chris@187
|
3133 // Vertical zoom step 0 shows the entire range from DC ->
|
Chris@187
|
3134 // Nyquist frequency. Step 1 shows 2^(1/4) of the range of
|
Chris@187
|
3135 // step 0, and so on until the visible range is smaller than
|
Chris@187
|
3136 // the frequency step between bins at the current fft size.
|
Chris@187
|
3137
|
Chris@901
|
3138 double dist = m_dist;
|
Chris@187
|
3139
|
Chris@187
|
3140 int n = 0;
|
Chris@187
|
3141 while (n < position) {
|
Chris@187
|
3142 dist /= m_s2;
|
Chris@187
|
3143 ++n;
|
Chris@187
|
3144 }
|
Chris@187
|
3145
|
Chris@187
|
3146 return dist;
|
Chris@187
|
3147 }
|
Chris@187
|
3148
|
Chris@901
|
3149 virtual double getValueForPositionUnclamped(int position) const {
|
Chris@724
|
3150 // We don't really support this
|
Chris@724
|
3151 return getValueForPosition(position);
|
Chris@724
|
3152 }
|
Chris@724
|
3153
|
Chris@187
|
3154 virtual QString getUnit() const { return "Hz"; }
|
Chris@187
|
3155
|
Chris@187
|
3156 protected:
|
Chris@901
|
3157 double m_dist;
|
Chris@901
|
3158 double m_s2;
|
Chris@187
|
3159 };
|
Chris@187
|
3160
|
Chris@133
|
3161 int
|
Chris@133
|
3162 SpectrogramLayer::getVerticalZoomSteps(int &defaultStep) const
|
Chris@133
|
3163 {
|
Chris@135
|
3164 if (!m_model) return 0;
|
Chris@187
|
3165
|
Chris@907
|
3166 sv_samplerate_t sr = m_model->getSampleRate();
|
Chris@187
|
3167
|
Chris@1087
|
3168 SpectrogramRangeMapper mapper(sr, getFFTSize());
|
Chris@1087
|
3169
|
Chris@1087
|
3170 // int maxStep = mapper.getPositionForValue((double(sr) / getFFTSize()) + 0.001);
|
Chris@187
|
3171 int maxStep = mapper.getPositionForValue(0);
|
Chris@905
|
3172 int minStep = mapper.getPositionForValue(double(sr) / 2);
|
Chris@250
|
3173
|
Chris@805
|
3174 int initialMax = m_initialMaxFrequency;
|
Chris@907
|
3175 if (initialMax == 0) initialMax = int(sr / 2);
|
Chris@250
|
3176
|
Chris@250
|
3177 defaultStep = mapper.getPositionForValue(initialMax) - minStep;
|
Chris@250
|
3178
|
Chris@587
|
3179 // SVDEBUG << "SpectrogramLayer::getVerticalZoomSteps: " << maxStep - minStep << " (" << maxStep <<"-" << minStep << "), default is " << defaultStep << " (from initial max freq " << initialMax << ")" << endl;
|
Chris@187
|
3180
|
Chris@187
|
3181 return maxStep - minStep;
|
Chris@133
|
3182 }
|
Chris@133
|
3183
|
Chris@133
|
3184 int
|
Chris@133
|
3185 SpectrogramLayer::getCurrentVerticalZoomStep() const
|
Chris@133
|
3186 {
|
Chris@133
|
3187 if (!m_model) return 0;
|
Chris@133
|
3188
|
Chris@905
|
3189 double dmin, dmax;
|
Chris@133
|
3190 getDisplayExtents(dmin, dmax);
|
Chris@133
|
3191
|
Chris@1087
|
3192 SpectrogramRangeMapper mapper(m_model->getSampleRate(), getFFTSize());
|
Chris@187
|
3193 int n = mapper.getPositionForValue(dmax - dmin);
|
Chris@587
|
3194 // SVDEBUG << "SpectrogramLayer::getCurrentVerticalZoomStep: " << n << endl;
|
Chris@133
|
3195 return n;
|
Chris@133
|
3196 }
|
Chris@133
|
3197
|
Chris@133
|
3198 void
|
Chris@133
|
3199 SpectrogramLayer::setVerticalZoomStep(int step)
|
Chris@133
|
3200 {
|
Chris@187
|
3201 if (!m_model) return;
|
Chris@187
|
3202
|
Chris@905
|
3203 double dmin = m_minFrequency, dmax = m_maxFrequency;
|
Chris@253
|
3204 // getDisplayExtents(dmin, dmax);
|
Chris@253
|
3205
|
Chris@682
|
3206 // cerr << "current range " << dmin << " -> " << dmax << ", range " << dmax-dmin << ", mid " << (dmax + dmin)/2 << endl;
|
Chris@133
|
3207
|
Chris@907
|
3208 sv_samplerate_t sr = m_model->getSampleRate();
|
Chris@1087
|
3209 SpectrogramRangeMapper mapper(sr, getFFTSize());
|
Chris@905
|
3210 double newdist = mapper.getValueForPosition(step);
|
Chris@905
|
3211
|
Chris@905
|
3212 double newmin, newmax;
|
Chris@253
|
3213
|
Chris@253
|
3214 if (m_frequencyScale == LogFrequencyScale) {
|
Chris@253
|
3215
|
Chris@253
|
3216 // need to pick newmin and newmax such that
|
Chris@253
|
3217 //
|
Chris@253
|
3218 // (log(newmin) + log(newmax)) / 2 == logmid
|
Chris@253
|
3219 // and
|
Chris@253
|
3220 // newmax - newmin = newdist
|
Chris@253
|
3221 //
|
Chris@253
|
3222 // so log(newmax - newdist) + log(newmax) == 2logmid
|
Chris@253
|
3223 // log(newmax(newmax - newdist)) == 2logmid
|
Chris@253
|
3224 // newmax.newmax - newmax.newdist == exp(2logmid)
|
Chris@253
|
3225 // newmax^2 + (-newdist)newmax + -exp(2logmid) == 0
|
Chris@253
|
3226 // quadratic with a = 1, b = -newdist, c = -exp(2logmid), all known
|
Chris@253
|
3227 //
|
Chris@253
|
3228 // positive root
|
Chris@253
|
3229 // newmax = (newdist + sqrt(newdist^2 + 4exp(2logmid))) / 2
|
Chris@253
|
3230 //
|
Chris@253
|
3231 // but logmid = (log(dmin) + log(dmax)) / 2
|
Chris@253
|
3232 // so exp(2logmid) = exp(log(dmin) + log(dmax))
|
Chris@253
|
3233 // = exp(log(dmin.dmax))
|
Chris@253
|
3234 // = dmin.dmax
|
Chris@253
|
3235 // so newmax = (newdist + sqrtf(newdist^2 + 4dmin.dmax)) / 2
|
Chris@253
|
3236
|
Chris@907
|
3237 newmax = (newdist + sqrt(newdist*newdist + 4*dmin*dmax)) / 2;
|
Chris@253
|
3238 newmin = newmax - newdist;
|
Chris@253
|
3239
|
Chris@682
|
3240 // cerr << "newmin = " << newmin << ", newmax = " << newmax << endl;
|
Chris@253
|
3241
|
Chris@253
|
3242 } else {
|
Chris@905
|
3243 double dmid = (dmax + dmin) / 2;
|
Chris@253
|
3244 newmin = dmid - newdist / 2;
|
Chris@253
|
3245 newmax = dmid + newdist / 2;
|
Chris@253
|
3246 }
|
Chris@187
|
3247
|
Chris@905
|
3248 double mmin, mmax;
|
Chris@187
|
3249 mmin = 0;
|
Chris@905
|
3250 mmax = double(sr) / 2;
|
Chris@133
|
3251
|
Chris@187
|
3252 if (newmin < mmin) {
|
Chris@187
|
3253 newmax += (mmin - newmin);
|
Chris@187
|
3254 newmin = mmin;
|
Chris@187
|
3255 }
|
Chris@187
|
3256 if (newmax > mmax) {
|
Chris@187
|
3257 newmax = mmax;
|
Chris@187
|
3258 }
|
Chris@133
|
3259
|
Chris@587
|
3260 // SVDEBUG << "SpectrogramLayer::setVerticalZoomStep: " << step << ": " << newmin << " -> " << newmax << " (range " << newdist << ")" << endl;
|
Chris@253
|
3261
|
Chris@907
|
3262 setMinFrequency(int(lrint(newmin)));
|
Chris@907
|
3263 setMaxFrequency(int(lrint(newmax)));
|
Chris@187
|
3264 }
|
Chris@187
|
3265
|
Chris@187
|
3266 RangeMapper *
|
Chris@187
|
3267 SpectrogramLayer::getNewVerticalZoomRangeMapper() const
|
Chris@187
|
3268 {
|
Chris@187
|
3269 if (!m_model) return 0;
|
Chris@1087
|
3270 return new SpectrogramRangeMapper(m_model->getSampleRate(), getFFTSize());
|
Chris@133
|
3271 }
|
Chris@133
|
3272
|
Chris@273
|
3273 void
|
Chris@918
|
3274 SpectrogramLayer::updateMeasureRectYCoords(LayerGeometryProvider *v, const MeasureRect &r) const
|
Chris@273
|
3275 {
|
Chris@273
|
3276 int y0 = 0;
|
Chris@907
|
3277 if (r.startY > 0.0) y0 = int(getYForFrequency(v, r.startY));
|
Chris@273
|
3278
|
Chris@273
|
3279 int y1 = y0;
|
Chris@907
|
3280 if (r.endY > 0.0) y1 = int(getYForFrequency(v, r.endY));
|
Chris@273
|
3281
|
Chris@587
|
3282 // SVDEBUG << "SpectrogramLayer::updateMeasureRectYCoords: start " << r.startY << " -> " << y0 << ", end " << r.endY << " -> " << y1 << endl;
|
Chris@273
|
3283
|
Chris@273
|
3284 r.pixrect = QRect(r.pixrect.x(), y0, r.pixrect.width(), y1 - y0);
|
Chris@273
|
3285 }
|
Chris@273
|
3286
|
Chris@273
|
3287 void
|
Chris@918
|
3288 SpectrogramLayer::setMeasureRectYCoord(LayerGeometryProvider *v, MeasureRect &r, bool start, int y) const
|
Chris@273
|
3289 {
|
Chris@273
|
3290 if (start) {
|
Chris@273
|
3291 r.startY = getFrequencyForY(v, y);
|
Chris@273
|
3292 r.endY = r.startY;
|
Chris@273
|
3293 } else {
|
Chris@273
|
3294 r.endY = getFrequencyForY(v, y);
|
Chris@273
|
3295 }
|
Chris@587
|
3296 // SVDEBUG << "SpectrogramLayer::setMeasureRectYCoord: start " << r.startY << " <- " << y << ", end " << r.endY << " <- " << y << endl;
|
Chris@273
|
3297
|
Chris@273
|
3298 }
|
Chris@273
|
3299
|
Chris@316
|
3300 void
|
Chris@316
|
3301 SpectrogramLayer::toXml(QTextStream &stream,
|
Chris@316
|
3302 QString indent, QString extraAttributes) const
|
Chris@6
|
3303 {
|
Chris@6
|
3304 QString s;
|
Chris@6
|
3305
|
Chris@6
|
3306 s += QString("channel=\"%1\" "
|
Chris@6
|
3307 "windowSize=\"%2\" "
|
Chris@153
|
3308 "windowHopLevel=\"%3\" "
|
Chris@153
|
3309 "gain=\"%4\" "
|
Chris@153
|
3310 "threshold=\"%5\" ")
|
Chris@6
|
3311 .arg(m_channel)
|
Chris@6
|
3312 .arg(m_windowSize)
|
Chris@97
|
3313 .arg(m_windowHopLevel)
|
Chris@37
|
3314 .arg(m_gain)
|
Chris@37
|
3315 .arg(m_threshold);
|
Chris@37
|
3316
|
Chris@37
|
3317 s += QString("minFrequency=\"%1\" "
|
Chris@37
|
3318 "maxFrequency=\"%2\" "
|
Chris@37
|
3319 "colourScale=\"%3\" "
|
Chris@37
|
3320 "colourScheme=\"%4\" "
|
Chris@37
|
3321 "colourRotation=\"%5\" "
|
Chris@37
|
3322 "frequencyScale=\"%6\" "
|
Chris@761
|
3323 "binDisplay=\"%7\" ")
|
Chris@37
|
3324 .arg(m_minFrequency)
|
Chris@6
|
3325 .arg(m_maxFrequency)
|
Chris@6
|
3326 .arg(m_colourScale)
|
Chris@197
|
3327 .arg(m_colourMap)
|
Chris@37
|
3328 .arg(m_colourRotation)
|
Chris@35
|
3329 .arg(m_frequencyScale)
|
Chris@761
|
3330 .arg(m_binDisplay);
|
Chris@761
|
3331
|
Chris@1009
|
3332 // New-style normalization attributes, allowing for more types of
|
Chris@1009
|
3333 // normalization in future: write out the column normalization
|
Chris@1009
|
3334 // type separately, and then whether we are normalizing visible
|
Chris@1009
|
3335 // area as well afterwards
|
Chris@1009
|
3336
|
Chris@1009
|
3337 s += QString("columnNormalization=\"%1\" ")
|
Chris@1063
|
3338 .arg(m_normalization == ColumnOp::NormalizeColumns ? "peak" :
|
Chris@1063
|
3339 m_normalization == ColumnOp::NormalizeHybrid ? "hybrid" : "none");
|
Chris@1009
|
3340
|
Chris@1009
|
3341 // Old-style normalization attribute. We *don't* write out
|
Chris@1009
|
3342 // normalizeHybrid here because the only release that would accept
|
Chris@1009
|
3343 // it (Tony v1.0) has a totally different scale factor for
|
Chris@1009
|
3344 // it. We'll just have to accept that session files from Tony
|
Chris@1009
|
3345 // v2.0+ will look odd in Tony v1.0
|
Chris@1009
|
3346
|
Chris@1009
|
3347 s += QString("normalizeColumns=\"%1\" ")
|
Chris@1063
|
3348 .arg(m_normalization == ColumnOp::NormalizeColumns ? "true" : "false");
|
Chris@1009
|
3349
|
Chris@1009
|
3350 // And this applies to both old- and new-style attributes
|
Chris@1009
|
3351
|
Chris@1009
|
3352 s += QString("normalizeVisibleArea=\"%1\" ")
|
Chris@1063
|
3353 .arg(m_normalization == ColumnOp::NormalizeVisibleArea ? "true" : "false");
|
Chris@1009
|
3354
|
Chris@316
|
3355 Layer::toXml(stream, indent, extraAttributes + " " + s);
|
Chris@6
|
3356 }
|
Chris@6
|
3357
|
Chris@11
|
3358 void
|
Chris@11
|
3359 SpectrogramLayer::setProperties(const QXmlAttributes &attributes)
|
Chris@11
|
3360 {
|
Chris@11
|
3361 bool ok = false;
|
Chris@11
|
3362
|
Chris@11
|
3363 int channel = attributes.value("channel").toInt(&ok);
|
Chris@11
|
3364 if (ok) setChannel(channel);
|
Chris@11
|
3365
|
Chris@805
|
3366 int windowSize = attributes.value("windowSize").toUInt(&ok);
|
Chris@11
|
3367 if (ok) setWindowSize(windowSize);
|
Chris@11
|
3368
|
Chris@805
|
3369 int windowHopLevel = attributes.value("windowHopLevel").toUInt(&ok);
|
Chris@97
|
3370 if (ok) setWindowHopLevel(windowHopLevel);
|
Chris@97
|
3371 else {
|
Chris@805
|
3372 int windowOverlap = attributes.value("windowOverlap").toUInt(&ok);
|
Chris@97
|
3373 // a percentage value
|
Chris@97
|
3374 if (ok) {
|
Chris@97
|
3375 if (windowOverlap == 0) setWindowHopLevel(0);
|
Chris@97
|
3376 else if (windowOverlap == 25) setWindowHopLevel(1);
|
Chris@97
|
3377 else if (windowOverlap == 50) setWindowHopLevel(2);
|
Chris@97
|
3378 else if (windowOverlap == 75) setWindowHopLevel(3);
|
Chris@97
|
3379 else if (windowOverlap == 90) setWindowHopLevel(4);
|
Chris@97
|
3380 }
|
Chris@97
|
3381 }
|
Chris@11
|
3382
|
Chris@11
|
3383 float gain = attributes.value("gain").toFloat(&ok);
|
Chris@11
|
3384 if (ok) setGain(gain);
|
Chris@11
|
3385
|
Chris@37
|
3386 float threshold = attributes.value("threshold").toFloat(&ok);
|
Chris@37
|
3387 if (ok) setThreshold(threshold);
|
Chris@37
|
3388
|
Chris@805
|
3389 int minFrequency = attributes.value("minFrequency").toUInt(&ok);
|
Chris@187
|
3390 if (ok) {
|
Chris@587
|
3391 SVDEBUG << "SpectrogramLayer::setProperties: setting min freq to " << minFrequency << endl;
|
Chris@187
|
3392 setMinFrequency(minFrequency);
|
Chris@187
|
3393 }
|
Chris@37
|
3394
|
Chris@805
|
3395 int maxFrequency = attributes.value("maxFrequency").toUInt(&ok);
|
Chris@187
|
3396 if (ok) {
|
Chris@587
|
3397 SVDEBUG << "SpectrogramLayer::setProperties: setting max freq to " << maxFrequency << endl;
|
Chris@187
|
3398 setMaxFrequency(maxFrequency);
|
Chris@187
|
3399 }
|
Chris@11
|
3400
|
Chris@11
|
3401 ColourScale colourScale = (ColourScale)
|
Chris@11
|
3402 attributes.value("colourScale").toInt(&ok);
|
Chris@11
|
3403 if (ok) setColourScale(colourScale);
|
Chris@11
|
3404
|
Chris@197
|
3405 int colourMap = attributes.value("colourScheme").toInt(&ok);
|
Chris@197
|
3406 if (ok) setColourMap(colourMap);
|
Chris@11
|
3407
|
Chris@37
|
3408 int colourRotation = attributes.value("colourRotation").toInt(&ok);
|
Chris@37
|
3409 if (ok) setColourRotation(colourRotation);
|
Chris@37
|
3410
|
Chris@11
|
3411 FrequencyScale frequencyScale = (FrequencyScale)
|
Chris@11
|
3412 attributes.value("frequencyScale").toInt(&ok);
|
Chris@11
|
3413 if (ok) setFrequencyScale(frequencyScale);
|
Chris@35
|
3414
|
Chris@37
|
3415 BinDisplay binDisplay = (BinDisplay)
|
Chris@37
|
3416 attributes.value("binDisplay").toInt(&ok);
|
Chris@37
|
3417 if (ok) setBinDisplay(binDisplay);
|
Chris@36
|
3418
|
Chris@1009
|
3419 bool haveNewStyleNormalization = false;
|
Chris@1009
|
3420
|
Chris@1009
|
3421 QString columnNormalization = attributes.value("columnNormalization");
|
Chris@1009
|
3422
|
Chris@1009
|
3423 if (columnNormalization != "") {
|
Chris@1009
|
3424
|
Chris@1009
|
3425 haveNewStyleNormalization = true;
|
Chris@1009
|
3426
|
Chris@1009
|
3427 if (columnNormalization == "peak") {
|
Chris@1063
|
3428 setNormalization(ColumnOp::NormalizeColumns);
|
Chris@1009
|
3429 } else if (columnNormalization == "hybrid") {
|
Chris@1063
|
3430 setNormalization(ColumnOp::NormalizeHybrid);
|
Chris@1009
|
3431 } else if (columnNormalization == "none") {
|
Chris@1009
|
3432 // do nothing
|
Chris@1009
|
3433 } else {
|
Chris@1009
|
3434 cerr << "NOTE: Unknown or unsupported columnNormalization attribute \""
|
Chris@1009
|
3435 << columnNormalization << "\"" << endl;
|
Chris@1009
|
3436 }
|
Chris@1009
|
3437 }
|
Chris@1009
|
3438
|
Chris@1009
|
3439 if (!haveNewStyleNormalization) {
|
Chris@1009
|
3440
|
Chris@1009
|
3441 bool normalizeColumns =
|
Chris@1009
|
3442 (attributes.value("normalizeColumns").trimmed() == "true");
|
Chris@1009
|
3443 if (normalizeColumns) {
|
Chris@1063
|
3444 setNormalization(ColumnOp::NormalizeColumns);
|
Chris@1009
|
3445 }
|
Chris@1009
|
3446
|
Chris@1009
|
3447 bool normalizeHybrid =
|
Chris@1009
|
3448 (attributes.value("normalizeHybrid").trimmed() == "true");
|
Chris@1009
|
3449 if (normalizeHybrid) {
|
Chris@1063
|
3450 setNormalization(ColumnOp::NormalizeHybrid);
|
Chris@1009
|
3451 }
|
Chris@862
|
3452 }
|
Chris@153
|
3453
|
Chris@153
|
3454 bool normalizeVisibleArea =
|
Chris@153
|
3455 (attributes.value("normalizeVisibleArea").trimmed() == "true");
|
Chris@862
|
3456 if (normalizeVisibleArea) {
|
Chris@1063
|
3457 setNormalization(ColumnOp::NormalizeVisibleArea);
|
Chris@862
|
3458 }
|
Chris@761
|
3459
|
Chris@1063
|
3460 if (!haveNewStyleNormalization && m_normalization == ColumnOp::NormalizeHybrid) {
|
Chris@1009
|
3461 // Tony v1.0 is (and hopefully will remain!) the only released
|
Chris@1009
|
3462 // SV-a-like to use old-style attributes when saving sessions
|
Chris@1009
|
3463 // that ask for hybrid normalization. It saves them with the
|
Chris@1009
|
3464 // wrong gain factor, so hack in a fix for that here -- this
|
Chris@1009
|
3465 // gives us backward but not forward compatibility.
|
Chris@1087
|
3466 setGain(m_gain / float(getFFTSize() / 2));
|
Chris@862
|
3467 }
|
Chris@11
|
3468 }
|
Chris@11
|
3469
|