add visual scope
This commit is contained in:
@@ -3,6 +3,10 @@
|
||||
|
||||
#include <iostream>
|
||||
|
||||
ScopeBuffer::ScopeBuffer(QObject *parent) : QObject(parent) {
|
||||
|
||||
}
|
||||
|
||||
ScopeBuffer::ScopeBuffer(size_t size) : buffer_(size) {
|
||||
|
||||
}
|
||||
@@ -27,21 +31,45 @@ Scope::Scope(QQuickItem *parent) : QQuickPaintedItem(parent) {
|
||||
setAntialiasing(true);
|
||||
}
|
||||
|
||||
void Scope::setBuffer(ScopeBuffer* scopeBuffer) {
|
||||
scopeBuffer_ = scopeBuffer;
|
||||
buffer_ = std::vector<float>(scopeBuffer_->size());
|
||||
}
|
||||
|
||||
void Scope::paint(QPainter *painter) {
|
||||
|
||||
//std::cout << "onPaint" << std::endl;
|
||||
while(scopeBuffer_->spinlock()) {
|
||||
// wait
|
||||
}
|
||||
|
||||
static float phase = 0.0f;
|
||||
phase += 0.1f;
|
||||
float sample = sin(phase);
|
||||
if(scopeBuffer_ != nullptr) scopeBuffer_->read(buffer_);
|
||||
|
||||
QPen pen(Qt::blue, 4);
|
||||
// TODO: scale to max amplitude ?
|
||||
float maxAmp = 1.0f;
|
||||
/*
|
||||
for(float s : buffer_) {
|
||||
maxAmp = std::max(maxAmp, std::abs(s));
|
||||
}
|
||||
*/
|
||||
|
||||
float scaleY = (height() * 0.45f) / maxAmp;
|
||||
float midY = height() / 2.0f;
|
||||
|
||||
painter->fillRect(0, 0, width(), height(), QColor(20, 20, 20));
|
||||
|
||||
QPen pen;
|
||||
pen.setWidthF(3.0f);
|
||||
QColor green(50, 255, 70);
|
||||
pen.setColor(green);
|
||||
painter->setPen(pen);
|
||||
painter->setRenderHint(QPainter::Antialiasing);
|
||||
|
||||
painter->drawRect(10, 10, width() - 20*sample, height() - 20*sample);
|
||||
|
||||
painter->setPen(QPen(Qt::red, 2));
|
||||
painter->drawLine(0, 0, width(), height());
|
||||
|
||||
for(size_t i = 1; i < buffer_.size(); i++) {
|
||||
painter->drawLine(
|
||||
i * width() / buffer_.size(),
|
||||
midY + buffer_[i-1] * scaleY,
|
||||
i * width() / buffer_.size(),
|
||||
midY + buffer_[i] * scaleY
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user