c_cpp 这个脚本以编程方式在Linux中找到ALSA设备,这可能有点棘手。 #alsa#device-driver #linux #c

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp 这个脚本以编程方式在Linux中找到ALSA设备,这可能有点棘手。 #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;
}

以上是关于c_cpp 这个脚本以编程方式在Linux中找到ALSA设备,这可能有点棘手。 #alsa#device-driver #linux #c的主要内容,如果未能解决你的问题,请参考以下文章

如何以编程方式在android中使用blktrace命令执行shell脚本?

每天三分钟搞定linux shell脚本24 后台模式运行

SHELL脚本编程基础

在 Linux 命令行上以编程方式删除所有蓝牙设备

swift presentViewController 以编程方式在destinationVC VDL中找到nil

Linux系统——shell脚本