midilibalsa

How to understand the seemingly default/implicit ALSA MIDI default queue?


Background

In the snippet below, I send a MIDI note with a duration to a synthesizer port. For this to work, I need to allocate and start a queue, or else I get an “Invalid argument.” (error code -22) from ALSA.

The operations to send the event, proper, makes no reference to the queue, which seems to be referred to, implicitly. However, a MIDI application may have multiple queues, and I wonder about it too.

In my understanding, the queue is not like a buffer and is rather needed to manage timed events (the reason why a queue is needed to send a note with a duration), so I understand one is required to send some event with a particular presentation (with a start and/or end time).

Questions

How is the seemingly default queue determined, when sending an event makes no explicit reference to a queue?

In the context of the above question, what happens precisely when an application creates multiple queues? Is the implicit one, the first one?

Is there already a default queue even before I create one, which I could start instead of a (and sole) newly created one?

Annexe

The snippet mentioned above:

static void test_send(void) {

   snd_seq_event_t ev;
   int queue = snd_seq_alloc_queue(seq);
   check_error(queue, "snd_seq_alloc_queue");

   snd_seq_start_queue(seq, queue, NULL);

   snd_seq_ev_clear(&ev);
   snd_seq_ev_set_note(&ev, 0, 64, 127, 1);
   snd_seq_ev_set_source(&ev, out_port);
   snd_seq_ev_set_dest(&ev, synth_addr.client, synth_addr.port);

   int status = snd_seq_event_output_direct(seq, &ev);
   check_error(status, "snd_seq_event_output_direct");

   snd_seq_free_queue(seq, queue);
}

Solution

  • There is no default queue.

    An event always must specify the queue through which it will be sent (this can be done with snd_seq_ev_schedule_tick() or snd_seq_ev_schedule_real()), or it must set the queue field to SND_SEQ_QUEUE_DIRECT to specify that no queue is to be used (this can be done with snd_seq_ev_set_direct()).

    When you forget to set the queue field of the event, it stays at zero, which happens to be the number of your queue.