Mercurial > hg > svgui
comparison layer/SpectrogramLayer.cpp @ 0:2a4f26e85b4c
initial import
author | Chris Cannam |
---|---|
date | Tue, 10 Jan 2006 16:33:16 +0000 |
parents | |
children | ab83c415a6cd |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:2a4f26e85b4c |
---|---|
1 /* -*- c-basic-offset: 4 -*- vi:set ts=8 sts=4 sw=4: */ | |
2 | |
3 /* | |
4 A waveform viewer and audio annotation editor. | |
5 Chris Cannam, Queen Mary University of London, 2005 | |
6 | |
7 This is experimental software. Not for distribution. | |
8 */ | |
9 | |
10 #include "SpectrogramLayer.h" | |
11 | |
12 #include "base/View.h" | |
13 #include "base/Profiler.h" | |
14 #include "base/AudioLevel.h" | |
15 #include "base/Window.h" | |
16 | |
17 #include <QPainter> | |
18 #include <QImage> | |
19 #include <QPixmap> | |
20 #include <QRect> | |
21 #include <QTimer> | |
22 | |
23 #include <iostream> | |
24 | |
25 #include <cassert> | |
26 #include <cmath> | |
27 | |
28 //#define DEBUG_SPECTROGRAM_REPAINT 1 | |
29 | |
30 | |
31 SpectrogramLayer::SpectrogramLayer(View *w, Configuration config) : | |
32 Layer(w), | |
33 m_model(0), | |
34 m_channel(0), | |
35 m_windowSize(1024), | |
36 m_windowType(HanningWindow), | |
37 m_windowOverlap(50), | |
38 m_gain(1.0), | |
39 m_maxFrequency(8000), | |
40 m_colourScale(dBColourScale), | |
41 m_colourScheme(DefaultColours), | |
42 m_frequencyScale(LinearFrequencyScale), | |
43 m_cache(0), | |
44 m_cacheInvalid(true), | |
45 m_maxCachedFrequency(0), | |
46 m_pixmapCache(0), | |
47 m_pixmapCacheInvalid(true), | |
48 m_fillThread(0), | |
49 m_updateTimer(0), | |
50 m_lastFillExtent(0), | |
51 m_exiting(false) | |
52 { | |
53 if (config == MelodicRange) { | |
54 setWindowSize(8192); | |
55 setWindowOverlap(90); | |
56 setWindowType(ParzenWindow); | |
57 setMaxFrequency(1000); | |
58 setColourScale(LinearColourScale); | |
59 } | |
60 | |
61 if (m_view) m_view->setLightBackground(false); | |
62 m_view->addLayer(this); | |
63 } | |
64 | |
65 SpectrogramLayer::~SpectrogramLayer() | |
66 { | |
67 delete m_updateTimer; | |
68 m_updateTimer = 0; | |
69 | |
70 m_exiting = true; | |
71 m_condition.wakeAll(); | |
72 if (m_fillThread) m_fillThread->wait(); | |
73 delete m_fillThread; | |
74 | |
75 delete m_cache; | |
76 } | |
77 | |
78 void | |
79 SpectrogramLayer::setModel(const DenseTimeValueModel *model) | |
80 { | |
81 m_mutex.lock(); | |
82 m_model = model; | |
83 m_mutex.unlock(); | |
84 | |
85 if (!m_model || !m_model->isOK()) return; | |
86 | |
87 connect(m_model, SIGNAL(modelChanged()), this, SIGNAL(modelChanged())); | |
88 connect(m_model, SIGNAL(modelChanged(size_t, size_t)), | |
89 this, SIGNAL(modelChanged(size_t, size_t))); | |
90 | |
91 connect(m_model, SIGNAL(completionChanged()), | |
92 this, SIGNAL(modelCompletionChanged())); | |
93 | |
94 connect(m_model, SIGNAL(modelChanged()), this, SLOT(cacheInvalid())); | |
95 connect(m_model, SIGNAL(modelChanged(size_t, size_t)), | |
96 this, SLOT(cacheInvalid(size_t, size_t))); | |
97 | |
98 emit modelReplaced(); | |
99 fillCache(); | |
100 } | |
101 | |
102 Layer::PropertyList | |
103 SpectrogramLayer::getProperties() const | |
104 { | |
105 PropertyList list; | |
106 list.push_back(tr("Colour")); | |
107 list.push_back(tr("Colour Scale")); | |
108 list.push_back(tr("Window Type")); | |
109 list.push_back(tr("Window Size")); | |
110 list.push_back(tr("Window Overlap")); | |
111 list.push_back(tr("Gain")); | |
112 list.push_back(tr("Max Frequency")); | |
113 list.push_back(tr("Frequency Scale")); | |
114 return list; | |
115 } | |
116 | |
117 Layer::PropertyType | |
118 SpectrogramLayer::getPropertyType(const PropertyName &name) const | |
119 { | |
120 if (name == tr("Gain")) return RangeProperty; | |
121 return ValueProperty; | |
122 } | |
123 | |
124 QString | |
125 SpectrogramLayer::getPropertyGroupName(const PropertyName &name) const | |
126 { | |
127 if (name == tr("Window Size") || | |
128 name == tr("Window Overlap")) return tr("Window"); | |
129 if (name == tr("Gain") || | |
130 name == tr("Colour Scale")) return tr("Scale"); | |
131 if (name == tr("Max Frequency") || | |
132 name == tr("Frequency Scale")) return tr("Frequency"); | |
133 return QString(); | |
134 } | |
135 | |
136 int | |
137 SpectrogramLayer::getPropertyRangeAndValue(const PropertyName &name, | |
138 int *min, int *max) const | |
139 { | |
140 int deft = 0; | |
141 | |
142 if (name == tr("Gain")) { | |
143 | |
144 *min = -50; | |
145 *max = 50; | |
146 | |
147 deft = lrint(log10(m_gain) * 20.0); | |
148 if (deft < *min) deft = *min; | |
149 if (deft > *max) deft = *max; | |
150 | |
151 } else if (name == tr("Colour Scale")) { | |
152 | |
153 *min = 0; | |
154 *max = 3; | |
155 | |
156 deft = (int)m_colourScale; | |
157 | |
158 } else if (name == tr("Colour")) { | |
159 | |
160 *min = 0; | |
161 *max = 5; | |
162 | |
163 deft = (int)m_colourScheme; | |
164 | |
165 } else if (name == tr("Window Type")) { | |
166 | |
167 *min = 0; | |
168 *max = 6; | |
169 | |
170 deft = (int)m_windowType; | |
171 | |
172 } else if (name == tr("Window Size")) { | |
173 | |
174 *min = 0; | |
175 *max = 10; | |
176 | |
177 deft = 0; | |
178 int ws = m_windowSize; | |
179 while (ws > 32) { ws >>= 1; deft ++; } | |
180 | |
181 } else if (name == tr("Window Overlap")) { | |
182 | |
183 *min = 0; | |
184 *max = 4; | |
185 | |
186 deft = m_windowOverlap / 25; | |
187 if (m_windowOverlap == 90) deft = 4; | |
188 | |
189 } else if (name == tr("Max Frequency")) { | |
190 | |
191 *min = 0; | |
192 *max = 9; | |
193 | |
194 switch (m_maxFrequency) { | |
195 case 500: deft = 0; break; | |
196 case 1000: deft = 1; break; | |
197 case 1500: deft = 2; break; | |
198 case 2000: deft = 3; break; | |
199 case 4000: deft = 4; break; | |
200 case 6000: deft = 5; break; | |
201 case 8000: deft = 6; break; | |
202 case 12000: deft = 7; break; | |
203 case 16000: deft = 8; break; | |
204 default: deft = 9; break; | |
205 } | |
206 | |
207 } else if (name == tr("Frequency Scale")) { | |
208 | |
209 *min = 0; | |
210 *max = 1; | |
211 deft = (int)m_frequencyScale; | |
212 | |
213 } else { | |
214 deft = Layer::getPropertyRangeAndValue(name, min, max); | |
215 } | |
216 | |
217 return deft; | |
218 } | |
219 | |
220 QString | |
221 SpectrogramLayer::getPropertyValueLabel(const PropertyName &name, | |
222 int value) const | |
223 { | |
224 if (name == tr("Colour")) { | |
225 switch (value) { | |
226 default: | |
227 case 0: return tr("Default"); | |
228 case 1: return tr("White on Black"); | |
229 case 2: return tr("Black on White"); | |
230 case 3: return tr("Red on Blue"); | |
231 case 4: return tr("Yellow on Black"); | |
232 case 5: return tr("Red on Black"); | |
233 } | |
234 } | |
235 if (name == tr("Colour Scale")) { | |
236 switch (value) { | |
237 default: | |
238 case 0: return tr("Level Linear"); | |
239 case 1: return tr("Level Meter"); | |
240 case 2: return tr("Level dB"); | |
241 case 3: return tr("Phase"); | |
242 } | |
243 } | |
244 if (name == tr("Window Type")) { | |
245 switch ((WindowType)value) { | |
246 default: | |
247 case RectangularWindow: return tr("Rectangular"); | |
248 case BartlettWindow: return tr("Bartlett"); | |
249 case HammingWindow: return tr("Hamming"); | |
250 case HanningWindow: return tr("Hanning"); | |
251 case BlackmanWindow: return tr("Blackman"); | |
252 case GaussianWindow: return tr("Gaussian"); | |
253 case ParzenWindow: return tr("Parzen"); | |
254 } | |
255 } | |
256 if (name == tr("Window Size")) { | |
257 return QString("%1").arg(32 << value); | |
258 } | |
259 if (name == tr("Window Overlap")) { | |
260 switch (value) { | |
261 default: | |
262 case 0: return tr("None"); | |
263 case 1: return tr("25 %"); | |
264 case 2: return tr("50 %"); | |
265 case 3: return tr("75 %"); | |
266 case 4: return tr("90 %"); | |
267 } | |
268 } | |
269 if (name == tr("Max Frequency")) { | |
270 switch (value) { | |
271 default: | |
272 case 0: return tr("500 Hz"); | |
273 case 1: return tr("1 KHz"); | |
274 case 2: return tr("1.5 KHz"); | |
275 case 3: return tr("2 KHz"); | |
276 case 4: return tr("4 KHz"); | |
277 case 5: return tr("6 KHz"); | |
278 case 6: return tr("8 KHz"); | |
279 case 7: return tr("12 KHz"); | |
280 case 8: return tr("16 KHz"); | |
281 case 9: return tr("All"); | |
282 } | |
283 } | |
284 if (name == tr("Frequency Scale")) { | |
285 switch (value) { | |
286 default: | |
287 case 0: return tr("Linear"); | |
288 case 1: return tr("Log"); | |
289 } | |
290 } | |
291 return tr("<unknown>"); | |
292 } | |
293 | |
294 void | |
295 SpectrogramLayer::setProperty(const PropertyName &name, int value) | |
296 { | |
297 if (name == tr("Gain")) { | |
298 setGain(pow(10, float(value)/20.0)); | |
299 } else if (name == tr("Colour")) { | |
300 if (m_view) m_view->setLightBackground(value == 2); | |
301 switch (value) { | |
302 default: | |
303 case 0: setColourScheme(DefaultColours); break; | |
304 case 1: setColourScheme(WhiteOnBlack); break; | |
305 case 2: setColourScheme(BlackOnWhite); break; | |
306 case 3: setColourScheme(RedOnBlue); break; | |
307 case 4: setColourScheme(YellowOnBlack); break; | |
308 case 5: setColourScheme(RedOnBlack); break; | |
309 } | |
310 } else if (name == tr("Window Type")) { | |
311 setWindowType(WindowType(value)); | |
312 } else if (name == tr("Window Size")) { | |
313 setWindowSize(32 << value); | |
314 } else if (name == tr("Window Overlap")) { | |
315 if (value == 4) setWindowOverlap(90); | |
316 else setWindowOverlap(25 * value); | |
317 } else if (name == tr("Max Frequency")) { | |
318 switch (value) { | |
319 case 0: setMaxFrequency(500); break; | |
320 case 1: setMaxFrequency(1000); break; | |
321 case 2: setMaxFrequency(1500); break; | |
322 case 3: setMaxFrequency(2000); break; | |
323 case 4: setMaxFrequency(4000); break; | |
324 case 5: setMaxFrequency(6000); break; | |
325 case 6: setMaxFrequency(8000); break; | |
326 case 7: setMaxFrequency(12000); break; | |
327 case 8: setMaxFrequency(16000); break; | |
328 default: | |
329 case 9: setMaxFrequency(0); break; | |
330 } | |
331 } else if (name == tr("Colour Scale")) { | |
332 switch (value) { | |
333 default: | |
334 case 0: setColourScale(LinearColourScale); break; | |
335 case 1: setColourScale(MeterColourScale); break; | |
336 case 2: setColourScale(dBColourScale); break; | |
337 case 3: setColourScale(PhaseColourScale); break; | |
338 } | |
339 } else if (name == tr("Frequency Scale")) { | |
340 switch (value) { | |
341 default: | |
342 case 0: setFrequencyScale(LinearFrequencyScale); break; | |
343 case 1: setFrequencyScale(LogFrequencyScale); break; | |
344 } | |
345 } | |
346 } | |
347 | |
348 void | |
349 SpectrogramLayer::setChannel(int ch) | |
350 { | |
351 if (m_channel == ch) return; | |
352 | |
353 m_mutex.lock(); | |
354 m_cacheInvalid = true; | |
355 m_pixmapCacheInvalid = true; | |
356 | |
357 m_channel = ch; | |
358 emit layerParametersChanged(); | |
359 | |
360 m_mutex.unlock(); | |
361 fillCache(); | |
362 | |
363 } | |
364 | |
365 int | |
366 SpectrogramLayer::getChannel() const | |
367 { | |
368 return m_channel; | |
369 } | |
370 | |
371 void | |
372 SpectrogramLayer::setWindowSize(size_t ws) | |
373 { | |
374 if (m_windowSize == ws) return; | |
375 | |
376 m_mutex.lock(); | |
377 m_cacheInvalid = true; | |
378 m_pixmapCacheInvalid = true; | |
379 | |
380 m_windowSize = ws; | |
381 emit layerParametersChanged(); | |
382 | |
383 m_mutex.unlock(); | |
384 fillCache(); | |
385 | |
386 } | |
387 | |
388 size_t | |
389 SpectrogramLayer::getWindowSize() const | |
390 { | |
391 return m_windowSize; | |
392 } | |
393 | |
394 void | |
395 SpectrogramLayer::setWindowOverlap(size_t wi) | |
396 { | |
397 if (m_windowOverlap == wi) return; | |
398 | |
399 m_mutex.lock(); | |
400 m_cacheInvalid = true; | |
401 m_pixmapCacheInvalid = true; | |
402 | |
403 m_windowOverlap = wi; | |
404 emit layerParametersChanged(); | |
405 | |
406 m_mutex.unlock(); | |
407 fillCache(); | |
408 } | |
409 | |
410 size_t | |
411 SpectrogramLayer::getWindowOverlap() const | |
412 { | |
413 return m_windowOverlap; | |
414 } | |
415 | |
416 void | |
417 SpectrogramLayer::setWindowType(WindowType w) | |
418 { | |
419 if (m_windowType == w) return; | |
420 | |
421 m_mutex.lock(); | |
422 m_cacheInvalid = true; | |
423 m_pixmapCacheInvalid = true; | |
424 | |
425 m_windowType = w; | |
426 emit layerParametersChanged(); | |
427 | |
428 m_mutex.unlock(); | |
429 fillCache(); | |
430 } | |
431 | |
432 WindowType | |
433 SpectrogramLayer::getWindowType() const | |
434 { | |
435 return m_windowType; | |
436 } | |
437 | |
438 void | |
439 SpectrogramLayer::setGain(float gain) | |
440 { | |
441 if (m_gain == gain) return; //!!! inadequate for floats! | |
442 | |
443 m_mutex.lock(); | |
444 m_cacheInvalid = true; | |
445 m_pixmapCacheInvalid = true; | |
446 | |
447 m_gain = gain; | |
448 emit layerParametersChanged(); | |
449 | |
450 m_mutex.unlock(); | |
451 fillCache(); | |
452 } | |
453 | |
454 float | |
455 SpectrogramLayer::getGain() const | |
456 { | |
457 return m_gain; | |
458 } | |
459 | |
460 void | |
461 SpectrogramLayer::setMaxFrequency(size_t mf) | |
462 { | |
463 if (m_maxFrequency == mf) return; | |
464 | |
465 m_mutex.lock(); | |
466 | |
467 // don't need to invalidate main cache here... | |
468 | |
469 m_pixmapCacheInvalid = true; | |
470 | |
471 m_maxFrequency = mf; | |
472 emit layerParametersChanged(); | |
473 | |
474 m_mutex.unlock(); | |
475 | |
476 // ... but we do still need to do this, in case m_maxFrequency | |
477 // now > m_maxCachedFrequency | |
478 fillCache(); | |
479 } | |
480 | |
481 size_t | |
482 SpectrogramLayer::getMaxFrequency() const | |
483 { | |
484 return m_maxFrequency; | |
485 } | |
486 | |
487 void | |
488 SpectrogramLayer::setColourScale(ColourScale colourScale) | |
489 { | |
490 if (m_colourScale == colourScale) return; | |
491 | |
492 m_mutex.lock(); | |
493 m_cacheInvalid = true; | |
494 m_pixmapCacheInvalid = true; | |
495 | |
496 m_colourScale = colourScale; | |
497 emit layerParametersChanged(); | |
498 | |
499 m_mutex.unlock(); | |
500 fillCache(); | |
501 } | |
502 | |
503 SpectrogramLayer::ColourScale | |
504 SpectrogramLayer::getColourScale() const | |
505 { | |
506 return m_colourScale; | |
507 } | |
508 | |
509 void | |
510 SpectrogramLayer::setColourScheme(ColourScheme scheme) | |
511 { | |
512 if (m_colourScheme == scheme) return; | |
513 | |
514 m_mutex.lock(); | |
515 // don't need to invalidate main cache here | |
516 m_pixmapCacheInvalid = true; | |
517 | |
518 m_colourScheme = scheme; | |
519 setCacheColourmap(); | |
520 emit layerParametersChanged(); | |
521 | |
522 m_mutex.unlock(); | |
523 } | |
524 | |
525 SpectrogramLayer::ColourScheme | |
526 SpectrogramLayer::getColourScheme() const | |
527 { | |
528 return m_colourScheme; | |
529 } | |
530 | |
531 void | |
532 SpectrogramLayer::setFrequencyScale(FrequencyScale frequencyScale) | |
533 { | |
534 if (m_frequencyScale == frequencyScale) return; | |
535 | |
536 m_mutex.lock(); | |
537 // don't need to invalidate main cache here | |
538 m_pixmapCacheInvalid = true; | |
539 | |
540 m_frequencyScale = frequencyScale; | |
541 emit layerParametersChanged(); | |
542 | |
543 m_mutex.unlock(); | |
544 } | |
545 | |
546 SpectrogramLayer::FrequencyScale | |
547 SpectrogramLayer::getFrequencyScale() const | |
548 { | |
549 return m_frequencyScale; | |
550 } | |
551 | |
552 void | |
553 SpectrogramLayer::cacheInvalid() | |
554 { | |
555 m_cacheInvalid = true; | |
556 m_pixmapCacheInvalid = true; | |
557 m_cachedInitialVisibleArea = false; | |
558 fillCache(); | |
559 } | |
560 | |
561 void | |
562 SpectrogramLayer::cacheInvalid(size_t, size_t) | |
563 { | |
564 // for now (or forever?) | |
565 cacheInvalid(); | |
566 } | |
567 | |
568 void | |
569 SpectrogramLayer::fillCache() | |
570 { | |
571 #ifdef DEBUG_SPECTROGRAM_REPAINT | |
572 std::cerr << "SpectrogramLayer::fillCache" << std::endl; | |
573 #endif | |
574 QMutexLocker locker(&m_mutex); | |
575 | |
576 m_lastFillExtent = 0; | |
577 | |
578 delete m_updateTimer; | |
579 m_updateTimer = new QTimer(this); | |
580 connect(m_updateTimer, SIGNAL(timeout()), this, SLOT(fillTimerTimedOut())); | |
581 m_updateTimer->start(200); | |
582 | |
583 if (!m_fillThread) { | |
584 std::cerr << "SpectrogramLayer::fillCache creating thread" << std::endl; | |
585 m_fillThread = new CacheFillThread(*this); | |
586 m_fillThread->start(); | |
587 } | |
588 | |
589 m_condition.wakeAll(); | |
590 } | |
591 | |
592 void | |
593 SpectrogramLayer::fillTimerTimedOut() | |
594 { | |
595 if (m_fillThread && m_model) { | |
596 size_t fillExtent = m_fillThread->getFillExtent(); | |
597 #ifdef DEBUG_SPECTROGRAM_REPAINT | |
598 std::cerr << "SpectrogramLayer::fillTimerTimedOut: extent " << fillExtent << ", last " << m_lastFillExtent << ", total " << m_model->getEndFrame() << std::endl; | |
599 #endif | |
600 if (fillExtent >= m_lastFillExtent) { | |
601 if (fillExtent >= m_model->getEndFrame() && m_lastFillExtent > 0) { | |
602 #ifdef DEBUG_SPECTROGRAM_REPAINT | |
603 std::cerr << "complete!" << std::endl; | |
604 #endif | |
605 emit modelChanged(); | |
606 m_pixmapCacheInvalid = true; | |
607 delete m_updateTimer; | |
608 m_updateTimer = 0; | |
609 m_lastFillExtent = 0; | |
610 } else if (fillExtent > m_lastFillExtent) { | |
611 #ifdef DEBUG_SPECTROGRAM_REPAINT | |
612 std::cerr << "SpectrogramLayer: emitting modelChanged(" | |
613 << m_lastFillExtent << "," << fillExtent << ")" << std::endl; | |
614 #endif | |
615 emit modelChanged(m_lastFillExtent, fillExtent); | |
616 m_pixmapCacheInvalid = true; | |
617 m_lastFillExtent = fillExtent; | |
618 } | |
619 } else { | |
620 if (m_view) { | |
621 size_t sf = 0; | |
622 if (m_view->getStartFrame() > 0) sf = m_view->getStartFrame(); | |
623 #ifdef DEBUG_SPECTROGRAM_REPAINT | |
624 std::cerr << "SpectrogramLayer: going backwards, emitting modelChanged(" | |
625 << sf << "," << m_view->getEndFrame() << ")" << std::endl; | |
626 #endif | |
627 emit modelChanged(sf, m_view->getEndFrame()); | |
628 m_pixmapCacheInvalid = true; | |
629 } | |
630 m_lastFillExtent = fillExtent; | |
631 } | |
632 } | |
633 } | |
634 | |
635 void | |
636 SpectrogramLayer::setCacheColourmap() | |
637 { | |
638 if (m_cacheInvalid || !m_cache) return; | |
639 | |
640 m_cache->setNumColors(256); | |
641 | |
642 m_cache->setColor(0, qRgb(255, 255, 255)); | |
643 | |
644 for (int pixel = 1; pixel < 256; ++pixel) { | |
645 | |
646 QColor colour; | |
647 int hue, px; | |
648 | |
649 switch (m_colourScheme) { | |
650 | |
651 default: | |
652 case DefaultColours: | |
653 hue = 256 - pixel; | |
654 colour = QColor::fromHsv(hue, pixel/2 + 128, pixel); | |
655 break; | |
656 | |
657 case WhiteOnBlack: | |
658 colour = QColor(pixel, pixel, pixel); | |
659 break; | |
660 | |
661 case BlackOnWhite: | |
662 colour = QColor(256-pixel, 256-pixel, 256-pixel); | |
663 break; | |
664 | |
665 case RedOnBlue: | |
666 colour = QColor(pixel > 128 ? (pixel - 128) * 2 : 0, 0, | |
667 pixel < 128 ? pixel : (256 - pixel)); | |
668 break; | |
669 | |
670 case YellowOnBlack: | |
671 px = 256 - pixel; | |
672 colour = QColor(px < 64 ? 255 - px/2 : | |
673 px < 128 ? 224 - (px - 64) : | |
674 px < 192 ? 160 - (px - 128) * 3 / 2 : | |
675 256 - px, | |
676 pixel, | |
677 pixel / 4); | |
678 break; | |
679 | |
680 case RedOnBlack: | |
681 colour = QColor::fromHsv(10, pixel, pixel); | |
682 break; | |
683 } | |
684 | |
685 m_cache->setColor | |
686 (pixel, qRgb(colour.red(), colour.green(), colour.blue())); | |
687 } | |
688 } | |
689 | |
690 bool | |
691 SpectrogramLayer::fillCacheColumn(int column, double *input, | |
692 fftw_complex *output, | |
693 fftw_plan plan, | |
694 const Window<double> &windower, | |
695 bool lock) const | |
696 { | |
697 size_t increment = m_windowSize - m_windowSize * m_windowOverlap / 100; | |
698 int startFrame = increment * column; | |
699 int endFrame = startFrame + m_windowSize; | |
700 | |
701 startFrame -= int(m_windowSize - increment) / 2; | |
702 endFrame -= int(m_windowSize - increment) / 2; | |
703 size_t pfx = 0; | |
704 | |
705 if (startFrame < 0) { | |
706 pfx = size_t(-startFrame); | |
707 for (size_t i = 0; i < pfx; ++i) { | |
708 input[i] = 0.0; | |
709 } | |
710 } | |
711 | |
712 size_t got = m_model->getValues(m_channel, startFrame + pfx, | |
713 endFrame, input + pfx); | |
714 while (got + pfx < m_windowSize) { | |
715 input[got + pfx] = 0.0; | |
716 ++got; | |
717 } | |
718 | |
719 if (m_gain != 1.0) { | |
720 for (size_t i = 0; i < m_windowSize; ++i) { | |
721 input[i] *= m_gain; | |
722 } | |
723 } | |
724 | |
725 windower.cut(input); | |
726 | |
727 fftw_execute(plan); | |
728 | |
729 if (lock) m_mutex.lock(); | |
730 bool interrupted = false; | |
731 | |
732 for (size_t i = 0; i < m_windowSize / 2; ++i) { | |
733 | |
734 if (int(i) >= m_cache->height()) break; | |
735 | |
736 int value = 0; | |
737 | |
738 if (m_colourScale == PhaseColourScale) { | |
739 | |
740 double phase = atan2(-output[i][1], output[i][0]); | |
741 value = int((phase * 128 / M_PI) + 128); | |
742 | |
743 } else { | |
744 double mag = sqrt(output[i][0] * output[i][0] + | |
745 output[i][1] * output[i][1]); | |
746 mag /= m_windowSize / 2; | |
747 | |
748 switch (m_colourScale) { | |
749 | |
750 default: | |
751 case LinearColourScale: | |
752 value = int(mag * 50 * 256); | |
753 break; | |
754 | |
755 case MeterColourScale: | |
756 value = AudioLevel::multiplier_to_preview(mag * 50, 256); | |
757 break; | |
758 | |
759 case dBColourScale: | |
760 mag = 20.0 * log10(mag); | |
761 mag = (mag + 80.0) / 80.0; | |
762 if (mag < 0.0) mag = 0.0; | |
763 if (mag > 1.0) mag = 1.0; | |
764 value = int(mag * 256); | |
765 } | |
766 } | |
767 | |
768 if (value > 254) value = 254; | |
769 if (value < 0) value = 0; | |
770 | |
771 if (m_cacheInvalid || m_exiting) { | |
772 interrupted = true; | |
773 break; | |
774 } | |
775 | |
776 if (column < m_cache->width()) { | |
777 m_cache->setPixel(column, i, value + 1); // 0 is "unset" | |
778 } | |
779 } | |
780 | |
781 if (lock) m_mutex.unlock(); | |
782 return !interrupted; | |
783 } | |
784 | |
785 void | |
786 SpectrogramLayer::CacheFillThread::run() | |
787 { | |
788 // std::cerr << "SpectrogramLayer::CacheFillThread::run" << std::endl; | |
789 | |
790 m_layer.m_mutex.lock(); | |
791 | |
792 while (!m_layer.m_exiting) { | |
793 | |
794 bool interrupted = false; | |
795 | |
796 // std::cerr << "SpectrogramLayer::CacheFillThread::run in loop" << std::endl; | |
797 | |
798 if (m_layer.m_model && | |
799 (m_layer.m_cacheInvalid || | |
800 m_layer.m_maxFrequency > m_layer.m_maxCachedFrequency)) { | |
801 | |
802 // std::cerr << "SpectrogramLayer::CacheFillThread::run: something to do" << std::endl; | |
803 | |
804 while (!m_layer.m_model->isReady()) { | |
805 m_layer.m_condition.wait(&m_layer.m_mutex, 100); | |
806 } | |
807 | |
808 size_t minFreq = 0; | |
809 if (!m_layer.m_cacheInvalid) { | |
810 minFreq = m_layer.m_maxCachedFrequency; | |
811 } | |
812 | |
813 m_layer.m_cachedInitialVisibleArea = false; | |
814 m_layer.m_cacheInvalid = false; | |
815 m_fillExtent = 0; | |
816 m_fillCompletion = 0; | |
817 | |
818 std::cerr << "SpectrogramLayer::CacheFillThread::run: model is ready" << std::endl; | |
819 | |
820 size_t start = m_layer.m_model->getStartFrame(); | |
821 size_t end = m_layer.m_model->getEndFrame(); | |
822 size_t windowSize = m_layer.m_windowSize; | |
823 size_t windowIncrement = m_layer.getWindowIncrement(); | |
824 | |
825 size_t visibleStart = start; | |
826 size_t visibleEnd = end; | |
827 | |
828 if (m_layer.m_view) { | |
829 if (m_layer.m_view->getStartFrame() < 0) { | |
830 visibleStart = 0; | |
831 } else { | |
832 visibleStart = m_layer.m_view->getStartFrame(); | |
833 visibleStart = (visibleStart / windowIncrement) * | |
834 windowIncrement; | |
835 } | |
836 visibleEnd = m_layer.m_view->getEndFrame(); | |
837 } | |
838 | |
839 delete m_layer.m_cache; | |
840 size_t bins = windowSize / 2; | |
841 if (m_layer.m_maxFrequency > 0) { | |
842 int sr = m_layer.m_model->getSampleRate(); | |
843 bins = int((double(m_layer.m_maxFrequency) * windowSize) / sr + 0.1); | |
844 if (bins > windowSize / 2) bins = windowSize / 2; | |
845 } | |
846 m_layer.m_cache = new QImage((end - start) / windowIncrement + 1, | |
847 bins, //!!! | |
848 QImage::Format_Indexed8); | |
849 | |
850 m_layer.setCacheColourmap(); | |
851 | |
852 m_layer.m_cache->fill(0); | |
853 m_layer.m_mutex.unlock(); | |
854 | |
855 double *input = (double *) | |
856 fftw_malloc(windowSize * sizeof(double)); | |
857 | |
858 fftw_complex *output = (fftw_complex *) | |
859 fftw_malloc(windowSize * sizeof(fftw_complex)); | |
860 | |
861 fftw_plan plan = fftw_plan_dft_r2c_1d(windowSize, input, | |
862 output, FFTW_MEASURE); | |
863 | |
864 Window<double> windower(m_layer.m_windowType, m_layer.m_windowSize); | |
865 | |
866 if (!plan) { | |
867 std::cerr << "WARNING: fftw_plan(" << windowSize << ") failed!" << std::endl; | |
868 fftw_free(input); | |
869 fftw_free(output); | |
870 m_layer.m_mutex.lock(); | |
871 continue; | |
872 } | |
873 | |
874 int counter = 0; | |
875 int updateAt = (end / windowIncrement) / 20; | |
876 if (updateAt < 100) updateAt = 100; | |
877 | |
878 bool doVisibleFirst = (visibleStart != start && visibleEnd != end); | |
879 | |
880 if (doVisibleFirst) { | |
881 | |
882 m_layer.m_mutex.lock(); | |
883 | |
884 for (size_t f = visibleStart; f < visibleEnd; f += windowIncrement) { | |
885 | |
886 m_layer.fillCacheColumn(int((f - start) / windowIncrement), | |
887 input, output, plan, windower, false); | |
888 | |
889 m_layer.m_mutex.unlock(); | |
890 m_layer.m_mutex.lock(); | |
891 | |
892 if (m_layer.m_cacheInvalid || m_layer.m_exiting) { | |
893 interrupted = true; | |
894 m_fillExtent = 0; | |
895 break; | |
896 } | |
897 | |
898 if (++counter == updateAt) { | |
899 if (f < end) m_fillExtent = f; | |
900 m_fillCompletion = size_t(100 * fabsf(float(f - visibleStart) / | |
901 float(end - start))); | |
902 counter = 0; | |
903 } | |
904 } | |
905 | |
906 m_layer.m_mutex.unlock(); | |
907 } | |
908 | |
909 m_layer.m_cachedInitialVisibleArea = true; | |
910 | |
911 if (!interrupted && doVisibleFirst) { | |
912 | |
913 for (size_t f = visibleEnd; f < end; f += windowIncrement) { | |
914 | |
915 if (!m_layer.fillCacheColumn(int((f - start) / windowIncrement), | |
916 input, output, plan, windower, true)) { | |
917 interrupted = true; | |
918 m_fillExtent = 0; | |
919 break; | |
920 } | |
921 | |
922 | |
923 if (++counter == updateAt) { | |
924 if (f < end) m_fillExtent = f; | |
925 m_fillCompletion = size_t(100 * fabsf(float(f - visibleStart) / | |
926 float(end - start))); | |
927 counter = 0; | |
928 } | |
929 } | |
930 } | |
931 | |
932 if (!interrupted) { | |
933 | |
934 size_t remainingEnd = end; | |
935 if (doVisibleFirst) { | |
936 remainingEnd = visibleStart; | |
937 if (remainingEnd > start) --remainingEnd; | |
938 else remainingEnd = start; | |
939 } | |
940 size_t baseCompletion = m_fillCompletion; | |
941 | |
942 for (size_t f = start; f < remainingEnd; f += windowIncrement) { | |
943 | |
944 if (!m_layer.fillCacheColumn(int((f - start) / windowIncrement), | |
945 input, output, plan, windower, true)) { | |
946 interrupted = true; | |
947 m_fillExtent = 0; | |
948 break; | |
949 } | |
950 | |
951 if (++counter == updateAt) { | |
952 m_fillExtent = f; | |
953 m_fillCompletion = baseCompletion + | |
954 size_t(100 * fabsf(float(f - start) / | |
955 float(end - start))); | |
956 counter = 0; | |
957 } | |
958 } | |
959 } | |
960 | |
961 fftw_destroy_plan(plan); | |
962 fftw_free(output); | |
963 fftw_free(input); | |
964 | |
965 if (!interrupted) { | |
966 m_fillExtent = end; | |
967 m_fillCompletion = 100; | |
968 } | |
969 | |
970 m_layer.m_mutex.lock(); | |
971 } | |
972 | |
973 if (!interrupted) m_layer.m_condition.wait(&m_layer.m_mutex, 2000); | |
974 } | |
975 } | |
976 | |
977 bool | |
978 SpectrogramLayer::getYBinRange(int y, float &q0, float &q1) const | |
979 { | |
980 int h = m_view->height(); | |
981 if (y < 0 || y >= h) return false; | |
982 | |
983 // Each pixel in a column is drawn from a possibly non- | |
984 // integral set of frequency bins. | |
985 | |
986 if (m_frequencyScale == LinearFrequencyScale) { | |
987 | |
988 size_t bins = m_windowSize / 2; | |
989 | |
990 if (m_maxFrequency > 0) { | |
991 int sr = m_model->getSampleRate(); | |
992 bins = int((double(m_maxFrequency) * m_windowSize) / sr + 0.1); | |
993 if (bins > m_windowSize / 2) bins = m_windowSize / 2; | |
994 } | |
995 | |
996 q0 = float(h - y - 1) * bins / h; | |
997 q1 = float(h - y) * bins / h; | |
998 | |
999 } else { | |
1000 | |
1001 // This is all most ad-hoc. I'm not at my brightest. | |
1002 | |
1003 int sr = m_model->getSampleRate(); | |
1004 | |
1005 float maxf = m_maxFrequency; | |
1006 if (maxf == 0.0) maxf = float(sr) / 2; | |
1007 | |
1008 float minf = float(sr) / m_windowSize; | |
1009 | |
1010 float maxlogf = log10f(maxf); | |
1011 float minlogf = log10f(minf); | |
1012 | |
1013 float logf0 = minlogf + ((maxlogf - minlogf) * (h - y - 1)) / h; | |
1014 float logf1 = minlogf + ((maxlogf - minlogf) * (h - y)) / h; | |
1015 | |
1016 float f0 = pow(10.f, logf0); | |
1017 float f1 = pow(10.f, logf1); | |
1018 | |
1019 q0 = ((f0 * m_windowSize) / sr) - 1; | |
1020 q1 = ((f1 * m_windowSize) / sr) - 1; | |
1021 | |
1022 // std::cout << "y=" << y << " h=" << h << " maxf=" << maxf << " maxlogf=" | |
1023 // << maxlogf << " logf0=" << logf0 << " f0=" << f0 << " q0=" | |
1024 // << q0 << std::endl; | |
1025 } | |
1026 | |
1027 return true; | |
1028 } | |
1029 | |
1030 bool | |
1031 SpectrogramLayer::getXBinRange(int x, float &s0, float &s1, LayerRange *range) const | |
1032 { | |
1033 long startFrame; | |
1034 int zoomLevel; | |
1035 size_t modelStart; | |
1036 size_t modelEnd; | |
1037 | |
1038 if (range) { | |
1039 startFrame = range->startFrame; | |
1040 zoomLevel = range->zoomLevel; | |
1041 modelStart = range->modelStart; | |
1042 modelEnd = range->modelEnd; | |
1043 } else { | |
1044 startFrame = m_view->getStartFrame(); | |
1045 zoomLevel = m_view->getZoomLevel(); | |
1046 modelStart = m_model->getStartFrame(); | |
1047 modelEnd = m_model->getEndFrame(); | |
1048 } | |
1049 | |
1050 // Each pixel column covers an exact range of sample frames: | |
1051 int f0 = startFrame + x * zoomLevel - modelStart; | |
1052 int f1 = f0 + zoomLevel - 1; | |
1053 | |
1054 if (f1 < int(modelStart) || f0 > int(modelEnd)) return false; | |
1055 | |
1056 // And that range may be drawn from a possibly non-integral | |
1057 // range of spectrogram windows: | |
1058 | |
1059 size_t windowIncrement = getWindowIncrement(); | |
1060 | |
1061 s0 = float(f0) / windowIncrement; | |
1062 s1 = float(f1) / windowIncrement; | |
1063 | |
1064 return true; | |
1065 } | |
1066 | |
1067 bool | |
1068 SpectrogramLayer::getXBinSourceRange(int x, RealTime &min, RealTime &max) const | |
1069 { | |
1070 float s0 = 0, s1 = 0; | |
1071 if (!getXBinRange(x, s0, s1)) return false; | |
1072 | |
1073 int s0i = int(s0 + 0.001); | |
1074 int s1i = int(s1); | |
1075 | |
1076 int windowIncrement = getWindowIncrement(); | |
1077 int w0 = s0i * windowIncrement - (m_windowSize - windowIncrement)/2; | |
1078 int w1 = s1i * windowIncrement + windowIncrement + | |
1079 (m_windowSize - windowIncrement)/2 - 1; | |
1080 | |
1081 min = RealTime::frame2RealTime(w0, m_model->getSampleRate()); | |
1082 max = RealTime::frame2RealTime(w1, m_model->getSampleRate()); | |
1083 return true; | |
1084 } | |
1085 | |
1086 bool | |
1087 SpectrogramLayer::getYBinSourceRange(int y, float &freqMin, float &freqMax) | |
1088 const | |
1089 { | |
1090 float q0 = 0, q1 = 0; | |
1091 if (!getYBinRange(y, q0, q1)) return false; | |
1092 | |
1093 int q0i = int(q0 + 0.001); | |
1094 int q1i = int(q1); | |
1095 | |
1096 int sr = m_model->getSampleRate(); | |
1097 | |
1098 for (int q = q0i; q <= q1i; ++q) { | |
1099 int binfreq = (sr * (q + 1)) / m_windowSize; | |
1100 if (q == q0i) freqMin = binfreq; | |
1101 if (q == q1i) freqMax = binfreq; | |
1102 } | |
1103 return true; | |
1104 } | |
1105 | |
1106 bool | |
1107 SpectrogramLayer::getXYBinSourceRange(int x, int y, float &dbMin, float &dbMax) const | |
1108 { | |
1109 float q0 = 0, q1 = 0; | |
1110 if (!getYBinRange(y, q0, q1)) return false; | |
1111 | |
1112 float s0 = 0, s1 = 0; | |
1113 if (!getXBinRange(x, s0, s1)) return false; | |
1114 | |
1115 int q0i = int(q0 + 0.001); | |
1116 int q1i = int(q1); | |
1117 | |
1118 int s0i = int(s0 + 0.001); | |
1119 int s1i = int(s1); | |
1120 | |
1121 if (m_mutex.tryLock()) { | |
1122 if (m_cache && !m_cacheInvalid) { | |
1123 | |
1124 int cw = m_cache->width(); | |
1125 int ch = m_cache->height(); | |
1126 | |
1127 int min = -1, max = -1; | |
1128 | |
1129 for (int q = q0i; q <= q1i; ++q) { | |
1130 for (int s = s0i; s <= s1i; ++s) { | |
1131 if (s >= 0 && q >= 0 && s < cw && q < ch) { | |
1132 int value = m_cache->scanLine(q)[s]; | |
1133 if (min == -1 || value < min) min = value; | |
1134 if (max == -1 || value > max) max = value; | |
1135 } | |
1136 } | |
1137 } | |
1138 | |
1139 if (min < 0) return false; | |
1140 | |
1141 dbMin = (float(min) / 256.0) * 80.0 - 80.0; | |
1142 dbMax = (float(max + 1) / 256.0) * 80.0 - 80.1; | |
1143 | |
1144 m_mutex.unlock(); | |
1145 return true; | |
1146 } | |
1147 | |
1148 m_mutex.unlock(); | |
1149 } | |
1150 | |
1151 return false; | |
1152 } | |
1153 | |
1154 void | |
1155 SpectrogramLayer::paint(QPainter &paint, QRect rect) const | |
1156 { | |
1157 // Profiler profiler("SpectrogramLayer::paint", true); | |
1158 #ifdef DEBUG_SPECTROGRAM_REPAINT | |
1159 std::cerr << "SpectrogramLayer::paint(): m_model is " << m_model << ", zoom level is " << m_view->getZoomLevel() << ", m_updateTimer " << m_updateTimer << ", pixmap cache invalid " << m_pixmapCacheInvalid << std::endl; | |
1160 #endif | |
1161 | |
1162 if (!m_model || !m_model->isOK() || !m_model->isReady()) { | |
1163 return; | |
1164 } | |
1165 | |
1166 #ifdef DEBUG_SPECTROGRAM_REPAINT | |
1167 std::cerr << "SpectrogramLayer::paint(): About to lock" << std::endl; | |
1168 #endif | |
1169 | |
1170 /* | |
1171 if (m_cachedInitialVisibleArea) { | |
1172 if (!m_mutex.tryLock()) { | |
1173 m_view->update(); | |
1174 return; | |
1175 } | |
1176 } else { | |
1177 */ | |
1178 m_mutex.lock(); | |
1179 // } | |
1180 | |
1181 #ifdef DEBUG_SPECTROGRAM_REPAINT | |
1182 std::cerr << "SpectrogramLayer::paint(): locked" << std::endl; | |
1183 #endif | |
1184 | |
1185 if (m_cacheInvalid) { // lock the mutex before checking this | |
1186 m_mutex.unlock(); | |
1187 #ifdef DEBUG_SPECTROGRAM_REPAINT | |
1188 std::cerr << "SpectrogramLayer::paint(): Cache invalid, returning" << std::endl; | |
1189 #endif | |
1190 return; | |
1191 } | |
1192 | |
1193 bool stillCacheing = (m_updateTimer != 0); | |
1194 | |
1195 #ifdef DEBUG_SPECTROGRAM_REPAINT | |
1196 std::cerr << "SpectrogramLayer::paint(): Still cacheing = " << stillCacheing << std::endl; | |
1197 #endif | |
1198 | |
1199 long startFrame = m_view->getStartFrame(); | |
1200 int zoomLevel = m_view->getZoomLevel(); | |
1201 | |
1202 int x0 = 0; | |
1203 int x1 = m_view->width(); | |
1204 int y0 = 0; | |
1205 int y1 = m_view->height(); | |
1206 | |
1207 bool recreateWholePixmapCache = true; | |
1208 | |
1209 if (!m_pixmapCacheInvalid) { | |
1210 | |
1211 //!!! This cache may have been obsoleted entirely by the | |
1212 //scrolling cache in View. Perhaps experiment with | |
1213 //removing it and see if it makes things even quicker (or else | |
1214 //make it optional) | |
1215 | |
1216 if (int(m_pixmapCacheZoomLevel) == zoomLevel && | |
1217 m_pixmapCache->width() == m_view->width() && | |
1218 m_pixmapCache->height() == m_view->height()) { | |
1219 | |
1220 if (m_pixmapCacheStartFrame / zoomLevel == | |
1221 startFrame / zoomLevel) { | |
1222 | |
1223 #ifdef DEBUG_SPECTROGRAM_REPAINT | |
1224 std::cerr << "SpectrogramLayer: pixmap cache good" << std::endl; | |
1225 #endif | |
1226 | |
1227 m_mutex.unlock(); | |
1228 paint.drawPixmap(rect, *m_pixmapCache, rect); | |
1229 return; | |
1230 | |
1231 } else { | |
1232 | |
1233 #ifdef DEBUG_SPECTROGRAM_REPAINT | |
1234 std::cerr << "SpectrogramLayer: pixmap cache partially OK" << std::endl; | |
1235 #endif | |
1236 | |
1237 recreateWholePixmapCache = false; | |
1238 | |
1239 int dx = (m_pixmapCacheStartFrame - startFrame) / zoomLevel; | |
1240 | |
1241 #ifdef DEBUG_SPECTROGRAM_REPAINT | |
1242 std::cerr << "SpectrogramLayer: dx = " << dx << " (pixmap cache " << m_pixmapCache->width() << "x" << m_pixmapCache->height() << ")" << std::endl; | |
1243 #endif | |
1244 | |
1245 if (dx > -m_pixmapCache->width() && dx < m_pixmapCache->width()) { | |
1246 | |
1247 #if defined(Q_WS_WIN32) || defined(Q_WS_MAC) | |
1248 // Copying a pixmap to itself doesn't work | |
1249 // properly on Windows or Mac (it only works when | |
1250 // moving in one direction). | |
1251 | |
1252 //!!! Need a utility function for this | |
1253 | |
1254 static QPixmap *tmpPixmap = 0; | |
1255 if (!tmpPixmap || | |
1256 tmpPixmap->width() != m_pixmapCache->width() || | |
1257 tmpPixmap->height() != m_pixmapCache->height()) { | |
1258 delete tmpPixmap; | |
1259 tmpPixmap = new QPixmap(m_pixmapCache->width(), | |
1260 m_pixmapCache->height()); | |
1261 } | |
1262 QPainter cachePainter; | |
1263 cachePainter.begin(tmpPixmap); | |
1264 cachePainter.drawPixmap(0, 0, *m_pixmapCache); | |
1265 cachePainter.end(); | |
1266 cachePainter.begin(m_pixmapCache); | |
1267 cachePainter.drawPixmap(dx, 0, *tmpPixmap); | |
1268 cachePainter.end(); | |
1269 #else | |
1270 QPainter cachePainter(m_pixmapCache); | |
1271 cachePainter.drawPixmap(dx, 0, *m_pixmapCache); | |
1272 cachePainter.end(); | |
1273 #endif | |
1274 | |
1275 paint.drawPixmap(rect, *m_pixmapCache, rect); | |
1276 | |
1277 if (dx < 0) { | |
1278 x0 = m_pixmapCache->width() + dx; | |
1279 x1 = m_pixmapCache->width(); | |
1280 } else { | |
1281 x0 = 0; | |
1282 x1 = dx; | |
1283 } | |
1284 } | |
1285 } | |
1286 } else { | |
1287 #ifdef DEBUG_SPECTROGRAM_REPAINT | |
1288 std::cerr << "SpectrogramLayer: pixmap cache useless" << std::endl; | |
1289 #endif | |
1290 } | |
1291 } | |
1292 | |
1293 if (stillCacheing) { | |
1294 x0 = rect.left(); | |
1295 x1 = rect.right() + 1; | |
1296 y0 = rect.top(); | |
1297 y1 = rect.bottom() + 1; | |
1298 } | |
1299 | |
1300 int w = x1 - x0; | |
1301 int h = y1 - y0; | |
1302 | |
1303 // std::cerr << "x0 " << x0 << ", x1 " << x1 << ", w " << w << ", h " << h << std::endl; | |
1304 | |
1305 QImage scaled(w, h, QImage::Format_RGB32); | |
1306 | |
1307 LayerRange range = { m_view->getStartFrame(), m_view->getZoomLevel(), | |
1308 m_model->getStartFrame(), m_model->getEndFrame() }; | |
1309 | |
1310 m_mutex.unlock(); | |
1311 | |
1312 for (int y = 0; y < h; ++y) { | |
1313 | |
1314 m_mutex.lock(); | |
1315 if (m_cacheInvalid) { | |
1316 m_mutex.unlock(); | |
1317 break; | |
1318 } | |
1319 | |
1320 int cw = m_cache->width(); | |
1321 int ch = m_cache->height(); | |
1322 | |
1323 float q0 = 0, q1 = 0; | |
1324 | |
1325 if (!getYBinRange(y0 + y, q0, q1)) { | |
1326 for (int x = 0; x < w; ++x) { | |
1327 assert(x <= scaled.width()); | |
1328 scaled.setPixel(x, y, qRgb(0, 0, 0)); | |
1329 } | |
1330 m_mutex.unlock(); | |
1331 continue; | |
1332 } | |
1333 | |
1334 int q0i = int(q0 + 0.001); | |
1335 int q1i = int(q1); | |
1336 | |
1337 for (int x = 0; x < w; ++x) { | |
1338 | |
1339 float s0 = 0, s1 = 0; | |
1340 | |
1341 if (!getXBinRange(x0 + x, s0, s1, &range)) { | |
1342 assert(x <= scaled.width()); | |
1343 scaled.setPixel(x, y, qRgb(0, 0, 0)); | |
1344 continue; | |
1345 } | |
1346 | |
1347 int s0i = int(s0 + 0.001); | |
1348 int s1i = int(s1); | |
1349 | |
1350 float total = 0, divisor = 0; | |
1351 | |
1352 for (int s = s0i; s <= s1i; ++s) { | |
1353 | |
1354 float sprop = 1.0; | |
1355 if (s == s0i) sprop *= (s + 1) - s0; | |
1356 if (s == s1i) sprop *= s1 - s; | |
1357 | |
1358 for (int q = q0i; q <= q1i; ++q) { | |
1359 | |
1360 float qprop = sprop; | |
1361 if (q == q0i) qprop *= (q + 1) - q0; | |
1362 if (q == q1i) qprop *= q1 - q; | |
1363 | |
1364 if (s >= 0 && q >= 0 && s < cw && q < ch) { | |
1365 total += qprop * m_cache->scanLine(q)[s]; | |
1366 divisor += qprop; | |
1367 } | |
1368 } | |
1369 } | |
1370 | |
1371 if (divisor > 0.0) { | |
1372 int pixel = int(total / divisor); | |
1373 if (pixel > 255) pixel = 255; | |
1374 if (pixel < 1) pixel = 1; | |
1375 assert(x <= scaled.width()); | |
1376 scaled.setPixel(x, y, m_cache->color(pixel)); | |
1377 } else { | |
1378 assert(x <= scaled.width()); | |
1379 scaled.setPixel(x, y, qRgb(0, 0, 0)); | |
1380 } | |
1381 } | |
1382 | |
1383 m_mutex.unlock(); | |
1384 } | |
1385 | |
1386 paint.drawImage(x0, y0, scaled); | |
1387 | |
1388 if (recreateWholePixmapCache) { | |
1389 delete m_pixmapCache; | |
1390 m_pixmapCache = new QPixmap(w, h); | |
1391 } | |
1392 | |
1393 QPainter cachePainter(m_pixmapCache); | |
1394 cachePainter.drawImage(x0, y0, scaled); | |
1395 cachePainter.end(); | |
1396 | |
1397 m_pixmapCacheInvalid = false; | |
1398 m_pixmapCacheStartFrame = startFrame; | |
1399 m_pixmapCacheZoomLevel = zoomLevel; | |
1400 | |
1401 #ifdef DEBUG_SPECTROGRAM_REPAINT | |
1402 std::cerr << "SpectrogramLayer::paint() returning" << std::endl; | |
1403 #endif | |
1404 | |
1405 //!!! drawLocalFeatureDescription(paint); | |
1406 } | |
1407 | |
1408 int | |
1409 SpectrogramLayer::getCompletion() const | |
1410 { | |
1411 if (m_updateTimer == 0) return 100; | |
1412 size_t completion = m_fillThread->getFillCompletion(); | |
1413 // std::cerr << "SpectrogramLayer::getCompletion: completion = " << completion << std::endl; | |
1414 return completion; | |
1415 } | |
1416 | |
1417 QRect | |
1418 SpectrogramLayer::getFeatureDescriptionRect(QPainter &paint, QPoint pos) const | |
1419 { | |
1420 if (!m_model || !m_model->isOK()) return QRect(); | |
1421 | |
1422 QString timeLabel = tr("Time: "); | |
1423 QString freqLabel = tr("Hz: "); | |
1424 QString dBLabel = tr("dB: "); | |
1425 | |
1426 // assume time is widest | |
1427 RealTime rtMin, rtMax; | |
1428 if (!getXBinSourceRange(pos.x(), rtMin, rtMax)) return QRect(); | |
1429 QString timeMinText = QString("%1").arg(rtMin.toText(true).c_str()); | |
1430 QString timeMaxText = QString(" - %1").arg(rtMax.toText(true).c_str()); | |
1431 | |
1432 QFontMetrics metrics = paint.fontMetrics(); | |
1433 | |
1434 int labelwidth = | |
1435 std::max(std::max(metrics.width(timeLabel), | |
1436 metrics.width(freqLabel)), | |
1437 metrics.width(dBLabel)); | |
1438 | |
1439 int boxwidth = labelwidth + | |
1440 metrics.width(timeMinText) + metrics.width(timeMaxText); | |
1441 | |
1442 int fontHeight = metrics.height(); | |
1443 int boxheight = fontHeight * 3 + 4; | |
1444 | |
1445 return QRect(0, 0, boxwidth + 20, boxheight + 15); | |
1446 } | |
1447 | |
1448 void | |
1449 SpectrogramLayer::paintLocalFeatureDescription(QPainter &paint, | |
1450 QRect rect, QPoint pos) const | |
1451 { | |
1452 int x = pos.x(); | |
1453 int y = pos.y(); | |
1454 | |
1455 if (!m_model || !m_model->isOK()) return; | |
1456 | |
1457 float dbMin = 0, dbMax = 0; | |
1458 float freqMin = 0, freqMax = 0; | |
1459 RealTime rtMin, rtMax; | |
1460 | |
1461 bool haveDb = false; | |
1462 | |
1463 if (!getXBinSourceRange(x, rtMin, rtMax)) return; | |
1464 if (!getYBinSourceRange(y, freqMin, freqMax)) return; | |
1465 if (getXYBinSourceRange(x, y, dbMin, dbMax)) haveDb = true; | |
1466 | |
1467 QString timeLabel = tr("Time: "); | |
1468 QString freqLabel = tr("Hz: "); | |
1469 QString dBLabel = tr("dB: "); | |
1470 | |
1471 QString timeMinText = QString("%1").arg(rtMin.toText(true).c_str()); | |
1472 QString timeMaxText = QString(" - %1").arg(rtMax.toText(true).c_str()); | |
1473 | |
1474 QString freqMinText = QString("%1").arg(freqMin); | |
1475 QString freqMaxText = ""; | |
1476 if (freqMax != freqMin) { | |
1477 freqMaxText = QString(" - %1").arg(freqMax); | |
1478 } | |
1479 | |
1480 QString dBMinText = ""; | |
1481 QString dBMaxText = ""; | |
1482 | |
1483 if (haveDb) { | |
1484 int dbmxi = int(dbMax - 0.001); | |
1485 int dbmni = int(dbMin - 0.001); | |
1486 dBMinText = QString("%1").arg(dbmni); | |
1487 if (dbmxi != dbmni) dBMaxText = QString(" - %1").arg(dbmxi); | |
1488 } | |
1489 | |
1490 QFontMetrics metrics = paint.fontMetrics(); | |
1491 | |
1492 int labelwidth = | |
1493 std::max(std::max(metrics.width(timeLabel), | |
1494 metrics.width(freqLabel)), | |
1495 metrics.width(dBLabel)); | |
1496 | |
1497 int minwidth = | |
1498 std::max(std::max(metrics.width(timeMinText), | |
1499 metrics.width(freqMinText)), | |
1500 metrics.width(dBMinText)); | |
1501 | |
1502 int maxwidth = | |
1503 std::max(std::max(metrics.width(timeMaxText), | |
1504 metrics.width(freqMaxText)), | |
1505 metrics.width(dBMaxText)); | |
1506 | |
1507 int boxwidth = labelwidth + minwidth + maxwidth; | |
1508 | |
1509 int fontAscent = metrics.ascent(); | |
1510 int fontHeight = metrics.height(); | |
1511 | |
1512 int boxheight = fontHeight * 3 + 4; | |
1513 | |
1514 // paint.setPen(Qt::white); | |
1515 // paint.setBrush(Qt::NoBrush); | |
1516 | |
1517 //!!! int xbase = m_view->width() - boxwidth - 20; | |
1518 int xbase = rect.x() + 5; | |
1519 int ybase = rect.y() + 5; | |
1520 | |
1521 paint.drawRect(xbase, ybase, boxwidth + 10, | |
1522 boxheight + 10 - metrics.descent() + 1); | |
1523 | |
1524 paint.drawText(xbase + 5 + labelwidth - metrics.width(timeLabel), | |
1525 ybase + 5 + fontAscent, timeLabel); | |
1526 | |
1527 paint.drawText(xbase + 5 + labelwidth - metrics.width(freqLabel), | |
1528 ybase + 7 + fontAscent + fontHeight, freqLabel); | |
1529 | |
1530 paint.drawText(xbase + 5 + labelwidth - metrics.width(dBLabel), | |
1531 ybase + 9 + fontAscent + fontHeight * 2, dBLabel); | |
1532 | |
1533 paint.drawText(xbase + 5 + labelwidth + minwidth - metrics.width(timeMinText), | |
1534 ybase + 5 + fontAscent, timeMinText); | |
1535 | |
1536 paint.drawText(xbase + 5 + labelwidth + minwidth - metrics.width(freqMinText), | |
1537 ybase + 7 + fontAscent + fontHeight, freqMinText); | |
1538 | |
1539 paint.drawText(xbase + 5 + labelwidth + minwidth - metrics.width(dBMinText), | |
1540 ybase + 9 + fontAscent + fontHeight * 2, dBMinText); | |
1541 | |
1542 paint.drawText(xbase + 5 + labelwidth + minwidth, | |
1543 ybase + 5 + fontAscent, timeMaxText); | |
1544 | |
1545 paint.drawText(xbase + 5 + labelwidth + minwidth, | |
1546 ybase + 7 + fontAscent + fontHeight, freqMaxText); | |
1547 | |
1548 paint.drawText(xbase + 5 + labelwidth + minwidth, | |
1549 ybase + 9 + fontAscent + fontHeight * 2, dBMaxText); | |
1550 } | |
1551 | |
1552 /*!!! | |
1553 | |
1554 bool | |
1555 SpectrogramLayer::identifyLocalFeatures(bool on, int x, int y) | |
1556 { | |
1557 return true; //!!! | |
1558 | |
1559 m_identify = on; | |
1560 m_identifyX = x; | |
1561 m_identifyY = y; | |
1562 | |
1563 m_view->update(); | |
1564 */ | |
1565 /* | |
1566 if (!m_model || !m_model->isOK()) return false; | |
1567 | |
1568 std::cerr << "SpectrogramLayer::identifyLocalFeatures(" << on << "," << x << "," << y << ")" << std::endl; | |
1569 | |
1570 float dbMin = 0, dbMax = 0; | |
1571 float freqMin = 0, freqMax = 0; | |
1572 RealTime rtMin, rtMax; | |
1573 | |
1574 if (getXBinSourceRange(x, rtMin, rtMax)) { | |
1575 std::cerr << "Times: " << rtMin << " -> " << rtMax << std::endl; | |
1576 } else return false; | |
1577 | |
1578 if (getYBinSourceRange(y, freqMin, freqMax)) { | |
1579 std::cerr << "Frequencies: " << freqMin << " -> " << freqMax << std::endl; | |
1580 } else return false; | |
1581 | |
1582 if (getXYBinSourceRange(x, y, dbMin, dbMax)) { | |
1583 std::cerr << "dB: " << dbMin << " -> " << dbMax << std::endl; | |
1584 } | |
1585 | |
1586 m_identifyX = x; | |
1587 m_identifyY = y; | |
1588 m_identify = true; | |
1589 */ | |
1590 /*!!! | |
1591 return true; | |
1592 } | |
1593 */ | |
1594 int | |
1595 SpectrogramLayer::getVerticalScaleWidth(QPainter &paint) const | |
1596 { | |
1597 if (!m_model || !m_model->isOK()) return 0; | |
1598 | |
1599 int tw = paint.fontMetrics().width(QString("%1") | |
1600 .arg(m_maxFrequency > 0 ? | |
1601 m_maxFrequency - 1 : | |
1602 m_model->getSampleRate() / 2)); | |
1603 | |
1604 int fw = paint.fontMetrics().width(QString("43Hz")); | |
1605 if (tw < fw) tw = fw; | |
1606 | |
1607 return tw + 13; | |
1608 } | |
1609 | |
1610 void | |
1611 SpectrogramLayer::paintVerticalScale(QPainter &paint, QRect rect) const | |
1612 { | |
1613 if (!m_model || !m_model->isOK()) { | |
1614 return; | |
1615 } | |
1616 | |
1617 int h = rect.height(), w = rect.width(); | |
1618 | |
1619 size_t bins = m_windowSize / 2; | |
1620 int sr = m_model->getSampleRate(); | |
1621 | |
1622 if (m_maxFrequency > 0) { | |
1623 bins = int((double(m_maxFrequency) * m_windowSize) / sr + 0.1); | |
1624 if (bins > m_windowSize / 2) bins = m_windowSize / 2; | |
1625 } | |
1626 | |
1627 int py = -1; | |
1628 int textHeight = paint.fontMetrics().height(); | |
1629 int toff = -textHeight + paint.fontMetrics().ascent() + 2; | |
1630 | |
1631 int bin = -1; | |
1632 | |
1633 for (int y = 0; y < m_view->height(); ++y) { | |
1634 | |
1635 float q0, q1; | |
1636 if (!getYBinRange(m_view->height() - y, q0, q1)) continue; | |
1637 | |
1638 int vy; | |
1639 | |
1640 if (int(q0) > bin) { | |
1641 vy = y; | |
1642 bin = int(q0); | |
1643 } else { | |
1644 continue; | |
1645 } | |
1646 | |
1647 int freq = (sr * (bin + 1)) / m_windowSize; | |
1648 | |
1649 if (py >= 0 && (vy - py) < textHeight - 1) { | |
1650 paint.drawLine(w - 4, h - vy, w, h - vy); | |
1651 continue; | |
1652 } | |
1653 | |
1654 QString text = QString("%1").arg(freq); | |
1655 if (bin == 0) text = QString("%1Hz").arg(freq); | |
1656 paint.drawLine(0, h - vy, w, h - vy); | |
1657 | |
1658 if (h - vy - textHeight >= -2) { | |
1659 int tx = w - 10 - paint.fontMetrics().width(text); | |
1660 paint.drawText(tx, h - vy + toff, text); | |
1661 } | |
1662 | |
1663 py = vy; | |
1664 } | |
1665 } | |
1666 | |
1667 #ifdef INCLUDE_MOCFILES | |
1668 #include "SpectrogramLayer.moc.cpp" | |
1669 #endif | |
1670 |