Skip to contentSkip to conversation

Control API, help

DISCUSSION Manuel Jander 2 participants 2 messages Jan 19, 2004

lore
Share & tools
lore
#0 lore raw

Hi,

I'm trying to familiarize myself with the Control API (userland programs). I'm somewhat scared about the huge amount of API functions...

I hammered some code together but i'm not very sure if i'm doing it right. If anyone can comment about this concept code, would be nice.

What i intend to do:

1) Gain access to a control with numeric ID "CTRL_HRTF".
2) Read some info (value limits and such).
3) Write 6 (upto 112) integer values into that control.

The commented code (//) are things that i believe are not needed.

void test() {
int i;
snd_kcontrol_t *ctlp;

  /* Open the control. Is this really needed ? */
  if ((err = snd_ctl_open(&ctlp, "3d_hrtf", SND_CTL_ASYNC)) != 0) {
    fprintf (stderr, "3D CTL failed: (%s)\n", snd_strerror (err));
    exit(1);
  }

/* Retrieve some ifno to check if anything is as expected. */ snd_ctl_elem_info_t *info; snd_ctl_card_info_alloca(info)

  /* Setup info struct. */
  //snd_ctl_elem_info_set_id(info, const snd_ctl_elem_id_t *ptr);
  snd_ctl_elem_info_set_numid(info, CTRL_HRTF);
  snd_ctl_elem_info_set_interface(info, snd_ctl_elem_iface_t val);
  snd_ctl_elem_info_set_device(info, 0);
  snd_ctl_elem_info_set_subdevice(info, 2);
  //snd_ctl_elem_info_set_name(info, const char *val);
	
  /* Get info field 0. */
  snd_ctl_elem_info_set_index(info, 0);
  if ((err = snd_ctl_elem_info(ctlp, info)) != 0) {
    fprintf (stderr, "3D CTL failed: (%s)\n", snd_strerror (err));
    exit(1);
  }
  snd_ctl_elem_info_free(info);
	
  /* Declare and setup ctl_elem_value struct. */
  snd_ctl_elem_value_t *ctl_val;
  snd_ctl_elem_value_alloca(ctl_val);

//snd_ctl_elem_value_set_id(ctl_val, const snd_ctl_elem_id_t *ptr); snd_ctl_elem_value_set_numid(ctl_val, CTRL_HRTF /* Some const defined
elsewhere. */);
snd_ctl_elem_value_set_interface(ctl_val, SNDRV_CTL_ELEM_IFACE_PCM); snd_ctl_elem_value_set_device(ctl_val, 0); snd_ctl_elem_value_set_subdevice(ctl_val, 2); //snd_ctl_elem_value_set_name(ctl_val, const char *val);

/* Write values. */

  for (i=0; i<6; i++) {
    snd_ctl_elem_value_set_index(ctl_val, i);
    snd_ctl_elem_value_alloca(ctl_val);
    snd_ctl_elem_value_set_integer(ctl_val, unsigned int idx, long val);
  }
  /* Dispatch ctl write to soundcard. */
  if ((err = snd_ctl_elem_write(ctlp, &control)) != 0) {
    fprintf (stderr, "3D CTL failed: (%s)\n", snd_strerror (err));
    exit(1);

}
snd_ctl_elem_value_free(ctl_val);
}

Thank you very much.

Signature · 10 lines
Manuel Jander
mjander(at)users(dot)sourceforge(dot)net



-------------------------------------------------------
The SF.Net email is sponsored by EclipseCon 2004
Premiere Conference on Open Tools Development and Integration
See the breadth of Eclipse activity. February 3-5 in Anaheim, CA.
http://www.eclipsecon.org/osdn
#1 lore raw

At Mon, 19 Jan 2004 08:31:01 -0400,

Quoted 15 lines · Manuel Jander

Hi,

I'm trying to familiarize myself with the Control API (userland programs). I'm somewhat scared about the huge amount of API functions...

I hammered some code together but i'm not very sure if i'm doing it right. If anyone can comment about this concept code, would be nice.

What i intend to do:

1) Gain access to a control with numeric ID "CTRL_HRTF".
2) Read some info (value limits and such).
3) Write 6 (upto 112) integer values into that control.

Quoted 5 lines

The commented code (//) are things that i believe are not needed.

void test() {
int i;
snd_kcontrol_t *ctlp;

    ^^^^^^^^^^^^^^
    snd_ctl_t
Quoted 6 lines
	
  /* Open the control. Is this really needed ? */
  if ((err = snd_ctl_open(&ctlp, "3d_hrtf", SND_CTL_ASYNC)) != 0) {
    fprintf (stderr, "3D CTL failed: (%s)\n", snd_strerror (err));
    exit(1);
  }

if you open "3d_hrtf", you have to define this in your asoundrc. (i think it's intentional, right?)

Quoted 4 lines

/* Retrieve some ifno to check if anything is as expected. */ snd_ctl_elem_info_t *info; snd_ctl_card_info_alloca(info)

            ^^^^
            elem
Quoted 4 lines
	
  /* Setup info struct. */
  //snd_ctl_elem_info_set_id(info, const snd_ctl_elem_id_t *ptr);
  snd_ctl_elem_info_set_numid(info, CTRL_HRTF);

you should use the name/index pair rather than the numid. the number id may be changed e.g. if the driver is modified.

Quoted 3 lines
  snd_ctl_elem_info_set_interface(info, snd_ctl_elem_iface_t val);
  snd_ctl_elem_info_set_device(info, 0);
  snd_ctl_elem_info_set_subdevice(info, 2);

subdevice 2 ??

Quoted 9 lines
  //snd_ctl_elem_info_set_name(info, const char *val);
	
  /* Get info field 0. */
  snd_ctl_elem_info_set_index(info, 0);
  if ((err = snd_ctl_elem_info(ctlp, info)) != 0) {
    fprintf (stderr, "3D CTL failed: (%s)\n", snd_strerror (err));
    exit(1);
  }
  snd_ctl_elem_info_free(info);

if you call alloca, you cannot (or don't have to) free it explicitly.

Quoted 12 lines
  /* Declare and setup ctl_elem_value struct. */
  snd_ctl_elem_value_t *ctl_val;
  snd_ctl_elem_value_alloca(ctl_val);

//snd_ctl_elem_value_set_id(ctl_val, const snd_ctl_elem_id_t *ptr); snd_ctl_elem_value_set_numid(ctl_val, CTRL_HRTF /* Some const defined
elsewhere. */);
snd_ctl_elem_value_set_interface(ctl_val, SNDRV_CTL_ELEM_IFACE_PCM); snd_ctl_elem_value_set_device(ctl_val, 0); snd_ctl_elem_value_set_subdevice(ctl_val, 2); //snd_ctl_elem_value_set_name(ctl_val, const char *val);

Quoted 4 lines
  /* Write values. */
  for (i=0; i<6; i++) {
    snd_ctl_elem_value_set_index(ctl_val, i);
    snd_ctl_elem_value_alloca(ctl_val);

allocate twice??

Quoted 1 line
    snd_ctl_elem_value_set_integer(ctl_val, unsigned int idx, long val);

the second argument of set_integer is NOT the index of the element itself. it's the array index of that element. (a control element is an array of integer/boolean/etc.)

if you want to set different elements with different indices, call snd_ctl_elem_write() in this loop.

Quoted 7 lines
  }
  /* Dispatch ctl write to soundcard. */
  if ((err = snd_ctl_elem_write(ctlp, &control)) != 0) {
    fprintf (stderr, "3D CTL failed: (%s)\n", snd_strerror (err));
    exit(1);
  }
  snd_ctl_elem_value_free(ctl_val);

don't free the alloca'ed record.

Quoted 1 line

}

ciao,

Takashi

-------------------------------------------------------
The SF.Net email is sponsored by EclipseCon 2004 Premiere Conference on Open Tools Development and Integration See the breadth of Eclipse activity. February 3-5 in Anaheim, CA.
http://www.eclipsecon.org/osdn