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) { } 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 diff --git a/mainwindow.cpp b/mainwindow.cpp index f9f4f57..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); @@ -16,7 +17,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); @@ -88,6 +97,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); diff --git a/osciwidget.cpp b/osciwidget.cpp index c7ffd10..97a82d0 100644 --- a/osciwidget.cpp +++ b/osciwidget.cpp @@ -4,25 +4,11 @@ #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) + QOpenGLWidget(parent) { restartTimer(); resizePixmap(); - createBlendPixmap(); } int OsciWidget::framerate() const @@ -60,7 +46,6 @@ void OsciWidget::setBlend(int blend) qDebug() << "change blend to" << blend; m_blend = blend; - createBlendPixmap(); } void OsciWidget::setFactor(float factor) @@ -76,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); } @@ -109,10 +98,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,15 +121,23 @@ void OsciWidget::resizeEvent(QResizeEvent *event) Q_UNUSED(event) resizePixmap(); - createBlendPixmap(); +} + +void OsciWidget::stop() +{ + if (m_timerId != -1) + killTimer(m_timerId); +} + +void OsciWidget::start() +{ + m_timerId = startTimer(1000/m_framerate); } void OsciWidget::restartTimer() { - if (m_timerId != -1) - killTimer(m_timerId); - - m_timerId = startTimer(1000/m_framerate); + stop(); + start(); } void OsciWidget::resizePixmap() @@ -147,9 +145,3 @@ 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..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 @@ -22,6 +24,8 @@ public: int framerate() const; int blend() const; float factor() const; + void start(); + void stop(); public slots: void setFramerate(int framerate); @@ -46,7 +50,6 @@ private: float m_factor{4.f}; QPixmap m_pixmap; - QPixmap m_fadeoutPixmap; - std::optional m_lastPoint; + std::optional m_lastPoint; };