Hi all,

first of all, thanks for the effort of all contributors to the excellent 
fluidsynth+qsynth :-)!

While using QSynth+fluidsynth mostly as a hobbist, I came across this need for
reconnecting the ALSA MIDI input to QSynth/fluidsynth everytime I restart the 
engine.

So I worked out the attached patch that adds a bool setting for auto-connecting 
ALSA
MIDI inputs. This is the dual of the "Auto-connect JACK outputs" already there 
in
fluidsynth/QSynth.

If this makes sense, I'd follow up with a corresponding patch to QSynth to set 
this
property when instantiating the fluidsynth engine.

Feel free to try out the attached patch, and please, share your 
comments/thoughts!

Thanks,

        Tommaso
-- 
Tommaso Cucinotta, Computer Engineering PhD
Associate Professor at the Real-Time Systems Laboratory (ReTiS)
Scuola Superiore Sant'Anna, Pisa, Italy
http://retis.sssup.it/people/tommaso
commit c4b71d13d9f756d30c126f23bdf8c734892e2da8
Author: Tommaso Cucinotta <tomm...@lyx.org>
Date:   Mon May 2 12:20:34 2016 +0200

    Autoconnect ALSA MIDI inputs.

diff --git a/fluidsynth/src/drivers/fluid_alsa.c b/fluidsynth/src/drivers/fluid_alsa.c
index 0d3330e..63f73df 100644
--- a/fluidsynth/src/drivers/fluid_alsa.c
+++ b/fluidsynth/src/drivers/fluid_alsa.c
@@ -128,6 +128,7 @@ static void fluid_alsa_midi_run(void* d);
 typedef struct {
   fluid_midi_driver_t driver;
   snd_seq_t *seq_handle;
+  snd_seq_port_info_t *port_info;
   struct pollfd *pfd;
   int npfd;
   fluid_thread_t *thread;
@@ -147,9 +148,59 @@ static void fluid_alsa_seq_run(void* d);
  *
  */
 
+// FIXME: any attempt to free any of the _alloca()ed structs causes a segfault
+static void autoconnect_inputs(fluid_alsa_seq_driver_t* dev) {
+  snd_seq_t *seq = dev->seq_handle;
+  snd_seq_client_info_t *cinfo;
+  snd_seq_port_info_t *pinfo;
+
+  snd_seq_port_subscribe_t *subs;
+  snd_seq_port_subscribe_alloca(&subs);
+  assert(subs != NULL);
+
+  snd_seq_client_info_alloca(&cinfo);
+  assert(cinfo != NULL);
+  snd_seq_port_info_alloca(&pinfo);
+  assert(pinfo != NULL);
+  snd_seq_client_info_set_client(cinfo, -1);
+  while (snd_seq_query_next_client(seq, cinfo) >= 0) {
+    const snd_seq_addr_t *dest = snd_seq_port_info_get_addr(dev->port_info);
+
+    /* reset query info */
+    snd_seq_port_info_set_client(pinfo, snd_seq_client_info_get_client(cinfo));
+    snd_seq_port_info_set_port(pinfo, -1);
+    while (snd_seq_query_next_port(seq, pinfo) >= 0) {
+      unsigned int needed_type = SND_SEQ_PORT_TYPE_MIDI_GENERIC;
+      if ((snd_seq_port_info_get_type(pinfo) & needed_type) != needed_type)
+	continue;
+      unsigned int needed_cap = SND_SEQ_PORT_CAP_READ|SND_SEQ_PORT_CAP_SUBS_READ;
+      if ((snd_seq_port_info_get_capability(pinfo) & needed_cap) != needed_cap)
+	continue;
+      const snd_seq_addr_t *sender = snd_seq_port_info_get_addr(pinfo);
+
+      snd_seq_port_subscribe_set_sender(subs, sender);
+      snd_seq_port_subscribe_set_dest(subs, dest);
+
+      if (snd_seq_get_port_subscription(seq, subs) == 0) {
+	fprintf(stderr, _("Connection is already subscribed\n"));
+	continue;
+      }
+      if (snd_seq_subscribe_port(seq, subs) < 0) {
+	fprintf(stderr, _("Connection failed (%s)\n"), snd_strerror(errno));
+	continue;
+      }
+      fprintf(stderr, _("MIDI input succesfully connected (%s)\n"), snd_seq_port_info_get_name(pinfo));
+    }
+  }
+  //snd_seq_port_info_free(pinfo);
+  //snd_seq_client_info_free(cinfo);
+  //snd_seq_port_subscribe_free(subs);
+}
+
 void fluid_alsa_audio_driver_settings(fluid_settings_t* settings)
 {
   fluid_settings_register_str(settings, "audio.alsa.device", "default", 0, NULL, NULL);
+  fluid_settings_register_int(settings, "audio.alsa.autoconnect_inputs", 1, 0, 1, FLUID_HINT_TOGGLED, NULL, NULL);
 }
 
 
@@ -877,6 +928,13 @@ new_fluid_alsa_seq_driver(fluid_settings_t* settings,
     }
   }
 
+  dev->port_info = port_info;
+
+  int autoconn_inputs = 0;
+  fluid_settings_getint(settings, "audio.alsa.autoconnect_inputs", &autoconn_inputs);
+  if (autoconn_inputs)
+    autoconnect_inputs(dev);
+
   /* tell the lash server our client id */
 #ifdef LASH_ENABLED
   {
_______________________________________________
fluid-dev mailing list
fluid-dev@nongnu.org
https://lists.nongnu.org/mailman/listinfo/fluid-dev

Reply via email to