Skip to content

Instantly share code, notes, and snippets.

@Makistos
Last active December 26, 2015 12:49
Show Gist options
  • Save Makistos/7154088 to your computer and use it in GitHub Desktop.
Save Makistos/7154088 to your computer and use it in GitHub Desktop.
This script finds out ALSA devices in Linux programatically, which can be a bit tricky. #alsa #device-driver #linux #c
/**
* 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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment