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. Get sound wave from QAudioInput
Forum Updated to NodeBB v4.3 + New Features

Get sound wave from QAudioInput

Scheduled Pinned Locked Moved Solved General and Desktop
qaudioinputsoundqbytearray
6 Posts 2 Posters 976 Views 1 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.
  • R Offline
    R Offline
    Robotechnic
    wrote on 7 Jun 2021, 09:15 last edited by Robotechnic 6 Jul 2021, 09:21
    #1

    Hello,
    I want to get a sound wave from QAudioInput like here:
    Description
    This is a python code from here.
    This code works bu I want to do that with c++.

    Here is my code to do that (only for test purpose, I will clean and optimise it later):
    code to get audio output

        auto io = this->audio->start();
    
        connect(io,&QIODevice::readyRead,[this,io]{
    
            QByteArray buff = io->readAll();
            int buffLenght = buff.length();
            if (buffLenght>0){
                this->processAudioFrame(buff.constData(),buffLenght);
            }
        });
    

    code to convert audio QByteArray to list of numbers:

    void Window::processAudioFrame(QByteArray data, int lenght){
        if(lenght > 0)
        {
             short* result = (short *)data.data();
                    for (int  i=0; i < lenght/2; i++ )
                    {
                        ui->soundView->pushSoundLevel(result[i]/32768.0);
                    }
        }
    }
    

    and here in my custom widget the pushSoundLevel and repaintEvent methods:

    void SoundWidget::pushSoundLevel(double level){
        buffer.push_back(level);
        buffer.pop_front();
        this->update();
    }
    
    
    void SoundWidget::paintEvent(QPaintEvent *event){
        QPainter p(this);
        QPolygonF soundWave;
    
        for (int x = 0; x<this->buffer.size(); x++){
            soundWave<<QPointF(x,(buffer.at(x)*0.2*this->height())+this->height()/2);
        }
    
        QRectF boundingRect = soundWave.boundingRect();
        QTransform t;
        t.scale(this->width()/boundingRect.width(),1);
    
        p.setTransform(t);
        p.drawPolyline(soundWave);
    }
    

    My QAudioInput's parameters are that:

        format.setSampleRate(11025);
        format.setChannelCount(1);
        format.setSampleSize(16);
        format.setSampleType(QAudioFormat::SignedInt);
        format.setByteOrder(QAudioFormat::LittleEndian);
        format.setCodec("audio/pcm");
    

    And I have this result:
    Description

    I think that my problem come from the QByteArray conversion but I don't know why.

    I am on Arch linux with Qt 5.15.2.

    Thanks for your help.

    J 1 Reply Last reply 7 Jun 2021, 09:27
    0
    • R Robotechnic
      7 Jun 2021, 09:15

      Hello,
      I want to get a sound wave from QAudioInput like here:
      Description
      This is a python code from here.
      This code works bu I want to do that with c++.

      Here is my code to do that (only for test purpose, I will clean and optimise it later):
      code to get audio output

          auto io = this->audio->start();
      
          connect(io,&QIODevice::readyRead,[this,io]{
      
              QByteArray buff = io->readAll();
              int buffLenght = buff.length();
              if (buffLenght>0){
                  this->processAudioFrame(buff.constData(),buffLenght);
              }
          });
      

      code to convert audio QByteArray to list of numbers:

      void Window::processAudioFrame(QByteArray data, int lenght){
          if(lenght > 0)
          {
               short* result = (short *)data.data();
                      for (int  i=0; i < lenght/2; i++ )
                      {
                          ui->soundView->pushSoundLevel(result[i]/32768.0);
                      }
          }
      }
      

      and here in my custom widget the pushSoundLevel and repaintEvent methods:

      void SoundWidget::pushSoundLevel(double level){
          buffer.push_back(level);
          buffer.pop_front();
          this->update();
      }
      
      
      void SoundWidget::paintEvent(QPaintEvent *event){
          QPainter p(this);
          QPolygonF soundWave;
      
          for (int x = 0; x<this->buffer.size(); x++){
              soundWave<<QPointF(x,(buffer.at(x)*0.2*this->height())+this->height()/2);
          }
      
          QRectF boundingRect = soundWave.boundingRect();
          QTransform t;
          t.scale(this->width()/boundingRect.width(),1);
      
          p.setTransform(t);
          p.drawPolyline(soundWave);
      }
      

      My QAudioInput's parameters are that:

          format.setSampleRate(11025);
          format.setChannelCount(1);
          format.setSampleSize(16);
          format.setSampleType(QAudioFormat::SignedInt);
          format.setByteOrder(QAudioFormat::LittleEndian);
          format.setCodec("audio/pcm");
      

      And I have this result:
      Description

      I think that my problem come from the QByteArray conversion but I don't know why.

      I am on Arch linux with Qt 5.15.2.

      Thanks for your help.

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 7 Jun 2021, 09:27 last edited by
      #2

      @Robotechnic said in Get sound wave from QAudioInput:

      for (int i=0; i < lenght/2; i++ )

      I would say it should be

      for (int  i=0; i < lenght; i+=2 )
      

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • R Offline
        R Offline
        Robotechnic
        wrote on 7 Jun 2021, 13:43 last edited by
        #3

        Ok thanks @jsulm but, unfortunatly, this solution doesn't work as espected

        J 1 Reply Last reply 7 Jun 2021, 13:46
        0
        • R Robotechnic
          7 Jun 2021, 13:43

          Ok thanks @jsulm but, unfortunatly, this solution doesn't work as espected

          J Offline
          J Offline
          jsulm
          Lifetime Qt Champion
          wrote on 7 Jun 2021, 13:46 last edited by
          #4

          @Robotechnic Just realised: result is already short*, so i+=2 is indeed wrong.

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • R Offline
            R Offline
            Robotechnic
            wrote on 7 Jun 2021, 14:37 last edited by Robotechnic 6 Jul 2021, 14:38
            #5

            I think my problem come from the range of the for loop because, when I play static note on a flute I got this graph:
            Description
            And as you can see around garbage of random values, I have good wave
            If someone know how to fix that thanks in advance
            I have also removed

                QRectF boundingRect = soundWave.boundingRect();
                QTransform t;
                t.scale(this->width()/boundingRect.width(),1);
                p.setTransform(t);
            

            because this create some problems

            1 Reply Last reply
            0
            • R Offline
              R Offline
              Robotechnic
              wrote on 8 Jun 2021, 09:23 last edited by
              #6

              I fixed my problem:

              with a qDebug of lenght I have:

              data.length() lenght
              136 2730
              740 1364
              1326 1366
              414 1364
              208 1366
              458 1364

              As you can see they are not the same so, with this code, my problem is fixed:

              void Window::processAudioFrame(QByteArray data){
                  const short* result = (short *)data.constData();
              
              
                  for (int  i=0; i < data.length()/2; i ++ ){
                      ui->soundView->pushSoundLevel(result[i]);
                  }
              
              }
              
              1 Reply Last reply
              0

              4/6

              7 Jun 2021, 13:46

              • Login

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