Page 1 of 1

Speaker volume control

Posted: Sat Dec 23, 2023 7:15 pm
by maxbraun
Hi!

I'm using an ESP32-S3-Korvo-2 (V3.1) and I was wondering how to adjust the speaker volume.

I'm able to run the pipeline_http_mp3 example (https://github.com/espressif/esp-adf/tr ... e_http_mp3) just fine. How would I adjust it to increase the volume?

I tried i2s_alc_volume_set(i2s_stream_writer, ...), but that call gives an error. I'm also not sure what values are valid.

Thanks!
Max

Re: Speaker volume control

Posted: Thu Jan 04, 2024 10:48 am
by volksvorg
audio_hal_set_volume(board_handle->audio_hal, 100);

Re: Speaker volume control

Posted: Sun Mar 31, 2024 2:20 pm
by gyui21g
When making i2s_stream_writer initialization, configure the i2s_cfg.use_alc=true.

Code: Select all

ESP_LOGI(TAG, "[2.2] Create i2s stream to write data to codec chip");
    int player_volume=0;
    i2s_stream_cfg_t i2s_cfg = I2S_STREAM_CFG_DEFAULT();
    i2s_cfg.use_alc = true;
    i2s_cfg.volume = player_volume;
    i2s_cfg.type = AUDIO_STREAM_WRITER;
    i2s_stream_writer = i2s_stream_init(&i2s_cfg);

Re: Speaker volume control

Posted: Tue May 28, 2024 9:00 am
by Linc08
volksvorg wrote:
Thu Jan 04, 2024 10:48 am
audio_hal_set_volume(board_handle->audio_hal, 100);
This works for me! Thanks! :lol:

Re: Speaker volume control

Posted: Sat Aug 31, 2024 2:46 pm
by parabuzzle
If you're attempting to use ALC you have to enable it on the i2s stream writer:

Code: Select all

i2s_stream_cfg_t i2s_cfg = I2S_STREAM_CFG_DEFAULT();
i2s_cfg.use_alc = true;
i2s_stream_writer = i2s_stream_init(&i2s_cfg);
Then when you call it to set the volume. You need to make make sure you are setting the volume properly.

If you review the docs (https://docs.espressif.com/projects/esp ... _handle_ti)
Supported range [-64, 63], unit: dB
So 0 is file volume level.. if you want it quieter you go into the negative where -64 is mute

so to make it half volume you call this:

Code: Select all

i2s_alc_volume_set(i2s_stream_writer, -32);
In my app I usually use 0 to 100 for volume so if you want to do that you can just convert it like this:

Code: Select all

float vol = -64.0 + 0.64 * volume; // volume is an int from 0 to 100
i2s_alc_volume_set(i2s_stream_writer, (int)vol);
This works well on all i2s streams and has no reliance on the HAL or special connections because the ALC adjustment is applied to the i2s stream data and mutates the bits to adjust gain level going to your DAC.

I hope this helps. The ADF is a bit of a tricky mess to figure out.