Skip to contentSkip to conversation

What is wrong in this code.

DISCUSSION Anders Gnistrup 2 participants 4 messages Oct 18, 2011 – Oct 19, 2011

lore
Share & tools
lore
#0 lore raw

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@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);

}

#1 lore raw
Quoted 8 lines · Anders Gnistrup

The code works just as expected when using the:

    snd_ctl_elem_id_set_name(id, "PCM Playback Volume");
But when I use the snd_ctl_elem_id_set_numid(id, numid); (with numid=23) it fails.
      ...
	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");

When using the numid to access elements, it must be the only field that is set in the elem id.

Regards,
Clemens

#2 lore raw

________________________________________
From: Clemens Ladisch [[email protected]]
Sent: Tuesday, October 18, 2011 5:11 PM
To: Anders Gnistrup
Cc: [email protected]
Subject: Re: [alsa-devel] What is wrong in this code.

Quoted 8 lines · Anders Gnistrup

The code works just as expected when using the:

    snd_ctl_elem_id_set_name(id, "PCM Playback Volume");
But when I use the snd_ctl_elem_id_set_numid(id, numid); (with numid=23) it fails.
      ...
      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");

When using the numid to access elements, it must be the only field that is set in the elem id.

Hello Clemens

I have tried just that, but is still does not work. It was my first try, but in frustration I shifted it to snd_ctl_elem_id_set_name. If both is set (numid,name) it still works, but I think the numid is silently ignored.

There might be a known bug/'not implemented feature' in the libalsa version I got. From the amixer.c source code in alsa-utils I have found this:

	snd_ctl_elem_info_get_id(info, id);	/* FIXME: Remove it when hctl find works ok !!! */
	type = snd_ctl_elem_info_get_type(info);
	count = snd_ctl_elem_info_get_count(info);
	snd_ctl_elem_value_set_id(control, id);

In the function "cset". I assume that FIXME note function is "snd_hctl_find_elem". The cset function also shows a valid path to get around the problem.
I have downloaded alsa-utils version 1.0.24.2.22.gaf0bf and the note is still present.

Have the problem with the /* FIXME: Remove it when hctl find works ok !!! */ been solved in latest git release of alsalib?

Regards,
Clemens

#3 lore raw

(quoting fixed)

Quoted 10 lines · Anders Gnistrup
Quoted 7 lines · Clemens Ladisch
Quoted 3 lines · Anders Gnistrup

The code works just as expected when using the:

    snd_ctl_elem_id_set_name(id, "PCM Playback Volume");

But when I use the snd_ctl_elem_id_set_numid(id, numid); (with numid=23) it fails.

When using the numid to access elements, it must be the only field that is set in the elem id.

I have tried just that, but is still does not work.

I looked at the source; with the hctl functions, searching by numid is not supported at all.

Regards,
Clemens