Skip to content

Determining the Volume

Phil Schatzmann edited this page Oct 5, 2024 · 9 revisions

You can determine the max amplitude (=volume) with the help of the VolumeMeter class. Just send the audio to this class and then you can access the volume information:

#include "AudioTools.h"
#include "AudioTools/AudioLibs/AudioKit.h"

AudioInfo info(44100, 2, 16);
AudioKitStream kit; // or use I2SStream
VolumeMeter out;
StreamCopy copier(out, kit); // copy kit to kit

// Arduino Setup
void setup(void) {
    Serial.begin(440100);
    AudioLogger::instance().begin(Serial, AudioLogger::Warning);

    // setup input
    auto cfg = kit.defaultConfig(RX_MODE);
    cfg.input_device = AUDIO_HAL_ADC_INPUT_LINE2; // input from microphone
    cfg.copyFrom(info)
    kit.begin(cfg);

    // setup output
    auto cfg_out = out.defaultConfig();
    cfg_out.copyFrom(info);
    out.begin(cfg_out);

}

// Arduino loop - copy data
void loop() {
    copier.copy();

    // Just print the max amplitude
    Serial.print("Volume: ");
    Serial.print(out.volume());
    Serial.print(" left: ");
    Serial.print(out.volume(0));
    Serial.print(" right: ");
    Serial.println(out.volume(1));
}

Instead of the volume as amplitude you can also get the ratio (range 0-1.0), percent (range 0-100) or the value in db (range negative to 0)

You can also use this class anywhere in your input or output chain.