Merge remote-tracking branch 'origin/master'

This commit is contained in:
2019-08-30 19:14:15 +02:00
5 changed files with 58 additions and 47 deletions

View File

@@ -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)
{
}

View File

@@ -5,10 +5,12 @@
#define qvoid void
struct SamplePair {
qint16 x, y;
template<typename T> struct SamplePairT {
T x, y;
};
typedef SamplePairT<qint16> SamplePair;
class Device : public QIODevice
{
Q_OBJECT

View File

@@ -8,9 +8,10 @@
#include <QStringBuilder>
#include <QRadioButton>
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);
@@ -19,7 +20,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);
@@ -79,6 +88,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<QAudioInput>(m_audioDevices.at(m_ui.comboBoxDevices->currentIndex()), format);
m_input->start(&m_device);
m_input->setBufferSize(format.sampleRate()/60*sizeof(qint16)*2);

View File

@@ -4,25 +4,11 @@
#include <QLine>
namespace {
template<typename T>
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<qint16>::max() / 2 * width() / 2 * m_factor) + (width() / 2);
const qint32 y = (float(-i->y) / std::numeric_limits<qint16>::max() / 2 * height() / 2 * m_factor) + (height() / 2);
const QPoint p{x,y};
const qreal x = (qreal(i->x) / std::numeric_limits<qint16>::max() / 2 * width() / 2 * m_factor);
const qreal y = (qreal(i->y) / std::numeric_limits<qint16>::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));
}

View File

@@ -1,6 +1,8 @@
#pragma once
#include <QWidget>
#include <QOpenGLWidget>
#include <QDebug>
#include <QPainter>
#include <QPixmap>
@@ -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<QPoint> m_lastPoint;
std::optional<QPointF> m_lastPoint;
};