Get sound level from audio input



  • Hey there.

    What would be the most simpliest way of getting the sound level (or rather, I suppose it'd be a function that'd process audio data) form the default input device?

    I've found examples, but they're so overcomplicated.. I just need to detect sound "loudness".


  • Lifetime Qt Champion

    Hi,

    The most straightforward way would be to do the math yourself on the data from coming from QAudioInput


  • Moderators

    Hi,

    What do you want to do with the information about sound loudness? Do you need it to be very accurate? Or do you just need it to be able to tell the difference between a "loud input" and "soft input"?

    There are many ways to measure "loudness" -- e.g. sound intensity level, sound power level, sound pressure level, etc. Is there a particular one that you want? (Note that a "sound level meter" measures sound pressure level)



  • I'm not really sure - I'm quite inexperienced with audio, but I need a bar that displays sound "loudness" like that when you click the sound icon in the right-bottom corner on windows, to adjust your sound level. It has a gray bar jumping up & down that displays what appears to be loudness.

    I suppose that I just want sample value. Aka something like this:

    @// "loudness" in percent
    float loudness = abs(sample) / std::numeric_limits<decltype(sample)>::max() * 100.0f;@

    That is how I at the moment calculate "loudness" in my other program, not Qt related. I suppose this could be wrong though.. I know like, nothing about audio. I've no idea how would I get intensity level or pressure or any of those things. I just need a reliable way to detect sound loudness - if you can tell me what would be a good way, then I'd be grateful (and I'd use that in my other non Qt related program aswell, instead of the current way, since I guess the current way is wrong)

    @EDIT

    Oh, I've found this:
    http://cnx.org/contents/031da8d3-b525-429c-80cf-6c8ed997733a@8.8:131

    Could prove quite useful. I suppose I just need to process captured audio samples then.



  • Okay, nevermind what I wrote before - what I need now is to somebody to tell me how would I process audio samples as it records them.

    At the moment, I've setup QAudioInput this way:

    @QAudioFormat format;
    // set up the format you want, eg.
    format.setSampleRate(44100);
    format.setChannelCount(1);
    format.setSampleSize(16);
    format.setCodec("audio/pcm");
    format.setByteOrder(QAudioFormat::LittleEndian);
    format.setSampleType(QAudioFormat::SignedInt);
    QAudioDeviceInfo info = QAudioDeviceInfo::defaultInputDevice();
    if (!info.isFormatSupported(format)) {
    format = info.nearestFormat(format);
    }
    m_audioInput = new QAudioInput(format,NULL);
    QTimer::singleShot(3000, this, SLOT(resetRecording()));
    m_ioDevice = m_audioInput->start();@

    and in resetRecording(), I've this:

    @m_audioInput->reset();@

    But I'm not really sure how to process that data as it records. Should I start a new thread that'd constantly check if m_ioDevice has any bytes to read, and if yes, process them?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.