Hi RL, thanks for the comments!
You can find attached a refined patch, with the meaningless comments removed :-), and the new option disabled by default. I kept the alloca() for now, just as it clears the memory, it seems a common paradigm across alsa utils and tutorials (eg, aconnect.c, where I took inspiration for the patch), it doesn't seem a huge in-stack allocation, and keeps code a bit simpler than real allocations. Though, if this really hurts, I can change it to using FLUID_MALLOC() and FLUID_MEMSET, checking for OOM etc.... just let me know what's best for the current code base. Thanks, T. On 05/03/2016 01:42 AM, R.L. Horn wrote: > On Mon, 2 May 2016, Tommaso Cucinotta wrote: > >> 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. > >> Feel free to try out the attached patch, and please, share your >> comments/thoughts! > > I've only glanced over it, but the alloca()s and assert()s jumped right out. > The X != NULL assertion is pretty much meaningless after an alloca() and it > looks like NDEBUG is normally defined anyway. > > Besides, there's no good reason to use alloca() there (there are very few > good reasons for using alloca() generally), especially as you've already seen > to freeing the variables (and commenting it out because, of course, it > segfaults). Use the corresponding malloc() functions instead. And tedious > old conditionals to check the return values. > > Other than that, it looks fairly sound. > > _______________________________________________ > fluid-dev mailing list > fluid-dev@nongnu.org > https://lists.nongnu.org/mailman/listinfo/fluid-dev > -- 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 97e3a6169e5a1abc84156609b0991a916a79b672 Author: Tommaso Cucinotta <tommaso.cucino...@gmail.com> 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..527dace 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,53 @@ static void fluid_alsa_seq_run(void* d); * */ +// Connect ALSA MIDI inputs to dev->port_info +static void autoconnect_inputs(fluid_alsa_seq_driver_t* dev) { + snd_seq_t *seq = dev->seq_handle; + snd_seq_port_subscribe_t *subs; + snd_seq_client_info_t *cinfo; + snd_seq_port_info_t *pinfo; + + snd_seq_port_subscribe_alloca(&subs); + snd_seq_client_info_alloca(&cinfo); + snd_seq_port_info_alloca(&pinfo); + + 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); + + 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); + const char *pname = snd_seq_port_info_get_name(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 %s is already subscribed\n"), pname); + continue; + } + if (snd_seq_subscribe_port(seq, subs) < 0) { + fprintf(stderr, _("Connection of %s failed (%s)\n"), pname, snd_strerror(errno)); + continue; + } + fprintf(stderr, _("Connection of %s succeeded\n"), pname); + } + } +} + 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", 0, 0, 1, FLUID_HINT_TOGGLED, NULL, NULL); } @@ -877,6 +922,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