comparison layer/WaveformLayer.cpp @ 0:2a4f26e85b4c

initial import
author Chris Cannam
date Tue, 10 Jan 2006 16:33:16 +0000
parents
children 77dad696d740
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 "WaveformLayer.h"
11
12 #include "base/AudioLevel.h"
13 #include "base/View.h"
14 #include "base/Profiler.h"
15
16 #include <QPainter>
17 #include <QPixmap>
18
19 #include <iostream>
20 #include <cmath>
21
22 using std::cerr;
23 using std::endl;
24
25 WaveformLayer::WaveformLayer(View *w) :
26 Layer(w),
27 m_model(0),
28 m_gain(1.0f),
29 m_colour(Qt::black),
30 m_showMeans(true),
31 m_greyscale(true),
32 m_channelMode(SeparateChannels),
33 m_channel(-1),
34 m_scale(LinearScale),
35 m_aggressive(false),
36 m_cache(0),
37 m_cacheValid(false)
38 {
39 m_view->addLayer(this);
40 }
41
42 WaveformLayer::~WaveformLayer()
43 {
44 delete m_cache;
45 }
46
47 void
48 WaveformLayer::setModel(const RangeSummarisableTimeValueModel *model)
49 {
50 m_model = model;
51 m_cacheValid = false;
52 if (!m_model || !m_model->isOK()) return;
53
54 connect(m_model, SIGNAL(modelChanged()), this, SIGNAL(modelChanged()));
55 connect(m_model, SIGNAL(modelChanged(size_t, size_t)),
56 this, SIGNAL(modelChanged(size_t, size_t)));
57
58 connect(m_model, SIGNAL(completionChanged()),
59 this, SIGNAL(modelCompletionChanged()));
60
61 emit modelReplaced();
62 }
63
64 Layer::PropertyList
65 WaveformLayer::getProperties() const
66 {
67 PropertyList list;
68 list.push_back(tr("Colour"));
69 list.push_back(tr("Scale"));
70 list.push_back(tr("Gain"));
71 list.push_back(tr("Merge Channels"));
72 return list;
73 }
74
75 Layer::PropertyType
76 WaveformLayer::getPropertyType(const PropertyName &name) const
77 {
78 if (name == tr("Gain")) return RangeProperty;
79 if (name == tr("Colour")) return ValueProperty;
80 if (name == tr("Merge Channels")) return ToggleProperty;
81 if (name == tr("Scale")) return ValueProperty;
82 return InvalidProperty;
83 }
84
85 QString
86 WaveformLayer::getPropertyGroupName(const PropertyName &name) const
87 {
88 if (name == tr("Gain") ||
89 name == tr("Scale")) return tr("Scale");
90 return QString();
91 }
92
93 int
94 WaveformLayer::getPropertyRangeAndValue(const PropertyName &name,
95 int *min, int *max) const
96 {
97 int deft = 0;
98
99 if (name == tr("Gain")) {
100
101 *min = -50;
102 *max = 50;
103
104 deft = int(nearbyint(log10(m_gain) * 20.0));
105 if (deft < *min) deft = *min;
106 if (deft > *max) deft = *max;
107
108 } else if (name == tr("Colour")) {
109
110 *min = 0;
111 *max = 5;
112
113 if (m_colour == Qt::black) deft = 0;
114 else if (m_colour == Qt::darkRed) deft = 1;
115 else if (m_colour == Qt::darkBlue) deft = 2;
116 else if (m_colour == Qt::darkGreen) deft = 3;
117 else if (m_colour == QColor(200, 50, 255)) deft = 4;
118 else if (m_colour == QColor(255, 150, 50)) deft = 5;
119
120 } else if (name == tr("Merge Channels")) {
121
122 deft = ((m_channelMode == MergeChannels) ? 1 : 0);
123
124 } else if (name == tr("Scale")) {
125
126 *min = 0;
127 *max = 2;
128
129 deft = (int)m_scale;
130
131 } else {
132 deft = Layer::getPropertyRangeAndValue(name, min, max);
133 }
134
135 return deft;
136 }
137
138 QString
139 WaveformLayer::getPropertyValueLabel(const PropertyName &name,
140 int value) const
141 {
142 if (name == tr("Colour")) {
143 switch (value) {
144 default:
145 case 0: return tr("Black");
146 case 1: return tr("Red");
147 case 2: return tr("Blue");
148 case 3: return tr("Green");
149 case 4: return tr("Purple");
150 case 5: return tr("Orange");
151 }
152 }
153 if (name == tr("Scale")) {
154 switch (value) {
155 default:
156 case 0: return tr("Linear");
157 case 1: return tr("Meter");
158 case 2: return tr("dB");
159 }
160 }
161 return tr("<unknown>");
162 }
163
164 void
165 WaveformLayer::setProperty(const PropertyName &name, int value)
166 {
167 if (name == tr("Gain")) {
168 setGain(pow(10, float(value)/20.0));
169 } else if (name == tr("Colour")) {
170 switch (value) {
171 default:
172 case 0: setBaseColour(Qt::black); break;
173 case 1: setBaseColour(Qt::darkRed); break;
174 case 2: setBaseColour(Qt::darkBlue); break;
175 case 3: setBaseColour(Qt::darkGreen); break;
176 case 4: setBaseColour(QColor(200, 50, 255)); break;
177 case 5: setBaseColour(QColor(255, 150, 50)); break;
178 }
179 } else if (name == tr("Merge Channels")) {
180 setChannelMode(value ? MergeChannels : SeparateChannels);
181 } else if (name == tr("Scale")) {
182 switch (value) {
183 default:
184 case 0: setScale(LinearScale); break;
185 case 1: setScale(MeterScale); break;
186 case 2: setScale(dBScale); break;
187 }
188 }
189 }
190
191 /*
192
193 int
194 WaveformLayer::getProperty(const PropertyName &name)
195 {
196 if (name == "Gain") {
197 return int((getGain() - 1.0) * 10.0 + 0.01);
198 }
199 if (name == "Colour") {
200
201 */
202
203 void
204 WaveformLayer::setGain(float gain) //!!! inadequate for floats!
205 {
206 if (m_gain == gain) return;
207 m_gain = gain;
208 m_cacheValid = false;
209 emit layerParametersChanged();
210 }
211
212 void
213 WaveformLayer::setBaseColour(QColor colour)
214 {
215 if (m_colour == colour) return;
216 m_colour = colour;
217 m_cacheValid = false;
218 emit layerParametersChanged();
219 }
220
221 void
222 WaveformLayer::setShowMeans(bool showMeans)
223 {
224 if (m_showMeans == showMeans) return;
225 m_showMeans = showMeans;
226 m_cacheValid = false;
227 emit layerParametersChanged();
228 }
229
230 void
231 WaveformLayer::setUseGreyscale(bool useGreyscale)
232 {
233 if (m_greyscale == useGreyscale) return;
234 m_greyscale = useGreyscale;
235 m_cacheValid = false;
236 emit layerParametersChanged();
237 }
238
239 void
240 WaveformLayer::setChannelMode(ChannelMode channelMode)
241 {
242 if (m_channelMode == channelMode) return;
243 m_channelMode = channelMode;
244 m_cacheValid = false;
245 emit layerParametersChanged();
246 }
247
248 void
249 WaveformLayer::setChannel(int channel)
250 {
251 std::cerr << "WaveformLayer::setChannel(" << channel << ")" << std::endl;
252
253 if (m_channel == channel) return;
254 m_channel = channel;
255 m_cacheValid = false;
256 emit layerParametersChanged();
257 }
258
259 void
260 WaveformLayer::setScale(Scale scale)
261 {
262 if (m_scale == scale) return;
263 m_scale = scale;
264 m_cacheValid = false;
265 emit layerParametersChanged();
266 }
267
268 void
269 WaveformLayer::setAggressiveCacheing(bool aggressive)
270 {
271 if (m_aggressive == aggressive) return;
272 m_aggressive = aggressive;
273 m_cacheValid = false;
274 emit layerParametersChanged();
275 }
276
277 int
278 WaveformLayer::getCompletion() const
279 {
280 int completion = 100;
281 if (!m_model || !m_model->isOK()) return completion;
282 if (m_model->isReady(&completion)) return 100;
283 return completion;
284 }
285
286 int
287 WaveformLayer::dBscale(float sample, int m) const
288 {
289 if (sample < 0.0) return -dBscale(-sample, m);
290 float dB = AudioLevel::multiplier_to_dB(sample);
291 if (dB < -50.0) return 0;
292 if (dB > 0.0) return m;
293 return int(((dB + 50.0) * m) / 50.0 + 0.1);
294 }
295
296 size_t
297 WaveformLayer::getChannelArrangement(size_t &min, size_t &max, bool &merging)
298 const
299 {
300 if (!m_model || !m_model->isOK()) return 0;
301
302 size_t channels = m_model->getChannelCount();
303 if (channels == 0) return 0;
304
305 size_t rawChannels = channels;
306
307 if (m_channel == -1) {
308 min = 0;
309 if (m_channelMode == MergeChannels) {
310 max = 0;
311 channels = 1;
312 } else {
313 max = channels - 1;
314 }
315 } else {
316 min = m_channel;
317 max = m_channel;
318 rawChannels = 1;
319 channels = 1;
320 }
321
322 merging = (m_channelMode == MergeChannels && rawChannels > 1);
323
324 // std::cerr << "WaveformLayer::getChannelArrangement: min " << min << ", max " << max << ", merging " << merging << ", channels " << channels << std::endl;
325
326 return channels;
327 }
328
329 void
330 WaveformLayer::paint(QPainter &viewPainter, QRect rect) const
331 {
332 if (!m_model || !m_model->isOK()) {
333 return;
334 }
335
336 long startFrame = m_view->getStartFrame();
337 int zoomLevel = m_view->getZoomLevel();
338
339
340 Profiler profiler("WaveformLayer::paint", true);
341 std::cerr << "WaveformLayer::paint (" << rect.x() << "," << rect.y()
342 << ") [" << rect.width() << "x" << rect.height() << "]: zoom " << zoomLevel << ", start " << startFrame << std::endl;
343
344
345 size_t channels = 0, minChannel = 0, maxChannel = 0;
346 bool mergingChannels = false;
347
348 channels = getChannelArrangement(minChannel, maxChannel, mergingChannels);
349 if (channels == 0) return;
350
351 int w = m_view->width();
352 int h = m_view->height();
353
354 bool ready = m_model->isReady();
355 QPainter *paint;
356
357 if (m_aggressive) {
358
359 if (m_cacheValid && (zoomLevel != m_cacheZoomLevel)) {
360 m_cacheValid = false;
361 }
362
363 if (m_cacheValid) {
364 viewPainter.drawPixmap(rect, *m_cache, rect);
365 return;
366 }
367
368 if (!m_cache || m_cache->width() != w || m_cache->height() != h) {
369 delete m_cache;
370 m_cache = new QPixmap(w, h);
371 }
372
373 paint = new QPainter(m_cache);
374
375 paint->setPen(Qt::NoPen);
376 paint->setBrush(m_view->palette().background());
377 paint->drawRect(rect);
378
379 paint->setPen(Qt::black);
380 paint->setBrush(Qt::NoBrush);
381
382 } else {
383 paint = &viewPainter;
384 }
385
386 int x0 = 0, x1 = w - 1;
387 int y0 = 0, y1 = h - 1;
388
389 x0 = rect.left();
390 x1 = rect.right();
391 y0 = rect.top();
392 y1 = rect.bottom();
393
394 long frame0 = startFrame + x0 * zoomLevel;
395 long frame1 = startFrame + (x1 + 1) * zoomLevel;
396
397 // std::cerr << "Painting waveform from " << frame0 << " to " << frame1 << " (" << (x1-x0+1) << " pixels at zoom " << zoomLevel << ")" << std::endl;
398
399 RangeSummarisableTimeValueModel::RangeBlock ranges;
400 RangeSummarisableTimeValueModel::RangeBlock otherChannelRanges;
401 RangeSummarisableTimeValueModel::Range range;
402
403 QColor greys[3];
404 if (m_colour == Qt::black) {
405 for (int i = 0; i < 3; ++i) {
406 int level = 192 - 64 * i;
407 greys[i] = QColor(level, level, level);
408 }
409 } else {
410 int factor = (m_view->hasLightBackground() ? 120 : 80);
411 greys[2] = m_colour.light(factor);
412 greys[1] = greys[2].light(factor);
413 greys[0] = greys[1].light(factor);
414 }
415
416 QColor midColour = m_colour;
417 if (midColour == Qt::black) {
418 midColour = Qt::gray;
419 } else if (m_view->hasLightBackground()) {
420 midColour = midColour.light(150);
421 } else {
422 midColour = midColour.light(50);
423 }
424
425 for (size_t ch = minChannel; ch <= maxChannel; ++ch) {
426
427 int prevRangeBottom = -1, prevRangeTop = -1;
428
429 int m = (h / channels) / 2;
430 int my = m + (((ch - minChannel) * h) / channels);
431
432 // std::cerr << "ch = " << ch << ", channels = " << channels << ", m = " << m << ", my = " << my << ", h = " << h << std::endl;
433
434 if (my - m > y1 || my + m < y0) continue;
435
436 paint->setPen(greys[0]);
437 paint->drawLine(x0, my, x1, my);
438
439 if (frame1 <= 0) continue;
440
441 size_t modelZoomLevel = zoomLevel;
442
443 ranges = m_model->getRanges
444 (ch, frame0 < 0 ? 0 : frame0, frame1, modelZoomLevel);
445
446 if (mergingChannels) {
447 otherChannelRanges = m_model->getRanges
448 (1, frame0 < 0 ? 0 : frame0, frame1, modelZoomLevel);
449 }
450
451 for (int x = x0; x <= x1; ++x) {
452
453 range = RangeSummarisableTimeValueModel::Range();
454 size_t index = x - x0;
455 size_t maxIndex = index;
456
457 if (frame0 < 0) {
458 if (index < size_t(-frame0 / zoomLevel)) {
459 continue;
460 } else {
461 index -= -frame0 / zoomLevel;
462 maxIndex = index;
463 }
464 }
465
466 if (int(modelZoomLevel) != zoomLevel) {
467
468 index = size_t((double(index) * zoomLevel) / modelZoomLevel);
469
470 if (int(modelZoomLevel) < zoomLevel) {
471 // Peaks may be missed! The model should avoid
472 // this by rounding zoom levels up rather than
473 // down, but we'd better cope in case it doesn't
474 maxIndex = index;
475 } else {
476 maxIndex = size_t((double(index + 1) * zoomLevel)
477 / modelZoomLevel) - 1;
478 }
479 }
480
481 if (index < ranges.size()) {
482
483 range = ranges[index];
484
485 if (maxIndex > index && maxIndex < ranges.size()) {
486 range.max = std::max(range.max, ranges[maxIndex].max);
487 range.min = std::min(range.min, ranges[maxIndex].min);
488 range.absmean = (range.absmean +
489 ranges[maxIndex].absmean) / 2;
490 }
491
492 } else {
493 continue;
494 }
495
496 int rangeBottom = 0, rangeTop = 0, meanBottom = 0, meanTop = 0;
497
498 if (mergingChannels) {
499
500 if (index < otherChannelRanges.size()) {
501
502 range.max = fabsf(range.max);
503 range.min = -fabsf(otherChannelRanges[index].max);
504 range.absmean = (range.absmean +
505 otherChannelRanges[index].absmean) / 2;
506
507 if (maxIndex > index && maxIndex < ranges.size()) {
508 // let's not concern ourselves about the mean
509 range.min = std::min
510 (range.min,
511 -fabsf(otherChannelRanges[maxIndex].max));
512 }
513 }
514 }
515
516 int greyLevels = 1;
517 if (m_greyscale && (m_scale == LinearScale)) greyLevels = 4;
518
519 switch (m_scale) {
520
521 case LinearScale:
522 rangeBottom = int( m * greyLevels * range.min * m_gain);
523 rangeTop = int( m * greyLevels * range.max * m_gain);
524 meanBottom = int(-m * range.absmean * m_gain);
525 meanTop = int( m * range.absmean * m_gain);
526 break;
527
528 case dBScale:
529 rangeBottom = dBscale(range.min * m_gain, m * greyLevels);
530 rangeTop = dBscale(range.max * m_gain, m * greyLevels);
531 meanBottom = -dBscale(range.absmean * m_gain, m);
532 meanTop = dBscale(range.absmean * m_gain, m);
533 break;
534
535 case MeterScale:
536 rangeBottom = AudioLevel::multiplier_to_preview(range.min * m_gain, m * greyLevels);
537 rangeTop = AudioLevel::multiplier_to_preview(range.max * m_gain, m * greyLevels);
538 meanBottom = -AudioLevel::multiplier_to_preview(range.absmean * m_gain, m);
539 meanTop = AudioLevel::multiplier_to_preview(range.absmean * m_gain, m);
540 }
541
542 int topFill = (rangeTop < 0 ? -rangeTop : rangeTop) % greyLevels;
543 int bottomFill = (rangeBottom < 0 ? -rangeBottom : rangeBottom) % greyLevels;
544 rangeTop = rangeTop / greyLevels;
545 rangeBottom = rangeBottom / greyLevels;
546
547 bool clipped = false;
548 if (rangeTop < -m) { rangeTop = -m; clipped = true; }
549 if (rangeTop > m) { rangeTop = m; clipped = true; }
550 if (rangeBottom < -m) { rangeBottom = -m; clipped = true; }
551 if (rangeBottom > m) { rangeBottom = m; clipped = true; }
552
553 rangeBottom = my - rangeBottom;
554 rangeTop = my - rangeTop;
555 meanBottom = my - meanBottom;
556 meanTop = my - meanTop;
557
558 if (meanBottom > rangeBottom) meanBottom = rangeBottom;
559 if (meanTop < rangeTop) meanTop = rangeTop;
560
561 bool drawMean = m_showMeans;
562 if (meanTop == rangeTop) {
563 if (meanTop < meanBottom) ++meanTop;
564 else drawMean = false;
565 }
566 if (meanBottom == rangeBottom) {
567 if (meanBottom > meanTop) --meanBottom;
568 else drawMean = false;
569 }
570
571 if (x != x0 && prevRangeBottom != -1) {
572 if (prevRangeBottom > rangeBottom &&
573 prevRangeTop > rangeBottom) {
574 paint->setPen(midColour);
575 paint->drawLine(x-1, prevRangeTop, x, rangeBottom);
576 paint->setPen(m_colour);
577 paint->drawPoint(x-1, prevRangeTop);
578 } else if (prevRangeBottom < rangeTop &&
579 prevRangeTop < rangeTop) {
580 paint->setPen(midColour);
581 paint->drawLine(x-1, prevRangeBottom, x, rangeTop);
582 paint->setPen(m_colour);
583 paint->drawPoint(x-1, prevRangeBottom);
584 }
585 }
586
587 if (ready) {
588 if (clipped ||
589 range.min * m_gain <= -1.0 ||
590 range.max * m_gain >= 1.0) {
591 paint->setPen(Qt::red);
592 } else {
593 paint->setPen(m_colour);
594 }
595 } else {
596 paint->setPen(midColour);
597 }
598
599 paint->drawLine(x, rangeBottom, x, rangeTop);
600
601 if (m_greyscale && (m_scale == LinearScale) && ready) {
602 if (!clipped) {
603 if (rangeTop < rangeBottom) {
604 if (topFill > 0 &&
605 (!drawMean || (rangeTop < meanTop - 1))) {
606 paint->setPen(greys[topFill - 1]);
607 paint->drawPoint(x, rangeTop - 1);
608 }
609 if (bottomFill > 0 &&
610 (!drawMean || (rangeBottom > meanBottom + 1))) {
611 paint->setPen(greys[bottomFill - 1]);
612 paint->drawPoint(x, rangeBottom + 1);
613 }
614 }
615 }
616 }
617
618 if (drawMean) {
619 paint->setPen(midColour);
620 paint->drawLine(x, meanBottom, x, meanTop);
621 }
622
623 prevRangeBottom = rangeBottom;
624 prevRangeTop = rangeTop;
625 }
626 }
627
628 if (m_aggressive) {
629 if (ready && rect == m_view->rect()) {
630 m_cacheValid = true;
631 m_cacheZoomLevel = zoomLevel;
632 }
633 paint->end();
634 delete paint;
635 viewPainter.drawPixmap(rect, *m_cache, rect);
636 }
637 }
638
639 int
640 WaveformLayer::getVerticalScaleWidth(QPainter &paint) const
641 {
642 if (m_scale == LinearScale) {
643 return paint.fontMetrics().width("0.0") + 13;
644 } else {
645 return std::max(paint.fontMetrics().width(tr("0dB")),
646 paint.fontMetrics().width(tr("-Inf"))) + 13;
647 }
648 }
649
650 void
651 WaveformLayer::paintVerticalScale(QPainter &paint, QRect rect) const
652 {
653 if (!m_model || !m_model->isOK()) {
654 return;
655 }
656
657 size_t channels = 0, minChannel = 0, maxChannel = 0;
658 bool mergingChannels = false;
659
660 channels = getChannelArrangement(minChannel, maxChannel, mergingChannels);
661 if (channels == 0) return;
662
663 int h = rect.height(), w = rect.width();
664 int textHeight = paint.fontMetrics().height();
665 int toff = -textHeight/2 + paint.fontMetrics().ascent() + 1;
666
667 for (size_t ch = minChannel; ch <= maxChannel; ++ch) {
668
669 int m = (h / channels) / 2;
670 int my = m + (((ch - minChannel) * h) / channels);
671 int py = -1;
672
673 for (int i = 0; i <= 10; ++i) {
674
675 int vy = 0;
676 QString text = "";
677
678 if (m_scale == LinearScale) {
679
680 vy = int((m * i * m_gain) / 10);
681
682 text = QString("%1").arg(float(i) / 10.0);
683 if (i == 0) text = "0.0";
684 if (i == 10) text = "1.0";
685
686 } else {
687
688 int db;
689 bool minvalue = false;
690
691 if (m_scale == MeterScale) {
692 static int dbs[] = { -50, -40, -30, -20, -15,
693 -10, -5, -3, -2, -1, 0 };
694 db = dbs[i];
695 if (db == -50) minvalue = true;
696 vy = AudioLevel::multiplier_to_preview
697 (AudioLevel::dB_to_multiplier(db) * m_gain, m);
698 } else {
699 db = -100 + i * 10;
700 if (db == -100) minvalue = true;
701 vy = dBscale
702 (AudioLevel::dB_to_multiplier(db) * m_gain, m);
703 }
704
705 text = QString("%1").arg(db);
706 if (db == 0) text = tr("0dB");
707 if (minvalue) {
708 text = tr("-Inf");
709 vy = 0;
710 }
711 }
712
713 if (vy < 0) vy = -vy;
714 if (vy >= m - 1) continue;
715
716 if (py >= 0 && (vy - py) < textHeight - 1) {
717 paint.drawLine(w - 4, my - vy, w, my - vy);
718 if (vy > 0) paint.drawLine(w - 4, my + vy, w, my + vy);
719 continue;
720 }
721
722 paint.drawLine(w - 7, my - vy, w, my - vy);
723 if (vy > 0) paint.drawLine(w - 7, my + vy, w, my + vy);
724
725 int tx = 3;
726 if (m_scale != LinearScale) {
727 tx = w - 10 - paint.fontMetrics().width(text);
728 }
729
730 paint.drawText(tx, my - vy + toff, text);
731 if (vy > 0) paint.drawText(tx, my + vy + toff, text);
732
733 py = vy;
734 }
735 }
736 }
737
738 #ifdef INCLUDE_MOCFILES
739 #include "WaveformLayer.moc.cpp"
740 #endif
741