#include <alsa/asoundlib.h> static char card[64] = "default"; void SetMicCaptureVolume(int uVolume) { int err; int orig_volume0 = 0; int orig_volume1 = 0; static snd_ctl_t *handle = NULL; snd_ctl_elem_info_t *info; snd_ctl_elem_id_t *id; snd_ctl_elem_value_t *control; unsigned int count; snd_ctl_elem_type_t type; snd_ctl_elem_info_alloca(&info); snd_ctl_elem_id_alloca(&id); snd_ctl_elem_value_alloca(&control); snd_ctl_elem_id_set_interface(id, SND_CTL_ELEM_IFACE_MIXER);/* default */ snd_ctl_elem_id_set_name(id, "Capture Volume"); //snd_ctl_elem_id_set_index(id, 0); // "Mic Capture Volume" does not have index. if ((err = snd_ctl_open(&handle, card, 0)) < 0) { printf("Control %s open error: %s\n", card, snd_strerror(err)); return; } snd_ctl_elem_info_set_id(info, id); if ((err = snd_ctl_elem_info(handle, info)) < 0) { printf("Cannot find the given element from control-1 %s\n", snd_strerror(err)); snd_ctl_close(handle); handle = NULL; return; } type = snd_ctl_elem_info_get_type(info); count = snd_ctl_elem_info_get_count(info); printf("type = %d, count= %d\n", type, count); /* if(type != SND_CTL_ELEM_TYPE_INTEGER || 1 != count) { printf("Cannot find the given element from control-2 %s\n", snd_strerror(err)); snd_ctl_close(handle); handle = NULL; return; }*/ snd_ctl_elem_value_set_id(control, id); if (!snd_ctl_elem_read(handle, control)) { orig_volume0 = snd_ctl_elem_value_get_integer(control, 0); orig_volume1 = snd_ctl_elem_value_get_integer(control, 1); } printf("min value = %ld, max value = %ld\n", snd_ctl_elem_info_get_min(info), snd_ctl_elem_info_get_max(info)); if(uVolume != orig_volume0) { printf("uVolume != orig_volume ##################### new_value(%d) orgin_value(%d, %d)\n",uVolume,orig_volume0, orig_volume1); snd_ctl_elem_value_set_integer(control, 0, (long)(uVolume)); snd_ctl_elem_value_set_integer(control, 1, (long)(uVolume)); if ((err = snd_ctl_elem_write(handle, control)) < 0) { printf("Control %s element write error: %s\n", card, snd_strerror(err)); snd_ctl_close(handle); handle = NULL; return; } } snd_ctl_close(handle); handle = NULL; return; } (carlsonlee_freec) |