Measure input audio volume
-
"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.
-
@Kent-Dorfman Ok.How can I get input volume and compare it to the high level?
-
Read it
Learn it
Live it -
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. -
I found this. A simple example:
https://github.com/soramimi/QtAudioInput -
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); } }
-
@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:
-
for the type of programming you are doing, truncation, overflow, and signedness will bite you in the hind quarters. Please explicitly type your vars.
-
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!
-
-
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.