如何在不阅读 `/proc/asound/seq/clients` 的情况下列出 ALSA MIDI 客户端?

How to list ALSA MIDI clients without reading `/proc/asound/seq/clients`?

是否有一种已知的方法可以仅使用 ALSA API 列出现有的 MIDI 客户端,而无需读取特殊文件 /proc/asound/seq/clients

我搜索了 ALSA MIDI API reference,但找不到任何匹配项。我相信一定有一种方法可以使用 API 来实现这一点,否则就太令人惊讶了。

我终于明白了:snd_seq_get_any_client_info 获取有关第一个客户端(应该至少有一个,系统一个)和 snd_seq_query_next_client 的信息。获得下一个。

这里是列出 MIDI 客户端的片段:

static void list_clients(void) {

   int count = 0;
   int status;
   snd_seq_client_info_t* info;

   snd_seq_client_info_alloca(&info);

   status = snd_seq_get_any_client_info(seq_handle, 0, info);

   while (status >= 0) {
      count += 1;
      int id = snd_seq_client_info_get_client(info);
      char const* name = snd_seq_client_info_get_name(info);
      int num_ports = snd_seq_client_info_get_num_ports(info);
      printf("Client “%s” #%i, with %i ports\n", name, id, num_ports);
      status = snd_seq_query_next_client(seq_handle, info);
   }

   printf("Found %i clients\n", count);
}

该代码片段假定 seq_handle 已在别处声明和初始化(使用 snd_seq_open 初始化)。

snd_seq_get_any_client_info的调用中使用0作为客户端ID是一种猜测:ALSA使用负数表示错误,所以我猜第一个有效的客户端ID是0。

aplaymidi和类似工具的源代码所示,ALSA sequencer clients 被枚举为snd_seq_query_next_client():

snd_seq_client_info_alloca(&cinfo);
snd_seq_client_info_set_client(cinfo, -1);
while (snd_seq_query_next_client(seq, cinfo) >= 0) {
    int client = snd_seq_client_info_get_client(cinfo);
    ...
}