Chris@0
|
1 /* -*- c-basic-offset: 4 -*- vi:set ts=8 sts=4 sw=4: */
|
Chris@0
|
2
|
Chris@0
|
3 /*
|
Chris@0
|
4 A waveform viewer and audio annotation editor.
|
Chris@5
|
5 Chris Cannam, Queen Mary University of London, 2005-2006
|
Chris@0
|
6
|
Chris@0
|
7 This is experimental software. Not for distribution.
|
Chris@0
|
8 */
|
Chris@0
|
9
|
Chris@0
|
10 #include "SpectrogramLayer.h"
|
Chris@0
|
11
|
Chris@0
|
12 #include "base/View.h"
|
Chris@0
|
13 #include "base/Profiler.h"
|
Chris@0
|
14 #include "base/AudioLevel.h"
|
Chris@0
|
15 #include "base/Window.h"
|
Chris@24
|
16 #include "base/Pitch.h"
|
Chris@0
|
17
|
Chris@35
|
18 #include "dsp/maths/MathUtilities.h"
|
Chris@35
|
19
|
Chris@0
|
20 #include <QPainter>
|
Chris@0
|
21 #include <QImage>
|
Chris@0
|
22 #include <QPixmap>
|
Chris@0
|
23 #include <QRect>
|
Chris@0
|
24 #include <QTimer>
|
Chris@0
|
25
|
Chris@0
|
26 #include <iostream>
|
Chris@0
|
27
|
Chris@0
|
28 #include <cassert>
|
Chris@0
|
29 #include <cmath>
|
Chris@0
|
30
|
Chris@0
|
31 //#define DEBUG_SPECTROGRAM_REPAINT 1
|
Chris@0
|
32
|
Chris@0
|
33
|
Chris@44
|
34 SpectrogramLayer::SpectrogramLayer(Configuration config) :
|
Chris@44
|
35 Layer(),
|
Chris@0
|
36 m_model(0),
|
Chris@0
|
37 m_channel(0),
|
Chris@0
|
38 m_windowSize(1024),
|
Chris@0
|
39 m_windowType(HanningWindow),
|
Chris@0
|
40 m_windowOverlap(50),
|
Chris@0
|
41 m_gain(1.0),
|
Chris@37
|
42 m_threshold(0.0),
|
Chris@9
|
43 m_colourRotation(0),
|
Chris@37
|
44 m_minFrequency(0),
|
Chris@0
|
45 m_maxFrequency(8000),
|
Chris@0
|
46 m_colourScale(dBColourScale),
|
Chris@0
|
47 m_colourScheme(DefaultColours),
|
Chris@0
|
48 m_frequencyScale(LinearFrequencyScale),
|
Chris@37
|
49 m_binDisplay(AllBins),
|
Chris@36
|
50 m_normalizeColumns(false),
|
Chris@0
|
51 m_cache(0),
|
Chris@0
|
52 m_cacheInvalid(true),
|
Chris@0
|
53 m_pixmapCache(0),
|
Chris@0
|
54 m_pixmapCacheInvalid(true),
|
Chris@0
|
55 m_fillThread(0),
|
Chris@0
|
56 m_updateTimer(0),
|
Chris@44
|
57 m_candidateFillStartFrame(0),
|
Chris@0
|
58 m_lastFillExtent(0),
|
Chris@0
|
59 m_exiting(false)
|
Chris@0
|
60 {
|
Chris@0
|
61 if (config == MelodicRange) {
|
Chris@0
|
62 setWindowSize(8192);
|
Chris@0
|
63 setWindowOverlap(90);
|
Chris@0
|
64 setWindowType(ParzenWindow);
|
Chris@0
|
65 setMaxFrequency(1000);
|
Chris@0
|
66 setColourScale(LinearColourScale);
|
Chris@37
|
67 } else if (config == MelodicPeaks) {
|
Chris@37
|
68 setWindowSize(4096);
|
Chris@37
|
69 setWindowOverlap(90);
|
Chris@37
|
70 setWindowType(BlackmanWindow);
|
Chris@40
|
71 setMaxFrequency(2000);
|
Chris@37
|
72 setMinFrequency(40);
|
Chris@37
|
73 setFrequencyScale(LogFrequencyScale);
|
Chris@41
|
74 setColourScale(MeterColourScale);
|
Chris@37
|
75 setBinDisplay(PeakFrequencies);
|
Chris@37
|
76 setNormalizeColumns(true);
|
Chris@0
|
77 }
|
Chris@0
|
78 }
|
Chris@0
|
79
|
Chris@0
|
80 SpectrogramLayer::~SpectrogramLayer()
|
Chris@0
|
81 {
|
Chris@0
|
82 delete m_updateTimer;
|
Chris@0
|
83 m_updateTimer = 0;
|
Chris@0
|
84
|
Chris@0
|
85 m_exiting = true;
|
Chris@0
|
86 m_condition.wakeAll();
|
Chris@0
|
87 if (m_fillThread) m_fillThread->wait();
|
Chris@0
|
88 delete m_fillThread;
|
Chris@0
|
89
|
Chris@0
|
90 delete m_cache;
|
Chris@0
|
91 }
|
Chris@0
|
92
|
Chris@0
|
93 void
|
Chris@0
|
94 SpectrogramLayer::setModel(const DenseTimeValueModel *model)
|
Chris@0
|
95 {
|
Chris@34
|
96 std::cerr << "SpectrogramLayer(" << this << "): setModel(" << model << ")" << std::endl;
|
Chris@34
|
97
|
Chris@0
|
98 m_mutex.lock();
|
Chris@35
|
99 m_cacheInvalid = true;
|
Chris@0
|
100 m_model = model;
|
Chris@34
|
101 delete m_cache; //!!! hang on, this isn't safe to do here is it?
|
Chris@34
|
102 // we need some sort of guard against the fill
|
Chris@34
|
103 // thread trying to read the defunct model too.
|
Chris@34
|
104 // should we use a scavenger?
|
Chris@31
|
105 m_cache = 0;
|
Chris@0
|
106 m_mutex.unlock();
|
Chris@0
|
107
|
Chris@0
|
108 if (!m_model || !m_model->isOK()) return;
|
Chris@0
|
109
|
Chris@0
|
110 connect(m_model, SIGNAL(modelChanged()), this, SIGNAL(modelChanged()));
|
Chris@0
|
111 connect(m_model, SIGNAL(modelChanged(size_t, size_t)),
|
Chris@0
|
112 this, SIGNAL(modelChanged(size_t, size_t)));
|
Chris@0
|
113
|
Chris@0
|
114 connect(m_model, SIGNAL(completionChanged()),
|
Chris@0
|
115 this, SIGNAL(modelCompletionChanged()));
|
Chris@0
|
116
|
Chris@0
|
117 connect(m_model, SIGNAL(modelChanged()), this, SLOT(cacheInvalid()));
|
Chris@0
|
118 connect(m_model, SIGNAL(modelChanged(size_t, size_t)),
|
Chris@0
|
119 this, SLOT(cacheInvalid(size_t, size_t)));
|
Chris@0
|
120
|
Chris@0
|
121 emit modelReplaced();
|
Chris@0
|
122 fillCache();
|
Chris@0
|
123 }
|
Chris@0
|
124
|
Chris@0
|
125 Layer::PropertyList
|
Chris@0
|
126 SpectrogramLayer::getProperties() const
|
Chris@0
|
127 {
|
Chris@0
|
128 PropertyList list;
|
Chris@0
|
129 list.push_back(tr("Colour"));
|
Chris@0
|
130 list.push_back(tr("Colour Scale"));
|
Chris@0
|
131 list.push_back(tr("Window Type"));
|
Chris@0
|
132 list.push_back(tr("Window Size"));
|
Chris@0
|
133 list.push_back(tr("Window Overlap"));
|
Chris@36
|
134 list.push_back(tr("Normalize"));
|
Chris@37
|
135 list.push_back(tr("Bin Display"));
|
Chris@37
|
136 list.push_back(tr("Threshold"));
|
Chris@0
|
137 list.push_back(tr("Gain"));
|
Chris@9
|
138 list.push_back(tr("Colour Rotation"));
|
Chris@37
|
139 list.push_back(tr("Min Frequency"));
|
Chris@0
|
140 list.push_back(tr("Max Frequency"));
|
Chris@0
|
141 list.push_back(tr("Frequency Scale"));
|
Chris@0
|
142 return list;
|
Chris@0
|
143 }
|
Chris@0
|
144
|
Chris@0
|
145 Layer::PropertyType
|
Chris@0
|
146 SpectrogramLayer::getPropertyType(const PropertyName &name) const
|
Chris@0
|
147 {
|
Chris@0
|
148 if (name == tr("Gain")) return RangeProperty;
|
Chris@9
|
149 if (name == tr("Colour Rotation")) return RangeProperty;
|
Chris@36
|
150 if (name == tr("Normalize")) return ToggleProperty;
|
Chris@37
|
151 if (name == tr("Threshold")) return RangeProperty;
|
Chris@0
|
152 return ValueProperty;
|
Chris@0
|
153 }
|
Chris@0
|
154
|
Chris@0
|
155 QString
|
Chris@0
|
156 SpectrogramLayer::getPropertyGroupName(const PropertyName &name) const
|
Chris@0
|
157 {
|
Chris@0
|
158 if (name == tr("Window Size") ||
|
Chris@35
|
159 name == tr("Window Type") ||
|
Chris@0
|
160 name == tr("Window Overlap")) return tr("Window");
|
Chris@35
|
161 if (name == tr("Colour") ||
|
Chris@38
|
162 name == tr("Gain") ||
|
Chris@38
|
163 name == tr("Threshold") ||
|
Chris@35
|
164 name == tr("Colour Rotation")) return tr("Colour");
|
Chris@38
|
165 if (name == tr("Normalize") ||
|
Chris@37
|
166 name == tr("Bin Display") ||
|
Chris@0
|
167 name == tr("Colour Scale")) return tr("Scale");
|
Chris@0
|
168 if (name == tr("Max Frequency") ||
|
Chris@37
|
169 name == tr("Min Frequency") ||
|
Chris@35
|
170 name == tr("Frequency Scale") ||
|
Chris@37
|
171 name == tr("Frequency Adjustment")) return tr("Range");
|
Chris@0
|
172 return QString();
|
Chris@0
|
173 }
|
Chris@0
|
174
|
Chris@0
|
175 int
|
Chris@0
|
176 SpectrogramLayer::getPropertyRangeAndValue(const PropertyName &name,
|
Chris@0
|
177 int *min, int *max) const
|
Chris@0
|
178 {
|
Chris@0
|
179 int deft = 0;
|
Chris@0
|
180
|
Chris@10
|
181 int throwaway;
|
Chris@10
|
182 if (!min) min = &throwaway;
|
Chris@10
|
183 if (!max) max = &throwaway;
|
Chris@10
|
184
|
Chris@0
|
185 if (name == tr("Gain")) {
|
Chris@0
|
186
|
Chris@0
|
187 *min = -50;
|
Chris@0
|
188 *max = 50;
|
Chris@0
|
189
|
Chris@0
|
190 deft = lrint(log10(m_gain) * 20.0);
|
Chris@0
|
191 if (deft < *min) deft = *min;
|
Chris@0
|
192 if (deft > *max) deft = *max;
|
Chris@0
|
193
|
Chris@37
|
194 } else if (name == tr("Threshold")) {
|
Chris@37
|
195
|
Chris@37
|
196 *min = -50;
|
Chris@37
|
197 *max = 0;
|
Chris@37
|
198
|
Chris@37
|
199 deft = lrintf(AudioLevel::multiplier_to_dB(m_threshold));
|
Chris@37
|
200 if (deft < *min) deft = *min;
|
Chris@37
|
201 if (deft > *max) deft = *max;
|
Chris@37
|
202
|
Chris@9
|
203 } else if (name == tr("Colour Rotation")) {
|
Chris@9
|
204
|
Chris@9
|
205 *min = 0;
|
Chris@9
|
206 *max = 256;
|
Chris@9
|
207
|
Chris@9
|
208 deft = m_colourRotation;
|
Chris@9
|
209
|
Chris@0
|
210 } else if (name == tr("Colour Scale")) {
|
Chris@0
|
211
|
Chris@0
|
212 *min = 0;
|
Chris@0
|
213 *max = 3;
|
Chris@0
|
214
|
Chris@0
|
215 deft = (int)m_colourScale;
|
Chris@0
|
216
|
Chris@0
|
217 } else if (name == tr("Colour")) {
|
Chris@0
|
218
|
Chris@0
|
219 *min = 0;
|
Chris@0
|
220 *max = 5;
|
Chris@0
|
221
|
Chris@0
|
222 deft = (int)m_colourScheme;
|
Chris@0
|
223
|
Chris@0
|
224 } else if (name == tr("Window Type")) {
|
Chris@0
|
225
|
Chris@0
|
226 *min = 0;
|
Chris@0
|
227 *max = 6;
|
Chris@0
|
228
|
Chris@0
|
229 deft = (int)m_windowType;
|
Chris@0
|
230
|
Chris@0
|
231 } else if (name == tr("Window Size")) {
|
Chris@0
|
232
|
Chris@0
|
233 *min = 0;
|
Chris@0
|
234 *max = 10;
|
Chris@0
|
235
|
Chris@0
|
236 deft = 0;
|
Chris@0
|
237 int ws = m_windowSize;
|
Chris@0
|
238 while (ws > 32) { ws >>= 1; deft ++; }
|
Chris@0
|
239
|
Chris@0
|
240 } else if (name == tr("Window Overlap")) {
|
Chris@0
|
241
|
Chris@0
|
242 *min = 0;
|
Chris@0
|
243 *max = 4;
|
Chris@0
|
244
|
Chris@0
|
245 deft = m_windowOverlap / 25;
|
Chris@0
|
246 if (m_windowOverlap == 90) deft = 4;
|
Chris@0
|
247
|
Chris@37
|
248 } else if (name == tr("Min Frequency")) {
|
Chris@37
|
249
|
Chris@37
|
250 *min = 0;
|
Chris@37
|
251 *max = 9;
|
Chris@37
|
252
|
Chris@37
|
253 switch (m_minFrequency) {
|
Chris@37
|
254 case 0: default: deft = 0; break;
|
Chris@37
|
255 case 10: deft = 1; break;
|
Chris@37
|
256 case 20: deft = 2; break;
|
Chris@37
|
257 case 40: deft = 3; break;
|
Chris@37
|
258 case 100: deft = 4; break;
|
Chris@37
|
259 case 250: deft = 5; break;
|
Chris@37
|
260 case 500: deft = 6; break;
|
Chris@37
|
261 case 1000: deft = 7; break;
|
Chris@37
|
262 case 4000: deft = 8; break;
|
Chris@37
|
263 case 10000: deft = 9; break;
|
Chris@37
|
264 }
|
Chris@37
|
265
|
Chris@0
|
266 } else if (name == tr("Max Frequency")) {
|
Chris@0
|
267
|
Chris@0
|
268 *min = 0;
|
Chris@0
|
269 *max = 9;
|
Chris@0
|
270
|
Chris@0
|
271 switch (m_maxFrequency) {
|
Chris@0
|
272 case 500: deft = 0; break;
|
Chris@0
|
273 case 1000: deft = 1; break;
|
Chris@0
|
274 case 1500: deft = 2; break;
|
Chris@0
|
275 case 2000: deft = 3; break;
|
Chris@0
|
276 case 4000: deft = 4; break;
|
Chris@0
|
277 case 6000: deft = 5; break;
|
Chris@0
|
278 case 8000: deft = 6; break;
|
Chris@0
|
279 case 12000: deft = 7; break;
|
Chris@0
|
280 case 16000: deft = 8; break;
|
Chris@0
|
281 default: deft = 9; break;
|
Chris@0
|
282 }
|
Chris@0
|
283
|
Chris@0
|
284 } else if (name == tr("Frequency Scale")) {
|
Chris@0
|
285
|
Chris@0
|
286 *min = 0;
|
Chris@0
|
287 *max = 1;
|
Chris@0
|
288 deft = (int)m_frequencyScale;
|
Chris@0
|
289
|
Chris@37
|
290 } else if (name == tr("Bin Display")) {
|
Chris@35
|
291
|
Chris@35
|
292 *min = 0;
|
Chris@35
|
293 *max = 2;
|
Chris@37
|
294 deft = (int)m_binDisplay;
|
Chris@35
|
295
|
Chris@36
|
296 } else if (name == tr("Normalize")) {
|
Chris@36
|
297
|
Chris@36
|
298 deft = (m_normalizeColumns ? 1 : 0);
|
Chris@36
|
299
|
Chris@0
|
300 } else {
|
Chris@0
|
301 deft = Layer::getPropertyRangeAndValue(name, min, max);
|
Chris@0
|
302 }
|
Chris@0
|
303
|
Chris@0
|
304 return deft;
|
Chris@0
|
305 }
|
Chris@0
|
306
|
Chris@0
|
307 QString
|
Chris@0
|
308 SpectrogramLayer::getPropertyValueLabel(const PropertyName &name,
|
Chris@9
|
309 int value) const
|
Chris@0
|
310 {
|
Chris@0
|
311 if (name == tr("Colour")) {
|
Chris@0
|
312 switch (value) {
|
Chris@0
|
313 default:
|
Chris@0
|
314 case 0: return tr("Default");
|
Chris@0
|
315 case 1: return tr("White on Black");
|
Chris@0
|
316 case 2: return tr("Black on White");
|
Chris@0
|
317 case 3: return tr("Red on Blue");
|
Chris@0
|
318 case 4: return tr("Yellow on Black");
|
Chris@40
|
319 case 5: return tr("Fruit Salad");
|
Chris@0
|
320 }
|
Chris@0
|
321 }
|
Chris@0
|
322 if (name == tr("Colour Scale")) {
|
Chris@0
|
323 switch (value) {
|
Chris@0
|
324 default:
|
Chris@37
|
325 case 0: return tr("Linear");
|
Chris@37
|
326 case 1: return tr("Meter");
|
Chris@37
|
327 case 2: return tr("dB");
|
Chris@0
|
328 case 3: return tr("Phase");
|
Chris@0
|
329 }
|
Chris@0
|
330 }
|
Chris@0
|
331 if (name == tr("Window Type")) {
|
Chris@0
|
332 switch ((WindowType)value) {
|
Chris@0
|
333 default:
|
Chris@35
|
334 case RectangularWindow: return tr("Rectangle");
|
Chris@0
|
335 case BartlettWindow: return tr("Bartlett");
|
Chris@0
|
336 case HammingWindow: return tr("Hamming");
|
Chris@0
|
337 case HanningWindow: return tr("Hanning");
|
Chris@0
|
338 case BlackmanWindow: return tr("Blackman");
|
Chris@0
|
339 case GaussianWindow: return tr("Gaussian");
|
Chris@0
|
340 case ParzenWindow: return tr("Parzen");
|
Chris@0
|
341 }
|
Chris@0
|
342 }
|
Chris@0
|
343 if (name == tr("Window Size")) {
|
Chris@0
|
344 return QString("%1").arg(32 << value);
|
Chris@0
|
345 }
|
Chris@0
|
346 if (name == tr("Window Overlap")) {
|
Chris@0
|
347 switch (value) {
|
Chris@0
|
348 default:
|
Chris@35
|
349 case 0: return tr("0%");
|
Chris@35
|
350 case 1: return tr("25%");
|
Chris@35
|
351 case 2: return tr("50%");
|
Chris@35
|
352 case 3: return tr("75%");
|
Chris@35
|
353 case 4: return tr("90%");
|
Chris@0
|
354 }
|
Chris@0
|
355 }
|
Chris@37
|
356 if (name == tr("Min Frequency")) {
|
Chris@37
|
357 switch (value) {
|
Chris@37
|
358 default:
|
Chris@38
|
359 case 0: return tr("No min");
|
Chris@37
|
360 case 1: return tr("10 Hz");
|
Chris@37
|
361 case 2: return tr("20 Hz");
|
Chris@37
|
362 case 3: return tr("40 Hz");
|
Chris@37
|
363 case 4: return tr("100 Hz");
|
Chris@37
|
364 case 5: return tr("250 Hz");
|
Chris@37
|
365 case 6: return tr("500 Hz");
|
Chris@37
|
366 case 7: return tr("1 KHz");
|
Chris@37
|
367 case 8: return tr("4 KHz");
|
Chris@37
|
368 case 9: return tr("10 KHz");
|
Chris@37
|
369 }
|
Chris@37
|
370 }
|
Chris@0
|
371 if (name == tr("Max Frequency")) {
|
Chris@0
|
372 switch (value) {
|
Chris@0
|
373 default:
|
Chris@0
|
374 case 0: return tr("500 Hz");
|
Chris@0
|
375 case 1: return tr("1 KHz");
|
Chris@0
|
376 case 2: return tr("1.5 KHz");
|
Chris@0
|
377 case 3: return tr("2 KHz");
|
Chris@0
|
378 case 4: return tr("4 KHz");
|
Chris@0
|
379 case 5: return tr("6 KHz");
|
Chris@0
|
380 case 6: return tr("8 KHz");
|
Chris@0
|
381 case 7: return tr("12 KHz");
|
Chris@0
|
382 case 8: return tr("16 KHz");
|
Chris@38
|
383 case 9: return tr("No max");
|
Chris@0
|
384 }
|
Chris@0
|
385 }
|
Chris@0
|
386 if (name == tr("Frequency Scale")) {
|
Chris@0
|
387 switch (value) {
|
Chris@0
|
388 default:
|
Chris@0
|
389 case 0: return tr("Linear");
|
Chris@0
|
390 case 1: return tr("Log");
|
Chris@0
|
391 }
|
Chris@0
|
392 }
|
Chris@37
|
393 if (name == tr("Bin Display")) {
|
Chris@35
|
394 switch (value) {
|
Chris@35
|
395 default:
|
Chris@37
|
396 case 0: return tr("All Bins");
|
Chris@37
|
397 case 1: return tr("Peak Bins");
|
Chris@37
|
398 case 2: return tr("Frequencies");
|
Chris@35
|
399 }
|
Chris@35
|
400 }
|
Chris@0
|
401 return tr("<unknown>");
|
Chris@0
|
402 }
|
Chris@0
|
403
|
Chris@0
|
404 void
|
Chris@0
|
405 SpectrogramLayer::setProperty(const PropertyName &name, int value)
|
Chris@0
|
406 {
|
Chris@0
|
407 if (name == tr("Gain")) {
|
Chris@0
|
408 setGain(pow(10, float(value)/20.0));
|
Chris@37
|
409 } else if (name == tr("Threshold")) {
|
Chris@37
|
410 if (value == -50) setThreshold(0.0);
|
Chris@37
|
411 else setThreshold(AudioLevel::dB_to_multiplier(value));
|
Chris@9
|
412 } else if (name == tr("Colour Rotation")) {
|
Chris@9
|
413 setColourRotation(value);
|
Chris@0
|
414 } else if (name == tr("Colour")) {
|
Chris@44
|
415 //!!! if (v) v->setLightBackground(value == 2);
|
Chris@0
|
416 switch (value) {
|
Chris@0
|
417 default:
|
Chris@0
|
418 case 0: setColourScheme(DefaultColours); break;
|
Chris@0
|
419 case 1: setColourScheme(WhiteOnBlack); break;
|
Chris@0
|
420 case 2: setColourScheme(BlackOnWhite); break;
|
Chris@0
|
421 case 3: setColourScheme(RedOnBlue); break;
|
Chris@0
|
422 case 4: setColourScheme(YellowOnBlack); break;
|
Chris@40
|
423 case 5: setColourScheme(Rainbow); break;
|
Chris@0
|
424 }
|
Chris@0
|
425 } else if (name == tr("Window Type")) {
|
Chris@0
|
426 setWindowType(WindowType(value));
|
Chris@0
|
427 } else if (name == tr("Window Size")) {
|
Chris@0
|
428 setWindowSize(32 << value);
|
Chris@0
|
429 } else if (name == tr("Window Overlap")) {
|
Chris@0
|
430 if (value == 4) setWindowOverlap(90);
|
Chris@0
|
431 else setWindowOverlap(25 * value);
|
Chris@37
|
432 } else if (name == tr("Min Frequency")) {
|
Chris@37
|
433 switch (value) {
|
Chris@37
|
434 default:
|
Chris@37
|
435 case 0: setMinFrequency(0); break;
|
Chris@37
|
436 case 1: setMinFrequency(10); break;
|
Chris@37
|
437 case 2: setMinFrequency(20); break;
|
Chris@37
|
438 case 3: setMinFrequency(40); break;
|
Chris@37
|
439 case 4: setMinFrequency(100); break;
|
Chris@37
|
440 case 5: setMinFrequency(250); break;
|
Chris@37
|
441 case 6: setMinFrequency(500); break;
|
Chris@37
|
442 case 7: setMinFrequency(1000); break;
|
Chris@37
|
443 case 8: setMinFrequency(4000); break;
|
Chris@37
|
444 case 9: setMinFrequency(10000); break;
|
Chris@37
|
445 }
|
Chris@0
|
446 } else if (name == tr("Max Frequency")) {
|
Chris@0
|
447 switch (value) {
|
Chris@0
|
448 case 0: setMaxFrequency(500); break;
|
Chris@0
|
449 case 1: setMaxFrequency(1000); break;
|
Chris@0
|
450 case 2: setMaxFrequency(1500); break;
|
Chris@0
|
451 case 3: setMaxFrequency(2000); break;
|
Chris@0
|
452 case 4: setMaxFrequency(4000); break;
|
Chris@0
|
453 case 5: setMaxFrequency(6000); break;
|
Chris@0
|
454 case 6: setMaxFrequency(8000); break;
|
Chris@0
|
455 case 7: setMaxFrequency(12000); break;
|
Chris@0
|
456 case 8: setMaxFrequency(16000); break;
|
Chris@0
|
457 default:
|
Chris@0
|
458 case 9: setMaxFrequency(0); break;
|
Chris@0
|
459 }
|
Chris@0
|
460 } else if (name == tr("Colour Scale")) {
|
Chris@0
|
461 switch (value) {
|
Chris@0
|
462 default:
|
Chris@0
|
463 case 0: setColourScale(LinearColourScale); break;
|
Chris@0
|
464 case 1: setColourScale(MeterColourScale); break;
|
Chris@0
|
465 case 2: setColourScale(dBColourScale); break;
|
Chris@0
|
466 case 3: setColourScale(PhaseColourScale); break;
|
Chris@0
|
467 }
|
Chris@0
|
468 } else if (name == tr("Frequency Scale")) {
|
Chris@0
|
469 switch (value) {
|
Chris@0
|
470 default:
|
Chris@0
|
471 case 0: setFrequencyScale(LinearFrequencyScale); break;
|
Chris@0
|
472 case 1: setFrequencyScale(LogFrequencyScale); break;
|
Chris@0
|
473 }
|
Chris@37
|
474 } else if (name == tr("Bin Display")) {
|
Chris@35
|
475 switch (value) {
|
Chris@35
|
476 default:
|
Chris@37
|
477 case 0: setBinDisplay(AllBins); break;
|
Chris@37
|
478 case 1: setBinDisplay(PeakBins); break;
|
Chris@37
|
479 case 2: setBinDisplay(PeakFrequencies); break;
|
Chris@35
|
480 }
|
Chris@36
|
481 } else if (name == "Normalize") {
|
Chris@36
|
482 setNormalizeColumns(value ? true : false);
|
Chris@0
|
483 }
|
Chris@0
|
484 }
|
Chris@0
|
485
|
Chris@0
|
486 void
|
Chris@0
|
487 SpectrogramLayer::setChannel(int ch)
|
Chris@0
|
488 {
|
Chris@0
|
489 if (m_channel == ch) return;
|
Chris@0
|
490
|
Chris@0
|
491 m_mutex.lock();
|
Chris@0
|
492 m_cacheInvalid = true;
|
Chris@0
|
493 m_pixmapCacheInvalid = true;
|
Chris@0
|
494
|
Chris@0
|
495 m_channel = ch;
|
Chris@9
|
496
|
Chris@9
|
497 m_mutex.unlock();
|
Chris@9
|
498
|
Chris@0
|
499 emit layerParametersChanged();
|
Chris@9
|
500
|
Chris@0
|
501 fillCache();
|
Chris@0
|
502 }
|
Chris@0
|
503
|
Chris@0
|
504 int
|
Chris@0
|
505 SpectrogramLayer::getChannel() const
|
Chris@0
|
506 {
|
Chris@0
|
507 return m_channel;
|
Chris@0
|
508 }
|
Chris@0
|
509
|
Chris@0
|
510 void
|
Chris@0
|
511 SpectrogramLayer::setWindowSize(size_t ws)
|
Chris@0
|
512 {
|
Chris@0
|
513 if (m_windowSize == ws) return;
|
Chris@0
|
514
|
Chris@0
|
515 m_mutex.lock();
|
Chris@0
|
516 m_cacheInvalid = true;
|
Chris@0
|
517 m_pixmapCacheInvalid = true;
|
Chris@0
|
518
|
Chris@0
|
519 m_windowSize = ws;
|
Chris@0
|
520
|
Chris@0
|
521 m_mutex.unlock();
|
Chris@9
|
522
|
Chris@9
|
523 emit layerParametersChanged();
|
Chris@9
|
524
|
Chris@0
|
525 fillCache();
|
Chris@0
|
526 }
|
Chris@0
|
527
|
Chris@0
|
528 size_t
|
Chris@0
|
529 SpectrogramLayer::getWindowSize() const
|
Chris@0
|
530 {
|
Chris@0
|
531 return m_windowSize;
|
Chris@0
|
532 }
|
Chris@0
|
533
|
Chris@0
|
534 void
|
Chris@0
|
535 SpectrogramLayer::setWindowOverlap(size_t wi)
|
Chris@0
|
536 {
|
Chris@0
|
537 if (m_windowOverlap == wi) return;
|
Chris@0
|
538
|
Chris@0
|
539 m_mutex.lock();
|
Chris@0
|
540 m_cacheInvalid = true;
|
Chris@0
|
541 m_pixmapCacheInvalid = true;
|
Chris@0
|
542
|
Chris@0
|
543 m_windowOverlap = wi;
|
Chris@0
|
544
|
Chris@0
|
545 m_mutex.unlock();
|
Chris@9
|
546
|
Chris@9
|
547 emit layerParametersChanged();
|
Chris@9
|
548
|
Chris@0
|
549 fillCache();
|
Chris@0
|
550 }
|
Chris@0
|
551
|
Chris@0
|
552 size_t
|
Chris@0
|
553 SpectrogramLayer::getWindowOverlap() const
|
Chris@0
|
554 {
|
Chris@0
|
555 return m_windowOverlap;
|
Chris@0
|
556 }
|
Chris@0
|
557
|
Chris@0
|
558 void
|
Chris@0
|
559 SpectrogramLayer::setWindowType(WindowType w)
|
Chris@0
|
560 {
|
Chris@0
|
561 if (m_windowType == w) return;
|
Chris@0
|
562
|
Chris@0
|
563 m_mutex.lock();
|
Chris@0
|
564 m_cacheInvalid = true;
|
Chris@0
|
565 m_pixmapCacheInvalid = true;
|
Chris@0
|
566
|
Chris@0
|
567 m_windowType = w;
|
Chris@0
|
568
|
Chris@0
|
569 m_mutex.unlock();
|
Chris@9
|
570
|
Chris@9
|
571 emit layerParametersChanged();
|
Chris@9
|
572
|
Chris@0
|
573 fillCache();
|
Chris@0
|
574 }
|
Chris@0
|
575
|
Chris@0
|
576 WindowType
|
Chris@0
|
577 SpectrogramLayer::getWindowType() const
|
Chris@0
|
578 {
|
Chris@0
|
579 return m_windowType;
|
Chris@0
|
580 }
|
Chris@0
|
581
|
Chris@0
|
582 void
|
Chris@0
|
583 SpectrogramLayer::setGain(float gain)
|
Chris@0
|
584 {
|
Chris@40
|
585 if (m_gain == gain) return;
|
Chris@0
|
586
|
Chris@0
|
587 m_mutex.lock();
|
Chris@0
|
588 m_pixmapCacheInvalid = true;
|
Chris@0
|
589
|
Chris@0
|
590 m_gain = gain;
|
Chris@0
|
591
|
Chris@0
|
592 m_mutex.unlock();
|
Chris@9
|
593
|
Chris@9
|
594 emit layerParametersChanged();
|
Chris@9
|
595
|
Chris@0
|
596 fillCache();
|
Chris@0
|
597 }
|
Chris@0
|
598
|
Chris@0
|
599 float
|
Chris@0
|
600 SpectrogramLayer::getGain() const
|
Chris@0
|
601 {
|
Chris@0
|
602 return m_gain;
|
Chris@0
|
603 }
|
Chris@0
|
604
|
Chris@0
|
605 void
|
Chris@37
|
606 SpectrogramLayer::setThreshold(float threshold)
|
Chris@37
|
607 {
|
Chris@40
|
608 if (m_threshold == threshold) return;
|
Chris@37
|
609
|
Chris@37
|
610 m_mutex.lock();
|
Chris@37
|
611 m_pixmapCacheInvalid = true;
|
Chris@37
|
612
|
Chris@37
|
613 m_threshold = threshold;
|
Chris@37
|
614
|
Chris@37
|
615 m_mutex.unlock();
|
Chris@37
|
616
|
Chris@37
|
617 emit layerParametersChanged();
|
Chris@37
|
618
|
Chris@37
|
619 fillCache();
|
Chris@37
|
620 }
|
Chris@37
|
621
|
Chris@37
|
622 float
|
Chris@37
|
623 SpectrogramLayer::getThreshold() const
|
Chris@37
|
624 {
|
Chris@37
|
625 return m_threshold;
|
Chris@37
|
626 }
|
Chris@37
|
627
|
Chris@37
|
628 void
|
Chris@37
|
629 SpectrogramLayer::setMinFrequency(size_t mf)
|
Chris@37
|
630 {
|
Chris@37
|
631 if (m_minFrequency == mf) return;
|
Chris@37
|
632
|
Chris@37
|
633 m_mutex.lock();
|
Chris@37
|
634 m_pixmapCacheInvalid = true;
|
Chris@37
|
635
|
Chris@37
|
636 m_minFrequency = mf;
|
Chris@37
|
637
|
Chris@37
|
638 m_mutex.unlock();
|
Chris@37
|
639
|
Chris@37
|
640 emit layerParametersChanged();
|
Chris@37
|
641 }
|
Chris@37
|
642
|
Chris@37
|
643 size_t
|
Chris@37
|
644 SpectrogramLayer::getMinFrequency() const
|
Chris@37
|
645 {
|
Chris@37
|
646 return m_minFrequency;
|
Chris@37
|
647 }
|
Chris@37
|
648
|
Chris@37
|
649 void
|
Chris@0
|
650 SpectrogramLayer::setMaxFrequency(size_t mf)
|
Chris@0
|
651 {
|
Chris@0
|
652 if (m_maxFrequency == mf) return;
|
Chris@0
|
653
|
Chris@0
|
654 m_mutex.lock();
|
Chris@0
|
655 m_pixmapCacheInvalid = true;
|
Chris@0
|
656
|
Chris@0
|
657 m_maxFrequency = mf;
|
Chris@0
|
658
|
Chris@0
|
659 m_mutex.unlock();
|
Chris@9
|
660
|
Chris@9
|
661 emit layerParametersChanged();
|
Chris@0
|
662 }
|
Chris@0
|
663
|
Chris@0
|
664 size_t
|
Chris@0
|
665 SpectrogramLayer::getMaxFrequency() const
|
Chris@0
|
666 {
|
Chris@0
|
667 return m_maxFrequency;
|
Chris@0
|
668 }
|
Chris@0
|
669
|
Chris@0
|
670 void
|
Chris@9
|
671 SpectrogramLayer::setColourRotation(int r)
|
Chris@9
|
672 {
|
Chris@9
|
673 m_mutex.lock();
|
Chris@9
|
674 m_pixmapCacheInvalid = true;
|
Chris@9
|
675
|
Chris@9
|
676 if (r < 0) r = 0;
|
Chris@9
|
677 if (r > 256) r = 256;
|
Chris@9
|
678 int distance = r - m_colourRotation;
|
Chris@9
|
679
|
Chris@9
|
680 if (distance != 0) {
|
Chris@9
|
681 rotateCacheColourmap(-distance);
|
Chris@9
|
682 m_colourRotation = r;
|
Chris@9
|
683 }
|
Chris@9
|
684
|
Chris@9
|
685 m_mutex.unlock();
|
Chris@9
|
686
|
Chris@9
|
687 emit layerParametersChanged();
|
Chris@9
|
688 }
|
Chris@9
|
689
|
Chris@9
|
690 void
|
Chris@0
|
691 SpectrogramLayer::setColourScale(ColourScale colourScale)
|
Chris@0
|
692 {
|
Chris@0
|
693 if (m_colourScale == colourScale) return;
|
Chris@0
|
694
|
Chris@0
|
695 m_mutex.lock();
|
Chris@0
|
696 m_pixmapCacheInvalid = true;
|
Chris@0
|
697
|
Chris@0
|
698 m_colourScale = colourScale;
|
Chris@0
|
699
|
Chris@0
|
700 m_mutex.unlock();
|
Chris@0
|
701 fillCache();
|
Chris@9
|
702
|
Chris@9
|
703 emit layerParametersChanged();
|
Chris@0
|
704 }
|
Chris@0
|
705
|
Chris@0
|
706 SpectrogramLayer::ColourScale
|
Chris@0
|
707 SpectrogramLayer::getColourScale() const
|
Chris@0
|
708 {
|
Chris@0
|
709 return m_colourScale;
|
Chris@0
|
710 }
|
Chris@0
|
711
|
Chris@0
|
712 void
|
Chris@0
|
713 SpectrogramLayer::setColourScheme(ColourScheme scheme)
|
Chris@0
|
714 {
|
Chris@0
|
715 if (m_colourScheme == scheme) return;
|
Chris@0
|
716
|
Chris@0
|
717 m_mutex.lock();
|
Chris@0
|
718 m_pixmapCacheInvalid = true;
|
Chris@0
|
719
|
Chris@0
|
720 m_colourScheme = scheme;
|
Chris@0
|
721 setCacheColourmap();
|
Chris@9
|
722
|
Chris@9
|
723 m_mutex.unlock();
|
Chris@9
|
724
|
Chris@0
|
725 emit layerParametersChanged();
|
Chris@0
|
726 }
|
Chris@0
|
727
|
Chris@0
|
728 SpectrogramLayer::ColourScheme
|
Chris@0
|
729 SpectrogramLayer::getColourScheme() const
|
Chris@0
|
730 {
|
Chris@0
|
731 return m_colourScheme;
|
Chris@0
|
732 }
|
Chris@0
|
733
|
Chris@0
|
734 void
|
Chris@0
|
735 SpectrogramLayer::setFrequencyScale(FrequencyScale frequencyScale)
|
Chris@0
|
736 {
|
Chris@0
|
737 if (m_frequencyScale == frequencyScale) return;
|
Chris@0
|
738
|
Chris@0
|
739 m_mutex.lock();
|
Chris@35
|
740
|
Chris@0
|
741 m_pixmapCacheInvalid = true;
|
Chris@0
|
742
|
Chris@0
|
743 m_frequencyScale = frequencyScale;
|
Chris@0
|
744
|
Chris@0
|
745 m_mutex.unlock();
|
Chris@9
|
746
|
Chris@9
|
747 emit layerParametersChanged();
|
Chris@0
|
748 }
|
Chris@0
|
749
|
Chris@0
|
750 SpectrogramLayer::FrequencyScale
|
Chris@0
|
751 SpectrogramLayer::getFrequencyScale() const
|
Chris@0
|
752 {
|
Chris@0
|
753 return m_frequencyScale;
|
Chris@0
|
754 }
|
Chris@0
|
755
|
Chris@0
|
756 void
|
Chris@37
|
757 SpectrogramLayer::setBinDisplay(BinDisplay binDisplay)
|
Chris@35
|
758 {
|
Chris@37
|
759 if (m_binDisplay == binDisplay) return;
|
Chris@35
|
760
|
Chris@35
|
761 m_mutex.lock();
|
Chris@35
|
762
|
Chris@35
|
763 m_pixmapCacheInvalid = true;
|
Chris@35
|
764
|
Chris@37
|
765 m_binDisplay = binDisplay;
|
Chris@35
|
766
|
Chris@35
|
767 m_mutex.unlock();
|
Chris@35
|
768
|
Chris@35
|
769 fillCache();
|
Chris@35
|
770
|
Chris@35
|
771 emit layerParametersChanged();
|
Chris@35
|
772 }
|
Chris@35
|
773
|
Chris@37
|
774 SpectrogramLayer::BinDisplay
|
Chris@37
|
775 SpectrogramLayer::getBinDisplay() const
|
Chris@35
|
776 {
|
Chris@37
|
777 return m_binDisplay;
|
Chris@35
|
778 }
|
Chris@35
|
779
|
Chris@35
|
780 void
|
Chris@36
|
781 SpectrogramLayer::setNormalizeColumns(bool n)
|
Chris@36
|
782 {
|
Chris@36
|
783 if (m_normalizeColumns == n) return;
|
Chris@36
|
784 m_mutex.lock();
|
Chris@36
|
785
|
Chris@36
|
786 m_pixmapCacheInvalid = true;
|
Chris@36
|
787 m_normalizeColumns = n;
|
Chris@36
|
788 m_mutex.unlock();
|
Chris@36
|
789
|
Chris@36
|
790 fillCache();
|
Chris@36
|
791 emit layerParametersChanged();
|
Chris@36
|
792 }
|
Chris@36
|
793
|
Chris@36
|
794 bool
|
Chris@36
|
795 SpectrogramLayer::getNormalizeColumns() const
|
Chris@36
|
796 {
|
Chris@36
|
797 return m_normalizeColumns;
|
Chris@36
|
798 }
|
Chris@36
|
799
|
Chris@36
|
800 void
|
Chris@44
|
801 SpectrogramLayer::setLayerDormant(const bool dormant)
|
Chris@29
|
802 {
|
Chris@33
|
803 if (dormant == m_dormant) return;
|
Chris@33
|
804
|
Chris@33
|
805 if (dormant) {
|
Chris@33
|
806
|
Chris@33
|
807 m_mutex.lock();
|
Chris@33
|
808 m_dormant = true;
|
Chris@33
|
809
|
Chris@34
|
810 // delete m_cache;
|
Chris@34
|
811 // m_cache = 0;
|
Chris@33
|
812
|
Chris@34
|
813 m_cacheInvalid = true;
|
Chris@33
|
814 m_pixmapCacheInvalid = true;
|
Chris@33
|
815 delete m_pixmapCache;
|
Chris@33
|
816 m_pixmapCache = 0;
|
Chris@33
|
817
|
Chris@33
|
818 m_mutex.unlock();
|
Chris@33
|
819
|
Chris@33
|
820 } else {
|
Chris@33
|
821
|
Chris@33
|
822 m_dormant = false;
|
Chris@33
|
823 fillCache();
|
Chris@33
|
824 }
|
Chris@29
|
825 }
|
Chris@29
|
826
|
Chris@29
|
827 void
|
Chris@0
|
828 SpectrogramLayer::cacheInvalid()
|
Chris@0
|
829 {
|
Chris@0
|
830 m_cacheInvalid = true;
|
Chris@0
|
831 m_pixmapCacheInvalid = true;
|
Chris@0
|
832 fillCache();
|
Chris@0
|
833 }
|
Chris@0
|
834
|
Chris@0
|
835 void
|
Chris@0
|
836 SpectrogramLayer::cacheInvalid(size_t, size_t)
|
Chris@0
|
837 {
|
Chris@0
|
838 // for now (or forever?)
|
Chris@0
|
839 cacheInvalid();
|
Chris@0
|
840 }
|
Chris@0
|
841
|
Chris@0
|
842 void
|
Chris@0
|
843 SpectrogramLayer::fillCache()
|
Chris@0
|
844 {
|
Chris@0
|
845 #ifdef DEBUG_SPECTROGRAM_REPAINT
|
Chris@0
|
846 std::cerr << "SpectrogramLayer::fillCache" << std::endl;
|
Chris@0
|
847 #endif
|
Chris@0
|
848 QMutexLocker locker(&m_mutex);
|
Chris@0
|
849
|
Chris@0
|
850 m_lastFillExtent = 0;
|
Chris@0
|
851
|
Chris@0
|
852 delete m_updateTimer;
|
Chris@0
|
853 m_updateTimer = new QTimer(this);
|
Chris@0
|
854 connect(m_updateTimer, SIGNAL(timeout()), this, SLOT(fillTimerTimedOut()));
|
Chris@0
|
855 m_updateTimer->start(200);
|
Chris@0
|
856
|
Chris@0
|
857 if (!m_fillThread) {
|
Chris@0
|
858 std::cerr << "SpectrogramLayer::fillCache creating thread" << std::endl;
|
Chris@0
|
859 m_fillThread = new CacheFillThread(*this);
|
Chris@0
|
860 m_fillThread->start();
|
Chris@0
|
861 }
|
Chris@0
|
862
|
Chris@0
|
863 m_condition.wakeAll();
|
Chris@0
|
864 }
|
Chris@0
|
865
|
Chris@0
|
866 void
|
Chris@0
|
867 SpectrogramLayer::fillTimerTimedOut()
|
Chris@0
|
868 {
|
Chris@0
|
869 if (m_fillThread && m_model) {
|
Chris@0
|
870 size_t fillExtent = m_fillThread->getFillExtent();
|
Chris@0
|
871 #ifdef DEBUG_SPECTROGRAM_REPAINT
|
Chris@0
|
872 std::cerr << "SpectrogramLayer::fillTimerTimedOut: extent " << fillExtent << ", last " << m_lastFillExtent << ", total " << m_model->getEndFrame() << std::endl;
|
Chris@0
|
873 #endif
|
Chris@0
|
874 if (fillExtent >= m_lastFillExtent) {
|
Chris@0
|
875 if (fillExtent >= m_model->getEndFrame() && m_lastFillExtent > 0) {
|
Chris@0
|
876 #ifdef DEBUG_SPECTROGRAM_REPAINT
|
Chris@0
|
877 std::cerr << "complete!" << std::endl;
|
Chris@0
|
878 #endif
|
Chris@0
|
879 emit modelChanged();
|
Chris@0
|
880 m_pixmapCacheInvalid = true;
|
Chris@0
|
881 delete m_updateTimer;
|
Chris@0
|
882 m_updateTimer = 0;
|
Chris@0
|
883 m_lastFillExtent = 0;
|
Chris@0
|
884 } else if (fillExtent > m_lastFillExtent) {
|
Chris@0
|
885 #ifdef DEBUG_SPECTROGRAM_REPAINT
|
Chris@0
|
886 std::cerr << "SpectrogramLayer: emitting modelChanged("
|
Chris@0
|
887 << m_lastFillExtent << "," << fillExtent << ")" << std::endl;
|
Chris@0
|
888 #endif
|
Chris@0
|
889 emit modelChanged(m_lastFillExtent, fillExtent);
|
Chris@0
|
890 m_pixmapCacheInvalid = true;
|
Chris@0
|
891 m_lastFillExtent = fillExtent;
|
Chris@0
|
892 }
|
Chris@0
|
893 } else {
|
Chris@44
|
894 // if (v) {
|
Chris@0
|
895 size_t sf = 0;
|
Chris@44
|
896 //!!! if (v->getStartFrame() > 0) sf = v->getStartFrame();
|
Chris@0
|
897 #ifdef DEBUG_SPECTROGRAM_REPAINT
|
Chris@0
|
898 std::cerr << "SpectrogramLayer: going backwards, emitting modelChanged("
|
Chris@44
|
899 << sf << "," << m_model->getEndFrame() << ")" << std::endl;
|
Chris@0
|
900 #endif
|
Chris@44
|
901 emit modelChanged(sf, m_model->getEndFrame());
|
Chris@0
|
902 m_pixmapCacheInvalid = true;
|
Chris@44
|
903 // }
|
Chris@0
|
904 m_lastFillExtent = fillExtent;
|
Chris@0
|
905 }
|
Chris@0
|
906 }
|
Chris@0
|
907 }
|
Chris@0
|
908
|
Chris@0
|
909 void
|
Chris@0
|
910 SpectrogramLayer::setCacheColourmap()
|
Chris@0
|
911 {
|
Chris@0
|
912 if (m_cacheInvalid || !m_cache) return;
|
Chris@0
|
913
|
Chris@10
|
914 int formerRotation = m_colourRotation;
|
Chris@10
|
915
|
Chris@38
|
916 if (m_colourScheme == BlackOnWhite) {
|
Chris@38
|
917 m_cache->setColour(NO_VALUE, Qt::white);
|
Chris@38
|
918 } else {
|
Chris@38
|
919 m_cache->setColour(NO_VALUE, Qt::black);
|
Chris@38
|
920 }
|
Chris@0
|
921
|
Chris@0
|
922 for (int pixel = 1; pixel < 256; ++pixel) {
|
Chris@0
|
923
|
Chris@0
|
924 QColor colour;
|
Chris@0
|
925 int hue, px;
|
Chris@0
|
926
|
Chris@0
|
927 switch (m_colourScheme) {
|
Chris@0
|
928
|
Chris@0
|
929 default:
|
Chris@0
|
930 case DefaultColours:
|
Chris@0
|
931 hue = 256 - pixel;
|
Chris@0
|
932 colour = QColor::fromHsv(hue, pixel/2 + 128, pixel);
|
Chris@0
|
933 break;
|
Chris@0
|
934
|
Chris@0
|
935 case WhiteOnBlack:
|
Chris@0
|
936 colour = QColor(pixel, pixel, pixel);
|
Chris@0
|
937 break;
|
Chris@0
|
938
|
Chris@0
|
939 case BlackOnWhite:
|
Chris@0
|
940 colour = QColor(256-pixel, 256-pixel, 256-pixel);
|
Chris@0
|
941 break;
|
Chris@0
|
942
|
Chris@0
|
943 case RedOnBlue:
|
Chris@0
|
944 colour = QColor(pixel > 128 ? (pixel - 128) * 2 : 0, 0,
|
Chris@0
|
945 pixel < 128 ? pixel : (256 - pixel));
|
Chris@0
|
946 break;
|
Chris@0
|
947
|
Chris@0
|
948 case YellowOnBlack:
|
Chris@0
|
949 px = 256 - pixel;
|
Chris@0
|
950 colour = QColor(px < 64 ? 255 - px/2 :
|
Chris@0
|
951 px < 128 ? 224 - (px - 64) :
|
Chris@0
|
952 px < 192 ? 160 - (px - 128) * 3 / 2 :
|
Chris@0
|
953 256 - px,
|
Chris@0
|
954 pixel,
|
Chris@0
|
955 pixel / 4);
|
Chris@0
|
956 break;
|
Chris@0
|
957
|
Chris@40
|
958 case Rainbow:
|
Chris@40
|
959 hue = 250 - pixel;
|
Chris@40
|
960 if (hue < 0) hue += 256;
|
Chris@40
|
961 colour = QColor::fromHsv(pixel, 255, 255);
|
Chris@0
|
962 break;
|
Chris@0
|
963 }
|
Chris@0
|
964
|
Chris@31
|
965 m_cache->setColour(pixel, colour);
|
Chris@0
|
966 }
|
Chris@9
|
967
|
Chris@9
|
968 m_colourRotation = 0;
|
Chris@10
|
969 rotateCacheColourmap(m_colourRotation - formerRotation);
|
Chris@10
|
970 m_colourRotation = formerRotation;
|
Chris@9
|
971 }
|
Chris@9
|
972
|
Chris@9
|
973 void
|
Chris@9
|
974 SpectrogramLayer::rotateCacheColourmap(int distance)
|
Chris@9
|
975 {
|
Chris@10
|
976 if (!m_cache) return;
|
Chris@10
|
977
|
Chris@31
|
978 QColor newPixels[256];
|
Chris@9
|
979
|
Chris@37
|
980 newPixels[NO_VALUE] = m_cache->getColour(NO_VALUE);
|
Chris@9
|
981
|
Chris@9
|
982 for (int pixel = 1; pixel < 256; ++pixel) {
|
Chris@9
|
983 int target = pixel + distance;
|
Chris@9
|
984 while (target < 1) target += 255;
|
Chris@9
|
985 while (target > 255) target -= 255;
|
Chris@31
|
986 newPixels[target] = m_cache->getColour(pixel);
|
Chris@9
|
987 }
|
Chris@9
|
988
|
Chris@9
|
989 for (int pixel = 0; pixel < 256; ++pixel) {
|
Chris@31
|
990 m_cache->setColour(pixel, newPixels[pixel]);
|
Chris@9
|
991 }
|
Chris@0
|
992 }
|
Chris@0
|
993
|
Chris@38
|
994 float
|
Chris@38
|
995 SpectrogramLayer::calculateFrequency(size_t bin,
|
Chris@38
|
996 size_t windowSize,
|
Chris@38
|
997 size_t windowIncrement,
|
Chris@38
|
998 size_t sampleRate,
|
Chris@38
|
999 float oldPhase,
|
Chris@38
|
1000 float newPhase,
|
Chris@38
|
1001 bool &steadyState)
|
Chris@38
|
1002 {
|
Chris@38
|
1003 // At frequency f, phase shift of 2pi (one cycle) happens in 1/f sec.
|
Chris@38
|
1004 // At hopsize h and sample rate sr, one hop happens in h/sr sec.
|
Chris@38
|
1005 // At window size w, for bin b, f is b*sr/w.
|
Chris@38
|
1006 // thus 2pi phase shift happens in w/(b*sr) sec.
|
Chris@38
|
1007 // We need to know what phase shift we expect from h/sr sec.
|
Chris@38
|
1008 // -> 2pi * ((h/sr) / (w/(b*sr)))
|
Chris@38
|
1009 // = 2pi * ((h * b * sr) / (w * sr))
|
Chris@38
|
1010 // = 2pi * (h * b) / w.
|
Chris@38
|
1011
|
Chris@38
|
1012 float frequency = (float(bin) * sampleRate) / windowSize;
|
Chris@38
|
1013
|
Chris@38
|
1014 float expectedPhase =
|
Chris@38
|
1015 oldPhase + (2.0 * M_PI * bin * windowIncrement) / windowSize;
|
Chris@38
|
1016
|
Chris@38
|
1017 float phaseError = MathUtilities::princarg(newPhase - expectedPhase);
|
Chris@38
|
1018
|
Chris@38
|
1019 if (fabs(phaseError) < (1.1 * (windowIncrement * M_PI) / windowSize)) {
|
Chris@38
|
1020
|
Chris@38
|
1021 // The new frequency estimate based on the phase error
|
Chris@38
|
1022 // resulting from assuming the "native" frequency of this bin
|
Chris@38
|
1023
|
Chris@38
|
1024 float newFrequency =
|
Chris@38
|
1025 (sampleRate * (expectedPhase + phaseError - oldPhase)) /
|
Chris@38
|
1026 (2 * M_PI * windowIncrement);
|
Chris@38
|
1027
|
Chris@38
|
1028 steadyState = true;
|
Chris@38
|
1029 return newFrequency;
|
Chris@38
|
1030 }
|
Chris@38
|
1031
|
Chris@38
|
1032 steadyState = false;
|
Chris@38
|
1033 return frequency;
|
Chris@38
|
1034 }
|
Chris@38
|
1035
|
Chris@38
|
1036 void
|
Chris@0
|
1037 SpectrogramLayer::fillCacheColumn(int column, double *input,
|
Chris@0
|
1038 fftw_complex *output,
|
Chris@0
|
1039 fftw_plan plan,
|
Chris@9
|
1040 size_t windowSize,
|
Chris@9
|
1041 size_t increment,
|
Chris@38
|
1042 const Window<double> &windower) const
|
Chris@0
|
1043 {
|
Chris@38
|
1044 //!!! we _do_ need a lock for these references to the model
|
Chris@38
|
1045 // though, don't we?
|
Chris@35
|
1046
|
Chris@0
|
1047 int startFrame = increment * column;
|
Chris@9
|
1048 int endFrame = startFrame + windowSize;
|
Chris@0
|
1049
|
Chris@9
|
1050 startFrame -= int(windowSize - increment) / 2;
|
Chris@9
|
1051 endFrame -= int(windowSize - increment) / 2;
|
Chris@0
|
1052 size_t pfx = 0;
|
Chris@0
|
1053
|
Chris@0
|
1054 if (startFrame < 0) {
|
Chris@0
|
1055 pfx = size_t(-startFrame);
|
Chris@0
|
1056 for (size_t i = 0; i < pfx; ++i) {
|
Chris@0
|
1057 input[i] = 0.0;
|
Chris@0
|
1058 }
|
Chris@0
|
1059 }
|
Chris@0
|
1060
|
Chris@0
|
1061 size_t got = m_model->getValues(m_channel, startFrame + pfx,
|
Chris@0
|
1062 endFrame, input + pfx);
|
Chris@9
|
1063 while (got + pfx < windowSize) {
|
Chris@0
|
1064 input[got + pfx] = 0.0;
|
Chris@0
|
1065 ++got;
|
Chris@0
|
1066 }
|
Chris@0
|
1067
|
Chris@37
|
1068 if (m_channel == -1) {
|
Chris@37
|
1069 int channels = m_model->getChannelCount();
|
Chris@37
|
1070 if (channels > 1) {
|
Chris@37
|
1071 for (size_t i = 0; i < windowSize; ++i) {
|
Chris@37
|
1072 input[i] /= channels;
|
Chris@37
|
1073 }
|
Chris@37
|
1074 }
|
Chris@37
|
1075 }
|
Chris@37
|
1076
|
Chris@0
|
1077 windower.cut(input);
|
Chris@0
|
1078
|
Chris@35
|
1079 for (size_t i = 0; i < windowSize/2; ++i) {
|
Chris@35
|
1080 double temp = input[i];
|
Chris@35
|
1081 input[i] = input[i + windowSize/2];
|
Chris@35
|
1082 input[i + windowSize/2] = temp;
|
Chris@35
|
1083 }
|
Chris@35
|
1084
|
Chris@0
|
1085 fftw_execute(plan);
|
Chris@0
|
1086
|
Chris@38
|
1087 double factor = 0.0;
|
Chris@0
|
1088
|
Chris@38
|
1089 // Calculate magnitude and phase from real and imaginary in
|
Chris@38
|
1090 // output[i][0] and output[i][1] respectively, and store the phase
|
Chris@38
|
1091 // straight into cache and the magnitude back into output[i][0]
|
Chris@38
|
1092 // (because we'll need to know the normalization factor,
|
Chris@38
|
1093 // i.e. maximum magnitude in this column, before we can store it)
|
Chris@37
|
1094
|
Chris@38
|
1095 for (size_t i = 0; i < windowSize/2; ++i) {
|
Chris@35
|
1096
|
Chris@36
|
1097 double mag = sqrt(output[i][0] * output[i][0] +
|
Chris@36
|
1098 output[i][1] * output[i][1]);
|
Chris@38
|
1099 mag /= windowSize / 2;
|
Chris@37
|
1100
|
Chris@38
|
1101 if (mag > factor) factor = mag;
|
Chris@37
|
1102
|
Chris@38
|
1103 double phase = atan2(output[i][1], output[i][0]);
|
Chris@38
|
1104 phase = MathUtilities::princarg(phase);
|
Chris@37
|
1105
|
Chris@38
|
1106 output[i][0] = mag;
|
Chris@38
|
1107 m_cache->setPhaseAt(column, i, phase);
|
Chris@38
|
1108 }
|
Chris@35
|
1109
|
Chris@38
|
1110 m_cache->setNormalizationFactor(column, factor);
|
Chris@37
|
1111
|
Chris@38
|
1112 for (size_t i = 0; i < windowSize/2; ++i) {
|
Chris@38
|
1113 m_cache->setMagnitudeAt(column, i, output[i][0]);
|
Chris@38
|
1114 }
|
Chris@38
|
1115 }
|
Chris@35
|
1116
|
Chris@38
|
1117 unsigned char
|
Chris@38
|
1118 SpectrogramLayer::getDisplayValue(float input) const
|
Chris@38
|
1119 {
|
Chris@38
|
1120 int value;
|
Chris@37
|
1121
|
Chris@40
|
1122 switch (m_colourScale) {
|
Chris@40
|
1123
|
Chris@40
|
1124 default:
|
Chris@40
|
1125 case LinearColourScale:
|
Chris@40
|
1126 value = int
|
Chris@40
|
1127 (input * (m_normalizeColumns ? 1.0 : 50.0) * 255.0) + 1;
|
Chris@40
|
1128 break;
|
Chris@40
|
1129
|
Chris@40
|
1130 case MeterColourScale:
|
Chris@40
|
1131 value = AudioLevel::multiplier_to_preview
|
Chris@40
|
1132 (input * (m_normalizeColumns ? 1.0 : 50.0), 255) + 1;
|
Chris@40
|
1133 break;
|
Chris@40
|
1134
|
Chris@40
|
1135 case dBColourScale:
|
Chris@40
|
1136 input = 20.0 * log10(input);
|
Chris@40
|
1137 input = (input + 80.0) / 80.0;
|
Chris@40
|
1138 if (input < 0.0) input = 0.0;
|
Chris@40
|
1139 if (input > 1.0) input = 1.0;
|
Chris@40
|
1140 value = int(input * 255.0) + 1;
|
Chris@40
|
1141 break;
|
Chris@40
|
1142
|
Chris@40
|
1143 case PhaseColourScale:
|
Chris@40
|
1144 value = int((input * 127.0 / M_PI) + 128);
|
Chris@40
|
1145 break;
|
Chris@0
|
1146 }
|
Chris@38
|
1147
|
Chris@38
|
1148 if (value > UCHAR_MAX) value = UCHAR_MAX;
|
Chris@38
|
1149 if (value < 0) value = 0;
|
Chris@38
|
1150 return value;
|
Chris@0
|
1151 }
|
Chris@0
|
1152
|
Chris@40
|
1153 float
|
Chris@40
|
1154 SpectrogramLayer::getInputForDisplayValue(unsigned char uc) const
|
Chris@40
|
1155 {
|
Chris@40
|
1156 int value = uc;
|
Chris@40
|
1157 float input;
|
Chris@40
|
1158
|
Chris@40
|
1159 switch (m_colourScale) {
|
Chris@40
|
1160
|
Chris@40
|
1161 default:
|
Chris@40
|
1162 case LinearColourScale:
|
Chris@40
|
1163 input = float(value - 1) / 255.0 / (m_normalizeColumns ? 1 : 50);
|
Chris@40
|
1164 break;
|
Chris@40
|
1165
|
Chris@40
|
1166 case MeterColourScale:
|
Chris@40
|
1167 input = AudioLevel::preview_to_multiplier(value - 1, 255)
|
Chris@40
|
1168 / (m_normalizeColumns ? 1.0 : 50.0);
|
Chris@40
|
1169 break;
|
Chris@40
|
1170
|
Chris@40
|
1171 case dBColourScale:
|
Chris@40
|
1172 input = float(value - 1) / 255.0;
|
Chris@40
|
1173 input = (input * 80.0) - 80.0;
|
Chris@40
|
1174 input = powf(10.0, input) / 20.0;
|
Chris@40
|
1175 value = int(input);
|
Chris@40
|
1176 break;
|
Chris@40
|
1177
|
Chris@40
|
1178 case PhaseColourScale:
|
Chris@40
|
1179 input = float(value - 128) * M_PI / 127.0;
|
Chris@40
|
1180 break;
|
Chris@40
|
1181 }
|
Chris@40
|
1182
|
Chris@40
|
1183 return input;
|
Chris@40
|
1184 }
|
Chris@40
|
1185
|
Chris@38
|
1186
|
Chris@38
|
1187 SpectrogramLayer::Cache::Cache() :
|
Chris@38
|
1188 m_width(0),
|
Chris@38
|
1189 m_height(0),
|
Chris@38
|
1190 m_magnitude(0),
|
Chris@38
|
1191 m_phase(0),
|
Chris@38
|
1192 m_factor(0)
|
Chris@31
|
1193 {
|
Chris@31
|
1194 }
|
Chris@31
|
1195
|
Chris@31
|
1196 SpectrogramLayer::Cache::~Cache()
|
Chris@31
|
1197 {
|
Chris@44
|
1198 for (size_t i = 0; i < m_width; ++i) {
|
Chris@38
|
1199 if (m_magnitude && m_magnitude[i]) free(m_magnitude[i]);
|
Chris@38
|
1200 if (m_phase && m_phase[i]) free(m_phase[i]);
|
Chris@38
|
1201 }
|
Chris@38
|
1202
|
Chris@38
|
1203 if (m_magnitude) free(m_magnitude);
|
Chris@38
|
1204 if (m_phase) free(m_phase);
|
Chris@38
|
1205 if (m_factor) free(m_factor);
|
Chris@31
|
1206 }
|
Chris@31
|
1207
|
Chris@35
|
1208 void
|
Chris@35
|
1209 SpectrogramLayer::Cache::resize(size_t width, size_t height)
|
Chris@35
|
1210 {
|
Chris@37
|
1211 std::cerr << "SpectrogramLayer::Cache[" << this << "]::resize(" << width << "x" << height << ")" << std::endl;
|
Chris@38
|
1212
|
Chris@38
|
1213 if (m_width == width && m_height == height) return;
|
Chris@35
|
1214
|
Chris@38
|
1215 resize(m_magnitude, width, height);
|
Chris@38
|
1216 resize(m_phase, width, height);
|
Chris@31
|
1217
|
Chris@38
|
1218 m_factor = (float *)realloc(m_factor, width * sizeof(float));
|
Chris@31
|
1219
|
Chris@38
|
1220 m_width = width;
|
Chris@38
|
1221 m_height = height;
|
Chris@41
|
1222
|
Chris@41
|
1223 std::cerr << "done, width = " << m_width << " height = " << m_height << std::endl;
|
Chris@31
|
1224 }
|
Chris@31
|
1225
|
Chris@31
|
1226 void
|
Chris@38
|
1227 SpectrogramLayer::Cache::resize(uint16_t **&array, size_t width, size_t height)
|
Chris@31
|
1228 {
|
Chris@44
|
1229 for (size_t i = width; i < m_width; ++i) {
|
Chris@38
|
1230 free(array[i]);
|
Chris@38
|
1231 }
|
Chris@31
|
1232
|
Chris@44
|
1233 if (width != m_width) {
|
Chris@44
|
1234 array = (uint16_t **)realloc(array, width * sizeof(uint16_t *));
|
Chris@38
|
1235 if (!array) throw std::bad_alloc();
|
Chris@44
|
1236 MUNLOCK(array, width * sizeof(uint16_t *));
|
Chris@38
|
1237 }
|
Chris@38
|
1238
|
Chris@44
|
1239 for (size_t i = m_width; i < width; ++i) {
|
Chris@38
|
1240 array[i] = 0;
|
Chris@38
|
1241 }
|
Chris@38
|
1242
|
Chris@44
|
1243 for (size_t i = 0; i < width; ++i) {
|
Chris@44
|
1244 array[i] = (uint16_t *)realloc(array[i], height * sizeof(uint16_t));
|
Chris@38
|
1245 if (!array[i]) throw std::bad_alloc();
|
Chris@44
|
1246 MUNLOCK(array[i], height * sizeof(uint16_t));
|
Chris@38
|
1247 }
|
Chris@31
|
1248 }
|
Chris@31
|
1249
|
Chris@31
|
1250 void
|
Chris@38
|
1251 SpectrogramLayer::Cache::reset()
|
Chris@31
|
1252 {
|
Chris@38
|
1253 for (size_t x = 0; x < m_width; ++x) {
|
Chris@38
|
1254 for (size_t y = 0; y < m_height; ++y) {
|
Chris@44
|
1255 m_magnitude[x][y] = 0;
|
Chris@44
|
1256 m_phase[x][y] = 0;
|
Chris@38
|
1257 }
|
Chris@40
|
1258 m_factor[x] = 1.0;
|
Chris@31
|
1259 }
|
Chris@38
|
1260 }
|
Chris@31
|
1261
|
Chris@0
|
1262 void
|
Chris@0
|
1263 SpectrogramLayer::CacheFillThread::run()
|
Chris@0
|
1264 {
|
Chris@0
|
1265 // std::cerr << "SpectrogramLayer::CacheFillThread::run" << std::endl;
|
Chris@0
|
1266
|
Chris@0
|
1267 m_layer.m_mutex.lock();
|
Chris@0
|
1268
|
Chris@0
|
1269 while (!m_layer.m_exiting) {
|
Chris@0
|
1270
|
Chris@0
|
1271 bool interrupted = false;
|
Chris@0
|
1272
|
Chris@0
|
1273 // std::cerr << "SpectrogramLayer::CacheFillThread::run in loop" << std::endl;
|
Chris@0
|
1274
|
Chris@34
|
1275 if (m_layer.m_dormant) {
|
Chris@34
|
1276
|
Chris@34
|
1277 if (m_layer.m_cacheInvalid) {
|
Chris@34
|
1278 delete m_layer.m_cache;
|
Chris@34
|
1279 m_layer.m_cache = 0;
|
Chris@34
|
1280 }
|
Chris@34
|
1281
|
Chris@34
|
1282 } else if (m_layer.m_model && m_layer.m_cacheInvalid) {
|
Chris@0
|
1283
|
Chris@0
|
1284 // std::cerr << "SpectrogramLayer::CacheFillThread::run: something to do" << std::endl;
|
Chris@0
|
1285
|
Chris@0
|
1286 while (!m_layer.m_model->isReady()) {
|
Chris@0
|
1287 m_layer.m_condition.wait(&m_layer.m_mutex, 100);
|
Chris@0
|
1288 }
|
Chris@0
|
1289
|
Chris@0
|
1290 m_layer.m_cacheInvalid = false;
|
Chris@0
|
1291 m_fillExtent = 0;
|
Chris@0
|
1292 m_fillCompletion = 0;
|
Chris@0
|
1293
|
Chris@0
|
1294 std::cerr << "SpectrogramLayer::CacheFillThread::run: model is ready" << std::endl;
|
Chris@0
|
1295
|
Chris@0
|
1296 size_t start = m_layer.m_model->getStartFrame();
|
Chris@0
|
1297 size_t end = m_layer.m_model->getEndFrame();
|
Chris@9
|
1298
|
Chris@41
|
1299 std::cerr << "start = " << start << ", end = " << end << std::endl;
|
Chris@41
|
1300
|
Chris@9
|
1301 WindowType windowType = m_layer.m_windowType;
|
Chris@0
|
1302 size_t windowSize = m_layer.m_windowSize;
|
Chris@0
|
1303 size_t windowIncrement = m_layer.getWindowIncrement();
|
Chris@0
|
1304
|
Chris@44
|
1305 size_t visibleStart = m_layer.m_candidateFillStartFrame;
|
Chris@44
|
1306 visibleStart = (visibleStart / windowIncrement) * windowIncrement;
|
Chris@0
|
1307
|
Chris@9
|
1308 size_t width = (end - start) / windowIncrement + 1;
|
Chris@9
|
1309 size_t height = windowSize / 2;
|
Chris@35
|
1310
|
Chris@35
|
1311 if (!m_layer.m_cache) {
|
Chris@38
|
1312 m_layer.m_cache = new Cache;
|
Chris@35
|
1313 }
|
Chris@9
|
1314
|
Chris@38
|
1315 m_layer.m_cache->resize(width, height);
|
Chris@0
|
1316 m_layer.setCacheColourmap();
|
Chris@43
|
1317 //!!! m_layer.m_cache->reset();
|
Chris@35
|
1318
|
Chris@33
|
1319 // We don't need a lock when writing to or reading from
|
Chris@38
|
1320 // the pixels in the cache. We do need to ensure we have
|
Chris@38
|
1321 // the width and height of the cache and the FFT
|
Chris@38
|
1322 // parameters known before we unlock, in case they change
|
Chris@38
|
1323 // in the model while we aren't holding a lock. It's safe
|
Chris@38
|
1324 // for us to continue to use the "old" values if that
|
Chris@38
|
1325 // happens, because they will continue to match the
|
Chris@38
|
1326 // dimensions of the actual cache (which we manage, not
|
Chris@38
|
1327 // the model).
|
Chris@0
|
1328 m_layer.m_mutex.unlock();
|
Chris@0
|
1329
|
Chris@0
|
1330 double *input = (double *)
|
Chris@0
|
1331 fftw_malloc(windowSize * sizeof(double));
|
Chris@0
|
1332
|
Chris@0
|
1333 fftw_complex *output = (fftw_complex *)
|
Chris@0
|
1334 fftw_malloc(windowSize * sizeof(fftw_complex));
|
Chris@0
|
1335
|
Chris@0
|
1336 fftw_plan plan = fftw_plan_dft_r2c_1d(windowSize, input,
|
Chris@1
|
1337 output, FFTW_ESTIMATE);
|
Chris@0
|
1338
|
Chris@9
|
1339 Window<double> windower(windowType, windowSize);
|
Chris@0
|
1340
|
Chris@0
|
1341 if (!plan) {
|
Chris@1
|
1342 std::cerr << "WARNING: fftw_plan_dft_r2c_1d(" << windowSize << ") failed!" << std::endl;
|
Chris@0
|
1343 fftw_free(input);
|
Chris@0
|
1344 fftw_free(output);
|
Chris@37
|
1345 m_layer.m_mutex.lock();
|
Chris@0
|
1346 continue;
|
Chris@0
|
1347 }
|
Chris@0
|
1348
|
Chris@0
|
1349 int counter = 0;
|
Chris@0
|
1350 int updateAt = (end / windowIncrement) / 20;
|
Chris@0
|
1351 if (updateAt < 100) updateAt = 100;
|
Chris@0
|
1352
|
Chris@44
|
1353 bool doVisibleFirst = (visibleStart != start);
|
Chris@0
|
1354
|
Chris@0
|
1355 if (doVisibleFirst) {
|
Chris@0
|
1356
|
Chris@44
|
1357 for (size_t f = visibleStart; f < end; f += windowIncrement) {
|
Chris@0
|
1358
|
Chris@38
|
1359 m_layer.fillCacheColumn(int((f - start) / windowIncrement),
|
Chris@38
|
1360 input, output, plan,
|
Chris@38
|
1361 windowSize, windowIncrement,
|
Chris@38
|
1362 windower);
|
Chris@38
|
1363
|
Chris@38
|
1364 if (m_layer.m_cacheInvalid || m_layer.m_exiting) {
|
Chris@0
|
1365 interrupted = true;
|
Chris@0
|
1366 m_fillExtent = 0;
|
Chris@0
|
1367 break;
|
Chris@0
|
1368 }
|
Chris@0
|
1369
|
Chris@38
|
1370 if (++counter == updateAt) {
|
Chris@37
|
1371 m_fillExtent = f;
|
Chris@0
|
1372 m_fillCompletion = size_t(100 * fabsf(float(f - visibleStart) /
|
Chris@0
|
1373 float(end - start)));
|
Chris@0
|
1374 counter = 0;
|
Chris@0
|
1375 }
|
Chris@0
|
1376 }
|
Chris@0
|
1377 }
|
Chris@0
|
1378
|
Chris@0
|
1379 if (!interrupted) {
|
Chris@0
|
1380
|
Chris@0
|
1381 size_t remainingEnd = end;
|
Chris@0
|
1382 if (doVisibleFirst) {
|
Chris@0
|
1383 remainingEnd = visibleStart;
|
Chris@0
|
1384 if (remainingEnd > start) --remainingEnd;
|
Chris@0
|
1385 else remainingEnd = start;
|
Chris@0
|
1386 }
|
Chris@0
|
1387 size_t baseCompletion = m_fillCompletion;
|
Chris@0
|
1388
|
Chris@0
|
1389 for (size_t f = start; f < remainingEnd; f += windowIncrement) {
|
Chris@0
|
1390
|
Chris@38
|
1391 m_layer.fillCacheColumn(int((f - start) / windowIncrement),
|
Chris@38
|
1392 input, output, plan,
|
Chris@38
|
1393 windowSize, windowIncrement,
|
Chris@38
|
1394 windower);
|
Chris@38
|
1395
|
Chris@38
|
1396 if (m_layer.m_cacheInvalid || m_layer.m_exiting) {
|
Chris@0
|
1397 interrupted = true;
|
Chris@0
|
1398 m_fillExtent = 0;
|
Chris@0
|
1399 break;
|
Chris@0
|
1400 }
|
Chris@0
|
1401
|
Chris@44
|
1402 if (++counter == updateAt) {
|
Chris@0
|
1403 m_fillExtent = f;
|
Chris@0
|
1404 m_fillCompletion = baseCompletion +
|
Chris@0
|
1405 size_t(100 * fabsf(float(f - start) /
|
Chris@0
|
1406 float(end - start)));
|
Chris@0
|
1407 counter = 0;
|
Chris@0
|
1408 }
|
Chris@0
|
1409 }
|
Chris@0
|
1410 }
|
Chris@0
|
1411
|
Chris@0
|
1412 fftw_destroy_plan(plan);
|
Chris@0
|
1413 fftw_free(output);
|
Chris@0
|
1414 fftw_free(input);
|
Chris@0
|
1415
|
Chris@0
|
1416 if (!interrupted) {
|
Chris@0
|
1417 m_fillExtent = end;
|
Chris@0
|
1418 m_fillCompletion = 100;
|
Chris@0
|
1419 }
|
Chris@0
|
1420
|
Chris@0
|
1421 m_layer.m_mutex.lock();
|
Chris@0
|
1422 }
|
Chris@0
|
1423
|
Chris@0
|
1424 if (!interrupted) m_layer.m_condition.wait(&m_layer.m_mutex, 2000);
|
Chris@0
|
1425 }
|
Chris@0
|
1426 }
|
Chris@0
|
1427
|
Chris@40
|
1428 float
|
Chris@40
|
1429 SpectrogramLayer::getEffectiveMinFrequency() const
|
Chris@40
|
1430 {
|
Chris@40
|
1431 int sr = m_model->getSampleRate();
|
Chris@40
|
1432 float minf = float(sr) / m_windowSize;
|
Chris@40
|
1433
|
Chris@40
|
1434 if (m_minFrequency > 0.0) {
|
Chris@40
|
1435 size_t minbin = size_t((double(m_minFrequency) * m_windowSize) / sr + 0.01);
|
Chris@40
|
1436 if (minbin < 1) minbin = 1;
|
Chris@40
|
1437 minf = minbin * sr / m_windowSize;
|
Chris@40
|
1438 }
|
Chris@40
|
1439
|
Chris@40
|
1440 return minf;
|
Chris@40
|
1441 }
|
Chris@40
|
1442
|
Chris@40
|
1443 float
|
Chris@40
|
1444 SpectrogramLayer::getEffectiveMaxFrequency() const
|
Chris@40
|
1445 {
|
Chris@40
|
1446 int sr = m_model->getSampleRate();
|
Chris@40
|
1447 float maxf = float(sr) / 2;
|
Chris@40
|
1448
|
Chris@40
|
1449 if (m_maxFrequency > 0.0) {
|
Chris@40
|
1450 size_t maxbin = size_t((double(m_maxFrequency) * m_windowSize) / sr + 0.1);
|
Chris@40
|
1451 if (maxbin > m_windowSize / 2) maxbin = m_windowSize / 2;
|
Chris@40
|
1452 maxf = maxbin * sr / m_windowSize;
|
Chris@40
|
1453 }
|
Chris@40
|
1454
|
Chris@40
|
1455 return maxf;
|
Chris@40
|
1456 }
|
Chris@40
|
1457
|
Chris@0
|
1458 bool
|
Chris@44
|
1459 SpectrogramLayer::getYBinRange(View *v, int y, float &q0, float &q1) const
|
Chris@0
|
1460 {
|
Chris@44
|
1461 int h = v->height();
|
Chris@0
|
1462 if (y < 0 || y >= h) return false;
|
Chris@0
|
1463
|
Chris@38
|
1464 int sr = m_model->getSampleRate();
|
Chris@40
|
1465 float minf = getEffectiveMinFrequency();
|
Chris@40
|
1466 float maxf = getEffectiveMaxFrequency();
|
Chris@0
|
1467
|
Chris@38
|
1468 bool logarithmic = (m_frequencyScale == LogFrequencyScale);
|
Chris@38
|
1469
|
Chris@44
|
1470 q0 = v->getFrequencyForY(y, minf, maxf, logarithmic);
|
Chris@44
|
1471 q1 = v->getFrequencyForY(y - 1, minf, maxf, logarithmic);
|
Chris@38
|
1472
|
Chris@38
|
1473 // Now map these on to actual bins
|
Chris@38
|
1474
|
Chris@40
|
1475 int b0 = int((q0 * m_windowSize) / sr);
|
Chris@40
|
1476 int b1 = int((q1 * m_windowSize) / sr);
|
Chris@0
|
1477
|
Chris@40
|
1478 //!!! this is supposed to return fractions-of-bins, as it were, hence the floats
|
Chris@38
|
1479 q0 = b0;
|
Chris@38
|
1480 q1 = b1;
|
Chris@38
|
1481
|
Chris@38
|
1482 // q0 = (b0 * sr) / m_windowSize;
|
Chris@38
|
1483 // q1 = (b1 * sr) / m_windowSize;
|
Chris@0
|
1484
|
Chris@0
|
1485 return true;
|
Chris@0
|
1486 }
|
Chris@38
|
1487
|
Chris@0
|
1488 bool
|
Chris@44
|
1489 SpectrogramLayer::getXBinRange(View *v, int x, float &s0, float &s1) const
|
Chris@0
|
1490 {
|
Chris@21
|
1491 size_t modelStart = m_model->getStartFrame();
|
Chris@21
|
1492 size_t modelEnd = m_model->getEndFrame();
|
Chris@0
|
1493
|
Chris@0
|
1494 // Each pixel column covers an exact range of sample frames:
|
Chris@44
|
1495 int f0 = v->getFrameForX(x) - modelStart;
|
Chris@44
|
1496 int f1 = v->getFrameForX(x + 1) - modelStart - 1;
|
Chris@20
|
1497
|
Chris@41
|
1498 if (f1 < int(modelStart) || f0 > int(modelEnd)) {
|
Chris@41
|
1499 return false;
|
Chris@41
|
1500 }
|
Chris@20
|
1501
|
Chris@0
|
1502 // And that range may be drawn from a possibly non-integral
|
Chris@0
|
1503 // range of spectrogram windows:
|
Chris@0
|
1504
|
Chris@0
|
1505 size_t windowIncrement = getWindowIncrement();
|
Chris@0
|
1506 s0 = float(f0) / windowIncrement;
|
Chris@0
|
1507 s1 = float(f1) / windowIncrement;
|
Chris@0
|
1508
|
Chris@0
|
1509 return true;
|
Chris@0
|
1510 }
|
Chris@0
|
1511
|
Chris@0
|
1512 bool
|
Chris@44
|
1513 SpectrogramLayer::getXBinSourceRange(View *v, int x, RealTime &min, RealTime &max) const
|
Chris@0
|
1514 {
|
Chris@0
|
1515 float s0 = 0, s1 = 0;
|
Chris@44
|
1516 if (!getXBinRange(v, x, s0, s1)) return false;
|
Chris@0
|
1517
|
Chris@0
|
1518 int s0i = int(s0 + 0.001);
|
Chris@0
|
1519 int s1i = int(s1);
|
Chris@0
|
1520
|
Chris@0
|
1521 int windowIncrement = getWindowIncrement();
|
Chris@0
|
1522 int w0 = s0i * windowIncrement - (m_windowSize - windowIncrement)/2;
|
Chris@0
|
1523 int w1 = s1i * windowIncrement + windowIncrement +
|
Chris@0
|
1524 (m_windowSize - windowIncrement)/2 - 1;
|
Chris@0
|
1525
|
Chris@0
|
1526 min = RealTime::frame2RealTime(w0, m_model->getSampleRate());
|
Chris@0
|
1527 max = RealTime::frame2RealTime(w1, m_model->getSampleRate());
|
Chris@0
|
1528 return true;
|
Chris@0
|
1529 }
|
Chris@0
|
1530
|
Chris@0
|
1531 bool
|
Chris@44
|
1532 SpectrogramLayer::getYBinSourceRange(View *v, int y, float &freqMin, float &freqMax)
|
Chris@0
|
1533 const
|
Chris@0
|
1534 {
|
Chris@0
|
1535 float q0 = 0, q1 = 0;
|
Chris@44
|
1536 if (!getYBinRange(v, y, q0, q1)) return false;
|
Chris@0
|
1537
|
Chris@0
|
1538 int q0i = int(q0 + 0.001);
|
Chris@0
|
1539 int q1i = int(q1);
|
Chris@0
|
1540
|
Chris@0
|
1541 int sr = m_model->getSampleRate();
|
Chris@0
|
1542
|
Chris@0
|
1543 for (int q = q0i; q <= q1i; ++q) {
|
Chris@35
|
1544 int binfreq = (sr * q) / m_windowSize;
|
Chris@0
|
1545 if (q == q0i) freqMin = binfreq;
|
Chris@0
|
1546 if (q == q1i) freqMax = binfreq;
|
Chris@0
|
1547 }
|
Chris@0
|
1548 return true;
|
Chris@0
|
1549 }
|
Chris@35
|
1550
|
Chris@35
|
1551 bool
|
Chris@44
|
1552 SpectrogramLayer::getAdjustedYBinSourceRange(View *v, int x, int y,
|
Chris@35
|
1553 float &freqMin, float &freqMax,
|
Chris@35
|
1554 float &adjFreqMin, float &adjFreqMax)
|
Chris@35
|
1555 const
|
Chris@35
|
1556 {
|
Chris@35
|
1557 float s0 = 0, s1 = 0;
|
Chris@44
|
1558 if (!getXBinRange(v, x, s0, s1)) return false;
|
Chris@35
|
1559
|
Chris@35
|
1560 float q0 = 0, q1 = 0;
|
Chris@44
|
1561 if (!getYBinRange(v, y, q0, q1)) return false;
|
Chris@35
|
1562
|
Chris@35
|
1563 int s0i = int(s0 + 0.001);
|
Chris@35
|
1564 int s1i = int(s1);
|
Chris@35
|
1565
|
Chris@35
|
1566 int q0i = int(q0 + 0.001);
|
Chris@35
|
1567 int q1i = int(q1);
|
Chris@35
|
1568
|
Chris@35
|
1569 int sr = m_model->getSampleRate();
|
Chris@35
|
1570
|
Chris@38
|
1571 size_t windowSize = m_windowSize;
|
Chris@38
|
1572 size_t windowIncrement = getWindowIncrement();
|
Chris@38
|
1573
|
Chris@35
|
1574 bool haveAdj = false;
|
Chris@35
|
1575
|
Chris@37
|
1576 bool peaksOnly = (m_binDisplay == PeakBins ||
|
Chris@37
|
1577 m_binDisplay == PeakFrequencies);
|
Chris@37
|
1578
|
Chris@35
|
1579 for (int q = q0i; q <= q1i; ++q) {
|
Chris@35
|
1580
|
Chris@35
|
1581 for (int s = s0i; s <= s1i; ++s) {
|
Chris@35
|
1582
|
Chris@35
|
1583 float binfreq = (sr * q) / m_windowSize;
|
Chris@35
|
1584 if (q == q0i) freqMin = binfreq;
|
Chris@35
|
1585 if (q == q1i) freqMax = binfreq;
|
Chris@37
|
1586
|
Chris@38
|
1587 if (!m_cache || m_cacheInvalid) break; //!!! lock?
|
Chris@38
|
1588
|
Chris@38
|
1589 if (peaksOnly && !m_cache->isLocalPeak(s, q)) continue;
|
Chris@38
|
1590
|
Chris@38
|
1591 if (!m_cache->isOverThreshold(s, q, m_threshold)) continue;
|
Chris@38
|
1592
|
Chris@38
|
1593 float freq = binfreq;
|
Chris@38
|
1594 bool steady = false;
|
Chris@40
|
1595
|
Chris@40
|
1596 if (s < int(m_cache->getWidth()) - 1) {
|
Chris@38
|
1597
|
Chris@38
|
1598 freq = calculateFrequency(q,
|
Chris@38
|
1599 windowSize,
|
Chris@38
|
1600 windowIncrement,
|
Chris@38
|
1601 sr,
|
Chris@38
|
1602 m_cache->getPhaseAt(s, q),
|
Chris@38
|
1603 m_cache->getPhaseAt(s+1, q),
|
Chris@38
|
1604 steady);
|
Chris@35
|
1605
|
Chris@38
|
1606 if (!haveAdj || freq < adjFreqMin) adjFreqMin = freq;
|
Chris@38
|
1607 if (!haveAdj || freq > adjFreqMax) adjFreqMax = freq;
|
Chris@35
|
1608
|
Chris@35
|
1609 haveAdj = true;
|
Chris@35
|
1610 }
|
Chris@35
|
1611 }
|
Chris@35
|
1612 }
|
Chris@35
|
1613
|
Chris@35
|
1614 if (!haveAdj) {
|
Chris@40
|
1615 adjFreqMin = adjFreqMax = 0.0;
|
Chris@35
|
1616 }
|
Chris@35
|
1617
|
Chris@35
|
1618 return haveAdj;
|
Chris@35
|
1619 }
|
Chris@0
|
1620
|
Chris@0
|
1621 bool
|
Chris@44
|
1622 SpectrogramLayer::getXYBinSourceRange(View *v, int x, int y,
|
Chris@38
|
1623 float &min, float &max,
|
Chris@38
|
1624 float &phaseMin, float &phaseMax) const
|
Chris@0
|
1625 {
|
Chris@0
|
1626 float q0 = 0, q1 = 0;
|
Chris@44
|
1627 if (!getYBinRange(v, y, q0, q1)) return false;
|
Chris@0
|
1628
|
Chris@0
|
1629 float s0 = 0, s1 = 0;
|
Chris@44
|
1630 if (!getXBinRange(v, x, s0, s1)) return false;
|
Chris@0
|
1631
|
Chris@0
|
1632 int q0i = int(q0 + 0.001);
|
Chris@0
|
1633 int q1i = int(q1);
|
Chris@0
|
1634
|
Chris@0
|
1635 int s0i = int(s0 + 0.001);
|
Chris@0
|
1636 int s1i = int(s1);
|
Chris@0
|
1637
|
Chris@37
|
1638 bool rv = false;
|
Chris@37
|
1639
|
Chris@0
|
1640 if (m_mutex.tryLock()) {
|
Chris@0
|
1641 if (m_cache && !m_cacheInvalid) {
|
Chris@0
|
1642
|
Chris@31
|
1643 int cw = m_cache->getWidth();
|
Chris@31
|
1644 int ch = m_cache->getHeight();
|
Chris@0
|
1645
|
Chris@38
|
1646 min = 0.0;
|
Chris@38
|
1647 max = 0.0;
|
Chris@38
|
1648 phaseMin = 0.0;
|
Chris@38
|
1649 phaseMax = 0.0;
|
Chris@38
|
1650 bool have = false;
|
Chris@0
|
1651
|
Chris@0
|
1652 for (int q = q0i; q <= q1i; ++q) {
|
Chris@0
|
1653 for (int s = s0i; s <= s1i; ++s) {
|
Chris@0
|
1654 if (s >= 0 && q >= 0 && s < cw && q < ch) {
|
Chris@38
|
1655
|
Chris@38
|
1656 float value;
|
Chris@38
|
1657
|
Chris@38
|
1658 value = m_cache->getPhaseAt(s, q);
|
Chris@38
|
1659 if (!have || value < phaseMin) { phaseMin = value; }
|
Chris@38
|
1660 if (!have || value > phaseMax) { phaseMax = value; }
|
Chris@38
|
1661
|
Chris@38
|
1662 value = m_cache->getMagnitudeAt(s, q);
|
Chris@38
|
1663 if (!have || value < min) { min = value; }
|
Chris@38
|
1664 if (!have || value > max) { max = value; }
|
Chris@38
|
1665
|
Chris@38
|
1666 have = true;
|
Chris@0
|
1667 }
|
Chris@0
|
1668 }
|
Chris@0
|
1669 }
|
Chris@0
|
1670
|
Chris@38
|
1671 if (have) {
|
Chris@37
|
1672 rv = true;
|
Chris@37
|
1673 }
|
Chris@0
|
1674 }
|
Chris@0
|
1675
|
Chris@0
|
1676 m_mutex.unlock();
|
Chris@0
|
1677 }
|
Chris@0
|
1678
|
Chris@37
|
1679 return rv;
|
Chris@0
|
1680 }
|
Chris@0
|
1681
|
Chris@0
|
1682 void
|
Chris@44
|
1683 SpectrogramLayer::paint(View *v, QPainter &paint, QRect rect) const
|
Chris@0
|
1684 {
|
Chris@0
|
1685 // Profiler profiler("SpectrogramLayer::paint", true);
|
Chris@0
|
1686 #ifdef DEBUG_SPECTROGRAM_REPAINT
|
Chris@44
|
1687 std::cerr << "SpectrogramLayer::paint(): m_model is " << m_model << ", zoom level is " << v->getZoomLevel() << ", m_updateTimer " << m_updateTimer << ", pixmap cache invalid " << m_pixmapCacheInvalid << std::endl;
|
Chris@0
|
1688 #endif
|
Chris@45
|
1689
|
Chris@45
|
1690 long sf = v->getStartFrame();
|
Chris@45
|
1691 if (sf < 0) m_candidateFillStartFrame = 0;
|
Chris@45
|
1692 else m_candidateFillStartFrame = sf;
|
Chris@44
|
1693
|
Chris@0
|
1694 if (!m_model || !m_model->isOK() || !m_model->isReady()) {
|
Chris@0
|
1695 return;
|
Chris@0
|
1696 }
|
Chris@0
|
1697
|
Chris@29
|
1698 if (m_dormant) {
|
Chris@33
|
1699 std::cerr << "SpectrogramLayer::paint(): Layer is dormant" << std::endl;
|
Chris@29
|
1700 return;
|
Chris@29
|
1701 }
|
Chris@29
|
1702
|
Chris@0
|
1703 #ifdef DEBUG_SPECTROGRAM_REPAINT
|
Chris@0
|
1704 std::cerr << "SpectrogramLayer::paint(): About to lock" << std::endl;
|
Chris@0
|
1705 #endif
|
Chris@0
|
1706
|
Chris@37
|
1707 m_mutex.lock();
|
Chris@0
|
1708
|
Chris@0
|
1709 #ifdef DEBUG_SPECTROGRAM_REPAINT
|
Chris@0
|
1710 std::cerr << "SpectrogramLayer::paint(): locked" << std::endl;
|
Chris@0
|
1711 #endif
|
Chris@0
|
1712
|
Chris@0
|
1713 if (m_cacheInvalid) { // lock the mutex before checking this
|
Chris@0
|
1714 m_mutex.unlock();
|
Chris@0
|
1715 #ifdef DEBUG_SPECTROGRAM_REPAINT
|
Chris@0
|
1716 std::cerr << "SpectrogramLayer::paint(): Cache invalid, returning" << std::endl;
|
Chris@0
|
1717 #endif
|
Chris@0
|
1718 return;
|
Chris@0
|
1719 }
|
Chris@0
|
1720
|
Chris@0
|
1721 bool stillCacheing = (m_updateTimer != 0);
|
Chris@0
|
1722
|
Chris@0
|
1723 #ifdef DEBUG_SPECTROGRAM_REPAINT
|
Chris@0
|
1724 std::cerr << "SpectrogramLayer::paint(): Still cacheing = " << stillCacheing << std::endl;
|
Chris@0
|
1725 #endif
|
Chris@0
|
1726
|
Chris@44
|
1727 long startFrame = v->getStartFrame();
|
Chris@44
|
1728 int zoomLevel = v->getZoomLevel();
|
Chris@0
|
1729
|
Chris@0
|
1730 int x0 = 0;
|
Chris@44
|
1731 int x1 = v->width();
|
Chris@0
|
1732 int y0 = 0;
|
Chris@44
|
1733 int y1 = v->height();
|
Chris@0
|
1734
|
Chris@0
|
1735 bool recreateWholePixmapCache = true;
|
Chris@0
|
1736
|
Chris@0
|
1737 if (!m_pixmapCacheInvalid) {
|
Chris@0
|
1738
|
Chris@0
|
1739 //!!! This cache may have been obsoleted entirely by the
|
Chris@0
|
1740 //scrolling cache in View. Perhaps experiment with
|
Chris@0
|
1741 //removing it and see if it makes things even quicker (or else
|
Chris@0
|
1742 //make it optional)
|
Chris@0
|
1743
|
Chris@0
|
1744 if (int(m_pixmapCacheZoomLevel) == zoomLevel &&
|
Chris@44
|
1745 m_pixmapCache->width() == v->width() &&
|
Chris@44
|
1746 m_pixmapCache->height() == v->height()) {
|
Chris@44
|
1747
|
Chris@44
|
1748 if (v->getXForFrame(m_pixmapCacheStartFrame) ==
|
Chris@44
|
1749 v->getXForFrame(startFrame)) {
|
Chris@0
|
1750
|
Chris@0
|
1751 #ifdef DEBUG_SPECTROGRAM_REPAINT
|
Chris@0
|
1752 std::cerr << "SpectrogramLayer: pixmap cache good" << std::endl;
|
Chris@0
|
1753 #endif
|
Chris@0
|
1754
|
Chris@0
|
1755 m_mutex.unlock();
|
Chris@0
|
1756 paint.drawPixmap(rect, *m_pixmapCache, rect);
|
Chris@0
|
1757 return;
|
Chris@0
|
1758
|
Chris@0
|
1759 } else {
|
Chris@0
|
1760
|
Chris@0
|
1761 #ifdef DEBUG_SPECTROGRAM_REPAINT
|
Chris@0
|
1762 std::cerr << "SpectrogramLayer: pixmap cache partially OK" << std::endl;
|
Chris@0
|
1763 #endif
|
Chris@0
|
1764
|
Chris@0
|
1765 recreateWholePixmapCache = false;
|
Chris@0
|
1766
|
Chris@44
|
1767 int dx = v->getXForFrame(m_pixmapCacheStartFrame) -
|
Chris@44
|
1768 v->getXForFrame(startFrame);
|
Chris@0
|
1769
|
Chris@0
|
1770 #ifdef DEBUG_SPECTROGRAM_REPAINT
|
Chris@0
|
1771 std::cerr << "SpectrogramLayer: dx = " << dx << " (pixmap cache " << m_pixmapCache->width() << "x" << m_pixmapCache->height() << ")" << std::endl;
|
Chris@0
|
1772 #endif
|
Chris@0
|
1773
|
Chris@0
|
1774 if (dx > -m_pixmapCache->width() && dx < m_pixmapCache->width()) {
|
Chris@0
|
1775
|
Chris@0
|
1776 #if defined(Q_WS_WIN32) || defined(Q_WS_MAC)
|
Chris@0
|
1777 // Copying a pixmap to itself doesn't work
|
Chris@0
|
1778 // properly on Windows or Mac (it only works when
|
Chris@0
|
1779 // moving in one direction).
|
Chris@0
|
1780
|
Chris@0
|
1781 //!!! Need a utility function for this
|
Chris@0
|
1782
|
Chris@0
|
1783 static QPixmap *tmpPixmap = 0;
|
Chris@0
|
1784 if (!tmpPixmap ||
|
Chris@0
|
1785 tmpPixmap->width() != m_pixmapCache->width() ||
|
Chris@0
|
1786 tmpPixmap->height() != m_pixmapCache->height()) {
|
Chris@0
|
1787 delete tmpPixmap;
|
Chris@0
|
1788 tmpPixmap = new QPixmap(m_pixmapCache->width(),
|
Chris@0
|
1789 m_pixmapCache->height());
|
Chris@0
|
1790 }
|
Chris@0
|
1791 QPainter cachePainter;
|
Chris@0
|
1792 cachePainter.begin(tmpPixmap);
|
Chris@0
|
1793 cachePainter.drawPixmap(0, 0, *m_pixmapCache);
|
Chris@0
|
1794 cachePainter.end();
|
Chris@0
|
1795 cachePainter.begin(m_pixmapCache);
|
Chris@0
|
1796 cachePainter.drawPixmap(dx, 0, *tmpPixmap);
|
Chris@0
|
1797 cachePainter.end();
|
Chris@0
|
1798 #else
|
Chris@0
|
1799 QPainter cachePainter(m_pixmapCache);
|
Chris@0
|
1800 cachePainter.drawPixmap(dx, 0, *m_pixmapCache);
|
Chris@0
|
1801 cachePainter.end();
|
Chris@0
|
1802 #endif
|
Chris@0
|
1803
|
Chris@0
|
1804 paint.drawPixmap(rect, *m_pixmapCache, rect);
|
Chris@0
|
1805
|
Chris@0
|
1806 if (dx < 0) {
|
Chris@0
|
1807 x0 = m_pixmapCache->width() + dx;
|
Chris@0
|
1808 x1 = m_pixmapCache->width();
|
Chris@0
|
1809 } else {
|
Chris@0
|
1810 x0 = 0;
|
Chris@0
|
1811 x1 = dx;
|
Chris@0
|
1812 }
|
Chris@0
|
1813 }
|
Chris@0
|
1814 }
|
Chris@0
|
1815 } else {
|
Chris@0
|
1816 #ifdef DEBUG_SPECTROGRAM_REPAINT
|
Chris@0
|
1817 std::cerr << "SpectrogramLayer: pixmap cache useless" << std::endl;
|
Chris@0
|
1818 #endif
|
Chris@0
|
1819 }
|
Chris@0
|
1820 }
|
Chris@0
|
1821
|
Chris@0
|
1822 if (stillCacheing) {
|
Chris@0
|
1823 x0 = rect.left();
|
Chris@0
|
1824 x1 = rect.right() + 1;
|
Chris@0
|
1825 y0 = rect.top();
|
Chris@0
|
1826 y1 = rect.bottom() + 1;
|
Chris@0
|
1827 }
|
Chris@0
|
1828
|
Chris@0
|
1829 int w = x1 - x0;
|
Chris@0
|
1830 int h = y1 - y0;
|
Chris@0
|
1831
|
Chris@0
|
1832 // std::cerr << "x0 " << x0 << ", x1 " << x1 << ", w " << w << ", h " << h << std::endl;
|
Chris@0
|
1833
|
Chris@0
|
1834 QImage scaled(w, h, QImage::Format_RGB32);
|
Chris@41
|
1835 scaled.fill(m_cache->getColour(0).rgb());
|
Chris@35
|
1836
|
Chris@35
|
1837 float ymag[h];
|
Chris@35
|
1838 float ydiv[h];
|
Chris@37
|
1839
|
Chris@37
|
1840 int sr = m_model->getSampleRate();
|
Chris@35
|
1841
|
Chris@35
|
1842 size_t bins = m_windowSize / 2;
|
Chris@35
|
1843 if (m_maxFrequency > 0) {
|
Chris@35
|
1844 bins = int((double(m_maxFrequency) * m_windowSize) / sr + 0.1);
|
Chris@35
|
1845 if (bins > m_windowSize / 2) bins = m_windowSize / 2;
|
Chris@35
|
1846 }
|
Chris@35
|
1847
|
Chris@40
|
1848 size_t minbin = 1;
|
Chris@37
|
1849 if (m_minFrequency > 0) {
|
Chris@37
|
1850 minbin = int((double(m_minFrequency) * m_windowSize) / sr + 0.1);
|
Chris@40
|
1851 if (minbin < 1) minbin = 1;
|
Chris@37
|
1852 if (minbin >= bins) minbin = bins - 1;
|
Chris@37
|
1853 }
|
Chris@37
|
1854
|
Chris@37
|
1855 float minFreq = (float(minbin) * sr) / m_windowSize;
|
Chris@35
|
1856 float maxFreq = (float(bins) * sr) / m_windowSize;
|
Chris@0
|
1857
|
Chris@38
|
1858 size_t increment = getWindowIncrement();
|
Chris@40
|
1859
|
Chris@40
|
1860 bool logarithmic = (m_frequencyScale == LogFrequencyScale);
|
Chris@38
|
1861
|
Chris@0
|
1862 m_mutex.unlock();
|
Chris@0
|
1863
|
Chris@35
|
1864 for (int x = 0; x < w; ++x) {
|
Chris@35
|
1865
|
Chris@35
|
1866 m_mutex.lock();
|
Chris@35
|
1867 if (m_cacheInvalid) {
|
Chris@35
|
1868 m_mutex.unlock();
|
Chris@35
|
1869 break;
|
Chris@35
|
1870 }
|
Chris@35
|
1871
|
Chris@35
|
1872 for (int y = 0; y < h; ++y) {
|
Chris@40
|
1873 ymag[y] = 0.0;
|
Chris@40
|
1874 ydiv[y] = 0.0;
|
Chris@35
|
1875 }
|
Chris@35
|
1876
|
Chris@35
|
1877 float s0 = 0, s1 = 0;
|
Chris@35
|
1878
|
Chris@44
|
1879 if (!getXBinRange(v, x0 + x, s0, s1)) {
|
Chris@35
|
1880 assert(x <= scaled.width());
|
Chris@35
|
1881 m_mutex.unlock();
|
Chris@35
|
1882 continue;
|
Chris@35
|
1883 }
|
Chris@35
|
1884
|
Chris@35
|
1885 int s0i = int(s0 + 0.001);
|
Chris@35
|
1886 int s1i = int(s1);
|
Chris@35
|
1887
|
Chris@45
|
1888 if (s1i >= m_cache->getWidth()) {
|
Chris@45
|
1889 if (s0i >= m_cache->getWidth()) {
|
Chris@45
|
1890 m_mutex.unlock();
|
Chris@45
|
1891 continue;
|
Chris@45
|
1892 } else {
|
Chris@45
|
1893 s1i = s0i;
|
Chris@45
|
1894 }
|
Chris@45
|
1895 }
|
Chris@45
|
1896
|
Chris@38
|
1897 for (size_t q = minbin; q < bins; ++q) {
|
Chris@35
|
1898
|
Chris@40
|
1899 float f0 = (float(q) * sr) / m_windowSize;
|
Chris@40
|
1900 float f1 = (float(q + 1) * sr) / m_windowSize;
|
Chris@40
|
1901
|
Chris@40
|
1902 float y0 = 0, y1 = 0;
|
Chris@40
|
1903
|
Chris@45
|
1904 if (m_binDisplay != PeakFrequencies) {
|
Chris@44
|
1905 y0 = v->getYForFrequency(f1, minFreq, maxFreq, logarithmic);
|
Chris@44
|
1906 y1 = v->getYForFrequency(f0, minFreq, maxFreq, logarithmic);
|
Chris@40
|
1907 }
|
Chris@40
|
1908
|
Chris@35
|
1909 for (int s = s0i; s <= s1i; ++s) {
|
Chris@35
|
1910
|
Chris@40
|
1911 if (m_binDisplay == PeakBins ||
|
Chris@40
|
1912 m_binDisplay == PeakFrequencies) {
|
Chris@40
|
1913 if (!m_cache->isLocalPeak(s, q)) continue;
|
Chris@40
|
1914 }
|
Chris@40
|
1915
|
Chris@40
|
1916 if (!m_cache->isOverThreshold(s, q, m_threshold)) continue;
|
Chris@40
|
1917
|
Chris@35
|
1918 float sprop = 1.0;
|
Chris@35
|
1919 if (s == s0i) sprop *= (s + 1) - s0;
|
Chris@35
|
1920 if (s == s1i) sprop *= s1 - s;
|
Chris@35
|
1921
|
Chris@38
|
1922 if (m_binDisplay == PeakFrequencies &&
|
Chris@40
|
1923 s < int(m_cache->getWidth()) - 1) {
|
Chris@35
|
1924
|
Chris@38
|
1925 bool steady = false;
|
Chris@38
|
1926 f0 = f1 = calculateFrequency(q,
|
Chris@38
|
1927 m_windowSize,
|
Chris@38
|
1928 increment,
|
Chris@38
|
1929 sr,
|
Chris@38
|
1930 m_cache->getPhaseAt(s, q),
|
Chris@38
|
1931 m_cache->getPhaseAt(s+1, q),
|
Chris@38
|
1932 steady);
|
Chris@40
|
1933
|
Chris@44
|
1934 y0 = y1 = v->getYForFrequency
|
Chris@40
|
1935 (f0, minFreq, maxFreq, logarithmic);
|
Chris@35
|
1936 }
|
Chris@38
|
1937
|
Chris@35
|
1938 int y0i = int(y0 + 0.001);
|
Chris@35
|
1939 int y1i = int(y1);
|
Chris@35
|
1940
|
Chris@35
|
1941 for (int y = y0i; y <= y1i; ++y) {
|
Chris@35
|
1942
|
Chris@35
|
1943 if (y < 0 || y >= h) continue;
|
Chris@35
|
1944
|
Chris@35
|
1945 float yprop = sprop;
|
Chris@35
|
1946 if (y == y0i) yprop *= (y + 1) - y0;
|
Chris@35
|
1947 if (y == y1i) yprop *= y1 - y;
|
Chris@37
|
1948
|
Chris@38
|
1949 float value;
|
Chris@38
|
1950
|
Chris@38
|
1951 if (m_colourScale == PhaseColourScale) {
|
Chris@38
|
1952 value = m_cache->getPhaseAt(s, q);
|
Chris@38
|
1953 } else if (m_normalizeColumns) {
|
Chris@38
|
1954 value = m_cache->getNormalizedMagnitudeAt(s, q) * m_gain;
|
Chris@38
|
1955 } else {
|
Chris@38
|
1956 value = m_cache->getMagnitudeAt(s, q) * m_gain;
|
Chris@38
|
1957 }
|
Chris@37
|
1958
|
Chris@37
|
1959 ymag[y] += yprop * value;
|
Chris@35
|
1960 ydiv[y] += yprop;
|
Chris@35
|
1961 }
|
Chris@35
|
1962 }
|
Chris@35
|
1963 }
|
Chris@35
|
1964
|
Chris@35
|
1965 for (int y = 0; y < h; ++y) {
|
Chris@35
|
1966
|
Chris@35
|
1967 if (ydiv[y] > 0.0) {
|
Chris@40
|
1968
|
Chris@40
|
1969 unsigned char pixel = 0;
|
Chris@40
|
1970
|
Chris@38
|
1971 float avg = ymag[y] / ydiv[y];
|
Chris@38
|
1972 pixel = getDisplayValue(avg);
|
Chris@40
|
1973
|
Chris@40
|
1974 assert(x <= scaled.width());
|
Chris@40
|
1975 QColor c = m_cache->getColour(pixel);
|
Chris@40
|
1976 scaled.setPixel(x, y,
|
Chris@40
|
1977 qRgb(c.red(), c.green(), c.blue()));
|
Chris@35
|
1978 }
|
Chris@35
|
1979 }
|
Chris@35
|
1980
|
Chris@35
|
1981 m_mutex.unlock();
|
Chris@35
|
1982 }
|
Chris@35
|
1983
|
Chris@0
|
1984 paint.drawImage(x0, y0, scaled);
|
Chris@0
|
1985
|
Chris@0
|
1986 if (recreateWholePixmapCache) {
|
Chris@0
|
1987 delete m_pixmapCache;
|
Chris@0
|
1988 m_pixmapCache = new QPixmap(w, h);
|
Chris@0
|
1989 }
|
Chris@0
|
1990
|
Chris@0
|
1991 QPainter cachePainter(m_pixmapCache);
|
Chris@0
|
1992 cachePainter.drawImage(x0, y0, scaled);
|
Chris@0
|
1993 cachePainter.end();
|
Chris@0
|
1994
|
Chris@0
|
1995 m_pixmapCacheInvalid = false;
|
Chris@0
|
1996 m_pixmapCacheStartFrame = startFrame;
|
Chris@0
|
1997 m_pixmapCacheZoomLevel = zoomLevel;
|
Chris@0
|
1998
|
Chris@0
|
1999 #ifdef DEBUG_SPECTROGRAM_REPAINT
|
Chris@0
|
2000 std::cerr << "SpectrogramLayer::paint() returning" << std::endl;
|
Chris@0
|
2001 #endif
|
Chris@0
|
2002 }
|
Chris@0
|
2003
|
Chris@42
|
2004 float
|
Chris@44
|
2005 SpectrogramLayer::getYForFrequency(View *v, float frequency) const
|
Chris@42
|
2006 {
|
Chris@44
|
2007 return v->getYForFrequency(frequency,
|
Chris@44
|
2008 getEffectiveMinFrequency(),
|
Chris@44
|
2009 getEffectiveMaxFrequency(),
|
Chris@44
|
2010 m_frequencyScale == LogFrequencyScale);
|
Chris@42
|
2011 }
|
Chris@42
|
2012
|
Chris@42
|
2013 float
|
Chris@44
|
2014 SpectrogramLayer::getFrequencyForY(View *v, int y) const
|
Chris@42
|
2015 {
|
Chris@44
|
2016 return v->getFrequencyForY(y,
|
Chris@44
|
2017 getEffectiveMinFrequency(),
|
Chris@44
|
2018 getEffectiveMaxFrequency(),
|
Chris@44
|
2019 m_frequencyScale == LogFrequencyScale);
|
Chris@42
|
2020 }
|
Chris@42
|
2021
|
Chris@0
|
2022 int
|
Chris@0
|
2023 SpectrogramLayer::getCompletion() const
|
Chris@0
|
2024 {
|
Chris@0
|
2025 if (m_updateTimer == 0) return 100;
|
Chris@0
|
2026 size_t completion = m_fillThread->getFillCompletion();
|
Chris@0
|
2027 // std::cerr << "SpectrogramLayer::getCompletion: completion = " << completion << std::endl;
|
Chris@0
|
2028 return completion;
|
Chris@0
|
2029 }
|
Chris@0
|
2030
|
Chris@28
|
2031 bool
|
Chris@44
|
2032 SpectrogramLayer::snapToFeatureFrame(View *v, int &frame,
|
Chris@28
|
2033 size_t &resolution,
|
Chris@28
|
2034 SnapType snap) const
|
Chris@13
|
2035 {
|
Chris@13
|
2036 resolution = getWindowIncrement();
|
Chris@28
|
2037 int left = (frame / resolution) * resolution;
|
Chris@28
|
2038 int right = left + resolution;
|
Chris@28
|
2039
|
Chris@28
|
2040 switch (snap) {
|
Chris@28
|
2041 case SnapLeft: frame = left; break;
|
Chris@28
|
2042 case SnapRight: frame = right; break;
|
Chris@28
|
2043 case SnapNearest:
|
Chris@28
|
2044 case SnapNeighbouring:
|
Chris@28
|
2045 if (frame - left > right - frame) frame = right;
|
Chris@28
|
2046 else frame = left;
|
Chris@28
|
2047 break;
|
Chris@28
|
2048 }
|
Chris@28
|
2049
|
Chris@28
|
2050 return true;
|
Chris@28
|
2051 }
|
Chris@13
|
2052
|
Chris@25
|
2053 QString
|
Chris@44
|
2054 SpectrogramLayer::getFeatureDescription(View *v, QPoint &pos) const
|
Chris@25
|
2055 {
|
Chris@25
|
2056 int x = pos.x();
|
Chris@25
|
2057 int y = pos.y();
|
Chris@0
|
2058
|
Chris@25
|
2059 if (!m_model || !m_model->isOK()) return "";
|
Chris@0
|
2060
|
Chris@38
|
2061 float magMin = 0, magMax = 0;
|
Chris@38
|
2062 float phaseMin = 0, phaseMax = 0;
|
Chris@0
|
2063 float freqMin = 0, freqMax = 0;
|
Chris@35
|
2064 float adjFreqMin = 0, adjFreqMax = 0;
|
Chris@25
|
2065 QString pitchMin, pitchMax;
|
Chris@0
|
2066 RealTime rtMin, rtMax;
|
Chris@0
|
2067
|
Chris@38
|
2068 bool haveValues = false;
|
Chris@0
|
2069
|
Chris@44
|
2070 if (!getXBinSourceRange(v, x, rtMin, rtMax)) {
|
Chris@38
|
2071 return "";
|
Chris@38
|
2072 }
|
Chris@44
|
2073 if (getXYBinSourceRange(v, x, y, magMin, magMax, phaseMin, phaseMax)) {
|
Chris@38
|
2074 haveValues = true;
|
Chris@38
|
2075 }
|
Chris@0
|
2076
|
Chris@35
|
2077 QString adjFreqText = "", adjPitchText = "";
|
Chris@35
|
2078
|
Chris@38
|
2079 if (m_binDisplay == PeakFrequencies) {
|
Chris@35
|
2080
|
Chris@44
|
2081 if (!getAdjustedYBinSourceRange(v, x, y, freqMin, freqMax,
|
Chris@38
|
2082 adjFreqMin, adjFreqMax)) {
|
Chris@38
|
2083 return "";
|
Chris@38
|
2084 }
|
Chris@35
|
2085
|
Chris@35
|
2086 if (adjFreqMin != adjFreqMax) {
|
Chris@35
|
2087 adjFreqText = tr("Adjusted Frequency:\t%1 - %2 Hz\n")
|
Chris@35
|
2088 .arg(adjFreqMin).arg(adjFreqMax);
|
Chris@35
|
2089 } else {
|
Chris@35
|
2090 adjFreqText = tr("Adjusted Frequency:\t%1 Hz\n")
|
Chris@35
|
2091 .arg(adjFreqMin);
|
Chris@38
|
2092 }
|
Chris@38
|
2093
|
Chris@38
|
2094 QString pmin = Pitch::getPitchLabelForFrequency(adjFreqMin);
|
Chris@38
|
2095 QString pmax = Pitch::getPitchLabelForFrequency(adjFreqMax);
|
Chris@38
|
2096
|
Chris@38
|
2097 if (pmin != pmax) {
|
Chris@38
|
2098 adjPitchText = tr("Adjusted Pitch:\t%3 - %4\n").arg(pmin).arg(pmax);
|
Chris@38
|
2099 } else {
|
Chris@38
|
2100 adjPitchText = tr("Adjusted Pitch:\t%2\n").arg(pmin);
|
Chris@35
|
2101 }
|
Chris@35
|
2102
|
Chris@35
|
2103 } else {
|
Chris@35
|
2104
|
Chris@44
|
2105 if (!getYBinSourceRange(v, y, freqMin, freqMax)) return "";
|
Chris@35
|
2106 }
|
Chris@35
|
2107
|
Chris@25
|
2108 QString text;
|
Chris@25
|
2109
|
Chris@25
|
2110 if (rtMin != rtMax) {
|
Chris@25
|
2111 text += tr("Time:\t%1 - %2\n")
|
Chris@25
|
2112 .arg(rtMin.toText(true).c_str())
|
Chris@25
|
2113 .arg(rtMax.toText(true).c_str());
|
Chris@25
|
2114 } else {
|
Chris@25
|
2115 text += tr("Time:\t%1\n")
|
Chris@25
|
2116 .arg(rtMin.toText(true).c_str());
|
Chris@0
|
2117 }
|
Chris@0
|
2118
|
Chris@25
|
2119 if (freqMin != freqMax) {
|
Chris@35
|
2120 text += tr("Frequency:\t%1 - %2 Hz\n%3Pitch:\t%4 - %5\n%6")
|
Chris@25
|
2121 .arg(freqMin)
|
Chris@25
|
2122 .arg(freqMax)
|
Chris@35
|
2123 .arg(adjFreqText)
|
Chris@25
|
2124 .arg(Pitch::getPitchLabelForFrequency(freqMin))
|
Chris@35
|
2125 .arg(Pitch::getPitchLabelForFrequency(freqMax))
|
Chris@35
|
2126 .arg(adjPitchText);
|
Chris@25
|
2127 } else {
|
Chris@35
|
2128 text += tr("Frequency:\t%1 Hz\n%2Pitch:\t%3\n%4")
|
Chris@25
|
2129 .arg(freqMin)
|
Chris@35
|
2130 .arg(adjFreqText)
|
Chris@35
|
2131 .arg(Pitch::getPitchLabelForFrequency(freqMin))
|
Chris@35
|
2132 .arg(adjPitchText);
|
Chris@25
|
2133 }
|
Chris@25
|
2134
|
Chris@38
|
2135 if (haveValues) {
|
Chris@38
|
2136 float dbMin = AudioLevel::multiplier_to_dB(magMin);
|
Chris@38
|
2137 float dbMax = AudioLevel::multiplier_to_dB(magMax);
|
Chris@43
|
2138 QString dbMinString;
|
Chris@43
|
2139 QString dbMaxString;
|
Chris@43
|
2140 if (dbMin == AudioLevel::DB_FLOOR) {
|
Chris@43
|
2141 dbMinString = tr("-Inf");
|
Chris@43
|
2142 } else {
|
Chris@43
|
2143 dbMinString = QString("%1").arg(lrintf(dbMin));
|
Chris@43
|
2144 }
|
Chris@43
|
2145 if (dbMax == AudioLevel::DB_FLOOR) {
|
Chris@43
|
2146 dbMaxString = tr("-Inf");
|
Chris@43
|
2147 } else {
|
Chris@43
|
2148 dbMaxString = QString("%1").arg(lrintf(dbMax));
|
Chris@43
|
2149 }
|
Chris@25
|
2150 if (lrintf(dbMin) != lrintf(dbMax)) {
|
Chris@25
|
2151 text += tr("dB:\t%1 - %2").arg(lrintf(dbMin)).arg(lrintf(dbMax));
|
Chris@25
|
2152 } else {
|
Chris@25
|
2153 text += tr("dB:\t%1").arg(lrintf(dbMin));
|
Chris@25
|
2154 }
|
Chris@38
|
2155 if (phaseMin != phaseMax) {
|
Chris@38
|
2156 text += tr("\nPhase:\t%1 - %2").arg(phaseMin).arg(phaseMax);
|
Chris@38
|
2157 } else {
|
Chris@38
|
2158 text += tr("\nPhase:\t%1").arg(phaseMin);
|
Chris@38
|
2159 }
|
Chris@25
|
2160 }
|
Chris@25
|
2161
|
Chris@25
|
2162 return text;
|
Chris@0
|
2163 }
|
Chris@25
|
2164
|
Chris@0
|
2165 int
|
Chris@40
|
2166 SpectrogramLayer::getColourScaleWidth(QPainter &paint) const
|
Chris@40
|
2167 {
|
Chris@40
|
2168 int cw;
|
Chris@40
|
2169
|
Chris@40
|
2170 switch (m_colourScale) {
|
Chris@40
|
2171 default:
|
Chris@40
|
2172 case LinearColourScale:
|
Chris@40
|
2173 cw = paint.fontMetrics().width(QString("0.00"));
|
Chris@40
|
2174 break;
|
Chris@40
|
2175
|
Chris@40
|
2176 case MeterColourScale:
|
Chris@40
|
2177 case dBColourScale:
|
Chris@40
|
2178 cw = std::max(paint.fontMetrics().width(tr("-Inf")),
|
Chris@40
|
2179 paint.fontMetrics().width(tr("-90")));
|
Chris@40
|
2180 break;
|
Chris@40
|
2181
|
Chris@40
|
2182 case PhaseColourScale:
|
Chris@40
|
2183 cw = paint.fontMetrics().width(QString("-") + QChar(0x3c0));
|
Chris@40
|
2184 break;
|
Chris@40
|
2185 }
|
Chris@40
|
2186
|
Chris@40
|
2187 return cw;
|
Chris@40
|
2188 }
|
Chris@40
|
2189
|
Chris@40
|
2190 int
|
Chris@44
|
2191 SpectrogramLayer::getVerticalScaleWidth(View *v, QPainter &paint) const
|
Chris@0
|
2192 {
|
Chris@0
|
2193 if (!m_model || !m_model->isOK()) return 0;
|
Chris@0
|
2194
|
Chris@40
|
2195 int cw = getColourScaleWidth(paint);
|
Chris@40
|
2196
|
Chris@0
|
2197 int tw = paint.fontMetrics().width(QString("%1")
|
Chris@0
|
2198 .arg(m_maxFrequency > 0 ?
|
Chris@0
|
2199 m_maxFrequency - 1 :
|
Chris@0
|
2200 m_model->getSampleRate() / 2));
|
Chris@0
|
2201
|
Chris@0
|
2202 int fw = paint.fontMetrics().width(QString("43Hz"));
|
Chris@0
|
2203 if (tw < fw) tw = fw;
|
Chris@40
|
2204
|
Chris@40
|
2205 int tickw = (m_frequencyScale == LogFrequencyScale ? 10 : 4);
|
Chris@0
|
2206
|
Chris@40
|
2207 return cw + tickw + tw + 13;
|
Chris@0
|
2208 }
|
Chris@0
|
2209
|
Chris@0
|
2210 void
|
Chris@44
|
2211 SpectrogramLayer::paintVerticalScale(View *v, QPainter &paint, QRect rect) const
|
Chris@0
|
2212 {
|
Chris@0
|
2213 if (!m_model || !m_model->isOK()) {
|
Chris@0
|
2214 return;
|
Chris@0
|
2215 }
|
Chris@0
|
2216
|
Chris@0
|
2217 int h = rect.height(), w = rect.width();
|
Chris@0
|
2218
|
Chris@40
|
2219 int tickw = (m_frequencyScale == LogFrequencyScale ? 10 : 4);
|
Chris@40
|
2220 int pkw = (m_frequencyScale == LogFrequencyScale ? 10 : 0);
|
Chris@40
|
2221
|
Chris@0
|
2222 size_t bins = m_windowSize / 2;
|
Chris@0
|
2223 int sr = m_model->getSampleRate();
|
Chris@0
|
2224
|
Chris@0
|
2225 if (m_maxFrequency > 0) {
|
Chris@0
|
2226 bins = int((double(m_maxFrequency) * m_windowSize) / sr + 0.1);
|
Chris@0
|
2227 if (bins > m_windowSize / 2) bins = m_windowSize / 2;
|
Chris@0
|
2228 }
|
Chris@0
|
2229
|
Chris@40
|
2230 int cw = getColourScaleWidth(paint);
|
Chris@40
|
2231
|
Chris@0
|
2232 int py = -1;
|
Chris@0
|
2233 int textHeight = paint.fontMetrics().height();
|
Chris@0
|
2234 int toff = -textHeight + paint.fontMetrics().ascent() + 2;
|
Chris@0
|
2235
|
Chris@40
|
2236 if (m_cache && !m_cacheInvalid && h > textHeight * 2 + 10) { //!!! lock?
|
Chris@40
|
2237
|
Chris@40
|
2238 int ch = h - textHeight * 2 - 8;
|
Chris@40
|
2239 paint.drawRect(4, textHeight + 4, cw - 1, ch + 1);
|
Chris@40
|
2240
|
Chris@40
|
2241 QString top, bottom;
|
Chris@40
|
2242
|
Chris@40
|
2243 switch (m_colourScale) {
|
Chris@40
|
2244 default:
|
Chris@40
|
2245 case LinearColourScale:
|
Chris@40
|
2246 top = (m_normalizeColumns ? "1.0" : "0.02");
|
Chris@40
|
2247 bottom = (m_normalizeColumns ? "0.0" : "0.00");
|
Chris@40
|
2248 break;
|
Chris@40
|
2249
|
Chris@40
|
2250 case MeterColourScale:
|
Chris@40
|
2251 top = (m_normalizeColumns ? QString("0") :
|
Chris@40
|
2252 QString("%1").arg(int(AudioLevel::multiplier_to_dB(0.02))));
|
Chris@40
|
2253 bottom = QString("%1").
|
Chris@40
|
2254 arg(int(AudioLevel::multiplier_to_dB
|
Chris@40
|
2255 (AudioLevel::preview_to_multiplier(0, 255))));
|
Chris@40
|
2256 break;
|
Chris@40
|
2257
|
Chris@40
|
2258 case dBColourScale:
|
Chris@40
|
2259 top = "0";
|
Chris@40
|
2260 bottom = "-80";
|
Chris@40
|
2261 break;
|
Chris@40
|
2262
|
Chris@40
|
2263 case PhaseColourScale:
|
Chris@40
|
2264 top = QChar(0x3c0);
|
Chris@40
|
2265 bottom = "-" + top;
|
Chris@40
|
2266 break;
|
Chris@40
|
2267 }
|
Chris@40
|
2268
|
Chris@40
|
2269 paint.drawText((cw + 6 - paint.fontMetrics().width(top)) / 2,
|
Chris@40
|
2270 2 + textHeight + toff, top);
|
Chris@40
|
2271
|
Chris@40
|
2272 paint.drawText((cw + 6 - paint.fontMetrics().width(bottom)) / 2,
|
Chris@40
|
2273 h + toff - 3, bottom);
|
Chris@40
|
2274
|
Chris@40
|
2275 paint.save();
|
Chris@40
|
2276 paint.setBrush(Qt::NoBrush);
|
Chris@40
|
2277 for (int i = 0; i < ch; ++i) {
|
Chris@40
|
2278 int v = (i * 255) / ch + 1;
|
Chris@40
|
2279 paint.setPen(m_cache->getColour(v));
|
Chris@40
|
2280 paint.drawLine(5, 4 + textHeight + ch - i,
|
Chris@40
|
2281 cw + 2, 4 + textHeight + ch - i);
|
Chris@40
|
2282 }
|
Chris@40
|
2283 paint.restore();
|
Chris@40
|
2284 }
|
Chris@40
|
2285
|
Chris@40
|
2286 paint.drawLine(cw + 7, 0, cw + 7, h);
|
Chris@40
|
2287
|
Chris@0
|
2288 int bin = -1;
|
Chris@0
|
2289
|
Chris@44
|
2290 for (int y = 0; y < v->height(); ++y) {
|
Chris@0
|
2291
|
Chris@0
|
2292 float q0, q1;
|
Chris@44
|
2293 if (!getYBinRange(v, v->height() - y, q0, q1)) continue;
|
Chris@0
|
2294
|
Chris@0
|
2295 int vy;
|
Chris@0
|
2296
|
Chris@0
|
2297 if (int(q0) > bin) {
|
Chris@0
|
2298 vy = y;
|
Chris@0
|
2299 bin = int(q0);
|
Chris@0
|
2300 } else {
|
Chris@0
|
2301 continue;
|
Chris@0
|
2302 }
|
Chris@0
|
2303
|
Chris@40
|
2304 int freq = (sr * bin) / m_windowSize;
|
Chris@0
|
2305
|
Chris@0
|
2306 if (py >= 0 && (vy - py) < textHeight - 1) {
|
Chris@40
|
2307 if (m_frequencyScale == LinearFrequencyScale) {
|
Chris@40
|
2308 paint.drawLine(w - tickw, h - vy, w, h - vy);
|
Chris@40
|
2309 }
|
Chris@0
|
2310 continue;
|
Chris@0
|
2311 }
|
Chris@0
|
2312
|
Chris@0
|
2313 QString text = QString("%1").arg(freq);
|
Chris@40
|
2314 if (bin == 1) text = QString("%1Hz").arg(freq); // bin 0 is DC
|
Chris@40
|
2315 paint.drawLine(cw + 7, h - vy, w - pkw - 1, h - vy);
|
Chris@0
|
2316
|
Chris@0
|
2317 if (h - vy - textHeight >= -2) {
|
Chris@40
|
2318 int tx = w - 3 - paint.fontMetrics().width(text) - std::max(tickw, pkw);
|
Chris@0
|
2319 paint.drawText(tx, h - vy + toff, text);
|
Chris@0
|
2320 }
|
Chris@0
|
2321
|
Chris@0
|
2322 py = vy;
|
Chris@0
|
2323 }
|
Chris@40
|
2324
|
Chris@40
|
2325 if (m_frequencyScale == LogFrequencyScale) {
|
Chris@40
|
2326
|
Chris@40
|
2327 paint.drawLine(w - pkw - 1, 0, w - pkw - 1, h);
|
Chris@40
|
2328
|
Chris@40
|
2329 int sr = m_model->getSampleRate();//!!! lock?
|
Chris@40
|
2330 float minf = getEffectiveMinFrequency();
|
Chris@40
|
2331 float maxf = getEffectiveMaxFrequency();
|
Chris@40
|
2332
|
Chris@40
|
2333 int py = h;
|
Chris@40
|
2334 paint.setBrush(paint.pen().color());
|
Chris@40
|
2335
|
Chris@40
|
2336 for (int i = 0; i < 128; ++i) {
|
Chris@40
|
2337
|
Chris@40
|
2338 float f = Pitch::getFrequencyForPitch(i);
|
Chris@44
|
2339 int y = lrintf(v->getYForFrequency(f, minf, maxf, true));
|
Chris@40
|
2340 int n = (i % 12);
|
Chris@40
|
2341 if (n == 1 || n == 3 || n == 6 || n == 8 || n == 10) {
|
Chris@40
|
2342 // black notes
|
Chris@40
|
2343 paint.drawLine(w - pkw, y, w, y);
|
Chris@41
|
2344 int rh = ((py - y) / 4) * 2;
|
Chris@41
|
2345 if (rh < 2) rh = 2;
|
Chris@41
|
2346 paint.drawRect(w - pkw, y - (py-y)/4, pkw/2, rh);
|
Chris@40
|
2347 } else if (n == 0 || n == 5) {
|
Chris@40
|
2348 // C, A
|
Chris@40
|
2349 if (py < h) {
|
Chris@40
|
2350 paint.drawLine(w - pkw, (y + py) / 2, w, (y + py) / 2);
|
Chris@40
|
2351 }
|
Chris@40
|
2352 }
|
Chris@40
|
2353
|
Chris@40
|
2354 py = y;
|
Chris@40
|
2355 }
|
Chris@40
|
2356 }
|
Chris@0
|
2357 }
|
Chris@0
|
2358
|
Chris@6
|
2359 QString
|
Chris@6
|
2360 SpectrogramLayer::toXmlString(QString indent, QString extraAttributes) const
|
Chris@6
|
2361 {
|
Chris@6
|
2362 QString s;
|
Chris@6
|
2363
|
Chris@6
|
2364 s += QString("channel=\"%1\" "
|
Chris@6
|
2365 "windowSize=\"%2\" "
|
Chris@6
|
2366 "windowType=\"%3\" "
|
Chris@6
|
2367 "windowOverlap=\"%4\" "
|
Chris@37
|
2368 "gain=\"%5\" "
|
Chris@37
|
2369 "threshold=\"%6\" ")
|
Chris@6
|
2370 .arg(m_channel)
|
Chris@6
|
2371 .arg(m_windowSize)
|
Chris@6
|
2372 .arg(m_windowType)
|
Chris@6
|
2373 .arg(m_windowOverlap)
|
Chris@37
|
2374 .arg(m_gain)
|
Chris@37
|
2375 .arg(m_threshold);
|
Chris@37
|
2376
|
Chris@37
|
2377 s += QString("minFrequency=\"%1\" "
|
Chris@37
|
2378 "maxFrequency=\"%2\" "
|
Chris@37
|
2379 "colourScale=\"%3\" "
|
Chris@37
|
2380 "colourScheme=\"%4\" "
|
Chris@37
|
2381 "colourRotation=\"%5\" "
|
Chris@37
|
2382 "frequencyScale=\"%6\" "
|
Chris@37
|
2383 "binDisplay=\"%7\" "
|
Chris@37
|
2384 "normalizeColumns=\"%8\"")
|
Chris@37
|
2385 .arg(m_minFrequency)
|
Chris@6
|
2386 .arg(m_maxFrequency)
|
Chris@6
|
2387 .arg(m_colourScale)
|
Chris@6
|
2388 .arg(m_colourScheme)
|
Chris@37
|
2389 .arg(m_colourRotation)
|
Chris@35
|
2390 .arg(m_frequencyScale)
|
Chris@37
|
2391 .arg(m_binDisplay)
|
Chris@36
|
2392 .arg(m_normalizeColumns ? "true" : "false");
|
Chris@6
|
2393
|
Chris@6
|
2394 return Layer::toXmlString(indent, extraAttributes + " " + s);
|
Chris@6
|
2395 }
|
Chris@6
|
2396
|
Chris@11
|
2397 void
|
Chris@11
|
2398 SpectrogramLayer::setProperties(const QXmlAttributes &attributes)
|
Chris@11
|
2399 {
|
Chris@11
|
2400 bool ok = false;
|
Chris@11
|
2401
|
Chris@11
|
2402 int channel = attributes.value("channel").toInt(&ok);
|
Chris@11
|
2403 if (ok) setChannel(channel);
|
Chris@11
|
2404
|
Chris@11
|
2405 size_t windowSize = attributes.value("windowSize").toUInt(&ok);
|
Chris@11
|
2406 if (ok) setWindowSize(windowSize);
|
Chris@11
|
2407
|
Chris@11
|
2408 WindowType windowType = (WindowType)
|
Chris@11
|
2409 attributes.value("windowType").toInt(&ok);
|
Chris@11
|
2410 if (ok) setWindowType(windowType);
|
Chris@11
|
2411
|
Chris@11
|
2412 size_t windowOverlap = attributes.value("windowOverlap").toUInt(&ok);
|
Chris@11
|
2413 if (ok) setWindowOverlap(windowOverlap);
|
Chris@11
|
2414
|
Chris@11
|
2415 float gain = attributes.value("gain").toFloat(&ok);
|
Chris@11
|
2416 if (ok) setGain(gain);
|
Chris@11
|
2417
|
Chris@37
|
2418 float threshold = attributes.value("threshold").toFloat(&ok);
|
Chris@37
|
2419 if (ok) setThreshold(threshold);
|
Chris@37
|
2420
|
Chris@37
|
2421 size_t minFrequency = attributes.value("minFrequency").toUInt(&ok);
|
Chris@37
|
2422 if (ok) setMinFrequency(minFrequency);
|
Chris@37
|
2423
|
Chris@11
|
2424 size_t maxFrequency = attributes.value("maxFrequency").toUInt(&ok);
|
Chris@11
|
2425 if (ok) setMaxFrequency(maxFrequency);
|
Chris@11
|
2426
|
Chris@11
|
2427 ColourScale colourScale = (ColourScale)
|
Chris@11
|
2428 attributes.value("colourScale").toInt(&ok);
|
Chris@11
|
2429 if (ok) setColourScale(colourScale);
|
Chris@11
|
2430
|
Chris@11
|
2431 ColourScheme colourScheme = (ColourScheme)
|
Chris@11
|
2432 attributes.value("colourScheme").toInt(&ok);
|
Chris@11
|
2433 if (ok) setColourScheme(colourScheme);
|
Chris@11
|
2434
|
Chris@37
|
2435 int colourRotation = attributes.value("colourRotation").toInt(&ok);
|
Chris@37
|
2436 if (ok) setColourRotation(colourRotation);
|
Chris@37
|
2437
|
Chris@11
|
2438 FrequencyScale frequencyScale = (FrequencyScale)
|
Chris@11
|
2439 attributes.value("frequencyScale").toInt(&ok);
|
Chris@11
|
2440 if (ok) setFrequencyScale(frequencyScale);
|
Chris@35
|
2441
|
Chris@37
|
2442 BinDisplay binDisplay = (BinDisplay)
|
Chris@37
|
2443 attributes.value("binDisplay").toInt(&ok);
|
Chris@37
|
2444 if (ok) setBinDisplay(binDisplay);
|
Chris@36
|
2445
|
Chris@36
|
2446 bool normalizeColumns =
|
Chris@36
|
2447 (attributes.value("normalizeColumns").trimmed() == "true");
|
Chris@36
|
2448 setNormalizeColumns(normalizeColumns);
|
Chris@11
|
2449 }
|
Chris@11
|
2450
|
Chris@11
|
2451
|
Chris@0
|
2452 #ifdef INCLUDE_MOCFILES
|
Chris@0
|
2453 #include "SpectrogramLayer.moc.cpp"
|
Chris@0
|
2454 #endif
|
Chris@0
|
2455
|