yes
This commit is contained in:
@@ -42,6 +42,10 @@ MainWindow::MainWindow(QWidget *parent) :
|
||||
for (const auto framerate : {15, 30, 50, 60})
|
||||
m_ui->comboBoxFps->addItem(tr("%0 FPS").arg(framerate), framerate);
|
||||
|
||||
connect(m_ui->comboBoxFps, qOverload<int>(&QComboBox::currentIndexChanged), m_ui->widget, [this](){
|
||||
m_ui->widget->setFps(m_ui->comboBoxFps->currentData().toInt());
|
||||
});
|
||||
|
||||
m_ui->spinBoxBlend->setValue(m_ui->widget->blend());
|
||||
|
||||
connect(m_ui->spinBoxBlend, qOverload<int>(&QSpinBox::valueChanged), m_ui->widget, &OsciWidget::setBlend);
|
||||
@@ -51,7 +55,7 @@ MainWindow::MainWindow(QWidget *parent) :
|
||||
connect(m_ui->spinBoxGlow, qOverload<int>(&QSpinBox::valueChanged), m_ui->widget, &OsciWidget::setGlow);
|
||||
|
||||
auto buttonGroup = new QButtonGroup;
|
||||
buttonGroup->setExclusive(true);
|
||||
buttonGroup->setExclusive(true);
|
||||
for (auto factor : { .5f, 1.f, 2.f, 4.f, 8.f })
|
||||
{
|
||||
auto radioButton = new QRadioButton(QString::number(factor));
|
||||
@@ -78,20 +82,18 @@ void MainWindow::toggle()
|
||||
m_input->stop();
|
||||
m_ui->comboBoxDevices->setEnabled(true);
|
||||
m_ui->comboBoxSamplerate->setEnabled(true);
|
||||
m_ui->comboBoxFps->setEnabled(true);
|
||||
m_ui->pushButtonToggle->setText("▶");
|
||||
}
|
||||
else
|
||||
{
|
||||
m_input->setSamplerate(m_ui->comboBoxSamplerate->currentData().toInt());
|
||||
m_input->setFramerate(m_ui->comboBoxFps->currentData().toInt());
|
||||
m_input->setFramerate(60);
|
||||
if (auto audioDevice = dynamic_cast<AudioDevice*>(m_input.get()))
|
||||
audioDevice->setDevice(m_audioDevices.at(m_ui->comboBoxDevices->currentIndex()));
|
||||
m_input->start();
|
||||
|
||||
m_ui->comboBoxDevices->setEnabled(false);
|
||||
m_ui->comboBoxSamplerate->setEnabled(false);
|
||||
m_ui->comboBoxFps->setEnabled(false);
|
||||
m_ui->pushButtonToggle->setText("▮▮");
|
||||
}
|
||||
}
|
||||
|
@@ -1,6 +1,6 @@
|
||||
QT = core gui widgets multimedia
|
||||
|
||||
CONFIG += c++17
|
||||
CONFIG += c++14
|
||||
DEFINES += QT_DEPRECATED_WARNINGS QT_DISABLE_DEPRECATED_BEFORE=0x060000
|
||||
|
||||
SOURCES += \
|
||||
|
@@ -2,12 +2,17 @@
|
||||
|
||||
#include <cmath>
|
||||
|
||||
#include <QLine>
|
||||
#include <QLineF>
|
||||
#include <QDebug>
|
||||
#include <QPainter>
|
||||
#include <QTimerEvent>
|
||||
|
||||
OsciWidget::OsciWidget(QWidget *parent) :
|
||||
QOpenGLWidget{parent}
|
||||
QWidget{parent},
|
||||
m_redrawTimerId(startTimer(1000/m_fps))
|
||||
{
|
||||
resizePixmap();
|
||||
m_fpsTimer.start();
|
||||
}
|
||||
|
||||
int OsciWidget::blend() const
|
||||
@@ -25,6 +30,11 @@ float OsciWidget::glow() const
|
||||
return m_glow;
|
||||
}
|
||||
|
||||
int OsciWidget::fps() const
|
||||
{
|
||||
return m_fps;
|
||||
}
|
||||
|
||||
void OsciWidget::setBlend(int blend)
|
||||
{
|
||||
if (blend == m_blend)
|
||||
@@ -47,6 +57,14 @@ void OsciWidget::setGlow(float glow)
|
||||
m_glow = glow;
|
||||
}
|
||||
|
||||
void OsciWidget::setFps(int fps)
|
||||
{
|
||||
qDebug() << fps;
|
||||
killTimer(m_redrawTimerId);
|
||||
m_fps = fps;
|
||||
m_redrawTimerId = startTimer(1000/m_fps);
|
||||
}
|
||||
|
||||
void OsciWidget::renderSamples(const SamplePair *begin, const SamplePair *end)
|
||||
{
|
||||
QPainter painter;
|
||||
@@ -59,35 +77,32 @@ void OsciWidget::renderSamples(const SamplePair *begin, const SamplePair *end)
|
||||
painter.setPen(pen);
|
||||
|
||||
// Paint from center
|
||||
painter.translate(width()/2, height()/2);
|
||||
painter.translate(m_pixmap.width()/2, m_pixmap.height()/2);
|
||||
|
||||
const auto pointToCoordinates = [width=m_pixmap.width()/2,height=m_pixmap.height()/2,factor=m_factor](const QPointF &point)
|
||||
{
|
||||
return QPoint{
|
||||
int(point.x() * factor * width),
|
||||
int(point.y() * factor * height)
|
||||
};
|
||||
};
|
||||
|
||||
for (auto i = begin; i < end; i++)
|
||||
{
|
||||
const QPointF p{
|
||||
float(i->x) / std::numeric_limits<qint16>::max() / 2,
|
||||
float(-i->y) / std::numeric_limits<qint16>::max() / 2
|
||||
float(i->x) / std::numeric_limits<qint16>::max(),
|
||||
float(-i->y) / std::numeric_limits<qint16>::max()
|
||||
};
|
||||
|
||||
if (Q_LIKELY(m_lastPoint.has_value()))
|
||||
{
|
||||
const QLineF line(*m_lastPoint, p);
|
||||
|
||||
auto brightness = 1.f / line.length() / m_glow;
|
||||
if (line.length() == 0.f || brightness > 255.f)
|
||||
brightness = 255.f;
|
||||
const QLineF line(m_lastPoint, p);
|
||||
|
||||
painter.setOpacity(brightness);
|
||||
auto brightness = 1.f / line.length() / m_glow;
|
||||
if (line.length() == 0.f || brightness > 255.f)
|
||||
brightness = 255.f;
|
||||
|
||||
const auto pointToCoordinates = [this](const QPointF &point)
|
||||
{
|
||||
return QPoint{
|
||||
int((point.x() * width() / 2 * m_factor)),
|
||||
int((point.y() * height() / 2 * m_factor))
|
||||
};
|
||||
};
|
||||
painter.setOpacity(brightness);
|
||||
|
||||
painter.drawLine(pointToCoordinates(*m_lastPoint), pointToCoordinates(p));
|
||||
}
|
||||
painter.drawLine(pointToCoordinates(m_lastPoint), pointToCoordinates(p));
|
||||
|
||||
m_lastPoint = p;
|
||||
}
|
||||
@@ -97,29 +112,51 @@ void OsciWidget::renderSamples(const SamplePair *begin, const SamplePair *end)
|
||||
painter.fillRect(m_pixmap.rect(), QColor(m_blend,m_blend,m_blend));
|
||||
|
||||
painter.end();
|
||||
|
||||
repaint();
|
||||
}
|
||||
|
||||
void OsciWidget::paintEvent(QPaintEvent *event)
|
||||
{
|
||||
Q_UNUSED(event)
|
||||
QWidget::paintEvent(event);
|
||||
|
||||
m_frameCounter++;
|
||||
if (m_fpsTimer.hasExpired(1000))
|
||||
{
|
||||
m_displayFrameCounter = m_frameCounter;
|
||||
m_frameCounter = 0;
|
||||
m_fpsTimer.restart();
|
||||
}
|
||||
|
||||
QPainter painter;
|
||||
painter.begin(this);
|
||||
painter.drawPixmap(0, 0, m_pixmap);
|
||||
|
||||
painter.setPen(Qt::white);
|
||||
painter.setBrush(Qt::white);
|
||||
QFont font;
|
||||
font.setPixelSize(24);
|
||||
painter.drawText(20, 20, QString("%0FPS").arg(m_displayFrameCounter));
|
||||
|
||||
painter.end();
|
||||
|
||||
if (m_pixmap.rect() != rect())
|
||||
qDebug() << m_pixmap.rect() << rect();
|
||||
}
|
||||
|
||||
void OsciWidget::timerEvent(QTimerEvent *event)
|
||||
{
|
||||
QWidget::timerEvent(event);
|
||||
if (event->timerId() == m_redrawTimerId)
|
||||
repaint();
|
||||
}
|
||||
|
||||
void OsciWidget::resizeEvent(QResizeEvent *event)
|
||||
{
|
||||
Q_UNUSED(event)
|
||||
|
||||
QWidget::resizeEvent(event);
|
||||
resizePixmap();
|
||||
}
|
||||
|
||||
void OsciWidget::resizePixmap()
|
||||
{
|
||||
m_pixmap = QPixmap(size());
|
||||
m_pixmap.fill(QColor());
|
||||
m_pixmap.fill(QColor{});
|
||||
}
|
||||
|
22
osciwidget.h
22
osciwidget.h
@@ -1,20 +1,13 @@
|
||||
#pragma once
|
||||
|
||||
#include <QWidget>
|
||||
|
||||
#include <QOpenGLWidget>
|
||||
#include <QDebug>
|
||||
#include <QPainter>
|
||||
#include <QPixmap>
|
||||
#include <QTimerEvent>
|
||||
#include <QPoint>
|
||||
#include <QPointF>
|
||||
#include <QElapsedTimer>
|
||||
|
||||
#include <optional>
|
||||
|
||||
#include "audiodevice.h"
|
||||
|
||||
class OsciWidget : public QOpenGLWidget
|
||||
class OsciWidget : public QWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
@@ -24,17 +17,20 @@ public:
|
||||
int blend() const;
|
||||
float factor() const;
|
||||
float glow() const;
|
||||
int fps() const;
|
||||
|
||||
public slots:
|
||||
void setBlend(int blend);
|
||||
void setFactor(float factor);
|
||||
void setGlow(float glow);
|
||||
void setFps(int fps);
|
||||
|
||||
void renderSamples(const SamplePair *begin, const SamplePair *end);
|
||||
|
||||
protected:
|
||||
void paintEvent(QPaintEvent *event) override;
|
||||
void resizeEvent(QResizeEvent *event) override;
|
||||
void timerEvent(QTimerEvent *event) override;
|
||||
|
||||
private:
|
||||
void resizePixmap();
|
||||
@@ -46,5 +42,11 @@ private:
|
||||
|
||||
QPixmap m_pixmap;
|
||||
|
||||
std::optional<QPointF> m_lastPoint;
|
||||
QPointF m_lastPoint;
|
||||
|
||||
int m_frameCounter{0}, m_displayFrameCounter{0};
|
||||
QElapsedTimer m_fpsTimer;
|
||||
|
||||
int m_fps{15};
|
||||
int m_redrawTimerId;
|
||||
};
|
||||
|
Reference in New Issue
Block a user