From 38f49bce85152d90f7a8834c19f03d477acee3c5 Mon Sep 17 00:00:00 2001
From: Albert Graef <aggraef@gmail.com>
Date: Mon, 8 Aug 2022 11:49:46 +0200
Subject: [PATCH] fluid~: Silence chatty drivers during the fluidsynth
 initialization.

Note that setting the fluidsynth log callbacks doesn't help with these
messages, thus we have to resort to the rather drastic method of temporarily
redirecting stderr to /dev/null.
---
 externals/fluid~/fluid~.c | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/externals/fluid~/fluid~.c b/externals/fluid~/fluid~.c
index c10a126fc..5f6674314 100644
--- a/externals/fluid~/fluid~.c
+++ b/externals/fluid~/fluid~.c
@@ -6,6 +6,7 @@
 #include <fluidsynth.h>
 #include <string.h>
 #include <unistd.h>
+#include <fcntl.h>
 
 #include "m_pd.h"
  
@@ -304,6 +305,14 @@ static void fluid_init(t_fluid_tilde *x, t_symbol *s, int argc, t_atom *argv)
 
     float sr = sys_getsr();
 
+    // Some drivers (e.g. ALSA) are very chatty and will print a lot of
+    // log messages to the terminal while fluidsynth is being
+    // initialized. The only way to get rid of those seems to temporarily
+    // redirect stderr, so here goes.
+    int saved_stderr = dup(STDERR_FILENO);
+    int devnull = open("/dev/null", O_RDWR);
+    dup2(devnull, STDERR_FILENO);
+
     x->x_settings = new_fluid_settings();
 
     if (x->x_settings == NULL)
@@ -355,6 +364,8 @@ static void fluid_init(t_fluid_tilde *x, t_symbol *s, int argc, t_atom *argv)
         if (x->x_synth)
             post("-- fluid~ for Pd%s --", x->smmf_mode?" (SMMF mode)":"");
     }
+    // Restore stderr.
+    dup2(saved_stderr, STDERR_FILENO);
 }
 
 static void *fluid_tilde_new(t_symbol *s, int argc, t_atom *argv)
-- 
GitLab