audio monitor functional with float system

This commit is contained in:
itsmattkc
2019-04-11 10:23:38 +10:00
parent e73011e192
commit 358d1ba16a
3 changed files with 63 additions and 37 deletions
+37 -15
View File
@@ -37,25 +37,33 @@ extern "C" {
}
AudioMonitor::AudioMonitor(QWidget *parent) :
QWidget(parent)
QWidget(parent),
peaked_(false)
{
clear_timer.setInterval(500);
connect(&clear_timer, SIGNAL(timeout()), this, SLOT(clear()));
}
void AudioMonitor::set_value(const QVector<double> &ivalues) {
value_lock.lock();
values = ivalues;
value_lock.unlock();
void AudioMonitor::set_value(const QVector<float> &ivalues) {
if (value_lock.tryLock()) {
values = ivalues;
QMetaObject::invokeMethod(this, "update", Qt::QueuedConnection);
QMetaObject::invokeMethod(&clear_timer, "start", Qt::QueuedConnection);
if (peaked_.size() != values.size()) {
peaked_.resize(values.size());
peaked_.fill(false);
}
value_lock.unlock();
QMetaObject::invokeMethod(this, "update", Qt::QueuedConnection);
QMetaObject::invokeMethod(&clear_timer, "start", Qt::QueuedConnection);
}
}
void AudioMonitor::clear() {
clear_timer.stop();
values.fill(1);
peaked_.fill(false);
values.fill(0.0);
update();
}
@@ -67,6 +75,12 @@ void AudioMonitor::resizeEvent(QResizeEvent *e) {
QWidget::resizeEvent(e);
}
void AudioMonitor::mousePressEvent(QMouseEvent *)
{
peaked_.fill(false);
update();
}
void AudioMonitor::paintEvent(QPaintEvent *) {
value_lock.lock();
if (values.size() > 0) {
@@ -74,18 +88,26 @@ void AudioMonitor::paintEvent(QPaintEvent *) {
int channel_x = AUDIO_MONITOR_GAP;
int channel_count = values.size();
int channel_width = (width()/channel_count) - AUDIO_MONITOR_GAP;
int i;
for (i=0;i<channel_count;i++) {
for (int i=0;i<channel_count;i++) {
// Fill audio monitor with red-yellow-green gradient
QRect r(channel_x, AUDIO_MONITOR_PEAK_HEIGHT + AUDIO_MONITOR_GAP, channel_width, height());
p.fillRect(r, gradient);
bool peak = false;
// Check if we've peaked
if (!peaked_[i] && values.at(i) > 1.0f) {
peaked_[i] = true;
}
r.setHeight(qRound(r.height()*(values.at(i))));
peak = (r.height() == 0);
// We draw an inverted dark shadow over the gradient to represent to are not lit up
// TODO change to decibel representation
float val = 1.0f - qMin(1.0f, values.at(i));
r.setHeight(qRound(r.height()*val));
QRect peak_rect(channel_x, 0, channel_width, AUDIO_MONITOR_PEAK_HEIGHT);
if (peak) {
if (peaked_[i]) {
// We're peaked on this channel, so we light up the peak light
p.fillRect(peak_rect, QColor(255, 0, 0));
} else {
p.fillRect(peak_rect, QColor(64, 0, 0));
+17 -5
View File
@@ -53,22 +53,29 @@ public:
* An array of doubles between 0.0 and 1.0 to display the amplitude. 0.0 is no audio, 1.0 is full volume. Each array
* entry is a channel and the audio monitor will automatically adjust to the channel count in the array.
*/
void set_value(const QVector<double>& values);
void set_value(const QVector<float> &values);
protected:
/**
* @brief Internal paint event
*
* Paints the
* Paints the audio monitor
*/
void paintEvent(QPaintEvent *);
virtual void paintEvent(QPaintEvent *) override;
/**
* @brief Internal resize event handler
*
* Triggers a repaint when the widget is resized.
*/
void resizeEvent(QResizeEvent *);
virtual void resizeEvent(QResizeEvent *) override;
/**
* @brief Internal mouse press handler
*
* @param event
*/
virtual void mousePressEvent(QMouseEvent *event) override;
signals:
@@ -83,7 +90,7 @@ private:
/**
* @brief Internal value storage
*/
QVector<double> values;
QVector<float> values;
/**
* @brief Value mutex
@@ -100,6 +107,11 @@ private:
*/
QTimer clear_timer;
/**
* @brief Internal variable for whether audio played as peaked or not
*/
QVector<bool> peaked_;
private slots:
/**
* @brief Slot to clear the audio monitor