2

Is there a known way to list existing MIDI clients using the ALSA API only, without reading the special file /proc/asound/seq/clients?

I searched the ALSA MIDI API reference, and could not find any match. I believe there must be a way to achieve this using the API, otherwise that's a lot surprising.

Hibou57
  • 6,870
  • 6
  • 52
  • 56

2 Answers2

3

As shown in the source code of aplaymidi and similar tools, ALSA sequencer clients are enumerated with 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);
    ...
}
CL.
  • 173,858
  • 17
  • 217
  • 259
  • That's close to and shorter than the one I figured: I use `snd_seq_get_any_client_info` with a client ID of zero instead of directly using `snd_seq_client_info_get_client` with client info whose client ID is initialized to `-1`. Both works, but yours is shorter. – Hibou57 Jun 04 '15 at 23:08
1

I could figure it, finally: snd_seq_get_any_client_info to get informations about the first client (there should be at least one, the system one) and snd_seq_query_next_client. to get the next one.

Here is a snippet to list MIDI clients:

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);
}

The snippet assumes seq_handle is declared and initialized elsewhere (initialized with snd_seq_open).

The use of 0 as the client ID in the invocation of snd_seq_get_any_client_info, is a guess: ALSA uses negative numbers for errors, so I guess the first valid client ID is 0.

Hibou57
  • 6,870
  • 6
  • 52
  • 56