Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QT6 QAudioProbe no more...
Forum Updated to NodeBB v4.3 + New Features

QT6 QAudioProbe no more...

Scheduled Pinned Locked Moved Unsolved General and Desktop
qtmultimediavideoplayeraudio meter
3 Posts 3 Posters 835 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    a.michelassi
    wrote on 27 Jun 2023, 07:42 last edited by SGaist
    #1

    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.

    #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();
    };
    
    #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);
    }
    
    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 27 Jun 2023, 20:35 last edited by SGaist
      #2

      Hi and welcome to devnet,

      The probe APIs have been removed. See here.

      You might be able to do something with the QAudioDecoder class.

      On a side note, please use coding tags around your code. That will make it readable.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • M Offline
        M Offline
        Morbius
        wrote on 8 Dec 2024, 17:31 last edited by
        #3

        They yanked perfectly good functionality out of Qt5, as far as I can tell. Concerning. M$ has a virtual monopoly on OS's, but much like two of the most enduring programming languages out there, win32, C++ and FORTRAN virtually never obsolete a given statement or call. These are also some of the most popular languages/APIs out there. I was looking to pick up the "peak meter" from an audio output, but nevermind. Not the first shortcoming of Qt I've had to code around. I figured out a way to do it with GetPeakValue() and adding the OLE lib to the .pro file. The advantage of doing it this way is the hw usu. does the calculation for you; and you don't have to scan your whole block of audio data. I also prefer linking to a windows distribution of CURL instead of QNetWorkManager; as I found it more reliable. That was in Qt5, however.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved