mardi 27 juin 2023

QT6 QAudioProbe no more

I was trying to replicate the Media player example provided. While it is possible to create an audio meter using QAudioProbe in Qt5, I am unable to figure out how to achieve the same in Qt6.

I have created a custom QWidget called VMeterWidget, which represents the vertical meter widget. It has a setLevel() function to set the meter level, an updateAudioLevel() function to update the level based on the peak value, and a paintEvent() function to paint the widget.

The VMeterWidget also includes a QTimer for periodic level resets and an updateLevel() slot for smooth level transitions. The paintEvent() function uses QPainter to draw the meter with a gradient color scheme.

I'm facing challenges in finding an alternative to QAudioProbe in Qt6 to capture and analyze audio data for the meter. Any assistance or guidance on how to achieve this functionality in Qt6 would be greatly appreciated.

I share the code for the audio meter.

#include <QWidget>
#include <QPainter>

class VMeterWidget : public QWidget
{
    Q_OBJECT
public:
    explicit VMeterWidget(QWidget *parent = nullptr);

    // Imposta il livello del meter (compreso tra 0 e 1)
    void setLevel(qreal level);
    void updateAudioLevel(qreal peak);

protected:
    void paintEvent(QPaintEvent *event);

private:
    qreal m_level;
    qreal targetLevel;
    const qreal maxValue = 1.2;
    QTimer *resetTimer;
    bool isUpdatingLevel = false;


private slots:
    void resetLevel();
    void updateLevel();
};

and this is the cpp file

#include "vuMeter.h"
#include "qtimer.h"
#include <QDebug>

VMeterWidget::VMeterWidget(QWidget *parent)
    : QWidget(parent), m_level(0)
{
    setMinimumSize(20, 100);
    // Crea un timer per il reset periodico
    resetTimer = new QTimer(this);
    connect(resetTimer, SIGNAL(timeout()), this, SLOT(resetLevel()));

    // Imposta l'intervallo del timer (ad esempio, 1 secondo)
    int resetInterval = 500; // Tempo in millisecondi
    resetTimer->setInterval(resetInterval);
    resetTimer->start();
}

void VMeterWidget::setLevel(qreal level)
{
    if (m_level != level) {
        isUpdatingLevel = true;
        targetLevel = level;
        updateLevel();
    }
}

void VMeterWidget::updateLevel()
{
    qreal step = 0.08; // Passo di incremento per ogni iterazione del ciclo
    qreal diff = targetLevel - m_level;
    if (qAbs(diff) > step) {
        if (diff > 0) {
            m_level += step;
        } else {
            m_level -= step;
        }
        update();
        QTimer::singleShot(10, this, SLOT(updateLevel()));
    } else {
        m_level = targetLevel;
        isUpdatingLevel = false;
        update();
    }

}

void VMeterWidget::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);

    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);

    // Sfondo del widget
    painter.fillRect(rect(), Qt::black);

    // Area del meter
    QRectF meterRect = QRectF(0, height() * (1 - m_level), width(), height() * m_level);

    // Gradiente per il meter
    QLinearGradient gradient(meterRect.topLeft(), meterRect.bottomLeft());

    // Imposta il colore per i primi 5 punti percentuali
    gradient.setColorAt(0, Qt::red);
    gradient.setColorAt(0.001, Qt::darkRed);
    gradient.setColorAt(0.02, Qt::darkYellow);
    gradient.setColorAt(0.03, Qt::yellow);
    gradient.setColorAt(0.04, Qt::darkGreen);

    // Imposta il colore per l'ultimo punto percentuale
    gradient.setColorAt(1, Qt::green);

    // Disegna il meter
    painter.fillRect(meterRect, gradient);
}


void VMeterWidget::updateAudioLevel(qreal peak)
{
    // Normalizza il valore del picco tra 0 e 1
    qreal level = peak / maxValue;

    // Imposta il livello del meter
    setLevel(level);

    // Resetta il timer per evitare il reset del livello
    // se viene ricevuto un nuovo picco audio entro l'intervallo
    resetTimer->start();
}

void VMeterWidget::resetLevel()
{
    setLevel(0);
}

Aucun commentaire:

Enregistrer un commentaire