Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ALSA - Change mic gain in c code using alsa api (linux)

Tags:

linux

alsa

I'm trying to mute the mic from inside my application using the alsa api on a linux box. I'm using the following code for changing the volume with volume=0:

long min, max;
snd_mixer_t *handle;
snd_mixer_selem_id_t *sid;
const char *card = "default";
const char *selem_name = "Capture";
snd_mixer_open(&handle, 0);
snd_mixer_attach(handle, card);
snd_mixer_selem_register(handle, NULL, NULL);
snd_mixer_load(handle);
snd_mixer_selem_id_alloca(&sid);
snd_mixer_selem_id_set_index(sid, 0);
snd_mixer_selem_id_set_name(sid, selem_name);
snd_mixer_elem_t* elem = snd_mixer_find_selem(handle, sid);
snd_mixer_selem_get_capture_volume_range(elem, &min, &max);
snd_mixer_selem_set_capture_volume_all(elem, volume * max / 100);
snd_mixer_close(handle);

This works and capture volume is reduced to zero in alsamixer. But I also want to change the mic gain to 0dB using the api and I don't seem to figure out how to achieve this...

Any ideas?

Thanks

Edit: Thanks CL. it worked. Can't paste my code for 7 more hours though.

like image 462
Dimi Avatar asked Mar 23 '26 03:03

Dimi


1 Answers

This code:

const char *selem_name = "Capture";
snd_mixer_selem_id_set_index(sid, 0);
snd_mixer_selem_id_set_name(sid, selem_name);

selects the first control named "Capture".

Change the name to whatever the control for the mic gain happens to be on your machine.

If that control's type is not number but enumerated, you have to use snd_mixer_selem_set_enum_item() to change its value.

like image 136
CL. Avatar answered Mar 25 '26 17:03

CL.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!