From 2b9b1cf9a47f5d206078d2fd9ad1fc4080f9cf0c Mon Sep 17 00:00:00 2001 From: Gitea Date: Sun, 22 Sep 2019 12:42:09 +0200 Subject: [PATCH] Factor out drawBuffer from updateDrawBuffer --- osciwidget.cpp | 59 ++++++++++++++++++++++++++------------------------ osciwidget.h | 7 ++++-- 2 files changed, 36 insertions(+), 30 deletions(-) diff --git a/osciwidget.cpp b/osciwidget.cpp index 77546df..0e5188e 100644 --- a/osciwidget.cpp +++ b/osciwidget.cpp @@ -113,19 +113,9 @@ void OsciWidget::darkenFrame() //m_pixmap.fill(Qt::black); } -void OsciWidget::updateDrawBuffer() + +void OsciWidget::drawBuffer(SampleBuffer::iterator &bufferPos, const SampleBuffer::iterator &end) { - // If there is no new data do not update - if(m_buffer.empty()) return; - - if (m_pixmap.size() != size()) - { - m_pixmap = QPixmap(size()); - m_pixmap.fill(Qt::black); - } - - darkenFrame(); - QPainter painter(&m_pixmap); painter.translate(m_pixmap.width()/2, m_pixmap.height()/2); painter.scale(m_factor * m_pixmap.width() / 2.0, m_factor * m_pixmap.height() / 2.0); @@ -136,18 +126,9 @@ void OsciWidget::updateDrawBuffer() pen.setColor(QColor(0, 255, 0)); painter.setPen(pen); - - // persistance time is the time it needs to decay to 1/e ~ 36,7% - - auto duration = 1000*(m_bufferTimer.elapsed()-m_lastTime); - auto framesOffset = framesForDuration(duration); - //qDebug() << framesOffset << m_buffer.size()-framesOffset << m_bufferOffset - m_buffer.begin(); - - //m_bufferBegin = m_buffer.begin(); - auto bufferEnd = m_buffer.begin() + framesOffset; - for (;m_bufferOffset < bufferEnd && m_bufferOffset != m_buffer.end(); ++m_bufferOffset) + for (;bufferPos < end; ++bufferPos) { - const auto &frame = *m_bufferOffset; + const auto &frame = *bufferPos; const QPointF p{ float(frame.first) / std::numeric_limits::max(), @@ -156,23 +137,45 @@ void OsciWidget::updateDrawBuffer() const QLineF line(m_lastPoint, p); - // the time of one sample is 1/samplerate - // the brightness auto beamOpacity = std::min(1.0, 1. / ((line.length() * m_lightspeed) + 1)); - double time = 1000.0 * std::distance(m_bufferOffset, bufferEnd) / 44100.0; + double time = 1000.0 * std::distance(bufferPos, end) / 44100.0; auto beamDecay = exp(-time/m_decayTime); - //qDebug() << time << beamDecay; painter.setOpacity(beamDecay*beamOpacity); - painter.drawLine(m_lastPoint, p); m_lastPoint = p; } } +void OsciWidget::resizeDrawBuffer() +{ + if (m_pixmap.size() != size()) + { + m_pixmap = QPixmap(size()); + m_pixmap.fill(Qt::black); + } +} + +void OsciWidget::updateDrawBuffer() +{ + // If there is no new data do not update + if(m_buffer.empty()) return; + + resizeDrawBuffer(); + darkenFrame(); + // persistance time is the time it needs to decay to 1/e ~ 36,7% + + auto duration = 1000*(m_bufferTimer.elapsed()-m_lastTime); + size_t framesOffset = framesForDuration(duration); + //qDebug() << framesOffset << m_buffer.size()-framesOffset << m_bufferOffset - m_buffer.begin(); + + auto bufferEnd = m_buffer.begin() + std::min(framesOffset, m_buffer.size()); + drawBuffer(m_bufferOffset, bufferEnd); +} + void OsciWidget::timerEvent(QTimerEvent *event) { QWidget::timerEvent(event); diff --git a/osciwidget.h b/osciwidget.h index 297ed1a..b12428b 100644 --- a/osciwidget.h +++ b/osciwidget.h @@ -48,8 +48,9 @@ private: float m_decayTime{25.0}; float m_lightspeed{35.f}; - std::vector m_buffer; - std::vector::iterator m_bufferOffset; + typedef std::vector SampleBuffer; + SampleBuffer m_buffer; + SampleBuffer::iterator m_bufferOffset; int m_frameCounter{0}, m_callbacksCounter{0}, m_samplesCounter{0}; QElapsedTimer m_statsTimer; @@ -60,4 +61,6 @@ private: QPointF m_lastPoint; QPixmap m_pixmap; void darkenFrame(); + void drawBuffer(SampleBuffer::iterator &bufferPos, const SampleBuffer::iterator &end); + void resizeDrawBuffer(); };