Advertisement
Guest User

List ALSA devices

a guest
Nov 23rd, 2011
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.62 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <alsa/asoundlib.h>
  3.  
  4. void print_card_info(snd_ctl_card_info_t *info)
  5. {
  6.         printf("Card #%d: id=%s, driver=%s, name=%s, longname=%s, mixername=%s, components=%s\n",
  7.                 snd_ctl_card_info_get_card(info),
  8.                 snd_ctl_card_info_get_id(info),
  9.                 snd_ctl_card_info_get_driver(info),
  10.                 snd_ctl_card_info_get_name(info),
  11.                 snd_ctl_card_info_get_longname(info),
  12.                 snd_ctl_card_info_get_mixername(info),
  13.                 snd_ctl_card_info_get_components(info));
  14. }
  15.  
  16. void print_elem_id(snd_ctl_elem_id_t *id)
  17. {
  18.         printf("elem_id: numid=%d, interface=%s, device=%d, subdevice=%d, name=%s, index=%d\n",
  19.                 snd_ctl_elem_id_get_numid(id),
  20.                 snd_ctl_elem_iface_name(snd_ctl_elem_id_get_interface(id)),
  21.                 snd_ctl_elem_id_get_device(id),
  22.                 snd_ctl_elem_id_get_subdevice(id),
  23.                 snd_ctl_elem_id_get_name(id),
  24.                 snd_ctl_elem_id_get_index(id));
  25. }
  26.  
  27. void print_elem_info(snd_ctl_elem_list_t *list, unsigned int i)
  28. {
  29.         snd_ctl_elem_id_t *id;
  30.  
  31.         printf("Element #%d: numid=%d, interface=%s, device=%d, subdevice=%d, name=%s, index=%d, elem_id follows:\n\t",
  32.                 i,
  33.                 snd_ctl_elem_list_get_numid(list, i),
  34.                 snd_ctl_elem_iface_name(snd_ctl_elem_list_get_interface(list, i)),
  35.                 snd_ctl_elem_list_get_device(list, i),
  36.                 snd_ctl_elem_list_get_subdevice(list, i),
  37.                 snd_ctl_elem_list_get_name(list, i),
  38.                 snd_ctl_elem_list_get_index(list, i));
  39.  
  40.         if (snd_ctl_elem_id_malloc(&id))
  41.         {
  42.                 printf("Error allocating snd_ctl_elem_id_t, aborting\n");
  43.                 return;
  44.         }
  45.         snd_ctl_elem_list_get_id(list, i, id);
  46.         print_elem_id(id);
  47.         snd_ctl_elem_id_free(id);
  48. }
  49.  
  50. void print_power_state(unsigned int state)
  51. {
  52.         char *str;
  53.  
  54.         switch(state & SND_CTL_POWER_MASK) {
  55.         case SND_CTL_POWER_D0:
  56.                 str = "D0";
  57.                 break;
  58.         case SND_CTL_POWER_D1:
  59.                 str = "D1";
  60.                 break;
  61.         case SND_CTL_POWER_D2:
  62.                 str = "D2";
  63.                 break;
  64.         case SND_CTL_POWER_D3hot:
  65.                 str = "D3 hot";
  66.                 break;
  67.         case SND_CTL_POWER_D3cold:
  68.                 str = "D3 cold";
  69.                 break;
  70.         default:
  71.                 str = "Unknown";
  72.         }
  73.  
  74.         printf("Power state = %s\n", str);
  75. }
  76.  
  77. void print_ctl_type(snd_ctl_type_t type)
  78. {
  79.         char *str;
  80.  
  81.         switch(type) {
  82.         case SND_CTL_TYPE_HW:
  83.                 str = "Kernel level CTL";
  84.                 break;
  85.         case SND_CTL_TYPE_SHM:
  86.                 str = "Shared memory client CTL";
  87.                 break;
  88.         case SND_CTL_TYPE_INET:
  89.                 str = "INET client CTL";
  90.                 break;
  91.         case SND_CTL_TYPE_EXT:
  92.                 str = "External control plugin";
  93.                 break;
  94.         default:
  95.                 str = "Unknown";
  96.         }
  97.  
  98.         printf("CTL type = %s\n", str);
  99. }
  100.  
  101. int main(int argc, char **argv)
  102. {
  103.         int card = -1;
  104.         char cardascii[6];
  105.         snd_ctl_t *handle;
  106.         snd_ctl_card_info_t *card_info;
  107.         snd_ctl_elem_list_t *elem_list;
  108.         unsigned int used, count, power_state;
  109.  
  110.         while (snd_card_next(&card) >= 0 && card >= 0) {
  111.                 snprintf(cardascii, sizeof(cardascii), "hw:%d", card);
  112.                 if (snd_ctl_open(&handle, cardascii, 0)) {
  113.                         printf("Error opening card #%d, skipping\n", card);
  114.                         continue;
  115.                 }
  116.                 if (snd_ctl_card_info_malloc(&card_info)) {
  117.                         printf("Error allocating snd_ctl_card_info_t structure, skipping\n");
  118.                         snd_ctl_close(handle);
  119.                         continue;
  120.                 }
  121.                 if (snd_ctl_card_info(handle, card_info)) {
  122.                         printf("Error getting info for card #%d, skipping\n", card);
  123.                         snd_ctl_close(handle);
  124.                         continue;
  125.                 }
  126.                 print_card_info(card_info);
  127.                 snd_ctl_card_info_free(card_info);
  128.                 if (snd_ctl_elem_list_malloc(&elem_list)) {
  129.                         printf("Error allocating snd_ctl_elem_list_t, skipping\n");
  130.                         snd_ctl_close(handle);
  131.                         continue;
  132.                 }
  133.                 if (snd_ctl_elem_list(handle, elem_list)) {
  134.                         printf("Error getting element list, skipping\n");
  135.                         snd_ctl_close(handle);
  136.                         snd_ctl_elem_list_free(elem_list);
  137.                         continue;
  138.                 }
  139.                 used = snd_ctl_elem_list_get_used(elem_list);
  140.                 count = snd_ctl_elem_list_get_count(elem_list);
  141.                 printf("Element used = %d, count = %d.  Allocating space for %d entries...\n", used, count, count);
  142.                 if (snd_ctl_elem_list_alloc_space(elem_list, count)) {
  143.                         printf("Error allocating element list space, skipping\n");
  144.                         snd_ctl_close(handle);
  145.                         snd_ctl_elem_list_free(elem_list);
  146.                         continue;
  147.                 }
  148.                 if (snd_ctl_elem_list(handle, elem_list)) {
  149.                         printf("Error getting element list, skipping\n");
  150.                         snd_ctl_close(handle);
  151.                         snd_ctl_elem_list_free(elem_list);
  152.                         snd_ctl_elem_list_free_space(elem_list);
  153.                         continue;
  154.                 }
  155.                 used = snd_ctl_elem_list_get_used(elem_list);
  156.                 count = snd_ctl_elem_list_get_count(elem_list);
  157.                 printf("Element used = %d, count = %d\n", used, count);
  158.                 for (unsigned int i = 0; i < used; i++) {
  159.                         print_elem_info(elem_list, i);
  160.                 }
  161.                 snd_ctl_elem_list_free_space(elem_list);
  162.                 snd_ctl_elem_list_free(elem_list);
  163.                 if (snd_ctl_get_power_state(handle, &power_state)) {
  164.                         printf("Error getting power state, skipping\n");
  165.                         snd_ctl_close(handle);
  166.                         continue;
  167.                 }
  168.                 print_power_state(power_state);
  169.                 print_ctl_type(snd_ctl_type(handle));
  170.                 snd_ctl_close(handle);
  171.         }
  172.  
  173.         printf("No more cards!\n");
  174.  
  175.         return 0;
  176. }
  177.  
  178.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement