shithub: sox

Download patch

ref: e9a66b7c904a9406dfa37251a94bc0970a007724
parent: 0c2f7a09c0be53b25892fa97e58e6506ed56ea04
author: Ulrich Klauer <ulrich@chirlu.de>
date: Mon Mar 7 20:45:36 EST 2011

Allow unlimited number of effects for libsox

Remove the SOX_MAX_EFFECTS limitation on the number of effects that was
hard-wired into libsox. Memory for the effects chain is reallocated as
needed. - Resolves feature request #2214765.

Increment library version number, as binary compatibility is not guaranteed.

--- a/configure.ac
+++ b/configure.ac
@@ -22,7 +22,7 @@
 AC_PROG_LN_S
 
 dnl Increase version when binary compatibility with previous version is broken
-SHLIB_VERSION=1:1:0
+SHLIB_VERSION=2:0:0
 AC_SUBST(SHLIB_VERSION)
 
 AC_ARG_WITH(libltdl,
--- a/src/effects.c
+++ b/src/effects.c
@@ -102,6 +102,7 @@
 {
     if (ecp && ecp->length)
         sox_delete_effects(ecp);
+    free(ecp->effects);
     free(ecp);
 } /* sox_delete_effects_chain */
 
@@ -117,6 +118,9 @@
   return SOX_SUCCESS;
 }
 
+/* Effects table to be extended in steps of EFF_TABLE_STEP */
+#define EFF_TABLE_STEP 8
+
 /* Add an effect to the chain. *in is the input signal for this effect. *out is
  * a suggestion as to what the output signal should be, but depending on its
  * given options and *in, the effect can choose to do differently.  Whatever
@@ -168,11 +172,13 @@
 
   *in = effp->out_signal;
 
-  if (chain->length == SOX_MAX_EFFECTS) {
-    lsx_fail("Too many effects!");
-    free(eff0.priv);
-    return SOX_EOF;
+  if (chain->length == chain->table_size) {
+    chain->table_size += EFF_TABLE_STEP;
+    lsx_debug_more("sox_add_effect: extending effects table, "
+      "new size = %lu", (unsigned long)chain->table_size);
+    lsx_revalloc(chain->effects, chain->table_size);
   }
+
   chain->effects[chain->length] =
     lsx_calloc(effp->flows, sizeof(chain->effects[chain->length][0]));
   chain->effects[chain->length][0] = *effp;
@@ -413,7 +419,14 @@
 
 void sox_push_effect_last(sox_effects_chain_t *chain, sox_effect_t *effp)
 {
-  chain->effects[chain->length++] = effp;;
+  if (chain->length == chain->table_size) {
+    chain->table_size += EFF_TABLE_STEP;
+    lsx_debug_more("sox_push_effect_last: extending effects table, "
+        "new size = %lu", (unsigned long)chain->table_size);
+    lsx_revalloc(chain->effects, chain->table_size);
+  }
+
+  chain->effects[chain->length++] = effp;
 } /* sox_push_effect_last */
 
 sox_effect_t *sox_pop_effect_last(sox_effects_chain_t *chain)
--- a/src/sox.c
+++ b/src/sox.c
@@ -173,7 +173,7 @@
  * the input effect, an optional resample effect, an optional mixer
  * effect, and the output effect.
  */
-#define MAX_USER_EFF (SOX_MAX_EFFECTS - 4)
+#define MAX_USER_EFF 16
 static sox_effect_t *user_efftab[MAX_USER_EFF];
 static sox_effects_chain_t *effects_chain = NULL;
 static sox_effect_t *save_output_eff = NULL;
--- a/src/sox.h
+++ b/src/sox.h
@@ -1005,8 +1005,6 @@
  * Structures for effects.
  */
 
-#define SOX_MAX_EFFECTS 20
-
 #define SOX_EFF_CHAN     1           /* Effect might alter the number of channels */
 #define SOX_EFF_RATE     2           /* Effect might alter sample rate */
 #define SOX_EFF_PREC     4           /* Effect might alter sample precision */
@@ -1129,7 +1127,8 @@
 
 /* Chain of effects to be applied to a stream */
 typedef struct sox_effects_chain_t {
-  sox_effect_t * effects[SOX_MAX_EFFECTS]; /* Array of effects to be applied to a stream */
+  sox_effect_t **effects;                  /* Table of effects to be applied to a stream */
+  unsigned table_size;                     /* Number of entries in effects table */
   unsigned length;                         /* Number of effects to be applied */
   sox_sample_t **ibufc;                    /* Channel interleave buffer */
   sox_sample_t **obufc;                    /* Channel interleave buffer */