]> git.sesse.net Git - nageru/blobdiff - nageru/peak_display.cpp
Begin working on a delay analyzer.
[nageru] / nageru / peak_display.cpp
diff --git a/nageru/peak_display.cpp b/nageru/peak_display.cpp
new file mode 100644 (file)
index 0000000..eda6b54
--- /dev/null
@@ -0,0 +1,42 @@
+#include "peak_display.h"
+
+#include <math.h>
+
+#include <memory>
+
+#include <QPainter>
+#include <QPaintEvent>
+#include <QRect>
+
+#include "audio_clip.h"
+
+using namespace std;
+
+PeakDisplay::PeakDisplay(QWidget *parent)
+       : QWidget(parent)
+{
+}
+
+void PeakDisplay::audio_clip_updated()
+{
+       QMetaObject::invokeMethod(this, "repaint", Qt::QueuedConnection);
+}
+
+void PeakDisplay::paintEvent(QPaintEvent *event)
+{
+       int w = width();
+       unique_ptr<pair<float, float>[]> min_max = audio_clip->get_min_max_peaks(w);
+
+       QPainter painter(this);
+       painter.fillRect(event->rect(), Qt::white);
+       painter.setClipRect(event->rect());
+       double mid_y = double(height()) * 0.5;
+       double scale_y = height() * 0.5;
+       for (int x = 0; x < w; ++x) {
+               if (isnan(min_max[x].first)) continue;
+
+               int y_min = lrint(min_max[x].first * scale_y + mid_y);
+               int y_max = lrint(min_max[x].second * scale_y + mid_y);
+               painter.drawLine(x, y_min, x, y_max);
+       }
+}