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. Measure input audio volume

Measure input audio volume

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 3 Posters 999 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
    addr3ss
    wrote on last edited by
    #1

    How can I measure input audio volume?
    I want to detect when input audio volume is lower than some level and do something.
    There is no problem if volume unit is decibel or other things.

    1 Reply Last reply
    0
    • Kent-DorfmanK Offline
      Kent-DorfmanK Offline
      Kent-Dorfman
      wrote on last edited by
      #2

      "lower" is harder than "higher" because with higher you just need to read the pcm amplitude values and compare to a high limit. Lower would involve an algorithm that looks at mean average amplitude and decides.

      If you meet the AI on the road, kill it.

      A 1 Reply Last reply
      2
      • Kent-DorfmanK Kent-Dorfman

        "lower" is harder than "higher" because with higher you just need to read the pcm amplitude values and compare to a high limit. Lower would involve an algorithm that looks at mean average amplitude and decides.

        A Offline
        A Offline
        addr3ss
        wrote on last edited by
        #3

        @Kent-Dorfman Ok.How can I get input volume and compare it to the high level?

        1 Reply Last reply
        0
        • Kent-DorfmanK Offline
          Kent-DorfmanK Offline
          Kent-Dorfman
          wrote on last edited by Kent-Dorfman
          #4

          https://www.springer.com/us/book/9783030117801?gclid=EAIaIQobChMI2ZfKkcma6AIVlpOzCh1mTQGgEAQYASABEgK3ofD_BwE

          https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=10&ved=2ahUKEwi-ksORyZroAhVtj3IEHWg9BfEQFjAJegQICRAB&url=http%3A%2F%2Fcfile21.uf.tistory.com%2Fattach%2F181FA6174A8C7AD652D80D&usg=AOvVaw1BML3PHYHummXEtx7IvMv8

          Read it
          Learn it
          Live it

          If you meet the AI on the road, kill it.

          1 Reply Last reply
          1
          • A Offline
            A Offline
            addr3ss
            wrote on last edited by
            #5

            I dont want to be an audio processing expert.I just want some qt c++ code.

            1 Reply Last reply
            0
            • mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Hi
              Maybe this example can be used
              https://doc.qt.io/qt-5/qtmultimedia-multimedia-spectrum-example.html
              as a base for reading the data and then apply some algorithm to get what you want.

              1 Reply Last reply
              2
              • A Offline
                A Offline
                addr3ss
                wrote on last edited by
                #7

                I found this. A simple example:
                https://github.com/soramimi/QtAudioInput

                1 Reply Last reply
                1
                • A Offline
                  A Offline
                  addr3ss
                  wrote on last edited by
                  #8

                  I wrote this code but it does not detect continuously. It just detect the level at the beginning and after, the level begins to reduce.How can I solve this?

                  #include "Bot.h"
                  
                  void Bot::start() {
                    format.setByteOrder(QAudioFormat::LittleEndian);
                    format.setChannelCount(2);
                    format.setCodec("audio/pcm");
                    format.setSampleRate(8000);
                    format.setSampleSize(16);
                    format.setSampleType(QAudioFormat::SignedInt);
                    source = std::shared_ptr<QAudioInput>(new QAudioInput(format));
                    device = source->start();
                    connect(device, SIGNAL(readyRead()), this, SLOT(onReadyRead()));
                  }
                  
                  void Bot::stop() {
                    source = nullptr;
                    device = nullptr;
                    peak = 0;
                    emit volumeChanged(peak);
                  }
                  
                  void Bot::onReadyRead() {
                    peak = 0;
                    n = source->bytesReady();
                    n /= 2;
                    if (n >= 80) {
                      for (int i = 0; i < n; i++) {
                        v = 0;
                        device->read((char *)&v, 2);
                        if (v < 0) {
                          v = -v;
                        }
                        if (peak < v) {
                          peak = v;
                        }
                      }
                      peak = peak * 100 / 32768;
                      if (peak < 0) {
                        peak = 0;
                      }
                      emit volumeChanged(peak);
                    }
                  }
                  
                  1 Reply Last reply
                  0
                  • Kent-DorfmanK Offline
                    Kent-DorfmanK Offline
                    Kent-Dorfman
                    wrote on last edited by
                    #9

                    @addr3ss said in Measure input audio volume:

                    > void Bot::onReadyRead() {
                    >   peak = 0;
                    >   n = source->bytesReady();
                    >   n /= 2;
                    >   if (n >= 80) {
                    >     for (int i = 0; i < n; i++) {
                    >       v = 0;
                    >       device->read((char *)&v, 2);
                    >       if (v < 0) {
                    >         v = -v;
                    >       }
                    >       if (peak < v) {
                    >         peak = v;
                    >       }
                    >     }
                    

                    Two things:

                    1. for the type of programming you are doing, truncation, overflow, and signedness will bite you in the hind quarters. Please explicitly type your vars.

                    2. please comment your algorithms. I can assume that n/2 is accounting for stereo samples, but I should not have to make assumptions about your algorithm. Explaining it to others often causes YOU to see the error in your process.

                    I still say you need to do some real research into digital sound processing, and that the "I don't want to be an expert" is why there is so much crap software out there.

                    Good luck!

                    If you meet the AI on the road, kill it.

                    1 Reply Last reply
                    1
                    • A Offline
                      A Offline
                      addr3ss
                      wrote on last edited by addr3ss
                      #10

                      I typed my vars in header file:

                        Q_INVOKABLE void start();
                        Q_INVOKABLE void stop();
                        std::shared_ptr<QAudioInput> source;
                        QIODevice *device;
                        QAudioFormat format;
                        int peak = 0, n;
                        int16_t v;
                      

                      And I copied this code from https://github.com/soramimi/QtAudioInput with some edit.
                      So actually I dont know how it works. But I hope someone can understand the code and help me.

                      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