Posted By

Makistos on 10/25/13


Tagged

linux audio alsa linuxdevices


Versions (?)

Finding out ALSA devices


 / Published in: C
 

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

  1. /**
  2.  * Reads audio devices from ALSA interface and returns count and array of
  3.  * strings containing the devices.
  4.  *
  5.  * @param[out] count Number of devices found.
  6.  * @param[out] Array of strings containing the device names.
  7.  *
  8.  */
  9. static void getALSADevices(int *count, char **devices)
  10. {
  11. void **hints;
  12. const char *ifaces[] = {"card", "pcm", "rawmidi", "timer", "seq", "hwdep", 0};
  13. int index = 0;
  14. void **str;
  15. char *name;
  16. char *desc;
  17. char *io;
  18. char *name_tmp;
  19. char *desc_tmp;
  20. int devIdx = 0;
  21. int len;
  22.  
  23. snd_config_update();
  24.  
  25. while (ifaces[index]) {
  26.  
  27. printf(" --- Trying interface %s ---\n", ifaces[index]);
  28. if (snd_device_name_hint(-1, ifaces[index], &hints) < 0) {
  29. printf("Querying devices failed for %s.\n", ifaces[index]);
  30. index++;
  31. continue;
  32. }
  33.  
  34. str = hints;
  35.  
  36. while (*str) {
  37. name = snd_device_name_get_hint(*str, "NAME");
  38. desc = snd_device_name_get_hint(*str, "DESC");
  39. io = snd_device_name_get_hint(*str, "IOID");
  40.  
  41. len = strlen(name)+1;
  42. name_tmp = (char*)malloc(len);
  43. devices[devIdx] = name_tmp;
  44. strcpy(name_tmp, name);
  45. devices[devIdx][len-1] = '\0';
  46.  
  47. printf("\n-- %s --\n", name);
  48. printf("IO: %s\n", io);
  49.  
  50. desc_tmp = strtok(desc, "\n");
  51.  
  52. while (desc_tmp != NULL) {
  53. printf("%s\n", desc_tmp);
  54. desc_tmp = strtok(NULL, "\n");
  55. }
  56.  
  57. free(name);
  58. free(desc);
  59. devIdx++;
  60. str++;
  61. }
  62. index++;
  63. snd_device_name_free_hint(hints);
  64. }
  65. *count = devIdx;
  66. return;
  67. }

Report this snippet  

You need to login to post a comment.