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