shithub: sox

Download patch

ref: f7c4b05cb1a74c9c64cc30312ffdf30bfcfb80ba
parent: f5156b42cb38830a77cee0c5fc23a643107b994a
author: rrt <rrt>
date: Sat Nov 11 17:58:04 EST 2006

Add libsamplerate (aka Secret Rabbit Code) support.

The addition ot aclocal.m4 is for pkg-config support which is required
by libsamplerate.

--- a/aclocal.m4
+++ b/aclocal.m4
@@ -47,7 +47,7 @@
 dnl      AC_COMPILE_CHECK_SIZEOF(off_t, $headers)
 dnl
 dnl @author Kaveh Ghazi <ghazi@caip.rutgers.edu>
-dnl @version $Id: aclocal.m4,v 1.4 2001/12/08 20:17:56 cbagwell Exp $
+dnl @version $Id: aclocal.m4,v 1.5 2006/11/11 22:58:04 rrt Exp $
 dnl
 AC_DEFUN([AC_COMPILE_CHECK_SIZEOF],
 [changequote(<<, >>)dnl
@@ -106,7 +106,7 @@
 dnl file will include it directly. No need for fuzzy HAVE_STDINT_H things...
 dnl
 dnl (this file is part of the http://ac-archive.sf.net/gstdint project)
-dnl @version $Id: aclocal.m4,v 1.4 2001/12/08 20:17:56 cbagwell Exp $
+dnl @version $Id: aclocal.m4,v 1.5 2006/11/11 22:58:04 rrt Exp $
 dnl @author  Guido Draheim <guidod@gmx.de>       STATUS: used on new platforms
 
 AC_DEFUN([AC_CREATE_STDINT_H],
@@ -534,3 +534,161 @@
 dnl discussed.  The probability that the "fast" types may be removed before
 dnl the standard is finalized is high enough that they are not currently
 dnl implemented.
+
+# pkg.m4 - Macros to locate and utilise pkg-config.            -*- Autoconf -*-
+# 
+# Copyright © 2004 Scott James Remnant <scott@netsplit.com>.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+#
+# As a special exception to the GNU General Public License, if you
+# distribute this file as part of a program that contains a
+# configuration script generated by Autoconf, you may include it under
+# the same distribution terms that you use for the rest of that program.
+
+# PKG_PROG_PKG_CONFIG([MIN-VERSION])
+# ----------------------------------
+AC_DEFUN([PKG_PROG_PKG_CONFIG],
+[m4_pattern_forbid([^_?PKG_[A-Z_]+$])
+m4_pattern_allow([^PKG_CONFIG(_PATH)?$])
+AC_ARG_VAR([PKG_CONFIG], [path to pkg-config utility])dnl
+if test "x$ac_cv_env_PKG_CONFIG_set" != "xset"; then
+	AC_PATH_TOOL([PKG_CONFIG], [pkg-config])
+fi
+if test -n "$PKG_CONFIG"; then
+	_pkg_min_version=m4_default([$1], [0.9.0])
+	AC_MSG_CHECKING([pkg-config is at least version $_pkg_min_version])
+	if $PKG_CONFIG --atleast-pkgconfig-version $_pkg_min_version; then
+		AC_MSG_RESULT([yes])
+	else
+		AC_MSG_RESULT([no])
+		PKG_CONFIG=""
+	fi
+		
+fi[]dnl
+])# PKG_PROG_PKG_CONFIG
+
+# PKG_CHECK_EXISTS(MODULES, [ACTION-IF-FOUND], [ACTION-IF-NOT-FOUND])
+#
+# Check to see whether a particular set of modules exists.  Similar
+# to PKG_CHECK_MODULES(), but does not set variables or print errors.
+#
+#
+# Similar to PKG_CHECK_MODULES, make sure that the first instance of
+# this or PKG_CHECK_MODULES is called, or make sure to call
+# PKG_CHECK_EXISTS manually
+# --------------------------------------------------------------
+AC_DEFUN([PKG_CHECK_EXISTS],
+[AC_REQUIRE([PKG_PROG_PKG_CONFIG])dnl
+if test -n "$PKG_CONFIG" && \
+    AC_RUN_LOG([$PKG_CONFIG --exists --print-errors "$1"]); then
+  m4_ifval([$2], [$2], [:])
+m4_ifvaln([$3], [else
+  $3])dnl
+fi])
+
+
+# _PKG_CONFIG([VARIABLE], [COMMAND], [MODULES])
+# ---------------------------------------------
+m4_define([_PKG_CONFIG],
+[if test -n "$PKG_CONFIG"; then
+    if test -n "$$1"; then
+        pkg_cv_[]$1="$$1"
+    else
+        PKG_CHECK_EXISTS([$3],
+                         [pkg_cv_[]$1=`$PKG_CONFIG --[]$2 "$3" 2>/dev/null`],
+			 [pkg_failed=yes])
+    fi
+else
+	pkg_failed=untried
+fi[]dnl
+])# _PKG_CONFIG
+
+# _PKG_SHORT_ERRORS_SUPPORTED
+# -----------------------------
+AC_DEFUN([_PKG_SHORT_ERRORS_SUPPORTED],
+[AC_REQUIRE([PKG_PROG_PKG_CONFIG])
+if $PKG_CONFIG --atleast-pkgconfig-version 0.20; then
+        _pkg_short_errors_supported=yes
+else
+        _pkg_short_errors_supported=no
+fi[]dnl
+])# _PKG_SHORT_ERRORS_SUPPORTED
+
+
+# PKG_CHECK_MODULES(VARIABLE-PREFIX, MODULES, [ACTION-IF-FOUND],
+# [ACTION-IF-NOT-FOUND])
+#
+#
+# Note that if there is a possibility the first call to
+# PKG_CHECK_MODULES might not happen, you should be sure to include an
+# explicit call to PKG_PROG_PKG_CONFIG in your configure.ac
+#
+#
+# --------------------------------------------------------------
+AC_DEFUN([PKG_CHECK_MODULES],
+[AC_REQUIRE([PKG_PROG_PKG_CONFIG])dnl
+AC_ARG_VAR([$1][_CFLAGS], [C compiler flags for $1, overriding pkg-config])dnl
+AC_ARG_VAR([$1][_LIBS], [linker flags for $1, overriding pkg-config])dnl
+
+pkg_failed=no
+AC_MSG_CHECKING([for $1])
+
+_PKG_CONFIG([$1][_CFLAGS], [cflags], [$2])
+_PKG_CONFIG([$1][_LIBS], [libs], [$2])
+
+m4_define([_PKG_TEXT], [Alternatively, you may set the environment variables $1[]_CFLAGS
+and $1[]_LIBS to avoid the need to call pkg-config.
+See the pkg-config man page for more details.])
+
+if test $pkg_failed = yes; then
+        _PKG_SHORT_ERRORS_SUPPORTED
+        if test $_pkg_short_errors_supported = yes; then
+	        $1[]_PKG_ERRORS=`$PKG_CONFIG --short-errors --errors-to-stdout --print-errors "$2"`
+        else 
+	        $1[]_PKG_ERRORS=`$PKG_CONFIG --errors-to-stdout --print-errors "$2"`
+        fi
+	# Put the nasty error message in config.log where it belongs
+	echo "$$1[]_PKG_ERRORS" >&AS_MESSAGE_LOG_FD
+
+	ifelse([$4], , [AC_MSG_ERROR(dnl
+[Package requirements ($2) were not met:
+
+$$1_PKG_ERRORS
+
+Consider adjusting the PKG_CONFIG_PATH environment variable if you
+installed software in a non-standard prefix.
+
+_PKG_TEXT
+])],
+		[AC_MSG_RESULT([no])
+                $4])
+elif test $pkg_failed = untried; then
+	ifelse([$4], , [AC_MSG_FAILURE(dnl
+[The pkg-config script could not be found or is too old.  Make sure it
+is in your PATH or set the PKG_CONFIG environment variable to the full
+path to pkg-config.
+
+_PKG_TEXT
+
+To get pkg-config, see <http://www.freedesktop.org/software/pkgconfig>.])],
+		[$4])
+else
+	$1[]_CFLAGS=$pkg_cv_[]$1[]_CFLAGS
+	$1[]_LIBS=$pkg_cv_[]$1[]_LIBS
+        AC_MSG_RESULT([yes])
+	ifelse([$3], , :, [$3])
+fi[]dnl
+])# PKG_CHECK_MODULES
--- a/configure.in
+++ b/configure.in
@@ -61,6 +61,11 @@
 	[enable_lame=$enableval],
 	[enable_lame=yes])
 
+AC_ARG_ENABLE(samplerate,
+        [  --disable-samplerate    Detection of libsamplerate (aka Secret Rabbit Code)],
+        [enable_samplerate=$enableval],
+        [enable_samplerate=yes])
+
 dnl Set host type
 AC_CANONICAL_SYSTEM
 
@@ -178,8 +183,23 @@
     fi
 fi
 
-dnl Checks for ASLA.
+dnl Test for libsamplerate.
 
+if test "$enable_samplerate" = yes
+then
+    PKG_CHECK_MODULES(SAMPLERATE, samplerate >= 0.0.15,
+            ac_cv_samplerate=1, ac_cv_samplerate=0)
+
+    AC_DEFINE_UNQUOTED([HAVE_SAMPLERATE],${ac_cv_samplerate},
+            [Set to 1 if you have libsamplerate.])
+
+    AC_SUBST(SAMPLERATE_CFLAGS)
+    AC_SUBST(SAMPLERATE_LIBS)
+    LIBS="$LIBS -lsamplerate"
+fi
+
+dnl Checks for ALSA.
+
 if test "$enable_alsa_dsp" = yes
 then
     AC_CHECK_HEADERS(alsa/asoundlib.h, found_alsa_dsp=yes)
@@ -287,6 +307,7 @@
 echo "Ogg Vorbis support................   $enable_ogg_vorbis"
 echo "MAD MP3 Decoder...................   $enable_mad"
 echo "LAME MP3 Encoder..................   $enable_lame"
+echo "Secret Rabbit Code resampling.....   $enable_samplerate"
 echo
 echo "Configure finished.  Do 'make; make install' to compile and install SoX."
 echo
--- a/src/Makefile.in
+++ b/src/Makefile.in
@@ -51,9 +51,9 @@
 	  copy.o dcshift.o deemphas.o earwax.o echo.o echos.o \
 	  fade.o FFT.o filter.o flanger.o highp.o highpass.o lowp.o \
 	  lowpass.o mask.o mcompand.o noiseprof.o noisered.o pan.o \
-	  phaser.o pitch.o polyphas.o rate.o repeat.o resample.o \
-	  reverb.o reverse.o silence.o speed.o stat.o stretch.o swap.o \
-	  synth.o trim.o vibro.o vol.o
+	  phaser.o pitch.o polyphas.o rabbit.o rate.o repeat.o resample.o \
+	  reverb.o reverse.o silence.o speed.o stat.o \
+	  stretch.o swap.o synth.o trim.o vibro.o vol.o
 
 
 GSMOBJ_0 =
--- a/src/handlers.c
+++ b/src/handlers.c
@@ -107,6 +107,9 @@
   st_pick_effect_fn,
   st_pitch_effect_fn,
   st_polyphase_effect_fn,
+#ifdef HAVE_SAMPLERATE
+  st_rabbit_effect_fn,
+#endif
   st_rate_effect_fn,
   st_repeat_effect_fn,
   st_resample_effect_fn,
--- /dev/null
+++ b/src/rabbit.c
@@ -1,0 +1,235 @@
+/*
+ * libsamplerate (aka Secret Rabbit Code) support for sox
+ * (c) Reuben Thomas <rrt@sc3d.org> 2006
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this library. If not, write to the Free Software
+ * Foundation, Fifth Floor, 51 Franklin Street, Boston, MA 02111-1301,
+ * USA.  */
+
+#include "st_i.h"
+
+#ifdef HAVE_SAMPLERATE
+
+#include <limits.h>
+#include <stdlib.h>
+#include <string.h>
+#include <math.h>
+#include <samplerate.h>
+
+static st_effect_t st_rabbit_effect;
+
+#ifdef min
+#undef min
+#endif
+#define min(a, b) ((a) <= (b) ? (a) : (b))
+
+#ifdef max
+#undef max
+#endif
+#define max(a, b) ((a) >= (b) ? (a) : (b))
+
+/* Private data for resampling */
+typedef struct {
+  int converter_type;           /* SRC converter type */
+  SRC_STATE *state;             /* SRC state struct */
+  SRC_DATA *data;               /* SRC_DATA control struct */
+  st_size_t samples;            /* Number of samples read so far */
+  st_size_t outsamp;            /* Next output sample */
+} *rabbit_t;
+
+/*
+ * Process options
+ */
+int st_rabbit_getopts(eff_t effp, int n, char **argv)
+{
+  rabbit_t r = (rabbit_t) effp->priv;
+
+  r->converter_type = SRC_SINC_BEST_QUALITY;
+
+  if (n >= 1) {
+    if (!strcmp(argv[0], "-c0")) {
+      r->converter_type = SRC_SINC_BEST_QUALITY;
+      n--; argv++;
+    } else if (!strcmp(argv[0], "-c1")) {
+      r->converter_type = SRC_SINC_MEDIUM_QUALITY;
+      n--; argv++;
+    } else if (!strcmp(argv[0], "-c2")) {
+      r->converter_type = SRC_SINC_FASTEST;
+      n--; argv++;
+    } else if (!strcmp(argv[0], "-c3")) {
+      r->converter_type = SRC_ZERO_ORDER_HOLD;
+      n--; argv++;
+    } else if (!strcmp(argv[0], "-c4")) {
+      r->converter_type = SRC_LINEAR;
+      n--; argv++;
+    }
+  }
+
+  if (n >= 1) {
+    st_fail(st_rabbit_effect.usage);
+    return (ST_EOF);
+  }
+
+  return (ST_SUCCESS);
+}
+
+/*
+ * Prepare processing.
+ */
+int st_rabbit_start(eff_t effp)
+{
+  rabbit_t r = (rabbit_t) effp->priv;
+
+  if (effp->ininfo.rate == effp->outinfo.rate) {
+    st_fail("Input and Output rates must be different to use rabbit effect");
+    return (ST_EOF);
+  }
+  if (effp->ininfo.channels != effp->outinfo.channels) {
+    st_fail("number of Input and Output channels must be equal to use rabbit effect");
+    return (ST_EOF);
+  }
+
+  if ((r->data = (SRC_DATA *)calloc(1, sizeof(SRC_DATA))) == NULL) {
+    st_fail("could not allocate SRC_DATA buffer");
+    return (ST_EOF);
+  }
+  r->data->src_ratio = (double)effp->outinfo.rate / effp->ininfo.rate;
+  r->data->input_frames_used = 0;
+  r->data->output_frames_gen = 0;
+
+  return (ST_SUCCESS);
+}
+
+/*
+ * Read all the data.
+ */
+int st_rabbit_flow(eff_t effp, st_sample_t *ibuf, st_sample_t *obuf,
+                   st_size_t *isamp, st_size_t *osamp)
+{
+  rabbit_t r = (rabbit_t) effp->priv;
+  int channels = effp->ininfo.channels;
+  st_size_t i, newsamples;
+
+  newsamples = r->samples + *isamp;
+  if (newsamples / channels > INT_MAX) {
+      st_fail("input data size %d too large for libsamplerate", newsamples);
+      return (ST_EOF);
+  }
+
+  if ((r->data->data_in = (float *)realloc(r->data->data_in, newsamples * sizeof(float))) == NULL) {
+    st_fail("unable to allocate input buffer of size %d", newsamples);
+    return (ST_EOF);
+  }
+
+  for (i = 0 ; i < *isamp; i++)
+    r->data->data_in[r->samples + i] = ST_SAMPLE_TO_FLOAT_DWORD(ibuf[i]);
+
+  r->samples = newsamples;
+  r->data->input_frames = r->samples / channels;
+  r->outsamp = 0;
+
+  *osamp = 0;           /* Signal that we didn't produce any output */
+
+  return (ST_SUCCESS);
+}
+
+/*
+ * Process samples and write output.
+ */
+int st_rabbit_drain(eff_t effp, st_sample_t *obuf, st_size_t *osamp)
+{
+  rabbit_t r = (rabbit_t) effp->priv;
+  int channels = effp->ininfo.channels;
+  st_size_t i, outsamps;
+
+  /* On first call, process the data */
+  if (r->data->data_out == NULL) {
+    /* Guess maximum number of possible output frames */
+    st_size_t outframes = r->data->input_frames * (r->data->src_ratio + 0.01) + 8;
+    int error, i;
+    double max_amp = 0.0;
+
+    if (outframes > INT_MAX) {
+      st_fail("too many output frames (%d) for libsamplerate", outframes);
+      return (ST_EOF);
+    }
+    r->data->output_frames = outframes;
+    r->data->data_out = (float *)malloc(r->data->output_frames * channels * sizeof(float));
+    if (r->data->data_out == NULL) {
+      st_fail("unable to allocate output frames buffer of size %d", r->data->output_frames);
+      return (ST_EOF);
+    }
+
+    /* Process the data */
+    if ((error = src_simple(r->data, r->converter_type, channels))) {
+      st_fail("libsamplerate processing failed: %s", src_strerror(error));
+      return (ST_EOF);
+    }
+
+    /* Find the scaling coefficient needed to avoid any values with
+       amplitude > 1.0 */
+    for (i = 0; i < r->data->output_frames_gen * channels; i++)
+      max_amp = max(max_amp, fabs(r->data->data_out[i]));
+
+    /* Rescale */
+    if (max_amp > 1.0)
+      for (i = 0; i < r->data->output_frames_gen * channels; i++)
+        r->data->data_out[i] /= max_amp;
+  }
+
+  /* Return the data one bufferful at a time */
+  if (*osamp > INT_MAX) {
+    st_fail("output buffer size %d too large for libsamplerate", *osamp);
+    return (ST_EOF);
+  }
+
+  outsamps = min(r->data->output_frames_gen * channels - r->outsamp, *osamp);
+  for (i = 0; i < outsamps; i++)
+    obuf[i] = ST_FLOAT_DWORD_TO_SAMPLE(r->data->data_out[r->outsamp + i]);
+  *osamp = (st_size_t)outsamps;
+  r->outsamp += outsamps;
+
+  return (ST_SUCCESS);
+}
+
+/*
+ * Do anything required when you stop reading samples.
+ * Don't close input file!
+ */
+int st_rabbit_stop(eff_t effp)
+{
+  rabbit_t r = (rabbit_t) effp->priv;
+
+  free(r->data);
+  src_delete(r->state);
+  return (ST_SUCCESS);
+}
+
+static st_effect_t st_rabbit_effect = {
+  "rabbit",
+  "Usage: rabbit [ -c0 | -c1 | -c2 | -c3 | -c4 ]",
+  ST_EFF_RATE | ST_EFF_MCHAN,
+  st_rabbit_getopts,
+  st_rabbit_start,
+  st_rabbit_flow,
+  st_rabbit_drain,
+  st_rabbit_stop
+};
+
+const st_effect_t *st_rabbit_effect_fn(void)
+{
+  return &st_rabbit_effect;
+}
+
+#endif /* HAVE_SAMPLERATE */
--- a/src/st_i.h
+++ b/src/st_i.h
@@ -256,6 +256,9 @@
 extern const st_effect_t *st_phaser_effect_fn(void);
 extern const st_effect_t *st_pitch_effect_fn(void);
 extern const st_effect_t *st_polyphase_effect_fn(void);
+#ifdef HAVE_SAMPLERATE
+extern const st_effect_t *st_rabbit_effect_fn(void);
+#endif
 extern const st_effect_t *st_rate_effect_fn(void);
 extern const st_effect_t *st_repeat_effect_fn(void);
 extern const st_effect_t *st_resample_effect_fn(void);
--- a/src/stconfig.h.in
+++ b/src/stconfig.h.in
@@ -33,6 +33,9 @@
 /* Define if you have LAME (LAME Ain't an MP3 Encoder) library installed */
 #undef HAVE_LAME
 
+/* Define if you have libsamplerate (Secret Rabbit Code) library installed */
+#undef HAVE_SAMPLERATE
+
 /* Define to 1 if you have the `m' library (-lm). */
 #undef HAVE_LIBM