[alsa-devel] What is wrong in this code.

Anders Gnistrup agn at datarespons.dk
Tue Oct 18 15:18:16 CEST 2011


Hello

I have some code, which I just can figure out why it is not working.

I have tried ti varify this using amixer.
agn at agn-HP-EliteBook-2540p:~/laerdal/audio-pulse2alsa/system/vs2-system/bu/var/lib/alsa$ amixer -Dhw:0 cget numid=23
numid=23,iface=MIXER,name='PCM Playback Volume'
  ; type=INTEGER,access=rw---RW-,values=2,min=0,max=255,step=0
  : values=100,200
  | dBscale-min=-51.00dB,step=0.20dB,mute=0

Ofcourse I can use the "cset" command to set the volumes. No problem.

Now, I would like to do this in code. Listed below.
The code works just as expected when using the: 
    snd_ctl_elem_id_set_name(id, "PCM Playback Volume"); 
Should be similar to "amixer -c0 cset name='PCM Playback Volume' 100,200"
But when I use the snd_ctl_elem_id_set_numid(id, numid); (with numid=23) it fails. snd_hctl_find_elem simply does not find the element!

The version I use is 1.0.24. I have looked into the amixer.c source code, and it works.
So I gees i am just missing a tiny option to make it work, but I hust can't figure out which one!

#include <errno.h>
#include <assert.h>
#include <alsa/asoundlib.h>
#include <sys/poll.h>


int main()
{
	snd_hctl_t *hctl;
	snd_ctl_elem_id_t *id;
	int err;
	unsigned int numid = 23;

	err = snd_hctl_open(&hctl, "hw:0", 0);
	err = snd_hctl_load(hctl);

	snd_ctl_elem_id_alloca(&id);
	snd_ctl_elem_id_set_interface(id, SND_CTL_ELEM_IFACE_MIXER);

	//snd_ctl_elem_id_set_numid(id, numid);
	snd_ctl_elem_id_set_name(id, "PCM Playback Volume");

	snd_hctl_elem_t *elem = snd_hctl_find_elem(hctl, id);
	if (elem != NULL)
	{
		snd_ctl_elem_value_t *control;
		snd_ctl_elem_value_alloca(&control);
		snd_ctl_elem_value_set_id(control, id);

		snd_ctl_elem_value_set_integer(control, 0, 100);
		err = snd_hctl_elem_write(elem, control);

		snd_ctl_elem_value_set_integer(control, 1, 200);
		err = snd_hctl_elem_write(elem, control);
	}
	else
		printf("Cound not find elem.\n");
	snd_hctl_close(hctl);



}


More information about the Alsa-devel mailing list