]> git.sesse.net Git - nageru/blob - nageru/peak_display.cpp
Make the delay analyzer understand that two sources can have different starting times.
[nageru] / nageru / peak_display.cpp
1 #include "peak_display.h"
2
3 #include <math.h>
4
5 #include <memory>
6
7 #include <QPainter>
8 #include <QPaintEvent>
9 #include <QRect>
10
11 #include "audio_clip.h"
12
13 using namespace std;
14 using namespace std::chrono;
15
16 PeakDisplay::PeakDisplay(QWidget *parent)
17         : QWidget(parent)
18 {
19 }
20
21 void PeakDisplay::audio_clip_updated()
22 {
23         QMetaObject::invokeMethod(this, "repaint", Qt::QueuedConnection);
24 }
25
26 void PeakDisplay::paintEvent(QPaintEvent *event)
27 {
28         steady_clock::time_point paint_base = base;
29         if (paint_base == steady_clock::time_point() && !audio_clip->empty()) {
30                 paint_base = audio_clip->get_first_sample();
31         }
32
33         int w = width();
34         unique_ptr<pair<float, float>[]> min_max = audio_clip->get_min_max_peaks(w, paint_base);
35
36         QPainter painter(this);
37         painter.fillRect(event->rect(), Qt::white);
38         painter.setClipRect(event->rect());
39         double mid_y = double(height()) * 0.5;
40         double scale_y = height() * 0.5;
41         for (int x = 0; x < w; ++x) {
42                 if (isnan(min_max[x].first)) continue;
43
44                 int y_min = lrint(min_max[x].first * scale_y + mid_y);
45                 int y_max = lrint(min_max[x].second * scale_y + mid_y);
46                 painter.drawLine(x, y_min, x, y_max);
47         }
48 }