From 300977537b6056bdbbba9df9100fa6e891ca1f44 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Andreas=20M=C3=BCller?= <schnitzeltony@gmail.com>
Date: Mon, 17 Dec 2018 14:08:45 +0100
Subject: [PATCH 2/2] fluid_synth_nwrite_float: Allow zero pointer for
 left/right and zero pointer in arrays
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

With this modification a client can define exactly what it wants to get into
buffers to avoid useless copying of data. On weak machines this leads to measurable
performance wins.

Upstream-Status: Submitted [1]

[1] https://github.com/FluidSynth/fluidsynth/pull/490

Signed-off-by: Andreas Müller <schnitzeltony@gmail.com>
---
 src/synth/fluid_synth.c | 69 ++++++++++++++++++++++++++++-------------
 1 file changed, 48 insertions(+), 21 deletions(-)

diff --git a/src/synth/fluid_synth.c b/src/synth/fluid_synth.c
index 1eb5d737..6c876efa 100644
--- a/src/synth/fluid_synth.c
+++ b/src/synth/fluid_synth.c
@@ -3628,10 +3628,10 @@ fluid_synth_program_reset(fluid_synth_t *synth)
  *
  * @param synth FluidSynth instance
  * @param len Count of audio frames to synthesize
- * @param left Array of float buffers to store left channel of planar audio (as many as \c synth.audio-channels buffers, each of \c len in size)
- * @param right Array of float buffers to store right channel of planar audio (size: dito)
- * @param fx_left Since 1.1.7: If not \c NULL, array of float buffers to store left effect channels (as many as \c synth.effects-channels buffers, each of \c len in size)
- * @param fx_right Since 1.1.7: If not \c NULL, array of float buffers to store right effect channels (size: dito)
+ * @param left Array of float buffers to store left channel of planar audio (as many as \c synth.audio-channels buffers, each of \c len in size). Since 2.0.3: NULL allowed / NULL allowed for array entry
+ * @param right Array of float buffers to store right channel of planar audio (size: dito). Since 2.0.3: NULL allowed / NULL allowed for array entry
+ * @param fx_left Since 1.1.7: If not \c NULL, array of float buffers to store left effect channels (as many as \c synth.effects-channels buffers, each of \c len in size). Since 2.0.3: NULL allowed for array entry
+ * @param fx_right Since 1.1.7: If not \c NULL, array of float buffers to store right effect channels (size: dito). Since 2.0.3: NULL allowed for array entry
  * @return #FLUID_OK on success, #FLUID_FAILED otherwise
  *
  * First effect channel used by reverb, second for chorus.
@@ -3719,15 +3719,27 @@ fluid_synth_nwrite_float(fluid_synth_t *synth, int len,
         for(i = 0; i < synth->audio_channels; i++)
         {
 #ifdef WITH_FLOAT
-            FLUID_MEMCPY(left[i], &left_in[i * FLUID_BUFSIZE * FLUID_MIXER_MAX_BUFFERS_DEFAULT + synth->cur], bytes);
-            FLUID_MEMCPY(right[i], &right_in[i * FLUID_BUFSIZE * FLUID_MIXER_MAX_BUFFERS_DEFAULT + synth->cur], bytes);
+            if(left != NULL && left[i] != NULL)
+            {
+                FLUID_MEMCPY(left[i], &left_in[i * FLUID_BUFSIZE * FLUID_MIXER_MAX_BUFFERS_DEFAULT + synth->cur], bytes);
+            }
+            if(right != NULL && right[i] != NULL)
+            {
+                FLUID_MEMCPY(right[i], &right_in[i * FLUID_BUFSIZE * FLUID_MIXER_MAX_BUFFERS_DEFAULT + synth->cur], bytes);
+            }
 #else //WITH_FLOAT
             int j;
 
             for(j = 0; j < num; j++)
             {
-                left[i][j] = (float) left_in[i * FLUID_BUFSIZE * FLUID_MIXER_MAX_BUFFERS_DEFAULT + j + synth->cur];
-                right[i][j] = (float) right_in[i * FLUID_BUFSIZE * FLUID_MIXER_MAX_BUFFERS_DEFAULT + j + synth->cur];
+                if(left != NULL && left[i] != NULL)
+                {
+                    left[i][j] = (float) left_in[i * FLUID_BUFSIZE * FLUID_MIXER_MAX_BUFFERS_DEFAULT + j + synth->cur];
+                }
+                if(right != NULL && right[i] != NULL)
+                {
+                    right[i][j] = (float) right_in[i * FLUID_BUFSIZE * FLUID_MIXER_MAX_BUFFERS_DEFAULT + j + synth->cur];
+                }
             }
 
 #endif //WITH_FLOAT
@@ -3737,12 +3749,12 @@ fluid_synth_nwrite_float(fluid_synth_t *synth, int len,
         {
 #ifdef WITH_FLOAT
 
-            if(fx_left != NULL)
+            if(fx_left != NULL && fx_left[i] != NULL)
             {
                 FLUID_MEMCPY(fx_left[i], &fx_left_in[i * FLUID_BUFSIZE * FLUID_MIXER_MAX_BUFFERS_DEFAULT + synth->cur], bytes);
             }
 
-            if(fx_right != NULL)
+            if(fx_right != NULL && fx_right[i] != NULL)
             {
                 FLUID_MEMCPY(fx_right[i], &fx_right_in[i * FLUID_BUFSIZE * FLUID_MIXER_MAX_BUFFERS_DEFAULT + synth->cur], bytes);
             }
@@ -3750,7 +3762,7 @@ fluid_synth_nwrite_float(fluid_synth_t *synth, int len,
 #else //WITH_FLOAT
             int j;
 
-            if(fx_left != NULL)
+            if(fx_left != NULL && fx_left[i] != NULL)
             {
                 for(j = 0; j < num; j++)
                 {
@@ -3758,7 +3770,7 @@ fluid_synth_nwrite_float(fluid_synth_t *synth, int len,
                 }
             }
 
-            if(fx_right != NULL)
+            if(fx_right != NULL && fx_right[i] != NULL)
             {
                 for(j = 0; j < num; j++)
                 {
@@ -3789,15 +3801,30 @@ fluid_synth_nwrite_float(fluid_synth_t *synth, int len,
         for(i = 0; i < synth->audio_channels; i++)
         {
 #ifdef WITH_FLOAT
-            FLUID_MEMCPY(left[i] + count, &left_in[i * FLUID_BUFSIZE * FLUID_MIXER_MAX_BUFFERS_DEFAULT], bytes);
-            FLUID_MEMCPY(right[i] + count, &right_in[i * FLUID_BUFSIZE * FLUID_MIXER_MAX_BUFFERS_DEFAULT], bytes);
+            if(left != NULL && left[i] != NULL)
+            {
+                FLUID_MEMCPY(left[i] + count, &left_in[i * FLUID_BUFSIZE * FLUID_MIXER_MAX_BUFFERS_DEFAULT], bytes);
+            }
+            if(right != NULL && right[i] != NULL)
+            {
+                FLUID_MEMCPY(right[i] + count, &right_in[i * FLUID_BUFSIZE * FLUID_MIXER_MAX_BUFFERS_DEFAULT], bytes);
+            }
 #else //WITH_FLOAT
             int j;
 
-            for(j = 0; j < num; j++)
+            if(left != NULL && left[i] != NULL)
+            {
+                for(j = 0; j < num; j++)
+                {
+                    left[i][j + count] = (float) left_in[i * FLUID_BUFSIZE * FLUID_MIXER_MAX_BUFFERS_DEFAULT + j];
+                }
+            }
+            if(right != NULL && right[i] != NULL)
             {
-                left[i][j + count] = (float) left_in[i * FLUID_BUFSIZE * FLUID_MIXER_MAX_BUFFERS_DEFAULT + j];
-                right[i][j + count] = (float) right_in[i * FLUID_BUFSIZE * FLUID_MIXER_MAX_BUFFERS_DEFAULT + j];
+                for(j = 0; j < num; j++)
+                {
+                    right[i][j + count] = (float) right_in[i * FLUID_BUFSIZE * FLUID_MIXER_MAX_BUFFERS_DEFAULT + j];
+                }
             }
 
 #endif //WITH_FLOAT
@@ -3807,12 +3834,12 @@ fluid_synth_nwrite_float(fluid_synth_t *synth, int len,
         {
 #ifdef WITH_FLOAT
 
-            if(fx_left != NULL)
+            if(fx_left != NULL && fx_left[i] != NULL)
             {
                 FLUID_MEMCPY(fx_left[i] + count, &fx_left_in[i * FLUID_BUFSIZE * FLUID_MIXER_MAX_BUFFERS_DEFAULT], bytes);
             }
 
-            if(fx_right != NULL)
+            if(fx_right != NULL && fx_right[i] != NULL)
             {
                 FLUID_MEMCPY(fx_right[i] + count, &fx_right_in[i * FLUID_BUFSIZE * FLUID_MIXER_MAX_BUFFERS_DEFAULT], bytes);
             }
@@ -3820,7 +3847,7 @@ fluid_synth_nwrite_float(fluid_synth_t *synth, int len,
 #else //WITH_FLOAT
             int j;
 
-            if(fx_left != NULL)
+            if(fx_left != NULL && fx_left[i] != NULL)
             {
                 for(j = 0; j < num; j++)
                 {
@@ -3828,7 +3855,7 @@ fluid_synth_nwrite_float(fluid_synth_t *synth, int len,
                 }
             }
 
-            if(fx_right != NULL)
+            if(fx_right != NULL && fx_right[i] != NULL)
             {
                 for(j = 0; j < num; j++)
                 {
-- 
2.14.5

