Return to Snippet

Revision: 65115
at October 25, 2013 19:33 by Makistos


Initial Code
/**
 * Reads audio devices from ALSA interface and returns count and array of
 * strings containing the devices.
 *
 * @param[out] count Number of devices found.
 * @param[out] Array of strings containing the device names.
 *
 */
static void getALSADevices(int *count, char **devices)
{
  void **hints;
  const char *ifaces[] = {"card", "pcm", "rawmidi", "timer", "seq", "hwdep", 0};
  int index = 0;
  void **str;
  char *name;
  char *desc;
  char *io;
  char *name_tmp;
  char *desc_tmp;
  int devIdx = 0;
  int len;

  snd_config_update();
 
  while (ifaces[index]) {
   
    printf(" --- Trying interface %s ---\n", ifaces[index]);
    if (snd_device_name_hint(-1, ifaces[index], &hints) < 0) {
      printf("Querying devices failed for %s.\n", ifaces[index]);
      index++;
      continue;
    }
   
    str = hints;
   
    while (*str) {
      name = snd_device_name_get_hint(*str, "NAME");
      desc = snd_device_name_get_hint(*str, "DESC");
      io = snd_device_name_get_hint(*str, "IOID");

      len = strlen(name)+1;
      name_tmp = (char*)malloc(len);
      devices[devIdx] = name_tmp;
      strcpy(name_tmp, name);
      devices[devIdx][len-1] = '\0';

      printf("\n-- %s --\n", name);
      printf("IO: %s\n", io);
     
      desc_tmp = strtok(desc, "\n");
     
      while (desc_tmp != NULL) {
          printf("%s\n", desc_tmp);
          desc_tmp = strtok(NULL, "\n");
      }
     
      free(name);
      free(desc);
      devIdx++;
      str++;
    }
    index++;
    snd_device_name_free_hint(hints);
  }
  *count = devIdx;
  return;
}

Initial URL


Initial Description
This script finds out ALSA devices in Linux programatically, which can be a bit tricky.

Initial Title
Finding out ALSA devices

Initial Tags
linux

Initial Language
C