From 2dd793bdee19397d6817413eaeef74771d87ecec Mon Sep 17 00:00:00 2001 From: Gitea Date: Fri, 30 Aug 2019 12:22:03 +0200 Subject: [PATCH 1/9] Fixes warnings --- device.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/device.cpp b/device.cpp index a022925..9c4fbfb 100644 --- a/device.cpp +++ b/device.cpp @@ -12,6 +12,8 @@ Device::Device(QObject *parent) : qint64 Device::readData(char *data, qint64 maxlen) { + Q_UNUSED(data) + Q_UNUSED(maxlen) qFatal("oida"); } @@ -24,7 +26,7 @@ qint64 Device::writeData(const char *data, qint64 len) } FakeDevice::FakeDevice(QObject *parent) : - QObject(parent), m_timerId(startTimer(1000/60)) + QObject(parent), m_timerId(startTimer(1000/60)), m_dingsDesHaltHochZaehlt(0) { } From 06354e55c542e5e9f8a1120ca5298150921bc4b8 Mon Sep 17 00:00:00 2001 From: Gitea Date: Fri, 30 Aug 2019 12:23:10 +0200 Subject: [PATCH 2/9] Templatize SamplePair --- device.h | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/device.h b/device.h index 5698a6e..e90e9e4 100644 --- a/device.h +++ b/device.h @@ -5,10 +5,12 @@ #define qvoid void -struct SamplePair { - qint16 x, y; +template struct SamplePairT { + T x, y; }; +typedef SamplePairT SamplePair; + class Device : public QIODevice { Q_OBJECT From a7a408a7aeb7c4f1ecc5aa3a3f0e060aafad49d8 Mon Sep 17 00:00:00 2001 From: Gitea Date: Fri, 30 Aug 2019 12:27:52 +0200 Subject: [PATCH 3/9] Auto-selects devices containing "monitor" in name --- mainwindow.cpp | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/mainwindow.cpp b/mainwindow.cpp index f9f4f57..39f2f81 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -16,7 +16,15 @@ MainWindow::MainWindow(QWidget *parent) : //connect(&m_fakeDevice, &FakeDevice::samplesReceived, m_ui.widget, &OsciWidget::samplesReceived); for (const auto &device : m_audioDevices) - m_ui.comboBoxDevices->addItem(device.deviceName()); + { + auto name = device.deviceName(); + m_ui.comboBoxDevices->addItem(name); + // Select last element containing monitor if available + if(name.contains("monitor")) + { + m_ui.comboBoxDevices->setCurrentIndex(m_ui.comboBoxDevices->count()-1); + } + } for (const auto samplerate : { 44100, 48000, 96000, 192000 }) m_ui.comboBoxSamplerate->addItem(tr("%0").arg(samplerate), samplerate); From 5e24aba5a69b6df697d26ec971a9566b645705d5 Mon Sep 17 00:00:00 2001 From: Gitea Date: Fri, 30 Aug 2019 12:28:40 +0200 Subject: [PATCH 4/9] Fail with error message in case no input audio device was found --- mainwindow.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/mainwindow.cpp b/mainwindow.cpp index 39f2f81..37e1f75 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -96,6 +96,9 @@ void MainWindow::toggle() format.setCodec("audio/pcm"); format.setByteOrder(QAudioFormat::LittleEndian); + if(m_audioDevices.empty()){ + qFatal("No audio devices found"); + } m_input = std::make_unique(m_audioDevices.at(m_ui.comboBoxDevices->currentIndex()), format); m_input->start(&m_device); m_input->setBufferSize(format.sampleRate()/60*sizeof(qint16)*2); From 8d402dd86c4fbfe5265f0d8ee3bd0f309d19450b Mon Sep 17 00:00:00 2001 From: Gitea Date: Fri, 30 Aug 2019 12:29:14 +0200 Subject: [PATCH 5/9] Restructure MainWindow initialization --- mainwindow.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/mainwindow.cpp b/mainwindow.cpp index 37e1f75..070e028 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -5,9 +5,10 @@ #include #include -MainWindow::MainWindow(QWidget *parent) : - QMainWindow(parent), - m_audioDevices(QAudioDeviceInfo::availableDevices(QAudio::AudioInput)) +MainWindow::MainWindow(QWidget *parent) + : QMainWindow(parent) + , m_audioDevices(QAudioDeviceInfo::availableDevices(QAudio::AudioInput)) + , m_ui() { m_ui.setupUi(this); From 41d557743a11a42c7d393bee169aff08cf0254d4 Mon Sep 17 00:00:00 2001 From: Gitea Date: Fri, 30 Aug 2019 12:38:15 +0200 Subject: [PATCH 6/9] Replaces blend pixmap with fillRect --- osciwidget.cpp | 15 +++++---------- osciwidget.h | 1 - 2 files changed, 5 insertions(+), 11 deletions(-) diff --git a/osciwidget.cpp b/osciwidget.cpp index c7ffd10..94e2ea5 100644 --- a/osciwidget.cpp +++ b/osciwidget.cpp @@ -22,7 +22,6 @@ OsciWidget::OsciWidget(QWidget *parent) : { restartTimer(); resizePixmap(); - createBlendPixmap(); } int OsciWidget::framerate() const @@ -60,7 +59,6 @@ void OsciWidget::setBlend(int blend) qDebug() << "change blend to" << blend; m_blend = blend; - createBlendPixmap(); } void OsciWidget::setFactor(float factor) @@ -109,10 +107,11 @@ void OsciWidget::paintEvent(QPaintEvent *event) painter.begin(this); painter.drawPixmap(0, 0, m_pixmap); painter.end(); + // Fade pixmap by multiplying all pixels by m_blend painter.begin(&m_pixmap); painter.setCompositionMode(QPainter::CompositionMode_Multiply); - painter.drawPixmap(0, 0, m_fadeoutPixmap); + painter.fillRect(m_pixmap.rect(), QColor(m_blend,m_blend,m_blend)); painter.end(); } @@ -131,7 +130,6 @@ void OsciWidget::resizeEvent(QResizeEvent *event) Q_UNUSED(event) resizePixmap(); - createBlendPixmap(); } void OsciWidget::restartTimer() @@ -142,14 +140,11 @@ void OsciWidget::restartTimer() m_timerId = startTimer(1000/m_framerate); } +{ +} + void OsciWidget::resizePixmap() { m_pixmap = QPixmap(size()); m_pixmap.fill(QColor()); } - -void OsciWidget::createBlendPixmap() -{ - m_fadeoutPixmap = QPixmap(size()); - m_fadeoutPixmap.fill(QColor(m_blend, m_blend, m_blend)); -} diff --git a/osciwidget.h b/osciwidget.h index 64d2ab0..2d54d26 100644 --- a/osciwidget.h +++ b/osciwidget.h @@ -46,7 +46,6 @@ private: float m_factor{4.f}; QPixmap m_pixmap; - QPixmap m_fadeoutPixmap; std::optional m_lastPoint; }; From b0ee466c323fe5a21aec373967200af9e389cd67 Mon Sep 17 00:00:00 2001 From: Gitea Date: Fri, 30 Aug 2019 12:43:25 +0200 Subject: [PATCH 7/9] Introduces public methods for starting and stopping the display refresh timer to osciwidget --- osciwidget.cpp | 8 +++++++- osciwidget.h | 2 ++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/osciwidget.cpp b/osciwidget.cpp index 94e2ea5..3e7c07d 100644 --- a/osciwidget.cpp +++ b/osciwidget.cpp @@ -132,15 +132,21 @@ void OsciWidget::resizeEvent(QResizeEvent *event) resizePixmap(); } -void OsciWidget::restartTimer() +void OsciWidget::stop() { if (m_timerId != -1) killTimer(m_timerId); +} +void OsciWidget::start() +{ m_timerId = startTimer(1000/m_framerate); } +void OsciWidget::restartTimer() { + stop(); + start(); } void OsciWidget::resizePixmap() diff --git a/osciwidget.h b/osciwidget.h index 2d54d26..98740d7 100644 --- a/osciwidget.h +++ b/osciwidget.h @@ -22,6 +22,8 @@ public: int framerate() const; int blend() const; float factor() const; + void start(); + void stop(); public slots: void setFramerate(int framerate); From 5f0687ca48ea65d098538d126f8482c97bb8d2ee Mon Sep 17 00:00:00 2001 From: Gitea Date: Fri, 30 Aug 2019 13:38:36 +0200 Subject: [PATCH 8/9] Replaces QPoint with QPointF, Uses .length instead of pythagoras, Uses setOpacity instead of pen.setColor --- osciwidget.cpp | 35 +++++++++++++---------------------- osciwidget.h | 2 +- 2 files changed, 14 insertions(+), 23 deletions(-) diff --git a/osciwidget.cpp b/osciwidget.cpp index 3e7c07d..330a64a 100644 --- a/osciwidget.cpp +++ b/osciwidget.cpp @@ -4,19 +4,6 @@ #include -namespace { -template -auto pythagoras(const T &dx, const T &dy) -{ - return std::sqrt(dx * dx + dy * dy); -} - -auto pythagoras(const QLine &line) -{ - return pythagoras(line.dx(), line.dy()); -} -} - OsciWidget::OsciWidget(QWidget *parent) : QWidget(parent) { @@ -74,22 +61,26 @@ void OsciWidget::renderSamples(const SamplePair *begin, const SamplePair *end) QPen pen; pen.setWidth(2); + pen.setColor(QColor(0, 255, 0)); + painter.setPen(pen); + + // Paint from center + painter.translate(width()/2, height()/2); for (auto i = begin; i < end; i++) { - const qint32 x = (float(i->x) / std::numeric_limits::max() / 2 * width() / 2 * m_factor) + (width() / 2); - const qint32 y = (float(-i->y) / std::numeric_limits::max() / 2 * height() / 2 * m_factor) + (height() / 2); - const QPoint p{x,y}; + const qreal x = (qreal(i->x) / std::numeric_limits::max() / 2 * width() / 2 * m_factor); + const qreal y = (qreal(i->y) / std::numeric_limits::max() / 2 * height() / 2 * m_factor); + const QPointF p{x, y}; if (Q_LIKELY(m_lastPoint.has_value())) { - auto dist = pythagoras(QLine(*m_lastPoint, p)); - if (dist < 1) - dist = 1; - - pen.setColor(QColor(0, 1./dist*255, 0)); - painter.setPen(pen); + auto speed = QLineF(*m_lastPoint, p).length(); + if (speed < 1) + speed = 1; + auto brightness = 1./speed; + painter.setOpacity(brightness); painter.drawLine(*m_lastPoint, p); } diff --git a/osciwidget.h b/osciwidget.h index 98740d7..70641f1 100644 --- a/osciwidget.h +++ b/osciwidget.h @@ -49,5 +49,5 @@ private: QPixmap m_pixmap; - std::optional m_lastPoint; + std::optional m_lastPoint; }; From 25fedfa36c56b2212f5ecbae3585ed9ac6562f7b Mon Sep 17 00:00:00 2001 From: Gitea Date: Fri, 30 Aug 2019 13:39:43 +0200 Subject: [PATCH 9/9] Uses QOpenGLWidget instead of QWidget --- osciwidget.cpp | 2 +- osciwidget.h | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/osciwidget.cpp b/osciwidget.cpp index 330a64a..97a82d0 100644 --- a/osciwidget.cpp +++ b/osciwidget.cpp @@ -5,7 +5,7 @@ #include OsciWidget::OsciWidget(QWidget *parent) : - QWidget(parent) + QOpenGLWidget(parent) { restartTimer(); resizePixmap(); diff --git a/osciwidget.h b/osciwidget.h index 70641f1..77a75a8 100644 --- a/osciwidget.h +++ b/osciwidget.h @@ -1,6 +1,8 @@ #pragma once #include + +#include #include #include #include @@ -12,7 +14,7 @@ #include "device.h" -class OsciWidget : public QWidget +class OsciWidget : public QOpenGLWidget { Q_OBJECT